@effect-dynamodb/schema 1.39.0 → 1.41.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 (122) hide show
  1. package/dist/Aggregate.d.ts +11 -4
  2. package/dist/Aggregate.d.ts.map +1 -1
  3. package/dist/Aggregate.js +12 -2
  4. package/dist/Aggregate.js.map +1 -1
  5. package/dist/DynamoModel.d.ts +48 -7
  6. package/dist/DynamoModel.d.ts.map +1 -1
  7. package/dist/DynamoModel.js +34 -16
  8. package/dist/DynamoModel.js.map +1 -1
  9. package/dist/DynamoSchema.d.ts +25 -0
  10. package/dist/DynamoSchema.d.ts.map +1 -1
  11. package/dist/DynamoSchema.js.map +1 -1
  12. package/dist/Entity.d.ts +66 -11
  13. package/dist/Entity.d.ts.map +1 -1
  14. package/dist/Entity.js +261 -34
  15. package/dist/Entity.js.map +1 -1
  16. package/dist/Errors.d.ts +61 -4
  17. package/dist/Errors.d.ts.map +1 -1
  18. package/dist/Errors.js +83 -6
  19. package/dist/Errors.js.map +1 -1
  20. package/dist/KeyComposer.d.ts +196 -5
  21. package/dist/KeyComposer.d.ts.map +1 -1
  22. package/dist/KeyComposer.js +490 -54
  23. package/dist/KeyComposer.js.map +1 -1
  24. package/dist/VectorIndex.d.ts.map +1 -1
  25. package/dist/VectorIndex.js +5 -4
  26. package/dist/VectorIndex.js.map +1 -1
  27. package/dist/index.d.ts +1 -1
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +1 -1
  30. package/dist/index.js.map +1 -1
  31. package/dist/internal/AggregateEdges.d.ts +3 -3
  32. package/dist/internal/AggregateSchemas.d.ts +306 -14
  33. package/dist/internal/AggregateSchemas.d.ts.map +1 -1
  34. package/dist/internal/AggregateSchemas.js +733 -54
  35. package/dist/internal/AggregateSchemas.js.map +1 -1
  36. package/dist/internal/AggregateTypes.d.ts +396 -36
  37. package/dist/internal/AggregateTypes.d.ts.map +1 -1
  38. package/dist/internal/CompositeCodec.d.ts +298 -8
  39. package/dist/internal/CompositeCodec.d.ts.map +1 -1
  40. package/dist/internal/CompositeCodec.js +940 -34
  41. package/dist/internal/CompositeCodec.js.map +1 -1
  42. package/dist/internal/EffectInstances.d.ts +87 -0
  43. package/dist/internal/EffectInstances.d.ts.map +1 -0
  44. package/dist/internal/EffectInstances.js +662 -0
  45. package/dist/internal/EffectInstances.js.map +1 -0
  46. package/dist/internal/EntityPacked.d.ts.map +1 -1
  47. package/dist/internal/EntityPacked.js +5 -4
  48. package/dist/internal/EntityPacked.js.map +1 -1
  49. package/dist/internal/EntitySchemas.d.ts +239 -30
  50. package/dist/internal/EntitySchemas.d.ts.map +1 -1
  51. package/dist/internal/EntitySchemas.js +1600 -197
  52. package/dist/internal/EntitySchemas.js.map +1 -1
  53. package/dist/internal/EntityTypes.d.ts +114 -25
  54. package/dist/internal/EntityTypes.d.ts.map +1 -1
  55. package/dist/internal/FailingAsBefore.d.ts +25 -0
  56. package/dist/internal/FailingAsBefore.d.ts.map +1 -0
  57. package/dist/internal/FailingAsBefore.js +42 -0
  58. package/dist/internal/FailingAsBefore.js.map +1 -0
  59. package/dist/internal/InstanceReads.d.ts +40 -0
  60. package/dist/internal/InstanceReads.d.ts.map +1 -0
  61. package/dist/internal/InstanceReads.js +183 -0
  62. package/dist/internal/InstanceReads.js.map +1 -0
  63. package/dist/internal/KeyReadings.d.ts +30 -0
  64. package/dist/internal/KeyReadings.d.ts.map +1 -0
  65. package/dist/internal/KeyReadings.js +99 -0
  66. package/dist/internal/KeyReadings.js.map +1 -0
  67. package/dist/internal/KeyText136.d.ts +4 -2
  68. package/dist/internal/KeyText136.d.ts.map +1 -1
  69. package/dist/internal/KeyText136.js +46 -13
  70. package/dist/internal/KeyText136.js.map +1 -1
  71. package/dist/internal/LostValues.d.ts +50 -0
  72. package/dist/internal/LostValues.d.ts.map +1 -0
  73. package/dist/internal/LostValues.js +465 -0
  74. package/dist/internal/LostValues.js.map +1 -0
  75. package/dist/internal/NumericKey.d.ts +21 -2
  76. package/dist/internal/NumericKey.d.ts.map +1 -1
  77. package/dist/internal/NumericKey.js +19 -1
  78. package/dist/internal/NumericKey.js.map +1 -1
  79. package/dist/internal/ObjectKeyText.d.ts +29 -0
  80. package/dist/internal/ObjectKeyText.d.ts.map +1 -0
  81. package/dist/internal/ObjectKeyText.js +50 -0
  82. package/dist/internal/ObjectKeyText.js.map +1 -0
  83. package/dist/internal/OwnNames.d.ts +205 -0
  84. package/dist/internal/OwnNames.d.ts.map +1 -0
  85. package/dist/internal/OwnNames.js +415 -0
  86. package/dist/internal/OwnNames.js.map +1 -0
  87. package/dist/internal/PackedCodec.d.ts +7 -1
  88. package/dist/internal/PackedCodec.d.ts.map +1 -1
  89. package/dist/internal/PackedCodec.js +83 -21
  90. package/dist/internal/PackedCodec.js.map +1 -1
  91. package/dist/internal/RestoredKeyText.d.ts +10 -0
  92. package/dist/internal/RestoredKeyText.d.ts.map +1 -0
  93. package/dist/internal/RestoredKeyText.js +50 -0
  94. package/dist/internal/RestoredKeyText.js.map +1 -0
  95. package/dist/internal/SchemaAccessors.d.ts +280 -1
  96. package/dist/internal/SchemaAccessors.d.ts.map +1 -1
  97. package/dist/internal/SchemaAccessors.js +1717 -1
  98. package/dist/internal/SchemaAccessors.js.map +1 -1
  99. package/dist/internal/SchemaIssue.d.ts +21 -0
  100. package/dist/internal/SchemaIssue.d.ts.map +1 -0
  101. package/dist/internal/SchemaIssue.js +34 -0
  102. package/dist/internal/SchemaIssue.js.map +1 -0
  103. package/dist/internal/SharedKeyAttribute.d.ts +60 -6
  104. package/dist/internal/SharedKeyAttribute.d.ts.map +1 -1
  105. package/dist/internal/SharedKeyAttribute.js +129 -13
  106. package/dist/internal/SharedKeyAttribute.js.map +1 -1
  107. package/dist/internal/SparseKey.d.ts +46 -0
  108. package/dist/internal/SparseKey.d.ts.map +1 -0
  109. package/dist/internal/SparseKey.js +69 -0
  110. package/dist/internal/SparseKey.js.map +1 -0
  111. package/dist/internal/StoredForm.d.ts +139 -0
  112. package/dist/internal/StoredForm.d.ts.map +1 -0
  113. package/dist/internal/StoredForm.js +503 -0
  114. package/dist/internal/StoredForm.js.map +1 -0
  115. package/dist/internal/ZOrderCurve.d.ts.map +1 -1
  116. package/dist/internal/ZOrderCurve.js +2 -1
  117. package/dist/internal/ZOrderCurve.js.map +1 -1
  118. package/dist/internal/ZoneKeyText.d.ts +55 -0
  119. package/dist/internal/ZoneKeyText.d.ts.map +1 -0
  120. package/dist/internal/ZoneKeyText.js +264 -0
  121. package/dist/internal/ZoneKeyText.js.map +1 -0
  122. package/package.json +4 -4
@@ -0,0 +1,415 @@
1
+ /**
2
+ * Looking a name up on an object the library builds or reads a definition from (#239).
3
+ *
4
+ * A name a definition or a payload supplies (a field, a path segment, an edge, a timestamp, the
5
+ * version) can be one every object inherits: `constructor`, `toString`, `valueOf`,
6
+ * `hasOwnProperty`, `__proto__`. On a plain object, `record[name]` and `name in record` then find
7
+ * `Object.prototype`'s member, and `record[name] = value` under `__proto__` replaces the object's
8
+ * prototype instead of holding the value. So the library's own maps (definition maps, encoded
9
+ * payloads, unmarshalled rows, a schema's fields) are read with {@link ownValue} /
10
+ * {@link hasOwnName} and written with {@link setOwn}.
11
+ *
12
+ * A caller's object is not read this way. Where the library reads it itself (a key's composites, a
13
+ * condition's or an update operand's record, `.within()` bounds, an aggregate's ids) it is read as
14
+ * 1.40.0 read it, `value[name]`: a getter, a class property, a prototype or a proxy answers, and a
15
+ * function there is given as 1.40.0 read it, but for a member every object inherits under a name
16
+ * `Object.prototype` holds (`constructor`, `toString`), which is never a value given (see
17
+ * {@link given}). A read that throws for a name the object doesn't hold is no value. A payload a
18
+ * schema decodes goes to Effect's parser as given, or as 1.40.0 copied it where the library adds to
19
+ * it first (a decoding default, a stamp, a fetched ref, a geo index's cells), and what the parser
20
+ * reads of it is the parser's to decide: under Effect 4.1.3, a declared name `Object.prototype`
21
+ * holds too only as the payload's own property.
22
+ */
23
+ import { Data, SchemaAST } from "effect";
24
+ /** Whether `record` holds `name` itself, not by inheritance. */
25
+ export const hasOwnName = (record, name) => record !== undefined && Object.hasOwn(record, name);
26
+ /**
27
+ * What `record` holds under `name` itself, `undefined` where it holds nothing there. Never a member
28
+ * every object inherits: a field named `constructor` that a map doesn't hold is not one.
29
+ */
30
+ export const ownValue = (record, name) => record !== undefined && Object.hasOwn(record, name) ? record[name] : undefined;
31
+ /**
32
+ * Why an attribute or a map entry named `__proto__` is refused (`EDD-9165`). The AWS SDK's
33
+ * marshaller assigns each attribute and entry to a plain object, and an assignment to `__proto__`
34
+ * sets that object's prototype instead, so it was never sent; stored by an update, every read drops
35
+ * it the same way, as its unmarshaller assigns too.
36
+ */
37
+ export const PROTO_ENTRY = 'an attribute or a map entry named "__proto__" is lost: the AWS SDK\'s marshaller and ' +
38
+ "unmarshaller assign each to a plain object, and assigning that name sets the object's " +
39
+ "prototype instead, so it is never sent, and one stored otherwise is never read back. Name it " +
40
+ "otherwise.";
41
+ /**
42
+ * Stores `value` under `name` as `record`'s own property. Assignment does that for every name but
43
+ * `__proto__`, where it would replace the prototype instead (or do nothing, for a value that isn't
44
+ * an object), so that one name is defined.
45
+ */
46
+ export const setOwn = (record, name, value) => {
47
+ if (name === "__proto__") {
48
+ Object.defineProperty(record, name, {
49
+ value,
50
+ writable: true,
51
+ enumerable: true,
52
+ configurable: true,
53
+ });
54
+ }
55
+ else
56
+ record[name] = value;
57
+ };
58
+ /**
59
+ * A caller's object threw when `field` was read from it, a name it doesn't hold itself but holds
60
+ * by inheritance (a getter of its class, say), read because the entity, aggregate or stream it was
61
+ * given to declares that name. Thrown where the read is, and failed with a `ValidationError` naming
62
+ * the field by the operation it was given to, never a defect (`Capacity.isRefusedValue`). Where the
63
+ * read is made for a key handed on (a geo search's filter), the error stands in the copy for the
64
+ * value, and the search fails with it when it reads it ({@link lackedByName}'s `keep`).
65
+ *
66
+ * Recognised by identity alone ({@link isUnreadableName}): only an error this module made is one.
67
+ * A caller's value carrying the same tag and fields (a stored map compared in a condition) is a
68
+ * value like any other.
69
+ */
70
+ export class UnreadableName extends Data.TaggedError("UnreadableName") {
71
+ }
72
+ /** Every {@link UnreadableName} this module made: what {@link isUnreadableName} recognises. */
73
+ const made = new WeakSet();
74
+ /** Is `u` an {@link UnreadableName} this module made? By identity, never by its tag or fields. */
75
+ export const isUnreadableName = (u) => typeof u === "object" && u !== null && made.has(u);
76
+ const unreadable = (field, thrown) => {
77
+ const error = new UnreadableName({
78
+ field: String(field),
79
+ thrown,
80
+ message: `Reading "${String(field)}" from the object given threw: ` +
81
+ `${thrown instanceof Error ? thrown.message : String(thrown)}. The object doesn't hold ` +
82
+ `"${String(field)}" itself but answers for it by inheritance (a getter of its class, a ` +
83
+ `prototype, a proxy), and it is a name the value is read for.`,
84
+ });
85
+ made.add(error);
86
+ return error;
87
+ };
88
+ /** Whether `u` is an object or a function: what `Object.hasOwn` and `in` take. */
89
+ const isObject = (u) => (typeof u === "object" && u !== null) || typeof u === "function";
90
+ /** Whether `name` is `in` `payload`, as 1.40.0 asked it; a proxy whose `has` trap throws holds nothing. */
91
+ const holds = (payload, name) => {
92
+ try {
93
+ return name in payload;
94
+ }
95
+ catch {
96
+ return false;
97
+ }
98
+ };
99
+ /**
100
+ * `payload[name]`, read as 1.40.0 read it: one read (one `get` of a proxy). A read that throws from
101
+ * the payload's own getter propagates, as on 1.40.0. One that throws for a name the payload doesn't
102
+ * hold at all (a strict proxy, throwing for every name it doesn't know) is no value, so such an
103
+ * object is read as a plain one; one that throws for a name it holds by inheritance (a getter of
104
+ * its class) is an {@link UnreadableName}.
105
+ */
106
+ const read = (payload, name) => {
107
+ try {
108
+ return payload[name];
109
+ }
110
+ catch (thrown) {
111
+ if (Object.hasOwn(payload, name))
112
+ throw thrown;
113
+ if (!holds(payload, name))
114
+ return undefined;
115
+ throw unreadable(name, thrown);
116
+ }
117
+ };
118
+ /**
119
+ * Whether `value`, read from a caller's object under `name`, is what every plain object inherits
120
+ * under that name: `Object.prototype`'s own member (`constructor`, `toString`, `valueOf`, …), or,
121
+ * under `__proto__`, `Object.prototype` itself. Judged on the value read alone, by identity: a
122
+ * class's `constructor` or its own `toString` is a value as 1.40.0 read it.
123
+ */
124
+ const isObjectPrototypeMember = (name, value) => name === "__proto__"
125
+ ? value === Object.prototype
126
+ : typeof value === "function" &&
127
+ Object.hasOwn(Object.prototype, name) &&
128
+ value === Object.prototype[name];
129
+ /**
130
+ * What a caller's payload gives under `name`, where the library reads it itself (a key's
131
+ * composites, an aggregate's ids and edges, `.within()` bounds' operators), read as 1.40.0 read it
132
+ * (`payload[name]`, one read, one `get` of a proxy): its own property, one its class gives it (a
133
+ * getter, a property of a prototype, a method), or one a proxy answers for, a function as much as
134
+ * anything else. Reading its own property is as it always was (a throw from its own getter
135
+ * propagates). A throw for a name it doesn't hold at all (a strict proxy) is no value; a throw from
136
+ * a getter it inherits is an {@link UnreadableName}. The one value not given is what every plain
137
+ * object inherits under the name ({@link isObjectPrototypeMember}: `Object.prototype.toString`
138
+ * under `toString`, `Object.prototype` under `__proto__`), which 1.40.0 took for a value of a field
139
+ * so named: such a name reads as absent. Only where the value read is that member is the payload
140
+ * asked anything more, whether it holds the name itself (`Object.hasOwn`): where it does, the value
141
+ * is given.
142
+ */
143
+ export const given = (payload, name) => {
144
+ // Anything but an object is read as 1.40.0 read it: `undefined` and `null` throw its TypeError.
145
+ if (!isObject(payload))
146
+ return payload[name];
147
+ const value = read(payload, name);
148
+ return isObjectPrototypeMember(name, value) && !Object.hasOwn(payload, name) ? undefined : value;
149
+ };
150
+ /**
151
+ * {@link given}, but never a function: where 1.40.0 took no function for a value (an aggregate's
152
+ * ids, `many` lists and sub-aggregates), and where the library reads a name 1.40.0 didn't read at
153
+ * all (a key composite its own entries lack, {@link keyEntries}), so a method of the payload's
154
+ * class is no value there.
155
+ */
156
+ export const givenData = (payload, name) => {
157
+ const value = given(payload, name);
158
+ return typeof value === "function" ? undefined : value;
159
+ };
160
+ /**
161
+ * What a caller's payload gives under `name`, where 1.40.0 asked `name in payload` and then read
162
+ * `payload[name]`: `{ value }` where it holds the name (as it asked, but never what every plain
163
+ * object inherits under it, {@link given}), `undefined` where it doesn't. Asked and read as 1.40.0
164
+ * asked and read it, once each.
165
+ */
166
+ export const givenEntry = (payload, name) => {
167
+ // Anything but an object is asked as 1.40.0 asked it, `name in payload`: a TypeError.
168
+ if (!isObject(payload)) {
169
+ const asked = payload;
170
+ return name in asked ? { value: asked[name] } : undefined;
171
+ }
172
+ if (!holds(payload, name))
173
+ return undefined;
174
+ const value = read(payload, name);
175
+ return isObjectPrototypeMember(name, value) && !Object.hasOwn(payload, name)
176
+ ? undefined
177
+ : { value };
178
+ };
179
+ /** Whether a caller's payload gives `name` ({@link givenEntry}), reading it once. */
180
+ export const givesName = (payload, name) => givenEntry(payload, name) !== undefined;
181
+ /** Whether `u` is an object a caller gives as a record of names: an object, not an array. */
182
+ export const isRecordObject = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
183
+ /**
184
+ * A caller's key as a plain object, read as 1.40.0 read it (`Object.entries`): its own enumerable
185
+ * properties, and nothing else of it, wherever they hold every one of `required` (the composites
186
+ * the key can't be composed without). Only where they don't, where 1.40.0 failed ("Missing
187
+ * composite attribute"), is each of `required` and `optional` they lack read by name
188
+ * ({@link lackedByName}), so a key given by `Object.create`, a class with getters or a proxy is
189
+ * read for its composites. `map` makes each value read the value held (a composite's key form).
190
+ */
191
+ export const keyEntries = (payload, required, optional = [], map = (_name, value) => value) => {
192
+ const copy = {};
193
+ const entries = Object.entries(payload);
194
+ for (let index = 0; index < entries.length; index++) {
195
+ const [name, value] = entries[index];
196
+ setOwn(copy, name, map(name, value));
197
+ }
198
+ return lacksAny(copy, required)
199
+ ? lackedByName(payload, copy, [...required, ...optional], map)
200
+ : copy;
201
+ };
202
+ /** Whether `copy` lacks any of `names` as its own property. */
203
+ export const lacksAny = (copy, names) => names.some((name) => !Object.hasOwn(copy, name));
204
+ /**
205
+ * `copy` (a copy of a caller's `payload`'s own properties), given each of `names` it lacks that the
206
+ * payload gives otherwise, read by name ({@link givenData}: from a prototype, a class's getter or a
207
+ * proxy, never a function there), `map`ped. Read only where 1.40.0 failed for the lack.
208
+ *
209
+ * With `keep`, a name whose read throws ({@link UnreadableName}) doesn't throw here: the copy holds a
210
+ * property of that name that throws it again when it's read (a key handed on to a search, whose read
211
+ * of it fails the search).
212
+ */
213
+ export const lackedByName = (payload, copy, names, map = (_name, value) => value, keep) => {
214
+ for (const name of names) {
215
+ if (Object.hasOwn(copy, name))
216
+ continue;
217
+ let value;
218
+ try {
219
+ value = givenData(payload, name);
220
+ }
221
+ catch (error) {
222
+ if (keep === undefined || !isUnreadableName(error))
223
+ throw error;
224
+ Object.defineProperty(copy, name, {
225
+ get: () => {
226
+ throw error;
227
+ },
228
+ enumerable: true,
229
+ configurable: true,
230
+ });
231
+ continue;
232
+ }
233
+ if (value !== undefined)
234
+ setOwn(copy, name, map(name, value));
235
+ }
236
+ return copy;
237
+ };
238
+ /**
239
+ * A plain object holding what a value the library decoded gives (an aggregate's node, as Effect's
240
+ * parser built it): its own enumerable properties, and each of `names` (the fields its schema
241
+ * declares) it gives otherwise ({@link givenData}). Never a caller's object: the parser built it.
242
+ */
243
+ export const declaredCopy = (value, names) => {
244
+ const copy = {};
245
+ const entries = Object.entries(value);
246
+ for (let index = 0; index < entries.length; index++) {
247
+ const [name, held] = entries[index];
248
+ setOwn(copy, name, held);
249
+ }
250
+ for (const name of names) {
251
+ if (Object.hasOwn(copy, name))
252
+ continue;
253
+ const held = givenData(value, name);
254
+ if (held !== undefined)
255
+ setOwn(copy, name, held);
256
+ }
257
+ return copy;
258
+ };
259
+ /**
260
+ * The class `value` is an instance of: the nearest of its prototypes holding a function as its own
261
+ * `constructor` value, as `value.constructor` reads it for an instance. Never `value`'s own property
262
+ * of that name, nor a prototype's that is no function or is a getter: either is a field of a payload
263
+ * given by `Object.create` or by a class with getters, not the class, and is never read. `undefined`
264
+ * for an object without a prototype.
265
+ */
266
+ export const classOf = (value) => {
267
+ for (let prototype = Object.getPrototypeOf(value); prototype !== null && typeof prototype === "object"; prototype = Object.getPrototypeOf(prototype)) {
268
+ const made = Object.getOwnPropertyDescriptor(prototype, "constructor")?.value;
269
+ if (typeof made === "function")
270
+ return made;
271
+ }
272
+ return undefined;
273
+ };
274
+ /**
275
+ * Whether every value of `stored` (an encoded form) stores something under its name, by a form
276
+ * this code models exactly: text (`String`, a literal, a template literal, an enum), a number, a
277
+ * boolean, a `bigint`, `null`, a map (a struct or a record, a class's encoded side among them), a
278
+ * list (an array or a tuple), a suspended one followed, or a union of only such forms. A
279
+ * transformation is judged by its encoded side, which Effect validates every encoded value
280
+ * against. Anything else is not known to: `Schema.declare` and the declarations built on it
281
+ * (`Option`, `Date`, a set), `Unknown`, `Any`, `Undefined`, `Void`, `Never`, a symbol, an object
282
+ * keyword, a union with any of them, and a suspension that leads back to itself. A value of
283
+ * those may encode to `undefined`, which the marshaller stores as nothing
284
+ * (`removeUndefinedValues`).
285
+ */
286
+ const storesAlways = (stored, suspended) => {
287
+ if (SchemaAST.isOptional(stored))
288
+ return false;
289
+ if (SchemaAST.isString(stored) ||
290
+ SchemaAST.isNumber(stored) ||
291
+ SchemaAST.isBoolean(stored) ||
292
+ SchemaAST.isBigInt(stored) ||
293
+ SchemaAST.isLiteral(stored) ||
294
+ SchemaAST.isTemplateLiteral(stored) ||
295
+ SchemaAST.isEnum(stored) ||
296
+ SchemaAST.isNull(stored) ||
297
+ SchemaAST.isObjects(stored) ||
298
+ SchemaAST.isArrays(stored)) {
299
+ return true;
300
+ }
301
+ if (SchemaAST.isSuspend(stored)) {
302
+ if (suspended.has(stored))
303
+ return false;
304
+ return storesAlways(SchemaAST.toEncoded(stored.thunk()), new Set([...suspended, stored]));
305
+ }
306
+ if (SchemaAST.isUnion(stored)) {
307
+ return stored.types.every((member) => storesAlways(SchemaAST.toEncoded(member), suspended));
308
+ }
309
+ return false;
310
+ };
311
+ /**
312
+ * Whether every value of `ast` is certain to store something under its name: it is required, and
313
+ * its stored (encoded) form is one this code models exactly with no value that stores nothing
314
+ * ({@link storesAlways}). One that may be left out, may be `undefined` (`UndefinedOr`,
315
+ * `NullishOr`, a decoding default), or isn't modelled exactly (`Schema.declare`, `Option`, a
316
+ * union with such a member) lets a write store nothing there, as 1.40.0 stored it: a definition
317
+ * naming `__proto__` there loads, and a write giving it a value is refused when it's sent.
318
+ */
319
+ export const isRequiredStored = (ast) => storesAlways(SchemaAST.toEncoded(ast), new Set());
320
+ /**
321
+ * The stored form of `ast` where every value stored there is certain to be a map: `ast` is
322
+ * required, and its stored (encoded) form is a struct, a suspended one followed. `undefined` where
323
+ * a value stored there may be no map, or may be no value at all: an optional or an `optionalKey`
324
+ * position, a nullable one, a union (one alternative may be the struct, another not), a list or a
325
+ * tuple (an empty one holds none), a record, text (`Schema.fromJsonString`), a declaration with no
326
+ * encoding (`Schema.Option`).
327
+ */
328
+ export const certainStruct = (ast, seen = new Set()) => {
329
+ const stored = SchemaAST.toEncoded(ast);
330
+ if (SchemaAST.isOptional(stored) || seen.has(stored))
331
+ return undefined;
332
+ seen.add(stored);
333
+ if (SchemaAST.isSuspend(stored))
334
+ return certainStruct(stored.thunk(), seen);
335
+ return SchemaAST.isObjects(stored) ? stored : undefined;
336
+ };
337
+ /**
338
+ * Where, below `path`, every stored value of `ast` holds a property named `__proto__` in a map: a
339
+ * required property of a struct, with no value that stores nothing ({@link isRequiredStored}),
340
+ * reached from `ast` through containers each certain to hold a map ({@link certainStruct}): `ast`
341
+ * itself required, and each property on the way a required struct.
342
+ * The walk is of the stored (encoded) form. Wherever a container on the way may hold no map, or
343
+ * nothing (an optional or `optionalKey` one, a nullable one, a union, a list or a tuple, a record,
344
+ * text, a declaration with no encoding), a value may or may not hold the name: the definition
345
+ * loads, and a write that gives the name is refused when it's sent (`EDD-9165`). `packed` names the
346
+ * properties of `ast`'s own struct that are stored as JSON text (`DynamoModel.Packed`), which hold
347
+ * any name: the walk doesn't enter them.
348
+ */
349
+ export const requiredProtoPropertyIn = (ast, path, packed = new Set(), seen = new Set()) => {
350
+ const stored = certainStruct(ast, seen);
351
+ if (stored === undefined)
352
+ return undefined;
353
+ for (const property of stored.propertySignatures) {
354
+ if (typeof property.name !== "string" || SchemaAST.isOptional(property.type))
355
+ continue;
356
+ const at = path === "" ? property.name : `${path}.${property.name}`;
357
+ if (property.name === "__proto__") {
358
+ if (isRequiredStored(property.type))
359
+ return at;
360
+ continue;
361
+ }
362
+ if (packed.has(property.name))
363
+ continue;
364
+ const found = requiredProtoPropertyIn(property.type, at, undefined, seen);
365
+ if (found !== undefined)
366
+ return found;
367
+ }
368
+ return undefined;
369
+ };
370
+ /**
371
+ * `[EDD-9152]`: an entity names `__proto__` where every item it writes would hold it, as an
372
+ * attribute or as a property of a stored map. A JavaScript object can't take that name by
373
+ * assignment (it replaces the prototype), and the AWS SDK's marshaller drops an attribute or a map
374
+ * entry so named, while its reader sets a prototype from one: no write could store the item whole.
375
+ * So: an attribute the library writes on every item ({@link names}: the created and updated
376
+ * timestamps, the version, the table's key attributes, an index's key attributes where every
377
+ * composite of both its halves always stores something, the attribute a field that always stores
378
+ * something is stored under), a required model field stored under its own name whose every value
379
+ * stores something ({@link isRequiredStored}), and such a property of one reached through required
380
+ * struct properties ({@link requiredProtoPropertyIn}). Anything else of that name loads (an
381
+ * optional field or property, one whose stored form admits `undefined` or isn't modelled exactly,
382
+ * an index's key attribute that some item may be without): a write that doesn't give it does what
383
+ * it did, and one that does is refused when it's sent (`EDD-9165`). Nothing stored as JSON text is
384
+ * looked into. Thrown at `make`, before anything else is derived.
385
+ *
386
+ * `fields` are the model's fields; `storage` says which are stored under another attribute and
387
+ * which are packed.
388
+ */
389
+ export const refuseProtoName = (entityType, fields, storage, names) => {
390
+ const refuse = (what) => protoRefusal("EDD-9152", `Entity "${entityType}"`, what);
391
+ for (const { name, what, always } of names) {
392
+ if (name === "__proto__" && always !== false)
393
+ throw refuse(what);
394
+ }
395
+ for (const [name, field] of Object.entries(fields)) {
396
+ if (name === "__proto__" && !storage.renamed.has(name) && isRequiredStored(field.ast)) {
397
+ throw refuse("the model field");
398
+ }
399
+ if (storage.packed.has(name))
400
+ continue;
401
+ const property = requiredProtoPropertyIn(field.ast, name);
402
+ if (property !== undefined)
403
+ throw refuse(`the model's property at "${property}"`);
404
+ }
405
+ };
406
+ /**
407
+ * The refusal of a definition naming `__proto__` where every row it writes would hold it:
408
+ * `[EDD-9152]` for an entity ({@link refuseProtoName}), `[EDD-9153]` for an aggregate
409
+ * (`refuseAggregateProtoName`). `what` says where the name is.
410
+ */
411
+ export const protoRefusal = (code, owner, what) => new Error(`[${code}] ${owner}: ${what} is named "__proto__", and every row written would hold it. A ` +
412
+ `JavaScript object can't hold a property of that name by assignment, and the AWS SDK's ` +
413
+ `marshaller drops an attribute or a map entry so named, so the value would be lost when ` +
414
+ `it's written or read. Name it otherwise.`);
415
+ //# sourceMappingURL=OwnNames.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OwnNames.js","sourceRoot":"","sources":["../../src/internal/OwnNames.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,IAAI,EAAe,SAAS,EAAE,MAAM,QAAQ,CAAA;AAErD,gEAAgE;AAChE,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,MAA0B,EAAE,IAAiB,EAAW,EAAE,CACnF,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;AAErD;;;GAGG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,MAA+C,EAC/C,IAAiB,EACF,EAAE,CACjB,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAc,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AAE1F;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GACtB,u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AAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,SAAQ;QACtC,MAAM,QAAQ,GAAG,uBAAuB,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;QACzD,IAAI,QAAQ,KAAK,SAAS;YAAE,MAAM,MAAM,CAAC,4BAA4B,QAAQ,GAAG,CAAC,CAAA;IACnF,CAAC;AACH,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,IAA6B,EAAE,KAAa,EAAE,IAAY,EAAS,EAAE,CAChG,IAAI,KAAK,CACP,IAAI,IAAI,KAAK,KAAK,KAAK,IAAI,gEAAgE;IACzF,wFAAwF;IACxF,yFAAyF;IACzF,0CAA0C,CAC7C,CAAA"}
@@ -37,6 +37,7 @@ import { Effect, Schema, SchemaAST } from "effect";
37
37
  import type { CompressionRegistry } from "../Compression.js";
