mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-13 06:10:01 -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 });
|
console.log({ body });
|
||||||
throw new Error("bare-client-custom passed an unexpected body type");
|
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,
|
method,
|
||||||
requestHeaders,
|
requestHeaders,
|
||||||
body,
|
body,
|
||||||
|
@ -45,8 +45,18 @@ export class AdriftBareClient extends Client {
|
||||||
cache,
|
cache,
|
||||||
duplex,
|
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(
|
connect(
|
||||||
remote: URL,
|
remote: URL,
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {
|
||||||
C2SRequestType,
|
C2SRequestType,
|
||||||
C2SRequestTypes,
|
C2SRequestTypes,
|
||||||
HTTPRequestPayload,
|
HTTPRequestPayload,
|
||||||
|
HTTPResponsePayload,
|
||||||
S2CRequestType,
|
S2CRequestType,
|
||||||
S2CRequestTypes,
|
S2CRequestTypes,
|
||||||
} from "../protocol";
|
} from "../protocol";
|
||||||
|
@ -29,14 +30,17 @@ export default class Connection {
|
||||||
|
|
||||||
switch (requestType) {
|
switch (requestType) {
|
||||||
case S2CRequestTypes.HTTPResponse: {
|
case S2CRequestTypes.HTTPResponse: {
|
||||||
let decoder = new TextDecoder();
|
const payloadLen = view.getUint32(cursor);
|
||||||
let text = decoder.decode(data.slice(cursor));
|
cursor += 4;
|
||||||
console.log(text);
|
const decoder = new TextDecoder();
|
||||||
let json = JSON.parse(text);
|
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]({ payload, body: data.slice(cursor) });
|
||||||
|
|
||||||
this.callbacks[requestID](json);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,7 +67,9 @@ export default class Connection {
|
||||||
console.log(buf);
|
console.log(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
httprequest(data: HTTPRequestPayload): Promise<object> {
|
httprequest(
|
||||||
|
data: HTTPRequestPayload
|
||||||
|
): Promise<{ payload: HTTPResponsePayload; body: ArrayBuffer }> {
|
||||||
let json = JSON.stringify(data);
|
let json = JSON.stringify(data);
|
||||||
|
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve) => {
|
||||||
|
|
|
@ -498,11 +498,11 @@ class Client {
|
||||||
|
|
||||||
sendHTTPResponse(seq: number, payload: HTTPResponsePayload, body: Buffer) {
|
sendHTTPResponse(seq: number, payload: HTTPResponsePayload, body: Buffer) {
|
||||||
const payloadBuffer = Buffer.from(JSON.stringify(payload));
|
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;
|
let cursor = 0;
|
||||||
cursor += buf.writeUInt16BE(seq, cursor);
|
cursor = buf.writeUInt16BE(seq, cursor);
|
||||||
cursor += buf.writeUInt16BE(S2CRequestTypes.HTTPResponse, cursor);
|
cursor = buf.writeUInt8(S2CRequestTypes.HTTPResponse, cursor);
|
||||||
cursor += buf.writeUInt32BE(payloadBuffer.length, cursor);
|
cursor = buf.writeUInt32BE(payloadBuffer.length, cursor);
|
||||||
cursor += payloadBuffer.copy(buf, cursor);
|
cursor += payloadBuffer.copy(buf, cursor);
|
||||||
body.copy(buf, cursor);
|
body.copy(buf, cursor);
|
||||||
this.send(buf);
|
this.send(buf);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue