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

lib/querystring: Add JSDoc Types #38185

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions lib/internal/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const isHexTable = new Int8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ... 256
]);

/**
* @param {string} str
* @param {Int8Array} noEscapeTable
* @param {string[]} hexTable
* @returns {string}
*/
function encodeStr(str, noEscapeTable, hexTable) {
const len = str.length;
if (len === 0)
Expand Down
71 changes: 60 additions & 11 deletions lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ const unhexTable = new Int8Array([
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 255
]);
// A safe fast alternative to decodeURIComponent
/**
* A safe fast alternative to decodeURIComponent
* @param {any} s
targos marked this conversation as resolved.
Show resolved Hide resolved
* @param {boolean} decodeSpaces
* @returns {string}
*/
function unescapeBuffer(s, decodeSpaces) {
const out = Buffer.allocUnsafe(s.length);
let index = 0;
Expand Down Expand Up @@ -119,7 +124,11 @@ function unescapeBuffer(s, decodeSpaces) {
return hasHex ? out.slice(0, outIndex) : out;
}


/**
* @param {string} s
* @param {boolean} decodeSpaces
* @returns {string}
*/
function qsUnescape(s, decodeSpaces) {
try {
return decodeURIComponent(s);
Expand All @@ -145,8 +154,13 @@ const noEscape = new Int8Array([
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, // 112 - 127
]);
// QueryString.escape() replaces encodeURIComponent()
// https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4

/**
* QueryString.escape() replaces encodeURIComponent()
* @see https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
* @param {any} str
* @returns {string}
*/
function qsEscape(str) {
if (typeof str !== 'string') {
if (typeof str === 'object')
Expand All @@ -158,6 +172,10 @@ function qsEscape(str) {
return encodeStr(str, noEscape, hexTable);
}

/**
* @param {string | number | BigInt | boolean} v
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @returns {string}
*/
function stringifyPrimitive(v) {
if (typeof v === 'string')
return v;
Expand All @@ -170,7 +188,11 @@ function stringifyPrimitive(v) {
return '';
}


/**
* @param {string | number | BigInt | boolean} v
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @param {(v: string) => string} encode
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @returns
bmeck marked this conversation as resolved.
Show resolved Hide resolved
*/
function encodeStringified(v, encode) {
if (typeof v === 'string')
return (v.length ? encode(v) : '');
Expand All @@ -186,12 +208,23 @@ function encodeStringified(v, encode) {
return '';
}


/**
* @param {string | number | boolean | null} v
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @param {(v: string) => string} encode
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @returns {string}
*/
function encodeStringifiedCustom(v, encode) {
return encode(stringifyPrimitive(v));
}


/**
*
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @param {Record<string, string | number | boolean | ReadonlyArray<string> | ReadonlyArray<number> | ReadonlyArray<boolean> | null>} obj
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} [sep]
* @param {string} [eq]
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @param {{ encodeURIComponent?: (v: string) => string }} [options]
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @returns {string}
*/
function stringify(obj, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
Expand Down Expand Up @@ -236,6 +269,10 @@ function stringify(obj, sep, eq, options) {
return '';
}

/**
* @param {string} str
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @returns {number[]}
*/
function charCodes(str) {
if (str.length === 0) return [];
if (str.length === 1) return [StringPrototypeCharCodeAt(str, 0)];
Expand Down Expand Up @@ -267,7 +304,14 @@ function addKeyVal(obj, key, value, keyEncoded, valEncoded, decode) {
}
}

// Parse a key/val string.
/**
* Parse a key/val string.
* @param {string} qs
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} [sep]
* @param {string} [eq]
bmeck marked this conversation as resolved.
Show resolved Hide resolved
Skn0tt marked this conversation as resolved.
Show resolved Hide resolved
* @param {{ maxKeys?: 1000; decodeURIComponent?(v: string): string; }} [options]
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Record<string, string | string[]>}
*/
function parse(qs, sep, eq, options) {
const obj = ObjectCreate(null);

Expand Down Expand Up @@ -421,9 +465,14 @@ function parse(qs, sep, eq, options) {
}


// v8 does not optimize functions with try-catch blocks, so we isolate them here
// to minimize the damage (Note: no longer true as of V8 5.4 -- but still will
// not be inlined).
/**
* v8 does not optimize functions with try-catch blocks, so we isolate them here
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* to minimize the damage (Note: no longer true as of V8 5.4 -- but still will
* not be inlined).
* @param {string} s
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @param {(v: string) => string} decoder
bmeck marked this conversation as resolved.
Show resolved Hide resolved
* @returns {string}
*/
function decodeStr(s, decoder) {
try {
return decoder(s);
Expand Down