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

feat: broad improvments #33

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions package-lock.json

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

40 changes: 20 additions & 20 deletions src/lib/PetitioRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ export class PetitioRequest {
}

/**
* @param {*} key The query key to use for the URL query parameters.
* @param {*} value The value to set the query key to.
* @param {string} key The query key to use for the URL query parameters.
* @param {string} value The value to set the query key to.
* @example
* If you wish to make a query at https://example.com/index?query=parameter
* you can use `.query("query", "parameter")`.
*/
public query(key: string, value: any): this
/**
* @param {*} key An object of query keys and their respective values.
* @param {Record<string, any>} key An object of query keys and their respective values.
* @example
* If you wish to make multiple queries at once, you can use
* `.query({"keyOne": "hello", "keyTwo": "world!"})`.
Expand All @@ -136,8 +136,8 @@ export class PetitioRequest {
}

/**
* @param {*} relativePath A path to resolve relative to the current URL.
* @return {*} The request object for further composition.
* @param {string} relativePath A path to resolve relative to the current URL.
* @return {this} The request object for further composition.
* @example `https://example.org/hello/world` with `.path("../petitio")`
* would resolve to `https://example.org/hello/petitio`.
*/
Expand All @@ -148,8 +148,8 @@ export class PetitioRequest {
}

/**
* @param {*} controller A controller instance that handles aborting the request.
* @return {*} The request object for further composition.
* @param {AbortController | globalThis.AbortController} controller A controller instance that handles aborting the request.
* @return {this} The request object for further composition.
* @example
* ```ts
* const controller = new AbortController();
Expand All @@ -164,28 +164,28 @@ export class PetitioRequest {
}

/**
* @param {*} data The data to be set for the request body.
* @param {Buffer | string} data The data to be set for the request body.
*/
public body(data: Buffer | string): this
/**
* @param {*} data The data to be set for the request body.
* @param {*} sendAs If data is set to any object type value other than a
* @param {Record<string, any>} data The data to be set for the request body.
* @param {string = "json"} sendAs If data is set to any object type value other than a
* buffer or this is set to `json`, the `Content-Type` header will be set to
* `application/json` and the request data will be set to the stringified
* JSON form of the supplied data.
*/
public body(data: Record<string, any>, sendAs?: "json"): this
/**
* @param {*} data The data to be set for the request body.
* @param {*} sendAs If data is a string or a parsed object of query
* @param {ParsedUrlQueryInput} data The data to be set for the request body.
* @param {"form"} sendAs If data is a string or a parsed object of query
* parameters *AND* this is set to `form`, the `Content-Type` header will be
* set to `application/x-www-form-urlencoded` and the request data will be
* set to the URL encoded version of the query string.
*/
public body(data: ParsedUrlQueryInput | string, sendAs: "form"): this
/**
* @param {*} data The data to be set for the request body.
* @param {*} sendAs If data is a stream.Readable *AND* this is set to
* @param {Readable} data The data to be set for the request body.
* @param {"stream"} sendAs If data is a stream.Readable *AND* this is set to
* `stream`, the body will be sent as the stream with no modifications to
* it or the headers.
*/
Expand Down Expand Up @@ -228,12 +228,12 @@ export class PetitioRequest {
}

/**
* @param {*} header The encoded header name to set.
* @param {*} value The value to set the header to.
* @param {string} header The encoded header name to set.
* @param {string} value The value to set the header to.
*/
public header(header: string, value: string): this
/**
* @param {*} header An object of keys and values to set headers to.
* @param {Record<string, string>} header An object of keys and values to set headers to.
*/
public header(header: Record<string, string>): this
public header(header: string | Record<string, string>, value?: string): this {
Expand All @@ -252,7 +252,7 @@ export class PetitioRequest {
}

/**
* @param {*} method The HTTP method to change the request to.
* @param {HTTPMethod} method The HTTP method to change the request to.
* @return {*} The request object for further composition.
*/
public method(method: HTTPMethod): this {
Expand Down Expand Up @@ -359,8 +359,8 @@ export class PetitioRequest {
onData: (buff: Buffer) => (data[data.length] = buff),
onError: (err: Error) => reject(err),
onComplete: () => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare
if (this.keepDispatcher === false) void dispatcher.close();
// eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare, eqeqeq
if (this.keepDispatcher == false) void dispatcher.close();
res._addBody(data);
resolve(res);
},
Expand Down
44 changes: 26 additions & 18 deletions src/lib/PetitioResponse.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { assert } from "console";

/**
* @module PetitioResponse
*/
Expand All @@ -24,32 +26,38 @@ export class PetitioResponse {
/**
* 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.
* @param {Buffer[] | Uint8Array[]} chunks The body to set for the response.
* @return {void} In place operation with no return.
*/
public _addBody(chunks: Buffer[] | Uint8Array[]) {
const length = Number(this.headers["content-length"]) || undefined;
public _addBody(chunks: Buffer[] | Uint8Array[]): void {
// eslint-disable-next-line radix
const length = parseInt(this.headers["content-length"]) || undefined;
this.body = Buffer.concat(chunks, length);
}

/**
* @param {*} headers The headers to add. This is done by splitting the
* @param {Buffer[]} headers The headers to add. This is done by splitting the
* array into chunks of two, where the first value becomes the header and
* the latter becomes its value. This will also append values to the header
* as an array if it already exists.
* @return {*} In place operation with no return.
* @return {void} In place operation with no return.
*/
public _parseHeaders(headers: Buffer[]) {
const len = headers.length;
for (let idx = 1; idx < len; idx += 2) {
const key = headers[idx - 1].toString().toLowerCase();
const toA = headers[idx].toString();
public _parseHeaders(headers: Buffer[]): void {
// eslint-disable-next-line no-var
var len = headers.length;
// @ts-ignore - TypeScript isn't picking up that we are changing this to string[].
// eslint-disable-next-line no-plusplus
while (len--) headers[len] = headers[len].toString();
len = headers.length;
// eslint-disable-next-line vars-on-top, no-var
for (var idx = 1; idx < len; idx += 2) {
// @ts-ignore - TypeScript isn't picking up that we are changing this to string[].
const key = headers[idx - 1].toLowerCase();
const toA = headers[idx];
const val = this.headers[key];
// eslint-disable-next-line curly
if (val !== undefined) {
if (!Array.isArray(val)) this.headers[key] = [val, toA];
else val[val.length] = toA;
} else this.headers[key] = toA;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare, max-len, no-unused-expressions, max-statements-per-line, eqeqeq
if (val !== undefined) if (Array.isArray(val) == false) (this.headers[key] = [val, toA]); else (val[val.length] = toA);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strict equality isn't necessary here:

Suggested change
if (val !== undefined) if (Array.isArray(val) == false) (this.headers[key] = [val, toA]); else (val[val.length] = toA);
if (val != undefined) if (Array.isArray(val) == false) (this.headers[key] = [val, toA]); else (val[val.length] = toA);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tbnritzdoge didn't we work out that strict equality ended up being faster in this instance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought we found out the opposite

else this.headers[key] = toA;
}
}

Expand All @@ -71,8 +79,8 @@ export class PetitioResponse {
}

/**
* @return {*} The raw response body as a buffer.
*/
* @return {*} The raw response body as a buffer.
*/
public raw(): Buffer {
return this.body;
}
Expand Down