@rocicorp/zero 0.24.2025091200 → 0.24.2025092500

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 (177) hide show
  1. package/out/analyze-query/src/bin-analyze.js +7 -8
  2. package/out/analyze-query/src/bin-analyze.js.map +1 -1
  3. package/out/analyze-query/src/explain-queries.d.ts +1 -1
  4. package/out/analyze-query/src/explain-queries.d.ts.map +1 -1
  5. package/out/analyze-query/src/run-ast.d.ts +13 -23
  6. package/out/analyze-query/src/run-ast.d.ts.map +1 -1
  7. package/out/analyze-query/src/run-ast.js +25 -20
  8. package/out/analyze-query/src/run-ast.js.map +1 -1
  9. package/out/{chunk-KXV3BZ4U.js → chunk-576ADFNS.js} +3 -3
  10. package/out/{chunk-6XNI6IVJ.js → chunk-N4QOJO4J.js} +48 -34
  11. package/out/chunk-N4QOJO4J.js.map +7 -0
  12. package/out/{chunk-K5ZSWG54.js → chunk-QZPMFA73.js} +841 -327
  13. package/out/chunk-QZPMFA73.js.map +7 -0
  14. package/out/{inspector-ZZSIUMBB.js → lazy-inspector-TOTYUTBC.js} +286 -249
  15. package/out/lazy-inspector-TOTYUTBC.js.map +7 -0
  16. package/out/react-native.js +1 -1
  17. package/out/react-native.js.map +2 -2
  18. package/out/react.js +2 -2
  19. package/out/replicache/src/connection-loop.d.ts.map +1 -1
  20. package/out/replicache/src/process-scheduler.d.ts.map +1 -1
  21. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  22. package/out/shared/src/sentinels.d.ts +2 -0
  23. package/out/shared/src/sentinels.d.ts.map +1 -1
  24. package/out/shared/src/sentinels.js +7 -0
  25. package/out/shared/src/sentinels.js.map +1 -0
  26. package/out/shared/src/valita.js +1 -1
  27. package/out/shared/src/valita.js.map +1 -1
  28. package/out/solid.js +3 -3
  29. package/out/zero/package.json +4 -4
  30. package/out/zero-cache/src/config/network.d.ts.map +1 -1
  31. package/out/zero-cache/src/config/network.js +1 -2
  32. package/out/zero-cache/src/config/network.js.map +1 -1
  33. package/out/zero-cache/src/server/anonymous-otel-start.d.ts +1 -0
  34. package/out/zero-cache/src/server/anonymous-otel-start.d.ts.map +1 -1
  35. package/out/zero-cache/src/server/anonymous-otel-start.js +18 -0
  36. package/out/zero-cache/src/server/anonymous-otel-start.js.map +1 -1
  37. package/out/zero-cache/src/server/inspector-delegate.d.ts.map +1 -1
  38. package/out/zero-cache/src/server/inspector-delegate.js +7 -6
  39. package/out/zero-cache/src/server/inspector-delegate.js.map +1 -1
  40. package/out/zero-cache/src/server/reaper.d.ts.map +1 -1
  41. package/out/zero-cache/src/server/reaper.js +8 -0
  42. package/out/zero-cache/src/server/reaper.js.map +1 -1
  43. package/out/zero-cache/src/services/analyze.d.ts +8 -0
  44. package/out/zero-cache/src/services/analyze.d.ts.map +1 -1
  45. package/out/zero-cache/src/services/analyze.js +103 -33
  46. package/out/zero-cache/src/services/analyze.js.map +1 -1
  47. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +3 -1
  48. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  49. package/out/zero-cache/src/services/change-streamer/schema/init.d.ts.map +1 -1
  50. package/out/zero-cache/src/services/change-streamer/schema/init.js +1 -0
  51. package/out/zero-cache/src/services/change-streamer/schema/init.js.map +1 -1
  52. package/out/zero-cache/src/services/mutagen/pusher.d.ts +28 -0
  53. package/out/zero-cache/src/services/mutagen/pusher.d.ts.map +1 -1
  54. package/out/zero-cache/src/services/replicator/change-processor.js +1 -1
  55. package/out/zero-cache/src/services/replicator/change-processor.js.map +1 -1
  56. package/out/zero-cache/src/services/replicator/incremental-sync.js +2 -2
  57. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  58. package/out/zero-cache/src/services/view-syncer/active-users-gauge.d.ts +16 -0
  59. package/out/zero-cache/src/services/view-syncer/active-users-gauge.d.ts.map +1 -0
  60. package/out/zero-cache/src/services/view-syncer/active-users-gauge.js +50 -0
  61. package/out/zero-cache/src/services/view-syncer/active-users-gauge.js.map +1 -0
  62. package/out/zero-cache/src/services/view-syncer/client-schema.js +1 -1
  63. package/out/zero-cache/src/services/view-syncer/client-schema.js.map +1 -1
  64. package/out/zero-cache/src/services/view-syncer/cvr-purger.d.ts.map +1 -1
  65. package/out/zero-cache/src/services/view-syncer/cvr-purger.js +21 -5
  66. package/out/zero-cache/src/services/view-syncer/cvr-purger.js.map +1 -1
  67. package/out/zero-cache/src/services/view-syncer/row-record-cache.js +1 -1
  68. package/out/zero-cache/src/services/view-syncer/row-record-cache.js.map +1 -1
  69. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +2 -3
  70. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  71. package/out/zero-cache/src/services/view-syncer/view-syncer.js +12 -0
  72. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  73. package/out/zero-cache/src/types/pg.d.ts.map +1 -1
  74. package/out/zero-cache/src/types/pg.js +16 -3
  75. package/out/zero-cache/src/types/pg.js.map +1 -1
  76. package/out/zero-client/src/client/inspector/client-group.d.ts +12 -0
  77. package/out/zero-client/src/client/inspector/client-group.d.ts.map +1 -0
  78. package/out/zero-client/src/client/inspector/client.d.ts +15 -0
  79. package/out/zero-client/src/client/inspector/client.d.ts.map +1 -0
  80. package/out/zero-client/src/client/inspector/html-dialog-prompt.d.ts +5 -0
  81. package/out/zero-client/src/client/inspector/html-dialog-prompt.d.ts.map +1 -0
  82. package/out/zero-client/src/client/inspector/inspector.d.ts +14 -18
  83. package/out/zero-client/src/client/inspector/inspector.d.ts.map +1 -1
  84. package/out/zero-client/src/client/inspector/lazy-inspector.d.ts +49 -0
  85. package/out/zero-client/src/client/inspector/lazy-inspector.d.ts.map +1 -0
  86. package/out/zero-client/src/client/inspector/query.d.ts +31 -0
  87. package/out/zero-client/src/client/inspector/query.d.ts.map +1 -0
  88. package/out/zero-client/src/client/options.d.ts +8 -4
  89. package/out/zero-client/src/client/options.d.ts.map +1 -1
  90. package/out/zero-client/src/client/zero.d.ts +4 -4
  91. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  92. package/out/zero-client/src/mod.d.ts +4 -1
  93. package/out/zero-client/src/mod.d.ts.map +1 -1
  94. package/out/zero-protocol/src/analyze-query-result.d.ts +22 -0
  95. package/out/zero-protocol/src/analyze-query-result.d.ts.map +1 -0
  96. package/out/zero-protocol/src/analyze-query-result.js +18 -0
  97. package/out/zero-protocol/src/analyze-query-result.js.map +1 -0
  98. package/out/zero-protocol/src/ast.d.ts +2 -0
  99. package/out/zero-protocol/src/ast.d.ts.map +1 -1
  100. package/out/zero-protocol/src/ast.js +2 -0
  101. package/out/zero-protocol/src/ast.js.map +1 -1
  102. package/out/zero-protocol/src/down.d.ts +15 -0
  103. package/out/zero-protocol/src/down.d.ts.map +1 -1
  104. package/out/zero-protocol/src/inspect-down.d.ts +47 -0
  105. package/out/zero-protocol/src/inspect-down.d.ts.map +1 -1
  106. package/out/zero-protocol/src/inspect-down.js +6 -1
  107. package/out/zero-protocol/src/inspect-down.js.map +1 -1
  108. package/out/zero-protocol/src/inspect-up.d.ts +34 -0
  109. package/out/zero-protocol/src/inspect-up.d.ts.map +1 -1
  110. package/out/zero-protocol/src/inspect-up.js +11 -1
  111. package/out/zero-protocol/src/inspect-up.js.map +1 -1
  112. package/out/zero-protocol/src/protocol-version.d.ts +1 -1
  113. package/out/zero-protocol/src/protocol-version.d.ts.map +1 -1
  114. package/out/zero-protocol/src/protocol-version.js +3 -1
  115. package/out/zero-protocol/src/protocol-version.js.map +1 -1
  116. package/out/zero-protocol/src/up.d.ts +9 -0
  117. package/out/zero-protocol/src/up.d.ts.map +1 -1
  118. package/out/zero.js +3 -3
  119. package/out/zql/src/builder/builder.d.ts.map +1 -1
  120. package/out/zql/src/builder/builder.js +23 -11
  121. package/out/zql/src/builder/builder.js.map +1 -1
  122. package/out/zql/src/builder/debug-delegate.d.ts +2 -5
  123. package/out/zql/src/builder/debug-delegate.d.ts.map +1 -1
  124. package/out/zql/src/builder/debug-delegate.js +1 -0
  125. package/out/zql/src/builder/debug-delegate.js.map +1 -1
  126. package/out/zql/src/ivm/constraint.js +1 -1
  127. package/out/zql/src/ivm/constraint.js.map +1 -1
  128. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  129. package/out/zql/src/ivm/fan-in.js +3 -92
  130. package/out/zql/src/ivm/fan-in.js.map +1 -1
  131. package/out/zql/src/ivm/fan-out.js +2 -2
  132. package/out/zql/src/ivm/fan-out.js.map +1 -1
  133. package/out/zql/src/ivm/flipped-join.d.ts +33 -0
  134. package/out/zql/src/ivm/flipped-join.d.ts.map +1 -0
  135. package/out/zql/src/ivm/flipped-join.js +341 -0
  136. package/out/zql/src/ivm/flipped-join.js.map +1 -0
  137. package/out/zql/src/ivm/join-utils.d.ts +14 -0
  138. package/out/zql/src/ivm/join-utils.d.ts.map +1 -0
  139. package/out/zql/src/ivm/join-utils.js +94 -0
  140. package/out/zql/src/ivm/join-utils.js.map +1 -0
  141. package/out/zql/src/ivm/join.d.ts +1 -1
  142. package/out/zql/src/ivm/join.d.ts.map +1 -1
  143. package/out/zql/src/ivm/join.js +3 -101
  144. package/out/zql/src/ivm/join.js.map +1 -1
  145. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  146. package/out/zql/src/ivm/memory-source.js +15 -4
  147. package/out/zql/src/ivm/memory-source.js.map +1 -1
  148. package/out/zql/src/ivm/push-accumulated.d.ts +88 -0
  149. package/out/zql/src/ivm/push-accumulated.d.ts.map +1 -0
  150. package/out/zql/src/ivm/push-accumulated.js +326 -0
  151. package/out/zql/src/ivm/push-accumulated.js.map +1 -0
  152. package/out/zql/src/query/expression.d.ts +3 -3
  153. package/out/zql/src/query/expression.d.ts.map +1 -1
  154. package/out/zql/src/query/expression.js +1 -1
  155. package/out/zql/src/query/expression.js.map +1 -1
  156. package/out/zql/src/query/query-impl.d.ts +3 -4
  157. package/out/zql/src/query/query-impl.d.ts.map +1 -1
  158. package/out/zql/src/query/query-impl.js +12 -5
  159. package/out/zql/src/query/query-impl.js.map +1 -1
  160. package/out/zql/src/query/query.d.ts +6 -3
  161. package/out/zql/src/query/query.d.ts.map +1 -1
  162. package/out/zql/src/query/query.js.map +1 -1
  163. package/out/zqlite/src/db.d.ts +1 -1
  164. package/out/zqlite/src/db.d.ts.map +1 -1
  165. package/out/zqlite/src/db.js +3 -3
  166. package/out/zqlite/src/db.js.map +1 -1
  167. package/out/zqlite/src/table-source.d.ts +0 -1
  168. package/out/zqlite/src/table-source.d.ts.map +1 -1
  169. package/out/zqlite/src/table-source.js +4 -5
  170. package/out/zqlite/src/table-source.js.map +1 -1
  171. package/package.json +4 -4
  172. package/out/chunk-6XNI6IVJ.js.map +0 -7
  173. package/out/chunk-K5ZSWG54.js.map +0 -7
  174. package/out/inspector-ZZSIUMBB.js.map +0 -7
  175. package/out/zero-client/src/client/inspector/types.d.ts +0 -50
  176. package/out/zero-client/src/client/inspector/types.d.ts.map +0 -1
  177. /package/out/{chunk-KXV3BZ4U.js.map → chunk-576ADFNS.js.map} +0 -0
