mol_wire_lib 1.0.1182 → 1.0.1184

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/node.mjs CHANGED
@@ -1910,10 +1910,19 @@ var $;
1910
1910
  "use strict";
1911
1911
  var $;
1912
1912
  (function ($) {
1913
+ const factories = new WeakMap();
1914
+ function factory(val) {
1915
+ let make = factories.get(val);
1916
+ if (make)
1917
+ return make;
1918
+ make = $mol_func_name_from((...args) => new val(...args), val);
1919
+ factories.set(val, make);
1920
+ return make;
1921
+ }
1913
1922
  function $mol_wire_sync(obj) {
1914
1923
  return new Proxy(obj, {
1915
1924
  get(obj, field) {
1916
- const val = obj[field];
1925
+ let val = obj[field];
1917
1926
  if (typeof val !== 'function')
1918
1927
  return val;
1919
1928
  const temp = $mol_wire_task.getter(val);
@@ -1922,10 +1931,13 @@ var $;
1922
1931
  return fiber.sync();
1923
1932
  };
1924
1933
  },
1934
+ construct(obj, args) {
1935
+ const temp = $mol_wire_task.getter(factory(obj));
1936
+ return temp(obj, args).sync();
1937
+ },
1925
1938
  apply(obj, self, args) {
1926
1939
  const temp = $mol_wire_task.getter(obj);
1927
- const fiber = temp(self, args);
1928
- return fiber.sync();
1940
+ return temp(self, args).sync();
1929
1941
  },
1930
1942
  });
1931
1943
  }
package/node.test.js CHANGED
@@ -1901,10 +1901,19 @@ var $;
1901
1901
  "use strict";
1902
1902
  var $;
1903
1903
  (function ($) {
1904
+ const factories = new WeakMap();
1905
+ function factory(val) {
1906
+ let make = factories.get(val);
1907
+ if (make)
1908
+ return make;
1909
+ make = $mol_func_name_from((...args) => new val(...args), val);
1910
+ factories.set(val, make);
1911
+ return make;
1912
+ }
1904
1913
  function $mol_wire_sync(obj) {
1905
1914
  return new Proxy(obj, {
1906
1915
  get(obj, field) {
1907
- const val = obj[field];
1916
+ let val = obj[field];
1908
1917
  if (typeof val !== 'function')
1909
1918
  return val;
1910
1919
  const temp = $mol_wire_task.getter(val);
@@ -1913,10 +1922,13 @@ var $;
1913
1922
  return fiber.sync();
1914
1923
  };
1915
1924
  },
1925
+ construct(obj, args) {
1926
+ const temp = $mol_wire_task.getter(factory(obj));
1927
+ return temp(obj, args).sync();
1928
+ },
1916
1929
  apply(obj, self, args) {
1917
1930
  const temp = $mol_wire_task.getter(obj);
1918
- const fiber = temp(self, args);
1919
- return fiber.sync();
1931
+ return temp(self, args).sync();
1920
1932
  },
1921
1933
  });
1922
1934
  }
@@ -2599,7 +2611,11 @@ var $node = new Proxy({ require }, {
2599
2611
  try {
2600
2612
  $$.$mol_exec('.', 'npm', 'install', '--omit=dev', '@types/' + name);
2601
2613
  }
2602
- catch { }
2614
+ catch (e) {
2615
+ if ($$.$mol_fail_catch(e)) {
2616
+ $$.$mol_fail_log(e);
2617
+ }
2618
+ }
2603
2619
  break;
2604
2620
  }
2605
2621
  else {
@@ -2610,7 +2626,7 @@ var $node = new Proxy({ require }, {
2610
2626
  return target.require(name);
2611
2627
  }
2612
2628
  catch (error) {
2613
- if (error.code === 'ERR_REQUIRE_ESM') {
2629
+ if ($.$mol_fail_catch(error) && error.code === 'ERR_REQUIRE_ESM') {
2614
2630
  const module = cache.get(name);
2615
2631
  if (module)
2616
2632
  return module;
@@ -2630,67 +2646,6 @@ require = (req => Object.assign(function require(name) {
2630
2646
  return $node[name];
2631
2647
  }, req))(require);
2632
2648
 
2633
- ;
2634
- "use strict";
2635
- var $;
2636
- (function ($_1) {
2637
- $mol_test_mocks.push($ => {
2638
- $.$mol_log3_come = () => { };
2639
- $.$mol_log3_done = () => { };
2640
- $.$mol_log3_fail = () => { };
2641
- $.$mol_log3_warn = () => { };
2642
- $.$mol_log3_rise = () => { };
2643
- $.$mol_log3_area = () => () => { };
2644
- });
2645
- })($ || ($ = {}));
2646
-
2647
- ;
2648
- "use strict";
2649
- var $;
2650
- (function ($) {
2651
- function $mol_env() {
2652
- return {};
2653
- }
2654
- $.$mol_env = $mol_env;
2655
- })($ || ($ = {}));
2656
-
2657
- ;
2658
- "use strict";
2659
- var $;
2660
- (function ($) {
2661
- $.$mol_env = function $mol_env() {
2662
- return this.process.env;
2663
- };
2664
- })($ || ($ = {}));
2665
-
2666
- ;
2667
- "use strict";
2668
- var $;
2669
- (function ($) {
2670
- function $mol_exec(dir, command, ...args) {
2671
- let [app, ...args0] = command.split(' ');
2672
- args = [...args0, ...args];
2673
- this.$mol_log3_come({
2674
- place: '$mol_exec',
2675
- dir: $node.path.relative('', dir),
2676
- message: 'Run',
2677
- command: `${app} ${args.join(' ')}`,
2678
- });
2679
- var res = $node['child_process'].spawnSync(app, args, {
2680
- cwd: $node.path.resolve(dir),
2681
- shell: true,
2682
- env: this.$mol_env(),
2683
- });
2684
- if (res.status || res.error) {
2685
- return $mol_fail(res.error || new Error(res.stderr.toString(), { cause: res.stdout }));
2686
- }
2687
- if (!res.stdout)
2688
- res.stdout = Buffer.from([]);
2689
- return res;
2690
- }
2691
- $.$mol_exec = $mol_exec;
2692
- })($ || ($ = {}));
2693
-
2694
2649
  ;
2695
2650
  "use strict";
2696
2651
  var $;
@@ -3515,6 +3470,20 @@ var $;
3515
3470
  });
3516
3471
  })($ || ($ = {}));
3517
3472
 
3473
+ ;
3474
+ "use strict";
3475
+ var $;
3476
+ (function ($_1) {
3477
+ $mol_test_mocks.push($ => {
3478
+ $.$mol_log3_come = () => { };
3479
+ $.$mol_log3_done = () => { };
3480
+ $.$mol_log3_fail = () => { };
3481
+ $.$mol_log3_warn = () => { };
3482
+ $.$mol_log3_rise = () => { };
3483
+ $.$mol_log3_area = () => () => { };
3484
+ });
3485
+ })($ || ($ = {}));
3486
+
3518
3487
  ;
3519
3488
  "use strict";
3520
3489
 
@@ -4054,24 +4023,6 @@ var $;
4054
4023
  ;
4055
4024
  "use strict";
4056
4025
 
4057
- ;
4058
- "use strict";
4059
- var $;
4060
- (function ($_1) {
4061
- $mol_test({
4062
- 'test types'($) {
4063
- class A {
4064
- static a() {
4065
- return Promise.resolve('');
4066
- }
4067
- static b() {
4068
- return $mol_wire_sync(this).a();
4069
- }
4070
- }
4071
- },
4072
- });
4073
- })($ || ($ = {}));
4074
-
4075
4026
  ;
4076
4027
  "use strict";
4077
4028
  var $;
@@ -4125,6 +4076,59 @@ var $;
4125
4076
  });
4126
4077
  })($ || ($ = {}));
