@anhlp/alp-daemon 0.0.1-beta.3 → 0.0.1-beta.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/README.md +4 -0
- package/dist/THIRD-PARTY-NOTICES.txt +26 -1
- package/dist/alpd.js +29928 -14898
- package/dist/attachments.d.ts +187 -0
- package/dist/attachments.js +856 -0
- package/dist/build-manifest.json +34 -1
- package/dist/conversation.d.ts +119 -0
- package/dist/conversation.js +688 -0
- package/dist/disk.d.ts +67 -0
- package/dist/disk.js +152 -0
- package/dist/files.d.ts +154 -2
- package/dist/files.js +392 -68
- package/dist/git.d.ts +93 -0
- package/dist/git.js +1192 -0
- package/dist/gitignore.d.ts +5 -0
- package/dist/gitignore.js +7 -1
- package/dist/history.d.ts +24 -0
- package/dist/history.js +149 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +21 -1
- package/dist/lifecycle.d.ts +101 -0
- package/dist/lifecycle.js +239 -0
- package/dist/main.js +43 -11
- package/dist/native-terminal.d.ts +45 -0
- package/dist/native-terminal.js +198 -0
- package/dist/operations.d.ts +8 -2
- package/dist/operations.js +20 -10
- package/dist/registry.d.ts +89 -0
- package/dist/registry.js +343 -0
- package/dist/scheduler.d.ts +96 -0
- package/dist/scheduler.js +296 -0
- package/dist/schedules.d.ts +70 -0
- package/dist/schedules.js +246 -0
- package/dist/server.d.ts +14 -2
- package/dist/server.js +836 -90
- package/dist/store.d.ts +72 -2
- package/dist/store.js +111 -4
- package/dist/terminal-native/README.md +201 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/dist/terminal-native/terminal-owner.c +389 -0
- package/dist/terminal-screen.d.ts +24 -0
- package/dist/terminal-screen.js +99 -0
- package/dist/terminals.d.ts +193 -0
- package/dist/terminals.js +570 -0
- package/dist/windows-terminal.d.ts +29 -0
- package/dist/windows-terminal.js +122 -0
- package/dist/worktrees.d.ts +65 -0
- package/dist/worktrees.js +318 -0
- package/native/README.md +201 -0
- package/native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/native/prebuilt/darwin-x64/manifest.json +11 -0
- package/native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/native/terminal-owner.c +389 -0
- package/package.json +70 -7
- package/src/attachments.ts +908 -0
- package/src/conversation.ts +857 -0
- package/src/disk.ts +175 -0
- package/src/files.ts +528 -82
- package/src/git.ts +1140 -0
- package/src/gitignore.ts +10 -0
- package/src/history.ts +118 -0
- package/src/index.ts +7 -0
- package/src/lifecycle.ts +283 -0
- package/src/main.ts +37 -12
- package/src/native-terminal.ts +122 -0
- package/src/operations.ts +27 -11
- package/src/registry.ts +238 -0
- package/src/scheduler.ts +432 -0
- package/src/schedules.ts +297 -0
- package/src/server.ts +948 -102
- package/src/store.ts +117 -4
- package/src/terminal-screen.ts +74 -0
- package/src/terminals.ts +641 -0
- package/src/windows-terminal.ts +123 -0
- package/src/worktrees.ts +382 -0
|
@@ -0,0 +1,908 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import { constants, type Stats } from 'node:fs';
|
|
3
|
+
import { lstat, mkdir, open, readdir, realpath, rename, rm, type FileHandle } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { Transform, type Readable } from 'node:stream';
|
|
6
|
+
import { ATTACHMENTS, ERROR, RPC_ERROR, attachmentChunkBytes } from '@anhlp/alp-protocol';
|
|
7
|
+
import type { AttachmentInfo, AttachmentLimits } from '@anhlp/alp-protocol/rpc';
|
|
8
|
+
import type { RuntimeAttachment } from '@anhlp/alp-protocol/session';
|
|
9
|
+
import { RpcError } from './rpc-error.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Non-image attachments (capability `attachments`, docs/protocol/alp-json-rpc-v1.md §12.6, issue
|
|
13
|
+
* #121 branch I): daemon-owned immutable objects under `<ALP_HOME>/state/attachments/<id>/`, each a
|
|
14
|
+
* directory (0700) holding an opaque `payload` (0600, 0400 once committed) and its `meta.json`,
|
|
15
|
+
* plus one durable reference index `refs.json` beside them.
|
|
16
|
+
*
|
|
17
|
+
* - An upload is bound to its principal (identity and grant epoch; `owner` for the local owner), the
|
|
18
|
+
* canonical project of its session, and the session. Every chunk, commit, get, delete and prompt
|
|
19
|
+
* checks the whole binding again; an id that is unknown, of another principal, epoch, project or
|
|
20
|
+
* session, or whose session is gone answers the same `1001 not_found`.
|
|
21
|
+
* - Nothing a client sends becomes a path: ids are generated here, the display name is only
|
|
22
|
+
* metadata. Files are created exclusively without following links, and every open checks a
|
|
23
|
+
* regular file with one name whose device and inode are the ones recorded at creation, so a
|
|
24
|
+
* swapped, linked or replaced directory or payload is refused rather than followed.
|
|
25
|
+
* - Quota is reserved at begin for the declared size, synchronously, so concurrent begins cannot
|
|
26
|
+
* oversubscribe; it is held until the object is deleted, whether or not it is referenced.
|
|
27
|
+
* - An object is published by writing its meta as committed, atomically, after its whole payload
|
|
28
|
+
* was read back and its SHA-256 matched. A write failure aborts the upload: nothing half written
|
|
29
|
+
* is ever committed and its reservation is released.
|
|
30
|
+
* - References are holders (a prompt receipt key, later a fork) naming the attachments they keep,
|
|
31
|
+
* in `refs.json`, written atomically before what needs them (a prompt's delivery). A referenced
|
|
32
|
+
* object is never collected; one whose references were released is kept `releasedGraceMs` more.
|
|
33
|
+
*
|
|
34
|
+
* Process crashes are covered (atomic renames, temporary files cleaned at start); power loss is not
|
|
35
|
+
* claimed. Windows is not supported (no O_NOFOLLOW, no POSIX modes): alpd does not serve this there.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export type Principal = { id: string; epoch: string; roots?: readonly string[] };
|
|
39
|
+
export type AttachmentLimitsInput = Partial<AttachmentLimits>;
|
|
40
|
+
|
|
41
|
+
/** What alpd binds for a runtime that takes attachments (the seam of issue #121 branch J). */
|
|
42
|
+
export type AttachmentReader = {
|
|
43
|
+
/**
|
|
44
|
+
* The verified bytes of an attachment some holder of `sessionId` pins (a prompt alpd delivers to
|
|
45
|
+
* it): a read-only stream of exactly `size` bytes whose SHA-256 is checked at its end, never a path.
|
|
46
|
+
*/
|
|
47
|
+
open(attachmentId: string, sessionId: string): Promise<{ stream: Readable; attachment: RuntimeAttachment }>;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** The runtime side of the seam: absent members mean the runtime takes no attachment parts. */
|
|
51
|
+
export type AttachmentRuntime = {
|
|
52
|
+
/** Whether this live session's runtime takes `{ type: 'attachment' }` parts (RuntimeAttachment) now. */
|
|
53
|
+
takesAttachments?(sessionId: string): boolean;
|
|
54
|
+
/** alpd hands its reader over once, at start, as it does its schedules. */
|
|
55
|
+
bindAttachments?(reader: AttachmentReader | undefined): void;
|
|
56
|
+
/** Removes the runtime's staged copy of an object alpd collected. */
|
|
57
|
+
dropAttachment?(attachmentId: string): Promise<void>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
type Meta = {
|
|
61
|
+
version: 1;
|
|
62
|
+
id: string;
|
|
63
|
+
state: 'uploading' | 'committed';
|
|
64
|
+
principalId: string;
|
|
65
|
+
principalEpoch: string;
|
|
66
|
+
project: string;
|
|
67
|
+
sessionId: string;
|
|
68
|
+
requestKey: string;
|
|
69
|
+
fingerprint: string;
|
|
70
|
+
name: string;
|
|
71
|
+
mimeType: string;
|
|
72
|
+
size: number;
|
|
73
|
+
declaredSha256?: string;
|
|
74
|
+
createdAt: string;
|
|
75
|
+
expiresAt: string;
|
|
76
|
+
dir: { dev: string; ino: string };
|
|
77
|
+
payload: { dev: string; ino: string };
|
|
78
|
+
sha256?: string;
|
|
79
|
+
detectedMimeType?: string;
|
|
80
|
+
committedAt?: string;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
type Holder = { sessionId: string; attachments: string[]; at: string; kind: string };
|
|
84
|
+
type Refs = { version: 1; holders: Record<string, Holder>; released: Record<string, string> };
|
|
85
|
+
|
|
86
|
+
const ID = /^[0-9a-f]{32}$/;
|
|
87
|
+
const UPLOAD = /^upl_([0-9a-f]{32})$/;
|
|
88
|
+
const ATTACHMENT = /^att_([0-9a-f]{32})$/;
|
|
89
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
90
|
+
const NOFOLLOW = constants.O_NOFOLLOW ?? 0;
|
|
91
|
+
const OPEN_READ = constants.O_RDONLY | NOFOLLOW;
|
|
92
|
+
const OPEN_WRITE = constants.O_RDWR | NOFOLLOW;
|
|
93
|
+
const OPEN_CREATE = constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | NOFOLLOW;
|
|
94
|
+
const MAX_META_BYTES = 64 * 1024;
|
|
95
|
+
const MAX_REFS_BYTES = 64 * 1024 * 1024;
|
|
96
|
+
|
|
97
|
+
const notFound = (what: 'upload' | 'attachment') => new RpcError(ERROR.notFound, what === 'upload' ? 'No such upload' : 'No such attachment', { reason: 'not_found' });
|
|
98
|
+
const invalid = (reason: string, message: string, extra: object = {}) => new RpcError(RPC_ERROR.invalidParams, message, { reason, ...extra });
|
|
99
|
+
const conflict = (reason: string, message: string, extra: object = {}) => new RpcError(ERROR.conflict, message, { reason, ...extra });
|
|
100
|
+
const unavailable = (reason: string, message: string, extra: object = {}) => new RpcError(ERROR.unavailable, message, { reason, ...extra });
|
|
101
|
+
const persistence = (what: string) => new RpcError(ERROR.failed, `alpd could not store ${what}`, { reason: 'persistence_failed' });
|
|
102
|
+
|
|
103
|
+
const sha = (value: unknown) => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
104
|
+
const iso = (ms: number) => new Date(ms).toISOString();
|
|
105
|
+
const identity = (info: Stats) => ({ dev: String(info.dev), ino: String(info.ino) });
|
|
106
|
+
const same = (info: Stats, recorded: { dev: string; ino: string }) => String(info.dev) === recorded.dev && String(info.ino) === recorded.ino;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A display name as alpd keeps it: NFC, without control or format characters (NUL, bidi
|
|
110
|
+
* overrides, zero-width), line or paragraph separators and path separators (/ and \), trimmed; at
|
|
111
|
+
* most `maxBytes` UTF-8 bytes. It is metadata only and never names a file. Undefined when nothing
|
|
112
|
+
* usable is left (empty, `.` or `..`) or it is too long.
|
|
113
|
+
*/
|
|
114
|
+
export function attachmentName(raw: unknown, maxBytes = ATTACHMENTS.maxNameBytes): string | undefined {
|
|
115
|
+
if (typeof raw !== 'string') return undefined;
|
|
116
|
+
const name = raw.normalize('NFC').replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}/\\]/gu, '').trim();
|
|
117
|
+
if (!name || name === '.' || name === '..') return undefined;
|
|
118
|
+
if (Buffer.byteLength(name, 'utf8') > maxBytes) return undefined;
|
|
119
|
+
return name;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* What the first bytes of a payload look like, for display only: the declared MIME type is the
|
|
124
|
+
* client's and is never trusted to choose behaviour, nor is this.
|
|
125
|
+
*/
|
|
126
|
+
export function sniffMimeType(head: Buffer): string {
|
|
127
|
+
const starts = (bytes: number[] | string, at = 0) => {
|
|
128
|
+
const expected = typeof bytes === 'string' ? Buffer.from(bytes, 'latin1') : Buffer.from(bytes);
|
|
129
|
+
return head.length >= at + expected.length && head.subarray(at, at + expected.length).equals(expected);
|
|
130
|
+
};
|
|
131
|
+
if (!head.length) return 'application/octet-stream';
|
|
132
|
+
if (starts('%PDF-')) return 'application/pdf';
|
|
133
|
+
if (starts([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return 'image/png';
|
|
134
|
+
if (starts([0xff, 0xd8, 0xff])) return 'image/jpeg';
|
|
135
|
+
if (starts('GIF87a') || starts('GIF89a')) return 'image/gif';
|
|
136
|
+
if (starts('RIFF') && starts('WEBP', 8)) return 'image/webp';
|
|
137
|
+
if (starts([0x50, 0x4b, 0x03, 0x04]) || starts([0x50, 0x4b, 0x05, 0x06])) return 'application/zip';
|
|
138
|
+
if (starts([0x1f, 0x8b])) return 'application/gzip';
|
|
139
|
+
if (head.includes(0)) return 'application/octet-stream';
|
|
140
|
+
// Valid UTF-8 (a character cut at the end of the head aside) reads as text.
|
|
141
|
+
for (let cut = 0; cut < 4 && cut < head.length; cut++) {
|
|
142
|
+
try { new TextDecoder('utf-8', { fatal: true }).decode(head.subarray(0, head.length - cut)); return 'text/plain'; } catch {}
|
|
143
|
+
}
|
|
144
|
+
return 'application/octet-stream';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Effective limits: ATTACHMENTS, each lowered (never raised) by `input`; the chunk also by the frame. */
|
|
148
|
+
export function attachmentLimits(input: AttachmentLimitsInput = {}, maxFrame?: number): AttachmentLimits {
|
|
149
|
+
const out: Record<string, number> = {};
|
|
150
|
+
for (const key of ['maxObjectBytes', 'maxPerPrompt', 'maxPromptBytes', 'maxChunkBytes', 'maxActiveUploads', 'maxPrincipalBytes', 'maxDaemonBytes', 'uploadTimeoutMs', 'unreferencedRetentionMs', 'releasedGraceMs'] as const) {
|
|
151
|
+
const value = (input as Record<string, unknown>)[key];
|
|
152
|
+
out[key] = typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? Math.min(value, ATTACHMENTS[key]) : ATTACHMENTS[key];
|
|
153
|
+
}
|
|
154
|
+
out.maxChunkBytes = attachmentChunkBytes(maxFrame, Math.max(1, out.maxChunkBytes));
|
|
155
|
+
return out as AttachmentLimits;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Hooks for tests: a write that fails as a full disk would, crash points, a clock. */
|
|
159
|
+
export type AttachmentStoreOptions = {
|
|
160
|
+
/** `<ALP_HOME>/state/attachments`. */
|
|
161
|
+
root: string;
|
|
162
|
+
limits?: AttachmentLimitsInput;
|
|
163
|
+
/** The project root of a session alpd knows (live or recorded), or undefined. */
|
|
164
|
+
sessionProject(sessionId: string): string | undefined;
|
|
165
|
+
now?: () => number;
|
|
166
|
+
crashPoint?: (name: string) => void;
|
|
167
|
+
log?: { warn(message: string, fields?: Record<string, unknown>): void; info?(message: string, fields?: Record<string, unknown>): void };
|
|
168
|
+
io?: { write?(handle: FileHandle, buffer: Buffer, position: number): Promise<void> };
|
|
169
|
+
/** How often expired uploads and unreferenced objects are collected while alpd runs. */
|
|
170
|
+
sweepMs?: number;
|
|
171
|
+
/** Told of each committed object alpd removes (the runtime drops its staged copy); failures are logged. */
|
|
172
|
+
onDiscard?: (attachmentId: string) => Promise<void> | void;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export type AttachmentStore = ReturnType<typeof createAttachmentStore>;
|
|
176
|
+
|
|
177
|
+
export function createAttachmentStore(options: AttachmentStoreOptions) {
|
|
178
|
+
const { root, sessionProject } = options;
|
|
179
|
+
const limits = attachmentLimits(options.limits);
|
|
180
|
+
const now = options.now ?? Date.now;
|
|
181
|
+
const crashPoint = options.crashPoint ?? (() => {});
|
|
182
|
+
const log = options.log;
|
|
183
|
+
/** Every object alpd knows, committed or uploading; reservations count from the moment they are here. */
|
|
184
|
+
const objects = new Map<string, Meta>();
|
|
185
|
+
let refs: Refs = { version: 1, holders: {}, released: {} };
|
|
186
|
+
/** Begins in flight by request key, so a repeat waits for the first. */
|
|
187
|
+
const beginning = new Map<string, Promise<unknown>>();
|
|
188
|
+
const queues = new Map<string, Promise<unknown>>();
|
|
189
|
+
// Track complete public operations, including awaits before their first queue entry.
|
|
190
|
+
const pending = new Set<Promise<unknown>>();
|
|
191
|
+
function admitted<A extends unknown[], R>(run: (...args: A) => Promise<R>) {
|
|
192
|
+
return async (...args: A): Promise<R> => {
|
|
193
|
+
usable();
|
|
194
|
+
const operation = run(...args);
|
|
195
|
+
pending.add(operation);
|
|
196
|
+
try { return await operation; } finally { pending.delete(operation); }
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
let failure: Error | undefined;
|
|
200
|
+
let sweeper: NodeJS.Timeout | undefined;
|
|
201
|
+
/** Referenced object directories whose meta could not be read (logged once). */
|
|
202
|
+
const unreadable = new Set<string>();
|
|
203
|
+
|
|
204
|
+
/** Runs `run` after everything queued under `key`. */
|
|
205
|
+
function serial<T>(key: string, run: () => Promise<T>): Promise<T> {
|
|
206
|
+
const previous = queues.get(key) ?? Promise.resolve();
|
|
207
|
+
const result = previous.catch(() => {}).then(run);
|
|
208
|
+
queues.set(key, result);
|
|
209
|
+
void result.finally(() => { if (queues.get(key) === result) queues.delete(key); }).catch(() => {});
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
const withRefs = <T>(run: () => Promise<T>) => serial('refs', run);
|
|
213
|
+
const withObject = <T>(id: string, run: () => Promise<T>) => serial(`object:${id}`, run);
|
|
214
|
+
|
|
215
|
+
const dirOf = (id: string) => path.join(root, id);
|
|
216
|
+
const payloadOf = (id: string) => path.join(root, id, 'payload');
|
|
217
|
+
const metaOf = (id: string) => path.join(root, id, 'meta.json');
|
|
218
|
+
const refsFile = path.join(root, 'refs.json');
|
|
219
|
+
|
|
220
|
+
/** Writes `file` through an exclusive temporary file beside it, then one rename. */
|
|
221
|
+
async function writeAtomic(file: string, body: string) {
|
|
222
|
+
const temporary = `${file}.${randomBytes(6).toString('hex')}.tmp`;
|
|
223
|
+
let handle: FileHandle | undefined;
|
|
224
|
+
try {
|
|
225
|
+
handle = await open(temporary, OPEN_CREATE, 0o600);
|
|
226
|
+
await handle.writeFile(body, 'utf8');
|
|
227
|
+
await handle.datasync();
|
|
228
|
+
await handle.close();
|
|
229
|
+
handle = undefined;
|
|
230
|
+
await rename(temporary, file);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
await handle?.close().catch(() => {});
|
|
233
|
+
await rm(temporary, { force: true }).catch(() => {});
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Reads a small regular file without following a link at its end. */
|
|
239
|
+
async function readSmall(file: string, max: number) {
|
|
240
|
+
const handle = await open(file, OPEN_READ);
|
|
241
|
+
try {
|
|
242
|
+
const info = await handle.stat();
|
|
243
|
+
if (!info.isFile() || info.nlink !== 1 || info.size > max) throw new Error('not a private regular file');
|
|
244
|
+
return await handle.readFile('utf8');
|
|
245
|
+
} finally { await handle.close(); }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** The object's directory, checked: a real directory (no link), private, the one created. */
|
|
249
|
+
async function checkDir(meta: Meta) {
|
|
250
|
+
const info = await lstat(dirOf(meta.id));
|
|
251
|
+
if (!info.isDirectory() || info.isSymbolicLink() || !same(info, meta.dir) || (process.platform !== 'win32' && (info.mode & 0o077) !== 0)) throw new Error('attachment directory changed');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Opens the object's payload, checked: no link, a regular file with one name, the one created, no other access. */
|
|
255
|
+
async function openPayload(meta: Meta, flags: number) {
|
|
256
|
+
await checkDir(meta);
|
|
257
|
+
const handle = await open(payloadOf(meta.id), flags);
|
|
258
|
+
try {
|
|
259
|
+
const info = await handle.stat();
|
|
260
|
+
if (!info.isFile() || info.nlink !== 1 || !same(info, meta.payload) || (process.platform !== 'win32' && (info.mode & 0o177) !== 0)) throw new Error('attachment payload changed');
|
|
261
|
+
return { handle, info };
|
|
262
|
+
} catch (error) { await handle.close().catch(() => {}); throw error; }
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function parseMeta(text: string, id: string): Meta | undefined {
|
|
266
|
+
let value: any;
|
|
267
|
+
try { value = JSON.parse(text); } catch { return undefined; }
|
|
268
|
+
const strings = ['principalId', 'principalEpoch', 'project', 'sessionId', 'requestKey', 'fingerprint', 'name', 'mimeType', 'createdAt', 'expiresAt'];
|
|
269
|
+
if (!value || value.version !== 1 || value.id !== id || !['uploading', 'committed'].includes(value.state) || strings.some(key => typeof value[key] !== 'string')) return undefined;
|
|
270
|
+
if (!Number.isSafeInteger(value.size) || value.size < 0) return undefined;
|
|
271
|
+
for (const key of ['dir', 'payload']) if (typeof value[key]?.dev !== 'string' || typeof value[key]?.ino !== 'string') return undefined;
|
|
272
|
+
if (value.state === 'committed' && (!SHA256.test(value.sha256 ?? '') || typeof value.committedAt !== 'string' || typeof value.detectedMimeType !== 'string')) return undefined;
|
|
273
|
+
return value as Meta;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const holdersOf = (id: string) => Object.values(refs.holders).filter(holder => holder.attachments.includes(id));
|
|
277
|
+
const usage = (principalId?: string) => {
|
|
278
|
+
let bytes = 0; let uploads = 0;
|
|
279
|
+
for (const meta of objects.values()) {
|
|
280
|
+
if (principalId !== undefined && meta.principalId !== principalId) continue;
|
|
281
|
+
bytes += meta.size;
|
|
282
|
+
if (meta.state === 'uploading') uploads++;
|
|
283
|
+
}
|
|
284
|
+
return { bytes, uploads };
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
const info = (meta: Meta): AttachmentInfo => ({
|
|
288
|
+
attachmentId: `att_${meta.id}`, sessionId: meta.sessionId, name: meta.name, mimeType: meta.mimeType,
|
|
289
|
+
detectedMimeType: meta.detectedMimeType!, size: meta.size, sha256: meta.sha256!, createdAt: meta.createdAt,
|
|
290
|
+
committedAt: meta.committedAt!, referenced: holdersOf(meta.id).length > 0,
|
|
291
|
+
});
|
|
292
|
+
const descriptor = (meta: Meta): RuntimeAttachment => ({ type: 'attachment', attachmentId: `att_${meta.id}`, name: meta.name, mimeType: meta.mimeType, detectedMimeType: meta.detectedMimeType!, size: meta.size, sha256: meta.sha256! });
|
|
293
|
+
|
|
294
|
+
/** The canonical project of a session alpd knows now, or undefined. */
|
|
295
|
+
async function projectOfSession(sessionId: string) {
|
|
296
|
+
const project = sessionProject(sessionId);
|
|
297
|
+
if (typeof project !== 'string' || !path.isAbsolute(project)) return undefined;
|
|
298
|
+
return realpath(project).catch(() => undefined);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Whether `principal` may see `meta`: its identity, epoch, an authorized project, and the session still in it. */
|
|
302
|
+
async function bound(meta: Meta, principal: Principal) {
|
|
303
|
+
if (meta.principalId !== principal.id || meta.principalEpoch !== principal.epoch) return false;
|
|
304
|
+
if (principal.roots !== undefined && !principal.roots.includes(meta.project)) return false;
|
|
305
|
+
return (await projectOfSession(meta.sessionId)) === meta.project;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function usable() {
|
|
309
|
+
if (closed) throw unavailable('attachment_unavailable', 'alpd attachment store is closed');
|
|
310
|
+
if (failure) throw unavailable('attachment_unavailable', 'alpd cannot use its attachment store; its log says why');
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** Forgets an object and removes its files; its reservation is released at once. */
|
|
314
|
+
async function discard(meta: Meta, why: string) {
|
|
315
|
+
objects.delete(meta.id);
|
|
316
|
+
if (refs.released[meta.id] !== undefined) delete refs.released[meta.id];
|
|
317
|
+
await rm(dirOf(meta.id), { recursive: true, force: true }).catch(error => log?.warn('could not remove an attachment directory', { code: 'ALPD_ATTACHMENT_REMOVE_FAILED', why, error }));
|
|
318
|
+
if (meta.state === 'committed') await Promise.resolve().then(() => options.onDiscard?.(`att_${meta.id}`)).catch(error => log?.warn('could not remove the staged copy of an attachment', { code: 'ALPD_ATTACHMENT_UNSTAGE_FAILED', why, error }));
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* An upload of `principal` by its id: uploading, bound, not expired. Only under the object's
|
|
323
|
+
* queue (`locked`) is an expired one removed.
|
|
324
|
+
*/
|
|
325
|
+
async function upload(uploadId: unknown, principal: Principal, locked = true) {
|
|
326
|
+
const id = typeof uploadId === 'string' ? UPLOAD.exec(uploadId)?.[1] : undefined;
|
|
327
|
+
const meta = id ? objects.get(id) : undefined;
|
|
328
|
+
if (!meta || meta.state !== 'uploading' || !await bound(meta, principal)) throw notFound('upload');
|
|
329
|
+
if (locked && Date.parse(meta.expiresAt) <= now()) {
|
|
330
|
+
await discard(meta, 'expired');
|
|
331
|
+
throw new RpcError(ERROR.notFound, 'This upload expired; begin it again', { reason: 'upload_expired' });
|
|
332
|
+
}
|
|
333
|
+
return meta;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** A committed attachment of `principal` by its id. */
|
|
337
|
+
async function attachment(attachmentId: unknown, principal: Principal) {
|
|
338
|
+
const id = typeof attachmentId === 'string' ? ATTACHMENT.exec(attachmentId)?.[1] : undefined;
|
|
339
|
+
const meta = id ? objects.get(id) : undefined;
|
|
340
|
+
if (!meta || meta.state !== 'committed' || !await bound(meta, principal)) throw notFound('attachment');
|
|
341
|
+
return meta;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async function saveRefs(next: Refs) {
|
|
345
|
+
await writeAtomic(refsFile, JSON.stringify(next));
|
|
346
|
+
refs = next;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Collects expired uploads, unreferenced objects past their retention and leftovers of a crash. */
|
|
350
|
+
async function sweep() {
|
|
351
|
+
if (failure || closed) return;
|
|
352
|
+
await withRefs(async () => {
|
|
353
|
+
const at = now();
|
|
354
|
+
const released = { ...refs.released };
|
|
355
|
+
let changed = false;
|
|
356
|
+
for (const meta of [...objects.values()]) {
|
|
357
|
+
await withObject(meta.id, async () => {
|
|
358
|
+
if (objects.get(meta.id) !== meta) return;
|
|
359
|
+
if (meta.state === 'uploading') {
|
|
360
|
+
if (Date.parse(meta.expiresAt) <= at) await discard(meta, 'expired');
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (holdersOf(meta.id).length) return;
|
|
364
|
+
const since = released[meta.id];
|
|
365
|
+
const due = since !== undefined ? Date.parse(since) + limits.releasedGraceMs : Date.parse(meta.committedAt!) + limits.unreferencedRetentionMs;
|
|
366
|
+
if (!(due <= at)) return;
|
|
367
|
+
if (since !== undefined) { delete released[meta.id]; changed = true; }
|
|
368
|
+
await discard(meta, since !== undefined ? 'released' : 'unreferenced');
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
for (const id of Object.keys(released)) if (!objects.has(id)) { delete released[id]; changed = true; }
|
|
372
|
+
if (changed) await saveRefs({ ...refs, released }).catch(error => log?.warn('could not record collected attachments', { code: 'ALPD_ATTACHMENT_REFS_FAILED', error }));
|
|
373
|
+
await removeLeftovers(at);
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** Directories without a readable meta (a begin a crash cut short) after the upload timeout; temporary files. */
|
|
378
|
+
async function removeLeftovers(at: number) {
|
|
379
|
+
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
380
|
+
const file = path.join(root, entry.name);
|
|
381
|
+
if (entry.name.startsWith('refs.json.') && entry.name.endsWith('.tmp')) { await rm(file, { force: true }); continue; }
|
|
382
|
+
if (!ID.test(entry.name) || objects.has(entry.name)) continue;
|
|
383
|
+
const info = await lstat(file).catch(() => undefined);
|
|
384
|
+
if (!info) continue;
|
|
385
|
+
if (!info.isDirectory() || info.isSymbolicLink()) { await rm(file, { force: true }); continue; }
|
|
386
|
+
// A directory whose meta cannot be read but that a reference names is never removed: it is kept for repair.
|
|
387
|
+
if (holdersOf(entry.name).length) {
|
|
388
|
+
if (!unreadable.has(entry.name)) { unreadable.add(entry.name); log?.warn('a referenced attachment\'s metadata cannot be read; it is kept', { code: 'ALPD_ATTACHMENT_UNAVAILABLE', attachmentId: `att_${entry.name}` }); }
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
if (at - info.mtimeMs >= limits.uploadTimeoutMs) await rm(file, { recursive: true, force: true });
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Start-up: the root checked private, the reference index loaded (an unreadable one kept aside
|
|
397
|
+
* and every committed object treated as referenced: nothing is collected on a guess), objects
|
|
398
|
+
* loaded with their payloads checked, temporary files and expired uploads removed.
|
|
399
|
+
*/
|
|
400
|
+
async function recover() {
|
|
401
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
402
|
+
const top = await lstat(root);
|
|
403
|
+
if (!top.isDirectory() || top.isSymbolicLink() || (process.platform !== 'win32' && ((top.mode & 0o077) !== 0 || top.uid !== process.getuid?.()))) throw new Error('the attachment store must be a private directory of the daemon user (0700), not a link');
|
|
404
|
+
let conservative = false;
|
|
405
|
+
try {
|
|
406
|
+
const parsed = JSON.parse(await readSmall(refsFile, MAX_REFS_BYTES));
|
|
407
|
+
if (!parsed || parsed.version !== 1 || typeof parsed.holders !== 'object' || typeof parsed.released !== 'object') throw new Error('not a reference index');
|
|
408
|
+
for (const holder of Object.values(parsed.holders) as Holder[]) if (typeof holder?.sessionId !== 'string' || !Array.isArray(holder.attachments)) throw new Error('not a reference index');
|
|
409
|
+
refs = { version: 1, holders: parsed.holders, released: parsed.released };
|
|
410
|
+
} catch (error: any) {
|
|
411
|
+
if (error?.code !== 'ENOENT') {
|
|
412
|
+
conservative = true;
|
|
413
|
+
const aside = `${refsFile}.corrupt-${now()}`;
|
|
414
|
+
await rename(refsFile, aside).catch(() => {});
|
|
415
|
+
log?.warn('the attachment reference index cannot be read; kept aside, every stored attachment is kept as referenced', { code: 'ALPD_ATTACHMENT_REFS_UNREADABLE', error });
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const at = now();
|
|
419
|
+
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
420
|
+
if (!ID.test(entry.name)) continue;
|
|
421
|
+
const directory = dirOf(entry.name);
|
|
422
|
+
const dirInfo = await lstat(directory).catch(() => undefined);
|
|
423
|
+
if (!dirInfo || !dirInfo.isDirectory() || dirInfo.isSymbolicLink()) continue;
|
|
424
|
+
for (const name of await readdir(directory)) if (name.startsWith('meta.json.') && name.endsWith('.tmp')) await rm(path.join(directory, name), { force: true });
|
|
425
|
+
const meta = parseMeta(await readSmall(metaOf(entry.name), MAX_META_BYTES).catch(() => ''), entry.name);
|
|
426
|
+
if (!meta) continue;
|
|
427
|
+
if (meta.state === 'uploading' && Date.parse(meta.expiresAt) <= at) { await rm(directory, { recursive: true, force: true }); continue; }
|
|
428
|
+
objects.set(meta.id, meta);
|
|
429
|
+
if (meta.state === 'committed') {
|
|
430
|
+
// A crash between publication and making it read-only.
|
|
431
|
+
const opened = await openPayload(meta, OPEN_READ).catch(() => undefined);
|
|
432
|
+
if (opened) { await opened.handle.chmod(0o400).catch(() => {}); await opened.handle.close(); }
|
|
433
|
+
else log?.warn('a stored attachment\'s payload is missing or changed; it is kept as unavailable', { code: 'ALPD_ATTACHMENT_UNAVAILABLE', attachmentId: `att_${meta.id}` });
|
|
434
|
+
if (conservative) refs.holders[`recovered:${meta.id}`] = { sessionId: meta.sessionId, attachments: [meta.id], at: iso(at), kind: 'recovered' };
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (conservative) await saveRefs(refs);
|
|
438
|
+
await removeLeftovers(at);
|
|
439
|
+
await sweep();
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const recovered = recover().catch(error => {
|
|
443
|
+
failure = error instanceof Error ? error : new Error(String(error));
|
|
444
|
+
log?.warn('the attachment store cannot be used', { code: 'ALPD_ATTACHMENTS_UNUSABLE', error });
|
|
445
|
+
});
|
|
446
|
+
let closed = false;
|
|
447
|
+
// Collection while alpd runs starts only once recovery has loaded every object: before that, an
|
|
448
|
+
// object directory not yet loaded would look like a leftover and be removed.
|
|
449
|
+
if (options.sweepMs !== 0) {
|
|
450
|
+
void recovered.then(() => {
|
|
451
|
+
if (closed || failure) return;
|
|
452
|
+
sweeper = setInterval(() => void admitted(sweep)().catch(error => log?.warn('attachment collection failed', { code: 'ALPD_ATTACHMENT_SWEEP_FAILED', error })), options.sweepMs ?? Math.min(15 * 60 * 1000, Math.max(1000, Math.floor(limits.uploadTimeoutMs / 4))));
|
|
453
|
+
sweeper.unref();
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const beginResult = (meta: Meta, received: number) => ({
|
|
458
|
+
uploadId: `upl_${meta.id}`, state: meta.state, received, size: meta.size, maxChunkBytes: limits.maxChunkBytes,
|
|
459
|
+
...(meta.state === 'uploading' ? { expiresAt: meta.expiresAt } : { attachmentId: `att_${meta.id}` }),
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
/** Bytes stored of an upload so far: its payload's size, read through the checked handle. */
|
|
463
|
+
async function received(meta: Meta) {
|
|
464
|
+
const { handle, info } = await openPayload(meta, OPEN_READ);
|
|
465
|
+
await handle.close();
|
|
466
|
+
return info.size;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
async function write(handle: FileHandle, buffer: Buffer, position: number) {
|
|
470
|
+
if (options.io?.write) return options.io.write(handle, buffer, position);
|
|
471
|
+
let done = 0;
|
|
472
|
+
while (done < buffer.length) {
|
|
473
|
+
const { bytesWritten } = await handle.write(buffer, done, buffer.length - done, position + done);
|
|
474
|
+
if (!bytesWritten) throw new Error('short write');
|
|
475
|
+
done += bytesWritten;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const api = {
|
|
480
|
+
/** Effective limits, advertised in daemon.hello. */
|
|
481
|
+
limits,
|
|
482
|
+
/** Settles when start-up recovery is done (or failed: the methods then answer 1005). */
|
|
483
|
+
ready: () => recovered,
|
|
484
|
+
/** One collection now, after start-up recovery (as the timer's). */
|
|
485
|
+
async sweep() { await recovered; return sweep(); },
|
|
486
|
+
|
|
487
|
+
async begin(principal: Principal, params: { sessionId: string; name: string; size: number; mimeType: string; sha256?: string; requestId: string }) {
|
|
488
|
+
await recovered; usable();
|
|
489
|
+
const name = attachmentName(params.name, ATTACHMENTS.maxNameBytes);
|
|
490
|
+
if (!name) throw invalid('invalid_name', `A file name needs a visible character and at most ${ATTACHMENTS.maxNameBytes} UTF-8 bytes once control characters and path separators are removed`);
|
|
491
|
+
if (!Number.isSafeInteger(params.size) || params.size < 0) throw invalid('invalid_params', 'size must be a byte count');
|
|
492
|
+
if (params.size > limits.maxObjectBytes) throw invalid('attachment_too_large', `An attachment is at most ${limits.maxObjectBytes} bytes`, { limit: limits.maxObjectBytes });
|
|
493
|
+
if (params.sha256 !== undefined && !SHA256.test(params.sha256)) throw invalid('invalid_params', 'sha256 must be 64 lowercase hex digits');
|
|
494
|
+
const mimeType = params.mimeType.toLowerCase();
|
|
495
|
+
const project = await projectOfSession(params.sessionId);
|
|
496
|
+
if (!project || (principal.roots !== undefined && !principal.roots.includes(project))) throw new RpcError(ERROR.notFound, 'No such session', { reason: 'not_found' });
|
|
497
|
+
const requestKey = sha(['attachment.begin', principal.id, principal.epoch, params.requestId]);
|
|
498
|
+
const fingerprint = sha([params.sessionId, name, params.size, mimeType, params.sha256 ?? null]);
|
|
499
|
+
// One request key makes at most one upload: after any await here the checks run again, as a
|
|
500
|
+
// concurrent repeat may have begun it meanwhile.
|
|
501
|
+
for (;;) {
|
|
502
|
+
while (beginning.has(requestKey)) await beginning.get(requestKey)!.catch(() => {});
|
|
503
|
+
const existing = [...objects.values()].find(meta => meta.requestKey === requestKey);
|
|
504
|
+
if (!existing || existing.principalId !== principal.id || existing.principalEpoch !== principal.epoch) break;
|
|
505
|
+
if (existing.fingerprint !== fingerprint) throw conflict('request_conflict', `requestId ${params.requestId} was used for another upload`);
|
|
506
|
+
if (!await bound(existing, principal)) throw new RpcError(ERROR.notFound, 'No such session', { reason: 'not_found' });
|
|
507
|
+
if (existing.state === 'committed') return beginResult(existing, existing.size);
|
|
508
|
+
if (Date.parse(existing.expiresAt) > now()) return withObject(existing.id, async () => beginResult(existing, await received(existing)));
|
|
509
|
+
await withObject(existing.id, () => discard(existing, 'expired'));
|
|
510
|
+
}
|
|
511
|
+
// Reserved here, before anything awaits: concurrent begins see each other's reservation.
|
|
512
|
+
const at = now();
|
|
513
|
+
// Expired uploads release their reservation now, and so do this principal's uploads of
|
|
514
|
+
// another grant epoch (a reissued credential): nobody can continue or abort those any more.
|
|
515
|
+
// Their files go after any call on them.
|
|
516
|
+
for (const meta of [...objects.values()]) {
|
|
517
|
+
if (meta.state !== 'uploading') continue;
|
|
518
|
+
if (Date.parse(meta.expiresAt) > at && !(meta.principalId === principal.id && meta.principalEpoch !== principal.epoch)) continue;
|
|
519
|
+
objects.delete(meta.id);
|
|
520
|
+
void withObject(meta.id, () => rm(dirOf(meta.id), { recursive: true, force: true })).catch(() => {});
|
|
521
|
+
}
|
|
522
|
+
const mine = usage(principal.id);
|
|
523
|
+
if (mine.uploads >= limits.maxActiveUploads) throw unavailable('quota_exceeded', `At most ${limits.maxActiveUploads} uploads may be in progress at once; commit or abort one first`, { limit: 'active_uploads', max: limits.maxActiveUploads });
|
|
524
|
+
if (mine.bytes + params.size > limits.maxPrincipalBytes) throw unavailable('quota_exceeded', `Attachments of this principal take at most ${limits.maxPrincipalBytes} bytes, referenced ones included; delete unreferenced ones first`, { limit: 'principal_bytes', max: limits.maxPrincipalBytes });
|
|
525
|
+
if (usage().bytes + params.size > limits.maxDaemonBytes) throw unavailable('quota_exceeded', 'The daemon host keeps no more attachment bytes now', { limit: 'daemon_bytes', max: limits.maxDaemonBytes });
|
|
526
|
+
const id = randomBytes(16).toString('hex');
|
|
527
|
+
const meta: Meta = {
|
|
528
|
+
version: 1, id, state: 'uploading', principalId: principal.id, principalEpoch: principal.epoch, project, sessionId: params.sessionId,
|
|
529
|
+
requestKey, fingerprint, name, mimeType, size: params.size, ...(params.sha256 ? { declaredSha256: params.sha256 } : {}),
|
|
530
|
+
createdAt: iso(at), expiresAt: iso(at + limits.uploadTimeoutMs), dir: { dev: '', ino: '' }, payload: { dev: '', ino: '' },
|
|
531
|
+
};
|
|
532
|
+
objects.set(id, meta);
|
|
533
|
+
const creating = withObject(id, async () => {
|
|
534
|
+
try {
|
|
535
|
+
await mkdir(dirOf(id), { mode: 0o700 });
|
|
536
|
+
const dirInfo = await lstat(dirOf(id));
|
|
537
|
+
if (!dirInfo.isDirectory() || dirInfo.isSymbolicLink()) throw new Error('attachment directory is not a directory');
|
|
538
|
+
meta.dir = identity(dirInfo);
|
|
539
|
+
const handle = await open(payloadOf(id), OPEN_CREATE, 0o600);
|
|
540
|
+
try { meta.payload = identity(await handle.stat()); } finally { await handle.close(); }
|
|
541
|
+
crashPoint('attachment.begin.before-meta');
|
|
542
|
+
await writeAtomic(metaOf(id), JSON.stringify(meta));
|
|
543
|
+
crashPoint('attachment.begin.after-meta');
|
|
544
|
+
return beginResult(meta, 0);
|
|
545
|
+
} catch (error) {
|
|
546
|
+
if (objects.get(id) === meta) objects.delete(id);
|
|
547
|
+
await rm(dirOf(id), { recursive: true, force: true }).catch(() => {});
|
|
548
|
+
log?.warn('could not begin an attachment upload', { code: 'ALPD_ATTACHMENT_BEGIN_FAILED', error });
|
|
549
|
+
throw persistence('the upload; it was not begun');
|
|
550
|
+
}
|
|
551
|
+
});
|
|
552
|
+
beginning.set(requestKey, creating);
|
|
553
|
+
try { return await creating; } finally { if (beginning.get(requestKey) === creating) beginning.delete(requestKey); }
|
|
554
|
+
},
|
|
555
|
+
|
|
556
|
+
async chunk(principal: Principal, params: { uploadId: string; offset: number; data: string }) {
|
|
557
|
+
await recovered; usable();
|
|
558
|
+
const first = await upload(params.uploadId, principal, false);
|
|
559
|
+
return withObject(first.id, async () => {
|
|
560
|
+
const meta = await upload(params.uploadId, principal);
|
|
561
|
+
const bytes = Buffer.from(params.data, 'base64');
|
|
562
|
+
// Canonical base64 only: what decodes is exactly what was sent.
|
|
563
|
+
if (!bytes.length || bytes.toString('base64') !== params.data) throw invalid('invalid_data', 'data must be canonical base64 of at least one byte');
|
|
564
|
+
if (bytes.length > limits.maxChunkBytes) throw invalid('invalid_data', `A chunk carries at most ${limits.maxChunkBytes} decoded bytes`, { limit: limits.maxChunkBytes });
|
|
565
|
+
if (!Number.isSafeInteger(params.offset) || params.offset < 0) throw invalid('invalid_params', 'offset must be a byte offset');
|
|
566
|
+
if (params.offset + bytes.length > meta.size) throw invalid('attachment_too_large', `This upload declared ${meta.size} bytes; the chunk goes beyond them`, { size: meta.size });
|
|
567
|
+
let opened;
|
|
568
|
+
try { opened = await openPayload(meta, OPEN_WRITE); }
|
|
569
|
+
catch (error) {
|
|
570
|
+
log?.warn('an upload\'s files changed or vanished; it was aborted', { code: 'ALPD_ATTACHMENT_TAMPERED', error });
|
|
571
|
+
await discard(meta, 'tampered');
|
|
572
|
+
throw persistence('the upload; it was aborted');
|
|
573
|
+
}
|
|
574
|
+
const { handle, info } = opened;
|
|
575
|
+
try {
|
|
576
|
+
const stored = info.size;
|
|
577
|
+
if (params.offset > stored) throw conflict('offset_mismatch', `The upload holds ${stored} bytes; send the chunk at offset ${stored}`, { received: stored });
|
|
578
|
+
// A retry of bytes already stored must be those bytes; the rest is appended.
|
|
579
|
+
const overlap = Math.min(stored - params.offset, bytes.length);
|
|
580
|
+
if (overlap > 0) {
|
|
581
|
+
const existing = Buffer.alloc(overlap);
|
|
582
|
+
await handle.read(existing, 0, overlap, params.offset);
|
|
583
|
+
if (!existing.equals(bytes.subarray(0, overlap))) throw conflict('chunk_conflict', `Bytes at offset ${params.offset} were already received with other content`, { received: stored });
|
|
584
|
+
}
|
|
585
|
+
if (overlap < bytes.length) {
|
|
586
|
+
try { await write(handle, bytes.subarray(overlap), params.offset + overlap); }
|
|
587
|
+
catch (error) {
|
|
588
|
+
await handle.close().catch(() => {});
|
|
589
|
+
log?.warn('could not write an upload chunk; the upload was aborted', { code: 'ALPD_ATTACHMENT_WRITE_FAILED', error });
|
|
590
|
+
await discard(meta, 'write failed');
|
|
591
|
+
throw persistence('the chunk; the upload was aborted and its space released');
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return { received: Math.max(stored, params.offset + bytes.length), size: meta.size };
|
|
595
|
+
} finally { await handle.close().catch(() => {}); }
|
|
596
|
+
});
|
|
597
|
+
},
|
|
598
|
+
|
|
599
|
+
async commit(principal: Principal, params: { uploadId: string; sha256: string }) {
|
|
600
|
+
await recovered; usable();
|
|
601
|
+
const id = UPLOAD.exec(params.uploadId)?.[1];
|
|
602
|
+
const known = id ? objects.get(id) : undefined;
|
|
603
|
+
// A repeat of a commit that succeeded answers the same attachment.
|
|
604
|
+
if (known?.state === 'committed' && await bound(known, principal)) {
|
|
605
|
+
if (known.sha256 !== params.sha256) throw conflict('hash_mismatch', 'This upload was committed with another SHA-256');
|
|
606
|
+
return { attachment: info(known) };
|
|
607
|
+
}
|
|
608
|
+
const first = await upload(params.uploadId, principal, false);
|
|
609
|
+
return withObject(first.id, async () => {
|
|
610
|
+
const again = objects.get(first.id);
|
|
611
|
+
if (again?.state === 'committed' && await bound(again, principal)) {
|
|
612
|
+
if (again.sha256 !== params.sha256) throw conflict('hash_mismatch', 'This upload was committed with another SHA-256');
|
|
613
|
+
return { attachment: info(again) };
|
|
614
|
+
}
|
|
615
|
+
const meta = await upload(params.uploadId, principal);
|
|
616
|
+
let opened;
|
|
617
|
+
try { opened = await openPayload(meta, OPEN_READ); }
|
|
618
|
+
catch (error) {
|
|
619
|
+
log?.warn('an upload\'s files changed or vanished; it was aborted', { code: 'ALPD_ATTACHMENT_TAMPERED', error });
|
|
620
|
+
await discard(meta, 'tampered');
|
|
621
|
+
throw persistence('the upload; it was aborted');
|
|
622
|
+
}
|
|
623
|
+
const { handle, info: stat } = opened;
|
|
624
|
+
let digest: string;
|
|
625
|
+
let head: Buffer;
|
|
626
|
+
try {
|
|
627
|
+
if (stat.size !== meta.size) throw conflict('upload_incomplete', `The upload holds ${stat.size} of ${meta.size} bytes`, { received: stat.size, size: meta.size });
|
|
628
|
+
// Every byte read back from the checked file: the digest is of what is stored, not of what was sent.
|
|
629
|
+
const hash = createHash('sha256');
|
|
630
|
+
const buffer = Buffer.alloc(64 * 1024);
|
|
631
|
+
head = Buffer.alloc(0);
|
|
632
|
+
for (let position = 0; position < meta.size;) {
|
|
633
|
+
const { bytesRead } = await handle.read(buffer, 0, Math.min(buffer.length, meta.size - position), position);
|
|
634
|
+
if (!bytesRead) throw new Error('short read');
|
|
635
|
+
hash.update(buffer.subarray(0, bytesRead));
|
|
636
|
+
if (head.length < 8192) head = Buffer.concat([head, buffer.subarray(0, Math.min(bytesRead, 8192 - head.length))]);
|
|
637
|
+
position += bytesRead;
|
|
638
|
+
}
|
|
639
|
+
digest = hash.digest('hex');
|
|
640
|
+
await handle.datasync();
|
|
641
|
+
} catch (error) {
|
|
642
|
+
await handle.close().catch(() => {});
|
|
643
|
+
if (error instanceof RpcError) throw error;
|
|
644
|
+
log?.warn('could not read an upload back; it was aborted', { code: 'ALPD_ATTACHMENT_READ_FAILED', error });
|
|
645
|
+
await discard(meta, 'read failed');
|
|
646
|
+
throw persistence('the upload; it was aborted');
|
|
647
|
+
}
|
|
648
|
+
await handle.close().catch(() => {});
|
|
649
|
+
if (digest !== params.sha256 || (meta.declaredSha256 !== undefined && digest !== meta.declaredSha256)) {
|
|
650
|
+
await discard(meta, 'hash mismatch');
|
|
651
|
+
throw conflict('hash_mismatch', 'The bytes received do not have this SHA-256; the upload was aborted');
|
|
652
|
+
}
|
|
653
|
+
// A begin may have released this upload meanwhile (expired, or of an earlier grant epoch):
|
|
654
|
+
// its files go after this call, and it is never published again.
|
|
655
|
+
if (objects.get(meta.id) !== meta) throw notFound('upload');
|
|
656
|
+
const committed: Meta = { ...meta, state: 'committed', sha256: digest, detectedMimeType: sniffMimeType(head), committedAt: iso(now()) };
|
|
657
|
+
crashPoint('attachment.commit.before-publish');
|
|
658
|
+
try { await writeAtomic(metaOf(meta.id), JSON.stringify(committed)); }
|
|
659
|
+
catch (error) {
|
|
660
|
+
log?.warn('could not publish an attachment; the upload was aborted', { code: 'ALPD_ATTACHMENT_PUBLISH_FAILED', error });
|
|
661
|
+
await discard(meta, 'publish failed');
|
|
662
|
+
throw persistence('the attachment; the upload was aborted and its space released');
|
|
663
|
+
}
|
|
664
|
+
if (objects.get(meta.id) !== meta) throw notFound('upload');
|
|
665
|
+
objects.set(meta.id, committed);
|
|
666
|
+
crashPoint('attachment.commit.after-publish');
|
|
667
|
+
// Read-only from now on; recovery repeats it after a crash in between.
|
|
668
|
+
const readOnly = await openPayload(committed, OPEN_READ).catch(() => undefined);
|
|
669
|
+
if (readOnly) { await readOnly.handle.chmod(0o400).catch(() => {}); await readOnly.handle.close(); }
|
|
670
|
+
return { attachment: info(committed) };
|
|
671
|
+
});
|
|
672
|
+
},
|
|
673
|
+
|
|
674
|
+
async abort(principal: Principal, params: { uploadId: string }) {
|
|
675
|
+
await recovered; usable();
|
|
676
|
+
const first = await upload(params.uploadId, principal, false);
|
|
677
|
+
return withObject(first.id, async () => {
|
|
678
|
+
const meta = await upload(params.uploadId, principal);
|
|
679
|
+
await discard(meta, 'aborted');
|
|
680
|
+
return { aborted: true as const };
|
|
681
|
+
});
|
|
682
|
+
},
|
|
683
|
+
|
|
684
|
+
async get(principal: Principal, params: { attachmentId: string }) {
|
|
685
|
+
await recovered; usable();
|
|
686
|
+
return { attachment: info(await attachment(params.attachmentId, principal)) };
|
|
687
|
+
},
|
|
688
|
+
|
|
689
|
+
/** Deletes an attachment nothing references; a referenced one is refused, never evicted. */
|
|
690
|
+
async remove(principal: Principal, params: { attachmentId: string }) {
|
|
691
|
+
await recovered; usable();
|
|
692
|
+
await attachment(params.attachmentId, principal);
|
|
693
|
+
return withRefs(() => withObject(ATTACHMENT.exec(params.attachmentId)![1], async () => {
|
|
694
|
+
const meta = await attachment(params.attachmentId, principal);
|
|
695
|
+
if (holdersOf(meta.id).length) throw conflict('attachment_referenced', 'A prompt still references this attachment; it is kept');
|
|
696
|
+
const released = { ...refs.released };
|
|
697
|
+
if (released[meta.id] !== undefined) { delete released[meta.id]; await saveRefs({ ...refs, released }).catch(() => {}); }
|
|
698
|
+
await discard(meta, 'deleted');
|
|
699
|
+
return { deleted: true as const };
|
|
700
|
+
}));
|
|
701
|
+
},
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* The attachments of a prompt to `sessionId`, in order, checked as every other call checks them
|
|
705
|
+
* (bound to this principal, project and session) and within the prompt limits, with their payloads
|
|
706
|
+
* there. What the runtime gets in their place: ids, display metadata and digests.
|
|
707
|
+
*/
|
|
708
|
+
async resolve(principal: Principal, sessionId: string, attachmentIds: unknown[]) {
|
|
709
|
+
await recovered; usable();
|
|
710
|
+
if (attachmentIds.length > limits.maxPerPrompt) throw invalid('attachment_limit', `A prompt carries at most ${limits.maxPerPrompt} attachments`, { maxPerPrompt: limits.maxPerPrompt });
|
|
711
|
+
const found: RuntimeAttachment[] = [];
|
|
712
|
+
let total = 0;
|
|
713
|
+
for (const attachmentId of attachmentIds) {
|
|
714
|
+
const meta = await attachment(attachmentId, principal);
|
|
715
|
+
if (meta.sessionId !== sessionId) throw notFound('attachment');
|
|
716
|
+
total += meta.size;
|
|
717
|
+
if (total > limits.maxPromptBytes) throw invalid('attachment_limit', `The attachments of a prompt are at most ${limits.maxPromptBytes} bytes together`, { maxPromptBytes: limits.maxPromptBytes });
|
|
718
|
+
const opened = await openPayload(meta, OPEN_READ).catch(() => undefined);
|
|
719
|
+
await opened?.handle.close();
|
|
720
|
+
if (!opened || opened.info.size !== meta.size) throw unavailable('attachment_unavailable', `Attachment att_${meta.id} is stored but its bytes cannot be read now`, { attachmentId: `att_${meta.id}` });
|
|
721
|
+
found.push(descriptor(meta));
|
|
722
|
+
}
|
|
723
|
+
return found;
|
|
724
|
+
},
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Pins `attachmentIds` for `holder` durably (a prompt's receipt key; t-15b0.4's fork adds the
|
|
728
|
+
* new session's holder the same way after re-authorizing the source's attachments for it). Each
|
|
729
|
+
* must still be committed; a write failure pins nothing. If interrupted by crash before the receipt
|
|
730
|
+
* is written, the pin is held conservatively until session cleanup (releaseSessions).
|
|
731
|
+
*/
|
|
732
|
+
async pin(holder: string, sessionId: string, attachmentIds: string[], kind = 'prompt') {
|
|
733
|
+
await recovered; usable();
|
|
734
|
+
const ids = attachmentIds.map(attachmentId => ATTACHMENT.exec(attachmentId)?.[1]);
|
|
735
|
+
return withRefs(async () => {
|
|
736
|
+
for (const id of ids) if (!id || objects.get(id)?.state !== 'committed') throw notFound('attachment');
|
|
737
|
+
const kept = new Set([...(refs.holders[holder]?.attachments ?? []), ...ids as string[]]);
|
|
738
|
+
const released = { ...refs.released };
|
|
739
|
+
for (const id of kept) delete released[id];
|
|
740
|
+
crashPoint('attachment.pin.before-write');
|
|
741
|
+
try { await saveRefs({ version: 1, holders: { ...refs.holders, [holder]: { sessionId, attachments: [...kept], at: iso(now()), kind } }, released }); }
|
|
742
|
+
catch (error) {
|
|
743
|
+
log?.warn('could not record attachment references', { code: 'ALPD_ATTACHMENT_REFS_FAILED', error });
|
|
744
|
+
throw persistence('the attachment references; the prompt was not sent');
|
|
745
|
+
}
|
|
746
|
+
crashPoint('attachment.pin.after-write');
|
|
747
|
+
});
|
|
748
|
+
},
|
|
749
|
+
|
|
750
|
+
/** The attachments these holders pin, as ids (a fork's kept prompts, §12.7); unknown holders hold none. */
|
|
751
|
+
async heldBy(holders: string[]) {
|
|
752
|
+
await recovered;
|
|
753
|
+
if (failure) return [];
|
|
754
|
+
const ids = new Set<string>();
|
|
755
|
+
for (const holder of holders) for (const id of refs.holders[holder]?.attachments ?? []) ids.add(`att_${id}`);
|
|
756
|
+
return [...ids];
|
|
757
|
+
},
|
|
758
|
+
|
|
759
|
+
/**
|
|
760
|
+
* Re-authorizes attachments for `principal` before another session pins them (a fork, §12.7): each
|
|
761
|
+
* must be committed and bound to this principal, its grant epoch and an authorized project, as every
|
|
762
|
+
* other call checks; otherwise refused as an unknown one.
|
|
763
|
+
*/
|
|
764
|
+
async authorize(principal: Principal, attachmentIds: string[], forks: string[] = []) {
|
|
765
|
+
await recovered; usable();
|
|
766
|
+
for (const attachmentId of attachmentIds) {
|
|
767
|
+
const id = typeof attachmentId === 'string' ? ATTACHMENT.exec(attachmentId)?.[1] : undefined;
|
|
768
|
+
const meta = id ? objects.get(id) : undefined;
|
|
769
|
+
if (meta?.state === 'committed' && await bound(meta, principal)) continue;
|
|
770
|
+
// A fork of a fork whose first ancestor's history is gone: the session the object was uploaded
|
|
771
|
+
// to is no longer known, but a fork in the lineage, still in the same project, pinned it after
|
|
772
|
+
// re-authorizing it for this very principal and epoch. Nothing else is relaxed.
|
|
773
|
+
if (meta?.state === 'committed' && meta.principalId === principal.id && meta.principalEpoch === principal.epoch && (principal.roots === undefined || principal.roots.includes(meta.project))) {
|
|
774
|
+
let held = false;
|
|
775
|
+
for (const fork of forks) {
|
|
776
|
+
if (refs.holders[`fork:${fork}`]?.sessionId === fork && refs.holders[`fork:${fork}`].attachments.includes(meta.id) && await projectOfSession(fork) === meta.project) { held = true; break; }
|
|
777
|
+
}
|
|
778
|
+
if (held) continue;
|
|
779
|
+
}
|
|
780
|
+
throw notFound('attachment');
|
|
781
|
+
}
|
|
782
|
+
},
|
|
783
|
+
|
|
784
|
+
/** Drops `holder`'s references (a prompt refused before it reached the runtime). */
|
|
785
|
+
async release(holder: string) {
|
|
786
|
+
await recovered; usable();
|
|
787
|
+
return withRefs(async () => {
|
|
788
|
+
if (!refs.holders[holder]) return;
|
|
789
|
+
const { [holder]: dropped, ...holders } = refs.holders;
|
|
790
|
+
const released = { ...refs.released };
|
|
791
|
+
for (const id of dropped.attachments) if (!Object.values(holders).some(other => other.attachments.includes(id))) released[id] = iso(now());
|
|
792
|
+
await saveRefs({ version: 1, holders, released });
|
|
793
|
+
});
|
|
794
|
+
},
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* Releases every reference of these sessions: the seam for deleting a session's history (today
|
|
798
|
+
* only retention removes a tree; archive keeps it). Released objects stay `releasedGraceMs`.
|
|
799
|
+
*/
|
|
800
|
+
async releaseSessions(sessionIds: string[]) {
|
|
801
|
+
await recovered;
|
|
802
|
+
if (failure) return;
|
|
803
|
+
const gone = new Set(sessionIds);
|
|
804
|
+
return withRefs(async () => {
|
|
805
|
+
const holders: Record<string, Holder> = {};
|
|
806
|
+
const dropped: string[] = [];
|
|
807
|
+
for (const [key, holder] of Object.entries(refs.holders)) {
|
|
808
|
+
if (gone.has(holder.sessionId)) dropped.push(...holder.attachments);
|
|
809
|
+
else holders[key] = holder;
|
|
810
|
+
}
|
|
811
|
+
if (!dropped.length && Object.keys(holders).length === Object.keys(refs.holders).length) return;
|
|
812
|
+
const released = { ...refs.released };
|
|
813
|
+
for (const id of dropped) if (!Object.values(holders).some(other => other.attachments.includes(id))) released[id] = iso(now());
|
|
814
|
+
await saveRefs({ version: 1, holders, released });
|
|
815
|
+
});
|
|
816
|
+
},
|
|
817
|
+
|
|
818
|
+
/** The runtime's reader (AttachmentReader). */
|
|
819
|
+
reader: {
|
|
820
|
+
async open(attachmentId: string, sessionId: string) {
|
|
821
|
+
await recovered; usable();
|
|
822
|
+
const id = ATTACHMENT.exec(attachmentId)?.[1];
|
|
823
|
+
const meta = id ? objects.get(id) : undefined;
|
|
824
|
+
if (!meta || meta.state !== 'committed' || !Object.values(refs.holders).some(holder => holder.sessionId === sessionId && holder.attachments.includes(meta.id))) throw notFound('attachment');
|
|
825
|
+
const { handle, info: stat } = await openPayload(meta, OPEN_READ);
|
|
826
|
+
if (stat.size !== meta.size) { await handle.close(); throw unavailable('attachment_unavailable', 'The attachment\'s bytes changed', { attachmentId: `att_${meta.id}` }); }
|
|
827
|
+
const hash = createHash('sha256');
|
|
828
|
+
const stream = handle.createReadStream({ start: 0, ...(meta.size ? { end: meta.size - 1 } : {}), autoClose: true });
|
|
829
|
+
const verified = stream.pipe(new Transform({
|
|
830
|
+
transform(chunk, _encoding, done) { hash.update(chunk); done(null, chunk); },
|
|
831
|
+
flush(done) { done(hash.digest('hex') === meta.sha256 ? null : new Error('attachment bytes do not match their SHA-256')); },
|
|
832
|
+
}));
|
|
833
|
+
stream.on('error', error => verified.destroy(error));
|
|
834
|
+
return { stream: verified as Readable, attachment: descriptor(meta) };
|
|
835
|
+
},
|
|
836
|
+
} satisfies AttachmentReader,
|
|
837
|
+
|
|
838
|
+
/** Usage, for tests and status: bytes reserved and uploads in progress, of one principal or all. */
|
|
839
|
+
usage,
|
|
840
|
+
/** The holders of the reference index, for tests. */
|
|
841
|
+
holders: () => structuredClone(refs.holders),
|
|
842
|
+
|
|
843
|
+
async close() {
|
|
844
|
+
closed = true;
|
|
845
|
+
if (sweeper) {
|
|
846
|
+
clearInterval(sweeper);
|
|
847
|
+
sweeper = undefined;
|
|
848
|
+
}
|
|
849
|
+
await recovered;
|
|
850
|
+
await Promise.allSettled([...pending]);
|
|
851
|
+
await Promise.allSettled([...queues.values()]);
|
|
852
|
+
},
|
|
853
|
+
};
|
|
854
|
+
return {
|
|
855
|
+
...api,
|
|
856
|
+
sweep: admitted(api.sweep),
|
|
857
|
+
begin: admitted(api.begin),
|
|
858
|
+
chunk: admitted(api.chunk),
|
|
859
|
+
commit: admitted(api.commit),
|
|
860
|
+
abort: admitted(api.abort),
|
|
861
|
+
get: admitted(api.get),
|
|
862
|
+
remove: admitted(api.remove),
|
|
863
|
+
resolve: admitted(api.resolve),
|
|
864
|
+
pin: admitted(api.pin),
|
|
865
|
+
heldBy: admitted(api.heldBy),
|
|
866
|
+
authorize: admitted(api.authorize),
|
|
867
|
+
release: admitted(api.release),
|
|
868
|
+
releaseSessions: admitted(api.releaseSessions),
|
|
869
|
+
reader: { ...api.reader, open: admitted(api.reader.open) },
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
const LIMIT_KEYS = ['maxObjectBytes', 'maxPerPrompt', 'maxPromptBytes', 'maxChunkBytes', 'maxActiveUploads', 'maxPrincipalBytes', 'maxDaemonBytes', 'uploadTimeoutMs', 'unreferencedRetentionMs', 'releasedGraceMs'] as const;
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* The operator's attachment limits: `<ALP_HOME>/attachments.json`, `{ "limits": { "<name>": n } }`
|
|
877
|
+
* with names of AttachmentLimits and non-negative integers. They may only lower ATTACHMENTS: a
|
|
878
|
+
* higher value, an unknown name or a bad value is ignored and reported. A missing file is no
|
|
879
|
+
* change; one that cannot be read is reported and ignored (the defaults hold).
|
|
880
|
+
*/
|
|
881
|
+
export async function readAttachmentConfig(home: string): Promise<{ limits: AttachmentLimitsInput; problems: string[] }> {
|
|
882
|
+
const problems: string[] = [];
|
|
883
|
+
const limits: Record<string, number> = {};
|
|
884
|
+
let text: string;
|
|
885
|
+
try {
|
|
886
|
+
const handle = await open(path.join(home, 'attachments.json'), OPEN_READ);
|
|
887
|
+
try {
|
|
888
|
+
const stat = await handle.stat();
|
|
889
|
+
if (!stat.isFile() || stat.size > 64 * 1024) throw new Error('not a small regular file');
|
|
890
|
+
text = await handle.readFile('utf8');
|
|
891
|
+
} finally { await handle.close(); }
|
|
892
|
+
} catch (error: any) {
|
|
893
|
+
if (error?.code !== 'ENOENT') problems.push(`attachments.json cannot be read (${error?.code ?? error?.message ?? error}); the default limits hold`);
|
|
894
|
+
return { limits, problems };
|
|
895
|
+
}
|
|
896
|
+
let parsed: any;
|
|
897
|
+
try { parsed = JSON.parse(text); } catch { problems.push('attachments.json is not JSON; the default limits hold'); return { limits, problems }; }
|
|
898
|
+
const given = parsed?.limits;
|
|
899
|
+
if (given === undefined) return { limits, problems };
|
|
900
|
+
if (!given || typeof given !== 'object' || Array.isArray(given)) { problems.push('attachments.json limits must be an object'); return { limits, problems }; }
|
|
901
|
+
for (const [key, value] of Object.entries(given)) {
|
|
902
|
+
if (!(LIMIT_KEYS as readonly string[]).includes(key)) { problems.push(`attachments.json names an unknown limit ${key}`); continue; }
|
|
903
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) { problems.push(`attachments.json limit ${key} must be a non-negative integer`); continue; }
|
|
904
|
+
if (value > ATTACHMENTS[key as keyof typeof ATTACHMENTS]) { problems.push(`attachments.json limit ${key} cannot raise the default ${ATTACHMENTS[key as keyof typeof ATTACHMENTS]}; it is ignored`); continue; }
|
|
905
|
+
limits[key] = value;
|
|
906
|
+
}
|
|
907
|
+
return { limits, problems };
|
|
908
|
+
}
|