@tanstack/db-sqlite-persistence-core 0.2.23 → 0.4.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 (58) hide show
  1. package/README.md +258 -7
  2. package/dist/cjs/broadcast-coordinator.cjs +1689 -0
  3. package/dist/cjs/broadcast-coordinator.cjs.map +1 -0
  4. package/dist/cjs/broadcast-coordinator.d.cts +116 -0
  5. package/dist/cjs/errors.cjs +73 -0
  6. package/dist/cjs/errors.cjs.map +1 -1
  7. package/dist/cjs/errors.d.cts +44 -0
  8. package/dist/cjs/index.cjs +19 -0
  9. package/dist/cjs/index.cjs.map +1 -1
  10. package/dist/cjs/index.d.cts +2 -0
  11. package/dist/cjs/persisted.cjs +1782 -394
  12. package/dist/cjs/persisted.cjs.map +1 -1
  13. package/dist/cjs/persisted.d.cts +176 -11
  14. package/dist/cjs/remote-subset-owner.cjs +20 -0
  15. package/dist/cjs/remote-subset-owner.cjs.map +1 -0
  16. package/dist/cjs/remote-subset-owner.d.cts +7 -0
  17. package/dist/cjs/remote-subset-wire.cjs +681 -0
  18. package/dist/cjs/remote-subset-wire.cjs.map +1 -0
  19. package/dist/cjs/remote-subset-wire.d.cts +59 -0
  20. package/dist/cjs/sqlite-core-adapter.cjs +969 -364
  21. package/dist/cjs/sqlite-core-adapter.cjs.map +1 -1
  22. package/dist/cjs/sqlite-core-adapter.d.cts +45 -1
  23. package/dist/cjs/sqlite-value.cjs +31 -0
  24. package/dist/cjs/sqlite-value.cjs.map +1 -0
  25. package/dist/cjs/sqlite-value.d.cts +10 -0
  26. package/dist/esm/broadcast-coordinator.d.ts +116 -0
  27. package/dist/esm/broadcast-coordinator.js +1689 -0
  28. package/dist/esm/broadcast-coordinator.js.map +1 -0
  29. package/dist/esm/errors.d.ts +44 -0
  30. package/dist/esm/errors.js +74 -1
  31. package/dist/esm/errors.js.map +1 -1
  32. package/dist/esm/index.d.ts +2 -0
  33. package/dist/esm/index.js +21 -2
  34. package/dist/esm/index.js.map +1 -1
  35. package/dist/esm/persisted.d.ts +176 -11
  36. package/dist/esm/persisted.js +1784 -396
  37. package/dist/esm/persisted.js.map +1 -1
  38. package/dist/esm/remote-subset-owner.d.ts +7 -0
  39. package/dist/esm/remote-subset-owner.js +20 -0
  40. package/dist/esm/remote-subset-owner.js.map +1 -0
  41. package/dist/esm/remote-subset-wire.d.ts +59 -0
  42. package/dist/esm/remote-subset-wire.js +681 -0
  43. package/dist/esm/remote-subset-wire.js.map +1 -0
  44. package/dist/esm/sqlite-core-adapter.d.ts +45 -1
  45. package/dist/esm/sqlite-core-adapter.js +955 -350
  46. package/dist/esm/sqlite-core-adapter.js.map +1 -1
  47. package/dist/esm/sqlite-value.d.ts +10 -0
  48. package/dist/esm/sqlite-value.js +31 -0
  49. package/dist/esm/sqlite-value.js.map +1 -0
  50. package/package.json +12 -2
  51. package/src/broadcast-coordinator.ts +2456 -0
  52. package/src/errors.ts +120 -0
  53. package/src/index.ts +2 -0
  54. package/src/persisted.ts +2617 -442
  55. package/src/remote-subset-owner.ts +36 -0
  56. package/src/remote-subset-wire.ts +946 -0
  57. package/src/sqlite-core-adapter.ts +1297 -410
  58. package/src/sqlite-value.ts +30 -0
