mhart / aws4fetch

A compact AWS client and signing utility for modern JS environments
MIT License
611 stars 40 forks source link

crypto is not defined #61

Open its-dibo opened 9 months ago

its-dibo commented 9 months ago

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

mhart commented 9 months ago

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

its-dibo commented 9 months ago

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

mhart commented 9 months ago

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: @.***>

its-dibo commented 9 months ago

Which Node.js version? Which OS?

Node v20 OS: ubuntu 22

mhart commented 9 months ago

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'
sino1641 commented 9 months ago

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.

its-dibo commented 8 months ago

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

zdmc23 commented 8 months ago

@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'
luizzappa commented 5 months ago

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;
  }
})
1rwilson commented 2 months ago

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 ?