functionalscript 0.32.4 → 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.
Files changed (90) hide show
  1. package/README.md +1 -1
  2. package/fs/asserts/module.f.d.ts +1 -1
  3. package/fs/asserts/module.f.js +4 -1
  4. package/fs/bnf/data/module.f.d.ts +0 -103
  5. package/fs/bnf/data/module.f.js +10 -246
  6. package/fs/bnf/data/proof.f.d.ts +0 -7
  7. package/fs/bnf/data/proof.f.js +3 -636
  8. package/fs/bnf/descent/module.f.d.ts +56 -0
  9. package/fs/bnf/descent/module.f.js +119 -0
  10. package/fs/bnf/descent/proof.f.d.ts +5 -0
  11. package/fs/bnf/descent/proof.f.js +296 -0
  12. package/fs/bnf/ll1/module.f.d.ts +72 -0
  13. package/fs/bnf/ll1/module.f.js +153 -0
  14. package/fs/bnf/ll1/proof.f.d.ts +9 -0
  15. package/fs/bnf/ll1/proof.f.js +358 -0
  16. package/fs/cas/cli/module.f.d.ts +6 -0
  17. package/fs/cas/cli/module.f.js +56 -0
  18. package/fs/cas/cli/proof.f.d.ts +14 -0
  19. package/fs/cas/cli/proof.f.js +149 -0
  20. package/fs/cas/mcp/module.f.d.ts +9 -17
  21. package/fs/cas/mcp/module.f.js +117 -83
  22. package/fs/cas/mcp/proof.f.d.ts +5 -1
  23. package/fs/cas/mcp/proof.f.js +79 -32
  24. package/fs/cas/module.f.d.ts +33 -27
  25. package/fs/cas/module.f.js +151 -126
  26. package/fs/cas/proof.f.d.ts +16 -13
  27. package/fs/cas/proof.f.js +255 -130
  28. package/fs/ci/config/module.f.d.ts +15 -15
  29. package/fs/ci/config/module.f.js +15 -15
  30. package/fs/ci/proof.f.js +8 -6
  31. package/fs/cli/proof.f.js +2 -12
  32. package/fs/djs/parser/module.f.d.ts +0 -5
  33. package/fs/djs/proof.f.js +5 -6
  34. package/fs/djs/serializer/module.f.d.ts +1 -3
  35. package/fs/djs/tokenizer-new/module.f.js +3 -2
  36. package/fs/djs/tokenizer-new/proof.f.d.ts +3 -0
  37. package/fs/djs/tokenizer-new/proof.f.js +458 -296
  38. package/fs/djs/transpiler/proof.f.js +10 -10
  39. package/fs/effects/list/module.f.d.ts +31 -0
  40. package/fs/effects/list/module.f.js +16 -0
  41. package/fs/effects/module.f.d.ts +11 -5
  42. package/fs/effects/module.f.js +16 -6
  43. package/fs/effects/node/module.f.d.ts +29 -3
  44. package/fs/effects/node/module.f.js +35 -1
  45. package/fs/effects/node/module.js +32 -18
  46. package/fs/effects/node/proof.f.js +36 -36
  47. package/fs/effects/node/virtual/module.f.d.ts +16 -2
  48. package/fs/effects/node/virtual/module.f.js +129 -20
  49. package/fs/effects/node/virtual/proof.f.d.ts +4 -0
  50. package/fs/effects/node/virtual/proof.f.js +47 -14
  51. package/fs/effects/proof.f.d.ts +4 -0
  52. package/fs/effects/proof.f.js +18 -1
  53. package/fs/emergent_testing/proof.f.js +3 -11
  54. package/fs/fjs/module.f.js +2 -8
  55. package/fs/fjs/proof.f.js +2 -11
  56. package/fs/html/module.f.js +6 -8
  57. package/fs/js/tokenizer/module.f.d.ts +5 -0
  58. package/fs/js/tokenizer/module.f.js +13 -0
  59. package/fs/json/serializer/module.f.js +0 -1
  60. package/fs/mcp/module.f.d.ts +1 -1
  61. package/fs/mime/module.f.d.ts +101 -5
  62. package/fs/mime/module.f.js +159 -6
  63. package/fs/mime/proof.f.d.ts +25 -0
  64. package/fs/mime/proof.f.js +193 -3
  65. package/fs/text/code_point/module.f.d.ts +39 -2
  66. package/fs/text/code_point/module.f.js +74 -2
  67. package/fs/text/code_point/proof.f.d.ts +8 -0
  68. package/fs/text/code_point/proof.f.js +72 -0
  69. package/fs/text/sgr/proof.f.js +2 -11
  70. package/fs/text/utf16/module.f.js +4 -26
  71. package/fs/text/utf8/module.f.d.ts +27 -0
  72. package/fs/text/utf8/module.f.js +8 -14
  73. package/fs/types/bigint/module.f.d.ts +8 -15
  74. package/fs/types/bigint/module.f.js +9 -22
  75. package/fs/types/bigint/proof.f.d.ts +0 -2
  76. package/fs/types/bigint/proof.f.js +6 -16
  77. package/fs/types/function/operator/module.f.d.ts +26 -0
  78. package/fs/types/list/module.f.d.ts +3 -0
  79. package/fs/types/result/module.d.ts +2 -4
  80. package/fs/types/result/module.js +9 -3
  81. package/package.json +3 -3
  82. package/issues/demo/data/data.f.js +0 -12
  83. package/issues/demo/data/shared.f.js +0 -3
  84. package/issues/demo/fs/app.js +0 -4
  85. package/issues/demo/fs/math.f.js +0 -4
  86. package/issues/demo/sample/proof.f.js +0 -13
  87. /package/{issues → todo}/031-json.f.d.ts +0 -0
  88. /package/{issues → todo}/031-json.f.js +0 -0
  89. /package/{issues → todo}/proof.f.d.ts +0 -0
  90. /package/{issues → todo}/proof.f.js +0 -0
