@gjsify/querystring 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -3
- package/lib/esm/index.js +19 -21
- package/lib/types/error.d.ts +12 -0
- package/lib/types/index.d.ts +86 -0
- package/package.json +13 -19
- package/src/error.ts +2 -0
- package/src/index.spec.ts +568 -47
- package/src/index.ts +18 -14
- package/tsconfig.json +22 -10
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/cjs/error.js +0 -25
- package/lib/cjs/index.js +0 -1035
- package/test.gjs.js +0 -33178
- package/test.gjs.mjs +0 -35769
- package/test.gjs.mjs.meta.json +0 -1
- package/test.node.js +0 -231
- package/test.node.mjs +0 -353
- package/tsconfig.types.json +0 -8
package/README.md
CHANGED
|
@@ -1,7 +1,34 @@
|
|
|
1
1
|
# @gjsify/querystring
|
|
2
2
|
|
|
3
|
-
Node.js querystring module
|
|
3
|
+
GJS implementation of the Node.js `querystring` module. Provides parse and stringify.
|
|
4
|
+
|
|
5
|
+
Part of the [gjsify](https://github.com/gjsify/gjsify) project — Node.js and Web APIs for GJS (GNOME JavaScript).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @gjsify/querystring
|
|
11
|
+
# or
|
|
12
|
+
yarn add @gjsify/querystring
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { parse, stringify } from '@gjsify/querystring';
|
|
19
|
+
|
|
20
|
+
const obj = parse('foo=bar&baz=qux');
|
|
21
|
+
console.log(obj); // { foo: 'bar', baz: 'qux' }
|
|
22
|
+
|
|
23
|
+
const str = stringify({ name: 'test', value: '123' });
|
|
24
|
+
console.log(str); // 'name=test&value=123'
|
|
25
|
+
```
|
|
4
26
|
|
|
5
27
|
## Inspirations and credits
|
|
6
|
-
|
|
7
|
-
|
|
28
|
+
|
|
29
|
+
- https://github.com/SpainTrain/querystring-es3
|
|
30
|
+
- https://github.com/denoland/deno_std/blob/main/node/querystring.ts
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
MIT
|
package/lib/esm/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Buffer } from "buffer";
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
2
|
import { NodeURIError } from "./error.js";
|
|
3
3
|
const hexTable = new Array(256);
|
|
4
4
|
for (let i = 0; i < 256; ++i) {
|
|
@@ -6,23 +6,19 @@ for (let i = 0; i < 256; ++i) {
|
|
|
6
6
|
}
|
|
7
7
|
function encodeStr(str, noEscapeTable, hexTable2) {
|
|
8
8
|
const len = str.length;
|
|
9
|
-
if (len === 0)
|
|
10
|
-
return "";
|
|
9
|
+
if (len === 0) return "";
|
|
11
10
|
let out = "";
|
|
12
11
|
let lastPos = 0;
|
|
13
12
|
for (let i = 0; i < len; i++) {
|
|
14
13
|
let c = str.charCodeAt(i);
|
|
15
14
|
if (c < 128) {
|
|
16
|
-
if (noEscapeTable[c] === 1)
|
|
17
|
-
|
|
18
|
-
if (lastPos < i)
|
|
19
|
-
out += str.slice(lastPos, i);
|
|
15
|
+
if (noEscapeTable[c] === 1) continue;
|
|
16
|
+
if (lastPos < i) out += str.slice(lastPos, i);
|
|
20
17
|
lastPos = i + 1;
|
|
21
18
|
out += hexTable2[c];
|
|
22
19
|
continue;
|
|
23
20
|
}
|
|
24
|
-
if (lastPos < i)
|
|
25
|
-
out += str.slice(lastPos, i);
|
|
21
|
+
if (lastPos < i) out += str.slice(lastPos, i);
|
|
26
22
|
if (c < 2048) {
|
|
27
23
|
lastPos = i + 1;
|
|
28
24
|
out += hexTable2[192 | c >> 6] + hexTable2[128 | c & 63];
|
|
@@ -34,17 +30,14 @@ function encodeStr(str, noEscapeTable, hexTable2) {
|
|
|
34
30
|
continue;
|
|
35
31
|
}
|
|
36
32
|
++i;
|
|
37
|
-
if (i >= len)
|
|
38
|
-
throw new ERR_INVALID_URI();
|
|
33
|
+
if (i >= len) throw new ERR_INVALID_URI();
|
|
39
34
|
const c2 = str.charCodeAt(i) & 1023;
|
|
40
35
|
lastPos = i + 1;
|
|
41
36
|
c = 65536 + ((c & 1023) << 10 | c2);
|
|
42
37
|
out += hexTable2[240 | c >> 18] + hexTable2[128 | c >> 12 & 63] + hexTable2[128 | c >> 6 & 63] + hexTable2[128 | c & 63];
|
|
43
38
|
}
|
|
44
|
-
if (lastPos === 0)
|
|
45
|
-
|
|
46
|
-
if (lastPos < len)
|
|
47
|
-
return out + str.slice(lastPos);
|
|
39
|
+
if (lastPos === 0) return str;
|
|
40
|
+
if (lastPos < len) return out + str.slice(lastPos);
|
|
48
41
|
return out;
|
|
49
42
|
}
|
|
50
43
|
class ERR_INVALID_URI extends NodeURIError {
|
|
@@ -342,10 +335,16 @@ function charCodes(str) {
|
|
|
342
335
|
}
|
|
343
336
|
function addKeyVal(obj, key, value, keyEncoded, valEncoded, decode2) {
|
|
344
337
|
if (key.length > 0 && keyEncoded) {
|
|
345
|
-
|
|
338
|
+
try {
|
|
339
|
+
key = decode2(key);
|
|
340
|
+
} catch {
|
|
341
|
+
}
|
|
346
342
|
}
|
|
347
343
|
if (value.length > 0 && valEncoded) {
|
|
348
|
-
|
|
344
|
+
try {
|
|
345
|
+
value = decode2(value);
|
|
346
|
+
} catch {
|
|
347
|
+
}
|
|
349
348
|
}
|
|
350
349
|
if (obj[key] === void 0) {
|
|
351
350
|
obj[key] = value;
|
|
@@ -673,8 +672,7 @@ function stringify(obj, sep, eq, options) {
|
|
|
673
672
|
ks += eq;
|
|
674
673
|
if (Array.isArray(v)) {
|
|
675
674
|
const vlen = v.length;
|
|
676
|
-
if (vlen === 0)
|
|
677
|
-
continue;
|
|
675
|
+
if (vlen === 0) continue;
|
|
678
676
|
if (fields) {
|
|
679
677
|
fields += sep;
|
|
680
678
|
}
|
|
@@ -1013,7 +1011,7 @@ function qsUnescape(s) {
|
|
|
1013
1011
|
}
|
|
1014
1012
|
}
|
|
1015
1013
|
const unescape = qsUnescape;
|
|
1016
|
-
var
|
|
1014
|
+
var index_default = {
|
|
1017
1015
|
parse,
|
|
1018
1016
|
stringify,
|
|
1019
1017
|
decode,
|
|
@@ -1025,7 +1023,7 @@ var src_default = {
|
|
|
1025
1023
|
export {
|
|
1026
1024
|
ERR_INVALID_URI,
|
|
1027
1025
|
decode,
|
|
1028
|
-
|
|
1026
|
+
index_default as default,
|
|
1029
1027
|
encode,
|
|
1030
1028
|
escape,
|
|
1031
1029
|
parse,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All error instances in Node have additional methods and properties
|
|
3
|
+
* This export class is meant to be extended by these instances abstracting native JS error instances
|
|
4
|
+
*/
|
|
5
|
+
export declare class NodeErrorAbstraction extends Error {
|
|
6
|
+
code: string;
|
|
7
|
+
constructor(name: string, code: string, message: string);
|
|
8
|
+
toString(): string;
|
|
9
|
+
}
|
|
10
|
+
export declare class NodeURIError extends NodeErrorAbstraction implements URIError {
|
|
11
|
+
constructor(code: string, message: string);
|
|
12
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { ParsedUrlQuery } from 'node:querystring';
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
import { NodeURIError } from "./error.js";
|
|
4
|
+
export declare class ERR_INVALID_URI extends NodeURIError {
|
|
5
|
+
constructor();
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Alias of querystring.parse()
|
|
9
|
+
* @legacy
|
|
10
|
+
*/
|
|
11
|
+
export declare const decode: typeof parse;
|
|
12
|
+
/**
|
|
13
|
+
* Alias of querystring.stringify()
|
|
14
|
+
* @legacy
|
|
15
|
+
*/
|
|
16
|
+
export declare const encode: typeof stringify;
|
|
17
|
+
/**
|
|
18
|
+
* replaces encodeURIComponent()
|
|
19
|
+
* @see https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
|
|
20
|
+
*/
|
|
21
|
+
declare function qsEscape(str: unknown): string;
|
|
22
|
+
/**
|
|
23
|
+
* Performs URL percent-encoding on the given `str` in a manner that is optimized for the specific requirements of URL query strings.
|
|
24
|
+
* Used by `querystring.stringify()` and is generally not expected to be used directly.
|
|
25
|
+
* It is exported primarily to allow application code to provide a replacement percent-encoding implementation if necessary by assigning `querystring.escape` to an alternative function.
|
|
26
|
+
* @legacy
|
|
27
|
+
* @see Tested in `test-querystring-escape.js`
|
|
28
|
+
*/
|
|
29
|
+
export declare const escape: typeof qsEscape;
|
|
30
|
+
export type { ParsedUrlQuery };
|
|
31
|
+
interface ParseOptions {
|
|
32
|
+
/** The function to use when decoding percent-encoded characters in the query string. */
|
|
33
|
+
decodeURIComponent?: (string: string) => string;
|
|
34
|
+
/** Specifies the maximum number of keys to parse. */
|
|
35
|
+
maxKeys?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parses a URL query string into a collection of key and value pairs.
|
|
39
|
+
* @param str The URL query string to parse
|
|
40
|
+
* @param sep The substring used to delimit key and value pairs in the query string. Default: '&'.
|
|
41
|
+
* @param eq The substring used to delimit keys and values in the query string. Default: '='.
|
|
42
|
+
* @param options The parse options
|
|
43
|
+
* @param options.decodeURIComponent The function to use when decoding percent-encoded characters in the query string. Default: `querystring.unescape()`.
|
|
44
|
+
* @param options.maxKeys Specifies the maximum number of keys to parse. Specify `0` to remove key counting limitations. Default: `1000`.
|
|
45
|
+
* @legacy
|
|
46
|
+
* @see Tested in test-querystring.js
|
|
47
|
+
*/
|
|
48
|
+
export declare function parse(str: string, sep?: string, eq?: string, { decodeURIComponent, maxKeys }?: ParseOptions): ParsedUrlQuery;
|
|
49
|
+
interface StringifyOptions {
|
|
50
|
+
/** The function to use when converting URL-unsafe characters to percent-encoding in the query string. */
|
|
51
|
+
encodeURIComponent: (string: string) => string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Produces a URL query string from a given obj by iterating through the object's "own properties".
|
|
55
|
+
* @param obj The object to serialize into a URL query string.
|
|
56
|
+
* @param sep The substring used to delimit key and value pairs in the query string. Default: '&'.
|
|
57
|
+
* @param eq The substring used to delimit keys and values in the query string. Default: '='.
|
|
58
|
+
* @param options The stringify options
|
|
59
|
+
* @param options.encodeURIComponent The function to use when converting URL-unsafe characters to percent-encoding in the query string. Default: `querystring.escape()`.
|
|
60
|
+
* @legacy
|
|
61
|
+
* @see Tested in `test-querystring.js`
|
|
62
|
+
*/
|
|
63
|
+
export declare function stringify(obj: Record<string, unknown>, sep?: string, eq?: string, options?: StringifyOptions): string;
|
|
64
|
+
/**
|
|
65
|
+
* A safe fast alternative to decodeURIComponent
|
|
66
|
+
*/
|
|
67
|
+
export declare function unescapeBuffer(s: string, decodeSpaces?: boolean): Buffer;
|
|
68
|
+
declare function qsUnescape(s: string): string;
|
|
69
|
+
/**
|
|
70
|
+
* Performs decoding of URL percent-encoded characters on the given `str`.
|
|
71
|
+
* Used by `querystring.parse()` and is generally not expected to be used directly.
|
|
72
|
+
* It is exported primarily to allow application code to provide a replacement decoding implementation if necessary by assigning `querystring.unescape` to an alternative function.
|
|
73
|
+
* @legacy
|
|
74
|
+
* @see Tested in `test-querystring-escape.js`
|
|
75
|
+
*/
|
|
76
|
+
export declare const unescape: typeof qsUnescape;
|
|
77
|
+
declare const _default: {
|
|
78
|
+
parse: typeof parse;
|
|
79
|
+
stringify: typeof stringify;
|
|
80
|
+
decode: typeof parse;
|
|
81
|
+
encode: typeof stringify;
|
|
82
|
+
unescape: typeof qsUnescape;
|
|
83
|
+
escape: typeof qsEscape;
|
|
84
|
+
unescapeBuffer: typeof unescapeBuffer;
|
|
85
|
+
};
|
|
86
|
+
export default _default;
|
package/package.json
CHANGED
|
@@ -1,31 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/querystring",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Node.js querystring module for Gjs",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "lib/cjs/index.js",
|
|
7
6
|
"module": "lib/esm/index.js",
|
|
7
|
+
"types": "lib/types/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
"default": "./lib/esm/index.js"
|
|
13
|
-
},
|
|
14
|
-
"require": {
|
|
15
|
-
"types": "./lib/types/index.d.ts",
|
|
16
|
-
"default": "./lib/cjs/index.js"
|
|
17
|
-
}
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"default": "./lib/esm/index.js"
|
|
18
12
|
}
|
|
19
13
|
},
|
|
20
14
|
"scripts": {
|
|
21
|
-
"clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
|
|
22
|
-
"
|
|
23
|
-
"build": "yarn
|
|
15
|
+
"clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
|
|
16
|
+
"check": "tsc --noEmit",
|
|
17
|
+
"build": "yarn build:gjsify && yarn build:types",
|
|
24
18
|
"build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
|
|
19
|
+
"build:types": "tsc",
|
|
25
20
|
"build:test": "yarn build:test:gjs && yarn build:test:node",
|
|
26
21
|
"build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
|
|
27
22
|
"build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
|
|
28
|
-
"test": "yarn
|
|
23
|
+
"test": "yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
|
|
29
24
|
"test:gjs": "gjs -m test.gjs.mjs",
|
|
30
25
|
"test:node": "node test.node.mjs"
|
|
31
26
|
},
|
|
@@ -35,10 +30,9 @@
|
|
|
35
30
|
"fs"
|
|
36
31
|
],
|
|
37
32
|
"devDependencies": {
|
|
38
|
-
"@gjsify/cli": "^0.0
|
|
39
|
-
"@gjsify/
|
|
40
|
-
"@
|
|
41
|
-
"
|
|
42
|
-
"@types/node": "^20.10.5"
|
|
33
|
+
"@gjsify/cli": "^0.1.0",
|
|
34
|
+
"@gjsify/unit": "^0.1.0",
|
|
35
|
+
"@types/node": "^25.5.0",
|
|
36
|
+
"typescript": "^6.0.2"
|
|
43
37
|
}
|
|
44
38
|
}
|