@johnhenry/sitefile 0.0.7 → 0.0.9
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 +104 -1
- package/README.md +12 -1
- package/lib/src/attest.d.ts +12 -3
- package/lib/src/attest.js +15 -10
- package/lib/src/bake.js +2 -1
- package/lib/src/base-image.d.ts +2 -0
- package/lib/src/base-image.js +8 -0
- package/lib/src/bucket-follow.d.ts +22 -0
- package/lib/src/bucket-follow.js +65 -0
- package/lib/src/bucket.d.ts +270 -0
- package/lib/src/bucket.js +1130 -0
- package/lib/src/cache-limit.d.ts +3 -3
- package/lib/src/cache-limit.js +21 -0
- package/lib/src/cli/bucket.d.ts +2 -0
- package/lib/src/cli/bucket.js +312 -0
- package/lib/src/cli/build-image.js +4 -2
- package/lib/src/cli/images.js +8 -5
- package/lib/src/cli/index.d.ts +1 -1
- package/lib/src/cli/index.js +13 -2
- package/lib/src/cli/serve-worker.js +63 -11
- package/lib/src/cli/servers.js +117 -29
- package/lib/src/cli/up.js +2 -0
- package/lib/src/cli/watch.d.ts +2 -0
- package/lib/src/cli/watch.js +2 -1
- package/lib/src/cli.js +6 -3
- package/lib/src/dictionary.d.ts +1 -7
- package/lib/src/dictionary.js +1 -35
- package/lib/src/executor.d.ts +16 -5
- package/lib/src/executor.js +127 -58
- package/lib/src/index.d.ts +2 -0
- package/lib/src/index.js +2 -0
- package/lib/src/mime-type.d.ts +6 -0
- package/lib/src/mime-type.js +20 -0
- package/lib/src/nfc.d.ts +6 -0
- package/lib/src/nfc.js +10 -0
- package/lib/src/oci.d.ts +65 -1
- package/lib/src/oci.js +104 -10
- package/lib/src/paths.d.ts +2 -6
- package/lib/src/paths.js +2 -8
- package/lib/src/plugins/env.d.ts +20 -0
- package/lib/src/plugins/env.js +37 -0
- package/lib/src/plugins/exec.d.ts +1 -1
- package/lib/src/plugins/exec.js +3 -2
- package/lib/src/plugins/host.js +3 -2
- package/lib/src/precompress.d.ts +6 -0
- package/lib/src/precompress.js +6 -0
- package/lib/src/recipe.d.ts +37 -0
- package/lib/src/recipe.js +96 -0
- package/lib/src/registry-ops.d.ts +5 -1
- package/lib/src/registry-ops.js +69 -45
- package/lib/src/request-path.d.ts +10 -0
- package/lib/src/request-path.js +42 -0
- package/lib/src/serve-dictionary.d.ts +11 -0
- package/lib/src/serve-dictionary.js +45 -0
- package/lib/src/serve-rules.js +1 -1
- package/lib/src/server.d.ts +16 -229
- package/lib/src/server.js +135 -795
- package/lib/src/servers.d.ts +25 -0
- package/lib/src/servers.js +62 -2
- package/lib/src/site-core.d.ts +368 -0
- package/lib/src/site-core.js +1065 -0
- package/lib/src/site-handler.d.ts +14 -0
- package/lib/src/site-handler.js +32 -0
- package/lib/src/store.d.ts +31 -2
- package/lib/src/store.js +140 -21
- package/lib/src/wbn.d.ts +1 -6
- package/lib/src/wbn.js +2 -18
- package/package.json +5 -1
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { type Stats } from 'node:fs';
|
|
2
|
+
import { SitefileError } from './errors.js';
|
|
3
|
+
import type { Store } from './store.js';
|
|
4
|
+
import { Tree, type FileNode } from './tree.js';
|
|
5
|
+
/**
|
|
6
|
+
* Buckets: named, mutable file trees owned by the store (docs/CLI.md, "Buckets"). Twin: rust/src/bucket.rs.
|
|
7
|
+
*
|
|
8
|
+
* $SITEFILE_HOME/buckets/<name>/ the files, plain (what `serve bucket:<name>` serves)
|
|
9
|
+
* $SITEFILE_HOME/buckets/.meta/<name>.json {"created": "<iso>"}, beside the tree and never in it
|
|
10
|
+
* $SITEFILE_HOME/buckets/.tmp/ files being written (renamed into place when whole)
|
|
11
|
+
* $SITEFILE_HOME/buckets/.staging/ a `--batch` tree being built, swapped in with two renames
|
|
12
|
+
* $SITEFILE_HOME/buckets/.swap/ the journal and the displaced tree of a swap in progress (never swept)
|
|
13
|
+
*
|
|
14
|
+
* Every change holds the store lock. A file is written to `.tmp` and renamed into place, so a reader sees
|
|
15
|
+
* the old file or the new one, never half of it. Nothing here follows a symbolic link, and a file with
|
|
16
|
+
* more than one hard link is never read: a bucket directory another program has planted links in is
|
|
17
|
+
* refused, not read through.
|
|
18
|
+
*/
|
|
19
|
+
export declare const BUCKET_NAME_MAX = 63;
|
|
20
|
+
/** What `bucket put` accepts from stdin or a file unless `--max-bytes` says otherwise. */
|
|
21
|
+
export declare const DEFAULT_PUT_MAX_BYTES: number;
|
|
22
|
+
/** Validate a bucket name (`E_USAGE`). The grammar is lowercase on purpose: two names never differ only in case. */
|
|
23
|
+
export declare function checkBucketName(name: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* The segments of a path inside a bucket (`a/b/c.txt`), NFC. Refused: NUL, backslashes, `..` and `.`, a leading
|
|
26
|
+
* `/` (absolute), empty segments, names Windows cannot hold, and over-long names. A single trailing `/` is
|
|
27
|
+
* dropped (the caller decides whether a directory is fine).
|
|
28
|
+
*/
|
|
29
|
+
export declare function checkBucketPath(raw: string): string[];
|
|
30
|
+
/** `name/path/inside` → its name and the segments of what follows the first `/` (none for a bare name). */
|
|
31
|
+
export declare function splitBucketRef(arg: string): {
|
|
32
|
+
name: string;
|
|
33
|
+
segs: string[];
|
|
34
|
+
slash: boolean;
|
|
35
|
+
};
|
|
36
|
+
/** `bucket:<name>[/prefix]` as `serve` and the servers file write it, or undefined when `arg` is not one. */
|
|
37
|
+
export declare function parseBucketSource(arg: string): {
|
|
38
|
+
name: string;
|
|
39
|
+
prefix: string | undefined;
|
|
40
|
+
} | undefined;
|
|
41
|
+
export declare const bucketLabel: (name: string, prefix?: string) => string;
|
|
42
|
+
/** What the tests may hook and count (never set in production). */
|
|
43
|
+
export declare const bucketHooks: {
|
|
44
|
+
beforeRead?: (file: string) => void | Promise<void>;
|
|
45
|
+
/** Called at each point of a swap; a test throws {@link SwapCrash} to stop the protocol there as a crash would (nothing is cleaned up). */
|
|
46
|
+
crash?: (point: 'swap-journal' | 'swap-displaced' | 'swap-placed' | 'rm-cleaned' | 'rm-displaced') => void;
|
|
47
|
+
/** Called before every flush with the path; a test throws to make that flush fail. */
|
|
48
|
+
flush?: (path: string) => void;
|
|
49
|
+
/** Where warnings go (default: standard error). */
|
|
50
|
+
warn?: (message: string) => void;
|
|
51
|
+
};
|
|
52
|
+
/** Thrown by a test's `bucketHooks.crash`: the swap stops where it is and leaves everything as a crash would. */
|
|
53
|
+
export declare class SwapCrash extends Error {
|
|
54
|
+
}
|
|
55
|
+
export declare const bucketStats: {
|
|
56
|
+
fileReads: number;
|
|
57
|
+
};
|
|
58
|
+
/** The most bytes `serve`, `snapshot` and `restore` hold in memory for a bucket unless `--max-bytes` says otherwise. */
|
|
59
|
+
export declare const DEFAULT_LOAD_MAX_BYTES: number;
|
|
60
|
+
/** A file that vanished or changed between being listed and being read: the whole read is tried again. */
|
|
61
|
+
export declare class BucketRace extends SitefileError {
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The bytes and mode of a regular file with one link. It is opened without following a symbolic link and without
|
|
65
|
+
* blocking (a FIFO swapped in after `st` was taken must not hang the command, which may hold the store lock), and
|
|
66
|
+
* the open file must still be the one `st` described. On Windows there is no `O_NOFOLLOW`, no FIFO in a directory
|
|
67
|
+
* and no stable inode here: a reparse point swapped in is caught by the file-type and link-count checks only.
|
|
68
|
+
*/
|
|
69
|
+
export declare function readRegular(file: string, st: Stats, what: string): Promise<{
|
|
70
|
+
bytes: Uint8Array;
|
|
71
|
+
mode: number;
|
|
72
|
+
}>;
|
|
73
|
+
export interface BucketEntry {
|
|
74
|
+
readonly path: string;
|
|
75
|
+
readonly size: number;
|
|
76
|
+
readonly mode: number;
|
|
77
|
+
/** The file on disk and what was seen of it (a read checks it is still that file). */
|
|
78
|
+
readonly file: string;
|
|
79
|
+
readonly st: Stats;
|
|
80
|
+
/** Size, mtime, ctime and inode: what says a file is the one read before. */
|
|
81
|
+
readonly stamp: string;
|
|
82
|
+
}
|
|
83
|
+
/** Files already read, by path: a reload reads only the files whose stamp changed and shares the others. */
|
|
84
|
+
export type BucketReadCache = Map<string, {
|
|
85
|
+
stamp: string;
|
|
86
|
+
node: FileNode;
|
|
87
|
+
}>;
|
|
88
|
+
/**
|
|
89
|
+
* Every file under `dir` (a real directory) with `lstat` only, as bucket-relative paths: nothing is read, so the cost is
|
|
90
|
+
* proportional to the number of entries. A file or directory that vanishes during the walk is skipped. Symbolic links
|
|
91
|
+
* and hard-linked files are errors that say which link to remove, special files are `E_UNSUPPORTED_FILE`, and two names
|
|
92
|
+
* that differ only in Unicode normalization are `E_PATH_COLLISION`. `o.dirs` receives every directory, empty ones too.
|
|
93
|
+
*/
|
|
94
|
+
export declare function scanDirectory(dir: string, name: string, relBase: readonly string[], o?: {
|
|
95
|
+
dirs?: string[];
|
|
96
|
+
}): Promise<BucketEntry[]>;
|
|
97
|
+
export interface BucketSummary {
|
|
98
|
+
readonly name: string;
|
|
99
|
+
readonly created: string;
|
|
100
|
+
readonly files: number;
|
|
101
|
+
readonly size: number;
|
|
102
|
+
/** Set when the bucket cannot be read (a link planted in it): the sentence that says what to do. */
|
|
103
|
+
readonly problem?: string;
|
|
104
|
+
}
|
|
105
|
+
export type PutResult = 'created' | 'replaced' | 'unchanged';
|
|
106
|
+
/** What a bulk change does to a bucket (`put` of a directory, `restore`). */
|
|
107
|
+
export interface BucketChange {
|
|
108
|
+
readonly writes: ReadonlyArray<readonly [string, FileNode]>;
|
|
109
|
+
/** Remove the files that are not among `writes`, after the writes (`restore --delete`). */
|
|
110
|
+
readonly deleteOthers: boolean;
|
|
111
|
+
}
|
|
112
|
+
export interface BucketChangeResult {
|
|
113
|
+
readonly written: number;
|
|
114
|
+
readonly unchanged: number;
|
|
115
|
+
readonly removed: number;
|
|
116
|
+
}
|
|
117
|
+
export declare class Buckets {
|
|
118
|
+
readonly store: Store;
|
|
119
|
+
readonly root: string;
|
|
120
|
+
private constructor();
|
|
121
|
+
static open(store: Store): Buckets;
|
|
122
|
+
private dirOf;
|
|
123
|
+
private metaFile;
|
|
124
|
+
private get tmpDir();
|
|
125
|
+
private get stagingDir();
|
|
126
|
+
/** `.swap/<name>.json` is the journal of a `--batch` swap, `.swap/<name>.old` the displaced tree: the sweep never touches either. */
|
|
127
|
+
private get swapDir();
|
|
128
|
+
private journalOf;
|
|
129
|
+
private oldOf;
|
|
130
|
+
private scratch;
|
|
131
|
+
/**
|
|
132
|
+
* A `--batch` swap is: build the new tree in `.staging/<bucket>.<token>`, journal `.swap/<name>.json` = `{"token": "<hex>"}`
|
|
133
|
+
* (data only: every path is derived from the bucket name and a token matching /^[a-f0-9]{16,64}$/, never read from
|
|
134
|
+
* the journal), rename the bucket to `.swap/<name>.old`, rename the staged tree to the bucket, remove the journal,
|
|
135
|
+
* remove `.old`. Durability, in this order: every staged file and directory is fsynced; the journal is written to a
|
|
136
|
+
* temporary file, fsynced, renamed, and `.swap` is fsynced, all BEFORE the first bucket rename (else power loss could
|
|
137
|
+
* keep that rename and lose the journal); after each rename that matters the directories it changed are fsynced (the
|
|
138
|
+
* bucket's parent and `.swap` after the first, the parent and `.staging` after the second, `.swap` after the
|
|
139
|
+
* journal's removal). Windows cannot fsync a directory: files are flushed (FlushFileBuffers) and NTFS journals its
|
|
140
|
+
* own metadata, but the order of two renames across a power loss is not guaranteed there; recovery below does not
|
|
141
|
+
* depend on the journal for that reason.
|
|
142
|
+
*
|
|
143
|
+
* Recovery runs under the lock before anything else touches the bucket, and decides from the directory state
|
|
144
|
+
* (J journal, D bucket directory, O `.swap/<name>.old`, S the staged tree `.staging/<name>.<token>`):
|
|
145
|
+
*
|
|
146
|
+
* J D O S action
|
|
147
|
+
* - - - . nothing (no such bucket)
|
|
148
|
+
* - y - . nothing
|
|
149
|
+
* - y y . keep O: a leftover of a finished swap; only the next successful swap of this bucket, or
|
|
150
|
+
* `bucket rm`, removes it, never create or a sweep
|
|
151
|
+
* - - y . put O back as D (power loss kept the first rename and lost the journal)
|
|
152
|
+
* y y - . the swap never began or was rolled back: remove S and J
|
|
153
|
+
* y y y . the swap finished: remove O, S and J
|
|
154
|
+
* y - y . the new tree was never placed: put O back as D, remove S and J (rolled back, not finished)
|
|
155
|
+
* y - - y O is gone and D is not there: the complete staged tree becomes D, J is removed
|
|
156
|
+
* y - - - nothing left to restore: remove J
|
|
157
|
+
*
|
|
158
|
+
* `bucket rm` runs the other way round: it recovers, then removes O, J and every S of the bucket and fsyncs `.swap`
|
|
159
|
+
* and `.staging`, failing loudly if it cannot, and only then renames D away, so a crash inside it leaves D (or
|
|
160
|
+
* nothing), never an O that the next command would put back: a removed bucket cannot come back with old contents.
|
|
161
|
+
* No removal of an O, J or S is ever ignored.
|
|
162
|
+
*
|
|
163
|
+
* A journal that is unreadable, empty, garbage or not UTF-8 names no token: the state is decided from D, O and J's
|
|
164
|
+
* existence alone. Running recovery again changes nothing.
|
|
165
|
+
*/
|
|
166
|
+
private needsRecovery;
|
|
167
|
+
/** Recover for a writer: under the lock, before it works on the bucket. */
|
|
168
|
+
recover(name: string): Promise<void>;
|
|
169
|
+
/**
|
|
170
|
+
* Recover for a reader: an intact bucket (its directory is there) is served as it is, leftovers and all, so a read
|
|
171
|
+
* never writes (the store may be read-only). Only a bucket whose directory is missing is recovered, which needs
|
|
172
|
+
* the lock (it also waits for a swap in progress); if that is impossible the read fails and says why.
|
|
173
|
+
*/
|
|
174
|
+
private recoverForRead;
|
|
175
|
+
/** {@link recover} for a caller that holds the lock. */
|
|
176
|
+
recoverLocked(name: string): Promise<void>;
|
|
177
|
+
/** A swap of this bucket has begun and not ended (a reader that finds the bucket missing waits for it). */
|
|
178
|
+
private swapPending;
|
|
179
|
+
/**
|
|
180
|
+
* Run a read of the bucket. A `--batch` swap in progress (or interrupted) is waited for or recovered first, and a
|
|
181
|
+
* read that is cut by a swap (the bucket's directory is another one now, or missing during the swap) is repeated,
|
|
182
|
+
* bounded: a reader sees the old tree or the new one, whatever kind of read it is.
|
|
183
|
+
*/
|
|
184
|
+
private reading;
|
|
185
|
+
/** What is at the bucket's name: a directory, a link (planted: `rm` may remove it), or nothing. */
|
|
186
|
+
kindOf(name: string): Promise<'dir' | 'link' | undefined>;
|
|
187
|
+
/** The bucket's directory when it exists as a real directory (never a symbolic link), else `E_NOT_FOUND`. */
|
|
188
|
+
realDir(name: string): Promise<string>;
|
|
189
|
+
/**
|
|
190
|
+
* The directory at `segs` below the bucket, each component checked with `lstat`: a symbolic link anywhere on
|
|
191
|
+
* the way is refused. A component is found by its NFC spelling, as the readers do.
|
|
192
|
+
*/
|
|
193
|
+
resolveDirectory(name: string, segs: readonly string[]): Promise<string>;
|
|
194
|
+
private resolveDir;
|
|
195
|
+
create(name: string): Promise<void>;
|
|
196
|
+
/**
|
|
197
|
+
* Delete the scratch files an interrupted write left (older than an hour). The caller holds the lock, and has
|
|
198
|
+
* recovered the bucket it works on: only `.tmp` and `.staging` are swept, never `.swap`, where a swap in progress
|
|
199
|
+
* keeps the tree it displaced.
|
|
200
|
+
*/
|
|
201
|
+
private sweepScratch;
|
|
202
|
+
/** The token a journal names, if it names one: data, never a path. */
|
|
203
|
+
private journalToken;
|
|
204
|
+
createdOf(name: string): Promise<string>;
|
|
205
|
+
stats(name: string): Promise<{
|
|
206
|
+
files: number;
|
|
207
|
+
size: number;
|
|
208
|
+
}>;
|
|
209
|
+
/** Every bucket; one that cannot be read (a link planted in it) carries a `problem` and the others are listed all the same. */
|
|
210
|
+
list(): Promise<BucketSummary[]>;
|
|
211
|
+
/** Remove a bucket. The caller has already refused what should be refused (files, running servers). */
|
|
212
|
+
remove(name: string): Promise<void>;
|
|
213
|
+
/** The files under `prefix` (a directory, or a single file), without their contents. */
|
|
214
|
+
listFiles(name: string, prefix: readonly string[]): Promise<BucketEntry[]>;
|
|
215
|
+
/** One file's bytes, with the sandbox rules of every read. */
|
|
216
|
+
get(name: string, segs: readonly string[]): Promise<{
|
|
217
|
+
bytes: Uint8Array;
|
|
218
|
+
mode: number;
|
|
219
|
+
}>;
|
|
220
|
+
/**
|
|
221
|
+
* The tree of a bucket (or of a directory `prefix` of it, re-rooted): every file, hidden ones too, read through
|
|
222
|
+
* the sandbox. The origin of its files is `bucket:<name>`.
|
|
223
|
+
*
|
|
224
|
+
* Listing is by `lstat`; only files whose stamp (size, mtime, ctime, inode) differs from `o.cache` are read, and the
|
|
225
|
+
* others are the very nodes of the last load, so a reload holds the changed files once, not the bucket twice. The
|
|
226
|
+
* cache is replaced by the files of this load. More than `o.maxBytes` (default {@link DEFAULT_LOAD_MAX_BYTES}) is
|
|
227
|
+
* `E_TOO_LARGE` before any byte is read. `o.locked`: the caller holds the store lock (no recovery is attempted).
|
|
228
|
+
*/
|
|
229
|
+
loadTree(name: string, prefix?: readonly string[], o?: {
|
|
230
|
+
cache?: BucketReadCache;
|
|
231
|
+
maxBytes?: number;
|
|
232
|
+
locked?: boolean;
|
|
233
|
+
}): Promise<Tree>;
|
|
234
|
+
private loadOnce;
|
|
235
|
+
/**
|
|
236
|
+
* Put one file. `source` yields its bytes (stdin, a file); it is staged in `.tmp` first (so a failed or
|
|
237
|
+
* oversized transfer changes nothing) and renamed into place under the lock.
|
|
238
|
+
*/
|
|
239
|
+
putFile(name: string, segs: readonly string[], source: AsyncIterable<Uint8Array>, mode: number, maxBytes: number): Promise<{
|
|
240
|
+
result: PutResult;
|
|
241
|
+
size: number;
|
|
242
|
+
}>;
|
|
243
|
+
/** The directory that will hold `segs`' last segment, created as needed, every step checked. The caller holds the lock. */
|
|
244
|
+
private prepareParent;
|
|
245
|
+
/** The on-disk name of `seg` among `entries` (the same NFC name), `undefined` when absent; a name that differs only in case is `E_PATH_COLLISION`. */
|
|
246
|
+
private matchEntry;
|
|
247
|
+
/** Move the staged file `tmp` to `segs`: the one rename that makes it visible. The caller holds the lock. */
|
|
248
|
+
private place;
|
|
249
|
+
/**
|
|
250
|
+
* Apply a bulk change. By default each file is written atomically on its own; with `batch` the whole
|
|
251
|
+
* resulting tree is built beside the bucket and swapped in (see {@link recover}), so a reader sees all of the
|
|
252
|
+
* change or none of it. **Nothing is written** when a conflict is found, and every conflict is found first: a file
|
|
253
|
+
* that would replace different content without `overwrite`, a file where a directory is (an empty one too), a name
|
|
254
|
+
* that differs only in case from a file or an empty directory, a file where a directory has to be. Existing files
|
|
255
|
+
* are listed by `lstat` and read only when a write names the same path, so the bucket is not held in memory.
|
|
256
|
+
*/
|
|
257
|
+
apply(name: string, change: BucketChange, o: {
|
|
258
|
+
overwrite: boolean;
|
|
259
|
+
batch: boolean;
|
|
260
|
+
}): Promise<BucketChangeResult>;
|
|
261
|
+
/**
|
|
262
|
+
* Build the new tree in `.staging` (unchanged files are copied from the bucket, changed ones written) and swap it
|
|
263
|
+
* for the bucket directory: journal, displace, place, unjournal. The caller holds the lock. Empty directories are
|
|
264
|
+
* not carried over (a tree has none).
|
|
265
|
+
*/
|
|
266
|
+
private swapIn;
|
|
267
|
+
/** Remove a file, or (with `recursive`) a directory and everything in it; empty parents go too. */
|
|
268
|
+
removePath(name: string, segs: readonly string[], recursive: boolean): Promise<void>;
|
|
269
|
+
private removeLocked;
|
|
270
|
+
}
|