@run0/jiki 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/dist/browser-bundle.d.ts +40 -0
  2. package/dist/builtins.d.ts +22 -0
  3. package/dist/code-transform.d.ts +7 -0
  4. package/dist/config/cdn.d.ts +13 -0
  5. package/dist/container.d.ts +101 -0
  6. package/dist/dev-server.d.ts +69 -0
  7. package/dist/errors.d.ts +19 -0
  8. package/dist/frameworks/code-transforms.d.ts +32 -0
  9. package/dist/frameworks/next-api-handler.d.ts +72 -0
  10. package/dist/frameworks/next-dev-server.d.ts +141 -0
  11. package/dist/frameworks/next-html-generator.d.ts +36 -0
  12. package/dist/frameworks/next-route-resolver.d.ts +19 -0
  13. package/dist/frameworks/next-shims.d.ts +78 -0
  14. package/dist/frameworks/remix-dev-server.d.ts +47 -0
  15. package/dist/frameworks/sveltekit-dev-server.d.ts +43 -0
  16. package/dist/frameworks/vite-dev-server.d.ts +50 -0
  17. package/dist/fs-errors.d.ts +36 -0
  18. package/dist/index.cjs +14916 -0
  19. package/dist/index.cjs.map +1 -0
  20. package/dist/index.d.ts +61 -0
  21. package/dist/index.mjs +14898 -0
  22. package/dist/index.mjs.map +1 -0
  23. package/dist/kernel.d.ts +48 -0
  24. package/dist/memfs.d.ts +144 -0
  25. package/dist/metrics.d.ts +78 -0
  26. package/dist/module-resolver.d.ts +60 -0
  27. package/dist/network-interceptor.d.ts +71 -0
  28. package/dist/npm/cache.d.ts +76 -0
  29. package/dist/npm/index.d.ts +60 -0
  30. package/dist/npm/lockfile-reader.d.ts +32 -0
  31. package/dist/npm/pnpm.d.ts +18 -0
  32. package/dist/npm/registry.d.ts +45 -0
  33. package/dist/npm/resolver.d.ts +39 -0
  34. package/dist/npm/sync-installer.d.ts +18 -0
  35. package/dist/npm/tarball.d.ts +4 -0
  36. package/dist/npm/workspaces.d.ts +46 -0
  37. package/dist/persistence.d.ts +94 -0
  38. package/dist/plugin.d.ts +156 -0
  39. package/dist/polyfills/assert.d.ts +30 -0
  40. package/dist/polyfills/child_process.d.ts +116 -0
  41. package/dist/polyfills/chokidar.d.ts +18 -0
  42. package/dist/polyfills/crypto.d.ts +49 -0
  43. package/dist/polyfills/events.d.ts +28 -0
  44. package/dist/polyfills/fs.d.ts +82 -0
  45. package/dist/polyfills/http.d.ts +147 -0
  46. package/dist/polyfills/module.d.ts +29 -0
  47. package/dist/polyfills/net.d.ts +53 -0
  48. package/dist/polyfills/os.d.ts +91 -0
  49. package/dist/polyfills/path.d.ts +96 -0
  50. package/dist/polyfills/perf_hooks.d.ts +21 -0
  51. package/dist/polyfills/process.d.ts +99 -0
  52. package/dist/polyfills/querystring.d.ts +15 -0
  53. package/dist/polyfills/readdirp.d.ts +18 -0
  54. package/dist/polyfills/readline.d.ts +32 -0
  55. package/dist/polyfills/stream.d.ts +106 -0
  56. package/dist/polyfills/stubs.d.ts +737 -0
  57. package/dist/polyfills/tty.d.ts +25 -0
  58. package/dist/polyfills/url.d.ts +41 -0
  59. package/dist/polyfills/util.d.ts +61 -0
  60. package/dist/polyfills/v8.d.ts +43 -0
  61. package/dist/polyfills/vm.d.ts +76 -0
  62. package/dist/polyfills/worker-threads.d.ts +77 -0
  63. package/dist/polyfills/ws.d.ts +32 -0
  64. package/dist/polyfills/zlib.d.ts +87 -0
  65. package/dist/runtime-helpers.d.ts +4 -0
  66. package/dist/runtime-interface.d.ts +39 -0
  67. package/dist/sandbox.d.ts +69 -0
  68. package/dist/server-bridge.d.ts +55 -0
  69. package/dist/shell-commands.d.ts +2 -0
  70. package/dist/shell.d.ts +101 -0
  71. package/dist/transpiler.d.ts +47 -0
  72. package/dist/type-checker.d.ts +57 -0
  73. package/dist/types/package-json.d.ts +17 -0
  74. package/dist/utils/binary-encoding.d.ts +4 -0
  75. package/dist/utils/hash.d.ts +6 -0
  76. package/dist/utils/safe-path.d.ts +6 -0
  77. package/dist/worker-runtime.d.ts +34 -0
  78. package/package.json +59 -0
  79. package/src/browser-bundle.ts +498 -0
  80. package/src/builtins.ts +222 -0
  81. package/src/code-transform.ts +183 -0
  82. package/src/config/cdn.ts +17 -0
  83. package/src/container.ts +343 -0
  84. package/src/dev-server.ts +322 -0
  85. package/src/errors.ts +604 -0
  86. package/src/frameworks/code-transforms.ts +667 -0
  87. package/src/frameworks/next-api-handler.ts +366 -0
  88. package/src/frameworks/next-dev-server.ts +1252 -0
  89. package/src/frameworks/next-html-generator.ts +585 -0
  90. package/src/frameworks/next-route-resolver.ts +521 -0
  91. package/src/frameworks/next-shims.ts +1084 -0
  92. package/src/frameworks/remix-dev-server.ts +163 -0
  93. package/src/frameworks/sveltekit-dev-server.ts +197 -0
  94. package/src/frameworks/vite-dev-server.ts +370 -0
  95. package/src/fs-errors.ts +118 -0
  96. package/src/index.ts +188 -0
  97. package/src/kernel.ts +381 -0
  98. package/src/memfs.ts +1006 -0
  99. package/src/metrics.ts +140 -0
  100. package/src/module-resolver.ts +511 -0
  101. package/src/network-interceptor.ts +143 -0
  102. package/src/npm/cache.ts +172 -0
  103. package/src/npm/index.ts +377 -0
  104. package/src/npm/lockfile-reader.ts +105 -0
  105. package/src/npm/pnpm.ts +108 -0
  106. package/src/npm/registry.ts +120 -0
  107. package/src/npm/resolver.ts +339 -0
  108. package/src/npm/sync-installer.ts +217 -0
  109. package/src/npm/tarball.ts +136 -0
  110. package/src/npm/workspaces.ts +255 -0
  111. package/src/persistence.ts +235 -0
  112. package/src/plugin.ts +293 -0
  113. package/src/polyfills/assert.ts +164 -0
  114. package/src/polyfills/child_process.ts +535 -0
  115. package/src/polyfills/chokidar.ts +52 -0
  116. package/src/polyfills/crypto.ts +433 -0
  117. package/src/polyfills/events.ts +178 -0
  118. package/src/polyfills/fs.ts +297 -0
  119. package/src/polyfills/http.ts +478 -0
  120. package/src/polyfills/module.ts +97 -0
  121. package/src/polyfills/net.ts +123 -0
  122. package/src/polyfills/os.ts +108 -0
  123. package/src/polyfills/path.ts +169 -0
  124. package/src/polyfills/perf_hooks.ts +30 -0
  125. package/src/polyfills/process.ts +349 -0
  126. package/src/polyfills/querystring.ts +66 -0
  127. package/src/polyfills/readdirp.ts +72 -0
  128. package/src/polyfills/readline.ts +80 -0
  129. package/src/polyfills/stream.ts +610 -0
  130. package/src/polyfills/stubs.ts +600 -0
  131. package/src/polyfills/tty.ts +43 -0
  132. package/src/polyfills/url.ts +97 -0
  133. package/src/polyfills/util.ts +173 -0
  134. package/src/polyfills/v8.ts +62 -0
  135. package/src/polyfills/vm.ts +111 -0
  136. package/src/polyfills/worker-threads.ts +189 -0
  137. package/src/polyfills/ws.ts +73 -0
  138. package/src/polyfills/zlib.ts +244 -0
  139. package/src/runtime-helpers.ts +83 -0
  140. package/src/runtime-interface.ts +46 -0
  141. package/src/sandbox.ts +178 -0
  142. package/src/server-bridge.ts +473 -0
  143. package/src/service-worker.ts +153 -0
  144. package/src/shell-commands.ts +708 -0
  145. package/src/shell.ts +795 -0
  146. package/src/transpiler.ts +282 -0
  147. package/src/type-checker.ts +241 -0
  148. package/src/types/package-json.ts +17 -0
  149. package/src/utils/binary-encoding.ts +38 -0
  150. package/src/utils/hash.ts +24 -0
  151. package/src/utils/safe-path.ts +38 -0
  152. package/src/worker-runtime.ts +42 -0
