Skip to content

Commit

Permalink
Revert "fix: Do not access global crypto (#1978)"
Browse files Browse the repository at this point in the history
This reverts commit 8777782.

Next.js was unhappy with the use of `await import(expr)`. Both top level
await as well as the expression in the dynamic import caused problems.
  • Loading branch information
arv committed Jun 7, 2024
1 parent c0e14da commit ecdf9a7
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 24 deletions.
2 changes: 0 additions & 2 deletions packages/reflect-client/src/util/nanoid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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:*', 'crypto');
external.push('node:diagnostics_channel');

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

let sessionID = '';
Expand Down
6 changes: 2 additions & 4 deletions packages/replicache/tool/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ const dirname = path.dirname(fileURLToPath(import.meta.url));
*/
async function buildReplicache(options) {
const define = await makeDefine(options.mode);
const {ext, mode, external = [], ...restOfOptions} = options;
// crypto is used as a fallback in older node versions
external.push('node:*', 'crypto');
const {ext, mode, external, ...restOfOptions} = options;
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: 2 additions & 9 deletions packages/shared/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

// 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(cryptoNodeModuleName)).webcrypto as Crypto);
: ((await import('crypto')).webcrypto as Crypto);

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

// rollup does not like `export const {subtle} = ...
// eslint-disable-next-line prefer-destructuring
export const subtle = localCrypto.subtle;
export const {subtle} = localCrypto;
3 changes: 2 additions & 1 deletion packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true
// For crypto when running under jest
"lib": ["dom"]
},
"include": ["src/**/*.ts", "src/**/*.js"]
}
2 changes: 0 additions & 2 deletions packages/zero-client/src/util/nanoid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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: 1 addition & 4 deletions packages/zero-client/tool/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@ 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,
external: await getExternalFromPackageJSON(import.meta.url),
platform: 'browser',
define: {
...define,
Expand Down

0 comments on commit ecdf9a7

Please sign in to comment.