mol_text_distance 0.0.1004 → 0.0.1005

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
@@ -414,108 +414,169 @@ var $;
414
414
  "use strict";
415
415
  var $;
416
416
  (function ($) {
417
- function $mol_log3_area_lazy(event) {
418
- const self = this;
419
- const stack = self.$mol_log3_stack;
420
- const deep = stack.length;
421
- let logged = false;
422
- stack.push(() => {
423
- logged = true;
424
- self.$mol_log3_area.call(self, event);
425
- });
426
- return () => {
427
- if (logged)
428
- self.console.groupEnd();
429
- if (stack.length > deep)
430
- stack.length = deep;
431
- };
417
+ $.$mol_ambient_ref = Symbol('$mol_ambient_ref');
418
+ function $mol_ambient(overrides) {
419
+ return Object.setPrototypeOf(overrides, this || $);
432
420
  }
433
- $.$mol_log3_area_lazy = $mol_log3_area_lazy;
434
- $.$mol_log3_stack = [];
421
+ $.$mol_ambient = $mol_ambient;
435
422
  })($ || ($ = {}));
436
423
 
437
424
  ;
438
425
  "use strict";
439
426
  var $;
440
427
  (function ($) {
441
- class $mol_term_color {
442
- static reset = this.ansi(0, 0);
443
- static bold = this.ansi(1, 22);
444
- static italic = this.ansi(3, 23);
445
- static underline = this.ansi(4, 24);
446
- static inverse = this.ansi(7, 27);
447
- static hidden = this.ansi(8, 28);
448
- static strike = this.ansi(9, 29);
449
- static gray = this.ansi(90, 39);
450
- static red = this.ansi(91, 39);
451
- static green = this.ansi(92, 39);
452
- static yellow = this.ansi(93, 39);
453
- static blue = this.ansi(94, 39);
454
- static magenta = this.ansi(95, 39);
455
- static cyan = this.ansi(96, 39);
456
- static Gray = (str) => this.inverse(this.gray(str));
457
- static Red = (str) => this.inverse(this.red(str));
458
- static Green = (str) => this.inverse(this.green(str));
459
- static Yellow = (str) => this.inverse(this.yellow(str));
460
- static Blue = (str) => this.inverse(this.blue(str));
461
- static Magenta = (str) => this.inverse(this.magenta(str));
462
- static Cyan = (str) => this.inverse(this.cyan(str));
463
- static ansi(open, close) {
464
- if (typeof process === 'undefined')
465
- return String;
466
- if (!process.stdout.isTTY)
467
- return String;
468
- const prefix = `\x1b[${open}m`;
469
- const postfix = `\x1b[${close}m`;
470
- const suffix_regexp = new RegExp(postfix.replace('[', '\\['), 'g');
471
- return function colorer(str) {
472
- str = String(str);
473
- if (str === '')
474
- return str;
475
- const suffix = str.replace(suffix_regexp, prefix);
476
- return prefix + suffix + postfix;
477
- };
428
+ $.$mol_owning_map = new WeakMap();
429
+ function $mol_owning_allow(having) {
430
+ try {
431
+ if (!having)
432
+ return false;
433
+ if (typeof having !== 'object' && typeof having !== 'function')
434
+ return false;
435
+ if (having instanceof $mol_delegate)
436
+ return false;
437
+ if (typeof having['destructor'] !== 'function')
438
+ return false;
439
+ return true;
440
+ }
441
+ catch {
442
+ return false;
478
443
  }
479
444
  }
480
- $.$mol_term_color = $mol_term_color;
445
+ $.$mol_owning_allow = $mol_owning_allow;
446
+ function $mol_owning_get(having, Owner) {
447
+ if (!$mol_owning_allow(having))
448
+ return null;
449
+ while (true) {
450
+ const owner = $.$mol_owning_map.get(having);
451
+ if (!owner)
452
+ return owner;
453
+ if (!Owner)
454
+ return owner;
455
+ if (owner instanceof Owner)
456
+ return owner;
457
+ having = owner;
458
+ }
459
+ }
460
+ $.$mol_owning_get = $mol_owning_get;
461
+ function $mol_owning_check(owner, having) {
462
+ if (!$mol_owning_allow(having))
463
+ return false;
464
+ if ($.$mol_owning_map.get(having) !== owner)
465
+ return false;
466
+ return true;
467
+ }
468
+ $.$mol_owning_check = $mol_owning_check;
469
+ function $mol_owning_catch(owner, having) {
470
+ if (!$mol_owning_allow(having))
471
+ return false;
472
+ if ($.$mol_owning_map.get(having))
473
+ return false;
474
+ $.$mol_owning_map.set(having, owner);
475
+ return true;
476
+ }
477
+ $.$mol_owning_catch = $mol_owning_catch;
481
478
  })($ || ($ = {}));
482
479
 
480
+ ;
481
+ "use strict";
482
+
483
+ ;
484
+ "use strict";
485
+
486
+ ;
487
+ "use strict";
488
+
489
+ ;
490
+ "use strict";
491
+
492
+ ;
493
+ "use strict";
494
+
495
+ ;
496
+ "use strict";
497
+
498
+ ;
499
+ "use strict";
500
+
483
501
  ;
484
502
  "use strict";
485
503
  var $;
486
504
  (function ($) {
487
- function $mol_log3_node_make(level, output, type, color) {
488
- return function $mol_log3_logger(event) {
489
- if (!event.time)
490
- event = { time: new Date().toISOString(), ...event };
491
- let tree = this.$mol_tree2_from_json(event);
492
- tree = tree.struct(type, tree.kids);
493
- let str = color(tree.toString());
494
- this.console[level](str);
495
- const self = this;
496
- return () => self.console.groupEnd();
497
- };
505
+ class $mol_object2 {
506
+ static $ = $;
507
+ [Symbol.toStringTag];
508
+ [$mol_ambient_ref] = null;
509
+ get $() {
510
+ if (this[$mol_ambient_ref])
511
+ return this[$mol_ambient_ref];
512
+ const owner = $mol_owning_get(this);
513
+ return this[$mol_ambient_ref] = owner?.$ || $mol_object2.$;
514
+ }
515
+ set $(next) {
516
+ if (this[$mol_ambient_ref])
517
+ $mol_fail_hidden(new Error('Context already defined'));
518
+ this[$mol_ambient_ref] = next;
519
+ }
520
+ static create(init) {
521
+ const obj = new this;
522
+ if (init)
523
+ init(obj);
524
+ return obj;
525
+ }
526
+ static [Symbol.toPrimitive]() {
527
+ return this.toString();
528
+ }
529
+ static toString() {
530
+ return this[Symbol.toStringTag] || this.$.$mol_func_name(this);
531
+ }
532
+ static toJSON() {
533
+ return this.toString();
534
+ }
535
+ destructor() { }
536
+ static destructor() { }
537
+ toString() {
538
+ return this[Symbol.toStringTag] || this.constructor.name + '<>';
539
+ }
498
540
  }
499
- $.$mol_log3_node_make = $mol_log3_node_make;
500
- $.$mol_log3_come = $mol_log3_node_make('info', 'stdout', 'come', $mol_term_color.blue);
501
- $.$mol_log3_done = $mol_log3_node_make('info', 'stdout', 'done', $mol_term_color.green);
502
- $.$mol_log3_fail = $mol_log3_node_make('error', 'stderr', 'fail', $mol_term_color.red);
503
- $.$mol_log3_warn = $mol_log3_node_make('warn', 'stderr', 'warn', $mol_term_color.yellow);
504
- $.$mol_log3_rise = $mol_log3_node_make('log', 'stdout', 'rise', $mol_term_color.magenta);
505
- $.$mol_log3_area = $mol_log3_node_make('log', 'stdout', 'area', $mol_term_color.cyan);
541
+ $.$mol_object2 = $mol_object2;
506
542
  })($ || ($ = {}));
507
543
 
508
544
  ;
509
545
  "use strict";
510
546
  var $;
511
547
  (function ($_1) {
512
- $mol_test_mocks.push($ => {
513
- $.$mol_log3_come = () => { };
514
- $.$mol_log3_done = () => { };
515
- $.$mol_log3_fail = () => { };
516
- $.$mol_log3_warn = () => { };
517
- $.$mol_log3_rise = () => { };
518
- $.$mol_log3_area = () => () => { };
548
+ let $$;
549
+ (function ($$) {
550
+ let $;
551
+ })($$ = $_1.$$ || ($_1.$$ = {}));
552
+ $_1.$mol_object_field = Symbol('$mol_object_field');
553
+ class $mol_object extends $mol_object2 {
554
+ static make(config) {
555
+ return super.create(obj => {
556
+ for (let key in config)
557
+ obj[key] = config[key];
558
+ });
559
+ }
560
+ }
561
+ $_1.$mol_object = $mol_object;
562
+ })($ || ($ = {}));
563
+
564
+ ;
565
+ "use strict";
566
+ var $;
567
+ (function ($) {
568
+ $mol_test({
569
+ 'init with overload'() {
570
+ class X extends $mol_object {
571
+ foo() {
572
+ return 1;
573
+ }
574
+ }
575
+ var x = X.make({
576
+ foo: () => 2,
577
+ });
578
+ $mol_assert_equal(x.foo(), 2);
579
+ },
519
580
  });
520
581
  })($ || ($ = {}));
521
582
 
@@ -633,15 +694,6 @@ var $;
633
694
  $.$mol_wire_affected = [];
634
695
  })($ || ($ = {}));
635
696
 
636
- ;
637
- "use strict";
638
-
639
- ;
640
- "use strict";
641
-
642
- ;
643
- "use strict";
644
-
645
697
  ;
646
698
  "use strict";
647
699
  var $;
@@ -946,128 +998,6 @@ var $;
946
998
  });
947
999
  })($ || ($ = {}));
