22 * The AGPL License (AGPL)
33 * Copyright (c) 2022 hans000
44 */
5- import { asyncGenerator , delayRun , tryToProxyUrl } from "../../tools" ;
5+ import { asyncGenerator , delayRun , formatChunk , tryToProxyUrl } from "../../tools" ;
66import { log } from "../../tools/log" ;
77import { parseUrl } from "../../tools" ;
88import { Options , __global__ } from "./globalVar" ;
@@ -21,7 +21,7 @@ export function proxyFetch(options: Options) {
2121
2222 const proxyFetch = new Proxy ( __global__ . NativeFetch , {
2323 async apply ( target , thisArg , args ) {
24- const [ input , init ] = args
24+ const [ input , init ] = args as [ Request | URL | string , RequestInit ]
2525 const isRequest = input instanceof Request
2626 const req = isRequest ? input . clone ( ) : new Request ( input . toString ( ) , init )
2727 const url = isRequest
@@ -35,6 +35,19 @@ export function proxyFetch(options: Options) {
3535 type : 'fetch' ,
3636 params : [ ...url . searchParams . entries ( ) ] ,
3737 } )
38+
39+ if ( matchItem ?. requestHeaders ) {
40+ Object . entries ( matchItem . requestHeaders ) . forEach ( ( [ key , value ] ) => {
41+ if ( init . headers instanceof Headers ) {
42+ init . headers . append ( key , value )
43+ } else if ( Array . isArray ( init . headers ) ) {
44+ init . headers . push ( [ key , value ] )
45+ } else if ( typeof init . headers === 'object' ) {
46+ init . headers [ key ] = value
47+ }
48+ } )
49+ }
50+
3851 const realFetch = __global__ . PageFetch || target
3952 const proxyUrl = tryToProxyUrl ( input , __global__ . options . proxy )
4053 const proxyInput = isRequest ? new Request ( proxyUrl , init ) : proxyUrl
@@ -49,26 +62,28 @@ export function proxyFetch(options: Options) {
4962 ...init ,
5063 } )
5164 }
52- const realResponse = options . faked
53- ? new Response ( new Blob ( [ 'null' ] ) )
65+
66+ const chunks = matchItem . chunks || [ ]
67+ const isEventSource = ! ! chunks . length
68+ const realResponse = ( options . faked || isEventSource )
69+ ? new Response ( new Blob ( [ 'null' ] ) , init )
5470 : await realFetch . call ( thisArg , proxyInput , init )
5571 const response = await onFetchIntercept ( matchItem ) ( realResponse )
5672
5773 return new Promise ( resolve => {
5874 delayRun ( async ( ) => {
5975 let res : Response = response || realResponse
6076
61- const chunks = matchItem . chunks || [ ]
62- const isEventSource = ! ! chunks . length
6377 if ( isEventSource ) {
6478 res = new Response ( new ReadableStream ( {
6579 async start ( controller ) {
66- for await ( const value of asyncGenerator ( chunks , matchItem . chunkSpeed ) ) {
67- controller . enqueue ( new TextEncoder ( ) . encode ( value ) ) ;
80+ for await ( const value of asyncGenerator ( chunks , matchItem . chunkInterval ) ) {
81+ const str = formatChunk ( value , matchItem . chunkTemplate )
82+ controller . enqueue ( new TextEncoder ( ) . encode ( str ) ) ;
6883 }
6984 controller . close ( ) ;
7085 } ,
71- } ) )
86+ } ) , init )
7287 }
7388
7489 resolve ( res )
0 commit comments