aws4fetch icon indicating copy to clipboard operation
aws4fetch copied to clipboard

crypto is not defined

Open its-dibo opened this issue 1 year ago • 12 comments

I got this error

 ReferenceError: crypto is not defined
0|mobarmegeen-backend  |     at hmac (node_modules/aws4fetch/dist/aws4fetch.cjs.js:223:21)
0|mobarmegeen-backend  |     at AwsV4Signer.signature (node_modules/aws4fetch/dist/aws4fetch.cjs.js:185:27)
0|mobarmegeen-backend  |     at AwsV4Signer.authHeader (node_modules/aws4fetch/dist/aws4fetch.cjs.js:177:34)
0|mobarmegeen-backend  |     at AwsV4Signer.sign (node_modules/aws4fetch/dist/aws4fetch.cjs.js:164:52)
0|mobarmegeen-backend  |     at AwsClient.sign (node_modules/aws4fetch/dist/aws4fetch.cjs.js:56:57)
0|mobarmegeen-backend  |     at AwsClient.fetch (node_modules/aws4fetch/dist/aws4fetch.cjs.js:69:40)
0|mobarmegeen-backend  |     at FilesCloudflareService.save (/apps/src/api/files/fs-providers/files.cloudflare.service.ts:54:8)
0|mobarmegeen-backend  |     at /apps/src/api/posts/posts.service.ts:135:10
0|mobarmegeen-backend  |     at matcher (/apps/src/api/files/files.utils.ts:47:14)
0|mobarmegeen-backend  |     at replaceAsync (/apps/src/api/files/files.utils.ts:90:9)
0|mobarmegeen-backend  |     at extractInlineData (/apps/src/api/files/files.utils.ts:52:7)
0|mobarmegeen-backend  |     at PostsService.saveFiles (/apps/src/api/posts/posts.service.ts:121:29)
0|mobarmegeen-backend  |     at PostsService.updateById (/apps/src/api/posts/posts.service.ts:45:17)
0|mobarmegeen-backend  |     at PostsController.update (/apps/src/api/posts/posts.controller.ts:104:25)
0|mobarmegeen-backend  |     at node_modules/@nestjs/core/router/router-execution-context.js:38:29
0|mobarmegeen-backend  |     at processTicksAndRejections (node:internal/process/task_queues:95:5)
0|mobarmegeen-backend  |     at async node_modules/@nestjs/core/router/router-execution-context.js:46:28
0|mobarmegeen-backend  |     at async Object.<anonymous> (node_modules/@nestjs/core/router/router-proxy.js:9:17)

the code:

let client = new AwsClient({
      accessKeyId: ***,
      secretAccessKey: ***!,
    });

let baseUrl =  `https://${accountId}.r2.cloudflarestorage.com`

client
      .fetch(`${baseUrl}/file.txt`, {
        method: 'put',
        body: 'test',
      })

I don't use crypto anywhere in my code even installing crypto doesn't help npm i crypto

its-dibo avatar Jan 18 '24 22:01 its-dibo

Where are you running this? As the README says, this is "for environments that support fetch and SubtleCrypto"

mhart avatar Jan 18 '24 23:01 mhart

Where are you running this? As the README says, this is "for environments that support fetch and SubtleCrypto"

in a Nodejs app which supports fetch

its-dibo avatar Jan 29 '24 08:01 its-dibo

Which Node.js version? Which OS?

Sent from Gmail Mobile

On Mon, 29 Jan 2024 at 7:30 pm, Dibo @.***> wrote:

AwsClient

in a Nodejs app which supports fetch

— Reply to this email directly, view it on GitHub https://github.com/mhart/aws4fetch/issues/61#issuecomment-1914196492, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACZ2QDN2NCCD2BFITRRBOTYQ5MY5AVCNFSM6AAAAABCBBRG2GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJUGE4TMNBZGI . You are receiving this because you commented.Message ID: @.***>

mhart avatar Jan 29 '24 08:01 mhart

Which Node.js version? Which OS?

Node v20 OS: ubuntu 22

its-dibo avatar Jan 31 '24 18:01 its-dibo

Not sure what's wrong with your env – it's defined for me:

$ node
Welcome to Node.js v20.11.0.
Type ".help" for more information.
> typeof crypto
'object'

mhart avatar Jan 31 '24 23:01 mhart

HI I tried adding const crypto = require('crypto'); to the top of the code then it works. I am day1 on nodejs using fedora with node output:

$ node
Welcome to Node.js v18.18.2.
Type ".help" for more information.
> typeof crypto
'object'

May it wouldbe of help.

sino1641 avatar Feb 14 '24 15:02 sino1641

I tried adding const crypto = require('crypto');

this wo'nt work here because aws4fetch is an external package and we cannot modify its code base

its-dibo avatar Feb 24 '24 08:02 its-dibo

@its-dibo , try adding the Vercel Edge Runtime Polyfill (https://edge-runtime.vercel.app/packages/ponyfill) package:

pnpm add @edge-runtime/ponyfill

then to use it:

import { crypto } from '@edge-runtime/ponyfill'

zdmc23 avatar Feb 29 '24 07:02 zdmc23

In case anyone is having this problem running Nuxt locally, you can create a plugin as follows:

export default defineNuxtPlugin(() => {
  const config = useRuntimeConfig();

  if (process.server && config.public.env.toLocaleLowerCase() !== 'prod') {
    const crypto = require('crypto');
    global.crypto = crypto;
  }
})

luizzappa avatar May 26 '24 22:05 luizzappa

I tried adding const crypto = require('crypto');

I am experiencing the same issue on both my local node.js environment and also when deploying on google cloud functions: Screenshot 2024-09-05 at 20 08 44

Simply adding const crypto = require('crypto'); to the beginning of aws4fetch.cjs.js fixes the problem on my local environment. But I can't do this when deploying to GCF. Could this one line be added as per aws4.js ?

1rwilson avatar Sep 05 '24 19:09 1rwilson