Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.

Commit e4c7d8a

Browse files
authored
Use subtle crypto for more browser support (#376)
1 parent e922bda commit e4c7d8a

File tree

8 files changed

+38
-36
lines changed

8 files changed

+38
-36
lines changed

src/common/integrity.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import { createHash as _createHash } from "crypto";
1+
let _nodeCrypto;
22

3-
let createHash = _createHash;
4-
5-
export function setCreateHash(_createHash) {
6-
createHash = _createHash;
7-
}
8-
9-
export function getIntegrity(buf: Uint8Array | string): `sha384-${string}` {
10-
const hash = createHash("sha384");
3+
export async function getIntegrityNodeLegacy(buf: Uint8Array | string): Promise<`sha384-${string}`> {
4+
const hash = (_nodeCrypto || (_nodeCrypto = (await (0, eval)('import("node:crypto")')))).createHash("sha384");
115
hash.update(buf);
126
return `sha384-${hash.digest("base64")}`;
137
}
8+
9+
export let getIntegrity = async function getIntegrity(buf: Uint8Array | string): Promise<`sha384-${string}`> {
10+
const data = typeof buf === "string" ? new TextEncoder().encode(buf) : buf;
11+
const hashBuffer = await crypto.subtle.digest("SHA-384", data);
12+
const hashArray = Array.from(new Uint8Array(hashBuffer));
13+
const hashBase64 = btoa(String.fromCharCode(...hashArray));
14+
return `sha384-${hashBase64}`;
15+
}
16+
17+
if (typeof crypto === 'undefined')
18+
getIntegrity = getIntegrityNodeLegacy;

src/generator-deno.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import { pathToFileURL } from "url";
88
import { setBabel as setBabelCjs } from "./trace/cjs.js";
99
import { setBabel as setBabelTs } from "./trace/ts.js";
1010
import { setPathFns } from "./trace/resolver.js";
11-
import { setCreateHash } from "./common/integrity.js";
1211

1312
setBabelCjs(babel);
1413
setBabelTs(babel, babelPresetTs, babelPluginSyntaxImportAttributes);
15-
setCreateHash(createHash);
1614
setPathFns(realpath, pathToFileURL);
1715

1816
export * from "./generator.js";

src/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ export class Generator {
755755

756756
esms = `<script async src="${esmsUrl}" crossorigin="anonymous"${
757757
integrity
758-
? ` integrity="${getIntegrity(
758+
? ` integrity="${await getIntegrity(
759759
new Uint8Array(
760760
await (
761761
await fetch(esmsUrl, this.traceMap.resolver.fetchOpts)

src/trace/analysis.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export interface Analysis {
2222
export { createTsAnalysis } from "./ts.js";
2323
export { createCjsAnalysis } from "./cjs.js";
2424

25-
export function createEsmAnalysis(
25+
export async function createEsmAnalysis(
2626
imports: any[],
2727
source: string,
2828
url: string
29-
): Analysis {
29+
): Promise<Analysis> { // Change the return type to Promise<Analysis>
3030
if (!imports.length && registerRegEx.test(source))
3131
return createSystemAnalysis(source, imports, url);
3232
const deps: string[] = [];
@@ -59,17 +59,17 @@ export function createEsmAnalysis(
5959
cjsLazyDeps: null,
6060
size,
6161
format: "esm",
62-
integrity: getIntegrity(source),
62+
integrity: await getIntegrity(source),
6363
};
6464
}
6565

6666
const registerRegEx =
6767
/^\s*(\/\*[^\*]*(\*(?!\/)[^\*]*)*\*\/|\s*\/\/[^\n]*)*\s*System\s*\.\s*register\s*\(\s*(\[[^\]]*\])\s*,\s*\(?function\s*\(\s*([^\),\s]+\s*(,\s*([^\),\s]+)\s*)?\s*)?\)/;
68-
export function createSystemAnalysis(
68+
export async function createSystemAnalysis(
6969
source: string,
7070
imports: string[],
7171
url: string
72-
): Analysis {
72+
): Promise<Analysis> {
7373
const [, , , rawDeps, , , contextId] = source.match(registerRegEx) || [];
7474
if (!rawDeps) return createEsmAnalysis(imports, source, url);
7575
const deps = JSON.parse(rawDeps.replace(/'/g, '"'));
@@ -101,6 +101,6 @@ export function createSystemAnalysis(
101101
cjsLazyDeps: null,
102102
size,
103103
format: "system",
104-
integrity: getIntegrity(source),
104+
integrity: await getIntegrity(source),
105105
};
106106
}

src/trace/cjs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export async function createCjsAnalysis(
110110
size: source.length,
111111
format: "commonjs",
112112
usesCjs,
113-
integrity: getIntegrity(source),
113+
integrity: await getIntegrity(source),
114114
};
115115
}
116116

src/trace/resolver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ export class Resolver {
900900
cjsLazyDeps: null,
901901
size: source.byteLength,
902902
format: "wasm",
903-
integrity: getIntegrity(new Uint8Array(source)),
903+
integrity: await getIntegrity(new Uint8Array(source)),
904904
};
905905
}
906906

@@ -923,7 +923,7 @@ export class Resolver {
923923
cjsLazyDeps: null,
924924
size: source.length,
925925
format: "json",
926-
integrity: getIntegrity(source),
926+
integrity: await getIntegrity(source),
927927
};
928928
} catch {}
929929
}
@@ -936,7 +936,7 @@ export class Resolver {
936936
cjsLazyDeps: null,
937937
size: source.length,
938938
format: "css",
939-
integrity: getIntegrity(source),
939+
integrity: await getIntegrity(source),
940940
};
941941
} catch {}
942942
}

src/trace/ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export async function createTsAnalysis(
117117
cjsLazyDeps: null,
118118
size: source.length,
119119
format: "typescript",
120-
integrity: getIntegrity(source),
120+
integrity: await getIntegrity(source),
121121
};
122122
}
123123

test/test.html

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
"../": {
1212
"#fetch": "../dist/fetch-native.js",
1313
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.25.2/lib/index.js",
14-
"@babel/plugin-syntax-import-attributes": "https://ga.jspm.io/npm:@babel/plugin-syntax-import-attributes@7.24.7/lib/index.js",
14+
"@babel/plugin-syntax-import-attributes": "https://ga.jspm.io/npm:@babel/plugin-syntax-import-attributes@7.25.6/lib/index.js",
1515
"@babel/preset-typescript": "https://ga.jspm.io/npm:@babel/preset-typescript@7.24.7/lib/index.js",
1616
"@jspm/import-map": "https://ga.jspm.io/npm:@jspm/import-map@1.1.0/dist/map.js",
17-
"crypto": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/crypto.js",
1817
"es-module-lexer": "https://ga.jspm.io/npm:es-module-lexer@1.5.4/dist/lexer.js",
1918
"es-module-lexer/js": "https://ga.jspm.io/npm:es-module-lexer@1.5.4/dist/lexer.asm.js",
2019
"fs": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/fs.js",
@@ -30,13 +29,13 @@
3029
"#node.js": "https://ga.jspm.io/npm:browserslist@4.23.3/browser.js",
3130
"@ampproject/remapping": "https://ga.jspm.io/npm:@ampproject/remapping@2.3.0/dist/remapping.umd.js",
3231
"@babel/code-frame": "https://ga.jspm.io/npm:@babel/code-frame@7.24.7/lib/index.js",
33-
"@babel/compat-data/native-modules": "https://ga.jspm.io/npm:@babel/compat-data@7.25.2/native-modules.js",
34-
"@babel/compat-data/plugins": "https://ga.jspm.io/npm:@babel/compat-data@7.25.2/plugins.js",
32+
"@babel/compat-data/native-modules": "https://ga.jspm.io/npm:@babel/compat-data@7.25.4/native-modules.js",
33+
"@babel/compat-data/plugins": "https://ga.jspm.io/npm:@babel/compat-data@7.25.4/plugins.js",
3534
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.25.2/lib/index.js",
36-
"@babel/generator": "https://ga.jspm.io/npm:@babel/generator@7.25.0/lib/index.js",
35+
"@babel/generator": "https://ga.jspm.io/npm:@babel/generator@7.25.6/lib/index.js",
3736
"@babel/helper-annotate-as-pure": "https://ga.jspm.io/npm:@babel/helper-annotate-as-pure@7.24.7/lib/index.js",
3837
"@babel/helper-compilation-targets": "https://ga.jspm.io/npm:@babel/helper-compilation-targets@7.25.2/lib/index.js",
39-
"@babel/helper-create-class-features-plugin": "https://ga.jspm.io/npm:@babel/helper-create-class-features-plugin@7.25.0/lib/index.js",
38+
"@babel/helper-create-class-features-plugin": "https://ga.jspm.io/npm:@babel/helper-create-class-features-plugin@7.25.4/lib/index.js",
4039
"@babel/helper-member-expression-to-functions": "https://ga.jspm.io/npm:@babel/helper-member-expression-to-functions@7.24.8/lib/index.js",
4140
"@babel/helper-module-imports": "https://ga.jspm.io/npm:@babel/helper-module-imports@7.24.7/lib/index.js",
4241
"@babel/helper-module-transforms": "https://ga.jspm.io/npm:@babel/helper-module-transforms@7.25.2/lib/index.js",
@@ -48,16 +47,16 @@
4847
"@babel/helper-string-parser": "https://ga.jspm.io/npm:@babel/helper-string-parser@7.24.8/lib/index.js",
4948
"@babel/helper-validator-identifier": "https://ga.jspm.io/npm:@babel/helper-validator-identifier@7.24.7/lib/index.js",
5049
"@babel/helper-validator-option": "https://ga.jspm.io/npm:@babel/helper-validator-option@7.24.8/lib/index.js",
51-
"@babel/helpers": "https://ga.jspm.io/npm:@babel/helpers@7.25.0/lib/index.js",
50+
"@babel/helpers": "https://ga.jspm.io/npm:@babel/helpers@7.25.6/lib/index.js",
5251
"@babel/highlight": "https://ga.jspm.io/npm:@babel/highlight@7.24.7/lib/index.js",
53-
"@babel/parser": "https://ga.jspm.io/npm:@babel/parser@7.25.3/lib/index.js",
52+
"@babel/parser": "https://ga.jspm.io/npm:@babel/parser@7.25.6/lib/index.js",
5453
"@babel/plugin-syntax-jsx": "https://ga.jspm.io/npm:@babel/plugin-syntax-jsx@7.24.7/lib/index.js",
55-
"@babel/plugin-syntax-typescript": "https://ga.jspm.io/npm:@babel/plugin-syntax-typescript@7.24.7/lib/index.js",
54+
"@babel/plugin-syntax-typescript": "https://ga.jspm.io/npm:@babel/plugin-syntax-typescript@7.25.4/lib/index.js",
5655
"@babel/plugin-transform-modules-commonjs": "https://ga.jspm.io/npm:@babel/plugin-transform-modules-commonjs@7.24.8/lib/index.js",
5756
"@babel/plugin-transform-typescript": "https://ga.jspm.io/npm:@babel/plugin-transform-typescript@7.25.2/lib/index.js",
5857
"@babel/template": "https://ga.jspm.io/npm:@babel/template@7.25.0/lib/index.js",
59-
"@babel/traverse": "https://ga.jspm.io/npm:@babel/traverse@7.25.3/lib/index.js",
60-
"@babel/types": "https://ga.jspm.io/npm:@babel/types@7.25.2/lib/index.js",
58+
"@babel/traverse": "https://ga.jspm.io/npm:@babel/traverse@7.25.6/lib/index.js",
59+
"@babel/types": "https://ga.jspm.io/npm:@babel/types@7.25.6/lib/index.js",
6160
"@jridgewell/gen-mapping": "https://ga.jspm.io/npm:@jridgewell/gen-mapping@0.3.5/dist/gen-mapping.umd.js",
6261
"@jridgewell/resolve-uri": "https://ga.jspm.io/npm:@jridgewell/resolve-uri@3.1.2/dist/resolve-uri.umd.js",
6362
"@jridgewell/set-array": "https://ga.jspm.io/npm:@jridgewell/set-array@1.2.1/dist/set-array.umd.js",
@@ -66,13 +65,13 @@
6665
"ansi-styles": "https://ga.jspm.io/npm:ansi-styles@3.2.1/index.js",
6766
"browserslist": "https://ga.jspm.io/npm:browserslist@4.23.3/index.js",
6867
"buffer": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/buffer.js",
69-
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:caniuse-lite@1.0.30001649/dist/unpacker/agents.js",
68+
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:caniuse-lite@1.0.30001655/dist/unpacker/agents.js",
7069
"chalk": "https://ga.jspm.io/npm:chalk@2.4.2/index.js",
7170
"color-convert": "https://ga.jspm.io/npm:color-convert@1.9.3/index.js",
7271
"color-name": "https://ga.jspm.io/npm:color-name@1.1.3/index.js",
7372
"convert-source-map": "https://ga.jspm.io/npm:convert-source-map@2.0.0/index.js",
7473
"debug": "https://ga.jspm.io/npm:debug@4.3.6/src/browser.js",
75-
"electron-to-chromium/versions": "https://ga.jspm.io/npm:electron-to-chromium@1.5.4/versions.js",
74+
"electron-to-chromium/versions": "https://ga.jspm.io/npm:electron-to-chromium@1.5.13/versions.js",
7675
"escape-string-regexp": "https://ga.jspm.io/npm:escape-string-regexp@1.0.5/index.js",
7776
"fs": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/fs.js",
7877
"gensync": "https://ga.jspm.io/npm:gensync@1.0.0-beta.2/index.js",

0 commit comments

Comments
 (0)