@cortexkit/opencode-openai-auth 1.9.4 → 1.9.5
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/bin.js +2 -2
- package/dist/{codex-request-By530v1D.d.ts → codex-request-CZ16goZ6.d.ts} +20 -4
- package/dist/index-1nrre3d8.js +91 -0
- package/dist/index-41yn5c02.js +8 -0
- package/dist/{index-jej9cfk7.js → index-4gnwkw3a.js} +1 -1
- package/dist/{index-7bjgmbx4.js → index-7qxa018z.js} +1 -1
- package/dist/{index-4p45bmbz.js → index-8rj2xddq.js} +12 -12
- package/dist/{index-v1ztg103.js → index-cf0mrvqj.js} +1 -1
- package/dist/{index-rrtsaz80.js → index-f1cqptqa.js} +1 -1
- package/dist/index-h3vtzckv.js +76 -0
- package/dist/index-mcy24ej8.js +2 -0
- package/dist/index-npj40h62.js +12 -0
- package/dist/{index-cmybrjbd.js → index-s2vrkdj4.js} +1 -1
- package/dist/{index-2bf808fw.js → index-s52j2h64.js} +1 -1
- package/dist/{index-g3gbp1m4.js → index-y4j7y8b8.js} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{port-file-DXCxv8tU.d.ts → port-file-CEjki7bM.d.ts} +6 -1
- package/dist/rpc/port-file.d.ts +1 -1
- package/dist/rpc/port-file.js +1 -1
- package/dist/rpc/protocol.js +1 -1
- package/dist/rpc/rpc-client.d.ts +1 -1
- package/dist/rpc/rpc-client.js +1 -1
- package/dist/rpc/rpc-dir.js +1 -1
- package/dist/{rpc-client-CmaZW1by.d.ts → rpc-client-rWuV04C-.d.ts} +1 -1
- package/dist/sidebar-state.js +1 -1
- package/dist/{token-dcr3tgsk.js → token-djwdynff.js} +1 -1
- package/dist/token-util-0xt1ws82.js +1 -0
- package/dist/tui-preferences.js +1 -1
- package/dist/tui.d.ts +1 -1
- package/dist/v2/server.d.ts +1 -1
- package/dist/v2/server.js +3 -3
- package/package.json +3 -3
- package/src/tui-compiled/raw/shared/0f77ebc3c914c9c6-custody.js +74 -9
- package/src/tui-compiled/raw/shared/169b2b4bf30909bc-consumer.js +36 -0
- package/src/tui-compiled/raw/shared/30f5bc480f31f51c-roster.js +2 -0
- package/src/tui-compiled/raw/shared/352d228be2832bd4-hub.js +8 -0
- package/src/tui-compiled/raw/shared/375d9cca2def037d-private-files.js +7 -0
- package/src/tui-compiled/raw/shared/45bf5343bbbf9add-admission.js +17 -7
- package/src/tui-compiled/raw/shared/4825e03209d6ae3e-with-lock.js +2 -5
- package/src/tui-compiled/raw/shared/4ede5af6a878a14e-usage.js +145 -0
- package/src/tui-compiled/raw/shared/5f8cc863449a6c85-limits-namespace.js +34 -0
- package/src/tui-compiled/raw/shared/754d6017f8e419cf-builtins.js +38 -0
- package/src/tui-compiled/raw/shared/836fc615de6afbd9-settings.js +11 -4
- package/src/tui-compiled/raw/shared/97484d9bb98c705b-provider-state.js +5 -5
- package/src/tui-compiled/raw/shared/9d3fc9e37456504e-refresh.js +1 -1
- package/src/tui-compiled/raw/shared/9deae3c4da4fd63b-hooks.js +20 -5
- package/src/tui-compiled/raw/shared/a62a27a204266acd-attribution.js +3 -2
- package/src/tui-compiled/raw/shared/c14988621dc60a75-port-file.js +46 -4
- package/src/tui-compiled/raw/shared/c852a15cfe489d12-pool.js +2 -0
- package/src/tui-compiled/raw/shared/cd94316a888b0a8e-schema.js +9 -4
- package/src/tui-compiled/raw/shared/e8cd4929507508d8-refresh-file-lock.js +142 -47
- package/src/tui-compiled/runtime/shared/0f77ebc3c914c9c6-custody.js +74 -9
- package/src/tui-compiled/runtime/shared/169b2b4bf30909bc-consumer.js +36 -0
- package/src/tui-compiled/runtime/shared/30f5bc480f31f51c-roster.js +2 -0
- package/src/tui-compiled/runtime/shared/352d228be2832bd4-hub.js +8 -0
- package/src/tui-compiled/runtime/shared/375d9cca2def037d-private-files.js +7 -0
- package/src/tui-compiled/runtime/shared/45bf5343bbbf9add-admission.js +17 -7
- package/src/tui-compiled/runtime/shared/4825e03209d6ae3e-with-lock.js +2 -5
- package/src/tui-compiled/runtime/shared/4ede5af6a878a14e-usage.js +145 -0
- package/src/tui-compiled/runtime/shared/5f8cc863449a6c85-limits-namespace.js +34 -0
- package/src/tui-compiled/runtime/shared/754d6017f8e419cf-builtins.js +38 -0
- package/src/tui-compiled/runtime/shared/836fc615de6afbd9-settings.js +11 -4
- package/src/tui-compiled/runtime/shared/97484d9bb98c705b-provider-state.js +5 -5
- package/src/tui-compiled/runtime/shared/9d3fc9e37456504e-refresh.js +1 -1
- package/src/tui-compiled/runtime/shared/9deae3c4da4fd63b-hooks.js +20 -5
- package/src/tui-compiled/runtime/shared/a62a27a204266acd-attribution.js +3 -2
- package/src/tui-compiled/runtime/shared/c14988621dc60a75-port-file.js +46 -4
- package/src/tui-compiled/runtime/shared/c852a15cfe489d12-pool.js +2 -0
- package/src/tui-compiled/runtime/shared/cd94316a888b0a8e-schema.js +9 -4
- package/src/tui-compiled/runtime/shared/e8cd4929507508d8-refresh-file-lock.js +142 -47
- package/dist/index-2esezxst.js +0 -12
- package/dist/index-6xz3n4qh.js +0 -91
- package/dist/index-81yvwra9.js +0 -8
- package/dist/index-hz6f9gpq.js +0 -2
- package/dist/index-xx3k2qbv.js +0 -76
- package/dist/token-util-gbd7jw6d.js +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { lstat, mkdir, open, readFile, rename, rm, stat, } from "node:fs/promises";
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
|
-
import { createPrivateBytes, createPrivateDirectoryExclusive, ensurePrivateDirectory, PrivateFileError, privateOperation, readPrivateText, verifyPrivate, windowsPrivateFiles, writePrivateAtomic, } from "./375d9cca2def037d-private-files.js";
|
|
4
|
+
import { createPrivateBytes, createPrivateDirectoryExclusive, ensurePrivateDirectory, isWindowsSharingViolation, PrivateFileError, privateOperation, readPrivateText, verifyPrivate, windowsPrivateFiles, writePrivateAtomic, } from "./375d9cca2def037d-private-files.js";
|
|
5
5
|
import { LockOwnershipError, lockPathFor } from "./4825e03209d6ae3e-with-lock.js";
|
|
6
6
|
/**
|
|
7
7
|
* Create a new owner-only file and write `text` into it, failing with EEXIST if
|
|
@@ -367,54 +367,109 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
367
367
|
else
|
|
368
368
|
await rm(path, { recursive: true, force: true }).catch(() => { });
|
|
369
369
|
}
|
|
370
|
-
|
|
371
|
-
if (
|
|
372
|
-
|
|
370
|
+
function isRetryableRemoval(error, path) {
|
|
371
|
+
if (!windowsPrivateFiles ||
|
|
372
|
+
!(error instanceof PrivateFileError) ||
|
|
373
|
+
error.stage !== 'remove' ||
|
|
374
|
+
error.path !== path)
|
|
375
|
+
return false;
|
|
376
|
+
// Native sharing/delete-pending errors use isWindowsSharingViolation.
|
|
377
|
+
// node:fs rm supplies only errno: EBUSY/EPERM can also mean a sharing refusal.
|
|
378
|
+
return (isWindowsSharingViolation(error) ||
|
|
379
|
+
(error.win32Code === undefined &&
|
|
380
|
+
error.reason === undefined &&
|
|
381
|
+
(error.code === 'EBUSY' || error.code === 'EPERM')));
|
|
382
|
+
}
|
|
383
|
+
async function removeOwnedLeaseObject(path, stillOwned, deadline = performance.now() + EVICT_TTL + 1_000) {
|
|
384
|
+
let original;
|
|
385
|
+
let delayMs = 5;
|
|
386
|
+
while (await stillOwned()) {
|
|
387
|
+
try {
|
|
388
|
+
await removeLeaseObject(path);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
catch (error) {
|
|
392
|
+
if (!isRetryableRemoval(error, path))
|
|
393
|
+
throw error;
|
|
394
|
+
original ??= error;
|
|
395
|
+
}
|
|
396
|
+
const remainingMs = deadline - performance.now();
|
|
397
|
+
if (remainingMs <= 0)
|
|
398
|
+
throw original;
|
|
399
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(delayMs, remainingMs)));
|
|
400
|
+
if (performance.now() >= deadline)
|
|
401
|
+
throw original;
|
|
402
|
+
delayMs = Math.min(delayMs * 2, 50);
|
|
373
403
|
}
|
|
374
404
|
}
|
|
405
|
+
async function releaseEvictionMarker(deadline) {
|
|
406
|
+
let ownedDirectory;
|
|
407
|
+
await removeOwnedLeaseObject(evictPath, async () => {
|
|
408
|
+
if (!windowsPrivateFiles)
|
|
409
|
+
return ownsEvictionMarker();
|
|
410
|
+
const current = await lstat(evictPath).catch((error) => {
|
|
411
|
+
if (error.code !== 'ENOENT')
|
|
412
|
+
throw error;
|
|
413
|
+
return undefined;
|
|
414
|
+
});
|
|
415
|
+
if (!current ||
|
|
416
|
+
(ownedDirectory && !sameMarkerDirectory(ownedDirectory, current)))
|
|
417
|
+
return false;
|
|
418
|
+
if (await ownsEvictionMarker()) {
|
|
419
|
+
ownedDirectory = current;
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
if (!ownedDirectory)
|
|
423
|
+
return false;
|
|
424
|
+
// Recursive rm can delete owner.json before the directory delete fails.
|
|
425
|
+
// Only that same, now-ownerless directory is still ours to clean up.
|
|
426
|
+
try {
|
|
427
|
+
await stat(evictOwnerPath);
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
catch (error) {
|
|
431
|
+
if (error.code !== 'ENOENT')
|
|
432
|
+
throw error;
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
}, deadline);
|
|
436
|
+
}
|
|
375
437
|
// Returns whether the directory at the marker path was still the one this
|
|
376
438
|
// process created when its owner write failed. Only then is the failure ours
|
|
377
439
|
// to report: a gone or replaced directory is a lost race.
|
|
378
|
-
async function removeOwnMarkerAfterFailedOwnerWrite(created) {
|
|
440
|
+
async function removeOwnMarkerAfterFailedOwnerWrite(created, deadline) {
|
|
379
441
|
// Best effort only: never throws, so the original owner-write error below
|
|
380
442
|
// keeps explaining the failure. Removes the directory this process just
|
|
381
443
|
// created when its owner write failed, and leaves a contender's
|
|
382
444
|
// replacement alone when the identities differ.
|
|
383
445
|
if (!created)
|
|
384
446
|
return false;
|
|
385
|
-
let
|
|
386
|
-
try {
|
|
387
|
-
const st = await lstat(evictPath);
|
|
388
|
-
current = { dev: st.dev, ino: st.ino, birthtimeMs: st.birthtimeMs };
|
|
389
|
-
}
|
|
390
|
-
catch {
|
|
391
|
-
// Renamed away by a contender's steal: nothing of ours left to clean.
|
|
392
|
-
return false;
|
|
393
|
-
}
|
|
394
|
-
if (!sameMarkerDirectory(created, current))
|
|
395
|
-
return false;
|
|
396
|
-
// Remove only an ownerless directory. An owner file that appeared after
|
|
397
|
-
// our failed write belongs to someone: ours goes through
|
|
398
|
-
// releaseEvictionMarker in the caller, and a contender's must stay, since
|
|
399
|
-
// it may be holding the marker right now.
|
|
447
|
+
let stillOurs = false;
|
|
400
448
|
try {
|
|
401
|
-
await
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
449
|
+
await removeOwnedLeaseObject(evictPath, async () => {
|
|
450
|
+
stillOurs = false;
|
|
451
|
+
const st = await lstat(evictPath).catch(() => undefined);
|
|
452
|
+
if (!st || !sameMarkerDirectory(created, st))
|
|
453
|
+
return false;
|
|
454
|
+
stillOurs = true;
|
|
455
|
+
// Recheck both identity and absence of an owner before every attempt;
|
|
456
|
+
// a contender's replacement or newly published owner must stay.
|
|
457
|
+
try {
|
|
458
|
+
await stat(evictOwnerPath);
|
|
459
|
+
return false;
|
|
460
|
+
}
|
|
461
|
+
catch (error) {
|
|
462
|
+
return error.code === 'ENOENT';
|
|
463
|
+
}
|
|
464
|
+
}, deadline);
|
|
410
465
|
}
|
|
411
466
|
catch {
|
|
412
467
|
// A removal failure keeps the stale marker recoverable later; it must
|
|
413
468
|
// never mask the original owner-write error.
|
|
414
469
|
}
|
|
415
|
-
return
|
|
470
|
+
return stillOurs;
|
|
416
471
|
}
|
|
417
|
-
async function tryAcquireEvictionMarker() {
|
|
472
|
+
async function tryAcquireEvictionMarker(deadline) {
|
|
418
473
|
if (windowsPrivateFiles)
|
|
419
474
|
await createPrivateDirectoryExclusive(evictPath);
|
|
420
475
|
else
|
|
@@ -455,11 +510,11 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
455
510
|
// A race code while our own directory is still in place is not a lost
|
|
456
511
|
// race but a write that fails here every time: report it, rather than
|
|
457
512
|
// retrying the same failure until a deadline hides the cause.
|
|
458
|
-
const stillOurs = await removeOwnMarkerAfterFailedOwnerWrite(created);
|
|
513
|
+
const stillOurs = await removeOwnMarkerAfterFailedOwnerWrite(created, deadline);
|
|
459
514
|
if (isLostMarkerRaceError(error) && !stillOurs)
|
|
460
515
|
return false;
|
|
461
516
|
try {
|
|
462
|
-
await releaseEvictionMarker();
|
|
517
|
+
await releaseEvictionMarker(deadline);
|
|
463
518
|
}
|
|
464
519
|
catch {
|
|
465
520
|
// Removal must never mask the original owner-write error.
|
|
@@ -470,7 +525,7 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
470
525
|
await options.onStep('eviction-marker-acquired');
|
|
471
526
|
return true;
|
|
472
527
|
}
|
|
473
|
-
async function recoverStaleEvictionMarker() {
|
|
528
|
+
async function recoverStaleEvictionMarker(deadline) {
|
|
474
529
|
let evictStat;
|
|
475
530
|
try {
|
|
476
531
|
evictStat = await stat(evictPath);
|
|
@@ -529,12 +584,26 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
529
584
|
}
|
|
530
585
|
if (options.onStep)
|
|
531
586
|
await options.onStep('stale-marker-claimed');
|
|
532
|
-
|
|
587
|
+
const claimedIdentity = windowsPrivateFiles
|
|
588
|
+
? await lstat(claimedPath)
|
|
589
|
+
: undefined;
|
|
590
|
+
// The unique renamed claim is ours, not the current marker path where a
|
|
591
|
+
// successor may already be working.
|
|
592
|
+
await removeOwnedLeaseObject(claimedPath, async () => {
|
|
593
|
+
if (!claimedIdentity)
|
|
594
|
+
return true;
|
|
595
|
+
const current = await lstat(claimedPath).catch((error) => {
|
|
596
|
+
if (error.code !== 'ENOENT')
|
|
597
|
+
throw error;
|
|
598
|
+
return undefined;
|
|
599
|
+
});
|
|
600
|
+
return !!current && sameMarkerDirectory(claimedIdentity, current);
|
|
601
|
+
}, deadline);
|
|
533
602
|
return 'recovered';
|
|
534
603
|
}
|
|
535
|
-
async function withEvictionMarker(action) {
|
|
604
|
+
async function withEvictionMarker(action, deadline) {
|
|
536
605
|
try {
|
|
537
|
-
if (!(await tryAcquireEvictionMarker()))
|
|
606
|
+
if (!(await tryAcquireEvictionMarker(deadline)))
|
|
538
607
|
return false;
|
|
539
608
|
}
|
|
540
609
|
catch (error) {
|
|
@@ -555,12 +624,28 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
555
624
|
}
|
|
556
625
|
throw error;
|
|
557
626
|
}
|
|
627
|
+
let actionFailed = false;
|
|
628
|
+
let actionError;
|
|
558
629
|
try {
|
|
559
630
|
await action();
|
|
560
631
|
}
|
|
561
|
-
|
|
562
|
-
|
|
632
|
+
catch (error) {
|
|
633
|
+
actionFailed = true;
|
|
634
|
+
actionError = error;
|
|
635
|
+
}
|
|
636
|
+
try {
|
|
637
|
+
await releaseEvictionMarker(deadline);
|
|
638
|
+
}
|
|
639
|
+
catch (error) {
|
|
640
|
+
// Cleanup sharing failures must not replace the lock's original
|
|
641
|
+
// sharing refusal when both removals exhaust the same deadline.
|
|
642
|
+
if (isRetryableRemoval(actionError, lockPath) &&
|
|
643
|
+
isRetryableRemoval(error, evictPath))
|
|
644
|
+
throw actionError;
|
|
645
|
+
throw error;
|
|
563
646
|
}
|
|
647
|
+
if (actionFailed)
|
|
648
|
+
throw actionError;
|
|
564
649
|
return true;
|
|
565
650
|
}
|
|
566
651
|
// Marker loss after a write may mean another contender has taken over.
|
|
@@ -847,6 +932,7 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
847
932
|
// wall-clock adjustments cannot extend the retry deadline.
|
|
848
933
|
const deadline = performance.now() + EVICT_TTL + 1_000;
|
|
849
934
|
let delayMs = 5;
|
|
935
|
+
let removalError;
|
|
850
936
|
for (;;) {
|
|
851
937
|
try {
|
|
852
938
|
let removalConfirmed = false;
|
|
@@ -873,7 +959,7 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
873
959
|
return;
|
|
874
960
|
await removeLeaseObject(lockPath);
|
|
875
961
|
removalConfirmed = true;
|
|
876
|
-
});
|
|
962
|
+
}, deadline);
|
|
877
963
|
if (markerAcquired) {
|
|
878
964
|
if (windowsPrivateFiles && !removalConfirmed)
|
|
879
965
|
throw new PrivateFileError(lockPath, 'release-not-confirmed', {
|
|
@@ -884,25 +970,34 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
884
970
|
}
|
|
885
971
|
if (signal?.aborted)
|
|
886
972
|
return;
|
|
887
|
-
await recoverStaleEvictionMarker();
|
|
973
|
+
await recoverStaleEvictionMarker(deadline);
|
|
888
974
|
}
|
|
889
975
|
catch (error) {
|
|
890
|
-
if (
|
|
891
|
-
|
|
892
|
-
|
|
976
|
+
if (!isRetryableRemoval(error, lockPath)) {
|
|
977
|
+
if (windowsPrivateFiles)
|
|
978
|
+
throw error;
|
|
979
|
+
return;
|
|
980
|
+
}
|
|
981
|
+
// Like rename, a fenced delete can be refused by a reader's Windows
|
|
982
|
+
// handle without invalidating ownership. Drop the marker, back off,
|
|
983
|
+
// and re-confirm the owner under a new marker before trying again.
|
|
984
|
+
removalError ??= error;
|
|
893
985
|
}
|
|
894
986
|
const remainingMs = deadline - performance.now();
|
|
895
987
|
if (remainingMs <= 0)
|
|
896
988
|
break;
|
|
897
989
|
await new Promise((resolve) => setTimeout(resolve, Math.min(delayMs, remainingMs)));
|
|
990
|
+
if (windowsPrivateFiles && performance.now() >= deadline)
|
|
991
|
+
break;
|
|
898
992
|
delayMs = Math.min(delayMs * 2, 50);
|
|
899
993
|
}
|
|
900
994
|
// Do not delete by pathname without the marker: bounded retries leave the
|
|
901
995
|
// lease to expire rather than risking removal of a successor's lock.
|
|
902
996
|
if (windowsPrivateFiles)
|
|
903
|
-
throw
|
|
904
|
-
|
|
905
|
-
|
|
997
|
+
throw (removalError ??
|
|
998
|
+
new PrivateFileError(lockPath, 'release-not-confirmed', {
|
|
999
|
+
code: 'ETIMEDOUT',
|
|
1000
|
+
}));
|
|
906
1001
|
},
|
|
907
1002
|
};
|
|
908
1003
|
}
|
package/dist/index-2esezxst.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import{Bf as y,rf as O,sf as U,vf as b,wf as m,xf as L}from"./index-hz6f9gpq.js";import{createHash as Zz,randomUUID as v}from"node:crypto";import{chmod as t,mkdir as r,open as e,readdir as j,rename as zz,rmdir as Gz,stat as Jz,unlink as S}from"node:fs/promises";import{join as R,resolve as D}from"node:path";function l(z){return typeof z==="number"&&Number.isSafeInteger(z)&&z>0}function F(z){if(!l(z))return!1;try{return process.kill(z,0),!0}catch(J){return J.code==="EPERM"}}function Cz(z){return(J)=>Kz(J,z)}function Kz(z,J){if(/^[0-9a-f]{16}$/.test(z))return!0;return z.startsWith(J)&&/^[0-9a-f]{16}$/.test(z.slice(J.length))}function Nz(z){let J=/^port-(\d+)\.json$/.exec(z);return J?Number(J[1]):void 0}function p(z,J){if(z===null||typeof z!=="object"||Array.isArray(z))return!1;let{pid:V,port:Q,token:K}=z;return l(V)&&Nz(J)===V&&typeof Q==="number"&&Number.isInteger(Q)&&Q>=1&&Q<=65535&&typeof K==="string"&&K.length>0}async function h(z,J){J?.debug("rpc corrupt port file",{pid:process.pid,portFile:z}),await S(z).catch(()=>{})}async function Az(z,J,V={}){let Q=async()=>{let K=R(z,`port-${J.pid}.json`);if(O)return await m(z,V.secureDir??!1),await V.beforeWrite?.(),await y(K,JSON.stringify({...J,startedAt:Date.now()}),{stagePath:`${K}.${(V.stageName??v)()}.tmp`}),K;if(await r(z,{recursive:!0,mode:V.secureDir?448:void 0}),V.secureDir)await t(z,448);await V.beforeWrite?.();let W={...J,startedAt:Date.now()},Y=`${K}.${(V.stageName??v)()}.tmp`,N,G=!1;try{N=await e(Y,"wx",384),G=!0,await N.writeFile(JSON.stringify(W),"utf8"),await N.chmod(384),await N.close(),N=void 0,await zz(Y,K),G=!1}finally{if(await N?.close().catch(()=>{}),G)await S(Y).catch(()=>{})}return K};try{return await Q()}catch(K){if(K.code==="ENOENT")return await Q();throw K}}async function Oz(z,J,V,Q){let K;try{K=await j(z,{withFileTypes:!0})}catch(Y){if(Y.code==="ENOENT")return;throw Y}let W=D(J);for(let Y of K){if(!Y.isDirectory()||!V(Y.name))continue;let N=R(z,Y.name),G;try{G=await j(N)}catch(X){if(X.code==="ENOENT")continue;throw X}for(let X of G){if(!X.startsWith("port-")||!X.endsWith(".json"))continue;let $=R(N,X),Z;try{Z=await L($)}catch(H){if(H instanceof U&&H.code!=="ENOENT")throw H;continue}if(Z===void 0)continue;let q;try{q=JSON.parse(Z)}catch{await h($,Q);continue}if(!p(q,X)){await h($,Q);continue}if(!F(q.pid))await S($).catch(()=>{})}if(D(N)!==W)await Gz(N).catch(()=>{})}}async function c(z,J){if(!F(J.pid))return null;let V=R(D(z),`port-${J.pid}.json`);try{if(O)await b(V,"file");let Q=await Jz(V);return`${V}:${Q.dev}:${Q.ino}:${Q.mtimeMs}:${Q.size}`}catch(Q){if(Q instanceof U&&Q.code!=="ENOENT")throw Q;return null}}async function a(z,J,V={}){let Q;try{if(O)await b(z,"directory");Q=await j(z)}catch(G){if(G instanceof U&&G.code!=="ENOENT")throw G;return null}let K=[];for(let G of Q){if(!G.startsWith("port-")||!G.endsWith(".json"))continue;try{let X=JSON.parse(await L(R(z,G)));if(p(X,G))if(F(X.pid))K.push(X);else await S(R(z,G)).catch(()=>{})}catch(X){if(X instanceof U&&X.code!=="ENOENT")throw X}}if(K.length===0)return null;let W=J!==void 0&&J>=1?K.filter((G)=>G.pid===J):[];if(V.exactPid===!0&&W.length===0)return null;let Y=W.length>0?W:K,N=(G)=>typeof G.startedAt==="number"&&Number.isFinite(G.startedAt)?G.startedAt:-1/0;return Y.sort((G,X)=>N(X)-N(G))[0]??null}import{open as Qz}from"node:fs/promises";import{connect as Vz}from"node:net";import{join as Wz,resolve as o}from"node:path";var i=2000,A=new Map,T=new Map;async function Xz(z,J){let V=Wz(o(z),`port-${J.pid}.json`);if(O){let K=JSON.parse(await L(V));if(K.pid!==J.pid||K.port!==J.port||K.token!==J.token)return null;return null}let Q;try{Q=await Qz(V,"r");let K=await Q.stat(),W=JSON.parse(await Q.readFile("utf8"));if(W.pid!==J.pid||W.port!==J.port||W.token!==J.token)return null;return`${V}:${K.dev}:${K.ino}:${K.mtimeMs}:${K.size}`}catch{return null}finally{await Q?.close().catch(()=>{})}}async function s(z,J,V,Q){let K=A.get(z);if(!O&&K&&await c(J,K.entry)===K.identity)return K;A.delete(z);let W=T.get(z);if(W)return W;let Y=(async()=>{let N=await(Q.discover??a)(J,V,Q);if(!N)return null;let G=await Xz(J,N);if(!G)return{entry:N,identity:""};let X={entry:N,identity:G};return A.set(z,X),X})();T.set(z,Y);try{return await Y}finally{if(T.get(z)===Y)T.delete(z)}}async function d(z,J,V,Q=i){return new Promise((K)=>{let W,Y=!1,N=!1,G=($,Z=!1)=>{if(Y)return;Y=!0,clearTimeout(X),W?.destroy(),K({value:$,stale:Z})},X=setTimeout(()=>G(null),Q);try{let $=JSON.stringify(V);W=Vz({host:"127.0.0.1",port:z.port}),W.on("connect",()=>{N=!0,W?.write(`POST /rpc/${J} HTTP/1.0\r
|
|
2
|
-
Host: 127.0.0.1:${z.port}\r
|
|
3
|
-
Connection: close\r
|
|
4
|
-
Content-Type: application/json\r
|
|
5
|
-
Content-Length: ${Buffer.byteLength($)}\r
|
|
6
|
-
Authorization: Bearer ${z.token}\r
|
|
7
|
-
\r
|
|
8
|
-
`+$)});let Z=[],q=0,C,H,_=Buffer.alloc(0),g=()=>{if(C===void 0)return G(null);try{let E=Buffer.concat(Z).subarray(C);G(JSON.parse(E.toString("utf8")))}catch{G(null)}};W.on("data",(E)=>{if(Y)return;if(q+=E.length,Z.push(E),C===void 0){_=Buffer.concat([_,E]);let B=_.indexOf(`\r
|
|
9
|
-
\r
|
|
10
|
-
`);if(B>=0)C=B+4;if((C??_.length)>16384)return G(null);if(C!==void 0){let x=_.subarray(0,B).toString("latin1").split(`\r
|
|
11
|
-
`),f=/^HTTP\/1\.[01] (\d{3})(?: |$)/.exec(x[0]??"");if(!f)return G(null);let w=Number(f[1]);if(w<200||w>=300)return G(null,[401,403,404,410].includes(w));for(let M of x.slice(1)){let k=M.indexOf(":"),u=M.slice(0,k).toLowerCase(),P=M.slice(k+1).trim();if(u==="transfer-encoding")return G(null);if(u==="content-length"){if(H!==void 0||!/^\d+$/.test(P))return G(null);if(H=Number(P),!Number.isSafeInteger(H)||H>8388608)return G(null)}}_=Buffer.alloc(0)}}let I=q-(C??q);if(I>8388608)return G(null);if(H!==void 0){if(I>H)return G(null);if(I===H)g()}}),W.on("end",()=>{if(H!==void 0&&q-(C??0)!==H)return G(null);g()}),W.on("error",()=>G(null,!N)),W.on("close",()=>G(null))}catch{G(null,!N)}})}async function n(z,J,V,Q,K,W,Y=i){let N=JSON.stringify([o(z),J??null,V.exactPid===!0]),G=await s(N,z,J,V);if(Q(G?.entry??null),!G)return null;let X=Date.now()+Y,$=await d(G.entry,K,W,Y);if(!$.stale)return $.value;if(A.get(N)===G)A.delete(N);let Z=await s(N,z,J,V);if(!Z||Date.now()>=X)return null;if(Z.entry.port===G.entry.port&&Z.entry.pid===G.entry.pid&&Z.entry.token===G.entry.token){if(A.get(N)===Z)A.delete(N);return null}let q=await d(Z.entry,K,W,X-Date.now());if(q.stale&&A.get(N)===Z)A.delete(N);return q.value}function Tz(z,J,V,Q={}){let K={exactPid:Q.exactPid,discover:Q.discover},W=!1,Y=(N)=>{if(W)return;V?.(N),W=!0};return{async pending(N,G,X){return(await n(z,J,K,Y,"pending-notifications",{lastReceivedId:N,sessionId:G},X))?.messages??[]},async apply(N,G){return await n(z,J,K,Y,"apply",{...N},G)??{text:"apply failed",knobs:{}}}}}
|
|
12
|
-
export{Cz as _e,Az as $e,Oz as af,a as bf,i as cf,Tz as df};
|