From 28ea0f0aaa9bf3093d3506c8aadd587ed4ca8a73 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 29 Jun 2020 03:28:26 -0400 Subject: [PATCH] Fixes node runtime error --- extensions/github-browser/src/sha1.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/extensions/github-browser/src/sha1.ts b/extensions/github-browser/src/sha1.ts index fd638f65b67..0469b3c98a1 100644 --- a/extensions/github-browser/src/sha1.ts +++ b/extensions/github-browser/src/sha1.ts @@ -8,15 +8,22 @@ const textDecoder = new TextDecoder(); const textEncoder = new TextEncoder(); -declare const WEBWORKER: boolean; +declare let WEBWORKER: boolean; export async function sha1(s: string | Uint8Array): Promise { - if (WEBWORKER) { - const hash = await globalThis.crypto.subtle.digest({ name: 'sha-1' }, typeof s === 'string' ? textEncoder.encode(s) : s); - // Use encodeURIComponent to avoid issues with btoa and Latin-1 characters - return globalThis.btoa(encodeURIComponent(textDecoder.decode(hash))); - } - else { - return (await import('crypto')).createHash('sha1').update(s).digest('base64'); + while (true) { + try { + if (WEBWORKER) { + const hash = await globalThis.crypto.subtle.digest({ name: 'sha-1' }, typeof s === 'string' ? textEncoder.encode(s) : s); + // Use encodeURIComponent to avoid issues with btoa and Latin-1 characters + return globalThis.btoa(encodeURIComponent(textDecoder.decode(hash))); + } else { + return (await import('crypto')).createHash('sha1').update(s).digest('base64'); + } + } catch (ex) { + if (ex instanceof ReferenceError) { + (global as any).WEBWORKER = false; + } + } } }