@@ -144,7 +144,7 @@ function getDeepestUnionParseError(value, schema, mode) {
144
144
  try {
145
145
  const str = JSON.stringify(value);
146
146
  return `Invalid union value: ${str}`;
147
- } catch (e) {
147
+ } catch {
148
148
  return `Invalid union value`;
149
149
  }
150
150
  }
@@ -236,28 +236,6 @@ function literalUnion(...literals) {
236
236
  return v.union(...literals.map(v.literal));
237
237
  }
238
238
 
239
- // ../replicache/src/dag/store.ts
240
- var ChunkNotFoundError = class extends Error {
241
- name = "ChunkNotFoundError";
242
- hash;
243
- constructor(hash2) {
244
- super(`Chunk not found ${hash2}`);
245
- this.hash = hash2;
246
- }
247
- };
248
- async function mustGetChunk(store, hash2) {
249
- const chunk = await store.getChunk(hash2);
250
- if (chunk) {
251
- return chunk;
252
- }
253
- throw new ChunkNotFoundError(hash2);
254
- }
255
- async function mustGetHeadHash(name, store) {
256
- const hash2 = await store.getHead(name);
257
- assert(hash2, `Missing head ${name}`);
258
- return hash2;
259
- }
260
-
261
239
  // ../shared/src/json.ts
262
240
  function deepEqual(a, b) {
263
241
  if (a === b) {
@@ -698,13 +676,13 @@ var DataNodeImpl = class extends NodeImpl {
698
676
  }
699
677
  }
700
678
  };
