From cd850efac6c7dca82eddfcd7ef7b0e58d0829e2c Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Thu, 14 Oct 2021 23:19:29 +0200 Subject: [PATCH] refactor(node): createRemoteJWKSet can now be easier mocked closes #259 --- src/runtime/node/fetch_jwks.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/runtime/node/fetch_jwks.ts b/src/runtime/node/fetch_jwks.ts index 7482be63..f35eaaf8 100644 --- a/src/runtime/node/fetch_jwks.ts +++ b/src/runtime/node/fetch_jwks.ts @@ -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' @@ -8,11 +8,6 @@ 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) => ClientRequest } = { - 'https:': https, - 'http:': http, -} - type AcceptedRequestOptions = Pick const fetchJwks: FetchFunction = async ( @@ -20,12 +15,20 @@ const fetchJwks: FetchFunction = async ( timeout: number, options: AcceptedRequestOptions, ) => { - if (protocols[url.protocol] === undefined) { - throw new TypeError('Unsupported URL protocol.') + let get: (...args: Parameters) => 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, })