From e398dc2bc69fc9e882744dce4d89deebe0c4ae8d Mon Sep 17 00:00:00 2001 From: Spencer Pogorzelski <34356756+Scoder12@users.noreply.github.com> Date: Fri, 11 Aug 2023 19:30:31 -0700 Subject: [PATCH] WORKING --- client/AdriftClient.ts | 14 ++++++++++++-- client/Connection.ts | 22 ++++++++++++++-------- server/main.ts | 8 ++++---- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/client/AdriftClient.ts b/client/AdriftClient.ts index 8d5089e..531edd8 100644 --- a/client/AdriftClient.ts +++ b/client/AdriftClient.ts @@ -37,7 +37,7 @@ export class AdriftBareClient extends Client { console.log({ body }); throw new Error("bare-client-custom passed an unexpected body type"); } - let rawResponse = await this.connection.httprequest({ + let { payload, body: respBody } = await this.connection.httprequest({ method, requestHeaders, body, @@ -45,8 +45,18 @@ export class AdriftBareClient extends Client { cache, duplex, }); + const headers = new Headers(); + for (const [header, values] of Object.entries(payload.headers)) { + for (const value of values) { + headers.append(header, value); + } + } - return new Response(JSON.stringify(rawResponse)) as BareResponse; + return new Response(respBody, { + status: payload.status, + statusText: payload.statusText, + headers, + }) as BareResponse; } connect( remote: URL, diff --git a/client/Connection.ts b/client/Connection.ts index ba533dc..e273f05 100644 --- a/client/Connection.ts +++ b/client/Connection.ts @@ -2,6 +2,7 @@ import { C2SRequestType, C2SRequestTypes, HTTPRequestPayload, + HTTPResponsePayload, S2CRequestType, S2CRequestTypes, } from "../protocol"; @@ -29,14 +30,17 @@ export default class Connection { switch (requestType) { case S2CRequestTypes.HTTPResponse: { - let decoder = new TextDecoder(); - let text = decoder.decode(data.slice(cursor)); - console.log(text); - let json = JSON.parse(text); + const payloadLen = view.getUint32(cursor); + cursor += 4; + const decoder = new TextDecoder(); + const payloadRaw = decoder.decode( + data.slice(cursor, cursor + payloadLen) + ); + console.log({ payloadLen, payloadRaw }); + const payload = JSON.parse(payloadRaw); + cursor += payloadLen; - console.log(requestID); - - this.callbacks[requestID](json); + this.callbacks[requestID]({ payload, body: data.slice(cursor) }); break; } } @@ -63,7 +67,9 @@ export default class Connection { console.log(buf); } - httprequest(data: HTTPRequestPayload): Promise { + httprequest( + data: HTTPRequestPayload + ): Promise<{ payload: HTTPResponsePayload; body: ArrayBuffer }> { let json = JSON.stringify(data); return new Promise(async (resolve) => { diff --git a/server/main.ts b/server/main.ts index 26dbbfa..48ab19e 100644 --- a/server/main.ts +++ b/server/main.ts @@ -498,11 +498,11 @@ class Client { sendHTTPResponse(seq: number, payload: HTTPResponsePayload, body: Buffer) { const payloadBuffer = Buffer.from(JSON.stringify(payload)); - const buf = Buffer.alloc(2 + 2 + 4 + payloadBuffer.length + body.length); + const buf = Buffer.alloc(2 + 1 + 4 + payloadBuffer.length + body.length); let cursor = 0; - cursor += buf.writeUInt16BE(seq, cursor); - cursor += buf.writeUInt16BE(S2CRequestTypes.HTTPResponse, cursor); - cursor += buf.writeUInt32BE(payloadBuffer.length, cursor); + cursor = buf.writeUInt16BE(seq, cursor); + cursor = buf.writeUInt8(S2CRequestTypes.HTTPResponse, cursor); + cursor = buf.writeUInt32BE(payloadBuffer.length, cursor); cursor += payloadBuffer.copy(buf, cursor); body.copy(buf, cursor); this.send(buf);