functionalscript 0.33.0 → 0.34.0
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 +1 -1
- package/fs/asserts/module.f.d.ts +1 -1
- package/fs/asserts/module.f.js +4 -1
- package/fs/bnf/data/module.f.d.ts +0 -103
- package/fs/bnf/data/module.f.js +10 -246
- package/fs/bnf/data/proof.f.d.ts +0 -10
- package/fs/bnf/data/proof.f.js +2 -642
- package/fs/bnf/descent/module.f.d.ts +56 -0
- package/fs/bnf/descent/module.f.js +119 -0
- package/fs/bnf/descent/proof.f.d.ts +5 -0
- package/fs/bnf/descent/proof.f.js +296 -0
- package/fs/bnf/ll1/module.f.d.ts +72 -0
- package/fs/bnf/ll1/module.f.js +153 -0
- package/fs/bnf/ll1/proof.f.d.ts +9 -0
- package/fs/bnf/ll1/proof.f.js +358 -0
- package/fs/cas/cli/module.f.d.ts +6 -0
- package/fs/cas/cli/module.f.js +56 -0
- package/fs/cas/cli/proof.f.d.ts +14 -0
- package/fs/cas/cli/proof.f.js +149 -0
- package/fs/cas/mcp/module.f.d.ts +9 -17
- package/fs/cas/mcp/module.f.js +116 -84
- package/fs/cas/mcp/proof.f.d.ts +5 -1
- package/fs/cas/mcp/proof.f.js +74 -32
- package/fs/cas/module.f.d.ts +35 -15
- package/fs/cas/module.f.js +168 -131
- package/fs/cas/proof.f.d.ts +16 -13
- package/fs/cas/proof.f.js +255 -121
- package/fs/ci/config/module.f.d.ts +14 -14
- package/fs/ci/config/module.f.js +14 -14
- package/fs/djs/tokenizer-new/module.f.js +1 -1
- package/fs/djs/tokenizer-new/proof.f.d.ts +2 -0
- package/fs/djs/tokenizer-new/proof.f.js +357 -229
- package/fs/effects/list/module.f.d.ts +31 -0
- package/fs/effects/list/module.f.js +16 -0
- package/fs/effects/module.f.d.ts +11 -5
- package/fs/effects/module.f.js +16 -6
- package/fs/effects/node/module.f.d.ts +29 -3
- package/fs/effects/node/module.f.js +35 -1
- package/fs/effects/node/module.js +32 -18
- package/fs/effects/node/virtual/module.f.js +57 -0
- package/fs/effects/proof.f.d.ts +4 -0
- package/fs/effects/proof.f.js +18 -1
- package/fs/fjs/module.f.js +2 -8
- package/fs/html/module.f.js +6 -8
- package/fs/mcp/module.f.d.ts +1 -1
- package/fs/mime/module.f.d.ts +101 -5
- package/fs/mime/module.f.js +159 -6
- package/fs/mime/proof.f.d.ts +25 -0
- package/fs/mime/proof.f.js +193 -3
- package/fs/text/code_point/module.f.d.ts +39 -2
- package/fs/text/code_point/module.f.js +74 -2
- package/fs/text/code_point/proof.f.d.ts +8 -0
- package/fs/text/code_point/proof.f.js +72 -0
- package/fs/text/utf16/module.f.js +4 -26
- package/fs/text/utf8/module.f.d.ts +27 -0
- package/fs/text/utf8/module.f.js +8 -14
- package/fs/types/function/operator/module.f.d.ts +26 -0
- package/fs/types/list/module.f.d.ts +3 -0
- package/fs/types/result/module.d.ts +2 -4
- package/fs/types/result/module.js +9 -3
- package/package.json +3 -3
- package/issues/demo/data/data.f.js +0 -12
- package/issues/demo/data/shared.f.js +0 -3
- package/issues/demo/fs/app.js +0 -4
- package/issues/demo/fs/math.f.js +0 -4
- package/issues/demo/sample/proof.f.js +0 -13
- /package/{issues → todo}/031-json.f.d.ts +0 -0
- /package/{issues → todo}/031-json.f.js +0 -0
- /package/{issues → todo}/proof.f.d.ts +0 -0
- /package/{issues → todo}/proof.f.js +0 -0
package/fs/cas/mcp/proof.f.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { assert, assertEq } from "../../asserts/module.f.js";
|
|
2
2
|
import { pure } from "../../effects/module.f.js";
|
|
3
3
|
import { run } from "../../effects/mock/module.f.js";
|
|
4
|
-
import { asBase, asNominal, create
|
|
5
|
-
import { msb, u8ListToVec, vec8 } from "../../types/bit_vec/module.f.js";
|
|
4
|
+
import { asBase, asNominal, create } from "../../effects/memory/module.f.js";
|
|
5
|
+
import { msb, u8ListToVec, vec8, repeat, length, maxLengthBytes } from "../../types/bit_vec/module.f.js";
|
|
6
6
|
import { vecToCBase32 } from "../../cbase32/module.f.js";
|
|
7
7
|
import { encode as base64Encode } from "../../base64/module.f.js";
|
|
8
|
-
import { computeSync, sha256 } from "../../crypto/sha2/module.f.js";
|
|
9
8
|
import { utf8 } from "../../text/module.f.js";
|
|
10
|
-
import {
|
|
9
|
+
import {} from "../module.f.js";
|
|
11
10
|
import { mcpStep, uninitializedState, } from "../../mcp/module.f.js";
|
|
12
|
-
import {} from "../../effects/node/module.f.js";
|
|
13
11
|
import { emptyState, virtual } from "../../effects/node/virtual/module.f.js";
|
|
14
12
|
import { casConfig, casMcpHandlers } from "./module.f.js";
|
|
15
13
|
import { ok as resultOk } from "../../types/result/module.f.js";
|
|
@@ -32,18 +30,15 @@ const mock = {
|
|
|
32
30
|
rename: (_src, _dst) => _ => { throw new Error('rename not supported in memory mock'); },
|
|
33
31
|
readBytes: (_path, _offset, _size) => _ => { throw new Error('readBytes not supported in memory mock'); },
|
|
34
32
|
randomInt: () => _ => { throw new Error('randomInt not supported in memory mock'); },
|
|
33
|
+
now: () => state => [state, 0],
|
|
34
|
+
access: (_path) => _ => { throw new Error('access not supported in memory mock'); },
|
|
35
|
+
createExclusive: (_path) => _ => { throw new Error('createExclusive not supported in memory mock'); },
|
|
36
|
+
readdir: (_path, _opts) => _ => { throw new Error('readdir not supported in memory mock'); },
|
|
37
|
+
rm: (_path) => _ => { throw new Error('rm not supported in memory mock'); },
|
|
38
|
+
stat: (_path) => _ => { throw new Error('stat not supported in memory mock'); },
|
|
39
|
+
writeBytes: (_path, _offset, _data) => _ => { throw new Error('writeBytes not supported in memory mock'); },
|
|
35
40
|
};
|
|
36
41
|
const runMem = (effect) => run(mock)(initialTestState)(effect)[1];
|
|
37
|
-
const memCas = (mapKey) => ({
|
|
38
|
-
read: (key) => read(mapKey).step(m => pure(m[vecToCBase32(key)]?.[1])),
|
|
39
|
-
write: (value) => {
|
|
40
|
-
const key = computeSync(sha256)([value]);
|
|
41
|
-
return read(mapKey)
|
|
42
|
-
.step(m => write(mapKey, { ...m, [vecToCBase32(key)]: [key, value] }))
|
|
43
|
-
.step(() => pure(key));
|
|
44
|
-
},
|
|
45
|
-
list: () => read(mapKey).step(m => pure(Object.values(m).map(([k]) => k))),
|
|
46
|
-
});
|
|
47
42
|
// ── Session driver ──────────────────────────────────────────────────────────────
|
|
48
43
|
// Feeds each message to `step` in order, collecting every response.
|
|
49
44
|
const feed = (step) => (msgs) => {
|
|
@@ -52,19 +47,12 @@ const feed = (step) => (msgs) => {
|
|
|
52
47
|
: step(msgs[i]).step(r => go(i + 1, [...acc, r]));
|
|
53
48
|
return go(0, []);
|
|
54
49
|
};
|
|
55
|
-
// Runs a full session over a fresh in-memory CAS, returning all responses.
|
|
56
|
-
const runSession = (msgs, home = '/home/user') => runMem(create({}).step(mapKey => create(uninitializedState).step(sessionKey => {
|
|
57
|
-
const c = memCas(mapKey);
|
|
58
|
-
const step = mcpStep(casConfig)(casMcpHandlers(c, home))(sessionKey);
|
|
59
|
-
return feed(step)(msgs);
|
|
60
|
-
})));
|
|
61
50
|
// Runs a session backed by the virtual node runner (for cas_upload which uses
|
|
62
51
|
// Rename/ReadBytes/RandomInt/Mkdir). Uses fileKvStore so upload and get share
|
|
63
52
|
// the same filesystem-backed CAS.
|
|
64
53
|
const runSessionVirtual = (root, home = '/home/user') => (msgs) => {
|
|
65
54
|
const effect = create(uninitializedState).step(sessionKey => {
|
|
66
|
-
const
|
|
67
|
-
const step = mcpStep(casConfig)(casMcpHandlers(c, home))(sessionKey);
|
|
55
|
+
const step = mcpStep(casConfig)(casMcpHandlers(home))(sessionKey);
|
|
68
56
|
return feed(step)(msgs);
|
|
69
57
|
});
|
|
70
58
|
return virtual({ ...emptyState, root })(effect)[1];
|
|
@@ -76,7 +64,7 @@ const initialized = { jsonrpc: '2.0', method: 'notifications/initialized' };
|
|
|
76
64
|
const call = (id, name, args) => ({ jsonrpc: '2.0', method: 'tools/call', id, params: { name, arguments: args } });
|
|
77
65
|
const list = (id) => ({ jsonrpc: '2.0', method: 'tools/list', id });
|
|
78
66
|
// Runs `init`, `notifications/initialized`, then `msgs`; returns the tool responses.
|
|
79
|
-
const session = (...msgs) =>
|
|
67
|
+
const session = (...msgs) => runSessionVirtual({}, '/home/user')([init, initialized, ...msgs]).slice(2);
|
|
80
68
|
const resultOf = (resp) => resp.result;
|
|
81
69
|
const item0 = (resp) => resultOf(resp).content[0];
|
|
82
70
|
const textOf = (resp) => item0(resp).text;
|
|
@@ -88,9 +76,47 @@ const binarySample = base64Encode(vec8(0x2an));
|
|
|
88
76
|
// `cas_get` detects its type and returns base64 with mime_type image/png.
|
|
89
77
|
const pngSample = base64Encode(u8ListToVec(msb)([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x01]));
|
|
90
78
|
// ── Tests ───────────────────────────────────────────────────────────────────────
|
|
79
|
+
// A blob larger than one read chunk (> 128 KiB) used to fail metadata-only
|
|
80
|
+
// cas_get because `collectRead` overflowed `maxLength`. With `detectStream`,
|
|
81
|
+
// content:false returns correct metadata regardless of size. Each chunk is
|
|
82
|
+
// exactly `maxLengthBytes` (the largest single `Vec` the runtime allows), so the
|
|
83
|
+
// two-chunk blob spans two read chunks without any one `Vec` exceeding the cap.
|
|
84
|
+
const largeMultiChunkBlobMeta = (chunk0, chunk1, expectedType, expectedMime) => () => {
|
|
85
|
+
const root = { 'home': { 'user': { 'cas_upload': { 'big': [chunk0, chunk1] } } } };
|
|
86
|
+
const [addResp] = runSessionVirtual(root)([
|
|
87
|
+
init, initialized,
|
|
88
|
+
call(2, 'cas_add', { content: '/home/user/cas_upload/big', type: 'url' }),
|
|
89
|
+
]).slice(2);
|
|
90
|
+
assert(!resultOf(addResp).isError);
|
|
91
|
+
const hash = textOf(addResp);
|
|
92
|
+
const [, metaResp] = runSessionVirtual(root)([
|
|
93
|
+
init, initialized,
|
|
94
|
+
call(2, 'cas_add', { content: '/home/user/cas_upload/big', type: 'url' }),
|
|
95
|
+
call(3, 'cas_get', { hash }),
|
|
96
|
+
]).slice(2);
|
|
97
|
+
assert(!resultOf(metaResp).isError);
|
|
98
|
+
const meta = JSON.parse(textOf(metaResp));
|
|
99
|
+
assertEq(meta.type, expectedType);
|
|
100
|
+
assertEq(meta.mime_type, expectedMime);
|
|
101
|
+
assertEq(meta.length, Number((length(chunk0) + length(chunk1)) / 8n));
|
|
102
|
+
assertEq(meta.content, undefined);
|
|
103
|
+
};
|
|
104
|
+
// A full `maxLengthBytes`-long chunk of repeated ASCII 'a' — valid UTF-8.
|
|
105
|
+
const asciiChunk = repeat(maxLengthBytes)(vec8(0x61n));
|
|
106
|
+
// The same length, but repeated 2-byte "é" (C3 A9) — valid UTF-8 of multi-byte symbols.
|
|
107
|
+
const symbolChunk = repeat(maxLengthBytes / 2n)(u8ListToVec(msb)([0xc3, 0xa9]));
|
|
108
|
+
// A full chunk of 0xFF — an invalid UTF-8 lead byte, so binary (no magic match).
|
|
109
|
+
const binaryChunk = repeat(maxLengthBytes)(vec8(0xffn));
|
|
91
110
|
export const proof = {
|
|
111
|
+
// Both chunks repeated ASCII → text/plain, no error on a > 128 KiB blob.
|
|
112
|
+
getMetaLargeMultiChunkBlobNoError: largeMultiChunkBlobMeta(asciiChunk, asciiChunk, 'text', 'text/plain'),
|
|
113
|
+
// Both chunks valid UTF-8 sequences of multi-byte symbols → text/plain.
|
|
114
|
+
getMetaLargeMultiChunkUtf8Symbols: largeMultiChunkBlobMeta(symbolChunk, symbolChunk, 'text', 'text/plain'),
|
|
115
|
+
// First chunk valid UTF-8, second chunk binary → base64/octet-stream. The
|
|
116
|
+
// streaming validator must see the trailing binary chunk to reject text.
|
|
117
|
+
getMetaLargeMultiChunkTextThenBinary: largeMultiChunkBlobMeta(asciiChunk, binaryChunk, 'base64', 'application/octet-stream'),
|
|
92
118
|
toolsListAdvertisesThreeTools: () => {
|
|
93
|
-
const [resp] =
|
|
119
|
+
const [resp] = runSessionVirtual({})([init, initialized, list(2)]).slice(2);
|
|
94
120
|
const tools = resp.result.tools;
|
|
95
121
|
assertEq(tools.length, 3);
|
|
96
122
|
assertEq(tools.map(t => t.name).join(','), 'cas_add,cas_get,cas_list');
|
|
@@ -278,7 +304,7 @@ export const proof = {
|
|
|
278
304
|
const meta = JSON.parse(textOf(metaResp));
|
|
279
305
|
assertEq(meta.mime_type, 'text/plain');
|
|
280
306
|
assertEq(meta.type, 'text');
|
|
281
|
-
assertEq(meta.length,
|
|
307
|
+
assertEq(meta.length, 12);
|
|
282
308
|
assertEq(meta.content, undefined);
|
|
283
309
|
},
|
|
284
310
|
getMetaBinaryBlob: () => {
|
|
@@ -304,15 +330,18 @@ export const proof = {
|
|
|
304
330
|
assertEq(meta.type, 'base64');
|
|
305
331
|
assertEq(meta.content, undefined);
|
|
306
332
|
},
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
const
|
|
333
|
+
// A NUL-bearing blob is valid UTF-8 yet binary: cas_get must report
|
|
334
|
+
// base64/octet-stream, not text/plain.
|
|
335
|
+
getMetaOctetStreamForNulBlob: () => {
|
|
336
|
+
const nulContent = u8ListToVec(msb)([0x00, 0x00, 0x00]);
|
|
337
|
+
const nulB64 = base64Encode(nulContent);
|
|
338
|
+
const [addResp] = session(call(2, 'cas_add', { content: nulB64, type: 'base64' }));
|
|
311
339
|
const hash = textOf(addResp);
|
|
312
|
-
const [, metaResp] = session(call(2, 'cas_add', { content:
|
|
340
|
+
const [, metaResp] = session(call(2, 'cas_add', { content: nulB64, type: 'base64' }), call(3, 'cas_get', { hash }));
|
|
313
341
|
assert(!resultOf(metaResp).isError);
|
|
314
342
|
const meta = JSON.parse(textOf(metaResp));
|
|
315
|
-
assertEq(meta.
|
|
343
|
+
assertEq(meta.mime_type, 'application/octet-stream');
|
|
344
|
+
assertEq(meta.type, 'base64');
|
|
316
345
|
},
|
|
317
346
|
getMetaMissingHashIsError: () => {
|
|
318
347
|
const absent = vecToCBase32(vec8(0x77n));
|
|
@@ -357,4 +386,17 @@ export const proof = {
|
|
|
357
386
|
assert(resultOf(resp).isError === true);
|
|
358
387
|
assert(textOf(resp).includes('/home/user/cas_upload/'));
|
|
359
388
|
},
|
|
389
|
+
// cas_get with content:true on octet-stream (no magic bytes, not UTF-8) returns inline base64.
|
|
390
|
+
getOctetStreamWithContentIncludesBase64: () => {
|
|
391
|
+
const binaryContent = u8ListToVec(msb)([0xFF, 0xFE, 0x00, 0x01]);
|
|
392
|
+
const binaryB64 = base64Encode(binaryContent);
|
|
393
|
+
const [addResp] = session(call(2, 'cas_add', { content: binaryB64, type: 'base64' }));
|
|
394
|
+
const hash = textOf(addResp);
|
|
395
|
+
const [, getResp] = session(call(2, 'cas_add', { content: binaryB64, type: 'base64' }), call(3, 'cas_get', { hash, content: true }));
|
|
396
|
+
assert(!resultOf(getResp).isError);
|
|
397
|
+
const result = JSON.parse(textOf(getResp));
|
|
398
|
+
assertEq(result.mime_type, 'application/octet-stream');
|
|
399
|
+
assertEq(result.type, 'base64');
|
|
400
|
+
assertEq(result.content, binaryB64);
|
|
401
|
+
},
|
|
360
402
|
};
|
package/fs/cas/module.f.d.ts
CHANGED
|
@@ -6,29 +6,49 @@
|
|
|
6
6
|
import { type Sha2 } from '../crypto/sha2/module.f.ts';
|
|
7
7
|
import { type Vec } from '../types/bit_vec/module.f.ts';
|
|
8
8
|
import { type Effect, type Operation } from '../effects/module.f.ts';
|
|
9
|
-
import { type Access, type
|
|
10
|
-
import { type
|
|
11
|
-
import type { MemOp } from '../effects/memory/module.f.ts';
|
|
9
|
+
import { type Access, type CreateExclusive, type IoResult, type Mkdir, type Now, type RandomInt, type ReadBytes, type Readdir, type Rename, type Rm, type Stat, type WriteBytes } from '../effects/node/module.f.ts';
|
|
10
|
+
import { type List } from '../effects/list/module.f.ts';
|
|
12
11
|
/** Converts a content key to its sharded relative CAS file path. */
|
|
13
12
|
export declare const toPath: (key: Vec) => string;
|
|
14
|
-
|
|
13
|
+
/**
|
|
14
|
+
* The filesystem effects the streaming CAS performs: `read` pulls shards
|
|
15
|
+
* (`ReadBytes`); `list` walks the store (`Access`/`Readdir`); `write` runs the
|
|
16
|
+
* lock-free staging upload (`Mkdir`/`CreateExclusive`/`WriteBytes`/`Rename`/`Rm`/
|
|
17
|
+
* `Stat`, lease deadlines from `Now`, staging names from `RandomInt`) and GC's
|
|
18
|
+
* expired staging files (`Readdir`/`Rm`).
|
|
19
|
+
*/
|
|
20
|
+
export type FileCasOperation = ReadBytes | Mkdir | Readdir | Access | Rename | Rm | RandomInt | Now | CreateExclusive | WriteBytes | Stat | Now | Readdir | Rm;
|
|
15
21
|
export type Cas<O extends Operation> = {
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Streams the content for `hash` out in `<=128 KiB` chunks. Every pull yields an
|
|
24
|
+
* explicit `ok(chunk)` or `error` item, so a missing shard or I/O error is a distinct
|
|
25
|
+
* error *item* in the stream, never collapsed into end-of-stream (`undefined`).
|
|
26
|
+
*/
|
|
27
|
+
readonly read: (hash: Vec) => List<O, IoResult<Vec>>;
|
|
28
|
+
/**
|
|
29
|
+
* Consumes a chunk stream — each item `ok(chunk)` or `error` — hashing incrementally,
|
|
30
|
+
* and returns the content address. An error item aborts the upload.
|
|
31
|
+
*/
|
|
32
|
+
readonly write: <O1 extends Operation>(payload: List<O1, IoResult<Vec>>) => Effect<O | O1, IoResult<Vec>>;
|
|
20
33
|
/** Lists all stored content hashes. */
|
|
21
34
|
readonly list: () => Effect<O, readonly Vec[]>;
|
|
22
35
|
};
|
|
36
|
+
export type FileCas = Cas<FileCasOperation> & {
|
|
37
|
+
url: (v: Vec) => string;
|
|
38
|
+
};
|
|
23
39
|
/**
|
|
24
40
|
* Builds a content-addressable storage facade from a SHA-2 implementation.
|
|
25
41
|
*/
|
|
26
|
-
export declare const fileCas: (sha2: Sha2) => (path: string) =>
|
|
42
|
+
export declare const fileCas: (sha2: Sha2) => (path: string) => FileCas;
|
|
43
|
+
/**
|
|
44
|
+
* Streams the file at `path` through `cas.write`, returning the content hash.
|
|
45
|
+
* Both the CLI `cas add` and the MCP `add` delegate to this; the MCP layer
|
|
46
|
+
* additionally deletes the source file on success.
|
|
47
|
+
*/
|
|
48
|
+
export declare const casAddFile: <O extends Operation>(cas: Cas<O>) => (path: string) => Effect<O | ReadBytes, IoResult<Vec>>;
|
|
27
49
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
50
|
+
* Upload pipeline: streams `fileName` from `~/cas_upload/` through `casAddFile`,
|
|
51
|
+
* then deletes the source on success. On failure the source is left in place so
|
|
52
|
+
* the upload can be retried; `write` already cleans up its own partial staging file.
|
|
31
53
|
*/
|
|
32
|
-
export declare const casUpload: (home: string) => (fileName: string) => Effect<
|
|
33
|
-
export declare const commands: Commands<FileCasOperation | Write | All | MemOp | Read>;
|
|
34
|
-
export declare const main: (options: NodeProgramOptions) => Effect<All | Read | Write | FileCasOperation | MemOp, number>;
|
|
54
|
+
export declare const casUpload: (home: string) => (fileName: string) => Effect<FileCasOperation, IoResult<Vec>>;
|
package/fs/cas/module.f.js
CHANGED
|
@@ -3,16 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { sha256 } from "../crypto/sha2/module.f.js";
|
|
7
7
|
import { join, normalize, parse } from "../path/module.f.js";
|
|
8
8
|
import { empty, length, maxLengthBytes, msb, vec } from "../types/bit_vec/module.f.js";
|
|
9
9
|
import { cBase32ToVec, vecToCBase32 } from "../cbase32/module.f.js";
|
|
10
10
|
import { foldStep, forEachStep, pure } from "../effects/module.f.js";
|
|
11
|
-
import { access,
|
|
12
|
-
import { dispatch } from "../cli/module.f.js";
|
|
11
|
+
import { access, createExclusive, isNotFound, mkdir, now, randomInt, readBytes, readdir, rename, rm, stat, writeBytes, } from "../effects/node/module.f.js";
|
|
13
12
|
import { toOption } from "../types/nullable/module.f.js";
|
|
14
13
|
import { error, ok, unwrap } from "../types/result/module.f.js";
|
|
15
14
|
import { splitAt } from "../types/string/module.f.js";
|
|
15
|
+
import { nonEmpty, empty as elEmpty } from "../effects/list/module.f.js";
|
|
16
16
|
const split2 = splitAt(2);
|
|
17
17
|
const prefix = '.cas';
|
|
18
18
|
/** Converts a content key to its sharded relative CAS file path. */
|
|
@@ -22,151 +22,188 @@ export const toPath = (key) => {
|
|
|
22
22
|
const [b, c] = split2(bc);
|
|
23
23
|
return join(prefix, a, b, c);
|
|
24
24
|
};
|
|
25
|
+
/** Maximum chunk size for streaming reads: the largest `Vec` the runtime allows. */
|
|
26
|
+
const chunkBytes = Number(maxLengthBytes);
|
|
27
|
+
/** Staging directory under the store root; GC and every uploader share it. */
|
|
28
|
+
const stageRel = '_stage';
|
|
29
|
+
/**
|
|
30
|
+
* Lease duration in ms: a staging file's deadline is `now() + leaseDelta`.
|
|
31
|
+
* Renewed after every chunk, so it only has to cover the gap between two
|
|
32
|
+
* consecutive chunks (see [staging-lease.md](../../issues/cas/staging-lease.md)).
|
|
33
|
+
*/
|
|
34
|
+
const leaseDelta = 30_000;
|
|
35
|
+
/**
|
|
36
|
+
* Fixed width for the zero-padded epoch-ms deadline embedded in a staging name,
|
|
37
|
+
* so names sort lexically exactly as they sort chronologically. 19 digits keeps
|
|
38
|
+
* epoch-ms (13 digits today) padded with headroom well past year 2286.
|
|
39
|
+
*/
|
|
40
|
+
const deadlineWidth = 19;
|
|
41
|
+
/** Builds a `<deadline>-<random256>` staging file name. */
|
|
42
|
+
const stageName = (deadline, rnd) => `${String(deadline).padStart(deadlineWidth, '0')}-${rnd}`;
|
|
43
|
+
/** Recovers the deadline (epoch ms) from a `<deadline>-<random256>` name. */
|
|
44
|
+
const deadlineOf = (name) => Number(name.slice(0, name.indexOf('-')));
|
|
45
|
+
/**
|
|
46
|
+
* Reclaims expired staging files: any `_stage/<deadline>-<rand>` whose deadline
|
|
47
|
+
* is already in the past. Lazy and piggy-backed on `write`. Best-effort — a
|
|
48
|
+
* missing `_stage/` (fresh store) or any `readdir`/`rm` error is ignored, and a
|
|
49
|
+
* still-live lease is left alone; the fencing rename keeps even a misjudged
|
|
50
|
+
* reclaim fail-safe (worst case: that upload restarts).
|
|
51
|
+
*/
|
|
52
|
+
const gcStage = (stageDir) => now().step(t => readdir(stageDir, {}).step(([k, v]) => {
|
|
53
|
+
if (k === 'error') {
|
|
54
|
+
return pure(undefined);
|
|
55
|
+
}
|
|
56
|
+
const expired = v.flatMap(d => d.isFile && deadlineOf(d.name) < t ? [d.name] : []);
|
|
57
|
+
return forEachStep((name) => rm(join(stageDir, name)).step(() => pure(undefined)))(expired);
|
|
58
|
+
}));
|
|
25
59
|
/**
|
|
26
60
|
* Builds a content-addressable storage facade from a SHA-2 implementation.
|
|
27
61
|
*/
|
|
28
|
-
export const fileCas = (sha2) => {
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
62
|
+
export const fileCas = (sha2) => (path) => {
|
|
63
|
+
const storePrefix = join(path, prefix);
|
|
64
|
+
const normalizedStorePrefix = normalize(storePrefix);
|
|
65
|
+
const stageDir = join(storePrefix, stageRel);
|
|
66
|
+
return {
|
|
67
|
+
read: (hash) => {
|
|
68
|
+
const p = join(path, toPath(hash));
|
|
69
|
+
const loop = (offset) => readBytes(p, offset, chunkBytes)
|
|
70
|
+
.step((result) => {
|
|
71
|
+
const [t, v] = result;
|
|
72
|
+
// A missing shard or read error is an explicit error item, never EOF.
|
|
73
|
+
if (t === 'error') {
|
|
74
|
+
return nonEmpty(result, elEmpty());
|
|
75
|
+
}
|
|
76
|
+
// End the stream only on an empty read; every non-empty read — including a
|
|
77
|
+
// final short (`< CHUNK_BYTES`) chunk — is emitted as an `ok` item.
|
|
78
|
+
return length(v) === 0n
|
|
79
|
+
? elEmpty()
|
|
80
|
+
: nonEmpty(ok(v), loop(offset + chunkBytes));
|
|
81
|
+
});
|
|
82
|
+
return loop(0);
|
|
83
|
+
},
|
|
84
|
+
// Lock-free staging upload (issues/cas/staging-lease.md): stream each chunk straight
|
|
85
|
+
// to a `_stage/<deadline>-<rand>` file via `writeBytes` while folding it into the
|
|
86
|
+
// running SHA-2 state — the payload never lives in memory as a whole. The lease is
|
|
87
|
+
// renewed (rename to a fresh deadline) after every chunk; any error deletes the
|
|
88
|
+
// partial file and fails. On end-of-stream the file is published to its hash-derived
|
|
89
|
+
// shard path by a replace-`rename` (which also dedups/repairs a same-content shard),
|
|
90
|
+
// and success is confirmed by a `stat` size check. GC of expired staging files is
|
|
91
|
+
// piggy-backed at the start.
|
|
92
|
+
write: (payload) => {
|
|
93
|
+
// Publish the finished staging file to its content-addressed shard. The three
|
|
94
|
+
// filesystem steps run best-effort with their results ignored; success is decided
|
|
95
|
+
// afterward by observing the target's size (see staging-lease.md "Publish ignores
|
|
96
|
+
// results and checks the end state").
|
|
97
|
+
const publish = (state, offset, curPath) => {
|
|
98
|
+
const hash = sha2.end(state);
|
|
99
|
+
const rel = toPath(hash);
|
|
100
|
+
const dst = join(path, rel);
|
|
101
|
+
const dstDir = join(path, ...parse(rel).slice(0, -1));
|
|
102
|
+
return mkdir(dstDir, { recursive: true })
|
|
103
|
+
.step(() => rename(curPath, dst))
|
|
104
|
+
.step(() => rm(curPath))
|
|
105
|
+
.step(() => stat(dst))
|
|
106
|
+
.step(st => pure(st[0] === 'ok' && st[1].size === offset ? ok(hash) : error('publish size mismatch')));
|
|
107
|
+
};
|
|
108
|
+
// Any streaming error fails closed: delete the partial file, return the error.
|
|
109
|
+
const fail = (curPath, e) => rm(curPath).step(() => pure(error(e)));
|
|
110
|
+
return gcStage(stageDir).step(() => random256.step(rnd => {
|
|
111
|
+
const rndStr = vecToCBase32(rnd);
|
|
112
|
+
const loop = (state, offset, curPath) => (stream) => stream
|
|
113
|
+
.step((node) => {
|
|
114
|
+
if (node === undefined) {
|
|
115
|
+
return publish(state, offset, curPath);
|
|
55
116
|
}
|
|
56
|
-
|
|
117
|
+
const { first, tail } = node;
|
|
118
|
+
if (first[0] === 'error') {
|
|
119
|
+
return fail(curPath, first[1]);
|
|
120
|
+
}
|
|
121
|
+
const chunk = first[1];
|
|
122
|
+
return writeBytes(curPath, offset, chunk)
|
|
123
|
+
.step(wb => {
|
|
124
|
+
if (wb[0] === 'error') {
|
|
125
|
+
return fail(curPath, wb[1]);
|
|
126
|
+
}
|
|
127
|
+
const newState = sha2.append(chunk)(state);
|
|
128
|
+
const newOffset = offset + Number(length(chunk) / 8n);
|
|
129
|
+
// Renew the lease: rename to a fresh deadline (keeps `delta` constant).
|
|
130
|
+
return now()
|
|
131
|
+
.step(t => {
|
|
132
|
+
const next = join(stageDir, stageName(t + leaseDelta, rndStr));
|
|
133
|
+
return rename(curPath, next).step(([t, v]) => t === 'error'
|
|
134
|
+
? fail(curPath, v)
|
|
135
|
+
: loop(newState, newOffset, next)(tail));
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
return mkdir(stageDir, { recursive: true })
|
|
140
|
+
.step(() => now()
|
|
141
|
+
.step(t0 => {
|
|
142
|
+
const path0 = join(stageDir, stageName(t0 + leaseDelta, rndStr));
|
|
143
|
+
return createExclusive(path0)
|
|
144
|
+
.step(([c, e]) => c === 'error'
|
|
145
|
+
? pure(error(e))
|
|
146
|
+
: loop(sha2.init, 0, path0)(payload));
|
|
147
|
+
}));
|
|
148
|
+
}));
|
|
149
|
+
},
|
|
150
|
+
list: () =>
|
|
151
|
+
// A fresh store has no `.cas` directory yet. Treat *only* that case as an
|
|
152
|
+
// empty store, mirroring how `read` maps a missing shard to an error item.
|
|
153
|
+
// A `.cas` that exists but cannot be read (permissions, corruption) is a
|
|
154
|
+
// genuine storage error and is surfaced, not masked as "no hashes".
|
|
155
|
+
access(storePrefix).step(a => {
|
|
156
|
+
if (a[0] === 'error') {
|
|
157
|
+
if (isNotFound(a[1])) {
|
|
158
|
+
return pure([]);
|
|
57
159
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
160
|
+
throw a[1];
|
|
161
|
+
}
|
|
162
|
+
return readdir(storePrefix, { recursive: true })
|
|
163
|
+
.step(r => pure(unwrap(r).flatMap(({ name, parentPath, isFile }) => toOption(isFile
|
|
164
|
+
? cBase32ToVec(normalize(parentPath).substring(normalizedStorePrefix.length).replaceAll('/', '') + name)
|
|
165
|
+
: null))));
|
|
166
|
+
}),
|
|
167
|
+
url: (hash) => join(path, toPath(hash))
|
|
64
168
|
};
|
|
65
169
|
};
|
|
66
|
-
/** Maximum chunk size for streaming reads: the largest `Vec` the runtime allows. */
|
|
67
|
-
const CHUNK_BYTES = Number(maxLengthBytes);
|
|
68
170
|
/** 256-bit random `Vec` built from 8 sequential `randomInt` (32-bit) calls. */
|
|
69
171
|
const random256 = foldStep((_) => (acc) => randomInt().step(r => pure(msb.concat(acc)(vec(32n)(BigInt(r))))))(empty)([0, 1, 2, 3, 4, 5, 6, 7]);
|
|
70
|
-
/**
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
*/
|
|
74
|
-
const streamHash = (sha2) => (path) => {
|
|
75
|
-
const chunkBits = BigInt(CHUNK_BYTES) * 8n;
|
|
76
|
-
const loop = (state, offset) => readBytes(path, offset, CHUNK_BYTES).step(result => {
|
|
172
|
+
/** Streams any file at `filePath` in `<=128 KiB` chunks as a `ListEffect` of `ok` items. */
|
|
173
|
+
const streamFile = (filePath) => {
|
|
174
|
+
const loop = (offset) => readBytes(filePath, offset, chunkBytes).step((result) => {
|
|
77
175
|
if (result[0] === 'error') {
|
|
78
|
-
return
|
|
176
|
+
return nonEmpty(result, elEmpty());
|
|
79
177
|
}
|
|
80
178
|
const chunk = result[1];
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
return loop(newState, offset + CHUNK_BYTES);
|
|
179
|
+
return length(chunk) === 0n
|
|
180
|
+
? elEmpty()
|
|
181
|
+
: nonEmpty(ok(chunk), loop(offset + chunkBytes));
|
|
86
182
|
});
|
|
87
|
-
return loop(
|
|
183
|
+
return loop(0);
|
|
88
184
|
};
|
|
89
185
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
186
|
+
* Streams the file at `path` through `cas.write`, returning the content hash.
|
|
187
|
+
* Both the CLI `cas add` and the MCP `add` delegate to this; the MCP layer
|
|
188
|
+
* additionally deletes the source file on success.
|
|
189
|
+
*/
|
|
190
|
+
export const casAddFile = (cas) => (path) =>
|
|
191
|
+
// streamFile produces only ReadBytes effects. TypeScript can't prove ListEffect<ReadBytes,T>
|
|
192
|
+
// ≤ ListEffect<O,T> for generic O (recursive type), but the cast is sound: every concrete
|
|
193
|
+
// caller passes a Cas<O> where ReadBytes ⊆ O (e.g. FileCasOperation).
|
|
194
|
+
cas.write(streamFile(path));
|
|
195
|
+
/**
|
|
196
|
+
* Upload pipeline: streams `fileName` from `~/cas_upload/` through `casAddFile`,
|
|
197
|
+
* then deletes the source on success. On failure the source is left in place so
|
|
198
|
+
* the upload can be retried; `write` already cleans up its own partial staging file.
|
|
93
199
|
*/
|
|
94
200
|
export const casUpload = (home) => (fileName) => {
|
|
95
201
|
const src = join(home, 'cas_upload', fileName);
|
|
96
|
-
const
|
|
97
|
-
return
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
.step(r => {
|
|
103
|
-
if (r[0] === 'error') {
|
|
104
|
-
return pure(error(r[1]));
|
|
105
|
-
}
|
|
106
|
-
return streamHash(sha256)(stagePath)
|
|
107
|
-
.step(hashResult => {
|
|
108
|
-
if (hashResult[0] === 'error') {
|
|
109
|
-
return pure(hashResult);
|
|
110
|
-
}
|
|
111
|
-
const hash = hashResult[1];
|
|
112
|
-
const p = toPath(hash);
|
|
113
|
-
const parts = parse(p);
|
|
114
|
-
const finalDir = join(home, ...parts.slice(0, -1));
|
|
115
|
-
const finalPath = join(home, p);
|
|
116
|
-
return mkdir(finalDir, { recursive: true })
|
|
117
|
-
.step(() => rename(stagePath, finalPath))
|
|
118
|
-
.step(r2 => pure(r2[0] === 'error' ? error(r2[1]) : ok(hash)));
|
|
119
|
-
});
|
|
120
|
-
});
|
|
202
|
+
const c = fileCas(sha256)(home);
|
|
203
|
+
return casAddFile(c)(src).step((result) => {
|
|
204
|
+
if (result[0] === 'error') {
|
|
205
|
+
return pure(result);
|
|
206
|
+
}
|
|
207
|
+
return rm(src).step(() => pure(result));
|
|
121
208
|
});
|
|
122
209
|
};
|
|
123
|
-
export const commands = [
|
|
124
|
-
{
|
|
125
|
-
names: ['add'],
|
|
126
|
-
description: 'Store file content and print its hash',
|
|
127
|
-
handler: ({ home, args: [path, ...rest] }) => {
|
|
128
|
-
if (path === undefined || rest.length !== 0) {
|
|
129
|
-
return errorExit("'cas add' expects one parameter");
|
|
130
|
-
}
|
|
131
|
-
const c = fileCas(sha256)(home);
|
|
132
|
-
return readFile(path)
|
|
133
|
-
.step(v => c.write(unwrap(v)))
|
|
134
|
-
.step(hash => hash === undefined
|
|
135
|
-
? pure(1)
|
|
136
|
-
: log(vecToCBase32(hash)).step(() => pure(0)));
|
|
137
|
-
},
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
names: ['get'],
|
|
141
|
-
description: 'Restore content by hash into a file',
|
|
142
|
-
handler: ({ home, args: [hashCBase32, path, ...rest] }) => {
|
|
143
|
-
if (hashCBase32 === undefined || path === undefined || rest.length !== 0) {
|
|
144
|
-
return errorExit("'cas get' expects two parameters");
|
|
145
|
-
}
|
|
146
|
-
const hash = cBase32ToVec(hashCBase32);
|
|
147
|
-
if (hash === null) {
|
|
148
|
-
return errorExit(`invalid hash format: ${hashCBase32}`);
|
|
149
|
-
}
|
|
150
|
-
const c = fileCas(sha256)(home);
|
|
151
|
-
return c.read(hash)
|
|
152
|
-
.step(v => {
|
|
153
|
-
const result = v === undefined
|
|
154
|
-
? errorExit(`no such hash: ${hashCBase32}`)
|
|
155
|
-
: writeFile(path, v)
|
|
156
|
-
.step(() => pure(0));
|
|
157
|
-
return result;
|
|
158
|
-
});
|
|
159
|
-
},
|
|
160
|
-
},
|
|
161
|
-
{
|
|
162
|
-
names: ['list'],
|
|
163
|
-
description: 'List all stored content hashes',
|
|
164
|
-
handler: ({ home }) => {
|
|
165
|
-
const c = fileCas(sha256)(home);
|
|
166
|
-
return c.list()
|
|
167
|
-
.step(forEachStep(j => log(vecToCBase32(j))))
|
|
168
|
-
.step(() => pure(0));
|
|
169
|
-
},
|
|
170
|
-
},
|
|
171
|
-
];
|
|
172
|
-
export const main = dispatch(commands);
|
package/fs/cas/proof.f.d.ts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
|
+
import { type FileCasOperation } from './module.f.ts';
|
|
2
|
+
import { type Effect } from '../effects/module.f.ts';
|
|
3
|
+
import { type ReadFile, type WriteFile, type Rm, type Mkdir } from '../effects/node/module.f.ts';
|
|
4
|
+
type TestOp = FileCasOperation | WriteFile | ReadFile | Rm | Mkdir;
|
|
1
5
|
export declare const proof: {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
casWrite: () => void;
|
|
14
|
-
casReadPassthrough: () => void;
|
|
6
|
+
addBigFile: () => Effect<TestOp, void>;
|
|
7
|
+
addAndGetBigFile: () => Effect<TestOp, void>;
|
|
8
|
+
casWriteRead: () => void;
|
|
9
|
+
casReadMissingShard: () => void;
|
|
10
|
+
casWriteMultiChunk: () => void;
|
|
11
|
+
casWriteDedup: () => void;
|
|
12
|
+
casWriteErrorItemAborts: () => void;
|
|
13
|
+
casWriteReadExceedsMaxLength: () => void;
|
|
14
|
+
casWriteGcReclaimsExpired: () => void;
|
|
15
|
+
casUploadSuccess: () => void;
|
|
16
|
+
casUploadFailureKeepsSource: () => void;
|
|
15
17
|
};
|
|
18
|
+
export {};
|