mol_data_all 1.1.1302 → 1.1.1304

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
@@ -855,108 +855,166 @@ var $;
855
855
  "use strict";
856
856
  var $;
857
857
  (function ($) {
858
- function $mol_log3_area_lazy(event) {
859
- const self = this;
860
- const stack = self.$mol_log3_stack;
861
- const deep = stack.length;
862
- let logged = false;
863
- stack.push(() => {
864
- logged = true;
865
- self.$mol_log3_area.call(self, event);
866
- });
867
- return () => {
868
- if (logged)
869
- self.console.groupEnd();
870
- if (stack.length > deep)
871
- stack.length = deep;
872
- };
858
+ $.$mol_ambient_ref = Symbol('$mol_ambient_ref');
859
+ function $mol_ambient(overrides) {
860
+ return Object.setPrototypeOf(overrides, this || $);
873
861
  }
874
- $.$mol_log3_area_lazy = $mol_log3_area_lazy;
875
- $.$mol_log3_stack = [];
862
+ $.$mol_ambient = $mol_ambient;
876
863
  })($ || ($ = {}));
877
864
 
878
865
  ;
879
866
  "use strict";
880
867
  var $;
881
868
  (function ($) {
882
- class $mol_term_color {
883
- static reset = this.ansi(0, 0);
884
- static bold = this.ansi(1, 22);
885
- static italic = this.ansi(3, 23);
886
- static underline = this.ansi(4, 24);
887
- static inverse = this.ansi(7, 27);
888
- static hidden = this.ansi(8, 28);
889
- static strike = this.ansi(9, 29);
890
- static gray = this.ansi(90, 39);
891
- static red = this.ansi(91, 39);
892
- static green = this.ansi(92, 39);
893
- static yellow = this.ansi(93, 39);
894
- static blue = this.ansi(94, 39);
895
- static magenta = this.ansi(95, 39);
896
- static cyan = this.ansi(96, 39);
897
- static Gray = (str) => this.inverse(this.gray(str));
898
- static Red = (str) => this.inverse(this.red(str));
899
- static Green = (str) => this.inverse(this.green(str));
900
- static Yellow = (str) => this.inverse(this.yellow(str));
901
- static Blue = (str) => this.inverse(this.blue(str));
902
- static Magenta = (str) => this.inverse(this.magenta(str));
903
- static Cyan = (str) => this.inverse(this.cyan(str));
904
- static ansi(open, close) {
905
- if (typeof process === 'undefined')
906
- return String;
907
- if (!process.stdout.isTTY)
908
- return String;
909
- const prefix = `\x1b[${open}m`;
910
- const postfix = `\x1b[${close}m`;
911
- const suffix_regexp = new RegExp(postfix.replace('[', '\\['), 'g');
912
- return function colorer(str) {
913
- str = String(str);
914
- if (str === '')
915
- return str;
916
- const suffix = str.replace(suffix_regexp, prefix);
917
- return prefix + suffix + postfix;
918
- };
869
+ $.$mol_owning_map = new WeakMap();
870
+ function $mol_owning_allow(having) {
871
+ try {
872
+ if (!having)
873
+ return false;
874
+ if (typeof having !== 'object' && typeof having !== 'function')
875
+ return false;
876
+ if (having instanceof $mol_delegate)
877
+ return false;
878
+ if (typeof having['destructor'] !== 'function')
879
+ return false;
880
+ return true;
881
+ }
882
+ catch {
883
+ return false;
919
884
  }
920
885
  }
921
- $.$mol_term_color = $mol_term_color;
886
+ $.$mol_owning_allow = $mol_owning_allow;
887
+ function $mol_owning_get(having, Owner) {
888
+ if (!$mol_owning_allow(having))
889
+ return null;
890
+ while (true) {
891
+ const owner = $.$mol_owning_map.get(having);
892
+ if (!owner)
893
+ return owner;
894
+ if (!Owner)
895
+ return owner;
896
+ if (owner instanceof Owner)
897
+ return owner;
898
+ having = owner;
899
+ }
900
+ }
901
+ $.$mol_owning_get = $mol_owning_get;
902
+ function $mol_owning_check(owner, having) {
903
+ if (!$mol_owning_allow(having))
904
+ return false;
905
+ if ($.$mol_owning_map.get(having) !== owner)
906
+ return false;
907
+ return true;
908
+ }
909
+ $.$mol_owning_check = $mol_owning_check;
910
+ function $mol_owning_catch(owner, having) {
911
+ if (!$mol_owning_allow(having))
912
+ return false;
913
+ if ($.$mol_owning_map.get(having))
914
+ return false;
915
+ $.$mol_owning_map.set(having, owner);
916
+ return true;
917
+ }
918
+ $.$mol_owning_catch = $mol_owning_catch;
922
919
  })($ || ($ = {}));
923
920
 
921
+ ;
922
+ "use strict";
923
+
924
+ ;
925
+ "use strict";
926
+
927
+ ;
928
+ "use strict";
929
+
930
+ ;
931
+ "use strict";
932
+
933
+ ;
934
+ "use strict";
935
+
936
+ ;
937
+ "use strict";
938
+
924
939
  ;
925
940
  "use strict";
926
941
  var $;
927
942
  (function ($) {
928
- function $mol_log3_node_make(level, output, type, color) {
929
- return function $mol_log3_logger(event) {
930
- if (!event.time)
931
- event = { time: new Date().toISOString(), ...event };
932
- let tree = this.$mol_tree2_from_json(event);
933
- tree = tree.struct(type, tree.kids);
934
- let str = color(tree.toString());
935
- this.console[level](str);
936
- const self = this;
937
- return () => self.console.groupEnd();
938
- };
943
+ class $mol_object2 {
944
+ static $ = $;
945
+ [Symbol.toStringTag];
946
+ [$mol_ambient_ref] = null;
947
+ get $() {
948
+ if (this[$mol_ambient_ref])
949
+ return this[$mol_ambient_ref];
950
+ const owner = $mol_owning_get(this);
951
+ return this[$mol_ambient_ref] = owner?.$ || $mol_object2.$;
952
+ }
953
+ set $(next) {
954
+ if (this[$mol_ambient_ref])
955
+ $mol_fail_hidden(new Error('Context already defined'));
956
+ this[$mol_ambient_ref] = next;
957
+ }
958
+ static create(init) {
959
+ const obj = new this;
960
+ if (init)
961
+ init(obj);
962
+ return obj;
963
+ }
964
+ static [Symbol.toPrimitive]() {
965
+ return this.toString();
966
+ }
967
+ static toString() {
968
+ return this[Symbol.toStringTag] || this.$.$mol_func_name(this);
969
+ }
970
+ static toJSON() {
971
+ return this.toString();
972
+ }
973
+ destructor() { }
974
+ static destructor() { }
975
+ toString() {
976
+ return this[Symbol.toStringTag] || this.constructor.name + '<>';
977
+ }
939
978
  }
940
- $.$mol_log3_node_make = $mol_log3_node_make;
941
- $.$mol_log3_come = $mol_log3_node_make('info', 'stdout', 'come', $mol_term_color.blue);
942
- $.$mol_log3_done = $mol_log3_node_make('info', 'stdout', 'done', $mol_term_color.green);
943
- $.$mol_log3_fail = $mol_log3_node_make('error', 'stderr', 'fail', $mol_term_color.red);
944
- $.$mol_log3_warn = $mol_log3_node_make('warn', 'stderr', 'warn', $mol_term_color.yellow);
945
- $.$mol_log3_rise = $mol_log3_node_make('log', 'stdout', 'rise', $mol_term_color.magenta);
946
- $.$mol_log3_area = $mol_log3_node_make('log', 'stdout', 'area', $mol_term_color.cyan);
979
+ $.$mol_object2 = $mol_object2;
947
980
  })($ || ($ = {}));
948
981
 
949
982
  ;
950
983
  "use strict";
951
984
  var $;
952
985
  (function ($_1) {
953
- $mol_test_mocks.push($ => {
954
- $.$mol_log3_come = () => { };
955
- $.$mol_log3_done = () => { };
956
- $.$mol_log3_fail = () => { };
957
- $.$mol_log3_warn = () => { };
958
- $.$mol_log3_rise = () => { };
959
- $.$mol_log3_area = () => () => { };
986
+ let $$;
987
+ (function ($$) {
988
+ let $;
989
+ })($$ = $_1.$$ || ($_1.$$ = {}));
990
+ $_1.$mol_object_field = Symbol('$mol_object_field');
991
+ class $mol_object extends $mol_object2 {
992
+ static make(config) {
993
+ return super.create(obj => {
994
+ for (let key in config)
995
+ obj[key] = config[key];
996
+ });
997
+ }
998
+ }
999
+ $_1.$mol_object = $mol_object;
1000
+ })($ || ($ = {}));
1001
+
1002
+ ;
1003
+ "use strict";
1004
+ var $;
1005
+ (function ($) {
1006
+ $mol_test({
1007
+ 'init with overload'() {
1008
+ class X extends $mol_object {
1009
+ foo() {
1010
+ return 1;
1011
+ }
1012
+ }
1013
+ var x = X.make({
1014
+ foo: () => 2,
1015
+ });
1016
+ $mol_assert_equal(x.foo(), 2);
1017
+ },
960
1018
  });
961
1019
  })($ || ($ = {}));
962
1020
 
@@ -1074,15 +1132,6 @@ var $;
1074
1132
  $.$mol_wire_affected = [];
1075
1133
  })($ || ($ = {}));
1076
1134
 
1077
- ;
1078
- "use strict";
1079
-
1080
- ;
1081
- "use strict";
1082
-
1083
- ;
1084
- "use strict";
1085
-
1086
1135
  ;
1087
1136
  "use strict";
1088
1137
  var $;
@@ -1387,125 +1436,6 @@ var $;
1387
1436
  });
1388
1437
  })($ || ($ = {}));
