diff options
author | Matthew Miller <matthew@millerti.me> | 2023-10-27 13:20:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-27 13:20:28 -0700 |
commit | 6ef44cc1002fe498fd393c0244eed5a9afe185b5 (patch) | |
tree | e346abd6b2b1d1b26366cb34602c4964b4533aea /packages/server/src | |
parent | 93067b16ec03285c4e1efe0b00fa2a9f69722548 (diff) | |
parent | 763f0b6734c8f0362d015e5b3fae4784f8e7c7f7 (diff) |
Merge pull request #468 from MasterKale/fix/better-get-web-crypto
fix/better-get-web-crypto
Diffstat (limited to 'packages/server/src')
-rw-r--r-- | packages/server/src/helpers/iso/isoCrypto/getWebCrypto.test.ts | 149 | ||||
-rw-r--r-- | packages/server/src/helpers/iso/isoCrypto/getWebCrypto.ts | 51 |
2 files changed, 181 insertions, 19 deletions
diff --git a/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.test.ts b/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.test.ts new file mode 100644 index 0000000..26025b8 --- /dev/null +++ b/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.test.ts @@ -0,0 +1,149 @@ +import { assertEquals, assertRejects } from 'https://deno.land/std@0.198.0/assert/mod.ts'; +import { returnsNext, stub } from 'https://deno.land/std@0.198.0/testing/mock.ts'; + +import { _getWebCryptoInternals, getWebCrypto, MissingWebCrypto } from './getWebCrypto.ts'; + +Deno.test('should return globalThis.crypto when present', async () => { + // Clear whatever version of crypto might have been set + _getWebCryptoInternals.setCachedCrypto(undefined); + + // Pretend globalThis.crypto exists + const newGlobalThisCrypto = {}; + const mockGlobalThisCrypto = stub( + _getWebCryptoInternals, + 'stubThisGlobalThisCrypto', + // @ts-ignore: globalThis.crypto + returnsNext([newGlobalThisCrypto]), + ); + + const returnedCrypto = await getWebCrypto(); + + assertEquals(returnedCrypto, newGlobalThisCrypto); + + mockGlobalThisCrypto.restore(); +}); + +Deno.test('should return node:crypto.webcrypto when globalThis.crypto is missing', async () => { + // Clear whatever version of crypto might have been set + _getWebCryptoInternals.setCachedCrypto(undefined); + + // Pretend globalThis.crypto doesn't exist + const mockGlobalThisCrypto = stub( + _getWebCryptoInternals, + 'stubThisGlobalThisCrypto', + // @ts-ignore: globalThis.crypto + returnsNext([undefined]), + ); + + // Mock out just enough of the 'node:crypto' module + const fakeNodeCrypto = { webcrypto: {} }; + const mockImportNodeCrypto = stub( + _getWebCryptoInternals, + 'stubThisImportNodeCrypto', + // @ts-ignore: node:crypto + returnsNext([fakeNodeCrypto]), + ); + + const returnedCrypto = await getWebCrypto(); + + assertEquals(returnedCrypto, fakeNodeCrypto.webcrypto); + + mockGlobalThisCrypto.restore(); + mockImportNodeCrypto.restore(); +}); + +Deno.test( + 'should return globalThis.crypto when present, while node:crypto.webcrypto is present', + async () => { + // Clear whatever version of crypto might have been set + _getWebCryptoInternals.setCachedCrypto(undefined); + + // Pretend globalThis.crypto exists + const fakeGlobalThisCrypto = {}; + const mockGlobalThisCrypto = stub( + _getWebCryptoInternals, + 'stubThisGlobalThisCrypto', + // @ts-ignore: globalThis.crypto + returnsNext([fakeGlobalThisCrypto]), + ); + + // Mock out just enough of the 'node:crypto' module, but like we're in Node v14 + const fakeNodeCrypto = { webcrypto: {} }; + const mockImportNodeCrypto = stub( + _getWebCryptoInternals, + 'stubThisImportNodeCrypto', + // @ts-ignore: node:crypto + returnsNext([fakeNodeCrypto]), + ); + + const returnedCrypto = await getWebCrypto(); + + assertEquals(returnedCrypto, fakeGlobalThisCrypto); + + mockGlobalThisCrypto.restore(); + mockImportNodeCrypto.restore(); + }, +); + +Deno.test( + 'should return globalThis.crypto when present, while node:crypto is present but missing webcrypto', + async () => { + // Clear whatever version of crypto might have been set + _getWebCryptoInternals.setCachedCrypto(undefined); + + // Pretend globalThis.crypto exists + const fakeGlobalThisCrypto = {}; + const mockGlobalThisCrypto = stub( + _getWebCryptoInternals, + 'stubThisGlobalThisCrypto', + // @ts-ignore: globalThis.crypto + returnsNext([fakeGlobalThisCrypto]), + ); + + // Mock out just enough of the 'node:crypto' module, but like we're in Node v14 + const fakeNodeCrypto = { webcrypto: undefined }; + const mockImportNodeCrypto = stub( + _getWebCryptoInternals, + 'stubThisImportNodeCrypto', + // @ts-ignore: node:crypto + returnsNext([fakeNodeCrypto]), + ); + + const returnedCrypto = await getWebCrypto(); + + assertEquals(returnedCrypto, fakeGlobalThisCrypto); + + mockGlobalThisCrypto.restore(); + mockImportNodeCrypto.restore(); + }, +); + +Deno.test('should raise MissingWebCrypto error when nothing is available', async () => { + // Clear whatever version of crypto might have been set + _getWebCryptoInternals.setCachedCrypto(undefined); + + // Pretend globalThis.crypto doesn't exist + const mockGlobalThisCrypto = stub( + _getWebCryptoInternals, + 'stubThisGlobalThisCrypto', + // @ts-ignore: globalThis.crypto + returnsNext([undefined]), + ); + + // Pretend node:crypto doesn't exist + const mockImportNodeCrypto = stub( + _getWebCryptoInternals, + 'stubThisImportNodeCrypto', + // @ts-ignore: node:crypto + returnsNext([undefined]), + ); + + await assertRejects( + () => getWebCrypto(), + MissingWebCrypto, + 'Crypto API could not be located', + ); + + mockGlobalThisCrypto.restore(); + mockImportNodeCrypto.restore(); +}); diff --git a/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.ts b/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.ts index a5e580e..fe86219 100644 --- a/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.ts +++ b/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.ts @@ -11,37 +11,50 @@ export async function getWebCrypto(): Promise<Crypto> { return webCrypto; } + /** + * Naively attempt to access Crypto as a global object, which popular alternative run-times + * support. + */ + const _crypto = _getWebCryptoInternals.stubThisGlobalThisCrypto(); + + if (_crypto) { + webCrypto = _crypto; + return webCrypto; + } + try { /** - * Naively attempt a Node import... - */ - // @ts-ignore: We'll handle any errors... - // dnt-shim-ignore - const _crypto = await import('node:crypto'); - webCrypto = _crypto.webcrypto as Crypto; - } catch (_err) { - /** - * Naively attempt to access Crypto as a global object, which popular alternative run-times - * support. + * `globalThis.crypto` isn't available, so attempt a Node import... */ - // @ts-ignore: ...right here. - const _crypto: Crypto = globalThis.crypto; + const _crypto = await _getWebCryptoInternals.stubThisImportNodeCrypto(); - if (!_crypto) { - // We tried to access it both in Node and globally, so bail out - throw new MissingWebCrypto(); + if (_crypto.webcrypto) { + webCrypto = _crypto.webcrypto as Crypto; + return webCrypto; } - - webCrypto = _crypto; + } catch (_err) { + // pass } - return webCrypto; + // We tried to access it both in Node and globally, so bail out + throw new MissingWebCrypto(); } -class MissingWebCrypto extends Error { +export class MissingWebCrypto extends Error { constructor() { const message = 'An instance of the Crypto API could not be located'; super(message); this.name = 'MissingWebCrypto'; } } + +// Make it possible to stub return values during testing +export const _getWebCryptoInternals = { + // dnt-shim-ignore + stubThisImportNodeCrypto: () => import('node:crypto'), + stubThisGlobalThisCrypto: () => globalThis.crypto, + // Make it possible to reset the `webCrypto` at the top of the file + setCachedCrypto: (newCrypto: Crypto | undefined) => { + webCrypto = newCrypto; + }, +}; |