mol_dump_lib 0.0.540 → 0.0.542

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.js CHANGED
@@ -2149,97 +2149,115 @@ var $;
2149
2149
  class $mol_run_error extends $mol_error_mix {
2150
2150
  }
2151
2151
  $.$mol_run_error = $mol_run_error;
2152
- const child_process = $node['child_process'];
2153
- $.$mol_run_spawn = child_process.spawn.bind(child_process);
2154
- $.$mol_run_spawn_sync = child_process.spawnSync.bind(child_process);
2155
- function $mol_run_async({ dir, timeout, command, env }) {
2156
- const args_raw = typeof command === 'string' ? command.split(' ') : command;
2157
- const [app, ...args] = args_raw;
2158
- if (!env?.MOL_RUN_ASYNC) {
2159
- this.$mol_log3_come({
2160
- place: '$mol_run_sync',
2152
+ $.$mol_run_spawn = (...args) => $node['child_process'].spawn(...args);
2153
+ $.$mol_run_spawn_sync = (...args) => $node['child_process'].spawnSync(...args);
2154
+ class $mol_run extends $mol_object {
2155
+ static async_enabled() {
2156
+ return Boolean(this.$.$mol_env()['MOL_RUN_ASYNC']);
2157
+ }
2158
+ static spawn(options) {
2159
+ const sync = !this.async_enabled() || !Boolean($mol_wire_auto());
2160
+ const env = options.env ?? this.$.$mol_env();
2161
+ return $mol_wire_sync(this).spawn_async({ ...options, sync, env });
2162
+ }
2163
+ static spawn_async({ dir, sync, timeout, command, env }) {
2164
+ const args_raw = typeof command === 'string' ? command.split(' ') : command;
2165
+ const [app, ...args] = args_raw;
2166
+ const opts = { shell: true, cwd: dir, env };
2167
+ const log_object = {
2168
+ place: `${this}.spawn()`,
2161
2169
  message: 'Run',
2162
2170
  command: args_raw.join(' '),
2163
2171
  dir: $node.path.relative('', dir),
2172
+ };
2173
+ if (sync) {
2174
+ this.$.$mol_log3_come({
2175
+ hint: 'Run inside fiber',
2176
+ ...log_object
2177
+ });
2178
+ let error;
2179
+ let res;
2180
+ try {
2181
+ res = this.$.$mol_run_spawn_sync(app, args, opts);
2182
+ error = res.error;
2183
+ }
2184
+ catch (err) {
2185
+ error = err;
2186
+ }
2187
+ if (!res || error || res.status) {
2188
+ throw new $mol_run_error(this.error_message(res), { ...log_object, status: res?.status, signal: res?.signal }, ...(error ? [error] : []));
2189
+ }
2190
+ return res;
2191
+ }
2192
+ let sub;
2193
+ try {
2194
+ sub = this.$.$mol_run_spawn(app, args, {
2195
+ ...opts,
2196
+ stdio: ['pipe', 'inherit', 'inherit'],
2197
+ });
2198
+ }
2199
+ catch (error) {
2200
+ throw new $mol_run_error(this.error_message(undefined), log_object, error);
2201
+ }
2202
+ const pid = sub.pid ?? 0;
2203
+ this.$.$mol_log3_come({
2204
+ ...log_object,
2205
+ pid,
2164
2206
  });
2165
- const res = this.$mol_run_spawn_sync(app, args, { shell: true, cwd: dir, env });
2166
- if (res.status)
2167
- $mol_fail(new Error(res.stderr.toString() || 'Exit(' + res.status + ')'));
2168
- return res;
2169
- }
2170
- const sub = this.$mol_run_spawn(app, args, {
2171
- shell: true,
2172
- cwd: dir,
2173
- env
2174
- });
2175
- this.$mol_log3_come({
2176
- place: '$mol_run_async',
2177
- pid: sub.pid,
2178
- message: 'Run',
2179
- command: args_raw.join(' '),
2180
- dir: $node.path.relative('', dir),
2181
- });
2182
- let killed = false;
2183
- let timer;
2184
- const std_data = [];
2185
- const error_data = [];
2186
- const add = (std_chunk, error_chunk) => {
2187
- if (std_chunk)
2188
- std_data.push(std_chunk);
2189
- if (error_chunk)
2190
- error_data.push(error_chunk);
2191
- if (!timeout)
2192
- return;
2193
- clearTimeout(timer);
2194
- timer = setTimeout(() => {
2195
- const signal = killed ? 'SIGKILL' : 'SIGTERM';
2196
- killed = true;
2197
- add();
2198
- sub.kill(signal);
2199
- }, timeout);
2200
- };
2201
- add();
2202
- sub.stdout?.on('data', data => add(data));
2203
- sub.stderr?.on('data', data => add(undefined, data));
2204
- const promise = new Promise((done, fail) => {
2205
- const close = (error, status = null, signal = null) => {
2206
- if (!timer && timeout)
2207
+ let timeout_kill = false;
2208
+ let timer;
2209
+ const std_data = [];
2210
+ const error_data = [];
2211
+ const add = (std_chunk, error_chunk) => {
2212
+ if (std_chunk)
2213
+ std_data.push(std_chunk);
2214
+ if (error_chunk)
2215
+ error_data.push(error_chunk);
2216
+ if (!timeout)
2207
2217
  return;
2208
2218
  clearTimeout(timer);
2209
- timer = undefined;
2210
- const res = {
2211
- pid: sub.pid,
2212
- status,
2213
- signal,
2214
- get stdout() { return Buffer.concat(std_data); },
2215
- get stderr() { return Buffer.concat(error_data); }
2216
- };
2217
- this.$mol_log3_done({
2218
- place: '$mol_run_async',
2219
- pid: sub.pid,
2220
- message: 'Run',
2221
- status,
2222
- command: args_raw.join(' '),
2223
- dir: $node.path.relative('', dir),
2224
- });
2225
- if (error || status || killed)
2226
- return fail(new $mol_run_error((res.stderr.toString() || res.stdout.toString() || 'Run error') + (killed ? ', timeout' : ''), { signal, timeout: killed }, ...error ? [error] : []));
2227
- done(res);
2219
+ timer = setTimeout(() => {
2220
+ const signal = timeout_kill ? 'SIGKILL' : 'SIGTERM';
2221
+ timeout_kill = true;
2222
+ add();
2223
+ sub.kill(signal);
2224
+ }, timeout);
2228
2225
  };
2229
- sub.on('disconnect', () => close(new Error('Disconnected')));
2230
- sub.on('error', err => close(err));
2231
- sub.on('exit', (status, signal) => close(null, status, signal));
2232
- });
2233
- return Object.assign(promise, { destructor: () => {
2234
- clearTimeout(timer);
2235
- sub.kill('SIGKILL');
2236
- } });
2237
- }
2238
- $.$mol_run_async = $mol_run_async;
2239
- function $mol_run(options) {
2240
- if (!options.env)
2241
- options = { ...options, env: this.$mol_env() };
2242
- return $mol_wire_sync(this).$mol_run_async(options);
2226
+ add();
2227
+ sub.stdout?.on('data', data => add(data));
2228
+ sub.stderr?.on('data', data => add(undefined, data));
2229
+ const result_promise = new Promise((done, fail) => {
2230
+ const close = (error, status = null, signal = null) => {
2231
+ if (!timer && timeout)
2232
+ return;
2233
+ clearTimeout(timer);
2234
+ timer = undefined;
2235
+ const res = {
2236
+ pid,
2237
+ signal,
2238
+ get stdout() { return Buffer.concat(std_data); },
2239
+ get stderr() { return Buffer.concat(error_data); }
2240
+ };
2241
+ if (error || status || timeout_kill)
2242
+ return fail(new $mol_run_error(this.error_message(res) + (timeout_kill ? ', timeout' : ''), { ...log_object, pid, status, signal, timeout_kill }, ...error ? [error] : []));
2243
+ this.$.$mol_log3_done({
2244
+ ...log_object,
2245
+ pid,
2246
+ });
2247
+ done(res);
2248
+ };
2249
+ sub.on('disconnect', () => close(new Error('Disconnected')));
2250
+ sub.on('error', err => close(err));
2251
+ sub.on('exit', (status, signal) => close(null, status, signal));
2252
+ });
2253
+ return Object.assign(result_promise, { destructor: () => {
2254
+ clearTimeout(timer);
2255
+ sub.kill('SIGKILL');
2256
+ } });
2257
+ }
2258
+ static error_message(res) {
2259
+ return res?.stderr.toString() || res?.stdout.toString() || 'Run error';
2260
+ }
2243
2261
  }
2244
2262
  $.$mol_run = $mol_run;
2245
2263
  })($ || ($ = {}));
@@ -2249,7 +2267,7 @@ var $;
2249
2267
  var $;
2250
2268
  (function ($) {
2251
2269
  function $mol_exec(dir, command, ...args) {
2252
- return this.$mol_run({ command: [command, ...args], dir });
2270
+ return this.$mol_run.spawn({ command: [command, ...args], dir });
2253
2271
  }
2254
2272
  $.$mol_exec = $mol_exec;
2255
2273
  })($ || ($ = {}));
@@ -5340,6 +5358,65 @@ var $;
5340
5358
  $.$mol_state_local = $mol_state_local;
5341
5359
  })($ || ($ = {}));
