mol_wire_lib 1.0.1185 → 1.0.1187

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.test.js CHANGED
@@ -2599,27 +2599,19 @@ var $node = new Proxy({ require }, {
2599
2599
  return target.require(name);
2600
2600
  if (name[0] === '.')
2601
2601
  return target.require(name);
2602
- const path = target.require('path');
2603
- const fs = target.require('fs');
2604
- let dir = path.resolve('.');
2605
- const suffix = `./node_modules/${name}`;
2606
- const $$ = $;
2607
- while (!fs.existsSync(path.join(dir, suffix))) {
2608
- const parent = path.resolve(dir, '..');
2609
- if (parent === dir) {
2610
- $$.$mol_exec('.', 'npm', 'install', '--omit=dev', name);
2611
- try {
2612
- $$.$mol_exec('.', 'npm', 'install', '--omit=dev', '@types/' + name);
2613
- }
2614
- catch (e) {
2615
- if ($$.$mol_fail_catch(e)) {
2616
- $$.$mol_fail_log(e);
2617
- }
2618
- }
2619
- break;
2602
+ try {
2603
+ target.require.resolve(name);
2604
+ }
2605
+ catch {
2606
+ const $$ = $;
2607
+ $$.$mol_exec('.', 'npm', 'install', '--omit=dev', name);
2608
+ try {
2609
+ $$.$mol_exec('.', 'npm', 'install', '--omit=dev', '@types/' + name);
2620
2610
  }
2621
- else {
2622
- dir = parent;
2611
+ catch (e) {
2612
+ if ($$.$mol_fail_catch(e)) {
2613
+ $$.$mol_fail_log(e);
2614
+ }
2623
2615
  }
2624
2616
  }
2625
2617
  try {
@@ -2646,6 +2638,318 @@ require = (req => Object.assign(function require(name) {
2646
2638
  return $node[name];
2647
2639
  }, req))(require);
2648
2640
 
2641
+ ;
2642
+ "use strict";
2643
+ var $;
2644
+ (function ($_1) {
2645
+ $mol_test({
2646
+ 'FQN of anon function'($) {
2647
+ const $$ = Object.assign($, { $mol_func_name_test: (() => () => { })() });
2648
+ $mol_assert_equal($$.$mol_func_name_test.name, '');
2649
+ $mol_assert_equal($$.$mol_func_name($$.$mol_func_name_test), '$mol_func_name_test');
2650
+ $mol_assert_equal($$.$mol_func_name_test.name, '$mol_func_name_test');
2651
+ },
2652
+ });
2653
+ })($ || ($ = {}));
2654
+
2655
+ ;
2656
+ "use strict";
2657
+ var $;
2658
+ (function ($) {
2659
+ class $mol_error_mix extends AggregateError {
2660
+ cause;
2661
+ name = $$.$mol_func_name(this.constructor).replace(/^\$/, '') + '_Error';
2662
+ constructor(message, cause = {}, ...errors) {
2663
+ super(errors, message, { cause });
2664
+ this.cause = cause;
2665
+ const stack_get = Object.getOwnPropertyDescriptor(this, 'stack')?.get ?? (() => super.stack);
2666
+ Object.defineProperty(this, 'stack', {
2667
+ 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()
2668
+ .replace(/at /gm, ' at ')
2669
+ .replace(/^(?! +at )(.*)/gm, ' at | $1 (#)')).join('\n')
2670
+ });
2671
+ }
2672
+ static [Symbol.toPrimitive]() {
2673
+ return this.toString();
2674
+ }
2675
+ static toString() {
2676
+ return $$.$mol_func_name(this);
2677
+ }
2678
+ static make(...params) {
2679
+ return new this(...params);
2680
+ }
2681
+ }
2682
+ $.$mol_error_mix = $mol_error_mix;
2683
+ })($ || ($ = {}));
2684
+
2685
+ ;
2686
+ "use strict";
2687
+ var $;
2688
+ (function ($) {
2689
+ $mol_test({
2690
+ 'auto name'() {
2691
+ class Invalid extends $mol_error_mix {
2692
+ }
2693
+ const mix = new Invalid('foo');
2694
+ $mol_assert_equal(mix.name, 'Invalid_Error');
2695
+ },
2696
+ 'simpe mix'() {
2697
+ const mix = new $mol_error_mix('foo', {}, new Error('bar'), new Error('lol'));
2698
+ $mol_assert_equal(mix.message, 'foo');
2699
+ $mol_assert_equal(mix.errors.map(e => e.message), ['bar', 'lol']);
2700
+ },
2701
+ 'provide additional info'() {
2702
+ class Invalid extends $mol_error_mix {
2703
+ }
2704
+ 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' }));
2705
+ const hints = [];
2706
+ if (mix instanceof $mol_error_mix) {
2707
+ for (const er of mix.errors) {
2708
+ if (er instanceof Invalid) {
2709
+ hints.push(er.cause?.hint ?? '');
2710
+ }
2711
+ }
2712
+ }
2713
+ $mol_assert_equal(hints, ['> 8 letters', 'need capital letter']);
2714
+ },
2715
+ });
2716
+ })($ || ($ = {}));
2717
+
2718
+ ;
2719
+ "use strict";
2720
+ var $;
2721
+ (function ($_1) {
2722
+ $mol_test_mocks.push($ => {
2723
+ $.$mol_log3_come = () => { };
2724
+ $.$mol_log3_done = () => { };
2725
+ $.$mol_log3_fail = () => { };
2726
+ $.$mol_log3_warn = () => { };
2727
+ $.$mol_log3_rise = () => { };
2728
+ $.$mol_log3_area = () => () => { };
2729
+ });
2730
+ })($ || ($ = {}));
2731
+
2732
+ ;
2733
+ "use strict";
2734
+ var $;
2735
+ (function ($) {
2736
+ function $mol_env() {
2737
+ return {};
2738
+ }
2739
+ $.$mol_env = $mol_env;
2740
+ })($ || ($ = {}));
2741
+
2742
+ ;
2743
+ "use strict";
2744
+ var $;
2745
+ (function ($) {
2746
+ $.$mol_env = function $mol_env() {
2747
+ return this.process.env;
2748
+ };
2749
+ })($ || ($ = {}));
2750
+
2751
+ ;
2752
+ "use strict";
2753
+
2754
+ ;
2755
+ "use strict";
2756
+
2757
+ ;
2758
+ "use strict";
2759
+
2760
+ ;
2761
+ "use strict";
2762
+ var $;
2763
+ (function ($_1) {
2764
+ $mol_test({
2765
+ 'test types'($) {
2766
+ class A {
2767
+ static a() {
2768
+ return Promise.resolve('');
2769
+ }
2770
+ static b() {
2771
+ return $mol_wire_sync(this).a();
2772
+ }
2773
+ }
2774
+ },
2775
+ async 'test method from host'($) {
2776
+ let count = 0;
2777
+ class A {
2778
+ static a() {
2779
+ return $mol_wire_sync(this).b();
2780
+ }
2781
+ static b() { return Promise.resolve(++count); }
2782
+ }
2783
+ $mol_assert_equal(await $mol_wire_async(A).a(), 1, count);
2784
+ },
2785
+ async 'test function'($) {
2786
+ let count = 0;
2787
+ class A {
2788
+ static a() {
2789
+ return $mol_wire_sync(this.b)();
2790
+ }
2791
+ static b() { return Promise.resolve(++count); }
2792
+ }
2793
+ $mol_assert_equal(await $mol_wire_async(A).a(), 1, count);
2794
+ },
2795
+ async 'test construct itself'($) {
2796
+ class A {
2797
+ static instances = [];
2798
+ static a() {
2799
+ const a = new ($mol_wire_sync(A))();
2800
+ this.instances.push(a);
2801
+ $mol_wire_sync(this).b();
2802
+ }
2803
+ static b() { return Promise.resolve(); }
2804
+ }
2805
+ await $mol_wire_async(A).a();
2806
+ $mol_assert_equal(A.instances.length, 2);
2807
+ $mol_assert_equal(A.instances[0] instanceof A);
2808
+ $mol_assert_equal(A.instances[0], A.instances[1]);
2809
+ }
2810
+ });
2811
+ })($ || ($ = {}));
2812
+
2813
+ ;
2814
+ "use strict";
2815
+ var $;
2816
+ (function ($) {
2817
+ class $mol_run_error extends $mol_error_mix {
2818
+ }
2819
+ $.$mol_run_error = $mol_run_error;
2820
+ const child_process = $node['child_process'];
2821
+ $.$mol_run_spawn = child_process.spawn.bind(child_process);
2822
+ $.$mol_run_spawn_sync = child_process.spawnSync.bind(child_process);
2823
+ function $mol_run_async({ dir, timeout, command, env }) {
2824
+ const args_raw = typeof command === 'string' ? command.split(' ') : command;
2825
+ const [app, ...args] = args_raw;
2826
+ if (!env?.MOL_RUN_ASYNC) {
2827
+ this.$mol_log3_come({
2828
+ place: '$mol_run_sync',
2829
+ message: 'Run',
2830
+ command: args_raw.join(' '),
2831
+ dir: $node.path.relative('', dir),
2832
+ });
2833
+ const res = this.$mol_run_spawn_sync(app, args, { shell: true, cwd: dir, env });
2834
+ if (res.status)
2835
+ $mol_fail(new Error(res.stderr.toString() || 'Exit(' + res.status + ')'));
2836
+ return res;
2837
+ }
2838
+ const sub = this.$mol_run_spawn(app, args, {
2839
+ shell: true,
2840
+ cwd: dir,
2841
+ env
2842
+ });
2843
+ this.$mol_log3_come({
2844
+ place: '$mol_run_async',
2845
+ pid: sub.pid,
2846
+ message: 'Run',
2847
+ command: args_raw.join(' '),
2848
+ dir: $node.path.relative('', dir),
2849
+ });
2850
+ let killed = false;
2851
+ let timer;
2852
+ const std_data = [];
2853
+ const error_data = [];
2854
+ const add = (std_chunk, error_chunk) => {
2855
+ if (std_chunk)
2856
+ std_data.push(std_chunk);
2857
+ if (error_chunk)
2858
+ error_data.push(error_chunk);
2859
+ if (!timeout)
2860
+ return;
2861
+ clearTimeout(timer);
2862
+ timer = setTimeout(() => {
2863
+ const signal = killed ? 'SIGKILL' : 'SIGTERM';
2864
+ killed = true;
2865
+ add();
2866
+ sub.kill(signal);
2867
+ }, timeout);
2868
+ };
2869
+ add();
2870
+ sub.stdout?.on('data', data => add(data));
2871
+ sub.stderr?.on('data', data => add(undefined, data));
2872
+ const promise = new Promise((done, fail) => {
2873
+ const close = (error, status = null, signal = null) => {
2874
+ if (!timer && timeout)
2875
+ return;
2876
+ clearTimeout(timer);
2877
+ timer = undefined;
2878
+ const res = {
2879
+ pid: sub.pid,
2880
+ status,
2881
+ signal,
2882
+ get stdout() { return Buffer.concat(std_data); },
2883
+ get stderr() { return Buffer.concat(error_data); }
2884
+ };
2885
+ this.$mol_log3_done({
2886
+ place: '$mol_run_async',
2887
+ pid: sub.pid,
2888
+ message: 'Run',
2889
+ status,
2890
+ command: args_raw.join(' '),
2891
+ dir: $node.path.relative('', dir),
2892
+ });
2893
+ if (error || status || killed)
2894
+ return fail(new $mol_run_error((res.stderr.toString() || res.stdout.toString() || 'Run error') + (killed ? ', timeout' : ''), { signal, timeout: killed }, ...error ? [error] : []));
2895
+ done(res);
2896
+ };
2897
+ sub.on('disconnect', () => close(new Error('Disconnected')));
2898
+ sub.on('error', err => close(err));
2899
+ sub.on('exit', (status, signal) => close(null, status, signal));
2900
+ });
2901
+ return Object.assign(promise, { destructor: () => {
2902
+ clearTimeout(timer);
2903
+ sub.kill('SIGKILL');
2904
+ } });
2905
+ }
2906
+ $.$mol_run_async = $mol_run_async;
2907
+ function $mol_run(options) {
2908
+ if (!options.env)
2909
+ options = { ...options, env: this.$mol_env() };
2910
+ return $mol_wire_sync(this).$mol_run_async(options);
2911
+ }
2912
+ $.$mol_run = $mol_run;
2913
+ })($ || ($ = {}));
2914
+
2915
+ ;
2916
+ "use strict";
2917
+ var $;
2918
+ (function ($_1) {
2919
+ $mol_test({
2920
+ async 'exec timeout auto kill child process'($) {
2921
+ let close_mock = () => { };
2922
+ const context_mock = $.$mol_ambient({
2923
+ $mol_run_spawn: () => ({
2924
+ on(name, cb) {
2925
+ if (name === 'exit')
2926
+ close_mock = cb;
2927
+ },
2928
+ kill() { close_mock(); }
2929
+ })
2930
+ });
2931
+ let message = '';
2932
+ try {
2933
+ const res = await $mol_wire_async(context_mock).$mol_run({ command: 'sleep 10', dir: '.', timeout: 10, env: { 'MOL_RUN_ASYNC': '1' } });
2934
+ }
2935
+ catch (e) {
2936
+ message = e.message;
2937
+ }
2938
+ $mol_assert_equal(message, 'Run error, timeout');
2939
+ }
2940
+ });
2941
+ })($ || ($ = {}));
2942
+
2943
+ ;
2944
+ "use strict";
2945
+ var $;
2946
+ (function ($) {
2947
+ function $mol_exec(dir, command, ...args) {
2948
+ return this.$mol_run({ command: [command, ...args], dir });
2949
+ }
2950
+ $.$mol_exec = $mol_exec;
2951
+ })($ || ($ = {}));
2952
+
2649
2953
  ;
2650
2954
  "use strict";
2651
2955
  var $;
@@ -2714,15 +3018,6 @@ var $;
2714
3018
  ;
2715
3019
  "use strict";
2716
3020
 
2717
- ;
2718
- "use strict";
2719
-
2720
- ;
2721
- "use strict";
2722
-
2723
- ;
2724
- "use strict";
2725
-
2726
3021
  ;
2727
3022
  "use strict";
2728
3023
  var $;
@@ -3470,20 +3765,6 @@ var $;
3470
3765
  });
3471
3766
  })($ || ($ = {}));
3472
3767
 
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
-
3487
3768
  ;
3488
3769
  "use strict";
3489
3770
 
@@ -3493,20 +3774,6 @@ var $;
3493
3774
  ;
3494
3775
  "use strict";
3495
3776
 
3496
- ;
3497
- "use strict";
3498
- var $;
3499
- (function ($_1) {
3500
- $mol_test({
3501
- 'FQN of anon function'($) {
3502
- const $$ = Object.assign($, { $mol_func_name_test: (() => () => { })() });
3503
- $mol_assert_equal($$.$mol_func_name_test.name, '');
3504
- $mol_assert_equal($$.$mol_func_name($$.$mol_func_name_test), '$mol_func_name_test');
3505
- $mol_assert_equal($$.$mol_func_name_test.name, '$mol_func_name_test');
3506
- },
3507
- });
3508
- })($ || ($ = {}));
3509
-
3510
3777
  ;
3511
3778
  "use strict";
3512
3779
  var $;
@@ -4049,83 +4316,30 @@ var $;
4049
4316
  this.last.push(next);
4050
4317
  }
