Skip to content

Commit

Permalink
Pass corsProxyUrl to TCPOverFetchWebsocket fro bootSiteClient
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Dec 7, 2024
1 parent a44731d commit 43255d9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
44 changes: 27 additions & 17 deletions packages/php-wasm/web/src/lib/tcp-over-fetch-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@
* From there, the plaintext data is treated by the same HTTP<->fetch() machinery as
* described in the previous paragraph.
*/
// @ts-ignore
// import { corsProxyUrl } from 'virtual:cors-proxy-url';
import { TLS_1_2_Connection } from './tls/1_2/connection';
import { generateCertificate, GeneratedCertificate } from './tls/certificates';
import { concatUint8Arrays } from './tls/utils';
import { ContentTypes } from './tls/1_2/types';
const corsProxyUrl = 'http://127.0.0.1:5263/cors-proxy.php';

export type TCPOverFetchOptions = {
CAroot: GeneratedCertificate;
corsProxyUrl?: string;
};

/**
Expand All @@ -70,6 +68,7 @@ export const tcpOverFetchWebsocket = (tcpOptions: TCPOverFetchOptions) => {
constructor(url: string, wsOptions: string[]) {
super(url, wsOptions, {
CAroot: tcpOptions.CAroot,
corsProxyUrl: tcpOptions.corsProxyUrl,
});
}
};
Expand All @@ -88,6 +87,7 @@ export interface TCPOverFetchWebsocketOptions {
* clientDownstream stream and tracking the closure of that stream.
*/
outputType?: 'messages' | 'stream';
corsProxyUrl?: string;
}

export class TCPOverFetchWebsocket {
Expand All @@ -104,6 +104,7 @@ export class TCPOverFetchWebsocket {
port = 0;
listeners = new Map<string, any>();
CAroot?: GeneratedCertificate;
corsProxyUrl?: string;

clientUpstream = new TransformStream();
clientUpstreamWriter = this.clientUpstream.writable.getWriter();
Expand All @@ -114,13 +115,18 @@ export class TCPOverFetchWebsocket {
constructor(
public url: string,
public options: string[],
{ CAroot, outputType = 'messages' }: TCPOverFetchWebsocketOptions = {}
{
CAroot,
corsProxyUrl,
outputType = 'messages',
}: TCPOverFetchWebsocketOptions = {}
) {
const wsUrl = new URL(url);
this.host = wsUrl.searchParams.get('host')!;
this.port = parseInt(wsUrl.searchParams.get('port')!, 10);
this.binaryType = 'arraybuffer';

this.corsProxyUrl = corsProxyUrl;
this.CAroot = CAroot;
if (outputType === 'messages') {
this.clientDownstream.readable
Expand Down Expand Up @@ -310,9 +316,10 @@ export class TCPOverFetchWebsocket {
'https'
);
try {
await RawBytesFetch.fetchRawResponseBytes(request).pipeTo(
tlsConnection.serverEnd.downstream.writable
);
await RawBytesFetch.fetchRawResponseBytes(
request,
this.corsProxyUrl
).pipeTo(tlsConnection.serverEnd.downstream.writable);
} catch (e) {
// Ignore errors from fetch()
// They are handled in the constructor
Expand All @@ -330,9 +337,10 @@ export class TCPOverFetchWebsocket {
'http'
);
try {
await RawBytesFetch.fetchRawResponseBytes(request).pipeTo(
this.clientDownstream.writable
);
await RawBytesFetch.fetchRawResponseBytes(
request,
this.corsProxyUrl
).pipeTo(this.clientDownstream.writable);
} catch (e) {
// Ignore errors from fetch()
// They are handled in the constructor
Expand Down Expand Up @@ -412,12 +420,14 @@ class RawBytesFetch {
/**
* Streams a HTTP response including the status line and headers.
*/
static fetchRawResponseBytes(request: Request) {
const requestClone = new Request(
// @TOOD: somehow handle the CORS proxy logic in the client, not
`${corsProxyUrl}?${request.url}`,
request
);
static fetchRawResponseBytes(request: Request, corsProxyUrl?: string) {
const targetRequest = corsProxyUrl
? new Request(
// @TOOD: somehow handle the CORS proxy logic in the client, not
`${corsProxyUrl}${request.url}`,
request
)
: request;

// This initially used a TransformStream and piped the response
// body to the writable side of the TransformStream.
Expand All @@ -428,7 +438,7 @@ class RawBytesFetch {
async start(controller) {
let response: Response;
try {
response = await fetch(requestClone);
response = await fetch(targetRequest);
} catch (error) {
/**
* Pretend we've got a 400 Bad Request response whenever
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/blueprints/src/lib/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ export class GitDirectoryResource extends Resource<Directory> {

async resolve() {
const repoUrl = this.options?.corsProxy
? `${this.options.corsProxy}?${this.reference.url}`
? `${this.options.corsProxy}${this.reference.url}`
: this.reference.url;
const ref = ['', 'HEAD'].includes(this.reference.ref)
? 'HEAD'
Expand Down
1 change: 1 addition & 0 deletions packages/playground/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export async function startPlaygroundWeb({
phpVersion: compiled.versions.php,
wpVersion: compiled.versions.wp,
withNetworking: compiled.features.networking,
corsProxyUrl: corsProxy,
});
await playground.isReady();
downloadPHPandWP.finish();
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/remote/src/lib/worker-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export type WorkerBootOptions = {
withNetworking: boolean;
mounts?: Array<MountDescriptor>;
shouldInstallWordPress?: boolean;
corsProxyUrl?: string;
};

/** @inheritDoc PHPClient */
Expand Down Expand Up @@ -168,6 +169,7 @@ export class PlaygroundWorkerEndpoint extends PHPWorker {
sapiName = 'cli',
withNetworking = false,
shouldInstallWordPress = true,
corsProxyUrl,
}: WorkerBootOptions) {
if (this.booted) {
throw new Error('Playground already booted');
Expand Down Expand Up @@ -262,6 +264,7 @@ export class PlaygroundWorkerEndpoint extends PHPWorker {
});
tcpOverFetch = {
CAroot,
corsProxyUrl,
};
} else {
phpIniEntries['allow_url_fopen'] = '0';
Expand Down
4 changes: 2 additions & 2 deletions packages/playground/website/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export default defineConfig(({ command, mode }) => {
content: `
export const corsProxyUrl = '${
mode === 'production'
? '/cors-proxy.php'
: 'http://127.0.0.1:5263/cors-proxy.php'
? '/cors-proxy.php?'
: 'http://127.0.0.1:5263/cors-proxy.php?'
}';`,
}),
// GitHub OAuth flow
Expand Down
12 changes: 0 additions & 12 deletions packages/vite-extensions/vite-cors-proxy-url.ts

This file was deleted.

0 comments on commit 43255d9

Please sign in to comment.