Skip to content

Commit

Permalink
fix: global detection in a browser worker runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Nov 24, 2020
1 parent 3bda579 commit 56ff8fa
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
"jsdoc/require-param": 2,
"jsdoc/valid-types": 2
},
"globals": {
"window": "readonly"
},
"ignorePatterns": ["src/runtime/*/*.ts", "src/runtime/webcrypto.ts", "dist", "src/runtime/*.d.ts"]
"ignorePatterns": [
"src/runtime/*/*.ts",
"src/runtime/webcrypto.ts",
"src/runtime/global.ts",
"dist",
"src/runtime/*.d.ts"
]
}
5 changes: 3 additions & 2 deletions src/runtime/browser/base64url.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Base64UrlDecode, Base64UrlEncode } from '../interfaces.d'
import { encoder, decoder } from '../../lib/buffer_utils.js'
import globalThis from './global.js'

export const encode: Base64UrlEncode = (input) => {
let unencoded = input
if (typeof unencoded === 'string') {
unencoded = encoder.encode(unencoded)
}
const base64string = window.btoa(String.fromCharCode.apply(0, [...unencoded]))
const base64string = globalThis.btoa(String.fromCharCode.apply(0, [...unencoded]))
return base64string.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
}

Expand All @@ -17,7 +18,7 @@ export const decode: Base64UrlDecode = (input) => {
}
encoded = encoded.replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, '')
return new Uint8Array(
window
globalThis
.atob(encoded)
.split('')
.map((c) => c.charCodeAt(0)),
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/browser/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FetchFunction } from '../interfaces.d'
import { JOSEError } from '../../util/errors.js'
import globalThis from './global.js'

const fetch: FetchFunction = async (url: URL, timeout: number) => {
let controller!: AbortController
Expand All @@ -8,7 +9,7 @@ const fetch: FetchFunction = async (url: URL, timeout: number) => {
setTimeout(() => controller.abort(), timeout)
}

const response = await window.fetch(url.href, {
const response = await globalThis.fetch(url.href, {
signal: controller ? controller.signal : undefined,
redirect: 'manual',
referrerPolicy: 'no-referrer',
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/browser/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable no-restricted-globals */

function getGlobal() {
if (typeof self !== 'undefined') return self
if (typeof window !== 'undefined') return window
throw new Error('unable to locate global object')
}

export default globalThis || getGlobal()
7 changes: 3 additions & 4 deletions src/runtime/browser/webcrypto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { JOSEError } from '../../util/errors.js'
import globalThis from './global.js'

const { crypto } = window

export default crypto
export default globalThis.crypto
export function ensureSecureContext() {
if (!window.isSecureContext && !crypto.subtle) {
if (!globalThis.isSecureContext && !globalThis.crypto.subtle) {
throw new JOSEError(
'Web Cryptography API is available only in Secure Contexts. See: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts',
)
Expand Down

0 comments on commit 56ff8fa

Please sign in to comment.