viell-effect-schema-http 0.4.0 → 0.6.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.
@@ -20,6 +20,8 @@ export interface Taken {
20
20
  readonly fields: ReadonlySet<PropertyKey>;
21
21
  /** The compared names schemas with no union allow. Each settles a raw value under its names. */
22
22
  readonly settled: ReadonlySet<string>;
23
+ /** Actual claims per applied binding; absent on legacy custom drafts, which cannot recover. */
24
+ readonly certificates?: ReadonlyMap<object, ReadonlySet<PropertyKey>>;
23
25
  }
24
26
 
25
27
  /** What a schema's encoded side states, in the schema's own spelling. */
@@ -253,11 +255,44 @@ export const others = (
253
255
  export const taking = (
254
256
  taken: Taken | undefined,
255
257
  own: ReadonlySet<PropertyKey>,
258
+ token: object,
256
259
  ): Taken => ({
257
260
  fields: new Set([...(taken?.fields ?? []), ...own]),
258
261
  settled: taken?.settled ?? new Set(),
262
+ ...(taken?.certificates === undefined
263
+ ? {}
264
+ : { certificates: new Map([...taken.certificates, [token, own]]) }),
259
265
  });
260
266
 
267
+ /** Replace one binding's claim only if no previously certified name disappears. */
268
+ export const retaking = (
269
+ taken: Taken,
270
+ own: ReadonlySet<PropertyKey>,
271
+ token: object,
272
+ ): Taken | undefined => {
273
+ if (taken.certificates === undefined || !taken.certificates.has(token))
274
+ return undefined;
275
+ const certificates = new Map(taken.certificates);
276
+ certificates.set(token, own);
277
+ const fields = new Set(
278
+ Array.from(certificates.values()).flatMap((set) => [...set]),
279
+ );
280
+ return Array.from(taken.fields).every((name) => fields.has(name))
281
+ ? { ...taken, fields, certificates }
282
+ : undefined;
283
+ };
284
+
285
+ /** Names an actual other binding took, never merely names it permits. */
286
+ export const certifiedElsewhere = (
287
+ taken: Taken,
288
+ token: object,
289
+ ): ReadonlySet<PropertyKey> =>
290
+ new Set(
291
+ Array.from(taken.certificates ?? []).flatMap(([binding, fields]) =>
292
+ binding === token ? [] : [...fields],
293
+ ),
294
+ );
295
+
261
296
  /**
262
297
  * What went into `settle` and is gone from what came out, in the spelling it went in with. A schema
263
298
  * can write less than it was given, and that must not pass as a group that was built.
@@ -275,7 +310,7 @@ export const lost = (
275
310
  };
276
311
 
277
312
  /** Only stable absent/data properties prove the caller did not supply this field. */
278
- const omitted = (input: object, name: string): boolean => {
313
+ const omitted = (input: object, name: PropertyKey): boolean => {
279
314
  const property = Object.getOwnPropertyDescriptor(input, name);
280
315
  return property === undefined
281
316
  ? !(name in input)
@@ -309,46 +344,68 @@ export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
309
344
  chooses,
310
345
  };
311
346
  // A group transformation or declaration can change names or construct in its own way.
312
- const constructorNames =
347
+ const directNames =
313
348
  contract.ast._tag === "Objects" && contract.ast.encoding === undefined
314
- ? contract.ast.propertySignatures.flatMap(({ name, type }) =>
315
- typeof name === "string" &&
316
- type.context?.constructorDefault !== undefined
317
- ? [name]
318
- : [],
349
+ ? contract.ast.propertySignatures.flatMap(({ name }) =>
350
+ typeof name === "string" ? [name] : [],
319
351
  )
320
352
  : [];
353
+ const end = Excess.encodedEnd(contract.ast);
354
+ const closedEndpoints =
355
+ contract.ast._tag === "Objects" &&
356
+ contract.ast.indexSignatures.length === 0 &&
357
+ end._tag === "Objects" &&
358
+ end.indexSignatures.length === 0
359
+ ? [contract.ast, end]
360
+ : [];
361
+ const encodedNames =
362
+ end._tag === "Objects"
363
+ ? end.propertySignatures.flatMap(({ name }) =>
364
+ typeof name === "string" ? [name] : [],
365
+ )
366
+ : [];
367
+ /** No caller field can explain generated output when both closed endpoints are absent. */
368
+ const empty = (input: object): boolean => {
369
+ const prototype: unknown = Object.getPrototypeOf(input);
370
+ return (
371
+ closedEndpoints.length > 0 &&
372
+ (prototype === Object.prototype || prototype === null) &&
373
+ Reflect.ownKeys(input).length === 0 &&
374
+ closedEndpoints.every((endpoint) =>
375
+ endpoint.propertySignatures.every(
376
+ ({ name }) => omitted(input, name) && !(name in input),
377
+ ),
378
+ )
379
+ );
380
+ };
321
381
  /** Snapshot caller omission before preparation can run a default or encoder. */
322
382
  const candidates = (
323
383
  input: unknown,
324
384
  edits: ReadonlySet<string> | undefined,
325
385
  ): ReadonlyArray<string> =>
326
386
  edits !== undefined && typeof input === "object" && input !== null
327
- ? constructorNames.filter(
328
- (name) => edits.has(fold(name)) && omitted(input, name),
329
- )
387
+ ? (contract.ast.encoding === undefined
388
+ ? directNames
389
+ : empty(input)
390
+ ? encodedNames
391
+ : []
392
+ ).filter((name) => edits.has(fold(name)) && omitted(input, name))
330
393
  : [];
331
- /** The successful first constructor actually supplied these edited, encoded fields. */
394
+ /** Preparation supplied these omitted, edited fields in the record it actually writes. */
332
395
  const filled = (
333
396
  names: ReadonlyArray<string>,
334
- constructed: unknown,
335
397
  output: unknown,
336
398
  ): ReadonlyArray<string> => {
337
399
  if (
338
- typeof constructed !== "object" ||
339
- constructed === null ||
340
400
  typeof output !== "object" ||
341
- output === null
401
+ output === null ||
402
+ Symbol.iterator in output
342
403
  )
343
404
  return [];
344
405
  return names
345
406
  .filter((name) => {
346
- const made = Object.getOwnPropertyDescriptor(constructed, name);
347
407
  const encoded = Object.getOwnPropertyDescriptor(output, name);
348
408
  return (
349
- made !== undefined &&
350
- "value" in made &&
351
- made.value !== undefined &&
352
409
  encoded?.enumerable === true &&
353
410
  "value" in encoded &&
354
411
  encoded.value !== undefined