@serveryyswys/node.operit 0.0.0-stage → 0.1.2

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.
Files changed (123) hide show
  1. package/BUILTINS.json +233 -0
  2. package/DESIGN.md +513 -0
  3. package/LICENSE +674 -0
  4. package/README.md +301 -2
  5. package/dist/assert/strict.d.ts +2 -0
  6. package/dist/assert/strict.js +7 -0
  7. package/dist/assert.d.ts +3 -0
  8. package/dist/assert.js +483 -0
  9. package/dist/buffer.d.ts +60 -0
  10. package/dist/buffer.js +524 -0
  11. package/dist/child_process.d.ts +56 -0
  12. package/dist/child_process.js +371 -0
  13. package/dist/constants.d.ts +2 -0
  14. package/dist/constants.js +19 -0
  15. package/dist/crypto.d.ts +76 -0
  16. package/dist/crypto.js +312 -0
  17. package/dist/dns/promises.d.ts +34 -0
  18. package/dist/dns/promises.js +190 -0
  19. package/dist/dns.d.ts +88 -0
  20. package/dist/dns.js +259 -0
  21. package/dist/events.d.ts +40 -0
  22. package/dist/events.js +237 -0
  23. package/dist/fs/promises.d.ts +159 -0
  24. package/dist/fs/promises.js +129 -0
  25. package/dist/fs.d.ts +221 -0
  26. package/dist/fs.js +944 -0
  27. package/dist/http.d.ts +92 -0
  28. package/dist/http.js +319 -0
  29. package/dist/https.d.ts +296 -0
  30. package/dist/https.js +50 -0
  31. package/dist/index.d.ts +2422 -0
  32. package/dist/index.js +57 -0
  33. package/dist/lib/bridge.d.ts +60 -0
  34. package/dist/lib/bridge.js +205 -0
  35. package/dist/lib/bytes.d.ts +20 -0
  36. package/dist/lib/bytes.js +239 -0
  37. package/dist/lib/child-process-types.d.ts +45 -0
  38. package/dist/lib/child-process-types.js +2 -0
  39. package/dist/lib/cwd.d.ts +2 -0
  40. package/dist/lib/cwd.js +32 -0
  41. package/dist/lib/deep-equal.d.ts +3 -0
  42. package/dist/lib/deep-equal.js +223 -0
  43. package/dist/lib/deflate.d.ts +2 -0
  44. package/dist/lib/deflate.js +234 -0
  45. package/dist/lib/dns-driver.d.ts +14 -0
  46. package/dist/lib/dns-driver.js +85 -0
  47. package/dist/lib/dns-types.d.ts +22 -0
  48. package/dist/lib/dns-types.js +2 -0
  49. package/dist/lib/errors.d.ts +48 -0
  50. package/dist/lib/errors.js +424 -0
  51. package/dist/lib/fs-driver.d.ts +70 -0
  52. package/dist/lib/fs-driver.js +364 -0
  53. package/dist/lib/fs-env.d.ts +30 -0
  54. package/dist/lib/fs-env.js +157 -0
  55. package/dist/lib/fs-types.d.ts +19 -0
  56. package/dist/lib/fs-types.js +2 -0
  57. package/dist/lib/hashes.d.ts +27 -0
  58. package/dist/lib/hashes.js +432 -0
  59. package/dist/lib/http-driver.d.ts +30 -0
  60. package/dist/lib/http-driver.js +72 -0
  61. package/dist/lib/http-types.d.ts +18 -0
  62. package/dist/lib/http-types.js +2 -0
  63. package/dist/lib/inflate.d.ts +6 -0
  64. package/dist/lib/inflate.js +284 -0
  65. package/dist/lib/path-types.d.ts +37 -0
  66. package/dist/lib/path-types.js +2 -0
  67. package/dist/lib/paths.d.ts +8 -0
  68. package/dist/lib/paths.js +106 -0
  69. package/dist/lib/perf-hooks-types.d.ts +18 -0
  70. package/dist/lib/perf-hooks-types.js +2 -0
  71. package/dist/lib/process-driver.d.ts +31 -0
  72. package/dist/lib/process-driver.js +102 -0
  73. package/dist/lib/punycode.d.ts +8 -0
  74. package/dist/lib/punycode.js +179 -0
  75. package/dist/lib/stream-types.d.ts +29 -0
  76. package/dist/lib/stream-types.js +2 -0
  77. package/dist/lib/timers-types.d.ts +18 -0
  78. package/dist/lib/timers-types.js +2 -0
  79. package/dist/lib/url-search-params.d.ts +26 -0
  80. package/dist/lib/url-search-params.js +281 -0
  81. package/dist/lib/url-types.d.ts +44 -0
  82. package/dist/lib/url-types.js +2 -0
  83. package/dist/lib/url-whatwg.d.ts +78 -0
  84. package/dist/lib/url-whatwg.js +857 -0
  85. package/dist/lib/zlib-types.d.ts +12 -0
  86. package/dist/lib/zlib-types.js +2 -0
  87. package/dist/module.d.ts +8 -0
  88. package/dist/module.js +72 -0
  89. package/dist/os.d.ts +80 -0
  90. package/dist/os.js +247 -0
  91. package/dist/path/posix.d.ts +2 -0
  92. package/dist/path/posix.js +7 -0
  93. package/dist/path/win32.d.ts +2 -0
  94. package/dist/path/win32.js +10 -0
  95. package/dist/path.d.ts +3 -0
  96. package/dist/path.js +1222 -0
  97. package/dist/perf_hooks.d.ts +36 -0
  98. package/dist/perf_hooks.js +144 -0
  99. package/dist/process.d.ts +69 -0
  100. package/dist/process.js +230 -0
  101. package/dist/querystring.d.ts +15 -0
  102. package/dist/querystring.js +209 -0
  103. package/dist/stream/promises.d.ts +5 -0
  104. package/dist/stream/promises.js +10 -0
  105. package/dist/stream.d.ts +84 -0
  106. package/dist/stream.js +875 -0
  107. package/dist/string_decoder.d.ts +15 -0
  108. package/dist/string_decoder.js +237 -0
  109. package/dist/timers/promises.d.ts +19 -0
  110. package/dist/timers/promises.js +74 -0
  111. package/dist/timers.d.ts +21 -0
  112. package/dist/timers.js +41 -0
  113. package/dist/tty.d.ts +9 -0
  114. package/dist/tty.js +25 -0
  115. package/dist/unsupported.d.ts +12 -0
  116. package/dist/unsupported.js +17 -0
  117. package/dist/url.d.ts +28 -0
  118. package/dist/url.js +486 -0
  119. package/dist/util.d.ts +74 -0
  120. package/dist/util.js +515 -0
  121. package/dist/zlib.d.ts +88 -0
  122. package/dist/zlib.js +371 -0
  123. package/package.json +29 -4