38
38
  import type { CompressionCodec, PackedConfig } from "../DynamoModel.js";
39
39
  import { ValidationError } from "../Errors.js";
40
+ export { BETA_DATE_MARKER, DATE_MARKER, ZONE_MARKER } from "./StoredForm.js";
40
41
  /** Layout version of every value this version writes (bits 7–5). */
41
42
  export declare const LAYOUT_VERSION_V1 = 1;
42
43
  /** Format codes (bits 4–3). */
@@ -97,7 +98,12 @@ export declare const notRegistered: (ctx: OperationContext, attribute: string, c
97
98
  * bigint, binary (`Uint8Array`, `ArrayBuffer`, typed arrays), `Set`, `Map`,
98
99
  * `Date`, any other non-plain object (including one with `toJSON`), non-finite
99
100
  * numbers, `undefined` array elements, symbols, functions and cycles.
100
- * `undefined` object properties are omitted.
101
+ * `undefined` object properties are omitted. One of Effect's own instances that
102
+ * a native attribute stores as a map is the exception: a `DateTime`, one of the
103
+ * look-alikes a read rebuilds (a `Duration`, a `BigDecimal`, a zone, a cookie,
104
+ * an address, ...), an `Option` or a `Result` is written as that map
105
+ * ({@link storedFormOf}), as the map read back from a native row has always
106
+ * been. A named zone's map holding `resolvedId` is written as 4.1.2 held it.
101
107
  *
102
108
  * @throws an `Error` naming the path (rooted at `root`) and the JS type.
103
109
  */
@@ -1 +1 @@
1
- {"version":3,"file":"PackedCodec.d.ts","sourceRoot":"","sources":["../../src/internal/PackedCodec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAClD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAC5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACvE,OAAO,EAA2B,eAAe,EAAE,MAAM,cAAc,CAAA;AAMvE,oEAAoE;AACpE,eAAO,MAAM,iBAAiB,IAAQ,CAAA;AAEtC,+BAA+B;AAC/B,eAAO,MAAM,WAAW,IAAO,CAAA;AAC/B,wFAAwF;AACxF,eAAO,MAAM,aAAa,IAAO,CAAA;AAEjC,8BAA8B;AAC9B,eAAO,MAAM,UAAU,IAAQ,CAAA;AAC/B,eAAO,MAAM,UAAU,IAAQ,CAAA;AAC/B,eAAO,MAAM,YAAY,IAAQ,CAAA;AACjC,eAAO,MAAM,UAAU,IAAQ,CAAA;AAe/B,yEAAyE;AACzE,eAAO,MAAM,UAAU,GACrB,OAAO,gBAAgB,GAAG,MAAM,EAChC,SAAQ,MAAoB,KAC3B,MAC6F,CAAA;AAEhG,sCAAsC;AACtC,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAA;IACnB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,KAAK,EAAE,gBAAgB,GAAG,MAAM,CAAA;CAC1C;AAID,6FAA6F;AAC7F,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,KAAG,YAAY,GAAG,MAqBzD,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,GA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,EAC7B,KAAK,aAAa,KACjB,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,eAAe,CAGb,CAAA;AAM5B;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,WAAW,GACnB;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAA;CAAE,GAC/B;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAA;CAAE,GAC3B;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAA;AAgFzD,qFAAqF;AACrF,eAAO,MAAM,WAAW,GAAI,QAAQ,MAAM,CAAC,GAAG,GAAG,SAAS,CAAC,GAAG,KAAG,WACG,CAAA;AAEpE,gFAAgF;AAChF,eAAO,MAAM,aAAa,GAAI,QAAQ,MAAM,CAAC,GAAG,GAAG,SAAS,CAAC,GAAG,KAAG,OAC3B,CAAA;AAExC;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,GAAI,QAAQ,MAAM,CAAC,GAAG,GAAG,SAAS,CAAC,GAAG,KAAG,OAC7B,CAAA"}
@@ -35,6 +35,10 @@
35
35
  */
36
36
  import { Effect, Schema, SchemaAST } from "effect";
37
37
  import { CompressorNotRegistered, ValidationError } from "../Errors.js";
38
+ import { lostAs, lostRefusal } from "./LostValues.js";
39
+ import { classOf } from "./OwnNames.js";
40
+ import { storedFormOf, ZONE_MARKER, zoneEntriesWritten } from "./StoredForm.js";
41
+ export { BETA_DATE_MARKER, DATE_MARKER, ZONE_MARKER } from "./StoredForm.js";
38
42
  // ---------------------------------------------------------------------------
39
43
  // Header layout
40
44
  // ---------------------------------------------------------------------------
@@ -141,69 +145,125 @@ const describe = (value) => {
141
145
  if (value instanceof Date)
142
146
  return "a Date";
143
147
  if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
144
- return `binary data (${value.constructor?.name ?? "ArrayBuffer"})`;
148
+ return `binary data (${classOf(value)?.name ?? "ArrayBuffer"})`;
145
149
  }
146
150
  if (value instanceof Set)
147
151
  return "a Set";
148
152
  if (value instanceof Map)
149
153
  return "a Map";
150
- const name = value.constructor?.name;
154
+ const name = classOf(value)?.name;
151
155
  return name ? `an instance of ${name}` : "a non-plain object";
152
156
  };
153
157
  class NotJson extends Error {
154
158
  path;
155
159
  what;
156
- constructor(path, what) {
157
- super(`the value at ${path} is ${what}, which JSON cannot represent. Packed storage holds ` +
158
- `JSON only: store this field natively, or give it a JSON-shaped schema ` +
159
- `(e.g. DateTimeUtcFromString, BigIntFromString, Uint8ArrayFromBase64, an array instead ` +
160
- `of a set).`);
160
+ constructor(path, what, message) {
161
+ super(message ??
162
+ `the value at ${path} is ${what}, which JSON cannot represent. Packed storage holds ` +
163
+ `JSON only: store this field natively, or give it a JSON-shaped schema ` +
164
+ `(e.g. DateTimeUtcFromString, BigIntFromString, Uint8ArrayFromBase64, an array instead ` +
165
+ `of a set).`);
161
166
  this.path = path;
162
167
  this.what = what;
163
168
  }
164
169
  }
170
+ /** A value DynamoDB doesn't store however it's written, refused as a native write refuses it. */
171
+ const notStored = (path, lost) => new NotJson(path, lost.what, lostRefusal(path, lost));
172
+ /**
173
+ * What JSON can't hold whatever it holds: a map, or binary data. Refused as not JSON, ahead of what a
174
+ * native write would lose of one ({@link lostAs}), which a packed field never stores either way.
175
+ */
176
+ const isNeverJson = (value) => value instanceof Map ||
177
+ value instanceof ArrayBuffer ||
178
+ ArrayBuffer.isView(value) ||
179
+ (typeof SharedArrayBuffer === "function" && value instanceof SharedArrayBuffer);
165
180
  const isPlainObject = (value) => {
166
181
  const proto = Object.getPrototypeOf(value);
167
182
  return proto === Object.prototype || proto === null;
168
183
  };
169
- const check = (value, path, stack) => {
184
+ /** A named zone's map, holding `resolvedId`, as it's written ({@link zoneEntriesWritten}). */
185
+ const zoneWritten = (value, entries) => {
186
+ const written = zoneEntriesWritten(entries, Object.hasOwn(value, ZONE_MARKER), (entry) => entry, (name) => name);
187
+ return written === entries ? undefined : written;
188
+ };
189
+ /**
190
+ * What a value is written as: the value itself where it holds nothing to write otherwise, and a copy
191
+ * where it does, so `JSON.stringify` reads each property this read, once. Refuses what JSON can't
192
+ * hold. One of Effect's instances is written as the map a native write stores
193
+ * ({@link storedFormOf}), which is then written as any other value is, so what it still holds that
194
+ * JSON can't is refused where it is; a named zone's map holding `resolvedId` as 4.1.2 held it
195
+ * ({@link zoneEntriesWritten}). `forms` holds each instance met, with what it's written as.
196
+ */
197
+ const written = (value, path, stack, forms) => {
170
198
  switch (typeof value) {
171
199
  case "string":
172
200
  case "boolean":
173
- return;
201
+ return value;
174
202
  case "number":
175
203
  if (!Number.isFinite(value))
176
204
  throw new NotJson(path, describe(value));
177
- return;
205
+ return value;
178
206
  case "object": {
179
207
  if (value === null)
180
- return;
208
+ return value;
181
209
  if (stack.has(value))
182
210
  throw new NotJson(path, "a circular reference");
211
+ // What no storage holds is refused with the words a native write refuses it in (`LostValues`).
212
+ const lost = isNeverJson(value) ? undefined : lostAs(value);
213
+ if (lost !== undefined)
214
+ throw notStored(path, lost);
183
215
  if (Array.isArray(value)) {
184
216
  stack.add(value);
217
+ let copy;
185
218
  for (let i = 0; i < value.length; i++) {
186
219
  const element = value[i];
187
220
  if (element === undefined)
188
221
  throw new NotJson(`${path}[${i}]`, "undefined");
189
- check(element, `${path}[${i}]`, stack);
222
+ const write = written(element, `${path}[${i}]`, stack, forms);
223
+ if (copy === undefined && write !== element)
224
+ copy = value.slice(0, i);
225
+ copy?.push(write);
190
226
  }
191
227
  stack.delete(value);
192
- return;
228
+ return copy ?? value;
193
229
  }
194
- if (!isPlainObject(value) || typeof value.toJSON === "function") {
230
+ if (!isPlainObject(value)) {
231
+ if (forms.has(value))
232
+ return forms.get(value);
233
+ const form = storedFormOf(value);
234
+ if (form === undefined)
235
+ throw new NotJson(path, describe(value));
236
+ stack.add(value);
237
+ const write = written(form, path, stack, forms);
238
+ stack.delete(value);
239
+ forms.set(value, write);
240
+ return write;
241
+ }
242
+ if (typeof value.toJSON === "function") {
195
243
  throw new NotJson(path, describe(value));
196
244
  }
197
245
  stack.add(value);
246
+ const entries = [];
247
+ let changed = false;
198
248
  for (const key of Object.keys(value)) {
199
249
  const v = value[key];
200
250
  // Omitted by JSON.stringify, as removeUndefinedValues omits it from a map.
201
251
  if (v === undefined)
202
252
  continue;
203
- check(v, IDENTIFIER.test(key) ? `${path}.${key}` : `${path}[${JSON.stringify(key)}]`, stack);
253
+ const at = IDENTIFIER.test(key) ? `${path}.${key}` : `${path}[${JSON.stringify(key)}]`;
254
+ const write = written(v, at, stack, forms);
255
+ if (write !== v)
256
+ changed = true;
257
+ entries.push([key, write]);
204
258
  }
205
259
  stack.delete(value);
206
- return;
260
+ const zone = zoneWritten(value, entries);
261
+ if (zone === undefined && !changed)
262
+ return value;
263
+ const copy = Object.create(null);
264
+ for (const [key, write] of zone ?? entries)
265
+ copy[key] = write;
266
+ return copy;
207
267
  }
208
268
  default:
209
269
  throw new NotJson(path, describe(value));
@@ -214,14 +274,16 @@ const check = (value, path, stack) => {
214
274
  * bigint, binary (`Uint8Array`, `ArrayBuffer`, typed arrays), `Set`, `Map`,
215
275
  * `Date`, any other non-plain object (including one with `toJSON`), non-finite
216
276
  * numbers, `undefined` array elements, symbols, functions and cycles.
217
- * `undefined` object properties are omitted.
277
+ * `undefined` object properties are omitted. One of Effect's own instances that
278
+ * a native attribute stores as a map is the exception: a `DateTime`, one of the
279
+ * look-alikes a read rebuilds (a `Duration`, a `BigDecimal`, a zone, a cookie,
280
+ * an address, ...), an `Option` or a `Result` is written as that map
281
+ * ({@link storedFormOf}), as the map read back from a native row has always
282
+ * been. A named zone's map holding `resolvedId` is written as 4.1.2 held it.
218
283
  *
219
284
  * @throws an `Error` naming the path (rooted at `root`) and the JS type.
220
285
  */
221
- export const stringifyStrict = (value, root = "value") => {
222
- check(value, root, new Set());
223
- return JSON.stringify(value);
224
- };
286
+ export const stringifyStrict = (value, root = "value") => JSON.stringify(written(value, root, new Set(), new Map()));
225
287
  // ---------------------------------------------------------------------------
226
288
  // Pack / unpack
227
289
  // ---------------------------------------------------------------------------