@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
package/dist/buffer.js ADDED
@@ -0,0 +1,524 @@
1
+ 'use strict';
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.INSPECT_MAX_BYTES = exports.kMaxLength = exports.constants = exports.Buffer = void 0;
37
+ exports.wrap = wrap;
38
+ /**
39
+ * Node Buffer 的移植。
40
+ *
41
+ * 实现要点:
42
+ * - 类型上 `class Buffer extends Uint8Array`,但实例一律由 `wrap()` 通过
43
+ * `Object.setPrototypeOf` 从普通 Uint8Array 转换而来,不走构造函数。
44
+ * 这样 `x instanceof Buffer` 与 `x instanceof Uint8Array` 同时成立,
45
+ * 也避开了派生 TypedArray 在各引擎上的构造差异。
46
+ * - 编码全部落在 lib/bytes(base64/latin1/utf8)与本地 hex/utf16le/ascii 上,
47
+ * 不依赖 TextEncoder / TextDecoder —— QuickJS 不保证有它们。
48
+ * - Node 的 `buf.slice()` 返回**视图**(与 Uint8Array.slice 的拷贝语义相反),这里保持一致。
49
+ */
50
+ const bytesCodec = __importStar(require("./lib/bytes"));
51
+ const HEX = '0123456789abcdef';
52
+ class Buffer extends Uint8Array {
53
+ static from(value, encodingOrOffset, length) {
54
+ if (typeof value === 'string') {
55
+ const encoding = typeof encodingOrOffset === 'string' ? encodingOrOffset : 'utf8';
56
+ return wrap(encodeString(value, normalizeEncoding(encoding)));
57
+ }
58
+ if (value instanceof ArrayBuffer) {
59
+ const offset = typeof encodingOrOffset === 'number' ? encodingOrOffset : 0;
60
+ return wrap(length === undefined
61
+ ? new Uint8Array(value, offset)
62
+ : new Uint8Array(value, offset, length));
63
+ }
64
+ if (value instanceof Uint8Array) {
65
+ const copy = new Uint8Array(value.length);
66
+ copy.set(value);
67
+ return wrap(copy);
68
+ }
69
+ return wrap(Uint8Array.from(value));
70
+ }
71
+ static alloc(size, fill, encoding) {
72
+ const length = checkSize(size);
73
+ const out = new Uint8Array(length);
74
+ const buffer = wrap(out);
75
+ if (fill !== undefined) {
76
+ buffer.fill(fill, 0, length, encoding);
77
+ }
78
+ return buffer;
79
+ }
80
+ static allocUnsafe(size) {
81
+ return wrap(new Uint8Array(checkSize(size)));
82
+ }
83
+ static allocUnsafeSlow(size) {
84
+ return wrap(new Uint8Array(checkSize(size)));
85
+ }
86
+ static isBuffer(value) {
87
+ return value instanceof Buffer;
88
+ }
89
+ static isEncoding(encoding) {
90
+ return ENCODINGS.indexOf(String(encoding).toLowerCase()) >= 0;
91
+ }
92
+ static byteLength(value, encoding) {
93
+ if (typeof value === 'string') {
94
+ return encodeString(value, normalizeEncoding(encoding)).length;
95
+ }
96
+ if (value instanceof ArrayBuffer) {
97
+ return value.byteLength;
98
+ }
99
+ return value.length;
100
+ }
101
+ static concat(list, totalLength) {
102
+ if (!Array.isArray(list) || list.length === 0) {
103
+ return wrap(new Uint8Array(0));
104
+ }
105
+ let total = 0;
106
+ for (let i = 0; i < list.length; i += 1) {
107
+ total += list[i].length;
108
+ }
109
+ const length = totalLength === undefined ? total : checkSize(totalLength);
110
+ const out = new Uint8Array(length);
111
+ let offset = 0;
112
+ for (let i = 0; i < list.length && offset < length; i += 1) {
113
+ const chunk = list[i];
114
+ const room = length - offset;
115
+ const take = chunk.length < room ? chunk.length : room;
116
+ out.set(take === chunk.length ? chunk : chunk.subarray(0, take), offset);
117
+ offset += take;
118
+ }
119
+ return wrap(out);
120
+ }
121
+ static compare(a, b) {
122
+ return compareBytes(a, b);
123
+ }
124
+ // ---------------------------------------------------------------- instance
125
+ toString(encoding, start, end) {
126
+ const begin = clampIndex(start === undefined ? 0 : start, this.length);
127
+ const finish = clampIndex(end === undefined ? this.length : end, this.length);
128
+ if (finish <= begin) {
129
+ return '';
130
+ }
131
+ const slice = viewOf(this, begin, finish);
132
+ switch (normalizeEncoding(encoding)) {
133
+ case 'hex':
134
+ return hexEncode(slice);
135
+ case 'base64':
136
+ return bytesCodec.encodeBase64(slice);
137
+ case 'latin1':
138
+ return bytesCodec.bytesToLatin1(slice);
139
+ case 'ascii':
140
+ return asciiDecode(slice);
141
+ case 'utf16le':
142
+ return utf16leDecode(slice);
143
+ default:
144
+ return bytesCodec.utf8Decode(slice);
145
+ }
146
+ }
147
+ toJSON() {
148
+ const data = new Array(this.length);
149
+ for (let i = 0; i < this.length; i += 1) {
150
+ data[i] = this[i];
151
+ }
152
+ return { type: 'Buffer', data: data };
153
+ }
154
+ equals(other) {
155
+ if (!(other instanceof Uint8Array)) {
156
+ throw new TypeError('Argument must be a Buffer or Uint8Array');
157
+ }
158
+ return compareBytes(this, other) === 0;
159
+ }
160
+ compare(other, targetStart, targetEnd, sourceStart, sourceEnd) {
161
+ const target = viewOf(other, clampIndex(targetStart ?? 0, other.length), clampIndex(targetEnd ?? other.length, other.length));
162
+ const source = viewOf(this, clampIndex(sourceStart ?? 0, this.length), clampIndex(sourceEnd ?? this.length, this.length));
163
+ return compareBytes(source, target);
164
+ }
165
+ copy(target, targetStart, sourceStart, sourceEnd) {
166
+ const start = clampIndex(targetStart ?? 0, target.length);
167
+ const from = clampIndex(sourceStart ?? 0, this.length);
168
+ const to = clampIndex(sourceEnd ?? this.length, this.length);
169
+ if (to <= from || start >= target.length) {
170
+ return 0;
171
+ }
172
+ const room = target.length - start;
173
+ const take = Math.min(to - from, room);
174
+ target.set(viewOf(this, from, from + take), start);
175
+ return take;
176
+ }
177
+ fill(value, offset, end, encoding) {
178
+ const start = clampIndex(offset ?? 0, this.length);
179
+ const finish = clampIndex(end ?? this.length, this.length);
180
+ if (finish <= start) {
181
+ return this;
182
+ }
183
+ let source;
184
+ if (typeof value === 'string') {
185
+ source = encodeString(value, normalizeEncoding(encoding));
186
+ }
187
+ else if (typeof value === 'number') {
188
+ source = Uint8Array.of(value & 255);
189
+ }
190
+ else {
191
+ source = value;
192
+ }
193
+ if (source.length === 0) {
194
+ return this;
195
+ }
196
+ for (let i = start; i < finish; i += 1) {
197
+ this[i] = source[(i - start) % source.length];
198
+ }
199
+ return this;
200
+ }
201
+ indexOf(value, byteOffset, encoding) {
202
+ return indexOfValue(this, value, byteOffset ?? 0, encoding, false);
203
+ }
204
+ lastIndexOf(value, byteOffset, encoding) {
205
+ return indexOfValue(this, value, byteOffset ?? this.length, encoding, true);
206
+ }
207
+ includes(value, byteOffset, encoding) {
208
+ return indexOfValue(this, value, byteOffset ?? 0, encoding, false) >= 0;
209
+ }
210
+ slice(start, end) {
211
+ return this.subarray(start, end);
212
+ }
213
+ subarray(start, end) {
214
+ const begin = clampIndex(start ?? 0, this.length);
215
+ const finish = clampIndex(end ?? this.length, this.length);
216
+ return wrap(viewOf(this, begin, Math.max(begin, finish)));
217
+ }
218
+ write(value, offset, length, encoding) {
219
+ const start = clampIndex(offset ?? 0, this.length);
220
+ const encoded = encodeString(value, normalizeEncoding(encoding));
221
+ const room = this.length - start;
222
+ const take = Math.min(length === undefined ? encoded.length : length, encoded.length, room);
223
+ this.set(encoded.subarray(0, take), start);
224
+ return take;
225
+ }
226
+ writeUInt8(value, offset) {
227
+ const at = checkOffset(offset ?? 0, 1, this.length);
228
+ this[at] = value & 255;
229
+ return at + 1;
230
+ }
231
+ writeInt8(value, offset) {
232
+ return this.writeUInt8(value < 0 ? value + 256 : value, offset);
233
+ }
234
+ writeUInt16LE(value, offset) {
235
+ const at = checkOffset(offset ?? 0, 2, this.length);
236
+ this[at] = value & 255;
237
+ this[at + 1] = (value >>> 8) & 255;
238
+ return at + 2;
239
+ }
240
+ writeUInt16BE(value, offset) {
241
+ const at = checkOffset(offset ?? 0, 2, this.length);
242
+ this[at] = (value >>> 8) & 255;
243
+ this[at + 1] = value & 255;
244
+ return at + 2;
245
+ }
246
+ writeUInt32LE(value, offset) {
247
+ const at = checkOffset(offset ?? 0, 4, this.length);
248
+ this[at] = value & 255;
249
+ this[at + 1] = (value >>> 8) & 255;
250
+ this[at + 2] = (value >>> 16) & 255;
251
+ this[at + 3] = (value >>> 24) & 255;
252
+ return at + 4;
253
+ }
254
+ writeUInt32BE(value, offset) {
255
+ const at = checkOffset(offset ?? 0, 4, this.length);
256
+ this[at] = (value >>> 24) & 255;
257
+ this[at + 1] = (value >>> 16) & 255;
258
+ this[at + 2] = (value >>> 8) & 255;
259
+ this[at + 3] = value & 255;
260
+ return at + 4;
261
+ }
262
+ writeInt16LE(value, offset) {
263
+ return this.writeUInt16LE(value < 0 ? value + 0x10000 : value, offset);
264
+ }
265
+ writeInt16BE(value, offset) {
266
+ return this.writeUInt16BE(value < 0 ? value + 0x10000 : value, offset);
267
+ }
268
+ writeInt32LE(value, offset) {
269
+ return this.writeUInt32LE(value < 0 ? value + 0x100000000 : value, offset);
270
+ }
271
+ writeInt32BE(value, offset) {
272
+ return this.writeUInt32BE(value < 0 ? value + 0x100000000 : value, offset);
273
+ }
274
+ readUInt8(offset) {
275
+ return this[checkOffset(offset ?? 0, 1, this.length)];
276
+ }
277
+ readInt8(offset) {
278
+ const value = this.readUInt8(offset);
279
+ return value > 127 ? value - 256 : value;
280
+ }
281
+ readUInt16LE(offset) {
282
+ const at = checkOffset(offset ?? 0, 2, this.length);
283
+ return this[at] | (this[at + 1] << 8);
284
+ }
285
+ readUInt16BE(offset) {
286
+ const at = checkOffset(offset ?? 0, 2, this.length);
287
+ return (this[at] << 8) | this[at + 1];
288
+ }
289
+ readUInt32LE(offset) {
290
+ const at = checkOffset(offset ?? 0, 4, this.length);
291
+ return (this[at] | (this[at + 1] << 8) | (this[at + 2] << 16) | (this[at + 3] << 24)) >>> 0;
292
+ }
293
+ readUInt32BE(offset) {
294
+ const at = checkOffset(offset ?? 0, 4, this.length);
295
+ return ((this[at] << 24) | (this[at + 1] << 16) | (this[at + 2] << 8) | this[at + 3]) >>> 0;
296
+ }
297
+ readInt16LE(offset) {
298
+ const value = this.readUInt16LE(offset);
299
+ return value > 0x7fff ? value - 0x10000 : value;
300
+ }
301
+ readInt16BE(offset) {
302
+ const value = this.readUInt16BE(offset);
303
+ return value > 0x7fff ? value - 0x10000 : value;
304
+ }
305
+ readInt32LE(offset) {
306
+ return this.readUInt32LE(offset) | 0;
307
+ }
308
+ readInt32BE(offset) {
309
+ return this.readUInt32BE(offset) | 0;
310
+ }
311
+ }
312
+ exports.Buffer = Buffer;
313
+ /** Node `buffer.constants` 的形状;MAX_STRING_LENGTH 与 V8 上限一致。 */
314
+ exports.constants = {
315
+ // Node 在 64 位平台取 2^53-1;Operit 只支持 arm64-v8a,故取同一值。
316
+ MAX_LENGTH: 9007199254740991,
317
+ MAX_STRING_LENGTH: 0x1fffffe8
318
+ };
319
+ exports.kMaxLength = exports.constants.MAX_LENGTH;
320
+ exports.INSPECT_MAX_BYTES = 50;
321
+ const ENCODINGS = [
322
+ 'utf8', 'utf-8', 'hex', 'base64', 'latin1', 'binary', 'ascii', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le'
323
+ ];
324
+ /** 把普通 Uint8Array 变成 Buffer 实例(不改内容、不拷贝)。 */
325
+ function wrap(source) {
326
+ Object.setPrototypeOf(source, Buffer.prototype);
327
+ return source;
328
+ }
329
+ function viewOf(source, start, end) {
330
+ return Uint8Array.prototype.subarray.call(source, start, end);
331
+ }
332
+ function normalizeEncoding(encoding) {
333
+ const value = typeof encoding === 'string' ? encoding.toLowerCase() : 'utf8';
334
+ if (value === 'utf-8') {
335
+ return 'utf8';
336
+ }
337
+ if (value === 'binary') {
338
+ return 'latin1';
339
+ }
340
+ if (value === 'ucs-2' || value === 'ucs2') {
341
+ return 'utf16le';
342
+ }
343
+ if (value === 'utf-16le') {
344
+ return 'utf16le';
345
+ }
346
+ if (ENCODINGS.indexOf(value) >= 0) {
347
+ return value;
348
+ }
349
+ throw new TypeError('Unknown encoding: ' + String(encoding));
350
+ }
351
+ function encodeString(value, encoding) {
352
+ switch (encoding) {
353
+ case 'hex':
354
+ return hexDecode(value);
355
+ case 'base64':
356
+ return bytesCodec.decodeBase64(value);
357
+ case 'latin1':
358
+ return bytesCodec.latin1ToBytes(value);
359
+ case 'ascii':
360
+ return asciiEncode(value);
361
+ case 'utf16le':
362
+ return utf16leEncode(value);
363
+ default:
364
+ return bytesCodec.utf8Encode(value);
365
+ }
366
+ }
367
+ function hexEncode(source) {
368
+ let out = '';
369
+ for (let i = 0; i < source.length; i += 1) {
370
+ out += HEX[source[i] >> 4] + HEX[source[i] & 15];
371
+ }
372
+ return out;
373
+ }
374
+ function hexDecode(value) {
375
+ const text = String(value);
376
+ const out = [];
377
+ for (let i = 0; i + 1 < text.length; i += 2) {
378
+ const high = hexValue(text.charCodeAt(i));
379
+ const low = hexValue(text.charCodeAt(i + 1));
380
+ if (high < 0 || low < 0) {
381
+ break;
382
+ }
383
+ out.push((high << 4) | low);
384
+ }
385
+ return new Uint8Array(out);
386
+ }
387
+ function hexValue(code) {
388
+ if (code >= 48 && code <= 57) {
389
+ return code - 48;
390
+ }
391
+ if (code >= 97 && code <= 102) {
392
+ return code - 87;
393
+ }
394
+ if (code >= 65 && code <= 70) {
395
+ return code - 55;
396
+ }
397
+ return -1;
398
+ }
399
+ /**
400
+ * ascii 编码按 Node 语义取低 8 位(与 latin1 一致,不是掩 0x7f);
401
+ * 解码时才把高位抹掉。这两侧不对称是 Node 的既有行为。
402
+ */
403
+ function asciiEncode(value) {
404
+ const out = new Uint8Array(value.length);
405
+ for (let i = 0; i < value.length; i += 1) {
406
+ out[i] = value.charCodeAt(i) & 255;
407
+ }
408
+ return out;
409
+ }
410
+ function asciiDecode(source) {
411
+ let out = '';
412
+ for (let i = 0; i < source.length; i += 1) {
413
+ out += String.fromCharCode(source[i] & 127);
414
+ }
415
+ return out;
416
+ }
417
+ function utf16leEncode(value) {
418
+ const out = new Uint8Array(value.length * 2);
419
+ for (let i = 0; i < value.length; i += 1) {
420
+ const code = value.charCodeAt(i);
421
+ out[i * 2] = code & 255;
422
+ out[i * 2 + 1] = (code >>> 8) & 255;
423
+ }
424
+ return out;
425
+ }
426
+ function utf16leDecode(source) {
427
+ let out = '';
428
+ for (let i = 0; i + 1 < source.length; i += 2) {
429
+ out += String.fromCharCode(source[i] | (source[i + 1] << 8));
430
+ }
431
+ return out;
432
+ }
433
+ function compareBytes(a, b) {
434
+ const length = Math.min(a.length, b.length);
435
+ for (let i = 0; i < length; i += 1) {
436
+ if (a[i] !== b[i]) {
437
+ return a[i] < b[i] ? -1 : 1;
438
+ }
439
+ }
440
+ if (a.length === b.length) {
441
+ return 0;
442
+ }
443
+ return a.length < b.length ? -1 : 1;
444
+ }
445
+ function indexOfValue(source, value, byteOffset, encoding, backwards) {
446
+ let needle;
447
+ if (typeof value === 'number') {
448
+ needle = Uint8Array.of(value & 255);
449
+ }
450
+ else if (typeof value === 'string') {
451
+ needle = encodeString(value, normalizeEncoding(encoding));
452
+ }
453
+ else {
454
+ needle = value;
455
+ }
456
+ if (needle.length === 0) {
457
+ return backwards ? Math.min(byteOffset, source.length) : Math.min(Math.max(byteOffset, 0), source.length);
458
+ }
459
+ if (backwards) {
460
+ let from = Math.min(byteOffset, source.length - needle.length);
461
+ for (; from >= 0; from -= 1) {
462
+ if (matchesAt(source, needle, from)) {
463
+ return from;
464
+ }
465
+ }
466
+ return -1;
467
+ }
468
+ for (let from = Math.max(byteOffset, 0); from + needle.length <= source.length; from += 1) {
469
+ if (matchesAt(source, needle, from)) {
470
+ return from;
471
+ }
472
+ }
473
+ return -1;
474
+ }
475
+ function matchesAt(source, needle, at) {
476
+ for (let i = 0; i < needle.length; i += 1) {
477
+ if (source[at + i] !== needle[i]) {
478
+ return false;
479
+ }
480
+ }
481
+ return true;
482
+ }
483
+ function checkSize(size) {
484
+ if (typeof size !== 'number' || !isFinite(size) || size < 0) {
485
+ throw new RangeError('The value of "size" is out of range.');
486
+ }
487
+ return Math.floor(size);
488
+ }
489
+ function clampIndex(value, length) {
490
+ if (!isFinite(value) || value <= 0) {
491
+ return 0;
492
+ }
493
+ return value >= length ? length : Math.floor(value);
494
+ }
495
+ function checkOffset(offset, size, length) {
496
+ if (typeof offset !== 'number' || offset < 0 || offset + size > length) {
497
+ throw new RangeError('Attempt to access memory outside buffer bounds');
498
+ }
499
+ return offset;
500
+ }
501
+ /**
502
+ * 把静态成员补成**可枚举的自有属性**。
503
+ *
504
+ * Node 的 Buffer 静态成员是赋值出来的,因此可枚举;而 `class` 的 static 方法不可枚举。
505
+ * 这不是吹毛求疵:safer-buffer(iconv-lite 的依赖,装机量极大)就是用
506
+ * `for (key in Buffer)` 遍历静态成员来做能力探测的 —— 不可枚举时它复制出空对象,
507
+ * `Buffer.concat` 之类随即消失,iconv-lite 一调用就炸。
508
+ * 这个差异只有在跑真实 npm 包时才会暴露,纯对拍 API 是看不出来的。
509
+ */
510
+ for (const staticKey of Object.getOwnPropertyNames(Buffer)) {
511
+ if (staticKey === 'length' || staticKey === 'name' || staticKey === 'prototype') {
512
+ continue;
513
+ }
514
+ const descriptor = Object.getOwnPropertyDescriptor(Buffer, staticKey);
515
+ if (descriptor !== undefined && descriptor.enumerable !== true) {
516
+ Object.defineProperty(Buffer, staticKey, {
517
+ value: descriptor.value,
518
+ writable: descriptor.writable === true,
519
+ enumerable: true,
520
+ configurable: descriptor.configurable === true
521
+ });
522
+ }
523
+ }
524
+ exports.default = Buffer;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Node child_process 模块的移植。
3
+ *
4
+ * 覆盖:exec / execSync / execFile / execFileSync / spawn / spawnSync,
5
+ * ChildProcess 的 spawn/exit/close/error 事件与 stdout/stderr/stdin、pid、exitCode、kill。
6
+ * 不覆盖:fork(需要 Node 运行时本身)。
7
+ *
8
+ * 时序上的重要差异:宿主只有 Java bridge 这一条同步通道,
9
+ * 所以 spawn 是「先同步跑完、再异步发事件」—— 接口是异步的,执行是同步的,
10
+ * 拿不到真正的并发与背压。需要真并发请用终端工具(Tools 层)。
11
+ *
12
+ * 输出按 UTF-8 解码(驱动层的选择,见 lib/process-driver.ts),二进制输出会有损。
13
+ */
14
+ import EventEmitter = require('./events');
15
+ import streamModule = require('./stream');
16
+ import { Buffer } from './buffer';
17
+ import { type NodeStyleError } from './lib/errors';
18
+ import type { ExecOptions, RunOutcome, SpawnSyncResult } from './lib/child-process-types';
19
+ type ReadStreamLike = InstanceType<typeof streamModule.Readable>;
20
+ type WriteStreamLike = InstanceType<typeof streamModule.Writable>;
21
+ declare function execSync(command: unknown, options?: ExecOptions): string | Buffer;
22
+ declare function execFileSync(file: unknown, args?: unknown, options?: ExecOptions): string | Buffer;
23
+ declare function spawnSync(command: unknown, args?: unknown, options?: unknown): SpawnSyncResult;
24
+ declare class ChildProcess extends EventEmitter {
25
+ pid: number;
26
+ exitCode: number | null;
27
+ signalCode: string | null;
28
+ killed: boolean;
29
+ readonly spawnfile: string;
30
+ readonly spawnargs: string[];
31
+ readonly stdout: ReadStreamLike;
32
+ readonly stderr: ReadStreamLike;
33
+ readonly stdin: WriteStreamLike;
34
+ /** 启动失败时保留的错误;exec/execFile 的回调要优先报它。 */
35
+ readonly spawnError?: NodeStyleError;
36
+ constructor(argv: readonly string[], outcome: RunOutcome);
37
+ /** 进程在本实现里已经跑完了,所以 kill 总是返回 false。 */
38
+ kill(): boolean;
39
+ ref(): this;
40
+ unref(): this;
41
+ }
42
+ declare function exec(command: unknown, options?: unknown, callback?: unknown): ChildProcess;
43
+ declare function execFile(file: unknown, args?: unknown, options?: unknown, callback?: unknown): ChildProcess;
44
+ declare function spawn(command: unknown, args?: unknown, options?: unknown): ChildProcess;
45
+ declare function fork(): never;
46
+ declare const childProcess: {
47
+ exec: typeof exec;
48
+ execSync: typeof execSync;
49
+ execFile: typeof execFile;
50
+ execFileSync: typeof execFileSync;
51
+ spawn: typeof spawn;
52
+ spawnSync: typeof spawnSync;
53
+ fork: typeof fork;
54
+ ChildProcess: typeof ChildProcess;
55
+ };
56
+ export = childProcess;