4127
4078
 
4079
+ ;
4080
+ "use strict";
4081
+ var $;
4082
+ (function ($_1) {
4083
+ $mol_test({
4084
+ 'test types'($) {
4085
+ class A {
4086
+ static a() {
4087
+ return Promise.resolve('');
4088
+ }
4089
+ static b() {
4090
+ return $mol_wire_sync(this).a();
4091
+ }
4092
+ }
4093
+ },
4094
+ async 'test method from host'($) {
4095
+ let count = 0;
4096
+ class A {
4097
+ static a() {
4098
+ return $mol_wire_sync(this).b();
4099
+ }
4100
+ static b() { return Promise.resolve(++count); }
4101
+ }
4102
+ $mol_assert_equal(await $mol_wire_async(A).a(), 1, count);
4103
+ },
4104
+ async 'test function'($) {
4105
+ let count = 0;
4106
+ class A {
4107
+ static a() {
4108
+ return $mol_wire_sync(this.b)();
4109
+ }
4110
+ static b() { return Promise.resolve(++count); }
4111
+ }
4112
+ $mol_assert_equal(await $mol_wire_async(A).a(), 1, count);
4113
+ },
4114
+ async 'test construct itself'($) {
4115
+ class A {
4116
+ static instances = [];
4117
+ static a() {
4118
+ const a = new ($mol_wire_sync(A))();
4119
+ this.instances.push(a);
4120
+ $mol_wire_sync(this).b();
4121
+ }
4122
+ static b() { return Promise.resolve(); }
4123
+ }
4124
+ await $mol_wire_async(A).a();
4125
+ $mol_assert_equal(A.instances.length, 2);
4126
+ $mol_assert_equal(A.instances[0] instanceof A);
4127
+ $mol_assert_equal(A.instances[0], A.instances[1]);
4128
+ }
4129
+ });
4130
+ })($ || ($ = {}));
4131
+
4128
4132
  ;
