Skip to content

Commit

Permalink
fix(node): body utils should respect buffer view offset
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 5, 2024
1 parent e00b4c9 commit 5e4ec69
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
49 changes: 24 additions & 25 deletions src/node-utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const NodeRequestProxy = /* @__PURE__ */ (() =>

#abortSignal?: AbortController;
#hasBody: boolean | undefined;
#rawBody?: Promise<Uint8Array>;
#bodyBytes?: Promise<Uint8Array>;
#blobBody?: Promise<Blob>;
#formDataBody?: Promise<FormData>;
#jsonBody?: Promise<any>;
Expand Down Expand Up @@ -124,30 +124,29 @@ export const NodeRequestProxy = /* @__PURE__ */ (() =>
return this.#bodyStream;
}

arrayBuffer(): Promise<ArrayBuffer> {
if (!this.#rawBody) {
const _bodyStream = this.body;
return _bodyStream
? _readStream(_bodyStream).then((buff) => buff.buffer as ArrayBuffer)
: Promise.resolve(new ArrayBuffer(0));
}
return this.#rawBody.then((buff) => buff.buffer as ArrayBuffer);
}

bytes(): Promise<Uint8Array> {
if (!this.#rawBody) {
if (!this.#bodyBytes) {
const _bodyStream = this.body;
return _bodyStream
this.#bodyBytes = _bodyStream
? _readStream(_bodyStream)
: Promise.resolve(new Uint8Array());
}
return this.#rawBody;
return this.#bodyBytes;
}

arrayBuffer(): Promise<ArrayBuffer> {
return this.bytes().then((buff) => {
return buff.buffer.slice(
buff.byteOffset,
buff.byteOffset + buff.byteLength,
) as ArrayBuffer;
});
}

blob(): Promise<Blob> {
if (!this.#blobBody) {
this.#blobBody = this.arrayBuffer().then((buff) => {
return new Blob([buff], {
this.#blobBody = this.bytes().then((bytes) => {
return new Blob([bytes], {
type: this[kNodeReq].headers["content-type"],
});
});
Expand All @@ -164,6 +163,15 @@ export const NodeRequestProxy = /* @__PURE__ */ (() =>
return this.#formDataBody;
}

text(): Promise<string> {
if (!this.#textBody) {
this.#textBody = this.bytes().then((bytes) => {
return new TextDecoder().decode(bytes);
});
}
return this.#textBody;
}

json(): Promise<any> {
if (!this.#jsonBody) {
this.#jsonBody = this.text().then((txt) => {
Expand All @@ -173,15 +181,6 @@ export const NodeRequestProxy = /* @__PURE__ */ (() =>
return this.#jsonBody;
}

text(): Promise<string> {
if (!this.#textBody) {
this.#textBody = this.arrayBuffer().then((buff) => {
return new TextDecoder().decode(buff);
});
}
return this.#textBody;
}

get [Symbol.toStringTag]() {
return "Request";
}
Expand Down
2 changes: 1 addition & 1 deletion test/_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function addTests(
);
});

test.skipIf(opts?.runtime === "node")("POST works (text body)", async () => {
test("POST works (text body)", async () => {
const response = await fetch(url("/body/text"), {
method: "POST",
body: "hello world",
Expand Down

0 comments on commit 5e4ec69

Please sign in to comment.