11import { config , flagEnabled } from "@/shared" ;
2- import { rewriteUrl , unrewriteUrl } from "@rewriters/url" ;
2+ import { rewriteUrl , unrewriteUrl , URLMeta } from "@rewriters/url" ;
33import { ScramjetClient } from "@client/index" ;
44
55export default function ( client : ScramjetClient , self : Self ) {
@@ -116,7 +116,6 @@ export default function (client: ScramjetClient, self: Self) {
116116 } ,
117117 } ) ;
118118
119- // send has no return value right
120119 ctx . return ( undefined ) ;
121120 } ,
122121 } ) ;
@@ -126,4 +125,39 @@ export default function (client: ScramjetClient, self: Self) {
126125 return unrewriteUrl ( ctx . get ( ) as string , client . meta ) ;
127126 } ,
128127 } ) ;
128+
129+ client . Proxy ( "XMLHttpRequest.prototype.getAllResponseHeaders" , {
130+ apply ( ctx ) {
131+ const headerstring = ctx . fn . call ( ctx . this ) as string ;
132+ if ( ! headerstring ) return headerstring ;
133+ const headers = headerstring . split ( "\r\n" ) ;
134+
135+ for ( const [ i , header ] of headers . entries ( ) ) {
136+ if ( header . toLowerCase ( ) . startsWith ( "link:" ) ) {
137+ headers [ i ] = `Link: ${ unrewriteLinkHeader (
138+ header . slice ( 5 ) . trim ( ) ,
139+ client . meta
140+ ) } `;
141+ }
142+ }
143+
144+ ctx . return ( headers . join ( "\r\n" ) ) ;
145+ } ,
146+ } ) ;
147+ client . Proxy ( "XMLHttpRequest.prototype.getResponseHeader" , {
148+ apply ( ctx ) {
149+ const header = ctx . fn . call ( ctx . this , ctx . args [ 0 ] ) as string | null ;
150+ if ( ! header ) return header ;
151+ if ( ctx . args [ 0 ] . toLowerCase ( ) === "link" ) {
152+ ctx . return ( unrewriteLinkHeader ( header , client . meta ) ) ;
153+ }
154+ } ,
155+ } ) ;
156+ }
157+
158+ export function unrewriteLinkHeader ( header : string , meta : URLMeta ) {
159+ return header . replace (
160+ / < ( [ ^ > ] + ) > / gi,
161+ ( match ) => `${ unrewriteUrl ( match , meta ) } `
162+ ) ;
129163}
0 commit comments