arri 0.0.15 → 0.0.17

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 (64) hide show
  1. package/dist/chunks/index.cjs +8836 -65
  2. package/dist/chunks/index.mjs +8817 -66
  3. package/dist/cli.cjs +16563 -52524
  4. package/dist/cli.mjs +16529 -52475
  5. package/dist/codegen.cjs +308 -31
  6. package/dist/codegen.d.cts +23 -15
  7. package/dist/codegen.d.mts +23 -15
  8. package/dist/codegen.d.ts +23 -15
  9. package/dist/codegen.mjs +274 -17
  10. package/dist/index.cjs +47 -21
  11. package/dist/index.d.cts +19 -2
  12. package/dist/index.d.mts +19 -2
  13. package/dist/index.d.ts +19 -2
  14. package/dist/index.mjs +30 -1
  15. package/dist/shared/arri.234caf5e.cjs +655 -0
  16. package/dist/shared/arri.38792eb4.cjs +850 -0
  17. package/dist/shared/arri.4d2ea869.mjs +641 -0
  18. package/dist/shared/arri.54e67ffe.mjs +811 -0
  19. package/dist/shared/{arri.bb7e9a4e.d.ts → arri.b176bba1.d.cts} +61 -13
  20. package/dist/shared/{arri.bb7e9a4e.d.cts → arri.b176bba1.d.mts} +61 -13
  21. package/dist/shared/{arri.bb7e9a4e.d.mts → arri.b176bba1.d.ts} +61 -13
  22. package/package.json +1 -1
  23. package/dist/chunks/acorn.cjs +0 -18
  24. package/dist/chunks/acorn.mjs +0 -15
  25. package/dist/chunks/angular.cjs +0 -7
  26. package/dist/chunks/angular.mjs +0 -4
  27. package/dist/chunks/babel.cjs +0 -21
  28. package/dist/chunks/babel.mjs +0 -18
  29. package/dist/chunks/browser.cjs +0 -21
  30. package/dist/chunks/browser.mjs +0 -19
  31. package/dist/chunks/estree.cjs +0 -42
  32. package/dist/chunks/estree.mjs +0 -37
  33. package/dist/chunks/flow.cjs +0 -26
  34. package/dist/chunks/flow.mjs +0 -23
  35. package/dist/chunks/glimmer.cjs +0 -37
  36. package/dist/chunks/glimmer.mjs +0 -32
  37. package/dist/chunks/graphql.cjs +0 -37
  38. package/dist/chunks/graphql.mjs +0 -31
  39. package/dist/chunks/html.cjs +0 -27
  40. package/dist/chunks/html.mjs +0 -21
  41. package/dist/chunks/index2.cjs +0 -231
  42. package/dist/chunks/index2.mjs +0 -220
  43. package/dist/chunks/index3.cjs +0 -1822
  44. package/dist/chunks/index3.mjs +0 -1812
  45. package/dist/chunks/index4.cjs +0 -256
  46. package/dist/chunks/index4.mjs +0 -247
  47. package/dist/chunks/index5.cjs +0 -8854
  48. package/dist/chunks/index5.mjs +0 -8832
  49. package/dist/chunks/markdown.cjs +0 -67
  50. package/dist/chunks/markdown.mjs +0 -61
  51. package/dist/chunks/meriyah.cjs +0 -10
  52. package/dist/chunks/meriyah.mjs +0 -7
  53. package/dist/chunks/postcss.cjs +0 -60
  54. package/dist/chunks/postcss.mjs +0 -54
  55. package/dist/chunks/typescript.cjs +0 -30
  56. package/dist/chunks/typescript.mjs +0 -27
  57. package/dist/chunks/xdg-open.cjs +0 -1071
  58. package/dist/chunks/xdg-open.mjs +0 -1069
  59. package/dist/chunks/yaml.cjs +0 -169
  60. package/dist/chunks/yaml.mjs +0 -163
  61. package/dist/shared/arri.9717f131.cjs +0 -29694
  62. package/dist/shared/arri.bfebed73.cjs +0 -8711
  63. package/dist/shared/arri.dab1f9df.mjs +0 -8670
  64. package/dist/shared/arri.e335c10b.mjs +0 -29668
