@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,284 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.inflateRaw = inflateRaw;
4
+ exports.crc32 = crc32;
5
+ exports.adler32 = adler32;
6
+ /**
7
+ * 纯 JS 的 DEFLATE 解压(RFC 1951),以及 zlib / gzip 的包装层校验和。
8
+ *
9
+ * 为什么自己实现:运行时注入的 pako 只是 1.5KB 的 shim,只支持
10
+ * inflate(base64String, { to: 'string' }),既不解字节流也不支持 raw/zlib/gzip 区分。
11
+ *
12
+ * 正确性由 test/zlib.test.js 双向对拍保证:
13
+ * Node 压的我们能解,我们解出的与原文逐字节相同。
14
+ */
15
+ const LENGTH_BASE = [
16
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
17
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
18
+ ];
19
+ const LENGTH_EXTRA = [
20
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
21
+ 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
22
+ ];
23
+ const DISTANCE_BASE = [
24
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
25
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
26
+ ];
27
+ const DISTANCE_EXTRA = [
28
+ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
29
+ 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
30
+ ];
31
+ /** 动态 Huffman 头里"码长码长"的读取顺序。 */
32
+ const CODE_LENGTH_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
33
+ function makeReader(data, start, end) {
34
+ return { data: data, position: start, end: end, bits: 0, bitCount: 0 };
35
+ }
36
+ function readBits(reader, count) {
37
+ let value = reader.bits;
38
+ while (reader.bitCount < count) {
39
+ if (reader.position >= reader.end) {
40
+ throw new Error('unexpected end of deflate stream');
41
+ }
42
+ value |= reader.data[reader.position] << reader.bitCount;
43
+ reader.position += 1;
44
+ reader.bitCount += 8;
45
+ }
46
+ reader.bits = value >>> count;
47
+ reader.bitCount -= count;
48
+ return value & ((1 << count) - 1);
49
+ }
50
+ function alignToByte(reader) {
51
+ reader.bits = 0;
52
+ reader.bitCount = 0;
53
+ }
54
+ function readByteAligned(reader) {
55
+ if (reader.position >= reader.end) {
56
+ throw new Error('unexpected end of deflate stream');
57
+ }
58
+ const value = reader.data[reader.position];
59
+ reader.position += 1;
60
+ return value;
61
+ }
62
+ /**
63
+ * 从码长建规范 Huffman 表。解码用 puff 的 counts/symbols 布局,
64
+ * 逐位左移即可自动处理"码本身按 MSB 优先"的位序。
65
+ */
66
+ function buildTable(lengths, count) {
67
+ const counts = new Int32Array(16);
68
+ for (let i = 0; i < count; i += 1) {
69
+ counts[lengths[i]] += 1;
70
+ }
71
+ if (counts[0] === count) {
72
+ // 全零码长:只有一种符号,按 DEFLATE 规定等价于 0 位编码
73
+ return { counts: counts, symbols: new Int32Array(0) };
74
+ }
75
+ counts[0] = 0;
76
+ const offsets = new Int32Array(16);
77
+ for (let i = 1; i < 16; i += 1) {
78
+ offsets[i] = offsets[i - 1] + counts[i - 1];
79
+ }
80
+ const symbols = new Int32Array(count);
81
+ for (let i = 0; i < count; i += 1) {
82
+ if (lengths[i] !== 0) {
83
+ symbols[offsets[lengths[i]]] = i;
84
+ offsets[lengths[i]] += 1;
85
+ }
86
+ }
87
+ return { counts: counts, symbols: symbols };
88
+ }
89
+ function decodeSymbol(reader, table) {
90
+ let code = 0;
91
+ let first = 0;
92
+ let index = 0;
93
+ for (let length = 1; length <= 15; length += 1) {
94
+ code |= readBits(reader, 1);
95
+ const count = table.counts[length];
96
+ if (code - first < count) {
97
+ return table.symbols[index + (code - first)];
98
+ }
99
+ index += count;
100
+ first = (first + count) << 1;
101
+ code <<= 1;
102
+ }
103
+ throw new Error('invalid huffman code');
104
+ }
105
+ let FIXED_LITERAL = null;
106
+ let FIXED_DISTANCE = null;
107
+ function fixedTables() {
108
+ if (FIXED_LITERAL === null || FIXED_DISTANCE === null) {
109
+ const literalLengths = new Int32Array(288);
110
+ for (let i = 0; i < 144; i += 1) {
111
+ literalLengths[i] = 8;
112
+ }
113
+ for (let i = 144; i < 256; i += 1) {
114
+ literalLengths[i] = 9;
115
+ }
116
+ for (let i = 256; i < 280; i += 1) {
117
+ literalLengths[i] = 7;
118
+ }
119
+ for (let i = 280; i < 288; i += 1) {
120
+ literalLengths[i] = 8;
121
+ }
122
+ const distanceLengths = new Int32Array(30);
123
+ for (let i = 0; i < 30; i += 1) {
124
+ distanceLengths[i] = 5;
125
+ }
126
+ FIXED_LITERAL = buildTable(literalLengths, 288);
127
+ FIXED_DISTANCE = buildTable(distanceLengths, 30);
128
+ }
129
+ return { literal: FIXED_LITERAL, distance: FIXED_DISTANCE };
130
+ }
131
+ function ensureCapacity(output, extra) {
132
+ if (output.length + extra <= output.bytes.length) {
133
+ return;
134
+ }
135
+ let size = output.bytes.length === 0 ? Math.max(64, extra) : output.bytes.length;
136
+ while (size < output.length + extra) {
137
+ size *= 2;
138
+ }
139
+ const grown = new Uint8Array(size);
140
+ grown.set(output.bytes.subarray(0, output.length));
141
+ output.bytes = grown;
142
+ }
143
+ function inflateBlockData(reader, output, literal, distance) {
144
+ for (;;) {
145
+ const symbol = decodeSymbol(reader, literal);
146
+ if (symbol === 256) {
147
+ return;
148
+ }
149
+ if (symbol < 256) {
150
+ ensureCapacity(output, 1);
151
+ output.bytes[output.length] = symbol;
152
+ output.length += 1;
153
+ continue;
154
+ }
155
+ const lengthIndex = symbol - 257;
156
+ if (lengthIndex >= LENGTH_BASE.length) {
157
+ throw new Error('invalid length code');
158
+ }
159
+ const matchLength = LENGTH_BASE[lengthIndex] + readBits(reader, LENGTH_EXTRA[lengthIndex]);
160
+ const distanceSymbol = decodeSymbol(reader, distance);
161
+ if (distanceSymbol >= DISTANCE_BASE.length) {
162
+ throw new Error('invalid distance code');
163
+ }
164
+ const matchDistance = DISTANCE_BASE[distanceSymbol] + readBits(reader, DISTANCE_EXTRA[distanceSymbol]);
165
+ if (matchDistance > output.length) {
166
+ throw new Error('distance too far back');
167
+ }
168
+ ensureCapacity(output, matchLength);
169
+ let source = output.length - matchDistance;
170
+ for (let i = 0; i < matchLength; i += 1) {
171
+ output.bytes[output.length] = output.bytes[source];
172
+ output.length += 1;
173
+ source += 1;
174
+ }
175
+ }
176
+ }
177
+ function inflateDynamicTables(reader) {
178
+ const literalCount = readBits(reader, 5) + 257;
179
+ const distanceCount = readBits(reader, 5) + 1;
180
+ const codeLengthCount = readBits(reader, 4) + 4;
181
+ const codeLengths = new Int32Array(19);
182
+ for (let i = 0; i < codeLengthCount; i += 1) {
183
+ codeLengths[CODE_LENGTH_ORDER[i]] = readBits(reader, 3);
184
+ }
185
+ const codeLengthTable = buildTable(codeLengths, 19);
186
+ const lengths = new Int32Array(literalCount + distanceCount);
187
+ let index = 0;
188
+ while (index < lengths.length) {
189
+ const symbol = decodeSymbol(reader, codeLengthTable);
190
+ if (symbol < 16) {
191
+ lengths[index] = symbol;
192
+ index += 1;
193
+ }
194
+ else if (symbol === 16) {
195
+ if (index === 0) {
196
+ throw new Error('repeat with no previous code length');
197
+ }
198
+ const previous = lengths[index - 1];
199
+ const repeat = readBits(reader, 2) + 3;
200
+ for (let i = 0; i < repeat; i += 1) {
201
+ lengths[index] = previous;
202
+ index += 1;
203
+ }
204
+ }
205
+ else if (symbol === 17) {
206
+ const repeat = readBits(reader, 3) + 3;
207
+ index += repeat;
208
+ }
209
+ else {
210
+ const repeat = readBits(reader, 7) + 11;
211
+ index += repeat;
212
+ }
213
+ }
214
+ const literal = buildTable(lengths.subarray(0, literalCount), literalCount);
215
+ const distance = buildTable(lengths.subarray(literalCount), distanceCount);
216
+ return { literal: literal, distance: distance };
217
+ }
218
+ /** 解压 raw DEFLATE 数据。 */
219
+ function inflateRaw(data, start, end) {
220
+ const reader = makeReader(data, start, end);
221
+ const output = { bytes: new Uint8Array(Math.max(64, (end - start) * 4)), length: 0 };
222
+ const fixed = fixedTables();
223
+ for (;;) {
224
+ const isFinal = readBits(reader, 1);
225
+ const blockType = readBits(reader, 2);
226
+ if (blockType === 0) {
227
+ alignToByte(reader);
228
+ const length = readByteAligned(reader) | (readByteAligned(reader) << 8);
229
+ const inverted = readByteAligned(reader) | (readByteAligned(reader) << 8);
230
+ if ((length ^ 0xffff) !== inverted) {
231
+ throw new Error('stored block length mismatch');
232
+ }
233
+ ensureCapacity(output, length);
234
+ for (let i = 0; i < length; i += 1) {
235
+ output.bytes[output.length] = readByteAligned(reader);
236
+ output.length += 1;
237
+ }
238
+ }
239
+ else if (blockType === 1) {
240
+ inflateBlockData(reader, output, fixed.literal, fixed.distance);
241
+ }
242
+ else if (blockType === 2) {
243
+ const tables = inflateDynamicTables(reader);
244
+ inflateBlockData(reader, output, tables.literal, tables.distance);
245
+ }
246
+ else {
247
+ throw new Error('invalid deflate block type');
248
+ }
249
+ if (isFinal === 1) {
250
+ break;
251
+ }
252
+ }
253
+ return output.bytes.subarray(0, output.length);
254
+ }
255
+ // ------------------------------------------------------------------ 校验和
256
+ const CRC_TABLE = (function buildCrcTable() {
257
+ const table = new Uint32Array(256);
258
+ for (let n = 0; n < 256; n += 1) {
259
+ let c = n;
260
+ for (let k = 0; k < 8; k += 1) {
261
+ c = (c & 1) !== 0 ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1);
262
+ }
263
+ table[n] = c >>> 0;
264
+ }
265
+ return table;
266
+ })();
267
+ /** gzip 的 CRC-32(IEEE 802.3)。 */
268
+ function crc32(data, start, end) {
269
+ let crc = 0xffffffff;
270
+ for (let i = start; i < end; i += 1) {
271
+ crc = CRC_TABLE[(crc ^ data[i]) & 0xff] ^ (crc >>> 8);
272
+ }
273
+ return (crc ^ 0xffffffff) >>> 0;
274
+ }
275
+ /** zlib 的 Adler-32。 */
276
+ function adler32(data, start, end) {
277
+ let a = 1;
278
+ let b = 0;
279
+ for (let i = start; i < end; i += 1) {
280
+ a = (a + data[i]) % 65521;
281
+ b = (b + a) % 65521;
282
+ }
283
+ return ((b << 16) | a) >>> 0;
284
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * path 模块的公开类型。
3
+ *
4
+ * 独立成文件的原因:path.ts 用 `export =` 导出模块本体,无法再导出类型;
5
+ * 类型放在这里既能让 index.ts 聚合时"能命名",也能让消费者引用。
6
+ */
7
+ export interface FormatInputPathObject {
8
+ root?: string;
9
+ dir?: string;
10
+ base?: string;
11
+ ext?: string;
12
+ name?: string;
13
+ }
14
+ export interface ParsedPath {
15
+ root: string;
16
+ dir: string;
17
+ base: string;
18
+ ext: string;
19
+ name: string;
20
+ }
21
+ export interface PathModule {
22
+ resolve(...paths: string[]): string;
23
+ normalize(path: string): string;
24
+ isAbsolute(path: string): boolean;
25
+ join(...paths: string[]): string;
26
+ relative(from: string, to: string): string;
27
+ toNamespacedPath(path: string): string;
28
+ dirname(path: string): string;
29
+ basename(path: string, ext?: string): string;
30
+ extname(path: string): string;
31
+ format(pathObject: FormatInputPathObject): string;
32
+ parse(path: string): ParsedPath;
33
+ sep: string;
34
+ delimiter: string;
35
+ win32: PathModule;
36
+ posix: PathModule;
37
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ export declare function pluginConfigDir(packageId?: string): string;
2
+ /** 当前包名:由 getPluginConfigDir('') 的返回路径反推。 */
3
+ export declare function packageName(): string;
4
+ export declare function downloadDir(): string;
5
+ export declare function cleanOnExitDir(): string;
6
+ export declare function appContext(): unknown;
7
+ export declare function filesDir(): string;
8
+ export declare function cacheDir(): string;
@@ -0,0 +1,106 @@
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.pluginConfigDir = pluginConfigDir;
37
+ exports.packageName = packageName;
38
+ exports.downloadDir = downloadDir;
39
+ exports.cleanOnExitDir = cleanOnExitDir;
40
+ exports.appContext = appContext;
41
+ exports.filesDir = filesDir;
42
+ exports.cacheDir = cacheDir;
43
+ /**
44
+ * 运行时路径根的来源。只使用 Operit 已经声明的全局与本库的 bridge 层。
45
+ *
46
+ * 公开面(Operit 的 types/index.d.ts 已声明):
47
+ * getPluginConfigDir(id) -> /sdcard/Download/Operit/plugins/<id>
48
+ * OPERIT_DOWNLOAD_DIR / OPERIT_CLEAN_ON_EXIT_DIR
49
+ * 内部面(源码里真实存在、d.ts 未声明):
50
+ * getPluginConfigDir('') 会回落到当前 call 的包名 —— packageName() 依赖这一行为。
51
+ * 该依赖集中在本模块,若将来失效只需改这一处。
52
+ */
53
+ const pathModule = require("../path");
54
+ const bridge = __importStar(require("./bridge"));
55
+ const errors_1 = require("./errors");
56
+ function resolvePluginConfigDir(packageId) {
57
+ if (typeof getPluginConfigDir !== 'function') {
58
+ throw (0, errors_1.onjError)('ONJ_MISSING_HOST', '全局 getPluginConfigDir 不可用。');
59
+ }
60
+ const dir = getPluginConfigDir(packageId);
61
+ if (typeof dir !== 'string' || dir.length === 0) {
62
+ throw (0, errors_1.onjError)('ONJ_MISSING_HOST', 'getPluginConfigDir(' + (packageId.length > 0 ? packageId : '<当前包>') + ') 返回空。');
63
+ }
64
+ return dir;
65
+ }
66
+ function pluginConfigDir(packageId) {
67
+ return resolvePluginConfigDir(packageId === undefined ? '' : String(packageId));
68
+ }
69
+ let cachedPackageName = null;
70
+ /** 当前包名:由 getPluginConfigDir('') 的返回路径反推。 */
71
+ function packageName() {
72
+ if (cachedPackageName === null) {
73
+ cachedPackageName = pathModule.basename(pluginConfigDir());
74
+ }
75
+ return cachedPackageName;
76
+ }
77
+ function downloadDir() {
78
+ if (typeof OPERIT_DOWNLOAD_DIR === 'string' && OPERIT_DOWNLOAD_DIR.length > 0) {
79
+ return OPERIT_DOWNLOAD_DIR;
80
+ }
81
+ throw (0, errors_1.onjError)('ONJ_MISSING_HOST', '全局 OPERIT_DOWNLOAD_DIR 不可用。');
82
+ }
83
+ function cleanOnExitDir() {
84
+ if (typeof OPERIT_CLEAN_ON_EXIT_DIR === 'string' && OPERIT_CLEAN_ON_EXIT_DIR.length > 0) {
85
+ return OPERIT_CLEAN_ON_EXIT_DIR;
86
+ }
87
+ throw (0, errors_1.onjError)('ONJ_MISSING_HOST', '全局 OPERIT_CLEAN_ON_EXIT_DIR 不可用。');
88
+ }
89
+ function appContext() {
90
+ return bridge.getApplicationContext();
91
+ }
92
+ function contextDir(methodName) {
93
+ const context = bridge.getApplicationContext();
94
+ const dir = bridge.callInstance(context, methodName, []);
95
+ const absolute = bridge.callInstance(dir, 'getAbsolutePath', []);
96
+ if (typeof absolute !== 'string' || absolute.length === 0) {
97
+ throw (0, errors_1.onjError)('ONJ_BRIDGE_PROTOCOL', 'Context.' + methodName + '() 未返回可用路径。');
98
+ }
99
+ return absolute;
100
+ }
101
+ function filesDir() {
102
+ return contextDir('getFilesDir');
103
+ }
104
+ function cacheDir() {
105
+ return contextDir('getCacheDir');
106
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * perf_hooks 的公开类型。
3
+ *
4
+ * 独立成文件的原因与其他 types 文件相同:perf_hooks.ts 用 `export =` 导出模块本体,
5
+ * 无法再导出类型,而 index.ts 聚合时要求这些名字是"可命名"的。
6
+ */
7
+ export interface PerformanceEntryLike {
8
+ name: string;
9
+ entryType: string;
10
+ startTime: number;
11
+ duration: number;
12
+ detail?: unknown;
13
+ }
14
+ export interface MeasureOptions {
15
+ start?: string | number;
16
+ end?: string | number;
17
+ detail?: unknown;
18
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,31 @@
1
+ export interface ProcessRunOptions {
2
+ cwd?: string;
3
+ env?: Record<string, string>;
4
+ }
5
+ export interface ProcessRunResult {
6
+ pid: number;
7
+ status: number | null;
8
+ stdout: string;
9
+ stderr: string;
10
+ }
11
+ export interface ProcessDriver {
12
+ /** 按 argv 起进程并等待结束;输出按 UTF-8 解码成文本。 */
13
+ run(argv: readonly string[], options: ProcessRunOptions): ProcessRunResult;
14
+ }
15
+ /** 宿主注入点:桌面测试用,传 null 恢复为 Operit Java 驱动。 */
16
+ export declare function setDriver(next: ProcessDriver | null): void;
17
+ export declare function getDriver(): ProcessDriver;
18
+ /**
19
+ * 基于 java.lang.ProcessBuilder 的驱动。
20
+ *
21
+ * 两处刻意的选择:
22
+ * - argv 用 JS 数组直接传:bridge 的 convertArg 对 `wrapper.isArray` 与 Collection 目标
23
+ * 都能把 JS 数组转成 Java 数组 / List,因此不需要手工拼命令行字符串;
24
+ * - 输出用 java.util.Scanner 读成 String:InputStream.readAllBytes() 是 Java 9,
25
+ * 在 Android 8~12 上不可用;Scanner 在所有 API 级别都在。
26
+ * 代价是输出按 UTF-8 解码,二进制输出会有损(已在 README 写明)。
27
+ *
28
+ * 已知限制:先读 stdout 再读 stderr,若子进程往 stderr 写超过管道缓冲(约 64KB)
29
+ * 会与我们的读取形成死锁。Node 用事件循环规避这一点,同步实现做不到。
30
+ */
31
+ export declare function createJavaDriver(): ProcessDriver;
@@ -0,0 +1,102 @@
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.setDriver = setDriver;
37
+ exports.getDriver = getDriver;
38
+ exports.createJavaDriver = createJavaDriver;
39
+ /**
40
+ * child_process 的宿主驱动层:把「怎么起进程」与「Node 的 child_process 语义」分开。
41
+ * 与 fs-driver 同一套思路 —— 桌面测试可以注入基于 Node child_process 的驱动,
42
+ * 从而把 child_process.ts 的语义与 Node 对拍,Java 相关的假设只收敛在一处。
43
+ *
44
+ * 实现一律是**同步**的:Java bridge 只有同步通道,没有真异步。
45
+ */
46
+ const bridge = __importStar(require("./bridge"));
47
+ let driver = null;
48
+ /** 宿主注入点:桌面测试用,传 null 恢复为 Operit Java 驱动。 */
49
+ function setDriver(next) {
50
+ driver = next;
51
+ }
52
+ function getDriver() {
53
+ if (driver === null) {
54
+ driver = createJavaDriver();
55
+ }
56
+ return driver;
57
+ }
58
+ /**
59
+ * 基于 java.lang.ProcessBuilder 的驱动。
60
+ *
61
+ * 两处刻意的选择:
62
+ * - argv 用 JS 数组直接传:bridge 的 convertArg 对 `wrapper.isArray` 与 Collection 目标
63
+ * 都能把 JS 数组转成 Java 数组 / List,因此不需要手工拼命令行字符串;
64
+ * - 输出用 java.util.Scanner 读成 String:InputStream.readAllBytes() 是 Java 9,
65
+ * 在 Android 8~12 上不可用;Scanner 在所有 API 级别都在。
66
+ * 代价是输出按 UTF-8 解码,二进制输出会有损(已在 README 写明)。
67
+ *
68
+ * 已知限制:先读 stdout 再读 stderr,若子进程往 stderr 写超过管道缓冲(约 64KB)
69
+ * 会与我们的读取形成死锁。Node 用事件循环规避这一点,同步实现做不到。
70
+ */
71
+ function createJavaDriver() {
72
+ function readAll(stream) {
73
+ const scanner = bridge.newInstance('java.util.Scanner', [stream, 'UTF-8']);
74
+ try {
75
+ if (!bridge.normalizeBoolean(bridge.callInstance(scanner, 'hasNext', []))) {
76
+ return '';
77
+ }
78
+ return String(bridge.callInstance(scanner, 'next', []));
79
+ }
80
+ finally {
81
+ bridge.callInstance(scanner, 'close', []);
82
+ }
83
+ }
84
+ return {
85
+ run(argv, options) {
86
+ const builder = bridge.newInstance('java.lang.ProcessBuilder', [argv.slice()]);
87
+ if (options.cwd !== undefined) {
88
+ bridge.callInstance(builder, 'directory', [bridge.newInstance('java.io.File', [options.cwd])]);
89
+ }
90
+ if (options.env !== undefined) {
91
+ const environment = bridge.callInstance(builder, 'environment', []);
92
+ bridge.callInstance(environment, 'putAll', [options.env]);
93
+ }
94
+ const process = bridge.callInstance(builder, 'start', []);
95
+ const stdout = readAll(bridge.callInstance(process, 'getInputStream', []));
96
+ const stderr = readAll(bridge.callInstance(process, 'getErrorStream', []));
97
+ const status = Number(bridge.callInstance(process, 'waitFor', []));
98
+ const pid = Number(bridge.callInstance(process, 'pid', [])) || 0;
99
+ return { pid: pid, status: status, stdout: stdout, stderr: stderr };
100
+ }
101
+ };
102
+ }
@@ -0,0 +1,8 @@
1
+ /** 把一段 Unicode 字符串编成 punycode(不含 xn-- 前缀)。 */
2
+ export declare function encode(input: string): string;
3
+ /** 把 punycode(不含 xn-- 前缀)解回 Unicode 字符串。非法输入抛错。 */
4
+ export declare function decode(input: string): string;
5
+ /** 域名 -> ASCII(xn--)。纯 ASCII 输入原样返回(已小写化由调用方负责)。 */
6
+ export declare function toASCII(domain: string): string;
7
+ /** 域名 -> Unicode。xn-- 标签解回 Unicode,其余原样。 */
8
+ export declare function toUnicode(domain: string): string;