Skip to content

Commit

Permalink
fix(browser): avoid global-conflicting variable name fetch (#199)
Browse files Browse the repository at this point in the history
Co-authored-by: Filip Skokan <panva.ip@gmail.com>
  • Loading branch information
ItalyPaleAle and panva committed May 14, 2021
1 parent 77b4654 commit b2c6273
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@
"lint": "eslint --config ./src/.eslintrc.json ./src",
"runtime-browser": "run-s runtime:clear runtime:browser:* runtime:refs",
"runtime-node": "run-s runtime:clear runtime:node:* runtime:refs",
"runtime-node-webcrypto": "run-s runtime:clear runtime:browser:* && cp ./src/runtime/node/webcrypto.ts ./src/runtime/ && cp ./src/runtime/node/fetch.ts ./src/runtime/ && cp ./src/runtime/node/base64url.ts ./src/runtime/ && cp ./src/runtime/node/zlib.ts ./src/runtime/ && run-s runtime:refs",
"runtime-node-webcrypto": "run-s runtime:clear runtime:browser:* && cp ./src/runtime/node/webcrypto.ts ./src/runtime/ && cp ./src/runtime/node/fetch_jwks.ts ./src/runtime/ && cp ./src/runtime/node/base64url.ts ./src/runtime/ && cp ./src/runtime/node/zlib.ts ./src/runtime/ && run-s runtime:refs",
"runtime:browser:copy": "cp ./src/runtime/browser/*.ts ./src/runtime",
"runtime:clear": "run-s -s runtime:find | xargs -0 rm -f",
"runtime:find": "find src/runtime -not -name '*.d.ts' -maxdepth 1 -type f -print0",
Expand Down
4 changes: 2 additions & 2 deletions src/jwks/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
JWKSNoMatchingKey,
JWKSMultipleMatchingKeys,
} from '../util/errors.js'
import fetchJson from '../runtime/fetch.js'
import fetchJwks from '../runtime/fetch_jwks.js'
import isObject from '../lib/is_object.js'

function getKtyFromAlg(alg: string) {
Expand Down Expand Up @@ -187,7 +187,7 @@ class RemoteJWKSet {

async reload() {
if (!this._pendingFetch) {
this._pendingFetch = fetchJson(this._url, this._timeoutDuration, this._options)
this._pendingFetch = fetchJwks(this._url, this._timeoutDuration, this._options)
.then((json) => {
if (
typeof json !== 'object' ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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) => {
const fetchJwks: FetchFunction = async (url: URL, timeout: number) => {
let controller!: AbortController
if (typeof AbortController === 'function') {
controller = new AbortController()
Expand All @@ -28,4 +28,4 @@ const fetch: FetchFunction = async (url: URL, timeout: number) => {
throw new JOSEError('Failed to parse the JSON Web Key Set HTTP response as JSON')
}
}
export default fetch
export default fetchJwks
8 changes: 6 additions & 2 deletions src/runtime/node/fetch.ts → src/runtime/node/fetch_jwks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const protocols: { [protocol: string]: (...args: Parameters<typeof https>) => Cl

type AcceptedRequestOptions = Pick<RequestOptions, 'agent'>

const fetch: FetchFunction = async (url: URL, timeout: number, options: AcceptedRequestOptions) => {
const fetchJwks: FetchFunction = async (
url: URL,
timeout: number,
options: AcceptedRequestOptions,
) => {
if (protocols[url.protocol] === undefined) {
throw new TypeError('Unsupported URL protocol.')
}
Expand Down Expand Up @@ -46,4 +50,4 @@ const fetch: FetchFunction = async (url: URL, timeout: number, options: Accepted
}
}

export default fetch
export default fetchJwks

0 comments on commit b2c6273

Please sign in to comment.