@@ -18,95 +18,23 @@ const truncate = (value, limit = MAX_ERROR_BODY_LENGTH) => {
1818} ;
1919
2020const resolveFetch = ( fetchImpl ) => {
21- if ( typeof fetchImpl === "function" ) {
22- return fetchImpl ;
23- }
24- if ( typeof fetch === "function" ) {
25- return fetch ;
26- }
21+ if ( typeof fetchImpl === "function" ) return fetchImpl ;
22+ if ( typeof fetch === "function" ) return fetch ;
2723 throw new Error ( "Fetch API is niet beschikbaar in de huidige runtime." ) ;
2824} ;
2925
30- const buildUrlFromEnv = ( baseUrl , realm , prefix , suffix = "" ) => {
31- const baseTrimmed = trimString ( baseUrl ) . replace ( / \/ + $ / , "" ) ;
32- const realmTrimmed = trimString ( realm ) ;
33- if ( ! baseTrimmed || ! realmTrimmed ) {
34- return "" ;
35- }
36- return `${ baseTrimmed } ${ prefix } ${ encodeURIComponent ( realmTrimmed ) } ${ suffix } ` ;
37- } ;
38-
3926const buildRequestSignal = ( externalSignal , timeoutMs ) => {
40- if ( externalSignal ) {
41- if (
42- typeof AbortSignal !== "undefined" &&
43- typeof AbortSignal . any === "function" &&
44- typeof AbortSignal . timeout === "function"
45- ) {
46- return AbortSignal . any ( [ externalSignal , AbortSignal . timeout ( timeoutMs ) ] ) ;
47- }
48- return externalSignal ;
49- }
50- if (
51- typeof AbortSignal !== "undefined" &&
52- typeof AbortSignal . timeout === "function"
53- ) {
54- return AbortSignal . timeout ( timeoutMs ) ;
55- }
56- return undefined ;
27+ const timeout = AbortSignal . timeout ( timeoutMs ) ;
28+ if ( ! externalSignal ) return timeout ;
29+ return typeof AbortSignal . any === "function"
30+ ? AbortSignal . any ( [ externalSignal , timeout ] )
31+ : timeout ;
5732} ;
5833
5934const isAbortError = ( error ) =>
6035 error ?. name === "AbortError" || error ?. name === "TimeoutError" ;
6136
62- const delay = ( ms , signal ) =>
63- new Promise ( ( resolve , reject ) => {
64- if ( ms <= 0 ) {
65- resolve ( ) ;
66- return ;
67- }
68- if ( signal ?. aborted ) {
69- reject ( new Error ( "Request geannuleerd" ) ) ;
70- return ;
71- }
72-
73- let settled = false ;
74- let timeoutId ;
75- let abortHandler ;
76-
77- const cleanup = ( ) => {
78- if ( timeoutId ) {
79- clearTimeout ( timeoutId ) ;
80- }
81- if ( signal && abortHandler ) {
82- signal . removeEventListener ( "abort" , abortHandler ) ;
83- }
84- } ;
85-
86- const resolveOnce = ( ) => {
87- if ( settled ) {
88- return ;
89- }
90- settled = true ;
91- cleanup ( ) ;
92- resolve ( ) ;
93- } ;
94-
95- const rejectOnce = ( error ) => {
96- if ( settled ) {
97- return ;
98- }
99- settled = true ;
100- cleanup ( ) ;
101- reject ( error ) ;
102- } ;
103-
104- abortHandler = ( ) => rejectOnce ( new Error ( "Request geannuleerd" ) ) ;
105- timeoutId = setTimeout ( resolveOnce , ms ) ;
106- if ( signal ) {
107- signal . addEventListener ( "abort" , abortHandler , { once : true } ) ;
108- }
109- } ) ;
37+ const delay = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
11038
11139const normalizeContact = ( contact ) => {
11240 if ( ! contact || typeof contact !== "object" || Array . isArray ( contact ) ) {
@@ -188,18 +116,9 @@ class HarvestService {
188116 }
189117
190118 static fromEnv ( ) {
191- const tokenFromEnv = trimString ( process . env . AUTH_TOKEN_URL ) ;
192- const tokenURL =
193- tokenFromEnv ||
194- buildUrlFromEnv (
195- process . env . KEYCLOAK_BASE_URL ,
196- process . env . KEYCLOAK_REALM ,
197- "/realms/" ,
198- "/protocol/openid-connect/token" ,
199- ) ;
200119 return new HarvestService ( {
201120 registerEndpoint : process . env . PDOK_REGISTER_ENDPOINT ,
202- tokenURL,
121+ tokenURL : process . env . AUTH_TOKEN_URL ,
203122 clientID : process . env . AUTH_CLIENT_ID ,
204123 clientSecret : process . env . AUTH_CLIENT_SECRET ,
205124 } ) ;
@@ -254,7 +173,7 @@ class HarvestService {
254173
255174 for ( let i = 0 ; i < hrefs . length ; i += 1 ) {
256175 if ( i > 0 ) {
257- await delay ( this . rateLimitDelayMs , signal ) ;
176+ await delay ( this . rateLimitDelayMs ) ;
258177 }
259178 const href = hrefs [ i ] ;
260179 const oasUrl = deriveOASURLWith ( href ) ;
0 commit comments