mol_mutable 0.0.735 → 0.0.737

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