@@ -0,0 +1,179 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encode = encode;
4
+ exports.decode = decode;
5
+ exports.toASCII = toASCII;
6
+ exports.toUnicode = toUnicode;
7
+ /**
8
+ * RFC 3492(Punycode)编解码。
9
+ *
10
+ * url.parse 会把非 ASCII 主机名转成 xn-- 形式(Node 用 domainToASCII),
11
+ * 所以这里是 url 模块的前置件。
12
+ *
13
+ * 范围说明:只做 punycode 本体,不做 UTS-46 / IDNA2008 的那套映射
14
+ * (大小写折叠、NFKC 归一化、禁用字符检查)。对常见域名足够;
15
+ * `faß.de` 这类需要 UTS-46 特殊处理的域名结果可能与 Node 不同,见 BUILTINS.json。
16
+ */
17
+ const BASE = 36;
18
+ const TMIN = 1;
19
+ const TMAX = 26;
20
+ const SKEW = 38;
21
+ const DAMP = 700;
22
+ const INITIAL_BIAS = 72;
23
+ const INITIAL_N = 128;
24
+ function adapt(delta, numPoints, firstTime) {
25
+ let value = firstTime ? Math.floor(delta / DAMP) : delta >> 1;
26
+ value += Math.floor(value / numPoints);
27
+ let k = 0;
28
+ while (value > ((BASE - TMIN) * TMAX) >> 1) {
29
+ value = Math.floor(value / (BASE - TMIN));
30
+ k += BASE;
31
+ }
32
+ return k + Math.floor(((BASE - TMIN + 1) * value) / (value + SKEW));
33
+ }
34
+ function encodeDigit(value) {
35
+ // 0-25 -> a-z, 26-35 -> 0-9
36
+ return value < 26 ? String.fromCharCode(97 + value) : String.fromCharCode(22 + value);
37
+ }
38
+ function decodeDigit(code) {
39
+ if (code >= 48 && code <= 57) {
40
+ return code - 22;
41
+ }
42
+ if (code >= 65 && code <= 90) {
43
+ return code - 65;
44
+ }
45
+ if (code >= 97 && code <= 122) {
46
+ return code - 97;
47
+ }
48
+ return BASE;
49
+ }
50
+ /** 把一段 Unicode 字符串编成 punycode(不含 xn-- 前缀)。 */
51
+ function encode(input) {
52
+ const codePoints = [];
53
+ for (const character of input) {
54
+ codePoints.push(character.codePointAt(0));
55
+ }
56
+ const output = [];
57
+ let n = INITIAL_N;
58
+ let delta = 0;
59
+ let bias = INITIAL_BIAS;
60
+ for (let i = 0; i < codePoints.length; i += 1) {
61
+ if (codePoints[i] < 0x80) {
62
+ output.push(String.fromCharCode(codePoints[i]));
63
+ }
64
+ }
65
+ const basicLength = output.length;
66
+ let handled = basicLength;
67
+ if (basicLength > 0) {
68
+ output.push('-');
69
+ }
70
+ while (handled < codePoints.length) {
71
+ let next = Number.MAX_SAFE_INTEGER;
72
+ for (let i = 0; i < codePoints.length; i += 1) {
73
+ if (codePoints[i] >= n && codePoints[i] < next) {
74
+ next = codePoints[i];
75
+ }
76
+ }
77
+ delta += (next - n) * (handled + 1);
78
+ n = next;
79
+ for (let i = 0; i < codePoints.length; i += 1) {
80
+ if (codePoints[i] < n) {
81
+ delta += 1;
82
+ }
83
+ if (codePoints[i] === n) {
84
+ let q = delta;
85
+ for (let k = BASE;; k += BASE) {
86
+ const threshold = k <= bias ? TMIN : (k >= bias + TMAX ? TMAX : k - bias);
87
+ if (q < threshold) {
88
+ break;
89
+ }
90
+ output.push(encodeDigit(threshold + ((q - threshold) % (BASE - threshold))));
91
+ q = Math.floor((q - threshold) / (BASE - threshold));
92
+ }
93
+ output.push(encodeDigit(q));
94
+ bias = adapt(delta, handled + 1, handled === basicLength);
95
+ delta = 0;
96
+ handled += 1;
97
+ }
98
+ }
99
+ delta += 1;
100
+ n += 1;
101
+ }
102
+ return output.join('');
103
+ }
104
+ /** 把 punycode(不含 xn-- 前缀)解回 Unicode 字符串。非法输入抛错。 */
105
+ function decode(input) {
106
+ const output = [];
107
+ let n = INITIAL_N;
108
+ let index = 0;
109
+ let bias = INITIAL_BIAS;
110
+ let basic = input.lastIndexOf('-');
111
+ if (basic < 0) {
112
+ basic = 0;
113
+ }
114
+ for (let i = 0; i < basic; i += 1) {
115
+ if (input.charCodeAt(i) >= 0x80) {
116
+ throw new Error('Invalid input: non-basic code point in the basic segment');
117
+ }
118
+ output.push(input.charCodeAt(i));
119
+ }
120
+ let cursor = basic > 0 ? basic + 1 : 0;
121
+ while (cursor < input.length) {
122
+ const oldIndex = index;
123
+ let weight = 1;
124
+ for (let k = BASE;; k += BASE) {
125
+ if (cursor >= input.length) {
126
+ throw new Error('Invalid input: truncated punycode');
127
+ }
128
+ const digit = decodeDigit(input.charCodeAt(cursor));
129
+ cursor += 1;
130
+ if (digit >= BASE) {
131
+ throw new Error('Invalid input: bad digit');
132
+ }
133
+ index += digit * weight;
134
+ const threshold = k <= bias ? TMIN : (k >= bias + TMAX ? TMAX : k - bias);
135
+ if (digit < threshold) {
136
+ break;
137
+ }
138
+ weight *= BASE - threshold;
139
+ }
140
+ const length = output.length + 1;
141
+ bias = adapt(index - oldIndex, length, oldIndex === 0);
142
+ n += Math.floor(index / length);
143
+ index %= length;
144
+ output.splice(index, 0, n);
145
+ index += 1;
146
+ }
147
+ let out = '';
148
+ for (let i = 0; i < output.length; i += 1) {
149
+ out += String.fromCodePoint(output[i]);
150
+ }
151
+ return out;
152
+ }
153
+ /** 域名 -> ASCII(xn--)。纯 ASCII 输入原样返回(已小写化由调用方负责)。 */
154
+ function toASCII(domain) {
155
+ return domain.split('.').map(function (label) {
156
+ let ascii = true;
157
+ for (let i = 0; i < label.length; i += 1) {
158
+ if (label.charCodeAt(i) >= 0x80) {
159
+ ascii = false;
160
+ break;
161
+ }
162
+ }
163
+ return ascii ? label : 'xn--' + encode(label.toLowerCase());
164
+ }).join('.');
165
+ }
166
+ /** 域名 -> Unicode。xn-- 标签解回 Unicode,其余原样。 */
167
+ function toUnicode(domain) {
168
+ return domain.split('.').map(function (label) {
169
+ if (label.length > 4 && label.slice(0, 4).toLowerCase() === 'xn--') {
170
+ try {
171
+ return decode(label.slice(4));
172
+ }
173
+ catch (failure) {
174
+ return label;
175
+ }
176
+ }
177
+ return label;
178
+ }).join('.');
179
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * stream 的公开类型。
3
+ *
4
+ * 独立成文件的原因与 path-types / zlib-types 相同:stream.ts 用 `export =` 导出模块本体,
5
+ * 无法再导出类型,而 index.ts 聚合时要求这些名字是"可命名"的。
6
+ */
7
+ /** 只有可写侧被用到的结构类型:pipe 的终点不需要是完整的 Writable。 */
8
+ export interface WritableLike {
9
+ write(chunk: unknown, encoding?: unknown, callback?: unknown): boolean;
10
+ end(chunk?: unknown, encoding?: unknown, callback?: unknown): void;
11
+ on(event: string, listener: (...args: unknown[]) => void): unknown;
12
+ once(event: string, listener: (...args: unknown[]) => void): unknown;
13
+ removeListener(event: string, listener: (...args: unknown[]) => void): unknown;
14
+ emit(event: string, ...args: unknown[]): boolean;
15
+ readonly writable?: boolean;
16
+ }
17
+ export interface StreamOptions {
18
+ highWaterMark?: number;
19
+ objectMode?: boolean;
20
+ encoding?: string;
21
+ read?: (this: unknown, size: number) => void;
22
+ write?: (chunk: unknown, encoding: string, callback: (error?: Error | null) => void) => void;
23
+ transform?: (chunk: unknown, encoding: string, callback: (error?: Error | null, data?: unknown) => void) => void;
24
+ flush?: (callback: (error?: Error | null) => void) => void;
25
+ final?: (callback: (error?: Error | null) => void) => void;
26
+ destroy?: (error: Error | null, callback: (error?: Error | null) => void) => void;
27
+ writableObjectMode?: boolean;
28
+ readableObjectMode?: boolean;
29
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * timers 的公开类型。
3
+ *
4
+ * 独立成文件的原因与其他 types 文件相同:timers.ts / timers/promises.ts 用 `export =`
5
+ * 导出模块本体,无法再导出类型,而 index.ts 聚合时要求这些名字是"可命名"的。
6
+ *
7
+ * signal 用的是结构类型而不是 AbortSignal:QuickJS 不保证提供 AbortController,
8
+ * 而打包后的代码可能传入 Node/浏览器那边的实现,只要形状对就行。
9
+ */
10
+ export interface AbortSignalLike {
11
+ aborted?: boolean;
12
+ addEventListener?: (type: string, listener: () => void) => void;
13
+ removeEventListener?: (type: string, listener: () => void) => void;
14
+ }
15
+ export interface TimerOptions {
16
+ signal?: AbortSignalLike;
17
+ ref?: boolean;
18
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,26 @@
1
+ import type { IteratorLike } from './url-types';
2
+ export declare function hexByte(value: number): string;
3
+ /** application/x-www-form-urlencoded 序列化:空格变 +,其余按 UTF-8 百分号编码。 */
4
+ export declare function formSerialize(value: string): string;
5
+ /** 表单反序列化:+ 变空格,再按 UTF-8 百分号解码。 */
6
+ export declare function formParse(value: string): string;
7
+ type PairList = Array<[string, string]>;
8
+ export declare class URLSearchParams {
9
+ list: PairList;
10
+ constructor(init?: unknown);
11
+ get size(): number;
12
+ append(name: unknown, value: unknown): void;
13
+ delete(name: unknown, value?: unknown): void;
14
+ get(name: unknown, value?: unknown): string | null;
15
+ getAll(name: unknown): string[];
16
+ has(name: unknown, value?: unknown): boolean;
17
+ set(name: unknown, value: unknown): void;
18
+ sort(): void;
19
+ forEach(callback: (value: string, name: string, params: URLSearchParams) => void, thisArg?: unknown): void;
20
+ entries(): IteratorLike<[string, string]>;
21
+ keys(): IteratorLike<string>;
22
+ values(): IteratorLike<string>;
23
+ [Symbol.iterator](): IteratorLike<[string, string]>;
24
+ toString(): string;
25
+ }
26
+ export {};
@@ -0,0 +1,281 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.URLSearchParams = void 0;
4
+ exports.hexByte = hexByte;
5
+ exports.formSerialize = formSerialize;
6
+ exports.formParse = formParse;
7
+ /**
8
+ * URLSearchParams(application/x-www-form-urlencoded 的 WHATWG 实现)。
9
+ *
10
+ * 从 url.ts 抽出来单独成文件,是为了让 lib/url-whatwg.ts 也能用它而不产生循环依赖:
11
+ * url.ts -> lib/url-search-params.ts, lib/url-whatwg.ts
12
+ * lib/url-whatwg.ts -> lib/url-search-params.ts
13
+ *
14
+ * 额外提供一个内部钩子:URL 的 searchParams 是"活视图",
15
+ * 参数一变 href / search 就得跟着变,所以这里允许注册一个变更回调。
16
+ * 这个回调不是标准 API,只是本库内部用。
17
+ */
18
+ const bytes_1 = require("./bytes");
19
+ const FORM_SAFE = new Set();
20
+ (function initFormSafe() {
21
+ for (let code = 0x41; code <= 0x5a; code += 1) {
22
+ FORM_SAFE.add(code);
23
+ }
24
+ for (let code = 0x61; code <= 0x7a; code += 1) {
25
+ FORM_SAFE.add(code);
26
+ }
27
+ for (let code = 0x30; code <= 0x39; code += 1) {
28
+ FORM_SAFE.add(code);
29
+ }
30
+ FORM_SAFE.add(0x2a); // *
31
+ FORM_SAFE.add(0x2d); // -
32
+ FORM_SAFE.add(0x2e); // .
33
+ FORM_SAFE.add(0x5f); // _
34
+ })();
35
+ function hexByte(value) {
36
+ return '%' + (value < 16 ? '0' : '') + value.toString(16).toUpperCase();
37
+ }
38
+ /** application/x-www-form-urlencoded 序列化:空格变 +,其余按 UTF-8 百分号编码。 */
39
+ function formSerialize(value) {
40
+ const bytes = (0, bytes_1.utf8Encode)(value);
41
+ let out = '';
42
+ for (let i = 0; i < bytes.length; i += 1) {
43
+ const byte = bytes[i];
44
+ if (FORM_SAFE.has(byte)) {
45
+ out += String.fromCharCode(byte);
46
+ }
47
+ else if (byte === 0x20) {
48
+ out += '+';
49
+ }
50
+ else {
51
+ out += hexByte(byte);
52
+ }
53
+ }
54
+ return out;
55
+ }
56
+ /** 表单反序列化:+ 变空格,再按 UTF-8 百分号解码。 */
57
+ function formParse(value) {
58
+ const bytes = [];
59
+ for (let i = 0; i < value.length; i += 1) {
60
+ const character = value.charAt(i);
61
+ if (character === '+') {
62
+ bytes.push(0x20);
63
+ continue;
64
+ }
65
+ if (character === '%' && i + 2 < value.length) {
66
+ const hex = value.slice(i + 1, i + 3);
67
+ if (/^[0-9A-Fa-f]{2}$/.test(hex)) {
68
+ bytes.push(parseInt(hex, 16));
69
+ i += 2;
70
+ continue;
71
+ }
72
+ }
73
+ const encoded = (0, bytes_1.utf8Encode)(character);
74
+ for (let j = 0; j < encoded.length; j += 1) {
75
+ bytes.push(encoded[j]);
76
+ }
77
+ }
78
+ return (0, bytes_1.utf8Decode)(Uint8Array.from(bytes));
79
+ }
80
+ /** 参数一变就通知持有者(URL 的 searchParams 是活视图,靠这个同步)。 */
81
+ function fireChange(params) {
82
+ const hook = params.__onjOnChange;
83
+ if (typeof hook === 'function') {
84
+ hook();
85
+ }
86
+ }
87
+ class URLSearchParams {
88
+ constructor(init) {
89
+ // 公开而非 private:`export =` 的模块里,导出类带 private 成员会触发 TS4094
90
+ this.list = [];
91
+ if (init === undefined || init === null) {
92
+ return;
93
+ }
94
+ if (typeof init === 'string') {
95
+ this.list = parseFormPairs(init);
96
+ return;
97
+ }
98
+ const iterable = init;
99
+ if (typeof iterable[Symbol.iterator] === 'function') {
100
+ for (const entry of init) {
101
+ const pair = entry;
102
+ if (pair === null || typeof pair !== 'object' || pair.length !== 2) {
103
+ throw new TypeError('Each query pair must be an iterable [name, value] tuple');
104
+ }
105
+ this.list.push([String(pair[0]), String(pair[1])]);
106
+ }
107
+ return;
108
+ }
109
+ const record = init;
110
+ const keys = Object.keys(record);
111
+ for (let i = 0; i < keys.length; i += 1) {
112
+ this.list.push([keys[i], String(record[keys[i]])]);
113
+ }
114
+ }
115
+ get size() {
116
+ return this.list.length;
117
+ }
118
+ append(name, value) {
119
+ this.list.push([String(name), String(value)]);
120
+ fireChange(this);
121
+ }
122
+ delete(name, value) {
123
+ const target = String(name);
124
+ const hasValue = value !== undefined;
125
+ const wanted = hasValue ? String(value) : '';
126
+ this.list = this.list.filter(function (pair) {
127
+ if (pair[0] !== target) {
128
+ return true;
129
+ }
130
+ return hasValue && pair[1] !== wanted;
131
+ });
132
+ fireChange(this);
133
+ }
134
+ get(name, value) {
135
+ const target = String(name);
136
+ const hasValue = value !== undefined;
137
+ const wanted = hasValue ? String(value) : '';
138
+ for (let i = 0; i < this.list.length; i += 1) {
139
+ if (this.list[i][0] === target && (!hasValue || this.list[i][1] === wanted)) {
140
+ return this.list[i][1];
141
+ }
142
+ }
143
+ return null;
144
+ }
145
+ getAll(name) {
146
+ const target = String(name);
147
+ const out = [];
148
+ for (let i = 0; i < this.list.length; i += 1) {
149
+ if (this.list[i][0] === target) {
150
+ out.push(this.list[i][1]);
151
+ }
152
+ }
153
+ return out;
154
+ }
155
+ has(name, value) {
156
+ return this.get(name, value) !== null;
157
+ }
158
+ set(name, value) {
159
+ const target = String(name);
160
+ const next = String(value);
161
+ let replaced = false;
162
+ const out = [];
163
+ for (let i = 0; i < this.list.length; i += 1) {
164
+ if (this.list[i][0] !== target) {
165
+ out.push(this.list[i]);
166
+ continue;
167
+ }
168
+ if (!replaced) {
169
+ out.push([target, next]);
170
+ replaced = true;
171
+ }
172
+ }
173
+ if (!replaced) {
174
+ out.push([target, next]);
175
+ }
176
+ this.list = out;
177
+ fireChange(this);
178
+ }
179
+ sort() {
180
+ // 按解码后的名字做稳定排序(比较 UTF-16 码元)
181
+ const indexed = this.list.map(function (pair, index) {
182
+ return { pair: pair, index: index };
183
+ });
184
+ indexed.sort(function (left, right) {
185
+ if (left.pair[0] < right.pair[0]) {
186
+ return -1;
187
+ }
188
+ if (left.pair[0] > right.pair[0]) {
189
+ return 1;
190
+ }
191
+ return left.index - right.index;
192
+ });
193
+ this.list = indexed.map(function (entry) { return entry.pair; });
194
+ fireChange(this);
195
+ }
196
+ forEach(callback, thisArg) {
197
+ const snapshot = this.list.slice();
198
+ for (let i = 0; i < snapshot.length; i += 1) {
199
+ callback.call(thisArg, snapshot[i][1], snapshot[i][0], this);
200
+ }
201
+ }
202
+ entries() {
203
+ let index = 0;
204
+ const list = this.list;
205
+ return asIterable({
206
+ next: function () {
207
+ if (index >= list.length) {
208
+ return { value: [undefined, undefined], done: true };
209
+ }
210
+ const value = list[index];
211
+ index += 1;
212
+ return { value: [value[0], value[1]], done: false };
213
+ }
214
+ });
215
+ }
216
+ keys() {
217
+ let index = 0;
218
+ const list = this.list;
219
+ return asIterable({
220
+ next: function () {
221
+ if (index >= list.length) {
222
+ return { value: undefined, done: true };
223
+ }
224
+ return { value: list[index++][0], done: false };
225
+ }
226
+ });
227
+ }
228
+ values() {
229
+ let index = 0;
230
+ const list = this.list;
231
+ return asIterable({
232
+ next: function () {
233
+ if (index >= list.length) {
234
+ return { value: undefined, done: true };
235
+ }
236
+ return { value: list[index++][1], done: false };
237
+ }
238
+ });
239
+ }
240
+ [Symbol.iterator]() {
241
+ return this.entries();
242
+ }
243
+ toString() {
244
+ const parts = [];
245
+ for (let i = 0; i < this.list.length; i += 1) {
246
+ parts.push(formSerialize(this.list[i][0]) + '=' + formSerialize(this.list[i][1]));
247
+ }
248
+ return parts.join('&');
249
+ }
250
+ }
251
+ exports.URLSearchParams = URLSearchParams;
252
+ /** 把裸迭代器补成可迭代对象 —— Node 的 entries/keys/values 都是可迭代的。 */
253
+ function asIterable(iterator) {
254
+ const wrapper = iterator;
255
+ wrapper[Symbol.iterator] = function () {
256
+ return wrapper;
257
+ };
258
+ return wrapper;
259
+ }
260
+ function parseFormPairs(input) {
261
+ const out = [];
262
+ const source = input.charAt(0) === '?' ? input.slice(1) : input;
263
+ if (source === '') {
264
+ return out;
265
+ }
266
+ const segments = source.split('&');
267
+ for (let i = 0; i < segments.length; i += 1) {
268
+ const segment = segments[i];
269
+ if (segment === '') {
270
+ continue;
271
+ }
272
+ const equals = segment.indexOf('=');
273
+ if (equals < 0) {
274
+ out.push([formParse(segment), '']);
275
+ }
276
+ else {
277
+ out.push([formParse(segment.slice(0, equals)), formParse(segment.slice(equals + 1))]);
278
+ }
279
+ }
280
+ return out;
281
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * url 的公开类型。
3
+ *
4
+ * 独立成文件的原因与 path-types / zlib-types / stream-types / fs-types /
5
+ * child-process-types 相同:url.ts 用 `export =` 导出模块本体,无法再导出类型,
6
+ * 而 index.ts 聚合时要求这些名字是"可命名"的。
7
+ */
8
+ /** 可迭代的迭代器(Node 的 entries/keys/values 都是可迭代的)。 */
9
+ export interface IteratorLike<T> {
10
+ next(): {
11
+ value: T;
12
+ done: boolean;
13
+ };
14
+ [Symbol.iterator](): IteratorLike<T>;
15
+ }
16
+ export interface ParsedUrl {
17
+ protocol: string | null;
18
+ slashes: boolean | null;
19
+ auth: string | null;
20
+ host: string | null;
21
+ port: string | null;
22
+ hostname: string | null;
23
+ hash: string | null;
24
+ search: string | null;
25
+ query: unknown;
26
+ pathname: string | null;
27
+ path: string | null;
28
+ href: string;
29
+ }
30
+ export interface URLObjectLike {
31
+ href: string;
32
+ origin: string;
33
+ protocol: string;
34
+ username: string;
35
+ password: string;
36
+ host: string;
37
+ hostname: string;
38
+ port: string;
39
+ pathname: string;
40
+ search: string;
41
+ hash: string;
42
+ toString(): string;
43
+ toJSON(): string;
44
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,78 @@
1
+ /**
2
+ * WHATWG URL(RFC 3986 + URL 标准的解析与序列化)。
3
+ *
4
+ * 覆盖:特殊协议(http/https/ws/wss/ftp/file)与自定义协议、authority(userinfo / host / port)、
5
+ * IPv4 / IPv6 / 域名的 punycode、按组件的百分号编码、点段规范化、相对引用解析、
6
+ * 全部 getter 与 setter、url.searchParams 的活视图、URL.canParse / URL.parse。
7
+ *
8
+ * 与 Node 的已知差异见 BUILTINS.json 的 url 条目:IPv4 的十六进制/八进制写法、
9
+ * 非 ASCII 域名的 UTS-46 映射(只做 punycode,不做映射与归一化)、blob: 的 origin。
10
+ */
11
+ import { URLSearchParams } from './url-search-params';
12
+ /** fragment 组:C0 组 + 空格 " < > ` */
13
+ export declare function fragmentEncodeSet(code: number): boolean;
14
+ /** query 组:C0 组 + 空格 " # < > */
15
+ export declare function queryEncodeSet(code: number): boolean;
16
+ /** special-query 组:query 组 + ' */
17
+ export declare function specialQueryEncodeSet(code: number): boolean;
18
+ /** path 组:query 组 + ? ` { } */
19
+ export declare function pathEncodeSet(code: number): boolean;
20
+ /** userinfo 组:path 组 + / : ; = @ [ \ ] ^ | */
21
+ export declare function userinfoEncodeSet(code: number): boolean;
22
+ interface UrlRecord {
23
+ scheme: string;
24
+ username: string;
25
+ password: string;
26
+ /** null 表示没有 authority(不透明路径) */
27
+ host: string | null;
28
+ port: number | null;
29
+ /** 有 authority 时是段数组;没有时是不透明路径字符串 */
30
+ path: string[] | string;
31
+ query: string | null;
32
+ fragment: string | null;
33
+ }
34
+ export declare function parseUrlRecord(input: string, base?: UrlRecord | null): UrlRecord | null;
35
+ export declare class URL {
36
+ private record;
37
+ searchParams: URLSearchParams;
38
+ constructor(input: unknown, base?: unknown);
39
+ private buildSearchParams;
40
+ /**
41
+ * 把 searchParams 写回 query。只在参数**真的被改动**时调用。
42
+ *
43
+ * 两处不能想当然:
44
+ * - 不能用 URLSearchParams.toString():那是表单序列化(空格 -> +),
45
+ * 而 URL 的 query 用 URL 的 query 编码集(空格 -> %20);
46
+ * - 没被改动过时必须保持 query 原样,否则 new URL('http://x/?b').search
47
+ * 会从 '?b' 变成 '?b='。
48
+ */
49
+ private applyParams;
50
+ /** 不透明路径(没有 authority)时用字符串路径。 */
51
+ private isOpaque;
52
+ get href(): string;
53
+ set href(value: unknown);
54
+ get origin(): string;
55
+ get protocol(): string;
56
+ set protocol(value: unknown);
57
+ get username(): string;
58
+ set username(value: unknown);
59
+ get password(): string;
60
+ set password(value: unknown);
61
+ get host(): string;
62
+ set host(value: unknown);
63
+ get hostname(): string;
64
+ set hostname(value: unknown);
65
+ get port(): string;
66
+ set port(value: unknown);
67
+ get pathname(): string;
68
+ set pathname(value: unknown);
69
+ get search(): string;
70
+ set search(value: unknown);
71
+ get hash(): string;
72
+ set hash(value: unknown);
73
+ toString(): string;
74
+ toJSON(): string;
75
+ static canParse(input: unknown, base?: unknown): boolean;
76
+ static parse(input: unknown, base?: unknown): URL | null;
77
+ }
78
+ export {};