@@ -0,0 +1,433 @@
1
+ import { uint8ToHex } from "../utils/binary-encoding";
2
+ import { BufferImpl } from "./stream";
3
+
4
+ export function randomBytes(size: number): BufferImpl {
5
+ const buf = new BufferImpl(size);
6
+ crypto.getRandomValues(buf);
7
+ return buf;
8
+ }
9
+
10
+ export function randomUUID(): string {
11
+ return crypto.randomUUID();
12
+ }
13
+ export function randomInt(min: number, max?: number): number {
14
+ if (max === undefined) {
15
+ max = min;
16
+ min = 0;
17
+ }
18
+ const range = max - min;
19
+ if (range <= 0) return min;
20
+ const arr = new Uint32Array(1);
21
+ crypto.getRandomValues(arr);
22
+ return min + (arr[0] % range);
23
+ }
24
+
25
+ // ---- Pure-JS SHA-256 (sync) ----
26
+ const SHA256_K = new Uint32Array([
27
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
28
+ 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
29
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
30
+ 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
31
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
32
+ 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
33
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
34
+ 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
35
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
36
+ 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
37
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
38
+ ]);
39
+
40
+ function sha256(data: Uint8Array): Uint8Array {
41
+ let h0 = 0x6a09e667,
42
+ h1 = 0xbb67ae85,
43
+ h2 = 0x3c6ef372,
44
+ h3 = 0xa54ff53a;
45
+ let h4 = 0x510e527f,
46
+ h5 = 0x9b05688c,
47
+ h6 = 0x1f83d9ab,
48
+ h7 = 0x5be0cd19;
49
+
50
+ const bitLen = data.length * 8;
51
+ const padLen = (data.length + 9 + 63) & ~63;
52
+ const padded = new Uint8Array(padLen);
53
+ padded.set(data);
54
+ padded[data.length] = 0x80;
55
+ const view = new DataView(padded.buffer);
56
+ view.setUint32(padLen - 4, bitLen, false);
57
+ if (bitLen > 0xffffffff)
58
+ view.setUint32(padLen - 8, Math.floor(bitLen / 0x100000000), false);
59
+
60
+ const w = new Uint32Array(64);
61
+ for (let off = 0; off < padLen; off += 64) {
62
+ for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + i * 4, false);
63
+ for (let i = 16; i < 64; i++) {
64
+ const s0 =
65
+ (((w[i - 15] >>> 7) | (w[i - 15] << 25)) ^
66
+ ((w[i - 15] >>> 18) | (w[i - 15] << 14)) ^
67
+ (w[i - 15] >>> 3)) >>>
68
+ 0;
69
+ const s1 =
70
+ (((w[i - 2] >>> 17) | (w[i - 2] << 15)) ^
71
+ ((w[i - 2] >>> 19) | (w[i - 2] << 13)) ^
72
+ (w[i - 2] >>> 10)) >>>
73
+ 0;
74
+ w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
75
+ }
76
+ let a = h0,
77
+ b = h1,
78
+ c = h2,
79
+ d = h3,
80
+ e = h4,
81
+ f = h5,
82
+ g = h6,
83
+ h = h7;
84
+ for (let i = 0; i < 64; i++) {
85
+ const S1 =
86
+ (((e >>> 6) | (e << 26)) ^
87
+ ((e >>> 11) | (e << 21)) ^
88
+ ((e >>> 25) | (e << 7))) >>>
89
+ 0;
90
+ const ch = ((e & f) ^ (~e & g)) >>> 0;
91
+ const temp1 = (h + S1 + ch + SHA256_K[i] + w[i]) >>> 0;
92
+ const S0 =
93
+ (((a >>> 2) | (a << 30)) ^
94
+ ((a >>> 13) | (a << 19)) ^
95
+ ((a >>> 22) | (a << 10))) >>>
96
+ 0;
97
+ const maj = ((a & b) ^ (a & c) ^ (b & c)) >>> 0;
98
+ const temp2 = (S0 + maj) >>> 0;
99
+ h = g;
100
+ g = f;
101
+ f = e;
102
+ e = (d + temp1) >>> 0;
103
+ d = c;
104
+ c = b;
105
+ b = a;
106
+ a = (temp1 + temp2) >>> 0;
107
+ }
108
+ h0 = (h0 + a) >>> 0;
109
+ h1 = (h1 + b) >>> 0;
110
+ h2 = (h2 + c) >>> 0;
111
+ h3 = (h3 + d) >>> 0;
112
+ h4 = (h4 + e) >>> 0;
113
+ h5 = (h5 + f) >>> 0;
114
+ h6 = (h6 + g) >>> 0;
115
+ h7 = (h7 + h) >>> 0;
116
+ }
117
+ const out = new Uint8Array(32);
118
+ const ov = new DataView(out.buffer);
119
+ ov.setUint32(0, h0, false);
120
+ ov.setUint32(4, h1, false);
121
+ ov.setUint32(8, h2, false);
122
+ ov.setUint32(12, h3, false);
123
+ ov.setUint32(16, h4, false);
124
+ ov.setUint32(20, h5, false);
125
+ ov.setUint32(24, h6, false);
126
+ ov.setUint32(28, h7, false);
127
+ return out;
128
+ }
129
+
130
+ // ---- Pure-JS MD5 (sync) ----
131
+ function md5(data: Uint8Array): Uint8Array {
132
+ let a0 = 0x67452301,
133
+ b0 = 0xefcdab89,
134
+ c0 = 0x98badcfe,
135
+ d0 = 0x10325476;
136
+ const s = [
137
+ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5,
138
+ 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11,
139
+ 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10,
140
+ 15, 21,
141
+ ];
142
+ const K = new Uint32Array(64);
143
+ for (let i = 0; i < 64; i++)
144
+ K[i] = Math.floor(2 ** 32 * Math.abs(Math.sin(i + 1))) >>> 0;
145
+
146
+ const bitLen = data.length * 8;
147
+ const padLen = (data.length + 9 + 63) & ~63;
148
+ const padded = new Uint8Array(padLen);
149
+ padded.set(data);
150
+ padded[data.length] = 0x80;
151
+ const pv = new DataView(padded.buffer);
152
+ pv.setUint32(padLen - 8, bitLen >>> 0, true);
153
+ pv.setUint32(padLen - 4, Math.floor(bitLen / 0x100000000), true);
154
+
155
+ for (let off = 0; off < padLen; off += 64) {
156
+ const M = new Uint32Array(16);
157
+ for (let j = 0; j < 16; j++) M[j] = pv.getUint32(off + j * 4, true);
158
+ let A = a0,
159
+ B = b0,
160
+ C = c0,
161
+ D = d0;
162
+ for (let i = 0; i < 64; i++) {
163
+ let F: number, g: number;
164
+ if (i < 16) {
165
+ F = (B & C) | (~B & D);
166
+ g = i;
167
+ } else if (i < 32) {
168
+ F = (D & B) | (~D & C);
169
+ g = (5 * i + 1) % 16;
170
+ } else if (i < 48) {
171
+ F = B ^ C ^ D;
172
+ g = (3 * i + 5) % 16;
173
+ } else {
174
+ F = C ^ (B | ~D);
175
+ g = (7 * i) % 16;
176
+ }
177
+ F = (F + A + K[i] + M[g]) >>> 0;
178
+ A = D;
179
+ D = C;
180
+ C = B;
181
+ B = (B + ((F << s[i]) | (F >>> (32 - s[i])))) >>> 0;
182
+ }
183
+ a0 = (a0 + A) >>> 0;
184
+ b0 = (b0 + B) >>> 0;
185
+ c0 = (c0 + C) >>> 0;
186
+ d0 = (d0 + D) >>> 0;
187
+ }
188
+ const out = new Uint8Array(16);
189
+ const ov = new DataView(out.buffer);
190
+ ov.setUint32(0, a0, true);
191
+ ov.setUint32(4, b0, true);
192
+ ov.setUint32(8, c0, true);
193
+ ov.setUint32(12, d0, true);
194
+ return out;
195
+ }
196
+
197
+ function hashBytes(algorithm: string, data: Uint8Array): Uint8Array {
198
+ const algo = algorithm.toLowerCase().replace("-", "");
199
+ if (algo === "sha256") return sha256(data);
200
+ if (algo === "md5") return md5(data);
201
+ // Fallback for unsupported algorithms: use sha256
202
+ return sha256(data);
203
+ }
204
+
205
+ function bytesToHex(bytes: Uint8Array): string {
206
+ return Array.from(bytes, b => b.toString(16).padStart(2, "0")).join("");
207
+ }
208
+
209
+ function bytesToBase64(bytes: Uint8Array): string {
210
+ let binary = "";
211
+ for (let i = 0; i < bytes.length; i++)
212
+ binary += String.fromCharCode(bytes[i]);
213
+ return btoa(binary);
214
+ }
215
+
216
+ class HashImpl {
217
+ private algorithm: string;
218
+ private data: Uint8Array[] = [];
219
+ constructor(algorithm: string) {
220
+ this.algorithm = algorithm.toLowerCase().replace("-", "");
221
+ }
222
+ update(data: string | Uint8Array, encoding?: string): this {
223
+ this.data.push(
224
+ typeof data === "string"
225
+ ? encoding === "hex"
226
+ ? BufferImpl.from(data, "hex")
227
+ : new TextEncoder().encode(data)
228
+ : data,
229
+ );
230
+ return this;
231
+ }
232
+ private _combine(): Uint8Array {
233
+ const total = this.data.reduce((s, b) => s + b.length, 0);
234
+ const combined = new Uint8Array(total);
235
+ let offset = 0;
236
+ for (const chunk of this.data) {
237
+ combined.set(chunk, offset);
238
+ offset += chunk.length;
239
+ }
240
+ return combined;
241
+ }
242
+ async digestAsync(encoding?: string): Promise<string | Uint8Array> {
243
+ const combined = this._combine();
244
+ const algoMap: Record<string, string> = {
245
+ sha256: "SHA-256",
246
+ sha1: "SHA-1",
247
+ sha384: "SHA-384",
248
+ sha512: "SHA-512",
249
+ };
250
+ const wcAlgo = algoMap[this.algorithm];
251
+ if (wcAlgo && typeof globalThis.crypto?.subtle?.digest === "function") {
252
+ const buf = combined.buffer.slice(
253
+ combined.byteOffset,
254
+ combined.byteOffset + combined.byteLength,
255
+ ) as ArrayBuffer;
256
+ const result = new Uint8Array(await crypto.subtle.digest(wcAlgo, buf));
257
+ if (encoding === "hex") return bytesToHex(result);
258
+ if (encoding === "base64") return bytesToBase64(result);
259
+ return BufferImpl.from(result);
260
+ }
261
+ return this.digest(encoding); // fallback to sync
262
+ }
263
+ digest(encoding?: string): string | Uint8Array {
264
+ const combined = this._combine();
265
+ const result = hashBytes(this.algorithm, combined);
266
+ if (encoding === "hex") return bytesToHex(result);
267
+ if (encoding === "base64") return bytesToBase64(result);
268
+ return BufferImpl.from(result);
269
+ }
270
+ }
271
+
272
+ class HmacImpl {
273
+ private algorithm: string;
274
+ private key: Uint8Array;
275
+ private chunks: Uint8Array[] = [];
276
+ constructor(algorithm: string, key: string | Uint8Array) {
277
+ this.algorithm = algorithm;
278
+ this.key = typeof key === "string" ? new TextEncoder().encode(key) : key;
279
+ }
280
+ update(data: string | Uint8Array, _encoding?: string): this {
281
+ this.chunks.push(
282
+ typeof data === "string" ? new TextEncoder().encode(data) : data,
283
+ );
284
+ return this;
285
+ }
286
+ digest(encoding?: string): string | Uint8Array {
287
+ let total = 0;
288
+ for (const c of this.chunks) total += c.length;
289
+ const combined = new Uint8Array(total);
290
+ let offset = 0;
291
+ for (const c of this.chunks) {
292
+ combined.set(c, offset);
293
+ offset += c.length;
294
+ }
295
+
296
+ if (this.algorithm === "sha256") {
297
+ const result = hmacSha256(this.key, combined);
298
+ if (encoding === "hex") return bytesToHex(result);
299
+ if (encoding === "base64") return bytesToBase64(result);
300
+ return BufferImpl.from(result);
301
+ }
302
+ // Fallback for unsupported algorithms: plain hash (best effort)
303
+ const result = hashBytes(this.algorithm, combined);
304
+ if (encoding === "hex") return bytesToHex(result);
305
+ if (encoding === "base64") return bytesToBase64(result);
306
+ return BufferImpl.from(result);
307
+ }
308
+ }
309
+
310
+ export function createHash(algorithm: string): HashImpl {
311
+ return new HashImpl(algorithm);
312
+ }
313
+ export function createHmac(
314
+ algorithm: string,
315
+ key: string | Uint8Array,
316
+ ): HmacImpl {
317
+ return new HmacImpl(algorithm, key);
318
+ }
319
+
320
+ export function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean {
321
+ if (a.length !== b.length)
322
+ throw new RangeError("Input buffers must have the same byte length");
323
+ let result = 0;
324
+ for (let i = 0; i < a.length; i++) result |= a[i] ^ b[i];
325
+ return result === 0;
326
+ }
327
+
328
+ export function getHashes(): string[] {
329
+ return ["sha1", "sha256", "sha384", "sha512"];
330
+ }
331
+ export function getCiphers(): string[] {
332
+ return ["aes-128-cbc", "aes-256-cbc", "aes-128-gcm", "aes-256-gcm"];
333
+ }
334
+
335
+ function hmacSha256(key: Uint8Array, data: Uint8Array): Uint8Array {
336
+ const blockSize = 64;
337
+ let keyBlock = key;
338
+ if (keyBlock.length > blockSize) keyBlock = sha256(keyBlock);
339
+ const padded = new Uint8Array(blockSize);
340
+ padded.set(keyBlock);
341
+
342
+ const ipad = new Uint8Array(blockSize + data.length);
343
+ for (let i = 0; i < blockSize; i++) ipad[i] = padded[i] ^ 0x36;
344
+ ipad.set(data, blockSize);
345
+ const inner = sha256(ipad);
346
+
347
+ const opad = new Uint8Array(blockSize + 32);
348
+ for (let i = 0; i < blockSize; i++) opad[i] = padded[i] ^ 0x5c;
349
+ opad.set(inner, blockSize);
350
+ return sha256(opad);
351
+ }
352
+
353
+ export function createSign(_algorithm: string) {
354
+ const chunks: Uint8Array[] = [];
355
+ return {
356
+ update(data: string | Uint8Array) {
357
+ chunks.push(
358
+ typeof data === "string" ? new TextEncoder().encode(data) : data,
359
+ );
360
+ return this;
361
+ },
362
+ sign(key: unknown, encoding?: string) {
363
+ const keyBytes =
364
+ typeof key === "string"
365
+ ? new TextEncoder().encode(key)
366
+ : key instanceof Uint8Array
367
+ ? key
368
+ : new TextEncoder().encode(String(key));
369
+ const total = chunks.reduce((s, b) => s + b.length, 0);
370
+ const combined = new Uint8Array(total);
371
+ let off = 0;
372
+ for (const c of chunks) {
373
+ combined.set(c, off);
374
+ off += c.length;
375
+ }
376
+ const sig = hmacSha256(keyBytes, combined);
377
+ if (encoding === "hex") return bytesToHex(sig);
378
+ if (encoding === "base64") return bytesToBase64(sig);
379
+ return BufferImpl.from(sig);
380
+ },
381
+ };
382
+ }
383
+
384
+ export function createVerify(_algorithm: string) {
385
+ const chunks: Uint8Array[] = [];
386
+ return {
387
+ update(data: string | Uint8Array) {
388
+ chunks.push(
389
+ typeof data === "string" ? new TextEncoder().encode(data) : data,
390
+ );
391
+ return this;
392
+ },
393
+ verify(key: unknown, signature: unknown, encoding?: string) {
394
+ const keyBytes =
395
+ typeof key === "string"
396
+ ? new TextEncoder().encode(key)
397
+ : key instanceof Uint8Array
398
+ ? key
399
+ : new TextEncoder().encode(String(key));
400
+ const total = chunks.reduce((s, b) => s + b.length, 0);
401
+ const combined = new Uint8Array(total);
402
+ let off = 0;
403
+ for (const c of chunks) {
404
+ combined.set(c, off);
405
+ off += c.length;
406
+ }
407
+ const expected = hmacSha256(keyBytes, combined);
408
+ const expectedStr =
409
+ encoding === "base64" ? bytesToBase64(expected) : bytesToHex(expected);
410
+ const sigStr =
411
+ typeof signature === "string"
412
+ ? signature
413
+ : bytesToHex(signature as Uint8Array);
414
+ return expectedStr === sigStr;
415
+ },
416
+ };
417
+ }
418
+
419
+ export const webcrypto = crypto;
420
+
421
+ export default {
422
+ randomBytes,
423
+ randomUUID,
424
+ randomInt,
425
+ createHash,
426
+ createHmac,
427
+ timingSafeEqual,
428
+ getHashes,
429
+ getCiphers,
430
+ createSign,
431
+ createVerify,
432
+ webcrypto,
433
+ };
@@ -0,0 +1,178 @@
1
+ export type EventListener = (...args: any[]) => void;
2
+
3
+ interface EventData {
4
+ events: Map<string, EventListener[]>;
5
+ maxListeners: number;
6
+ }
7
+
8
+ const store = new WeakMap<object, EventData>();
9
+
10
+ function getData(self: object): EventData {
11
+ let data = store.get(self);
12
+ if (!data) {
13
+ data = { events: new Map(), maxListeners: 10 };
14
+ store.set(self, data);
15
+ }
16
+ return data;
17
+ }
18
+
19
+ export class EventEmitter {
20
+ on(event: string, listener: EventListener): this {
21
+ return this.addListener(event, listener);
22
+ }
23
+
24
+ addListener(event: string, listener: EventListener): this {
25
+ const { events } = getData(this);
26
+ if (!events.has(event)) events.set(event, []);
27
+ events.get(event)!.push(listener);
28
+ return this;
29
+ }
30
+
31
+ once(event: string, listener: EventListener): this {
32
+ const wrapper = (...args: unknown[]) => {
33
+ this.removeListener(event, wrapper);
34
+ listener.apply(this, args);
35
+ };
36
+ return this.addListener(event, wrapper);
37
+ }
38
+
39
+ off(event: string, listener: EventListener): this {
40
+ return this.removeListener(event, listener);
41
+ }
42
+
43
+ removeListener(event: string, listener: EventListener): this {
44
+ const listeners = getData(this).events.get(event);
45
+ if (listeners) {
46
+ const idx = listeners.indexOf(listener);
47
+ if (idx !== -1) listeners.splice(idx, 1);
48
+ }
49
+ return this;
50
+ }
51
+
52
+ removeAllListeners(event?: string): this {
53
+ const { events } = getData(this);
54
+ if (event) events.delete(event);
55
+ else events.clear();
56
+ return this;
57
+ }
58
+
59
+ emit(event: string, ...args: unknown[]): boolean {
60
+ const listeners = getData(this).events.get(event);
61
+ if (!listeners || listeners.length === 0) {
62
+ if (event === "error") {
63
+ const err = args[0];
64
+ throw err instanceof Error ? err : new Error("Unhandled error event");
65
+ }
66
+ return false;
67
+ }
68
+ for (const listener of [...listeners]) {
69
+ try {
70
+ listener.apply(this, args);
71
+ } catch (err) {
72
+ console.error("Event listener error:", err);
73
+ }
74
+ }
75
+ return true;
76
+ }
77
+
78
+ listeners(event: string): EventListener[] {
79
+ return [...(getData(this).events.get(event) || [])];
80
+ }
81
+ rawListeners(event: string): EventListener[] {
82
+ return this.listeners(event);
83
+ }
84
+ listenerCount(event: string): number {
85
+ return getData(this).events.get(event)?.length || 0;
86
+ }
87
+ eventNames(): string[] {
88
+ return [...getData(this).events.keys()];
89
+ }
90
+
91
+ setMaxListeners(n: number): this {
92
+ getData(this).maxListeners = n;
93
+ return this;
94
+ }
95
+ getMaxListeners(): number {
96
+ return getData(this).maxListeners;
97
+ }
98
+
99
+ prependListener(event: string, listener: EventListener): this {
100
+ const { events } = getData(this);
101
+ if (!events.has(event)) events.set(event, []);
102
+ events.get(event)!.unshift(listener);
103
+ return this;
104
+ }
105
+
106
+ prependOnceListener(event: string, listener: EventListener): this {
107
+ const wrapper = (...args: unknown[]) => {
108
+ this.removeListener(event, wrapper);
109
+ listener.apply(this, args);
110
+ };
111
+ return this.prependListener(event, wrapper);
112
+ }
113
+
114
+ static listenerCount(emitter: EventEmitter, event: string): number {
115
+ return emitter.listenerCount(event);
116
+ }
117
+
118
+ static once(emitter: EventEmitter, event: string): Promise<unknown[]> {
119
+ return new Promise((resolve, reject) => {
120
+ const onEvent: EventListener = (...args) => {
121
+ emitter.removeListener("error", onError);
122
+ resolve(args);
123
+ };
124
+ const onError: EventListener = (...args) => {
125
+ emitter.removeListener(event, onEvent);
126
+ reject(args[0]);
127
+ };
128
+ emitter.once(event, onEvent);
129
+ if (event !== "error") {
130
+ emitter.once("error", onError);
131
+ }
132
+ });
133
+ }
134
+ }
135
+
136
+ const events = EventEmitter as typeof EventEmitter & {
137
+ EventEmitter: typeof EventEmitter;
138
+ once: (emitter: EventEmitter, event: string) => Promise<unknown[]>;
139
+ on: (emitter: EventEmitter, event: string) => AsyncIterable<unknown[]>;
140
+ getEventListeners: (emitter: EventEmitter, event: string) => EventListener[];
141
+ listenerCount: (emitter: EventEmitter, event: string) => number;
142
+ };
143
+
144
+ events.EventEmitter = EventEmitter;
145
+ events.once = async (
146
+ emitter: EventEmitter,
147
+ event: string,
148
+ ): Promise<unknown[]> => {
149
+ return new Promise((resolve, reject) => {
150
+ const onEvent: EventListener = (...args) => {
151
+ emitter.removeListener("error", onError);
152
+ resolve(args);
153
+ };
154
+ const onError: EventListener = (...args) => {
155
+ emitter.removeListener(event, onEvent);
156
+ reject(args[0]);
157
+ };
158
+ emitter.once(event, onEvent);
159
+ emitter.once("error", onError);
160
+ });
161
+ };
162
+ events.on = (emitter: EventEmitter, event: string) => {
163
+ const iterator = {
164
+ async next() {
165
+ return new Promise<{ value: unknown[]; done: boolean }>(resolve => {
166
+ emitter.once(event, (...args) => resolve({ value: args, done: false }));
167
+ });
168
+ },
169
+ [Symbol.asyncIterator]() {
170
+ return this;
171
+ },
172
+ };
173
+ return iterator as AsyncIterable<unknown[]>;
174
+ };
175
+ events.getEventListeners = (emitter, event) => emitter.listeners(event);
176
+ events.listenerCount = (emitter, event) => emitter.listenerCount(event);
177
+
178
+ export default events;