@@ -1,10 +1,15 @@
1
1
  /**
2
2
  * Magic-byte MIME type detection.
3
3
  *
4
- * A pure table lookup over the leading bytes of a `Vec`: no I/O, no
5
- * dependencies beyond `fs/types/bit_vec`. `detect` returns a MIME type string
6
- * for the container formats whose signatures it knows, or `null` for anything
7
- * else — text, unknown binary, or a `Vec` too short to match.
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 recognised format, or `null` when the
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 {};
@@ -1,10 +1,15 @@
1
1
  /**
2
2
  * Magic-byte MIME type detection.
3
3
  *
4
- * A pure table lookup over the leading bytes of a `Vec`: no I/O, no
5
- * dependencies beyond `fs/types/bit_vec`. `detect` returns a MIME type string
6
- * for the container formats whose signatures it knows, or `null` for anything
7
- * else — text, unknown binary, or a `Vec` too short to match.
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 recognised format, or `null` when the
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
+ };
@@ -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
  };
@@ -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 { detect } from "./module.f.js";
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
  };
@@ -1,8 +1,10 @@
1
1
  /**
2
2
  * Shared Unicode code-point contract for the UTF-8 and UTF-16 decoders: the
3
- * error-tag mask used to flag invalid sequences, and a streaming `decoder`
3
+ * error-tag mask used to flag invalid sequences, the streaming `decoder`
4
4
  * factory that wraps a per-unit step and an end-of-input step into a single
5
- * `List`-to-`List` conversion.
5
+ * `List`-to-`List` conversion, and the code-point classification predicates
6
+ * (BMP / surrogate / supplementary-plane / overall validity) that both codecs
7
+ * share.
6
8
  *
7
9
  * @module
8
10
  */
@@ -26,3 +28,38 @@ export declare const errorMask = 2147483648;
26
28
  * @returns A function converting a list of code units into a list of code points.
27
29
  */
28
30
  export declare const decoder: <Unit, S, Cp>(byteOp: StateScan<Unit, S | null, List<Cp>>, eofOp: (state: S | null) => readonly [List<Cp>, S | null]) => (input: List<Unit>) => List<Cp>;
31
+ /**
32
+ * Checks whether the 16-bit word (U16) is a high surrogate.
33
+ * Range: 0xD800 - 0xDBFF.
34
+ */
35
+ export declare const isHighSurrogate: (i: number) => boolean;
36
+ /**
37
+ * Checks whether the 16-bit word (U16) is a low surrogate.
38
+ * Range: 0xDC00 - 0xDFFF.
39
+ */
40
+ export declare const isLowSurrogate: (i: number) => boolean;
41
+ /**
42
+ * Checks whether the code point is in the BMP range.
43
+ * BMP is the main Unicode plane that covers code points 0x0000 - 0xFFFF, except
44
+ * for the range of surrogates.
45
+ */
46
+ export declare const isBmpCodePoint: (codePoint: number) => boolean;
47
+ /**
48
+ * Checks whether the code point belongs to a supplementary (additional) Unicode
49
+ * plane. Supplementary planes cover code points from 0x010000 to 0x10FFFF.
50
+ */
51
+ export declare const isSupplementaryPlane: (i: number) => boolean;
52
+ /**
53
+ * Checks whether the code point is a valid scalar value: within the assignable
54
+ * Unicode range (0x0000 - 0x10FFFF) and not a surrogate.
55
+ */
56
+ export declare const isValidCodePoint: (c: number) => boolean;
57
+ /**
58
+ * Checks whether the code point is a *text* code point — distinct from
59
+ * {@link isValidCodePoint}, which gates well-formedness for decoding. A code
60
+ * point is text unless it is a control character: the controls are 0x0000 -
61
+ * 0x001F, 0x007F (DEL), and 0x0080 - 0x009F (C1), minus the whitespace block
62
+ * 0x0009 - 0x000D (TAB, LF, VT, FF, CR), which is legitimate in text. Every
63
+ * code point at or above 0x0020 that is not DEL or a C1 control is text.
64
+ */
65
+ export declare const isTextCodePoint: (c: number) => boolean;