@@ -0,0 +1,681 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const db = require("@tanstack/db");
4
+ class RemoteSubsetWireValueError extends TypeError {
5
+ constructor(path, reason) {
6
+ super(`Unsupported remote subset wire value at ${path}: ${reason}`);
7
+ this.path = path;
8
+ this.name = `RemoteSubsetWireValueError`;
9
+ }
10
+ }
11
+ const typedArrayConstructors = [
12
+ Int8Array,
13
+ Uint8Array,
14
+ Uint8ClampedArray,
15
+ Int16Array,
16
+ Uint16Array,
17
+ Int32Array,
18
+ Uint32Array,
19
+ Float32Array,
20
+ Float64Array,
21
+ BigInt64Array,
22
+ BigUint64Array
23
+ ];
24
+ const NativeDate = Date;
25
+ const identifierPattern = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
26
+ function toTransportedLoadSubsetOptions(options) {
27
+ assertPlainContainer(options, `options`);
28
+ assertAllowedProperties(options, `options`, [
29
+ `where`,
30
+ `orderBy`,
31
+ `limit`,
32
+ `cursor`,
33
+ `offset`,
34
+ `signal`,
35
+ `subscription`
36
+ ]);
37
+ const state = {
38
+ expressions: /* @__PURE__ */ new WeakMap(),
39
+ expressionArrays: /* @__PURE__ */ new WeakMap(),
40
+ stringArrays: /* @__PURE__ */ new WeakMap(),
41
+ orderByArrays: /* @__PURE__ */ new WeakMap(),
42
+ orderByClauses: /* @__PURE__ */ new WeakMap(),
43
+ compareOptions: /* @__PURE__ */ new WeakMap(),
44
+ cursors: /* @__PURE__ */ new WeakMap(),
45
+ wireValues: /* @__PURE__ */ new WeakMap()
46
+ };
47
+ const projected = {};
48
+ const where = readDataProperty(options, `where`, `options.where`);
49
+ const orderBy = readDataProperty(options, `orderBy`, `options.orderBy`);
50
+ const limit = readDataProperty(options, `limit`, `options.limit`);
51
+ const cursor = readDataProperty(options, `cursor`, `options.cursor`);
52
+ const offset = readDataProperty(options, `offset`, `options.offset`);
53
+ if (where.present && where.value !== void 0) {
54
+ projected.where = projectExpression(where.value, `options.where`, state);
55
+ }
56
+ if (orderBy.present && orderBy.value !== void 0) {
57
+ projected.orderBy = projectOrderBy(orderBy.value, `options.orderBy`, state);
58
+ }
59
+ if (limit.present && limit.value !== void 0) {
60
+ projected.limit = projectNumber(limit.value, `options.limit`);
61
+ }
62
+ if (cursor.present && cursor.value !== void 0) {
63
+ projected.cursor = projectCursor(cursor.value, `options.cursor`, state);
64
+ }
65
+ if (offset.present && offset.value !== void 0) {
66
+ projected.offset = projectNumber(offset.value, `options.offset`);
67
+ }
68
+ return projected;
69
+ }
70
+ function toProcessLocalLoadSubsetOptions(options, transported = toTransportedLoadSubsetOptions(options)) {
71
+ const projected = {
72
+ ...transported
73
+ };
74
+ const signal = readDataProperty(options, `signal`, `options.signal`);
75
+ const subscription = readDataProperty(
76
+ options,
77
+ `subscription`,
78
+ `options.subscription`
79
+ );
80
+ if (signal.present) {
81
+ projected.signal = signal.value;
82
+ }
83
+ if (subscription.present) {
84
+ projected.subscription = subscription.value;
85
+ }
86
+ return projected;
87
+ }
88
+ function projectExpression(value, path, state) {
89
+ const object = requireObject(value, path);
90
+ const existing = state.expressions.get(object);
91
+ if (existing) return existing;
92
+ const type = readDataProperty(object, `type`, `${path}.type`);
93
+ if (!type.present || typeof type.value !== `string`) {
94
+ throw new RemoteSubsetWireValueError(`${path}.type`, describe(type.value));
95
+ }
96
+ switch (type.value) {
97
+ case `ref`: {
98
+ assertExpressionPrototype(object, path, db.IR.PropRef.prototype);
99
+ assertAllowedProperties(object, path, [`type`, `path`, `sourceAlias`]);
100
+ const projected = {
101
+ type: `ref`,
102
+ path: []
103
+ };
104
+ state.expressions.set(object, projected);
105
+ const sourcePath = readRequiredDataProperty(
106
+ object,
107
+ `path`,
108
+ `${path}.path`
109
+ );
110
+ projected.path = projectStringArray(sourcePath, `${path}.path`, state);
111
+ const sourceAlias = readDataProperty(
112
+ object,
113
+ `sourceAlias`,
114
+ `${path}.sourceAlias`
115
+ );
116
+ if (sourceAlias.present) {
117
+ if (typeof sourceAlias.value !== `string` || projected.path[0] !== sourceAlias.value) {
118
+ throw new RemoteSubsetWireValueError(
119
+ `${path}.sourceAlias`,
120
+ `source alias must match the first path segment`
121
+ );
122
+ }
123
+ projected.sourceAlias = sourceAlias.value;
124
+ }
125
+ return projected;
126
+ }
127
+ case `val`: {
128
+ assertExpressionPrototype(object, path, db.IR.Value.prototype);
129
+ assertAllowedProperties(object, path, [`type`, `value`]);
130
+ const projected = {
131
+ type: `val`,
132
+ value: void 0
133
+ };
134
+ state.expressions.set(object, projected);
135
+ const sourceValue = readRequiredDataProperty(
136
+ object,
137
+ `value`,
138
+ `${path}.value`
139
+ );
140
+ projected.value = projectWireValue(sourceValue, `${path}.value`, state);
141
+ return projected;
142
+ }
143
+ case `func`: {
144
+ assertExpressionPrototype(object, path, db.IR.Func.prototype);
145
+ assertAllowedProperties(object, path, [`type`, `name`, `args`]);
146
+ const projected = {
147
+ type: `func`,
148
+ name: ``,
149
+ args: []
150
+ };
151
+ state.expressions.set(object, projected);
152
+ const name = readRequiredDataProperty(object, `name`, `${path}.name`);
153
+ if (typeof name !== `string`) {
154
+ throw new RemoteSubsetWireValueError(`${path}.name`, describe(name));
155
+ }
156
+ projected.name = name;
157
+ const args = readRequiredDataProperty(object, `args`, `${path}.args`);
158
+ projected.args = projectExpressionArray(args, `${path}.args`, state);
159
+ return projected;
160
+ }
161
+ default:
162
+ throw new RemoteSubsetWireValueError(`${path}.type`, String(type.value));
163
+ }
164
+ }
165
+ function projectExpressionArray(value, path, state) {
166
+ const array = requireArray(value, path);
167
+ const existing = state.expressionArrays.get(array);
168
+ if (existing) return existing;
169
+ assertArrayShape(array, path);
170
+ const projected = new Array(array.length);
171
+ state.expressionArrays.set(array, projected);
172
+ for (let index = 0; index < array.length; index++) {
173
+ if (!(index in array)) {
174
+ throw new RemoteSubsetWireValueError(
175
+ `${path}[${index}]`,
176
+ `missing expression`
177
+ );
178
+ }
179
+ projected[index] = projectExpression(
180
+ array[index],
181
+ `${path}[${index}]`,
182
+ state
183
+ );
184
+ }
185
+ return projected;
186
+ }
187
+ function projectStringArray(value, path, state) {
188
+ const array = requireArray(value, path);
189
+ const existing = state.stringArrays.get(array);
190
+ if (existing) return existing;
191
+ assertArrayShape(array, path);
192
+ const projected = new Array(array.length);
193
+ state.stringArrays.set(array, projected);
194
+ for (let index = 0; index < array.length; index++) {
195
+ const entry = array[index];
196
+ if (!(index in array) || typeof entry !== `string`) {
197
+ throw new RemoteSubsetWireValueError(`${path}[${index}]`, describe(entry));
198
+ }
199
+ projected[index] = entry;
200
+ }
201
+ return projected;
202
+ }
203
+ function projectOrderBy(value, path, state) {
204
+ const array = requireArray(value, path);
205
+ const existing = state.orderByArrays.get(array);
206
+ if (existing) return existing;
207
+ assertArrayShape(array, path);
208
+ const projected = new Array(array.length);
209
+ state.orderByArrays.set(array, projected);
210
+ for (let index = 0; index < array.length; index++) {
211
+ const clausePath = `${path}[${index}]`;
212
+ const clause = requirePlainRecord(array[index], clausePath);
213
+ const existingClause = state.orderByClauses.get(clause);
214
+ if (existingClause) {
215
+ projected[index] = existingClause;
216
+ continue;
217
+ }
218
+ assertAllowedProperties(clause, clausePath, [
219
+ `expression`,
220
+ `compareOptions`
221
+ ]);
222
+ const projectedClause = {
223
+ expression: void 0,
224
+ compareOptions: void 0
225
+ };
226
+ projected[index] = projectedClause;
227
+ state.orderByClauses.set(clause, projectedClause);
228
+ projectedClause.expression = projectExpression(
229
+ readRequiredDataProperty(
230
+ clause,
231
+ `expression`,
232
+ `${clausePath}.expression`
233
+ ),
234
+ `${clausePath}.expression`,
235
+ state
236
+ );
237
+ projectedClause.compareOptions = projectCompareOptions(
238
+ readRequiredDataProperty(
239
+ clause,
240
+ `compareOptions`,
241
+ `${clausePath}.compareOptions`
242
+ ),
243
+ `${clausePath}.compareOptions`,
244
+ state
245
+ );
246
+ }
247
+ return projected;
248
+ }
249
+ function projectCompareOptions(value, path, state) {
250
+ const source = requirePlainRecord(value, path);
251
+ const existing = state.compareOptions.get(source);
252
+ if (existing) return existing;
253
+ assertAllowedProperties(source, path, [
254
+ `direction`,
255
+ `nulls`,
256
+ `stringSort`,
257
+ `locale`,
258
+ `localeOptions`
259
+ ]);
260
+ const projected = {};
261
+ const direction = readRequiredDataProperty(
262
+ source,
263
+ `direction`,
264
+ `${path}.direction`
265
+ );
266
+ if (direction !== `asc` && direction !== `desc`) {
267
+ throw new RemoteSubsetWireValueError(
268
+ `${path}.direction`,
269
+ describe(direction)
270
+ );
271
+ }
272
+ projected.direction = direction;
273
+ const nulls = readRequiredDataProperty(source, `nulls`, `${path}.nulls`);
274
+ if (nulls !== `first` && nulls !== `last`) {
275
+ throw new RemoteSubsetWireValueError(`${path}.nulls`, describe(nulls));
276
+ }
277
+ projected.nulls = nulls;
278
+ const stringSort = readDataProperty(
279
+ source,
280
+ `stringSort`,
281
+ `${path}.stringSort`
282
+ );
283
+ if (stringSort.present && stringSort.value !== void 0) {
284
+ if (stringSort.value !== `lexical` && stringSort.value !== `locale`) {
285
+ throw new RemoteSubsetWireValueError(
286
+ `${path}.stringSort`,
287
+ describe(stringSort.value)
288
+ );
289
+ }
290
+ projected.stringSort = stringSort.value;
291
+ }
292
+ if (projected.stringSort !== `lexical`) {
293
+ const locale = readDataProperty(source, `locale`, `${path}.locale`);
294
+ if (locale.present && locale.value !== void 0) {
295
+ if (typeof locale.value !== `string`) {
296
+ throw new RemoteSubsetWireValueError(
297
+ `${path}.locale`,
298
+ describe(locale.value)
299
+ );
300
+ }
301
+ projected.locale = locale.value;
302
+ }
303
+ const localeOptions = readDataProperty(
304
+ source,
305
+ `localeOptions`,
306
+ `${path}.localeOptions`
307
+ );
308
+ if (localeOptions.present && localeOptions.value !== void 0) {
309
+ projected.localeOptions = projectWireRecord(
310
+ localeOptions.value,
311
+ `${path}.localeOptions`,
312
+ state
313
+ );
314
+ }
315
+ }
316
+ const result = projected;
317
+ state.compareOptions.set(source, result);
318
+ return result;
319
+ }
320
+ function projectCursor(value, path, state) {
321
+ const source = requirePlainRecord(value, path);
322
+ const existing = state.cursors.get(source);
323
+ if (existing) return existing;
324
+ assertAllowedProperties(source, path, [
325
+ `whereFrom`,
326
+ `whereCurrent`,
327
+ `lastKey`
328
+ ]);
329
+ const projected = {
330
+ whereFrom: void 0,
331
+ whereCurrent: void 0
332
+ };
333
+ state.cursors.set(source, projected);
334
+ projected.whereFrom = projectExpression(
335
+ readRequiredDataProperty(source, `whereFrom`, `${path}.whereFrom`),
336
+ `${path}.whereFrom`,
337
+ state
338
+ );
339
+ projected.whereCurrent = projectExpression(
340
+ readRequiredDataProperty(source, `whereCurrent`, `${path}.whereCurrent`),
341
+ `${path}.whereCurrent`,
342
+ state
343
+ );
344
+ const lastKey = readDataProperty(source, `lastKey`, `${path}.lastKey`);
345
+ if (lastKey.present && lastKey.value !== void 0) {
346
+ if (typeof lastKey.value !== `string` && typeof lastKey.value !== `number`) {
347
+ throw new RemoteSubsetWireValueError(
348
+ `${path}.lastKey`,
349
+ describe(lastKey.value)
350
+ );
351
+ }
352
+ projected.lastKey = lastKey.value;
353
+ }
354
+ return projected;
355
+ }
356
+ function projectWireValue(value, path, state) {
357
+ if (value === void 0 || value === null || typeof value === `boolean` || typeof value === `string` || typeof value === `number` || typeof value === `bigint`) {
358
+ return value;
359
+ }
360
+ if (typeof value !== `object`) {
361
+ throw new RemoteSubsetWireValueError(path, describe(value));
362
+ }
363
+ const existing = state.wireValues.get(value);
364
+ if (existing) return existing;
365
+ if (typeof SharedArrayBuffer !== `undefined` && value instanceof SharedArrayBuffer) {
366
+ throw new RemoteSubsetWireValueError(path, `SharedArrayBuffer`);
367
+ }
368
+ if (value instanceof NativeDate) {
369
+ assertExactPrototype(value, NativeDate.prototype, path);
370
+ assertNoOwnProperties(value, path);
371
+ const projected = new NativeDate(value.getTime());
372
+ state.wireValues.set(value, projected);
373
+ return projected;
374
+ }
375
+ if (value instanceof RegExp) {
376
+ assertExactPrototype(value, RegExp.prototype, path);
377
+ assertRegExpShape(value, path);
378
+ const projected = new RegExp(value.source, value.flags);
379
+ state.wireValues.set(value, projected);
380
+ return projected;
381
+ }
382
+ if (value instanceof ArrayBuffer) {
383
+ assertExactPrototype(value, ArrayBuffer.prototype, path);
384
+ assertNoOwnProperties(value, path);
385
+ assertFixedArrayBuffer(value, path);
386
+ let projected;
387
+ try {
388
+ projected = value.slice(0);
389
+ } catch {
390
+ throw new RemoteSubsetWireValueError(path, `detached ArrayBuffer`);
391
+ }
392
+ state.wireValues.set(value, projected);
393
+ return projected;
394
+ }
395
+ if (value instanceof DataView) {
396
+ assertExactPrototype(value, DataView.prototype, path);
397
+ assertNoOwnProperties(value, path);
398
+ let byteOffset;
399
+ let byteLength;
400
+ let buffer;
401
+ try {
402
+ byteOffset = value.byteOffset;
403
+ byteLength = value.byteLength;
404
+ buffer = value.buffer;
405
+ } catch {
406
+ throw new RemoteSubsetWireValueError(path, `detached DataView`);
407
+ }
408
+ const projectedBuffer = projectWireValue(buffer, path, state);
409
+ if (!(projectedBuffer instanceof ArrayBuffer)) {
410
+ throw new RemoteSubsetWireValueError(path, `shared DataView buffer`);
411
+ }
412
+ const projected = new DataView(projectedBuffer, byteOffset, byteLength);
413
+ state.wireValues.set(value, projected);
414
+ return projected;
415
+ }
416
+ const typedArrayConstructor = getTypedArrayConstructor(value);
417
+ if (typedArrayConstructor) {
418
+ const typedArray = value;
419
+ assertTypedArrayShape(typedArray, path);
420
+ let byteOffset;
421
+ let length;
422
+ let buffer;
423
+ try {
424
+ byteOffset = typedArray.byteOffset;
425
+ length = typedArray.length;
426
+ buffer = typedArray.buffer;
427
+ } catch {
428
+ throw new RemoteSubsetWireValueError(path, `detached typed array`);
429
+ }
430
+ if (!(buffer instanceof ArrayBuffer)) {
431
+ throw new RemoteSubsetWireValueError(path, `shared typed-array buffer`);
432
+ }
433
+ const projectedBuffer = projectWireValue(buffer, path, state);
434
+ if (!(projectedBuffer instanceof ArrayBuffer)) {
435
+ throw new RemoteSubsetWireValueError(path, `shared typed-array buffer`);
436
+ }
437
+ let projected;
438
+ try {
439
+ projected = new typedArrayConstructor(projectedBuffer, byteOffset, length);
440
+ } catch {
441
+ throw new RemoteSubsetWireValueError(path, `detached typed array`);
442
+ }
443
+ state.wireValues.set(value, projected);
444
+ return projected;
445
+ }
446
+ if (ArrayBuffer.isView(value)) {
447
+ throw new RemoteSubsetWireValueError(path, describe(value));
448
+ }
449
+ if (Array.isArray(value)) return projectWireArray(value, path, state);
450
+ if (value instanceof Map) return projectWireMap(value, path, state);
451
+ if (value instanceof Set) return projectWireSet(value, path, state);
452
+ return projectWireRecord(value, path, state);
453
+ }
454
+ function projectWireArray(value, path, state) {
455
+ assertArrayShape(value, path);
456
+ const projected = new Array(value.length);
457
+ state.wireValues.set(value, projected);
458
+ for (let index = 0; index < value.length; index++) {
459
+ if (index in value) {
460
+ projected[index] = projectWireValue(
461
+ value[index],
462
+ `${path}[${index}]`,
463
+ state
464
+ );
465
+ }
466
+ }
467
+ return projected;
468
+ }
469
+ function projectWireMap(value, path, state) {
470
+ assertExactPrototype(value, Map.prototype, path);
471
+ assertNoOwnProperties(value, path);
472
+ const projected = /* @__PURE__ */ new Map();
473
+ state.wireValues.set(value, projected);
474
+ let index = 0;
475
+ for (const [key, entry] of value) {
476
+ projected.set(
477
+ projectWireValue(key, `${path}.entries[${index}].key`, state),
478
+ projectWireValue(entry, `${path}.entries[${index}].value`, state)
479
+ );
480
+ index++;
481
+ }
482
+ return projected;
483
+ }
484
+ function projectWireSet(value, path, state) {
485
+ assertExactPrototype(value, Set.prototype, path);
486
+ assertNoOwnProperties(value, path);
487
+ const projected = /* @__PURE__ */ new Set();
488
+ state.wireValues.set(value, projected);
489
+ let index = 0;
490
+ for (const entry of value) {
491
+ projected.add(projectWireValue(entry, `${path}.values[${index}]`, state));
492
+ index++;
493
+ }
494
+ return projected;
495
+ }
496
+ function projectWireRecord(value, path, state) {
497
+ const source = requirePlainRecord(value, path);
498
+ const existing = state.wireValues.get(source);
499
+ if (existing) return existing;
500
+ const projected = {};
501
+ state.wireValues.set(source, projected);
502
+ for (const key of ownKeys(source, path)) {
503
+ const childPath = propertyPath(path, key);
504
+ if (typeof key === `symbol`) {
505
+ throw new RemoteSubsetWireValueError(childPath, `symbol property key`);
506
+ }
507
+ const descriptor = Object.getOwnPropertyDescriptor(source, key);
508
+ if (!descriptor || !descriptor.enumerable || !(`value` in descriptor)) {
509
+ throw new RemoteSubsetWireValueError(childPath, `non-data property`);
510
+ }
511
+ Object.defineProperty(projected, key, {
512
+ value: projectWireValue(descriptor.value, childPath, state),
513
+ enumerable: true,
514
+ writable: true,
515
+ configurable: true
516
+ });
517
+ }
518
+ return projected;
519
+ }
520
+ function projectNumber(value, path) {
521
+ if (typeof value !== `number` || !Number.isSafeInteger(value) || value < 0) {
522
+ throw new RemoteSubsetWireValueError(path, `non-negative safe integer`);
523
+ }
524
+ return value;
525
+ }
526
+ function readRequiredDataProperty(value, key, path) {
527
+ const property = readDataProperty(value, key, path);
528
+ if (!property.present) {
529
+ throw new RemoteSubsetWireValueError(path, `missing property`);
530
+ }
531
+ return property.value;
532
+ }
533
+ function readDataProperty(value, key, path) {
534
+ let descriptor;
535
+ try {
536
+ descriptor = Object.getOwnPropertyDescriptor(value, key);
537
+ } catch {
538
+ throw new RemoteSubsetWireValueError(path, `unreadable property`);
539
+ }
540
+ if (!descriptor) return { present: false };
541
+ if (!descriptor.enumerable || !(`value` in descriptor)) {
542
+ throw new RemoteSubsetWireValueError(path, `non-data property`);
543
+ }
544
+ return { present: true, value: descriptor.value };
545
+ }
546
+ function assertAllowedProperties(value, path, allowed) {
547
+ const allowedSet = new Set(allowed);
548
+ for (const key of ownKeys(value, path)) {
549
+ if (typeof key === `symbol` || !allowedSet.has(key)) {
550
+ throw new RemoteSubsetWireValueError(propertyPath(path, key), `property`);
551
+ }
552
+ readDataProperty(value, key, propertyPath(path, key));
553
+ }
554
+ }
555
+ function assertPlainContainer(value, path) {
556
+ assertExactPrototype(value, Object.prototype, path);
557
+ }
558
+ function assertExpressionPrototype(value, path, expressionPrototype) {
559
+ let prototype;
560
+ try {
561
+ prototype = Object.getPrototypeOf(value);
562
+ } catch {
563
+ throw new RemoteSubsetWireValueError(path, `unreadable prototype`);
564
+ }
565
+ if (prototype !== Object.prototype && prototype !== expressionPrototype) {
566
+ throw new RemoteSubsetWireValueError(path, describe(value));
567
+ }
568
+ }
569
+ function assertExactPrototype(value, expected, path) {
570
+ let prototype;
571
+ try {
572
+ prototype = Object.getPrototypeOf(value);
573
+ } catch {
574
+ throw new RemoteSubsetWireValueError(path, `unreadable prototype`);
575
+ }
576
+ if (prototype !== expected) {
577
+ throw new RemoteSubsetWireValueError(path, describe(value));
578
+ }
579
+ }
580
+ function assertNoOwnProperties(value, path) {
581
+ const keys = ownKeys(value, path);
582
+ if (keys.length > 0) {
583
+ throw new RemoteSubsetWireValueError(
584
+ propertyPath(path, keys[0]),
585
+ `property`
586
+ );
587
+ }
588
+ }
589
+ function assertFixedArrayBuffer(value, path) {
590
+ if (value.resizable) {
591
+ throw new RemoteSubsetWireValueError(path, `resizable ArrayBuffer`);
592
+ }
593
+ }
594
+ function assertRegExpShape(value, path) {
595
+ const keys = ownKeys(value, path);
596
+ for (const key of keys) {
597
+ if (key !== `lastIndex`) {
598
+ throw new RemoteSubsetWireValueError(propertyPath(path, key), `property`);
599
+ }
600
+ }
601
+ const descriptor = Object.getOwnPropertyDescriptor(value, `lastIndex`);
602
+ if (!descriptor || !(`value` in descriptor) || descriptor.value !== 0) {
603
+ throw new RemoteSubsetWireValueError(
604
+ `${path}.lastIndex`,
605
+ `RegExp lastIndex must be 0`
606
+ );
607
+ }
608
+ }
609
+ function assertTypedArrayShape(value, path) {
610
+ const keys = ownKeys(value, path);
611
+ for (const key of keys) {
612
+ if (typeof key !== `string` || !isCanonicalArrayIndex(key, value.length)) {
613
+ throw new RemoteSubsetWireValueError(propertyPath(path, key), `property`);
614
+ }
615
+ }
616
+ }
617
+ function assertArrayShape(value, path) {
618
+ assertExactPrototype(value, Array.prototype, path);
619
+ const keys = ownKeys(value, path);
620
+ for (const key of keys) {
621
+ if (key !== `length` && (typeof key !== `string` || !isCanonicalArrayIndex(key, value.length))) {
622
+ throw new RemoteSubsetWireValueError(propertyPath(path, key), `property`);
623
+ }
624
+ if (key !== `length`) {
625
+ readDataProperty(value, key, propertyPath(path, key));
626
+ }
627
+ }
628
+ }
629
+ function isCanonicalArrayIndex(key, length) {
630
+ if (!/^(0|[1-9][0-9]*)$/.test(key)) return false;
631
+ const index = Number(key);
632
+ return Number.isSafeInteger(index) && index >= 0 && index < length;
633
+ }
634
+ function getTypedArrayConstructor(value) {
635
+ let prototype;
636
+ try {
637
+ prototype = Object.getPrototypeOf(value);
638
+ } catch {
639
+ return void 0;
640
+ }
641
+ return typedArrayConstructors.find(
642
+ (constructor) => prototype === constructor.prototype
643
+ );
644
+ }
645
+ function requireArray(value, path) {
646
+ if (!Array.isArray(value)) {
647
+ throw new RemoteSubsetWireValueError(path, describe(value));
648
+ }
649
+ return value;
650
+ }
651
+ function requireObject(value, path) {
652
+ if (value === null || typeof value !== `object`) {
653
+ throw new RemoteSubsetWireValueError(path, describe(value));
654
+ }
655
+ return value;
656
+ }
657
+ function requirePlainRecord(value, path) {
658
+ const object = requireObject(value, path);
659
+ assertExactPrototype(object, Object.prototype, path);
660
+ return object;
661
+ }
662
+ function ownKeys(value, path) {
663
+ try {
664
+ return Reflect.ownKeys(value);
665
+ } catch {
666
+ throw new RemoteSubsetWireValueError(path, `unreadable properties`);
667
+ }
668
+ }
669
+ function propertyPath(path, key) {
670
+ if (typeof key === `symbol`) return `${path}[${String(key)}]`;
671
+ return identifierPattern.test(key) ? `${path}.${key}` : `${path}[${JSON.stringify(key)}]`;
672
+ }
673
+ function describe(value) {
674
+ if (value === null) return `null`;
675
+ if (typeof value !== `object`) return typeof value;
676
+ return `object`;
677
+ }
678
+ exports.RemoteSubsetWireValueError = RemoteSubsetWireValueError;
679
+ exports.toProcessLocalLoadSubsetOptions = toProcessLocalLoadSubsetOptions;
680
+ exports.toTransportedLoadSubsetOptions = toTransportedLoadSubsetOptions;
681
+ //# sourceMappingURL=remote-subset-wire.cjs.map