mirror of
https://github.com/danbulant/jose
synced 2026-05-24 12:35:36 +00:00
refactor(node): createRemoteJWKSet can now be easier mocked
closes #259
This commit is contained in:
parent
5d77921e50
commit
cd850efac6
1 changed files with 13 additions and 10 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import { get as http } from 'http'
|
import * as http from 'http'
|
||||||
import { get as https } from 'https'
|
import * as https from 'https'
|
||||||
import { once } from 'events'
|
import { once } from 'events'
|
||||||
import type { ClientRequest, IncomingMessage } from 'http'
|
import type { ClientRequest, IncomingMessage } from 'http'
|
||||||
import type { RequestOptions } from 'https'
|
import type { RequestOptions } from 'https'
|
||||||
|
|
@ -8,11 +8,6 @@ import type { FetchFunction } from '../interfaces.d'
|
||||||
import { JOSEError, JWKSTimeout } from '../../util/errors.js'
|
import { JOSEError, JWKSTimeout } from '../../util/errors.js'
|
||||||
import { concat, decoder } from '../../lib/buffer_utils.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'>
|
type AcceptedRequestOptions = Pick<RequestOptions, 'agent'>
|
||||||
|
|
||||||
const fetchJwks: FetchFunction = async (
|
const fetchJwks: FetchFunction = async (
|
||||||
|
|
@ -20,12 +15,20 @@ const fetchJwks: FetchFunction = async (
|
||||||
timeout: number,
|
timeout: number,
|
||||||
options: AcceptedRequestOptions,
|
options: AcceptedRequestOptions,
|
||||||
) => {
|
) => {
|
||||||
if (protocols[url.protocol] === undefined) {
|
let get: (...args: Parameters<typeof https.get>) => ClientRequest
|
||||||
throw new TypeError('Unsupported URL protocol.')
|
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 { agent } = options
|
||||||
const req = protocols[url.protocol](url.href, {
|
const req = get(url.href, {
|
||||||
agent,
|
agent,
|
||||||
timeout,
|
timeout,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue