viell-effect-schema-http 0.10.0 → 0.12.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.
@@ -0,0 +1,250 @@
1
+ import type { SchemaAST } from "effect";
2
+ import { encodedInput } from "./encoded-input.js";
3
+
4
+ type CaptureTerminal = NonNullable<ReturnType<typeof encodedInput>>;
5
+ type Key = string | symbol;
6
+
7
+ interface Node {
8
+ readonly terminal?: CaptureTerminal;
9
+ readonly names: ReadonlyArray<Key>;
10
+ readonly fields: ReadonlyArray<readonly [Key, Node]>;
11
+ }
12
+
13
+ /** A selected path and write, never another candidate on a later check. */
14
+ interface Selected {
15
+ readonly key: Key;
16
+ readonly matches: () => boolean;
17
+ }
18
+
19
+ interface Selection extends Selected {
20
+ readonly written: () => boolean;
21
+ }
22
+
23
+ interface Captured {
24
+ readonly select: (output: unknown) => Selection | undefined;
25
+ }
26
+
27
+ const keyOf = (name: PropertyKey): Key =>
28
+ typeof name === "number" ? String(name) : name;
29
+ const ancestor = (ast: SchemaAST.AST): ast is SchemaAST.Objects =>
30
+ ast._tag === "Objects" &&
31
+ ast.encoding === undefined &&
32
+ ast.indexSignatures.length === 0 &&
33
+ !ast.propertySignatures.some(({ name }) => name === Symbol.iterator);
34
+
35
+ /** Retain the actual enumerable output link or write, including its object identity. */
36
+ const writtenData = (output: unknown, key: Key) => {
37
+ if (
38
+ typeof output !== "object" ||
39
+ output === null ||
40
+ Symbol.iterator in output
41
+ )
42
+ return undefined;
43
+ const prototype: unknown = Object.getPrototypeOf(output);
44
+ const property = Object.getOwnPropertyDescriptor(output, key);
45
+ if (
46
+ property?.enumerable !== true ||
47
+ !("value" in property) ||
48
+ property.value === undefined
49
+ )
50
+ return undefined;
51
+ return {
52
+ value: property.value as unknown,
53
+ matches() {
54
+ try {
55
+ const current = Object.getOwnPropertyDescriptor(output, key);
56
+ return (
57
+ Object.getPrototypeOf(output) === prototype &&
58
+ !(Symbol.iterator in output) &&
59
+ current?.enumerable === true &&
60
+ "value" in current &&
61
+ Object.is(property.value, current.value) &&
62
+ current.writable === property.writable &&
63
+ current.configurable === property.configurable
64
+ );
65
+ } catch {
66
+ return false;
67
+ }
68
+ },
69
+ };
70
+ };
71
+
72
+ /** Data only; capture each shared ancestor prefix once, without copying the caller's values. */
73
+ const ownData = (input: unknown, names: ReadonlyArray<Key>) => {
74
+ if (typeof input !== "object" || input === null) return undefined;
75
+ const prototype: unknown = Object.getPrototypeOf(input);
76
+ if (
77
+ (prototype !== Object.prototype && prototype !== null) ||
78
+ Symbol.iterator in input
79
+ )
80
+ return undefined;
81
+ const keys = Reflect.ownKeys(input);
82
+ const properties = new Map(
83
+ keys.map(
84
+ (key) => [key, Object.getOwnPropertyDescriptor(input, key)!] as const,
85
+ ),
86
+ );
87
+ if (
88
+ [...properties.values()].some(
89
+ (property) => property === undefined || !("value" in property),
90
+ )
91
+ )
92
+ return undefined;
93
+ const absent = names.filter((name) => !properties.has(name));
94
+ if (absent.some((name) => name in input)) return undefined;
95
+ return {
96
+ properties,
97
+ matches() {
98
+ try {
99
+ if (
100
+ Object.getPrototypeOf(input) !== prototype ||
101
+ Symbol.iterator in input ||
102
+ absent.some((name) => name in input)
103
+ )
104
+ return false;
105
+ const currentKeys = Reflect.ownKeys(input);
106
+ return (
107
+ currentKeys.length === keys.length &&
108
+ keys.every((key, index) => {
109
+ const before = properties.get(key)!;
110
+ const after = Object.getOwnPropertyDescriptor(input, key);
111
+ return (
112
+ key === currentKeys[index] &&
113
+ after !== undefined &&
114
+ "value" in after &&
115
+ Object.is(before.value, after.value) &&
116
+ before.enumerable === after.enumerable &&
117
+ before.writable === after.writable &&
118
+ before.configurable === after.configurable
119
+ );
120
+ })
121
+ );
122
+ } catch {
123
+ return false;
124
+ }
125
+ },
126
+ };
127
+ };
128
+
129
+ /**
130
+ * Nested encoded-only evidence through unchanged direct object fields. Union and transformed
131
+ * nodes may be terminals; they never supply ancestor correspondence. No schema or thunk runs.
132
+ */
133
+ export const nestedEncodedInput = (ast: SchemaAST.AST) => {
134
+ if (!ancestor(ast)) return undefined;
135
+ const known = new Map<SchemaAST.AST, Node | undefined>();
136
+ const active = new Set<SchemaAST.AST>();
137
+ let cyclic = false;
138
+ const nodeOf = (part: SchemaAST.AST, root = false): Node | undefined => {
139
+ if (active.has(part)) {
140
+ cyclic = true;
141
+ return undefined;
142
+ }
143
+ if (known.has(part)) return known.get(part);
144
+ const terminal = root ? undefined : encodedInput(part);
145
+ if (terminal !== undefined) {
146
+ const node = { terminal, names: [], fields: [] };
147
+ known.set(part, node);
148
+ return node;
149
+ }
150
+ if (!ancestor(part)) {
151
+ known.set(part, undefined);
152
+ return undefined;
153
+ }
154
+ active.add(part);
155
+ const fields = part.propertySignatures.flatMap(({ name, type }) => {
156
+ const node = nodeOf(type);
157
+ return node === undefined ? [] : [[keyOf(name), node] as const];
158
+ });
159
+ active.delete(part);
160
+ const node =
161
+ fields.length === 0
162
+ ? undefined
163
+ : {
164
+ names: part.propertySignatures.map(({ name }) => keyOf(name)),
165
+ fields,
166
+ };
167
+ known.set(part, node);
168
+ return node;
169
+ };
170
+ const root = nodeOf(ast, true);
171
+ if (root === undefined || cyclic) return undefined;
172
+ const capture = (node: Node, input: unknown): Captured | undefined => {
173
+ if (node.terminal !== undefined) {
174
+ const evidence = node.terminal(input);
175
+ if (evidence === undefined) return undefined;
176
+ return {
177
+ select(output) {
178
+ if (!evidence.matches()) return undefined;
179
+ const key = evidence.witness(output);
180
+ if (key === undefined || !evidence.matches()) return undefined;
181
+ const written = writtenData(output, key);
182
+ return written === undefined
183
+ ? undefined
184
+ : {
185
+ key,
186
+ matches: evidence.matches,
187
+ written: written.matches,
188
+ };
189
+ },
190
+ };
191
+ }
192
+ const data = ownData(input, node.names);
193
+ if (data === undefined) return undefined;
194
+ const children = node.fields.flatMap(([name, child]) => {
195
+ const property = data.properties.get(name);
196
+ const captured =
197
+ property === undefined ? undefined : capture(child, property.value);
198
+ return captured === undefined ? [] : [[name, captured] as const];
199
+ });
200
+ if (children.length === 0) return undefined;
201
+ return {
202
+ select(output) {
203
+ try {
204
+ if (
205
+ !data.matches() ||
206
+ typeof output !== "object" ||
207
+ output === null ||
208
+ Symbol.iterator in output
209
+ )
210
+ return undefined;
211
+ for (const [name, child] of children) {
212
+ const written = writtenData(output, name);
213
+ if (written === undefined) continue;
214
+ const selected = child.select(written.value);
215
+ if (selected !== undefined) {
216
+ return {
217
+ key: selected.key,
218
+ matches: () => selected.matches() && data.matches(),
219
+ // Descendant reflection runs before rechecking the earlier links it may change.
220
+ written: () => selected.written() && written.matches(),
221
+ };
222
+ }
223
+ }
224
+ return undefined;
225
+ } catch {
226
+ return undefined;
227
+ }
228
+ },
229
+ };
230
+ };
231
+ return (input: unknown) => {
232
+ try {
233
+ const captured = capture(root, input);
234
+ if (captured === undefined) return undefined;
235
+ return {
236
+ select(output: unknown): Selected | undefined {
237
+ const selected = captured.select(output);
238
+ // Prove this fixed write once, then retain caller evidence while decoding fresh output.
239
+ return selected !== undefined &&
240
+ selected.written() &&
241
+ selected.matches()
242
+ ? { key: selected.key, matches: selected.matches }
243
+ : undefined;
244
+ },
245
+ };
246
+ } catch {
247
+ return undefined;
248
+ }
249
+ };
250
+ };