5342
5360
 
5361
+ ;
5362
+ "use strict";
5363
+ var $;
5364
+ (function ($) {
5365
+ $.$mol_action = $mol_wire_method;
5366
+ })($ || ($ = {}));
5367
+
5368
+ ;
5369
+ "use strict";
5370
+ var $;
5371
+ (function ($) {
5372
+ class $mol_lock extends $mol_object {
5373
+ promise = null;
5374
+ async wait() {
5375
+ let next = () => { };
5376
+ let destructed = false;
5377
+ const task = $mol_wire_auto();
5378
+ if (!task)
5379
+ return next;
5380
+ const destructor = task.destructor.bind(task);
5381
+ task.destructor = () => {
5382
+ destructor();
5383
+ destructed = true;
5384
+ next();
5385
+ };
5386
+ let promise;
5387
+ do {
5388
+ promise = this.promise;
5389
+ await promise;
5390
+ if (destructed)
5391
+ return next;
5392
+ } while (promise !== this.promise);
5393
+ this.promise = new Promise(done => { next = done; });
5394
+ return next;
5395
+ }
5396
+ grab() { return $mol_wire_sync(this).wait(); }
5397
+ }
5398
+ $.$mol_lock = $mol_lock;
5399
+ })($ || ($ = {}));
5400
+
5401
+ ;
5402
+ "use strict";
5403
+ var $;
5404
+ (function ($) {
5405
+ function $mol_compare_array(a, b) {
5406
+ if (a === b)
5407
+ return true;
5408
+ if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
5409
+ return false;
5410
+ if (a.length !== b.length)
5411
+ return false;
5412
+ for (let i = 0; i < a.length; i++)
5413
+ if (a[i] !== b[i])
5414
+ return false;
5415
+ return true;
5416
+ }
5417
+ $.$mol_compare_array = $mol_compare_array;
5418
+ })($ || ($ = {}));
5419
+
5343
5420
  ;