701
- function readonlySplice(array6, start, deleteCount, ...items) {
702
- const arr = array6.slice(0, start);
679
+ function readonlySplice(array7, start, deleteCount, ...items) {
680
+ const arr = array7.slice(0, start);
703
681
  for (let i = 0; i < items.length; i++) {
704
682
  arr.push(items[i]);
705
683
  }
706
- for (let i = start + deleteCount; i < array6.length; i++) {
707
- arr.push(array6[i]);
684
+ for (let i = start + deleteCount; i < array7.length; i++) {
685
+ arr.push(array7[i]);
708
686
  }
709
687
  return arr;
710
688
  }
@@ -1359,6 +1337,28 @@ function createChunk(data, refs, chunkHasher) {
1359
1337
  return new Chunk(hash2, data, refs);
1360
1338
  }
1361
1339
 
1340
+ // ../replicache/src/dag/store.ts
1341
+ var ChunkNotFoundError = class extends Error {
1342
+ name = "ChunkNotFoundError";
1343
+ hash;
1344
+ constructor(hash2) {
1345
+ super(`Chunk not found ${hash2}`);
1346
+ this.hash = hash2;
1347
+ }
1348
+ };
1349
+ async function mustGetChunk(store, hash2) {
1350
+ const chunk = await store.getChunk(hash2);
1351
+ if (chunk) {
1352
+ return chunk;
1353
+ }
1354
+ throw new ChunkNotFoundError(hash2);
1355
+ }
1356
+ async function mustGetHeadHash(name, store) {
1357
+ const hash2 = await store.getHead(name);
1358
+ assert(hash2, `Missing head ${name}`);
1359
+ return hash2;
1360
+ }
1361
+
1362
1362
  // ../replicache/src/db/meta-type-enum.ts
1363
1363
  var LocalDD31 = 4;
1364
1364
  var SnapshotDD31 = 5;
@@ -3290,73 +3290,6 @@ function makeID(row, schema) {
3290
3290
  return JSON.stringify(schema.primaryKey.map((k) => row[k]));
3291
3291
  }
3292
3292
 
3293
- // ../zql/src/query/ttl.ts
3294
- var DEFAULT_TTL_MS = 1e3 * 60 * 5;
3295
- var DEFAULT_PRELOAD_TTL_MS = 0;
3296
- var MAX_TTL = "10m";
3297
- var MAX_TTL_MS = 1e3 * 60 * 10;
3298
- var multiplier = {
3299
- s: 1e3,
3300
- m: 60 * 1e3,
3301
- h: 60 * 60 * 1e3,
3302
- d: 24 * 60 * 60 * 1e3,
3303
- y: 365 * 24 * 60 * 60 * 1e3
3304
- };
3305
- function parseTTL(ttl) {
3306
- if (typeof ttl === "number") {
3307
- return Number.isNaN(ttl) ? 0 : !Number.isFinite(ttl) || ttl < 0 ? -1 : ttl;
3308
- }
3309
- if (ttl === "none") {
3310
- return 0;
3311
- }
3312
- if (ttl === "forever") {
3313
- return -1;
3314
- }
3315
- const multi = multiplier[ttl[ttl.length - 1]];
3316
- return Number(ttl.slice(0, -1)) * multi;
3317
- }
3318
- function compareTTL(a, b) {
3319
- const ap = parseTTL(a);
3320
- const bp = parseTTL(b);
3321
- if (ap === -1 && bp !== -1) {
3322
- return 1;
3323
- }
3324
- if (ap !== -1 && bp === -1) {
3325
- return -1;
3326
- }
3327
- return ap - bp;
3328
- }
3329
- function normalizeTTL(ttl) {
3330
- if (typeof ttl === "string") {
3331
- return ttl;
3332
- }
3333
- if (ttl < 0) {
3334
- return "forever";
3335
- }
3336
- if (ttl === 0) {
3337
- return "none";
3338
- }
3339
- let shortest = ttl.toString();
3340
- const lengthOfNumber = shortest.length;
3341
- for (const unit of ["y", "d", "h", "m", "s"]) {
3342
- const multi = multiplier[unit];
3343
- const value = ttl / multi;
3344
- const candidate = `${value}${unit}`;
3345
- if (candidate.length < shortest.length) {
3346
- shortest = candidate;
3347
- }
3348
- }
3349
- return shortest.length < lengthOfNumber ? shortest : ttl;
3350
- }
3351
- function clampTTL(ttl, lc) {
3352
- const parsedTTL = parseTTL(ttl);
3353
- if (parsedTTL === -1 || parsedTTL > 10 * 60 * 1e3) {
3354
- lc?.warn?.(`TTL (${ttl}) is too high, clamping to ${MAX_TTL}`);
3355
- return parseTTL(MAX_TTL);
3356
- }
3357
- return parsedTTL;
3358
- }
3359
-
3360
3293
  // ../shared/src/json-schema.ts
3361
3294
  import * as valita from "@badrap/valita";
3362
3295
  var path = [];
@@ -3386,6 +3319,27 @@ var jsonObjectSchema = valita_exports.unknown().chain((v2) => {
3386
3319
  // ../shared/src/tdigest-schema.ts
3387
3320
  var tdigestSchema = valita_exports.tuple([valita_exports.number()]).concat(valita_exports.array(valita_exports.number()));
3388
3321
 
3322
+ // ../zero-protocol/src/data.ts
3323
+ var valueSchema = valita_exports.union(jsonSchema, valita_exports.undefined());
3324
+ var rowSchema = readonlyRecord(valueSchema);
3325
+
3326
+ // ../zero-protocol/src/analyze-query-result.ts
3327
+ var rowCountsByQuerySchema = valita_exports.record(valita_exports.number());
3328
+ var rowCountsBySourceSchema = valita_exports.record(rowCountsByQuerySchema);
3329
+ var rowsByQuerySchema = valita_exports.record(valita_exports.array(rowSchema));
3330
+ var rowsBySourceSchema = valita_exports.record(rowsByQuerySchema);
3331
+ var analyzeQueryResultSchema = valita_exports.object({
3332
+ warnings: valita_exports.array(valita_exports.string()),
3333
+ syncedRows: valita_exports.record(valita_exports.array(rowSchema)).optional(),
3334
+ syncedRowCount: valita_exports.number(),
3335
+ start: valita_exports.number(),
3336
+ end: valita_exports.number(),
3337
+ afterPermissions: valita_exports.string().optional(),
3338
+ vendedRowCounts: rowCountsBySourceSchema.optional(),
3339
+ vendedRows: rowsBySourceSchema.optional(),
3340
+ plans: valita_exports.record(valita_exports.array(valita_exports.string())).optional()
3341
+ });
3342
+
3389
3343
  // ../zero-protocol/src/ast.ts
3390
3344
  import { compareUTF8 as compareUTF83 } from "compare-utf8";
3391
3345
 
@@ -3408,10 +3362,6 @@ function areEqual(arr1, arr2) {
3408
3362
  return arr1.length === arr2.length && arr1.every((e, i) => e === arr2[i]);
3409
3363
  }
3410
3364
 
3411
- // ../zero-protocol/src/data.ts
3412
- var valueSchema = valita_exports.union(jsonSchema, valita_exports.undefined());
3413
- var rowSchema = readonlyRecord(valueSchema);
3414
-
3415
3365
  // ../zero-protocol/src/ast.ts
3416
3366
  var selectorSchema = valita_exports.string();
3417
3367
  var toStaticParam = Symbol();
@@ -3509,7 +3459,8 @@ var correlationSchema = readonlyObject({
3509
3459
  var correlatedSubquerySchemaOmitSubquery = readonlyObject({
3510
3460
  correlation: correlationSchema,
3511
3461
  hidden: valita_exports.boolean().optional(),
3512
- system: literalUnion("permissions", "client", "test").optional()
3462
+ system: literalUnion("permissions", "client", "test").optional(),
3463
+ flip: valita_exports.boolean().optional()
3513
3464
  });
3514
3465
  var correlatedSubquerySchema = correlatedSubquerySchemaOmitSubquery.extend({
3515
3466
  subquery: valita_exports.lazy(() => astSchema)
@@ -3549,7 +3500,8 @@ function transformAST(ast, transform) {
3549
3500
  },
3550
3501
  hidden: r.hidden,
3551
3502
  subquery: transformAST(r.subquery, transform),
3552
- system: r.system
3503
+ system: r.system,
3504
+ flip: r.flip
3553
3505
  })
3554
3506
  )
3555
3507
  ) : void 0,
@@ -3760,11 +3712,16 @@ var inspectAuthenticatedDownSchema = inspectBaseDownSchema.extend({
3760
3712
  op: valita_exports.literal("authenticated"),
3761
3713
  value: valita_exports.boolean()
3762
3714
  });
3715
+ var inspectAnalyzeQueryDownSchema = inspectBaseDownSchema.extend({
3716
+ op: valita_exports.literal("analyze-query"),
3717
+ value: analyzeQueryResultSchema
3718
+ });
3763
3719
  var inspectDownBodySchema = valita_exports.union(
3764
3720
  inspectQueriesDownSchema,
3765
3721
  inspectMetricsDownSchema,
3766
3722
  inspectVersionDownSchema,
3767
- inspectAuthenticatedDownSchema
3723
+ inspectAuthenticatedDownSchema,
3724
+ inspectAnalyzeQueryDownSchema
3768
3725
  );
3769
3726
  var inspectDownMessageSchema = valita_exports.tuple([
3770
3727
  valita_exports.literal("inspect"),
@@ -3772,14 +3729,14 @@ var inspectDownMessageSchema = valita_exports.tuple([
3772
3729
  ]);
3773
3730
 
3774
3731
  // ../shared/src/random-values.ts
3775
- function getNonCryptoRandomValues(array6) {
3776
- if (array6 === null) {
3732
+ function getNonCryptoRandomValues(array7) {
3733
+ if (array7 === null) {
3777
3734
  throw new TypeError("array cannot be null");
3778
3735
  }
3779
- for (let i = 0; i < array6.length; i++) {
3780
- array6[i] = Math.floor(Math.random() * 256);
3736
+ for (let i = 0; i < array7.length; i++) {
3737
+ array7[i] = Math.floor(Math.random() * 256);
3781
3738
  }
3782
- return array6;
3739
+ return array7;
3783
3740
  }
3784
3741
 
3785
3742
  // ../zero-client/src/util/nanoid.ts
@@ -3856,6 +3813,56 @@ function sourceNameFromKey(key) {
3856
3813
  return key.slice(ENTITIES_KEY_PREFIX.length, slash);
3857
3814
  }
3858
3815
 
3816
+ // ../zero-client/src/client/inspector/client-group.ts
3817
+ var ClientGroup = class {
3818
+ #delegate;
3819
+ id;
3820
+ constructor(delegate, clientGroupID) {
3821
+ this.#delegate = delegate;
3822
+ this.id = clientGroupID;
3823
+ }
3824
+ async clients() {
3825
+ return (await this.#delegate.lazy).clientGroupClients(
3826
+ this.#delegate,
3827
+ this.id
3828
+ );
3829
+ }
3830
+ async clientsWithQueries() {
3831
+ return (await this.#delegate.lazy).clientGroupClientsWithQueries(
3832
+ this.#delegate,
3833
+ this.id
3834
+ );
3835
+ }
3836
+ async queries() {
3837
+ return (await this.#delegate.lazy).clientGroupQueries(this.#delegate);
3838
+ }
3839
+ };
3840
+
3841
+ // ../zero-client/src/client/inspector/client.ts
3842
+ var Client = class {
3843
+ #delegate;
3844
+ id;
3845
+ clientGroup;
3846
+ constructor(delegate, clientID, clientGroupID) {
3847
+ this.#delegate = delegate;
3848
+ this.id = clientID;
3849
+ this.clientGroup = new ClientGroup(this.#delegate, clientGroupID);
3850
+ }
3851
+ async queries() {
3852
+ return (await this.#delegate.lazy).clientQueries(this.#delegate, this.id);
3853
+ }
3854
+ async map() {
3855
+ return (await this.#delegate.lazy).clientMap(this.#delegate, this.id);
3856
+ }
3857
+ async rows(tableName) {
3858
+ return (await this.#delegate.lazy).clientRows(
3859
+ this.#delegate,
3860
+ this.id,
3861
+ tableName
3862
+ );
3863
+ }
3864
+ };
3865
+
3859
3866
  // ../shared/src/centroid.ts
3860
3867
  var Centroid = class {
3861
3868
  mean;
@@ -4159,6 +4166,41 @@ function unprocessedSize(size, compression) {
4159
4166
  return size;
4160
4167
  }
4161
4168
 
4169
+ // ../zero-client/src/client/inspector/inspector.ts
4170
+ var Inspector = class {
4171
+ #delegate;
4172
+ client;
4173
+ clientGroup;
4174
+ constructor(rep, delegate, getSocket) {
4175
+ this.#delegate = {
4176
+ getQueryMetrics: delegate.getQueryMetrics.bind(delegate),
4177
+ getAST: delegate.getAST.bind(delegate),
4178
+ get metrics() {
4179
+ return delegate.metrics;
4180
+ },
4181
+ rep,
4182
+ getSocket,
4183
+ lazy: import("./lazy-inspector-TOTYUTBC.js")
4184
+ };
4185
+ this.client = new Client(this.#delegate, rep.clientID, rep.clientGroupID);
4186
+ this.clientGroup = this.client.clientGroup;
4187
+ }
4188
+ async metrics() {
4189
+ return (await this.#delegate.lazy).inspectorMetrics(this.#delegate);
4190
+ }
4191
+ async clients() {
4192
+ return (await this.#delegate.lazy).inspectorClients(this.#delegate);
4193
+ }
4194
+ async clientsWithQueries() {
4195
+ return (await this.#delegate.lazy).inspectorClientsWithQueries(
4196
+ this.#delegate
4197
+ );
4198
+ }
4199
+ async serverVersion() {
4200
+ return (await this.#delegate.lazy).serverVersion(this.#delegate);
4201
+ }
4202
+ };
4203
+
4162
4204
  // ../zero-schema/src/table-schema.ts
4163
4205
  function isOneHop(r) {
4164
4206
  return r.length === 1;
@@ -4167,9 +4209,85 @@ function isTwoHop(r) {
4167
4209
  return r.length === 2;
4168
4210
  }
4169
4211
 
4212
+ // ../shared/src/sentinels.ts
4213
+ function emptyFunction() {
4214
+ }
4215
+ var emptyObject = Object.freeze({});
4216
+ var emptyArray = Object.freeze([]);
4217
+ function identity(x) {
4218
+ return x;
4219
+ }
4220
+
4170
4221
  // ../zql/src/query/query.ts
4171
4222
  var delegateSymbol = Symbol("delegate");
4172
4223
 
4224
+ // ../zql/src/query/ttl.ts
4225
+ var DEFAULT_TTL_MS = 1e3 * 60 * 5;
4226
+ var DEFAULT_PRELOAD_TTL_MS = 0;
4227
+ var MAX_TTL = "10m";
4228
+ var MAX_TTL_MS = 1e3 * 60 * 10;
4229
+ var multiplier = {
4230
+ s: 1e3,
4231
+ m: 60 * 1e3,
4232
+ h: 60 * 60 * 1e3,
4233
+ d: 24 * 60 * 60 * 1e3,
4234
+ y: 365 * 24 * 60 * 60 * 1e3
4235
+ };
4236
+ function parseTTL(ttl) {
4237
+ if (typeof ttl === "number") {
4238
+ return Number.isNaN(ttl) ? 0 : !Number.isFinite(ttl) || ttl < 0 ? -1 : ttl;
4239
+ }
4240
+ if (ttl === "none") {
4241
+ return 0;
4242
+ }
4243
+ if (ttl === "forever") {
4244
+ return -1;
4245
+ }
4246
+ const multi = multiplier[ttl[ttl.length - 1]];
4247
+ return Number(ttl.slice(0, -1)) * multi;
4248
+ }
4249
+ function compareTTL(a, b) {
4250
+ const ap = parseTTL(a);
4251
+ const bp = parseTTL(b);
4252
+ if (ap === -1 && bp !== -1) {
4253
+ return 1;
4254
+ }
4255
+ if (ap !== -1 && bp === -1) {
4256
+ return -1;
4257
+ }
4258
+ return ap - bp;
4259
+ }
4260
+ function normalizeTTL(ttl) {
4261
+ if (typeof ttl === "string") {
4262
+ return ttl;
4263
+ }
4264
+ if (ttl < 0) {
4265
+ return "forever";
4266
+ }
4267
+ if (ttl === 0) {
4268
+ return "none";
4269
+ }
4270
+ let shortest = ttl.toString();
4271
+ const lengthOfNumber = shortest.length;
4272
+ for (const unit of ["y", "d", "h", "m", "s"]) {
4273
+ const multi = multiplier[unit];
4274
+ const value = ttl / multi;
4275
+ const candidate = `${value}${unit}`;
4276
+ if (candidate.length < shortest.length) {
4277
+ shortest = candidate;
4278
+ }
4279
+ }
4280
+ return shortest.length < lengthOfNumber ? shortest : ttl;
4281
+ }
4282
+ function clampTTL(ttl, lc) {
4283
+ const parsedTTL = parseTTL(ttl);
4284
+ if (parsedTTL === -1 || parsedTTL > 10 * 60 * 1e3) {
4285
+ lc?.warn?.(`TTL (${ttl}) is too high, clamping to ${MAX_TTL}`);
4286
+ return parseTTL(MAX_TTL);
4287
+ }
4288
+ return parsedTTL;
4289
+ }
4290
+
4173
4291
  // ../zql/src/query/query-impl.ts
4174
4292
  import { resolver } from "@rocicorp/resolver";
4175
4293
 
@@ -4533,132 +4651,156 @@ var Exists = class {
4533
4651
  }
4534
4652
  };
4535
4653
 
4536
- // ../zql/src/ivm/fan-in.ts
4537
- var FanIn = class {
4538
- #inputs;
4539
- #schema;
4540
- #output = throwFilterOutput;
4541
- #accumulatedPushes = [];
4542
- constructor(fanOut, inputs) {
4543
- this.#inputs = inputs;
4544
- this.#schema = fanOut.getSchema();
4545
- for (const input of inputs) {
4546
- input.setFilterOutput(this);
4547
- assert(this.#schema === input.getSchema(), `Schema mismatch in fan-in`);
4548
- }
4549
- }
4550
- setFilterOutput(output) {
4551
- this.#output = output;
4654
+ // ../zql/src/ivm/push-accumulated.ts
4655
+ function pushAccumulatedChanges(accumulatedPushes, output, fanOutChangeType, mergeRelationships, addEmptyRelationships) {
4656
+ if (accumulatedPushes.length === 0) {
4657
+ return;
4552
4658
  }
4553
- destroy() {
4554
- for (const input of this.#inputs) {
4555
- input.destroy();
4659
+ const candidatesToPush = /* @__PURE__ */ new Map();
4660
+ for (const change of accumulatedPushes) {
4661
+ if (fanOutChangeType === "child" && change.type !== "child") {
4662
+ assert(
4663
+ candidatesToPush.has(change.type) === false,
4664
+ () => `Fan-in:child expected at most one ${change.type} when fan-out is of type child`
4665
+ );
4556
4666
  }
4667
+ const existing = candidatesToPush.get(change.type);
4668
+ let mergedChange = change;
4669
+ if (existing) {
4670
+ mergedChange = mergeRelationships(existing, change);
4671
+ }
4672
+ candidatesToPush.set(change.type, mergedChange);
4557
4673
  }
4558
- getSchema() {
4559
- return this.#schema;
4560
- }
4561
- filter(node, cleanup) {
4562
- return this.#output.filter(node, cleanup);
4563
- }
4564
- push(change) {
4565
- this.#accumulatedPushes.push(change);
4566
- }
4567
- fanOutDonePushingToAllBranches(fanOutChangeType) {
4568
- if (this.#inputs.length === 0) {
4674
+ accumulatedPushes.length = 0;
4675
+ const types = [...candidatesToPush.keys()];
4676
+ switch (fanOutChangeType) {
4677
+ case "remove":
4569
4678
  assert(
4570
- this.#accumulatedPushes.length === 0,
4571
- "If there are no inputs then fan-in should not receive any pushes."
4679
+ types.length === 1 && types[0] === "remove",
4680
+ "Fan-in:remove expected all removes"
4572
4681
  );
4682
+ output.push(addEmptyRelationships(must(candidatesToPush.get("remove"))));
4573
4683
  return;
4574
- }
4575
- if (this.#accumulatedPushes.length === 0) {
4576
- return;
4577
- }
4578
- const candidatesToPush = /* @__PURE__ */ new Map();
4579
- for (const change of this.#accumulatedPushes) {
4580
- if (fanOutChangeType === "child" && change.type !== "child") {
4581
- assert(
4582
- candidatesToPush.has(change.type) === false,
4583
- () => `Fan-in:child expected at most one ${change.type} when fan-out is of type child`
4584
- );
4585
- }
4586
- candidatesToPush.set(change.type, change);
4587
- }
4588
- this.#accumulatedPushes = [];
4589
- const types = [...candidatesToPush.keys()];
4590
- switch (fanOutChangeType) {
4591
- case "remove":
4592
- assert(
4593
- types.length === 1 && types[0] === "remove",
4594
- "Fan-in:remove expected all removes"
4595
- );
4596
- this.#output.push(must(candidatesToPush.get("remove")));
4597
- return;
4598
- case "add":
4599
- assert(
4600
- types.length === 1 && types[0] === "add",
4601
- "Fan-in:add expected all adds"
4602
- );
4603
- this.#output.push(must(candidatesToPush.get("add")));
4604
- return;
4605
- case "edit": {
4606
- assert(
4607
- types.every(
4608
- (type) => type === "add" || type === "remove" || type === "edit"
4609
- ),
4610
- "Fan-in:edit expected all adds, removes, or edits"
4611
- );
4612
- const addChange = candidatesToPush.get("add");
4613
- const removeChange = candidatesToPush.get("remove");
4614
- const editChange = candidatesToPush.get("edit");
4615
- if (editChange) {
4616
- this.#output.push(editChange);
4617
- return;
4684
+ case "add":
4685
+ assert(
4686
+ types.length === 1 && types[0] === "add",
4687
+ "Fan-in:add expected all adds"
4688
+ );
4689
+ output.push(addEmptyRelationships(must(candidatesToPush.get("add"))));
4690
+ return;
4691
+ case "edit": {
4692
+ assert(
4693
+ types.every(
4694
+ (type) => type === "add" || type === "remove" || type === "edit"
4695
+ ),
4696
+ "Fan-in:edit expected all adds, removes, or edits"
4697
+ );
4698
+ const addChange = candidatesToPush.get("add");
4699
+ const removeChange = candidatesToPush.get("remove");
4700
+ let editChange = candidatesToPush.get("edit");
4701
+ if (editChange) {
4702
+ if (addChange) {
4703
+ editChange = mergeRelationships(editChange, addChange);
4618
4704
  }
4619
- if (addChange && removeChange) {
4620
- this.#output.push({
4705
+ if (removeChange) {
4706
+ editChange = mergeRelationships(editChange, removeChange);
4707
+ }
4708
+ output.push(addEmptyRelationships(editChange));
4709
+ return;
4710
+ }
4711
+ if (addChange && removeChange) {
4712
+ output.push(
4713
+ addEmptyRelationships({
4621
4714
  type: "edit",
4622
4715
  node: addChange.node,
4623
4716
  oldNode: removeChange.node
4624
- });
4625
- return;
4626
- }
4627
- this.#output.push(must(addChange ?? removeChange));
4717
+ })
4718
+ );
4628
4719
  return;
4629
4720
  }
4630
- case "child": {
4631
- assert(
4632
- types.every(
4633
- (type) => type === "add" || // exists can change child to add or remove
4634
- type === "remove" || // exists can change child to add or remove
4635
- type === "child"
4636
- // other operators may preserve the child change
4637
- ),
4638
- "Fan-in:child expected all adds, removes, or children"
4639
- );
4640
- assert(
4641
- types.length <= 2,
4642
- "Fan-in:child expected at most 2 types on a child change from fan-out"
4643
- );
4644
- const childChange = candidatesToPush.get("child");
4645
- if (childChange) {
4646
- this.#output.push(childChange);
4647
- return;
4648
- }
4649
- const addChange = candidatesToPush.get("add");
4650
- const removeChange = candidatesToPush.get("remove");
4651
- assert(
4652
- addChange === void 0 || removeChange === void 0,
4653
- "Fan-in:child expected either add or remove, not both"
4654
- );
4655
- this.#output.push(must(addChange ?? removeChange));
4721
+ output.push(addEmptyRelationships(must(addChange ?? removeChange)));
4722
+ return;
4723
+ }
4724
+ case "child": {
4725
+ assert(
4726
+ types.every(
4727
+ (type) => type === "add" || // exists can change child to add or remove
4728
+ type === "remove" || // exists can change child to add or remove
4729
+ type === "child"
4730
+ // other operators may preserve the child change
4731
+ ),
4732
+ "Fan-in:child expected all adds, removes, or children"
4733
+ );
4734
+ assert(
4735
+ types.length <= 2,
4736
+ "Fan-in:child expected at most 2 types on a child change from fan-out"
4737
+ );
4738
+ const childChange = candidatesToPush.get("child");
4739
+ if (childChange) {
4740
+ output.push(childChange);
4656
4741
  return;
4657
4742
  }
4658
- default:
4659
- fanOutChangeType;
4743
+ const addChange = candidatesToPush.get("add");
4744
+ const removeChange = candidatesToPush.get("remove");
4745
+ assert(
4746
+ addChange === void 0 || removeChange === void 0,
4747
+ "Fan-in:child expected either add or remove, not both"
4748
+ );
4749
+ output.push(addEmptyRelationships(must(addChange ?? removeChange)));
4750
+ return;
4751
+ }
4752
+ default:
4753
+ fanOutChangeType;
4754
+ }
4755
+ }
4756
+
4757
+ // ../zql/src/ivm/fan-in.ts
4758
+ var FanIn = class {
4759
+ #inputs;
4760
+ #schema;
4761
+ #output = throwFilterOutput;
4762
+ #accumulatedPushes = [];
4763
+ constructor(fanOut, inputs) {
4764
+ this.#inputs = inputs;
4765
+ this.#schema = fanOut.getSchema();
4766
+ for (const input of inputs) {
4767
+ input.setFilterOutput(this);
4768
+ assert(this.#schema === input.getSchema(), `Schema mismatch in fan-in`);
4660
4769
  }
4661
4770
  }
4771
+ setFilterOutput(output) {
4772
+ this.#output = output;
4773
+ }
4774
+ destroy() {
4775
+ for (const input of this.#inputs) {
4776
+ input.destroy();
4777
+ }
4778
+ }
4779
+ getSchema() {
4780
+ return this.#schema;
4781
+ }
4782
+ filter(node, cleanup) {
4783
+ return this.#output.filter(node, cleanup);
4784
+ }
4785
+ push(change) {
4786
+ this.#accumulatedPushes.push(change);
4787
+ }
4788
+ fanOutDonePushingToAllBranches(fanOutChangeType) {
4789
+ if (this.#inputs.length === 0) {
4790
+ assert(
4791
+ this.#accumulatedPushes.length === 0,
4792
+ "If there are no inputs then fan-in should not receive any pushes."
4793
+ );
4794
+ return;
4795
+ }
4796
+ pushAccumulatedChanges(
4797
+ this.#accumulatedPushes,
4798
+ this.#output,
4799
+ fanOutChangeType,
4800
+ identity,
4801
+ identity
4802
+ );
4803
+ }
4662
4804
  };
4663
4805
 
4664
4806
  // ../zql/src/ivm/fan-out.ts
@@ -4679,10 +4821,10 @@ var FanOut = class {
4679
4821
  }
4680
4822
  destroy() {
4681
4823
  if (this.#destroyCount < this.#outputs.length) {
4682
- if (this.#destroyCount === 0) {
4824
+ ++this.#destroyCount;
4825
+ if (this.#destroyCount === this.#outputs.length) {
4683
4826
  this.#input.destroy();
4684
4827
  }
4685
- ++this.#destroyCount;
4686
4828
  } else {
4687
4829
  throw new Error("FanOut already destroyed once for each output");
4688
4830
  }
@@ -4783,6 +4925,446 @@ var Filter = class {
4783
4925
  }
4784
4926
  };
4785
4927
 
4928
+ // ../zql/src/ivm/join-utils.ts
4929
+ function* generateWithOverlay(stream, overlay, schema) {
4930
+ let applied = false;
4931
+ let editOldApplied = false;
4932
+ let editNewApplied = false;
4933
+ for (const node of stream) {
4934
+ let yieldNode = true;
4935
+ if (!applied) {
4936
+ switch (overlay.type) {
4937
+ case "add": {
4938
+ if (schema.compareRows(overlay.node.row, node.row) === 0) {
4939
+ applied = true;
4940
+ yieldNode = false;
4941
+ }
4942
+ break;
4943
+ }
4944
+ case "remove": {
4945
+ if (schema.compareRows(overlay.node.row, node.row) < 0) {
4946
+ applied = true;
4947
+ yield overlay.node;
4948
+ }
4949
+ break;
4950
+ }
4951
+ case "edit": {
4952
+ if (!editOldApplied && schema.compareRows(overlay.oldNode.row, node.row) < 0) {
4953
+ editOldApplied = true;
4954
+ if (editNewApplied) {
4955
+ applied = true;
4956
+ }
4957
+ yield overlay.oldNode;
4958
+ }
4959
+ if (!editNewApplied && schema.compareRows(overlay.node.row, node.row) === 0) {
4960
+ editNewApplied = true;
4961
+ if (editOldApplied) {
4962
+ applied = true;
4963
+ }
4964
+ yieldNode = false;
4965
+ }
4966
+ break;
4967
+ }
4968
+ case "child": {
4969
+ if (schema.compareRows(overlay.node.row, node.row) === 0) {
4970
+ applied = true;
4971
+ yield {
4972
+ row: node.row,
4973
+ relationships: {
4974
+ ...node.relationships,
4975
+ [overlay.child.relationshipName]: () => generateWithOverlay(
4976
+ node.relationships[overlay.child.relationshipName](),
4977
+ overlay.child.change,
4978
+ schema.relationships[overlay.child.relationshipName]
4979
+ )
4980
+ }
4981
+ };
4982
+ yieldNode = false;
4983
+ }
4984
+ break;
4985
+ }
4986
+ }
4987
+ }
4988
+ if (yieldNode) {
4989
+ yield node;
4990
+ }
4991
+ }
4992
+ if (!applied) {
4993
+ if (overlay.type === "remove") {
4994
+ applied = true;
4995
+ yield overlay.node;
4996
+ } else if (overlay.type === "edit") {
4997
+ assert(editNewApplied);
4998
+ editOldApplied = true;
4999
+ applied = true;
5000
+ yield overlay.oldNode;
5001
+ }
5002
+ }
5003
+ assert(applied);
5004
+ }
5005
+ function rowEqualsForCompoundKey(a, b, key) {
5006
+ for (let i = 0; i < key.length; i++) {
5007
+ if (compareValues(a[key[i]], b[key[i]]) !== 0) {
5008
+ return false;
5009
+ }
5010
+ }
5011
+ return true;
5012
+ }
5013
+ function isJoinMatch(parent, parentKey, child, childKey) {
5014
+ for (let i = 0; i < parentKey.length; i++) {
5015
+ if (!valuesEqual(parent[parentKey[i]], child[childKey[i]])) {
5016
+ return false;
5017
+ }
5018
+ }
5019
+ return true;
5020
+ }
5021
+
5022
+ // ../zql/src/ivm/flipped-join.ts
5023
+ var FlippedJoin = class {
5024
+ #parent;
5025
+ #child;
5026
+ #parentKey;
5027
+ #childKey;
5028
+ #relationshipName;
5029
+ #schema;
5030
+ #output = throwOutput;
5031
+ #inprogressChildChange;
5032
+ constructor({
5033
+ parent,
5034
+ child,
5035
+ parentKey,
5036
+ childKey,
5037
+ relationshipName,
5038
+ hidden,
5039
+ system
5040
+ }) {
5041
+ assert(parent !== child, "Parent and child must be different operators");
5042
+ assert(
5043
+ parentKey.length === childKey.length,
5044
+ "The parentKey and childKey keys must have same length"
5045
+ );
5046
+ this.#parent = parent;
5047
+ this.#child = child;
5048
+ this.#parentKey = parentKey;
5049
+ this.#childKey = childKey;
5050
+ this.#relationshipName = relationshipName;
5051
+ const parentSchema = parent.getSchema();
5052
+ const childSchema = child.getSchema();
5053
+ this.#schema = {
5054
+ ...parentSchema,
5055
+ relationships: {
5056
+ ...parentSchema.relationships,
5057
+ [relationshipName]: {
5058
+ ...childSchema,
5059
+ isHidden: hidden,
5060
+ system
5061
+ }
5062
+ }
5063
+ };
5064
+ parent.setOutput({
5065
+ push: (change) => this.#pushParent(change)
5066
+ });
5067
+ child.setOutput({
5068
+ push: (change) => this.#pushChild(change)
5069
+ });
5070
+ }
5071
+ destroy() {
5072
+ this.#child.destroy();
5073
+ this.#parent.destroy();
5074
+ }
5075
+ setOutput(output) {
5076
+ this.#output = output;
5077
+ }
5078
+ getSchema() {
5079
+ return this.#schema;
5080
+ }
5081
+ // TODO: When parentKey is the parent's primary key (or more
5082
+ // generally when the parent cardinality is expected to be small) a different
5083
+ // algorithm should be used: For each child node, fetch all parent nodes
5084
+ // eagerly and then sort using quicksort.
5085
+ *fetch(req) {
5086
+ const childNodes = [...this.#child.fetch({})];
5087
+ if (this.#inprogressChildChange?.change.type === "remove") {
5088
+ const removedNode = this.#inprogressChildChange.change.node;
5089
+ const compare = this.#child.getSchema().compareRows;
5090
+ const insertPos = binarySearch(
5091
+ childNodes.length,
5092
+ (i) => compare(childNodes[i].row, removedNode.row)
5093
+ );
5094
+ childNodes.splice(insertPos, 0, removedNode);
5095
+ }
5096
+ const parentIterators = [];
5097
+ let threw = false;
5098
+ try {
5099
+ for (const childNode of childNodes) {
5100
+ const stream = this.#parent.fetch({
5101
+ ...req,
5102
+ constraint: {
5103
+ ...req.constraint,
5104
+ ...Object.fromEntries(
5105
+ this.#parentKey.map((key, i) => [
5106
+ key,
5107
+ childNode.row[this.#childKey[i]]
5108
+ ])
5109
+ )
5110
+ }
5111
+ });
5112
+ const iterator = stream[Symbol.iterator]();
5113
+ parentIterators.push(iterator);
5114
+ }
5115
+ const nextParentNodes = [];
5116
+ for (let i = 0; i < parentIterators.length; i++) {
5117
+ const iter = parentIterators[i];
5118
+ const result = iter.next();
5119
+ nextParentNodes[i] = result.done ? null : result.value;
5120
+ }
5121
+ while (true) {
5122
+ let minParentNode = null;
5123
+ let minParentNodeChildIndexes = [];
5124
+ for (let i = 0; i < nextParentNodes.length; i++) {
5125
+ const parentNode = nextParentNodes[i];
5126
+ if (parentNode === null) {
5127
+ continue;
5128
+ }
5129
+ if (minParentNode === null) {
5130
+ minParentNode = parentNode;
5131
+ minParentNodeChildIndexes.push(i);
5132
+ } else {
5133
+ const compareResult = this.#schema.compareRows(parentNode.row, minParentNode.row) * (req.reverse ? -1 : 1);
5134
+ if (compareResult === 0) {
5135
+ minParentNodeChildIndexes.push(i);
5136
+ } else if (compareResult < 0) {
5137
+ minParentNode = parentNode;
5138
+ minParentNodeChildIndexes = [i];
5139
+ }
5140
+ }
5141
+ }
5142
+ if (minParentNode === null) {
5143
+ return;
5144
+ }
5145
+ const relatedChildNodes = [];
5146
+ for (const minParentNodeChildIndex of minParentNodeChildIndexes) {
5147
+ relatedChildNodes.push(childNodes[minParentNodeChildIndex]);
5148
+ const iter = parentIterators[minParentNodeChildIndex];
5149
+ const result = iter.next();
5150
+ nextParentNodes[minParentNodeChildIndex] = result.done ? null : result.value;
5151
+ }
5152
+ let overlaidRelatedChildNodes = relatedChildNodes;
5153
+ if (this.#inprogressChildChange && this.#inprogressChildChange.position && isJoinMatch(
5154
+ this.#inprogressChildChange.change.node.row,
5155
+ this.#childKey,
5156
+ minParentNode.row,
5157
+ this.#parentKey
5158
+ )) {
5159
+ if (this.#inprogressChildChange.change.type === "remove") {
5160
+ if (this.#parent.getSchema().compareRows(
5161
+ minParentNode.row,
5162
+ this.#inprogressChildChange.position
5163
+ ) <= 0) {
5164
+ overlaidRelatedChildNodes = relatedChildNodes.filter(
5165
+ (n) => n !== this.#inprogressChildChange?.change.node
5166
+ );
5167
+ }
5168
+ } else if (this.#parent.getSchema().compareRows(
5169
+ minParentNode.row,
5170
+ this.#inprogressChildChange.position
5171
+ ) > 0) {
5172
+ overlaidRelatedChildNodes = [
5173
+ ...generateWithOverlay(
5174
+ relatedChildNodes,
5175
+ this.#inprogressChildChange.change,
5176
+ this.#child.getSchema()
5177
+ )
5178
+ ];
5179
+ }
5180
+ }
5181
+ if (overlaidRelatedChildNodes.length > 0) {
5182
+ yield {
5183
+ ...minParentNode,
5184
+ relationships: {
5185
+ ...minParentNode.relationships,
5186
+ [this.#relationshipName]: () => overlaidRelatedChildNodes
5187
+ }
5188
+ };
5189
+ }
5190
+ }
5191
+ } catch (e) {
5192
+ threw = true;
5193
+ for (const iter of parentIterators) {
5194
+ try {
5195
+ iter.throw?.(e);
5196
+ } catch (_cleanupError) {
5197
+ }
5198
+ }
5199
+ throw e;
5200
+ } finally {
5201
+ if (!threw) {
5202
+ for (const iter of parentIterators) {
5203
+ try {
5204
+ iter.return?.();
5205
+ } catch (_cleanupError) {
5206
+ }
5207
+ }
5208
+ }
5209
+ }
5210
+ }
5211
+ *cleanup(_req) {
5212
+ }
5213
+ #pushChild(change) {
5214
+ const pushChildChange = (exists) => {
5215
+ this.#inprogressChildChange = {
5216
+ change,
5217
+ position: void 0
5218
+ };
5219
+ try {
5220
+ const parentNodeStream = this.#parent.fetch({
5221
+ constraint: Object.fromEntries(
5222
+ this.#parentKey.map((key, i) => [
5223
+ key,
5224
+ change.node.row[this.#childKey[i]]
5225
+ ])
5226
+ )
5227
+ });
5228
+ for (const parentNode of parentNodeStream) {
5229
+ this.#inprogressChildChange = {
5230
+ change,
5231
+ position: parentNode.row
5232
+ };
5233
+ const childNodeStream = () => this.#child.fetch({
5234
+ constraint: Object.fromEntries(
5235
+ this.#childKey.map((key, i) => [
5236
+ key,
5237
+ parentNode.row[this.#parentKey[i]]
5238
+ ])
5239
+ )
5240
+ });
5241
+ if (!exists) {
5242
+ for (const childNode of childNodeStream()) {
5243
+ if (this.#child.getSchema().compareRows(childNode.row, change.node.row) !== 0) {
5244
+ exists = true;
5245
+ break;
5246
+ }
5247
+ }
5248
+ }
5249
+ if (exists) {
5250
+ this.#output.push({
5251
+ type: "child",
5252
+ node: {
5253
+ ...parentNode,
5254
+ relationships: {
5255
+ ...parentNode.relationships,
5256
+ [this.#relationshipName]: childNodeStream
5257
+ }
5258
+ },
5259
+ child: {
5260
+ relationshipName: this.#relationshipName,
5261
+ change
5262
+ }
5263
+ });
5264
+ } else {
5265
+ this.#output.push({
5266
+ ...change,
5267
+ node: {
5268
+ ...parentNode,
5269
+ relationships: {
5270
+ ...parentNode.relationships,
5271
+ [this.#relationshipName]: () => [change.node]
5272
+ }
5273
+ }
5274
+ });
5275
+ }
5276
+ }
5277
+ } finally {
5278
+ this.#inprogressChildChange = void 0;
5279
+ }
5280
+ };
5281
+ switch (change.type) {
5282
+ case "add":
5283
+ case "remove":
5284
+ pushChildChange();
5285
+ break;
5286
+ case "edit": {
5287
+ assert(
5288
+ rowEqualsForCompoundKey(
5289
+ change.oldNode.row,
5290
+ change.node.row,
5291
+ this.#childKey
5292
+ ),
5293
+ `Child edit must not change relationship.`
5294
+ );
5295
+ pushChildChange(true);
5296
+ break;
5297
+ }
5298
+ case "child":
5299
+ pushChildChange(true);
5300
+ break;
5301
+ }
5302
+ }
5303
+ #pushParent(change) {
5304
+ const childNodeStream = (node) => () => this.#child.fetch({
5305
+ constraint: Object.fromEntries(
5306
+ this.#childKey.map((key, i) => [key, node.row[this.#parentKey[i]]])
5307
+ )
5308
+ });
5309
+ const flip = (node) => ({
5310
+ ...node,
5311
+ relationships: {
5312
+ ...node.relationships,
5313
+ [this.#relationshipName]: childNodeStream(node)
5314
+ }
5315
+ });
5316
+ switch (change.type) {
5317
+ case "add":
5318
+ case "remove":
5319
+ case "child": {
5320
+ if (first(childNodeStream(change.node)()) === void 0) {
5321
+ return;
5322
+ }
5323
+ this.#output.push({
5324
+ ...change,
5325
+ node: flip(change.node)
5326
+ });
5327
+ break;
5328
+ }
5329
+ case "edit": {
5330
+ const oldHasChild = first(childNodeStream(change.oldNode)()) !== void 0;
5331
+ const hasChild = first(childNodeStream(change.node)()) !== void 0;
5332
+ assert(
5333
+ rowEqualsForCompoundKey(
5334
+ change.oldNode.row,
5335
+ change.node.row,
5336
+ this.#parentKey
5337
+ ),
5338
+ `Parent edit must not change relationship.`
5339
+ );
5340
+ if (oldHasChild && hasChild) {
5341
+ this.#output.push({
5342
+ type: "edit",
5343
+ oldNode: flip(change.oldNode),
5344
+ node: flip(change.node)
5345
+ });
5346
+ break;
5347
+ }
5348
+ if (oldHasChild) {
5349
+ this.#output.push({
5350
+ type: "remove",
5351
+ node: flip(change.node)
5352
+ });
5353
+ }
5354
+ if (hasChild) {
5355
+ this.#output.push({
5356
+ type: "add",
5357
+ node: flip(change.node)
5358
+ });
5359
+ }
5360
+ break;
5361
+ }
5362
+ default:
5363
+ unreachable(change);
5364
+ }
5365
+ }
5366
+ };
5367
+
4786
5368
  // ../zql/src/ivm/join.ts
4787
5369
  var Join = class {
4788
5370
  #parent;
@@ -4978,81 +5560,6 @@ var Join = class {
4978
5560
  unreachable(change);
4979
5561
  }
4980
5562
  }
4981
- *#generateChildStreamWithOverlay(stream, overlay) {
4982
- let applied = false;
4983
- let editOldApplied = false;
4984
- let editNewApplied = false;
4985
- for (const child of stream) {
4986
- let yieldChild = true;
4987
- if (!applied) {
4988
- switch (overlay.type) {
4989
- case "add": {
4990
- if (this.#child.getSchema().compareRows(overlay.node.row, child.row) === 0) {
4991
- applied = true;
4992
- yieldChild = false;
4993
- }
4994
- break;
4995
- }
4996
- case "remove": {
4997
- if (this.#child.getSchema().compareRows(overlay.node.row, child.row) < 0) {
4998
- applied = true;
4999
- yield overlay.node;
5000
- }
5001
- break;
5002
- }
5003
- case "edit": {
5004
- if (this.#child.getSchema().compareRows(overlay.oldNode.row, child.row) < 0) {
5005
- editOldApplied = true;
5006
- if (editNewApplied) {
5007
- applied = true;
5008
- }
5009
- yield overlay.oldNode;
5010
- }
5011
- if (this.#child.getSchema().compareRows(overlay.node.row, child.row) === 0) {
5012
- editNewApplied = true;
5013
- if (editOldApplied) {
5014
- applied = true;
5015
- }
5016
- yieldChild = false;
5017
- }
5018
- break;
5019
- }
5020
- case "child": {
5021
- if (this.#child.getSchema().compareRows(overlay.node.row, child.row) === 0) {
5022
- applied = true;
5023
- yield {
5024
- row: child.row,
5025
- relationships: {
5026
- ...child.relationships,
5027
- [overlay.child.relationshipName]: () => this.#generateChildStreamWithOverlay(
5028
- child.relationships[overlay.child.relationshipName](),
5029
- overlay.child.change
5030
- )
5031
- }
5032
- };
5033
- yieldChild = false;
5034
- }
5035
- break;
5036
- }
5037
- }
5038
- }
5039
- if (yieldChild) {
5040
- yield child;
5041
- }
5042
- }
5043
- if (!applied) {
5044
- if (overlay.type === "remove") {
5045
- applied = true;
5046
- yield overlay.node;
5047
- } else if (overlay.type === "edit") {
5048
- assert(editNewApplied);
5049
- editOldApplied = true;
5050
- applied = true;
5051
- yield overlay.oldNode;
5052
- }
5053
- }
5054
- assert(applied);
5055
- }
5056
5563
  #processParentNode(parentNodeRow, parentNodeRelations, mode) {
