From 4ae7810a402d60bd3db27109cb6c5504ad96d7e7 Mon Sep 17 00:00:00 2001 From: Bryan J Swift Date: Tue, 18 Jul 2023 21:53:36 -0400 Subject: [PATCH] @uppy/aws-s3-multipart: do not access `globalThis.crypto` on the top-level (#4584) Referencing `globalThis` before the functions are called causes `require` errors in server rendered environments as soon as the `createSignedURL.js` file is imported because `globalThis.crypto` is `undefined`. --- packages/@uppy/aws-s3-multipart/src/createSignedURL.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/@uppy/aws-s3-multipart/src/createSignedURL.js b/packages/@uppy/aws-s3-multipart/src/createSignedURL.js index 80bcedc3c5..593b327b6e 100644 --- a/packages/@uppy/aws-s3-multipart/src/createSignedURL.js +++ b/packages/@uppy/aws-s3-multipart/src/createSignedURL.js @@ -49,15 +49,16 @@ function createCanonicalRequest ({ ].join('\n') } -const { subtle } = globalThis.crypto const ec = new TextEncoder() const algorithm = { name: 'HMAC', hash: 'SHA-256' } async function digest (data) { + const { subtle } = globalThis.crypto return subtle.digest(algorithm.hash, ec.encode(data)) } async function generateHmacKey (secret) { + const { subtle } = globalThis.crypto return subtle.importKey('raw', typeof secret === 'string' ? ec.encode(secret) : secret, algorithm, false, ['sign']) } @@ -71,6 +72,7 @@ function arrayBufferToHexString (arrayBuffer) { } async function hash (key, data) { + const { subtle } = globalThis.crypto return subtle.sign(algorithm, await generateHmacKey(key), ec.encode(data)) }