@@ -16,7 +16,6 @@ export function createHandleResponseHeaders(
1616 next : TidewaveNext ,
1717 ) : Promise < void > {
1818 if ( ! isTidewaveRequest ( req ) ) {
19- wrapResponseHeaders ( res , config ) ;
2019 if ( shouldBufferHtml ( req , config ) ) {
2120 wrapHtmlResponseBody ( res , config , getLocalPort ) ;
2221 }
@@ -26,58 +25,6 @@ export function createHandleResponseHeaders(
2625 } ;
2726}
2827
29- export function rewriteCsp ( config : TidewaveConfig , csp : string ) : string {
30- const hasTrailingSemicolon = / ; \s * $ / . test ( csp ) ;
31- const toolbarHost =
32- config . toolbar === false ? '' : `${ config . clientUrl || 'https://tidewave.ai' } ` ;
33- const directives = csp
34- . split ( ';' )
35- . map ( directive => directive . trim ( ) )
36- . filter ( Boolean )
37- . flatMap ( directive => {
38- if ( directive . startsWith ( 'frame-ancestors' ) ) return [ ] ;
39-
40- const [ policy , values ] = splitDirective ( directive ) ;
41- if ( policy !== 'script-src' || ! values ) return [ directive ] ;
42-
43- if ( values . includes ( "'unsafe-eval'" ) ) {
44- return [ `script-src ${ toolbarHost } ${ values } ` ] ;
45- }
46-
47- return [ `script-src ${ toolbarHost } 'unsafe-eval' ${ values } ` ] ;
48- } ) ;
49-
50- return `${ directives . join ( '; ' ) } ${ hasTrailingSemicolon ? '; ' : '' } ` ;
51- }
52-
53- function wrapResponseHeaders ( res : TidewaveResponse , config : TidewaveConfig ) : void {
54- const originalSetHeader = res . setHeader . bind ( res ) ;
55- const originalWriteHead = res . writeHead . bind ( res ) ;
56-
57- res . removeHeader ( 'x-frame-options' ) ;
58-
59- res . setHeader = ( name , value ) : TidewaveResponse => {
60- const header = name . toLowerCase ( ) ;
61-
62- if ( header === 'x-frame-options' ) {
63- return res ;
64- }
65-
66- return originalSetHeader ( name , rewriteHeader ( config , header , value ) ) ;
67- } ;
68-
69- res . writeHead = function writeHead ( ...args : unknown [ ] ) : TidewaveResponse {
70- const headersIndex = typeof args [ 1 ] === 'object' && args [ 1 ] !== null ? 1 : 2 ;
71- const headers = args [ headersIndex ] ;
72-
73- if ( isHeaderMap ( headers ) ) {
74- args [ headersIndex ] = rewriteHeaders ( config , headers ) ;
75- }
76-
77- return originalWriteHead ( ...( args as Parameters < TidewaveResponse [ 'writeHead' ] > ) ) ;
78- } as TidewaveResponse [ 'writeHead' ] ;
79- }
80-
8128function wrapHtmlResponseBody (
8229 res : TidewaveResponse ,
8330 config : TidewaveConfig ,
@@ -122,39 +69,6 @@ function wrapHtmlResponseBody(
12269 } as TidewaveResponse [ 'end' ] ;
12370}
12471
125- function rewriteHeaders ( config : TidewaveConfig , headers : HeaderMap ) : Record < string , HeaderValue > {
126- const rewritten : Record < string , HeaderValue > = { } ;
127-
128- for ( const [ name , value ] of Object . entries ( headers ) ) {
129- if ( value === undefined ) continue ;
130-
131- const header = name . toLowerCase ( ) ;
132-
133- if ( header === 'x-frame-options' ) continue ;
134-
135- rewritten [ name ] = rewriteHeader ( config , header , value ) ;
136- }
137-
138- return rewritten ;
139- }
140-
141- function rewriteHeader (
142- config : TidewaveConfig ,
143- header : string ,
144- value : number | string | readonly string [ ] ,
145- ) : HeaderValue {
146- if ( Array . isArray ( value ) ) {
147- if ( header !== 'content-security-policy' ) return [ ...value ] ;
148- return value . map ( item => rewriteCsp ( config , item ) ) ;
149- }
150-
151- const scalar = value as number | string ;
152-
153- if ( header !== 'content-security-policy' ) return scalar ;
154- if ( typeof scalar === 'string' ) return rewriteCsp ( config , scalar ) ;
155- return scalar ;
156- }
157-
15872function toBuffer ( chunk : unknown ) : Buffer {
15973 if ( Buffer . isBuffer ( chunk ) ) return chunk ;
16074 if ( chunk instanceof Uint8Array ) return Buffer . from ( chunk ) ;
@@ -191,17 +105,6 @@ function endCallback(chunk: unknown, args: unknown[]): (() => void) | undefined
191105 return typeof callback === 'function' ? ( callback as ( ) => void ) : undefined ;
192106}
193107
194- function splitDirective ( directive : string ) : [ string , string ?] {
195- const index = directive . indexOf ( ' ' ) ;
196- if ( index === - 1 ) return [ directive ] ;
197-
198- return [ directive . slice ( 0 , index ) , directive . slice ( index + 1 ) ] ;
199- }
200-
201- function isHeaderMap ( value : unknown ) : value is HeaderMap {
202- return typeof value === 'object' && value !== null && ! Array . isArray ( value ) ;
203- }
204-
205108function isTidewaveRequest ( req : TidewaveRequest ) : boolean {
206109 const url = req . url || '' ;
207110 const pathname = url . split ( '?' ) [ 0 ] || '' ;
0 commit comments