Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve response buffer handling #20

Merged
merged 3 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/lib/PetitioRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

// @ts-expect-error 7016 - Unusual type exports
import Client from "undici/lib/core/client";
import type ClientType from "undici/types/client"; // eslint-disable-line node/no-missing-import
// eslint-disable-next-line node/no-missing-import
import type ClientType from "undici/types/client";
import type { IncomingHttpHeaders } from "http";
import type { ParsedUrlQueryInput } from "querystring";
import { PetitioResponse } from "./PetitioResponse";
Expand Down Expand Up @@ -297,14 +298,16 @@ export class PetitioRequest {
const client = this.kClient ?? new Client(this.url.origin, this.coreOptions);

const res: PetitioResponse = new PetitioResponse();
const data: Uint8Array[] | Buffer[] = [];

client.dispatch(options, {
onData: (data: Buffer) => {
return res._addChunk(data);
onData: (buff: Buffer) => {
return data.push(buff);
},
onError: (err: Error) => reject(err),
onComplete: () => {
if (!this.keepClient) client.close();
res._addBody(data);
resolve(res);
},
onConnect: () => null,
Expand Down
16 changes: 8 additions & 8 deletions src/lib/PetitioResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
export class PetitioResponse {
/**
* The response body received from the server.
* This is updated in chunks through [[PetitioResponse._addChunk]], either
* This is updated through [[PetitioResponse._addBody]], either
* from [[PetitioRequest.send]] or directly on a response object from
* another source.
*/
public body: Buffer = Buffer.alloc(0);
public body!: Buffer;
/**
* The response headers received from the server.
* This is updated through [[PetitioResponse._parseHeaders]].
Expand All @@ -23,14 +23,14 @@ export class PetitioResponse {
public statusCode: number | null = null;

/**
* This appends data to the body, dynamically reallocating the buffer size
* as chunks are added. Therefore, this is currently unsuitable for handling
* large responses, as the exact size is allocated in memory as a buffer.
* @param {*} chunk The chunk of data to append to the body.
* This takes the data chunks and creates a Buffer, and it sets
* that buffer as the body.
* @param {*} chunks The body to set for the response.
* @return {*} In place operation with no return.
*/
public _addChunk(chunk: Buffer | Uint8Array) {
this.body = Buffer.concat([this.body, chunk]);
public _addBody(chunks: Buffer[] | Uint8Array[]) {
const length = this.headers["content-length"];
this.body = Buffer.concat(chunks, length ? Number(length) : undefined);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/PetitioRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("PetitioRequest", () => {
const bodyString2 = qs.stringify(body);
const bodyBuffer = Buffer.from(bodyString);

// eslint-disable-next-line node/no-unsupported-features/node-builtins
// eslint-disable-next-line
tbnritzdoge marked this conversation as resolved.
Show resolved Hide resolved
const bodyStream = Readable.from(text, {objectMode: false});

test("CHECK THAT passed body MATCH RECIEVED stringified body", () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/PetitioResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("PetitioResponse", () => {

test("ADDING BUFFER TO RESPONSE", () => {
const res = new PetitioResponse();
res._addChunk(buffer);
res._addBody([buffer]);

const final = res.json();

Expand All @@ -56,7 +56,7 @@ describe("PetitioResponse", () => {
const buffer = Buffer.from(text);
test("TEXT", () => {
const res = new PetitioResponse();
res._addChunk(buffer);
res._addBody([buffer]);

const final = res.text();

Expand All @@ -70,7 +70,7 @@ describe("PetitioResponse", () => {
const encoded = buffer.toString("base64");
test("BASE64", () => {
const res = new PetitioResponse();
res._addChunk(buffer);
res._addBody([buffer]);

const final = res.text("base64");

Expand Down