functionalscript 0.33.0 → 0.35.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 +27 -14
- package/fs/asserts/module.f.d.ts +1 -1
- package/fs/asserts/module.f.js +4 -1
- package/fs/base64/proof.f.d.ts +1 -0
- package/fs/base64/proof.f.js +25 -56
- package/fs/base_n/module.f.js +11 -5
- package/fs/base_n/proof.f.d.ts +1 -0
- package/fs/base_n/proof.f.js +12 -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 +123 -96
- package/fs/cas/mcp/proof.f.d.ts +12 -1
- package/fs/cas/mcp/proof.f.js +199 -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/mcp/stdio/module.f.d.ts +8 -0
- package/fs/mcp/stdio/module.f.js +33 -6
- package/fs/mcp/stdio/proof.f.d.ts +3 -0
- package/fs/mcp/stdio/proof.f.js +52 -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/module.f.d.ts +11 -1
- package/fs/text/module.f.js +13 -3
- package/fs/text/proof.f.d.ts +6 -0
- package/fs/text/proof.f.js +25 -2
- 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/bit_vec/module.f.d.ts +20 -2
- package/fs/types/bit_vec/module.f.js +77 -27
- package/fs/types/bit_vec/proof.f.d.ts +8 -0
- package/fs/types/bit_vec/proof.f.js +24 -6
- package/fs/types/function/operator/module.f.d.ts +26 -0
- package/fs/types/list/module.f.d.ts +3 -0
- package/fs/types/nullable/module.f.d.ts +9 -5
- package/fs/types/nullable/module.f.js +19 -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/mcp/stdio/proof.f.js
CHANGED
|
@@ -4,8 +4,9 @@ import { emptyState, virtual } from "../../effects/node/virtual/module.f.js";
|
|
|
4
4
|
import { stringify } from "../../json/module.f.js";
|
|
5
5
|
import { utf8 } from "../../text/module.f.js";
|
|
6
6
|
import { fromVec } from "../../types/uint8array/module.f.js";
|
|
7
|
+
import { maxLengthBytes } from "../../types/bit_vec/module.f.js";
|
|
7
8
|
import { sort } from "../../types/object/module.f.js";
|
|
8
|
-
import { jsonrpc, parseError } from "../../json/rpc/module.f.js";
|
|
9
|
+
import { internalError, jsonrpc, parseError } from "../../json/rpc/module.f.js";
|
|
9
10
|
import { stdioTransport } from "./module.f.js";
|
|
10
11
|
const stringifyJson = stringify(sort);
|
|
11
12
|
// Extracts the request `id` (a request has one; a notification does not).
|
|
@@ -29,7 +30,11 @@ const runStep = (step) => (input) => virtual({ ...emptyState, stdin: toBytes(inp
|
|
|
29
30
|
const run = runStep(echoStep);
|
|
30
31
|
const okResponse = (id) => stringifyJson({ jsonrpc, result: { ok: true }, id }) + '\n';
|
|
31
32
|
const parseErrorLine = stringifyJson({ jsonrpc, error: parseError, id: null }) + '\n';
|
|
33
|
+
const internalErrorLine = (id) => stringifyJson({ jsonrpc, error: internalError, id }) + '\n';
|
|
32
34
|
const ping = (id) => `{"jsonrpc":"2.0","method":"ping","id":${id}}`;
|
|
35
|
+
// One byte past `maxLengthBytes` on its own; embedded in a response envelope
|
|
36
|
+
// it stays comfortably over the limit despite the surrounding JSON overhead.
|
|
37
|
+
const oversizedString = 'a'.repeat(Number(maxLengthBytes) + 1);
|
|
33
38
|
const notification = '{"jsonrpc":"2.0","method":"notifications/initialized"}';
|
|
34
39
|
export const proof = {
|
|
35
40
|
// EOF on the very first read: clean shutdown, nothing written, no further reads.
|
|
@@ -81,6 +86,52 @@ export const proof = {
|
|
|
81
86
|
const state = runStep(step)(ping(1) + '\n');
|
|
82
87
|
assertEq(state.stdout, okResponse(1));
|
|
83
88
|
},
|
|
89
|
+
// A response that would exceed `maxLengthBytes` once UTF-8 encoded cannot
|
|
90
|
+
// be written as a single bit vector (`tryUtf8` reports overflow); the loop
|
|
91
|
+
// writes a JSON-RPC internal-error response — carrying the original
|
|
92
|
+
// request's `id`, not `null` — instead of throwing or silently dropping
|
|
93
|
+
// the reply.
|
|
94
|
+
oversizedResponseWritesInternalError: () => {
|
|
95
|
+
const step = (value) => {
|
|
96
|
+
const id = idOf(value);
|
|
97
|
+
return pure(id === undefined
|
|
98
|
+
? null
|
|
99
|
+
: { jsonrpc, result: { big: oversizedString }, id });
|
|
100
|
+
};
|
|
101
|
+
const state = runStep(step)(ping(1) + '\n');
|
|
102
|
+
assertEq(state.stdout, internalErrorLine(1));
|
|
103
|
+
},
|
|
104
|
+
// The loop recovers from the oversized-response error and keeps draining
|
|
105
|
+
// stdin: a well-behaved request on the next line still gets its normal
|
|
106
|
+
// reply.
|
|
107
|
+
loopContinuesAfterOversizedResponse: () => {
|
|
108
|
+
const step = (value) => {
|
|
109
|
+
const id = idOf(value);
|
|
110
|
+
return pure(id === undefined
|
|
111
|
+
? null
|
|
112
|
+
: id === 1
|
|
113
|
+
? { jsonrpc, result: { big: oversizedString }, id }
|
|
114
|
+
: { jsonrpc, result: { ok: true }, id });
|
|
115
|
+
};
|
|
116
|
+
const state = runStep(step)([ping(1), ping(2)].join('\n'));
|
|
117
|
+
assertEq(state.stdout, internalErrorLine(1) + okResponse(2));
|
|
118
|
+
assertEq(state.stdin.length, 0);
|
|
119
|
+
},
|
|
120
|
+
// When even the `id`-preserving internal-error fallback would overflow —
|
|
121
|
+
// because the `id` itself is the oversized part, not just `result` — the
|
|
122
|
+
// loop falls back once more to a fixed `id: null` internal-error, the only
|
|
123
|
+
// shape in this transport guaranteed to always fit. Without this second
|
|
124
|
+
// fallback tier the request would get no response line at all.
|
|
125
|
+
oversizedIdFallsBackToNullId: () => {
|
|
126
|
+
const step = (value) => {
|
|
127
|
+
const id = idOf(value);
|
|
128
|
+
return pure(id === undefined
|
|
129
|
+
? null
|
|
130
|
+
: { jsonrpc, result: { ok: true }, id: oversizedString });
|
|
131
|
+
};
|
|
132
|
+
const state = runStep(step)(ping(1) + '\n');
|
|
133
|
+
assertEq(state.stdout, internalErrorLine(null));
|
|
134
|
+
},
|
|
84
135
|
// A multi-line session interleaving all cases: request, notification, and
|
|
85
136
|
// malformed line, ending with an unterminated request. Order is preserved
|
|
86
137
|
// and the notification contributes nothing.
|
package/fs/mime/module.f.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Magic-byte MIME type detection.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* for
|
|
7
|
-
*
|
|
4
|
+
* `detect` is a pure table lookup over the leading bytes of a `Vec`: it returns a
|
|
5
|
+
* MIME type string for the container formats whose signatures it knows, or `null`
|
|
6
|
+
* for anything else — text, unknown binary, or a `Vec` too short to match.
|
|
7
|
+
*
|
|
8
|
+
* Beside it, `detectStream` is the **streaming counterpart**: a byte-accepting
|
|
9
|
+
* state machine (length × magic-byte eliminator × UTF-8 validity DFA) that derives
|
|
10
|
+
* `{ length, mime_type, type }` by folding a CAS read stream in O(1) space, without
|
|
11
|
+
* ever buffering the blob into a single `maxLength`-bounded `Vec`. See the README
|
|
12
|
+
* for the factored design.
|
|
8
13
|
*
|
|
9
14
|
* The CAS store is type-agnostic and keeps raw bytes only, so type is never
|
|
10
15
|
* stored; it is recovered on read by sniffing the content. Callers decide what
|
|
@@ -29,11 +34,102 @@
|
|
|
29
34
|
*/
|
|
30
35
|
import { type Vec } from '../types/bit_vec/module.f.ts';
|
|
31
36
|
import type { Nullable } from '../types/nullable/module.f.ts';
|
|
37
|
+
import { type Effect, type Operation } from '../effects/module.f.ts';
|
|
38
|
+
import type { List } from '../effects/list/module.f.ts';
|
|
39
|
+
import type { IoResult } from '../effects/node/module.f.ts';
|
|
40
|
+
import { type Utf8State } from '../text/utf8/module.f.ts';
|
|
32
41
|
/**
|
|
33
42
|
* Detects the MIME type of `bytes` from its leading magic-byte signature.
|
|
34
43
|
*
|
|
35
|
-
* @returns the MIME type string for a
|
|
44
|
+
* @returns the MIME type string for a recognized format, or `null` when the
|
|
36
45
|
* leading bytes match no known signature (including any `Vec` shorter than
|
|
37
46
|
* the signature it might otherwise match).
|
|
38
47
|
*/
|
|
39
48
|
export declare const detect: (bytes: Vec) => Nullable<string>;
|
|
49
|
+
/**
|
|
50
|
+
* A magic-byte signature as a byte pattern. `null` entries are wildcards (the
|
|
51
|
+
* four little-endian size bytes of WebP, between its `RIFF` and `WEBP` markers).
|
|
52
|
+
*/
|
|
53
|
+
type Signature = {
|
|
54
|
+
readonly pattern: readonly Nullable<number>[];
|
|
55
|
+
readonly mime: string;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* `A_magic`: signature elimination. `scan` holds the byte offset and the still-viable
|
|
59
|
+
* signatures; a fully matched signature absorbs into `matched`, an empty viable set
|
|
60
|
+
* into `dead`. Settles within 12 bytes — `matched`/`dead` are absorbing.
|
|
61
|
+
*/
|
|
62
|
+
type MagicState = {
|
|
63
|
+
readonly tag: 'scan';
|
|
64
|
+
readonly pos: number;
|
|
65
|
+
readonly viable: readonly Signature[];
|
|
66
|
+
} | {
|
|
67
|
+
readonly tag: 'matched';
|
|
68
|
+
readonly mime: string;
|
|
69
|
+
} | {
|
|
70
|
+
readonly tag: 'dead';
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* `A_utf8`: a streaming UTF-8 validity-and-text check riding the shared
|
|
74
|
+
* `utf8ByteToCodePointOp` decoder. `st` is the decoder's mid-sequence state;
|
|
75
|
+
* `valid` is `false` once an illegal byte, surrogate, or out-of-range code point
|
|
76
|
+
* is seen — `valid: false` is absorbing. A non-null `st` at EOF (a truncated
|
|
77
|
+
* multi-byte sequence) is invalid. `text` is the orthogonal text-ness verdict: it
|
|
78
|
+
* is `false` once a non-text (control) code point is decoded, even though that
|
|
79
|
+
* code point is perfectly well-formed UTF-8 — `text: false` is absorbing too.
|
|
80
|
+
* Keeping the two distinct lets a valid-but-control blob (e.g. NUL) decode
|
|
81
|
+
* cleanly yet still classify as binary.
|
|
82
|
+
*/
|
|
83
|
+
type Utf8Detect = {
|
|
84
|
+
readonly st: Utf8State;
|
|
85
|
+
readonly valid: boolean;
|
|
86
|
+
readonly text: boolean;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* The product state: running bit length × magic eliminator × UTF-8 validator.
|
|
90
|
+
* The factors never read each other; they meet only in {@link finish}.
|
|
91
|
+
*/
|
|
92
|
+
export type DetectState = {
|
|
93
|
+
readonly length: bigint;
|
|
94
|
+
readonly magic: MagicState;
|
|
95
|
+
readonly utf8: Utf8Detect;
|
|
96
|
+
};
|
|
97
|
+
/** The initial detector state `q₀`. */
|
|
98
|
+
export declare const detectInit: DetectState;
|
|
99
|
+
/**
|
|
100
|
+
* Folds one `Vec` chunk into the detector state (`δ` over a whole chunk). Length
|
|
101
|
+
* always advances by the chunk's bit length; per-byte iteration stops as soon as
|
|
102
|
+
* the verdict is fixed (see {@link isSettled}), so large blobs — including large
|
|
103
|
+
* magic-matched ones — cost ≈ length counting.
|
|
104
|
+
*/
|
|
105
|
+
export declare const push: (s: DetectState) => (chunk: Vec) => DetectState;
|
|
106
|
+
/** The metadata read off the detector at end-of-stream. */
|
|
107
|
+
export type DetectMeta = {
|
|
108
|
+
readonly length: bigint;
|
|
109
|
+
readonly mime_type: string;
|
|
110
|
+
readonly type: 'text' | 'base64';
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Reads the answer off the final state (`λ`). Reproduces the three-way result of
|
|
114
|
+
* the pure path: magic hit → `base64` + detected mime; else whole-blob-valid UTF-8
|
|
115
|
+
* that is also all-text (byte-aligned, no invalidity, no control bytes) → `text` +
|
|
116
|
+
* `text/plain`; else → `base64` + `application/octet-stream`. A valid-but-control
|
|
117
|
+
* blob (NUL, other controls) is well-formed UTF-8 yet falls through to the binary
|
|
118
|
+
* branch.
|
|
119
|
+
*/
|
|
120
|
+
export declare const finish: (s: DetectState) => DetectMeta;
|
|
121
|
+
/**
|
|
122
|
+
* Classifies a whole `Vec` with the same state machine as {@link detectStream}.
|
|
123
|
+
* The single-buffer counterpart for callers that already hold the bytes (the
|
|
124
|
+
* `cas_get` `content: true` path materializes the blob anyway): both paths read
|
|
125
|
+
* the three-way `{ length, mime_type, type }` verdict from one machine instead of
|
|
126
|
+
* re-deriving it from `detect` + a separate UTF-8 check.
|
|
127
|
+
*/
|
|
128
|
+
export declare const detectVec: (bytes: Vec) => DetectMeta;
|
|
129
|
+
/**
|
|
130
|
+
* Folds a CAS read stream through {@link push} and reads {@link finish} at EOF,
|
|
131
|
+
* deriving `cas_get` metadata without ever materializing the blob. A read `error`
|
|
132
|
+
* item short-circuits into the `IoResult` error.
|
|
133
|
+
*/
|
|
134
|
+
export declare const detectStream: <O extends Operation>(stream: List<O, IoResult<Vec>>) => Effect<O, IoResult<DetectMeta>>;
|
|
135
|
+
export {};
|
package/fs/mime/module.f.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Magic-byte MIME type detection.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* for
|
|
7
|
-
*
|
|
4
|
+
* `detect` is a pure table lookup over the leading bytes of a `Vec`: it returns a
|
|
5
|
+
* MIME type string for the container formats whose signatures it knows, or `null`
|
|
6
|
+
* for anything else — text, unknown binary, or a `Vec` too short to match.
|
|
7
|
+
*
|
|
8
|
+
* Beside it, `detectStream` is the **streaming counterpart**: a byte-accepting
|
|
9
|
+
* state machine (length × magic-byte eliminator × UTF-8 validity DFA) that derives
|
|
10
|
+
* `{ length, mime_type, type }` by folding a CAS read stream in O(1) space, without
|
|
11
|
+
* ever buffering the blob into a single `maxLength`-bounded `Vec`. See the README
|
|
12
|
+
* for the factored design.
|
|
8
13
|
*
|
|
9
14
|
* The CAS store is type-agnostic and keeps raw bytes only, so type is never
|
|
10
15
|
* stored; it is recovered on read by sniffing the content. Callers decide what
|
|
@@ -27,7 +32,12 @@
|
|
|
27
32
|
*
|
|
28
33
|
* @module
|
|
29
34
|
*/
|
|
30
|
-
import { msb, fromSentinel, length } from "../types/bit_vec/module.f.js";
|
|
35
|
+
import { msb, fromSentinel, length, u8List } from "../types/bit_vec/module.f.js";
|
|
36
|
+
import { iterable } from "../types/list/module.f.js";
|
|
37
|
+
import { pure } from "../effects/module.f.js";
|
|
38
|
+
import { ok, error } from "../types/result/module.f.js";
|
|
39
|
+
import { isValidCodePoint, isTextCodePoint } from "../text/code_point/module.f.js";
|
|
40
|
+
import { utf8ByteToCodePointOp } from "../text/utf8/module.f.js";
|
|
31
41
|
const { startsWith, removeFront } = msb;
|
|
32
42
|
// Each signature is written as a hex literal whose leading `1` nibble is a
|
|
33
43
|
// sentinel marking the start of the byte run (so leading zero bytes survive)
|
|
@@ -61,7 +71,7 @@ const isWebp = (bytes) => length(bytes) >= 96n
|
|
|
61
71
|
/**
|
|
62
72
|
* Detects the MIME type of `bytes` from its leading magic-byte signature.
|
|
63
73
|
*
|
|
64
|
-
* @returns the MIME type string for a
|
|
74
|
+
* @returns the MIME type string for a recognized format, or `null` when the
|
|
65
75
|
* leading bytes match no known signature (including any `Vec` shorter than
|
|
66
76
|
* the signature it might otherwise match).
|
|
67
77
|
*/
|
|
@@ -76,3 +86,146 @@ export const detect = (bytes) => {
|
|
|
76
86
|
}
|
|
77
87
|
return null;
|
|
78
88
|
};
|
|
89
|
+
// The streaming counterpart of `table`/`isWebp`: the same signatures expressed as
|
|
90
|
+
// byte patterns the eliminator can consume one byte at a time. WebP's gap is the
|
|
91
|
+
// only wildcard run.
|
|
92
|
+
const signatures = [
|
|
93
|
+
{ pattern: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a], mime: 'image/png' },
|
|
94
|
+
{ pattern: [0xff, 0xd8, 0xff], mime: 'image/jpeg' },
|
|
95
|
+
{ pattern: [0x47, 0x49, 0x46, 0x38, 0x37, 0x61], mime: 'image/gif' },
|
|
96
|
+
{ pattern: [0x47, 0x49, 0x46, 0x38, 0x39, 0x61], mime: 'image/gif' },
|
|
97
|
+
{ pattern: [0x25, 0x50, 0x44, 0x46, 0x2d], mime: 'application/pdf' },
|
|
98
|
+
{ pattern: [0x50, 0x4b, 0x03, 0x04], mime: 'application/zip' },
|
|
99
|
+
{ pattern: [0x50, 0x4b, 0x05, 0x06], mime: 'application/zip' },
|
|
100
|
+
{ pattern: [0x50, 0x4b, 0x07, 0x08], mime: 'application/zip' },
|
|
101
|
+
{
|
|
102
|
+
pattern: [0x52, 0x49, 0x46, 0x46, null, null, null, null, 0x57, 0x45, 0x42, 0x50],
|
|
103
|
+
mime: 'image/webp',
|
|
104
|
+
},
|
|
105
|
+
];
|
|
106
|
+
const magicInit = { tag: 'scan', pos: 0, viable: signatures };
|
|
107
|
+
const magicStep = (m, byte) => {
|
|
108
|
+
if (m.tag !== 'scan') {
|
|
109
|
+
return m;
|
|
110
|
+
}
|
|
111
|
+
const { pos } = m;
|
|
112
|
+
const viable = m.viable.filter(s => {
|
|
113
|
+
const p = s.pattern[pos];
|
|
114
|
+
return p === null || p === byte;
|
|
115
|
+
});
|
|
116
|
+
for (const s of viable) {
|
|
117
|
+
if (s.pattern.length === pos + 1) {
|
|
118
|
+
return { tag: 'matched', mime: s.mime };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return viable.length === 0 ? { tag: 'dead' } : { tag: 'scan', pos: pos + 1, viable };
|
|
122
|
+
};
|
|
123
|
+
const magicMime = (m) => m.tag === 'matched' ? m.mime : null;
|
|
124
|
+
const utf8Init = { st: null, valid: true, text: true };
|
|
125
|
+
const utf8Step = (u, byte) => {
|
|
126
|
+
if (!u.valid) {
|
|
127
|
+
return u;
|
|
128
|
+
}
|
|
129
|
+
const [cps, st] = utf8ByteToCodePointOp(byte, u.st);
|
|
130
|
+
let text = u.text;
|
|
131
|
+
for (const cp of cps) {
|
|
132
|
+
if (!isValidCodePoint(cp)) {
|
|
133
|
+
return { st, valid: false, text };
|
|
134
|
+
}
|
|
135
|
+
if (!isTextCodePoint(cp)) {
|
|
136
|
+
text = false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return { st, valid: true, text };
|
|
140
|
+
};
|
|
141
|
+
const utf8Valid = (u) => u.valid && u.st === null;
|
|
142
|
+
// A blob is text only when it is whole-blob-valid UTF-8 *and* every decoded code
|
|
143
|
+
// point is a text code point (no NUL/other controls).
|
|
144
|
+
const utf8Text = (u) => utf8Valid(u) && u.text;
|
|
145
|
+
/** The initial detector state `q₀`. */
|
|
146
|
+
export const detectInit = {
|
|
147
|
+
length: 0n,
|
|
148
|
+
magic: magicInit,
|
|
149
|
+
utf8: utf8Init,
|
|
150
|
+
};
|
|
151
|
+
// The outcome can no longer change — `push` may stop decoding and only count
|
|
152
|
+
// length — once `finish` is pinned down. A magic `matched` pins it on its own
|
|
153
|
+
// (`finish` returns the detected mime and ignores the utf8 verdict), so we must
|
|
154
|
+
// not wait for utf8 to go invalid (it may stay valid forever, e.g. an ASCII PDF).
|
|
155
|
+
// A magic `dead` leaves text-vs-octet open, so it settles only once utf8 can no
|
|
156
|
+
// longer be text — either invalid or a control byte seen (both absorbing); `scan`
|
|
157
|
+
// is never settled.
|
|
158
|
+
const isSettled = (magic, utf8) => {
|
|
159
|
+
switch (magic.tag) {
|
|
160
|
+
case 'matched': return true;
|
|
161
|
+
case 'dead': return !utf8.valid || !utf8.text;
|
|
162
|
+
case 'scan': return false;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Folds one `Vec` chunk into the detector state (`δ` over a whole chunk). Length
|
|
167
|
+
* always advances by the chunk's bit length; per-byte iteration stops as soon as
|
|
168
|
+
* the verdict is fixed (see {@link isSettled}), so large blobs — including large
|
|
169
|
+
* magic-matched ones — cost ≈ length counting.
|
|
170
|
+
*/
|
|
171
|
+
export const push = (s) => (chunk) => {
|
|
172
|
+
const bits = length(chunk);
|
|
173
|
+
let magic = s.magic;
|
|
174
|
+
let utf8 = s.utf8;
|
|
175
|
+
if (!isSettled(magic, utf8)) {
|
|
176
|
+
for (const byte of iterable(u8List(msb)(chunk))) {
|
|
177
|
+
magic = magicStep(magic, byte);
|
|
178
|
+
utf8 = utf8Step(utf8, byte);
|
|
179
|
+
if (isSettled(magic, utf8)) {
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { length: s.length + bits, magic, utf8 };
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Reads the answer off the final state (`λ`). Reproduces the three-way result of
|
|
188
|
+
* the pure path: magic hit → `base64` + detected mime; else whole-blob-valid UTF-8
|
|
189
|
+
* that is also all-text (byte-aligned, no invalidity, no control bytes) → `text` +
|
|
190
|
+
* `text/plain`; else → `base64` + `application/octet-stream`. A valid-but-control
|
|
191
|
+
* blob (NUL, other controls) is well-formed UTF-8 yet falls through to the binary
|
|
192
|
+
* branch.
|
|
193
|
+
*/
|
|
194
|
+
export const finish = (s) => {
|
|
195
|
+
const byteLength = s.length >> 3n;
|
|
196
|
+
const mime = magicMime(s.magic);
|
|
197
|
+
if (mime !== null) {
|
|
198
|
+
return { length: byteLength, mime_type: mime, type: 'base64' };
|
|
199
|
+
}
|
|
200
|
+
if (utf8Text(s.utf8) && (s.length & 7n) === 0n) {
|
|
201
|
+
return { length: byteLength, mime_type: 'text/plain', type: 'text' };
|
|
202
|
+
}
|
|
203
|
+
return { length: byteLength, mime_type: 'application/octet-stream', type: 'base64' };
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Classifies a whole `Vec` with the same state machine as {@link detectStream}.
|
|
207
|
+
* The single-buffer counterpart for callers that already hold the bytes (the
|
|
208
|
+
* `cas_get` `content: true` path materializes the blob anyway): both paths read
|
|
209
|
+
* the three-way `{ length, mime_type, type }` verdict from one machine instead of
|
|
210
|
+
* re-deriving it from `detect` + a separate UTF-8 check.
|
|
211
|
+
*/
|
|
212
|
+
export const detectVec = (bytes) => finish(push(detectInit)(bytes));
|
|
213
|
+
/**
|
|
214
|
+
* Folds a CAS read stream through {@link push} and reads {@link finish} at EOF,
|
|
215
|
+
* deriving `cas_get` metadata without ever materializing the blob. A read `error`
|
|
216
|
+
* item short-circuits into the `IoResult` error.
|
|
217
|
+
*/
|
|
218
|
+
export const detectStream = (stream) => {
|
|
219
|
+
const loop = (s) => (l) => l.step((node) => {
|
|
220
|
+
if (node === undefined) {
|
|
221
|
+
return pure(ok(finish(s)));
|
|
222
|
+
}
|
|
223
|
+
const { first, tail } = node;
|
|
224
|
+
const [t, v] = first;
|
|
225
|
+
if (t === 'error') {
|
|
226
|
+
return pure(error(v));
|
|
227
|
+
}
|
|
228
|
+
return loop(push(s)(v))(tail);
|
|
229
|
+
});
|
|
230
|
+
return loop(detectInit)(stream);
|
|
231
|
+
};
|
package/fs/mime/proof.f.d.ts
CHANGED
|
@@ -12,4 +12,29 @@ export declare const proof: {
|
|
|
12
12
|
textIsNull: () => void;
|
|
13
13
|
shortIsNull: () => void;
|
|
14
14
|
emptyIsNull: () => void;
|
|
15
|
+
stream: {
|
|
16
|
+
text: () => void;
|
|
17
|
+
png: () => void;
|
|
18
|
+
octetStream: () => void;
|
|
19
|
+
nul: () => void;
|
|
20
|
+
controlByte: () => void;
|
|
21
|
+
c1Control: () => void;
|
|
22
|
+
whitespaceControlsStayText: () => void;
|
|
23
|
+
magicAcrossChunks: () => void;
|
|
24
|
+
webp: () => void;
|
|
25
|
+
utf8AcrossChunks: () => void;
|
|
26
|
+
validUntilTrailingInvalidByte: () => void;
|
|
27
|
+
truncatedSequence: () => void;
|
|
28
|
+
largeMultiChunkBlob: () => void;
|
|
29
|
+
pdfThenLargeTextTail: () => void;
|
|
30
|
+
pdfThenBinaryTail: () => void;
|
|
31
|
+
readErrorSurfaces: () => void;
|
|
32
|
+
empty: () => void;
|
|
33
|
+
};
|
|
34
|
+
vec: {
|
|
35
|
+
text: () => void;
|
|
36
|
+
png: () => void;
|
|
37
|
+
octetStream: () => void;
|
|
38
|
+
nul: () => void;
|
|
39
|
+
};
|
|
15
40
|
};
|
package/fs/mime/proof.f.js
CHANGED
|
@@ -1,9 +1,31 @@
|
|
|
1
|
-
import { assertEq } from "../asserts/module.f.js";
|
|
2
|
-
import { msb, u8ListToVec, empty } from "../types/bit_vec/module.f.js";
|
|
3
|
-
import {
|
|
1
|
+
import { assert, assertEq } from "../asserts/module.f.js";
|
|
2
|
+
import { msb, u8ListToVec, vec8, repeat, empty } from "../types/bit_vec/module.f.js";
|
|
3
|
+
import { decode } from "../effects/module.f.js";
|
|
4
|
+
import { nonEmpty, empty as emptyList } from "../effects/list/module.f.js";
|
|
5
|
+
import { ok } from "../types/result/module.f.js";
|
|
6
|
+
import { detect, detectStream, detectVec } from "./module.f.js";
|
|
4
7
|
// Builds a big-endian `Vec` from a list of byte values — mirrors how the CAS
|
|
5
8
|
// store would hold the leading bytes of a stored blob.
|
|
6
9
|
const bytes = (...b) => u8ListToVec(msb)(b);
|
|
10
|
+
// ── Streaming detector helpers ──────────────────────────────────────────────────
|
|
11
|
+
// Evaluates a fully pure effect (no operations) to its result.
|
|
12
|
+
const runPure = (e) => {
|
|
13
|
+
const d = decode(e);
|
|
14
|
+
if (!d.done) {
|
|
15
|
+
throw 'effect is not pure';
|
|
16
|
+
}
|
|
17
|
+
return d.result;
|
|
18
|
+
};
|
|
19
|
+
// Builds a CAS-style read stream from a sequence of ok(chunk) items.
|
|
20
|
+
const stream = (...chunks) => chunks.reduceRight((tail, c) => nonEmpty(ok(c), tail), emptyList());
|
|
21
|
+
// Runs the streaming detector over the given chunks and unwraps the metadata.
|
|
22
|
+
const detectChunks = (...chunks) => {
|
|
23
|
+
const r = runPure(detectStream(stream(...chunks)));
|
|
24
|
+
if (r[0] === 'error') {
|
|
25
|
+
throw r[1];
|
|
26
|
+
}
|
|
27
|
+
return r[1];
|
|
28
|
+
};
|
|
7
29
|
export const proof = {
|
|
8
30
|
png: () => assertEq(detect(bytes(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)), 'image/png'),
|
|
9
31
|
jpeg: () => assertEq(detect(bytes(0xff, 0xd8, 0xff, 0xe0)), 'image/jpeg'),
|
|
@@ -27,4 +49,172 @@ export const proof = {
|
|
|
27
49
|
// A prefix shorter than any signature falls through to null, not a partial match.
|
|
28
50
|
shortIsNull: () => assertEq(detect(bytes(0x89, 0x50)), null),
|
|
29
51
|
emptyIsNull: () => assertEq(detect(empty), null),
|
|
52
|
+
// ── Streaming detector (detectStream) ───────────────────────────────────────
|
|
53
|
+
stream: {
|
|
54
|
+
// Plain UTF-8 text classifies as text/plain with the correct byte length.
|
|
55
|
+
text: () => {
|
|
56
|
+
const m = detectChunks(bytes(0x68, 0x65, 0x6c, 0x6c, 0x6f)); // "hello"
|
|
57
|
+
assertEq(m.type, 'text');
|
|
58
|
+
assertEq(m.mime_type, 'text/plain');
|
|
59
|
+
assertEq(m.length, 5n);
|
|
60
|
+
},
|
|
61
|
+
// A magic-byte signature classifies as base64 with the detected mime type.
|
|
62
|
+
png: () => {
|
|
63
|
+
const m = detectChunks(bytes(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x01));
|
|
64
|
+
assertEq(m.type, 'base64');
|
|
65
|
+
assertEq(m.mime_type, 'image/png');
|
|
66
|
+
assertEq(m.length, 10n);
|
|
67
|
+
},
|
|
68
|
+
// Unknown binary (no signature, not UTF-8) falls back to octet-stream/base64.
|
|
69
|
+
octetStream: () => {
|
|
70
|
+
const m = detectChunks(bytes(0xff, 0xfe, 0x00, 0x01));
|
|
71
|
+
assertEq(m.type, 'base64');
|
|
72
|
+
assertEq(m.mime_type, 'application/octet-stream');
|
|
73
|
+
assertEq(m.length, 4n);
|
|
74
|
+
},
|
|
75
|
+
// A NUL-byte blob is valid single-byte UTF-8 (all U+0000) but is binary:
|
|
76
|
+
// NUL is the sharpest binary marker, so it classifies as octet-stream.
|
|
77
|
+
nul: () => {
|
|
78
|
+
const m = detectChunks(bytes(0x00, 0x00, 0x00));
|
|
79
|
+
assertEq(m.type, 'base64');
|
|
80
|
+
assertEq(m.mime_type, 'application/octet-stream');
|
|
81
|
+
assertEq(m.length, 3n);
|
|
82
|
+
},
|
|
83
|
+
// A control byte embedded in otherwise-valid ASCII (here ESC) is still
|
|
84
|
+
// binary — text/plain must not imply terminal escapes.
|
|
85
|
+
controlByte: () => {
|
|
86
|
+
const m = detectChunks(bytes(0x68, 0x69, 0x1b, 0x5b, 0x30, 0x6d)); // "hi\x1b[0m"
|
|
87
|
+
assertEq(m.type, 'base64');
|
|
88
|
+
assertEq(m.mime_type, 'application/octet-stream');
|
|
89
|
+
assertEq(m.length, 6n);
|
|
90
|
+
},
|
|
91
|
+
// A C1 control arrives as 2-byte UTF-8 (C2 85 = U+0085, NEL); invisible at
|
|
92
|
+
// the byte level, it is caught at the code-point level and reads as binary.
|
|
93
|
+
c1Control: () => {
|
|
94
|
+
const m = detectChunks(bytes(0x41, 0xc2, 0x85)); // "A" + U+0085
|
|
95
|
+
assertEq(m.type, 'base64');
|
|
96
|
+
assertEq(m.mime_type, 'application/octet-stream');
|
|
97
|
+
assertEq(m.length, 3n);
|
|
98
|
+
},
|
|
99
|
+
// The text whitespace controls (TAB, LF, VT, FF, CR) are legitimate in text.
|
|
100
|
+
whitespaceControlsStayText: () => {
|
|
101
|
+
const m = detectChunks(bytes(0x61, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x62)); // "a\t\n\v\f\rb"
|
|
102
|
+
assertEq(m.type, 'text');
|
|
103
|
+
assertEq(m.mime_type, 'text/plain');
|
|
104
|
+
assertEq(m.length, 7n);
|
|
105
|
+
},
|
|
106
|
+
// Magic-byte detection threads across a chunk boundary mid-signature.
|
|
107
|
+
magicAcrossChunks: () => {
|
|
108
|
+
const m = detectChunks(bytes(0x89, 0x50, 0x4e), // first 3 bytes of the PNG signature
|
|
109
|
+
bytes(0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00)); // the rest, in a second chunk
|
|
110
|
+
assertEq(m.type, 'base64');
|
|
111
|
+
assertEq(m.mime_type, 'image/png');
|
|
112
|
+
assertEq(m.length, 9n);
|
|
113
|
+
},
|
|
114
|
+
// WebP's wildcard size gap is handled in the streaming form too.
|
|
115
|
+
webp: () => {
|
|
116
|
+
const m = detectChunks(bytes(0x52, 0x49, 0x46, 0x46), // "RIFF"
|
|
117
|
+
bytes(0x1a, 0x00, 0x00, 0x00), // 4-byte size (wildcards)
|
|
118
|
+
bytes(0x57, 0x45, 0x42, 0x50)); // "WEBP"
|
|
119
|
+
assertEq(m.type, 'base64');
|
|
120
|
+
assertEq(m.mime_type, 'image/webp');
|
|
121
|
+
assertEq(m.length, 12n);
|
|
122
|
+
},
|
|
123
|
+
// A multi-byte UTF-8 sequence split across chunks stays valid text.
|
|
124
|
+
utf8AcrossChunks: () => {
|
|
125
|
+
const m = detectChunks(bytes(0xc3), bytes(0xa9)); // "é" = C3 A9
|
|
126
|
+
assertEq(m.type, 'text');
|
|
127
|
+
assertEq(m.mime_type, 'text/plain');
|
|
128
|
+
assertEq(m.length, 2n);
|
|
129
|
+
},
|
|
130
|
+
// The decisive case: valid UTF-8 for every chunk but the last, which adds a
|
|
131
|
+
// lone invalid byte. A leading-bytes buffer would wrongly say "text"; the
|
|
132
|
+
// streaming validator sees the whole blob and classifies it as base64.
|
|
133
|
+
validUntilTrailingInvalidByte: () => {
|
|
134
|
+
const m = detectChunks(bytes(0x68, 0x65, 0x6c, 0x6c, 0x6f), // "hello" — valid UTF-8
|
|
135
|
+
bytes(0xff)); // trailing invalid byte
|
|
136
|
+
assertEq(m.type, 'base64');
|
|
137
|
+
assertEq(m.mime_type, 'application/octet-stream');
|
|
138
|
+
assertEq(m.length, 6n);
|
|
139
|
+
},
|
|
140
|
+
// A truncated multi-byte sequence at EOF is invalid UTF-8, hence base64.
|
|
141
|
+
truncatedSequence: () => {
|
|
142
|
+
const m = detectChunks(bytes(0xc3)); // lead byte with no continuation
|
|
143
|
+
assertEq(m.type, 'base64');
|
|
144
|
+
assertEq(m.mime_type, 'application/octet-stream');
|
|
145
|
+
assertEq(m.length, 1n);
|
|
146
|
+
},
|
|
147
|
+
// A blob larger than `maxLength` (1,048,576 bits) split across chunks — the
|
|
148
|
+
// exact case where `collectRead` would error — returns correct metadata.
|
|
149
|
+
largeMultiChunkBlob: () => {
|
|
150
|
+
const a = repeat(70000n)(vec8(0x61n)); // 70,000 bytes of 'a' (560,000 bits)
|
|
151
|
+
const m = detectChunks(a, a); // 140,000 bytes = 1,120,000 bits > maxLength
|
|
152
|
+
assertEq(m.type, 'text');
|
|
153
|
+
assertEq(m.mime_type, 'text/plain');
|
|
154
|
+
assertEq(m.length, 140000n);
|
|
155
|
+
},
|
|
156
|
+
// A magic match settles the verdict on its own: a large magic-matched blob
|
|
157
|
+
// whose tail stays valid UTF-8 (an ASCII PDF) classifies by the signature.
|
|
158
|
+
// `push` stops decoding the tail once `magic` is matched — `finish` ignores
|
|
159
|
+
// the utf8 verdict here — so this is the magic-matched fast path.
|
|
160
|
+
pdfThenLargeTextTail: () => {
|
|
161
|
+
const header = bytes(0x25, 0x50, 0x44, 0x46, 0x2d); // "%PDF-"
|
|
162
|
+
const tail = repeat(70000n)(vec8(0x61n)); // valid ASCII, never invalidates utf8
|
|
163
|
+
const m = detectChunks(header, tail);
|
|
164
|
+
assertEq(m.type, 'base64');
|
|
165
|
+
assertEq(m.mime_type, 'application/pdf');
|
|
166
|
+
assertEq(m.length, 70005n);
|
|
167
|
+
},
|
|
168
|
+
// A magic match wins regardless of the tail: a binary (non-UTF-8) tail
|
|
169
|
+
// after the signature is still classified by the signature.
|
|
170
|
+
pdfThenBinaryTail: () => {
|
|
171
|
+
const header = bytes(0x25, 0x50, 0x44, 0x46, 0x2d); // "%PDF-"
|
|
172
|
+
const m = detectChunks(header, bytes(0xff, 0xfe, 0x00));
|
|
173
|
+
assertEq(m.type, 'base64');
|
|
174
|
+
assertEq(m.mime_type, 'application/pdf');
|
|
175
|
+
assertEq(m.length, 8n);
|
|
176
|
+
},
|
|
177
|
+
// A read `error` item short-circuits into the IoResult error.
|
|
178
|
+
readErrorSurfaces: () => {
|
|
179
|
+
const errStream = nonEmpty(['error', 'boom'], emptyList());
|
|
180
|
+
const r = runPure(detectStream(errStream));
|
|
181
|
+
assert(r[0] === 'error');
|
|
182
|
+
},
|
|
183
|
+
// Empty stream: zero-length text/plain.
|
|
184
|
+
empty: () => {
|
|
185
|
+
const m = detectChunks();
|
|
186
|
+
assertEq(m.type, 'text');
|
|
187
|
+
assertEq(m.length, 0n);
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
// ── Single-Vec detector (detectVec) ─────────────────────────────────────────
|
|
191
|
+
// The whole-Vec form used by the `cas_get` content:true path; same machine,
|
|
192
|
+
// same three-way verdict as the streaming form.
|
|
193
|
+
vec: {
|
|
194
|
+
text: () => {
|
|
195
|
+
const m = detectVec(bytes(0x68, 0x65, 0x6c, 0x6c, 0x6f)); // "hello"
|
|
196
|
+
assertEq(m.type, 'text');
|
|
197
|
+
assertEq(m.mime_type, 'text/plain');
|
|
198
|
+
assertEq(m.length, 5n);
|
|
199
|
+
},
|
|
200
|
+
png: () => {
|
|
201
|
+
const m = detectVec(bytes(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x01));
|
|
202
|
+
assertEq(m.type, 'base64');
|
|
203
|
+
assertEq(m.mime_type, 'image/png');
|
|
204
|
+
assertEq(m.length, 10n);
|
|
205
|
+
},
|
|
206
|
+
octetStream: () => {
|
|
207
|
+
const m = detectVec(bytes(0xff, 0xfe, 0x00, 0x01));
|
|
208
|
+
assertEq(m.type, 'base64');
|
|
209
|
+
assertEq(m.mime_type, 'application/octet-stream');
|
|
210
|
+
assertEq(m.length, 4n);
|
|
211
|
+
},
|
|
212
|
+
// Valid UTF-8 NUL run is binary in the single-Vec path too.
|
|
213
|
+
nul: () => {
|
|
214
|
+
const m = detectVec(bytes(0x00, 0x00, 0x00));
|
|
215
|
+
assertEq(m.type, 'base64');
|
|
216
|
+
assertEq(m.mime_type, 'application/octet-stream');
|
|
217
|
+
assertEq(m.length, 3n);
|
|
218
|
+
},
|
|
219
|
+
},
|
|
30
220
|
};
|