Skip to content

Commit 43255d9

Browse files
committed
Pass corsProxyUrl to TCPOverFetchWebsocket fro bootSiteClient
1 parent a44731d commit 43255d9

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

packages/php-wasm/web/src/lib/tcp-over-fetch-websocket.ts

+27-17
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@
3838
* From there, the plaintext data is treated by the same HTTP<->fetch() machinery as
3939
* described in the previous paragraph.
4040
*/
41-
// @ts-ignore
42-
// import { corsProxyUrl } from 'virtual:cors-proxy-url';
4341
import { TLS_1_2_Connection } from './tls/1_2/connection';
4442
import { generateCertificate, GeneratedCertificate } from './tls/certificates';
4543
import { concatUint8Arrays } from './tls/utils';
4644
import { ContentTypes } from './tls/1_2/types';
47-
const corsProxyUrl = 'http://127.0.0.1:5263/cors-proxy.php';
4845

4946
export type TCPOverFetchOptions = {
5047
CAroot: GeneratedCertificate;
48+
corsProxyUrl?: string;
5149
};
5250

5351
/**
@@ -70,6 +68,7 @@ export const tcpOverFetchWebsocket = (tcpOptions: TCPOverFetchOptions) => {
7068
constructor(url: string, wsOptions: string[]) {
7169
super(url, wsOptions, {
7270
CAroot: tcpOptions.CAroot,
71+
corsProxyUrl: tcpOptions.corsProxyUrl,
7372
});
7473
}
7574
};
@@ -88,6 +87,7 @@ export interface TCPOverFetchWebsocketOptions {
8887
* clientDownstream stream and tracking the closure of that stream.
8988
*/
9089
outputType?: 'messages' | 'stream';
90+
corsProxyUrl?: string;
9191
}
9292

9393
export class TCPOverFetchWebsocket {
@@ -104,6 +104,7 @@ export class TCPOverFetchWebsocket {
104104
port = 0;
105105
listeners = new Map<string, any>();
106106
CAroot?: GeneratedCertificate;
107+
corsProxyUrl?: string;
107108

108109
clientUpstream = new TransformStream();
109110
clientUpstreamWriter = this.clientUpstream.writable.getWriter();
@@ -114,13 +115,18 @@ export class TCPOverFetchWebsocket {
114115
constructor(
115116
public url: string,
116117
public options: string[],
117-
{ CAroot, outputType = 'messages' }: TCPOverFetchWebsocketOptions = {}
118+
{
119+
CAroot,
120+
corsProxyUrl,
121+
outputType = 'messages',
122+
}: TCPOverFetchWebsocketOptions = {}
118123
) {
119124
const wsUrl = new URL(url);
120125
this.host = wsUrl.searchParams.get('host')!;
121126
this.port = parseInt(wsUrl.searchParams.get('port')!, 10);
122127
this.binaryType = 'arraybuffer';
123128

129+
this.corsProxyUrl = corsProxyUrl;
124130
this.CAroot = CAroot;
125131
if (outputType === 'messages') {
126132
this.clientDownstream.readable
@@ -310,9 +316,10 @@ export class TCPOverFetchWebsocket {
310316
'https'
311317
);
312318
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);
316323
} catch (e) {
317324
// Ignore errors from fetch()
318325
// They are handled in the constructor
@@ -330,9 +337,10 @@ export class TCPOverFetchWebsocket {
330337
'http'
331338
);
332339
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);
336344
} catch (e) {
337345
// Ignore errors from fetch()
338346
// They are handled in the constructor
@@ -412,12 +420,14 @@ class RawBytesFetch {
412420
/**
413421
* Streams a HTTP response including the status line and headers.
414422
*/
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;
421431

422432
// This initially used a TransformStream and piped the response
423433
// body to the writable side of the TransformStream.
@@ -428,7 +438,7 @@ class RawBytesFetch {
428438
async start(controller) {
429439
let response: Response;
430440
try {
431-
response = await fetch(requestClone);
441+
response = await fetch(targetRequest);
432442
} catch (error) {
433443
/**
434444
* Pretend we've got a 400 Bad Request response whenever

packages/playground/blueprints/src/lib/resources.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export class GitDirectoryResource extends Resource<Directory> {
459459

460460
async resolve() {
461461
const repoUrl = this.options?.corsProxy
462-
? `${this.options.corsProxy}?${this.reference.url}`
462+
? `${this.options.corsProxy}${this.reference.url}`
463463
: this.reference.url;
464464
const ref = ['', 'HEAD'].includes(this.reference.ref)
465465
? 'HEAD'

packages/playground/client/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export async function startPlaygroundWeb({
152152
phpVersion: compiled.versions.php,
153153
wpVersion: compiled.versions.wp,
154154
withNetworking: compiled.features.networking,
155+
corsProxyUrl: corsProxy,
155156
});
156157
await playground.isReady();
157158
downloadPHPandWP.finish();

packages/playground/remote/src/lib/worker-thread.ts

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export type WorkerBootOptions = {
7575
withNetworking: boolean;
7676
mounts?: Array<MountDescriptor>;
7777
shouldInstallWordPress?: boolean;
78+
corsProxyUrl?: string;
7879
};
7980

8081
/** @inheritDoc PHPClient */
@@ -168,6 +169,7 @@ export class PlaygroundWorkerEndpoint extends PHPWorker {
168169
sapiName = 'cli',
169170
withNetworking = false,
170171
shouldInstallWordPress = true,
172+
corsProxyUrl,
171173
}: WorkerBootOptions) {
172174
if (this.booted) {
173175
throw new Error('Playground already booted');
@@ -262,6 +264,7 @@ export class PlaygroundWorkerEndpoint extends PHPWorker {
262264
});
263265
tcpOverFetch = {
264266
CAroot,
267+
corsProxyUrl,
265268
};
266269
} else {
267270
phpIniEntries['allow_url_fopen'] = '0';

packages/playground/website/vite.config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ export default defineConfig(({ command, mode }) => {
8383
content: `
8484
export const corsProxyUrl = '${
8585
mode === 'production'
86-
? '/cors-proxy.php'
87-
: 'http://127.0.0.1:5263/cors-proxy.php'
86+
? '/cors-proxy.php?'
87+
: 'http://127.0.0.1:5263/cors-proxy.php?'
8888
}';`,
8989
}),
9090
// GitHub OAuth flow

packages/vite-extensions/vite-cors-proxy-url.ts

-12
This file was deleted.

0 commit comments

Comments
 (0)