mol_wire_lib 1.0.1237 → 1.0.1238

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
@@ -2740,17 +2740,40 @@ var $;
2740
2740
  });
2741
2741
  })($ || ($ = {}));
2742
2742
 
2743
+ ;
2744
+ "use strict";
2745
+
2746
+ ;
2747
+ "use strict";
2748
+
2749
+ ;
2750
+ "use strict";
2751
+
2752
+ ;
2753
+ "use strict";
2754
+
2755
+ ;
2756
+ "use strict";
2757
+
2758
+ ;
2759
+ "use strict";
2760
+
2743
2761
  ;
2744
2762
  "use strict";
2745
2763
  var $;
2746
- (function ($_1) {
2747
- $mol_test_mocks.push($ => {
2748
- $.$mol_log3_come = () => { };
2749
- $.$mol_log3_done = () => { };
2750
- $.$mol_log3_fail = () => { };
2751
- $.$mol_log3_warn = () => { };
2752
- $.$mol_log3_rise = () => { };
2753
- $.$mol_log3_area = () => () => { };
2764
+ (function ($) {
2765
+ $mol_test({
2766
+ 'init with overload'() {
2767
+ class X extends $mol_object {
2768
+ foo() {
2769
+ return 1;
2770
+ }
2771
+ }
2772
+ var x = X.make({
2773
+ foo: () => 2,
2774
+ });
2775
+ $mol_assert_equal(x.foo(), 2);
2776
+ },
2754
2777
  });
2755
2778
  })($ || ($ = {}));
2756
2779
 
@@ -2773,15 +2796,6 @@ var $;
2773
2796
  };
2774
2797
  })($ || ($ = {}));
2775
2798
 
2776
- ;
2777
- "use strict";
2778
-
2779
- ;
2780
- "use strict";
2781
-
2782
- ;
2783
- "use strict";
2784
-
2785
2799
  ;
2786
2800
  "use strict";
2787
2801
  var $;
@@ -2835,6 +2849,20 @@ var $;
2835
2849
  });
2836
2850
  })($ || ($ = {}));
2837
2851
 
2852
+ ;
2853
+ "use strict";
2854
+ var $;
2855
+ (function ($_1) {
2856
+ $mol_test_mocks.push($ => {
2857
+ $.$mol_log3_come = () => { };
2858
+ $.$mol_log3_done = () => { };
2859
+ $.$mol_log3_fail = () => { };
2860
+ $.$mol_log3_warn = () => { };
2861
+ $.$mol_log3_rise = () => { };
2862
+ $.$mol_log3_area = () => () => { };
2863
+ });
2864
+ })($ || ($ = {}));
2865
+
2838
2866
  ;
2839
2867
  "use strict";
2840
2868
  var $;
@@ -2842,97 +2870,115 @@ var $;
2842
2870
  class $mol_run_error extends $mol_error_mix {
2843
2871
  }
2844
2872
  $.$mol_run_error = $mol_run_error;
