@serveryyswys/node.operit 0.0.0-stage → 0.1.3
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/BUILTINS.json +383 -0
- package/DESIGN.md +513 -0
- package/LICENSE +201 -0
- package/NOTICE +17 -0
- package/README.md +311 -2
- package/dist/assert/strict.d.ts +2 -0
- package/dist/assert/strict.js +7 -0
- package/dist/assert.d.ts +3 -0
- package/dist/assert.js +483 -0
- package/dist/buffer.d.ts +60 -0
- package/dist/buffer.js +524 -0
- package/dist/child_process.d.ts +56 -0
- package/dist/child_process.js +371 -0
- package/dist/console.d.ts +60 -0
- package/dist/console.js +193 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +19 -0
- package/dist/crypto.d.ts +76 -0
- package/dist/crypto.js +312 -0
- package/dist/diagnostics_channel.d.ts +36 -0
- package/dist/diagnostics_channel.js +76 -0
- package/dist/dns/promises.d.ts +34 -0
- package/dist/dns/promises.js +190 -0
- package/dist/dns.d.ts +88 -0
- package/dist/dns.js +259 -0
- package/dist/events.d.ts +40 -0
- package/dist/events.js +237 -0
- package/dist/fs/promises.d.ts +160 -0
- package/dist/fs/promises.js +130 -0
- package/dist/fs.d.ts +221 -0
- package/dist/fs.js +944 -0
- package/dist/http.d.ts +92 -0
- package/dist/http.js +319 -0
- package/dist/https.d.ts +296 -0
- package/dist/https.js +50 -0
- package/dist/index.d.ts +2821 -0
- package/dist/index.js +93 -0
- package/dist/lib/bridge.d.ts +60 -0
- package/dist/lib/bridge.js +207 -0
- package/dist/lib/bytes.d.ts +20 -0
- package/dist/lib/bytes.js +239 -0
- package/dist/lib/child-process-types.d.ts +45 -0
- package/dist/lib/child-process-types.js +2 -0
- package/dist/lib/console-types.d.ts +12 -0
- package/dist/lib/console-types.js +2 -0
- package/dist/lib/cwd.d.ts +2 -0
- package/dist/lib/cwd.js +32 -0
- package/dist/lib/deep-equal.d.ts +3 -0
- package/dist/lib/deep-equal.js +223 -0
- package/dist/lib/deflate.d.ts +2 -0
- package/dist/lib/deflate.js +234 -0
- package/dist/lib/diagnostics-channel-types.d.ts +7 -0
- package/dist/lib/diagnostics-channel-types.js +2 -0
- package/dist/lib/dns-driver.d.ts +14 -0
- package/dist/lib/dns-driver.js +85 -0
- package/dist/lib/dns-types.d.ts +22 -0
- package/dist/lib/dns-types.js +2 -0
- package/dist/lib/errors.d.ts +48 -0
- package/dist/lib/errors.js +424 -0
- package/dist/lib/fs-driver.d.ts +70 -0
- package/dist/lib/fs-driver.js +364 -0
- package/dist/lib/fs-env.d.ts +30 -0
- package/dist/lib/fs-env.js +157 -0
- package/dist/lib/fs-types.d.ts +19 -0
- package/dist/lib/fs-types.js +2 -0
- package/dist/lib/hashes.d.ts +27 -0
- package/dist/lib/hashes.js +432 -0
- package/dist/lib/http-driver.d.ts +30 -0
- package/dist/lib/http-driver.js +72 -0
- package/dist/lib/http-types.d.ts +18 -0
- package/dist/lib/http-types.js +2 -0
- package/dist/lib/inflate.d.ts +6 -0
- package/dist/lib/inflate.js +284 -0
- package/dist/lib/path-types.d.ts +37 -0
- package/dist/lib/path-types.js +2 -0
- package/dist/lib/paths.d.ts +8 -0
- package/dist/lib/paths.js +106 -0
- package/dist/lib/perf-hooks-types.d.ts +18 -0
- package/dist/lib/perf-hooks-types.js +2 -0
- package/dist/lib/process-driver.d.ts +31 -0
- package/dist/lib/process-driver.js +102 -0
- package/dist/lib/punycode.d.ts +8 -0
- package/dist/lib/punycode.js +179 -0
- package/dist/lib/stream-consumers-types.d.ts +4 -0
- package/dist/lib/stream-consumers-types.js +2 -0
- package/dist/lib/stream-types.d.ts +29 -0
- package/dist/lib/stream-types.js +2 -0
- package/dist/lib/timers-types.d.ts +18 -0
- package/dist/lib/timers-types.js +2 -0
- package/dist/lib/url-search-params.d.ts +26 -0
- package/dist/lib/url-search-params.js +281 -0
- package/dist/lib/url-types.d.ts +44 -0
- package/dist/lib/url-types.js +2 -0
- package/dist/lib/url-whatwg.d.ts +78 -0
- package/dist/lib/url-whatwg.js +857 -0
- package/dist/lib/util-types.d.ts +13 -0
- package/dist/lib/util-types.js +2 -0
- package/dist/lib/v8-types.d.ts +30 -0
- package/dist/lib/v8-types.js +2 -0
- package/dist/lib/zlib-types.d.ts +12 -0
- package/dist/lib/zlib-types.js +2 -0
- package/dist/module.d.ts +8 -0
- package/dist/module.js +102 -0
- package/dist/os.d.ts +80 -0
- package/dist/os.js +247 -0
- package/dist/path/posix.d.ts +2 -0
- package/dist/path/posix.js +7 -0
- package/dist/path/win32.d.ts +2 -0
- package/dist/path/win32.js +10 -0
- package/dist/path.d.ts +3 -0
- package/dist/path.js +1222 -0
- package/dist/perf_hooks.d.ts +36 -0
- package/dist/perf_hooks.js +144 -0
- package/dist/process.d.ts +69 -0
- package/dist/process.js +230 -0
- package/dist/punycode.d.ts +22 -0
- package/dist/punycode.js +53 -0
- package/dist/querystring.d.ts +15 -0
- package/dist/querystring.js +209 -0
- package/dist/stream/consumers.d.ts +24 -0
- package/dist/stream/consumers.js +75 -0
- package/dist/stream/promises.d.ts +5 -0
- package/dist/stream/promises.js +10 -0
- package/dist/stream.d.ts +84 -0
- package/dist/stream.js +875 -0
- package/dist/string_decoder.d.ts +15 -0
- package/dist/string_decoder.js +237 -0
- package/dist/sys.d.ts +8 -0
- package/dist/sys.js +9 -0
- package/dist/timers/promises.d.ts +19 -0
- package/dist/timers/promises.js +74 -0
- package/dist/timers.d.ts +21 -0
- package/dist/timers.js +41 -0
- package/dist/tty.d.ts +9 -0
- package/dist/tty.js +25 -0
- package/dist/unsupported.d.ts +12 -0
- package/dist/unsupported.js +17 -0
- package/dist/url.d.ts +28 -0
- package/dist/url.js +486 -0
- package/dist/util/types.d.ts +16 -0
- package/dist/util/types.js +9 -0
- package/dist/util.d.ts +63 -0
- package/dist/util.js +527 -0
- package/dist/v8.d.ts +25 -0
- package/dist/v8.js +127 -0
- package/dist/zlib.d.ts +88 -0
- package/dist/zlib.js +371 -0
- package/package.json +31 -4
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Buffer } from './buffer';
|
|
2
|
+
import type { HashSpec } from './lib/hashes';
|
|
3
|
+
declare class Hash {
|
|
4
|
+
readonly spec: HashSpec;
|
|
5
|
+
chunks: Uint8Array[];
|
|
6
|
+
total: number;
|
|
7
|
+
finalized: boolean;
|
|
8
|
+
constructor(algorithm: string);
|
|
9
|
+
update(data: unknown, inputEncoding?: string): this;
|
|
10
|
+
digest(encoding?: string): string | Buffer;
|
|
11
|
+
copy(): Hash;
|
|
12
|
+
}
|
|
13
|
+
declare class Hmac {
|
|
14
|
+
readonly spec: HashSpec;
|
|
15
|
+
readonly key: Uint8Array;
|
|
16
|
+
chunks: Uint8Array[];
|
|
17
|
+
total: number;
|
|
18
|
+
finalized: boolean;
|
|
19
|
+
constructor(algorithm: string, key: unknown, keyEncoding?: string);
|
|
20
|
+
update(data: unknown, inputEncoding?: string): this;
|
|
21
|
+
digest(encoding?: string): string | Buffer;
|
|
22
|
+
}
|
|
23
|
+
declare function createHash(algorithm: string, options?: {
|
|
24
|
+
outputLength?: number;
|
|
25
|
+
}): Hash;
|
|
26
|
+
declare function createHmac(algorithm: string, key: unknown, options?: {
|
|
27
|
+
encoding?: string;
|
|
28
|
+
}): Hmac;
|
|
29
|
+
declare function randomBytes(size: number, callback?: (error: Error | null, buffer: Buffer) => void): Buffer | void;
|
|
30
|
+
declare function randomFillSync(buffer: Uint8Array): Uint8Array;
|
|
31
|
+
declare function randomUUID(): string;
|
|
32
|
+
declare function randomInt(min: number, max?: number | ((error: Error | null, value: number) => void), callback?: (error: Error | null, value: number) => void): number | void;
|
|
33
|
+
declare function timingSafeEqual(left: unknown, right: unknown): boolean;
|
|
34
|
+
declare function pbkdf2Sync(password: unknown, salt: unknown, iterations: number, keylen: number, digest: string): Buffer;
|
|
35
|
+
declare function pbkdf2(password: unknown, salt: unknown, iterations: number, keylen: number, digest: string, callback: (error: Error | null, derivedKey: Buffer) => void): void;
|
|
36
|
+
declare function getHashes(): string[];
|
|
37
|
+
declare function getCiphers(): string[];
|
|
38
|
+
declare function createCipheriv(): never;
|
|
39
|
+
declare function createDecipheriv(): never;
|
|
40
|
+
declare function createSign(): never;
|
|
41
|
+
declare function createVerify(): never;
|
|
42
|
+
declare function generateKeyPairSync(): never;
|
|
43
|
+
declare function createDiffieHellman(): never;
|
|
44
|
+
declare const crypto: {
|
|
45
|
+
createHash: typeof createHash;
|
|
46
|
+
createHmac: typeof createHmac;
|
|
47
|
+
randomBytes: typeof randomBytes;
|
|
48
|
+
randomFillSync: typeof randomFillSync;
|
|
49
|
+
randomUUID: typeof randomUUID;
|
|
50
|
+
randomInt: typeof randomInt;
|
|
51
|
+
timingSafeEqual: typeof timingSafeEqual;
|
|
52
|
+
pbkdf2Sync: typeof pbkdf2Sync;
|
|
53
|
+
pbkdf2: typeof pbkdf2;
|
|
54
|
+
getHashes: typeof getHashes;
|
|
55
|
+
getCiphers: typeof getCiphers;
|
|
56
|
+
createCipheriv: typeof createCipheriv;
|
|
57
|
+
createDecipheriv: typeof createDecipheriv;
|
|
58
|
+
createSign: typeof createSign;
|
|
59
|
+
createVerify: typeof createVerify;
|
|
60
|
+
generateKeyPairSync: typeof generateKeyPairSync;
|
|
61
|
+
createDiffieHellman: typeof createDiffieHellman;
|
|
62
|
+
constants: {
|
|
63
|
+
RSA_PKCS1_PADDING: number;
|
|
64
|
+
RSA_NO_PADDING: number;
|
|
65
|
+
RSA_PKCS1_OAEP_PADDING: number;
|
|
66
|
+
RSA_X931_PADDING: number;
|
|
67
|
+
RSA_PKCS1_PSS_PADDING: number;
|
|
68
|
+
SSL_OP_ALL: number;
|
|
69
|
+
SSL_OP_NO_SSLv2: number;
|
|
70
|
+
SSL_OP_NO_SSLv3: number;
|
|
71
|
+
SSL_OP_NO_TLSv1: number;
|
|
72
|
+
};
|
|
73
|
+
Hash: typeof Hash;
|
|
74
|
+
Hmac: typeof Hmac;
|
|
75
|
+
};
|
|
76
|
+
export = crypto;
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
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
|
+
/**
|
|
36
|
+
* Node crypto 模块的移植。
|
|
37
|
+
*
|
|
38
|
+
* 覆盖范围:
|
|
39
|
+
* createHash / createHmac / pbkdf2Sync / pbkdf2 / randomBytes / randomFillSync
|
|
40
|
+
* randomUUID / randomInt / timingSafeEqual / getHashes / constants
|
|
41
|
+
*
|
|
42
|
+
* 不覆盖(调用即显式抛错,不静默):createCipheriv / createDecipheriv / createSign /
|
|
43
|
+
* createVerify / generateKeyPair(一个) / createDiffieHellman —— 见 BUILTINS.json。
|
|
44
|
+
*
|
|
45
|
+
* 随机数来源用 java.util.UUID.randomUUID():它由 SecureRandom 支撑,
|
|
46
|
+
* 每次调用给 16 字节强熵,而且**不需要往 bridge 传 byte[]**(那是本库最不确定的一段)。
|
|
47
|
+
*
|
|
48
|
+
* 哈希是纯 JS 实现(lib/hashes),因为运行时注入的 CryptoJS 只是个 4KB shim,
|
|
49
|
+
* 只有 MD5(string) 与 AES.decrypt,撑不起 crypto 模块。
|
|
50
|
+
*/
|
|
51
|
+
const bridge = __importStar(require("./lib/bridge"));
|
|
52
|
+
const hashes = __importStar(require("./lib/hashes"));
|
|
53
|
+
const buffer_1 = require("./buffer");
|
|
54
|
+
const errors_1 = require("./lib/errors");
|
|
55
|
+
function toBytes(data, encoding) {
|
|
56
|
+
if (typeof data === 'string') {
|
|
57
|
+
return buffer_1.Buffer.from(data, (encoding === undefined ? 'utf8' : encoding));
|
|
58
|
+
}
|
|
59
|
+
if (data instanceof Uint8Array) {
|
|
60
|
+
return data;
|
|
61
|
+
}
|
|
62
|
+
if (data instanceof ArrayBuffer) {
|
|
63
|
+
return new Uint8Array(data);
|
|
64
|
+
}
|
|
65
|
+
throw new TypeError('The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.');
|
|
66
|
+
}
|
|
67
|
+
function concatChunks(chunks, totalLength) {
|
|
68
|
+
const out = new Uint8Array(totalLength);
|
|
69
|
+
let offset = 0;
|
|
70
|
+
for (let i = 0; i < chunks.length; i += 1) {
|
|
71
|
+
out.set(chunks[i], offset);
|
|
72
|
+
offset += chunks[i].length;
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
class Hash {
|
|
77
|
+
constructor(algorithm) {
|
|
78
|
+
this.chunks = [];
|
|
79
|
+
this.total = 0;
|
|
80
|
+
this.finalized = false;
|
|
81
|
+
this.spec = hashes.resolveAlgorithm(algorithm);
|
|
82
|
+
}
|
|
83
|
+
update(data, inputEncoding) {
|
|
84
|
+
if (this.finalized) {
|
|
85
|
+
throw (0, errors_1.onjError)('ONJ_UNSUPPORTED', 'Hash.digest() 之后不能再 update()。');
|
|
86
|
+
}
|
|
87
|
+
const bytes = toBytes(data, inputEncoding);
|
|
88
|
+
this.chunks.push(bytes);
|
|
89
|
+
this.total += bytes.length;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
digest(encoding) {
|
|
93
|
+
if (this.finalized) {
|
|
94
|
+
throw (0, errors_1.onjError)('ONJ_UNSUPPORTED', 'Hash.digest() 只能调用一次。');
|
|
95
|
+
}
|
|
96
|
+
this.finalized = true;
|
|
97
|
+
const result = buffer_1.Buffer.from(this.spec.compute(concatChunks(this.chunks, this.total)));
|
|
98
|
+
this.chunks = [];
|
|
99
|
+
this.total = 0;
|
|
100
|
+
return encoding === undefined ? result : result.toString(encoding);
|
|
101
|
+
}
|
|
102
|
+
copy() {
|
|
103
|
+
const clone = new Hash(this.spec.name);
|
|
104
|
+
clone.chunks = this.chunks.slice();
|
|
105
|
+
clone.total = this.total;
|
|
106
|
+
return clone;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
class Hmac {
|
|
110
|
+
constructor(algorithm, key, keyEncoding) {
|
|
111
|
+
this.chunks = [];
|
|
112
|
+
this.total = 0;
|
|
113
|
+
this.finalized = false;
|
|
114
|
+
this.spec = hashes.resolveAlgorithm(algorithm);
|
|
115
|
+
this.key = toBytes(key, keyEncoding);
|
|
116
|
+
}
|
|
117
|
+
update(data, inputEncoding) {
|
|
118
|
+
if (this.finalized) {
|
|
119
|
+
throw (0, errors_1.onjError)('ONJ_UNSUPPORTED', 'Hmac.digest() 之后不能再 update()。');
|
|
120
|
+
}
|
|
121
|
+
const bytes = toBytes(data, inputEncoding);
|
|
122
|
+
this.chunks.push(bytes);
|
|
123
|
+
this.total += bytes.length;
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
digest(encoding) {
|
|
127
|
+
if (this.finalized) {
|
|
128
|
+
throw (0, errors_1.onjError)('ONJ_UNSUPPORTED', 'Hmac.digest() 只能调用一次。');
|
|
129
|
+
}
|
|
130
|
+
this.finalized = true;
|
|
131
|
+
const message = concatChunks(this.chunks, this.total);
|
|
132
|
+
const result = buffer_1.Buffer.from(hashes.hmac(this.spec, this.key, message));
|
|
133
|
+
this.chunks = [];
|
|
134
|
+
this.total = 0;
|
|
135
|
+
return encoding === undefined ? result : result.toString(encoding);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function createHash(algorithm, options) {
|
|
139
|
+
void options;
|
|
140
|
+
return new Hash(algorithm);
|
|
141
|
+
}
|
|
142
|
+
function createHmac(algorithm, key, options) {
|
|
143
|
+
return new Hmac(algorithm, key, options === undefined ? undefined : options.encoding);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 用 UUID.randomUUID() 填满目标缓冲区:每次 16 字节强熵,且不需要传 byte[]。
|
|
147
|
+
*/
|
|
148
|
+
function fillRandom(target) {
|
|
149
|
+
let offset = 0;
|
|
150
|
+
while (offset < target.length) {
|
|
151
|
+
const uuid = bridge.callStatic('java.util.UUID', 'randomUUID', []);
|
|
152
|
+
const text = String(bridge.callInstance(uuid, 'toString', []));
|
|
153
|
+
const hex = text.replace(/-/g, '');
|
|
154
|
+
for (let i = 0; i + 1 < hex.length && offset < target.length; i += 2) {
|
|
155
|
+
target[offset] = parseInt(hex.slice(i, i + 2), 16);
|
|
156
|
+
offset += 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return target;
|
|
160
|
+
}
|
|
161
|
+
function randomBytes(size, callback) {
|
|
162
|
+
if (typeof size !== 'number' || size < 0 || !isFinite(size)) {
|
|
163
|
+
throw new RangeError('The value of "size" is out of range.');
|
|
164
|
+
}
|
|
165
|
+
const buffer = buffer_1.Buffer.from(fillRandom(new Uint8Array(Math.floor(size))));
|
|
166
|
+
if (callback !== undefined) {
|
|
167
|
+
callback(null, buffer);
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
return buffer;
|
|
171
|
+
}
|
|
172
|
+
function randomFillSync(buffer) {
|
|
173
|
+
if (!(buffer instanceof Uint8Array)) {
|
|
174
|
+
throw new TypeError('The "buffer" argument must be an instance of Buffer or Uint8Array.');
|
|
175
|
+
}
|
|
176
|
+
return fillRandom(buffer);
|
|
177
|
+
}
|
|
178
|
+
function randomUUID() {
|
|
179
|
+
const uuid = bridge.callStatic('java.util.UUID', 'randomUUID', []);
|
|
180
|
+
return String(bridge.callInstance(uuid, 'toString', []));
|
|
181
|
+
}
|
|
182
|
+
function unbiasedBelow(limit) {
|
|
183
|
+
if (limit <= 0) {
|
|
184
|
+
throw new RangeError('The value of "max" is out of range.');
|
|
185
|
+
}
|
|
186
|
+
if (limit === 1) {
|
|
187
|
+
return 0;
|
|
188
|
+
}
|
|
189
|
+
// 48 位样本 + 拒绝采样,避免取模偏置
|
|
190
|
+
const ceiling = Math.floor(281474976710656 / limit) * limit;
|
|
191
|
+
const sample = new Uint8Array(6);
|
|
192
|
+
for (;;) {
|
|
193
|
+
fillRandom(sample);
|
|
194
|
+
let value = 0;
|
|
195
|
+
for (let i = 0; i < 6; i += 1) {
|
|
196
|
+
value = value * 256 + sample[i];
|
|
197
|
+
}
|
|
198
|
+
if (value < ceiling) {
|
|
199
|
+
return value % limit;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function randomInt(min, max, callback) {
|
|
204
|
+
// Node 有 randomInt(max) 与 randomInt(min, max) 两种形式
|
|
205
|
+
let low = 0;
|
|
206
|
+
let high;
|
|
207
|
+
let done = callback;
|
|
208
|
+
if (typeof max === 'function') {
|
|
209
|
+
done = max;
|
|
210
|
+
high = min;
|
|
211
|
+
}
|
|
212
|
+
else if (max === undefined) {
|
|
213
|
+
high = min;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
low = min;
|
|
217
|
+
high = max;
|
|
218
|
+
}
|
|
219
|
+
if (high <= low) {
|
|
220
|
+
throw new RangeError('The value of "max" is out of range. It must be greater than the value of "min".');
|
|
221
|
+
}
|
|
222
|
+
const value = low + unbiasedBelow(high - low);
|
|
223
|
+
if (done !== undefined) {
|
|
224
|
+
done(null, value);
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
return value;
|
|
228
|
+
}
|
|
229
|
+
function timingSafeEqual(left, right) {
|
|
230
|
+
if (!(left instanceof Uint8Array) || !(right instanceof Uint8Array)) {
|
|
231
|
+
throw new TypeError('The "buf1" and "buf2" arguments must be Buffer or Uint8Array.');
|
|
232
|
+
}
|
|
233
|
+
if (left.length !== right.length) {
|
|
234
|
+
const error = new RangeError('Input buffers must have the same byte length');
|
|
235
|
+
error.code = 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH';
|
|
236
|
+
throw error;
|
|
237
|
+
}
|
|
238
|
+
let difference = 0;
|
|
239
|
+
for (let i = 0; i < left.length; i += 1) {
|
|
240
|
+
difference |= left[i] ^ right[i];
|
|
241
|
+
}
|
|
242
|
+
return difference === 0;
|
|
243
|
+
}
|
|
244
|
+
function pbkdf2Sync(password, salt, iterations, keylen, digest) {
|
|
245
|
+
const spec = hashes.resolveAlgorithm(digest);
|
|
246
|
+
return buffer_1.Buffer.from(hashes.pbkdf2(spec, toBytes(password, 'utf8'), toBytes(salt, 'utf8'), iterations, keylen));
|
|
247
|
+
}
|
|
248
|
+
function pbkdf2(password, salt, iterations, keylen, digest, callback) {
|
|
249
|
+
callback(null, pbkdf2Sync(password, salt, iterations, keylen, digest));
|
|
250
|
+
}
|
|
251
|
+
function getHashes() {
|
|
252
|
+
return hashes.hashNames();
|
|
253
|
+
}
|
|
254
|
+
function getCiphers() {
|
|
255
|
+
// 未实现任何分组密码,因此这里如实为空
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
function unsupported(name) {
|
|
259
|
+
throw (0, errors_1.onjError)('ONJ_UNSUPPORTED', 'crypto.' + name + '() 尚未实现(见 BUILTINS.json)。');
|
|
260
|
+
}
|
|
261
|
+
function createCipheriv() {
|
|
262
|
+
return unsupported('createCipheriv');
|
|
263
|
+
}
|
|
264
|
+
function createDecipheriv() {
|
|
265
|
+
return unsupported('createDecipheriv');
|
|
266
|
+
}
|
|
267
|
+
function createSign() {
|
|
268
|
+
return unsupported('createSign');
|
|
269
|
+
}
|
|
270
|
+
function createVerify() {
|
|
271
|
+
return unsupported('createVerify');
|
|
272
|
+
}
|
|
273
|
+
function generateKeyPairSync() {
|
|
274
|
+
return unsupported('generateKeyPairSync');
|
|
275
|
+
}
|
|
276
|
+
function createDiffieHellman() {
|
|
277
|
+
return unsupported('createDiffieHellman');
|
|
278
|
+
}
|
|
279
|
+
const constants = {
|
|
280
|
+
RSA_PKCS1_PADDING: 1,
|
|
281
|
+
RSA_NO_PADDING: 3,
|
|
282
|
+
RSA_PKCS1_OAEP_PADDING: 4,
|
|
283
|
+
RSA_X931_PADDING: 5,
|
|
284
|
+
RSA_PKCS1_PSS_PADDING: 6,
|
|
285
|
+
SSL_OP_ALL: 0,
|
|
286
|
+
SSL_OP_NO_SSLv2: 0,
|
|
287
|
+
SSL_OP_NO_SSLv3: 0,
|
|
288
|
+
SSL_OP_NO_TLSv1: 0
|
|
289
|
+
};
|
|
290
|
+
const crypto = {
|
|
291
|
+
createHash: createHash,
|
|
292
|
+
createHmac: createHmac,
|
|
293
|
+
randomBytes: randomBytes,
|
|
294
|
+
randomFillSync: randomFillSync,
|
|
295
|
+
randomUUID: randomUUID,
|
|
296
|
+
randomInt: randomInt,
|
|
297
|
+
timingSafeEqual: timingSafeEqual,
|
|
298
|
+
pbkdf2Sync: pbkdf2Sync,
|
|
299
|
+
pbkdf2: pbkdf2,
|
|
300
|
+
getHashes: getHashes,
|
|
301
|
+
getCiphers: getCiphers,
|
|
302
|
+
createCipheriv: createCipheriv,
|
|
303
|
+
createDecipheriv: createDecipheriv,
|
|
304
|
+
createSign: createSign,
|
|
305
|
+
createVerify: createVerify,
|
|
306
|
+
generateKeyPairSync: generateKeyPairSync,
|
|
307
|
+
createDiffieHellman: createDiffieHellman,
|
|
308
|
+
constants: constants,
|
|
309
|
+
Hash: Hash,
|
|
310
|
+
Hmac: Hmac
|
|
311
|
+
};
|
|
312
|
+
module.exports = crypto;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* diagnostics_channel。
|
|
3
|
+
*
|
|
4
|
+
* 它的语义是纯粹的「按名字的发布订阅」,本身不依赖任何平台能力,所以可以完整实现。
|
|
5
|
+
*
|
|
6
|
+
* 与 Node 的差异(都写进 BUILTINS.json):
|
|
7
|
+
* - Channel 的 bindStore / unbindStore / runStores 依赖 AsyncLocalStorage(需要 async_hooks),
|
|
8
|
+
* Android 的 QuickJS 运行时没有物化这些钩子,所以这三个方法显式抛 ONJ_UNSUPPORTED,
|
|
9
|
+
* 而不是给一个"看着像但语义不对"的空实现;
|
|
10
|
+
* - tracingChannel 未实现(依赖上面那组 API),同样显式拒绝。
|
|
11
|
+
*/
|
|
12
|
+
import { Subscriber, ChannelStore } from './lib/diagnostics-channel-types';
|
|
13
|
+
declare class Channel {
|
|
14
|
+
constructor(name: string);
|
|
15
|
+
readonly name: string;
|
|
16
|
+
subscribers: Subscriber[];
|
|
17
|
+
get hasSubscribers(): boolean;
|
|
18
|
+
subscribe(onMessage: Subscriber): void;
|
|
19
|
+
unsubscribe(onMessage: Subscriber): void;
|
|
20
|
+
publish(message: unknown): void;
|
|
21
|
+
bindStore(_store: ChannelStore, _transform?: unknown): void;
|
|
22
|
+
unbindStore(_store: ChannelStore): void;
|
|
23
|
+
runStores(_context: unknown, _fn: Function, _thisArg?: unknown, ..._args: unknown[]): unknown;
|
|
24
|
+
}
|
|
25
|
+
declare function channel(name: string): Channel;
|
|
26
|
+
declare function hasSubscribers(name: string): boolean;
|
|
27
|
+
declare function subscribe(name: string, onMessage: Subscriber): void;
|
|
28
|
+
declare function unsubscribe(name: string, onMessage: Subscriber): void;
|
|
29
|
+
declare const diagnosticsChannel: {
|
|
30
|
+
channel: typeof channel;
|
|
31
|
+
hasSubscribers: typeof hasSubscribers;
|
|
32
|
+
subscribe: typeof subscribe;
|
|
33
|
+
unsubscribe: typeof unsubscribe;
|
|
34
|
+
Channel: typeof Channel;
|
|
35
|
+
};
|
|
36
|
+
export = diagnosticsChannel;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const channels = new Map();
|
|
3
|
+
class Channel {
|
|
4
|
+
constructor(name) {
|
|
5
|
+
this.subscribers = [];
|
|
6
|
+
this.name = name;
|
|
7
|
+
}
|
|
8
|
+
get hasSubscribers() {
|
|
9
|
+
return this.subscribers.length > 0;
|
|
10
|
+
}
|
|
11
|
+
subscribe(onMessage) {
|
|
12
|
+
if (typeof onMessage !== 'function') {
|
|
13
|
+
throw new TypeError('The "onMessage" argument must be of type function.');
|
|
14
|
+
}
|
|
15
|
+
if (this.subscribers.indexOf(onMessage) < 0) {
|
|
16
|
+
this.subscribers.push(onMessage);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
unsubscribe(onMessage) {
|
|
20
|
+
const index = this.subscribers.indexOf(onMessage);
|
|
21
|
+
if (index >= 0) {
|
|
22
|
+
this.subscribers.splice(index, 1);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
publish(message) {
|
|
26
|
+
const listeners = this.subscribers.slice();
|
|
27
|
+
for (let index = 0; index < listeners.length; index += 1) {
|
|
28
|
+
listeners[index](message, this.name);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
bindStore(_store, _transform) {
|
|
32
|
+
throw onjUnsupported('diagnostics_channel.Channel.bindStore');
|
|
33
|
+
}
|
|
34
|
+
unbindStore(_store) {
|
|
35
|
+
throw onjUnsupported('diagnostics_channel.Channel.unbindStore');
|
|
36
|
+
}
|
|
37
|
+
runStores(_context, _fn, _thisArg, ..._args) {
|
|
38
|
+
throw onjUnsupported('diagnostics_channel.Channel.runStores');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function onjUnsupported(feature) {
|
|
42
|
+
const failure = new Error(feature + ' 需要 AsyncLocalStorage(async_hooks),Android 的 QuickJS 运行时不提供异步钩子。');
|
|
43
|
+
failure.code = 'ONJ_UNSUPPORTED';
|
|
44
|
+
return failure;
|
|
45
|
+
}
|
|
46
|
+
function channel(name) {
|
|
47
|
+
if (typeof name !== 'string' && typeof name !== 'symbol') {
|
|
48
|
+
throw new TypeError('The "channel" argument must be of type string or symbol.');
|
|
49
|
+
}
|
|
50
|
+
const key = String(name);
|
|
51
|
+
const existing = channels.get(key);
|
|
52
|
+
if (existing !== undefined) {
|
|
53
|
+
return existing;
|
|
54
|
+
}
|
|
55
|
+
const created = new Channel(key);
|
|
56
|
+
channels.set(key, created);
|
|
57
|
+
return created;
|
|
58
|
+
}
|
|
59
|
+
function hasSubscribers(name) {
|
|
60
|
+
const existing = channels.get(String(name));
|
|
61
|
+
return existing !== undefined && existing.hasSubscribers;
|
|
62
|
+
}
|
|
63
|
+
function subscribe(name, onMessage) {
|
|
64
|
+
channel(name).subscribe(onMessage);
|
|
65
|
+
}
|
|
66
|
+
function unsubscribe(name, onMessage) {
|
|
67
|
+
channel(name).unsubscribe(onMessage);
|
|
68
|
+
}
|
|
69
|
+
const diagnosticsChannel = {
|
|
70
|
+
channel: channel,
|
|
71
|
+
hasSubscribers: hasSubscribers,
|
|
72
|
+
subscribe: subscribe,
|
|
73
|
+
unsubscribe: unsubscribe,
|
|
74
|
+
Channel: Channel
|
|
75
|
+
};
|
|
76
|
+
module.exports = diagnosticsChannel;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { LookupOptions } from '../lib/dns-types';
|
|
2
|
+
declare function lookup(hostname: unknown, options?: number | LookupOptions): Promise<unknown>;
|
|
3
|
+
declare function lookupService(address: unknown, port: unknown): Promise<{
|
|
4
|
+
hostname: string;
|
|
5
|
+
service: string;
|
|
6
|
+
}>;
|
|
7
|
+
declare function getServers(): string[];
|
|
8
|
+
declare function setServers(): never;
|
|
9
|
+
declare function getDefaultResultOrder(): string;
|
|
10
|
+
declare function setDefaultResultOrder(order: unknown): void;
|
|
11
|
+
declare const api: {
|
|
12
|
+
lookup: typeof lookup;
|
|
13
|
+
lookupService: typeof lookupService;
|
|
14
|
+
resolve: () => never;
|
|
15
|
+
resolve4: () => never;
|
|
16
|
+
resolve6: () => never;
|
|
17
|
+
resolveAny: () => never;
|
|
18
|
+
resolveCaa: () => never;
|
|
19
|
+
resolveCname: () => never;
|
|
20
|
+
resolveMx: () => never;
|
|
21
|
+
resolveNaptr: () => never;
|
|
22
|
+
resolveNs: () => never;
|
|
23
|
+
resolvePtr: () => never;
|
|
24
|
+
resolveSoa: () => never;
|
|
25
|
+
resolveSrv: () => never;
|
|
26
|
+
resolveTlsa: () => never;
|
|
27
|
+
resolveTxt: () => never;
|
|
28
|
+
reverse: () => never;
|
|
29
|
+
getServers: typeof getServers;
|
|
30
|
+
setServers: typeof setServers;
|
|
31
|
+
getDefaultResultOrder: typeof getDefaultResultOrder;
|
|
32
|
+
setDefaultResultOrder: typeof setDefaultResultOrder;
|
|
33
|
+
};
|
|
34
|
+
export = api;
|