38
38
* From there, the plaintext data is treated by the same HTTP<->fetch() machinery as
39
39
* described in the previous paragraph.
40
40
*/
41
- // @ts -ignore
42
- // import { corsProxyUrl } from 'virtual:cors-proxy-url';
43
41
import { TLS_1_2_Connection } from './tls/1_2/connection' ;
44
42
import { generateCertificate , GeneratedCertificate } from './tls/certificates' ;
45
43
import { concatUint8Arrays } from './tls/utils' ;
46
44
import { ContentTypes } from './tls/1_2/types' ;
47
- const corsProxyUrl = 'http://127.0.0.1:5263/cors-proxy.php' ;
48
45
49
46
export type TCPOverFetchOptions = {
50
47
CAroot : GeneratedCertificate ;
48
+ corsProxyUrl ?: string ;
51
49
} ;
52
50
53
51
/**
@@ -70,6 +68,7 @@ export const tcpOverFetchWebsocket = (tcpOptions: TCPOverFetchOptions) => {
70
68
constructor ( url : string , wsOptions : string [ ] ) {
71
69
super ( url , wsOptions , {
72
70
CAroot : tcpOptions . CAroot ,
71
+ corsProxyUrl : tcpOptions . corsProxyUrl ,
73
72
} ) ;
74
73
}
75
74
} ;
@@ -88,6 +87,7 @@ export interface TCPOverFetchWebsocketOptions {
88
87
* clientDownstream stream and tracking the closure of that stream.
89
88
*/
90
89
outputType ?: 'messages' | 'stream' ;
90
+ corsProxyUrl ?: string ;
91
91
}
92
92
93
93
export class TCPOverFetchWebsocket {
@@ -104,6 +104,7 @@ export class TCPOverFetchWebsocket {
104
104
port = 0 ;
105
105
listeners = new Map < string , any > ( ) ;
106
106
CAroot ?: GeneratedCertificate ;
107
+ corsProxyUrl ?: string ;
107
108
108
109
clientUpstream = new TransformStream ( ) ;
109
110
clientUpstreamWriter = this . clientUpstream . writable . getWriter ( ) ;
@@ -114,13 +115,18 @@ export class TCPOverFetchWebsocket {
114
115
constructor (
115
116
public url : string ,
116
117
public options : string [ ] ,
117
- { CAroot, outputType = 'messages' } : TCPOverFetchWebsocketOptions = { }
118
+ {
119
+ CAroot,
120
+ corsProxyUrl,
121
+ outputType = 'messages' ,
122
+ } : TCPOverFetchWebsocketOptions = { }
118
123
) {
119
124
const wsUrl = new URL ( url ) ;
120
125
this . host = wsUrl . searchParams . get ( 'host' ) ! ;
121
126
this . port = parseInt ( wsUrl . searchParams . get ( 'port' ) ! , 10 ) ;
122
127
this . binaryType = 'arraybuffer' ;
123
128
129
+ this . corsProxyUrl = corsProxyUrl ;
124
130
this . CAroot = CAroot ;
125
131
if ( outputType === 'messages' ) {
126
132
this . clientDownstream . readable
@@ -310,9 +316,10 @@ export class TCPOverFetchWebsocket {
310
316
'https'
311
317
) ;
312
318
try {
313
- await RawBytesFetch . fetchRawResponseBytes ( request ) . pipeTo (
314
- tlsConnection . serverEnd . downstream . writable
315
- ) ;
319
+ await RawBytesFetch . fetchRawResponseBytes (
320
+ request ,
321
+ this . corsProxyUrl
322
+ ) . pipeTo ( tlsConnection . serverEnd . downstream . writable ) ;
316
323
} catch ( e ) {
317
324
// Ignore errors from fetch()
318
325
// They are handled in the constructor
@@ -330,9 +337,10 @@ export class TCPOverFetchWebsocket {
330
337
'http'
331
338
) ;
332
339
try {
333
- await RawBytesFetch . fetchRawResponseBytes ( request ) . pipeTo (
334
- this . clientDownstream . writable
335
- ) ;
340
+ await RawBytesFetch . fetchRawResponseBytes (
341
+ request ,
342
+ this . corsProxyUrl
343
+ ) . pipeTo ( this . clientDownstream . writable ) ;
336
344
} catch ( e ) {
337
345
// Ignore errors from fetch()
338
346
// They are handled in the constructor
@@ -412,12 +420,14 @@ class RawBytesFetch {
412
420
/**
413
421
* Streams a HTTP response including the status line and headers.
414
422
*/
415
- static fetchRawResponseBytes ( request : Request ) {
416
- const requestClone = new Request (
417
- // @TOOD : somehow handle the CORS proxy logic in the client, not
418
- `${ corsProxyUrl } ?${ request . url } ` ,
419
- request
420
- ) ;
423
+ static fetchRawResponseBytes ( request : Request , corsProxyUrl ?: string ) {
424
+ const targetRequest = corsProxyUrl
425
+ ? new Request (
426
+ // @TOOD : somehow handle the CORS proxy logic in the client, not
427
+ `${ corsProxyUrl } ${ request . url } ` ,
428
+ request
429
+ )
430
+ : request ;
421
431
422
432
// This initially used a TransformStream and piped the response
423
433
// body to the writable side of the TransformStream.
@@ -428,7 +438,7 @@ class RawBytesFetch {
428
438
async start ( controller ) {
429
439
let response : Response ;
430
440
try {
431
- response = await fetch ( requestClone ) ;
441
+ response = await fetch ( targetRequest ) ;
432
442
} catch ( error ) {
433
443
/**
434
444
* Pretend we've got a 400 Bad Request response whenever
0 commit comments