Fixes web build (sha1 issues)

This commit is contained in:
Eric Amodio
2020-06-25 13:21:46 -04:00
parent 787f4cdbe1
commit 1af716cdca
2 changed files with 14 additions and 6 deletions

View File

@@ -8,13 +8,15 @@
const textDecoder = new TextDecoder();
const textEncoder = new TextEncoder();
declare const WEBWORKER: boolean;
export async function sha1(s: string | Uint8Array): Promise<string> {
if (process?.release.name === 'node') {
return (await import('crypto')).createHash('sha1').update(s).digest('base64');
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 {
const hash = await 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 btoa(encodeURIComponent(textDecoder.decode(hash)));
return (await import('crypto')).createHash('sha1').update(s).digest('base64');
}
}