@jini-ai/platform 0.7.3-next.2 → 0.7.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/CHANGELOG.md +7 -0
- package/README.md +13 -0
- package/dist/download-lock.d.ts +3 -1
- package/dist/download-lock.js +4 -6
- package/dist/download-maintenance.js +6 -6
- package/dist/download-state.js +17 -14
- package/dist/download-store.d.ts +34 -10
- package/dist/download-store.js +24 -20
- package/dist/download-target.d.ts +6 -2
- package/dist/download-target.js +7 -7
- package/dist/download-transfer.js +18 -12
- package/dist/download.js +4 -4
- package/dist/fetch-with-timeout.d.ts +0 -53
- package/dist/fetch-with-timeout.js +62 -13
- package/dist/fs/directory-running-lock.d.ts +4 -0
- package/dist/fs/directory-running-lock.js +8 -1
- package/dist/fs/directory-workspace.d.ts +2 -1
- package/dist/fs/directory-workspace.js +2 -2
- package/dist/fs/durable-json.d.ts +3 -16
- package/dist/fs/durable-json.js +4 -2
- package/dist/fs/guarded-reader.d.ts +10 -0
- package/dist/fs/guarded-reader.js +34 -1
- package/dist/fs/index.d.ts +4 -0
- package/dist/fs/index.js +4 -0
- package/dist/fs/leased-directory-move.d.ts +49 -0
- package/dist/fs/leased-directory-move.js +31 -0
- package/dist/fs/owned-temp-root.d.ts +74 -0
- package/dist/fs/owned-temp-root.js +203 -0
- package/dist/fs/recent-directories.d.ts +2 -1
- package/dist/fs/resolve-directory-target.d.ts +33 -0
- package/dist/fs/resolve-directory-target.js +58 -0
- package/dist/fs/staged-file.d.ts +40 -0
- package/dist/fs/staged-file.js +75 -0
- package/dist/fs/strict-containment.d.ts +14 -0
- package/dist/fs/strict-containment.js +14 -0
- package/dist/image/png-encode.d.ts +41 -0
- package/dist/image/png-encode.js +96 -0
- package/dist/index.d.ts +3 -1
- package/dist/mail/email-template-agent-tools.d.ts +150 -0
- package/dist/mail/email-template-agent-tools.js +86 -0
- package/dist/mail/email-template-tool-registrations.d.ts +73 -0
- package/dist/mail/email-template-tool-registrations.js +66 -0
- package/dist/mail/email-template.js +3 -7
- package/dist/mail/feedback-route.d.ts +50 -0
- package/dist/mail/feedback-route.js +46 -0
- package/dist/mail/feedback-setup.d.ts +44 -0
- package/dist/mail/feedback-setup.js +53 -0
- package/dist/mail/feedback-sources.d.ts +67 -0
- package/dist/mail/feedback-sources.js +42 -0
- package/dist/mail/index.d.ts +10 -1
- package/dist/mail/index.js +4 -0
- package/dist/mail/mail-status.d.ts +32 -0
- package/dist/mail/mail-status.js +2 -0
- package/dist/mail/positional-purpose-gate.d.ts +14 -0
- package/dist/mail/positional-purpose-gate.js +18 -0
- package/dist/mail/purpose-gate-probe.d.ts +18 -0
- package/dist/mail/purpose-gate-probe.js +22 -0
- package/dist/mail/purpose-scoped-mailer.d.ts +4 -0
- package/dist/mail/purpose-scoped-mailer.js +4 -3
- package/dist/mail/resolve-mailer.d.ts +13 -1
- package/dist/mail/resolve-mailer.js +11 -5
- package/dist/mail/sql/activity.repo.d.ts +20 -0
- package/dist/mail/sql/activity.repo.js +133 -0
- package/dist/mail/sql/attempts.repo.d.ts +17 -0
- package/dist/mail/sql/attempts.repo.js +32 -0
- package/dist/mail/sql/capture.repo.d.ts +69 -0
- package/dist/mail/sql/capture.repo.js +119 -0
- package/dist/mail/sql/feedback-inbox.repo.d.ts +12 -0
- package/dist/mail/sql/feedback-inbox.repo.js +32 -0
- package/dist/mail/sql/index.d.ts +12 -0
- package/dist/mail/sql/index.js +12 -0
- package/dist/mail/sql/ledger.repo.d.ts +29 -0
- package/dist/mail/sql/ledger.repo.js +57 -0
- package/dist/mail/sql/retention.d.ts +23 -0
- package/dist/mail/sql/retention.js +53 -0
- package/dist/mail/sql/site-feedback-route.d.ts +18 -0
- package/dist/mail/sql/site-feedback-route.js +22 -0
- package/dist/mail/sql/suppression-admin.repo.d.ts +11 -0
- package/dist/mail/sql/suppression-admin.repo.js +76 -0
- package/dist/mail/sql/suppression.repo.d.ts +10 -0
- package/dist/mail/sql/suppression.repo.js +33 -0
- package/dist/mail/test-send.d.ts +147 -0
- package/dist/mail/test-send.js +198 -0
- package/dist/mail/use-readiness.d.ts +104 -0
- package/dist/mail/use-readiness.js +140 -0
- package/dist/object-store/local-fs.js +34 -1
- package/dist/object-store/site/artifact-provider.d.ts +65 -0
- package/dist/object-store/site/artifact-provider.js +37 -0
- package/dist/object-store/site/registry.d.ts +110 -0
- package/dist/object-store/site/registry.js +93 -0
- package/dist/object-store/site/setting.d.ts +34 -0
- package/dist/object-store/site/setting.js +32 -0
- package/dist/process-probes.d.ts +11 -4
- package/dist/process-probes.js +3 -3
- package/dist/process-top-footprint.d.ts +1 -1
- package/dist/process-top-footprint.js +1 -3
- package/dist/process.d.ts +3 -17
- package/dist/process.js +3 -2
- package/dist/retention/index.d.ts +6 -0
- package/dist/retention/index.js +6 -0
- package/dist/retention/measure-tree.d.ts +68 -0
- package/dist/retention/measure-tree.js +205 -0
- package/dist/retention/preview-store.d.ts +12 -0
- package/dist/retention/preview-store.js +28 -0
- package/dist/retention/routes.d.ts +23 -0
- package/dist/retention/routes.js +24 -0
- package/dist/retention/runner.d.ts +103 -0
- package/dist/retention/runner.js +164 -0
- package/dist/secrets/credential-sets/index.d.ts +1 -0
- package/dist/secrets/credential-sets/index.js +1 -0
- package/dist/secrets/credential-sets/store-vault-guards.d.ts +26 -0
- package/dist/secrets/credential-sets/store-vault-guards.js +30 -0
- package/dist/secrets/credential-sets/vault.service.d.ts +1 -1
- package/dist/secrets/index.d.ts +1 -1
- package/dist/secrets/index.js +1 -1
- package/dist/secrets/keyring.env.d.ts +1 -1
- package/dist/secrets/keyring.env.js +3 -5
- package/dist/secrets/keyring.memory.d.ts +4 -1
- package/dist/secrets/keyring.memory.js +4 -1
- package/dist/secrets/legacy-codecs.d.ts +381 -0
- package/dist/secrets/legacy-codecs.js +468 -0
- package/dist/secrets/legacy-owner.d.ts +3 -11
- package/dist/secrets/root-key/ensure.d.ts +154 -0
- package/dist/secrets/root-key/ensure.js +328 -0
- package/dist/secrets/root-key/files.d.ts +104 -0
- package/dist/secrets/root-key/files.js +166 -0
- package/dist/secrets/root-key/material.d.ts +132 -0
- package/dist/secrets/root-key/material.js +116 -0
- package/dist/secrets/root-key/ordered-keyring.d.ts +157 -0
- package/dist/secrets/root-key/ordered-keyring.js +104 -0
- package/dist/secrets/root-key/reconcile.d.ts +50 -0
- package/dist/secrets/root-key/reconcile.js +147 -0
- package/dist/secrets/root-key/site-keyring-errors.d.ts +78 -0
- package/dist/secrets/root-key/site-keyring-errors.js +37 -0
- package/dist/secrets/root-key/sources.d.ts +79 -0
- package/dist/secrets/root-key/sources.js +119 -0
- package/dist/secrets/secret-sealer.aesgcm.d.ts +50 -2
- package/dist/secrets/secret-sealer.aesgcm.js +107 -19
- package/dist/web/tool-registrations.d.ts +67 -0
- package/dist/web/tool-registrations.js +44 -0
- package/package.json +138 -6
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writer-owned temporary roots: a short-lived folder whose owner is provable, so a sweeper can tidy
|
|
3
|
+
* what a crashed writer left behind without ever touching a root that is still in use.
|
|
4
|
+
*
|
|
5
|
+
* Protocol (every writer): take `<root>.lock` first, then write the provenance marker
|
|
6
|
+
* `<root>.owner`, then create `<root>` (mode 0700, name = host prefix + exact UUID). The writer
|
|
7
|
+
* stays inside the lock until its work (child processes, reads, publication) settles, removes the
|
|
8
|
+
* root in `finally`, and removes the marker only once the root is gone. A root therefore never
|
|
9
|
+
* exists without an owner, and a failed removal keeps the writer's provenance on disk.
|
|
10
|
+
*
|
|
11
|
+
* Sweep: only exact `<prefix><uuid>` names in the named parent; links and roots outside the parent
|
|
12
|
+
* are skipped; roots without a marker (written before this protocol, or unconfirmed) are only
|
|
13
|
+
* reported; a marked root is removed only after acquiring its lock (which succeeds only when the
|
|
14
|
+
* recorded owner is proven dead, or released) and re-checking its dev/ino under that exclusion.
|
|
15
|
+
*
|
|
16
|
+
* Accepted deviation from ALGO-P11 §9.7: the design placed this helper in the host
|
|
17
|
+
* (`platform/temp/owned-root.ts`). The protocol is generic, so it lives here; hosts supply the
|
|
18
|
+
* product-specific prefixes, parents, the lock adapter (and through it the liveness probe).
|
|
19
|
+
*/
|
|
20
|
+
import { basename, dirname, join } from "node:path";
|
|
21
|
+
import { FileLockTimeoutError } from "@jini-ai/core/contracts/runtime";
|
|
22
|
+
import { errorCodeOf } from "./errno.js";
|
|
23
|
+
export const OWNED_TEMP_ROOT_LOCK_SUFFIX = ".lock";
|
|
24
|
+
export const OWNED_TEMP_ROOT_MARKER_SUFFIX = ".owner";
|
|
25
|
+
const UUID = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
|
|
26
|
+
const EXACT_UUID = new RegExp(`^${UUID}$`);
|
|
27
|
+
const PLAIN_PREFIX = /^[A-Za-z0-9._-]+$/;
|
|
28
|
+
const DAY_MS = 86_400_000;
|
|
29
|
+
function ownedRootName(prefix, id) {
|
|
30
|
+
if (!PLAIN_PREFIX.test(prefix) || !EXACT_UUID.test(id))
|
|
31
|
+
throw new RangeError("A temporary folder needs a plain name prefix and an exact lowercase UUID.");
|
|
32
|
+
return prefix + id;
|
|
33
|
+
}
|
|
34
|
+
async function removeOwnedRoot(root, fs, onRemovalError) {
|
|
35
|
+
try {
|
|
36
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
// The marker is the writer's durable provenance: kept, the sweeper can finish this removal later.
|
|
40
|
+
onRemovalError?.({ root, error });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
await fs.rm(root + OWNED_TEMP_ROOT_MARKER_SUFFIX, { force: true });
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Runs `work` inside a fresh owned root, holding the root's lock for the whole call.
|
|
47
|
+
* @param required.parent existing folder that holds the roots; @param required.prefix host prefix.
|
|
48
|
+
* @param required.id UUID source; @param required.lock lock adapter; @param required.work the job.
|
|
49
|
+
* @param optional.onRemovalError observer for a failed cleanup (the root and marker then stay).
|
|
50
|
+
* @returns what `work` returns. @throws RangeError for a non-plain prefix or a non-UUID id.
|
|
51
|
+
* @complexity O(work) plus one lock, two creates and two removals.
|
|
52
|
+
*/
|
|
53
|
+
export async function withOwnedTempRoot(required, optional = {}) {
|
|
54
|
+
const { parent, prefix, fs, work } = required;
|
|
55
|
+
const root = join(parent, ownedRootName(prefix, required.id()));
|
|
56
|
+
const run = async () => {
|
|
57
|
+
await fs.writeFile(root + OWNED_TEMP_ROOT_MARKER_SUFFIX, JSON.stringify({ version: 1, prefix }), { flag: "wx", mode: 0o600 });
|
|
58
|
+
try {
|
|
59
|
+
await fs.mkdir(root, { mode: 0o700 });
|
|
60
|
+
return await work({ root });
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
await removeOwnedRoot(root, fs, optional.onRemovalError);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
// staleMs Infinity: a slow writer is never age-evicted; only a proven-dead owner loses its lock.
|
|
67
|
+
return required.lock({ lockPath: root + OWNED_TEMP_ROOT_LOCK_SUFFIX, run }, { staleMs: Infinity });
|
|
68
|
+
}
|
|
69
|
+
const entry = (path, outcome, reason) => ({ path, outcome, reason });
|
|
70
|
+
async function lstatOrUndefined(fs, path) {
|
|
71
|
+
try {
|
|
72
|
+
return await fs.lstat(path);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (errorCodeOf({ error }) === "ENOENT")
|
|
76
|
+
return undefined;
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function escapeRegExp(text) { return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); }
|
|
81
|
+
/** Distinct exact root names in `names` (from the root itself, its lock or its marker). O(n). */
|
|
82
|
+
function exactRootNames(names, prefix) {
|
|
83
|
+
const exact = new RegExp(`^(${escapeRegExp(prefix)}${UUID})(\\.lock|\\.owner)?$`);
|
|
84
|
+
const bases = new Set();
|
|
85
|
+
for (const name of names) {
|
|
86
|
+
const match = exact.exec(name);
|
|
87
|
+
if (match)
|
|
88
|
+
bases.add(match[1]);
|
|
89
|
+
}
|
|
90
|
+
return [...bases].sort();
|
|
91
|
+
}
|
|
92
|
+
/** Acquire without waiting; `undefined` means another owner is alive or its liveness is unknown. */
|
|
93
|
+
async function tryExclusive(ctx, lockPath, run) {
|
|
94
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
95
|
+
try {
|
|
96
|
+
return await ctx.lock({ lockPath, run }, { timeoutMs: 0, staleMs: Infinity });
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
// withFileLock checks its deadline right after breaking a dead owner's lock, before creating
|
|
100
|
+
// its own; with a zero wait that first attempt reports a timeout. One retry then acquires,
|
|
101
|
+
// while a live (or foreign, unprovable) owner times out again.
|
|
102
|
+
if (!(error instanceof FileLockTimeoutError) && error?.name !== "FileLockTimeoutError")
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
async function contained(ctx, parent, root) {
|
|
109
|
+
const [realParent, realRoot] = await Promise.all([ctx.fs.realpath(parent), ctx.fs.realpath(root)]);
|
|
110
|
+
return dirname(realRoot) === realParent && basename(realRoot) === basename(root);
|
|
111
|
+
}
|
|
112
|
+
async function removeUnderLock(ctx, root, seen) {
|
|
113
|
+
const result = await tryExclusive(ctx, root + OWNED_TEMP_ROOT_LOCK_SUFFIX, async () => {
|
|
114
|
+
const again = await lstatOrUndefined(ctx.fs, root);
|
|
115
|
+
if (!again?.isDirectory() || again.dev !== seen.dev || again.ino !== seen.ino)
|
|
116
|
+
return entry(root, "kept", "changed");
|
|
117
|
+
await ctx.fs.rm(root, { recursive: true, force: true });
|
|
118
|
+
await ctx.fs.rm(root + OWNED_TEMP_ROOT_MARKER_SUFFIX, { force: true });
|
|
119
|
+
return entry(root, "removed", "dead-owner");
|
|
120
|
+
});
|
|
121
|
+
return result ?? entry(root, "kept", "owner-alive");
|
|
122
|
+
}
|
|
123
|
+
/** A lock/marker whose root is absent: a writer died between taking its lock and creating the root. */
|
|
124
|
+
async function sweepOrphan(ctx, root) {
|
|
125
|
+
const files = await Promise.all([OWNED_TEMP_ROOT_LOCK_SUFFIX, OWNED_TEMP_ROOT_MARKER_SUFFIX].map((suffix) => lstatOrUndefined(ctx.fs, root + suffix)));
|
|
126
|
+
const present = files.filter((info) => info !== undefined);
|
|
127
|
+
if (present.length === 0)
|
|
128
|
+
return undefined;
|
|
129
|
+
if (present.some((info) => !info.isFile()))
|
|
130
|
+
return entry(root, "kept", "not-a-directory");
|
|
131
|
+
if (present.some((info) => ctx.nowMs - info.mtimeMs < ctx.olderThanMs))
|
|
132
|
+
return entry(root, "kept", "young");
|
|
133
|
+
// Same stale-break path as a root: acquiring breaks a dead owner's lock; ours is released on exit.
|
|
134
|
+
const result = await tryExclusive(ctx, root + OWNED_TEMP_ROOT_LOCK_SUFFIX, async () => {
|
|
135
|
+
if (await lstatOrUndefined(ctx.fs, root))
|
|
136
|
+
return entry(root, "kept", "changed");
|
|
137
|
+
await ctx.fs.rm(root + OWNED_TEMP_ROOT_MARKER_SUFFIX, { force: true });
|
|
138
|
+
return entry(root, "removed", "orphan-lock");
|
|
139
|
+
});
|
|
140
|
+
return result ?? entry(root, "kept", "owner-alive");
|
|
141
|
+
}
|
|
142
|
+
async function sweepOne(ctx, parent, root) {
|
|
143
|
+
const info = await lstatOrUndefined(ctx.fs, root);
|
|
144
|
+
if (!info)
|
|
145
|
+
return sweepOrphan(ctx, root);
|
|
146
|
+
// lstat never follows links: a symlink named like a root is not a directory here.
|
|
147
|
+
if (!info.isDirectory())
|
|
148
|
+
return entry(root, "kept", "not-a-directory");
|
|
149
|
+
if (!await contained(ctx, parent, root))
|
|
150
|
+
return entry(root, "kept", "outside-root");
|
|
151
|
+
const marker = await lstatOrUndefined(ctx.fs, root + OWNED_TEMP_ROOT_MARKER_SUFFIX);
|
|
152
|
+
if (!marker?.isFile())
|
|
153
|
+
return entry(root, "report-only", await lstatOrUndefined(ctx.fs, root + OWNED_TEMP_ROOT_LOCK_SUFFIX) ? "unconfirmed" : "legacy");
|
|
154
|
+
// Age is only a cheap prefilter. It cannot establish that the owner died: a live writer may hold
|
|
155
|
+
// a root for days (a long import, a paused laptop), so removal still requires the lock below,
|
|
156
|
+
// which only a proven-dead or released owner yields.
|
|
157
|
+
if (ctx.nowMs - info.mtimeMs < ctx.olderThanMs)
|
|
158
|
+
return entry(root, "kept", "young");
|
|
159
|
+
return removeUnderLock(ctx, root, info);
|
|
160
|
+
}
|
|
161
|
+
async function sweepOneSafely(ctx, parent, root) {
|
|
162
|
+
try {
|
|
163
|
+
return await sweepOne(ctx, parent, root);
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
// Windows reports an open handle as EBUSY/EPERM/EACCES; the next pass retries.
|
|
167
|
+
if (["EBUSY", "EPERM", "EACCES"].includes(errorCodeOf({ error }) ?? ""))
|
|
168
|
+
return entry(root, "kept", "busy");
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Why a pass must stop before the next root; abort wins over the limit. */
|
|
173
|
+
function sweepStop({ signal, count, limit }) {
|
|
174
|
+
if (signal.aborted)
|
|
175
|
+
return { truncated: false, aborted: true };
|
|
176
|
+
if (count >= limit)
|
|
177
|
+
return { truncated: true, aborted: false };
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Tidies owned roots left by dead writers in the named parents, reporting everything else.
|
|
182
|
+
* @param required.roots `{ parent, prefix }` pairs; nothing outside them is read or touched.
|
|
183
|
+
* @param required.lock lock adapter whose liveness probe proves owner death; @param required.signal stops between roots.
|
|
184
|
+
* @param optional.olderThanMs minimum root age (default one day); @param optional.limit roots per pass (default 20).
|
|
185
|
+
* @returns one entry per examined root; `truncated` when the limit stopped the pass.
|
|
186
|
+
* @complexity O(n) directory entries plus O(limit) lock attempts and removals.
|
|
187
|
+
*/
|
|
188
|
+
export async function sweepOwnedTempRoots(required, optional = {}) {
|
|
189
|
+
const ctx = { fs: required.fs, lock: required.lock, nowMs: required.clock.nowMs(), olderThanMs: optional.olderThanMs ?? DAY_MS };
|
|
190
|
+
const limit = optional.limit ?? 20, entries = [];
|
|
191
|
+
for (const { parent, prefix } of required.roots) {
|
|
192
|
+
for (const name of exactRootNames(await required.fs.readdir(parent), prefix)) {
|
|
193
|
+
const stop = sweepStop({ signal: required.signal, count: entries.length, limit });
|
|
194
|
+
if (stop)
|
|
195
|
+
return { entries, ...stop };
|
|
196
|
+
const result = await sweepOneSafely(ctx, parent, join(parent, name));
|
|
197
|
+
if (result)
|
|
198
|
+
entries.push(result);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return { entries, truncated: false, aborted: false };
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=owned-temp-root.js.map
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { DurableJsonFile
|
|
1
|
+
import type { DurableJsonFile } from "./durable-json.js";
|
|
2
|
+
import type { QuarantineNotice } from "@jini-ai/core/contracts/site/fs-durable-json";
|
|
2
3
|
/**
|
|
3
4
|
* Read the persisted state, treating any unreadable or malformed file as "no state yet".
|
|
4
5
|
*
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Path-containment resolution for a caller-supplied directory argument.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* Resolve a caller-supplied `dirArg` to the ONE real target path every subsequent fs operation must derive
|
|
6
|
+
* from — never re-derived from the raw, unresolved argument. Handles three cases:
|
|
7
|
+
* 1. An ordinary path (existing or not, and NOT itself a symlink) — `path.resolve` collapses
|
|
8
|
+
* any embedded `..` segments purely lexically, with no filesystem access needed. Returned
|
|
9
|
+
* as-is, deliberately WITHOUT canonicalizing every ancestor segment (`fs.realpathSync` on
|
|
10
|
+
* the whole path would also resolve unrelated, pre-existing OS-level symlinks the caller
|
|
11
|
+
* never named — e.g. macOS aliases `/var` to `/private/var` — which would silently change
|
|
12
|
+
* an already-unambiguous, non-symlinked target path for no containment benefit).
|
|
13
|
+
* 2. `dirArg` itself IS an existing symlink whose destination already exists — `fs.realpathSync`
|
|
14
|
+
* is authoritative here and handles nested symlinks / relative link targets in one call.
|
|
15
|
+
* 3. `dirArg` itself IS an existing symlink whose destination does NOT exist yet (e.g. a
|
|
16
|
+
* dangling symlink at an `init` target) — `fs.realpathSync` cannot resolve this (it requires
|
|
17
|
+
* the final path to exist), so this falls back to one `readlinkSync` level, which is enough
|
|
18
|
+
* for the "symlink at the target" shape this containment guarantee covers.
|
|
19
|
+
*
|
|
20
|
+
* Architectural role:
|
|
21
|
+
* Node-only filesystem utility with no host-specific input; hosts call it before deriving any path from a
|
|
22
|
+
* directory argument.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Resolve `dirArg` to the real target path every subsequent write must be derived from.
|
|
26
|
+
*
|
|
27
|
+
* @complexity O(1) — at most one `lstatSync` plus one `realpathSync`/`readlinkSync` call, plus a
|
|
28
|
+
* constant number of `path` operations; not a function of any caller-controlled collection.
|
|
29
|
+
*/
|
|
30
|
+
export declare function resolveDirectoryTarget({ dirArg }: {
|
|
31
|
+
dirArg: string;
|
|
32
|
+
}, _optional?: {}): string;
|
|
33
|
+
//# sourceMappingURL=resolve-directory-target.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* @file Path-containment resolution for a caller-supplied directory argument.
|
|
5
|
+
*
|
|
6
|
+
* Purpose:
|
|
7
|
+
* Resolve a caller-supplied `dirArg` to the ONE real target path every subsequent fs operation must derive
|
|
8
|
+
* from — never re-derived from the raw, unresolved argument. Handles three cases:
|
|
9
|
+
* 1. An ordinary path (existing or not, and NOT itself a symlink) — `path.resolve` collapses
|
|
10
|
+
* any embedded `..` segments purely lexically, with no filesystem access needed. Returned
|
|
11
|
+
* as-is, deliberately WITHOUT canonicalizing every ancestor segment (`fs.realpathSync` on
|
|
12
|
+
* the whole path would also resolve unrelated, pre-existing OS-level symlinks the caller
|
|
13
|
+
* never named — e.g. macOS aliases `/var` to `/private/var` — which would silently change
|
|
14
|
+
* an already-unambiguous, non-symlinked target path for no containment benefit).
|
|
15
|
+
* 2. `dirArg` itself IS an existing symlink whose destination already exists — `fs.realpathSync`
|
|
16
|
+
* is authoritative here and handles nested symlinks / relative link targets in one call.
|
|
17
|
+
* 3. `dirArg` itself IS an existing symlink whose destination does NOT exist yet (e.g. a
|
|
18
|
+
* dangling symlink at an `init` target) — `fs.realpathSync` cannot resolve this (it requires
|
|
19
|
+
* the final path to exist), so this falls back to one `readlinkSync` level, which is enough
|
|
20
|
+
* for the "symlink at the target" shape this containment guarantee covers.
|
|
21
|
+
*
|
|
22
|
+
* Architectural role:
|
|
23
|
+
* Node-only filesystem utility with no host-specific input; hosts call it before deriving any path from a
|
|
24
|
+
* directory argument.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Resolve `dirArg` to the real target path every subsequent write must be derived from.
|
|
28
|
+
*
|
|
29
|
+
* @complexity O(1) — at most one `lstatSync` plus one `realpathSync`/`readlinkSync` call, plus a
|
|
30
|
+
* constant number of `path` operations; not a function of any caller-controlled collection.
|
|
31
|
+
*/
|
|
32
|
+
export function resolveDirectoryTarget({ dirArg }, _optional = {}) {
|
|
33
|
+
const lexicallyResolved = path.resolve(dirArg);
|
|
34
|
+
let lst;
|
|
35
|
+
try {
|
|
36
|
+
lst = fs.lstatSync(lexicallyResolved);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// Doesn't exist at all — the common `init` case. Nothing to resolve beyond the lexical form.
|
|
40
|
+
return lexicallyResolved;
|
|
41
|
+
}
|
|
42
|
+
if (!lst.isSymbolicLink()) {
|
|
43
|
+
// An ordinary existing file/dir — return as-is, without canonicalizing unrelated ancestor
|
|
44
|
+
// symlinks (see file header).
|
|
45
|
+
return lexicallyResolved;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
// The target IS a symlink whose destination exists — resolve it fully and authoritatively.
|
|
49
|
+
return fs.realpathSync(lexicallyResolved);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// A dangling symlink — its destination doesn't exist yet. Follow the link one level instead
|
|
53
|
+
// of requiring the destination to already exist.
|
|
54
|
+
const linkTarget = fs.readlinkSync(lexicallyResolved);
|
|
55
|
+
return path.isAbsolute(linkTarget) ? linkTarget : path.resolve(path.dirname(lexicallyResolved), linkTarget);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=resolve-directory-target.js.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type * as nodeFs from "node:fs/promises";
|
|
2
|
+
import type { StagedMediaFile } from "@jini-ai/core/contracts/site/cms-media";
|
|
3
|
+
export interface StagedFileDependencies {
|
|
4
|
+
filesystem: Pick<typeof nodeFs, "open" | "stat" | "chmod">;
|
|
5
|
+
withTempRoot: <T>(required: {
|
|
6
|
+
work: (owned: {
|
|
7
|
+
root: string;
|
|
8
|
+
}) => Promise<T>;
|
|
9
|
+
}, optional?: Record<string, never>) => Promise<T>;
|
|
10
|
+
sniffContentType: (required: {
|
|
11
|
+
bytes: Uint8Array;
|
|
12
|
+
}) => string | undefined;
|
|
13
|
+
}
|
|
14
|
+
declare function withStagedFile<T>({ deps, chunks, byteLength, contentType, consume }: {
|
|
15
|
+
deps: StagedFileDependencies;
|
|
16
|
+
chunks: AsyncIterable<Uint8Array>;
|
|
17
|
+
byteLength: number;
|
|
18
|
+
contentType: string;
|
|
19
|
+
consume: (staged: StagedMediaFile) => Promise<T>;
|
|
20
|
+
}, { signal }?: {
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
}): Promise<T>;
|
|
23
|
+
declare function sealStagedFile({ deps, root, path, maxBytes }: {
|
|
24
|
+
deps: StagedFileDependencies;
|
|
25
|
+
root: string;
|
|
26
|
+
path: string;
|
|
27
|
+
maxBytes: number;
|
|
28
|
+
}, { signal }?: {
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
}): Promise<StagedMediaFile>;
|
|
31
|
+
export declare function createStagedFileAdapter(deps: StagedFileDependencies, _optional?: Record<string, never>): {
|
|
32
|
+
withStagedFile: <T>(required: Omit<Parameters<typeof withStagedFile<T>>[0], "deps">, optional?: {
|
|
33
|
+
signal?: AbortSignal;
|
|
34
|
+
}) => Promise<T>;
|
|
35
|
+
sealStagedFile: (required: Omit<Parameters<typeof sealStagedFile>[0], "deps">, optional?: {
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
}) => Promise<StagedMediaFile>;
|
|
38
|
+
};
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=staged-file.d.ts.map
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
async function* openChunks({ filesystem, path }, { signal } = {}) {
|
|
4
|
+
signal?.throwIfAborted();
|
|
5
|
+
const file = await filesystem.open(path, "r");
|
|
6
|
+
try {
|
|
7
|
+
while (true) {
|
|
8
|
+
signal?.throwIfAborted();
|
|
9
|
+
const buffer = Buffer.alloc(64 * 1024);
|
|
10
|
+
const { bytesRead } = await file.read(buffer, 0, buffer.length, null);
|
|
11
|
+
if (bytesRead === 0)
|
|
12
|
+
return;
|
|
13
|
+
yield buffer.subarray(0, bytesRead);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
finally {
|
|
17
|
+
await file.close();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async function withStagedFile({ deps, chunks, byteLength, contentType, consume }, { signal } = {}) {
|
|
21
|
+
return deps.withTempRoot({ work: async ({ root }) => {
|
|
22
|
+
const path = join(root, "input");
|
|
23
|
+
const file = await deps.filesystem.open(path, "wx", 0o600);
|
|
24
|
+
const hash = createHash("sha256");
|
|
25
|
+
let length = 0;
|
|
26
|
+
try {
|
|
27
|
+
for await (const chunk of chunks) {
|
|
28
|
+
signal?.throwIfAborted();
|
|
29
|
+
length += chunk.byteLength;
|
|
30
|
+
if (length > byteLength)
|
|
31
|
+
throw new Error("staged source length mismatch");
|
|
32
|
+
await file.writeFile(chunk);
|
|
33
|
+
hash.update(chunk);
|
|
34
|
+
}
|
|
35
|
+
if (length !== byteLength)
|
|
36
|
+
throw new Error("staged source length mismatch");
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
await file.close();
|
|
40
|
+
}
|
|
41
|
+
signal?.throwIfAborted();
|
|
42
|
+
return consume(Object.freeze({ path, root, byteLength, contentType, sha256: hash.digest("hex"),
|
|
43
|
+
openChunks: (_required, optional = {}) => openChunks({ filesystem: deps.filesystem, path }, optional),
|
|
44
|
+
}));
|
|
45
|
+
} }, {});
|
|
46
|
+
}
|
|
47
|
+
async function sealStagedFile({ deps, root, path, maxBytes }, { signal } = {}) {
|
|
48
|
+
await deps.filesystem.chmod(path, 0o600);
|
|
49
|
+
const { size: byteLength } = await deps.filesystem.stat(path);
|
|
50
|
+
if (byteLength > maxBytes)
|
|
51
|
+
throw Object.assign(new Error("staged output exceeds limit"), { reason: "too-large" });
|
|
52
|
+
const hash = createHash("sha256");
|
|
53
|
+
let length = 0;
|
|
54
|
+
let contentType;
|
|
55
|
+
for await (const chunk of openChunks({ filesystem: deps.filesystem, path }, signal ? { signal } : {})) {
|
|
56
|
+
if (length === 0)
|
|
57
|
+
contentType = deps.sniffContentType({ bytes: chunk });
|
|
58
|
+
length += chunk.byteLength;
|
|
59
|
+
if (length > byteLength)
|
|
60
|
+
throw Object.assign(new Error("staged output grew"), { reason: "too-large" });
|
|
61
|
+
hash.update(chunk);
|
|
62
|
+
}
|
|
63
|
+
if (length !== byteLength || contentType !== "video/mp4")
|
|
64
|
+
throw Object.assign(new Error("invalid staged output"), { reason: "invalid" });
|
|
65
|
+
return Object.freeze({ path, root, byteLength, contentType, sha256: hash.digest("hex"),
|
|
66
|
+
openChunks: (_required, optional = {}) => openChunks({ filesystem: deps.filesystem, path }, optional),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
export function createStagedFileAdapter(deps, _optional = {}) {
|
|
70
|
+
return {
|
|
71
|
+
withStagedFile: (required, optional = {}) => withStagedFile({ deps, ...required }, optional),
|
|
72
|
+
sealStagedFile: (required, optional = {}) => sealStagedFile({ deps, ...required }, optional),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=staged-file.js.map
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves `<root>/<segment>` and refuses (returns `null`) unless the result stays strictly
|
|
3
|
+
* within `root`. Two different escapes are checked, not one duplicated:
|
|
4
|
+
* `resolved !== path.join(root, segment)` catches `path.resolve` diverging from a naive join —
|
|
5
|
+
* the case where `root` itself was not already absolute, so `resolve` silently anchors against
|
|
6
|
+
* `process.cwd()` instead; `!resolved.startsWith(prefix)` catches `segment` carrying enough
|
|
7
|
+
* `../` to walk back out of an (already-absolute) `root` even though both agree on the
|
|
8
|
+
* normalized result. `prefix` is built from `path.resolve(root)` rather than raw `root` so a
|
|
9
|
+
* trailing separator on `root` (or `root` being exactly `/`) can't double up into a separator
|
|
10
|
+
* nothing would ever start with — that would silently refuse every valid child instead of only
|
|
11
|
+
* real traversal. Calling `path.resolve` on `root` here is safe specifically because the guard
|
|
12
|
+
* above already proved `root` is absolute (a relative `root` would have diverged from
|
|
13
|
+
* `path.join` and returned already), so this can only normalize `root`, not re-anchor it.
|
|
14
|
+
*/
|
|
1
15
|
export declare function resolvePathWithin({ root, segment }: {
|
|
2
16
|
root: string;
|
|
3
17
|
segment: string;
|
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
/** Preserve strict lexical containment: relative roots, root equality and traversal are refused (except the filesystem-root edge case). Symlinks require a separate physical-path guard. */
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Resolves `<root>/<segment>` and refuses (returns `null`) unless the result stays strictly
|
|
5
|
+
* within `root`. Two different escapes are checked, not one duplicated:
|
|
6
|
+
* `resolved !== path.join(root, segment)` catches `path.resolve` diverging from a naive join —
|
|
7
|
+
* the case where `root` itself was not already absolute, so `resolve` silently anchors against
|
|
8
|
+
* `process.cwd()` instead; `!resolved.startsWith(prefix)` catches `segment` carrying enough
|
|
9
|
+
* `../` to walk back out of an (already-absolute) `root` even though both agree on the
|
|
10
|
+
* normalized result. `prefix` is built from `path.resolve(root)` rather than raw `root` so a
|
|
11
|
+
* trailing separator on `root` (or `root` being exactly `/`) can't double up into a separator
|
|
12
|
+
* nothing would ever start with — that would silently refuse every valid child instead of only
|
|
13
|
+
* real traversal. Calling `path.resolve` on `root` here is safe specifically because the guard
|
|
14
|
+
* above already proved `root` is absolute (a relative `root` would have diverged from
|
|
15
|
+
* `path.join` and returned already), so this can only normalize `root`, not re-anchor it.
|
|
16
|
+
*/
|
|
3
17
|
export function resolvePathWithin({ root, segment }, _optional = {}) {
|
|
4
18
|
const resolved = path.resolve(root, segment);
|
|
5
19
|
if (resolved !== path.join(root, segment))
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file A dependency-free, minimal PNG encoder — just enough to produce a real, decodable image for
|
|
3
|
+
* a host demo tool to prove the typed-media pipeline end to end ("tool results must carry typed
|
|
4
|
+
* media rather than flattened text").
|
|
5
|
+
*
|
|
6
|
+
* A solid-color square, not a photo or a fetched asset, deliberately: the point of such a demo
|
|
7
|
+
* tool is proving the TRANSPORT (daemon -> wire -> chat-core -> ToolCard `<img>`), not
|
|
8
|
+
* building an image generator. A hand-rolled encoder over a third-party image library or a real
|
|
9
|
+
* external call keeps the dependency count at zero — `node:zlib`, already in the runtime — and
|
|
10
|
+
* deterministic, so its own tests assert exact pixel bytes rather than "didn't throw".
|
|
11
|
+
*
|
|
12
|
+
* The format written is the minimum PNG needs to be valid: signature, `IHDR` (8-bit truecolor,
|
|
13
|
+
* no interlacing), one `IDAT` holding every scanline (each prefixed with filter type 0 — "none",
|
|
14
|
+
* the simplest legal choice for uncompressed-looking row data before deflate), and an empty `IEND`.
|
|
15
|
+
* Every chunk is real deflate + real CRC-32, so a real PNG decoder (a browser `<img>`, `zlib.inflateSync`
|
|
16
|
+
* in this file's own tests) can round-trip it — nothing here is a stub shaped like a PNG.
|
|
17
|
+
*/
|
|
18
|
+
/** 0-255 per channel. */
|
|
19
|
+
export interface RgbColor {
|
|
20
|
+
readonly r: number;
|
|
21
|
+
readonly g: number;
|
|
22
|
+
readonly b: number;
|
|
23
|
+
}
|
|
24
|
+
export interface RenderSolidColorPngOptions {
|
|
25
|
+
/** Pixel width. Must be a positive integer. @default 64 */
|
|
26
|
+
readonly width?: number;
|
|
27
|
+
/** Pixel height. Must be a positive integer. @default 64 */
|
|
28
|
+
readonly height?: number;
|
|
29
|
+
/** Fill color. @default { r: 79, g: 70, b: 229 } (indigo). */
|
|
30
|
+
readonly color?: RgbColor;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Renders a solid-color square as a real, valid 8-bit truecolor PNG.
|
|
34
|
+
*
|
|
35
|
+
* @param options - Dimensions and fill color; every field optional with the module's own defaults.
|
|
36
|
+
* @returns The complete PNG file as a `Buffer` — signature through `IEND`.
|
|
37
|
+
* @throws {Error} If `width`/`height` is not a positive integer, or any `color` channel is outside `0..255`.
|
|
38
|
+
* @complexity O(width * height) to build the raw scanlines plus the `deflateSync` call's own cost.
|
|
39
|
+
*/
|
|
40
|
+
export declare function renderSolidColorPng(options?: RenderSolidColorPngOptions): Buffer;
|
|
41
|
+
//# sourceMappingURL=png-encode.d.ts.map
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { deflateSync } from "node:zlib";
|
|
2
|
+
const DEFAULT_WIDTH = 64;
|
|
3
|
+
const DEFAULT_HEIGHT = 64;
|
|
4
|
+
const DEFAULT_COLOR = { r: 79, g: 70, b: 229 };
|
|
5
|
+
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
6
|
+
/**
|
|
7
|
+
* The standard PNG/zlib CRC-32 table (ITU-T V.42 polynomial 0xEDB88320), built once at module load
|
|
8
|
+
* — every chunk's trailing CRC is computed against it.
|
|
9
|
+
*
|
|
10
|
+
* @complexity O(1) — 256 iterations, once per process.
|
|
11
|
+
*/
|
|
12
|
+
function buildCrcTable() {
|
|
13
|
+
const table = new Uint32Array(256);
|
|
14
|
+
for (let n = 0; n < 256; n++) {
|
|
15
|
+
let c = n;
|
|
16
|
+
for (let k = 0; k < 8; k++) {
|
|
17
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
18
|
+
}
|
|
19
|
+
table[n] = c >>> 0;
|
|
20
|
+
}
|
|
21
|
+
return table;
|
|
22
|
+
}
|
|
23
|
+
const CRC_TABLE = buildCrcTable();
|
|
24
|
+
/** PNG's chunk CRC: computed over the chunk's `type` + `data` bytes, per the spec — never over `length`. */
|
|
25
|
+
function crc32({ bytes }, _optional = {}) {
|
|
26
|
+
let crc = 0xffffffff;
|
|
27
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
28
|
+
crc = (CRC_TABLE[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8)) >>> 0;
|
|
29
|
+
}
|
|
30
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
31
|
+
}
|
|
32
|
+
/** One length-prefixed, CRC-suffixed PNG chunk: `length(4) + type(4) + data(n) + crc(4)`. */
|
|
33
|
+
function chunk({ type, data }, _optional = {}) {
|
|
34
|
+
const typeBytes = Buffer.from(type, "ascii");
|
|
35
|
+
const length = Buffer.alloc(4);
|
|
36
|
+
length.writeUInt32BE(data.length, 0);
|
|
37
|
+
const crcInput = Buffer.concat([typeBytes, data]);
|
|
38
|
+
const crc = Buffer.alloc(4);
|
|
39
|
+
crc.writeUInt32BE(crc32({ bytes: crcInput }), 0);
|
|
40
|
+
return Buffer.concat([length, typeBytes, data, crc]);
|
|
41
|
+
}
|
|
42
|
+
/** @throws {Error} `"renderSolidColorPng: {label} must be a positive integer, got {value}"` for a non-integer or non-positive dimension. */
|
|
43
|
+
function assertPositiveInteger({ value, label }, _optional = {}) {
|
|
44
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
45
|
+
throw new Error(`renderSolidColorPng: ${label} must be a positive integer, got ${value}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** @throws {Error} `"renderSolidColorPng: color.{channel} must be an integer between 0 and 255, got {value}"` for an out-of-range channel. */
|
|
49
|
+
function assertChannel({ value, channel }, _optional = {}) {
|
|
50
|
+
if (!Number.isInteger(value) || value < 0 || value > 255) {
|
|
51
|
+
throw new Error(`renderSolidColorPng: color.${channel} must be an integer between 0 and 255, got ${value}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Renders a solid-color square as a real, valid 8-bit truecolor PNG.
|
|
56
|
+
*
|
|
57
|
+
* @param options - Dimensions and fill color; every field optional with the module's own defaults.
|
|
58
|
+
* @returns The complete PNG file as a `Buffer` — signature through `IEND`.
|
|
59
|
+
* @throws {Error} If `width`/`height` is not a positive integer, or any `color` channel is outside `0..255`.
|
|
60
|
+
* @complexity O(width * height) to build the raw scanlines plus the `deflateSync` call's own cost.
|
|
61
|
+
*/
|
|
62
|
+
export function renderSolidColorPng(options = {}) {
|
|
63
|
+
const width = options.width ?? DEFAULT_WIDTH;
|
|
64
|
+
const height = options.height ?? DEFAULT_HEIGHT;
|
|
65
|
+
const color = options.color ?? DEFAULT_COLOR;
|
|
66
|
+
assertPositiveInteger({ value: width, label: "width" });
|
|
67
|
+
assertPositiveInteger({ value: height, label: "height" });
|
|
68
|
+
assertChannel({ value: color.r, channel: "r" });
|
|
69
|
+
assertChannel({ value: color.g, channel: "g" });
|
|
70
|
+
assertChannel({ value: color.b, channel: "b" });
|
|
71
|
+
// One filter byte (0 = "None") plus 3 bytes/pixel per scanline — PNG's raw, pre-deflate layout for
|
|
72
|
+
// 8-bit truecolor (colorType 2, no alpha).
|
|
73
|
+
const bytesPerRow = 1 + width * 3;
|
|
74
|
+
const raw = Buffer.alloc(bytesPerRow * height);
|
|
75
|
+
for (let y = 0; y < height; y++) {
|
|
76
|
+
const rowStart = y * bytesPerRow;
|
|
77
|
+
raw[rowStart] = 0; // filter type: None
|
|
78
|
+
for (let x = 0; x < width; x++) {
|
|
79
|
+
const pixelStart = rowStart + 1 + x * 3;
|
|
80
|
+
raw[pixelStart] = color.r;
|
|
81
|
+
raw[pixelStart + 1] = color.g;
|
|
82
|
+
raw[pixelStart + 2] = color.b;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const ihdr = Buffer.alloc(13);
|
|
86
|
+
ihdr.writeUInt32BE(width, 0);
|
|
87
|
+
ihdr.writeUInt32BE(height, 4);
|
|
88
|
+
ihdr[8] = 8; // bit depth
|
|
89
|
+
ihdr[9] = 2; // color type: truecolor (RGB, no palette, no alpha)
|
|
90
|
+
ihdr[10] = 0; // compression method: deflate (the only defined value)
|
|
91
|
+
ihdr[11] = 0; // filter method: adaptive (the only defined value; per-row filter is still 0/"None" above)
|
|
92
|
+
ihdr[12] = 0; // interlace method: none
|
|
93
|
+
const idat = deflateSync(raw);
|
|
94
|
+
return Buffer.concat([PNG_SIGNATURE, chunk({ type: "IHDR", data: ihdr }), chunk({ type: "IDAT", data: idat }), chunk({ type: "IEND", data: Buffer.alloc(0) })]);
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=png-encode.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -34,7 +34,9 @@
|
|
|
34
34
|
export { createCommandInvocation, createPackageManagerInvocation } from "./command.js";
|
|
35
35
|
export type { ResolveSystemProxyEnvOptions, SystemProxyCommandRunner } from "./proxy-env.js";
|
|
36
36
|
export { mergeProxyAwareEnv, parseMacosScutilProxyOutput, parseWindowsInternetSettingsProxyOutput, resolveSystemProxyEnv, } from "./proxy-env.js";
|
|
37
|
-
|
|
37
|
+
/** Deprecated re-export: import these from `@jini-ai/core/contracts/site/platform-process`; removed at the next major. */
|
|
38
|
+
export type { ProcessUsage } from "@jini-ai/core/contracts/site/platform-process";
|
|
39
|
+
export type { ProcessUsagePorts, ProcessUsageOptions, ProcessStampContract, ProcessStampField, ProcessStampShape, SpawnProcessRequest, StampedProcessMatchCriteria, } from "./process.js";
|
|
38
40
|
export { createProcessStampArgs, isProcessAlive, listProcessSnapshots, listProcessUsage, matchesProcessStamp, matchesStampedProcess, readFlagValue, readProcessStamp, readProcessStampFromCommand, spawnBackgroundProcess, spawnLoggedProcess, stopProcesses, waitForProcessExit, } from "./process.js";
|
|
39
41
|
export type { AtomicCopyFileOptions, AtomicCopyFileResult, RemovePathBestEffortOptions, RemovePathBestEffortResult, } from "./fs.js";
|
|
40
42
|
export { atomicCopyFile, pathContains, readLogTail, removePathBestEffort } from "./fs.js";
|