From b7e4f6f297945c370009bf03305e0843eb390cea Mon Sep 17 00:00:00 2001 From: Steven Luscher Date: Wed, 12 Jul 2023 06:06:43 +0000 Subject: [PATCH] refactor(experimental): shim `Crypto` in both browser and Node environments ## Summary The idea here is that we're going to _presume_ that the browser has an Ed25519-compatible key generator, then polyfill it otherwise. From that perspective, the tests should just presume that it's present, and we should leave the testing of the polyfill up to the polyfill tests. --- packages/test-config/jest-unit.config.common.ts | 1 + packages/test-config/jest-unit.config.node.ts | 3 --- packages/test-config/setup-node-crypto.ts | 5 ----- packages/test-config/setup-webcrypto.ts | 13 +++++++++++++ 4 files changed, 14 insertions(+), 8 deletions(-) delete mode 100644 packages/test-config/setup-node-crypto.ts create mode 100644 packages/test-config/setup-webcrypto.ts diff --git a/packages/test-config/jest-unit.config.common.ts b/packages/test-config/jest-unit.config.common.ts index 65f17295306f..3fd2b3a4743f 100644 --- a/packages/test-config/jest-unit.config.common.ts +++ b/packages/test-config/jest-unit.config.common.ts @@ -8,6 +8,7 @@ const config: Partial = { path.resolve(__dirname, 'setup-dev-mode.ts'), path.resolve(__dirname, 'setup-define-version-constant.ts'), path.resolve(__dirname, 'setup-fetch-mock.ts'), + path.resolve(__dirname, 'setup-webcrypto.ts'), ], transform: { '^.+\\.(ts|js)$': [ diff --git a/packages/test-config/jest-unit.config.node.ts b/packages/test-config/jest-unit.config.node.ts index 3d859e73b9e8..d5cc92fa4ccf 100644 --- a/packages/test-config/jest-unit.config.node.ts +++ b/packages/test-config/jest-unit.config.node.ts @@ -1,5 +1,3 @@ -import path from 'node:path'; - import { Config } from '@jest/types'; import commonConfig from './jest-unit.config.common'; @@ -16,7 +14,6 @@ const config: Partial = { __NODEJS__: true, __REACTNATIVE__: false, }, - setupFilesAfterEnv: [...(commonConfig.setupFilesAfterEnv ?? []), path.resolve(__dirname, 'setup-node-crypto.ts')], }; export default config; diff --git a/packages/test-config/setup-node-crypto.ts b/packages/test-config/setup-node-crypto.ts deleted file mode 100644 index 98a6867069e2..000000000000 --- a/packages/test-config/setup-node-crypto.ts +++ /dev/null @@ -1,5 +0,0 @@ -import crypto from 'node:crypto'; - -if (typeof globalThis.crypto === 'undefined') { - globalThis.crypto = crypto as Crypto; -} diff --git a/packages/test-config/setup-webcrypto.ts b/packages/test-config/setup-webcrypto.ts new file mode 100644 index 000000000000..e0fd2b0d165b --- /dev/null +++ b/packages/test-config/setup-webcrypto.ts @@ -0,0 +1,13 @@ +import crypto from 'node:crypto'; + +if (typeof globalThis.crypto === 'undefined') { + Object.defineProperty(globalThis, 'crypto', { + value: crypto.webcrypto, + writable: true, // Allow tests to delete it. + }); +} +if (typeof globalThis.crypto.subtle === 'undefined') { + Object.defineProperty(globalThis.crypto, 'subtle', { + value: crypto.webcrypto.subtle, + }); +}