1389
1438
 
1390
- ;
1391
- "use strict";
1392
- var $;
1393
- (function ($) {
1394
- $.$mol_ambient_ref = Symbol('$mol_ambient_ref');
1395
- function $mol_ambient(overrides) {
1396
- return Object.setPrototypeOf(overrides, this || $);
1397
- }
1398
- $.$mol_ambient = $mol_ambient;
1399
- })($ || ($ = {}));
1400
-
1401
- ;
1402
- "use strict";
1403
- var $;
1404
- (function ($) {
1405
- $.$mol_owning_map = new WeakMap();
1406
- function $mol_owning_allow(having) {
1407
- try {
1408
- if (!having)
1409
- return false;
1410
- if (typeof having !== 'object' && typeof having !== 'function')
1411
- return false;
1412
- if (having instanceof $mol_delegate)
1413
- return false;
1414
- if (typeof having['destructor'] !== 'function')
1415
- return false;
1416
- return true;
1417
- }
1418
- catch {
1419
- return false;
1420
- }
1421
- }
1422
- $.$mol_owning_allow = $mol_owning_allow;
1423
- function $mol_owning_get(having, Owner) {
1424
- if (!$mol_owning_allow(having))
1425
- return null;
1426
- while (true) {
1427
- const owner = $.$mol_owning_map.get(having);
1428
- if (!owner)
1429
- return owner;
1430
- if (!Owner)
1431
- return owner;
1432
- if (owner instanceof Owner)
1433
- return owner;
1434
- having = owner;
1435
- }
1436
- }
1437
- $.$mol_owning_get = $mol_owning_get;
1438
- function $mol_owning_check(owner, having) {
1439
- if (!$mol_owning_allow(having))
1440
- return false;
1441
- if ($.$mol_owning_map.get(having) !== owner)
1442
- return false;
1443
- return true;
1444
- }
1445
- $.$mol_owning_check = $mol_owning_check;
1446
- function $mol_owning_catch(owner, having) {
1447
- if (!$mol_owning_allow(having))
1448
- return false;
1449
- if ($.$mol_owning_map.get(having))
1450
- return false;
1451
- $.$mol_owning_map.set(having, owner);
1452
- return true;
1453
- }
1454
- $.$mol_owning_catch = $mol_owning_catch;
1455
- })($ || ($ = {}));
1456
-
1457
- ;
1458
- "use strict";
1459
-
1460
- ;
1461
- "use strict";
1462
-
1463
- ;
1464
- "use strict";
1465
-
1466
- ;
1467
- "use strict";
1468
- var $;
1469
- (function ($) {
1470
- class $mol_object2 {
1471
- static $ = $;
1472
- [Symbol.toStringTag];
1473
- [$mol_ambient_ref] = null;
1474
- get $() {
1475
- if (this[$mol_ambient_ref])
1476
- return this[$mol_ambient_ref];
1477
- const owner = $mol_owning_get(this);
1478
- return this[$mol_ambient_ref] = owner?.$ || $mol_object2.$;
1479
- }
1480
- set $(next) {
1481
- if (this[$mol_ambient_ref])
1482
- $mol_fail_hidden(new Error('Context already defined'));
1483
- this[$mol_ambient_ref] = next;
1484
- }
1485
- static create(init) {
1486
- const obj = new this;
1487
- if (init)
1488
- init(obj);
1489
- return obj;
1490
- }
1491
- static [Symbol.toPrimitive]() {
1492
- return this.toString();
1493
- }
1494
- static toString() {
1495
- return this[Symbol.toStringTag] || this.$.$mol_func_name(this);
1496
- }
1497
- static toJSON() {
1498
- return this.toString();
1499
- }
1500
- destructor() { }
1501
- static destructor() { }
1502
- toString() {
1503
- return this[Symbol.toStringTag] || this.constructor.name + '<>';
1504
- }
1505
- }
1506
- $.$mol_object2 = $mol_object2;
1507
- })($ || ($ = {}));
1508
-
1509
1439
  ;