5057
5564
  let method = mode;
5058
5565
  let storageUpdated = false;
@@ -5096,16 +5603,19 @@ var Join = class {
5096
5603
  ])
5097
5604
  )
5098
5605
  });
5099
- if (this.#inprogressChildChange && this.#isJoinMatch(
5606
+ if (this.#inprogressChildChange && isJoinMatch(
5100
5607
  parentNodeRow,
5101
- this.#inprogressChildChange.change.node.row
5608
+ this.#parentKey,
5609
+ this.#inprogressChildChange.change.node.row,
5610
+ this.#childKey
5102
5611
  ) && this.#inprogressChildChange.position && this.#schema.compareRows(
5103
5612
  parentNodeRow,
5104
5613
  this.#inprogressChildChange.position
5105
5614
  ) > 0) {
5106
- return this.#generateChildStreamWithOverlay(
5615
+ return generateWithOverlay(
5107
5616
  stream,
5108
- this.#inprogressChildChange.change
5617
+ this.#inprogressChildChange.change,
5618
+ this.#child.getSchema()
5109
5619
  );
5110
5620
  }
5111
5621
  return stream;
@@ -5118,14 +5628,6 @@ var Join = class {
5118
5628
  }
5119
5629
  };
5120
5630
  }
5121
- #isJoinMatch(parent, child) {
5122
- for (let i = 0; i < this.#parentKey.length; i++) {
5123
- if (!valuesEqual(parent[this.#parentKey[i]], child[this.#childKey[i]])) {
5124
- return false;
5125
- }
5126
- }
5127
- return true;
5128
- }
5129
5631
  };
