fix error while constructing responses

"Response with null body status cannot have body"
This commit is contained in:
Spencer Pogorzelski 2023-08-14 15:05:46 -07:00
parent d449ed2e42
commit 52a55f616b
2 changed files with 15 additions and 11 deletions

View file

@ -9,13 +9,8 @@ import {
} from "bare-client-custom"; } from "bare-client-custom";
import { Connection } from "./Connection"; import { Connection } from "./Connection";
// export class Adrift { // https://fetch.spec.whatwg.org/#statuses
// bareclient:AdriftBareClient, const NULL_BODY_STATUSES = [101, 103, 204, 205, 304];
// constructor(connection:Connection){
//
// }
// }
//
export class AdriftBareClient extends Client { export class AdriftBareClient extends Client {
constructor(private connection: Connection) { constructor(private connection: Connection) {
@ -38,7 +33,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 { payload, body: respBody } = await this.connection.httprequest({ let { payload, body: respRawBody } = await this.connection.httprequest({
method, method,
requestHeaders, requestHeaders,
body, body,
@ -51,6 +46,18 @@ export class AdriftBareClient extends Client {
} }
} }
let respBody: ArrayBuffer | null = respRawBody;
if (
respBody.byteLength == 0 ||
NULL_BODY_STATUSES.includes(payload.status)
) {
respBody = null;
}
console.log("constructing Response", {
status: payload.status,
body: respBody?.byteLength,
});
return new Response(respBody, { return new Response(respBody, {
status: payload.status, status: payload.status,
statusText: payload.statusText, statusText: payload.statusText,

View file

@ -43,12 +43,9 @@ export class Connection {
let requestType = view.getUint8(cursor) as S2CRequestType; let requestType = view.getUint8(cursor) as S2CRequestType;
cursor += 1; cursor += 1;
console.log(requestID, requestType);
const msgText = () => new TextDecoder().decode(data.slice(cursor)); const msgText = () => new TextDecoder().decode(data.slice(cursor));
const msgJSON = () => JSON.parse(msgText()); const msgJSON = () => JSON.parse(msgText());
console.log({ requestType });
switch (requestType) { switch (requestType) {
case S2CRequestTypes.HTTPResponseStart: case S2CRequestTypes.HTTPResponseStart:
const payload = msgJSON(); const payload = msgJSON();