4129
4133
  "use strict";
4130
4134
  var $;
@@ -5074,5 +5078,224 @@ var $;
5074
5078
  });
5075
5079
  })($ || ($ = {}));
5076
5080
 
5081
+ ;
5082
+ "use strict";
5083
+ var $;
5084
+ (function ($) {
5085
+ class $mol_error_mix extends AggregateError {
5086
+ cause;
5087
+ name = $$.$mol_func_name(this.constructor).replace(/^\$/, '') + '_Error';
5088
+ constructor(message, cause = {}, ...errors) {
5089
+ super(errors, message, { cause });
5090
+ this.cause = cause;
5091
+ const stack_get = Object.getOwnPropertyDescriptor(this, 'stack')?.get ?? (() => super.stack);
5092
+ Object.defineProperty(this, 'stack', {
5093
+ get: () => (stack_get.call(this) ?? this.message) + '\n' + [JSON.stringify(this.cause, null, ' ') ?? 'no cause', ...this.errors.map(e => e.stack)].map(e => e.trim()
5094
+ .replace(/at /gm, ' at ')
5095
+ .replace(/^(?! +at )(.*)/gm, ' at | $1 (#)')).join('\n')
5096
+ });
5097
+ }
5098
+ static [Symbol.toPrimitive]() {
5099
+ return this.toString();
5100
+ }
5101
+ static toString() {
5102
+ return $$.$mol_func_name(this);
5103
+ }
5104
+ static make(...params) {
5105
+ return new this(...params);
5106
+ }
5107
+ }
5108
+ $.$mol_error_mix = $mol_error_mix;
5109
+ })($ || ($ = {}));
5110
+
5111
+ ;
5112
+ "use strict";
5113
+ var $;
5114
+ (function ($) {
5115
+ $mol_test({
5116
+ 'auto name'() {
5117
+ class Invalid extends $mol_error_mix {
5118
+ }
5119
+ const mix = new Invalid('foo');
5120
+ $mol_assert_equal(mix.name, 'Invalid_Error');
5121
+ },
5122
+ 'simpe mix'() {
5123
+ const mix = new $mol_error_mix('foo', {}, new Error('bar'), new Error('lol'));
5124
+ $mol_assert_equal(mix.message, 'foo');
5125
+ $mol_assert_equal(mix.errors.map(e => e.message), ['bar', 'lol']);
5126
+ },
5127
+ 'provide additional info'() {
5128
+ class Invalid extends $mol_error_mix {
5129
+ }
5130
+ const mix = new $mol_error_mix('Wrong password', {}, new Invalid('Too short', { value: 'p@ssw0rd', hint: '> 8 letters' }), new Invalid('Too simple', { value: 'p@ssw0rd', hint: 'need capital letter' }));
5131
+ const hints = [];
5132
+ if (mix instanceof $mol_error_mix) {
5133
+ for (const er of mix.errors) {
5134
+ if (er instanceof Invalid) {
5135
+ hints.push(er.cause?.hint ?? '');
5136
+ }
5137
+ }
5138
+ }
5139
+ $mol_assert_equal(hints, ['> 8 letters', 'need capital letter']);
5140
+ },
5141
+ });
5142
+ })($ || ($ = {}));
5143
+
5144
+ ;
5145
+ "use strict";
5146
+ var $;
5147
+ (function ($) {
5148
+ function $mol_env() {
5149
+ return {};
5150
+ }
5151
+ $.$mol_env = $mol_env;
5152
+ })($ || ($ = {}));
5153
+
5154
+ ;
5155
+ "use strict";
5156
+ var $;
5157
+ (function ($) {
5158
+ $.$mol_env = function $mol_env() {
5159
+ return this.process.env;
5160
+ };
5161
+ })($ || ($ = {}));
5162
+
5163
+ ;
5164
+ "use strict";
5165
+ var $;
5166
+ (function ($) {
5167
+ class $mol_run_error extends $mol_error_mix {
5168
+ }
5169
+ $.$mol_run_error = $mol_run_error;
5170
+ const child_process = $node['child_process'];
5171
+ $.$mol_run_spawn = child_process.spawn.bind(child_process);
5172
+ $.$mol_run_spawn_sync = child_process.spawnSync.bind(child_process);
5173
+ function $mol_run_async({ dir, timeout, command, env }) {
5174
+ const args_raw = typeof command === 'string' ? command.split(' ') : command;
5175
+ const [app, ...args] = args_raw;
5176
+ if (!env?.MOL_RUN_ASYNC) {
5177
+ this.$mol_log3_come({
5178
+ place: '$mol_run_sync',
5179
+ message: 'Run',
5180
+ command: args_raw.join(' '),
5181
+ dir: $node.path.relative('', dir),
5182
+ });
5183
+ return this.$mol_run_spawn_sync(app, args, { shell: true, cwd: dir, env });
5184
+ }
5185
+ const sub = this.$mol_run_spawn(app, args, {
5186
+ shell: true,
5187
+ cwd: dir,
5188
+ env
5189
+ });
5190
+ this.$mol_log3_come({
5191
+ place: '$mol_run_async',
5192
+ pid: sub.pid,
5193
+ message: 'Run',
5194
+ command: args_raw.join(' '),
5195
+ dir: $node.path.relative('', dir),
5196
+ });
5197
+ let killed = false;
5198
+ let timer;
5199
+ const std_data = [];
5200
+ const error_data = [];
5201
+ const add = (std_chunk, error_chunk) => {
5202
+ if (std_chunk)
5203
+ std_data.push(std_chunk);
5204
+ if (error_chunk)
5205
+ error_data.push(error_chunk);
5206
+ if (!timeout)
5207
+ return;
5208
+ clearTimeout(timer);
5209
+ timer = setTimeout(() => {
5210
+ const signal = killed ? 'SIGKILL' : 'SIGTERM';
5211
+ killed = true;
5212
+ add();
5213
+ sub.kill(signal);
5214
+ }, timeout);
5215
+ };
5216
+ add();
5217
+ sub.stdout?.on('data', data => add(data));
5218
+ sub.stderr?.on('data', data => add(undefined, data));
5219
+ const promise = new Promise((done, fail) => {
5220
+ const close = (error, status = null, signal = null) => {
5221
+ if (!timer && timeout)
5222
+ return;
5223
+ clearTimeout(timer);
5224
+ timer = undefined;
5225
+ const res = {
5226
+ pid: sub.pid,
5227
+ status,
5228
+ signal,
5229
+ get stdout() { return Buffer.concat(std_data); },
5230
+ get stderr() { return Buffer.concat(error_data); }
5231
+ };
5232
+ this.$mol_log3_done({
5233
+ place: '$mol_run_async',
5234
+ pid: sub.pid,
5235
+ message: 'Run',
5236
+ status,
5237
+ command: args_raw.join(' '),
5238
+ dir: $node.path.relative('', dir),
5239
+ });
5240
+ if (error || status || killed)
5241
+ return fail(new $mol_run_error((res.stderr.toString() || res.stdout.toString() || 'Run error') + (killed ? ', timeout' : ''), { signal, timeout: killed }, ...error ? [error] : []));
5242
+ done(res);
5243
+ };
5244
+ sub.on('disconnect', () => close(new Error('Disconnected')));
5245
+ sub.on('error', err => close(err));
5246
+ sub.on('exit', (status, signal) => close(null, status, signal));
5247
+ });
5248
+ return Object.assign(promise, { destructor: () => {
5249
+ clearTimeout(timer);
5250
+ sub.kill('SIGKILL');
5251
+ } });
5252
+ }
5253
+ $.$mol_run_async = $mol_run_async;
5254
+ function $mol_run(options) {
5255
+ if (!options.env)
5256
+ options = { ...options, env: this.$mol_env() };
5257
+ return $mol_wire_sync(this).$mol_run_async(options);
5258
+ }
5259
+ $.$mol_run = $mol_run;
5260
+ })($ || ($ = {}));
5261
+
5262
+ ;
5263
+ "use strict";
5264
+ var $;
5265
+ (function ($_1) {
5266
+ $mol_test({
5267
+ async 'exec timeout auto kill child process'($) {
5268
+ let close_mock = () => { };
5269
+ const context_mock = $.$mol_ambient({
5270
+ $mol_run_spawn: () => ({
5271
+ on(name, cb) {
5272
+ if (name === 'exit')
5273
+ close_mock = cb;
5274
+ },
5275
+ kill() { close_mock(); }
5276
+ })
5277
+ });
5278
+ let message = '';
5279
+ try {
5280
+ const res = await $mol_wire_async(context_mock).$mol_run({ command: 'sleep 10', dir: '.', timeout: 10, env: { 'MOL_RUN_ASYNC': '1' } });
5281
+ }
5282
+ catch (e) {
5283
+ message = e.message;
5284
+ }
5285
+ $mol_assert_equal(message, 'Run error, timeout');
5286
+ }
5287
+ });
5288
+ })($ || ($ = {}));
5289
+
5290
+ ;
5291
+ "use strict";
5292
+ var $;
5293
+ (function ($) {
5294
+ function $mol_exec(dir, command, ...args) {
5295
+ return this.$mol_run({ command: [command, ...args], dir });
5296
+ }
5297
+ $.$mol_exec = $mol_exec;
5298
+ })($ || ($ = {}));
5299
+
5077
5300
 
5078
5301
  //# sourceMappingURL=node.test.js.map