5130
5632
  function makeStorageKeyForValues(values) {
5131
5633
  const json = JSON.stringify(["pKeySet", ...values]);
@@ -5141,14 +5643,6 @@ function makeStorageKey(key, primaryKey, row) {
5141
5643
  }
5142
5644
  return makeStorageKeyForValues(values);
5143
5645
  }
5144
- function rowEqualsForCompoundKey(a, b, key) {
5145
- for (let i = 0; i < key.length; i++) {
5146
- if (compareValues(a[key[i]], b[key[i]]) !== 0) {
5147
- return false;
5148
- }
5149
- }
5150
- return true;
5151
- }
5152
5646
 
5153
5647
  // ../zql/src/ivm/skip.ts
5154
5648
  var Skip = class {
@@ -5767,7 +6261,7 @@ var ExpressionBuilder = class {
5767
6261
  and = and;
5768
6262
  or = or;
5769
6263
  not = not;
5770
- exists = (relationship, cb) => this.#exists(relationship, cb);
6264
+ exists = (relationship, cb, options) => this.#exists(relationship, cb, options);
5771
6265
  };
5772
6266
  function and(...conditions) {
5773
6267
  const expressions = filterTrue(filterUndefined(conditions));
@@ -5902,8 +6396,8 @@ var negateOperatorMap = {
5902
6396
  function negateOperator(op) {
5903
6397
  return must(negateOperatorMap[op]);
5904
6398
  }
5905
- function filterUndefined(array6) {
5906
- return array6.filter((e) => e !== void 0);
6399
+ function filterUndefined(array7) {
6400
+ return array7.filter((e) => e !== void 0);
5907
6401
  }
5908
6402
  function filterTrue(conditions) {
5909
6403
  return conditions.filter((c) => !isAlwaysTrue(c));
@@ -6289,8 +6783,16 @@ function applyCorrelatedSubQuery(sq, delegate, queryID, end, name, fromCondition
6289
6783
  `${name}.${sq.subquery.alias}`,
6290
6784
  sq.correlation.childField
6291
6785
  );
6292
- const joinName = `${name}:join(${sq.subquery.alias})`;
6293
- const join = new Join({
6786
+ const joinName = `${name}:${sq.flip ? "flipped-join" : "join"}(${sq.subquery.alias})`;
6787
+ const join = sq.flip ? new FlippedJoin({
6788
+ parent: end,
6789
+ child,
6790
+ parentKey: sq.correlation.parentField,
6791
+ childKey: sq.correlation.childField,
6792
+ relationshipName: sq.subquery.alias,
6793
+ hidden: sq.hidden ?? false,
6794
+ system: sq.system ?? "client"
6795
+ }) : new Join({
6294
6796
  parent: end,
6295
6797
  child,
6296
6798
  storage: delegate.createStorage(joinName),
@@ -6635,7 +7137,12 @@ var AbstractQuery = class {
6635
7137
  this.customQueryID,
6636
7138
  this.#currentJunction
6637
7139
  );
6638
- whereExists = (relationship, cb) => this.where(({ exists }) => exists(relationship, cb));
7140
+ whereExists = (relationship, cbOrOptions, options) => {
7141
+ const cb = typeof cbOrOptions === "function" ? cbOrOptions : void 0;
7142
+ const opts = typeof cbOrOptions === "function" ? options : cbOrOptions;
7143
+ const flipped = opts?.flip ?? false;
7144
+ return this.where(({ exists }) => exists(relationship, cb, { flip: flipped }));
7145
+ };
6639
7146
  related = (relationship, cb) => {
6640
7147
  if (relationship.startsWith(SUBQ_PREFIX)) {
6641
7148
  throw new Error(
@@ -6874,7 +7381,9 @@ var AbstractQuery = class {
6874
7381
  this.#currentJunction
6875
7382
  );
6876
7383
  };
6877
- _exists = (relationship, cb = (q) => q) => {
7384
+ _exists = (relationship, cb, options) => {
7385
+ cb = cb ?? ((q) => q);
7386
+ const flip = options?.flip ?? false;
6878
7387
  const related = this.#schema.relationships[this.#tableName][relationship];
6879
7388
  assert(related, "Invalid relationship");
6880
7389
  if (isOneHop(related)) {
@@ -6906,7 +7415,8 @@ var AbstractQuery = class {
6906
7415
  subquery: addPrimaryKeysToAst(
6907
7416
  this.#schema.tables[destSchema],
6908
7417
  sq._ast
6909
- )
7418
+ ),
7419
+ flip
6910
7420
  },
6911
7421
  op: "EXISTS"
6912
7422
  };
@@ -6941,6 +7451,7 @@ var AbstractQuery = class {
6941
7451
  parentField: firstRelation.sourceField,
6942
7452
  childField: firstRelation.destField
6943
7453
  },
7454
+ flip,
6944
7455
  subquery: {
6945
7456
  table: junctionSchema,
6946
7457
  alias: `${SUBQ_PREFIX}${relationship}`,
@@ -6959,7 +7470,8 @@ var AbstractQuery = class {
6959
7470
  subquery: addPrimaryKeysToAst(
6960
7471
  this.#schema.tables[destSchema],
6961
7472
  queryToDest._ast
6962
- )
7473
+ ),
7474
+ flip
6963
7475
  },
6964
7476
  op: "EXISTS"
6965
7477
  }
@@ -7003,9 +7515,6 @@ var AbstractQuery = class {
7003
7515
  }
7004
7516
  return this.#completedAST;
7005
7517
  }
7006
- then(onFulfilled, onRejected) {
7007
- return this.run().then(onFulfilled, onRejected);
7008
- }
7009
7518
  };
7010
7519
  var completedAstSymbol = Symbol();
7011
7520
  var QueryImpl = class _QueryImpl extends AbstractQuery {
@@ -7305,6 +7814,8 @@ export {
7305
7814
  makeComparator,
7306
7815
  valuesEqual,
7307
7816
  throwOutput,
7817
+ emptyFunction,
7818
+ emptyObject,
7308
7819
  filterPush,
7309
7820
  ExpressionBuilder,
7310
7821
  createPredicate,
@@ -7329,6 +7840,7 @@ export {
7329
7840
  inspectMetricsDownSchema,
7330
7841
  inspectVersionDownSchema,
7331
7842
  inspectAuthenticatedDownSchema,
7843
+ inspectAnalyzeQueryDownSchema,
7332
7844
  inspectDownMessageSchema,
7333
7845
  primaryKeySchema,
7334
7846
  primaryKeyValueRecordSchema,
@@ -7342,6 +7854,8 @@ export {
7342
7854
  toMutationResponseKey,
7343
7855
  toPrimaryKeyString,
7344
7856
  sourceNameFromKey,
7345
- TDigest
7857
+ Client,
7858
+ TDigest,
7859
+ Inspector
7346
7860
  };
7347
- //# sourceMappingURL=chunk-K5ZSWG54.js.map
7861
+ //# sourceMappingURL=chunk-QZPMFA73.js.map