diff --git a/src/lib/crypto_key.ts b/src/lib/crypto_key.ts index 4b822edcf9..bd0c936f68 100644 --- a/src/lib/crypto_key.ts +++ b/src/lib/crypto_key.ts @@ -1,4 +1,4 @@ -import { isCloudflareWorkers, isNodeJs } from '../runtime/global.js' +import { isCloudflareWorkers, isNodeJs } from '../runtime/env.js' function unusable(name: string | number, prop = 'algorithm.name') { return new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`) diff --git a/src/runtime/browser/asn1.ts b/src/runtime/browser/asn1.ts index 44bfcb8219..498d162969 100644 --- a/src/runtime/browser/asn1.ts +++ b/src/runtime/browser/asn1.ts @@ -1,4 +1,4 @@ -import globalThis, { isCloudflareWorkers, isNodeJs } from './global.js' +import { isCloudflareWorkers, isNodeJs } from './env.js' import crypto, { isCryptoKey } from './webcrypto.js' import type { PEMExportFunction, PEMImportFunction } from '../interfaces.d' import invalidKeyInput from '../../lib/invalid_key_input.js' @@ -85,8 +85,7 @@ const genericImport = async ( let keyUsages: KeyUsage[] const keyData = new Uint8Array( - globalThis - .atob(pem.replace(replace, '')) + atob(pem.replace(replace, '')) .split('') .map((c) => c.charCodeAt(0)), ) diff --git a/src/runtime/browser/base64url.ts b/src/runtime/browser/base64url.ts index 89d6efad23..1ccb6b154d 100644 --- a/src/runtime/browser/base64url.ts +++ b/src/runtime/browser/base64url.ts @@ -1,5 +1,4 @@ import { encoder, decoder } from '../../lib/buffer_utils.js' -import globalThis from './global.js' export const encodeBase64 = (input: Uint8Array | string) => { let unencoded = input @@ -12,7 +11,7 @@ export const encodeBase64 = (input: Uint8Array | string) => { // @ts-expect-error arr.push(String.fromCharCode.apply(null, unencoded.subarray(i, i + CHUNK_SIZE))) } - return globalThis.btoa(arr.join('')) + return btoa(arr.join('')) } export const encode = (input: Uint8Array | string) => { @@ -21,8 +20,7 @@ export const encode = (input: Uint8Array | string) => { export const decodeBase64 = (encoded: string): Uint8Array => { return new Uint8Array( - globalThis - .atob(encoded) + atob(encoded) .split('') .map((c) => c.charCodeAt(0)), ) diff --git a/src/runtime/browser/env.ts b/src/runtime/browser/env.ts new file mode 100644 index 0000000000..52195baf02 --- /dev/null +++ b/src/runtime/browser/env.ts @@ -0,0 +1,13 @@ +export function isCloudflareWorkers(): boolean { + // @ts-expect-error + return typeof WebSocketPair === 'function' +} + +export function isNodeJs(): boolean { + try { + // @deno-expect-error + return process.versions.node !== undefined + } catch { + return false + } +} diff --git a/src/runtime/browser/fetch_jwks.ts b/src/runtime/browser/fetch_jwks.ts index d8a9bcfab6..0de56218a5 100644 --- a/src/runtime/browser/fetch_jwks.ts +++ b/src/runtime/browser/fetch_jwks.ts @@ -1,6 +1,6 @@ import type { FetchFunction } from '../interfaces.d' import { JOSEError, JWKSTimeout } from '../../util/errors.js' -import globalThis, { isCloudflareWorkers } from './global.js' +import { isCloudflareWorkers } from './env.js' const fetchJwks: FetchFunction = async (url: URL, timeout: number) => { let controller!: AbortController @@ -14,23 +14,21 @@ const fetchJwks: FetchFunction = async (url: URL, timeout: number) => { }, timeout) } - const response = await globalThis - .fetch(url.href, { - signal: controller ? controller.signal : undefined, - redirect: 'manual', - method: 'GET', - ...(!isCloudflareWorkers() - ? { - referrerPolicy: 'no-referrer', - credentials: 'omit', - mode: 'cors', - } - : undefined), - }) - .catch((err) => { - if (timedOut) throw new JWKSTimeout() - throw err - }) + const response = await fetch(url.href, { + signal: controller ? controller.signal : undefined, + redirect: 'manual', + method: 'GET', + ...(!isCloudflareWorkers() + ? { + referrerPolicy: 'no-referrer', + credentials: 'omit', + mode: 'cors', + } + : undefined), + }).catch((err) => { + if (timedOut) throw new JWKSTimeout() + throw err + }) if (id !== undefined) clearTimeout(id) diff --git a/src/runtime/browser/generate.ts b/src/runtime/browser/generate.ts index e6939bbb31..48c1732085 100644 --- a/src/runtime/browser/generate.ts +++ b/src/runtime/browser/generate.ts @@ -1,4 +1,4 @@ -import { isCloudflareWorkers, isNodeJs } from './global.js' +import { isCloudflareWorkers, isNodeJs } from './env.js' import crypto from './webcrypto.js' import { JOSENotSupported } from '../../util/errors.js' import random from './random.js' diff --git a/src/runtime/browser/global.ts b/src/runtime/browser/global.ts deleted file mode 100644 index b0b3718c08..0000000000 --- a/src/runtime/browser/global.ts +++ /dev/null @@ -1,24 +0,0 @@ -function getGlobal() { - if (typeof globalThis !== 'undefined') return globalThis - if (typeof self !== 'undefined') return self - if (typeof window !== 'undefined') return window - throw new Error('unable to locate global object') -} - -export default getGlobal() -export function isCloudflareWorkers(): boolean { - try { - // @ts-expect-error - return getGlobal().WebSocketPair !== undefined - } catch { - return false - } -} -export function isNodeJs(): boolean { - try { - // @deno-expect-error - return getGlobal().process?.versions?.node !== undefined - } catch { - return false - } -} diff --git a/src/runtime/browser/jwk_to_key.ts b/src/runtime/browser/jwk_to_key.ts index d5e6502f41..e74665a2aa 100644 --- a/src/runtime/browser/jwk_to_key.ts +++ b/src/runtime/browser/jwk_to_key.ts @@ -1,4 +1,4 @@ -import { isCloudflareWorkers, isNodeJs } from './global.js' +import { isCloudflareWorkers, isNodeJs } from './env.js' import crypto from './webcrypto.js' import type { JWKImportFunction } from '../interfaces.d' import { JOSENotSupported } from '../../util/errors.js' diff --git a/src/runtime/browser/subtle_dsa.ts b/src/runtime/browser/subtle_dsa.ts index af31b667a2..2e1840c469 100644 --- a/src/runtime/browser/subtle_dsa.ts +++ b/src/runtime/browser/subtle_dsa.ts @@ -1,4 +1,4 @@ -import { isCloudflareWorkers, isNodeJs } from './global.js' +import { isCloudflareWorkers, isNodeJs } from './env.js' import { JOSENotSupported } from '../../util/errors.js' export default function subtleDsa(alg: string, namedCurve?: string) { diff --git a/src/runtime/browser/webcrypto.ts b/src/runtime/browser/webcrypto.ts index 1ec3cb9162..026bc29887 100644 --- a/src/runtime/browser/webcrypto.ts +++ b/src/runtime/browser/webcrypto.ts @@ -1,10 +1,14 @@ -import globalThis from './global.js' - -export default globalThis.crypto +export default crypto export function isCryptoKey(key: unknown): key is CryptoKey { - if (typeof globalThis.CryptoKey === 'undefined') { + try { + return ( + key != null && + typeof (key).extractable === 'boolean' && + typeof (key).algorithm.name === 'string' && + typeof (key).type === 'string' + ) + } catch { return false } - return key != null && key instanceof globalThis.CryptoKey } diff --git a/src/runtime/node/global.ts b/src/runtime/node/env.ts similarity index 100% rename from src/runtime/node/global.ts rename to src/runtime/node/env.ts