Skip to content

Commit

Permalink
fix(edge-functions): don't use globalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Nov 1, 2021
1 parent 92df703 commit 3952030
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/lib/crypto_key.ts
Original file line number Diff line number Diff line change
@@ -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}`)
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/browser/asn1.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)),
)
Expand Down
6 changes: 2 additions & 4 deletions src/runtime/browser/base64url.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) => {
Expand All @@ -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)),
)
Expand Down
13 changes: 13 additions & 0 deletions src/runtime/browser/env.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
34 changes: 16 additions & 18 deletions src/runtime/browser/fetch_jwks.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/browser/generate.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
24 changes: 0 additions & 24 deletions src/runtime/browser/global.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/runtime/browser/jwk_to_key.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/browser/subtle_dsa.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions src/runtime/browser/webcrypto.ts
Original file line number Diff line number Diff line change
@@ -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 (<CryptoKey>key).extractable === 'boolean' &&
typeof (<CryptoKey>key).algorithm.name === 'string' &&
typeof (<CryptoKey>key).type === 'string'
)
} catch {
return false
}
return key != null && key instanceof globalThis.CryptoKey
}
File renamed without changes.

0 comments on commit 3952030

Please sign in to comment.