Skip to content

Commit

Permalink
fix: Do not access global crypto (#1978)
Browse files Browse the repository at this point in the history
Instead go through shared/src/crypto.ts which will use the global crypto
if available, and fallback node's crypto module if not.
  • Loading branch information
arv authored Jun 5, 2024
1 parent fd7e43b commit 8777782
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/reflect-client/src/util/nanoid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as crypto from 'shared/src/crypto.js';

// This is taken from https://github.com/ai/nanoid/blob/main/index.browser.js We
// copy this because we want to use `--platform=neutral` which doesn't work with
// the npm package
Expand Down
2 changes: 1 addition & 1 deletion packages/reflect/tool/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async function buildPackages() {
'reflect-shared',
'reflect-react',
);
external.push('node:diagnostics_channel');
external.push('node:*', 'crypto');

await esbuild.build({
...shared,
Expand Down
1 change: 1 addition & 0 deletions packages/replicache/src/sync/request-id.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as crypto from 'shared/src/crypto.js';
import type {ClientID} from './ids.js';

let sessionID = '';
Expand Down
6 changes: 4 additions & 2 deletions packages/replicache/tool/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ const dirname = path.dirname(fileURLToPath(import.meta.url));
*/
async function buildReplicache(options) {
const define = await makeDefine(options.mode);
const {ext, mode, external, ...restOfOptions} = options;
const {ext, mode, external = [], ...restOfOptions} = options;
// crypto is used as a fallback in older node versions
external.push('node:*', 'crypto');
const outfile = path.join(dirname, '..', 'out', 'replicache.' + ext);
const result = await esbuild.build({
...sharedOptions(options.minify, metafile),
...(external ? {external} : {}),
external,
...restOfOptions,
format: 'esm',
// Use neutral to remove the automatic define for process.env.NODE_ENV
Expand Down
11 changes: 9 additions & 2 deletions packages/shared/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

// Firebase and Jest do not correctly setup the global crypto object.

// Don't inline this. It is done this way to prevent TS from type checking the
// module as well as to prevent esbuild (actually web-dev-server which does not
// support external) from trying to resolve the module.
const cryptoNodeModuleName = 'crypto';

const localCrypto =
typeof crypto !== 'undefined'
? crypto
: ((await import('crypto')).webcrypto as Crypto);
: ((await import(cryptoNodeModuleName)).webcrypto as Crypto);

export function getRandomValues<T extends ArrayBufferView | null>(array: T): T {
return localCrypto.getRandomValues(array);
}

export const {subtle} = localCrypto;
// rollup does not like `export const {subtle} = ...
// eslint-disable-next-line prefer-destructuring
export const subtle = localCrypto.subtle;
3 changes: 1 addition & 2 deletions packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
// For crypto when running under jest
"lib": ["dom"]
"noEmit": true
},
"include": ["src/**/*.ts", "src/**/*.js"]
}
2 changes: 2 additions & 0 deletions packages/zero-client/src/util/nanoid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as crypto from 'shared/src/crypto.js';

// This is taken from https://github.com/ai/nanoid/blob/main/index.browser.js We
// copy this because we want to use `--platform=neutral` which doesn't work with
// the npm package
Expand Down
5 changes: 4 additions & 1 deletion packages/zero-client/tool/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ async function buildPackages() {
const define = makeDefine();

fs.rmSync(basePath('out'), {recursive: true, force: true});
const external = await getExternalFromPackageJSON(import.meta.url);
// crypto is used as a fallback in older node versions
external.push('node:*', 'crypto');

await esbuild.build({
...shared,
external: await getExternalFromPackageJSON(import.meta.url),
external,
platform: 'browser',
define: {
...define,
Expand Down

0 comments on commit 8777782

Please sign in to comment.