@jini-ai/platform 0.7.1 → 0.7.3-next.1
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/CHANGELOG.md +2 -2
- package/README.md +16 -4
- package/dist/asset-cache.d.ts +13 -3
- package/dist/asset-cache.js +163 -154
- package/dist/blob-storage.d.ts +3 -32
- package/dist/blob-storage.js +1 -9
- package/dist/child-environment.d.ts +0 -16
- package/dist/child-environment.js +0 -25
- package/dist/command.d.ts +6 -11
- package/dist/command.js +8 -54
- package/dist/download-lock.d.ts +4 -0
- package/dist/download-lock.js +82 -0
- package/dist/download-maintenance.d.ts +5 -0
- package/dist/download-maintenance.js +82 -0
- package/dist/download-state.d.ts +8 -0
- package/dist/download-state.js +126 -0
- package/dist/download-store.d.ts +22 -0
- package/dist/download-store.js +174 -0
- package/dist/download-target.d.ts +10 -0
- package/dist/download-target.js +102 -0
- package/dist/download-transfer.d.ts +12 -0
- package/dist/download-transfer.js +174 -0
- package/dist/download-types.d.ts +162 -0
- package/dist/download-types.js +31 -0
- package/dist/download.d.ts +1 -89
- package/dist/download.js +8 -738
- package/dist/fetch-with-timeout.d.ts +7 -32
- package/dist/fetch-with-timeout.js +1 -77
- package/dist/fs/app-data-root.d.ts +28 -0
- package/dist/fs/app-data-root.js +35 -0
- package/dist/fs/atomic-write.js +1 -1
- package/dist/fs/boot-identity.d.ts +1 -4
- package/dist/fs/directory-delete-guard.d.ts +93 -0
- package/dist/fs/directory-delete-guard.js +148 -0
- package/dist/fs/directory-registry.js +45 -35
- package/dist/fs/directory-relocation.d.ts +82 -0
- package/dist/fs/directory-relocation.js +99 -0
- package/dist/fs/directory-running-lock.d.ts +1 -1
- package/dist/fs/directory-running-lock.js +24 -21
- package/dist/fs/directory-sync.js +1 -1
- package/dist/fs/directory-user-config.js +30 -3
- package/dist/fs/directory-workspace.d.ts +34 -30
- package/dist/fs/directory-workspace.js +70 -29
- package/dist/fs/durable-json.d.ts +6 -15
- package/dist/fs/durable-json.js +38 -35
- package/dist/fs/errno.d.ts +12 -3
- package/dist/fs/errno.js +5 -3
- package/dist/fs/file-lock.d.ts +3 -87
- package/dist/fs/file-lock.js +16 -30
- package/dist/fs/guarded-reader.js +335 -315
- package/dist/fs/index.d.ts +5 -0
- package/dist/fs/index.js +5 -0
- package/dist/fs/recent-directories.d.ts +46 -0
- package/dist/fs/recent-directories.js +68 -0
- package/dist/fs/remove-directory-tree.d.ts +20 -0
- package/dist/fs/remove-directory-tree.js +28 -0
- package/dist/fs/user-data-directories.js +2 -2
- package/dist/fs/workspace-directory.js +2 -1
- package/dist/fs.d.ts +16 -4
- package/dist/fs.js +4 -4
- package/dist/home-expansion.d.ts +7 -2
- package/dist/home-expansion.js +3 -3
- package/dist/http/guarded/client.d.ts +1 -2
- package/dist/http/guarded/client.js +2 -1
- package/dist/http/guarded/index.d.ts +0 -3
- package/dist/http/guarded/node-ports.d.ts +1 -1
- package/dist/http/guarded/transport.fetch.d.ts +2 -3
- package/dist/http/guarded/transport.fetch.js +64 -58
- package/dist/http.d.ts +3 -1
- package/dist/http.js +2 -2
- package/dist/index.d.ts +10 -11
- package/dist/index.js +7 -6
- package/dist/mail/activity/activity.d.ts +136 -0
- package/dist/mail/activity/activity.js +172 -0
- package/dist/mail/activity/ports.d.ts +113 -0
- package/dist/mail/activity/suppressions.d.ts +82 -0
- package/dist/mail/activity/suppressions.js +119 -0
- package/dist/mail/activity/vocabulary.d.ts +10 -0
- package/dist/mail/activity/vocabulary.js +8 -0
- package/dist/mail/builtin-providers.d.ts +12 -6
- package/dist/mail/builtin-providers.js +14 -11
- package/dist/mail/delivery-wrapper.js +2 -1
- package/dist/mail/email-sender-registry.d.ts +1 -1
- package/dist/mail/email-sender-registry.js +1 -1
- package/dist/mail/email-template-operations.d.ts +186 -0
- package/dist/mail/email-template-operations.js +226 -0
- package/dist/mail/email-template.d.ts +120 -0
- package/dist/mail/email-template.js +243 -0
- package/dist/mail/index.d.ts +4 -0
- package/dist/mail/index.js +3 -0
- package/dist/mail/prepare-sender-email.d.ts +1 -10
- package/dist/mail/prepare-sender-email.js +1 -11
- package/dist/mail/resolve-mailer.d.ts +2 -1
- package/dist/mail/resolve-mailer.js +5 -2
- package/dist/mail/senders.d.ts +0 -1
- package/dist/mail/senders.js +0 -1
- package/dist/mail/smtp.d.ts +1 -31
- package/dist/net/address.d.ts +1 -8
- package/dist/net/address.js +1 -18
- package/dist/net/index.d.ts +1 -1
- package/dist/net/index.js +1 -1
- package/dist/net/local-http-probe.js +1 -1
- package/dist/net/local-http-request.js +1 -1
- package/dist/process-probes.d.ts +36 -0
- package/dist/process-probes.js +151 -0
- package/dist/process-top-footprint.d.ts +7 -0
- package/dist/process-top-footprint.js +30 -0
- package/dist/process.d.ts +29 -31
- package/dist/process.js +18 -196
- package/dist/proxy-env.d.ts +12 -3
- package/dist/proxy-env.js +9 -8
- package/dist/resource-paths.d.ts +15 -3
- package/dist/resource-paths.js +7 -5
- package/dist/sandbox-env.d.ts +26 -5
- package/dist/sandbox-env.js +7 -7
- package/dist/secrets/credential-sets/contracts.d.ts +0 -1
- package/dist/secrets/credential-sets/contracts.js +0 -1
- package/dist/secrets/credential-sets/index.d.ts +2 -3
- package/dist/secrets/credential-sets/index.js +1 -1
- package/dist/secrets/credential-sets/provider-matches.d.ts +1 -1
- package/dist/secrets/credential-sets/provider-matches.js +1 -1
- package/dist/secrets/credential-sets/vault-archive.d.ts +1 -1
- package/dist/secrets/credential-sets/vault-archive.js +14 -8
- package/dist/secrets/credential-sets/vault.aad.d.ts +1 -1
- package/dist/secrets/credential-sets/vault.meta.d.ts +1 -1
- package/dist/secrets/credential-sets/vault.meta.js +1 -1
- package/dist/secrets/credential-sets/vault.repo.memory.d.ts +1 -1
- package/dist/secrets/credential-sets/vault.repo.memory.js +1 -1
- package/dist/secrets/credential-sets/vault.reseal-refused.d.ts +23 -0
- package/dist/secrets/credential-sets/vault.reseal-refused.js +36 -0
- package/dist/secrets/credential-sets/vault.service.d.ts +25 -5
- package/dist/secrets/credential-sets/vault.service.js +42 -8
- package/dist/secrets/index.d.ts +0 -3
- package/dist/secrets/index.js +5 -0
- package/dist/secrets/keyring.env.d.ts +1 -1
- package/dist/secrets/legacy-owner.d.ts +61 -0
- package/dist/secrets/legacy-owner.js +196 -0
- package/dist/secrets/root-key/decide.d.ts +89 -0
- package/dist/secrets/root-key/decide.js +60 -0
- package/dist/secrets/sealer-contract.d.ts +19 -0
- package/dist/secrets/sealer-contract.js +37 -0
- package/dist/secrets/secret-sealer.aesgcm.d.ts +1 -1
- package/dist/secrets/testing.d.ts +1 -0
- package/dist/secrets/testing.js +1 -0
- package/dist/shell.d.ts +4 -1
- package/dist/shell.js +1 -1
- package/dist/system.d.ts +6 -0
- package/dist/system.js +9 -0
- package/dist/terminal.d.ts +11 -96
- package/dist/terminal.js +80 -56
- package/dist/web/agent-tools.d.ts +107 -0
- package/dist/web/agent-tools.js +49 -0
- package/dist/web/index.d.ts +1 -0
- package/dist/web/index.js +1 -0
- package/package.json +38 -12
- package/dist/http/guarded/ports.d.ts +0 -56
- package/dist/http/guarded/types.d.ts +0 -8
- package/dist/http/guarded/types.js +0 -2
- package/dist/mail/email-sender.d.ts +0 -109
- package/dist/mail/email-sender.js +0 -10
- package/dist/net/endpoint-policy.d.ts +0 -39
- package/dist/net/endpoint-policy.js +0 -218
- package/dist/secrets/credential-sets/vault.types.d.ts +0 -27
- package/dist/secrets/credential-sets/vault.types.js +0 -49
- /package/dist/{http/guarded → mail/activity}/ports.js +0 -0
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
* Shared stores once wrote JSON directly, treated torn reads as empty, then persisted the empty
|
|
3
|
-
* state on the next write, erasing recoverable records. One mechanism keeps stores from drifting:
|
|
4
|
-
* atomic replacement, explicit unreadable state, quarantine, and locks for shared read-modify-write.
|
|
5
|
-
* Recovery policy stays with each store: salvage, restart, or leave untouched is not decided here.
|
|
6
|
-
* Native effects are Electron-free so desktop, CLI, and bridge processes can share the mechanism.
|
|
7
|
-
*/
|
|
8
|
-
import { type BootIdentity } from "./boot-identity.js";
|
|
1
|
+
import { type BootIdentity } from "@jini-ai/core/contracts/runtime";
|
|
9
2
|
import nodeFs from "node:fs";
|
|
10
|
-
import type
|
|
3
|
+
import { type Clock, type Logger } from "@jini-ai/core/primitives";
|
|
11
4
|
export type JsonFileRead = {
|
|
12
5
|
state: "ok";
|
|
13
6
|
value: unknown;
|
|
@@ -42,11 +35,7 @@ export interface DurableJsonPorts {
|
|
|
42
35
|
random: {
|
|
43
36
|
token(_required: Record<string, never>): string;
|
|
44
37
|
};
|
|
45
|
-
report:
|
|
46
|
-
error(required: {
|
|
47
|
-
message: string;
|
|
48
|
-
}): void;
|
|
49
|
-
};
|
|
38
|
+
report: Pick<Logger, 'error'>;
|
|
50
39
|
}
|
|
51
40
|
export interface DurableJsonNotices {
|
|
52
41
|
prefix: string;
|
|
@@ -56,7 +45,9 @@ export interface DurableJsonNotices {
|
|
|
56
45
|
}): string;
|
|
57
46
|
}
|
|
58
47
|
/** Native synchronous adapters. Liveness probes treat permission errors as alive. */
|
|
59
|
-
export declare function createNodeDurableJsonPorts(_required: Record<string, never
|
|
48
|
+
export declare function createNodeDurableJsonPorts(_required: Record<string, never>, { report }?: {
|
|
49
|
+
report?: DurableJsonPorts['report'] | undefined;
|
|
50
|
+
}): DurableJsonPorts;
|
|
60
51
|
export interface DurableJsonFile {
|
|
61
52
|
read(required: Record<string, never>): JsonFileRead;
|
|
62
53
|
tempPath(required: Record<string, never>, optional?: {
|
package/dist/fs/durable-json.js
CHANGED
|
@@ -6,13 +6,15 @@
|
|
|
6
6
|
* Native effects are Electron-free so desktop, CLI, and bridge processes can share the mechanism.
|
|
7
7
|
*/
|
|
8
8
|
import { readBootIdentity } from "./boot-identity.js";
|
|
9
|
+
import {} from "@jini-ai/core/contracts/runtime";
|
|
9
10
|
import nodeFs from "node:fs";
|
|
10
11
|
import path from "node:path";
|
|
11
12
|
import { randomBytes } from "node:crypto";
|
|
13
|
+
import { createConsoleLogger } from "@jini-ai/core/primitives";
|
|
12
14
|
import { createNodeAtomicFilesystem, writeJsonFileAtomic } from "./atomic-write.js";
|
|
13
15
|
import { withFileLockSync } from "./file-lock.js";
|
|
14
16
|
/** Native synchronous adapters. Liveness probes treat permission errors as alive. */
|
|
15
|
-
export function createNodeDurableJsonPorts(_required) {
|
|
17
|
+
export function createNodeDurableJsonPorts(_required, { report = createConsoleLogger({ prefix: '' }) } = {}) {
|
|
16
18
|
const cell = new Int32Array(new SharedArrayBuffer(4));
|
|
17
19
|
return {
|
|
18
20
|
filesystem: nodeFs,
|
|
@@ -31,9 +33,41 @@ export function createNodeDurableJsonPorts(_required) {
|
|
|
31
33
|
}
|
|
32
34
|
} },
|
|
33
35
|
random: { token: (_required) => randomBytes(4).toString("hex") },
|
|
34
|
-
report
|
|
36
|
+
report,
|
|
35
37
|
};
|
|
36
38
|
}
|
|
39
|
+
function readJsonFile({ filePath, fs }) {
|
|
40
|
+
let text;
|
|
41
|
+
try {
|
|
42
|
+
text = fs.readFileSync(filePath, "utf8");
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
return error.code === "ENOENT" ? { state: "missing" } : { state: "unreadable" };
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
return { state: "ok", value: JSON.parse(text) };
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return { state: "unreadable", text };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Preserve damaged bytes for inspection instead of deleting/overwriting them. False means callers
|
|
55
|
+
// must leave the original alone; an already-moved file is safe because another process cleared it.
|
|
56
|
+
function quarantineUnreadableFile({ filePath, notice, fs, clock, report, notices }) {
|
|
57
|
+
const asidePath = `${filePath}.corrupt-${clock.nowMs()}`;
|
|
58
|
+
try {
|
|
59
|
+
fs.renameSync(filePath, asidePath);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error.code === "ENOENT")
|
|
63
|
+
return true;
|
|
64
|
+
const logger = report;
|
|
65
|
+
logger.error({ message: `${notices.prefix}: ${notice.label} ${filePath} is unreadable and could not be moved aside. Left untouched; nothing was written over it.` }, { error });
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
report.error({ message: `${notices.prefix}: ${notice.label} ${filePath} was unreadable (torn or corrupt). Moved it aside to ${asidePath}; ${notice.consequence}` });
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
37
71
|
/** Atomic replacement, distinct corrupt reads, quarantine, and token-owned synchronous locks.
|
|
38
72
|
* Lock stealing rechecks inode, bytes and mtime; conditional unlink is not available on all hosts.
|
|
39
73
|
* File fsync precedes rename; directory flush afterwards is best-effort: skipped on Windows,
|
|
@@ -52,21 +86,6 @@ export function createDurableJsonFile({ filePath, filesystem: fs, clock, sleep,
|
|
|
52
86
|
// Thirty-second staleness also handles pid reuse after a crash:
|
|
53
87
|
// an unrelated live process must not hold the abandoned file forever merely by inheriting its pid.
|
|
54
88
|
const LOCK_WAIT_MS = 5000, LOCK_POLL_MS = pollMs, LOCK_STALE_MS = staleMs;
|
|
55
|
-
function readJsonFile(filePath) {
|
|
56
|
-
let text;
|
|
57
|
-
try {
|
|
58
|
-
text = fs.readFileSync(filePath, "utf8");
|
|
59
|
-
}
|
|
60
|
-
catch (error) {
|
|
61
|
-
return error.code === "ENOENT" ? { state: "missing" } : { state: "unreadable" };
|
|
62
|
-
}
|
|
63
|
-
try {
|
|
64
|
-
return { state: "ok", value: JSON.parse(text) };
|
|
65
|
-
}
|
|
66
|
-
catch {
|
|
67
|
-
return { state: "unreadable", text };
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
89
|
// Per-process sibling temps prevent one writer truncating another's pending replacement. Writes
|
|
71
90
|
// within one process are synchronous, so it cannot collide with itself; its next write reuses the temp.
|
|
72
91
|
function tempPathFor(filePath, pid = process.pid) {
|
|
@@ -83,30 +102,14 @@ export function createDurableJsonFile({ filePath, filesystem: fs, clock, sleep,
|
|
|
83
102
|
defaultMode: 0o666 & ~globalThis.process.umask(), tempPath: () => tempPathFor(filePath),
|
|
84
103
|
});
|
|
85
104
|
}
|
|
86
|
-
// Preserve damaged bytes for inspection instead of deleting/overwriting them. False means callers
|
|
87
|
-
// must leave the original alone; an already-moved file is safe because another process cleared it.
|
|
88
|
-
function quarantineUnreadableFile(filePath, notice) {
|
|
89
|
-
const asidePath = `${filePath}.corrupt-${clock.nowMs()}`;
|
|
90
|
-
try {
|
|
91
|
-
fs.renameSync(filePath, asidePath);
|
|
92
|
-
}
|
|
93
|
-
catch (error) {
|
|
94
|
-
if (error.code === "ENOENT")
|
|
95
|
-
return true;
|
|
96
|
-
report.error({ message: `${notices.prefix}: ${notice.label} ${filePath} is unreadable and could not be moved aside (${String(error)}). Left untouched; nothing was written over it.` });
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
report.error({ message: `${notices.prefix}: ${notice.label} ${filePath} was unreadable (torn or corrupt). Moved it aside to ${asidePath}; ${notice.consequence}` });
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
105
|
// Atomic replacement prevents tears; this lock serializes shared read-modify-write operations.
|
|
103
106
|
// Deliberately non-reentrant. Token-owned release must not evict a later owner after a stale break.
|
|
104
107
|
// The canonical lock rechecks the inode at steal time; its documented check-to-unlink race remains.
|
|
105
108
|
return {
|
|
106
|
-
read: (_required) => readJsonFile(filePath),
|
|
109
|
+
read: (_required) => readJsonFile({ filePath, fs }),
|
|
107
110
|
tempPath: (_required, { pid = process.pid } = {}) => tempPathFor(filePath, pid),
|
|
108
111
|
write: ({ value }) => writeJson(filePath, value),
|
|
109
|
-
quarantine: ({ notice }) => quarantineUnreadableFile(filePath, notice),
|
|
112
|
+
quarantine: ({ notice }) => quarantineUnreadableFile({ filePath, notice, fs, clock, report, notices }),
|
|
110
113
|
withLock: ({ critical }, optional = {}) => {
|
|
111
114
|
if (optional.waitMs !== undefined && (!Number.isSafeInteger(optional.waitMs) || optional.waitMs < 0))
|
|
112
115
|
throw new TypeError("waitMs must be a nonnegative safe integer");
|
package/dist/fs/errno.d.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
/** The string `code` of a Node system error (`ENOENT`, `EEXIST`, ...), or undefined for anything else.
|
|
2
2
|
* @complexity O(1). */
|
|
3
|
-
export declare function errorCodeOf(error
|
|
3
|
+
export declare function errorCodeOf({ error }: {
|
|
4
|
+
error: unknown;
|
|
5
|
+
}, _optional?: Record<string, never>): string | undefined;
|
|
4
6
|
/** Returns `fallback` when `matches(error)` and rethrows anything else, so an expected failure reads
|
|
5
7
|
* `catch (error) { return fallbackIf(error, isExpected, undefined); }`. @complexity O(matches). */
|
|
6
|
-
export declare function fallbackIf<T>(error
|
|
8
|
+
export declare function fallbackIf<T>({ error, matches, fallback }: {
|
|
9
|
+
error: unknown;
|
|
10
|
+
matches: (error: unknown) => boolean;
|
|
11
|
+
fallback: T;
|
|
12
|
+
}, _optional?: Record<string, never>): T;
|
|
7
13
|
/** {@link fallbackIf} for a missing entry (ENOENT). @complexity O(1). */
|
|
8
|
-
export declare function fallbackIfMissing<T>(error
|
|
14
|
+
export declare function fallbackIfMissing<T>({ error, fallback }: {
|
|
15
|
+
error: unknown;
|
|
16
|
+
fallback: T;
|
|
17
|
+
}, _optional?: Record<string, never>): T;
|
|
9
18
|
//# sourceMappingURL=errno.d.ts.map
|
package/dist/fs/errno.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
/** The string `code` of a Node system error (`ENOENT`, `EEXIST`, ...), or undefined for anything else.
|
|
2
2
|
* @complexity O(1). */
|
|
3
|
-
export function errorCodeOf(error) {
|
|
3
|
+
export function errorCodeOf({ error }, _optional = {}) {
|
|
4
4
|
if (typeof error !== "object" || error === null || !("code" in error))
|
|
5
5
|
return undefined;
|
|
6
6
|
return typeof error.code === "string" ? error.code : undefined;
|
|
7
7
|
}
|
|
8
8
|
/** Returns `fallback` when `matches(error)` and rethrows anything else, so an expected failure reads
|
|
9
9
|
* `catch (error) { return fallbackIf(error, isExpected, undefined); }`. @complexity O(matches). */
|
|
10
|
-
|
|
10
|
+
// With the (required, optional = {}) args shape that call is spelled
|
|
11
|
+
// `fallbackIf({ error, matches: isExpected, fallback: undefined })`.
|
|
12
|
+
export function fallbackIf({ error, matches, fallback }, _optional = {}) {
|
|
11
13
|
if (matches(error))
|
|
12
14
|
return fallback;
|
|
13
15
|
throw error;
|
|
14
16
|
}
|
|
15
17
|
/** {@link fallbackIf} for a missing entry (ENOENT). @complexity O(1). */
|
|
16
|
-
export function fallbackIfMissing(error, fallback) {
|
|
18
|
+
export function fallbackIfMissing({ error, fallback }, _optional = {}) {
|
|
17
19
|
if (error.code === "ENOENT")
|
|
18
20
|
return fallback;
|
|
19
21
|
throw error;
|
package/dist/fs/file-lock.d.ts
CHANGED
|
@@ -1,99 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
* Atomic writes prevent tears but not lost updates between shared read-modify-writes. Plain readers
|
|
3
|
-
* need no lock. Deliberately non-reentrant: nesting times out rather than silently sharing an
|
|
4
|
-
* exclusive section; compose store operations from unlocked internals instead.
|
|
5
|
-
* Kernel exclusive create chooses the winner. A unique token AND inode identify that owner;
|
|
6
|
-
* pid alone cannot distinguish a later owner after a stale break. Signal-zero liveness probes
|
|
7
|
-
* treat only ESRCH as death: permission failure must never authorize evicting a live process.
|
|
8
|
-
* Stale removal rechecks inode, bytes and mtime immediately before unlink. There is still a residual
|
|
9
|
-
* check-to-unlink race on filesystems without conditional unlink; rename/restore chains create wider
|
|
10
|
-
* multi-process races and are deliberately avoided. Atomic file replacement independently prevents
|
|
11
|
-
* torn content. Proven live owners are never age-evicted; staleMs: Infinity also disables age
|
|
12
|
-
* recovery for owners whose liveness is unknown.
|
|
13
|
-
*/
|
|
14
|
-
import { type BootIdentity } from "./boot-identity.js";
|
|
1
|
+
import { type FileLockPortOptions, type HeldFileLock, type HeldFileLockSync } from "@jini-ai/core/contracts/runtime";
|
|
15
2
|
import fs from "node:fs";
|
|
16
3
|
import * as asyncFs from "node:fs/promises";
|
|
17
|
-
import { type Clock } from "@jini-ai/core/primitives";
|
|
18
4
|
import { type PlatformMessages } from "../messages.js";
|
|
19
5
|
/** Re-exported so a host's own process port can record the boot its locks were taken in. */
|
|
20
|
-
export { readBootIdentity
|
|
6
|
+
export { readBootIdentity } from "./boot-identity.js";
|
|
21
7
|
export declare const DEFAULT_LOCK_TIMEOUT_MS = 15000;
|
|
22
8
|
export declare const DEFAULT_LOCK_STALE_AFTER_MS = 10000;
|
|
23
|
-
export interface FileLockHolder {
|
|
24
|
-
readonly pid: number;
|
|
25
|
-
readonly hostname: string;
|
|
26
|
-
readonly token: string;
|
|
27
|
-
readonly acquiredAt: string;
|
|
28
|
-
readonly bootId?: string;
|
|
29
|
-
}
|
|
30
|
-
export interface HeldFileLock {
|
|
31
|
-
readonly lockPath: string;
|
|
32
|
-
assertHeld(required: Record<string, never>): Promise<void>;
|
|
33
|
-
}
|
|
34
|
-
export interface HeldFileLockSync {
|
|
35
|
-
readonly lockPath: string;
|
|
36
|
-
assertHeld(required: Record<string, never>): void;
|
|
37
|
-
}
|
|
38
9
|
export type FileLockSyncFilesystem = Pick<typeof fs, "mkdirSync" | "openSync" | "writeSync" | "fstatSync" | "readFileSync" | "statSync" | "closeSync" | "rmSync">;
|
|
39
10
|
export type FileLockAsyncFilesystem = Pick<typeof asyncFs, "mkdir" | "open" | "stat" | "rm">;
|
|
40
|
-
export interface FileLockOptions {
|
|
41
|
-
/** Infinity follows an operation with its own deadline; dead local owners remain reclaimable. */
|
|
42
|
-
timeoutMs?: number;
|
|
43
|
-
/** Age threshold for unknown owners. Proven live owners are never evicted; Infinity disables age recovery. */
|
|
44
|
-
staleMs?: number;
|
|
45
|
-
pollMs?: number;
|
|
46
|
-
clock?: Clock;
|
|
47
|
-
/** Times the wait budget (start and timeout check). Defaults to `clock.nowMs`; hosts with a
|
|
48
|
-
* monotonic source pass it so a wall-clock jump can neither end nor stretch the wait. Stale age
|
|
49
|
-
* stays on `clock.nowMs`, because lock-file mtimes are wall-clock times. */
|
|
50
|
-
monotonicMs?: () => number;
|
|
51
|
-
sleep?: (required: {
|
|
52
|
-
durationMs: number;
|
|
53
|
-
}) => void | Promise<void>;
|
|
54
|
-
process?: {
|
|
55
|
-
pid: number;
|
|
56
|
-
isAlive(required: {
|
|
57
|
-
pid: number;
|
|
58
|
-
}): boolean;
|
|
59
|
-
bootIdentity?: () => BootIdentity | undefined;
|
|
60
|
-
};
|
|
61
|
-
token?: () => string;
|
|
62
|
-
hostname?: () => string;
|
|
63
|
-
onStaleLockRemoved?: (required: {
|
|
64
|
-
holder: FileLockHolder | undefined;
|
|
65
|
-
}) => void;
|
|
66
|
-
createParent?: boolean;
|
|
67
|
-
format?: "json" | "token";
|
|
68
|
-
/** Host stores keep their existing user-facing timeout copy. */
|
|
69
|
-
timeoutMessage?: (required: {
|
|
70
|
-
lockPath: string;
|
|
71
|
-
waitMs: number;
|
|
72
|
-
}) => string;
|
|
11
|
+
export interface FileLockOptions extends Omit<FileLockPortOptions, "filesystem"> {
|
|
73
12
|
messages?: PlatformMessages;
|
|
74
|
-
/** Durable stores historically ignored release failures to avoid masking their critical section. */
|
|
75
|
-
suppressReleaseErrors?: boolean;
|
|
76
|
-
}
|
|
77
|
-
export declare class FileLockTimeoutError extends Error {
|
|
78
|
-
readonly lockPath: string;
|
|
79
|
-
readonly holder: FileLockHolder | undefined;
|
|
80
|
-
readonly waitedMs: number;
|
|
81
|
-
constructor(required: {
|
|
82
|
-
lockPath: string;
|
|
83
|
-
holder: FileLockHolder | undefined;
|
|
84
|
-
waitedMs: number;
|
|
85
|
-
}, optional?: {
|
|
86
|
-
message?: string;
|
|
87
|
-
messages?: PlatformMessages;
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
export declare class FileLockLostError extends Error {
|
|
91
|
-
readonly lockPath: string;
|
|
92
|
-
constructor({ lockPath }: {
|
|
93
|
-
lockPath: string;
|
|
94
|
-
}, { messages }?: {
|
|
95
|
-
messages?: PlatformMessages;
|
|
96
|
-
});
|
|
97
13
|
}
|
|
98
14
|
/** Platform-specific contention classification retains the Windows exclusive-open behavior. */
|
|
99
15
|
export declare function isContendedLockError({ code, platform }: {
|
package/dist/fs/file-lock.js
CHANGED
|
@@ -12,50 +12,31 @@
|
|
|
12
12
|
* recovery for owners whose liveness is unknown.
|
|
13
13
|
*/
|
|
14
14
|
import { fromEarlierBoot, fromThisBoot, readBootIdentity } from "./boot-identity.js";
|
|
15
|
+
import { FileLockLostError, FileLockTimeoutError } from "@jini-ai/core/contracts/runtime";
|
|
15
16
|
import fs from "node:fs";
|
|
16
17
|
import * as asyncFs from "node:fs/promises";
|
|
17
18
|
import { randomUUID } from "node:crypto";
|
|
18
19
|
import { hostname as nodeHostname } from "node:os";
|
|
19
20
|
import path from "node:path";
|
|
20
|
-
import { createSystemClock } from "@jini-ai/core/primitives";
|
|
21
21
|
import { defaultPlatformMessages } from "../messages.js";
|
|
22
|
+
import { createSystemClock } from "../system.js";
|
|
22
23
|
import { errorCodeOf, fallbackIf } from "./errno.js";
|
|
23
24
|
/** Re-exported so a host's own process port can record the boot its locks were taken in. */
|
|
24
25
|
export { readBootIdentity } from "./boot-identity.js";
|
|
25
26
|
export const DEFAULT_LOCK_TIMEOUT_MS = 15_000;
|
|
26
27
|
export const DEFAULT_LOCK_STALE_AFTER_MS = 10_000;
|
|
27
|
-
export class FileLockTimeoutError extends Error {
|
|
28
|
-
lockPath;
|
|
29
|
-
holder;
|
|
30
|
-
waitedMs;
|
|
31
|
-
constructor(required, optional = {}) {
|
|
32
|
-
super(optional.message ?? (optional.messages ?? defaultPlatformMessages).fileLockTimeout({ waitedMs: required.waitedMs }));
|
|
33
|
-
this.name = "FileLockTimeoutError";
|
|
34
|
-
this.lockPath = required.lockPath;
|
|
35
|
-
this.holder = required.holder;
|
|
36
|
-
this.waitedMs = required.waitedMs;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
export class FileLockLostError extends Error {
|
|
40
|
-
lockPath;
|
|
41
|
-
constructor({ lockPath }, { messages = defaultPlatformMessages } = {}) {
|
|
42
|
-
super(messages.fileLockLost());
|
|
43
|
-
this.name = "FileLockLostError";
|
|
44
|
-
this.lockPath = lockPath;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
28
|
/** Platform-specific contention classification retains the Windows exclusive-open behavior. */
|
|
48
29
|
export function isContendedLockError({ code, platform }) {
|
|
49
30
|
return code === "EEXIST" || (code === "EPERM" && platform === "win32");
|
|
50
31
|
}
|
|
51
32
|
function isContended(error) {
|
|
52
|
-
return isContendedLockError({ code: errorCodeOf(error), platform: process.platform });
|
|
33
|
+
return isContendedLockError({ code: errorCodeOf({ error }), platform: process.platform });
|
|
53
34
|
}
|
|
54
35
|
function isAbsentOrContended(error) {
|
|
55
|
-
const code = errorCodeOf(error);
|
|
36
|
+
const code = errorCodeOf({ error });
|
|
56
37
|
return code === "ENOENT" || isContendedLockError({ code, platform: process.platform });
|
|
57
38
|
}
|
|
58
|
-
function rethrowUnlessMissing(error) { if (errorCodeOf(error) !== "ENOENT")
|
|
39
|
+
function rethrowUnlessMissing(error) { if (errorCodeOf({ error }) !== "ENOENT")
|
|
59
40
|
throw error; }
|
|
60
41
|
function validPid(pid) { return typeof pid === "number" && Number.isSafeInteger(pid) && pid > 0; }
|
|
61
42
|
function isHolderRecord(value) {
|
|
@@ -104,7 +85,7 @@ function defaultProcessPort() {
|
|
|
104
85
|
return true;
|
|
105
86
|
}
|
|
106
87
|
catch (error) {
|
|
107
|
-
return errorCodeOf(error) !== "ESRCH";
|
|
88
|
+
return errorCodeOf({ error }) !== "ESRCH";
|
|
108
89
|
} } };
|
|
109
90
|
}
|
|
110
91
|
function settings(options) {
|
|
@@ -179,7 +160,12 @@ function sameFile(a, b) { return a.ino === b.ino && a.dev === b.dev; }
|
|
|
179
160
|
function sameStat(a, b) { return sameFile(a, b) && a.mtimeMs === b.mtimeMs; }
|
|
180
161
|
function unchanged(a, b) { return sameStat(a, b) && a.raw === b.raw; }
|
|
181
162
|
function stale(observed, current, s) {
|
|
182
|
-
|
|
163
|
+
if (!unchanged(observed, current))
|
|
164
|
+
return false;
|
|
165
|
+
const alive = ownerAlive(current, s);
|
|
166
|
+
if (current.holder !== undefined && alive !== false)
|
|
167
|
+
return false;
|
|
168
|
+
return isLockStale({ nowMs: s.clock.nowMs(), mtimeMs: observed.mtimeMs, staleMs: s.staleMs, ownerAlive: alive, observedInode: observed.ino, currentInode: current.ino, observedDevice: observed.dev, currentDevice: current.dev });
|
|
183
169
|
}
|
|
184
170
|
function messagesOption(options) { return options.messages ? { messages: options.messages } : {}; }
|
|
185
171
|
function checkTimeout(lockPath, holder, start, s, options) {
|
|
@@ -203,7 +189,7 @@ function inspectSync(lockPath, filesystem) {
|
|
|
203
189
|
descriptor = filesystem.openSync(lockPath, "r");
|
|
204
190
|
}
|
|
205
191
|
catch (error) {
|
|
206
|
-
return fallbackIf(error, isAbsentOrContended, undefined);
|
|
192
|
+
return fallbackIf({ error, matches: isAbsentOrContended, fallback: undefined });
|
|
207
193
|
}
|
|
208
194
|
try {
|
|
209
195
|
const info = filesystem.fstatSync(descriptor), raw = filesystem.readFileSync(descriptor, "utf8");
|
|
@@ -219,7 +205,7 @@ async function inspectAsync(lockPath, filesystem) {
|
|
|
219
205
|
handle = await filesystem.open(lockPath, "r");
|
|
220
206
|
}
|
|
221
207
|
catch (error) {
|
|
222
|
-
return fallbackIf(error, isAbsentOrContended, undefined);
|
|
208
|
+
return fallbackIf({ error, matches: isAbsentOrContended, fallback: undefined });
|
|
223
209
|
}
|
|
224
210
|
try {
|
|
225
211
|
const info = await handle.stat(), raw = await handle.readFile("utf8");
|
|
@@ -247,7 +233,7 @@ function tryCreateSync(ctx) {
|
|
|
247
233
|
descriptor = filesystem.openSync(lockPath, "wx", 0o600);
|
|
248
234
|
}
|
|
249
235
|
catch (error) {
|
|
250
|
-
return fallbackIf(error, isContended, undefined);
|
|
236
|
+
return fallbackIf({ error, matches: isContended, fallback: undefined });
|
|
251
237
|
}
|
|
252
238
|
let identity;
|
|
253
239
|
try {
|
|
@@ -342,7 +328,7 @@ async function tryCreateAsync(ctx) {
|
|
|
342
328
|
handle = await ctx.filesystem.open(ctx.lockPath, "wx", 0o600);
|
|
343
329
|
}
|
|
344
330
|
catch (error) {
|
|
345
|
-
return fallbackIf(error, isContended, undefined);
|
|
331
|
+
return fallbackIf({ error, matches: isContended, fallback: undefined });
|
|
346
332
|
}
|
|
347
333
|
let identity;
|
|
348
334
|
try {
|