5344
5421
  "use strict";
5345
5422
 
@@ -5373,12 +5450,82 @@ var $;
5373
5450
  "use strict";
5374
5451
  var $;
5375
5452
  (function ($) {
5376
- class $mol_file_not_found extends Error {
5453
+ class $mol_file_transaction extends $mol_object {
5454
+ path() { return ''; }
5455
+ modes() { return []; }
5456
+ write(options) {
5457
+ return 0;
5458
+ }
5459
+ read() {
5460
+ return new Uint8Array();
5461
+ }
5462
+ truncate(size) { }
5463
+ close() { }
5464
+ destructor() {
5465
+ this.close();
5466
+ }
5377
5467
  }
5378
- $.$mol_file_not_found = $mol_file_not_found;
5379
- class $mol_file extends $mol_object {
5468
+ $.$mol_file_transaction = $mol_file_transaction;
5469
+ })($ || ($ = {}));
5470
+
5471
+ ;
5472
+ "use strict";
5473
+ var $;
5474
+ (function ($) {
5475
+ let file_modes;
5476
+ (function (file_modes) {
5477
+ file_modes[file_modes["create"] = $node.fs.constants.O_CREAT] = "create";
5478
+ file_modes[file_modes["exists_truncate"] = $node.fs.constants.O_TRUNC] = "exists_truncate";
5479
+ file_modes[file_modes["exists_fail"] = $node.fs.constants.O_EXCL] = "exists_fail";
5480
+ file_modes[file_modes["read_only"] = $node.fs.constants.O_RDONLY] = "read_only";
5481
+ file_modes[file_modes["write_only"] = $node.fs.constants.O_WRONLY] = "write_only";
5482
+ file_modes[file_modes["read_write"] = $node.fs.constants.O_RDWR] = "read_write";
5483
+ file_modes[file_modes["append"] = $node.fs.constants.O_APPEND] = "append";
5484
+ })(file_modes || (file_modes = {}));
5485
+ function mode_mask(modes) {
5486
+ return modes.reduce((res, mode) => res | file_modes[mode], 0);
5487
+ }
5488
+ class $mol_file_transaction_node extends $mol_file_transaction {
5489
+ descr() {
5490
+ $mol_wire_solid();
5491
+ return $node.fs.openSync(this.path(), mode_mask(this.modes()));
5492
+ }
5493
+ write({ buffer, offset = 0, length, position = null }) {
5494
+ if (Array.isArray(buffer)) {
5495
+ return $node.fs.writevSync(this.descr(), buffer, position ?? undefined);
5496
+ }
5497
+ if (typeof buffer === 'string') {
5498
+ return $node.fs.writeSync(this.descr(), buffer, position);
5499
+ }
5500
+ length = length ?? buffer.byteLength;
5501
+ return $node.fs.writeSync(this.descr(), buffer, offset, length, position);
5502
+ }
5503
+ truncate(size) {
5504
+ $node.fs.ftruncateSync(this.descr());
5505
+ }
5506
+ read() {
5507
+ return $mol_file_node_buffer_normalize($node.fs.readFileSync(this.descr()));
5508
+ }
5509
+ close() {
5510
+ $node.fs.closeSync(this.descr());
5511
+ }
5512
+ }
5513
+ __decorate([
5514
+ $mol_mem
5515
+ ], $mol_file_transaction_node.prototype, "descr", null);
5516
+ $.$mol_file_transaction_node = $mol_file_transaction_node;
5517
+ $.$mol_file_transaction = $mol_file_transaction_node;
5518
+ })($ || ($ = {}));
5519
+
5520
+ ;
5521
+ "use strict";
5522
+ var $;
5523
+ (function ($) {
5524
+ class $mol_file_base extends $mol_object {
5380
5525
  static absolute(path) {
5381
- throw new Error('Not implemented yet');
5526
+ return this.make({
5527
+ path: $mol_const(path)
5528
+ });
5382
5529
  }
5383
5530
  static relative(path) {
5384
5531
  throw new Error('Not implemented yet');
@@ -5390,27 +5537,164 @@ var $;
5390
5537
  parent() {
5391
5538
  return this.resolve('..');
5392
5539
  }
5393
- reset() {
5394
- try {
5395
- this.stat(null);
5540
+ exists_cut() { return this.exists(); }
5541
+ root() {
5542
+ const path = this.path();
5543
+ const base = this.constructor.base;
5544
+ return base.startsWith(path) || this == this.parent();
5545
+ }
5546
+ stat(next, virt) {
5547
+ const path = this.path();
5548
+ const parent = this.parent();
5549
+ if (!this.root()) {
5550
+ parent.version();
5396
5551
  }
5397
- catch (error) {
5398
- if (error instanceof $mol_file_not_found)
5552
+ parent.watcher();
5553
+ if (virt)
5554
+ return next ?? null;
5555
+ return next ?? this.info(path);
5556
+ }
5557
+ static changed = new Set;
5558
+ static frame = null;
5559
+ static changed_add(type, path) {
5560
+ if (/([\/\\]\.|___$)/.test(path))
5561
+ return;
5562
+ const file = this.relative(path.at(-1) === '/' ? path.slice(0, -1) : path);
5563
+ this.changed.add(file);
5564
+ if (!this.watching)
5565
+ return;
5566
+ this.frame?.destructor();
5567
+ this.frame = new this.$.$mol_after_timeout(this.watch_debounce(), () => {
5568
+ if (!this.watching)
5399
5569
  return;
5400
- return $mol_fail_hidden(error);
5570
+ this.watching = false;
5571
+ $mol_wire_async(this).flush();
5572
+ });
5573
+ }
5574
+ static watch_debounce() { return 500; }
5575
+ static flush() {
5576
+ for (const file of this.changed) {
5577
+ const parent = file.parent();
5578
+ try {
5579
+ if ($mol_wire_probe(() => parent.sub()))
5580
+ parent.sub(null);
5581
+ file.reset();
5582
+ }
5583
+ catch (error) {
5584
+ if ($mol_fail_catch(error))
5585
+ $mol_fail_log(error);
5586
+ }
5587
+ }
5588
+ this.changed.clear();
5589
+ this.watching = true;
5590
+ }
5591
+ static watching = true;
5592
+ static lock = new $mol_lock;
5593
+ static watch_off(path) {
5594
+ this.watching = false;
5595
+ this.flush();
5596
+ this.watching = false;
5597
+ this.changed.add(this.absolute(path));
5598
+ }
5599
+ static unwatched(side_effect, affected_dir) {
5600
+ const unlock = this.lock.grab();
5601
+ this.watch_off(affected_dir);
5602
+ try {
5603
+ const result = side_effect();
5604
+ this.flush();
5605
+ unlock();
5606
+ return result;
5607
+ }
5608
+ catch (e) {
5609
+ if (!$mol_promise_like(e)) {
5610
+ this.flush();
5611
+ unlock();
5612
+ }
5613
+ $mol_fail_hidden(e);
5401
5614
  }
5402
5615
  }
5616
+ reset() {
5617
+ this.stat(null);
5618
+ }
5619
+ modified() { return this.stat()?.mtime ?? null; }
5403
5620
  version() {
5404
- return this.stat()?.mtime.getTime().toString(36).toUpperCase() ?? '';
5621
+ const next = this.stat()?.mtime.getTime().toString(36).toUpperCase() ?? '';
5622
+ return next;
5623
+ }
5624
+ info(path) { return null; }
5625
+ ensure() { }
5626
+ drop() { }
5627
+ copy(to) { }
5628
+ read() { return new Uint8Array; }
5629
+ write(buffer) { }
5630
+ kids() {
5631
+ return [];
5632
+ }
5633
+ readable(opts) {
5634
+ return new ReadableStream;
5635
+ }
5636
+ writable(opts) {
5637
+ return new WritableStream;
5638
+ }
5639
+ buffer(next) {
5640
+ let readed = new Uint8Array();
5641
+ if (next === undefined) {
5642
+ if (this.version())
5643
+ readed = this.read();
5644
+ }
5645
+ const prev = $mol_mem_cached(() => this.buffer());
5646
+ const changed = prev === undefined || !$mol_compare_array(prev, next ?? readed);
5647
+ if (prev !== undefined && changed) {
5648
+ this.$.$mol_log3_rise({
5649
+ place: `$mol_file_node.buffer()`,
5650
+ message: 'Changed',
5651
+ path: this.relate(),
5652
+ });
5653
+ }
5654
+ if (next === undefined)
5655
+ return changed ? readed : prev;
5656
+ if (!changed && this.exists())
5657
+ return prev;
5658
+ this.parent().exists(true);
5659
+ this.stat(this.stat_make(next.length), 'virt');
5660
+ this.write(next);
5661
+ return next;
5662
+ }
5663
+ stat_make(size) {
5664
+ const now = new Date();
5665
+ return {
5666
+ type: 'file',
5667
+ size,
5668
+ atime: now,
5669
+ mtime: now,
5670
+ ctime: now,
5671
+ };
5672
+ }
5673
+ clone(to) {
5674
+ if (!this.exists())
5675
+ return null;
5676
+ const target = this.constructor.absolute(to);
5677
+ try {
5678
+ this.version();
5679
+ target.parent().exists(true);
5680
+ this.copy(to);
5681
+ target.reset();
5682
+ return target;
5683
+ }
5684
+ catch (error) {
5685
+ if ($mol_fail_catch(error)) {
5686
+ console.error(error);
5687
+ }
5688
+ }
5689
+ return null;
5405
5690
  }
5406
5691
  watcher() {
5407
- console.warn('$mol_file_web.watcher() not implemented');
5408
5692
  return {
5409
5693
  destructor() { }
5410
5694
  };
5411
5695
  }
5412
5696
  exists(next) {
5413
- let exists = Boolean(this.stat());
5697
+ const exists = Boolean(this.stat());
5414
5698
  if (next === undefined)
5415
5699
  return exists;
5416
5700
  if (next === exists)
@@ -5436,26 +5720,40 @@ var $;
5436
5720
  return match ? match[1].substring(1) : '';
5437
5721
  }
5438
5722
  text(next, virt) {
5723
+ if (next !== undefined)
5724
+ this.exists();
5725
+ return this.text_int(next, virt);
5726
+ }
5727
+ text_int(next, virt) {
5439
5728
  if (virt) {
5440
- const now = new Date;
5441
- this.stat({
5442
- type: 'file',
5443
- size: 0,
5444
- atime: now,
5445
- mtime: now,
5446
- ctime: now,
5447
- }, 'virt');
5729
+ this.stat(this.stat_make(0), 'virt');
5448
5730
  return next;
5449
5731
  }
5450
5732
  if (next === undefined) {
5451
- return $mol_charset_decode(this.buffer(undefined));
5733
+ return $mol_charset_decode(this.buffer());
5452
5734
  }
5453
5735
  else {
5454
- const buffer = next === undefined ? undefined : $mol_charset_encode(next);
5736
+ const buffer = $mol_charset_encode(next);
5455
5737
  this.buffer(buffer);
5456
5738
  return next;
5457
5739
  }
5458
5740
  }
5741
+ sub(reset) {
5742
+ if (!this.exists())
5743
+ return [];
5744
+ if (this.type() !== 'dir')
5745
+ return [];
5746
+ this.version();
5747
+ return this.kids().filter(file => file.exists());
5748
+ }
5749
+ resolve(path) {
5750
+ throw new Error('implement');
5751
+ }
5752
+ relate(base = this.constructor.relative('.')) {
5753
+ const base_path = base.path();
5754
+ const path = this.path();
5755
+ return path.startsWith(base_path) ? path.slice(base_path.length) : path;
5756
+ }
5459
5757
  find(include, exclude) {
5460
5758
  const found = [];
5461
5759
  const sub = this.sub();
@@ -5479,49 +5777,80 @@ var $;
5479
5777
  default: return 0;
5480
5778
  }
5481
5779
  }
5482
- open(...modes) {
5483
- return 0;
5484
- }
5485
5780
  toJSON() {
5486
5781
  return this.path();
5487
5782
  }
5783
+ open(...modes) {
5784
+ return this.$.$mol_file_transaction.make({
5785
+ path: () => this.path(),
5786
+ modes: () => modes
5787
+ });
5788
+ }
5488
5789
  }
5790
+ __decorate([
5791
+ $mol_action
5792
+ ], $mol_file_base.prototype, "exists_cut", null);
5793
+ __decorate([
5794
+ $mol_mem
5795
+ ], $mol_file_base.prototype, "stat", null);
5489
5796
  __decorate([
5490
5797
  $mol_mem
5491
- ], $mol_file.prototype, "exists", null);
5798
+ ], $mol_file_base.prototype, "modified", null);
5492
5799
  __decorate([
5493
5800
  $mol_mem
5494
- ], $mol_file.prototype, "text", null);
5801
+ ], $mol_file_base.prototype, "version", null);
5495
5802
  __decorate([
5496
5803
  $mol_mem_key
5497
- ], $mol_file, "absolute", null);
5498
- $.$mol_file = $mol_file;
5499
- })($ || ($ = {}));
5500
-
5501
- ;
5502
- "use strict";
5503
- var $;
5504
- (function ($) {
5505
- $.$mol_action = $mol_wire_method;
5804
+ ], $mol_file_base.prototype, "readable", null);
5805
+ __decorate([
5806
+ $mol_mem_key
5807
+ ], $mol_file_base.prototype, "writable", null);
5808
+ __decorate([
5809
+ $mol_mem
5810
+ ], $mol_file_base.prototype, "buffer", null);
5811
+ __decorate([
5812
+ $mol_action
5813
+ ], $mol_file_base.prototype, "stat_make", null);
5814
+ __decorate([
5815
+ $mol_mem_key
5816
+ ], $mol_file_base.prototype, "clone", null);
5817
+ __decorate([
5818
+ $mol_mem
5819
+ ], $mol_file_base.prototype, "exists", null);
5820
+ __decorate([
5821
+ $mol_mem
5822
+ ], $mol_file_base.prototype, "type", null);
5823
+ __decorate([
5824
+ $mol_mem
5825
+ ], $mol_file_base.prototype, "text_int", null);
5826
+ __decorate([
5827
+ $mol_mem
5828
+ ], $mol_file_base.prototype, "sub", null);
5829
+ __decorate([
5830
+ $mol_mem
5831
+ ], $mol_file_base.prototype, "size", null);
5832
+ __decorate([
5833
+ $mol_action
5834
+ ], $mol_file_base.prototype, "open", null);
5835
+ __decorate([
5836
+ $mol_mem_key
5837
+ ], $mol_file_base, "absolute", null);
5838
+ __decorate([
5839
+ $mol_action
5840
+ ], $mol_file_base, "flush", null);
5841
+ __decorate([
5842
+ $mol_action
5843
+ ], $mol_file_base, "watch_off", null);
5844
+ $.$mol_file_base = $mol_file_base;
5506
5845
  })($ || ($ = {}));
5507
5846
 
5508
5847
  ;
5509
5848
  "use strict";
5510
5849
  var $;
5511
5850
  (function ($) {
5512
- function $mol_compare_array(a, b) {
5513
- if (a === b)
5514
- return true;
5515
- if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
5516
- return false;
5517
- if (a.length !== b.length)
5518
- return false;
5519
- for (let i = 0; i < a.length; i++)
5520
- if (a[i] !== b[i])
5521
- return false;
5522
- return true;
5851
+ class $mol_file extends $mol_file_base {
5523
5852
  }
5524
- $.$mol_compare_array = $mol_compare_array;
5853
+ $.$mol_file = $mol_file;
5525
5854
  })($ || ($ = {}));
5526
5855
 
5527
5856
  ;
@@ -5548,186 +5877,183 @@ var $;
5548
5877
  ctime: stat.ctime
5549
5878
  };
5550
5879
  }
5551
- function buffer_normalize(buf) {
5880
+ function $mol_file_node_buffer_normalize(buf) {
5552
5881
  return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
5553
5882
  }
5554
- let $mol_file_mode_open;
5555
- (function ($mol_file_mode_open) {
5556
- $mol_file_mode_open[$mol_file_mode_open["create"] = $node.fs.constants.O_CREAT] = "create";
5557
- $mol_file_mode_open[$mol_file_mode_open["exists_truncate"] = $node.fs.constants.O_TRUNC] = "exists_truncate";
5558
- $mol_file_mode_open[$mol_file_mode_open["exists_fail"] = $node.fs.constants.O_EXCL] = "exists_fail";
5559
- $mol_file_mode_open[$mol_file_mode_open["read_only"] = $node.fs.constants.O_RDONLY] = "read_only";
5560
- $mol_file_mode_open[$mol_file_mode_open["write_only"] = $node.fs.constants.O_WRONLY] = "write_only";
5561
- $mol_file_mode_open[$mol_file_mode_open["read_write"] = $node.fs.constants.O_RDWR] = "read_write";
5562
- $mol_file_mode_open[$mol_file_mode_open["append"] = $node.fs.constants.O_APPEND] = "append";
5563
- })($mol_file_mode_open = $.$mol_file_mode_open || ($.$mol_file_mode_open = {}));
5883
+ $.$mol_file_node_buffer_normalize = $mol_file_node_buffer_normalize;
5564
5884
  class $mol_file_node extends $mol_file {
5565
- static absolute(path) {
5566
- return this.make({
5567
- path: $mol_const(path)
5568
- });
5569
- }
5570
5885
  static relative(path) {
5571
5886
  return this.absolute($node.path.resolve(this.base, path).replace(/\\/g, '/'));
5572
5887
  }
5573
- watcher() {
5574
- if (/\/\./.test(this.path()))
5575
- return { destructor() { } };
5576
- const watcher = $node.chokidar.watch(this.path(), {
5577
- persistent: true,
5578
- ignored: path => /([\/\\]\.|___$)/.test(path),
5579
- depth: 0,
5580
- ignoreInitial: true,
5581
- awaitWriteFinish: {
5582
- stabilityThreshold: 100,
5583
- },
5584
- });
5585
- watcher
5586
- .on('all', (type, path) => {
5587
- const file = $mol_file.relative(path.replace(/\\/g, '/'));
5588
- file.reset();
5589
- if (type === 'change') {
5590
- this.stat(null);
5591
- }
5592
- else {
5593
- file.parent().reset();
5888
+ watcher(reset) {
5889
+ const path = this.path();
5890
+ const root = this.root();
5891
+ if (!root && !this.exists())
5892
+ return super.watcher();
5893
+ let watcher;
5894
+ try {
5895
+ watcher = $node.fs.watch(path);
5896
+ }
5897
+ catch (error) {
5898
+ if (!(error instanceof Error))
5899
+ error = new Error('Unknown watch error', { cause: error });
5900
+ error.message += '\n' + path;
5901
+ if (root || error.code !== 'ENOENT') {
5902
+ this.$.$mol_fail_log(error);
5594
5903
  }
5595
- })
5596
- .on('error', $mol_fail_log);
5904
+ return super.watcher();
5905
+ }
5906
+ watcher.on('change', (type, name) => {
5907
+ if (!name)
5908
+ return;
5909
+ const path = $node.path.join(this.path(), name.toString());
5910
+ this.constructor.changed_add(type, path);
5911
+ });
5912
+ watcher.on('error', e => this.$.$mol_fail_log(e));
5913
+ let destructed = false;
5914
+ watcher.on('close', () => {
5915
+ if (!destructed)
5916
+ setTimeout(() => $mol_wire_async(this).watcher(null), 500);
5917
+ });
5597
5918
  return {
5598
5919
  destructor() {
5920
+ destructed = true;
5599
5921
  watcher.close();
5600
5922
  }
5601
5923
  };
5602
5924
  }
5603
- stat(next, virt) {
5604
- let stat = next;
5605
- const path = this.path();
5606
- this.parent().watcher();
5607
- if (virt)
5608
- return next;
5925
+ info(path) {
5609
5926
  try {
5610
- stat = next ?? stat_convert($node.fs.statSync(path, { throwIfNoEntry: false }));
5927
+ return stat_convert($node.fs.statSync(path));
5611
5928
  }
5612
5929
  catch (error) {
5613
- if (error.code === 'ENOENT')
5614
- error = new $mol_file_not_found(`File not found`);
5615
- error.message += '\n' + path;
5616
- return this.$.$mol_fail_hidden(error);
5930
+ if (this.$.$mol_fail_catch(error)) {
5931
+ if (error.code === 'ENOENT')
5932
+ return null;
5933
+ error.message += '\n' + path;
5934
+ this.$.$mol_fail_hidden(error);
5935
+ }
5617
5936
  }
5618
- return stat;
5937
+ return null;
5619
5938
  }
5620
5939
  ensure() {
5621
5940
  const path = this.path();
5622
5941
  try {
5623
- $node.fs.mkdirSync(path);
5942
+ $node.fs.mkdirSync(path, { recursive: true });
5943
+ return null;
5624
5944
  }
5625
5945
  catch (e) {
5626
- e.message += '\n' + path;
5627
- this.$.$mol_fail_hidden(e);
5946
+ if (this.$.$mol_fail_catch(e)) {
5947
+ if (e.code === 'EEXIST')
5948
+ return null;
5949
+ e.message += '\n' + path;
5950
+ this.$.$mol_fail_hidden(e);
5951
+ }
5628
5952
  }
5629
5953
  }
5954
+ copy(to) {
5955
+ $node.fs.copyFileSync(this.path(), to);
5956
+ }
5630
5957
  drop() {
5631
5958
  $node.fs.unlinkSync(this.path());
5632
5959
  }
5633
- buffer(next) {
5960
+ read() {
5634
5961
  const path = this.path();
5635
- if (next === undefined) {
5636
- if (!this.stat())
5637
- return new Uint8Array;
5638
- try {
5639
- const prev = $mol_mem_cached(() => this.buffer());
5640
- next = buffer_normalize($node.fs.readFileSync(path));
5641
- if (prev !== undefined && !$mol_compare_array(prev, next)) {
5642
- this.$.$mol_log3_rise({
5643
- place: `$mol_file_node..buffer()`,
5644
- message: 'Changed',
5645
- path: this.relate(),
5646
- });
5647
- }
5648
- return next;
5649
- }
5650
- catch (error) {
5962
+ try {
5963
+ return $mol_file_node_buffer_normalize($node.fs.readFileSync(path));
5964
+ }
5965
+ catch (error) {
5966
+ if (!$mol_promise_like(error)) {
5651
5967
  error.message += '\n' + path;
5652
- return this.$.$mol_fail_hidden(error);
5653
5968
  }
5969
+ $mol_fail_hidden(error);
5654
5970
  }
5655
- this.parent().exists(true);
5656
- const now = new Date;
5657
- this.stat({
5658
- type: 'file',
5659
- size: next.length,
5660
- atime: now,
5661
- mtime: now,
5662
- ctime: now,
5663
- }, 'virt');
5971
+ }
5972
+ write(buffer) {
5973
+ const path = this.path();
5664
5974
  try {
5665
- $node.fs.writeFileSync(path, next);
5975
+ $node.fs.writeFileSync(path, buffer);
5666
5976
  }
5667
5977
  catch (error) {
5668
- error.message += '\n' + path;
5978
+ if (this.$.$mol_fail_catch(error)) {
5979
+ error.message += '\n' + path;
5980
+ }
5669
5981
  return this.$.$mol_fail_hidden(error);
5670
5982
  }
5671
- return next;
5672
5983
  }
5673
- sub() {
5674
- if (!this.exists())
5675
- return [];
5676
- if (this.type() !== 'dir')
5677
- return [];
5984
+ kids() {
5678
5985
  const path = this.path();
5679
- this.stat();
5680
5986
  try {
5681
- return $node.fs.readdirSync(path)
5987
+ const kids = $node.fs.readdirSync(path)
5682
5988
  .filter(name => !/^\.+$/.test(name))
5683
5989
  .map(name => this.resolve(name));
5990
+ return kids;
5684
5991
  }
5685
5992
  catch (e) {
5686
- e.message += '\n' + path;
5687
- return this.$.$mol_fail_hidden(e);
5993
+ if (this.$.$mol_fail_catch(e)) {
5994
+ if (e.code === 'ENOENT')
5995
+ return [];
5996
+ e.message += '\n' + path;
5997
+ }
5998
+ $mol_fail_hidden(e);
5688
5999
  }
5689
6000
  }
5690
6001
  resolve(path) {
5691
- return this.constructor.relative($node.path.join(this.path(), path));
6002
+ return this.constructor
6003
+ .relative($node.path.join(this.path(), path));
5692
6004
  }
5693
6005
  relate(base = this.constructor.relative('.')) {
5694
6006
  return $node.path.relative(base.path(), this.path()).replace(/\\/g, '/');
5695
6007
  }
5696
- append(next) {
5697
- const path = this.path();
5698
- try {
5699
- $node.fs.appendFileSync(path, next);
5700
- }
5701
- catch (e) {
5702
- e.message += '\n' + path;
5703
- return this.$.$mol_fail_hidden(e);
5704
- }
5705
- }
5706
- open(...modes) {
5707
- return $node.fs.openSync(this.path(), modes.reduce((res, mode) => res | $mol_file_mode_open[mode], 0));
6008
+ readable(opts) {
6009
+ const { Readable } = $node['node:stream'];
6010
+ const stream = $node.fs.createReadStream(this.path(), {
6011
+ flags: 'r',
6012
+ autoClose: true,
6013
+ start: opts?.start,
6014
+ end: opts?.end,
6015
+ encoding: 'binary',
6016
+ });
6017
+ return Readable.toWeb(stream);
6018
+ }
6019
+ writable(opts) {
6020
+ const { Writable } = $node['node:stream'];
6021
+ const stream = $node.fs.createWriteStream(this.path(), {
6022
+ flags: 'w+',
6023
+ autoClose: true,
6024
+ start: opts?.start,
6025
+ encoding: 'binary',
6026
+ });
6027
+ return Writable.toWeb(stream);
5708
6028
  }
5709
6029
  }
5710
6030
  __decorate([
5711
6031
  $mol_mem
5712
6032
  ], $mol_file_node.prototype, "watcher", null);
5713
6033
  __decorate([
5714
- $mol_mem
5715
- ], $mol_file_node.prototype, "stat", null);
6034
+ $mol_action
6035
+ ], $mol_file_node.prototype, "info", null);
5716
6036
  __decorate([
5717
- $mol_mem
6037
+ $mol_action
5718
6038
  ], $mol_file_node.prototype, "ensure", null);
6039
+ __decorate([
6040
+ $mol_action
6041
+ ], $mol_file_node.prototype, "copy", null);
5719
6042
  __decorate([
5720
6043
  $mol_action
5721
6044
  ], $mol_file_node.prototype, "drop", null);
5722
6045
  __decorate([
5723
- $mol_mem
5724
- ], $mol_file_node.prototype, "buffer", null);
6046
+ $mol_action
6047
+ ], $mol_file_node.prototype, "read", null);
5725
6048
  __decorate([
5726
- $mol_mem
5727
- ], $mol_file_node.prototype, "sub", null);
6049
+ $mol_action
6050
+ ], $mol_file_node.prototype, "write", null);
5728
6051
  __decorate([
5729
6052
  $mol_mem_key
5730
- ], $mol_file_node, "absolute", null);
6053
+ ], $mol_file_node.prototype, "readable", null);
6054
+ __decorate([
6055
+ $mol_mem
6056
+ ], $mol_file_node.prototype, "writable", null);
5731
6057
  $.$mol_file_node = $mol_file_node;
5732
6058
  $.$mol_file = $mol_file_node;
5733
6059
  })($ || ($ = {}));