1510
1440
  "use strict";
1511
1441
  var $;
@@ -1909,6 +1839,115 @@ var $;
1909
1839
  });
1910
1840
  })($ || ($ = {}));
1911
1841
 
1842
+ ;
1843
+ "use strict";
1844
+ var $;
1845
+ (function ($) {
1846
+ function $mol_log3_area_lazy(event) {
1847
+ const self = this;
1848
+ const stack = self.$mol_log3_stack;
1849
+ const deep = stack.length;
1850
+ let logged = false;
1851
+ stack.push(() => {
1852
+ logged = true;
1853
+ self.$mol_log3_area.call(self, event);
1854
+ });
1855
+ return () => {
1856
+ if (logged)
1857
+ self.console.groupEnd();
1858
+ if (stack.length > deep)
1859
+ stack.length = deep;
1860
+ };
1861
+ }
1862
+ $.$mol_log3_area_lazy = $mol_log3_area_lazy;
1863
+ $.$mol_log3_stack = [];
1864
+ })($ || ($ = {}));
1865
+
1866
+ ;
1867
+ "use strict";
1868
+ var $;
1869
+ (function ($) {
1870
+ class $mol_term_color {
1871
+ static reset = this.ansi(0, 0);
1872
+ static bold = this.ansi(1, 22);
1873
+ static italic = this.ansi(3, 23);
1874
+ static underline = this.ansi(4, 24);
1875
+ static inverse = this.ansi(7, 27);
1876
+ static hidden = this.ansi(8, 28);
1877
+ static strike = this.ansi(9, 29);
1878
+ static gray = this.ansi(90, 39);
1879
+ static red = this.ansi(91, 39);
1880
+ static green = this.ansi(92, 39);
1881
+ static yellow = this.ansi(93, 39);
1882
+ static blue = this.ansi(94, 39);
1883
+ static magenta = this.ansi(95, 39);
1884
+ static cyan = this.ansi(96, 39);
1885
+ static Gray = (str) => this.inverse(this.gray(str));
1886
+ static Red = (str) => this.inverse(this.red(str));
1887
+ static Green = (str) => this.inverse(this.green(str));
1888
+ static Yellow = (str) => this.inverse(this.yellow(str));
1889
+ static Blue = (str) => this.inverse(this.blue(str));
1890
+ static Magenta = (str) => this.inverse(this.magenta(str));
1891
+ static Cyan = (str) => this.inverse(this.cyan(str));
1892
+ static ansi(open, close) {
1893
+ if (typeof process === 'undefined')
1894
+ return String;
1895
+ if (!process.stdout.isTTY)
1896
+ return String;
1897
+ const prefix = `\x1b[${open}m`;
1898
+ const postfix = `\x1b[${close}m`;
1899
+ const suffix_regexp = new RegExp(postfix.replace('[', '\\['), 'g');
1900
+ return function colorer(str) {
1901
+ str = String(str);
1902
+ if (str === '')
1903
+ return str;
1904
+ const suffix = str.replace(suffix_regexp, prefix);
1905
+ return prefix + suffix + postfix;
1906
+ };
1907
+ }
1908
+ }
1909
+ $.$mol_term_color = $mol_term_color;
1910
+ })($ || ($ = {}));
1911
+
1912
+ ;
1913
+ "use strict";
1914
+ var $;
1915
+ (function ($) {
1916
+ function $mol_log3_node_make(level, output, type, color) {
1917
+ return function $mol_log3_logger(event) {
1918
+ if (!event.time)
1919
+ event = { time: new Date().toISOString(), ...event };
1920
+ let tree = this.$mol_tree2_from_json(event);
1921
+ tree = tree.struct(type, tree.kids);
1922
+ let str = color(tree.toString());
1923
+ this.console[level](str);
1924
+ const self = this;
1925
+ return () => self.console.groupEnd();
1926
+ };
1927
+ }
1928
+ $.$mol_log3_node_make = $mol_log3_node_make;
1929
+ $.$mol_log3_come = $mol_log3_node_make('info', 'stdout', 'come', $mol_term_color.blue);
1930
+ $.$mol_log3_done = $mol_log3_node_make('info', 'stdout', 'done', $mol_term_color.green);
1931
+ $.$mol_log3_fail = $mol_log3_node_make('error', 'stderr', 'fail', $mol_term_color.red);
1932
+ $.$mol_log3_warn = $mol_log3_node_make('warn', 'stderr', 'warn', $mol_term_color.yellow);
1933
+ $.$mol_log3_rise = $mol_log3_node_make('log', 'stdout', 'rise', $mol_term_color.magenta);
1934
+ $.$mol_log3_area = $mol_log3_node_make('log', 'stdout', 'area', $mol_term_color.cyan);
1935
+ })($ || ($ = {}));
1936
+
1937
+ ;
1938
+ "use strict";
1939
+ var $;
1940
+ (function ($_1) {
1941
+ $mol_test_mocks.push($ => {
1942
+ $.$mol_log3_come = () => { };
1943
+ $.$mol_log3_done = () => { };
1944
+ $.$mol_log3_fail = () => { };
1945
+ $.$mol_log3_warn = () => { };
1946
+ $.$mol_log3_rise = () => { };
1947
+ $.$mol_log3_area = () => () => { };
1948
+ });
1949
+ })($ || ($ = {}));
1950
+
1912
1951
  ;