@@ -1,1812 +0,0 @@
1
- import fs from 'fs';
2
- import require$$0 from 'path';
3
- import { m as micromatch_1, i as isGlob$1 } from '../cli.mjs';
4
- import 'node:tty';
5
- import 'node:fs';
6
- import '../shared/arri.dab1f9df.mjs';
7
- import '../shared/arri.e335c10b.mjs';
8
- import 'module';
9
- import 'url';
10
- import 'process';
11
- import 'os';
12
- import 'tty';
13
- import 'assert';
14
- import 'fs/promises';
15
- import 'v8';
16
- import 'util';
17
- import 'child_process';
18
- import 'node:fs/promises';
19
- import 'crypto';
20
- import 'node:os';
21
- import 'perf_hooks';
22
- import 'vm';
23
- import 'node:path';
24
- import 'node:module';
25
- import 'node:url';
26
- import 'node:assert';
27
- import 'node:process';
28
- import 'node:v8';
29
- import 'node:util';
30
- import 'events';
31
- import 'stream';
32
- import 'node:stream';
33
- import 'node:http';
34
- import 'node:https';
35
- import 'node:net';
36
- import 'http';
37
- import 'https';
38
- import 'node:child_process';
39
-
40
- const NAPI_OK = 0;
41
- const NAPI_GENERIC_FAILURE = 9;
42
- const NAPI_PENDING_EXCEPTION = 10;
43
- const NAPI_CANCELED = 11;
44
- const NAPI_HANDLE_SCOPE_MISMATCH = 13;
45
- const NAPI_NO_EXTERNAL_BUFFERS_ALLOWED = 22;
46
-
47
- // https://nodejs.org/api/n-api.html#napi_property_attributes
48
- const NAPI_WRITABLE = 1 << 0;
49
- const NAPI_ENUMERABLE = 1 << 1;
50
- const NAPI_CONFIGURABLE = 1 << 2;
51
- const NAPI_STATIC = 1 << 10;
52
-
53
- // https://nodejs.org/api/n-api.html#napi_typedarray_type
54
- const typedArrays = [
55
- Int8Array,
56
- Uint8Array,
57
- Uint8ClampedArray,
58
- Int16Array,
59
- Uint16Array,
60
- Int32Array,
61
- Uint32Array,
62
- Float32Array,
63
- Float64Array,
64
- BigInt64Array,
65
- BigUint64Array
66
- ];
67
-
68
- const environments = [];
69
-
70
- class Environment {
71
- scopes = [];
72
- referenceId = 1;
73
- references = new Map();
74
- deferred = [null];
75
- wrappedObjects = new WeakMap();
76
- externalObjects = new WeakMap();
77
- buffers = new Map();
78
- instanceData = 0;
79
- pendingException = null;
80
-
81
- constructor(instance) {
82
- this.id = environments.length;
83
- environments.push(this);
84
-
85
- this.instance = instance;
86
- this.table = instance.exports.__indirect_function_table;
87
- this.exports = {};
88
-
89
- this.pushScope();
90
- let values = this.scopes[this.scopes.length - 1];
91
- let exports = values.length;
92
- values.push(this.exports);
93
-
94
- try {
95
- if (this.instance.exports.napi_register_module_v1) {
96
- this.instance.exports.napi_register_module_v1(this.id, exports);
97
- }
98
-
99
- if (this.instance.exports.napi_register_wasm_v1) {
100
- this.instance.exports.napi_register_wasm_v1(this.id, exports);
101
- }
102
- } finally {
103
- this.popScope();
104
- if (this.pendingException) {
105
- let e = this.pendingException;
106
- this.pendingException = null;
107
- throw e;
108
- }
109
- }
110
- }
111
-
112
- destroy() {
113
- environments[this.id] = undefined;
114
- }
115
-
116
- getString(ptr, len = strlen(this.memory, ptr)) {
117
- return decoder.decode(this.memory.subarray(ptr, Math.max(0, ptr + len)));
118
- }
119
-
120
- pushScope() {
121
- let id = this.scopes.length;
122
- this.scopes.push(id ? [...this.scopes[id - 1]] : [undefined, null, globalThis, true, false]);
123
- return id;
124
- }
125
-
126
- popScope() {
127
- this.scopes.pop();
128
-
129
- // Update any buffers with values which might have been modified in WASM copy.
130
- for (let [buffer, slice] of this.buffers) {
131
- // Ignore if buffer or slice has been detached.
132
- if (buffer.byteLength && slice.byteLength) {
133
- buffer.set(slice);
134
- }
135
- }
136
-
137
- this.buffers.clear();
138
- }
139
-
140
- get(idx) {
141
- return this.scopes[this.scopes.length - 1][idx];
142
- }
143
-
144
- set(idx, value) {
145
- this.scopes[this.scopes.length - 1][idx] = value;
146
- }
147
-
148
- pushValue(value, scope = this.scopes.length - 1) {
149
- let values = this.scopes[scope];
150
- let id = values.length;
151
- values.push(value);
152
- return id;
153
- }
154
-
155
- createValue(value, result, scope) {
156
- if (typeof value === 'boolean') {
157
- this.setPointer(result, value ? 3 : 4);
158
- return NAPI_OK;
159
- } else if (typeof value === 'undefined') {
160
- this.setPointer(result, 0);
161
- return NAPI_OK;
162
- } else if (value === null) {
163
- this.setPointer(result, 1);
164
- return NAPI_OK;
165
- } else if (value === globalThis) {
166
- this.setPointer(result, 2);
167
- return NAPI_OK;
168
- }
169
-
170
- let id = this.pushValue(value, scope);
171
- this.setPointer(result, id);
172
- return NAPI_OK;
173
- }
174
-
175
- setPointer(ptr, value) {
176
- this.u32[ptr >> 2] = value;
177
- return NAPI_OK;
178
- }
179
-
180
- _u32 = new Uint32Array();
181
- get u32() {
182
- if (this._u32.byteLength === 0) {
183
- this._u32 = new Uint32Array(this.instance.exports.memory.buffer);
184
- }
185
-
186
- return this._u32;
187
- }
188
-
189
- _i32 = new Int32Array();
190
- get i32() {
191
- if (this._i32.byteLength === 0) {
192
- this._i32 = new Int32Array(this.instance.exports.memory.buffer);
193
- }
194
-
195
- return this._i32;
196
- }
197
-
198
- _u16 = new Uint16Array();
199
- get u16() {
200
- if (this._u16.byteLength === 0) {
201
- this._u16 = new Uint16Array(this.instance.exports.memory.buffer);
202
- }
203
-
204
- return this._u16;
205
- }
206
-
207
- _u64 = new BigUint64Array();
208
- get u64() {
209
- if (this._u64.byteLength === 0) {
210
- this._u64 = new BigUint64Array(this.instance.exports.memory.buffer);
211
- }
212
-
213
- return this._u64;
214
- }
215
-
216
- _i64 = new BigInt64Array();
217
- get i64() {
218
- if (this._i64.byteLength === 0) {
219
- this._i64 = new BigInt64Array(this.instance.exports.memory.buffer);
220
- }
221
-
222
- return this._i64;
223
- }
224
-
225
- _f64 = new Float64Array();
226
- get f64() {
227
- if (this._f64.byteLength === 0) {
228
- this._f64 = new Float64Array(this.instance.exports.memory.buffer);
229
- }
230
-
231
- return this._f64;
232
- }
233
-
234
- _buf = new Uint8Array();
235
- get memory() {
236
- if (this._buf.byteLength === 0) {
237
- this._buf = new Uint8Array(this.instance.exports.memory.buffer);
238
- }
239
-
240
- return this._buf;
241
- }
242
-
243
- getBufferInfo(buf, ptr) {
244
- if (this.buffers.has(buf)) {
245
- let b = this.buffers.get(buf);
246
- this.setPointer(ptr, b.byteOffset);
247
- return b.byteLength;
248
- }
249
-
250
- if (buf instanceof ArrayBuffer) {
251
- let b = this.copyBuffer(new Uint8Array(buf));
252
- this.setPointer(ptr, b.byteOffset);
253
- return b.byteLength;
254
- }
255
-
256
- // If this is a view into WASM memory, no copies needed.
257
- if (buf.buffer === this.instance.exports.memory.buffer) {
258
- this.setPointer(ptr, buf.byteOffset);
259
- return buf.byteLength;
260
- }
261
-
262
- let b = this.copyBuffer(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
263
- this.setPointer(ptr, b.byteOffset);
264
- return b.byteLength;
265
- }
266
-
267
- copyBuffer(data) {
268
- let ptr = this.instance.exports.napi_wasm_malloc(data.byteLength);
269
- let mem = this.memory;
270
- mem.set(data, ptr);
271
- let buf = mem.subarray(ptr, ptr + data.byteLength);
272
- this.buffers.set(data, buf);
273
- return buf;
274
- }
275
-
276
- createFunction(cb, data) {
277
- let env = this;
278
- let fn = env.table.get(cb);
279
- let func = function (...args) {
280
- let scope = env.pushScope();
281
-
282
- try {
283
- let values = env.scopes[scope];
284
- let info = values.length;
285
- values.push({
286
- thisArg: this,
287
- args,
288
- data,
289
- newTarget: new.target
290
- });
291
-
292
- let res = fn(env.id, info);
293
- return env.get(res);
294
- } finally {
295
- env.popScope();
296
- if (env.pendingException) {
297
- let e = env.pendingException;
298
- env.pendingException = null;
299
- throw e;
300
- }
301
- }
302
- };
303
-
304
- return func;
305
- }
306
-
307
- readPropertyDescriptor(ptr) {
308
- // https://nodejs.org/api/n-api.html#napi_property_descriptor
309
- let buf = this.u32;
310
- let utf8name = buf[ptr++];
311
- let nameValue = buf[ptr++];
312
- let method = buf[ptr++];
313
- let getter = buf[ptr++];
314
- let setter = buf[ptr++];
315
- let val = buf[ptr++];
316
- let attrs = buf[ptr++];
317
- let data = buf[ptr++];
318
-
319
- let name = utf8name ? this.getString(utf8name) : this.get(nameValue);
320
- let writable = Boolean(attrs & NAPI_WRITABLE);
321
- let enumerable = Boolean(attrs & NAPI_ENUMERABLE);
322
- let configurable = Boolean(attrs & NAPI_CONFIGURABLE);
323
- let isStatic = Boolean(attrs & NAPI_STATIC);
324
- let get = getter ? this.createFunction(getter, data) : undefined;
325
- let set = setter ? this.createFunction(setter, data) : undefined;
326
- let value = method ? this.createFunction(method, data) : val ? this.get(val) : undefined;
327
-
328
- let descriptor = {
329
- name,
330
- static: isStatic,
331
- configurable,
332
- enumerable
333
- };
334
- if (get || set) {
335
- descriptor.get = get;
336
- descriptor.set = set;
337
- } else if (value) {
338
- descriptor.writable = writable;
339
- descriptor.value = value;
340
- }
341
-
342
- return descriptor;
343
- }
344
- }
345
-
346
- const decoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
347
- const latin1Decoder = new TextDecoder('latin1');
348
- const utf16Decoder = new TextDecoder('utf-16');
349
- const encoder$1 = new TextEncoder();
350
-
351
- class FinalizeRecord {
352
- constructor(env, finalize, hint, data) {
353
- this.env = env;
354
- this.finalize = finalize;
355
- this.hint = hint;
356
- this.data = data;
357
- }
358
- }
359
-
360
- const finalizationRegistry = new FinalizationRegistry(buffer => {
361
- if (buffer.finalize) {
362
- buffer.finalize(buffer.env, buffer.data, buffer.hint);
363
- }
364
- });
365
-
366
- class ExternalValue {}
367
-
368
- const threadsafeFunctions = [];
369
-
370
- class ThreadSafeFunction {
371
- constructor(env, fn, nativeFn, context) {
372
- this.env = env;
373
- this.fn = fn;
374
- this.nativeFn = nativeFn;
375
- this.context = context;
376
- this.id = threadsafeFunctions.length;
377
- threadsafeFunctions.push(this);
378
- }
379
- }
380
-
381
- const asyncWork = [null];
382
-
383
- class AsyncWork {
384
- constructor(env, execute, complete, data) {
385
- this.env = env;
386
- this.execute = execute;
387
- this.complete = complete;
388
- this.data = data;
389
- this.id = asyncWork.length;
390
- asyncWork.push(this);
391
- }
392
- }
393
-
394
- const napi = {
395
- napi_open_handle_scope(env_id, result) {
396
- let env = environments[env_id];
397
- let id = env.pushScope();
398
- return env.setPointer(result, id);
399
- },
400
- napi_close_handle_scope(env_id, scope) {
401
- let env = environments[env_id];
402
- if (scope !== env.scopes.length - 1) {
403
- return NAPI_HANDLE_SCOPE_MISMATCH;
404
- }
405
- env.popScope();
406
- return NAPI_OK;
407
- },
408
- napi_open_escapable_handle_scope(env_id, result) {
409
- let env = environments[env_id];
410
- let id = env.pushScope();
411
- return env.setPointer(result, id);
412
- },
413
- napi_close_escapable_handle_scope(env_id, scope) {
414
- let env = environments[env_id];
415
- if (scope !== env.scopes.length - 1) {
416
- return NAPI_HANDLE_SCOPE_MISMATCH;
417
- }
418
- env.popScope();
419
- return NAPI_OK;
420
- },
421
- napi_escape_handle(env_id, scope_id, escapee, result) {
422
- let env = environments[env_id];
423
- let value = env.get(escapee);
424
- // Create a value in the outer scope.
425
- return env.createValue(value, result, scope_id - 1);
426
- },
427
- napi_create_object(env_id, result) {
428
- let env = environments[env_id];
429
- return env.createValue({}, result);
430
- },
431
- napi_set_property(env_id, object, key, value) {
432
- let env = environments[env_id];
433
- let obj = env.get(object);
434
- let name = env.get(key);
435
- let val = env.get(value);
436
- obj[name] = val;
437
- return NAPI_OK;
438
- },
439
- napi_get_property(env_id, object, key, result) {
440
- let env = environments[env_id];
441
- let obj = env.get(object);
442
- let name = env.get(key);
443
- return env.createValue(obj[name], result);
444
- },
445
- napi_delete_property(env_id, object, key, result) {
446
- let env = environments[env_id];
447
- let obj = env.get(object);
448
- let name = env.get(key);
449
- let res = false;
450
- try {
451
- res = delete obj[name];
452
- } catch (err) {}
453
- if (result) {
454
- env.memory[result] = res ? 1 : 0;
455
- }
456
- return NAPI_OK;
457
- },
458
- napi_has_property(env_id, object, key, result) {
459
- let env = environments[env_id];
460
- let obj = env.get(object);
461
- let name = env.get(key);
462
- // return env.setPointer(result, name in obj ? 1 : 0);
463
- env.memory[result] = name in obj ? 1 : 0;
464
- return NAPI_OK;
465
- },
466
- napi_has_own_property(env_id, object, key, result) {
467
- let env = environments[env_id];
468
- let obj = env.get(object);
469
- let name = env.get(key);
470
- env.memory[result] = obj.hasOwnProperty(name) ? 1 : 0;
471
- return NAPI_OK;
472
- },
473
- napi_set_named_property(env_id, object, utf8Name, value) {
474
- let env = environments[env_id];
475
- let obj = env.get(object);
476
- let val = env.get(value);
477
- let name = env.getString(utf8Name);
478
- obj[name] = val;
479
- return NAPI_OK;
480
- },
481
- napi_get_named_property(env_id, object, utf8Name, result) {
482
- let env = environments[env_id];
483
- let obj = env.get(object);
484
- let name = env.getString(utf8Name);
485
- return env.createValue(obj[name], result);
486
- },
487
- napi_has_named_property(env_id, object, utf8Name, result) {
488
- let env = environments[env_id];
489
- let obj = env.get(object);
490
- let name = env.getString(utf8Name);
491
- env.memory[result] = name in obj ? 1 : 0;
492
- return NAPI_OK;
493
- },
494
- napi_get_property_names(env_id, object, result) {
495
- let env = environments[env_id];
496
- let obj = env.get(object);
497
- let properties = Object.keys(obj);
498
- return env.createValue(properties, result);
499
- },
500
- napi_get_all_property_names(env_id, object, key_mode, key_filter, key_conversion, result) {
501
- throw new Error('not implemented');
502
- },
503
- napi_define_properties(env_id, object, property_count, properties) {
504
- let env = environments[env_id];
505
- let obj = env.get(object);
506
- let ptr = properties >> 2;
507
- for (let i = 0; i < property_count; i++) {
508
- let descriptor = env.readPropertyDescriptor(ptr);
509
- Object.defineProperty(obj, descriptor.name, descriptor);
510
- ptr += 8;
511
- }
512
- return NAPI_OK;
513
- },
514
- napi_object_freeze(env_id, object) {
515
- let env = environments[env_id];
516
- let obj = env.get(object);
517
- Object.freeze(obj);
518
- return NAPI_OK;
519
- },
520
- napi_object_seal(env_id, object) {
521
- let env = environments[env_id];
522
- let obj = env.get(object);
523
- Object.seal(obj);
524
- return NAPI_OK;
525
- },
526
- napi_get_prototype(env_id, object, result) {
527
- let env = environments[env_id];
528
- let obj = env.get(object);
529
- return env.createValue(Object.getPrototypeOf(obj), result);
530
- },
531
- napi_define_class(env_id, utf8name, length, constructor, data, property_count, properties, result) {
532
- let env = environments[env_id];
533
- let func = env.createFunction(constructor, data);
534
-
535
- Object.defineProperty(func, 'name', {
536
- value: env.getString(utf8name, length),
537
- configurable: true
538
- });
539
-
540
- let ptr = properties >> 2;
541
- for (let i = 0; i < property_count; i++) {
542
- let descriptor = env.readPropertyDescriptor(ptr);
543
- if (descriptor.static) {
544
- Object.defineProperty(func, descriptor.name, descriptor);
545
- } else {
546
- Object.defineProperty(func.prototype, descriptor.name, descriptor);
547
- }
548
- ptr += 8;
549
- }
550
-
551
- return env.createValue(func, result);
552
- },
553
- napi_create_reference(env_id, value, refcount, result) {
554
- let env = environments[env_id];
555
- let id = env.referenceId++;
556
- env.references.set(id, {
557
- value: env.get(value),
558
- refcount
559
- });
560
- return env.setPointer(result, id);
561
- },
562
- napi_delete_reference(env_id, ref) {
563
- let env = environments[env_id];
564
- env.references.delete(ref);
565
- return NAPI_OK;
566
- },
567
- napi_get_reference_value(env_id, ref, result) {
568
- let env = environments[env_id];
569
- let reference = env.references.get(ref);
570
- return env.createValue(reference.value, result);
571
- },
572
- napi_reference_ref(env_id, ref, result) {
573
- let env = environments[env_id];
574
- let reference = env.references.get(ref);
575
- reference.refcount++;
576
- return env.setPointer(result, reference.refcount);
577
- },
578
- napi_reference_unref(env_id, ref, result) {
579
- let env = environments[env_id];
580
- let reference = env.references.get(ref);
581
- if (reference.refcount === 0) {
582
- return NAPI_GENERIC_FAILURE;
583
- }
584
- reference.refcount--;
585
- return env.setPointer(result, reference.refcount);
586
- },
587
- napi_add_env_cleanup_hook() {
588
- return NAPI_OK;
589
- },
590
- napi_remove_env_cleanup_hook() {
591
- return NAPI_OK;
592
- },
593
- napi_add_async_cleanup_hook() {
594
- return NAPI_OK;
595
- },
596
- napi_remove_async_cleanup_hook() {
597
- return NAPI_OK;
598
- },
599
- napi_set_instance_data(env_id, data, finalize_cb, finalize_hint) {
600
- let env = environments[env_id];
601
- env.instanceData = data;
602
- return NAPI_OK;
603
- },
604
- napi_get_instance_data(env_id, data) {
605
- let env = environments[env_id];
606
- return env.setPointer(data, env.instanceData);
607
- },
608
- napi_get_boolean(env_id, value, result) {
609
- let env = environments[env_id];
610
- return env.setPointer(result, value ? 3 : 4);
611
- },
612
- napi_get_value_bool(env_id, value, result) {
613
- let env = environments[env_id];
614
- let val = env.get(value);
615
- env.memory[result] = val ? 1 : 0;
616
- return NAPI_OK;
617
- },
618
- napi_create_int32(env_id, value, result) {
619
- let env = environments[env_id];
620
- return env.createValue(value, result);
621
- },
622
- napi_get_value_int32(env_id, value, result) {
623
- let env = environments[env_id];
624
- let val = env.get(value);
625
- env.i32[result >> 2] = val;
626
- return NAPI_OK;
627
- },
628
- napi_create_uint32(env_id, value, result) {
629
- let env = environments[env_id];
630
- return env.createValue(value, result);
631
- },
632
- napi_get_value_uint32(env_id, value, result) {
633
- let env = environments[env_id];
634
- let val = env.get(value);
635
- return env.setPointer(result, val);
636
- },
637
- napi_create_int64(env_id, value, result) {
638
- let env = environments[env_id];
639
- return env.createValue(Number(value), result);
640
- },
641
- napi_get_value_int64(env_id, value, result) {
642
- let env = environments[env_id];
643
- let val = env.get(value);
644
- env.i64[result >> 3] = val;
645
- return NAPI_OK;
646
- },
647
- napi_create_double(env_id, value, result) {
648
- let env = environments[env_id];
649
- return env.createValue(value, result);
650
- },
651
- napi_get_value_double(env_id, value, result) {
652
- let env = environments[env_id];
653
- let val = env.get(value);
654
- env.f64[result >> 3] = val;
655
- return NAPI_OK;
656
- },
657
- napi_create_bigint_int64(env_id, value, result) {
658
- let env = environments[env_id];
659
- return env.createValue(BigInt.asIntN(64, value), result);
660
- },
661
- napi_get_value_bigint_int64(env_id, value, result, lossless) {
662
- let env = environments[env_id];
663
- let val = env.get(value);
664
- env.i64[result >> 3] = val;
665
- if (lossless) {
666
- env.memory[lossless] = BigInt.asIntN(64, val) === val ? 1 : 0;
667
- }
668
- return NAPI_OK;
669
- },
670
- napi_create_bigint_uint64(env_id, value, result) {
671
- let env = environments[env_id];
672
- return env.createValue(BigInt.asUintN(64, value), result);
673
- },
674
- napi_get_value_bigint_uint64(env_id, value, result, lossless) {
675
- let env = environments[env_id];
676
- let val = env.get(value);
677
- env.u64[result >> 3] = val;
678
- if (lossless) {
679
- env.memory[lossless] = BigInt.asUintN(64, val) === val ? 1 : 0;
680
- }
681
- return NAPI_OK;
682
- },
683
- napi_create_bigint_words(env_id, sign_bit, word_count, words, result) {
684
- let env = environments[env_id];
685
- let buf = env.u64;
686
- let ptr = words >> 3;
687
- let res = 0n;
688
- let shift = 0n;
689
-
690
- for (let i = 0; i < word_count; i++) {
691
- let word = buf[ptr++];
692
- res += word << shift;
693
- shift += 64n;
694
- }
695
-
696
- res *= BigInt((-1) ** sign_bit);
697
- return env.createValue(res, result);
698
- },
699
- napi_get_value_bigint_words(env_id, value, sign_bit, word_count, words) {
700
- let env = environments[env_id];
701
- let val = env.get(value);
702
- let count = env.u32[word_count >> 2];
703
-
704
- if (sign_bit) {
705
- env.i32[sign_bit] = val < 0n ? 1 : 0;
706
- }
707
-
708
- let i = 0;
709
- if (words) {
710
- let mask = (1n << 64n) - 1n;
711
- let buf = env.u64;
712
- let ptr = words >> 3;
713
- if (val < 0n) {
714
- val = -val;
715
- }
716
-
717
- for (; i < count && val !== 0n; i++) {
718
- buf[ptr++] = val & mask;
719
- val >>= 64n;
720
- }
721
- }
722
-
723
- while (val > 0n) {
724
- i++;
725
- val >>= 64n;
726
- }
727
-
728
- return env.setPointer(word_count, i);
729
- },
730
- napi_get_null(env_id, result) {
731
- let env = environments[env_id];
732
- return env.setPointer(result, 1);
733
- },
734
- napi_create_array(env_id, result) {
735
- let env = environments[env_id];
736
- return env.createValue([], result);
737
- },
738
- napi_create_array_with_length(env_id, length, result) {
739
- let env = environments[env_id];
740
- return env.createValue(new Array(length), result);
741
- },
742
- napi_set_element(env_id, object, index, value) {
743
- let env = environments[env_id];
744
- let obj = env.get(object);
745
- let val = env.get(value);
746
- obj[index] = val;
747
- return NAPI_OK;
748
- },
749
- napi_get_element(env_id, object, index, result) {
750
- let env = environments[env_id];
751
- let obj = env.get(object);
752
- let val = obj[index];
753
- return env.createValue(val, result);
754
- },
755
- napi_has_element(env_id, object, index, result) {
756
- let env = environments[env_id];
757
- let obj = env.get(object);
758
- env.memory[result] = obj.hasOwnProperty(index) ? 1 : 0;
759
- return NAPI_OK;
760
- },
761
- napi_delete_element(env_id, object, index, result) {
762
- let env = environments[env_id];
763
- let obj = env.get(object);
764
- let res = false;
765
- try {
766
- res = delete obj[index];
767
- } catch (err) {}
768
- if (result) {
769
- env.memory[result] = res ? 1 : 0;
770
- }
771
- return NAPI_OK;
772
- },
773
- napi_get_array_length(env_id, value, result) {
774
- let env = environments[env_id];
775
- let val = env.get(value);
776
- return env.setPointer(result, val.length);
777
- },
778
- napi_get_undefined(env_id, result) {
779
- let env = environments[env_id];
780
- return env.setPointer(result, 0);
781
- },
782
- napi_create_function(env_id, utf8name, length, cb, data, result) {
783
- let env = environments[env_id];
784
- let func = env.createFunction(cb, data);
785
-
786
- Object.defineProperty(func, 'name', {
787
- value: env.getString(utf8name, length),
788
- configurable: true
789
- });
790
-
791
- return env.createValue(func, result);
792
- },
793
- napi_call_function(env_id, recv, func, argc, argv, result) {
794
- let env = environments[env_id];
795
- let thisArg = env.get(recv);
796
- let fn = env.get(func);
797
- let args = new Array(argc);
798
- let mem = env.u32;
799
- for (let i = 0; i < argc; i++) {
800
- args[i] = env.get(mem[argv >> 2]);
801
- argv += 4;
802
- }
803
-
804
- try {
805
- let res = fn.apply(thisArg, args);
806
- return env.createValue(res, result);
807
- } catch (err) {
808
- env.pendingException = err;
809
- return NAPI_PENDING_EXCEPTION;
810
- }
811
- },
812
- napi_new_instance(env_id, cons, argc, argv, result) {
813
- let env = environments[env_id];
814
- let Class = env.get(cons);
815
- let args = new Array(argc);
816
- let mem = env.u32;
817
- for (let i = 0; i < argc; i++) {
818
- args[i] = env.get(mem[argv >> 2]);
819
- argv += 4;
820
- }
821
-
822
- try {
823
- let value = new Class(...args);
824
- return env.createValue(value, result);
825
- } catch (err) {
826
- env.pendingException = err;
827
- return NAPI_PENDING_EXCEPTION;
828
- }
829
- },
830
- napi_get_cb_info(env_id, cbinfo, argc, argv, thisArg, data) {
831
- let env = environments[env_id];
832
- let info = env.get(cbinfo);
833
- env.setPointer(argc, info.args.length);
834
- for (let i = 0; i < info.args.length; i++) {
835
- env.createValue(info.args[i], argv);
836
- argv += 4;
837
- }
838
- env.createValue(info.thisArg, thisArg);
839
- env.setPointer(data, info.data);
840
- return NAPI_OK;
841
- },
842
- napi_get_new_target(env_id, cbinfo, result) {
843
- let env = environments[env_id];
844
- let info = env.get(cbinfo);
845
- return env.createValue(info.newTarget, result);
846
- },
847
- napi_create_threadsafe_function(
848
- env_id,
849
- func,
850
- async_resource,
851
- async_resource_name,
852
- max_queue_size,
853
- initial_thread_count,
854
- thread_finalize_data,
855
- thread_finalize_cb,
856
- context,
857
- call_js_cb,
858
- result
859
- ) {
860
- let env = environments[env_id];
861
- let fn = func ? env.get(func) : undefined;
862
- let cb = call_js_cb ? env.table.get(call_js_cb) : undefined;
863
- let f = new ThreadSafeFunction(env, fn, cb, context);
864
-
865
- if (thread_finalize_cb) {
866
- let cb = env.table.get(thread_finalize_cb);
867
- finalizationRegistry.register(f, new FinalizeRecord(env_id, cb, 0, f.id));
868
- }
869
-
870
- env.setPointer(result, f.id);
871
- return NAPI_OK;
872
- },
873
- napi_ref_threadsafe_function() {
874
- return NAPI_OK;
875
- },
876
- napi_unref_threadsafe_function() {
877
- return NAPI_OK;
878
- },
879
- napi_acquire_threadsafe_function() {
880
- return NAPI_OK;
881
- },
882
- napi_release_threadsafe_function(func, mode) {
883
- threadsafeFunctions[func] = undefined;
884
- return NAPI_OK;
885
- },
886
- napi_call_threadsafe_function(func, data, is_blocking) {
887
- let f = threadsafeFunctions[func];
888
- f.env.pushScope();
889
- try {
890
- if (f.nativeFn) {
891
- let id = f.fn ? f.env.pushValue(f.fn) : 0;
892
- f.nativeFn(f.env.id, id, f.context, data);
893
- } else if (f.fn) {
894
- f.fn();
895
- }
896
- } finally {
897
- f.env.popScope();
898
- }
899
- },
900
- napi_get_threadsafe_function_context(func, result) {
901
- let f = threadsafeFunctions[func];
902
- f.env.setPointer(result, f.context);
903
- return NAPI_OK;
904
- },
905
- napi_create_async_work(env_id, async_resource, async_resource_name, execute, complete, data, result) {
906
- let env = environments[env_id];
907
- let executeFn = execute ? env.table.get(execute) : undefined;
908
- let completeFn = complete ? env.table.get(complete) : undefined;
909
- let w = new AsyncWork(env, executeFn, completeFn, data);
910
- env.setPointer(result, w.id);
911
- return NAPI_OK;
912
- },
913
- napi_delete_async_work(env, work) {
914
- asyncWork[work] = undefined;
915
- return NAPI_OK;
916
- },
917
- napi_queue_async_work(env, work) {
918
- queueMicrotask(() => {
919
- let w = asyncWork[work];
920
- if (w) {
921
- w.execute(env, w.data);
922
- w.complete(env, NAPI_OK, w.data);
923
- }
924
- });
925
- return NAPI_OK;
926
- },
927
- napi_cancel_async_work() {
928
- let w = asyncWork[work];
929
- w.complete(env, NAPI_CANCELED, w.data);
930
- asyncWork[work] = undefined;
931
- return NAPI_OK;
932
- },
933
- napi_throw(env_id, error) {
934
- let env = environments[env_id];
935
- env.pendingException = env.get(error);
936
- return NAPI_OK;
937
- },
938
- napi_throw_error(env_id, code, msg) {
939
- let env = environments[env_id];
940
- let err = new Error(env.getString(msg));
941
- err.code = code;
942
- env.pendingException = err;
943
- return NAPI_OK;
944
- },
945
- napi_throw_type_error(env_id, code, msg) {
946
- let env = environments[env_id];
947
- let err = new TypeError(env.getString(msg));
948
- err.code = code;
949
- env.pendingException = err;
950
- return NAPI_OK;
951
- },
952
- napi_throw_range_error(env_id, code, msg) {
953
- let env = environments[env_id];
954
- let err = new RangeError(env.getString(msg));
955
- err.code = code;
956
- env.pendingException = err;
957
- return NAPI_OK;
958
- },
959
- napi_create_error(env_id, code, msg, result) {
960
- let env = environments[env_id];
961
- let err = new Error(env.get(msg));
962
- err.code = env.get(code);
963
- return env.createValue(err, result);
964
- },
965
- napi_create_type_error(env_id, code, msg, result) {
966
- let env = environments[env_id];
967
- let err = new TypeError(env.get(msg));
968
- err.code = env.get(code);
969
- return env.createValue(err, result);
970
- },
971
- napi_create_range_error(env_id, code, msg, result) {
972
- let env = environments[env_id];
973
- let err = new RangeError(env.get(msg));
974
- err.code = env.get(code);
975
- return env.createValue(err, result);
976
- },
977
- napi_get_and_clear_last_exception(env_id, result) {
978
- let env = environments[env_id];
979
- let e = env.pendingException;
980
- env.pendingException = null;
981
- return env.createValue(e, result);
982
- },
983
- napi_is_exception_pending(env_id, result) {
984
- let env = environments[env_id];
985
- env.memory[result] = env.pendingException ? 1 : 0;
986
- return NAPI_OK;
987
- },
988
- napi_fatal_exception(env_id, err) {
989
- throw new Error('not implemented');
990
- },
991
- napi_fatal_error(location, location_len, message, message_len) {
992
- throw new Error('not implemented');
993
- },
994
- napi_get_global(env_id, result) {
995
- let env = environments[env_id];
996
- return env.setPointer(result, 2);
997
- },
998
- napi_create_buffer(env_id, length, data, result) {
999
- let env = environments[env_id];
1000
- let ptr = env.instance.exports.napi_wasm_malloc(length);
1001
- if (data) {
1002
- env.setPointer(data, ptr);
1003
- }
1004
-
1005
- // Return a view into WASM memory.
1006
- let buf = typeof globalThis.Buffer !== 'undefined'
1007
- ? globalThis.Buffer.from(env.memory.buffer, ptr, length)
1008
- : env.memory.subarray(ptr, ptr + length);
1009
- return env.createValue(buf, result);
1010
- },
1011
- napi_create_buffer_copy(env_id, length, data, result_data, result) {
1012
- let env = environments[env_id];
1013
- let ptr = env.instance.exports.napi_wasm_malloc(length);
1014
- env.memory.set(env.memory.subarray(data, data + length), ptr);
1015
- if (result_data) {
1016
- env.setPointer(result_data, ptr);
1017
- }
1018
-
1019
- // Return a view into WASM memory.
1020
- let res = typeof globalThis.Buffer !== 'undefined'
1021
- ? globalThis.Buffer.from(env.memory.buffer, ptr, length)
1022
- : env.memory.subarray(ptr, ptr + length);
1023
- return env.createValue(res, result);
1024
- },
1025
- napi_create_external_buffer(env_id, length, data, finalize_cb, finalize_hint, result) {
1026
- let env = environments[env_id];
1027
- let buf = typeof globalThis.Buffer !== 'undefined'
1028
- ? globalThis.Buffer.from(env.memory.buffer, data, length)
1029
- : env.memory.subarray(data, data + length);
1030
- if (finalize_cb) {
1031
- let cb = env.table.get(finalize_cb);
1032
- finalizationRegistry.register(buf, new FinalizeRecord(env_id, cb, finalize_hint, data));
1033
- }
1034
-
1035
- return env.createValue(buf, result);
1036
- },
1037
- napi_get_buffer_info(env_id, value, data, length) {
1038
- let env = environments[env_id];
1039
- let buf = env.get(value);
1040
- let len = env.getBufferInfo(buf, data);
1041
- return env.setPointer(length, len);
1042
- },
1043
- napi_create_arraybuffer(env_id, length, data, result) {
1044
- let env = environments[env_id];
1045
- let buf = new ArrayBuffer(length);
1046
- if (data) {
1047
- // This copies the ArrayBuffer into the WASM memory.
1048
- env.getBufferInfo(buf, data);
1049
- }
1050
- return env.createValue(buf, result);
1051
- },
1052
- napi_create_external_arraybuffer(env_id, data, length, finalize_cb, finalize_hint, result) {
1053
- // There is no way to actually create an external ArrayBuffer without copying.
1054
- // You can only create typed arrays as subarrays, not ArrayBuffer.
1055
- return NAPI_NO_EXTERNAL_BUFFERS_ALLOWED;
1056
- },
1057
- napi_get_arraybuffer_info(env_id, value, data, length) {
1058
- let env = environments[env_id];
1059
- let len = env.getBufferInfo(env.get(value), data);
1060
- return env.setPointer(length, len);
1061
- },
1062
- napi_detach_arraybuffer(env_id, arraybuffer) {
1063
- let env = environments[env_id];
1064
- let buffer = env.get(arraybuffer);
1065
- if (typeof structuredClone === 'function') {
1066
- structuredClone(buffer, {transfer: [buffer]});
1067
- }
1068
- return NAPI_OK;
1069
- },
1070
- napi_is_detached_arraybuffer(env_id, arraybuffer, result) {
1071
- let env = environments[env_id];
1072
- let buffer = env.get(arraybuffer);
1073
- env.memory[result] = buffer.byteLength === 0 ? 1 : 0; // ??
1074
- return NAPI_OK;
1075
- },
1076
- napi_create_typedarray(env_id, type, length, arraybuffer, offset, result) {
1077
- let env = environments[env_id];
1078
- let Class = typedArrays[type];
1079
- let buffer = env.get(arraybuffer);
1080
- let buf = new Class(buffer, offset, length);
1081
- return env.createValue(buf, result);
1082
- },
1083
- napi_create_dataview(env_id, byte_length, arraybuffer, byte_offset, result) {
1084
- let env = environments[env_id];
1085
- let buffer = env.get(arraybuffer);
1086
- let view = new DataView(buffer, byte_offset, byte_length);
1087
- return env.createValue(view, result);
1088
- },
1089
- napi_get_typedarray_info(env_id, typedarray, type, length, data, arraybuffer, byte_offset) {
1090
- let env = environments[env_id];
1091
- let val = env.get(typedarray);
1092
- env.setPointer(type, typedArrays.indexOf(val.constructor));
1093
- env.setPointer(length, val.length);
1094
- env.getBufferInfo(val, data);
1095
- env.createValue(val.buffer, arraybuffer);
1096
- return env.setPointer(byte_offset, val.byteOffset);
1097
- },
1098
- napi_get_dataview_info(env_id, dataview, byte_length, data, arraybuffer, byte_offset) {
1099
- let env = environments[env_id];
1100
- let val = env.get(dataview);
1101
- env.setPointer(byte_length, val.byteLength);
1102
- env.getBufferInfo(val, data);
1103
- env.createValue(val.buffer, arraybuffer);
1104
- return env.setPointer(byte_offset, val.byteOffset);
1105
- },
1106
- napi_create_string_utf8(env_id, str, length, result) {
1107
- let env = environments[env_id];
1108
- let s = decoder.decode(env.memory.subarray(str, str + length));
1109
- return env.createValue(s, result);
1110
- },
1111
- napi_get_value_string_utf8(env_id, value, buf, bufsize, result) {
1112
- let env = environments[env_id];
1113
- let val = env.get(value);
1114
- if (buf == 0) {
1115
- return env.setPointer(result, utf8Length$1(val));
1116
- }
1117
- let res = encoder$1.encodeInto(val, env.memory.subarray(buf, buf + bufsize - 1));
1118
- env.memory[buf + res.written] = 0; // null terminate
1119
- return env.setPointer(result, res.written);
1120
- },
1121
- napi_create_string_latin1(env_id, str, length, result) {
1122
- let env = environments[env_id];
1123
- let s = latin1Decoder.decode(env.memory.subarray(str, str + length));
1124
- return env.createValue(s, result);
1125
- },
1126
- napi_get_value_string_latin1(env_id, value, buf, bufsize, result) {
1127
- let env = environments[env_id];
1128
- let val = env.get(value);
1129
- if (buf == 0) {
1130
- return env.setPointer(result, val.length);
1131
- }
1132
- let mem = env.memory;
1133
- let len = Math.min(val.length, bufsize - 1);
1134
- for (let i = 0; i < len; i++) {
1135
- let code = val.charCodeAt(i);
1136
- mem[buf++] = code;
1137
- }
1138
- mem[buf] = 0; // null terminate
1139
- return env.setPointer(result, len);
1140
- },
1141
- napi_create_string_utf16(env_id, str, length, result) {
1142
- let env = environments[env_id];
1143
- let s = utf16Decoder.decode(env.memory.subarray(str, str + length * 2));
1144
- return env.createValue(s, result);
1145
- },
1146
- napi_get_value_string_utf16(env_id, value, buf, bufsize, result) {
1147
- let env = environments[env_id];
1148
- let val = env.get(value);
1149
- if (buf == 0) {
1150
- return env.setPointer(result, val.length);
1151
- }
1152
- let mem = env.u16;
1153
- let ptr = buf >> 1;
1154
- let len = Math.min(val.length, bufsize - 1);
1155
- for (let i = 0; i < len; i++) {
1156
- let code = val.charCodeAt(i);
1157
- mem[ptr++] = code;
1158
- }
1159
- mem[ptr] = 0; // null terminate
1160
- return env.setPointer(result, len);
1161
- },
1162
- napi_create_date(env_id, time, result) {
1163
- let env = environments[env_id];
1164
- return env.createValue(new Date(time), result);
1165
- },
1166
- napi_get_date_value(env_id, value, result) {
1167
- let env = environments[env_id];
1168
- let date = env.get(value);
1169
- env.f64[result >> 3] = date.valueOf();
1170
- },
1171
- napi_create_symbol(env_id, description, result) {
1172
- let env = environments[env_id];
1173
- let desc = env.get(description);
1174
- return env.createValue(Symbol(desc), result);
1175
- },
1176
- napi_coerce_to_bool(env_id, value, result) {
1177
- let env = environments[env_id];
1178
- return env.createValue(Boolean(env.get(value)), result);
1179
- },
1180
- napi_coerce_to_number(env_id, value, result) {
1181
- let env = environments[env_id];
1182
- return env.createValue(Number(env.get(value)), result);
1183
- },
1184
- napi_coerce_to_object(env_id, value, result) {
1185
- let env = environments[env_id];
1186
- return env.createValue(Object(env.get(value)), result);
1187
- },
1188
- napi_coerce_to_string(env_id, value, result) {
1189
- let env = environments[env_id];
1190
- return env.createValue(String(env.get(value)), result);
1191
- },
1192
- napi_typeof(env_id, value, result) {
1193
- let env = environments[env_id];
1194
- let val = env.get(value);
1195
- return env.setPointer(result, (() => {
1196
- switch (typeof val) {
1197
- case 'undefined':
1198
- return 0;
1199
- case 'boolean':
1200
- return 2;
1201
- case 'number':
1202
- return 3;
1203
- case 'string':
1204
- return 4;
1205
- case 'symbol':
1206
- return 5;
1207
- case 'object':
1208
- if (val === null) {
1209
- return 1;
1210
- } else if (val instanceof ExternalValue) {
1211
- return 8;
1212
- }
1213
- return 6;
1214
- case 'function':
1215
- return 7;
1216
- case 'bigint':
1217
- return 9;
1218
- }
1219
- })());
1220
- },
1221
- napi_instanceof(env_id, object, constructor, result) {
1222
- let env = environments[env_id];
1223
- let obj = env.get(object);
1224
- let cons = env.get(constructor);
1225
- env.memory[result] = obj instanceof cons ? 1 : 0;
1226
- return NAPI_OK;
1227
- },
1228
- napi_is_array(env_id, value, result) {
1229
- let env = environments[env_id];
1230
- let val = env.get(value);
1231
- env.memory[result] = Array.isArray(val) ? 1 : 0;
1232
- return NAPI_OK;
1233
- },
1234
- napi_is_buffer(env_id, value, result) {
1235
- let env = environments[env_id];
1236
- let val = env.get(value);
1237
- env.memory[result] = (typeof globalThis.Buffer !== 'undefined' ? globalThis.Buffer.isBuffer(val) : val instanceof Uint8Array) ? 1 : 0;
1238
- return NAPI_OK;
1239
- },
1240
- napi_is_date(env_id, value, result) {
1241
- let env = environments[env_id];
1242
- let val = env.get(value);
1243
- env.memory[result] = val instanceof Date ? 1 : 0;
1244
- return NAPI_OK;
1245
- },
1246
- napi_is_error(env_id, value, result) {
1247
- let env = environments[env_id];
1248
- let err = env.get(value);
1249
- env.memory[result] = err instanceof Error ? 1 : 0;
1250
- return NAPI_OK;
1251
- },
1252
- napi_is_typedarray(env_id, value, result) {
1253
- let env = environments[env_id];
1254
- let buf = env.get(value);
1255
- env.memory[result] = ArrayBuffer.isView(buf) && !(buf instanceof DataView) ? 1 : 0;
1256
- return NAPI_OK;
1257
- },
1258
- napi_is_dataview(env_id, value, result) {
1259
- let env = environments[env_id];
1260
- let val = env.get(value);
1261
- env.memory[result] = val instanceof DataView ? 1 : 0;
1262
- return NAPI_OK;
1263
- },
1264
- napi_strict_equals(env_id, lhs, rhs, result) {
1265
- let env = environments[env_id];
1266
- env.memory[result] = env.get(lhs) === env.get(rhs) ? 1 : 0;
1267
- return NAPI_OK;
1268
- },
1269
- napi_wrap(env_id, js_object, native_object, finalize_cb, finalize_hint, result) {
1270
- let env = environments[env_id];
1271
- let obj = env.get(js_object);
1272
- env.wrappedObjects.set(obj, native_object);
1273
- if (finalize_cb) {
1274
- let cb = env.table.get(finalize_cb);
1275
- finalizationRegistry.register(obj, new FinalizeRecord(env_id, cb, finalize_hint, native_object));
1276
- }
1277
-
1278
- if (result) {
1279
- return napi.napi_create_reference(env_id, js_object, 1, result);
1280
- }
1281
-
1282
- return NAPI_OK;
1283
- },
1284
- napi_unwrap(env_id, js_object, result) {
1285
- let env = environments[env_id];
1286
- let obj = env.get(js_object);
1287
- let native_object = env.wrappedObjects.get(obj);
1288
- env.setPointer(result, native_object);
1289
- return NAPI_OK;
1290
- },
1291
- napi_remove_wrap(env_id, js_object, result) {
1292
- let env = environments[env_id];
1293
- let obj = env.get(js_object);
1294
- let native_object = env.wrappedObjects.get(obj);
1295
- finalizationRegistry.unregister(obj);
1296
- env.wrappedObjects.delete(obj);
1297
- return env.setPointer(result, native_object);
1298
- },
1299
- napi_type_tag_object(env_id, js_object, type_tag) {
1300
- throw new Error('not implemented');
1301
- },
1302
- napi_check_object_type_tag(env_id, js_object, type_tag) {
1303
- throw new Error('not implemented');
1304
- },
1305
- napi_add_finalizer(env_id, js_object, native_object, finalize_cb, finalize_hint, result) {
1306
- let env = environments[env_id];
1307
- let obj = env.get(js_object);
1308
- let cb = env.table.get(finalize_cb);
1309
- finalizationRegistry.register(obj, new FinalizeRecord(env_id, cb, finalize_hint, native_object));
1310
- if (result) {
1311
- return napi.napi_create_reference(env_id, js_object, 1, result);
1312
- }
1313
-
1314
- return NAPI_OK;
1315
- },
1316
- napi_create_promise(env_id, deferred, promise) {
1317
- let env = environments[env_id];
1318
- let p = new Promise((resolve, reject) => {
1319
- let id = env.deferred.length;
1320
- env.deferred.push({resolve, reject});
1321
- env.setPointer(deferred, id);
1322
- });
1323
- return env.createValue(p, promise);
1324
- },
1325
- napi_resolve_deferred(env_id, deferred, resolution) {
1326
- let env = environments[env_id];
1327
- let { resolve } = env.deferred[deferred];
1328
- let value = env.get(resolution);
1329
- resolve(value);
1330
- env.deferred[deferred] = undefined;
1331
- return NAPI_OK;
1332
- },
1333
- napi_reject_deferred(env_id, deferred, rejection) {
1334
- let env = environments[env_id];
1335
- let { reject } = env.deferred[deferred];
1336
- let value = env.get(rejection);
1337
- reject(value);
1338
- env.deferred[deferred] = undefined;
1339
- return NAPI_OK;
1340
- },
1341
- napi_is_promise(env_id, value, result) {
1342
- let env = environments[env_id];
1343
- let val = env.get(value);
1344
- env.memory[result] = val instanceof Promise ? 1 : 0;
1345
- return NAPI_OK;
1346
- },
1347
- napi_run_script(env_id, script, result) {
1348
- let env = environments[env_id];
1349
- let source = env.get(script);
1350
- let res = (0, eval)(source);
1351
- return env.createValue(res, result);
1352
- },
1353
- napi_create_external(env_id, data, finalize_cb, finalize_hint, result) {
1354
- let env = environments[env_id];
1355
- let external = new ExternalValue;
1356
- env.externalObjects.set(external, data);
1357
- if (finalize_cb) {
1358
- let cb = env.table.get(finalize_cb);
1359
- finalizationRegistry.register(external, new FinalizeRecord(env_id, cb, finalize_hint, data));
1360
- }
1361
- return env.createValue(external, result);
1362
- },
1363
- napi_get_value_external(env_id, value, result) {
1364
- let env = environments[env_id];
1365
- let external = env.get(value);
1366
- let val = env.externalObjects.get(external);
1367
- return env.setPointer(result, val);
1368
- },
1369
- napi_adjust_external_memory() {
1370
- return NAPI_OK;
1371
- }
1372
- };
1373
-
1374
- function strlen(buf, ptr) {
1375
- let len = 0;
1376
- while (buf[ptr] !== 0) {
1377
- len++;
1378
- ptr++;
1379
- }
1380
-
1381
- return len;
1382
- }
1383
-
1384
- function utf8Length$1(string) {
1385
- let len = 0;
1386
- for (let i = 0; i < string.length; i++) {
1387
- let c = string.charCodeAt(i);
1388
-
1389
- if (c >= 0xd800 && c <= 0xdbff && i < string.length - 1) {
1390
- let c2 = string.charCodeAt(++i);
1391
- if ((c2 & 0xfc00) === 0xdc00) {
1392
- c = ((c & 0x3ff) << 10) + (c2 & 0x3ff) + 0x10000;
1393
- } else {
1394
- // unmatched surrogate.
1395
- i--;
1396
- }
1397
- }
1398
-
1399
- if ((c & 0xffffff80) === 0) {
1400
- len++;
1401
- } else if ((c & 0xfffff800) === 0) {
1402
- len += 2;
1403
- } else if ((c & 0xffff0000) === 0) {
1404
- len += 3;
1405
- } else if ((c & 0xffe00000) === 0) {
1406
- len += 4;
1407
- }
1408
- }
1409
- return len;
1410
- }
1411
-
1412
- const path = require$$0;
1413
- const micromatch = micromatch_1;
1414
- const isGlob = isGlob$1;
1415
-
1416
- function normalizeOptions(dir, opts = {}) {
1417
- const { ignore, ...rest } = opts;
1418
-
1419
- if (Array.isArray(ignore)) {
1420
- opts = { ...rest };
1421
-
1422
- for (const value of ignore) {
1423
- if (isGlob(value)) {
1424
- if (!opts.ignoreGlobs) {
1425
- opts.ignoreGlobs = [];
1426
- }
1427
-
1428
- const regex = micromatch.makeRe(value, {
1429
- // We set `dot: true` to workaround an issue with the
1430
- // regular expression on Linux where the resulting
1431
- // negative lookahead `(?!(\\/|^)` was never matching
1432
- // in some cases. See also https://bit.ly/3UZlQDm
1433
- dot: true,
1434
- // C++ does not support lookbehind regex patterns, they
1435
- // were only added later to JavaScript engines
1436
- // (https://bit.ly/3V7S6UL)
1437
- lookbehinds: false
1438
- });
1439
- opts.ignoreGlobs.push(regex.source);
1440
- } else {
1441
- if (!opts.ignorePaths) {
1442
- opts.ignorePaths = [];
1443
- }
1444
-
1445
- opts.ignorePaths.push(path.resolve(dir, value));
1446
- }
1447
- }
1448
- }
1449
-
1450
- return opts;
1451
- }
1452
-
1453
- var createWrapper = (binding) => {
1454
- return {
1455
- writeSnapshot(dir, snapshot, opts) {
1456
- return binding.writeSnapshot(
1457
- path.resolve(dir),
1458
- path.resolve(snapshot),
1459
- normalizeOptions(dir, opts),
1460
- );
1461
- },
1462
- getEventsSince(dir, snapshot, opts) {
1463
- return binding.getEventsSince(
1464
- path.resolve(dir),
1465
- path.resolve(snapshot),
1466
- normalizeOptions(dir, opts),
1467
- );
1468
- },
1469
- async subscribe(dir, fn, opts) {
1470
- dir = path.resolve(dir);
1471
- opts = normalizeOptions(dir, opts);
1472
- await binding.subscribe(dir, fn, opts);
1473
-
1474
- return {
1475
- unsubscribe() {
1476
- return binding.unsubscribe(dir, fn, opts);
1477
- },
1478
- };
1479
- },
1480
- unsubscribe(dir, fn, opts) {
1481
- return binding.unsubscribe(
1482
- path.resolve(dir),
1483
- fn,
1484
- normalizeOptions(dir, opts),
1485
- );
1486
- }
1487
- };
1488
- };
1489
-
1490
- let env$1;
1491
- let encoder = new TextEncoder;
1492
-
1493
- let constants = {
1494
- O_ACCMODE: 0o00000003,
1495
- O_RDONLY: 0,
1496
- O_WRONLY: 0o00000001,
1497
- O_RDWR: 0o00000002,
1498
- O_CREAT: 0o00000100,
1499
- O_EXCL: 0o00000200,
1500
- O_NOCTTY: 0o00000400,
1501
- O_TRUNC: 0o00001000,
1502
- O_APPEND: 0o00002000,
1503
- O_NONBLOCK: 0o00004000,
1504
- O_SYNC: 0o00010000,
1505
- FASYNC: 0o00020000,
1506
- O_DIRECT: 0o00040000,
1507
- O_LARGEFILE: 0o00100000,
1508
- O_DIRECTORY: 0o00200000,
1509
- O_NOFOLLOW: 0o00400000,
1510
- O_NOATIME: 0o01000000,
1511
- O_CLOEXEC: 0o02000000
1512
- };
1513
-
1514
- napi.napi_get_last_error_info = () => {};
1515
-
1516
- const fds = new Map();
1517
- const dirs = new Map();
1518
- const regexCache = new Map();
1519
- const watches = [null];
1520
-
1521
- const wasm_env = {
1522
- __syscall_newfstatat(dirfd, path, buf, flags) {
1523
- let dir = dirfd === -100 ? process.cwd() : fds.get(dirfd).path;
1524
- let p = require$$0.resolve(dir, env$1.getString(path));
1525
- let nofollow = flags & 256;
1526
- try {
1527
- let stat = nofollow ? fs.lstatSync(p, {bigint: true}) : fs.statSync(p, {bigint: true});
1528
- return writeStat(stat, buf);
1529
- } catch (err) {
1530
- env$1.i32[env$1.instance.exports.__errno_location >> 2] = err.errno;
1531
- return -1;
1532
- }
1533
- },
1534
- __syscall_lstat64(path, buf) {
1535
- let p = env$1.getString(path);
1536
- try {
1537
- let stat = fs.lstatSync(p, {bigint: true});
1538
- return writeStat(stat, buf);
1539
- } catch (err) {
1540
- env$1.i32[env$1.instance.exports.__errno_location >> 2] = err.errno;
1541
- return -1;
1542
- }
1543
- },
1544
- __syscall_fstat64(fd, buf) {
1545
- try {
1546
- let stat = fs.fstatSync(fd, {bigint: true});
1547
- return writeStat(stat, buf);
1548
- } catch (err) {
1549
- env$1.i32[env$1.instance.exports.__errno_location >> 2] = err.errno;
1550
- return -1;
1551
- }
1552
- },
1553
- __syscall_stat64(path, buf) {
1554
- let p = env$1.getString(path);
1555
- try {
1556
- let stat = fs.statSync(p, {bigint: true});
1557
- return writeStat(stat, buf);
1558
- } catch (err) {
1559
- env$1.i32[env$1.instance.exports.__errno_location >> 2] = err.errno;
1560
- return -1;
1561
- }
1562
- },
1563
- __syscall_getdents64(fd, dirp, count) {
1564
- let p = fds.get(fd).path;
1565
- let dir = dirs.get(fd);
1566
- let entries = dir?.entries;
1567
- if (!entries) {
1568
- try {
1569
- entries = fs.readdirSync(p, {withFileTypes: true});
1570
- } catch (err) {
1571
- env$1.i32[env$1.instance.exports.__errno_location >> 2] = err.errno;
1572
- return -1;
1573
- }
1574
- }
1575
-
1576
- let start = dirp;
1577
- let i = dir?.index || 0;
1578
- for (; i < entries.length; i++) {
1579
- let entry = entries[i];
1580
- let type = entry.isFIFO() ? 1
1581
- : entry.isCharacterDevice() ? 2
1582
- : entry.isDirectory() ? 4
1583
- : entry.isBlockDevice() ? 6
1584
- : entry.isFile() ? 8
1585
- : entry.isSymbolicLink() ? 10
1586
- : entry.isSocket() ? 12
1587
- : 0;
1588
- let len = align(utf8Length(entry.name) + 20, 8);
1589
- if ((dirp - start + len) > count) {
1590
- break;
1591
- }
1592
-
1593
- // Write a linux_dirent64 struct into wasm memory.
1594
- env$1.u64[dirp >> 3] = 1n; // ino
1595
- env$1.u64[(dirp + 8) >> 3] = BigInt((dirp - start) + len); // offset
1596
- env$1.u16[(dirp + 16) >> 1] = len;
1597
- env$1.memory[dirp + 18] = type;
1598
- let {written} = encoder.encodeInto(entry.name, env$1.memory.subarray(dirp + 19));
1599
- env$1.memory[dirp + 19 + written] = 0; // null terminate
1600
- dirp += len;
1601
- }
1602
-
1603
- dirs.set(fd, {index: i, entries});
1604
- return dirp - start;
1605
- },
1606
- __syscall_openat(dirfd, path, flags, mode) {
1607
- // Convert flags to Node values.
1608
- let f = 0;
1609
- for (let c in constants) {
1610
- if (flags & constants[c]) {
1611
- f |= fs.constants[c] || 0;
1612
- }
1613
- }
1614
- let dir = dirfd === -100 ? process.cwd() : fds.get(dirfd)?.path;
1615
- if (!dir) {
1616
- env$1.i32[env$1.instance.exports.__errno_location >> 2] = 9970; // ENOTDIR
1617
- return -1;
1618
- }
1619
- let p = require$$0.resolve(dir, env$1.getString(path));
1620
- try {
1621
- let fd = fs.openSync(p, f);
1622
- fds.set(fd, {path: p, flags});
1623
- return fd;
1624
- } catch (err) {
1625
- env$1.i32[env$1.instance.exports.__errno_location >> 2] = err.errno;
1626
- return -1;
1627
- }
1628
- },
1629
- __syscall_fcntl64(fd, cmd) {
1630
- switch (cmd) {
1631
- case 3:
1632
- return fds.get(fd).flags;
1633
- case 2:
1634
- return 0;
1635
- default:
1636
- throw new Error('Unknown fcntl64 call: ' + cmd);
1637
- }
1638
- },
1639
- __syscall_ioctl() {},
1640
- emscripten_resize_heap() {
1641
- return 0;
1642
- },
1643
- abort() {},
1644
- wasm_backend_add_watch(filename, backend) {
1645
- let path = env$1.getString(filename);
1646
- let watch = fs.watch(path, {encoding: 'buffer'}, (eventType, filename) => {
1647
- if (filename) {
1648
- let type = eventType === 'change' ? 1 : 2;
1649
- let fptr = env$1.instance.exports.malloc(filename.byteLength + 1);
1650
- env$1.memory.set(filename, fptr);
1651
- env$1.memory[fptr + filename.byteLength] = 0;
1652
- env$1.instance.exports.wasm_backend_event_handler(backend, wd, type, fptr);
1653
- env$1.instance.exports.free(fptr);
1654
- }
1655
- });
1656
-
1657
- let wd = watches.length;
1658
- watches.push(watch);
1659
- return wd;
1660
- },
1661
- wasm_backend_remove_watch(wd) {
1662
- watches[wd].close();
1663
- watches[wd] = undefined;
1664
- },
1665
- set_timeout(ms, ctx) {
1666
- return setTimeout(() => {
1667
- env$1.instance.exports.on_timeout(ctx);
1668
- }, ms);
1669
- },
1670
- clear_timeout(t) {
1671
- clearTimeout(t);
1672
- },
1673
- emscripten_date_now() {
1674
- return Date.now();
1675
- },
1676
- _emscripten_get_now_is_monotonic() {
1677
- return true;
1678
- },
1679
- emscripten_get_now() {
1680
- return performance.now();
1681
- },
1682
- wasm_regex_match(string, regex) {
1683
- let re = regexCache.get(regex);
1684
- if (!re) {
1685
- re = new RegExp(env$1.getString(regex));
1686
- regexCache.set(regex, re);
1687
- }
1688
- return re.test(env$1.getString(string)) ? 1 : 0;
1689
- }
1690
- };
1691
-
1692
- const wasi = {
1693
- fd_close(fd) {
1694
- fs.closeSync(fd);
1695
- fds.delete(fd);
1696
- dirs.delete(fd);
1697
- return 0;
1698
- },
1699
- fd_seek(fd, offset_low, offset_high, whence, newOffset) {
1700
- return 0;
1701
- },
1702
- fd_write(fd, iov, iovcnt, pnum) {
1703
- let buffers = [];
1704
- for (let i = 0; i < iovcnt; i++) {
1705
- let ptr = env$1.u32[iov >> 2];
1706
- let len = env$1.u32[(iov + 4) >> 2];
1707
- iov += 8;
1708
- if (len > 0) {
1709
- buffers.push(env$1.memory.subarray(ptr, ptr + len));
1710
- }
1711
- }
1712
- let wrote = fs.writevSync(fd, buffers);
1713
- env$1.u32[pnum >> 2] = wrote;
1714
- return 0;
1715
- },
1716
- fd_read(fd, iov, iovcnt, pnum) {
1717
- let buffers = [];
1718
- for (let i = 0; i < iovcnt; i++) {
1719
- let ptr = env$1.u32[iov >> 2];
1720
- let len = env$1.u32[(iov + 4) >> 2];
1721
- iov += 8;
1722
- if (len > 0) {
1723
- buffers.push(env$1.memory.subarray(ptr, ptr + len));
1724
- }
1725
- }
1726
-
1727
- let read = fs.readvSync(fd, buffers);
1728
- env$1.u32[pnum >> 2] = read;
1729
- return 0;
1730
- }
1731
- };
1732
-
1733
- function writeStat(stat, buf) {
1734
- env$1.i32[buf >> 2] = Number(stat.dev);
1735
- env$1.i32[(buf + 4) >> 2] = Number(stat.mode);
1736
- env$1.u32[(buf + 8) >> 2] = Number(stat.nlink);
1737
- env$1.i32[(buf + 12) >> 2] = Number(stat.uid);
1738
- env$1.i32[(buf + 16) >> 2] = Number(stat.gid);
1739
- env$1.i32[(buf + 20) >> 2] = Number(stat.rdev);
1740
- env$1.u64[(buf + 24) >> 3] = stat.size;
1741
- env$1.i32[(buf + 32) >> 2] = Number(stat.blksize);
1742
- env$1.i32[(buf + 36) >> 2] = Number(stat.blocks);
1743
- env$1.u64[(buf + 40) >> 3] = stat.atimeMs;
1744
- env$1.u32[(buf + 48) >> 2] = Number(stat.atimeNs);
1745
- env$1.u64[(buf + 56) >> 3] = stat.mtimeMs;
1746
- env$1.u32[(buf + 64) >> 2] = Number(stat.mtimeNs);
1747
- env$1.u64[(buf + 72) >> 3] = stat.ctimeMs;
1748
- env$1.u32[(buf + 80) >> 2] = Number(stat.ctimeNs);
1749
- env$1.u64[(buf + 88) >> 3] = stat.ino;
1750
- return 0;
1751
- }
1752
-
1753
- function utf8Length(string) {
1754
- let len = 0;
1755
- for (let i = 0; i < string.length; i++) {
1756
- let c = string.charCodeAt(i);
1757
-
1758
- if (c >= 0xd800 && c <= 0xdbff && i < string.length - 1) {
1759
- let c2 = string.charCodeAt(++i);
1760
- if ((c2 & 0xfc00) === 0xdc00) {
1761
- c = ((c & 0x3ff) << 10) + (c2 & 0x3ff) + 0x10000;
1762
- } else {
1763
- // unmatched surrogate.
1764
- i--;
1765
- }
1766
- }
1767
-
1768
- if ((c & 0xffffff80) === 0) {
1769
- len++;
1770
- } else if ((c & 0xfffff800) === 0) {
1771
- len += 2;
1772
- } else if ((c & 0xffff0000) === 0) {
1773
- len += 3;
1774
- } else if ((c & 0xffe00000) === 0) {
1775
- len += 4;
1776
- }
1777
- }
1778
- return len;
1779
- }
1780
-
1781
- function align(len, p) {
1782
- return Math.ceil(len / p) * p;
1783
- }
1784
-
1785
- let wasmBytes = fs.readFileSync(new URL('watcher.wasm', import.meta.url));
1786
- let wasmModule = new WebAssembly.Module(wasmBytes);
1787
- let instance = new WebAssembly.Instance(wasmModule, {
1788
- napi,
1789
- env: wasm_env,
1790
- wasi_snapshot_preview1: wasi
1791
- });
1792
-
1793
- env$1 = new Environment(instance);
1794
- let wrapper = createWrapper(env$1.exports);
1795
-
1796
- function writeSnapshot(dir, snapshot, opts) {
1797
- return wrapper.writeSnapshot(dir, snapshot, opts);
1798
- }
1799
-
1800
- function getEventsSince(dir, snapshot, opts) {
1801
- return wrapper.getEventsSince(dir, snapshot, opts);
1802
- }
1803
-
1804
- function subscribe(dir, fn, opts) {
1805
- return wrapper.subscribe(dir, fn, opts);
1806
- }
1807
-
1808
- function unsubscribe(dir, fn, opts) {
1809
- return wrapper.unsubscribe(dir, fn, opts);
1810
- }
1811
-
1812
- export { getEventsSince, subscribe, unsubscribe, writeSnapshot };