viell-effect-schema-http 0.15.0 → 0.16.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.
@@ -1,5 +1,6 @@
1
1
  import type { SchemaAST } from "effect";
2
2
  import { encodedInput } from "./encoded-input.js";
3
+ import { isNonObjectAlternative } from "./primitive.js";
3
4
 
4
5
  type CaptureTerminal = NonNullable<ReturnType<typeof encodedInput>>;
5
6
  type Key = string | symbol;
@@ -31,6 +32,36 @@ interface Captured {
31
32
  const keyOf = (name: PropertyKey): Key =>
32
33
  typeof name === "number" ? String(name) : name;
33
34
 
35
+ /** Canonical own positions only: symbols and index-looking metadata never become array cells. */
36
+ const arrayIndex = (key: Key, length: number): number | undefined => {
37
+ if (typeof key !== "string") return undefined;
38
+ const index = Number(key);
39
+ return Number.isInteger(index) &&
40
+ index >= 0 &&
41
+ index < length &&
42
+ String(index) === key
43
+ ? index
44
+ : undefined;
45
+ };
46
+
47
+ const arrayIterator: unknown = Object.getOwnPropertyDescriptor(
48
+ Array.prototype,
49
+ Symbol.iterator,
50
+ )?.value;
51
+
52
+ const nativeIteratorUnchanged = (): boolean => {
53
+ const current = Object.getOwnPropertyDescriptor(
54
+ Array.prototype,
55
+ Symbol.iterator,
56
+ );
57
+ return (
58
+ typeof arrayIterator === "function" &&
59
+ current !== undefined &&
60
+ "value" in current &&
61
+ current.value === arrayIterator
62
+ );
63
+ };
64
+
34
65
  /** Retain the actual enumerable output link or write, including its object identity. */
35
66
  const writtenData = (output: unknown, key: Key, array = false) => {
36
67
  if (
@@ -69,7 +100,11 @@ const writtenData = (output: unknown, key: Key, array = false) => {
69
100
  };
70
101
 
71
102
  /** Data only; capture each shared ancestor prefix once, without copying the caller's values. */
72
- const ownData = (input: unknown, names: ReadonlyArray<Key>, array = false) => {
103
+ const ownData = (
104
+ input: unknown,
105
+ names: ReadonlyArray<Key>,
106
+ array: false | "caller" | "output" = false,
107
+ ) => {
73
108
  if (typeof input !== "object" || input === null) return undefined;
74
109
  const prototype: unknown = Object.getPrototypeOf(input);
75
110
  if (
@@ -90,6 +125,12 @@ const ownData = (input: unknown, names: ReadonlyArray<Key>, array = false) => {
90
125
  )
91
126
  )
92
127
  return undefined;
128
+ const ownIterator = properties.get(Symbol.iterator);
129
+ const iteratorAllowed = () =>
130
+ ownIterator === undefined ||
131
+ (array === "caller" &&
132
+ ownIterator.value === arrayIterator &&
133
+ nativeIteratorUnchanged());
93
134
  const length: unknown = array ? properties.get("length")?.value : undefined;
94
135
  if (
95
136
  array &&
@@ -97,17 +138,10 @@ const ownData = (input: unknown, names: ReadonlyArray<Key>, array = false) => {
97
138
  !Number.isInteger(length) ||
98
139
  length < 0 ||
99
140
  length > 0xffff_ffff ||
100
- properties.has(Symbol.iterator) ||
101
- keys.filter((key) => {
102
- if (typeof key !== "string") return false;
103
- const index = Number(key);
104
- return (
105
- Number.isInteger(index) &&
106
- index >= 0 &&
107
- index < length &&
108
- String(index) === key
109
- );
110
- }).length !== length)
141
+ !iteratorAllowed() ||
142
+ (array === "output" &&
143
+ keys.filter((key) => arrayIndex(key, length) !== undefined).length !==
144
+ length))
111
145
  )
112
146
  return undefined;
113
147
  const absent = names.filter((name) => !properties.has(name));
@@ -120,6 +154,7 @@ const ownData = (input: unknown, names: ReadonlyArray<Key>, array = false) => {
120
154
  if (
121
155
  Object.getPrototypeOf(input) !== prototype ||
122
156
  (array ? !Array.isArray(input) : Symbol.iterator in input) ||
157
+ (array && !iteratorAllowed()) ||
123
158
  absent.some((name) => name in input)
124
159
  )
125
160
  return false;
@@ -148,7 +183,7 @@ const ownData = (input: unknown, names: ReadonlyArray<Key>, array = false) => {
148
183
  };
149
184
 
150
185
  /**
151
- * Nested encoded-only evidence through unchanged object/union fields and dense array positions.
186
+ * Nested encoded-only evidence through unchanged object/union fields and own array positions.
152
187
  * A parent union contributes a field only when every declaring member uses the same schema.
153
188
  * No schema or thunk runs.
154
189
  */
@@ -194,18 +229,23 @@ export const nestedEncodedInput = (ast: SchemaAST.AST) => {
194
229
  }
195
230
  viewing.add(part);
196
231
  const fields = new Map<Key, SchemaAST.AST | undefined>();
232
+ let hasObjectMember = false;
197
233
  for (const member of part.types) {
234
+ if (isNonObjectAlternative(member)) continue;
198
235
  const declared = fieldsOf(member);
199
236
  if (declared === undefined) {
200
237
  viewing.delete(part);
201
238
  views.set(part, undefined);
202
239
  return undefined;
203
240
  }
241
+ hasObjectMember = true;
204
242
  for (const [name, type] of declared) mergeField(fields, name, type);
205
243
  }
206
244
  viewing.delete(part);
207
- views.set(part, fields);
208
- return fields;
245
+ // Match the terminal view: a primitive-only union never becomes an empty object view.
246
+ const view = hasObjectMember ? fields : undefined;
247
+ views.set(part, view);
248
+ return view;
209
249
  };
210
250
  const known = new Map<SchemaAST.AST, Node | undefined>();
211
251
  const active = new Set<SchemaAST.AST>();
@@ -286,10 +326,16 @@ export const nestedEncodedInput = (ast: SchemaAST.AST) => {
286
326
  };
287
327
  }
288
328
  const positions = node.array;
289
- const data = ownData(input, node.names, positions !== undefined);
329
+ const data = ownData(
330
+ input,
331
+ node.names,
332
+ positions === undefined ? false : "caller",
333
+ );
290
334
  if (data === undefined) return undefined;
291
335
  const fields = positions
292
- ? Array.from({ length: data.length! }, (_, index) => {
336
+ ? Array.from(data.properties.keys()).flatMap((name) => {
337
+ const index = arrayIndex(name, data.length!);
338
+ if (index === undefined) return [];
293
339
  const tail = Math.max(
294
340
  positions.elements.length,
295
341
  data.length! - Math.max(0, positions.rest.length - 1),
@@ -300,8 +346,8 @@ export const nestedEncodedInput = (ast: SchemaAST.AST) => {
300
346
  : index >= tail
301
347
  ? positions.rest[index - tail + 1]
302
348
  : positions.rest[0];
303
- return child === undefined ? [] : [[String(index), child] as const];
304
- }).flat()
349
+ return child === undefined ? [] : [[name, child] as const];
350
+ })
305
351
  : node.fields;
306
352
  const children = fields.flatMap(([name, child]) => {
307
353
  const property = data.properties.get(name);
@@ -321,7 +367,7 @@ export const nestedEncodedInput = (ast: SchemaAST.AST) => {
321
367
  )
322
368
  return undefined;
323
369
  const arrayOutput = positions
324
- ? ownData(output, node.names, true)
370
+ ? ownData(output, node.names, "output")
325
371
  : undefined;
326
372
  if (
327
373
  positions &&
@@ -0,0 +1,20 @@
1
+ import type { SchemaAST } from "effect";
2
+
3
+ const nonObjectTags: ReadonlySet<SchemaAST.AST["_tag"]> = new Set([
4
+ "String",
5
+ "Number",
6
+ "Boolean",
7
+ "BigInt",
8
+ "Symbol",
9
+ "UniqueSymbol",
10
+ "Literal",
11
+ "Enum",
12
+ "TemplateLiteral",
13
+ "Null",
14
+ "Undefined",
15
+ "Never",
16
+ ]);
17
+
18
+ /** Unencoded union members that cannot consume an actual object; Void deliberately does not qualify. */
19
+ export const isNonObjectAlternative = (ast: SchemaAST.AST): boolean =>
20
+ ast.encoding === undefined && nonObjectTags.has(ast._tag);