Skip to content

Commit

Permalink
refactor(node): createRemoteJWKSet can now be easier mocked
Browse files Browse the repository at this point in the history
closes #259
  • Loading branch information
panva committed Oct 14, 2021
1 parent 5d77921 commit cd850ef
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/runtime/node/fetch_jwks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { get as http } from 'http'
import { get as https } from 'https'
import * as http from 'http'
import * as https from 'https'
import { once } from 'events'
import type { ClientRequest, IncomingMessage } from 'http'
import type { RequestOptions } from 'https'
Expand All @@ -8,24 +8,27 @@ import type { FetchFunction } from '../interfaces.d'
import { JOSEError, JWKSTimeout } from '../../util/errors.js'
import { concat, decoder } from '../../lib/buffer_utils.js'

const protocols: { [protocol: string]: (...args: Parameters<typeof https>) => ClientRequest } = {
'https:': https,
'http:': http,
}

type AcceptedRequestOptions = Pick<RequestOptions, 'agent'>

const fetchJwks: FetchFunction = async (
url: URL,
timeout: number,
options: AcceptedRequestOptions,
) => {
if (protocols[url.protocol] === undefined) {
throw new TypeError('Unsupported URL protocol.')
let get: (...args: Parameters<typeof https.get>) => ClientRequest
switch (url.protocol) {
case 'https:':
get = https.get
break
case 'http:':
get = http.get
break
default:
throw new TypeError('Unsupported URL protocol.')
}

const { agent } = options
const req = protocols[url.protocol](url.href, {
const req = get(url.href, {
agent,
timeout,
})
Expand Down

0 comments on commit cd850ef

Please sign in to comment.