948
1000
 
949
- ;
950
- "use strict";
951
- var $;
952
- (function ($) {
953
- $.$mol_ambient_ref = Symbol('$mol_ambient_ref');
954
- function $mol_ambient(overrides) {
955
- return Object.setPrototypeOf(overrides, this || $);
956
- }
957
- $.$mol_ambient = $mol_ambient;
958
- })($ || ($ = {}));
959
-
960
- ;
961
- "use strict";
962
- var $;
963
- (function ($) {
964
- $.$mol_owning_map = new WeakMap();
965
- function $mol_owning_allow(having) {
966
- try {
967
- if (!having)
968
- return false;
969
- if (typeof having !== 'object' && typeof having !== 'function')
970
- return false;
971
- if (having instanceof $mol_delegate)
972
- return false;
973
- if (typeof having['destructor'] !== 'function')
974
- return false;
975
- return true;
976
- }
977
- catch {
978
- return false;
979
- }
980
- }
981
- $.$mol_owning_allow = $mol_owning_allow;
982
- function $mol_owning_get(having, Owner) {
983
- if (!$mol_owning_allow(having))
984
- return null;
985
- while (true) {
986
- const owner = $.$mol_owning_map.get(having);
987
- if (!owner)
988
- return owner;
989
- if (!Owner)
990
- return owner;
991
- if (owner instanceof Owner)
992
- return owner;
993
- having = owner;
994
- }
995
- }
996
- $.$mol_owning_get = $mol_owning_get;
997
- function $mol_owning_check(owner, having) {
998
- if (!$mol_owning_allow(having))
999
- return false;
1000
- if ($.$mol_owning_map.get(having) !== owner)
1001
- return false;
1002
- return true;
1003
- }
1004
- $.$mol_owning_check = $mol_owning_check;
1005
- function $mol_owning_catch(owner, having) {
1006
- if (!$mol_owning_allow(having))
1007
- return false;
1008
- if ($.$mol_owning_map.get(having))
1009
- return false;
1010
- $.$mol_owning_map.set(having, owner);
1011
- return true;
1012
- }
1013
- $.$mol_owning_catch = $mol_owning_catch;
1014
- })($ || ($ = {}));
1015
-
1016
- ;
1017
- "use strict";
1018
-
1019
- ;
1020
- "use strict";
1021
-
1022
- ;
1023
- "use strict";
1024
-
1025
- ;
1026
- "use strict";
1027
-
1028
- ;
1029
- "use strict";
1030
- var $;
1031
- (function ($) {
1032
- class $mol_object2 {
1033
- static $ = $;
1034
- [Symbol.toStringTag];
1035
- [$mol_ambient_ref] = null;
1036
- get $() {
1037
- if (this[$mol_ambient_ref])
1038
- return this[$mol_ambient_ref];
1039
- const owner = $mol_owning_get(this);
1040
- return this[$mol_ambient_ref] = owner?.$ || $mol_object2.$;
1041
- }
1042
- set $(next) {
1043
- if (this[$mol_ambient_ref])
1044
- $mol_fail_hidden(new Error('Context already defined'));
1045
- this[$mol_ambient_ref] = next;
1046
- }
1047
- static create(init) {
1048
- const obj = new this;
1049
- if (init)
1050
- init(obj);
1051
- return obj;
1052
- }
1053
- static [Symbol.toPrimitive]() {
1054
- return this.toString();
1055
- }
1056
- static toString() {
1057
- return this[Symbol.toStringTag] || this.$.$mol_func_name(this);
1058
- }
1059
- static toJSON() {
1060
- return this.toString();
1061
- }
1062
- destructor() { }
1063
- static destructor() { }
1064
- toString() {
1065
- return this[Symbol.toStringTag] || this.constructor.name + '<>';
1066
- }
1067
- }
1068
- $.$mol_object2 = $mol_object2;
1069
- })($ || ($ = {}));
1070
-
1071
1001
  ;
