hyoo_sync_client 0.0.550 → 0.0.552

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.mjs CHANGED
@@ -368,1295 +368,1315 @@ var $;
368
368
  $.$mol_error_mix = $mol_error_mix;
369
369
  })($ || ($ = {}));
370
370
 
371
+ ;
372
+ "use strict";
373
+ var $;
374
+ (function ($_1) {
375
+ let $$;
376
+ (function ($$) {
377
+ let $;
378
+ })($$ = $_1.$$ || ($_1.$$ = {}));
379
+ $_1.$mol_object_field = Symbol('$mol_object_field');
380
+ class $mol_object extends $mol_object2 {
381
+ static make(config) {
382
+ return super.create(obj => {
383
+ for (let key in config)
384
+ obj[key] = config[key];
385
+ });
386
+ }
387
+ }
388
+ $_1.$mol_object = $mol_object;
389
+ })($ || ($ = {}));
390
+
371
391
  ;
372
392
  "use strict";
373
393
  var $;
374
394
  (function ($) {
375
- function $mol_log3_area_lazy(event) {
376
- const self = this;
377
- const stack = self.$mol_log3_stack;
378
- const deep = stack.length;
379
- let logged = false;
380
- stack.push(() => {
381
- logged = true;
382
- self.$mol_log3_area.call(self, event);
383
- });
384
- return () => {
385
- if (logged)
386
- self.console.groupEnd();
387
- if (stack.length > deep)
388
- stack.length = deep;
389
- };
395
+ function $mol_env() {
396
+ return {};
390
397
  }
391
- $.$mol_log3_area_lazy = $mol_log3_area_lazy;
392
- $.$mol_log3_stack = [];
398
+ $.$mol_env = $mol_env;
393
399
  })($ || ($ = {}));
394
400
 
395
401
  ;
396
402
  "use strict";
397
403
  var $;
398
404
  (function ($) {
399
- class $mol_span extends $mol_object2 {
400
- uri;
401
- source;
402
- row;
403
- col;
404
- length;
405
- constructor(uri, source, row, col, length) {
406
- super();
407
- this.uri = uri;
408
- this.source = source;
409
- this.row = row;
410
- this.col = col;
411
- this.length = length;
412
- this[Symbol.toStringTag] = this.uri + ('#' + this.row + ':' + this.col + '/' + this.length);
405
+ $.$mol_env = function $mol_env() {
406
+ return this.process.env;
407
+ };
408
+ })($ || ($ = {}));
409
+
410
+ ;
411
+ "use strict";
412
+ var $;
413
+ (function ($) {
414
+ let $mol_wire_cursor;
415
+ (function ($mol_wire_cursor) {
416
+ $mol_wire_cursor[$mol_wire_cursor["stale"] = -1] = "stale";
417
+ $mol_wire_cursor[$mol_wire_cursor["doubt"] = -2] = "doubt";
418
+ $mol_wire_cursor[$mol_wire_cursor["fresh"] = -3] = "fresh";
419
+ $mol_wire_cursor[$mol_wire_cursor["final"] = -4] = "final";
420
+ })($mol_wire_cursor = $.$mol_wire_cursor || ($.$mol_wire_cursor = {}));
421
+ })($ || ($ = {}));
422
+
423
+ ;
424
+ "use strict";
425
+ var $;
426
+ (function ($) {
427
+ class $mol_wire_pub extends Object {
428
+ data = [];
429
+ static get [Symbol.species]() {
430
+ return Array;
413
431
  }
414
- static unknown = $mol_span.begin('?');
415
- static begin(uri, source = '') {
416
- return new $mol_span(uri, source, 1, 1, 0);
432
+ sub_from = 0;
433
+ get sub_list() {
434
+ const res = [];
435
+ for (let i = this.sub_from; i < this.data.length; i += 2) {
436
+ res.push(this.data[i]);
437
+ }
438
+ return res;
417
439
  }
418
- static end(uri, source) {
419
- return new $mol_span(uri, source, 1, source.length + 1, 0);
440
+ get sub_empty() {
441
+ return this.sub_from === this.data.length;
420
442
  }
421
- static entire(uri, source) {
422
- return new $mol_span(uri, source, 1, 1, source.length);
443
+ sub_on(sub, pub_pos) {
444
+ const pos = this.data.length;
445
+ this.data.push(sub, pub_pos);
446
+ return pos;
423
447
  }
424
- toString() {
425
- return this[Symbol.toStringTag];
448
+ sub_off(sub_pos) {
449
+ if (!(sub_pos < this.data.length)) {
450
+ $mol_fail(new Error(`Wrong pos ${sub_pos}`));
451
+ }
452
+ const end = this.data.length - 2;
453
+ if (sub_pos !== end) {
454
+ this.peer_move(end, sub_pos);
455
+ }
456
+ this.data.pop();
457
+ this.data.pop();
458
+ if (this.data.length === this.sub_from)
459
+ this.reap();
426
460
  }
427
- toJSON() {
428
- return {
429
- uri: this.uri,
430
- row: this.row,
431
- col: this.col,
432
- length: this.length
433
- };
461
+ reap() { }
462
+ promote() {
463
+ $mol_wire_auto()?.track_next(this);
434
464
  }
435
- error(message, Class = Error) {
436
- return new Class(`${message} (${this})`);
465
+ fresh() { }
466
+ complete() { }
467
+ get incompleted() {
468
+ return false;
437
469
  }
438
- span(row, col, length) {
439
- return new $mol_span(this.uri, this.source, row, col, length);
470
+ emit(quant = $mol_wire_cursor.stale) {
471
+ for (let i = this.sub_from; i < this.data.length; i += 2) {
472
+ ;
473
+ this.data[i].absorb(quant);
474
+ }
440
475
  }
441
- after(length = 0) {
442
- return new $mol_span(this.uri, this.source, this.row, this.col + this.length, length);
476
+ peer_move(from_pos, to_pos) {
477
+ const peer = this.data[from_pos];
478
+ const self_pos = this.data[from_pos + 1];
479
+ this.data[to_pos] = peer;
480
+ this.data[to_pos + 1] = self_pos;
481
+ peer.peer_repos(self_pos, to_pos);
443
482
  }
444
- slice(begin, end = -1) {
445
- let len = this.length;
446
- if (begin < 0)
447
- begin += len;
448
- if (end < 0)
449
- end += len;
450
- if (begin < 0 || begin > len)
451
- this.$.$mol_fail(this.error(`Begin value '${begin}' out of range`, RangeError));
452
- if (end < 0 || end > len)
453
- this.$.$mol_fail(this.error(`End value '${end}' out of range`, RangeError));
454
- if (end < begin)
455
- this.$.$mol_fail(this.error(`End value '${end}' can't be less than begin value`, RangeError));
456
- return this.span(this.row, this.col + begin, end - begin);
483
+ peer_repos(peer_pos, self_pos) {
484
+ this.data[peer_pos + 1] = self_pos;
457
485
  }
458
486
  }
459
- $.$mol_span = $mol_span;
487
+ $.$mol_wire_pub = $mol_wire_pub;
460
488
  })($ || ($ = {}));
461
489
 
490
+ ;
491
+ "use strict";
492
+
462
493
  ;
463
494
  "use strict";
464
495
  var $;
465
496
  (function ($) {
466
- function $mol_tree2_to_string(tree) {
467
- let output = [];
468
- function dump(tree, prefix = '') {
469
- if (tree.type.length) {
470
- if (!prefix.length) {
471
- prefix = "\t";
472
- }
473
- output.push(tree.type);
474
- if (tree.kids.length == 1) {
475
- output.push(' ');
476
- dump(tree.kids[0], prefix);
477
- return;
478
- }
479
- output.push("\n");
480
- }
481
- else if (tree.value.length || prefix.length) {
482
- output.push("\\" + tree.value + "\n");
483
- }
484
- for (const kid of tree.kids) {
485
- output.push(prefix);
486
- dump(kid, prefix + "\t");
487
- }
488
- }
489
- dump(tree);
490
- return output.join('');
497
+ $.$mol_wire_auto_sub = null;
498
+ function $mol_wire_auto(next = $.$mol_wire_auto_sub) {
499
+ return $.$mol_wire_auto_sub = next;
491
500
  }
492
- $.$mol_tree2_to_string = $mol_tree2_to_string;
501
+ $.$mol_wire_auto = $mol_wire_auto;
502
+ $.$mol_wire_affected = [];
493
503
  })($ || ($ = {}));
494
504
 
495
505
  ;
496
506
  "use strict";
497
507
  var $;
498
508
  (function ($) {
499
- class $mol_tree2 extends Object {
500
- type;
501
- value;
502
- kids;
503
- span;
504
- constructor(type, value, kids, span) {
505
- super();
506
- this.type = type;
507
- this.value = value;
508
- this.kids = kids;
509
- this.span = span;
510
- this[Symbol.toStringTag] = type || '\\' + value;
511
- }
512
- static list(kids, span = $mol_span.unknown) {
513
- return new $mol_tree2('', '', kids, span);
514
- }
515
- list(kids) {
516
- return $mol_tree2.list(kids, this.span);
517
- }
518
- static data(value, kids = [], span = $mol_span.unknown) {
519
- const chunks = value.split('\n');
520
- if (chunks.length > 1) {
521
- let kid_span = span.span(span.row, span.col, 0);
522
- const data = chunks.map(chunk => {
523
- kid_span = kid_span.after(chunk.length);
524
- return new $mol_tree2('', chunk, [], kid_span);
525
- });
526
- kids = [...data, ...kids];
527
- value = '';
509
+ $['devtoolsFormatters'] ||= [];
510
+ function $mol_dev_format_register(config) {
511
+ $['devtoolsFormatters'].push(config);
512
+ }
513
+ $.$mol_dev_format_register = $mol_dev_format_register;
514
+ $.$mol_dev_format_head = Symbol('$mol_dev_format_head');
515
+ $.$mol_dev_format_body = Symbol('$mol_dev_format_body');
516
+ $mol_dev_format_register({
517
+ header: (val, config = false) => {
518
+ if (config)
519
+ return null;
520
+ if (!val)
521
+ return null;
522
+ if ($.$mol_dev_format_head in val) {
523
+ try {
524
+ return val[$.$mol_dev_format_head]();
525
+ }
526
+ catch (error) {
527
+ return $.$mol_dev_format_accent($mol_dev_format_native(val), '💨', $mol_dev_format_native(error), '');
528
+ }
528
529
  }
529
- return new $mol_tree2('', value, kids, span);
530
- }
531
- data(value, kids = []) {
532
- return $mol_tree2.data(value, kids, this.span);
533
- }
534
- static struct(type, kids = [], span = $mol_span.unknown) {
535
- if (/[ \n\t\\]/.test(type)) {
536
- $$.$mol_fail(span.error(`Wrong type ${JSON.stringify(type)}`));
530
+ if (typeof val === 'function') {
531
+ return $mol_dev_format_native(val);
537
532
  }
538
- return new $mol_tree2(type, '', kids, span);
539
- }
540
- struct(type, kids = []) {
541
- return $mol_tree2.struct(type, kids, this.span);
542
- }
543
- clone(kids, span = this.span) {
544
- return new $mol_tree2(this.type, this.value, kids, span);
533
+ if (Symbol.toStringTag in val) {
534
+ return $mol_dev_format_native(val);
535
+ }
536
+ return null;
537
+ },
538
+ hasBody: val => val[$.$mol_dev_format_body],
539
+ body: val => val[$.$mol_dev_format_body](),
540
+ });
541
+ function $mol_dev_format_native(obj) {
542
+ if (typeof obj === 'undefined')
543
+ return $.$mol_dev_format_shade('undefined');
544
+ return [
545
+ 'object',
546
+ {
547
+ object: obj,
548
+ config: true,
549
+ },
550
+ ];
551
+ }
552
+ $.$mol_dev_format_native = $mol_dev_format_native;
553
+ function $mol_dev_format_auto(obj) {
554
+ if (obj == null)
555
+ return $.$mol_dev_format_shade(String(obj));
556
+ return [
557
+ 'object',
558
+ {
559
+ object: obj,
560
+ config: false,
561
+ },
562
+ ];
563
+ }
564
+ $.$mol_dev_format_auto = $mol_dev_format_auto;
565
+ function $mol_dev_format_element(element, style, ...content) {
566
+ const styles = [];
567
+ for (let key in style)
568
+ styles.push(`${key} : ${style[key]}`);
569
+ return [
570
+ element,
571
+ {
572
+ style: styles.join(' ; '),
573
+ },
574
+ ...content,
575
+ ];
576
+ }
577
+ $.$mol_dev_format_element = $mol_dev_format_element;
578
+ function $mol_dev_format_span(style, ...content) {
579
+ return $mol_dev_format_element('span', {
580
+ ...style,
581
+ }, ...content);
582
+ }
583
+ $.$mol_dev_format_span = $mol_dev_format_span;
584
+ $.$mol_dev_format_div = $mol_dev_format_element.bind(null, 'div');
585
+ $.$mol_dev_format_ol = $mol_dev_format_element.bind(null, 'ol');
586
+ $.$mol_dev_format_li = $mol_dev_format_element.bind(null, 'li');
587
+ $.$mol_dev_format_table = $mol_dev_format_element.bind(null, 'table');
588
+ $.$mol_dev_format_tr = $mol_dev_format_element.bind(null, 'tr');
589
+ $.$mol_dev_format_td = $mol_dev_format_element.bind(null, 'td');
590
+ $.$mol_dev_format_accent = $mol_dev_format_span.bind(null, {
591
+ 'color': 'magenta',
592
+ });
593
+ $.$mol_dev_format_strong = $mol_dev_format_span.bind(null, {
594
+ 'font-weight': 'bold',
595
+ });
596
+ $.$mol_dev_format_string = $mol_dev_format_span.bind(null, {
597
+ 'color': 'green',
598
+ });
599
+ $.$mol_dev_format_shade = $mol_dev_format_span.bind(null, {
600
+ 'color': 'gray',
601
+ });
602
+ $.$mol_dev_format_indent = $.$mol_dev_format_div.bind(null, {
603
+ 'margin-left': '13px'
604
+ });
605
+ })($ || ($ = {}));
606
+
607
+ ;
608
+ "use strict";
609
+ var $;
610
+ (function ($) {
611
+ class $mol_wire_pub_sub extends $mol_wire_pub {
612
+ pub_from = 0;
613
+ cursor = $mol_wire_cursor.stale;
614
+ get temp() {
615
+ return false;
545
616
  }
546
- text() {
547
- var values = [];
548
- for (var kid of this.kids) {
549
- if (kid.type)
550
- continue;
551
- values.push(kid.value);
617
+ get pub_list() {
618
+ const res = [];
619
+ const max = this.cursor >= 0 ? this.cursor : this.sub_from;
620
+ for (let i = this.pub_from; i < max; i += 2) {
621
+ if (this.data[i])
622
+ res.push(this.data[i]);
552
623
  }
553
- return this.value + values.join('\n');
624
+ return res;
554
625
  }
555
- static fromString(str, uri = 'unknown') {
556
- return $$.$mol_tree2_from_string(str, uri);
626
+ track_on() {
627
+ this.cursor = this.pub_from;
628
+ const sub = $mol_wire_auto();
629
+ $mol_wire_auto(this);
630
+ return sub;
557
631
  }
558
- toString() {
559
- return $$.$mol_tree2_to_string(this);
632
+ promote() {
633
+ if (this.cursor >= this.pub_from) {
634
+ $mol_fail(new Error('Circular subscription'));
635
+ }
636
+ super.promote();
560
637
  }
561
- insert(value, ...path) {
562
- if (path.length === 0)
563
- return value;
564
- const type = path[0];
565
- if (typeof type === 'string') {
566
- let replaced = false;
567
- const sub = this.kids.map((item, index) => {
568
- if (item.type !== type)
569
- return item;
570
- replaced = true;
571
- return item.insert(value, ...path.slice(1));
572
- }).filter(Boolean);
573
- if (!replaced && value) {
574
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
638
+ track_next(pub) {
639
+ if (this.cursor < 0)
640
+ $mol_fail(new Error('Promo to non begun sub'));
641
+ if (this.cursor < this.sub_from) {
642
+ const next = this.data[this.cursor];
643
+ if (pub === undefined)
644
+ return next ?? null;
645
+ if (next === pub) {
646
+ this.cursor += 2;
647
+ return next;
648
+ }
649
+ if (next) {
650
+ if (this.sub_from < this.data.length) {
651
+ this.peer_move(this.sub_from, this.data.length);
652
+ }
653
+ this.peer_move(this.cursor, this.sub_from);
654
+ this.sub_from += 2;
575
655
  }
576
- return this.clone(sub);
577
- }
578
- else if (typeof type === 'number') {
579
- const sub = this.kids.slice();
580
- sub[type] = (sub[type] || this.list([]))
581
- .insert(value, ...path.slice(1));
582
- return this.clone(sub.filter(Boolean));
583
656
  }
584
657
  else {
585
- const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
586
- .map(item => item.insert(value, ...path.slice(1)))
587
- .filter(Boolean);
588
- return this.clone(kids);
658
+ if (pub === undefined)
659
+ return null;
660
+ if (this.sub_from < this.data.length) {
661
+ this.peer_move(this.sub_from, this.data.length);
662
+ }
663
+ this.sub_from += 2;
589
664
  }
665
+ this.data[this.cursor] = pub;
666
+ this.data[this.cursor + 1] = pub.sub_on(this, this.cursor);
667
+ this.cursor += 2;
668
+ return pub;
590
669
  }
591
- select(...path) {
592
- let next = [this];
593
- for (const type of path) {
594
- if (!next.length)
595
- break;
596
- const prev = next;
597
- next = [];
598
- for (var item of prev) {
599
- switch (typeof (type)) {
600
- case 'string':
601
- for (var child of item.kids) {
602
- if (child.type == type) {
603
- next.push(child);
604
- }
605
- }
606
- break;
607
- case 'number':
608
- if (type < item.kids.length)
609
- next.push(item.kids[type]);
610
- break;
611
- default: next.push(...item.kids);
612
- }
613
- }
670
+ track_off(sub) {
671
+ $mol_wire_auto(sub);
672
+ if (this.cursor < 0) {
673
+ $mol_fail(new Error('End of non begun sub'));
614
674
  }
615
- return this.list(next);
675
+ for (let cursor = this.pub_from; cursor < this.cursor; cursor += 2) {
676
+ const pub = this.data[cursor];
677
+ pub.fresh();
678
+ }
679
+ this.cursor = $mol_wire_cursor.fresh;
616
680
  }
617
- filter(path, value) {
618
- const sub = this.kids.filter(item => {
619
- var found = item.select(...path);
620
- if (value === undefined) {
621
- return Boolean(found.kids.length);
681
+ pub_off(sub_pos) {
682
+ this.data[sub_pos] = undefined;
683
+ this.data[sub_pos + 1] = undefined;
684
+ }
685
+ destructor() {
686
+ for (let cursor = this.data.length - 2; cursor >= this.sub_from; cursor -= 2) {
687
+ const sub = this.data[cursor];
688
+ const pos = this.data[cursor + 1];
689
+ sub.pub_off(pos);
690
+ this.data.pop();
691
+ this.data.pop();
692
+ }
693
+ this.cursor = this.pub_from;
694
+ this.track_cut();
695
+ this.cursor = $mol_wire_cursor.final;
696
+ }
697
+ track_cut() {
698
+ if (this.cursor < this.pub_from) {
699
+ $mol_fail(new Error('Cut of non begun sub'));
700
+ }
701
+ let tail = 0;
702
+ for (let cursor = this.cursor; cursor < this.sub_from; cursor += 2) {
703
+ const pub = this.data[cursor];
704
+ pub?.sub_off(this.data[cursor + 1]);
705
+ if (this.sub_from < this.data.length) {
706
+ this.peer_move(this.data.length - 2, cursor);
707
+ this.data.pop();
708
+ this.data.pop();
622
709
  }
623
710
  else {
624
- return found.kids.some(child => child.value == value);
711
+ ++tail;
625
712
  }
626
- });
627
- return this.clone(sub);
628
- }
629
- hack_self(belt, context = {}) {
630
- let handle = belt[this.type] || belt[''];
631
- if (!handle || handle === Object.prototype[this.type]) {
632
- handle = (input, belt, context) => [
633
- input.clone(input.hack(belt, context), context.span)
634
- ];
635
713
  }
636
- try {
637
- return handle(this, belt, context);
714
+ for (; tail; --tail) {
715
+ this.data.pop();
716
+ this.data.pop();
638
717
  }
639
- catch (error) {
640
- error.message += `\n${this.clone([])}${this.span}`;
641
- $mol_fail_hidden(error);
718
+ this.sub_from = this.cursor;
719
+ }
720
+ complete() { }
721
+ complete_pubs() {
722
+ const limit = this.cursor < 0 ? this.sub_from : this.cursor;
723
+ for (let cursor = this.pub_from; cursor < limit; cursor += 2) {
724
+ const pub = this.data[cursor];
725
+ if (pub?.incompleted)
726
+ return;
727
+ }
728
+ for (let cursor = this.pub_from; cursor < limit; cursor += 2) {
729
+ const pub = this.data[cursor];
730
+ pub?.complete();
642
731
  }
643
732
  }
644
- hack(belt, context = {}) {
645
- return [].concat(...this.kids.map(child => child.hack_self(belt, context)));
733
+ absorb(quant = $mol_wire_cursor.stale) {
734
+ if (this.cursor === $mol_wire_cursor.final)
735
+ return;
736
+ if (this.cursor >= quant)
737
+ return;
738
+ this.cursor = quant;
739
+ this.emit($mol_wire_cursor.doubt);
646
740
  }
647
- error(message, Class = Error) {
648
- return this.span.error(`${message}\n${this.clone([])}`, Class);
741
+ [$mol_dev_format_head]() {
742
+ return $mol_dev_format_native(this);
743
+ }
744
+ get pub_empty() {
745
+ return this.sub_from === this.pub_from;
649
746
  }
650
747
  }
651
- $.$mol_tree2 = $mol_tree2;
652
- class $mol_tree2_empty extends $mol_tree2 {
653
- constructor() {
654
- super('', '', [], $mol_span.unknown);
748
+ $.$mol_wire_pub_sub = $mol_wire_pub_sub;
749
+ })($ || ($ = {}));
750
+
751
+ ;
752
+ "use strict";
753
+ var $;
754
+ (function ($) {
755
+ class $mol_after_tick extends $mol_object2 {
756
+ task;
757
+ static promise = null;
758
+ cancelled = false;
759
+ constructor(task) {
760
+ super();
761
+ this.task = task;
762
+ if (!$mol_after_tick.promise)
763
+ $mol_after_tick.promise = Promise.resolve().then(() => {
764
+ $mol_after_tick.promise = null;
765
+ });
766
+ $mol_after_tick.promise.then(() => {
767
+ if (this.cancelled)
768
+ return;
769
+ task();
770
+ });
655
771
  }
656
- }
657
- $.$mol_tree2_empty = $mol_tree2_empty;
658
- })($ || ($ = {}));
659
-
660
- ;
661
- "use strict";
662
- var $;
663
- (function ($) {
664
- class $mol_error_syntax extends SyntaxError {
665
- reason;
666
- line;
667
- span;
668
- constructor(reason, line, span) {
669
- super(`${reason}\n${span}\n${line.substring(0, span.col - 1).replace(/\S/g, ' ')}${''.padEnd(span.length, '!')}\n${line}`);
670
- this.reason = reason;
671
- this.line = line;
672
- this.span = span;
772
+ destructor() {
773
+ this.cancelled = true;
673
774
  }
674
775
  }
675
- $.$mol_error_syntax = $mol_error_syntax;
776
+ $.$mol_after_tick = $mol_after_tick;
676
777
  })($ || ($ = {}));
677
778
 
678
779
  ;
679
780
  "use strict";
680
781
  var $;
681
782
  (function ($) {
682
- function $mol_tree2_from_string(str, uri = '?') {
683
- const span = $mol_span.entire(uri, str);
684
- var root = $mol_tree2.list([], span);
685
- var stack = [root];
686
- var pos = 0, row = 0, min_indent = 0;
687
- while (str.length > pos) {
688
- var indent = 0;
689
- var line_start = pos;
690
- row++;
691
- while (str.length > pos && str[pos] == '\t') {
692
- indent++;
693
- pos++;
694
- }
695
- if (!root.kids.length) {
696
- min_indent = indent;
697
- }
698
- indent -= min_indent;
699
- if (indent < 0 || indent >= stack.length) {
700
- const sp = span.span(row, 1, pos - line_start);
701
- while (str.length > pos && str[pos] != '\n') {
702
- pos++;
783
+ const wrappers = new WeakMap();
784
+ class $mol_wire_fiber extends $mol_wire_pub_sub {
785
+ task;
786
+ host;
787
+ static warm = true;
788
+ static planning = new Set();
789
+ static reaping = new Set();
790
+ static plan_task = null;
791
+ static plan() {
792
+ if (this.plan_task)
793
+ return;
794
+ this.plan_task = new $mol_after_tick(() => {
795
+ try {
796
+ this.sync();
703
797
  }
704
- if (indent < 0) {
705
- if (str.length > pos) {
706
- this.$mol_fail(new this.$mol_error_syntax(`Too few tabs`, str.substring(line_start, pos), sp));
707
- }
798
+ finally {
799
+ $mol_wire_fiber.plan_task = null;
708
800
  }
709
- else {
710
- this.$mol_fail(new this.$mol_error_syntax(`Too many tabs`, str.substring(line_start, pos), sp));
801
+ });
802
+ }
803
+ static sync() {
804
+ while (this.planning.size) {
805
+ for (const fiber of this.planning) {
806
+ this.planning.delete(fiber);
807
+ if (fiber.cursor >= 0)
808
+ continue;
809
+ if (fiber.cursor === $mol_wire_cursor.final)
810
+ continue;
811
+ fiber.fresh();
711
812
  }
712
813
  }
713
- stack.length = indent + 1;
714
- var parent = stack[indent];
715
- while (str.length > pos && str[pos] != '\\' && str[pos] != '\n') {
716
- var error_start = pos;
717
- while (str.length > pos && (str[pos] == ' ' || str[pos] == '\t')) {
718
- pos++;
814
+ while (this.reaping.size) {
815
+ const fibers = this.reaping;
816
+ this.reaping = new Set;
817
+ for (const fiber of fibers) {
818
+ if (!fiber.sub_empty)
819
+ continue;
820
+ fiber.destructor();
719
821
  }
720
- if (pos > error_start) {
721
- let line_end = str.indexOf('\n', pos);
722
- if (line_end === -1)
723
- line_end = str.length;
724
- const sp = span.span(row, error_start - line_start + 1, pos - error_start);
725
- this.$mol_fail(new this.$mol_error_syntax(`Wrong nodes separator`, str.substring(line_start, line_end), sp));
822
+ }
823
+ }
824
+ [Symbol.toStringTag];
825
+ cache = undefined;
826
+ get args() {
827
+ return this.data.slice(0, this.pub_from);
828
+ }
829
+ result() {
830
+ if ($mol_promise_like(this.cache))
831
+ return;
832
+ if (this.cache instanceof Error)
833
+ return;
834
+ return this.cache;
835
+ }
836
+ get incompleted() {
837
+ return $mol_promise_like(this.cache);
838
+ }
839
+ field() {
840
+ return this.task.name + '<>';
841
+ }
842
+ constructor(id, task, host, args) {
843
+ super();
844
+ this.task = task;
845
+ this.host = host;
846
+ if (args)
847
+ this.data.push(...args);
848
+ this.pub_from = this.sub_from = args?.length ?? 0;
849
+ this[Symbol.toStringTag] = id;
850
+ }
851
+ plan() {
852
+ $mol_wire_fiber.planning.add(this);
853
+ $mol_wire_fiber.plan();
854
+ return this;
855
+ }
856
+ reap() {
857
+ $mol_wire_fiber.reaping.add(this);
858
+ $mol_wire_fiber.plan();
859
+ }
860
+ toString() {
861
+ return this[Symbol.toStringTag];
862
+ }
863
+ toJSON() {
864
+ return this[Symbol.toStringTag];
865
+ }
866
+ [$mol_dev_format_head]() {
867
+ const cursor = {
868
+ [$mol_wire_cursor.stale]: '🔴',
869
+ [$mol_wire_cursor.doubt]: '🟡',
870
+ [$mol_wire_cursor.fresh]: '🟢',
871
+ [$mol_wire_cursor.final]: '🔵',
872
+ }[this.cursor] ?? this.cursor.toString();
873
+ return $mol_dev_format_div({}, $mol_owning_check(this, this.cache)
874
+ ? $mol_dev_format_auto({
875
+ [$mol_dev_format_head]: () => $mol_dev_format_shade(cursor),
876
+ [$mol_dev_format_body]: () => $mol_dev_format_native(this),
877
+ })
878
+ : $mol_dev_format_shade($mol_dev_format_native(this), cursor), $mol_dev_format_auto(this.cache));
879
+ }
880
+ get $() {
881
+ return (this.host ?? this.task)['$'];
882
+ }
883
+ emit(quant = $mol_wire_cursor.stale) {
884
+ if (this.sub_empty)
885
+ this.plan();
886
+ else
887
+ super.emit(quant);
888
+ }
889
+ fresh() {
890
+ if (this.cursor === $mol_wire_cursor.fresh)
891
+ return;
892
+ if (this.cursor === $mol_wire_cursor.final)
893
+ return;
894
+ check: if (this.cursor === $mol_wire_cursor.doubt) {
895
+ for (let i = this.pub_from; i < this.sub_from; i += 2) {
896
+ ;
897
+ this.data[i]?.fresh();
898
+ if (this.cursor !== $mol_wire_cursor.doubt)
899
+ break check;
726
900
  }
727
- var type_start = pos;
728
- while (str.length > pos &&
729
- str[pos] != '\\' &&
730
- str[pos] != ' ' &&
731
- str[pos] != '\t' &&
732
- str[pos] != '\n') {
733
- pos++;
901
+ this.cursor = $mol_wire_cursor.fresh;
902
+ return;
903
+ }
904
+ const bu = this.track_on();
905
+ let result;
906
+ try {
907
+ switch (this.pub_from) {
908
+ case 0:
909
+ result = this.task.call(this.host);
910
+ break;
911
+ case 1:
912
+ result = this.task.call(this.host, this.data[0]);
913
+ break;
914
+ default:
915
+ result = this.task.call(this.host, ...this.args);
916
+ break;
734
917
  }
735
- if (pos > type_start) {
736
- let next = new $mol_tree2(str.slice(type_start, pos), '', [], span.span(row, type_start - line_start + 1, pos - type_start));
737
- const parent_kids = parent.kids;
738
- parent_kids.push(next);
739
- parent = next;
918
+ if ($mol_promise_like(result)) {
919
+ if (wrappers.has(result)) {
920
+ result = wrappers.get(result).then(a => a);
921
+ }
922
+ else {
923
+ const put = (res) => {
924
+ if (this.cache === result)
925
+ this.put(res);
926
+ return res;
927
+ };
928
+ wrappers.set(result, result = Object.assign(result.then(put, put), { destructor: result.destructor || (() => { }) }));
929
+ wrappers.set(result, result);
930
+ const error = new Error(`Promise in ${this}`);
931
+ Object.defineProperty(result, 'stack', { get: () => error.stack });
932
+ }
740
933
  }
741
- if (str.length > pos && str[pos] == ' ') {
742
- pos++;
934
+ }
935
+ catch (error) {
936
+ if (error instanceof Error || $mol_promise_like(error)) {
937
+ result = error;
938
+ }
939
+ else {
940
+ result = new Error(String(error), { cause: error });
941
+ }
942
+ if ($mol_promise_like(result)) {
943
+ if (wrappers.has(result)) {
944
+ result = wrappers.get(result);
945
+ }
946
+ else {
947
+ wrappers.set(result, result = Object.assign(result.finally(() => {
948
+ if (this.cache === result)
949
+ this.absorb();
950
+ }), { destructor: result.destructor || (() => { }) }));
951
+ const error = new Error(`Promise in ${this}`);
952
+ Object.defineProperty(result, 'stack', { get: () => error.stack });
953
+ }
743
954
  }
744
955
  }
745
- if (str.length > pos && str[pos] == '\\') {
746
- var data_start = pos;
747
- while (str.length > pos && str[pos] != '\n') {
748
- pos++;
956
+ if (!$mol_promise_like(result)) {
957
+ this.track_cut();
958
+ }
959
+ this.track_off(bu);
960
+ this.put(result);
961
+ return this;
962
+ }
963
+ refresh() {
964
+ this.cursor = $mol_wire_cursor.stale;
965
+ this.fresh();
966
+ }
967
+ sync() {
968
+ if (!$mol_wire_fiber.warm) {
969
+ return this.result();
970
+ }
971
+ this.promote();
972
+ this.fresh();
973
+ if (this.cache instanceof Error) {
974
+ return $mol_fail_hidden(this.cache);
975
+ }
976
+ if ($mol_promise_like(this.cache)) {
977
+ return $mol_fail_hidden(this.cache);
978
+ }
979
+ return this.cache;
980
+ }
981
+ async async_raw() {
982
+ while (true) {
983
+ this.fresh();
984
+ if (this.cache instanceof Error) {
985
+ $mol_fail_hidden(this.cache);
986
+ }
987
+ if (!$mol_promise_like(this.cache))
988
+ return this.cache;
989
+ await Promise.race([this.cache, this.step()]);
990
+ if (!$mol_promise_like(this.cache))
991
+ return this.cache;
992
+ if (this.cursor === $mol_wire_cursor.final) {
993
+ await new Promise(() => { });
749
994
  }
750
- let next = new $mol_tree2('', str.slice(data_start + 1, pos), [], span.span(row, data_start - line_start + 2, pos - data_start - 1));
751
- const parent_kids = parent.kids;
752
- parent_kids.push(next);
753
- parent = next;
754
995
  }
755
- if (str.length === pos && stack.length > 0) {
756
- const sp = span.span(row, pos - line_start + 1, 1);
757
- this.$mol_fail(new this.$mol_error_syntax(`Unexpected EOF, LF required`, str.substring(line_start, str.length), sp));
996
+ }
997
+ async() {
998
+ const promise = this.async_raw();
999
+ if (!promise.destructor)
1000
+ promise.destructor = () => this.destructor();
1001
+ return promise;
1002
+ }
1003
+ step() {
1004
+ return new Promise(done => {
1005
+ const sub = new $mol_wire_pub_sub;
1006
+ const prev = sub.track_on();
1007
+ sub.track_next(this);
1008
+ sub.track_off(prev);
1009
+ sub.absorb = () => {
1010
+ done(null);
1011
+ setTimeout(() => sub.destructor());
1012
+ };
1013
+ });
1014
+ }
1015
+ destructor() {
1016
+ super.destructor();
1017
+ if ($mol_owning_check(this, this.cache)) {
1018
+ this.cache.destructor();
758
1019
  }
759
- stack.push(parent);
760
- pos++;
761
1020
  }
762
- return root;
763
1021
  }
764
- $.$mol_tree2_from_string = $mol_tree2_from_string;
1022
+ $.$mol_wire_fiber = $mol_wire_fiber;
765
1023
  })($ || ($ = {}));
766
1024
 
767
1025
  ;
768
1026
  "use strict";
769
1027
  var $;
770
1028
  (function ($) {
771
- function $mol_tree2_from_json(json, span = $mol_span.unknown) {
772
- if (typeof json === 'boolean' || typeof json === 'number' || json === null) {
773
- return new $mol_tree2(String(json), '', [], span);
774
- }
775
- if (typeof json === 'string') {
776
- return $mol_tree2.data(json, [], span);
777
- }
778
- if (typeof json.toJSON === 'function') {
779
- return $mol_tree2_from_json(json.toJSON());
1029
+ $.$mol_compare_deep_cache = new WeakMap();
1030
+ function $mol_compare_deep(left, right) {
1031
+ if (Object.is(left, right))
1032
+ return true;
1033
+ if (left === null)
1034
+ return false;
1035
+ if (right === null)
1036
+ return false;
1037
+ if (typeof left !== 'object')
1038
+ return false;
1039
+ if (typeof right !== 'object')
1040
+ return false;
1041
+ const left_proto = Reflect.getPrototypeOf(left);
1042
+ const right_proto = Reflect.getPrototypeOf(right);
1043
+ if (left_proto !== right_proto)
1044
+ return false;
1045
+ if (left instanceof Boolean)
1046
+ return Object.is(left.valueOf(), right['valueOf']());
1047
+ if (left instanceof Number)
1048
+ return Object.is(left.valueOf(), right['valueOf']());
1049
+ if (left instanceof String)
1050
+ return Object.is(left.valueOf(), right['valueOf']());
1051
+ if (left instanceof Date)
1052
+ return Object.is(left.valueOf(), right['valueOf']());
1053
+ if (left instanceof RegExp)
1054
+ return left.source === right.source && left.flags === right.flags;
1055
+ if (left instanceof Error)
1056
+ return left.message === right.message && left.stack === right.stack;
1057
+ let left_cache = $.$mol_compare_deep_cache.get(left);
1058
+ if (left_cache) {
1059
+ const right_cache = left_cache.get(right);
1060
+ if (typeof right_cache === 'boolean')
1061
+ return right_cache;
780
1062
  }
781
- if (Array.isArray(json)) {
782
- const sub = json.map(json => $mol_tree2_from_json(json, span));
783
- return new $mol_tree2('/', '', sub, span);
1063
+ else {
1064
+ left_cache = new WeakMap();
1065
+ $.$mol_compare_deep_cache.set(left, left_cache);
784
1066
  }
785
- if (ArrayBuffer.isView(json)) {
786
- const buf = new Uint8Array(json.buffer, json.byteOffset, json.byteLength);
787
- return $mol_tree2.data(String.fromCharCode(...buf), [], span);
1067
+ left_cache.set(right, true);
1068
+ let result;
1069
+ try {
1070
+ if (!left_proto)
1071
+ result = compare_pojo(left, right);
1072
+ else if (!Reflect.getPrototypeOf(left_proto))
1073
+ result = compare_pojo(left, right);
1074
+ else if (Symbol.toPrimitive in left)
1075
+ result = compare_primitive(left, right);
1076
+ else if (Array.isArray(left))
1077
+ result = compare_array(left, right);
1078
+ else if (left instanceof Set)
1079
+ result = compare_set(left, right);
1080
+ else if (left instanceof Map)
1081
+ result = compare_map(left, right);
1082
+ else if (ArrayBuffer.isView(left))
1083
+ result = compare_buffer(left, right);
1084
+ else if (Symbol.iterator in left)
1085
+ result = compare_iterator(left[Symbol.iterator](), right[Symbol.iterator]());
1086
+ else
1087
+ result = false;
788
1088
  }
789
- if (json instanceof Date) {
790
- return new $mol_tree2('', json.toISOString(), [], span);
1089
+ finally {
1090
+ left_cache.set(right, result);
791
1091
  }
792
- if (json.toString !== Object.prototype.toString) {
793
- return $mol_tree2.data(json.toString(), [], span);
1092
+ return result;
1093
+ }
1094
+ $.$mol_compare_deep = $mol_compare_deep;
1095
+ function compare_array(left, right) {
1096
+ const len = left.length;
1097
+ if (len !== right.length)
1098
+ return false;
1099
+ for (let i = 0; i < len; ++i) {
1100
+ if (!$mol_compare_deep(left[i], right[i]))
1101
+ return false;
794
1102
  }
795
- if (json instanceof Error) {
796
- const { name, message, stack } = json;
797
- json = { ...json, name, message, stack };
1103
+ return true;
1104
+ }
1105
+ function compare_buffer(left, right) {
1106
+ const len = left.byteLength;
1107
+ if (len !== right.byteLength)
1108
+ return false;
1109
+ if (left instanceof DataView)
1110
+ return compare_buffer(new Uint8Array(left.buffer, left.byteOffset, left.byteLength), new Uint8Array(right.buffer, left.byteOffset, left.byteLength));
1111
+ for (let i = 0; i < len; ++i) {
1112
+ if (left[i] !== right[i])
1113
+ return false;
798
1114
  }
799
- const sub = [];
800
- for (var key in json) {
801
- const val = json[key];
802
- if (val === undefined)
803
- continue;
804
- const subsub = $mol_tree2_from_json(val, span);
805
- if (/^[^\n\t\\ ]+$/.test(key)) {
806
- sub.push(new $mol_tree2(key, '', [subsub], span));
807
- }
808
- else {
809
- sub.push($mol_tree2.data(key, [subsub], span));
810
- }
1115
+ return true;
1116
+ }
1117
+ function compare_iterator(left, right) {
1118
+ while (true) {
1119
+ const left_next = left.next();
1120
+ const right_next = right.next();
1121
+ if (left_next.done !== right_next.done)
1122
+ return false;
1123
+ if (left_next.done)
1124
+ break;
1125
+ if (!$mol_compare_deep(left_next.value, right_next.value))
1126
+ return false;
811
1127
  }
812
- return new $mol_tree2('*', '', sub, span);
1128
+ return true;
813
1129
  }
814
- $.$mol_tree2_from_json = $mol_tree2_from_json;
815
- })($ || ($ = {}));
816
-
817
- ;
818
- "use strict";
819
- var $;
820
- (function ($) {
821
- class $mol_term_color {
822
- static reset = this.ansi(0, 0);
823
- static bold = this.ansi(1, 22);
824
- static italic = this.ansi(3, 23);
825
- static underline = this.ansi(4, 24);
826
- static inverse = this.ansi(7, 27);
827
- static hidden = this.ansi(8, 28);
828
- static strike = this.ansi(9, 29);
829
- static gray = this.ansi(90, 39);
830
- static red = this.ansi(91, 39);
831
- static green = this.ansi(92, 39);
832
- static yellow = this.ansi(93, 39);
833
- static blue = this.ansi(94, 39);
834
- static magenta = this.ansi(95, 39);
835
- static cyan = this.ansi(96, 39);
836
- static Gray = (str) => this.inverse(this.gray(str));
837
- static Red = (str) => this.inverse(this.red(str));
838
- static Green = (str) => this.inverse(this.green(str));
839
- static Yellow = (str) => this.inverse(this.yellow(str));
840
- static Blue = (str) => this.inverse(this.blue(str));
841
- static Magenta = (str) => this.inverse(this.magenta(str));
842
- static Cyan = (str) => this.inverse(this.cyan(str));
843
- static ansi(open, close) {
844
- if (typeof process === 'undefined')
845
- return String;
846
- if (!process.stdout.isTTY)
847
- return String;
848
- const prefix = `\x1b[${open}m`;
849
- const postfix = `\x1b[${close}m`;
850
- const suffix_regexp = new RegExp(postfix.replace('[', '\\['), 'g');
851
- return function colorer(str) {
852
- str = String(str);
853
- if (str === '')
854
- return str;
855
- const suffix = str.replace(suffix_regexp, prefix);
856
- return prefix + suffix + postfix;
857
- };
1130
+ function compare_set(left, right) {
1131
+ if (left.size !== right.size)
1132
+ return false;
1133
+ return compare_iterator(left.values(), right.values());
1134
+ }
1135
+ function compare_map(left, right) {
1136
+ if (left.size !== right.size)
1137
+ return false;
1138
+ return compare_iterator(left.keys(), right.keys())
1139
+ && compare_iterator(left.values(), right.values());
1140
+ }
1141
+ function compare_pojo(left, right) {
1142
+ const left_keys = Object.getOwnPropertyNames(left);
1143
+ const right_keys = Object.getOwnPropertyNames(right);
1144
+ if (!compare_array(left_keys, right_keys))
1145
+ return false;
1146
+ for (let key of left_keys) {
1147
+ if (!$mol_compare_deep(left[key], right[key]))
1148
+ return false;
1149
+ }
1150
+ const left_syms = Object.getOwnPropertySymbols(left);
1151
+ const right_syms = Object.getOwnPropertySymbols(right);
1152
+ if (!compare_array(left_syms, right_syms))
1153
+ return false;
1154
+ for (let key of left_syms) {
1155
+ if (!$mol_compare_deep(left[key], right[key]))
1156
+ return false;
858
1157
  }
1158
+ return true;
859
1159
  }
860
- $.$mol_term_color = $mol_term_color;
861
- })($ || ($ = {}));
862
-
863
- ;
864
- "use strict";
865
- var $;
866
- (function ($) {
867
- function $mol_log3_node_make(level, output, type, color) {
868
- return function $mol_log3_logger(event) {
869
- if (!event.time)
870
- event = { time: new Date().toISOString(), ...event };
871
- let tree = this.$mol_tree2_from_json(event);
872
- tree = tree.struct(type, tree.kids);
873
- let str = color(tree.toString());
874
- this.console[level](str);
875
- const self = this;
876
- return () => self.console.groupEnd();
877
- };
1160
+ function compare_primitive(left, right) {
1161
+ return Object.is(left[Symbol.toPrimitive]('default'), right[Symbol.toPrimitive]('default'));
878
1162
  }
879
- $.$mol_log3_node_make = $mol_log3_node_make;
880
- $.$mol_log3_come = $mol_log3_node_make('info', 'stdout', 'come', $mol_term_color.blue);
881
- $.$mol_log3_done = $mol_log3_node_make('info', 'stdout', 'done', $mol_term_color.green);
882
- $.$mol_log3_fail = $mol_log3_node_make('error', 'stderr', 'fail', $mol_term_color.red);
883
- $.$mol_log3_warn = $mol_log3_node_make('warn', 'stderr', 'warn', $mol_term_color.yellow);
884
- $.$mol_log3_rise = $mol_log3_node_make('log', 'stdout', 'rise', $mol_term_color.magenta);
885
- $.$mol_log3_area = $mol_log3_node_make('log', 'stdout', 'area', $mol_term_color.cyan);
886
1163
  })($ || ($ = {}));
887
1164
 
888
1165
  ;
889
1166
  "use strict";
890
1167
  var $;
891
1168
  (function ($) {
892
- function $mol_env() {
893
- return {};
1169
+ function $mol_log3_area_lazy(event) {
1170
+ const self = this;
1171
+ const stack = self.$mol_log3_stack;
1172
+ const deep = stack.length;
1173
+ let logged = false;
1174
+ stack.push(() => {
1175
+ logged = true;
1176
+ self.$mol_log3_area.call(self, event);
1177
+ });
1178
+ return () => {
1179
+ if (logged)
1180
+ self.console.groupEnd();
1181
+ if (stack.length > deep)
1182
+ stack.length = deep;
1183
+ };
894
1184
  }
895
- $.$mol_env = $mol_env;
896
- })($ || ($ = {}));
897
-
898
- ;
899
- "use strict";
900
- var $;
901
- (function ($) {
902
- $.$mol_env = function $mol_env() {
903
- return this.process.env;
904
- };
905
- })($ || ($ = {}));
906
-
907
- ;
908
- "use strict";
909
- var $;
910
- (function ($) {
911
- let $mol_wire_cursor;
912
- (function ($mol_wire_cursor) {
913
- $mol_wire_cursor[$mol_wire_cursor["stale"] = -1] = "stale";
914
- $mol_wire_cursor[$mol_wire_cursor["doubt"] = -2] = "doubt";
915
- $mol_wire_cursor[$mol_wire_cursor["fresh"] = -3] = "fresh";
916
- $mol_wire_cursor[$mol_wire_cursor["final"] = -4] = "final";
917
- })($mol_wire_cursor = $.$mol_wire_cursor || ($.$mol_wire_cursor = {}));
1185
+ $.$mol_log3_area_lazy = $mol_log3_area_lazy;
1186
+ $.$mol_log3_stack = [];
918
1187
  })($ || ($ = {}));
919
1188
 
920
1189
  ;
921
1190
  "use strict";
922
1191
  var $;
923
1192
  (function ($) {
924
- class $mol_wire_pub extends Object {
925
- data = [];
926
- static get [Symbol.species]() {
927
- return Array;
928
- }
929
- sub_from = 0;
930
- get sub_list() {
931
- const res = [];
932
- for (let i = this.sub_from; i < this.data.length; i += 2) {
933
- res.push(this.data[i]);
934
- }
935
- return res;
1193
+ class $mol_span extends $mol_object2 {
1194
+ uri;
1195
+ source;
1196
+ row;
1197
+ col;
1198
+ length;
1199
+ constructor(uri, source, row, col, length) {
1200
+ super();
1201
+ this.uri = uri;
1202
+ this.source = source;
1203
+ this.row = row;
1204
+ this.col = col;
1205
+ this.length = length;
1206
+ this[Symbol.toStringTag] = this.uri + ('#' + this.row + ':' + this.col + '/' + this.length);
936
1207
  }
937
- get sub_empty() {
938
- return this.sub_from === this.data.length;
1208
+ static unknown = $mol_span.begin('?');
1209
+ static begin(uri, source = '') {
1210
+ return new $mol_span(uri, source, 1, 1, 0);
939
1211
  }
940
- sub_on(sub, pub_pos) {
941
- const pos = this.data.length;
942
- this.data.push(sub, pub_pos);
943
- return pos;
1212
+ static end(uri, source) {
1213
+ return new $mol_span(uri, source, 1, source.length + 1, 0);
944
1214
  }
945
- sub_off(sub_pos) {
946
- if (!(sub_pos < this.data.length)) {
947
- $mol_fail(new Error(`Wrong pos ${sub_pos}`));
948
- }
949
- const end = this.data.length - 2;
950
- if (sub_pos !== end) {
951
- this.peer_move(end, sub_pos);
952
- }
953
- this.data.pop();
954
- this.data.pop();
955
- if (this.data.length === this.sub_from)
956
- this.reap();
1215
+ static entire(uri, source) {
1216
+ return new $mol_span(uri, source, 1, 1, source.length);
957
1217
  }
958
- reap() { }
959
- promote() {
960
- $mol_wire_auto()?.track_next(this);
1218
+ toString() {
1219
+ return this[Symbol.toStringTag];
961
1220
  }
962
- fresh() { }
963
- complete() { }
964
- get incompleted() {
965
- return false;
1221
+ toJSON() {
1222
+ return {
1223
+ uri: this.uri,
1224
+ row: this.row,
1225
+ col: this.col,
1226
+ length: this.length
1227
+ };
966
1228
  }
967
- emit(quant = $mol_wire_cursor.stale) {
968
- for (let i = this.sub_from; i < this.data.length; i += 2) {
969
- ;
970
- this.data[i].absorb(quant);
971
- }
1229
+ error(message, Class = Error) {
1230
+ return new Class(`${message} (${this})`);
972
1231
  }
973
- peer_move(from_pos, to_pos) {
974
- const peer = this.data[from_pos];
975
- const self_pos = this.data[from_pos + 1];
976
- this.data[to_pos] = peer;
977
- this.data[to_pos + 1] = self_pos;
978
- peer.peer_repos(self_pos, to_pos);
1232
+ span(row, col, length) {
1233
+ return new $mol_span(this.uri, this.source, row, col, length);
979
1234
  }
980
- peer_repos(peer_pos, self_pos) {
981
- this.data[peer_pos + 1] = self_pos;
1235
+ after(length = 0) {
1236
+ return new $mol_span(this.uri, this.source, this.row, this.col + this.length, length);
1237
+ }
1238
+ slice(begin, end = -1) {
1239
+ let len = this.length;
1240
+ if (begin < 0)
1241
+ begin += len;
1242
+ if (end < 0)
1243
+ end += len;
1244
+ if (begin < 0 || begin > len)
1245
+ this.$.$mol_fail(this.error(`Begin value '${begin}' out of range`, RangeError));
1246
+ if (end < 0 || end > len)
1247
+ this.$.$mol_fail(this.error(`End value '${end}' out of range`, RangeError));
1248
+ if (end < begin)
1249
+ this.$.$mol_fail(this.error(`End value '${end}' can't be less than begin value`, RangeError));
1250
+ return this.span(this.row, this.col + begin, end - begin);
982
1251
  }
983
1252
  }
984
- $.$mol_wire_pub = $mol_wire_pub;
985
- })($ || ($ = {}));
986
-
987
- ;
988
- "use strict";
989
-
990
- ;
991
- "use strict";
992
- var $;
993
- (function ($) {
994
- $.$mol_wire_auto_sub = null;
995
- function $mol_wire_auto(next = $.$mol_wire_auto_sub) {
996
- return $.$mol_wire_auto_sub = next;
997
- }
998
- $.$mol_wire_auto = $mol_wire_auto;
999
- $.$mol_wire_affected = [];
1000
- })($ || ($ = {}));
1001
-
1002
- ;
1003
- "use strict";
1004
- var $;
1005
- (function ($) {
1006
- $['devtoolsFormatters'] ||= [];
1007
- function $mol_dev_format_register(config) {
1008
- $['devtoolsFormatters'].push(config);
1009
- }
1010
- $.$mol_dev_format_register = $mol_dev_format_register;
1011
- $.$mol_dev_format_head = Symbol('$mol_dev_format_head');
1012
- $.$mol_dev_format_body = Symbol('$mol_dev_format_body');
1013
- $mol_dev_format_register({
1014
- header: (val, config = false) => {
1015
- if (config)
1016
- return null;
1017
- if (!val)
1018
- return null;
1019
- if ($.$mol_dev_format_head in val) {
1020
- try {
1021
- return val[$.$mol_dev_format_head]();
1022
- }
1023
- catch (error) {
1024
- return $.$mol_dev_format_accent($mol_dev_format_native(val), '💨', $mol_dev_format_native(error), '');
1025
- }
1026
- }
1027
- if (typeof val === 'function') {
1028
- return $mol_dev_format_native(val);
1029
- }
1030
- if (Symbol.toStringTag in val) {
1031
- return $mol_dev_format_native(val);
1032
- }
1033
- return null;
1034
- },
1035
- hasBody: val => val[$.$mol_dev_format_body],
1036
- body: val => val[$.$mol_dev_format_body](),
1037
- });
1038
- function $mol_dev_format_native(obj) {
1039
- if (typeof obj === 'undefined')
1040
- return $.$mol_dev_format_shade('undefined');
1041
- return [
1042
- 'object',
1043
- {
1044
- object: obj,
1045
- config: true,
1046
- },
1047
- ];
1048
- }
1049
- $.$mol_dev_format_native = $mol_dev_format_native;
1050
- function $mol_dev_format_auto(obj) {
1051
- if (obj == null)
1052
- return $.$mol_dev_format_shade(String(obj));
1053
- return [
1054
- 'object',
1055
- {
1056
- object: obj,
1057
- config: false,
1058
- },
1059
- ];
1060
- }
1061
- $.$mol_dev_format_auto = $mol_dev_format_auto;
1062
- function $mol_dev_format_element(element, style, ...content) {
1063
- const styles = [];
1064
- for (let key in style)
1065
- styles.push(`${key} : ${style[key]}`);
1066
- return [
1067
- element,
1068
- {
1069
- style: styles.join(' ; '),
1070
- },
1071
- ...content,
1072
- ];
1073
- }
1074
- $.$mol_dev_format_element = $mol_dev_format_element;
1075
- function $mol_dev_format_span(style, ...content) {
1076
- return $mol_dev_format_element('span', {
1077
- ...style,
1078
- }, ...content);
1253
+ $.$mol_span = $mol_span;
1254
+ })($ || ($ = {}));
1255
+
1256
+ ;
1257
+ "use strict";
1258
+ var $;
1259
+ (function ($) {
1260
+ function $mol_tree2_to_string(tree) {
1261
+ let output = [];
1262
+ function dump(tree, prefix = '') {
1263
+ if (tree.type.length) {
1264
+ if (!prefix.length) {
1265
+ prefix = "\t";
1266
+ }
1267
+ output.push(tree.type);
1268
+ if (tree.kids.length == 1) {
1269
+ output.push(' ');
1270
+ dump(tree.kids[0], prefix);
1271
+ return;
1272
+ }
1273
+ output.push("\n");
1274
+ }
1275
+ else if (tree.value.length || prefix.length) {
1276
+ output.push("\\" + tree.value + "\n");
1277
+ }
1278
+ for (const kid of tree.kids) {
1279
+ output.push(prefix);
1280
+ dump(kid, prefix + "\t");
1281
+ }
1282
+ }
1283
+ dump(tree);
1284
+ return output.join('');
1079
1285
  }
1080
- $.$mol_dev_format_span = $mol_dev_format_span;
1081
- $.$mol_dev_format_div = $mol_dev_format_element.bind(null, 'div');
1082
- $.$mol_dev_format_ol = $mol_dev_format_element.bind(null, 'ol');
1083
- $.$mol_dev_format_li = $mol_dev_format_element.bind(null, 'li');
1084
- $.$mol_dev_format_table = $mol_dev_format_element.bind(null, 'table');
1085
- $.$mol_dev_format_tr = $mol_dev_format_element.bind(null, 'tr');
1086
- $.$mol_dev_format_td = $mol_dev_format_element.bind(null, 'td');
1087
- $.$mol_dev_format_accent = $mol_dev_format_span.bind(null, {
1088
- 'color': 'magenta',
1089
- });
1090
- $.$mol_dev_format_strong = $mol_dev_format_span.bind(null, {
1091
- 'font-weight': 'bold',
1092
- });
1093
- $.$mol_dev_format_string = $mol_dev_format_span.bind(null, {
1094
- 'color': 'green',
1095
- });
1096
- $.$mol_dev_format_shade = $mol_dev_format_span.bind(null, {
1097
- 'color': 'gray',
1098
- });
1099
- $.$mol_dev_format_indent = $.$mol_dev_format_div.bind(null, {
1100
- 'margin-left': '13px'
1101
- });
1286
+ $.$mol_tree2_to_string = $mol_tree2_to_string;
1102
1287
  })($ || ($ = {}));
1103
1288
 
1104
1289
  ;
1105
1290
  "use strict";
1106
1291
  var $;
1107
1292
  (function ($) {
1108
- class $mol_wire_pub_sub extends $mol_wire_pub {
1109
- pub_from = 0;
1110
- cursor = $mol_wire_cursor.stale;
1111
- get temp() {
1112
- return false;
1293
+ class $mol_tree2 extends Object {
1294
+ type;
1295
+ value;
1296
+ kids;
1297
+ span;
1298
+ constructor(type, value, kids, span) {
1299
+ super();
1300
+ this.type = type;
1301
+ this.value = value;
1302
+ this.kids = kids;
1303
+ this.span = span;
1304
+ this[Symbol.toStringTag] = type || '\\' + value;
1113
1305
  }
1114
- get pub_list() {
1115
- const res = [];
1116
- const max = this.cursor >= 0 ? this.cursor : this.sub_from;
1117
- for (let i = this.pub_from; i < max; i += 2) {
1118
- if (this.data[i])
1119
- res.push(this.data[i]);
1306
+ static list(kids, span = $mol_span.unknown) {
1307
+ return new $mol_tree2('', '', kids, span);
1308
+ }
1309
+ list(kids) {
1310
+ return $mol_tree2.list(kids, this.span);
1311
+ }
1312
+ static data(value, kids = [], span = $mol_span.unknown) {
1313
+ const chunks = value.split('\n');
1314
+ if (chunks.length > 1) {
1315
+ let kid_span = span.span(span.row, span.col, 0);
1316
+ const data = chunks.map(chunk => {
1317
+ kid_span = kid_span.after(chunk.length);
1318
+ return new $mol_tree2('', chunk, [], kid_span);
1319
+ });
1320
+ kids = [...data, ...kids];
1321
+ value = '';
1120
1322
  }
1121
- return res;
1323
+ return new $mol_tree2('', value, kids, span);
1122
1324
  }
1123
- track_on() {
1124
- this.cursor = this.pub_from;
1125
- const sub = $mol_wire_auto();
1126
- $mol_wire_auto(this);
1127
- return sub;
1325
+ data(value, kids = []) {
1326
+ return $mol_tree2.data(value, kids, this.span);
1128
1327
  }
1129
- promote() {
1130
- if (this.cursor >= this.pub_from) {
1131
- $mol_fail(new Error('Circular subscription'));
1328
+ static struct(type, kids = [], span = $mol_span.unknown) {
1329
+ if (/[ \n\t\\]/.test(type)) {
1330
+ $$.$mol_fail(span.error(`Wrong type ${JSON.stringify(type)}`));
1132
1331
  }
1133
- super.promote();
1332
+ return new $mol_tree2(type, '', kids, span);
1134
1333
  }
1135
- track_next(pub) {
1136
- if (this.cursor < 0)
1137
- $mol_fail(new Error('Promo to non begun sub'));
1138
- if (this.cursor < this.sub_from) {
1139
- const next = this.data[this.cursor];
1140
- if (pub === undefined)
1141
- return next ?? null;
1142
- if (next === pub) {
1143
- this.cursor += 2;
1144
- return next;
1145
- }
1146
- if (next) {
1147
- if (this.sub_from < this.data.length) {
1148
- this.peer_move(this.sub_from, this.data.length);
1149
- }
1150
- this.peer_move(this.cursor, this.sub_from);
1151
- this.sub_from += 2;
1152
- }
1334
+ struct(type, kids = []) {
1335
+ return $mol_tree2.struct(type, kids, this.span);
1336
+ }
1337
+ clone(kids, span = this.span) {
1338
+ return new $mol_tree2(this.type, this.value, kids, span);
1339
+ }
1340
+ text() {
1341
+ var values = [];
1342
+ for (var kid of this.kids) {
1343
+ if (kid.type)
1344
+ continue;
1345
+ values.push(kid.value);
1153
1346
  }
1154
- else {
1155
- if (pub === undefined)
1156
- return null;
1157
- if (this.sub_from < this.data.length) {
1158
- this.peer_move(this.sub_from, this.data.length);
1347
+ return this.value + values.join('\n');
1348
+ }
1349
+ static fromString(str, uri = 'unknown') {
1350
+ return $$.$mol_tree2_from_string(str, uri);
1351
+ }
1352
+ toString() {
1353
+ return $$.$mol_tree2_to_string(this);
1354
+ }
1355
+ insert(value, ...path) {
1356
+ if (path.length === 0)
1357
+ return value;
1358
+ const type = path[0];
1359
+ if (typeof type === 'string') {
1360
+ let replaced = false;
1361
+ const sub = this.kids.map((item, index) => {
1362
+ if (item.type !== type)
1363
+ return item;
1364
+ replaced = true;
1365
+ return item.insert(value, ...path.slice(1));
1366
+ }).filter(Boolean);
1367
+ if (!replaced && value) {
1368
+ sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1159
1369
  }
1160
- this.sub_from += 2;
1370
+ return this.clone(sub);
1161
1371
  }
1162
- this.data[this.cursor] = pub;
1163
- this.data[this.cursor + 1] = pub.sub_on(this, this.cursor);
1164
- this.cursor += 2;
1165
- return pub;
1166
- }
1167
- track_off(sub) {
1168
- $mol_wire_auto(sub);
1169
- if (this.cursor < 0) {
1170
- $mol_fail(new Error('End of non begun sub'));
1372
+ else if (typeof type === 'number') {
1373
+ const sub = this.kids.slice();
1374
+ sub[type] = (sub[type] || this.list([]))
1375
+ .insert(value, ...path.slice(1));
1376
+ return this.clone(sub.filter(Boolean));
1171
1377
  }
1172
- for (let cursor = this.pub_from; cursor < this.cursor; cursor += 2) {
1173
- const pub = this.data[cursor];
1174
- pub.fresh();
1378
+ else {
1379
+ const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1380
+ .map(item => item.insert(value, ...path.slice(1)))
1381
+ .filter(Boolean);
1382
+ return this.clone(kids);
1175
1383
  }
1176
- this.cursor = $mol_wire_cursor.fresh;
1177
- }
1178
- pub_off(sub_pos) {
1179
- this.data[sub_pos] = undefined;
1180
- this.data[sub_pos + 1] = undefined;
1181
1384
  }
1182
- destructor() {
1183
- for (let cursor = this.data.length - 2; cursor >= this.sub_from; cursor -= 2) {
1184
- const sub = this.data[cursor];
1185
- const pos = this.data[cursor + 1];
1186
- sub.pub_off(pos);
1187
- this.data.pop();
1188
- this.data.pop();
1385
+ select(...path) {
1386
+ let next = [this];
1387
+ for (const type of path) {
1388
+ if (!next.length)
1389
+ break;
1390
+ const prev = next;
1391
+ next = [];
1392
+ for (var item of prev) {
1393
+ switch (typeof (type)) {
1394
+ case 'string':
1395
+ for (var child of item.kids) {
1396
+ if (child.type == type) {
1397
+ next.push(child);
1398
+ }
1399
+ }
1400
+ break;
1401
+ case 'number':
1402
+ if (type < item.kids.length)
1403
+ next.push(item.kids[type]);
1404
+ break;
1405
+ default: next.push(...item.kids);
1406
+ }
1407
+ }
1189
1408
  }
1190
- this.cursor = this.pub_from;
1191
- this.track_cut();
1192
- this.cursor = $mol_wire_cursor.final;
1409
+ return this.list(next);
1193
1410
  }
1194
- track_cut() {
1195
- if (this.cursor < this.pub_from) {
1196
- $mol_fail(new Error('Cut of non begun sub'));
1197
- }
1198
- let tail = 0;
1199
- for (let cursor = this.cursor; cursor < this.sub_from; cursor += 2) {
1200
- const pub = this.data[cursor];
1201
- pub?.sub_off(this.data[cursor + 1]);
1202
- if (this.sub_from < this.data.length) {
1203
- this.peer_move(this.data.length - 2, cursor);
1204
- this.data.pop();
1205
- this.data.pop();
1411
+ filter(path, value) {
1412
+ const sub = this.kids.filter(item => {
1413
+ var found = item.select(...path);
1414
+ if (value === undefined) {
1415
+ return Boolean(found.kids.length);
1206
1416
  }
1207
1417
  else {
1208
- ++tail;
1418
+ return found.kids.some(child => child.value == value);
1209
1419
  }
1210
- }
1211
- for (; tail; --tail) {
1212
- this.data.pop();
1213
- this.data.pop();
1214
- }
1215
- this.sub_from = this.cursor;
1420
+ });
1421
+ return this.clone(sub);
1216
1422
  }
1217
- complete() { }
1218
- complete_pubs() {
1219
- const limit = this.cursor < 0 ? this.sub_from : this.cursor;
1220
- for (let cursor = this.pub_from; cursor < limit; cursor += 2) {
1221
- const pub = this.data[cursor];
1222
- if (pub?.incompleted)
1223
- return;
1423
+ hack_self(belt, context = {}) {
1424
+ let handle = belt[this.type] || belt[''];
1425
+ if (!handle || handle === Object.prototype[this.type]) {
1426
+ handle = (input, belt, context) => [
1427
+ input.clone(input.hack(belt, context), context.span)
1428
+ ];
1224
1429
  }
1225
- for (let cursor = this.pub_from; cursor < limit; cursor += 2) {
1226
- const pub = this.data[cursor];
1227
- pub?.complete();
1430
+ try {
1431
+ return handle(this, belt, context);
1432
+ }
1433
+ catch (error) {
1434
+ error.message += `\n${this.clone([])}${this.span}`;
1435
+ $mol_fail_hidden(error);
1228
1436
  }
1229
1437
  }
1230
- absorb(quant = $mol_wire_cursor.stale) {
1231
- if (this.cursor === $mol_wire_cursor.final)
1232
- return;
1233
- if (this.cursor >= quant)
1234
- return;
1235
- this.cursor = quant;
1236
- this.emit($mol_wire_cursor.doubt);
1438
+ hack(belt, context = {}) {
1439
+ return [].concat(...this.kids.map(child => child.hack_self(belt, context)));
1237
1440
  }
1238
- [$mol_dev_format_head]() {
1239
- return $mol_dev_format_native(this);
1441
+ error(message, Class = Error) {
1442
+ return this.span.error(`${message}\n${this.clone([])}`, Class);
1240
1443
  }
1241
- get pub_empty() {
1242
- return this.sub_from === this.pub_from;
1444
+ }
1445
+ $.$mol_tree2 = $mol_tree2;
1446
+ class $mol_tree2_empty extends $mol_tree2 {
1447
+ constructor() {
1448
+ super('', '', [], $mol_span.unknown);
1243
1449
  }
1244
1450
  }
1245
- $.$mol_wire_pub_sub = $mol_wire_pub_sub;
1451
+ $.$mol_tree2_empty = $mol_tree2_empty;
1246
1452
  })($ || ($ = {}));
1247
1453
 
1248
1454
  ;
1249
1455
  "use strict";
1250
1456
  var $;
1251
1457
  (function ($) {
1252
- class $mol_after_tick extends $mol_object2 {
1253
- task;
1254
- static promise = null;
1255
- cancelled = false;
1256
- constructor(task) {
1257
- super();
1258
- this.task = task;
1259
- if (!$mol_after_tick.promise)
1260
- $mol_after_tick.promise = Promise.resolve().then(() => {
1261
- $mol_after_tick.promise = null;
1262
- });
1263
- $mol_after_tick.promise.then(() => {
1264
- if (this.cancelled)
1265
- return;
1266
- task();
1267
- });
1268
- }
1269
- destructor() {
1270
- this.cancelled = true;
1458
+ class $mol_error_syntax extends SyntaxError {
1459
+ reason;
1460
+ line;
1461
+ span;
1462
+ constructor(reason, line, span) {
1463
+ super(`${reason}\n${span}\n${line.substring(0, span.col - 1).replace(/\S/g, ' ')}${''.padEnd(span.length, '!')}\n${line}`);
1464
+ this.reason = reason;
1465
+ this.line = line;
1466
+ this.span = span;
1271
1467
  }
1272
1468
  }
1273
- $.$mol_after_tick = $mol_after_tick;
1469
+ $.$mol_error_syntax = $mol_error_syntax;
1274
1470
  })($ || ($ = {}));
1275
1471
 
1276
1472
  ;
1277
1473
  "use strict";
1278
1474
  var $;
1279
1475
  (function ($) {
1280
- const wrappers = new WeakMap();
1281
- class $mol_wire_fiber extends $mol_wire_pub_sub {
1282
- task;
1283
- host;
1284
- static warm = true;
1285
- static planning = new Set();
1286
- static reaping = new Set();
1287
- static plan_task = null;
1288
- static plan() {
1289
- if (this.plan_task)
1290
- return;
1291
- this.plan_task = new $mol_after_tick(() => {
1292
- try {
1293
- this.sync();
1294
- }
1295
- finally {
1296
- $mol_wire_fiber.plan_task = null;
1297
- }
1298
- });
1299
- }
1300
- static sync() {
1301
- while (this.planning.size) {
1302
- for (const fiber of this.planning) {
1303
- this.planning.delete(fiber);
1304
- if (fiber.cursor >= 0)
1305
- continue;
1306
- if (fiber.cursor === $mol_wire_cursor.final)
1307
- continue;
1308
- fiber.fresh();
1309
- }
1310
- }
1311
- while (this.reaping.size) {
1312
- const fibers = this.reaping;
1313
- this.reaping = new Set;
1314
- for (const fiber of fibers) {
1315
- if (!fiber.sub_empty)
1316
- continue;
1317
- fiber.destructor();
1318
- }
1476
+ function $mol_tree2_from_string(str, uri = '?') {
1477
+ const span = $mol_span.entire(uri, str);
1478
+ var root = $mol_tree2.list([], span);
1479
+ var stack = [root];
1480
+ var pos = 0, row = 0, min_indent = 0;
1481
+ while (str.length > pos) {
1482
+ var indent = 0;
1483
+ var line_start = pos;
1484
+ row++;
1485
+ while (str.length > pos && str[pos] == '\t') {
1486
+ indent++;
1487
+ pos++;
1319
1488
  }
1320
- }
1321
- [Symbol.toStringTag];
1322
- cache = undefined;
1323
- get args() {
1324
- return this.data.slice(0, this.pub_from);
1325
- }
1326
- result() {
1327
- if ($mol_promise_like(this.cache))
1328
- return;
1329
- if (this.cache instanceof Error)
1330
- return;
1331
- return this.cache;
1332
- }
1333
- get incompleted() {
1334
- return $mol_promise_like(this.cache);
1335
- }
1336
- field() {
1337
- return this.task.name + '<>';
1338
- }
1339
- constructor(id, task, host, args) {
1340
- super();
1341
- this.task = task;
1342
- this.host = host;
1343
- if (args)
1344
- this.data.push(...args);
1345
- this.pub_from = this.sub_from = args?.length ?? 0;
1346
- this[Symbol.toStringTag] = id;
1347
- }
1348
- plan() {
1349
- $mol_wire_fiber.planning.add(this);
1350
- $mol_wire_fiber.plan();
1351
- return this;
1352
- }
1353
- reap() {
1354
- $mol_wire_fiber.reaping.add(this);
1355
- $mol_wire_fiber.plan();
1356
- }
1357
- toString() {
1358
- return this[Symbol.toStringTag];
1359
- }
1360
- toJSON() {
1361
- return this[Symbol.toStringTag];
1362
- }
1363
- [$mol_dev_format_head]() {
1364
- const cursor = {
1365
- [$mol_wire_cursor.stale]: '🔴',
1366
- [$mol_wire_cursor.doubt]: '🟡',
1367
- [$mol_wire_cursor.fresh]: '🟢',
1368
- [$mol_wire_cursor.final]: '🔵',
1369
- }[this.cursor] ?? this.cursor.toString();
1370
- return $mol_dev_format_div({}, $mol_owning_check(this, this.cache)
1371
- ? $mol_dev_format_auto({
1372
- [$mol_dev_format_head]: () => $mol_dev_format_shade(cursor),
1373
- [$mol_dev_format_body]: () => $mol_dev_format_native(this),
1374
- })
1375
- : $mol_dev_format_shade($mol_dev_format_native(this), cursor), $mol_dev_format_auto(this.cache));
1376
- }
1377
- get $() {
1378
- return (this.host ?? this.task)['$'];
1379
- }
1380
- emit(quant = $mol_wire_cursor.stale) {
1381
- if (this.sub_empty)
1382
- this.plan();
1383
- else
1384
- super.emit(quant);
1385
- }
1386
- fresh() {
1387
- if (this.cursor === $mol_wire_cursor.fresh)
1388
- return;
1389
- if (this.cursor === $mol_wire_cursor.final)
1390
- return;
1391
- check: if (this.cursor === $mol_wire_cursor.doubt) {
1392
- for (let i = this.pub_from; i < this.sub_from; i += 2) {
1393
- ;
1394
- this.data[i]?.fresh();
1395
- if (this.cursor !== $mol_wire_cursor.doubt)
1396
- break check;
1397
- }
1398
- this.cursor = $mol_wire_cursor.fresh;
1399
- return;
1489
+ if (!root.kids.length) {
1490
+ min_indent = indent;
1400
1491
  }
1401
- const bu = this.track_on();
1402
- let result;
1403
- try {
1404
- switch (this.pub_from) {
1405
- case 0:
1406
- result = this.task.call(this.host);
1407
- break;
1408
- case 1:
1409
- result = this.task.call(this.host, this.data[0]);
1410
- break;
1411
- default:
1412
- result = this.task.call(this.host, ...this.args);
1413
- break;
1492
+ indent -= min_indent;
1493
+ if (indent < 0 || indent >= stack.length) {
1494
+ const sp = span.span(row, 1, pos - line_start);
1495
+ while (str.length > pos && str[pos] != '\n') {
1496
+ pos++;
1414
1497
  }
1415
- if ($mol_promise_like(result)) {
1416
- if (wrappers.has(result)) {
1417
- result = wrappers.get(result).then(a => a);
1418
- }
1419
- else {
1420
- const put = (res) => {
1421
- if (this.cache === result)
1422
- this.put(res);
1423
- return res;
1424
- };
1425
- wrappers.set(result, result = Object.assign(result.then(put, put), { destructor: result.destructor || (() => { }) }));
1426
- wrappers.set(result, result);
1427
- const error = new Error(`Promise in ${this}`);
1428
- Object.defineProperty(result, 'stack', { get: () => error.stack });
1498
+ if (indent < 0) {
1499
+ if (str.length > pos) {
1500
+ this.$mol_fail(new this.$mol_error_syntax(`Too few tabs`, str.substring(line_start, pos), sp));
1429
1501
  }
1430
1502
  }
1503
+ else {
1504
+ this.$mol_fail(new this.$mol_error_syntax(`Too many tabs`, str.substring(line_start, pos), sp));
1505
+ }
1431
1506
  }
1432
- catch (error) {
1433
- if (error instanceof Error || $mol_promise_like(error)) {
1434
- result = error;
1507
+ stack.length = indent + 1;
1508
+ var parent = stack[indent];
1509
+ while (str.length > pos && str[pos] != '\\' && str[pos] != '\n') {
1510
+ var error_start = pos;
1511
+ while (str.length > pos && (str[pos] == ' ' || str[pos] == '\t')) {
1512
+ pos++;
1435
1513
  }
1436
- else {
1437
- result = new Error(String(error), { cause: error });
1514
+ if (pos > error_start) {
1515
+ let line_end = str.indexOf('\n', pos);
1516
+ if (line_end === -1)
1517
+ line_end = str.length;
1518
+ const sp = span.span(row, error_start - line_start + 1, pos - error_start);
1519
+ this.$mol_fail(new this.$mol_error_syntax(`Wrong nodes separator`, str.substring(line_start, line_end), sp));
1438
1520
  }
1439
- if ($mol_promise_like(result)) {
1440
- if (wrappers.has(result)) {
1441
- result = wrappers.get(result);
1442
- }
1443
- else {
1444
- wrappers.set(result, result = Object.assign(result.finally(() => {
1445
- if (this.cache === result)
1446
- this.absorb();
1447
- }), { destructor: result.destructor || (() => { }) }));
1448
- const error = new Error(`Promise in ${this}`);
1449
- Object.defineProperty(result, 'stack', { get: () => error.stack });
1450
- }
1521
+ var type_start = pos;
1522
+ while (str.length > pos &&
1523
+ str[pos] != '\\' &&
1524
+ str[pos] != ' ' &&
1525
+ str[pos] != '\t' &&
1526
+ str[pos] != '\n') {
1527
+ pos++;
1451
1528
  }
1452
- }
1453
- if (!$mol_promise_like(result)) {
1454
- this.track_cut();
1455
- }
1456
- this.track_off(bu);
1457
- this.put(result);
1458
- return this;
1459
- }
1460
- refresh() {
1461
- this.cursor = $mol_wire_cursor.stale;
1462
- this.fresh();
1463
- }
1464
- sync() {
1465
- if (!$mol_wire_fiber.warm) {
1466
- return this.result();
1467
- }
1468
- this.promote();
1469
- this.fresh();
1470
- if (this.cache instanceof Error) {
1471
- return $mol_fail_hidden(this.cache);
1472
- }
1473
- if ($mol_promise_like(this.cache)) {
1474
- return $mol_fail_hidden(this.cache);
1475
- }
1476
- return this.cache;
1477
- }
1478
- async async_raw() {
1479
- while (true) {
1480
- this.fresh();
1481
- if (this.cache instanceof Error) {
1482
- $mol_fail_hidden(this.cache);
1529
+ if (pos > type_start) {
1530
+ let next = new $mol_tree2(str.slice(type_start, pos), '', [], span.span(row, type_start - line_start + 1, pos - type_start));
1531
+ const parent_kids = parent.kids;
1532
+ parent_kids.push(next);
1533
+ parent = next;
1483
1534
  }
1484
- if (!$mol_promise_like(this.cache))
1485
- return this.cache;
1486
- await Promise.race([this.cache, this.step()]);
1487
- if (!$mol_promise_like(this.cache))
1488
- return this.cache;
1489
- if (this.cursor === $mol_wire_cursor.final) {
1490
- await new Promise(() => { });
1535
+ if (str.length > pos && str[pos] == ' ') {
1536
+ pos++;
1491
1537
  }
1492
1538
  }
1493
- }
1494
- async() {
1495
- const promise = this.async_raw();
1496
- if (!promise.destructor)
1497
- promise.destructor = () => this.destructor();
1498
- return promise;
1499
- }
1500
- step() {
1501
- return new Promise(done => {
1502
- const sub = new $mol_wire_pub_sub;
1503
- const prev = sub.track_on();
1504
- sub.track_next(this);
1505
- sub.track_off(prev);
1506
- sub.absorb = () => {
1507
- done(null);
1508
- setTimeout(() => sub.destructor());
1509
- };
1510
- });
1511
- }
1512
- destructor() {
1513
- super.destructor();
1514
- if ($mol_owning_check(this, this.cache)) {
1515
- this.cache.destructor();
1539
+ if (str.length > pos && str[pos] == '\\') {
1540
+ var data_start = pos;
1541
+ while (str.length > pos && str[pos] != '\n') {
1542
+ pos++;
1543
+ }
1544
+ let next = new $mol_tree2('', str.slice(data_start + 1, pos), [], span.span(row, data_start - line_start + 2, pos - data_start - 1));
1545
+ const parent_kids = parent.kids;
1546
+ parent_kids.push(next);
1547
+ parent = next;
1548
+ }
1549
+ if (str.length === pos && stack.length > 0) {
1550
+ const sp = span.span(row, pos - line_start + 1, 1);
1551
+ this.$mol_fail(new this.$mol_error_syntax(`Unexpected EOF, LF required`, str.substring(line_start, str.length), sp));
1516
1552
  }
1553
+ stack.push(parent);
1554
+ pos++;
1517
1555
  }
1556
+ return root;
1518
1557
  }
1519
- $.$mol_wire_fiber = $mol_wire_fiber;
1558
+ $.$mol_tree2_from_string = $mol_tree2_from_string;
1520
1559
  })($ || ($ = {}));
1521
1560
 
1522
1561
  ;
1523
1562
  "use strict";
1524
1563
  var $;
1525
1564
  (function ($) {
1526
- $.$mol_compare_deep_cache = new WeakMap();
1527
- function $mol_compare_deep(left, right) {
1528
- if (Object.is(left, right))
1529
- return true;
1530
- if (left === null)
1531
- return false;
1532
- if (right === null)
1533
- return false;
1534
- if (typeof left !== 'object')
1535
- return false;
1536
- if (typeof right !== 'object')
1537
- return false;
1538
- const left_proto = Reflect.getPrototypeOf(left);
1539
- const right_proto = Reflect.getPrototypeOf(right);
1540
- if (left_proto !== right_proto)
1541
- return false;
1542
- if (left instanceof Boolean)
1543
- return Object.is(left.valueOf(), right['valueOf']());
1544
- if (left instanceof Number)
1545
- return Object.is(left.valueOf(), right['valueOf']());
1546
- if (left instanceof String)
1547
- return Object.is(left.valueOf(), right['valueOf']());
1548
- if (left instanceof Date)
1549
- return Object.is(left.valueOf(), right['valueOf']());
1550
- if (left instanceof RegExp)
1551
- return left.source === right.source && left.flags === right.flags;
1552
- if (left instanceof Error)
1553
- return left.message === right.message && left.stack === right.stack;
1554
- let left_cache = $.$mol_compare_deep_cache.get(left);
1555
- if (left_cache) {
1556
- const right_cache = left_cache.get(right);
1557
- if (typeof right_cache === 'boolean')
1558
- return right_cache;
1565
+ function $mol_tree2_from_json(json, span = $mol_span.unknown) {
1566
+ if (typeof json === 'boolean' || typeof json === 'number' || json === null) {
1567
+ return new $mol_tree2(String(json), '', [], span);
1559
1568
  }
1560
- else {
1561
- left_cache = new WeakMap();
1562
- $.$mol_compare_deep_cache.set(left, left_cache);
1569
+ if (typeof json === 'string') {
1570
+ return $mol_tree2.data(json, [], span);
1563
1571
  }
1564
- left_cache.set(right, true);
1565
- let result;
1566
- try {
1567
- if (!left_proto)
1568
- result = compare_pojo(left, right);
1569
- else if (!Reflect.getPrototypeOf(left_proto))
1570
- result = compare_pojo(left, right);
1571
- else if (Symbol.toPrimitive in left)
1572
- result = compare_primitive(left, right);
1573
- else if (Array.isArray(left))
1574
- result = compare_array(left, right);
1575
- else if (left instanceof Set)
1576
- result = compare_set(left, right);
1577
- else if (left instanceof Map)
1578
- result = compare_map(left, right);
1579
- else if (ArrayBuffer.isView(left))
1580
- result = compare_buffer(left, right);
1581
- else if (Symbol.iterator in left)
1582
- result = compare_iterator(left[Symbol.iterator](), right[Symbol.iterator]());
1583
- else
1584
- result = false;
1572
+ if (typeof json.toJSON === 'function') {
1573
+ return $mol_tree2_from_json(json.toJSON());
1585
1574
  }
1586
- finally {
1587
- left_cache.set(right, result);
1575
+ if (Array.isArray(json)) {
1576
+ const sub = json.map(json => $mol_tree2_from_json(json, span));
1577
+ return new $mol_tree2('/', '', sub, span);
1588
1578
  }
1589
- return result;
1590
- }
1591
- $.$mol_compare_deep = $mol_compare_deep;
1592
- function compare_array(left, right) {
1593
- const len = left.length;
1594
- if (len !== right.length)
1595
- return false;
1596
- for (let i = 0; i < len; ++i) {
1597
- if (!$mol_compare_deep(left[i], right[i]))
1598
- return false;
1579
+ if (ArrayBuffer.isView(json)) {
1580
+ const buf = new Uint8Array(json.buffer, json.byteOffset, json.byteLength);
1581
+ return $mol_tree2.data(String.fromCharCode(...buf), [], span);
1599
1582
  }
1600
- return true;
1601
- }
1602
- function compare_buffer(left, right) {
1603
- const len = left.byteLength;
1604
- if (len !== right.byteLength)
1605
- return false;
1606
- if (left instanceof DataView)
1607
- return compare_buffer(new Uint8Array(left.buffer, left.byteOffset, left.byteLength), new Uint8Array(right.buffer, left.byteOffset, left.byteLength));
1608
- for (let i = 0; i < len; ++i) {
1609
- if (left[i] !== right[i])
1610
- return false;
1583
+ if (json instanceof Date) {
1584
+ return new $mol_tree2('', json.toISOString(), [], span);
1611
1585
  }
1612
- return true;
1613
- }
1614
- function compare_iterator(left, right) {
1615
- while (true) {
1616
- const left_next = left.next();
1617
- const right_next = right.next();
1618
- if (left_next.done !== right_next.done)
1619
- return false;
1620
- if (left_next.done)
1621
- break;
1622
- if (!$mol_compare_deep(left_next.value, right_next.value))
1623
- return false;
1586
+ if (json.toString !== Object.prototype.toString) {
1587
+ return $mol_tree2.data(json.toString(), [], span);
1624
1588
  }
1625
- return true;
1626
- }
1627
- function compare_set(left, right) {
1628
- if (left.size !== right.size)
1629
- return false;
1630
- return compare_iterator(left.values(), right.values());
1631
- }
1632
- function compare_map(left, right) {
1633
- if (left.size !== right.size)
1634
- return false;
1635
- return compare_iterator(left.keys(), right.keys())
1636
- && compare_iterator(left.values(), right.values());
1637
- }
1638
- function compare_pojo(left, right) {
1639
- const left_keys = Object.getOwnPropertyNames(left);
1640
- const right_keys = Object.getOwnPropertyNames(right);
1641
- if (!compare_array(left_keys, right_keys))
1642
- return false;
1643
- for (let key of left_keys) {
1644
- if (!$mol_compare_deep(left[key], right[key]))
1645
- return false;
1589
+ if (json instanceof Error) {
1590
+ const { name, message, stack } = json;
1591
+ json = { ...json, name, message, stack };
1646
1592
  }
1647
- const left_syms = Object.getOwnPropertySymbols(left);
1648
- const right_syms = Object.getOwnPropertySymbols(right);
1649
- if (!compare_array(left_syms, right_syms))
1650
- return false;
1651
- for (let key of left_syms) {
1652
- if (!$mol_compare_deep(left[key], right[key]))
1653
- return false;
1593
+ const sub = [];
1594
+ for (var key in json) {
1595
+ const val = json[key];
1596
+ if (val === undefined)
1597
+ continue;
1598
+ const subsub = $mol_tree2_from_json(val, span);
1599
+ if (/^[^\n\t\\ ]+$/.test(key)) {
1600
+ sub.push(new $mol_tree2(key, '', [subsub], span));
1601
+ }
1602
+ else {
1603
+ sub.push($mol_tree2.data(key, [subsub], span));
1604
+ }
1654
1605
  }
1655
- return true;
1606
+ return new $mol_tree2('*', '', sub, span);
1656
1607
  }
1657
- function compare_primitive(left, right) {
1658
- return Object.is(left[Symbol.toPrimitive]('default'), right[Symbol.toPrimitive]('default'));
1608
+ $.$mol_tree2_from_json = $mol_tree2_from_json;
1609
+ })($ || ($ = {}));
1610
+
1611
+ ;
1612
+ "use strict";
1613
+ var $;
1614
+ (function ($) {
1615
+ class $mol_term_color {
1616
+ static reset = this.ansi(0, 0);
1617
+ static bold = this.ansi(1, 22);
1618
+ static italic = this.ansi(3, 23);
1619
+ static underline = this.ansi(4, 24);
1620
+ static inverse = this.ansi(7, 27);
1621
+ static hidden = this.ansi(8, 28);
1622
+ static strike = this.ansi(9, 29);
1623
+ static gray = this.ansi(90, 39);
1624
+ static red = this.ansi(91, 39);
1625
+ static green = this.ansi(92, 39);
1626
+ static yellow = this.ansi(93, 39);
1627
+ static blue = this.ansi(94, 39);
1628
+ static magenta = this.ansi(95, 39);
1629
+ static cyan = this.ansi(96, 39);
1630
+ static Gray = (str) => this.inverse(this.gray(str));
1631
+ static Red = (str) => this.inverse(this.red(str));
1632
+ static Green = (str) => this.inverse(this.green(str));
1633
+ static Yellow = (str) => this.inverse(this.yellow(str));
1634
+ static Blue = (str) => this.inverse(this.blue(str));
1635
+ static Magenta = (str) => this.inverse(this.magenta(str));
1636
+ static Cyan = (str) => this.inverse(this.cyan(str));
1637
+ static ansi(open, close) {
1638
+ if (typeof process === 'undefined')
1639
+ return String;
1640
+ if (!process.stdout.isTTY)
1641
+ return String;
1642
+ const prefix = `\x1b[${open}m`;
1643
+ const postfix = `\x1b[${close}m`;
1644
+ const suffix_regexp = new RegExp(postfix.replace('[', '\\['), 'g');
1645
+ return function colorer(str) {
1646
+ str = String(str);
1647
+ if (str === '')
1648
+ return str;
1649
+ const suffix = str.replace(suffix_regexp, prefix);
1650
+ return prefix + suffix + postfix;
1651
+ };
1652
+ }
1653
+ }
1654
+ $.$mol_term_color = $mol_term_color;
1655
+ })($ || ($ = {}));
1656
+
1657
+ ;
1658
+ "use strict";
1659
+ var $;
1660
+ (function ($) {
1661
+ function $mol_log3_node_make(level, output, type, color) {
1662
+ return function $mol_log3_logger(event) {
1663
+ if (!event.time)
1664
+ event = { time: new Date().toISOString(), ...event };
1665
+ let tree = this.$mol_tree2_from_json(event);
1666
+ tree = tree.struct(type, tree.kids);
1667
+ let str = color(tree.toString());
1668
+ this.console[level](str);
1669
+ const self = this;
1670
+ return () => self.console.groupEnd();
1671
+ };
1659
1672
  }
1673
+ $.$mol_log3_node_make = $mol_log3_node_make;
1674
+ $.$mol_log3_come = $mol_log3_node_make('info', 'stdout', 'come', $mol_term_color.blue);
1675
+ $.$mol_log3_done = $mol_log3_node_make('info', 'stdout', 'done', $mol_term_color.green);
1676
+ $.$mol_log3_fail = $mol_log3_node_make('error', 'stderr', 'fail', $mol_term_color.red);
1677
+ $.$mol_log3_warn = $mol_log3_node_make('warn', 'stderr', 'warn', $mol_term_color.yellow);
1678
+ $.$mol_log3_rise = $mol_log3_node_make('log', 'stdout', 'rise', $mol_term_color.magenta);
1679
+ $.$mol_log3_area = $mol_log3_node_make('log', 'stdout', 'area', $mol_term_color.cyan);
1660
1680
  })($ || ($ = {}));
1661
1681
 
1662
1682
  ;
@@ -1775,97 +1795,115 @@ var $;
1775
1795
  class $mol_run_error extends $mol_error_mix {
1776
1796
  }
1777
1797
  $.$mol_run_error = $mol_run_error;
1778
- const child_process = $node['child_process'];
1779
- $.$mol_run_spawn = child_process.spawn.bind(child_process);
1780
- $.$mol_run_spawn_sync = child_process.spawnSync.bind(child_process);
1781
- function $mol_run_async({ dir, timeout, command, env }) {
1782
- const args_raw = typeof command === 'string' ? command.split(' ') : command;
1783
- const [app, ...args] = args_raw;
1784
- if (!env?.MOL_RUN_ASYNC) {
1785
- this.$mol_log3_come({
1786
- place: '$mol_run_sync',
1798
+ $.$mol_run_spawn = (...args) => $node['child_process'].spawn(...args);
1799
+ $.$mol_run_spawn_sync = (...args) => $node['child_process'].spawnSync(...args);
1800
+ class $mol_run extends $mol_object {
1801
+ static async_enabled() {
1802
+ return Boolean(this.$.$mol_env()['MOL_RUN_ASYNC']);
1803
+ }
1804
+ static spawn(options) {
1805
+ const sync = !this.async_enabled() || !Boolean($mol_wire_auto());
1806
+ const env = options.env ?? this.$.$mol_env();
1807
+ return $mol_wire_sync(this).spawn_async({ ...options, sync, env });
1808
+ }
1809
+ static spawn_async({ dir, sync, timeout, command, env }) {
1810
+ const args_raw = typeof command === 'string' ? command.split(' ') : command;
1811
+ const [app, ...args] = args_raw;
1812
+ const opts = { shell: true, cwd: dir, env };
1813
+ const log_object = {
1814
+ place: `${this}.spawn()`,
1787
1815
  message: 'Run',
1788
1816
  command: args_raw.join(' '),
1789
1817
  dir: $node.path.relative('', dir),
1818
+ };
1819
+ if (sync) {
1820
+ this.$.$mol_log3_come({
1821
+ hint: 'Run inside fiber',
1822
+ ...log_object
1823
+ });
1824
+ let error;
1825
+ let res;
1826
+ try {
1827
+ res = this.$.$mol_run_spawn_sync(app, args, opts);
1828
+ error = res.error;
1829
+ }
1830
+ catch (err) {
1831
+ error = err;
1832
+ }
1833
+ if (!res || error || res.status) {
1834
+ throw new $mol_run_error(this.error_message(res), { ...log_object, status: res?.status, signal: res?.signal }, ...(error ? [error] : []));
1835
+ }
1836
+ return res;
1837
+ }
1838
+ let sub;
1839
+ try {
1840
+ sub = this.$.$mol_run_spawn(app, args, {
1841
+ ...opts,
1842
+ stdio: ['pipe', 'inherit', 'inherit'],
1843
+ });
1844
+ }
1845
+ catch (error) {
1846
+ throw new $mol_run_error(this.error_message(undefined), log_object, error);
1847
+ }
1848
+ const pid = sub.pid ?? 0;
1849
+ this.$.$mol_log3_come({
1850
+ ...log_object,
1851
+ pid,
1790
1852
  });
1791
- const res = this.$mol_run_spawn_sync(app, args, { shell: true, cwd: dir, env });
1792
- if (res.status)
1793
- $mol_fail(new Error(res.stderr.toString() || 'Exit(' + res.status + ')'));
1794
- return res;
1795
- }
1796
- const sub = this.$mol_run_spawn(app, args, {
1797
- shell: true,
1798
- cwd: dir,
1799
- env
1800
- });
1801
- this.$mol_log3_come({
1802
- place: '$mol_run_async',
1803
- pid: sub.pid,
1804
- message: 'Run',
1805
- command: args_raw.join(' '),
1806
- dir: $node.path.relative('', dir),
1807
- });
1808
- let killed = false;
1809
- let timer;
1810
- const std_data = [];
1811
- const error_data = [];
1812
- const add = (std_chunk, error_chunk) => {
1813
- if (std_chunk)
1814
- std_data.push(std_chunk);
1815
- if (error_chunk)
1816
- error_data.push(error_chunk);
1817
- if (!timeout)
1818
- return;
1819
- clearTimeout(timer);
1820
- timer = setTimeout(() => {
1821
- const signal = killed ? 'SIGKILL' : 'SIGTERM';
1822
- killed = true;
1823
- add();
1824
- sub.kill(signal);
1825
- }, timeout);
1826
- };
1827
- add();
1828
- sub.stdout?.on('data', data => add(data));
1829
- sub.stderr?.on('data', data => add(undefined, data));
1830
- const promise = new Promise((done, fail) => {
1831
- const close = (error, status = null, signal = null) => {
1832
- if (!timer && timeout)
1853
+ let timeout_kill = false;
1854
+ let timer;
1855
+ const std_data = [];
1856
+ const error_data = [];
1857
+ const add = (std_chunk, error_chunk) => {
1858
+ if (std_chunk)
1859
+ std_data.push(std_chunk);
1860
+ if (error_chunk)
1861
+ error_data.push(error_chunk);
1862
+ if (!timeout)
1833
1863
  return;
1834
1864
  clearTimeout(timer);
1835
- timer = undefined;
1836
- const res = {
1837
- pid: sub.pid,
1838
- status,
1839
- signal,
1840
- get stdout() { return Buffer.concat(std_data); },
1841
- get stderr() { return Buffer.concat(error_data); }
1842
- };
1843
- this.$mol_log3_done({
1844
- place: '$mol_run_async',
1845
- pid: sub.pid,
1846
- message: 'Run',
1847
- status,
1848
- command: args_raw.join(' '),
1849
- dir: $node.path.relative('', dir),
1850
- });
1851
- if (error || status || killed)
1852
- return fail(new $mol_run_error((res.stderr.toString() || res.stdout.toString() || 'Run error') + (killed ? ', timeout' : ''), { signal, timeout: killed }, ...error ? [error] : []));
1853
- done(res);
1865
+ timer = setTimeout(() => {
1866
+ const signal = timeout_kill ? 'SIGKILL' : 'SIGTERM';
1867
+ timeout_kill = true;
1868
+ add();
1869
+ sub.kill(signal);
1870
+ }, timeout);
1854
1871
  };
1855
- sub.on('disconnect', () => close(new Error('Disconnected')));
1856
- sub.on('error', err => close(err));
1857
- sub.on('exit', (status, signal) => close(null, status, signal));
1858
- });
1859
- return Object.assign(promise, { destructor: () => {
1860
- clearTimeout(timer);
1861
- sub.kill('SIGKILL');
1862
- } });
1863
- }
1864
- $.$mol_run_async = $mol_run_async;
1865
- function $mol_run(options) {
1866
- if (!options.env)
1867
- options = { ...options, env: this.$mol_env() };
1868
- return $mol_wire_sync(this).$mol_run_async(options);
1872
+ add();
1873
+ sub.stdout?.on('data', data => add(data));
1874
+ sub.stderr?.on('data', data => add(undefined, data));
1875
+ const result_promise = new Promise((done, fail) => {
1876
+ const close = (error, status = null, signal = null) => {
1877
+ if (!timer && timeout)
1878
+ return;
1879
+ clearTimeout(timer);
1880
+ timer = undefined;
1881
+ const res = {
1882
+ pid,
1883
+ signal,
1884
+ get stdout() { return Buffer.concat(std_data); },
1885
+ get stderr() { return Buffer.concat(error_data); }
1886
+ };
1887
+ if (error || status || timeout_kill)
1888
+ return fail(new $mol_run_error(this.error_message(res) + (timeout_kill ? ', timeout' : ''), { ...log_object, pid, status, signal, timeout_kill }, ...error ? [error] : []));
1889
+ this.$.$mol_log3_done({
1890
+ ...log_object,
1891
+ pid,
1892
+ });
1893
+ done(res);
1894
+ };
1895
+ sub.on('disconnect', () => close(new Error('Disconnected')));
1896
+ sub.on('error', err => close(err));
1897
+ sub.on('exit', (status, signal) => close(null, status, signal));
1898
+ });
1899
+ return Object.assign(result_promise, { destructor: () => {
1900
+ clearTimeout(timer);
1901
+ sub.kill('SIGKILL');
1902
+ } });
1903
+ }
1904
+ static error_message(res) {
1905
+ return res?.stderr.toString() || res?.stdout.toString() || 'Run error';
1906
+ }
1869
1907
  }
1870
1908
  $.$mol_run = $mol_run;
1871
1909
  })($ || ($ = {}));
@@ -1875,7 +1913,7 @@ var $;
1875
1913
  var $;
1876
1914
  (function ($) {
1877
1915
  function $mol_exec(dir, command, ...args) {
1878
- return this.$mol_run({ command: [command, ...args], dir });
1916
+ return this.$mol_run.spawn({ command: [command, ...args], dir });
1879
1917
  }
1880
1918
  $.$mol_exec = $mol_exec;
1881
1919
  })($ || ($ = {}));
@@ -2796,26 +2834,6 @@ var $;
2796
2834
  $.$mol_mem_key = $mol_wire_plex;
2797
2835
  })($ || ($ = {}));
2798
2836
 
2799
- ;
2800
- "use strict";
2801
- var $;
2802
- (function ($_1) {
2803
- let $$;
2804
- (function ($$) {
2805
- let $;
2806
- })($$ = $_1.$$ || ($_1.$$ = {}));
2807
- $_1.$mol_object_field = Symbol('$mol_object_field');
2808
- class $mol_object extends $mol_object2 {
2809
- static make(config) {
2810
- return super.create(obj => {
2811
- for (let key in config)
2812
- obj[key] = config[key];
2813
- });
2814
- }
2815
- }
2816
- $_1.$mol_object = $mol_object;
2817
- })($ || ($ = {}));
2818
-
2819
2837
  ;
2820
2838
  "use strict";
2821
2839
  var $;
@@ -2990,12 +3008,185 @@ var $;
2990
3008
  "use strict";
2991
3009
  var $;
2992
3010
  (function ($) {
2993
- class $mol_file_not_found extends Error {
3011
+ function $mol_const(value) {
3012
+ const getter = (() => value);
3013
+ getter['()'] = value;
3014
+ getter[Symbol.toStringTag] = value;
3015
+ getter[$mol_dev_format_head] = () => $mol_dev_format_span({}, '()=> ', $mol_dev_format_auto(value));
3016
+ return getter;
3017
+ }
3018
+ $.$mol_const = $mol_const;
3019
+ })($ || ($ = {}));
3020
+
3021
+ ;
3022
+ "use strict";
3023
+ var $;
3024
+ (function ($) {
3025
+ $.$mol_action = $mol_wire_method;
3026
+ })($ || ($ = {}));
3027
+
3028
+ ;
3029
+ "use strict";
3030
+ var $;
3031
+ (function ($) {
3032
+ function $mol_wire_async(obj) {
3033
+ let fiber;
3034
+ const temp = $mol_wire_task.getter(obj);
3035
+ return new Proxy(obj, {
3036
+ get(obj, field) {
3037
+ const val = obj[field];
3038
+ if (typeof val !== 'function')
3039
+ return val;
3040
+ let fiber;
3041
+ const temp = $mol_wire_task.getter(val);
3042
+ return function $mol_wire_async(...args) {
3043
+ fiber?.destructor();
3044
+ fiber = temp(obj, args);
3045
+ return fiber.async();
3046
+ };
3047
+ },
3048
+ apply(obj, self, args) {
3049
+ fiber?.destructor();
3050
+ fiber = temp(self, args);
3051
+ return fiber.async();
3052
+ },
3053
+ });
3054
+ }
3055
+ $.$mol_wire_async = $mol_wire_async;
3056
+ })($ || ($ = {}));
3057
+
3058
+ ;
3059
+ "use strict";
3060
+ var $;
3061
+ (function ($) {
3062
+ class $mol_lock extends $mol_object {
3063
+ promise = null;
3064
+ async wait() {
3065
+ let next = () => { };
3066
+ let destructed = false;
3067
+ const task = $mol_wire_auto();
3068
+ if (!task)
3069
+ return next;
3070
+ const destructor = task.destructor.bind(task);
3071
+ task.destructor = () => {
3072
+ destructor();
3073
+ destructed = true;
3074
+ next();
3075
+ };
3076
+ let promise;
3077
+ do {
3078
+ promise = this.promise;
3079
+ await promise;
3080
+ if (destructed)
3081
+ return next;
3082
+ } while (promise !== this.promise);
3083
+ this.promise = new Promise(done => { next = done; });
3084
+ return next;
3085
+ }
3086
+ grab() { return $mol_wire_sync(this).wait(); }
3087
+ }
3088
+ $.$mol_lock = $mol_lock;
3089
+ })($ || ($ = {}));
3090
+
3091
+ ;
3092
+ "use strict";
3093
+ var $;
3094
+ (function ($) {
3095
+ function $mol_compare_array(a, b) {
3096
+ if (a === b)
3097
+ return true;
3098
+ if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
3099
+ return false;
3100
+ if (a.length !== b.length)
3101
+ return false;
3102
+ for (let i = 0; i < a.length; i++)
3103
+ if (a[i] !== b[i])
3104
+ return false;
3105
+ return true;
3106
+ }
3107
+ $.$mol_compare_array = $mol_compare_array;
3108
+ })($ || ($ = {}));
3109
+
3110
+ ;
3111
+ "use strict";
3112
+ var $;
3113
+ (function ($) {
3114
+ class $mol_file_transaction extends $mol_object {
3115
+ path() { return ''; }
3116
+ modes() { return []; }
3117
+ write(options) {
3118
+ return 0;
3119
+ }
3120
+ read() {
3121
+ return new Uint8Array();
3122
+ }
3123
+ truncate(size) { }
3124
+ close() { }
3125
+ destructor() {
3126
+ this.close();
3127
+ }
3128
+ }
3129
+ $.$mol_file_transaction = $mol_file_transaction;
3130
+ })($ || ($ = {}));
3131
+
3132
+ ;
3133
+ "use strict";
3134
+ var $;
3135
+ (function ($) {
3136
+ let file_modes;
3137
+ (function (file_modes) {
3138
+ file_modes[file_modes["create"] = $node.fs.constants.O_CREAT] = "create";
3139
+ file_modes[file_modes["exists_truncate"] = $node.fs.constants.O_TRUNC] = "exists_truncate";
3140
+ file_modes[file_modes["exists_fail"] = $node.fs.constants.O_EXCL] = "exists_fail";
3141
+ file_modes[file_modes["read_only"] = $node.fs.constants.O_RDONLY] = "read_only";
3142
+ file_modes[file_modes["write_only"] = $node.fs.constants.O_WRONLY] = "write_only";
3143
+ file_modes[file_modes["read_write"] = $node.fs.constants.O_RDWR] = "read_write";
3144
+ file_modes[file_modes["append"] = $node.fs.constants.O_APPEND] = "append";
3145
+ })(file_modes || (file_modes = {}));
3146
+ function mode_mask(modes) {
3147
+ return modes.reduce((res, mode) => res | file_modes[mode], 0);
3148
+ }
3149
+ class $mol_file_transaction_node extends $mol_file_transaction {
3150
+ descr() {
3151
+ $mol_wire_solid();
3152
+ return $node.fs.openSync(this.path(), mode_mask(this.modes()));
3153
+ }
3154
+ write({ buffer, offset = 0, length, position = null }) {
3155
+ if (Array.isArray(buffer)) {
3156
+ return $node.fs.writevSync(this.descr(), buffer, position ?? undefined);
3157
+ }
3158
+ if (typeof buffer === 'string') {
3159
+ return $node.fs.writeSync(this.descr(), buffer, position);
3160
+ }
3161
+ length = length ?? buffer.byteLength;
3162
+ return $node.fs.writeSync(this.descr(), buffer, offset, length, position);
3163
+ }
3164
+ truncate(size) {
3165
+ $node.fs.ftruncateSync(this.descr());
3166
+ }
3167
+ read() {
3168
+ return $mol_file_node_buffer_normalize($node.fs.readFileSync(this.descr()));
3169
+ }
3170
+ close() {
3171
+ $node.fs.closeSync(this.descr());
3172
+ }
2994
3173
  }
2995
- $.$mol_file_not_found = $mol_file_not_found;
2996
- class $mol_file extends $mol_object {
3174
+ __decorate([
3175
+ $mol_mem
3176
+ ], $mol_file_transaction_node.prototype, "descr", null);
3177
+ $.$mol_file_transaction_node = $mol_file_transaction_node;
3178
+ $.$mol_file_transaction = $mol_file_transaction_node;
3179
+ })($ || ($ = {}));
3180
+
3181
+ ;
3182
+ "use strict";
3183
+ var $;
3184
+ (function ($) {
3185
+ class $mol_file_base extends $mol_object {
2997
3186
  static absolute(path) {
2998
- throw new Error('Not implemented yet');
3187
+ return this.make({
3188
+ path: $mol_const(path)
3189
+ });
2999
3190
  }
3000
3191
  static relative(path) {
3001
3192
  throw new Error('Not implemented yet');
@@ -3007,27 +3198,164 @@ var $;
3007
3198
  parent() {
3008
3199
  return this.resolve('..');
3009
3200
  }
3010
- reset() {
3011
- try {
3012
- this.stat(null);
3201
+ exists_cut() { return this.exists(); }
3202
+ root() {
3203
+ const path = this.path();
3204
+ const base = this.constructor.base;
3205
+ return base.startsWith(path) || this == this.parent();
3206
+ }
3207
+ stat(next, virt) {
3208
+ const path = this.path();
3209
+ const parent = this.parent();
3210
+ if (!this.root()) {
3211
+ parent.version();
3013
3212
  }
3014
- catch (error) {
3015
- if (error instanceof $mol_file_not_found)
3213
+ parent.watcher();
3214
+ if (virt)
3215
+ return next ?? null;
3216
+ return next ?? this.info(path);
3217
+ }
3218
+ static changed = new Set;
3219
+ static frame = null;
3220
+ static changed_add(type, path) {
3221
+ if (/([\/\\]\.|___$)/.test(path))
3222
+ return;
3223
+ const file = this.relative(path.at(-1) === '/' ? path.slice(0, -1) : path);
3224
+ this.changed.add(file);
3225
+ if (!this.watching)
3226
+ return;
3227
+ this.frame?.destructor();
3228
+ this.frame = new this.$.$mol_after_timeout(this.watch_debounce(), () => {
3229
+ if (!this.watching)
3016
3230
  return;
3017
- return $mol_fail_hidden(error);
3231
+ this.watching = false;
3232
+ $mol_wire_async(this).flush();
3233
+ });
3234
+ }
3235
+ static watch_debounce() { return 500; }
3236
+ static flush() {
3237
+ for (const file of this.changed) {
3238
+ const parent = file.parent();
3239
+ try {
3240
+ if ($mol_wire_probe(() => parent.sub()))
3241
+ parent.sub(null);
3242
+ file.reset();
3243
+ }
3244
+ catch (error) {
3245
+ if ($mol_fail_catch(error))
3246
+ $mol_fail_log(error);
3247
+ }
3248
+ }
3249
+ this.changed.clear();
3250
+ this.watching = true;
3251
+ }
3252
+ static watching = true;
3253
+ static lock = new $mol_lock;
3254
+ static watch_off(path) {
3255
+ this.watching = false;
3256
+ this.flush();
3257
+ this.watching = false;
3258
+ this.changed.add(this.absolute(path));
3259
+ }
3260
+ static unwatched(side_effect, affected_dir) {
3261
+ const unlock = this.lock.grab();
3262
+ this.watch_off(affected_dir);
3263
+ try {
3264
+ const result = side_effect();
3265
+ this.flush();
3266
+ unlock();
3267
+ return result;
3268
+ }
3269
+ catch (e) {
3270
+ if (!$mol_promise_like(e)) {
3271
+ this.flush();
3272
+ unlock();
3273
+ }
3274
+ $mol_fail_hidden(e);
3018
3275
  }
3019
3276
  }
3277
+ reset() {
3278
+ this.stat(null);
3279
+ }
3280
+ modified() { return this.stat()?.mtime ?? null; }
3020
3281
  version() {
3021
- return this.stat()?.mtime.getTime().toString(36).toUpperCase() ?? '';
3282
+ const next = this.stat()?.mtime.getTime().toString(36).toUpperCase() ?? '';
3283
+ return next;
3284
+ }
3285
+ info(path) { return null; }
3286
+ ensure() { }
3287
+ drop() { }
3288
+ copy(to) { }
3289
+ read() { return new Uint8Array; }
3290
+ write(buffer) { }
3291
+ kids() {
3292
+ return [];
3293
+ }
3294
+ readable(opts) {
3295
+ return new ReadableStream;
3296
+ }
3297
+ writable(opts) {
3298
+ return new WritableStream;
3299
+ }
3300
+ buffer(next) {
3301
+ let readed = new Uint8Array();
3302
+ if (next === undefined) {
3303
+ if (this.version())
3304
+ readed = this.read();
3305
+ }
3306
+ const prev = $mol_mem_cached(() => this.buffer());
3307
+ const changed = prev === undefined || !$mol_compare_array(prev, next ?? readed);
3308
+ if (prev !== undefined && changed) {
3309
+ this.$.$mol_log3_rise({
3310
+ place: `$mol_file_node.buffer()`,
3311
+ message: 'Changed',
3312
+ path: this.relate(),
3313
+ });
3314
+ }
3315
+ if (next === undefined)
3316
+ return changed ? readed : prev;
3317
+ if (!changed && this.exists())
3318
+ return prev;
3319
+ this.parent().exists(true);
3320
+ this.stat(this.stat_make(next.length), 'virt');
3321
+ this.write(next);
3322
+ return next;
3323
+ }
3324
+ stat_make(size) {
3325
+ const now = new Date();
3326
+ return {
3327
+ type: 'file',
3328
+ size,
3329
+ atime: now,
3330
+ mtime: now,
3331
+ ctime: now,
3332
+ };
3333
+ }
3334
+ clone(to) {
3335
+ if (!this.exists())
3336
+ return null;
3337
+ const target = this.constructor.absolute(to);
3338
+ try {
3339
+ this.version();
3340
+ target.parent().exists(true);
3341
+ this.copy(to);
3342
+ target.reset();
3343
+ return target;
3344
+ }
3345
+ catch (error) {
3346
+ if ($mol_fail_catch(error)) {
3347
+ console.error(error);
3348
+ }
3349
+ }
3350
+ return null;
3022
3351
  }
3023
3352
  watcher() {
3024
- console.warn('$mol_file_web.watcher() not implemented');
3025
3353
  return {
3026
3354
  destructor() { }
3027
3355
  };
3028
3356
  }
3029
3357
  exists(next) {
3030
- let exists = Boolean(this.stat());
3358
+ const exists = Boolean(this.stat());
3031
3359
  if (next === undefined)
3032
3360
  return exists;
3033
3361
  if (next === exists)
@@ -3053,26 +3381,40 @@ var $;
3053
3381
  return match ? match[1].substring(1) : '';
3054
3382
  }
3055
3383
  text(next, virt) {
3384
+ if (next !== undefined)
3385
+ this.exists();
3386
+ return this.text_int(next, virt);
3387
+ }
3388
+ text_int(next, virt) {
3056
3389
  if (virt) {
3057
- const now = new Date;
3058
- this.stat({
3059
- type: 'file',
3060
- size: 0,
3061
- atime: now,
3062
- mtime: now,
3063
- ctime: now,
3064
- }, 'virt');
3390
+ this.stat(this.stat_make(0), 'virt');
3065
3391
  return next;
3066
3392
  }
3067
3393
  if (next === undefined) {
3068
- return $mol_charset_decode(this.buffer(undefined));
3394
+ return $mol_charset_decode(this.buffer());
3069
3395
  }
3070
3396
  else {
3071
- const buffer = next === undefined ? undefined : $mol_charset_encode(next);
3397
+ const buffer = $mol_charset_encode(next);
3072
3398
  this.buffer(buffer);
3073
3399
  return next;
3074
3400
  }
3075
3401
  }
3402
+ sub(reset) {
3403
+ if (!this.exists())
3404
+ return [];
3405
+ if (this.type() !== 'dir')
3406
+ return [];
3407
+ this.version();
3408
+ return this.kids().filter(file => file.exists());
3409
+ }
3410
+ resolve(path) {
3411
+ throw new Error('implement');
3412
+ }
3413
+ relate(base = this.constructor.relative('.')) {
3414
+ const base_path = base.path();
3415
+ const path = this.path();
3416
+ return path.startsWith(base_path) ? path.slice(base_path.length) : path;
3417
+ }
3076
3418
  find(include, exclude) {
3077
3419
  const found = [];
3078
3420
  const sub = this.sub();
@@ -3096,63 +3438,80 @@ var $;
3096
3438
  default: return 0;
3097
3439
  }
3098
3440
  }
3099
- open(...modes) {
3100
- return 0;
3101
- }
3102
3441
  toJSON() {
3103
3442
  return this.path();
3104
3443
  }
3444
+ open(...modes) {
3445
+ return this.$.$mol_file_transaction.make({
3446
+ path: () => this.path(),
3447
+ modes: () => modes
3448
+ });
3449
+ }
3105
3450
  }
3451
+ __decorate([
3452
+ $mol_action
3453
+ ], $mol_file_base.prototype, "exists_cut", null);
3454
+ __decorate([
3455
+ $mol_mem
3456
+ ], $mol_file_base.prototype, "stat", null);
3106
3457
  __decorate([
3107
3458
  $mol_mem
3108
- ], $mol_file.prototype, "exists", null);
3459
+ ], $mol_file_base.prototype, "modified", null);
3109
3460
  __decorate([
3110
3461
  $mol_mem
3111
- ], $mol_file.prototype, "text", null);
3462
+ ], $mol_file_base.prototype, "version", null);
3112
3463
  __decorate([
3113
3464
  $mol_mem_key
3114
- ], $mol_file, "absolute", null);
3115
- $.$mol_file = $mol_file;
3116
- })($ || ($ = {}));
3117
-
3118
- ;
3119
- "use strict";
3120
- var $;
3121
- (function ($) {
3122
- function $mol_const(value) {
3123
- const getter = (() => value);
3124
- getter['()'] = value;
3125
- getter[Symbol.toStringTag] = value;
3126
- getter[$mol_dev_format_head] = () => $mol_dev_format_span({}, '()=> ', $mol_dev_format_auto(value));
3127
- return getter;
3128
- }
3129
- $.$mol_const = $mol_const;
3130
- })($ || ($ = {}));
3131
-
3132
- ;
3133
- "use strict";
3134
- var $;
3135
- (function ($) {
3136
- $.$mol_action = $mol_wire_method;
3465
+ ], $mol_file_base.prototype, "readable", null);
3466
+ __decorate([
3467
+ $mol_mem_key
3468
+ ], $mol_file_base.prototype, "writable", null);
3469
+ __decorate([
3470
+ $mol_mem
3471
+ ], $mol_file_base.prototype, "buffer", null);
3472
+ __decorate([
3473
+ $mol_action
3474
+ ], $mol_file_base.prototype, "stat_make", null);
3475
+ __decorate([
3476
+ $mol_mem_key
3477
+ ], $mol_file_base.prototype, "clone", null);
3478
+ __decorate([
3479
+ $mol_mem
3480
+ ], $mol_file_base.prototype, "exists", null);
3481
+ __decorate([
3482
+ $mol_mem
3483
+ ], $mol_file_base.prototype, "type", null);
3484
+ __decorate([
3485
+ $mol_mem
3486
+ ], $mol_file_base.prototype, "text_int", null);
3487
+ __decorate([
3488
+ $mol_mem
3489
+ ], $mol_file_base.prototype, "sub", null);
3490
+ __decorate([
3491
+ $mol_mem
3492
+ ], $mol_file_base.prototype, "size", null);
3493
+ __decorate([
3494
+ $mol_action
3495
+ ], $mol_file_base.prototype, "open", null);
3496
+ __decorate([
3497
+ $mol_mem_key
3498
+ ], $mol_file_base, "absolute", null);
3499
+ __decorate([
3500
+ $mol_action
3501
+ ], $mol_file_base, "flush", null);
3502
+ __decorate([
3503
+ $mol_action
3504
+ ], $mol_file_base, "watch_off", null);
3505
+ $.$mol_file_base = $mol_file_base;
3137
3506
  })($ || ($ = {}));
3138
3507
 
3139
3508
  ;
3140
3509
  "use strict";
3141
3510
  var $;
3142
3511
  (function ($) {
3143
- function $mol_compare_array(a, b) {
3144
- if (a === b)
3145
- return true;
3146
- if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
3147
- return false;
3148
- if (a.length !== b.length)
3149
- return false;
3150
- for (let i = 0; i < a.length; i++)
3151
- if (a[i] !== b[i])
3152
- return false;
3153
- return true;
3512
+ class $mol_file extends $mol_file_base {
3154
3513
  }
3155
- $.$mol_compare_array = $mol_compare_array;
3514
+ $.$mol_file = $mol_file;
3156
3515
  })($ || ($ = {}));
3157
3516
 
3158
3517
  ;
@@ -3179,186 +3538,183 @@ var $;
3179
3538
  ctime: stat.ctime
3180
3539
  };
3181
3540
  }
3182
- function buffer_normalize(buf) {
3541
+ function $mol_file_node_buffer_normalize(buf) {
3183
3542
  return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
3184
3543
  }
3185
- let $mol_file_mode_open;
3186
- (function ($mol_file_mode_open) {
3187
- $mol_file_mode_open[$mol_file_mode_open["create"] = $node.fs.constants.O_CREAT] = "create";
3188
- $mol_file_mode_open[$mol_file_mode_open["exists_truncate"] = $node.fs.constants.O_TRUNC] = "exists_truncate";
3189
- $mol_file_mode_open[$mol_file_mode_open["exists_fail"] = $node.fs.constants.O_EXCL] = "exists_fail";
3190
- $mol_file_mode_open[$mol_file_mode_open["read_only"] = $node.fs.constants.O_RDONLY] = "read_only";
3191
- $mol_file_mode_open[$mol_file_mode_open["write_only"] = $node.fs.constants.O_WRONLY] = "write_only";
3192
- $mol_file_mode_open[$mol_file_mode_open["read_write"] = $node.fs.constants.O_RDWR] = "read_write";
3193
- $mol_file_mode_open[$mol_file_mode_open["append"] = $node.fs.constants.O_APPEND] = "append";
3194
- })($mol_file_mode_open = $.$mol_file_mode_open || ($.$mol_file_mode_open = {}));
3544
+ $.$mol_file_node_buffer_normalize = $mol_file_node_buffer_normalize;
3195
3545
  class $mol_file_node extends $mol_file {
3196
- static absolute(path) {
3197
- return this.make({
3198
- path: $mol_const(path)
3199
- });
3200
- }
3201
3546
  static relative(path) {
3202
3547
  return this.absolute($node.path.resolve(this.base, path).replace(/\\/g, '/'));
3203
3548
  }
3204
- watcher() {
3205
- if (/\/\./.test(this.path()))
3206
- return { destructor() { } };
3207
- const watcher = $node.chokidar.watch(this.path(), {
3208
- persistent: true,
3209
- ignored: path => /([\/\\]\.|___$)/.test(path),
3210
- depth: 0,
3211
- ignoreInitial: true,
3212
- awaitWriteFinish: {
3213
- stabilityThreshold: 100,
3214
- },
3215
- });
3216
- watcher
3217
- .on('all', (type, path) => {
3218
- const file = $mol_file.relative(path.replace(/\\/g, '/'));
3219
- file.reset();
3220
- if (type === 'change') {
3221
- this.stat(null);
3222
- }
3223
- else {
3224
- file.parent().reset();
3549
+ watcher(reset) {
3550
+ const path = this.path();
3551
+ const root = this.root();
3552
+ if (!root && !this.exists())
3553
+ return super.watcher();
3554
+ let watcher;
3555
+ try {
3556
+ watcher = $node.fs.watch(path);
3557
+ }
3558
+ catch (error) {
3559
+ if (!(error instanceof Error))
3560
+ error = new Error('Unknown watch error', { cause: error });
3561
+ error.message += '\n' + path;
3562
+ if (root || error.code !== 'ENOENT') {
3563
+ this.$.$mol_fail_log(error);
3225
3564
  }
3226
- })
3227
- .on('error', $mol_fail_log);
3565
+ return super.watcher();
3566
+ }
3567
+ watcher.on('change', (type, name) => {
3568
+ if (!name)
3569
+ return;
3570
+ const path = $node.path.join(this.path(), name.toString());
3571
+ this.constructor.changed_add(type, path);
3572
+ });
3573
+ watcher.on('error', e => this.$.$mol_fail_log(e));
3574
+ let destructed = false;
3575
+ watcher.on('close', () => {
3576
+ if (!destructed)
3577
+ setTimeout(() => $mol_wire_async(this).watcher(null), 500);
3578
+ });
3228
3579
  return {
3229
3580
  destructor() {
3581
+ destructed = true;
3230
3582
  watcher.close();
3231
3583
  }
3232
3584
  };
3233
3585
  }
3234
- stat(next, virt) {
3235
- let stat = next;
3236
- const path = this.path();
3237
- this.parent().watcher();
3238
- if (virt)
3239
- return next;
3586
+ info(path) {
3240
3587
  try {
3241
- stat = next ?? stat_convert($node.fs.statSync(path, { throwIfNoEntry: false }));
3588
+ return stat_convert($node.fs.statSync(path));
3242
3589
  }
3243
3590
  catch (error) {
3244
- if (error.code === 'ENOENT')
3245
- error = new $mol_file_not_found(`File not found`);
3246
- error.message += '\n' + path;
3247
- return this.$.$mol_fail_hidden(error);
3591
+ if (this.$.$mol_fail_catch(error)) {
3592
+ if (error.code === 'ENOENT')
3593
+ return null;
3594
+ error.message += '\n' + path;
3595
+ this.$.$mol_fail_hidden(error);
3596
+ }
3248
3597
  }
3249
- return stat;
3598
+ return null;
3250
3599
  }
3251
3600
  ensure() {
3252
3601
  const path = this.path();
3253
3602
  try {
3254
- $node.fs.mkdirSync(path);
3603
+ $node.fs.mkdirSync(path, { recursive: true });
3604
+ return null;
3255
3605
  }
3256
3606
  catch (e) {
3257
- e.message += '\n' + path;
3258
- this.$.$mol_fail_hidden(e);
3607
+ if (this.$.$mol_fail_catch(e)) {
3608
+ if (e.code === 'EEXIST')
3609
+ return null;
3610
+ e.message += '\n' + path;
3611
+ this.$.$mol_fail_hidden(e);
3612
+ }
3259
3613
  }
3260
3614
  }
3615
+ copy(to) {
3616
+ $node.fs.copyFileSync(this.path(), to);
3617
+ }
3261
3618
  drop() {
3262
3619
  $node.fs.unlinkSync(this.path());
3263
3620
  }
3264
- buffer(next) {
3621
+ read() {
3265
3622
  const path = this.path();
3266
- if (next === undefined) {
3267
- if (!this.stat())
3268
- return new Uint8Array;
3269
- try {
3270
- const prev = $mol_mem_cached(() => this.buffer());
3271
- next = buffer_normalize($node.fs.readFileSync(path));
3272
- if (prev !== undefined && !$mol_compare_array(prev, next)) {
3273
- this.$.$mol_log3_rise({
3274
- place: `$mol_file_node..buffer()`,
3275
- message: 'Changed',
3276
- path: this.relate(),
3277
- });
3278
- }
3279
- return next;
3280
- }
3281
- catch (error) {
3623
+ try {
3624
+ return $mol_file_node_buffer_normalize($node.fs.readFileSync(path));
3625
+ }
3626
+ catch (error) {
3627
+ if (!$mol_promise_like(error)) {
3282
3628
  error.message += '\n' + path;
3283
- return this.$.$mol_fail_hidden(error);
3284
3629
  }
3630
+ $mol_fail_hidden(error);
3285
3631
  }
3286
- this.parent().exists(true);
3287
- const now = new Date;
3288
- this.stat({
3289
- type: 'file',
3290
- size: next.length,
3291
- atime: now,
3292
- mtime: now,
3293
- ctime: now,
3294
- }, 'virt');
3632
+ }
3633
+ write(buffer) {
3634
+ const path = this.path();
3295
3635
  try {
3296
- $node.fs.writeFileSync(path, next);
3636
+ $node.fs.writeFileSync(path, buffer);
3297
3637
  }
3298
3638
  catch (error) {
3299
- error.message += '\n' + path;
3639
+ if (this.$.$mol_fail_catch(error)) {
3640
+ error.message += '\n' + path;
3641
+ }
3300
3642
  return this.$.$mol_fail_hidden(error);
3301
3643
  }
3302
- return next;
3303
3644
  }
3304
- sub() {
3305
- if (!this.exists())
3306
- return [];
3307
- if (this.type() !== 'dir')
3308
- return [];
3645
+ kids() {
3309
3646
  const path = this.path();
3310
- this.stat();
3311
3647
  try {
3312
- return $node.fs.readdirSync(path)
3648
+ const kids = $node.fs.readdirSync(path)
3313
3649
  .filter(name => !/^\.+$/.test(name))
3314
3650
  .map(name => this.resolve(name));
3651
+ return kids;
3315
3652
  }
3316
3653
  catch (e) {
3317
- e.message += '\n' + path;
3318
- return this.$.$mol_fail_hidden(e);
3654
+ if (this.$.$mol_fail_catch(e)) {
3655
+ if (e.code === 'ENOENT')
3656
+ return [];
3657
+ e.message += '\n' + path;
3658
+ }
3659
+ $mol_fail_hidden(e);
3319
3660
  }
3320
3661
  }
3321
3662
  resolve(path) {
3322
- return this.constructor.relative($node.path.join(this.path(), path));
3663
+ return this.constructor
3664
+ .relative($node.path.join(this.path(), path));
3323
3665
  }
3324
3666
  relate(base = this.constructor.relative('.')) {
3325
3667
  return $node.path.relative(base.path(), this.path()).replace(/\\/g, '/');
3326
3668
  }
3327
- append(next) {
3328
- const path = this.path();
3329
- try {
3330
- $node.fs.appendFileSync(path, next);
3331
- }
3332
- catch (e) {
3333
- e.message += '\n' + path;
3334
- return this.$.$mol_fail_hidden(e);
3335
- }
3336
- }
3337
- open(...modes) {
3338
- return $node.fs.openSync(this.path(), modes.reduce((res, mode) => res | $mol_file_mode_open[mode], 0));
3669
+ readable(opts) {
3670
+ const { Readable } = $node['node:stream'];
3671
+ const stream = $node.fs.createReadStream(this.path(), {
3672
+ flags: 'r',
3673
+ autoClose: true,
3674
+ start: opts?.start,
3675
+ end: opts?.end,
3676
+ encoding: 'binary',
3677
+ });
3678
+ return Readable.toWeb(stream);
3679
+ }
3680
+ writable(opts) {
3681
+ const { Writable } = $node['node:stream'];
3682
+ const stream = $node.fs.createWriteStream(this.path(), {
3683
+ flags: 'w+',
3684
+ autoClose: true,
3685
+ start: opts?.start,
3686
+ encoding: 'binary',
3687
+ });
3688
+ return Writable.toWeb(stream);
3339
3689
  }
3340
3690
  }
3341
3691
  __decorate([
3342
3692
  $mol_mem
3343
3693
  ], $mol_file_node.prototype, "watcher", null);
3344
3694
  __decorate([
3345
- $mol_mem
3346
- ], $mol_file_node.prototype, "stat", null);
3695
+ $mol_action
3696
+ ], $mol_file_node.prototype, "info", null);
3347
3697
  __decorate([
3348
- $mol_mem
3698
+ $mol_action
3349
3699
  ], $mol_file_node.prototype, "ensure", null);
3700
+ __decorate([
3701
+ $mol_action
3702
+ ], $mol_file_node.prototype, "copy", null);
3350
3703
  __decorate([
3351
3704
  $mol_action
3352
3705
  ], $mol_file_node.prototype, "drop", null);
3353
3706
  __decorate([
3354
- $mol_mem
3355
- ], $mol_file_node.prototype, "buffer", null);
3707
+ $mol_action
3708
+ ], $mol_file_node.prototype, "read", null);
3356
3709
  __decorate([
3357
- $mol_mem
3358
- ], $mol_file_node.prototype, "sub", null);
3710
+ $mol_action
3711
+ ], $mol_file_node.prototype, "write", null);
3359
3712
  __decorate([
3360
3713
  $mol_mem_key
3361
- ], $mol_file_node, "absolute", null);
3714
+ ], $mol_file_node.prototype, "readable", null);
3715
+ __decorate([
3716
+ $mol_mem
3717
+ ], $mol_file_node.prototype, "writable", null);
3362
3718
  $.$mol_file_node = $mol_file_node;
3363
3719
  $.$mol_file = $mol_file_node;
3364
3720
  })($ || ($ = {}));
@@ -4445,36 +4801,6 @@ var $;
4445
4801
  ];
4446
4802
  })($ || ($ = {}));
4447
4803
 
4448
- ;
4449
- "use strict";
4450
- var $;
4451
- (function ($) {
4452
- function $mol_wire_async(obj) {
4453
- let fiber;
4454
- const temp = $mol_wire_task.getter(obj);
4455
- return new Proxy(obj, {
4456
- get(obj, field) {
4457
- const val = obj[field];
4458
- if (typeof val !== 'function')
4459
- return val;
4460
- let fiber;
4461
- const temp = $mol_wire_task.getter(val);
4462
- return function $mol_wire_async(...args) {
4463
- fiber?.destructor();
4464
- fiber = temp(obj, args);
4465
- return fiber.async();
4466
- };
4467
- },
4468
- apply(obj, self, args) {
4469
- fiber?.destructor();
4470
- fiber = temp(self, args);
4471
- return fiber.async();
4472
- },
4473
- });
4474
- }
4475
- $.$mol_wire_async = $mol_wire_async;
4476
- })($ || ($ = {}));
4477
-
4478
4804
  ;
4479
4805
  "use strict";
4480
4806
  var $;