Skip to content

Commit

Permalink
@uppy/aws-s3-multipart: do not access globalThis.crypto on the top-…
Browse files Browse the repository at this point in the history
…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`.
  • Loading branch information
bryanjswift authored Jul 19, 2023
1 parent a93e6cd commit 4ae7810
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/@uppy/aws-s3-multipart/src/createSignedURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
}

Expand All @@ -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))
}

Expand Down

0 comments on commit 4ae7810

Please sign in to comment.