sst / ion

SST v3
https://sst.dev
MIT License
1.89k stars 222 forks source link

crypto is not defined when calling bus.publish() #1086

Open 5o50 opened 1 week ago

5o50 commented 1 week ago

node: 18.18.2 sst: 3.1.10

Hello

In my lambda function crypto is well defined and I can use it without problem only when I call bus.publish() I get the following error.

err: {
    "type": "ReferenceError",
    "message": "crypto is not defined",
    "stack":
        ReferenceError: crypto is not defined
            at hmac (/home/john/workspace/app/node_modules/aws4fetch/dist/aws4fetch.esm.mjs:219:21)
            at AwsV4Signer.signature (/home/john/workspace/app/node_modules/aws4fetch/dist/aws4fetch.esm.mjs:181:27)
            at AwsV4Signer.authHeader (/home/john/workspace/app/node_modules/aws4fetch/dist/aws4fetch.esm.mjs:173:34)
            at AwsV4Signer.sign (/home/john/workspace/app/node_modules/aws4fetch/dist/aws4fetch.esm.mjs:160:52)
            at AwsClient.sign (/home/john/workspace/app/node_modules/aws4fetch/dist/aws4fetch.esm.mjs:52:57)
            at AwsClient.fetch (/home/john/workspace/app/node_modules/aws4fetch/dist/aws4fetch.esm.mjs:65:40)
            at Object.publish (/home/john/workspace/app/node_modules/sst/dist/aws/bus.js:35:29)
            at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
            at <anonymous> (/home/john/workspace/app/apps/auth/src/v2/verify.ts:162:5)
            at async file:///home/john/workspace/app/.sst/platform/dist/nodejs-runtime/index.js:96:16
  }

It seems related to aws4fetch specifically to https://github.com/mhart/aws4fetch/issues/61

After some tests I've found that adding global.crypto = require('crypto'); to the top of sst/aws/bus.js fix the issue

We could integrate this fix until aws4fetch figure it out that would make Bus usable

5o50 commented 2 days ago

I've switched to using this instead

import crypto from 'crypto';
global.crypto = crypto

with global.crypto = require('crypto'); the require() is messing with ES module scope

@jayair @thdxr can you reproduce the issue ?