Skip to content

Commit 28ea0f0

Browse files
author
Eric Amodio
committed
Fixes node runtime error
1 parent f61fed5 commit 28ea0f0

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

  • extensions/github-browser/src

extensions/github-browser/src/sha1.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@
88
const textDecoder = new TextDecoder();
99
const textEncoder = new TextEncoder();
1010

11-
declare const WEBWORKER: boolean;
11+
declare let WEBWORKER: boolean;
1212

1313
export async function sha1(s: string | Uint8Array): Promise<string> {
14-
if (WEBWORKER) {
15-
const hash = await globalThis.crypto.subtle.digest({ name: 'sha-1' }, typeof s === 'string' ? textEncoder.encode(s) : s);
16-
// Use encodeURIComponent to avoid issues with btoa and Latin-1 characters
17-
return globalThis.btoa(encodeURIComponent(textDecoder.decode(hash)));
18-
}
19-
else {
20-
return (await import('crypto')).createHash('sha1').update(s).digest('base64');
14+
while (true) {
15+
try {
16+
if (WEBWORKER) {
17+
const hash = await globalThis.crypto.subtle.digest({ name: 'sha-1' }, typeof s === 'string' ? textEncoder.encode(s) : s);
18+
// Use encodeURIComponent to avoid issues with btoa and Latin-1 characters
19+
return globalThis.btoa(encodeURIComponent(textDecoder.decode(hash)));
20+
} else {
21+
return (await import('crypto')).createHash('sha1').update(s).digest('base64');
22+
}
23+
} catch (ex) {
24+
if (ex instanceof ReferenceError) {
25+
(global as any).WEBWORKER = false;
26+
}
27+
}
2128
}
2229
}

0 commit comments

Comments
 (0)