Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/471-more-resilient-getwebcrypto #472

Merged
merged 3 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Deno.test('should return node:crypto.webcrypto when globalThis.crypto is missing
_getWebCryptoInternals,
'stubThisImportNodeCrypto',
// @ts-ignore: node:crypto
returnsNext([fakeNodeCrypto]),
returnsNext([Promise.resolve(fakeNodeCrypto)]),
);

const returnedCrypto = await getWebCrypto();
Expand Down Expand Up @@ -73,7 +73,7 @@ Deno.test(
_getWebCryptoInternals,
'stubThisImportNodeCrypto',
// @ts-ignore: node:crypto
returnsNext([fakeNodeCrypto]),
returnsNext([Promise.resolve(fakeNodeCrypto)]),
);

const returnedCrypto = await getWebCrypto();
Expand Down Expand Up @@ -106,7 +106,7 @@ Deno.test(
_getWebCryptoInternals,
'stubThisImportNodeCrypto',
// @ts-ignore: node:crypto
returnsNext([fakeNodeCrypto]),
returnsNext([Promise.resolve(fakeNodeCrypto)]),
);

const returnedCrypto = await getWebCrypto();
Expand Down Expand Up @@ -135,7 +135,7 @@ Deno.test('should raise MissingWebCrypto error when nothing is available', async
_getWebCryptoInternals,
'stubThisImportNodeCrypto',
// @ts-ignore: node:crypto
returnsNext([undefined]),
returnsNext([Promise.resolve({ webcrypto: undefined })]),
);

await assertRejects(
Expand Down
41 changes: 25 additions & 16 deletions packages/server/src/helpers/iso/isoCrypto/getWebCrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,21 @@ export async function getWebCrypto(): Promise<Crypto> {
* Naively attempt to access Crypto as a global object, which popular alternative run-times
* support.
*/
const _crypto = _getWebCryptoInternals.stubThisGlobalThisCrypto();
const _globalThisCrypto = _getWebCryptoInternals.stubThisGlobalThisCrypto();

if (_crypto) {
webCrypto = _crypto;
if (_globalThisCrypto) {
webCrypto = _globalThisCrypto;
return webCrypto;
}

try {
/**
* `globalThis.crypto` isn't available, so attempt a Node import...
*/
const _crypto = await _getWebCryptoInternals.stubThisImportNodeCrypto();
/**
* `globalThis.crypto` isn't available, so attempt a Node import...
*/
const _nodeCrypto = await _getWebCryptoInternals.stubThisImportNodeCrypto();

if (_crypto.webcrypto) {
webCrypto = _crypto.webcrypto as Crypto;
return webCrypto;
}
} catch (_err) {
// pass
if (_nodeCrypto?.webcrypto) {
webCrypto = _nodeCrypto.webcrypto as Crypto;
return webCrypto;
}

// We tried to access it both in Node and globally, so bail out
Expand All @@ -50,8 +46,21 @@ export class MissingWebCrypto extends Error {

// Make it possible to stub return values during testing
export const _getWebCryptoInternals = {
// dnt-shim-ignore
stubThisImportNodeCrypto: () => import('node:crypto'),
stubThisImportNodeCrypto: async () => {
try {
// dnt-shim-ignore
const _nodeCrypto = await import('node:crypto');
return _nodeCrypto;
} catch (_err) {
/**
* Intentionally declaring webcrypto as undefined because we're assuming the Node import
* failed due to either:
* - `import()` isn't supported
* - `node:crypto` is unavailable.
*/
return { webcrypto: undefined };
}
},
stubThisGlobalThisCrypto: () => globalThis.crypto,
// Make it possible to reset the `webCrypto` at the top of the file
setCachedCrypto: (newCrypto: Crypto | undefined) => {
Expand Down