viell-effect-schema-http 0.5.0 → 0.6.1
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 +7 -6
- package/dist/http/Request.d.ts +33 -5
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +134 -90
- package/dist/http/Request.js.map +1 -1
- package/dist/http/internal/group.d.ts +10 -0
- package/dist/http/internal/group.d.ts.map +1 -0
- package/dist/http/internal/group.js +38 -0
- package/dist/http/internal/group.js.map +1 -0
- package/dist/http/internal/input.d.ts +7 -1
- package/dist/http/internal/input.d.ts.map +1 -1
- package/dist/http/internal/input.js +82 -2
- package/dist/http/internal/input.js.map +1 -1
- package/dist/http/internal/shape.d.ts +7 -1
- package/dist/http/internal/shape.d.ts.map +1 -1
- package/dist/http/internal/shape.js +40 -2
- package/dist/http/internal/shape.js.map +1 -1
- package/dist/http/internal/snapshot.d.ts +12 -0
- package/dist/http/internal/snapshot.d.ts.map +1 -0
- package/dist/http/internal/snapshot.js +179 -0
- package/dist/http/internal/snapshot.js.map +1 -0
- package/package.json +1 -1
- package/src/http/Request.ts +81 -19
- package/src/http/internal/group.ts +60 -0
- package/src/http/internal/input.ts +111 -2
- package/src/http/internal/shape.ts +70 -4
- package/src/http/internal/snapshot.ts +208 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import type { SchemaAST } from "effect";
|
|
2
|
+
|
|
3
|
+
interface Snapshot {
|
|
4
|
+
readonly value: unknown;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** Static visible sides only: capturing evidence must never evaluate a suspended schema. */
|
|
8
|
+
const sidesOf = (
|
|
9
|
+
types: ReadonlyArray<SchemaAST.AST>,
|
|
10
|
+
): ReadonlyArray<SchemaAST.AST> | undefined => {
|
|
11
|
+
const sides: SchemaAST.AST[] = [];
|
|
12
|
+
for (const type of types) {
|
|
13
|
+
if (type._tag === "Suspend") return undefined;
|
|
14
|
+
const inner = sidesOf([
|
|
15
|
+
...(type._tag === "Union" ? type.types : []),
|
|
16
|
+
...(type.encoding ?? []).map(({ to }) => to),
|
|
17
|
+
]);
|
|
18
|
+
if (inner === undefined) return undefined;
|
|
19
|
+
sides.push(type, ...inner);
|
|
20
|
+
}
|
|
21
|
+
return sides;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const describe = (types: ReadonlyArray<SchemaAST.AST>) => {
|
|
25
|
+
const sides = sidesOf(types);
|
|
26
|
+
if (sides === undefined) return undefined;
|
|
27
|
+
const objects = sides.filter((side) => side._tag === "Objects");
|
|
28
|
+
const arrays = sides.filter((side) => side._tag === "Arrays");
|
|
29
|
+
const fields = objects.flatMap((side) => side.propertySignatures);
|
|
30
|
+
const rest = [
|
|
31
|
+
...objects.flatMap((side) => side.indexSignatures.map(({ type }) => type)),
|
|
32
|
+
...arrays.flatMap((side) => side.rest),
|
|
33
|
+
];
|
|
34
|
+
const children = new Map<PropertyKey, ReadonlyArray<SchemaAST.AST>>();
|
|
35
|
+
const onlyRest =
|
|
36
|
+
fields.length === 0 && arrays.every((side) => side.elements.length === 0);
|
|
37
|
+
return {
|
|
38
|
+
unsafe:
|
|
39
|
+
sides.some(({ _tag }) =>
|
|
40
|
+
["Any", "Unknown", "ObjectKeyword"].includes(_tag),
|
|
41
|
+
) ||
|
|
42
|
+
objects.some(
|
|
43
|
+
(side) =>
|
|
44
|
+
side.propertySignatures.length === 0 &&
|
|
45
|
+
side.indexSignatures.length === 0,
|
|
46
|
+
) ||
|
|
47
|
+
((objects.length > 0 || arrays.length > 0) &&
|
|
48
|
+
sides.some(({ _tag }) => _tag === "Declaration")),
|
|
49
|
+
visible: objects.length > 0 || arrays.length > 0,
|
|
50
|
+
names: fields.map(({ name }) => name),
|
|
51
|
+
child(key: PropertyKey) {
|
|
52
|
+
if (onlyRest) return rest;
|
|
53
|
+
const known = children.get(key);
|
|
54
|
+
if (known !== undefined) return known;
|
|
55
|
+
const fixed = arrays.flatMap((side) => {
|
|
56
|
+
const element =
|
|
57
|
+
typeof key === "symbol" ? undefined : side.elements[Number(key)];
|
|
58
|
+
return element === undefined ? [] : [element];
|
|
59
|
+
});
|
|
60
|
+
if (!fields.some(({ name }) => name === key) && fixed.length === 0)
|
|
61
|
+
return rest;
|
|
62
|
+
const inner = [
|
|
63
|
+
...fields.filter(({ name }) => name === key).map(({ type }) => type),
|
|
64
|
+
...fixed,
|
|
65
|
+
...rest,
|
|
66
|
+
];
|
|
67
|
+
children.set(key, inner);
|
|
68
|
+
return inner;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Copy visible data before callbacks can mutate it. Opaque values keep their identity. Accessors,
|
|
74
|
+
* inherited fields, shared references, open or declared/structural mixtures, nonplain visible
|
|
75
|
+
* objects and suspended structure provide no safe snapshot. Reuse only schema metadata; no caller
|
|
76
|
+
* value belongs in the cache.
|
|
77
|
+
*/
|
|
78
|
+
export const snapshotter = (schemaTypes: ReadonlyArray<SchemaAST.AST>) => {
|
|
79
|
+
const metadata = new Map<
|
|
80
|
+
ReadonlyArray<SchemaAST.AST>,
|
|
81
|
+
ReturnType<typeof describe>
|
|
82
|
+
>();
|
|
83
|
+
const copy = (
|
|
84
|
+
types: ReadonlyArray<SchemaAST.AST>,
|
|
85
|
+
value: unknown,
|
|
86
|
+
seen: Set<object>,
|
|
87
|
+
arrayIndices: () => ReadonlyArray<readonly [string, number]> | undefined,
|
|
88
|
+
): Snapshot | undefined => {
|
|
89
|
+
if (typeof value !== "object" || value === null) return { value };
|
|
90
|
+
if (seen.has(value)) return undefined;
|
|
91
|
+
seen.add(value);
|
|
92
|
+
if (!metadata.has(types)) metadata.set(types, describe(types));
|
|
93
|
+
const shape = metadata.get(types);
|
|
94
|
+
if (shape === undefined || shape.unsafe) return undefined;
|
|
95
|
+
if (!shape.visible) return { value };
|
|
96
|
+
const list = Array.isArray(value);
|
|
97
|
+
const prototype: unknown = Object.getPrototypeOf(value);
|
|
98
|
+
if (
|
|
99
|
+
(list
|
|
100
|
+
? prototype !== Array.prototype
|
|
101
|
+
: prototype !== Object.prototype && prototype !== null) ||
|
|
102
|
+
(!list && Symbol.iterator in value)
|
|
103
|
+
)
|
|
104
|
+
return undefined;
|
|
105
|
+
const descriptors: Record<PropertyKey, PropertyDescriptor> =
|
|
106
|
+
Object.getOwnPropertyDescriptors(value);
|
|
107
|
+
if (list) {
|
|
108
|
+
const length = descriptors.length!.value as number;
|
|
109
|
+
const inherited = arrayIndices();
|
|
110
|
+
if (
|
|
111
|
+
inherited === undefined ||
|
|
112
|
+
inherited.some(
|
|
113
|
+
([key, index]) => index < length && !Object.hasOwn(descriptors, key),
|
|
114
|
+
)
|
|
115
|
+
)
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
if (
|
|
119
|
+
shape.names.some(
|
|
120
|
+
(name) => !Object.hasOwn(descriptors, name) && name in value,
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
return undefined;
|
|
124
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
125
|
+
const descriptor = descriptors[key]!;
|
|
126
|
+
if (!("value" in descriptor)) return undefined;
|
|
127
|
+
const next = copy(shape.child(key), descriptor.value, seen, arrayIndices);
|
|
128
|
+
if (next === undefined) return undefined;
|
|
129
|
+
descriptor.value = next.value;
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
value: list
|
|
133
|
+
? Object.defineProperties([], descriptors)
|
|
134
|
+
: Object.create(prototype as object | null, descriptors),
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
const matches = (
|
|
138
|
+
types: ReadonlyArray<SchemaAST.AST>,
|
|
139
|
+
captured: unknown,
|
|
140
|
+
current: unknown,
|
|
141
|
+
): boolean => {
|
|
142
|
+
if (Object.is(captured, current)) return true;
|
|
143
|
+
if (
|
|
144
|
+
typeof captured !== "object" ||
|
|
145
|
+
captured === null ||
|
|
146
|
+
typeof current !== "object" ||
|
|
147
|
+
current === null ||
|
|
148
|
+
Array.isArray(captured) !== Array.isArray(current) ||
|
|
149
|
+
Object.getPrototypeOf(captured) !== Object.getPrototypeOf(current)
|
|
150
|
+
)
|
|
151
|
+
return false;
|
|
152
|
+
const shape = metadata.get(types);
|
|
153
|
+
// Opaque values must retain their identity; compare only captured visible data structurally.
|
|
154
|
+
if (shape === undefined || shape.unsafe || !shape.visible) return false;
|
|
155
|
+
const keys = Reflect.ownKeys(captured);
|
|
156
|
+
const properties: Record<PropertyKey, PropertyDescriptor> =
|
|
157
|
+
Object.getOwnPropertyDescriptors(current);
|
|
158
|
+
const currentKeys = Reflect.ownKeys(properties);
|
|
159
|
+
if (keys.length !== currentKeys.length) return false;
|
|
160
|
+
return keys.every((key, index) => {
|
|
161
|
+
if (key !== currentKeys[index]) return false;
|
|
162
|
+
const before = Object.getOwnPropertyDescriptor(captured, key)!;
|
|
163
|
+
const after = properties[key];
|
|
164
|
+
return (
|
|
165
|
+
after !== undefined &&
|
|
166
|
+
"value" in after &&
|
|
167
|
+
before.enumerable === after.enumerable &&
|
|
168
|
+
before.configurable === after.configurable &&
|
|
169
|
+
before.writable === after.writable &&
|
|
170
|
+
matches(shape.child(key), before.value, after.value)
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
return (input: unknown) => {
|
|
175
|
+
// Inspect inherited names once per capture, without walking potentially enormous sparse holes.
|
|
176
|
+
let indices: ReadonlyArray<readonly [string, number]> | undefined;
|
|
177
|
+
const arrayIndices = () => {
|
|
178
|
+
if (Object.getPrototypeOf(Array.prototype) !== Object.prototype)
|
|
179
|
+
return undefined;
|
|
180
|
+
return (indices ??= [Array.prototype, Object.prototype].flatMap(
|
|
181
|
+
(prototype) =>
|
|
182
|
+
Reflect.ownKeys(prototype).flatMap((key) => {
|
|
183
|
+
if (typeof key !== "string") return [];
|
|
184
|
+
const index = Number(key);
|
|
185
|
+
return Number.isInteger(index) &&
|
|
186
|
+
index >= 0 &&
|
|
187
|
+
String(index) === key
|
|
188
|
+
? [[key, index] as const]
|
|
189
|
+
: [];
|
|
190
|
+
}),
|
|
191
|
+
));
|
|
192
|
+
};
|
|
193
|
+
const captured = copy(schemaTypes, input, new Set(), arrayIndices);
|
|
194
|
+
return captured === undefined
|
|
195
|
+
? undefined
|
|
196
|
+
: {
|
|
197
|
+
value: captured.value,
|
|
198
|
+
matches(current: unknown) {
|
|
199
|
+
try {
|
|
200
|
+
return matches(schemaTypes, captured.value, current);
|
|
201
|
+
} catch {
|
|
202
|
+
// Failed reflection provides no stable evidence; no schema callback runs here.
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
};
|