99import { Readable } from 'stream' ;
1010import { URL } from 'url' ;
1111
12- import { Headers , HeadersProps } from './headers' ;
12+ import { HeadersProps } from './headers' ;
1313import {
14- ProcessHeadersAsyncHook ,
14+ AsyncHeadersReducer ,
1515 SSRIAlgorithm ,
1616 SSRILock ,
1717 SSRIMap ,
@@ -24,13 +24,17 @@ export const SSRI_MAP: Map<string, Map<SSRIAlgorithm, string>> = new Map();
2424const concurrency = parseInt ( process . env . WPUS_SSRI_CONCURRENCY ?? '' ) ;
2525const limit = pLimit ( Number . isNaN ( concurrency ) ? 6 : concurrency ) ;
2626
27- export const processSSRI : ProcessHeadersAsyncHook = async ( data ) => {
27+ export const processSSRI : AsyncHeadersReducer = async ( data ) => {
2828 const {
2929 headers,
3030 ssriLock,
3131 options : { ssri } ,
3232 } = data ;
3333
34+ if ( headers . resource == undefined && headers . require === undefined ) {
35+ return headers ;
36+ }
37+
3438 const ssriOptions : SSRIOptions =
3539 ssri === true || ssri === undefined ? { } : ssri ;
3640
@@ -78,7 +82,10 @@ export const processSSRI: ProcessHeadersAsyncHook = async (data) => {
7882 data . ssriLock = toSSRILock ( ssriMap ) ;
7983 }
8084
81- return updateHeaders ( headers , ssriMap ) ;
85+ return {
86+ ...headers ,
87+ ...patchHeaders ( headers , ssriMap ) ,
88+ } ;
8289} ;
8390
8491export function normalizeURL ( url : string ) : string {
@@ -88,7 +95,7 @@ export function normalizeURL(url: string): string {
8895}
8996
9097export function getTargetURLs (
91- headers : Headers ,
98+ headers : HeadersProps ,
9299 options : Pick < SSRIOptions , 'include' | 'exclude' > ,
93100) : string [ ] {
94101 const urls : string [ ] = [ ] ;
@@ -202,13 +209,12 @@ export function updateURL(url: string, ssriMap: SSRIMap): string {
202209 return urlObj . toString ( ) ;
203210}
204211
205- export function updateHeaders ( headers : Headers , ssriMap : SSRIMap ) : Headers {
212+ export function patchHeaders (
213+ headers : HeadersProps ,
214+ ssriMap : SSRIMap ,
215+ ) : HeadersProps {
206216 const headersProps : HeadersProps = { } ;
207217
208- if ( headers . resource == undefined && headers . require === undefined ) {
209- return headers ;
210- }
211-
212218 if ( headers . require !== undefined ) {
213219 if ( Array . isArray ( headers . require ) ) {
214220 headersProps . require = headers . require . map ( ( url ) =>
@@ -228,5 +234,5 @@ export function updateHeaders(headers: Headers, ssriMap: SSRIMap): Headers {
228234 ) ;
229235 }
230236
231- return headers . update ( headersProps ) ;
237+ return headersProps ;
232238}
0 commit comments