2845
- const child_process = $node['child_process'];
2846
- $.$mol_run_spawn = child_process.spawn.bind(child_process);
2847
- $.$mol_run_spawn_sync = child_process.spawnSync.bind(child_process);
2848
- function $mol_run_async({ dir, timeout, command, env }) {
2849
- const args_raw = typeof command === 'string' ? command.split(' ') : command;
2850
- const [app, ...args] = args_raw;
2851
- if (!env?.MOL_RUN_ASYNC) {
2852
- this.$mol_log3_come({
2853
- place: '$mol_run_sync',
2873
+ $.$mol_run_spawn = (...args) => $node['child_process'].spawn(...args);
2874
+ $.$mol_run_spawn_sync = (...args) => $node['child_process'].spawnSync(...args);
2875
+ class $mol_run extends $mol_object {
2876
+ static async_enabled() {
2877
+ return Boolean(this.$.$mol_env()['MOL_RUN_ASYNC']);
2878
+ }
2879
+ static spawn(options) {
2880
+ const sync = !this.async_enabled() || !Boolean($mol_wire_auto());
2881
+ const env = options.env ?? this.$.$mol_env();
2882
+ return $mol_wire_sync(this).spawn_async({ ...options, sync, env });
2883
+ }
2884
+ static spawn_async({ dir, sync, timeout, command, env }) {
2885
+ const args_raw = typeof command === 'string' ? command.split(' ') : command;
2886
+ const [app, ...args] = args_raw;
2887
+ const opts = { shell: true, cwd: dir, env };
2888
+ const log_object = {
2889
+ place: `${this}.spawn()`,
2854
2890
  message: 'Run',
2855
2891
  command: args_raw.join(' '),
2856
2892
  dir: $node.path.relative('', dir),
2893
+ };
2894
+ if (sync) {
2895
+ this.$.$mol_log3_come({
2896
+ hint: 'Run inside fiber',
2897
+ ...log_object
2898
+ });
2899
+ let error;
2900
+ let res;
2901
+ try {
2902
+ res = this.$.$mol_run_spawn_sync(app, args, opts);
2903
+ error = res.error;
2904
+ }
2905
+ catch (err) {
2906
+ error = err;
2907
+ }
2908
+ if (!res || error || res.status) {
2909
+ throw new $mol_run_error(this.error_message(res), { ...log_object, status: res?.status, signal: res?.signal }, ...(error ? [error] : []));
2910
+ }
2911
+ return res;
2912
+ }
2913
+ let sub;
2914
+ try {
2915
+ sub = this.$.$mol_run_spawn(app, args, {
2916
+ ...opts,
2917
+ stdio: ['pipe', 'inherit', 'inherit'],
2918
+ });
2919
+ }
2920
+ catch (error) {
2921
+ throw new $mol_run_error(this.error_message(undefined), log_object, error);
2922
+ }
2923
+ const pid = sub.pid ?? 0;
2924
+ this.$.$mol_log3_come({
2925
+ ...log_object,
2926
+ pid,
2857
2927
  });
2858
- const res = this.$mol_run_spawn_sync(app, args, { shell: true, cwd: dir, env });
2859
- if (res.status)
2860
- $mol_fail(new Error(res.stderr.toString() || 'Exit(' + res.status + ')'));
2861
- return res;
2862
- }
2863
- const sub = this.$mol_run_spawn(app, args, {
2864
- shell: true,
2865
- cwd: dir,
2866
- env
2867
- });
2868
- this.$mol_log3_come({
2869
- place: '$mol_run_async',
2870
- pid: sub.pid,
2871
- message: 'Run',
2872
- command: args_raw.join(' '),
2873
- dir: $node.path.relative('', dir),
2874
- });
2875
- let killed = false;
2876
- let timer;
2877
- const std_data = [];
2878
- const error_data = [];
2879
- const add = (std_chunk, error_chunk) => {
2880
- if (std_chunk)
2881
- std_data.push(std_chunk);
2882
- if (error_chunk)
2883
- error_data.push(error_chunk);
2884
- if (!timeout)
2885
- return;
2886
- clearTimeout(timer);
2887
- timer = setTimeout(() => {
2888
- const signal = killed ? 'SIGKILL' : 'SIGTERM';
2889
- killed = true;
2890
- add();
2891
- sub.kill(signal);
2892
- }, timeout);
2893
- };
2894
- add();
2895
- sub.stdout?.on('data', data => add(data));
2896
- sub.stderr?.on('data', data => add(undefined, data));
2897
- const promise = new Promise((done, fail) => {
2898
- const close = (error, status = null, signal = null) => {
2899
- if (!timer && timeout)
2928
+ let timeout_kill = false;
2929
+ let timer;
2930
+ const std_data = [];
2931
+ const error_data = [];
2932
+ const add = (std_chunk, error_chunk) => {
2933
+ if (std_chunk)
2934
+ std_data.push(std_chunk);
2935
+ if (error_chunk)
2936
+ error_data.push(error_chunk);
2937
+ if (!timeout)
2900
2938
  return;
2901
2939
  clearTimeout(timer);
2902
- timer = undefined;
2903
- const res = {
2904
- pid: sub.pid,
2905
- status,
2906
- signal,
2907
- get stdout() { return Buffer.concat(std_data); },
2908
- get stderr() { return Buffer.concat(error_data); }
2909
- };
2910
- this.$mol_log3_done({
2911
- place: '$mol_run_async',
2912
- pid: sub.pid,
2913
- message: 'Run',
2914
- status,
2915
- command: args_raw.join(' '),
2916
- dir: $node.path.relative('', dir),
2917
- });
2918
- if (error || status || killed)
2919
- return fail(new $mol_run_error((res.stderr.toString() || res.stdout.toString() || 'Run error') + (killed ? ', timeout' : ''), { signal, timeout: killed }, ...error ? [error] : []));
2920
- done(res);
2940
+ timer = setTimeout(() => {
2941
+ const signal = timeout_kill ? 'SIGKILL' : 'SIGTERM';
2942
+ timeout_kill = true;
2943
+ add();
2944
+ sub.kill(signal);
2945
+ }, timeout);
2921
2946
  };
2922
- sub.on('disconnect', () => close(new Error('Disconnected')));
2923
- sub.on('error', err => close(err));
2924
- sub.on('exit', (status, signal) => close(null, status, signal));
2925
- });
2926
- return Object.assign(promise, { destructor: () => {
2927
- clearTimeout(timer);
2928
- sub.kill('SIGKILL');
2929
- } });
2930
- }
2931
- $.$mol_run_async = $mol_run_async;
2932
- function $mol_run(options) {
2933
- if (!options.env)
2934
- options = { ...options, env: this.$mol_env() };
2935
- return $mol_wire_sync(this).$mol_run_async(options);
2947
+ add();
2948
+ sub.stdout?.on('data', data => add(data));
2949
+ sub.stderr?.on('data', data => add(undefined, data));
2950
+ const result_promise = new Promise((done, fail) => {
2951
+ const close = (error, status = null, signal = null) => {
2952
+ if (!timer && timeout)
2953
+ return;
2954
+ clearTimeout(timer);
2955
+ timer = undefined;
2956
+ const res = {
2957
+ pid,
2958
+ signal,
2959
+ get stdout() { return Buffer.concat(std_data); },
2960
+ get stderr() { return Buffer.concat(error_data); }
2961
+ };
2962
+ if (error || status || timeout_kill)
2963
+ return fail(new $mol_run_error(this.error_message(res) + (timeout_kill ? ', timeout' : ''), { ...log_object, pid, status, signal, timeout_kill }, ...error ? [error] : []));
2964
+ this.$.$mol_log3_done({
2965
+ ...log_object,
2966
+ pid,
2967
+ });
2968
+ done(res);
2969
+ };
2970
+ sub.on('disconnect', () => close(new Error('Disconnected')));
2971
+ sub.on('error', err => close(err));
2972
+ sub.on('exit', (status, signal) => close(null, status, signal));
2973
+ });
2974
+ return Object.assign(result_promise, { destructor: () => {
2975
+ clearTimeout(timer);
2976
+ sub.kill('SIGKILL');
2977
+ } });
2978
+ }
2979
+ static error_message(res) {
2980
+ return res?.stderr.toString() || res?.stdout.toString() || 'Run error';
2981
+ }
2936
2982
  }
2937
2983
  $.$mol_run = $mol_run;
2938
2984
  })($ || ($ = {}));
@@ -2944,23 +2990,49 @@ var $;
2944
2990
  $mol_test({
2945
2991
  async 'exec timeout auto kill child process'($) {
2946
2992
  let close_mock = () => { };
2947
- const context_mock = $.$mol_ambient({
2948
- $mol_run_spawn: () => ({
2993
+ const error_message = 'Run error, timeout';
2994
+ function mol_run_spawn_sync_mock() {
2995
+ return {
2996
+ output: [],
2997
+ stdout: error_message,
2998
+ stderr: '',
2999
+ status: 0,
3000
+ signal: null,
3001
+ pid: 123,
3002
+ };
3003
+ }
3004
+ function mol_run_spawn_mock() {
3005
+ return {
2949
3006
  on(name, cb) {
2950
3007
  if (name === 'exit')
2951
3008
  close_mock = cb;
2952
3009
  },
2953
3010
  kill() { close_mock(); }
2954
- })
3011
+ };
3012
+ }
3013
+ const context_mock = $.$mol_ambient({
3014
+ $mol_run_spawn_sync: mol_run_spawn_sync_mock,
3015
+ $mol_run_spawn: mol_run_spawn_mock
2955
3016
  });
3017
+ class $mol_run_mock extends $mol_run {
3018
+ static get $() { return context_mock; }
3019
+ static async_enabled() {
3020
+ return true;
3021
+ }
3022
+ }
2956
3023
  let message = '';
2957
3024
  try {
2958
- const res = await $mol_wire_async(context_mock).$mol_run({ command: 'sleep 10', dir: '.', timeout: 10, env: { 'MOL_RUN_ASYNC': '1' } });
3025
+ const res = await $mol_wire_async($mol_run_mock).spawn({
3026
+ command: 'sleep 10',
3027
+ dir: '.',
3028
+ timeout: 10,
3029
+ env: { 'MOL_RUN_ASYNC': '1' }
3030
+ });
2959
3031
  }
2960
3032
  catch (e) {
2961
3033
  message = e.message;
2962
3034
  }
2963
- $mol_assert_equal(message, 'Run error, timeout');
3035
+ $mol_assert_equal(message, error_message);
2964
3036
  }
2965
3037
  });
2966
3038
  })($ || ($ = {}));
@@ -2970,7 +3042,7 @@ var $;
2970
3042
  var $;
2971
3043
  (function ($) {
2972
3044
  function $mol_exec(dir, command, ...args) {
2973
- return this.$mol_run({ command: [command, ...args], dir });
3045
+ return this.$mol_run.spawn({ command: [command, ...args], dir });
2974
3046
  }
2975
3047
  $.$mol_exec = $mol_exec;
2976
3048
  })($ || ($ = {}));
@@ -3814,15 +3886,6 @@ var $;
3814
3886
  });
3815
3887
  })($ || ($ = {}));
3816
3888
 
3817
- ;
3818
- "use strict";
3819
-
3820
- ;
3821
- "use strict";
3822
-
3823
- ;
3824
- "use strict";
3825
-
3826
3889
  ;
3827
3890
  "use strict";
3828
3891
  var $;
@@ -5136,25 +5199,6 @@ var $;
5136
5199
  });
5137
5200
  })($ || ($ = {}));
5138
5201
 
5139
- ;
5140
- "use strict";
5141
- var $;
5142
- (function ($) {
5143
- $mol_test({
5144
- 'init with overload'() {
5145
- class X extends $mol_object {
5146
- foo() {
5147
- return 1;
5148
- }
5149
- }
5150
- var x = X.make({
5151
- foo: () => 2,
5152
- });
5153
- $mol_assert_equal(x.foo(), 2);
5154
- },
5155
- });
5156
- })($ || ($ = {}));
5157
-
5158
5202
  ;
5159
5203
  "use strict";
5160
5204