Explorar o código

@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`.
Bryan J Swift hai 1 ano
pai
achega
4ae7810a40
Modificáronse 1 ficheiros con 3 adicións e 1 borrados
  1. 3 1
      packages/@uppy/aws-s3-multipart/src/createSignedURL.js

+ 3 - 1
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))
 }