4051
4318
  }
4052
- const name = $mol_wire_async(NameLogger).send;
4053
- name('john');
4054
- const promise = name('jin');
4055
- $.$mol_after_mock_warp();
4056
- await promise;
4057
- $mol_assert_like(NameLogger.first, ['john', 'jin']);
4058
- $mol_assert_like(NameLogger.last, ['jin']);
4059
- },
4060
- async 'Latest function calls wins'($) {
4061
- const first = [];
4062
- const last = [];
4063
- function send_name(next) {
4064
- $mol_wire_sync(first).push(next);
4065
- $.$mol_wait_timeout(0);
4066
- last.push(next);
4067
- }
4068
- const name = $mol_wire_async(send_name);
4069
- name('john');
4070
- const promise = name('jin');
4071
- $.$mol_after_mock_warp();
4072
- await promise;
4073
- $mol_assert_like(first, ['john', 'jin']);
4074
- $mol_assert_like(last, ['jin']);
4075
- },
4076
- });
4077
- })($ || ($ = {}));
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);
4319
+ const name = $mol_wire_async(NameLogger).send;
4320
+ name('john');
4321
+ const promise = name('jin');
4322
+ $.$mol_after_mock_warp();
4323
+ await promise;
4324
+ $mol_assert_like(NameLogger.first, ['john', 'jin']);
4325
+ $mol_assert_like(NameLogger.last, ['jin']);
4113
4326
  },
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(); }
4327
+ async 'Latest function calls wins'($) {
4328
+ const first = [];
4329
+ const last = [];
4330
+ function send_name(next) {
4331
+ $mol_wire_sync(first).push(next);
4332
+ $.$mol_wait_timeout(0);
4333
+ last.push(next);
4123
4334
  }
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
- }
4335
+ const name = $mol_wire_async(send_name);
4336
+ name('john');
4337
+ const promise = name('jin');
4338
+ $.$mol_after_mock_warp();
4339
+ await promise;
4340
+ $mol_assert_like(first, ['john', 'jin']);
4341
+ $mol_assert_like(last, ['jin']);
4342
+ },
4129
4343
  });
4130
4344
  })($ || ($ = {}));
4131
4345
 
@@ -5078,224 +5292,5 @@ var $;
5078
5292
  });
5079
5293
  })($ || ($ = {}));
5080
5294
 
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
-
5300
5295
 
5301
5296
  //# sourceMappingURL=node.test.js.map