Noticed this in CI of opentelemetry-js-contrib only for node 18:
ReferenceError: crypto is not defined
at Object.v4 (/home/runner/work/opentelemetry-js-contrib/opentelemetry-js-contrib/node_modules/@smithy/core/dist-cjs/submodules/serde/index.js:445:5)
at /home/runner/work/opentelemetry-js-contrib/opentelemetry-js-contrib/node_modules/@smithy/core/dist-cjs/submodules/retry/index.js:165:63
at async /home/runner/work/opentelemetry-js-contrib/opentelemetry-js-contrib/node_modules/@aws-sdk/middleware-sdk-sqs/dist-cjs/index.js:88:18
at async /home/runner/work/opentelemetry-js-contrib/opentelemetry-js-contrib/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:5:26
at async Context.<anonymous> (test/aws-sdk-v3-sqs.test.ts:70:22)
Did some digging and came to following conclusion:
The v4 uuid generator in serde/uuid/v4.ts has always had a fallback using the global crypto without a guard, but on node it use to be dead code: randomUUID.ts used import * as crypto from "node:crypto" so randomUUID was always defined and v4 always took the fast path. #2009 replaced the node-specific import with a runtime typeof crypto check:
|
export const randomUUID = |
|
typeof crypto !== "undefined" && typeof crypto.randomUUID === "function" |
|
? (crypto.randomUUID.bind(crypto) as typeof crypto.randomUUID) |
|
: undefined; |
So that means the fallback will be hit whenever globalThis.crypto isn't on the global object, which is the case in node 18 (where it sits behind a feature flag --experimental-global-webcrypto). node 18 is currently the package's minimum engine version:
|
"engines": { |
|
"node": ">=18.0.0" |
|
}, |
But this breaks
every node 18 consumer of
any AWS SDK call that needs an idempotency token.
Noticed this in CI of opentelemetry-js-contrib only for node 18:
Did some digging and came to following conclusion:
The v4 uuid generator in
serde/uuid/v4.tshas always had a fallback using the globalcryptowithout a guard, but on node it use to be dead code:randomUUID.tsusedimport * as crypto from "node:crypto"so randomUUID was always defined and v4 always took the fast path. #2009 replaced the node-specific import with a runtimetypeof cryptocheck:smithy-typescript/packages/core/src/submodules/serde/uuid/randomUUID.ts
Lines 1 to 4 in bf13524
So that means the fallback will be hit whenever
globalThis.cryptoisn't on the global object, which is the case in node 18 (where it sits behind a feature flag --experimental-global-webcrypto). node 18 is currently the package's minimum engine version:smithy-typescript/packages/core/package.json
Lines 168 to 170 in bf13524
But this breaks every node 18 consumer of any AWS SDK call that needs an idempotency token.