mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-12 22:00:02 -04:00
WORKING
This commit is contained in:
parent
bcaf437fd3
commit
e398dc2bc6
3 changed files with 30 additions and 14 deletions
|
@ -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,
|
||||
|
|
|
@ -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<object> {
|
||||
httprequest(
|
||||
data: HTTPRequestPayload
|
||||
): Promise<{ payload: HTTPResponsePayload; body: ArrayBuffer }> {
|
||||
let json = JSON.stringify(data);
|
||||
|
||||
return new Promise(async (resolve) => {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue