remotelink 0.4.3 → 0.4.4

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/dist/index.js CHANGED
@@ -32,6 +32,1591 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
32
32
  mod
33
33
  ));
34
34
 
35
+ // ../../node_modules/.pnpm/graceful-fs@4.2.11/node_modules/graceful-fs/polyfills.js
36
+ var require_polyfills = __commonJS({
37
+ "../../node_modules/.pnpm/graceful-fs@4.2.11/node_modules/graceful-fs/polyfills.js"(exports, module) {
38
+ "use strict";
39
+ var constants = __require("constants");
40
+ var origCwd = process.cwd;
41
+ var cwd = null;
42
+ var platform = process.env.GRACEFUL_FS_PLATFORM || process.platform;
43
+ process.cwd = function() {
44
+ if (!cwd)
45
+ cwd = origCwd.call(process);
46
+ return cwd;
47
+ };
48
+ try {
49
+ process.cwd();
50
+ } catch (er) {
51
+ }
52
+ if (typeof process.chdir === "function") {
53
+ chdir = process.chdir;
54
+ process.chdir = function(d) {
55
+ cwd = null;
56
+ chdir.call(process, d);
57
+ };
58
+ if (Object.setPrototypeOf) Object.setPrototypeOf(process.chdir, chdir);
59
+ }
60
+ var chdir;
61
+ module.exports = patch;
62
+ function patch(fs8) {
63
+ if (constants.hasOwnProperty("O_SYMLINK") && process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
64
+ patchLchmod(fs8);
65
+ }
66
+ if (!fs8.lutimes) {
67
+ patchLutimes(fs8);
68
+ }
69
+ fs8.chown = chownFix(fs8.chown);
70
+ fs8.fchown = chownFix(fs8.fchown);
71
+ fs8.lchown = chownFix(fs8.lchown);
72
+ fs8.chmod = chmodFix(fs8.chmod);
73
+ fs8.fchmod = chmodFix(fs8.fchmod);
74
+ fs8.lchmod = chmodFix(fs8.lchmod);
75
+ fs8.chownSync = chownFixSync(fs8.chownSync);
76
+ fs8.fchownSync = chownFixSync(fs8.fchownSync);
77
+ fs8.lchownSync = chownFixSync(fs8.lchownSync);
78
+ fs8.chmodSync = chmodFixSync(fs8.chmodSync);
79
+ fs8.fchmodSync = chmodFixSync(fs8.fchmodSync);
80
+ fs8.lchmodSync = chmodFixSync(fs8.lchmodSync);
81
+ fs8.stat = statFix(fs8.stat);
82
+ fs8.fstat = statFix(fs8.fstat);
83
+ fs8.lstat = statFix(fs8.lstat);
84
+ fs8.statSync = statFixSync(fs8.statSync);
85
+ fs8.fstatSync = statFixSync(fs8.fstatSync);
86
+ fs8.lstatSync = statFixSync(fs8.lstatSync);
87
+ if (fs8.chmod && !fs8.lchmod) {
88
+ fs8.lchmod = function(path8, mode, cb) {
89
+ if (cb) process.nextTick(cb);
90
+ };
91
+ fs8.lchmodSync = function() {
92
+ };
93
+ }
94
+ if (fs8.chown && !fs8.lchown) {
95
+ fs8.lchown = function(path8, uid, gid, cb) {
96
+ if (cb) process.nextTick(cb);
97
+ };
98
+ fs8.lchownSync = function() {
99
+ };
100
+ }
101
+ if (platform === "win32") {
102
+ fs8.rename = typeof fs8.rename !== "function" ? fs8.rename : (function(fs$rename) {
103
+ function rename(from, to, cb) {
104
+ var start = Date.now();
105
+ var backoff = 0;
106
+ fs$rename(from, to, function CB(er) {
107
+ if (er && (er.code === "EACCES" || er.code === "EPERM" || er.code === "EBUSY") && Date.now() - start < 6e4) {
108
+ setTimeout(function() {
109
+ fs8.stat(to, function(stater, st) {
110
+ if (stater && stater.code === "ENOENT")
111
+ fs$rename(from, to, CB);
112
+ else
113
+ cb(er);
114
+ });
115
+ }, backoff);
116
+ if (backoff < 100)
117
+ backoff += 10;
118
+ return;
119
+ }
120
+ if (cb) cb(er);
121
+ });
122
+ }
123
+ if (Object.setPrototypeOf) Object.setPrototypeOf(rename, fs$rename);
124
+ return rename;
125
+ })(fs8.rename);
126
+ }
127
+ fs8.read = typeof fs8.read !== "function" ? fs8.read : (function(fs$read) {
128
+ function read(fd, buffer, offset, length, position, callback_) {
129
+ var callback;
130
+ if (callback_ && typeof callback_ === "function") {
131
+ var eagCounter = 0;
132
+ callback = function(er, _, __) {
133
+ if (er && er.code === "EAGAIN" && eagCounter < 10) {
134
+ eagCounter++;
135
+ return fs$read.call(fs8, fd, buffer, offset, length, position, callback);
136
+ }
137
+ callback_.apply(this, arguments);
138
+ };
139
+ }
140
+ return fs$read.call(fs8, fd, buffer, offset, length, position, callback);
141
+ }
142
+ if (Object.setPrototypeOf) Object.setPrototypeOf(read, fs$read);
143
+ return read;
144
+ })(fs8.read);
145
+ fs8.readSync = typeof fs8.readSync !== "function" ? fs8.readSync : /* @__PURE__ */ (function(fs$readSync) {
146
+ return function(fd, buffer, offset, length, position) {
147
+ var eagCounter = 0;
148
+ while (true) {
149
+ try {
150
+ return fs$readSync.call(fs8, fd, buffer, offset, length, position);
151
+ } catch (er) {
152
+ if (er.code === "EAGAIN" && eagCounter < 10) {
153
+ eagCounter++;
154
+ continue;
155
+ }
156
+ throw er;
157
+ }
158
+ }
159
+ };
160
+ })(fs8.readSync);
161
+ function patchLchmod(fs9) {
162
+ fs9.lchmod = function(path8, mode, callback) {
163
+ fs9.open(
164
+ path8,
165
+ constants.O_WRONLY | constants.O_SYMLINK,
166
+ mode,
167
+ function(err, fd) {
168
+ if (err) {
169
+ if (callback) callback(err);
170
+ return;
171
+ }
172
+ fs9.fchmod(fd, mode, function(err2) {
173
+ fs9.close(fd, function(err22) {
174
+ if (callback) callback(err2 || err22);
175
+ });
176
+ });
177
+ }
178
+ );
179
+ };
180
+ fs9.lchmodSync = function(path8, mode) {
181
+ var fd = fs9.openSync(path8, constants.O_WRONLY | constants.O_SYMLINK, mode);
182
+ var threw = true;
183
+ var ret;
184
+ try {
185
+ ret = fs9.fchmodSync(fd, mode);
186
+ threw = false;
187
+ } finally {
188
+ if (threw) {
189
+ try {
190
+ fs9.closeSync(fd);
191
+ } catch (er) {
192
+ }
193
+ } else {
194
+ fs9.closeSync(fd);
195
+ }
196
+ }
197
+ return ret;
198
+ };
199
+ }
200
+ function patchLutimes(fs9) {
201
+ if (constants.hasOwnProperty("O_SYMLINK") && fs9.futimes) {
202
+ fs9.lutimes = function(path8, at, mt, cb) {
203
+ fs9.open(path8, constants.O_SYMLINK, function(er, fd) {
204
+ if (er) {
205
+ if (cb) cb(er);
206
+ return;
207
+ }
208
+ fs9.futimes(fd, at, mt, function(er2) {
209
+ fs9.close(fd, function(er22) {
210
+ if (cb) cb(er2 || er22);
211
+ });
212
+ });
213
+ });
214
+ };
215
+ fs9.lutimesSync = function(path8, at, mt) {
216
+ var fd = fs9.openSync(path8, constants.O_SYMLINK);
217
+ var ret;
218
+ var threw = true;
219
+ try {
220
+ ret = fs9.futimesSync(fd, at, mt);
221
+ threw = false;
222
+ } finally {
223
+ if (threw) {
224
+ try {
225
+ fs9.closeSync(fd);
226
+ } catch (er) {
227
+ }
228
+ } else {
229
+ fs9.closeSync(fd);
230
+ }
231
+ }
232
+ return ret;
233
+ };
234
+ } else if (fs9.futimes) {
235
+ fs9.lutimes = function(_a, _b, _c, cb) {
236
+ if (cb) process.nextTick(cb);
237
+ };
238
+ fs9.lutimesSync = function() {
239
+ };
240
+ }
241
+ }
242
+ function chmodFix(orig) {
243
+ if (!orig) return orig;
244
+ return function(target, mode, cb) {
245
+ return orig.call(fs8, target, mode, function(er) {
246
+ if (chownErOk(er)) er = null;
247
+ if (cb) cb.apply(this, arguments);
248
+ });
249
+ };
250
+ }
251
+ function chmodFixSync(orig) {
252
+ if (!orig) return orig;
253
+ return function(target, mode) {
254
+ try {
255
+ return orig.call(fs8, target, mode);
256
+ } catch (er) {
257
+ if (!chownErOk(er)) throw er;
258
+ }
259
+ };
260
+ }
261
+ function chownFix(orig) {
262
+ if (!orig) return orig;
263
+ return function(target, uid, gid, cb) {
264
+ return orig.call(fs8, target, uid, gid, function(er) {
265
+ if (chownErOk(er)) er = null;
266
+ if (cb) cb.apply(this, arguments);
267
+ });
268
+ };
269
+ }
270
+ function chownFixSync(orig) {
271
+ if (!orig) return orig;
272
+ return function(target, uid, gid) {
273
+ try {
274
+ return orig.call(fs8, target, uid, gid);
275
+ } catch (er) {
276
+ if (!chownErOk(er)) throw er;
277
+ }
278
+ };
279
+ }
280
+ function statFix(orig) {
281
+ if (!orig) return orig;
282
+ return function(target, options, cb) {
283
+ if (typeof options === "function") {
284
+ cb = options;
285
+ options = null;
286
+ }
287
+ function callback(er, stats) {
288
+ if (stats) {
289
+ if (stats.uid < 0) stats.uid += 4294967296;
290
+ if (stats.gid < 0) stats.gid += 4294967296;
291
+ }
292
+ if (cb) cb.apply(this, arguments);
293
+ }
294
+ return options ? orig.call(fs8, target, options, callback) : orig.call(fs8, target, callback);
295
+ };
296
+ }
297
+ function statFixSync(orig) {
298
+ if (!orig) return orig;
299
+ return function(target, options) {
300
+ var stats = options ? orig.call(fs8, target, options) : orig.call(fs8, target);
301
+ if (stats) {
302
+ if (stats.uid < 0) stats.uid += 4294967296;
303
+ if (stats.gid < 0) stats.gid += 4294967296;
304
+ }
305
+ return stats;
306
+ };
307
+ }
308
+ function chownErOk(er) {
309
+ if (!er)
310
+ return true;
311
+ if (er.code === "ENOSYS")
312
+ return true;
313
+ var nonroot = !process.getuid || process.getuid() !== 0;
314
+ if (nonroot) {
315
+ if (er.code === "EINVAL" || er.code === "EPERM")
316
+ return true;
317
+ }
318
+ return false;
319
+ }
320
+ }
321
+ }
322
+ });
323
+
324
+ // ../../node_modules/.pnpm/graceful-fs@4.2.11/node_modules/graceful-fs/legacy-streams.js
325
+ var require_legacy_streams = __commonJS({
326
+ "../../node_modules/.pnpm/graceful-fs@4.2.11/node_modules/graceful-fs/legacy-streams.js"(exports, module) {
327
+ "use strict";
328
+ var Stream = __require("stream").Stream;
329
+ module.exports = legacy;
330
+ function legacy(fs8) {
331
+ return {
332
+ ReadStream,
333
+ WriteStream
334
+ };
335
+ function ReadStream(path8, options) {
336
+ if (!(this instanceof ReadStream)) return new ReadStream(path8, options);
337
+ Stream.call(this);
338
+ var self = this;
339
+ this.path = path8;
340
+ this.fd = null;
341
+ this.readable = true;
342
+ this.paused = false;
343
+ this.flags = "r";
344
+ this.mode = 438;
345
+ this.bufferSize = 64 * 1024;
346
+ options = options || {};
347
+ var keys = Object.keys(options);
348
+ for (var index = 0, length = keys.length; index < length; index++) {
349
+ var key = keys[index];
350
+ this[key] = options[key];
351
+ }
352
+ if (this.encoding) this.setEncoding(this.encoding);
353
+ if (this.start !== void 0) {
354
+ if ("number" !== typeof this.start) {
355
+ throw TypeError("start must be a Number");
356
+ }
357
+ if (this.end === void 0) {
358
+ this.end = Infinity;
359
+ } else if ("number" !== typeof this.end) {
360
+ throw TypeError("end must be a Number");
361
+ }
362
+ if (this.start > this.end) {
363
+ throw new Error("start must be <= end");
364
+ }
365
+ this.pos = this.start;
366
+ }
367
+ if (this.fd !== null) {
368
+ process.nextTick(function() {
369
+ self._read();
370
+ });
371
+ return;
372
+ }
373
+ fs8.open(this.path, this.flags, this.mode, function(err, fd) {
374
+ if (err) {
375
+ self.emit("error", err);
376
+ self.readable = false;
377
+ return;
378
+ }
379
+ self.fd = fd;
380
+ self.emit("open", fd);
381
+ self._read();
382
+ });
383
+ }
384
+ function WriteStream(path8, options) {
385
+ if (!(this instanceof WriteStream)) return new WriteStream(path8, options);
386
+ Stream.call(this);
387
+ this.path = path8;
388
+ this.fd = null;
389
+ this.writable = true;
390
+ this.flags = "w";
391
+ this.encoding = "binary";
392
+ this.mode = 438;
393
+ this.bytesWritten = 0;
394
+ options = options || {};
395
+ var keys = Object.keys(options);
396
+ for (var index = 0, length = keys.length; index < length; index++) {
397
+ var key = keys[index];
398
+ this[key] = options[key];
399
+ }
400
+ if (this.start !== void 0) {
401
+ if ("number" !== typeof this.start) {
402
+ throw TypeError("start must be a Number");
403
+ }
404
+ if (this.start < 0) {
405
+ throw new Error("start must be >= zero");
406
+ }
407
+ this.pos = this.start;
408
+ }
409
+ this.busy = false;
410
+ this._queue = [];
411
+ if (this.fd === null) {
412
+ this._open = fs8.open;
413
+ this._queue.push([this._open, this.path, this.flags, this.mode, void 0]);
414
+ this.flush();
415
+ }
416
+ }
417
+ }
418
+ }
419
+ });
420
+
421
+ // ../../node_modules/.pnpm/graceful-fs@4.2.11/node_modules/graceful-fs/clone.js
422
+ var require_clone = __commonJS({
423
+ "../../node_modules/.pnpm/graceful-fs@4.2.11/node_modules/graceful-fs/clone.js"(exports, module) {
424
+ "use strict";
425
+ module.exports = clone;
426
+ var getPrototypeOf = Object.getPrototypeOf || function(obj) {
427
+ return obj.__proto__;
428
+ };
429
+ function clone(obj) {
430
+ if (obj === null || typeof obj !== "object")
431
+ return obj;
432
+ if (obj instanceof Object)
433
+ var copy = { __proto__: getPrototypeOf(obj) };
434
+ else
435
+ var copy = /* @__PURE__ */ Object.create(null);
436
+ Object.getOwnPropertyNames(obj).forEach(function(key) {
437
+ Object.defineProperty(copy, key, Object.getOwnPropertyDescriptor(obj, key));
438
+ });
439
+ return copy;
440
+ }
441
+ }
442
+ });
443
+
444
+ // ../../node_modules/.pnpm/graceful-fs@4.2.11/node_modules/graceful-fs/graceful-fs.js
445
+ var require_graceful_fs = __commonJS({
446
+ "../../node_modules/.pnpm/graceful-fs@4.2.11/node_modules/graceful-fs/graceful-fs.js"(exports, module) {
447
+ "use strict";
448
+ var fs8 = __require("fs");
449
+ var polyfills = require_polyfills();
450
+ var legacy = require_legacy_streams();
451
+ var clone = require_clone();
452
+ var util = __require("util");
453
+ var gracefulQueue;
454
+ var previousSymbol;
455
+ if (typeof Symbol === "function" && typeof Symbol.for === "function") {
456
+ gracefulQueue = /* @__PURE__ */ Symbol.for("graceful-fs.queue");
457
+ previousSymbol = /* @__PURE__ */ Symbol.for("graceful-fs.previous");
458
+ } else {
459
+ gracefulQueue = "___graceful-fs.queue";
460
+ previousSymbol = "___graceful-fs.previous";
461
+ }
462
+ function noop() {
463
+ }
464
+ function publishQueue(context, queue2) {
465
+ Object.defineProperty(context, gracefulQueue, {
466
+ get: function() {
467
+ return queue2;
468
+ }
469
+ });
470
+ }
471
+ var debug = noop;
472
+ if (util.debuglog)
473
+ debug = util.debuglog("gfs4");
474
+ else if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || ""))
475
+ debug = function() {
476
+ var m = util.format.apply(util, arguments);
477
+ m = "GFS4: " + m.split(/\n/).join("\nGFS4: ");
478
+ console.error(m);
479
+ };
480
+ if (!fs8[gracefulQueue]) {
481
+ queue = global[gracefulQueue] || [];
482
+ publishQueue(fs8, queue);
483
+ fs8.close = (function(fs$close) {
484
+ function close(fd, cb) {
485
+ return fs$close.call(fs8, fd, function(err) {
486
+ if (!err) {
487
+ resetQueue();
488
+ }
489
+ if (typeof cb === "function")
490
+ cb.apply(this, arguments);
491
+ });
492
+ }
493
+ Object.defineProperty(close, previousSymbol, {
494
+ value: fs$close
495
+ });
496
+ return close;
497
+ })(fs8.close);
498
+ fs8.closeSync = (function(fs$closeSync) {
499
+ function closeSync2(fd) {
500
+ fs$closeSync.apply(fs8, arguments);
501
+ resetQueue();
502
+ }
503
+ Object.defineProperty(closeSync2, previousSymbol, {
504
+ value: fs$closeSync
505
+ });
506
+ return closeSync2;
507
+ })(fs8.closeSync);
508
+ if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || "")) {
509
+ process.on("exit", function() {
510
+ debug(fs8[gracefulQueue]);
511
+ __require("assert").equal(fs8[gracefulQueue].length, 0);
512
+ });
513
+ }
514
+ }
515
+ var queue;
516
+ if (!global[gracefulQueue]) {
517
+ publishQueue(global, fs8[gracefulQueue]);
518
+ }
519
+ module.exports = patch(clone(fs8));
520
+ if (process.env.TEST_GRACEFUL_FS_GLOBAL_PATCH && !fs8.__patched) {
521
+ module.exports = patch(fs8);
522
+ fs8.__patched = true;
523
+ }
524
+ function patch(fs9) {
525
+ polyfills(fs9);
526
+ fs9.gracefulify = patch;
527
+ fs9.createReadStream = createReadStream2;
528
+ fs9.createWriteStream = createWriteStream;
529
+ var fs$readFile = fs9.readFile;
530
+ fs9.readFile = readFile;
531
+ function readFile(path8, options, cb) {
532
+ if (typeof options === "function")
533
+ cb = options, options = null;
534
+ return go$readFile(path8, options, cb);
535
+ function go$readFile(path9, options2, cb2, startTime) {
536
+ return fs$readFile(path9, options2, function(err) {
537
+ if (err && (err.code === "EMFILE" || err.code === "ENFILE"))
538
+ enqueue([go$readFile, [path9, options2, cb2], err, startTime || Date.now(), Date.now()]);
539
+ else {
540
+ if (typeof cb2 === "function")
541
+ cb2.apply(this, arguments);
542
+ }
543
+ });
544
+ }
545
+ }
546
+ var fs$writeFile = fs9.writeFile;
547
+ fs9.writeFile = writeFile;
548
+ function writeFile(path8, data, options, cb) {
549
+ if (typeof options === "function")
550
+ cb = options, options = null;
551
+ return go$writeFile(path8, data, options, cb);
552
+ function go$writeFile(path9, data2, options2, cb2, startTime) {
553
+ return fs$writeFile(path9, data2, options2, function(err) {
554
+ if (err && (err.code === "EMFILE" || err.code === "ENFILE"))
555
+ enqueue([go$writeFile, [path9, data2, options2, cb2], err, startTime || Date.now(), Date.now()]);
556
+ else {
557
+ if (typeof cb2 === "function")
558
+ cb2.apply(this, arguments);
559
+ }
560
+ });
561
+ }
562
+ }
563
+ var fs$appendFile = fs9.appendFile;
564
+ if (fs$appendFile)
565
+ fs9.appendFile = appendFile;
566
+ function appendFile(path8, data, options, cb) {
567
+ if (typeof options === "function")
568
+ cb = options, options = null;
569
+ return go$appendFile(path8, data, options, cb);
570
+ function go$appendFile(path9, data2, options2, cb2, startTime) {
571
+ return fs$appendFile(path9, data2, options2, function(err) {
572
+ if (err && (err.code === "EMFILE" || err.code === "ENFILE"))
573
+ enqueue([go$appendFile, [path9, data2, options2, cb2], err, startTime || Date.now(), Date.now()]);
574
+ else {
575
+ if (typeof cb2 === "function")
576
+ cb2.apply(this, arguments);
577
+ }
578
+ });
579
+ }
580
+ }
581
+ var fs$copyFile = fs9.copyFile;
582
+ if (fs$copyFile)
583
+ fs9.copyFile = copyFile;
584
+ function copyFile(src, dest, flags, cb) {
585
+ if (typeof flags === "function") {
586
+ cb = flags;
587
+ flags = 0;
588
+ }
589
+ return go$copyFile(src, dest, flags, cb);
590
+ function go$copyFile(src2, dest2, flags2, cb2, startTime) {
591
+ return fs$copyFile(src2, dest2, flags2, function(err) {
592
+ if (err && (err.code === "EMFILE" || err.code === "ENFILE"))
593
+ enqueue([go$copyFile, [src2, dest2, flags2, cb2], err, startTime || Date.now(), Date.now()]);
594
+ else {
595
+ if (typeof cb2 === "function")
596
+ cb2.apply(this, arguments);
597
+ }
598
+ });
599
+ }
600
+ }
601
+ var fs$readdir = fs9.readdir;
602
+ fs9.readdir = readdir;
603
+ var noReaddirOptionVersions = /^v[0-5]\./;
604
+ function readdir(path8, options, cb) {
605
+ if (typeof options === "function")
606
+ cb = options, options = null;
607
+ var go$readdir = noReaddirOptionVersions.test(process.version) ? function go$readdir2(path9, options2, cb2, startTime) {
608
+ return fs$readdir(path9, fs$readdirCallback(
609
+ path9,
610
+ options2,
611
+ cb2,
612
+ startTime
613
+ ));
614
+ } : function go$readdir2(path9, options2, cb2, startTime) {
615
+ return fs$readdir(path9, options2, fs$readdirCallback(
616
+ path9,
617
+ options2,
618
+ cb2,
619
+ startTime
620
+ ));
621
+ };
622
+ return go$readdir(path8, options, cb);
623
+ function fs$readdirCallback(path9, options2, cb2, startTime) {
624
+ return function(err, files) {
625
+ if (err && (err.code === "EMFILE" || err.code === "ENFILE"))
626
+ enqueue([
627
+ go$readdir,
628
+ [path9, options2, cb2],
629
+ err,
630
+ startTime || Date.now(),
631
+ Date.now()
632
+ ]);
633
+ else {
634
+ if (files && files.sort)
635
+ files.sort();
636
+ if (typeof cb2 === "function")
637
+ cb2.call(this, err, files);
638
+ }
639
+ };
640
+ }
641
+ }
642
+ if (process.version.substr(0, 4) === "v0.8") {
643
+ var legStreams = legacy(fs9);
644
+ ReadStream = legStreams.ReadStream;
645
+ WriteStream = legStreams.WriteStream;
646
+ }
647
+ var fs$ReadStream = fs9.ReadStream;
648
+ if (fs$ReadStream) {
649
+ ReadStream.prototype = Object.create(fs$ReadStream.prototype);
650
+ ReadStream.prototype.open = ReadStream$open;
651
+ }
652
+ var fs$WriteStream = fs9.WriteStream;
653
+ if (fs$WriteStream) {
654
+ WriteStream.prototype = Object.create(fs$WriteStream.prototype);
655
+ WriteStream.prototype.open = WriteStream$open;
656
+ }
657
+ Object.defineProperty(fs9, "ReadStream", {
658
+ get: function() {
659
+ return ReadStream;
660
+ },
661
+ set: function(val) {
662
+ ReadStream = val;
663
+ },
664
+ enumerable: true,
665
+ configurable: true
666
+ });
667
+ Object.defineProperty(fs9, "WriteStream", {
668
+ get: function() {
669
+ return WriteStream;
670
+ },
671
+ set: function(val) {
672
+ WriteStream = val;
673
+ },
674
+ enumerable: true,
675
+ configurable: true
676
+ });
677
+ var FileReadStream = ReadStream;
678
+ Object.defineProperty(fs9, "FileReadStream", {
679
+ get: function() {
680
+ return FileReadStream;
681
+ },
682
+ set: function(val) {
683
+ FileReadStream = val;
684
+ },
685
+ enumerable: true,
686
+ configurable: true
687
+ });
688
+ var FileWriteStream = WriteStream;
689
+ Object.defineProperty(fs9, "FileWriteStream", {
690
+ get: function() {
691
+ return FileWriteStream;
692
+ },
693
+ set: function(val) {
694
+ FileWriteStream = val;
695
+ },
696
+ enumerable: true,
697
+ configurable: true
698
+ });
699
+ function ReadStream(path8, options) {
700
+ if (this instanceof ReadStream)
701
+ return fs$ReadStream.apply(this, arguments), this;
702
+ else
703
+ return ReadStream.apply(Object.create(ReadStream.prototype), arguments);
704
+ }
705
+ function ReadStream$open() {
706
+ var that = this;
707
+ open(that.path, that.flags, that.mode, function(err, fd) {
708
+ if (err) {
709
+ if (that.autoClose)
710
+ that.destroy();
711
+ that.emit("error", err);
712
+ } else {
713
+ that.fd = fd;
714
+ that.emit("open", fd);
715
+ that.read();
716
+ }
717
+ });
718
+ }
719
+ function WriteStream(path8, options) {
720
+ if (this instanceof WriteStream)
721
+ return fs$WriteStream.apply(this, arguments), this;
722
+ else
723
+ return WriteStream.apply(Object.create(WriteStream.prototype), arguments);
724
+ }
725
+ function WriteStream$open() {
726
+ var that = this;
727
+ open(that.path, that.flags, that.mode, function(err, fd) {
728
+ if (err) {
729
+ that.destroy();
730
+ that.emit("error", err);
731
+ } else {
732
+ that.fd = fd;
733
+ that.emit("open", fd);
734
+ }
735
+ });
736
+ }
737
+ function createReadStream2(path8, options) {
738
+ return new fs9.ReadStream(path8, options);
739
+ }
740
+ function createWriteStream(path8, options) {
741
+ return new fs9.WriteStream(path8, options);
742
+ }
743
+ var fs$open = fs9.open;
744
+ fs9.open = open;
745
+ function open(path8, flags, mode, cb) {
746
+ if (typeof mode === "function")
747
+ cb = mode, mode = null;
748
+ return go$open(path8, flags, mode, cb);
749
+ function go$open(path9, flags2, mode2, cb2, startTime) {
750
+ return fs$open(path9, flags2, mode2, function(err, fd) {
751
+ if (err && (err.code === "EMFILE" || err.code === "ENFILE"))
752
+ enqueue([go$open, [path9, flags2, mode2, cb2], err, startTime || Date.now(), Date.now()]);
753
+ else {
754
+ if (typeof cb2 === "function")
755
+ cb2.apply(this, arguments);
756
+ }
757
+ });
758
+ }
759
+ }
760
+ return fs9;
761
+ }
762
+ function enqueue(elem) {
763
+ debug("ENQUEUE", elem[0].name, elem[1]);
764
+ fs8[gracefulQueue].push(elem);
765
+ retry();
766
+ }
767
+ var retryTimer;
768
+ function resetQueue() {
769
+ var now = Date.now();
770
+ for (var i = 0; i < fs8[gracefulQueue].length; ++i) {
771
+ if (fs8[gracefulQueue][i].length > 2) {
772
+ fs8[gracefulQueue][i][3] = now;
773
+ fs8[gracefulQueue][i][4] = now;
774
+ }
775
+ }
776
+ retry();
777
+ }
778
+ function retry() {
779
+ clearTimeout(retryTimer);
780
+ retryTimer = void 0;
781
+ if (fs8[gracefulQueue].length === 0)
782
+ return;
783
+ var elem = fs8[gracefulQueue].shift();
784
+ var fn = elem[0];
785
+ var args = elem[1];
786
+ var err = elem[2];
787
+ var startTime = elem[3];
788
+ var lastTime = elem[4];
789
+ if (startTime === void 0) {
790
+ debug("RETRY", fn.name, args);
791
+ fn.apply(null, args);
792
+ } else if (Date.now() - startTime >= 6e4) {
793
+ debug("TIMEOUT", fn.name, args);
794
+ var cb = args.pop();
795
+ if (typeof cb === "function")
796
+ cb.call(null, err);
797
+ } else {
798
+ var sinceAttempt = Date.now() - lastTime;
799
+ var sinceStart = Math.max(lastTime - startTime, 1);
800
+ var desiredDelay = Math.min(sinceStart * 1.2, 100);
801
+ if (sinceAttempt >= desiredDelay) {
802
+ debug("RETRY", fn.name, args);
803
+ fn.apply(null, args.concat([startTime]));
804
+ } else {
805
+ fs8[gracefulQueue].push(elem);
806
+ }
807
+ }
808
+ if (retryTimer === void 0) {
809
+ retryTimer = setTimeout(retry, 0);
810
+ }
811
+ }
812
+ }
813
+ });
814
+
815
+ // ../../node_modules/.pnpm/retry@0.12.0/node_modules/retry/lib/retry_operation.js
816
+ var require_retry_operation = __commonJS({
817
+ "../../node_modules/.pnpm/retry@0.12.0/node_modules/retry/lib/retry_operation.js"(exports, module) {
818
+ "use strict";
819
+ function RetryOperation(timeouts, options) {
820
+ if (typeof options === "boolean") {
821
+ options = { forever: options };
822
+ }
823
+ this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
824
+ this._timeouts = timeouts;
825
+ this._options = options || {};
826
+ this._maxRetryTime = options && options.maxRetryTime || Infinity;
827
+ this._fn = null;
828
+ this._errors = [];
829
+ this._attempts = 1;
830
+ this._operationTimeout = null;
831
+ this._operationTimeoutCb = null;
832
+ this._timeout = null;
833
+ this._operationStart = null;
834
+ if (this._options.forever) {
835
+ this._cachedTimeouts = this._timeouts.slice(0);
836
+ }
837
+ }
838
+ module.exports = RetryOperation;
839
+ RetryOperation.prototype.reset = function() {
840
+ this._attempts = 1;
841
+ this._timeouts = this._originalTimeouts;
842
+ };
843
+ RetryOperation.prototype.stop = function() {
844
+ if (this._timeout) {
845
+ clearTimeout(this._timeout);
846
+ }
847
+ this._timeouts = [];
848
+ this._cachedTimeouts = null;
849
+ };
850
+ RetryOperation.prototype.retry = function(err) {
851
+ if (this._timeout) {
852
+ clearTimeout(this._timeout);
853
+ }
854
+ if (!err) {
855
+ return false;
856
+ }
857
+ var currentTime = (/* @__PURE__ */ new Date()).getTime();
858
+ if (err && currentTime - this._operationStart >= this._maxRetryTime) {
859
+ this._errors.unshift(new Error("RetryOperation timeout occurred"));
860
+ return false;
861
+ }
862
+ this._errors.push(err);
863
+ var timeout = this._timeouts.shift();
864
+ if (timeout === void 0) {
865
+ if (this._cachedTimeouts) {
866
+ this._errors.splice(this._errors.length - 1, this._errors.length);
867
+ this._timeouts = this._cachedTimeouts.slice(0);
868
+ timeout = this._timeouts.shift();
869
+ } else {
870
+ return false;
871
+ }
872
+ }
873
+ var self = this;
874
+ var timer = setTimeout(function() {
875
+ self._attempts++;
876
+ if (self._operationTimeoutCb) {
877
+ self._timeout = setTimeout(function() {
878
+ self._operationTimeoutCb(self._attempts);
879
+ }, self._operationTimeout);
880
+ if (self._options.unref) {
881
+ self._timeout.unref();
882
+ }
883
+ }
884
+ self._fn(self._attempts);
885
+ }, timeout);
886
+ if (this._options.unref) {
887
+ timer.unref();
888
+ }
889
+ return true;
890
+ };
891
+ RetryOperation.prototype.attempt = function(fn, timeoutOps) {
892
+ this._fn = fn;
893
+ if (timeoutOps) {
894
+ if (timeoutOps.timeout) {
895
+ this._operationTimeout = timeoutOps.timeout;
896
+ }
897
+ if (timeoutOps.cb) {
898
+ this._operationTimeoutCb = timeoutOps.cb;
899
+ }
900
+ }
901
+ var self = this;
902
+ if (this._operationTimeoutCb) {
903
+ this._timeout = setTimeout(function() {
904
+ self._operationTimeoutCb();
905
+ }, self._operationTimeout);
906
+ }
907
+ this._operationStart = (/* @__PURE__ */ new Date()).getTime();
908
+ this._fn(this._attempts);
909
+ };
910
+ RetryOperation.prototype.try = function(fn) {
911
+ console.log("Using RetryOperation.try() is deprecated");
912
+ this.attempt(fn);
913
+ };
914
+ RetryOperation.prototype.start = function(fn) {
915
+ console.log("Using RetryOperation.start() is deprecated");
916
+ this.attempt(fn);
917
+ };
918
+ RetryOperation.prototype.start = RetryOperation.prototype.try;
919
+ RetryOperation.prototype.errors = function() {
920
+ return this._errors;
921
+ };
922
+ RetryOperation.prototype.attempts = function() {
923
+ return this._attempts;
924
+ };
925
+ RetryOperation.prototype.mainError = function() {
926
+ if (this._errors.length === 0) {
927
+ return null;
928
+ }
929
+ var counts = {};
930
+ var mainError = null;
931
+ var mainErrorCount = 0;
932
+ for (var i = 0; i < this._errors.length; i++) {
933
+ var error = this._errors[i];
934
+ var message = error.message;
935
+ var count = (counts[message] || 0) + 1;
936
+ counts[message] = count;
937
+ if (count >= mainErrorCount) {
938
+ mainError = error;
939
+ mainErrorCount = count;
940
+ }
941
+ }
942
+ return mainError;
943
+ };
944
+ }
945
+ });
946
+
947
+ // ../../node_modules/.pnpm/retry@0.12.0/node_modules/retry/lib/retry.js
948
+ var require_retry = __commonJS({
949
+ "../../node_modules/.pnpm/retry@0.12.0/node_modules/retry/lib/retry.js"(exports) {
950
+ "use strict";
951
+ var RetryOperation = require_retry_operation();
952
+ exports.operation = function(options) {
953
+ var timeouts = exports.timeouts(options);
954
+ return new RetryOperation(timeouts, {
955
+ forever: options && options.forever,
956
+ unref: options && options.unref,
957
+ maxRetryTime: options && options.maxRetryTime
958
+ });
959
+ };
960
+ exports.timeouts = function(options) {
961
+ if (options instanceof Array) {
962
+ return [].concat(options);
963
+ }
964
+ var opts = {
965
+ retries: 10,
966
+ factor: 2,
967
+ minTimeout: 1 * 1e3,
968
+ maxTimeout: Infinity,
969
+ randomize: false
970
+ };
971
+ for (var key in options) {
972
+ opts[key] = options[key];
973
+ }
974
+ if (opts.minTimeout > opts.maxTimeout) {
975
+ throw new Error("minTimeout is greater than maxTimeout");
976
+ }
977
+ var timeouts = [];
978
+ for (var i = 0; i < opts.retries; i++) {
979
+ timeouts.push(this.createTimeout(i, opts));
980
+ }
981
+ if (options && options.forever && !timeouts.length) {
982
+ timeouts.push(this.createTimeout(i, opts));
983
+ }
984
+ timeouts.sort(function(a, b) {
985
+ return a - b;
986
+ });
987
+ return timeouts;
988
+ };
989
+ exports.createTimeout = function(attempt, opts) {
990
+ var random = opts.randomize ? Math.random() + 1 : 1;
991
+ var timeout = Math.round(random * opts.minTimeout * Math.pow(opts.factor, attempt));
992
+ timeout = Math.min(timeout, opts.maxTimeout);
993
+ return timeout;
994
+ };
995
+ exports.wrap = function(obj, options, methods) {
996
+ if (options instanceof Array) {
997
+ methods = options;
998
+ options = null;
999
+ }
1000
+ if (!methods) {
1001
+ methods = [];
1002
+ for (var key in obj) {
1003
+ if (typeof obj[key] === "function") {
1004
+ methods.push(key);
1005
+ }
1006
+ }
1007
+ }
1008
+ for (var i = 0; i < methods.length; i++) {
1009
+ var method = methods[i];
1010
+ var original = obj[method];
1011
+ obj[method] = function retryWrapper(original2) {
1012
+ var op = exports.operation(options);
1013
+ var args = Array.prototype.slice.call(arguments, 1);
1014
+ var callback = args.pop();
1015
+ args.push(function(err) {
1016
+ if (op.retry(err)) {
1017
+ return;
1018
+ }
1019
+ if (err) {
1020
+ arguments[0] = op.mainError();
1021
+ }
1022
+ callback.apply(this, arguments);
1023
+ });
1024
+ op.attempt(function() {
1025
+ original2.apply(obj, args);
1026
+ });
1027
+ }.bind(obj, original);
1028
+ obj[method].options = options;
1029
+ }
1030
+ };
1031
+ }
1032
+ });
1033
+
1034
+ // ../../node_modules/.pnpm/retry@0.12.0/node_modules/retry/index.js
1035
+ var require_retry2 = __commonJS({
1036
+ "../../node_modules/.pnpm/retry@0.12.0/node_modules/retry/index.js"(exports, module) {
1037
+ "use strict";
1038
+ module.exports = require_retry();
1039
+ }
1040
+ });
1041
+
1042
+ // ../../node_modules/.pnpm/signal-exit@3.0.7/node_modules/signal-exit/signals.js
1043
+ var require_signals = __commonJS({
1044
+ "../../node_modules/.pnpm/signal-exit@3.0.7/node_modules/signal-exit/signals.js"(exports, module) {
1045
+ "use strict";
1046
+ module.exports = [
1047
+ "SIGABRT",
1048
+ "SIGALRM",
1049
+ "SIGHUP",
1050
+ "SIGINT",
1051
+ "SIGTERM"
1052
+ ];
1053
+ if (process.platform !== "win32") {
1054
+ module.exports.push(
1055
+ "SIGVTALRM",
1056
+ "SIGXCPU",
1057
+ "SIGXFSZ",
1058
+ "SIGUSR2",
1059
+ "SIGTRAP",
1060
+ "SIGSYS",
1061
+ "SIGQUIT",
1062
+ "SIGIOT"
1063
+ // should detect profiler and enable/disable accordingly.
1064
+ // see #21
1065
+ // 'SIGPROF'
1066
+ );
1067
+ }
1068
+ if (process.platform === "linux") {
1069
+ module.exports.push(
1070
+ "SIGIO",
1071
+ "SIGPOLL",
1072
+ "SIGPWR",
1073
+ "SIGSTKFLT",
1074
+ "SIGUNUSED"
1075
+ );
1076
+ }
1077
+ }
1078
+ });
1079
+
1080
+ // ../../node_modules/.pnpm/signal-exit@3.0.7/node_modules/signal-exit/index.js
1081
+ var require_signal_exit = __commonJS({
1082
+ "../../node_modules/.pnpm/signal-exit@3.0.7/node_modules/signal-exit/index.js"(exports, module) {
1083
+ "use strict";
1084
+ var process4 = global.process;
1085
+ var processOk = function(process5) {
1086
+ return process5 && typeof process5 === "object" && typeof process5.removeListener === "function" && typeof process5.emit === "function" && typeof process5.reallyExit === "function" && typeof process5.listeners === "function" && typeof process5.kill === "function" && typeof process5.pid === "number" && typeof process5.on === "function";
1087
+ };
1088
+ if (!processOk(process4)) {
1089
+ module.exports = function() {
1090
+ return function() {
1091
+ };
1092
+ };
1093
+ } else {
1094
+ assert = __require("assert");
1095
+ signals = require_signals();
1096
+ isWin = /^win/i.test(process4.platform);
1097
+ EE = __require("events");
1098
+ if (typeof EE !== "function") {
1099
+ EE = EE.EventEmitter;
1100
+ }
1101
+ if (process4.__signal_exit_emitter__) {
1102
+ emitter = process4.__signal_exit_emitter__;
1103
+ } else {
1104
+ emitter = process4.__signal_exit_emitter__ = new EE();
1105
+ emitter.count = 0;
1106
+ emitter.emitted = {};
1107
+ }
1108
+ if (!emitter.infinite) {
1109
+ emitter.setMaxListeners(Infinity);
1110
+ emitter.infinite = true;
1111
+ }
1112
+ module.exports = function(cb, opts) {
1113
+ if (!processOk(global.process)) {
1114
+ return function() {
1115
+ };
1116
+ }
1117
+ assert.equal(typeof cb, "function", "a callback must be provided for exit handler");
1118
+ if (loaded === false) {
1119
+ load();
1120
+ }
1121
+ var ev = "exit";
1122
+ if (opts && opts.alwaysLast) {
1123
+ ev = "afterexit";
1124
+ }
1125
+ var remove = function() {
1126
+ emitter.removeListener(ev, cb);
1127
+ if (emitter.listeners("exit").length === 0 && emitter.listeners("afterexit").length === 0) {
1128
+ unload();
1129
+ }
1130
+ };
1131
+ emitter.on(ev, cb);
1132
+ return remove;
1133
+ };
1134
+ unload = function unload2() {
1135
+ if (!loaded || !processOk(global.process)) {
1136
+ return;
1137
+ }
1138
+ loaded = false;
1139
+ signals.forEach(function(sig) {
1140
+ try {
1141
+ process4.removeListener(sig, sigListeners[sig]);
1142
+ } catch (er) {
1143
+ }
1144
+ });
1145
+ process4.emit = originalProcessEmit;
1146
+ process4.reallyExit = originalProcessReallyExit;
1147
+ emitter.count -= 1;
1148
+ };
1149
+ module.exports.unload = unload;
1150
+ emit = function emit2(event, code, signal) {
1151
+ if (emitter.emitted[event]) {
1152
+ return;
1153
+ }
1154
+ emitter.emitted[event] = true;
1155
+ emitter.emit(event, code, signal);
1156
+ };
1157
+ sigListeners = {};
1158
+ signals.forEach(function(sig) {
1159
+ sigListeners[sig] = function listener() {
1160
+ if (!processOk(global.process)) {
1161
+ return;
1162
+ }
1163
+ var listeners = process4.listeners(sig);
1164
+ if (listeners.length === emitter.count) {
1165
+ unload();
1166
+ emit("exit", null, sig);
1167
+ emit("afterexit", null, sig);
1168
+ if (isWin && sig === "SIGHUP") {
1169
+ sig = "SIGINT";
1170
+ }
1171
+ process4.kill(process4.pid, sig);
1172
+ }
1173
+ };
1174
+ });
1175
+ module.exports.signals = function() {
1176
+ return signals;
1177
+ };
1178
+ loaded = false;
1179
+ load = function load2() {
1180
+ if (loaded || !processOk(global.process)) {
1181
+ return;
1182
+ }
1183
+ loaded = true;
1184
+ emitter.count += 1;
1185
+ signals = signals.filter(function(sig) {
1186
+ try {
1187
+ process4.on(sig, sigListeners[sig]);
1188
+ return true;
1189
+ } catch (er) {
1190
+ return false;
1191
+ }
1192
+ });
1193
+ process4.emit = processEmit;
1194
+ process4.reallyExit = processReallyExit;
1195
+ };
1196
+ module.exports.load = load;
1197
+ originalProcessReallyExit = process4.reallyExit;
1198
+ processReallyExit = function processReallyExit2(code) {
1199
+ if (!processOk(global.process)) {
1200
+ return;
1201
+ }
1202
+ process4.exitCode = code || /* istanbul ignore next */
1203
+ 0;
1204
+ emit("exit", process4.exitCode, null);
1205
+ emit("afterexit", process4.exitCode, null);
1206
+ originalProcessReallyExit.call(process4, process4.exitCode);
1207
+ };
1208
+ originalProcessEmit = process4.emit;
1209
+ processEmit = function processEmit2(ev, arg) {
1210
+ if (ev === "exit" && processOk(global.process)) {
1211
+ if (arg !== void 0) {
1212
+ process4.exitCode = arg;
1213
+ }
1214
+ var ret = originalProcessEmit.apply(this, arguments);
1215
+ emit("exit", process4.exitCode, null);
1216
+ emit("afterexit", process4.exitCode, null);
1217
+ return ret;
1218
+ } else {
1219
+ return originalProcessEmit.apply(this, arguments);
1220
+ }
1221
+ };
1222
+ }
1223
+ var assert;
1224
+ var signals;
1225
+ var isWin;
1226
+ var EE;
1227
+ var emitter;
1228
+ var unload;
1229
+ var emit;
1230
+ var sigListeners;
1231
+ var loaded;
1232
+ var load;
1233
+ var originalProcessReallyExit;
1234
+ var processReallyExit;
1235
+ var originalProcessEmit;
1236
+ var processEmit;
1237
+ }
1238
+ });
1239
+
1240
+ // ../../node_modules/.pnpm/proper-lockfile@4.1.2/node_modules/proper-lockfile/lib/mtime-precision.js
1241
+ var require_mtime_precision = __commonJS({
1242
+ "../../node_modules/.pnpm/proper-lockfile@4.1.2/node_modules/proper-lockfile/lib/mtime-precision.js"(exports, module) {
1243
+ "use strict";
1244
+ var cacheSymbol = /* @__PURE__ */ Symbol();
1245
+ function probe(file, fs8, callback) {
1246
+ const cachedPrecision = fs8[cacheSymbol];
1247
+ if (cachedPrecision) {
1248
+ return fs8.stat(file, (err, stat) => {
1249
+ if (err) {
1250
+ return callback(err);
1251
+ }
1252
+ callback(null, stat.mtime, cachedPrecision);
1253
+ });
1254
+ }
1255
+ const mtime = new Date(Math.ceil(Date.now() / 1e3) * 1e3 + 5);
1256
+ fs8.utimes(file, mtime, mtime, (err) => {
1257
+ if (err) {
1258
+ return callback(err);
1259
+ }
1260
+ fs8.stat(file, (err2, stat) => {
1261
+ if (err2) {
1262
+ return callback(err2);
1263
+ }
1264
+ const precision = stat.mtime.getTime() % 1e3 === 0 ? "s" : "ms";
1265
+ Object.defineProperty(fs8, cacheSymbol, { value: precision });
1266
+ callback(null, stat.mtime, precision);
1267
+ });
1268
+ });
1269
+ }
1270
+ function getMtime(precision) {
1271
+ let now = Date.now();
1272
+ if (precision === "s") {
1273
+ now = Math.ceil(now / 1e3) * 1e3;
1274
+ }
1275
+ return new Date(now);
1276
+ }
1277
+ module.exports.probe = probe;
1278
+ module.exports.getMtime = getMtime;
1279
+ }
1280
+ });
1281
+
1282
+ // ../../node_modules/.pnpm/proper-lockfile@4.1.2/node_modules/proper-lockfile/lib/lockfile.js
1283
+ var require_lockfile = __commonJS({
1284
+ "../../node_modules/.pnpm/proper-lockfile@4.1.2/node_modules/proper-lockfile/lib/lockfile.js"(exports, module) {
1285
+ "use strict";
1286
+ var path8 = __require("path");
1287
+ var fs8 = require_graceful_fs();
1288
+ var retry = require_retry2();
1289
+ var onExit = require_signal_exit();
1290
+ var mtimePrecision = require_mtime_precision();
1291
+ var locks = {};
1292
+ function getLockFile(file, options) {
1293
+ return options.lockfilePath || `${file}.lock`;
1294
+ }
1295
+ function resolveCanonicalPath(file, options, callback) {
1296
+ if (!options.realpath) {
1297
+ return callback(null, path8.resolve(file));
1298
+ }
1299
+ options.fs.realpath(file, callback);
1300
+ }
1301
+ function acquireLock(file, options, callback) {
1302
+ const lockfilePath = getLockFile(file, options);
1303
+ options.fs.mkdir(lockfilePath, (err) => {
1304
+ if (!err) {
1305
+ return mtimePrecision.probe(lockfilePath, options.fs, (err2, mtime, mtimePrecision2) => {
1306
+ if (err2) {
1307
+ options.fs.rmdir(lockfilePath, () => {
1308
+ });
1309
+ return callback(err2);
1310
+ }
1311
+ callback(null, mtime, mtimePrecision2);
1312
+ });
1313
+ }
1314
+ if (err.code !== "EEXIST") {
1315
+ return callback(err);
1316
+ }
1317
+ if (options.stale <= 0) {
1318
+ return callback(Object.assign(new Error("Lock file is already being held"), { code: "ELOCKED", file }));
1319
+ }
1320
+ options.fs.stat(lockfilePath, (err2, stat) => {
1321
+ if (err2) {
1322
+ if (err2.code === "ENOENT") {
1323
+ return acquireLock(file, { ...options, stale: 0 }, callback);
1324
+ }
1325
+ return callback(err2);
1326
+ }
1327
+ if (!isLockStale(stat, options)) {
1328
+ return callback(Object.assign(new Error("Lock file is already being held"), { code: "ELOCKED", file }));
1329
+ }
1330
+ removeLock(file, options, (err3) => {
1331
+ if (err3) {
1332
+ return callback(err3);
1333
+ }
1334
+ acquireLock(file, { ...options, stale: 0 }, callback);
1335
+ });
1336
+ });
1337
+ });
1338
+ }
1339
+ function isLockStale(stat, options) {
1340
+ return stat.mtime.getTime() < Date.now() - options.stale;
1341
+ }
1342
+ function removeLock(file, options, callback) {
1343
+ options.fs.rmdir(getLockFile(file, options), (err) => {
1344
+ if (err && err.code !== "ENOENT") {
1345
+ return callback(err);
1346
+ }
1347
+ callback();
1348
+ });
1349
+ }
1350
+ function updateLock(file, options) {
1351
+ const lock3 = locks[file];
1352
+ if (lock3.updateTimeout) {
1353
+ return;
1354
+ }
1355
+ lock3.updateDelay = lock3.updateDelay || options.update;
1356
+ lock3.updateTimeout = setTimeout(() => {
1357
+ lock3.updateTimeout = null;
1358
+ options.fs.stat(lock3.lockfilePath, (err, stat) => {
1359
+ const isOverThreshold = lock3.lastUpdate + options.stale < Date.now();
1360
+ if (err) {
1361
+ if (err.code === "ENOENT" || isOverThreshold) {
1362
+ return setLockAsCompromised(file, lock3, Object.assign(err, { code: "ECOMPROMISED" }));
1363
+ }
1364
+ lock3.updateDelay = 1e3;
1365
+ return updateLock(file, options);
1366
+ }
1367
+ const isMtimeOurs = lock3.mtime.getTime() === stat.mtime.getTime();
1368
+ if (!isMtimeOurs) {
1369
+ return setLockAsCompromised(
1370
+ file,
1371
+ lock3,
1372
+ Object.assign(
1373
+ new Error("Unable to update lock within the stale threshold"),
1374
+ { code: "ECOMPROMISED" }
1375
+ )
1376
+ );
1377
+ }
1378
+ const mtime = mtimePrecision.getMtime(lock3.mtimePrecision);
1379
+ options.fs.utimes(lock3.lockfilePath, mtime, mtime, (err2) => {
1380
+ const isOverThreshold2 = lock3.lastUpdate + options.stale < Date.now();
1381
+ if (lock3.released) {
1382
+ return;
1383
+ }
1384
+ if (err2) {
1385
+ if (err2.code === "ENOENT" || isOverThreshold2) {
1386
+ return setLockAsCompromised(file, lock3, Object.assign(err2, { code: "ECOMPROMISED" }));
1387
+ }
1388
+ lock3.updateDelay = 1e3;
1389
+ return updateLock(file, options);
1390
+ }
1391
+ lock3.mtime = mtime;
1392
+ lock3.lastUpdate = Date.now();
1393
+ lock3.updateDelay = null;
1394
+ updateLock(file, options);
1395
+ });
1396
+ });
1397
+ }, lock3.updateDelay);
1398
+ if (lock3.updateTimeout.unref) {
1399
+ lock3.updateTimeout.unref();
1400
+ }
1401
+ }
1402
+ function setLockAsCompromised(file, lock3, err) {
1403
+ lock3.released = true;
1404
+ if (lock3.updateTimeout) {
1405
+ clearTimeout(lock3.updateTimeout);
1406
+ }
1407
+ if (locks[file] === lock3) {
1408
+ delete locks[file];
1409
+ }
1410
+ lock3.options.onCompromised(err);
1411
+ }
1412
+ function lock2(file, options, callback) {
1413
+ options = {
1414
+ stale: 1e4,
1415
+ update: null,
1416
+ realpath: true,
1417
+ retries: 0,
1418
+ fs: fs8,
1419
+ onCompromised: (err) => {
1420
+ throw err;
1421
+ },
1422
+ ...options
1423
+ };
1424
+ options.retries = options.retries || 0;
1425
+ options.retries = typeof options.retries === "number" ? { retries: options.retries } : options.retries;
1426
+ options.stale = Math.max(options.stale || 0, 2e3);
1427
+ options.update = options.update == null ? options.stale / 2 : options.update || 0;
1428
+ options.update = Math.max(Math.min(options.update, options.stale / 2), 1e3);
1429
+ resolveCanonicalPath(file, options, (err, file2) => {
1430
+ if (err) {
1431
+ return callback(err);
1432
+ }
1433
+ const operation = retry.operation(options.retries);
1434
+ operation.attempt(() => {
1435
+ acquireLock(file2, options, (err2, mtime, mtimePrecision2) => {
1436
+ if (operation.retry(err2)) {
1437
+ return;
1438
+ }
1439
+ if (err2) {
1440
+ return callback(operation.mainError());
1441
+ }
1442
+ const lock3 = locks[file2] = {
1443
+ lockfilePath: getLockFile(file2, options),
1444
+ mtime,
1445
+ mtimePrecision: mtimePrecision2,
1446
+ options,
1447
+ lastUpdate: Date.now()
1448
+ };
1449
+ updateLock(file2, options);
1450
+ callback(null, (releasedCallback) => {
1451
+ if (lock3.released) {
1452
+ return releasedCallback && releasedCallback(Object.assign(new Error("Lock is already released"), { code: "ERELEASED" }));
1453
+ }
1454
+ unlock(file2, { ...options, realpath: false }, releasedCallback);
1455
+ });
1456
+ });
1457
+ });
1458
+ });
1459
+ }
1460
+ function unlock(file, options, callback) {
1461
+ options = {
1462
+ fs: fs8,
1463
+ realpath: true,
1464
+ ...options
1465
+ };
1466
+ resolveCanonicalPath(file, options, (err, file2) => {
1467
+ if (err) {
1468
+ return callback(err);
1469
+ }
1470
+ const lock3 = locks[file2];
1471
+ if (!lock3) {
1472
+ return callback(Object.assign(new Error("Lock is not acquired/owned by you"), { code: "ENOTACQUIRED" }));
1473
+ }
1474
+ lock3.updateTimeout && clearTimeout(lock3.updateTimeout);
1475
+ lock3.released = true;
1476
+ delete locks[file2];
1477
+ removeLock(file2, options, callback);
1478
+ });
1479
+ }
1480
+ function check2(file, options, callback) {
1481
+ options = {
1482
+ stale: 1e4,
1483
+ realpath: true,
1484
+ fs: fs8,
1485
+ ...options
1486
+ };
1487
+ options.stale = Math.max(options.stale || 0, 2e3);
1488
+ resolveCanonicalPath(file, options, (err, file2) => {
1489
+ if (err) {
1490
+ return callback(err);
1491
+ }
1492
+ options.fs.stat(getLockFile(file2, options), (err2, stat) => {
1493
+ if (err2) {
1494
+ return err2.code === "ENOENT" ? callback(null, false) : callback(err2);
1495
+ }
1496
+ return callback(null, !isLockStale(stat, options));
1497
+ });
1498
+ });
1499
+ }
1500
+ function getLocks() {
1501
+ return locks;
1502
+ }
1503
+ onExit(() => {
1504
+ for (const file in locks) {
1505
+ const options = locks[file].options;
1506
+ try {
1507
+ options.fs.rmdirSync(getLockFile(file, options));
1508
+ } catch (e) {
1509
+ }
1510
+ }
1511
+ });
1512
+ module.exports.lock = lock2;
1513
+ module.exports.unlock = unlock;
1514
+ module.exports.check = check2;
1515
+ module.exports.getLocks = getLocks;
1516
+ }
1517
+ });
1518
+
1519
+ // ../../node_modules/.pnpm/proper-lockfile@4.1.2/node_modules/proper-lockfile/lib/adapter.js
1520
+ var require_adapter = __commonJS({
1521
+ "../../node_modules/.pnpm/proper-lockfile@4.1.2/node_modules/proper-lockfile/lib/adapter.js"(exports, module) {
1522
+ "use strict";
1523
+ var fs8 = require_graceful_fs();
1524
+ function createSyncFs(fs9) {
1525
+ const methods = ["mkdir", "realpath", "stat", "rmdir", "utimes"];
1526
+ const newFs = { ...fs9 };
1527
+ methods.forEach((method) => {
1528
+ newFs[method] = (...args) => {
1529
+ const callback = args.pop();
1530
+ let ret;
1531
+ try {
1532
+ ret = fs9[`${method}Sync`](...args);
1533
+ } catch (err) {
1534
+ return callback(err);
1535
+ }
1536
+ callback(null, ret);
1537
+ };
1538
+ });
1539
+ return newFs;
1540
+ }
1541
+ function toPromise(method) {
1542
+ return (...args) => new Promise((resolve, reject) => {
1543
+ args.push((err, result) => {
1544
+ if (err) {
1545
+ reject(err);
1546
+ } else {
1547
+ resolve(result);
1548
+ }
1549
+ });
1550
+ method(...args);
1551
+ });
1552
+ }
1553
+ function toSync(method) {
1554
+ return (...args) => {
1555
+ let err;
1556
+ let result;
1557
+ args.push((_err, _result) => {
1558
+ err = _err;
1559
+ result = _result;
1560
+ });
1561
+ method(...args);
1562
+ if (err) {
1563
+ throw err;
1564
+ }
1565
+ return result;
1566
+ };
1567
+ }
1568
+ function toSyncOptions(options) {
1569
+ options = { ...options };
1570
+ options.fs = createSyncFs(options.fs || fs8);
1571
+ if (typeof options.retries === "number" && options.retries > 0 || options.retries && typeof options.retries.retries === "number" && options.retries.retries > 0) {
1572
+ throw Object.assign(new Error("Cannot use retries with the sync api"), { code: "ESYNC" });
1573
+ }
1574
+ return options;
1575
+ }
1576
+ module.exports = {
1577
+ toPromise,
1578
+ toSync,
1579
+ toSyncOptions
1580
+ };
1581
+ }
1582
+ });
1583
+
1584
+ // ../../node_modules/.pnpm/proper-lockfile@4.1.2/node_modules/proper-lockfile/index.js
1585
+ var require_proper_lockfile = __commonJS({
1586
+ "../../node_modules/.pnpm/proper-lockfile@4.1.2/node_modules/proper-lockfile/index.js"(exports, module) {
1587
+ "use strict";
1588
+ var lockfile = require_lockfile();
1589
+ var { toPromise, toSync, toSyncOptions } = require_adapter();
1590
+ async function lock2(file, options) {
1591
+ const release = await toPromise(lockfile.lock)(file, options);
1592
+ return toPromise(release);
1593
+ }
1594
+ function lockSync(file, options) {
1595
+ const release = toSync(lockfile.lock)(file, toSyncOptions(options));
1596
+ return toSync(release);
1597
+ }
1598
+ function unlock(file, options) {
1599
+ return toPromise(lockfile.unlock)(file, options);
1600
+ }
1601
+ function unlockSync(file, options) {
1602
+ return toSync(lockfile.unlock)(file, toSyncOptions(options));
1603
+ }
1604
+ function check2(file, options) {
1605
+ return toPromise(lockfile.check)(file, options);
1606
+ }
1607
+ function checkSync(file, options) {
1608
+ return toSync(lockfile.check)(file, toSyncOptions(options));
1609
+ }
1610
+ module.exports = lock2;
1611
+ module.exports.lock = lock2;
1612
+ module.exports.unlock = unlock;
1613
+ module.exports.lockSync = lockSync;
1614
+ module.exports.unlockSync = unlockSync;
1615
+ module.exports.check = check2;
1616
+ module.exports.checkSync = checkSync;
1617
+ }
1618
+ });
1619
+
35
1620
  // ../../node_modules/.pnpm/ws@8.21.3/node_modules/ws/lib/constants.js
36
1621
  var require_constants = __commonJS({
37
1622
  "../../node_modules/.pnpm/ws@8.21.3/node_modules/ws/lib/constants.js"(exports, module) {
@@ -5247,11 +6832,111 @@ async function goalWorkspace(args, policy = {}) {
5247
6832
  }
5248
6833
  }
5249
6834
 
6835
+ // src/agent-runtime.ts
6836
+ var import_proper_lockfile = __toESM(require_proper_lockfile(), 1);
6837
+ import fs5 from "fs/promises";
6838
+ import { appendFileSync, mkdirSync, renameSync, statSync, openSync, closeSync } from "fs";
6839
+ import path5 from "path";
6840
+ import { spawn as spawn3 } from "child_process";
6841
+ var AGENT_LEASE_STALE_MS = 15e3;
6842
+ async function agentLeaseActive(directory) {
6843
+ return (0, import_proper_lockfile.check)(path5.join(directory, "execution"), {
6844
+ realpath: false,
6845
+ stale: AGENT_LEASE_STALE_MS
6846
+ });
6847
+ }
6848
+ async function tryAgentLease(directory, role = "execution") {
6849
+ await fs5.mkdir(directory, { recursive: true, mode: 448 });
6850
+ try {
6851
+ return await (0, import_proper_lockfile.lock)(path5.join(directory, role), {
6852
+ realpath: false,
6853
+ stale: AGENT_LEASE_STALE_MS,
6854
+ update: 3e3,
6855
+ retries: 0
6856
+ });
6857
+ } catch (error) {
6858
+ if (error.code === "ELOCKED") return null;
6859
+ throw error;
6860
+ }
6861
+ }
6862
+ function appendAgentEvent(directory, line) {
6863
+ try {
6864
+ mkdirSync(directory, { recursive: true, mode: 448 });
6865
+ const file = path5.join(directory, "events.log");
6866
+ if ((statSync(file, { throwIfNoEntry: false })?.size ?? 0) > 5 * 1024 * 1024) {
6867
+ renameSync(file, file + ".1");
6868
+ }
6869
+ appendFileSync(file, line.replace(/\x1b\[[0-9;]*m/g, ""), { mode: 384 });
6870
+ } catch {
6871
+ }
6872
+ }
6873
+ async function readAgentEvents(directory, cursor) {
6874
+ const file = path5.join(directory, "events.log");
6875
+ let handle;
6876
+ try {
6877
+ handle = await fs5.open(file, "r");
6878
+ const size = (await handle.stat()).size;
6879
+ const start = cursor === null ? Math.max(0, size - 16384) : cursor > size ? 0 : cursor;
6880
+ const buffer = Buffer.alloc(Math.min(size - start, 64 * 1024));
6881
+ const { bytesRead } = await handle.read(buffer, 0, buffer.length, start);
6882
+ const content = buffer.subarray(0, bytesRead).toString("utf8");
6883
+ return {
6884
+ cursor: start + bytesRead,
6885
+ text: cursor === null ? content.split("\n").slice(-21).join("\n") : content
6886
+ };
6887
+ } catch {
6888
+ return { cursor, text: "" };
6889
+ } finally {
6890
+ await handle?.close();
6891
+ }
6892
+ }
6893
+ async function superviseAgent(options) {
6894
+ let delay = options.delayMs ?? 1e3;
6895
+ while (!options.signal.aborted && await options.enabled()) {
6896
+ const startedAt = Date.now();
6897
+ const child = options.start();
6898
+ const abort = () => child.kill();
6899
+ options.signal.addEventListener("abort", abort, { once: true });
6900
+ await new Promise((resolve) => {
6901
+ child.once("error", () => resolve());
6902
+ child.once("close", () => resolve());
6903
+ if (options.signal.aborted) abort();
6904
+ });
6905
+ options.signal.removeEventListener("abort", abort);
6906
+ if (options.signal.aborted || !await options.enabled()) break;
6907
+ await new Promise((resolve) => {
6908
+ const finish = () => {
6909
+ clearTimeout(timer);
6910
+ options.signal.removeEventListener("abort", finish);
6911
+ resolve();
6912
+ };
6913
+ const timer = setTimeout(finish, delay);
6914
+ options.signal.addEventListener("abort", finish, { once: true });
6915
+ if (options.signal.aborted) finish();
6916
+ });
6917
+ delay = Date.now() - startedAt > 6e4 ? options.delayMs ?? 1e3 : Math.min(delay * 2, 3e4);
6918
+ }
6919
+ }
6920
+ function startSupervisedAgent(node, bundle, logs) {
6921
+ if (!logs) return spawn3(node, [bundle, "--agent"], { stdio: "inherit", windowsHide: true });
6922
+ mkdirSync(logs, { recursive: true, mode: 448 });
6923
+ const out = openSync(path5.join(logs, "agent.log"), "a", 384);
6924
+ let err;
6925
+ try {
6926
+ err = openSync(path5.join(logs, "agent-error.log"), "a", 384);
6927
+ return spawn3(node, [bundle, "--agent"], { stdio: ["ignore", out, err], windowsHide: true });
6928
+ } finally {
6929
+ closeSync(out);
6930
+ if (err !== void 0) closeSync(err);
6931
+ }
6932
+ }
6933
+
5250
6934
  // src/index.ts
5251
6935
  import os4 from "os";
5252
- import path6 from "path";
5253
- import fs6 from "fs/promises";
5254
- import { spawn as spawn4 } from "child_process";
6936
+ import path7 from "path";
6937
+ import fs7 from "fs/promises";
6938
+ import { spawn as spawn5 } from "child_process";
6939
+ import { randomUUID } from "crypto";
5255
6940
  import process3 from "process";
5256
6941
  import { createInterface } from "readline/promises";
5257
6942
  import { fileURLToPath } from "url";
@@ -5269,26 +6954,29 @@ var wrapper_default = import_websocket.default;
5269
6954
 
5270
6955
  // src/background.ts
5271
6956
  import os3 from "os";
5272
- import path5 from "path";
5273
- import fs5 from "fs/promises";
5274
- import { spawn as spawn3 } from "child_process";
5275
- var CONFIG_DIR = path5.join(os3.homedir(), ".remotearc");
5276
- var AGENT_DIR = path5.join(CONFIG_DIR, "agent");
5277
- var LOG_DIR = path5.join(CONFIG_DIR, "logs");
5278
- var AGENT_BUNDLE = path5.join(AGENT_DIR, "remotelink.mjs");
5279
- var AGENT_VERSION_FILE = path5.join(AGENT_DIR, "version");
5280
- var MAC_LABEL = "app.remotearc.agent";
5281
- var WINDOWS_RUN_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run";
5282
- var WINDOWS_RUN_VALUE = "Remote Arc Agent";
5283
- var LINUX_UNIT = "remotearc-agent.service";
6957
+ import path6 from "path";
6958
+ import fs6 from "fs/promises";
6959
+ import { spawn as spawn4 } from "child_process";
6960
+ var LegacyBackgroundAgentError = class extends Error {
6961
+ };
6962
+ function windowsSupervisorLaunchScript(node, bundle) {
6963
+ const quote = (value) => "'" + value.replaceAll("'", "''") + "'";
6964
+ return "Start-Process -FilePath " + quote(node) + " -ArgumentList " + quote('"' + bundle + '" --supervise') + " -WindowStyle Hidden | Out-Null";
6965
+ }
5284
6966
  async function run2(command, args, options = {}) {
5285
6967
  return new Promise((resolve, reject) => {
5286
- const child = spawn3(command, args, {
6968
+ const child = spawn4(command, args, {
5287
6969
  stdio: ["ignore", "pipe", "pipe"],
5288
6970
  windowsHide: true
5289
6971
  });
5290
6972
  let stdout = "";
5291
6973
  let stderr = "";
6974
+ const timer = setTimeout(() => {
6975
+ child.kill();
6976
+ const detail = `${command} timed out after 10 seconds`;
6977
+ if (options.ignoreFailure) resolve({ code: 124, stdout, stderr: detail });
6978
+ else reject(new Error(detail));
6979
+ }, 1e4);
5292
6980
  child.stdout?.on("data", (chunk) => {
5293
6981
  stdout += chunk.toString();
5294
6982
  });
@@ -5296,6 +6984,7 @@ async function run2(command, args, options = {}) {
5296
6984
  stderr += chunk.toString();
5297
6985
  });
5298
6986
  child.once("error", (error) => {
6987
+ clearTimeout(timer);
5299
6988
  if (options.ignoreFailure) {
5300
6989
  resolve({ code: 1, stdout, stderr: stderr || error.message });
5301
6990
  return;
@@ -5303,6 +6992,7 @@ async function run2(command, args, options = {}) {
5303
6992
  reject(error);
5304
6993
  });
5305
6994
  child.once("close", (code) => {
6995
+ clearTimeout(timer);
5306
6996
  const result = { code: code ?? 1, stdout, stderr };
5307
6997
  if ((code ?? 1) !== 0 && !options.ignoreFailure) {
5308
6998
  reject(
@@ -5316,48 +7006,70 @@ async function run2(command, args, options = {}) {
5316
7006
  });
5317
7007
  });
5318
7008
  }
5319
- function xmlEscape(value) {
5320
- return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
5321
- }
5322
- function psQuote(value) {
5323
- return "'" + value.replaceAll("'", "''") + "'";
5324
- }
5325
- async function ensureBundle(sourcePath, version) {
5326
- await fs5.mkdir(AGENT_DIR, { recursive: true });
5327
- await fs5.mkdir(LOG_DIR, { recursive: true });
5328
- if (path5.resolve(sourcePath) !== path5.resolve(AGENT_BUNDLE)) {
5329
- await fs5.copyFile(sourcePath, AGENT_BUNDLE);
5330
- if (process.platform !== "win32") {
5331
- await fs5.chmod(AGENT_BUNDLE, 448).catch(() => void 0);
7009
+ function createBackgroundController(options = {}) {
7010
+ const platform = options.platform ?? process.platform;
7011
+ const home = options.home ?? os3.homedir();
7012
+ const nodePath = options.nodePath ?? process.execPath;
7013
+ const uid = options.uid ?? process.getuid?.() ?? os3.userInfo().uid;
7014
+ const runCommand = options.run ?? run2;
7015
+ const wait = options.wait ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
7016
+ const leaseActive = options.leaseActive ?? agentLeaseActive;
7017
+ const CONFIG_DIR2 = path6.join(home, ".remotearc");
7018
+ const AGENT_DIR = path6.join(CONFIG_DIR2, "agent");
7019
+ const LOG_DIR = path6.join(CONFIG_DIR2, "logs");
7020
+ const AGENT_BUNDLE = path6.join(AGENT_DIR, "remotelink.mjs");
7021
+ const AGENT_VERSION_FILE = path6.join(AGENT_DIR, "version");
7022
+ const MAC_LABEL = "app.remotearc.agent";
7023
+ const WINDOWS_RUN_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run";
7024
+ const WINDOWS_RUN_VALUE = "Remote Arc Agent";
7025
+ const LINUX_UNIT = "remotearc-agent.service";
7026
+ function xmlEscape(value) {
7027
+ return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
7028
+ }
7029
+ function psQuote(value) {
7030
+ return "'" + value.replaceAll("'", "''") + "'";
7031
+ }
7032
+ function unitQuote(value) {
7033
+ return JSON.stringify(value).replaceAll("%", "%%");
7034
+ }
7035
+ async function ensureBundle(sourcePath, version) {
7036
+ await fs6.mkdir(AGENT_DIR, { recursive: true });
7037
+ await fs6.mkdir(LOG_DIR, { recursive: true });
7038
+ if (path6.resolve(sourcePath) !== path6.resolve(AGENT_BUNDLE)) {
7039
+ const staging = AGENT_BUNDLE + "." + process.pid + ".tmp";
7040
+ await fs6.copyFile(sourcePath, staging);
7041
+ await fs6.rename(staging, AGENT_BUNDLE);
7042
+ if (platform !== "win32") {
7043
+ await fs6.chmod(AGENT_BUNDLE, 448).catch(() => void 0);
7044
+ }
7045
+ }
7046
+ if (version) {
7047
+ await fs6.writeFile(AGENT_VERSION_FILE, version + "\n", { mode: 384 });
5332
7048
  }
5333
7049
  }
5334
- if (version) {
5335
- await fs5.writeFile(AGENT_VERSION_FILE, version + "\n", { mode: 384 });
7050
+ async function backgroundBundleVersion() {
7051
+ return fs6.readFile(AGENT_VERSION_FILE, "utf8").then((value) => value.trim() || null).catch(() => null);
5336
7052
  }
5337
- }
5338
- async function backgroundBundleVersion() {
5339
- return fs5.readFile(AGENT_VERSION_FILE, "utf8").then((value) => value.trim() || null).catch(() => null);
5340
- }
5341
- function macPlistPath() {
5342
- return path5.join(os3.homedir(), "Library", "LaunchAgents", MAC_LABEL + ".plist");
5343
- }
5344
- function linuxUnitPath() {
5345
- return path5.join(os3.homedir(), ".config", "systemd", "user", LINUX_UNIT);
5346
- }
5347
- async function enableMac(sourcePath, preserveCurrent, version) {
5348
- await ensureBundle(sourcePath, version);
5349
- const plistPath = macPlistPath();
5350
- await fs5.mkdir(path5.dirname(plistPath), { recursive: true });
5351
- const stdoutPath = path5.join(LOG_DIR, "agent.log");
5352
- const stderrPath = path5.join(LOG_DIR, "agent-error.log");
5353
- const plist = `<?xml version="1.0" encoding="UTF-8"?>
7053
+ function macPlistPath() {
7054
+ return path6.join(home, "Library", "LaunchAgents", MAC_LABEL + ".plist");
7055
+ }
7056
+ function linuxUnitPath() {
7057
+ return path6.join(home, ".config", "systemd", "user", LINUX_UNIT);
7058
+ }
7059
+ async function enableMac(sourcePath, preserveCurrent, version) {
7060
+ await ensureBundle(sourcePath, version);
7061
+ const plistPath = macPlistPath();
7062
+ await fs6.mkdir(path6.dirname(plistPath), { recursive: true });
7063
+ const stdoutPath = path6.join(LOG_DIR, "agent.log");
7064
+ const stderrPath = path6.join(LOG_DIR, "agent-error.log");
7065
+ const plist = `<?xml version="1.0" encoding="UTF-8"?>
5354
7066
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
5355
7067
  <plist version="1.0">
5356
7068
  <dict>
5357
7069
  <key>Label</key><string>${MAC_LABEL}</string>
5358
7070
  <key>ProgramArguments</key>
5359
7071
  <array>
5360
- <string>${xmlEscape(process.execPath)}</string>
7072
+ <string>${xmlEscape(nodePath)}</string>
5361
7073
  <string>${xmlEscape(AGENT_BUNDLE)}</string>
5362
7074
  <string>--agent</string>
5363
7075
  </array>
@@ -5370,7 +7082,7 @@ async function enableMac(sourcePath, preserveCurrent, version) {
5370
7082
  <key>EnvironmentVariables</key>
5371
7083
  <dict>
5372
7084
  <key>PATH</key><string>${xmlEscape(process.env.PATH || "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin")}</string>
5373
- <key>HOME</key><string>${xmlEscape(os3.homedir())}</string>
7085
+ <key>HOME</key><string>${xmlEscape(home)}</string>
5374
7086
  </dict>
5375
7087
  <key>ThrottleInterval</key><integer>5</integer>
5376
7088
  <key>StandardOutPath</key><string>${xmlEscape(stdoutPath)}</string>
@@ -5378,211 +7090,295 @@ async function enableMac(sourcePath, preserveCurrent, version) {
5378
7090
  </dict>
5379
7091
  </plist>
5380
7092
  `;
5381
- await fs5.writeFile(plistPath, plist, { mode: 384 });
5382
- const domain = `gui/${process.getuid?.() ?? os3.userInfo().uid}`;
5383
- const loaded = await run2(
5384
- "launchctl",
5385
- ["print", domain + "/" + MAC_LABEL],
5386
- { ignoreFailure: true }
5387
- );
5388
- await run2("launchctl", ["enable", domain + "/" + MAC_LABEL], {
5389
- ignoreFailure: true
5390
- });
5391
- if (!(preserveCurrent && loaded.code === 0)) {
5392
- await run2("launchctl", ["bootout", domain + "/" + MAC_LABEL], {
7093
+ await fs6.writeFile(plistPath, plist, { mode: 384 });
7094
+ const domain = `gui/${uid}`;
7095
+ const loaded = await runCommand(
7096
+ "launchctl",
7097
+ ["print", domain + "/" + MAC_LABEL],
7098
+ { ignoreFailure: true }
7099
+ );
7100
+ await runCommand("launchctl", ["enable", domain + "/" + MAC_LABEL], {
5393
7101
  ignoreFailure: true
5394
7102
  });
5395
- await run2("launchctl", ["bootstrap", domain, plistPath]);
5396
- await run2("launchctl", ["kickstart", "-k", domain + "/" + MAC_LABEL], {
7103
+ if (loaded.code !== 0) {
7104
+ const bootstrapped = await runCommand(
7105
+ "launchctl",
7106
+ ["bootstrap", domain, plistPath],
7107
+ { ignoreFailure: true }
7108
+ );
7109
+ if (bootstrapped.code !== 0) {
7110
+ const observed = await runCommand(
7111
+ "launchctl",
7112
+ ["print", domain + "/" + MAC_LABEL],
7113
+ { ignoreFailure: true }
7114
+ );
7115
+ if (observed.code !== 0) {
7116
+ throw new Error(
7117
+ "launchctl bootstrap exited with " + bootstrapped.code + ": " + (bootstrapped.stderr.trim() || bootstrapped.stdout.trim()) + ". The current terminal remains connected."
7118
+ );
7119
+ }
7120
+ }
7121
+ }
7122
+ await runCommand("launchctl", ["kickstart", domain + "/" + MAC_LABEL], {
5397
7123
  ignoreFailure: true
5398
7124
  });
7125
+ return backgroundAgentStatus2();
5399
7126
  }
5400
- return backgroundAgentStatus();
5401
- }
5402
- async function disableMac(stopCurrent) {
5403
- const plistPath = macPlistPath();
5404
- const domain = `gui/${process.getuid?.() ?? os3.userInfo().uid}`;
5405
- await run2("launchctl", ["disable", domain + "/" + MAC_LABEL], {
5406
- ignoreFailure: true
5407
- });
5408
- await fs5.rm(plistPath, { force: true });
5409
- if (stopCurrent) {
5410
- await run2("launchctl", ["bootout", domain + "/" + MAC_LABEL], {
7127
+ async function disableMac(stopCurrent) {
7128
+ const plistPath = macPlistPath();
7129
+ const domain = `gui/${uid}`;
7130
+ await runCommand("launchctl", ["disable", domain + "/" + MAC_LABEL], {
5411
7131
  ignoreFailure: true
5412
7132
  });
7133
+ await fs6.rm(plistPath, { force: true });
7134
+ if (stopCurrent) {
7135
+ await runCommand("launchctl", ["bootout", domain + "/" + MAC_LABEL], {
7136
+ ignoreFailure: true
7137
+ });
7138
+ }
7139
+ return backgroundAgentStatus2();
5413
7140
  }
5414
- return backgroundAgentStatus();
5415
- }
5416
- async function statusMac() {
5417
- const plistPath = macPlistPath();
5418
- const enabled = await fs5.access(plistPath).then(() => true).catch(() => false);
5419
- const domain = "gui/" + (process.getuid?.() ?? os3.userInfo().uid);
5420
- const result = await run2("launchctl", ["print", domain + "/" + MAC_LABEL], { ignoreFailure: true });
5421
- const pidMatch = result.stdout.match(/\\bpid = (\\d+)/);
5422
- const pid = pidMatch ? Number(pidMatch[1]) : null;
5423
- const active = result.code === 0 && pid !== null && /\\bstate = running\\b/i.test(result.stdout);
5424
- return {
5425
- supported: true,
5426
- enabled,
5427
- active,
5428
- service: "launchd",
5429
- pid,
5430
- version: await backgroundBundleVersion(),
5431
- detail: enabled && !active ? "Background connection is configured but the launchd agent is not currently running." : void 0
5432
- };
5433
- }
5434
- async function enableLinux(sourcePath, preserveCurrent, version) {
5435
- await ensureBundle(sourcePath, version);
5436
- const unitPath = linuxUnitPath();
5437
- await fs5.mkdir(path5.dirname(unitPath), { recursive: true });
5438
- const unit = `[Unit]
7141
+ async function statusMac() {
7142
+ const plistPath = macPlistPath();
7143
+ const installed = await fs6.access(plistPath).then(() => true).catch(() => false);
7144
+ const domain = "gui/" + uid;
7145
+ const result = await runCommand(
7146
+ "launchctl",
7147
+ ["print", domain + "/" + MAC_LABEL],
7148
+ { ignoreFailure: true }
7149
+ );
7150
+ const disabled = await runCommand("launchctl", ["print-disabled", domain], {
7151
+ ignoreFailure: true
7152
+ });
7153
+ const enabled = installed && !disabled.stdout.includes('"' + MAC_LABEL + '" => true');
7154
+ const pidMatch = result.stdout.match(/\bpid = (\d+)/);
7155
+ const pid = pidMatch ? Number(pidMatch[1]) : null;
7156
+ const active = result.code === 0 && pid !== null && /\bstate = running\b/i.test(result.stdout);
7157
+ return {
7158
+ supported: true,
7159
+ enabled,
7160
+ active,
7161
+ service: "launchd",
7162
+ pid,
7163
+ workerPid: pid,
7164
+ version: await backgroundBundleVersion(),
7165
+ detail: enabled && !active ? "Background connection is configured but the launchd agent is not currently running." : void 0
7166
+ };
7167
+ }
7168
+ async function enableLinux(sourcePath, preserveCurrent, version) {
7169
+ await ensureBundle(sourcePath, version);
7170
+ const unitPath = linuxUnitPath();
7171
+ await fs6.mkdir(path6.dirname(unitPath), { recursive: true });
7172
+ const unit = `[Unit]
5439
7173
  Description=Remote Arc background agent
5440
7174
  After=network-online.target
5441
7175
  Wants=network-online.target
5442
7176
 
5443
7177
  [Service]
5444
7178
  Type=simple
5445
- ExecStart=${process.execPath} ${AGENT_BUNDLE} --agent
7179
+ ExecStart=:${unitQuote(nodePath)} ${unitQuote(AGENT_BUNDLE)} --agent
5446
7180
  Restart=on-failure
5447
7181
  RestartSec=3
5448
7182
  Environment=NODE_ENV=production
7183
+ Environment=${unitQuote("PATH=" + (process.env.PATH || "/usr/local/bin:/usr/bin:/bin"))}
7184
+ Environment=${unitQuote("HOME=" + home)}
5449
7185
 
5450
7186
  [Install]
5451
7187
  WantedBy=default.target
5452
7188
  `;
5453
- await fs5.writeFile(unitPath, unit, { mode: 384 });
5454
- await run2("systemctl", ["--user", "daemon-reload"]);
5455
- await run2(
5456
- "systemctl",
5457
- preserveCurrent ? ["--user", "enable", LINUX_UNIT] : ["--user", "enable", "--now", LINUX_UNIT]
5458
- );
5459
- return backgroundAgentStatus();
5460
- }
5461
- async function disableLinux(stopCurrent) {
5462
- await run2(
5463
- "systemctl",
5464
- stopCurrent ? ["--user", "disable", "--now", LINUX_UNIT] : ["--user", "disable", LINUX_UNIT],
5465
- { ignoreFailure: true }
5466
- );
5467
- await fs5.rm(linuxUnitPath(), { force: true });
5468
- if (stopCurrent) {
5469
- await run2("systemctl", ["--user", "daemon-reload"], {
5470
- ignoreFailure: true
5471
- });
7189
+ await fs6.writeFile(unitPath, unit, { mode: 384 });
7190
+ await runCommand("systemctl", ["--user", "daemon-reload"]);
7191
+ await runCommand("systemctl", ["--user", "enable", "--now", LINUX_UNIT]);
7192
+ return backgroundAgentStatus2();
5472
7193
  }
5473
- return backgroundAgentStatus();
5474
- }
5475
- async function statusLinux() {
5476
- const enabledResult = await run2("systemctl", ["--user", "is-enabled", LINUX_UNIT], { ignoreFailure: true });
5477
- const activeResult = await run2("systemctl", ["--user", "is-active", LINUX_UNIT], { ignoreFailure: true });
5478
- const pidResult = await run2("systemctl", ["--user", "show", LINUX_UNIT, "--property", "MainPID", "--value"], { ignoreFailure: true });
5479
- const supported = !enabledResult.stderr.toLowerCase().includes("failed to connect to bus");
5480
- const pidValue = Number(pidResult.stdout.trim());
5481
- return {
5482
- supported,
5483
- enabled: enabledResult.code === 0,
5484
- active: activeResult.code === 0 && Number.isFinite(pidValue) && pidValue > 0,
5485
- service: supported ? "systemd-user" : "unsupported",
5486
- pid: Number.isFinite(pidValue) && pidValue > 0 ? pidValue : null,
5487
- version: await backgroundBundleVersion(),
5488
- detail: supported ? void 0 : "systemd user services are unavailable"
5489
- };
5490
- }
5491
- async function powershell(script, ignoreFailure = false) {
5492
- const utf8Script = "$OutputEncoding = [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new(); " + script;
5493
- return run2(
5494
- "powershell.exe",
5495
- ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", utf8Script],
5496
- { ignoreFailure }
5497
- );
5498
- }
5499
- async function windowsAgentPid() {
5500
- const escapedBundle = AGENT_BUNDLE.replaceAll("'", "''");
5501
- const script = "$p = Get-CimInstance Win32_Process | Where-Object { $_.Name -ieq 'node.exe' -and $_.CommandLine -like '*" + escapedBundle + "*' -and $_.CommandLine -like '*--agent*' } | Select-Object -First 1 -ExpandProperty ProcessId; if ($null -ne $p) { Write-Output $p }";
5502
- const result = await powershell(script, true);
5503
- const pid = Number(result.stdout.trim());
5504
- return Number.isFinite(pid) && pid > 0 ? pid : null;
5505
- }
5506
- async function enableWindows(sourcePath, preserveCurrent, version) {
5507
- await ensureBundle(sourcePath, version);
5508
- const command = '"' + process.execPath + '" "' + AGENT_BUNDLE + '" --agent';
5509
- await run2("reg.exe", [
5510
- "add",
5511
- WINDOWS_RUN_KEY,
5512
- "/v",
5513
- WINDOWS_RUN_VALUE,
5514
- "/t",
5515
- "REG_SZ",
5516
- "/d",
5517
- command,
5518
- "/f"
5519
- ]);
5520
- if (!preserveCurrent && !await windowsAgentPid()) {
5521
- const stdoutPath = path5.join(LOG_DIR, "agent.log");
5522
- const stderrPath = path5.join(LOG_DIR, "agent-error.log");
5523
- await powershell(
5524
- "Start-Process -FilePath " + psQuote(process.execPath) + " -ArgumentList @(" + psQuote(AGENT_BUNDLE) + ", '--agent') -WindowStyle Hidden -RedirectStandardOutput " + psQuote(stdoutPath) + " -RedirectStandardError " + psQuote(stderrPath) + " | Out-Null"
7194
+ async function disableLinux(stopCurrent) {
7195
+ await runCommand(
7196
+ "systemctl",
7197
+ stopCurrent ? ["--user", "disable", "--now", LINUX_UNIT] : ["--user", "disable", LINUX_UNIT],
7198
+ { ignoreFailure: true }
5525
7199
  );
5526
- await new Promise((resolve) => setTimeout(resolve, 350));
7200
+ await fs6.rm(linuxUnitPath(), { force: true });
7201
+ if (stopCurrent) {
7202
+ await runCommand("systemctl", ["--user", "daemon-reload"], {
7203
+ ignoreFailure: true
7204
+ });
7205
+ }
7206
+ return backgroundAgentStatus2();
5527
7207
  }
5528
- return backgroundAgentStatus();
5529
- }
5530
- async function disableWindows(stopCurrent) {
5531
- await run2("reg.exe", ["delete", WINDOWS_RUN_KEY, "/v", WINDOWS_RUN_VALUE, "/f"], { ignoreFailure: true });
5532
- if (stopCurrent) {
7208
+ async function statusLinux() {
7209
+ const enabledResult = await runCommand(
7210
+ "systemctl",
7211
+ ["--user", "is-enabled", LINUX_UNIT],
7212
+ { ignoreFailure: true }
7213
+ );
7214
+ const activeResult = await runCommand(
7215
+ "systemctl",
7216
+ ["--user", "is-active", LINUX_UNIT],
7217
+ { ignoreFailure: true }
7218
+ );
7219
+ const pidResult = await runCommand(
7220
+ "systemctl",
7221
+ ["--user", "show", LINUX_UNIT, "--property", "MainPID", "--value"],
7222
+ { ignoreFailure: true }
7223
+ );
7224
+ const supported = !enabledResult.stderr.toLowerCase().includes("failed to connect to bus");
7225
+ const pidValue = Number(pidResult.stdout.trim());
7226
+ return {
7227
+ supported,
7228
+ enabled: enabledResult.code === 0,
7229
+ active: activeResult.code === 0 && Number.isFinite(pidValue) && pidValue > 0,
7230
+ service: supported ? "systemd-user" : "unsupported",
7231
+ pid: Number.isFinite(pidValue) && pidValue > 0 ? pidValue : null,
7232
+ workerPid: Number.isFinite(pidValue) && pidValue > 0 ? pidValue : null,
7233
+ version: await backgroundBundleVersion(),
7234
+ detail: supported ? void 0 : "systemd user services are unavailable"
7235
+ };
7236
+ }
7237
+ async function powershell(script, ignoreFailure = false) {
7238
+ const utf8Script = "$OutputEncoding = [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new(); " + script;
7239
+ return runCommand(
7240
+ "powershell.exe",
7241
+ [
7242
+ "-NoProfile",
7243
+ "-NonInteractive",
7244
+ "-ExecutionPolicy",
7245
+ "Bypass",
7246
+ "-Command",
7247
+ utf8Script
7248
+ ],
7249
+ { ignoreFailure }
7250
+ );
7251
+ }
7252
+ async function windowsAgentPid(role = "--supervise") {
7253
+ const script = "$p = Get-CimInstance Win32_Process | Where-Object { $_.Name -ieq 'node.exe' -and $_.ExecutablePath -ieq " + psQuote(nodePath) + ` -and $_.CommandLine -match ('^"?' + [regex]::Escape(` + psQuote(nodePath) + `) + '"?\\s+"?' + [regex]::Escape(` + psQuote(AGENT_BUNDLE) + `) + '"?\\s+` + role + "(?:\\s|$)') } | Select-Object -First 1 -ExpandProperty ProcessId; if ($null -ne $p) { Write-Output $p }";
7254
+ const result = await powershell(script, true);
7255
+ const pid = Number(result.stdout.trim());
7256
+ return Number.isFinite(pid) && pid > 0 ? pid : null;
7257
+ }
7258
+ async function enableWindows(sourcePath, preserveCurrent, version) {
7259
+ await ensureBundle(sourcePath, version);
7260
+ const start = windowsSupervisorLaunchScript(nodePath, AGENT_BUNDLE);
7261
+ const encoded = Buffer.from(start, "utf16le").toString("base64");
7262
+ const command = "powershell.exe -NoProfile -NonInteractive -WindowStyle Hidden -EncodedCommand " + encoded;
7263
+ await runCommand("reg.exe", [
7264
+ "add",
7265
+ WINDOWS_RUN_KEY,
7266
+ "/v",
7267
+ WINDOWS_RUN_VALUE,
7268
+ "/t",
7269
+ "REG_SZ",
7270
+ "/d",
7271
+ command,
7272
+ "/f"
7273
+ ]);
7274
+ if (!await windowsAgentPid()) await powershell(start);
7275
+ return backgroundAgentStatus2();
7276
+ }
7277
+ async function disableWindows(stopCurrent) {
7278
+ await runCommand(
7279
+ "reg.exe",
7280
+ ["delete", WINDOWS_RUN_KEY, "/v", WINDOWS_RUN_VALUE, "/f"],
7281
+ { ignoreFailure: true }
7282
+ );
7283
+ if (stopCurrent) {
7284
+ for (const role of ["--agent", "--supervise"]) {
7285
+ const pid = await windowsAgentPid(role);
7286
+ if (pid && pid !== process.pid)
7287
+ await powershell(
7288
+ "Stop-Process -Id " + pid + " -Force -ErrorAction SilentlyContinue",
7289
+ true
7290
+ );
7291
+ }
7292
+ }
7293
+ return backgroundAgentStatus2();
7294
+ }
7295
+ async function statusWindows() {
7296
+ const enabledResult = await runCommand(
7297
+ "reg.exe",
7298
+ ["query", WINDOWS_RUN_KEY, "/v", WINDOWS_RUN_VALUE],
7299
+ { ignoreFailure: true }
7300
+ );
5533
7301
  const pid = await windowsAgentPid();
5534
- if (pid) {
5535
- await powershell("Stop-Process -Id " + pid + " -Force -ErrorAction SilentlyContinue", true);
7302
+ return {
7303
+ supported: true,
7304
+ enabled: enabledResult.code === 0,
7305
+ active: pid !== null,
7306
+ service: "registry-run",
7307
+ pid,
7308
+ workerPid: await windowsAgentPid("--agent"),
7309
+ version: await backgroundBundleVersion(),
7310
+ detail: enabledResult.code === 0 && pid === null ? "Login startup is configured, but its supervisor is not running; recovery is unavailable." : void 0
7311
+ };
7312
+ }
7313
+ async function backgroundAgentStatus2() {
7314
+ if (platform === "darwin") return statusMac();
7315
+ if (platform === "win32") return statusWindows();
7316
+ if (platform === "linux") return statusLinux();
7317
+ return {
7318
+ supported: false,
7319
+ enabled: false,
7320
+ active: false,
7321
+ service: "unsupported",
7322
+ detail: "unsupported operating system"
7323
+ };
7324
+ }
7325
+ async function enableBackgroundAgent2(sourcePath, options2 = {}) {
7326
+ const preserveCurrent = options2.preserveCurrent ?? false;
7327
+ const before = await backgroundAgentStatus2();
7328
+ if (before.workerPid && before.workerPid !== process.pid) {
7329
+ let participates = await leaseActive(AGENT_DIR);
7330
+ for (let attempt = 0; !participates && attempt < 6; attempt++) {
7331
+ await wait(500);
7332
+ participates = await leaseActive(AGENT_DIR);
7333
+ }
7334
+ if (!participates)
7335
+ throw new LegacyBackgroundAgentError(
7336
+ "An existing background Agent does not hold the execution lease. Stop the older Agent locally before upgrading; this launch will not create a competing executor."
7337
+ );
7338
+ }
7339
+ if (platform === "darwin")
7340
+ await enableMac(sourcePath, preserveCurrent, options2.version);
7341
+ else if (platform === "win32")
7342
+ await enableWindows(sourcePath, preserveCurrent, options2.version);
7343
+ else if (platform === "linux")
7344
+ await enableLinux(sourcePath, preserveCurrent, options2.version);
7345
+ else return backgroundAgentStatus2();
7346
+ for (let attempt = 0; attempt < 20; attempt++) {
7347
+ const status2 = await backgroundAgentStatus2();
7348
+ if (status2.enabled && status2.active) return status2;
7349
+ if (!status2.supported) return status2;
7350
+ await wait(200);
5536
7351
  }
7352
+ const status = await backgroundAgentStatus2();
7353
+ throw new Error(
7354
+ status.detail || "Background supervisor did not start; the current terminal remains connected."
7355
+ );
7356
+ }
7357
+ async function disableBackgroundAgent2(options2 = {}) {
7358
+ const stopCurrent = options2.stopCurrent ?? true;
7359
+ if (platform === "darwin") return disableMac(stopCurrent);
7360
+ if (platform === "win32") return disableWindows(stopCurrent);
7361
+ if (platform === "linux") return disableLinux(stopCurrent);
7362
+ return backgroundAgentStatus2();
5537
7363
  }
5538
- return backgroundAgentStatus();
5539
- }
5540
- async function statusWindows() {
5541
- const enabledResult = await run2("reg.exe", ["query", WINDOWS_RUN_KEY, "/v", WINDOWS_RUN_VALUE], { ignoreFailure: true });
5542
- const pid = await windowsAgentPid();
5543
- return {
5544
- supported: true,
5545
- enabled: enabledResult.code === 0,
5546
- active: pid !== null,
5547
- service: "registry-run",
5548
- pid,
5549
- version: await backgroundBundleVersion(),
5550
- detail: enabledResult.code === 0 && pid === null ? "Background connection is configured but the user-level agent is not currently running." : void 0
5551
- };
5552
- }
5553
- async function backgroundAgentStatus() {
5554
- if (process.platform === "darwin") return statusMac();
5555
- if (process.platform === "win32") return statusWindows();
5556
- if (process.platform === "linux") return statusLinux();
5557
7364
  return {
5558
- supported: false,
5559
- enabled: false,
5560
- active: false,
5561
- service: "unsupported",
5562
- detail: "unsupported operating system"
7365
+ backgroundAgentStatus: backgroundAgentStatus2,
7366
+ enableBackgroundAgent: enableBackgroundAgent2,
7367
+ disableBackgroundAgent: disableBackgroundAgent2
5563
7368
  };
5564
7369
  }
5565
- async function enableBackgroundAgent(sourcePath, options = {}) {
5566
- const preserveCurrent = options.preserveCurrent ?? false;
5567
- if (process.platform === "darwin") return enableMac(sourcePath, preserveCurrent, options.version);
5568
- if (process.platform === "win32") return enableWindows(sourcePath, preserveCurrent, options.version);
5569
- if (process.platform === "linux") return enableLinux(sourcePath, preserveCurrent, options.version);
5570
- return backgroundAgentStatus();
5571
- }
5572
- async function disableBackgroundAgent(options = {}) {
5573
- const stopCurrent = options.stopCurrent ?? true;
5574
- if (process.platform === "darwin") return disableMac(stopCurrent);
5575
- if (process.platform === "win32") return disableWindows(stopCurrent);
5576
- if (process.platform === "linux") return disableLinux(stopCurrent);
5577
- return backgroundAgentStatus();
5578
- }
7370
+ var {
7371
+ backgroundAgentStatus,
7372
+ enableBackgroundAgent,
7373
+ disableBackgroundAgent
7374
+ } = createBackgroundController();
5579
7375
 
5580
7376
  // src/index.ts
5581
- var VERSION = "0.4.3";
7377
+ var VERSION = "0.4.4";
5582
7378
  var DEFAULT_ORIGIN = "https://mcp.remotearc.app";
5583
- var CONFIG_DIR2 = path6.join(os4.homedir(), ".remotearc");
5584
- var CONFIG_PATH = path6.join(CONFIG_DIR2, "config.json");
5585
- var LEGACY_CONFIG_PATH = path6.join(os4.homedir(), ".remote-link", "config.json");
7379
+ var CONFIG_DIR = path7.join(os4.homedir(), ".remotearc");
7380
+ var CONFIG_PATH = path7.join(CONFIG_DIR, "config.json");
7381
+ var LEGACY_CONFIG_PATH = path7.join(os4.homedir(), ".remote-link", "config.json");
5586
7382
  var SELF_PATH = fileURLToPath(import.meta.url);
5587
7383
  var RevokedDeviceCredentialError = class extends Error {
5588
7384
  constructor() {
@@ -5696,6 +7492,8 @@ function nowTime() {
5696
7492
  function logLine(level, message) {
5697
7493
  const icon = level === "success" ? green("\u2713") : level === "warn" ? yellow("!") : level === "error" ? red("\xD7") : level === "event" ? cyan("\u2192") : cyan("\u2022");
5698
7494
  process3.stdout.write(`${dim(nowTime())} ${icon} ${message}
7495
+ `);
7496
+ appendAgentEvent(path7.join(CONFIG_DIR, "logs"), `${nowTime()} ${level} ${message}
5699
7497
  `);
5700
7498
  }
5701
7499
  function banner() {
@@ -5718,11 +7516,11 @@ function modeLabel(mode) {
5718
7516
  }
5719
7517
  async function readConfig() {
5720
7518
  try {
5721
- return JSON.parse(await fs6.readFile(CONFIG_PATH, "utf8"));
7519
+ return JSON.parse(await fs7.readFile(CONFIG_PATH, "utf8"));
5722
7520
  } catch {
5723
7521
  try {
5724
7522
  const legacy = JSON.parse(
5725
- await fs6.readFile(LEGACY_CONFIG_PATH, "utf8")
7523
+ await fs7.readFile(LEGACY_CONFIG_PATH, "utf8")
5726
7524
  );
5727
7525
  await writeConfig(legacy);
5728
7526
  logLine("success", "Migrated existing Remote Link pairing to Remote Arc.");
@@ -5733,22 +7531,26 @@ async function readConfig() {
5733
7531
  }
5734
7532
  }
5735
7533
  async function writeConfig(config) {
5736
- await fs6.mkdir(CONFIG_DIR2, { recursive: true });
5737
- await fs6.writeFile(CONFIG_PATH, JSON.stringify(config, null, 2) + "\n", {
5738
- mode: 384
5739
- });
7534
+ await fs7.mkdir(CONFIG_DIR, { recursive: true, mode: 448 });
7535
+ const staging = CONFIG_PATH + "." + randomUUID() + ".tmp";
7536
+ try {
7537
+ await fs7.writeFile(staging, JSON.stringify(config, null, 2) + "\n", { mode: 384 });
7538
+ await fs7.rename(staging, CONFIG_PATH);
7539
+ } finally {
7540
+ await fs7.rm(staging, { force: true });
7541
+ }
5740
7542
  }
5741
7543
  async function resetConfig() {
5742
7544
  await Promise.all([
5743
- fs6.rm(CONFIG_PATH, { force: true }),
5744
- fs6.rm(LEGACY_CONFIG_PATH, { force: true })
7545
+ fs7.rm(CONFIG_PATH, { force: true }),
7546
+ fs7.rm(LEGACY_CONFIG_PATH, { force: true })
5745
7547
  ]);
5746
7548
  logLine("success", "Removed saved device credentials.");
5747
7549
  }
5748
7550
  function openBrowser(url) {
5749
7551
  const command = process3.platform === "win32" ? "cmd" : process3.platform === "darwin" ? "open" : "xdg-open";
5750
7552
  const args = process3.platform === "win32" ? ["/c", "start", "", url] : [url];
5751
- const child = spawn4(command, args, {
7553
+ const child = spawn5(command, args, {
5752
7554
  detached: true,
5753
7555
  stdio: "ignore"
5754
7556
  });
@@ -5855,9 +7657,13 @@ async function connectAgent(config) {
5855
7657
  }
5856
7658
  const wsUrl = new URL("/agent", config.origin.replace(/^http/, "ws"));
5857
7659
  let stopped = false;
7660
+ let rePair = false;
5858
7661
  let backoff = 1e3;
7662
+ let currentSocket = null;
7663
+ let backgroundSettings = Promise.resolve();
5859
7664
  const shutdown = async () => {
5860
7665
  stopped = true;
7666
+ currentSocket?.terminate();
5861
7667
  keepAwake.close();
5862
7668
  await core.close().catch(() => void 0);
5863
7669
  };
@@ -5870,6 +7676,15 @@ async function connectAgent(config) {
5870
7676
  Authorization: "Bearer " + config.deviceToken
5871
7677
  }
5872
7678
  });
7679
+ currentSocket = ws;
7680
+ const send = (message) => {
7681
+ if (ws.readyState !== wrapper_default.OPEN) return;
7682
+ try {
7683
+ ws.send(JSON.stringify(message));
7684
+ } catch {
7685
+ ws.terminate();
7686
+ }
7687
+ };
5873
7688
  const absorbSocketError = () => void 0;
5874
7689
  ws.on("error", absorbSocketError);
5875
7690
  try {
@@ -5905,29 +7720,48 @@ async function connectAgent(config) {
5905
7720
  ws.once("unexpected-response", onUnexpectedResponse);
5906
7721
  });
5907
7722
  backoff = 1e3;
5908
- ws.send(
5909
- JSON.stringify({
5910
- type: "hello",
5911
- device: {
5912
- id: config.deviceId,
5913
- name: config.deviceName,
5914
- platform: process3.platform,
5915
- arch: process3.arch,
5916
- hostname: os4.hostname(),
5917
- agentVersion: VERSION,
5918
- pid: process3.pid,
5919
- backgroundProcess: argFlag("--agent"),
5920
- connectedAt: (/* @__PURE__ */ new Date()).toISOString()
5921
- },
5922
- tools: [...tools.map((tool) => tool.name), ...internalTools],
5923
- capabilities: [
5924
- "native_core_v1",
5925
- "device_policy_v1",
5926
- "undo_history_v1",
5927
- "background_agent_v1"
5928
- ]
5929
- })
5930
- );
7723
+ const connectedAt = (/* @__PURE__ */ new Date()).toISOString();
7724
+ const publishPresence = async () => {
7725
+ const saved = await readConfig();
7726
+ if (!saved || saved.deviceId !== config.deviceId || saved.deviceToken !== config.deviceToken) {
7727
+ logLine("warn", "The local pairing changed or was removed; stopping this execution owner.");
7728
+ await shutdown();
7729
+ return;
7730
+ }
7731
+ const local = await backgroundAgentStatus().catch(() => null);
7732
+ const recovery = local ? { ...local, enabled: saved?.backgroundEnabled === true && local.enabled && local.active } : null;
7733
+ if (ws.readyState !== wrapper_default.OPEN) return;
7734
+ ws.send(
7735
+ JSON.stringify({
7736
+ type: "hello",
7737
+ device: {
7738
+ id: config.deviceId,
7739
+ name: config.deviceName,
7740
+ platform: process3.platform,
7741
+ arch: process3.arch,
7742
+ hostname: os4.hostname(),
7743
+ agentVersion: VERSION,
7744
+ pid: process3.pid,
7745
+ backgroundProcess: argFlag("--agent"),
7746
+ connectedAt,
7747
+ recoveryEnabled: recovery?.enabled ?? false,
7748
+ supervisorActive: recovery?.active,
7749
+ supervisorPid: recovery?.pid,
7750
+ supervisorService: recovery?.service
7751
+ },
7752
+ tools: [...tools.map((tool) => tool.name), ...internalTools],
7753
+ capabilities: [
7754
+ "native_core_v1",
7755
+ "device_policy_v1",
7756
+ "undo_history_v1",
7757
+ "background_agent_v1",
7758
+ "background_recovery_v2"
7759
+ ]
7760
+ })
7761
+ );
7762
+ };
7763
+ await publishPresence();
7764
+ const presenceTimer = setInterval(() => void publishPresence().catch(() => void 0), 3e4);
5931
7765
  logLine("success", `Relay connected \xB7 ${bold(config.deviceName)} \xB7 ${config.mode} mode`);
5932
7766
  logLine("success", "Device presence published.");
5933
7767
  process3.stdout.write(" " + dim(config.origin) + "\n");
@@ -5942,7 +7776,7 @@ async function connectAgent(config) {
5942
7776
  "content-type": "application/json"
5943
7777
  },
5944
7778
  body: JSON.stringify({
5945
- background_enabled: background?.enabled,
7779
+ background_enabled: background ? (await readConfig())?.backgroundEnabled === true && background.enabled && background.active : void 0,
5946
7780
  background_process: argFlag("--agent"),
5947
7781
  background_service: background?.service
5948
7782
  })
@@ -6011,29 +7845,32 @@ async function connectAgent(config) {
6011
7845
  if (typeof enabled !== "boolean") {
6012
7846
  throw new Error("enabled must be a boolean");
6013
7847
  }
6014
- config.backgroundEnabled = enabled;
6015
- await writeConfig(config);
6016
- if (enabled) {
6017
- const current = await backgroundAgentStatus();
6018
- result = current.enabled && current.active ? { ...current, desired_enabled: true } : {
6019
- ...await enableBackgroundAgent(SELF_PATH, {
6020
- preserveCurrent: argFlag("--agent"),
6021
- version: VERSION
6022
- }),
6023
- desired_enabled: true
6024
- };
6025
- exitAfterResponse = !argFlag("--agent");
6026
- } else {
6027
- const stopCurrent = message.arguments?.stop_current === true;
6028
- const runningAsBackgroundAgent = argFlag("--agent");
6029
- result = {
6030
- ...await disableBackgroundAgent({
6031
- stopCurrent: stopCurrent && !runningAsBackgroundAgent
6032
- }),
6033
- desired_enabled: false
6034
- };
6035
- exitAfterResponse = stopCurrent && runningAsBackgroundAgent;
6036
- }
7848
+ const change = backgroundSettings.then(async () => {
7849
+ config = { ...await readConfig() || config, backgroundEnabled: enabled };
7850
+ await writeConfig(config);
7851
+ if (enabled) {
7852
+ const current = await backgroundAgentStatus();
7853
+ result = current.enabled && current.active ? { ...current, desired_enabled: true } : {
7854
+ ...await enableBackgroundAgent(SELF_PATH, {
7855
+ preserveCurrent: argFlag("--agent"),
7856
+ version: VERSION
7857
+ }),
7858
+ desired_enabled: true
7859
+ };
7860
+ } else {
7861
+ const stopCurrent = message.arguments?.stop_current === true;
7862
+ const runningAsBackgroundAgent = argFlag("--agent");
7863
+ result = {
7864
+ ...await disableBackgroundAgent({
7865
+ stopCurrent: stopCurrent && !runningAsBackgroundAgent
7866
+ }),
7867
+ desired_enabled: false
7868
+ };
7869
+ exitAfterResponse = stopCurrent && runningAsBackgroundAgent;
7870
+ }
7871
+ });
7872
+ backgroundSettings = change.catch(() => void 0);
7873
+ await change;
6037
7874
  } else {
6038
7875
  result = await core.call(
6039
7876
  message.tool,
@@ -6042,30 +7879,31 @@ async function connectAgent(config) {
6042
7879
  );
6043
7880
  }
6044
7881
  logLine("success", `tool.done ${message.tool} \xB7 ${Date.now() - callStarted}ms`);
6045
- ws.send(
6046
- JSON.stringify({
6047
- type: "result",
6048
- id: message.id,
6049
- result
6050
- })
6051
- );
7882
+ send({
7883
+ type: "result",
7884
+ id: message.id,
7885
+ result
7886
+ });
7887
+ if (message.tool === "set_background_agent") {
7888
+ await publishPresence();
7889
+ void sendHeartbeat();
7890
+ }
6052
7891
  if (exitAfterResponse) {
6053
- setTimeout(() => process3.exit(0), 500);
7892
+ setTimeout(() => void shutdown(), 500);
6054
7893
  }
6055
7894
  } catch (error) {
6056
7895
  logLine("error", `tool.fail ${message.tool} \xB7 ${error instanceof Error ? error.message : String(error)}`);
6057
- ws.send(
6058
- JSON.stringify({
6059
- type: "result",
6060
- id: message.id,
6061
- error: error instanceof Error ? error.message : String(error)
6062
- })
6063
- );
7896
+ send({
7897
+ type: "result",
7898
+ id: message.id,
7899
+ error: error instanceof Error ? error.message : String(error)
7900
+ });
6064
7901
  }
6065
7902
  });
6066
7903
  ws.once("close", resolve);
6067
7904
  ws.once("error", resolve);
6068
7905
  });
7906
+ clearInterval(presenceTimer);
6069
7907
  clearInterval(heartbeatTimer);
6070
7908
  clearInterval(approvalTimer);
6071
7909
  clearInterval(livenessTimer);
@@ -6074,8 +7912,8 @@ async function connectAgent(config) {
6074
7912
  logLine("warn", error.message);
6075
7913
  await resetConfig();
6076
7914
  logLine("event", "Starting a fresh device pairing\u2026");
6077
- await core.close().catch(() => void 0);
6078
- return "rePair";
7915
+ rePair = true;
7916
+ await shutdown();
6079
7917
  }
6080
7918
  logLine(
6081
7919
  "error",
@@ -6105,8 +7943,10 @@ async function connectAgent(config) {
6105
7943
  backoff = Math.min(backoff * 2, 3e4);
6106
7944
  }
6107
7945
  }
7946
+ process3.off("SIGINT", shutdown);
7947
+ process3.off("SIGTERM", shutdown);
6108
7948
  await core.close().catch(() => void 0);
6109
- return "stopped";
7949
+ return rePair ? "rePair" : "stopped";
6110
7950
  }
6111
7951
  async function main() {
6112
7952
  if (argFlag("--help") || argFlag("-h")) {
@@ -6123,9 +7963,9 @@ async function main() {
6123
7963
  "Options:",
6124
7964
  " --safe Hard local read-only cap; dashboard cannot enable write tools",
6125
7965
  " --developer Legacy alias for dashboard-managed capabilities",
6126
- " --foreground Keep this session attached to the terminal instead of background mode",
6127
- " --background Enable the login background agent",
6128
- " --no-background Disable login background mode and run this session in the foreground",
7966
+ " --foreground Stay attached without installing or repairing recovery",
7967
+ " --background Enable process recovery and keep this terminal attached",
7968
+ " --no-background Disable recovery; preserve the currently executing Agent",
6129
7969
  " --reset Remove local pairing and background-agent registration",
6130
7970
  " --version Print CLI version",
6131
7971
  " --help Show this help",
@@ -6146,6 +7986,7 @@ async function main() {
6146
7986
  const origin = process3.env.REMOTEARC_ORIGIN || process3.env.REMOTE_LINK_ORIGIN || process3.argv.find((value) => value.startsWith("--origin="))?.slice(9) || DEFAULT_ORIGIN;
6147
7987
  const agentMode = argFlag("--agent");
6148
7988
  const foregroundMode = argFlag("--foreground");
7989
+ const supervisorMode = argFlag("--supervise");
6149
7990
  const disableBackground = argFlag("--no-background");
6150
7991
  const enableBackground = argFlag("--background");
6151
7992
  let config = await readConfig();
@@ -6156,45 +7997,60 @@ async function main() {
6156
7997
  logLine("info", "Migrated relay origin to " + DEFAULT_ORIGIN);
6157
7998
  }
6158
7999
  if (!config) {
6159
- if (agentMode) return;
8000
+ if (agentMode || supervisorMode) return;
6160
8001
  config = await pair(origin, selectedMode());
6161
- } else if (argFlag("--safe") || argFlag("--developer")) {
6162
- config.mode = argFlag("--safe") ? "safe" : "managed";
6163
- await writeConfig(config);
6164
8002
  }
6165
8003
  if (disableBackground) {
6166
8004
  config.backgroundEnabled = false;
6167
8005
  await writeConfig(config);
6168
- await disableBackgroundAgent({ stopCurrent: true }).catch(() => void 0);
8006
+ await disableBackgroundAgent({ stopCurrent: false }).catch(() => void 0);
6169
8007
  } else if (enableBackground) {
6170
8008
  config.backgroundEnabled = true;
6171
8009
  await writeConfig(config);
6172
8010
  }
6173
8011
  logLine("info", `Using paired device identity ${dim(config.deviceId.slice(0, 8))}\u2026`);
6174
- if (agentMode) {
6175
- if (config.backgroundEnabled !== true) return;
6176
- const result2 = await connectAgent(config);
6177
- if (result2 !== "rePair") return;
8012
+ if (supervisorMode) {
8013
+ const release2 = await tryAgentLease(path7.join(CONFIG_DIR, "agent"), "supervisor");
8014
+ if (!release2) return;
8015
+ const abort2 = new AbortController();
8016
+ const stop = () => abort2.abort();
8017
+ process3.on("SIGINT", stop);
8018
+ process3.on("SIGTERM", stop);
8019
+ try {
8020
+ await superviseAgent({
8021
+ enabled: async () => (await readConfig())?.backgroundEnabled === true,
8022
+ signal: abort2.signal,
8023
+ start: () => startSupervisedAgent(process3.execPath, SELF_PATH, path7.join(CONFIG_DIR, "logs"))
8024
+ });
8025
+ } finally {
8026
+ process3.off("SIGINT", stop);
8027
+ process3.off("SIGTERM", stop);
8028
+ await release2();
8029
+ }
6178
8030
  return;
6179
8031
  }
6180
- if (!foregroundMode && config.backgroundEnabled === true) {
8032
+ if (agentMode && config.backgroundEnabled !== true) return;
8033
+ if (!agentMode && !foregroundMode && config.backgroundEnabled === true) {
6181
8034
  try {
6182
8035
  const status = await enableBackgroundAgent(SELF_PATH, { version: VERSION });
6183
- if (status.supported && status.enabled) {
8036
+ if (status.supported && status.enabled && status.active) {
6184
8037
  banner();
6185
- logLine("success", "Background connection enabled.");
8038
+ logLine("success", "Process recovery enabled; this terminal stays attached.");
6186
8039
  logLine(
6187
8040
  "info",
6188
- `Service: ${status.service} \xB7 starts automatically at login`
8041
+ `Supervisor: ${status.service} \xB7 PID ${status.pid} \xB7 takes over if the executing Agent ends`
6189
8042
  );
6190
8043
  logLine(
6191
8044
  "info",
6192
8045
  "Use the Remote Arc dashboard or --no-background to disable it."
6193
8046
  );
8047
+ } else logLine("warn", status.detail || "Background services are unavailable on this system.");
8048
+ } catch (error) {
8049
+ if (error instanceof LegacyBackgroundAgentError) {
8050
+ logLine("error", error.message);
8051
+ process3.exitCode = 1;
6194
8052
  return;
6195
8053
  }
6196
- logLine("warn", status.detail || "Background services are unavailable on this system.");
6197
- } catch (error) {
6198
8054
  logLine(
6199
8055
  "warn",
6200
8056
  "Could not enable background mode; keeping this terminal session connected: " + (error instanceof Error ? error.message : String(error))
@@ -6207,8 +8063,56 @@ async function main() {
6207
8063
  "Background connection is not configured yet \xB7 finish setup in the browser or Devices."
6208
8064
  );
6209
8065
  }
6210
- const result = await connectAgent(config);
6211
- if (result !== "rePair") return;
8066
+ const abort = new AbortController();
8067
+ const stopWaiting = () => abort.abort();
8068
+ process3.on("SIGINT", stopWaiting);
8069
+ process3.on("SIGTERM", stopWaiting);
8070
+ let release = null;
8071
+ let cursor = null;
8072
+ let announced = false;
8073
+ let lastApprovalPoll = 0;
8074
+ try {
8075
+ while (!abort.signal.aborted && !release) {
8076
+ if (agentMode && (await readConfig())?.backgroundEnabled !== true) return;
8077
+ release = await tryAgentLease(path7.join(CONFIG_DIR, "agent"));
8078
+ if (release) break;
8079
+ if (!agentMode && (argFlag("--safe") || argFlag("--developer"))) {
8080
+ logLine("error", "Another Agent owns execution. Stop it locally before changing the local permission profile.");
8081
+ process3.exitCode = 1;
8082
+ return;
8083
+ }
8084
+ if (!agentMode) {
8085
+ if (!announced) {
8086
+ logLine("info", "An Agent already owns execution. Following its operation log; Ctrl+C closes this viewer.");
8087
+ announced = true;
8088
+ }
8089
+ const events = await readAgentEvents(path7.join(CONFIG_DIR, "logs"), cursor);
8090
+ cursor = events.cursor;
8091
+ if (events.text) process3.stdout.write(events.text);
8092
+ if (Date.now() - lastApprovalPoll >= 4e3) {
8093
+ lastApprovalPoll = Date.now();
8094
+ void promptPendingApproval(config);
8095
+ }
8096
+ }
8097
+ await sleep(500);
8098
+ }
8099
+ if (!release || abort.signal.aborted) return;
8100
+ if (argFlag("--safe") || argFlag("--developer")) {
8101
+ config.mode = argFlag("--safe") ? "safe" : "managed";
8102
+ await writeConfig(config);
8103
+ }
8104
+ process3.off("SIGINT", stopWaiting);
8105
+ process3.off("SIGTERM", stopWaiting);
8106
+ const result = await connectAgent(config);
8107
+ if (result !== "rePair") {
8108
+ if (agentMode && (await readConfig())?.backgroundEnabled === true) process3.exitCode = 1;
8109
+ return;
8110
+ }
8111
+ } finally {
8112
+ process3.off("SIGINT", stopWaiting);
8113
+ process3.off("SIGTERM", stopWaiting);
8114
+ await release?.();
8115
+ }
6212
8116
  config = null;
6213
8117
  }
6214
8118
  }