1072
1002
  "use strict";
1073
1003
  var $;
@@ -1471,6 +1401,115 @@ var $;
1471
1401
  });
1472
1402
  })($ || ($ = {}));
1473
1403
 
1404
+ ;
1405
+ "use strict";
1406
+ var $;
1407
+ (function ($) {
1408
+ function $mol_log3_area_lazy(event) {
1409
+ const self = this;
1410
+ const stack = self.$mol_log3_stack;
1411
+ const deep = stack.length;
1412
+ let logged = false;
1413
+ stack.push(() => {
1414
+ logged = true;
1415
+ self.$mol_log3_area.call(self, event);
1416
+ });
1417
+ return () => {
1418
+ if (logged)
1419
+ self.console.groupEnd();
1420
+ if (stack.length > deep)
1421
+ stack.length = deep;
1422
+ };
1423
+ }
1424
+ $.$mol_log3_area_lazy = $mol_log3_area_lazy;
1425
+ $.$mol_log3_stack = [];
1426
+ })($ || ($ = {}));
1427
+
1428
+ ;
1429
+ "use strict";
1430
+ var $;
1431
+ (function ($) {
1432
+ class $mol_term_color {
1433
+ static reset = this.ansi(0, 0);
1434
+ static bold = this.ansi(1, 22);
1435
+ static italic = this.ansi(3, 23);
1436
+ static underline = this.ansi(4, 24);
1437
+ static inverse = this.ansi(7, 27);
1438
+ static hidden = this.ansi(8, 28);
1439
+ static strike = this.ansi(9, 29);
1440
+ static gray = this.ansi(90, 39);
1441
+ static red = this.ansi(91, 39);
1442
+ static green = this.ansi(92, 39);
1443
+ static yellow = this.ansi(93, 39);
1444
+ static blue = this.ansi(94, 39);
1445
+ static magenta = this.ansi(95, 39);
1446
+ static cyan = this.ansi(96, 39);
1447
+ static Gray = (str) => this.inverse(this.gray(str));
1448
+ static Red = (str) => this.inverse(this.red(str));
1449
+ static Green = (str) => this.inverse(this.green(str));
1450
+ static Yellow = (str) => this.inverse(this.yellow(str));
1451
+ static Blue = (str) => this.inverse(this.blue(str));
1452
+ static Magenta = (str) => this.inverse(this.magenta(str));
1453
+ static Cyan = (str) => this.inverse(this.cyan(str));
1454
+ static ansi(open, close) {
1455
+ if (typeof process === 'undefined')
1456
+ return String;
1457
+ if (!process.stdout.isTTY)
1458
+ return String;
1459
+ const prefix = `\x1b[${open}m`;
1460
+ const postfix = `\x1b[${close}m`;
1461
+ const suffix_regexp = new RegExp(postfix.replace('[', '\\['), 'g');
1462
+ return function colorer(str) {
1463
+ str = String(str);
1464
+ if (str === '')
1465
+ return str;
1466
+ const suffix = str.replace(suffix_regexp, prefix);
1467
+ return prefix + suffix + postfix;
1468
+ };
1469
+ }
1470
+ }
1471
+ $.$mol_term_color = $mol_term_color;
1472
+ })($ || ($ = {}));
1473
+
1474
+ ;
1475
+ "use strict";
1476
+ var $;
1477
+ (function ($) {
1478
+ function $mol_log3_node_make(level, output, type, color) {
1479
+ return function $mol_log3_logger(event) {
1480
+ if (!event.time)
1481
+ event = { time: new Date().toISOString(), ...event };
1482
+ let tree = this.$mol_tree2_from_json(event);
1483
+ tree = tree.struct(type, tree.kids);
1484
+ let str = color(tree.toString());
1485
+ this.console[level](str);
1486
+ const self = this;
1487
+ return () => self.console.groupEnd();
1488
+ };
1489
+ }
1490
+ $.$mol_log3_node_make = $mol_log3_node_make;
1491
+ $.$mol_log3_come = $mol_log3_node_make('info', 'stdout', 'come', $mol_term_color.blue);
1492
+ $.$mol_log3_done = $mol_log3_node_make('info', 'stdout', 'done', $mol_term_color.green);
1493
+ $.$mol_log3_fail = $mol_log3_node_make('error', 'stderr', 'fail', $mol_term_color.red);
1494
+ $.$mol_log3_warn = $mol_log3_node_make('warn', 'stderr', 'warn', $mol_term_color.yellow);
1495
+ $.$mol_log3_rise = $mol_log3_node_make('log', 'stdout', 'rise', $mol_term_color.magenta);
1496
+ $.$mol_log3_area = $mol_log3_node_make('log', 'stdout', 'area', $mol_term_color.cyan);
1497
+ })($ || ($ = {}));
1498
+
1499
+ ;
1500
+ "use strict";
1501
+ var $;
1502
+ (function ($_1) {
1503
+ $mol_test_mocks.push($ => {
1504
+ $.$mol_log3_come = () => { };
1505
+ $.$mol_log3_done = () => { };
1506
+ $.$mol_log3_fail = () => { };
1507
+ $.$mol_log3_warn = () => { };
1508
+ $.$mol_log3_rise = () => { };
1509
+ $.$mol_log3_area = () => () => { };
1510
+ });
1511
+ })($ || ($ = {}));
1512
+
1474
1513
  ;