1913
1952
  "use strict";
1914
1953
  var $;
@@ -2329,97 +2368,115 @@ var $;
2329
2368
  class $mol_run_error extends $mol_error_mix {
2330
2369
  }
2331
2370
  $.$mol_run_error = $mol_run_error;
2332
- const child_process = $node['child_process'];
2333
- $.$mol_run_spawn = child_process.spawn.bind(child_process);
2334
- $.$mol_run_spawn_sync = child_process.spawnSync.bind(child_process);
2335
- function $mol_run_async({ dir, timeout, command, env }) {
2336
- const args_raw = typeof command === 'string' ? command.split(' ') : command;
2337
- const [app, ...args] = args_raw;
2338
- if (!env?.MOL_RUN_ASYNC) {
2339
- this.$mol_log3_come({
2340
- place: '$mol_run_sync',
2371
+ $.$mol_run_spawn = (...args) => $node['child_process'].spawn(...args);
2372
+ $.$mol_run_spawn_sync = (...args) => $node['child_process'].spawnSync(...args);
2373
+ class $mol_run extends $mol_object {
2374
+ static async_enabled() {
2375
+ return Boolean(this.$.$mol_env()['MOL_RUN_ASYNC']);
2376
+ }
2377
+ static spawn(options) {
2378
+ const sync = !this.async_enabled() || !Boolean($mol_wire_auto());
2379
+ const env = options.env ?? this.$.$mol_env();
2380
+ return $mol_wire_sync(this).spawn_async({ ...options, sync, env });
2381
+ }
2382
+ static spawn_async({ dir, sync, timeout, command, env }) {
2383
+ const args_raw = typeof command === 'string' ? command.split(' ') : command;
2384
+ const [app, ...args] = args_raw;
2385
+ const opts = { shell: true, cwd: dir, env };
2386
+ const log_object = {
2387
+ place: `${this}.spawn()`,
2341
2388
  message: 'Run',
2342
2389
  command: args_raw.join(' '),
2343
2390
  dir: $node.path.relative('', dir),
2391
+ };
2392
+ if (sync) {
2393
+ this.$.$mol_log3_come({
2394
+ hint: 'Run inside fiber',
2395
+ ...log_object
2396
+ });
2397
+ let error;
2398
+ let res;
2399
+ try {
2400
+ res = this.$.$mol_run_spawn_sync(app, args, opts);
2401
+ error = res.error;
2402
+ }
2403
+ catch (err) {
2404
+ error = err;
2405
+ }
2406
+ if (!res || error || res.status) {
2407
+ throw new $mol_run_error(this.error_message(res), { ...log_object, status: res?.status, signal: res?.signal }, ...(error ? [error] : []));
2408
+ }
2409
+ return res;
2410
+ }
2411
+ let sub;
2412
+ try {
2413
+ sub = this.$.$mol_run_spawn(app, args, {
2414
+ ...opts,
2415
+ stdio: ['pipe', 'inherit', 'inherit'],
2416
+ });
2417
+ }
2418
+ catch (error) {
2419
+ throw new $mol_run_error(this.error_message(undefined), log_object, error);
2420
+ }
2421
+ const pid = sub.pid ?? 0;
2422
+ this.$.$mol_log3_come({
2423
+ ...log_object,
2424
+ pid,
2344
2425
  });
2345
- const res = this.$mol_run_spawn_sync(app, args, { shell: true, cwd: dir, env });
2346
- if (res.status)
2347
- $mol_fail(new Error(res.stderr.toString() || 'Exit(' + res.status + ')'));
2348
- return res;
2349
- }
2350
- const sub = this.$mol_run_spawn(app, args, {
2351
- shell: true,
2352
- cwd: dir,
2353
- env
2354
- });
2355
- this.$mol_log3_come({
2356
- place: '$mol_run_async',
2357
- pid: sub.pid,
2358
- message: 'Run',
2359
- command: args_raw.join(' '),
2360
- dir: $node.path.relative('', dir),
2361
- });
2362
- let killed = false;
2363
- let timer;
2364
- const std_data = [];
2365
- const error_data = [];
2366
- const add = (std_chunk, error_chunk) => {
2367
- if (std_chunk)
2368
- std_data.push(std_chunk);
2369
- if (error_chunk)
2370
- error_data.push(error_chunk);
2371
- if (!timeout)
2372
- return;
2373
- clearTimeout(timer);
2374
- timer = setTimeout(() => {
2375
- const signal = killed ? 'SIGKILL' : 'SIGTERM';
2376
- killed = true;
2377
- add();
2378
- sub.kill(signal);
2379
- }, timeout);
2380
- };
2381
- add();
2382
- sub.stdout?.on('data', data => add(data));
2383
- sub.stderr?.on('data', data => add(undefined, data));
2384
- const promise = new Promise((done, fail) => {
2385
- const close = (error, status = null, signal = null) => {
2386
- if (!timer && timeout)
2426
+ let timeout_kill = false;
2427
+ let timer;
2428
+ const std_data = [];
2429
+ const error_data = [];
2430
+ const add = (std_chunk, error_chunk) => {
2431
+ if (std_chunk)
2432
+ std_data.push(std_chunk);
2433
+ if (error_chunk)
2434
+ error_data.push(error_chunk);
2435
+ if (!timeout)
2387
2436
  return;
2388
2437
  clearTimeout(timer);
2389
- timer = undefined;
2390
- const res = {
2391
- pid: sub.pid,
2392
- status,
2393
- signal,
2394
- get stdout() { return Buffer.concat(std_data); },
2395
- get stderr() { return Buffer.concat(error_data); }
2396
- };
2397
- this.$mol_log3_done({
2398
- place: '$mol_run_async',
2399
- pid: sub.pid,
2400
- message: 'Run',
2401
- status,
2402
- command: args_raw.join(' '),
2403
- dir: $node.path.relative('', dir),
2404
- });
2405
- if (error || status || killed)
2406
- return fail(new $mol_run_error((res.stderr.toString() || res.stdout.toString() || 'Run error') + (killed ? ', timeout' : ''), { signal, timeout: killed }, ...error ? [error] : []));
2407
- done(res);
2438
+ timer = setTimeout(() => {
2439
+ const signal = timeout_kill ? 'SIGKILL' : 'SIGTERM';
2440
+ timeout_kill = true;
2441
+ add();
2442
+ sub.kill(signal);
2443
+ }, timeout);
2408
2444
  };
2409
- sub.on('disconnect', () => close(new Error('Disconnected')));
2410
- sub.on('error', err => close(err));
2411
- sub.on('exit', (status, signal) => close(null, status, signal));
2412
- });
2413
- return Object.assign(promise, { destructor: () => {
2414
- clearTimeout(timer);
2415
- sub.kill('SIGKILL');
2416
- } });
2417
- }
2418
- $.$mol_run_async = $mol_run_async;
2419
- function $mol_run(options) {
2420
- if (!options.env)
2421
- options = { ...options, env: this.$mol_env() };
2422
- return $mol_wire_sync(this).$mol_run_async(options);
2445
+ add();
2446
+ sub.stdout?.on('data', data => add(data));
2447
+ sub.stderr?.on('data', data => add(undefined, data));
2448
+ const result_promise = new Promise((done, fail) => {
2449
+ const close = (error, status = null, signal = null) => {
2450
+ if (!timer && timeout)
2451
+ return;
2452
+ clearTimeout(timer);
2453
+ timer = undefined;
2454
+ const res = {
2455
+ pid,
2456
+ signal,
2457
+ get stdout() { return Buffer.concat(std_data); },
2458
+ get stderr() { return Buffer.concat(error_data); }
2459
+ };
2460
+ if (error || status || timeout_kill)
2461
+ return fail(new $mol_run_error(this.error_message(res) + (timeout_kill ? ', timeout' : ''), { ...log_object, pid, status, signal, timeout_kill }, ...error ? [error] : []));
2462
+ this.$.$mol_log3_done({
2463
+ ...log_object,
2464
+ pid,
2465
+ });
2466
+ done(res);
2467
+ };
2468
+ sub.on('disconnect', () => close(new Error('Disconnected')));
2469
+ sub.on('error', err => close(err));
2470
+ sub.on('exit', (status, signal) => close(null, status, signal));
2471
+ });
2472
+ return Object.assign(result_promise, { destructor: () => {
2473
+ clearTimeout(timer);
2474
+ sub.kill('SIGKILL');
2475
+ } });
2476
+ }
2477
+ static error_message(res) {
2478
+ return res?.stderr.toString() || res?.stdout.toString() || 'Run error';
2479
+ }
2423
2480
  }
2424
2481
  $.$mol_run = $mol_run;
2425
2482
  })($ || ($ = {}));
@@ -2431,23 +2488,49 @@ var $;
2431
2488
  $mol_test({
2432
2489
  async 'exec timeout auto kill child process'($) {
2433
2490
  let close_mock = () => { };
2434
- const context_mock = $.$mol_ambient({
2435
- $mol_run_spawn: () => ({
2491
+ const error_message = 'Run error, timeout';
2492
+ function mol_run_spawn_sync_mock() {
2493
+ return {
2494
+ output: [],
2495
+ stdout: error_message,
2496
+ stderr: '',
2497
+ status: 0,
2498
+ signal: null,
2499
+ pid: 123,
2500
+ };
2501
+ }
2502
+ function mol_run_spawn_mock() {
2503
+ return {
2436
2504
  on(name, cb) {
2437
2505
  if (name === 'exit')
2438
2506
  close_mock = cb;
2439
2507
  },
2440
2508
  kill() { close_mock(); }
2441
- })
2509
+ };
2510
+ }
2511
+ const context_mock = $.$mol_ambient({
2512
+ $mol_run_spawn_sync: mol_run_spawn_sync_mock,
2513
+ $mol_run_spawn: mol_run_spawn_mock
2442
2514
  });
2515
+ class $mol_run_mock extends $mol_run {
2516
+ static get $() { return context_mock; }
2517
+ static async_enabled() {
2518
+ return true;
2519
+ }
2520
+ }
2443
2521
  let message = '';
2444
2522
  try {
2445
- const res = await $mol_wire_async(context_mock).$mol_run({ command: 'sleep 10', dir: '.', timeout: 10, env: { 'MOL_RUN_ASYNC': '1' } });
2523
+ const res = await $mol_wire_async($mol_run_mock).spawn({
2524
+ command: 'sleep 10',
2525
+ dir: '.',
2526
+ timeout: 10,
2527
+ env: { 'MOL_RUN_ASYNC': '1' }
2528
+ });
2446
2529
  }
2447
2530
  catch (e) {
2448
2531
  message = e.message;
2449
2532
  }
2450
- $mol_assert_equal(message, 'Run error, timeout');
2533
+ $mol_assert_equal(message, error_message);
2451
2534
  }
2452
2535
  });
2453
2536
  })($ || ($ = {}));
@@ -2457,7 +2540,7 @@ var $;
2457
2540
  var $;
2458
2541
  (function ($) {
2459
2542
  function $mol_exec(dir, command, ...args) {
2460
- return this.$mol_run({ command: [command, ...args], dir });
2543
+ return this.$mol_run.spawn({ command: [command, ...args], dir });
2461
2544
  }
2462
2545
  $.$mol_exec = $mol_exec;
2463
2546
  })($ || ($ = {}));