1475
1514
  "use strict";
1476
1515
  var $;
@@ -1891,97 +1930,115 @@ var $;
1891
1930
  class $mol_run_error extends $mol_error_mix {
1892
1931
  }
1893
1932
  $.$mol_run_error = $mol_run_error;
1894
- const child_process = $node['child_process'];
1895
- $.$mol_run_spawn = child_process.spawn.bind(child_process);
1896
- $.$mol_run_spawn_sync = child_process.spawnSync.bind(child_process);
1897
- function $mol_run_async({ dir, timeout, command, env }) {
1898
- const args_raw = typeof command === 'string' ? command.split(' ') : command;
1899
- const [app, ...args] = args_raw;
1900
- if (!env?.MOL_RUN_ASYNC) {
1901
- this.$mol_log3_come({
1902
- place: '$mol_run_sync',
1933
+ $.$mol_run_spawn = (...args) => $node['child_process'].spawn(...args);
1934
+ $.$mol_run_spawn_sync = (...args) => $node['child_process'].spawnSync(...args);
1935
+ class $mol_run extends $mol_object {
1936
+ static async_enabled() {
1937
+ return Boolean(this.$.$mol_env()['MOL_RUN_ASYNC']);
1938
+ }
1939
+ static spawn(options) {
1940
+ const sync = !this.async_enabled() || !Boolean($mol_wire_auto());
1941
+ const env = options.env ?? this.$.$mol_env();
1942
+ return $mol_wire_sync(this).spawn_async({ ...options, sync, env });
1943
+ }
1944
+ static spawn_async({ dir, sync, timeout, command, env }) {
1945
+ const args_raw = typeof command === 'string' ? command.split(' ') : command;
1946
+ const [app, ...args] = args_raw;
1947
+ const opts = { shell: true, cwd: dir, env };
1948
+ const log_object = {
1949
+ place: `${this}.spawn()`,
1903
1950
  message: 'Run',
1904
1951
  command: args_raw.join(' '),
1905
1952
  dir: $node.path.relative('', dir),
1953
+ };
1954
+ if (sync) {
1955
+ this.$.$mol_log3_come({
1956
+ hint: 'Run inside fiber',
1957
+ ...log_object
1958
+ });
1959
+ let error;
1960
+ let res;
1961
+ try {
1962
+ res = this.$.$mol_run_spawn_sync(app, args, opts);
1963
+ error = res.error;
1964
+ }
1965
+ catch (err) {
1966
+ error = err;
1967
+ }
1968
+ if (!res || error || res.status) {
1969
+ throw new $mol_run_error(this.error_message(res), { ...log_object, status: res?.status, signal: res?.signal }, ...(error ? [error] : []));
1970
+ }
1971
+ return res;
1972
+ }
1973
+ let sub;
1974
+ try {
1975
+ sub = this.$.$mol_run_spawn(app, args, {
1976
+ ...opts,
1977
+ stdio: ['pipe', 'inherit', 'inherit'],
1978
+ });
1979
+ }
1980
+ catch (error) {
1981
+ throw new $mol_run_error(this.error_message(undefined), log_object, error);
1982
+ }
1983
+ const pid = sub.pid ?? 0;
1984
+ this.$.$mol_log3_come({
1985
+ ...log_object,
1986
+ pid,
1906
1987
  });
1907
- const res = this.$mol_run_spawn_sync(app, args, { shell: true, cwd: dir, env });
1908
- if (res.status)
1909
- $mol_fail(new Error(res.stderr.toString() || 'Exit(' + res.status + ')'));
1910
- return res;
1911
- }
1912
- const sub = this.$mol_run_spawn(app, args, {
1913
- shell: true,
1914
- cwd: dir,
1915
- env
1916
- });
1917
- this.$mol_log3_come({
1918
- place: '$mol_run_async',
1919
- pid: sub.pid,
1920
- message: 'Run',
1921
- command: args_raw.join(' '),
1922
- dir: $node.path.relative('', dir),
1923
- });
1924
- let killed = false;
1925
- let timer;
1926
- const std_data = [];
1927
- const error_data = [];
1928
- const add = (std_chunk, error_chunk) => {
1929
- if (std_chunk)
1930
- std_data.push(std_chunk);
1931
- if (error_chunk)
1932
- error_data.push(error_chunk);
1933
- if (!timeout)
1934
- return;
1935
- clearTimeout(timer);
1936
- timer = setTimeout(() => {
1937
- const signal = killed ? 'SIGKILL' : 'SIGTERM';
1938
- killed = true;
1939
- add();
1940
- sub.kill(signal);
1941
- }, timeout);
1942
- };
1943
- add();
1944
- sub.stdout?.on('data', data => add(data));
1945
- sub.stderr?.on('data', data => add(undefined, data));
1946
- const promise = new Promise((done, fail) => {
1947
- const close = (error, status = null, signal = null) => {
1948
- if (!timer && timeout)
1988
+ let timeout_kill = false;
1989
+ let timer;
1990
+ const std_data = [];
1991
+ const error_data = [];
1992
+ const add = (std_chunk, error_chunk) => {
1993
+ if (std_chunk)
1994
+ std_data.push(std_chunk);
1995
+ if (error_chunk)
1996
+ error_data.push(error_chunk);
1997
+ if (!timeout)
1949
1998
  return;
1950
1999
  clearTimeout(timer);
1951
- timer = undefined;
1952
- const res = {
1953
- pid: sub.pid,
1954
- status,
1955
- signal,
1956
- get stdout() { return Buffer.concat(std_data); },
1957
- get stderr() { return Buffer.concat(error_data); }
1958
- };
1959
- this.$mol_log3_done({
1960
- place: '$mol_run_async',
1961
- pid: sub.pid,
1962
- message: 'Run',
1963
- status,
1964
- command: args_raw.join(' '),
1965
- dir: $node.path.relative('', dir),
1966
- });
1967
- if (error || status || killed)
1968
- return fail(new $mol_run_error((res.stderr.toString() || res.stdout.toString() || 'Run error') + (killed ? ', timeout' : ''), { signal, timeout: killed }, ...error ? [error] : []));
1969
- done(res);
2000
+ timer = setTimeout(() => {
2001
+ const signal = timeout_kill ? 'SIGKILL' : 'SIGTERM';
2002
+ timeout_kill = true;
2003
+ add();
2004
+ sub.kill(signal);
2005
+ }, timeout);
1970
2006
  };
1971
- sub.on('disconnect', () => close(new Error('Disconnected')));
1972
- sub.on('error', err => close(err));
1973
- sub.on('exit', (status, signal) => close(null, status, signal));
1974
- });
1975
- return Object.assign(promise, { destructor: () => {
1976
- clearTimeout(timer);
1977
- sub.kill('SIGKILL');
1978
- } });
1979
- }
1980
- $.$mol_run_async = $mol_run_async;
1981
- function $mol_run(options) {
1982
- if (!options.env)
1983
- options = { ...options, env: this.$mol_env() };
1984
- return $mol_wire_sync(this).$mol_run_async(options);
2007
+ add();
2008
+ sub.stdout?.on('data', data => add(data));
2009
+ sub.stderr?.on('data', data => add(undefined, data));
2010
+ const result_promise = new Promise((done, fail) => {
2011
+ const close = (error, status = null, signal = null) => {
2012
+ if (!timer && timeout)
2013
+ return;
2014
+ clearTimeout(timer);
2015
+ timer = undefined;
2016
+ const res = {
2017
+ pid,
2018
+ signal,
2019
+ get stdout() { return Buffer.concat(std_data); },
2020
+ get stderr() { return Buffer.concat(error_data); }
2021
+ };
2022
+ if (error || status || timeout_kill)
2023
+ return fail(new $mol_run_error(this.error_message(res) + (timeout_kill ? ', timeout' : ''), { ...log_object, pid, status, signal, timeout_kill }, ...error ? [error] : []));
2024
+ this.$.$mol_log3_done({
2025
+ ...log_object,
2026
+ pid,
2027
+ });
2028
+ done(res);
2029
+ };
2030
+ sub.on('disconnect', () => close(new Error('Disconnected')));
2031
+ sub.on('error', err => close(err));
2032
+ sub.on('exit', (status, signal) => close(null, status, signal));
2033
+ });
2034
+ return Object.assign(result_promise, { destructor: () => {
2035
+ clearTimeout(timer);
2036
+ sub.kill('SIGKILL');
2037
+ } });
2038
+ }
2039
+ static error_message(res) {
2040
+ return res?.stderr.toString() || res?.stdout.toString() || 'Run error';
2041
+ }
1985
2042
  }
1986
2043
  $.$mol_run = $mol_run;
1987
2044
  })($ || ($ = {}));
@@ -1993,23 +2050,49 @@ var $;
1993
2050
  $mol_test({
1994
2051
  async 'exec timeout auto kill child process'($) {
1995
2052
  let close_mock = () => { };
1996
- const context_mock = $.$mol_ambient({
1997
- $mol_run_spawn: () => ({
2053
+ const error_message = 'Run error, timeout';
2054
+ function mol_run_spawn_sync_mock() {
2055
+ return {
2056
+ output: [],
2057
+ stdout: error_message,
2058
+ stderr: '',
2059
+ status: 0,
2060
+ signal: null,
2061
+ pid: 123,
2062
+ };
2063
+ }
2064
+ function mol_run_spawn_mock() {
2065
+ return {
1998
2066
  on(name, cb) {
1999
2067
  if (name === 'exit')
2000
2068
  close_mock = cb;
2001
2069
  },
2002
2070
  kill() { close_mock(); }
2003
- })
2071
+ };
2072
+ }
2073
+ const context_mock = $.$mol_ambient({
2074
+ $mol_run_spawn_sync: mol_run_spawn_sync_mock,
2075
+ $mol_run_spawn: mol_run_spawn_mock
2004
2076
  });
2077
+ class $mol_run_mock extends $mol_run {
2078
+ static get $() { return context_mock; }
2079
+ static async_enabled() {
2080
+ return true;
2081
+ }
2082
+ }
2005
2083
  let message = '';
2006
2084
  try {
2007
- const res = await $mol_wire_async(context_mock).$mol_run({ command: 'sleep 10', dir: '.', timeout: 10, env: { 'MOL_RUN_ASYNC': '1' } });
2085
+ const res = await $mol_wire_async($mol_run_mock).spawn({
2086
+ command: 'sleep 10',
2087
+ dir: '.',
2088
+ timeout: 10,
2089
+ env: { 'MOL_RUN_ASYNC': '1' }
2090
+ });
2008
2091
  }
2009
2092
  catch (e) {
2010
2093
  message = e.message;
2011
2094
  }
2012
- $mol_assert_equal(message, 'Run error, timeout');
2095
+ $mol_assert_equal(message, error_message);
2013
2096
  }
2014
2097
  });
2015
2098
  })($ || ($ = {}));
@@ -2019,7 +2102,7 @@ var $;
2019
2102
  var $;
2020
2103
  (function ($) {
2021
2104
  function $mol_exec(dir, command, ...args) {
2022
- return this.$mol_run({ command: [command, ...args], dir });
2105
+ return this.$mol_run.spawn({ command: [command, ...args], dir });
2023
2106
  }
2024
2107
  $.$mol_exec = $mol_exec;
2025
2108
  })($ || ($ = {}));