@rocicorp/zero 1.0.0 → 1.0.1-canary.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 (75) hide show
  1. package/out/analyze-query/src/bin-analyze.js +19 -7
  2. package/out/analyze-query/src/bin-analyze.js.map +1 -1
  3. package/out/zero/package.js +1 -1
  4. package/out/zero/package.js.map +1 -1
  5. package/out/zero-cache/src/config/zero-config.d.ts +6 -0
  6. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  7. package/out/zero-cache/src/config/zero-config.js +12 -0
  8. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  9. package/out/zero-cache/src/server/anonymous-otel-start.d.ts.map +1 -1
  10. package/out/zero-cache/src/server/anonymous-otel-start.js +1 -14
  11. package/out/zero-cache/src/server/anonymous-otel-start.js.map +1 -1
  12. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  13. package/out/zero-cache/src/server/change-streamer.js +2 -2
  14. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  15. package/out/zero-cache/src/services/analyze.js +1 -1
  16. package/out/zero-cache/src/services/change-source/change-source.d.ts +7 -0
  17. package/out/zero-cache/src/services/change-source/change-source.d.ts.map +1 -1
  18. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.d.ts.map +1 -1
  19. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js +1 -1
  20. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js.map +1 -1
  21. package/out/zero-cache/src/services/change-source/custom/change-source.d.ts.map +1 -1
  22. package/out/zero-cache/src/services/change-source/custom/change-source.js +3 -0
  23. package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
  24. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts +9 -1
  25. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  26. package/out/zero-cache/src/services/change-source/pg/change-source.js +150 -45
  27. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  28. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts +8 -0
  29. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts.map +1 -1
  30. package/out/zero-cache/src/services/change-source/protocol/current/status.d.ts +26 -1
  31. package/out/zero-cache/src/services/change-source/protocol/current/status.d.ts.map +1 -1
  32. package/out/zero-cache/src/services/change-source/protocol/current/status.js +7 -2
  33. package/out/zero-cache/src/services/change-source/protocol/current/status.js.map +1 -1
  34. package/out/zero-cache/src/services/change-source/protocol/current/upstream.d.ts +8 -0
  35. package/out/zero-cache/src/services/change-source/protocol/current/upstream.d.ts.map +1 -1
  36. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  37. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +10 -2
  38. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  39. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +25 -0
  40. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  41. package/out/zero-cache/src/services/change-streamer/change-streamer.js +8 -1
  42. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  43. package/out/zero-cache/src/services/change-streamer/forwarder.d.ts +2 -0
  44. package/out/zero-cache/src/services/change-streamer/forwarder.d.ts.map +1 -1
  45. package/out/zero-cache/src/services/change-streamer/forwarder.js +3 -0
  46. package/out/zero-cache/src/services/change-streamer/forwarder.js.map +1 -1
  47. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts +3 -2
  48. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts.map +1 -1
  49. package/out/zero-cache/src/services/change-streamer/subscriber.js +17 -8
  50. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  51. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts +2 -2
  52. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts.map +1 -1
  53. package/out/zero-cache/src/services/replicator/incremental-sync.js +19 -4
  54. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  55. package/out/zero-cache/src/services/replicator/replicator.d.ts.map +1 -1
  56. package/out/zero-cache/src/services/replicator/replicator.js +2 -2
  57. package/out/zero-cache/src/services/replicator/replicator.js.map +1 -1
  58. package/out/zero-cache/src/services/replicator/reporter/recorder.d.ts +12 -0
  59. package/out/zero-cache/src/services/replicator/reporter/recorder.d.ts.map +1 -0
  60. package/out/zero-cache/src/services/replicator/reporter/recorder.js +58 -0
  61. package/out/zero-cache/src/services/replicator/reporter/recorder.js.map +1 -0
  62. package/out/zero-cache/src/services/replicator/reporter/report-schema.d.ts +35 -0
  63. package/out/zero-cache/src/services/replicator/reporter/report-schema.d.ts.map +1 -0
  64. package/out/zero-cache/src/services/replicator/reporter/report-schema.js +20 -0
  65. package/out/zero-cache/src/services/replicator/reporter/report-schema.js.map +1 -0
  66. package/out/zero-cache/src/services/run-ast.js +1 -1
  67. package/out/zero-cache/src/types/pg.d.ts.map +1 -1
  68. package/out/zero-cache/src/types/pg.js +2 -0
  69. package/out/zero-cache/src/types/pg.js.map +1 -1
  70. package/out/zero-client/src/client/version.js +1 -1
  71. package/package.json +1 -1
  72. package/out/analyze-query/src/run-ast.d.ts +0 -22
  73. package/out/analyze-query/src/run-ast.d.ts.map +0 -1
  74. package/out/analyze-query/src/run-ast.js +0 -75
  75. package/out/analyze-query/src/run-ast.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"replicator.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/replicator/replicator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,gCAAgC,CAAC;AACvE,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,8BAA8B,CAAC;AAC3D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,wBAAwB,CAAC;AACnD,OAAO,KAAK,EAAC,cAAc,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O,KAAK,EAAC,MAAM,EAAC,MAAM,wBAAwB,CAAC;AACnD,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,uCAAuC,CAAC;AAC1E,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AAE3C,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEhE,oDAAoD;AACpD,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,KAAK,EAAE,eAAe,CAAC;IAIhC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,MAAM,WAAW,oBAAoB;IACnC;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,SAAS,IAAI,MAAM,CAAC,YAAY,CAAC,CAAC;CACnC;AAED,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IACtD;;;;OAIG;IACH,MAAM,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAAC;CACvC;AAED,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,SAAS,CAAC;AAElD,qBAAa,iBAAkB,YAAW,UAAU,EAAE,OAAO;;IAC3D,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;gBAOlB,EAAE,EAAE,UAAU,EACd,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,cAAc,EACpB,cAAc,EAAE,cAAc,EAC9B,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,iBAAiB,EACzB,wBAAwB,EAAE,OAAO;IAoBnC,MAAM;;;IAIN,GAAG;IAKH,SAAS,IAAI,MAAM,CAAC,YAAY,CAAC;IAI3B,IAAI;CAQX"}
@@ -10,13 +10,13 @@ var ReplicatorService = class {
10
10
  this.id = id;
11
11
  this.#lc = lc.withContext("component", "replicator").withContext("serviceID", this.id);
12
12
  this.#worker = worker;
13
- this.#incrementalSyncer = new IncrementalSyncer(taskID, `${taskID}/${id}`, changeStreamer, statusDb, worker, mode, publishReplicationStatus);
13
+ this.#incrementalSyncer = new IncrementalSyncer(this.#lc, taskID, `${taskID}/${id}`, changeStreamer, statusDb, worker, mode, publishReplicationStatus);
14
14
  }
15
15
  status() {
16
16
  return Promise.resolve({ status: "ok" });
17
17
  }
18
18
  run() {
19
- this.#runPromise = this.#incrementalSyncer.run(this.#lc);
19
+ this.#runPromise = this.#incrementalSyncer.run();
20
20
  return this.#runPromise;
21
21
  }
22
22
  subscribe() {
@@ -1 +1 @@
1
- {"version":3,"file":"replicator.js","names":["#lc","#incrementalSyncer","#worker","#runPromise"],"sources":["../../../../../../zero-cache/src/services/replicator/replicator.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {ReadonlyJSONObject} from '../../../../shared/src/json.ts';\nimport type {Database} from '../../../../zqlite/src/db.ts';\nimport type {Source} from '../../types/streams.ts';\nimport type {ChangeStreamer} from '../change-streamer/change-streamer.ts';\nimport type {Service} from '../service.ts';\nimport {IncrementalSyncer} from './incremental-sync.ts';\nimport type {WriteWorkerClient} from './write-worker-client.ts';\n\n/** See {@link ReplicaStateNotifier.subscribe()}. */\nexport type ReplicaState = {\n readonly state: 'version-ready';\n\n // Used in tests to verify behavior when additional information\n // is ferried in the future. Not set in production.\n readonly testSeqNum?: number;\n};\n\nexport interface ReplicaStateNotifier {\n /**\n * Creates a cancelable subscription of changes in the replica state.\n *\n * A `version-ready` message indicates that the replica is ready to be\n * read, and henceforth that a _new_ version is ready, i.e. whenever a\n * change is committed to the replica. The `version-ready` message itself\n * otherwise contains no other information; the subscriber queries the\n * replica for the current data.\n *\n * A `maintenance` state indicates that the replica should not be read from.\n * If a subscriber is holding any transaction locks, it should release them\n * until the next `version-ready` signal.\n *\n * Upon subscription, the current state of the replica is sent immediately\n * if known. If multiple notifications occur before the subscriber\n * can consume them, all but the last notification are discarded by the\n * Subscription object (i.e. not buffered). Thus, a subscriber only\n * ever consumes the current (i.e. known) state of the replica. This avoids\n * a buildup of \"work\" if a subscriber is too busy to consume all\n * notifications.\n */\n subscribe(): Source<ReplicaState>;\n}\n\nexport interface Replicator extends ReplicaStateNotifier {\n /**\n * Returns an opaque message for human-readable consumption. This is\n * purely for ensuring that the Replicator has started at least once to\n * bootstrap a new replica.\n */\n status(): Promise<ReadonlyJSONObject>;\n}\n\nexport type ReplicatorMode = 'backup' | 'serving';\n\nexport class ReplicatorService implements Replicator, Service {\n readonly id: string;\n readonly #lc: LogContext;\n readonly #incrementalSyncer: IncrementalSyncer;\n readonly #worker: WriteWorkerClient;\n #runPromise: Promise<void> | undefined;\n\n constructor(\n lc: LogContext,\n taskID: string,\n id: string,\n mode: ReplicatorMode,\n changeStreamer: ChangeStreamer,\n statusDb: Database,\n worker: WriteWorkerClient,\n publishReplicationStatus: boolean,\n ) {\n this.id = id;\n this.#lc = lc\n .withContext('component', 'replicator')\n .withContext('serviceID', this.id);\n this.#worker = worker;\n\n this.#incrementalSyncer = new IncrementalSyncer(\n taskID,\n `${taskID}/${id}`,\n changeStreamer,\n statusDb,\n worker,\n mode,\n publishReplicationStatus,\n );\n }\n\n status() {\n return Promise.resolve({status: 'ok'});\n }\n\n run() {\n this.#runPromise = this.#incrementalSyncer.run(this.#lc);\n return this.#runPromise;\n }\n\n subscribe(): Source<ReplicaState> {\n return this.#incrementalSyncer.subscribe();\n }\n\n async stop() {\n this.#incrementalSyncer.stop(this.#lc);\n // Wait for the syncer's run loop to finish so that any in-flight\n // worker.processMessage() call completes and clears #pending\n // before we send the 'stop' message to the worker.\n await this.#runPromise;\n await this.#worker.stop();\n }\n}\n"],"mappings":";;AAsDA,IAAa,oBAAb,MAA8D;CAC5D;CACA;CACA;CACA;CACA;CAEA,YACE,IACA,QACA,IACA,MACA,gBACA,UACA,QACA,0BACA;AACA,OAAK,KAAK;AACV,QAAA,KAAW,GACR,YAAY,aAAa,aAAa,CACtC,YAAY,aAAa,KAAK,GAAG;AACpC,QAAA,SAAe;AAEf,QAAA,oBAA0B,IAAI,kBAC5B,QACA,GAAG,OAAO,GAAG,MACb,gBACA,UACA,QACA,MACA,yBACD;;CAGH,SAAS;AACP,SAAO,QAAQ,QAAQ,EAAC,QAAQ,MAAK,CAAC;;CAGxC,MAAM;AACJ,QAAA,aAAmB,MAAA,kBAAwB,IAAI,MAAA,GAAS;AACxD,SAAO,MAAA;;CAGT,YAAkC;AAChC,SAAO,MAAA,kBAAwB,WAAW;;CAG5C,MAAM,OAAO;AACX,QAAA,kBAAwB,KAAK,MAAA,GAAS;AAItC,QAAM,MAAA;AACN,QAAM,MAAA,OAAa,MAAM"}
1
+ {"version":3,"file":"replicator.js","names":["#lc","#incrementalSyncer","#worker","#runPromise"],"sources":["../../../../../../zero-cache/src/services/replicator/replicator.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {ReadonlyJSONObject} from '../../../../shared/src/json.ts';\nimport type {Database} from '../../../../zqlite/src/db.ts';\nimport type {Source} from '../../types/streams.ts';\nimport type {ChangeStreamer} from '../change-streamer/change-streamer.ts';\nimport type {Service} from '../service.ts';\nimport {IncrementalSyncer} from './incremental-sync.ts';\nimport type {WriteWorkerClient} from './write-worker-client.ts';\n\n/** See {@link ReplicaStateNotifier.subscribe()}. */\nexport type ReplicaState = {\n readonly state: 'version-ready';\n\n // Used in tests to verify behavior when additional information\n // is ferried in the future. Not set in production.\n readonly testSeqNum?: number;\n};\n\nexport interface ReplicaStateNotifier {\n /**\n * Creates a cancelable subscription of changes in the replica state.\n *\n * A `version-ready` message indicates that the replica is ready to be\n * read, and henceforth that a _new_ version is ready, i.e. whenever a\n * change is committed to the replica. The `version-ready` message itself\n * otherwise contains no other information; the subscriber queries the\n * replica for the current data.\n *\n * A `maintenance` state indicates that the replica should not be read from.\n * If a subscriber is holding any transaction locks, it should release them\n * until the next `version-ready` signal.\n *\n * Upon subscription, the current state of the replica is sent immediately\n * if known. If multiple notifications occur before the subscriber\n * can consume them, all but the last notification are discarded by the\n * Subscription object (i.e. not buffered). Thus, a subscriber only\n * ever consumes the current (i.e. known) state of the replica. This avoids\n * a buildup of \"work\" if a subscriber is too busy to consume all\n * notifications.\n */\n subscribe(): Source<ReplicaState>;\n}\n\nexport interface Replicator extends ReplicaStateNotifier {\n /**\n * Returns an opaque message for human-readable consumption. This is\n * purely for ensuring that the Replicator has started at least once to\n * bootstrap a new replica.\n */\n status(): Promise<ReadonlyJSONObject>;\n}\n\nexport type ReplicatorMode = 'backup' | 'serving';\n\nexport class ReplicatorService implements Replicator, Service {\n readonly id: string;\n readonly #lc: LogContext;\n readonly #incrementalSyncer: IncrementalSyncer;\n readonly #worker: WriteWorkerClient;\n #runPromise: Promise<void> | undefined;\n\n constructor(\n lc: LogContext,\n taskID: string,\n id: string,\n mode: ReplicatorMode,\n changeStreamer: ChangeStreamer,\n statusDb: Database,\n worker: WriteWorkerClient,\n publishReplicationStatus: boolean,\n ) {\n this.id = id;\n this.#lc = lc\n .withContext('component', 'replicator')\n .withContext('serviceID', this.id);\n this.#worker = worker;\n\n this.#incrementalSyncer = new IncrementalSyncer(\n this.#lc,\n taskID,\n `${taskID}/${id}`,\n changeStreamer,\n statusDb,\n worker,\n mode,\n publishReplicationStatus,\n );\n }\n\n status() {\n return Promise.resolve({status: 'ok'});\n }\n\n run() {\n this.#runPromise = this.#incrementalSyncer.run();\n return this.#runPromise;\n }\n\n subscribe(): Source<ReplicaState> {\n return this.#incrementalSyncer.subscribe();\n }\n\n async stop() {\n this.#incrementalSyncer.stop(this.#lc);\n // Wait for the syncer's run loop to finish so that any in-flight\n // worker.processMessage() call completes and clears #pending\n // before we send the 'stop' message to the worker.\n await this.#runPromise;\n await this.#worker.stop();\n }\n}\n"],"mappings":";;AAsDA,IAAa,oBAAb,MAA8D;CAC5D;CACA;CACA;CACA;CACA;CAEA,YACE,IACA,QACA,IACA,MACA,gBACA,UACA,QACA,0BACA;AACA,OAAK,KAAK;AACV,QAAA,KAAW,GACR,YAAY,aAAa,aAAa,CACtC,YAAY,aAAa,KAAK,GAAG;AACpC,QAAA,SAAe;AAEf,QAAA,oBAA0B,IAAI,kBAC5B,MAAA,IACA,QACA,GAAG,OAAO,GAAG,MACb,gBACA,UACA,QACA,MACA,yBACD;;CAGH,SAAS;AACP,SAAO,QAAQ,QAAQ,EAAC,QAAQ,MAAK,CAAC;;CAGxC,MAAM;AACJ,QAAA,aAAmB,MAAA,kBAAwB,KAAK;AAChD,SAAO,MAAA;;CAGT,YAAkC;AAChC,SAAO,MAAA,kBAAwB,WAAW;;CAG5C,MAAM,OAAO;AACX,QAAA,kBAAwB,KAAK,MAAA,GAAS;AAItC,QAAM,MAAA;AACN,QAAM,MAAA,OAAa,MAAM"}
@@ -0,0 +1,12 @@
1
+ import type { ObservableResult } from '@opentelemetry/api';
2
+ import type { LogContext } from '@rocicorp/logger';
3
+ import type { ReplicationReport } from './report-schema.ts';
4
+ export declare class ReplicationReportRecorder {
5
+ #private;
6
+ constructor(lc: LogContext, now?: () => number);
7
+ record(report: ReplicationReport): void;
8
+ readonly reportUpstreamLag: (o: ObservableResult) => void;
9
+ readonly reportReplicaLag: (o: ObservableResult) => void;
10
+ readonly reportTotalLag: (o: ObservableResult) => void;
11
+ }
12
+ //# sourceMappingURL=recorder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recorder.d.ts","sourceRoot":"","sources":["../../../../../../../zero-cache/src/services/replicator/reporter/recorder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,oBAAoB,CAAC;AAM1D,qBAAa,yBAAyB;;gBAKxB,EAAE,EAAE,UAAU,EAAE,GAAG,eAAW;IAK1C,MAAM,CAAC,MAAM,EAAE,iBAAiB;IA2ChC,QAAQ,CAAC,iBAAiB,GAAI,GAAG,gBAAgB,UAK/C;IAEF,QAAQ,CAAC,gBAAgB,GAAI,GAAG,gBAAgB,UAK9C;IAEF,QAAQ,CAAC,cAAc,GAAI,GAAG,gBAAgB,UAU5C;CACH"}
@@ -0,0 +1,58 @@
1
+ import { getOrCreateGauge } from "../../../observability/metrics.js";
2
+ //#region ../zero-cache/src/services/replicator/reporter/recorder.ts
3
+ var LOG_ALL_REPLICATION_REPORTS_AT_DEBUG = process.env.ZERO_LOG_ALL_REPLICATION_REPORTS_AT_DEBUG === "1";
4
+ var ReplicationReportRecorder = class {
5
+ #lc;
6
+ #now;
7
+ #last = null;
8
+ constructor(lc, now = Date.now) {
9
+ this.#lc = lc;
10
+ this.#now = now;
11
+ }
12
+ record(report) {
13
+ const first = this.#last === null;
14
+ this.#last = report;
15
+ const { lastTimings } = report;
16
+ if (lastTimings) {
17
+ const total = lastTimings.replicateTimeMs - lastTimings.sendTimeMs;
18
+ if (total > 1e4) this.#lc.warn?.(`high replication lag: ${total} ms`, report);
19
+ else if (total > 1e3) this.#lc.info?.(`replication lag: ${total} ms`, report);
20
+ if (LOG_ALL_REPLICATION_REPORTS_AT_DEBUG) this.#lc.debug?.(`replication lag ${total} ms`, report);
21
+ }
22
+ if (first) {
23
+ getOrCreateGauge("replication", "upstream_lag", {
24
+ description: "Latency from sending an upstream replication report to receiving it in the replication stream",
25
+ unit: "millisecond"
26
+ }).addCallback(this.reportUpstreamLag);
27
+ getOrCreateGauge("replication", "replica_lag", {
28
+ description: "Latency from receiving an upstream replication report to its reaching the replica",
29
+ unit: "millisecond"
30
+ }).addCallback(this.reportReplicaLag);
31
+ getOrCreateGauge("replication", "total_lag", {
32
+ description: "Latency from sending an upstream replication report to its reaching the replica. This will be a (growing) estimate if the next expected report has yet to be received and the elapsed time has exceeded the previous report's total lag.",
33
+ unit: "millisecond"
34
+ }).addCallback(this.reportTotalLag);
35
+ }
36
+ }
37
+ reportUpstreamLag = (o) => {
38
+ const last = this.#last?.lastTimings;
39
+ if (last) o.observe(last.receiveTimeMs - last.sendTimeMs);
40
+ };
41
+ reportReplicaLag = (o) => {
42
+ const last = this.#last?.lastTimings;
43
+ if (last) o.observe(last.replicateTimeMs - last.receiveTimeMs);
44
+ };
45
+ reportTotalLag = (o) => {
46
+ const last = this.#last;
47
+ if (last) {
48
+ const nextLagEstimate = this.#now() - last.nextSendTimeMs;
49
+ const timings = last.lastTimings;
50
+ const lastLag = timings ? timings.replicateTimeMs - timings.sendTimeMs : 0;
51
+ o.observe(Math.max(lastLag, nextLagEstimate));
52
+ }
53
+ };
54
+ };
55
+ //#endregion
56
+ export { ReplicationReportRecorder };
57
+
58
+ //# sourceMappingURL=recorder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recorder.js","names":["#lc","#now","#last"],"sources":["../../../../../../../zero-cache/src/services/replicator/reporter/recorder.ts"],"sourcesContent":["import type {ObservableResult} from '@opentelemetry/api';\nimport type {LogContext} from '@rocicorp/logger';\nimport {getOrCreateGauge} from '../../../observability/metrics.ts';\nimport type {ReplicationReport} from './report-schema.ts';\n\n// Hook for sanity checking lag reports in development.\nconst LOG_ALL_REPLICATION_REPORTS_AT_DEBUG =\n process.env.ZERO_LOG_ALL_REPLICATION_REPORTS_AT_DEBUG === '1';\n\nexport class ReplicationReportRecorder {\n readonly #lc: LogContext;\n readonly #now: () => number;\n #last: ReplicationReport | null = null;\n\n constructor(lc: LogContext, now = Date.now) {\n this.#lc = lc;\n this.#now = now;\n }\n\n record(report: ReplicationReport) {\n const first = this.#last === null;\n this.#last = report;\n\n const {lastTimings} = report;\n if (lastTimings) {\n const total = lastTimings.replicateTimeMs - lastTimings.sendTimeMs;\n if (total > 10_000) {\n this.#lc.warn?.(`high replication lag: ${total} ms`, report);\n } else if (total > 1_000) {\n this.#lc.info?.(`replication lag: ${total} ms`, report);\n }\n if (LOG_ALL_REPLICATION_REPORTS_AT_DEBUG) {\n this.#lc.debug?.(`replication lag ${total} ms`, report);\n }\n }\n\n if (first) {\n getOrCreateGauge('replication', 'upstream_lag', {\n description:\n 'Latency from sending an upstream replication report ' +\n 'to receiving it in the replication stream',\n unit: 'millisecond',\n }).addCallback(this.reportUpstreamLag);\n\n getOrCreateGauge('replication', 'replica_lag', {\n description:\n 'Latency from receiving an upstream replication report ' +\n 'to its reaching the replica',\n unit: 'millisecond',\n }).addCallback(this.reportReplicaLag);\n\n getOrCreateGauge('replication', 'total_lag', {\n description:\n 'Latency from sending an upstream replication report to its ' +\n 'reaching the replica. This will be a (growing) estimate if the ' +\n 'next expected report has yet to be received and the elapsed ' +\n `time has exceeded the previous report's total lag.`,\n unit: 'millisecond',\n }).addCallback(this.reportTotalLag);\n }\n }\n\n readonly reportUpstreamLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.receiveTimeMs - last.sendTimeMs);\n }\n };\n\n readonly reportReplicaLag = (o: ObservableResult) => {\n const last = this.#last?.lastTimings;\n if (last) {\n o.observe(last.replicateTimeMs - last.receiveTimeMs);\n }\n };\n\n readonly reportTotalLag = (o: ObservableResult) => {\n const last = this.#last;\n if (last) {\n const nextLagEstimate = this.#now() - last.nextSendTimeMs;\n const timings = last.lastTimings;\n const lastLag = timings\n ? timings.replicateTimeMs - timings.sendTimeMs\n : 0;\n o.observe(Math.max(lastLag, nextLagEstimate));\n }\n };\n}\n"],"mappings":";;AAMA,IAAM,uCACJ,QAAQ,IAAI,8CAA8C;AAE5D,IAAa,4BAAb,MAAuC;CACrC;CACA;CACA,QAAkC;CAElC,YAAY,IAAgB,MAAM,KAAK,KAAK;AAC1C,QAAA,KAAW;AACX,QAAA,MAAY;;CAGd,OAAO,QAA2B;EAChC,MAAM,QAAQ,MAAA,SAAe;AAC7B,QAAA,OAAa;EAEb,MAAM,EAAC,gBAAe;AACtB,MAAI,aAAa;GACf,MAAM,QAAQ,YAAY,kBAAkB,YAAY;AACxD,OAAI,QAAQ,IACV,OAAA,GAAS,OAAO,yBAAyB,MAAM,MAAM,OAAO;YACnD,QAAQ,IACjB,OAAA,GAAS,OAAO,oBAAoB,MAAM,MAAM,OAAO;AAEzD,OAAI,qCACF,OAAA,GAAS,QAAQ,mBAAmB,MAAM,MAAM,OAAO;;AAI3D,MAAI,OAAO;AACT,oBAAiB,eAAe,gBAAgB;IAC9C,aACE;IAEF,MAAM;IACP,CAAC,CAAC,YAAY,KAAK,kBAAkB;AAEtC,oBAAiB,eAAe,eAAe;IAC7C,aACE;IAEF,MAAM;IACP,CAAC,CAAC,YAAY,KAAK,iBAAiB;AAErC,oBAAiB,eAAe,aAAa;IAC3C,aACE;IAIF,MAAM;IACP,CAAC,CAAC,YAAY,KAAK,eAAe;;;CAIvC,qBAA8B,MAAwB;EACpD,MAAM,OAAO,MAAA,MAAY;AACzB,MAAI,KACF,GAAE,QAAQ,KAAK,gBAAgB,KAAK,WAAW;;CAInD,oBAA6B,MAAwB;EACnD,MAAM,OAAO,MAAA,MAAY;AACzB,MAAI,KACF,GAAE,QAAQ,KAAK,kBAAkB,KAAK,cAAc;;CAIxD,kBAA2B,MAAwB;EACjD,MAAM,OAAO,MAAA;AACb,MAAI,MAAM;GACR,MAAM,kBAAkB,MAAA,KAAW,GAAG,KAAK;GAC3C,MAAM,UAAU,KAAK;GACrB,MAAM,UAAU,UACZ,QAAQ,kBAAkB,QAAQ,aAClC;AACJ,KAAE,QAAQ,KAAK,IAAI,SAAS,gBAAgB,CAAC"}
@@ -0,0 +1,35 @@
1
+ import * as v from '../../../../../shared/src/valita.ts';
2
+ export declare const changeSourceTimingsSchema: v.ObjectType<{
3
+ sendTimeMs: v.Type<number>;
4
+ commitTimeMs: v.Type<number>;
5
+ receiveTimeMs: v.Type<number>;
6
+ }, undefined>;
7
+ export declare const changeSourceReportSchema: v.ObjectType<{
8
+ lastTimings: v.ObjectType<{
9
+ sendTimeMs: v.Type<number>;
10
+ commitTimeMs: v.Type<number>;
11
+ receiveTimeMs: v.Type<number>;
12
+ }, undefined>;
13
+ nextSendTimeMs: v.Type<number>;
14
+ }, undefined>;
15
+ export declare const replicationTimingsSchema: v.ObjectType<Omit<{
16
+ sendTimeMs: v.Type<number>;
17
+ commitTimeMs: v.Type<number>;
18
+ receiveTimeMs: v.Type<number>;
19
+ }, "replicateTimeMs"> & {
20
+ replicateTimeMs: v.Type<number>;
21
+ }, undefined>;
22
+ export declare const replicationReportSchema: v.ObjectType<{
23
+ lastTimings: v.Optional<{
24
+ sendTimeMs: number;
25
+ commitTimeMs: number;
26
+ receiveTimeMs: number;
27
+ replicateTimeMs: number;
28
+ }>;
29
+ nextSendTimeMs: v.Type<number>;
30
+ }, undefined>;
31
+ export type ChangeSourceTimings = v.Infer<typeof changeSourceTimingsSchema>;
32
+ export type ChangeSourceReport = v.Infer<typeof changeSourceReportSchema>;
33
+ export type ReplicationTimings = v.Infer<typeof replicationTimingsSchema>;
34
+ export type ReplicationReport = v.Infer<typeof replicationReportSchema>;
35
+ //# sourceMappingURL=report-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"report-schema.d.ts","sourceRoot":"","sources":["../../../../../../../zero-cache/src/services/replicator/reporter/report-schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,qCAAqC,CAAC;AAEzD,eAAO,MAAM,yBAAyB;;;;aAIpC,CAAC;AAEH,eAAO,MAAM,wBAAwB;;;;;;;aAGnC,CAAC;AAEH,eAAO,MAAM,wBAAwB;;;;;;aAEnC,CAAC;AAEH,eAAO,MAAM,uBAAuB;;;;;;;;aAGlC,CAAC;AAEH,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,yBAAyB,CAAC,CAAC;AAC5E,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAE1E,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAC1E,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { valita_exports } from "../../../../../shared/src/valita.js";
2
+ //#region ../zero-cache/src/services/replicator/reporter/report-schema.ts
3
+ var changeSourceTimingsSchema = valita_exports.object({
4
+ sendTimeMs: valita_exports.number(),
5
+ commitTimeMs: valita_exports.number(),
6
+ receiveTimeMs: valita_exports.number()
7
+ });
8
+ var changeSourceReportSchema = valita_exports.object({
9
+ lastTimings: changeSourceTimingsSchema,
10
+ nextSendTimeMs: valita_exports.number()
11
+ });
12
+ var replicationTimingsSchema = changeSourceTimingsSchema.extend({ replicateTimeMs: valita_exports.number() });
13
+ valita_exports.object({
14
+ lastTimings: replicationTimingsSchema.optional(),
15
+ nextSendTimeMs: valita_exports.number()
16
+ });
17
+ //#endregion
18
+ export { changeSourceReportSchema, changeSourceTimingsSchema };
19
+
20
+ //# sourceMappingURL=report-schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"report-schema.js","names":[],"sources":["../../../../../../../zero-cache/src/services/replicator/reporter/report-schema.ts"],"sourcesContent":["import * as v from '../../../../../shared/src/valita.ts';\n\nexport const changeSourceTimingsSchema = v.object({\n sendTimeMs: v.number(),\n commitTimeMs: v.number(),\n receiveTimeMs: v.number(),\n});\n\nexport const changeSourceReportSchema = v.object({\n lastTimings: changeSourceTimingsSchema,\n nextSendTimeMs: v.number(),\n});\n\nexport const replicationTimingsSchema = changeSourceTimingsSchema.extend({\n replicateTimeMs: v.number(),\n});\n\nexport const replicationReportSchema = v.object({\n lastTimings: replicationTimingsSchema.optional(),\n nextSendTimeMs: v.number(),\n});\n\nexport type ChangeSourceTimings = v.Infer<typeof changeSourceTimingsSchema>;\nexport type ChangeSourceReport = v.Infer<typeof changeSourceReportSchema>;\n\nexport type ReplicationTimings = v.Infer<typeof replicationTimingsSchema>;\nexport type ReplicationReport = v.Infer<typeof replicationReportSchema>;\n"],"mappings":";;AAEA,IAAa,4BAA4B,eAAE,OAAO;CAChD,YAAY,eAAE,QAAQ;CACtB,cAAc,eAAE,QAAQ;CACxB,eAAe,eAAE,QAAQ;CAC1B,CAAC;AAEF,IAAa,2BAA2B,eAAE,OAAO;CAC/C,aAAa;CACb,gBAAgB,eAAE,QAAQ;CAC3B,CAAC;AAEF,IAAa,2BAA2B,0BAA0B,OAAO,EACvE,iBAAiB,eAAE,QAAQ,EAC5B,CAAC;AAEqC,eAAE,OAAO;CAC9C,aAAa,yBAAyB,UAAU;CAChD,gBAAgB,eAAE,QAAQ;CAC3B,CAAC"}
@@ -8,8 +8,8 @@ import { buildPipeline } from "../../../zql/src/builder/builder.js";
8
8
  import { computeZqlSpecs } from "../db/lite-tables.js";
9
9
  import { astToZQL } from "../../../ast-to-zql/src/ast-to-zql.js";
10
10
  import { formatOutput } from "../../../ast-to-zql/src/format.js";
11
- import { transformAndHashQuery } from "../auth/read-authorizer.js";
12
11
  import { resolveSimpleScalarSubqueries } from "../../../zqlite/src/resolve-scalar-subqueries.js";
12
+ import { transformAndHashQuery } from "../auth/read-authorizer.js";
13
13
  import { hydrate } from "./view-syncer/pipeline-driver.js";
14
14
  //#region ../zero-cache/src/services/run-ast.ts
15
15
  async function runAst(lc, clientSchema, ast, isTransformed, options, yieldProcess) {
@@ -1 +1 @@
1
- {"version":3,"file":"pg.d.ts","sourceRoot":"","sources":["../../../../../zero-cache/src/types/pg.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,QAAQ,EAAE,EAAc,KAAK,YAAY,EAAC,MAAM,UAAU,CAAC;AAClE,OAAO,EAAa,KAAK,SAAS,EAAC,MAAM,oCAAoC,CAAC;AAc9E,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAqC7D;AAED,iBAAS,kBAAkB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAuBhD;AAcD,wBAAgB,0BAA0B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAoBvE;AAED,wBAAgB,0BAA0B,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAkFrE;AAED,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,EAAE,QAAQ,CAAC,OAAO,CAAC;IACzB,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;IAC7B,IAAI,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC;CAC/B,CAAC,EACF,IAAI,CAAC,EAAE,WAAW,GACjB,UAAU,CA4CZ;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,mBAAmB,QAE/D;AAED,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAOhD,CAAC"}
@@ -137,6 +137,8 @@ function pgClient(lc, connectionURI, options, opts) {
137
137
  lc.debug?.(n);
138
138
  return;
139
139
  case "WARNING":
140
+ lc.warn?.(n);
141
+ return;
140
142
  case "EXCEPTION":
141
143
  lc.error?.(n);
142
144
  return;
@@ -1 +1 @@
1
- {"version":3,"file":"pg.js","names":[],"sources":["../../../../../zero-cache/src/types/pg.ts"],"sourcesContent":["import {PreciseDate} from '@google-cloud/precise-date';\nimport {OID} from '@postgresql-typed/oids';\nimport type {LogContext} from '@rocicorp/logger';\nimport postgres, {type Notice, type PostgresType} from 'postgres';\nimport {BigIntJSON, type JSONValue} from '../../../shared/src/bigint-json.ts';\nimport {randInt} from '../../../shared/src/rand.ts';\nimport {\n DATE,\n JSON,\n JSONB,\n NUMERIC,\n TIME,\n TIMESTAMP,\n TIMESTAMPTZ,\n TIMETZ,\n} from './pg-types.ts';\n\n// exported for testing.\nexport function timestampToFpMillis(timestamp: string): number {\n // Convert from PG's time string, e.g. \"1999-01-08 12:05:06+00\" to \"Z\"\n // format expected by PreciseDate.\n timestamp = timestamp.replace(' ', 'T');\n const positiveOffset = timestamp.includes('+');\n const tzSplitIndex = positiveOffset\n ? timestamp.lastIndexOf('+')\n : timestamp.indexOf('-', timestamp.indexOf('T'));\n const timezoneOffset =\n tzSplitIndex === -1 ? undefined : timestamp.substring(tzSplitIndex);\n const tsWithoutTimezone =\n (tzSplitIndex === -1 ? timestamp : timestamp.substring(0, tzSplitIndex)) +\n 'Z';\n\n try {\n // PreciseDate does not return microsecond precision unless the provided\n // timestamp is in UTC time so we need to add the timezone offset back in.\n const fullTime = new PreciseDate(tsWithoutTimezone).getFullTime();\n const millis = Number(fullTime / 1_000_000n);\n const nanos = Number(fullTime % 1_000_000n);\n const ret = millis + nanos * 1e-6; // floating point milliseconds\n\n // add back in the timezone offset\n if (timezoneOffset) {\n const [hours, minutes] = timezoneOffset.split(':');\n const offset =\n Math.abs(Number(hours)) * 60 + (minutes ? Number(minutes) : 0);\n const offsetMillis = offset * 60 * 1_000;\n // If it is a positive offset, we subtract the offset from the UTC\n // because we passed in the \"local time\" as if it was UTC.\n // The opposite is true for negative offsets.\n return positiveOffset ? ret - offsetMillis : ret + offsetMillis;\n }\n return ret;\n } catch (e) {\n throw new Error(`Error parsing ${timestamp}`, {cause: e});\n }\n}\n\nfunction serializeTimestamp(val: unknown): string {\n switch (typeof val) {\n case 'string':\n return val; // Let Postgres parse it\n case 'number': {\n if (Number.isInteger(val)) {\n return new PreciseDate(val).toISOString();\n }\n // Convert floating point to bigint nanoseconds.\n const nanoseconds =\n 1_000_000n * BigInt(Math.trunc(val)) +\n BigInt(Math.trunc((val % 1) * 1e6));\n return new PreciseDate(nanoseconds).toISOString();\n }\n // Note: Don't support bigint inputs until we decide what the semantics are (e.g. micros vs nanos)\n // case 'bigint':\n // return new PreciseDate(val).toISOString();\n default:\n if (val instanceof Date) {\n return val.toISOString();\n }\n }\n throw new Error(`Unsupported type \"${typeof val}\" for timestamp: ${val}`);\n}\n\nconst MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;\n\nfunction serializeTime(x: unknown, type: 'time' | 'timetz'): string {\n switch (typeof x) {\n case 'string':\n return x; // Let Postgres parse it\n case 'number':\n return millisecondsToPostgresTime(x);\n }\n throw new Error(`Unsupported type \"${typeof x}\" for ${type}: ${x}`);\n}\n\nexport function millisecondsToPostgresTime(milliseconds: number): string {\n if (milliseconds < 0) {\n throw new Error('Milliseconds cannot be negative');\n }\n\n if (milliseconds >= MILLISECONDS_PER_DAY) {\n throw new Error(\n `Milliseconds cannot exceed 24 hours (${MILLISECONDS_PER_DAY}ms)`,\n );\n }\n\n milliseconds = Math.floor(milliseconds); // Ensure it's an integer\n\n const totalSeconds = Math.floor(milliseconds / 1000);\n const hours = Math.floor(totalSeconds / 3600);\n const minutes = Math.floor((totalSeconds % 3600) / 60);\n const seconds = totalSeconds % 60;\n const ms = milliseconds % 1000;\n\n return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}.${ms.toString().padStart(3, '0')}+00`;\n}\n\nexport function postgresTimeToMilliseconds(timeString: string): number {\n // Validate basic format\n if (!timeString || typeof timeString !== 'string') {\n throw new Error('Invalid time string: must be a non-empty string');\n }\n\n // Regular expression to match HH:MM:SS, HH:MM:SS.mmm, or HH:MM:SS+00 / HH:MM:SS.mmm+00\n // Supports optional timezone offset\n const timeRegex =\n /^(\\d{1,2}):(\\d{2}):(\\d{2})(?:\\.(\\d{1,6}))?(?:([+-])(\\d{1,2})(?::(\\d{2}))?)?$/;\n const match = timeString.match(timeRegex);\n\n if (!match) {\n throw new Error(\n `Invalid time format: \"${timeString}\". Expected HH:MM:SS[.mmm][+|-HH[:MM]]`,\n );\n }\n\n // Extract components\n const hours = parseInt(match[1], 10);\n const minutes = parseInt(match[2], 10);\n const seconds = parseInt(match[3], 10);\n // Handle optional milliseconds, pad right with zeros if needed\n let milliseconds = 0;\n if (match[4]) {\n // Pad microseconds to 6 digits\n const msString = match[4].padEnd(6, '0');\n // slice milliseconds out of the microseconds\n // e.g. 123456 -> 123, 1234 -> 123,\n milliseconds = parseInt(msString.slice(0, 3), 10);\n }\n\n // Validate ranges\n if (hours < 0 || hours > 24) {\n throw new Error(\n `Invalid hours: ${hours}. Must be between 0 and 24 (24 means end of day)`,\n );\n }\n\n if (minutes < 0 || minutes >= 60) {\n throw new Error(`Invalid minutes: ${minutes}. Must be between 0 and 59`);\n }\n\n if (seconds < 0 || seconds >= 60) {\n throw new Error(`Invalid seconds: ${seconds}. Must be between 0 and 59`);\n }\n\n if (milliseconds < 0 || milliseconds >= 1000) {\n throw new Error(\n `Invalid milliseconds: ${milliseconds}. Must be between 0 and 999`,\n );\n }\n\n // Special case: PostgreSQL allows 24:00:00 to represent end of day\n if (hours === 24 && (minutes !== 0 || seconds !== 0 || milliseconds !== 0)) {\n throw new Error(\n 'Invalid time: when hours is 24, minutes, seconds, and milliseconds must be 0',\n );\n }\n\n // Calculate total milliseconds\n let totalMs =\n hours * 3600000 + minutes * 60000 + seconds * 1000 + milliseconds;\n\n // Timezone Offset\n if (match[5]) {\n const sign = match[5] === '+' ? 1 : -1;\n const tzHours = parseInt(match[6], 10);\n const tzMinutes = match[7] ? parseInt(match[7], 10) : 0;\n const offsetMs = sign * (tzHours * 3600000 + tzMinutes * 60000);\n totalMs -= offsetMs;\n }\n\n // Normalize to 0-24h only if outside valid range\n if (totalMs > MILLISECONDS_PER_DAY || totalMs < 0) {\n return (\n ((totalMs % MILLISECONDS_PER_DAY) + MILLISECONDS_PER_DAY) %\n MILLISECONDS_PER_DAY\n );\n }\n\n return totalMs;\n}\n\nfunction dateToUTCMidnight(date: string): number {\n const d = new Date(date);\n return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());\n}\n\n/**\n * The (javascript) types of objects that can be returned by our configured\n * Postgres clients. For initial-sync, these comes from the postgres.js client:\n *\n * https://github.com/porsager/postgres/blob/master/src/types.js\n *\n * and for the replication stream these come from the the node-postgres client:\n *\n * https://github.com/brianc/node-pg-types/blob/master/lib/textParsers.js\n */\nexport type PostgresValueType = JSONValue | Uint8Array;\n\nexport type TypeOptions = {\n /**\n * Sends strings directly as JSON values (i.e. without JSON stringification).\n * The application is responsible for ensuring that string inputs for JSON\n * columns are already stringified. Other data types (e.g. objects) will\n * still be stringified by the pg client.\n */\n sendStringAsJson?: boolean;\n};\n\n/**\n * Configures types for the Postgres.js client library (`postgres`).\n *\n * @param jsonAsString Keep JSON / JSONB values as strings instead of parsing.\n */\nexport const postgresTypeConfig = ({sendStringAsJson}: TypeOptions = {}) => ({\n // Type the type IDs as `number` so that Typescript doesn't complain about\n // referencing external types during type inference.\n types: {\n bigint: postgres.BigInt,\n json: {\n to: JSON,\n from: [JSON, JSONB],\n serialize: sendStringAsJson\n ? (x: unknown) => (typeof x === 'string' ? x : BigIntJSON.stringify(x))\n : BigIntJSON.stringify,\n parse: BigIntJSON.parse,\n },\n // Timestamps are converted to PreciseDate objects.\n timestamp: {\n to: TIMESTAMP,\n from: [TIMESTAMP, TIMESTAMPTZ],\n serialize: serializeTimestamp,\n parse: timestampToFpMillis,\n },\n // Times are converted as strings\n time: {\n to: TIME,\n from: [TIME, TIMETZ],\n serialize: (x: unknown) => serializeTime(x, 'time'),\n parse: postgresTimeToMilliseconds,\n },\n\n timetz: {\n to: TIMETZ,\n from: [TIME, TIMETZ],\n serialize: (x: unknown) => serializeTime(x, 'timetz'),\n parse: postgresTimeToMilliseconds,\n },\n\n // The DATE type is stored directly as the PG normalized date string.\n date: {\n to: DATE,\n from: [DATE],\n serialize: (x: string | Date) =>\n (x instanceof Date ? x : new Date(x)).toISOString(),\n parse: dateToUTCMidnight,\n },\n // Returns a `js` number which can lose precision for large numbers.\n // JS number is 53 bits so this should generally not occur.\n // An API will be provided for users to override this type.\n numeric: {\n to: NUMERIC,\n from: [NUMERIC],\n serialize: (x: number) => String(x), // pg expects a string\n parse: (x: string | number) => Number(x),\n },\n },\n});\n\nexport type PostgresDB = postgres.Sql<{\n bigint: bigint;\n json: JSONValue;\n}>;\n\nexport type PostgresTransaction = postgres.TransactionSql<{\n bigint: bigint;\n json: JSONValue;\n}>;\n\nexport function pgClient(\n lc: LogContext,\n connectionURI: string,\n options?: postgres.Options<{\n bigint: PostgresType<bigint>;\n json: PostgresType<JSONValue>;\n }>,\n opts?: TypeOptions,\n): PostgresDB {\n const onnotice = (n: Notice) => {\n // https://www.postgresql.org/docs/current/plpgsql-errors-and-messages.html#PLPGSQL-STATEMENTS-RAISE\n switch (n.severity) {\n case 'NOTICE':\n return; // silenced\n case 'DEBUG':\n lc.debug?.(n);\n return;\n case 'WARNING':\n case 'EXCEPTION':\n lc.error?.(n);\n return;\n case 'LOG':\n case 'INFO':\n default:\n lc.info?.(n);\n }\n };\n const url = new URL(connectionURI);\n const sslFlag =\n url.searchParams.get('ssl') ?? url.searchParams.get('sslmode') ?? 'prefer';\n\n let ssl: boolean | 'prefer' | {rejectUnauthorized: boolean};\n if (sslFlag === 'disable' || sslFlag === 'false') {\n ssl = false;\n } else if (sslFlag === 'no-verify') {\n ssl = {rejectUnauthorized: false};\n } else {\n ssl = sslFlag as 'prefer';\n }\n\n // Set connections to expire between 5 and 10 minutes to free up state on PG.\n const maxLifetimeSeconds = randInt(5 * 60, 10 * 60);\n\n return postgres(connectionURI, {\n ...postgresTypeConfig(opts),\n onnotice,\n ['max_lifetime']: maxLifetimeSeconds,\n ssl,\n ...options,\n });\n}\n\n/**\n * Disables any statement_timeout for the current transaction. By default,\n * Postgres does not impose a statement timeout, but some users and providers\n * set one at the database level (even though it is explicitly discouraged by\n * the Postgres documentation).\n *\n * Zero logic in particular often does not fit into the category of general\n * application logic; for potentially long-running operations like migrations\n * and background cleanup, the statement timeout should be disabled to prevent\n * these operations from timing out.\n */\nexport function disableStatementTimeout(sql: PostgresTransaction) {\n void sql`SET LOCAL statement_timeout = 0;`.execute();\n}\n\nexport const typeNameByOID: Record<number, string> = Object.freeze(\n Object.fromEntries(\n Object.entries(OID).map(([name, oid]) => [\n oid,\n name.startsWith('_') ? `${name.substring(1)}[]` : name,\n ]),\n ),\n);\n"],"mappings":";;;;;;;AAkBA,SAAgB,oBAAoB,WAA2B;AAG7D,aAAY,UAAU,QAAQ,KAAK,IAAI;CACvC,MAAM,iBAAiB,UAAU,SAAS,IAAI;CAC9C,MAAM,eAAe,iBACjB,UAAU,YAAY,IAAI,GAC1B,UAAU,QAAQ,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AAO;EACR;CAGD,MAAM;EACJ,IAAI;EACJ,MAAM,CAAC,KAAK;EACZ,YAAY,OACT,aAAa,OAAO,IAAI,IAAI,KAAK,EAAE,EAAE,aAAa;EACrD,OAAO;EACR;CAID,SAAS;EACP,IAAI;EACJ,MAAM,CAAC,QAAQ;EACf,YAAY,MAAc,OAAO,EAAE;EACnC,QAAQ,MAAuB,OAAO,EAAE;EACzC;CACF,EACF;AAYD,SAAgB,SACd,IACA,eACA,SAIA,MACY;CACZ,MAAM,YAAY,MAAc;AAE9B,UAAQ,EAAE,UAAV;GACE,KAAK,SACH;GACF,KAAK;AACH,OAAG,QAAQ,EAAE;AACb;GACF,KAAK;GACL,KAAK;AACH,OAAG,QAAQ,EAAE;AACb;GAGF,QACE,IAAG,OAAO,EAAE;;;CAGlB,MAAM,MAAM,IAAI,IAAI,cAAc;CAClC,MAAM,UACJ,IAAI,aAAa,IAAI,MAAM,IAAI,IAAI,aAAa,IAAI,UAAU,IAAI;CAEpE,IAAI;AACJ,KAAI,YAAY,aAAa,YAAY,QACvC,OAAM;UACG,YAAY,YACrB,OAAM,EAAC,oBAAoB,OAAM;KAEjC,OAAM;CAIR,MAAM,qBAAqB,QAAQ,KAAQ,IAAQ;AAEnD,QAAO,SAAS,eAAe;EAC7B,GAAG,mBAAmB,KAAK;EAC3B;GACC,iBAAiB;EAClB;EACA,GAAG;EACJ,CAAC;;AAkBiD,OAAO,OAC1D,OAAO,YACL,OAAO,QAAQ,IAAI,CAAC,KAAK,CAAC,MAAM,SAAS,CACvC,KACA,KAAK,WAAW,IAAI,GAAG,GAAG,KAAK,UAAU,EAAE,CAAC,MAAM,KACnD,CAAC,CACH,CACF"}
1
+ {"version":3,"file":"pg.js","names":[],"sources":["../../../../../zero-cache/src/types/pg.ts"],"sourcesContent":["import {PreciseDate} from '@google-cloud/precise-date';\nimport {OID} from '@postgresql-typed/oids';\nimport type {LogContext} from '@rocicorp/logger';\nimport postgres, {type Notice, type PostgresType} from 'postgres';\nimport {BigIntJSON, type JSONValue} from '../../../shared/src/bigint-json.ts';\nimport {randInt} from '../../../shared/src/rand.ts';\nimport {\n DATE,\n JSON,\n JSONB,\n NUMERIC,\n TIME,\n TIMESTAMP,\n TIMESTAMPTZ,\n TIMETZ,\n} from './pg-types.ts';\n\n// exported for testing.\nexport function timestampToFpMillis(timestamp: string): number {\n // Convert from PG's time string, e.g. \"1999-01-08 12:05:06+00\" to \"Z\"\n // format expected by PreciseDate.\n timestamp = timestamp.replace(' ', 'T');\n const positiveOffset = timestamp.includes('+');\n const tzSplitIndex = positiveOffset\n ? timestamp.lastIndexOf('+')\n : timestamp.indexOf('-', timestamp.indexOf('T'));\n const timezoneOffset =\n tzSplitIndex === -1 ? undefined : timestamp.substring(tzSplitIndex);\n const tsWithoutTimezone =\n (tzSplitIndex === -1 ? timestamp : timestamp.substring(0, tzSplitIndex)) +\n 'Z';\n\n try {\n // PreciseDate does not return microsecond precision unless the provided\n // timestamp is in UTC time so we need to add the timezone offset back in.\n const fullTime = new PreciseDate(tsWithoutTimezone).getFullTime();\n const millis = Number(fullTime / 1_000_000n);\n const nanos = Number(fullTime % 1_000_000n);\n const ret = millis + nanos * 1e-6; // floating point milliseconds\n\n // add back in the timezone offset\n if (timezoneOffset) {\n const [hours, minutes] = timezoneOffset.split(':');\n const offset =\n Math.abs(Number(hours)) * 60 + (minutes ? Number(minutes) : 0);\n const offsetMillis = offset * 60 * 1_000;\n // If it is a positive offset, we subtract the offset from the UTC\n // because we passed in the \"local time\" as if it was UTC.\n // The opposite is true for negative offsets.\n return positiveOffset ? ret - offsetMillis : ret + offsetMillis;\n }\n return ret;\n } catch (e) {\n throw new Error(`Error parsing ${timestamp}`, {cause: e});\n }\n}\n\nfunction serializeTimestamp(val: unknown): string {\n switch (typeof val) {\n case 'string':\n return val; // Let Postgres parse it\n case 'number': {\n if (Number.isInteger(val)) {\n return new PreciseDate(val).toISOString();\n }\n // Convert floating point to bigint nanoseconds.\n const nanoseconds =\n 1_000_000n * BigInt(Math.trunc(val)) +\n BigInt(Math.trunc((val % 1) * 1e6));\n return new PreciseDate(nanoseconds).toISOString();\n }\n // Note: Don't support bigint inputs until we decide what the semantics are (e.g. micros vs nanos)\n // case 'bigint':\n // return new PreciseDate(val).toISOString();\n default:\n if (val instanceof Date) {\n return val.toISOString();\n }\n }\n throw new Error(`Unsupported type \"${typeof val}\" for timestamp: ${val}`);\n}\n\nconst MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;\n\nfunction serializeTime(x: unknown, type: 'time' | 'timetz'): string {\n switch (typeof x) {\n case 'string':\n return x; // Let Postgres parse it\n case 'number':\n return millisecondsToPostgresTime(x);\n }\n throw new Error(`Unsupported type \"${typeof x}\" for ${type}: ${x}`);\n}\n\nexport function millisecondsToPostgresTime(milliseconds: number): string {\n if (milliseconds < 0) {\n throw new Error('Milliseconds cannot be negative');\n }\n\n if (milliseconds >= MILLISECONDS_PER_DAY) {\n throw new Error(\n `Milliseconds cannot exceed 24 hours (${MILLISECONDS_PER_DAY}ms)`,\n );\n }\n\n milliseconds = Math.floor(milliseconds); // Ensure it's an integer\n\n const totalSeconds = Math.floor(milliseconds / 1000);\n const hours = Math.floor(totalSeconds / 3600);\n const minutes = Math.floor((totalSeconds % 3600) / 60);\n const seconds = totalSeconds % 60;\n const ms = milliseconds % 1000;\n\n return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}.${ms.toString().padStart(3, '0')}+00`;\n}\n\nexport function postgresTimeToMilliseconds(timeString: string): number {\n // Validate basic format\n if (!timeString || typeof timeString !== 'string') {\n throw new Error('Invalid time string: must be a non-empty string');\n }\n\n // Regular expression to match HH:MM:SS, HH:MM:SS.mmm, or HH:MM:SS+00 / HH:MM:SS.mmm+00\n // Supports optional timezone offset\n const timeRegex =\n /^(\\d{1,2}):(\\d{2}):(\\d{2})(?:\\.(\\d{1,6}))?(?:([+-])(\\d{1,2})(?::(\\d{2}))?)?$/;\n const match = timeString.match(timeRegex);\n\n if (!match) {\n throw new Error(\n `Invalid time format: \"${timeString}\". Expected HH:MM:SS[.mmm][+|-HH[:MM]]`,\n );\n }\n\n // Extract components\n const hours = parseInt(match[1], 10);\n const minutes = parseInt(match[2], 10);\n const seconds = parseInt(match[3], 10);\n // Handle optional milliseconds, pad right with zeros if needed\n let milliseconds = 0;\n if (match[4]) {\n // Pad microseconds to 6 digits\n const msString = match[4].padEnd(6, '0');\n // slice milliseconds out of the microseconds\n // e.g. 123456 -> 123, 1234 -> 123,\n milliseconds = parseInt(msString.slice(0, 3), 10);\n }\n\n // Validate ranges\n if (hours < 0 || hours > 24) {\n throw new Error(\n `Invalid hours: ${hours}. Must be between 0 and 24 (24 means end of day)`,\n );\n }\n\n if (minutes < 0 || minutes >= 60) {\n throw new Error(`Invalid minutes: ${minutes}. Must be between 0 and 59`);\n }\n\n if (seconds < 0 || seconds >= 60) {\n throw new Error(`Invalid seconds: ${seconds}. Must be between 0 and 59`);\n }\n\n if (milliseconds < 0 || milliseconds >= 1000) {\n throw new Error(\n `Invalid milliseconds: ${milliseconds}. Must be between 0 and 999`,\n );\n }\n\n // Special case: PostgreSQL allows 24:00:00 to represent end of day\n if (hours === 24 && (minutes !== 0 || seconds !== 0 || milliseconds !== 0)) {\n throw new Error(\n 'Invalid time: when hours is 24, minutes, seconds, and milliseconds must be 0',\n );\n }\n\n // Calculate total milliseconds\n let totalMs =\n hours * 3600000 + minutes * 60000 + seconds * 1000 + milliseconds;\n\n // Timezone Offset\n if (match[5]) {\n const sign = match[5] === '+' ? 1 : -1;\n const tzHours = parseInt(match[6], 10);\n const tzMinutes = match[7] ? parseInt(match[7], 10) : 0;\n const offsetMs = sign * (tzHours * 3600000 + tzMinutes * 60000);\n totalMs -= offsetMs;\n }\n\n // Normalize to 0-24h only if outside valid range\n if (totalMs > MILLISECONDS_PER_DAY || totalMs < 0) {\n return (\n ((totalMs % MILLISECONDS_PER_DAY) + MILLISECONDS_PER_DAY) %\n MILLISECONDS_PER_DAY\n );\n }\n\n return totalMs;\n}\n\nfunction dateToUTCMidnight(date: string): number {\n const d = new Date(date);\n return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());\n}\n\n/**\n * The (javascript) types of objects that can be returned by our configured\n * Postgres clients. For initial-sync, these comes from the postgres.js client:\n *\n * https://github.com/porsager/postgres/blob/master/src/types.js\n *\n * and for the replication stream these come from the the node-postgres client:\n *\n * https://github.com/brianc/node-pg-types/blob/master/lib/textParsers.js\n */\nexport type PostgresValueType = JSONValue | Uint8Array;\n\nexport type TypeOptions = {\n /**\n * Sends strings directly as JSON values (i.e. without JSON stringification).\n * The application is responsible for ensuring that string inputs for JSON\n * columns are already stringified. Other data types (e.g. objects) will\n * still be stringified by the pg client.\n */\n sendStringAsJson?: boolean;\n};\n\n/**\n * Configures types for the Postgres.js client library (`postgres`).\n *\n * @param jsonAsString Keep JSON / JSONB values as strings instead of parsing.\n */\nexport const postgresTypeConfig = ({sendStringAsJson}: TypeOptions = {}) => ({\n // Type the type IDs as `number` so that Typescript doesn't complain about\n // referencing external types during type inference.\n types: {\n bigint: postgres.BigInt,\n json: {\n to: JSON,\n from: [JSON, JSONB],\n serialize: sendStringAsJson\n ? (x: unknown) => (typeof x === 'string' ? x : BigIntJSON.stringify(x))\n : BigIntJSON.stringify,\n parse: BigIntJSON.parse,\n },\n // Timestamps are converted to PreciseDate objects.\n timestamp: {\n to: TIMESTAMP,\n from: [TIMESTAMP, TIMESTAMPTZ],\n serialize: serializeTimestamp,\n parse: timestampToFpMillis,\n },\n // Times are converted as strings\n time: {\n to: TIME,\n from: [TIME, TIMETZ],\n serialize: (x: unknown) => serializeTime(x, 'time'),\n parse: postgresTimeToMilliseconds,\n },\n\n timetz: {\n to: TIMETZ,\n from: [TIME, TIMETZ],\n serialize: (x: unknown) => serializeTime(x, 'timetz'),\n parse: postgresTimeToMilliseconds,\n },\n\n // The DATE type is stored directly as the PG normalized date string.\n date: {\n to: DATE,\n from: [DATE],\n serialize: (x: string | Date) =>\n (x instanceof Date ? x : new Date(x)).toISOString(),\n parse: dateToUTCMidnight,\n },\n // Returns a `js` number which can lose precision for large numbers.\n // JS number is 53 bits so this should generally not occur.\n // An API will be provided for users to override this type.\n numeric: {\n to: NUMERIC,\n from: [NUMERIC],\n serialize: (x: number) => String(x), // pg expects a string\n parse: (x: string | number) => Number(x),\n },\n },\n});\n\nexport type PostgresDB = postgres.Sql<{\n bigint: bigint;\n json: JSONValue;\n}>;\n\nexport type PostgresTransaction = postgres.TransactionSql<{\n bigint: bigint;\n json: JSONValue;\n}>;\n\nexport function pgClient(\n lc: LogContext,\n connectionURI: string,\n options?: postgres.Options<{\n bigint: PostgresType<bigint>;\n json: PostgresType<JSONValue>;\n }>,\n opts?: TypeOptions,\n): PostgresDB {\n const onnotice = (n: Notice) => {\n // https://www.postgresql.org/docs/current/plpgsql-errors-and-messages.html#PLPGSQL-STATEMENTS-RAISE\n switch (n.severity) {\n case 'NOTICE':\n return; // silenced\n case 'DEBUG':\n lc.debug?.(n);\n return;\n case 'WARNING':\n lc.warn?.(n);\n return;\n case 'EXCEPTION':\n lc.error?.(n);\n return;\n case 'LOG':\n case 'INFO':\n default:\n lc.info?.(n);\n }\n };\n const url = new URL(connectionURI);\n const sslFlag =\n url.searchParams.get('ssl') ?? url.searchParams.get('sslmode') ?? 'prefer';\n\n let ssl: boolean | 'prefer' | {rejectUnauthorized: boolean};\n if (sslFlag === 'disable' || sslFlag === 'false') {\n ssl = false;\n } else if (sslFlag === 'no-verify') {\n ssl = {rejectUnauthorized: false};\n } else {\n ssl = sslFlag as 'prefer';\n }\n\n // Set connections to expire between 5 and 10 minutes to free up state on PG.\n const maxLifetimeSeconds = randInt(5 * 60, 10 * 60);\n\n return postgres(connectionURI, {\n ...postgresTypeConfig(opts),\n onnotice,\n ['max_lifetime']: maxLifetimeSeconds,\n ssl,\n ...options,\n });\n}\n\n/**\n * Disables any statement_timeout for the current transaction. By default,\n * Postgres does not impose a statement timeout, but some users and providers\n * set one at the database level (even though it is explicitly discouraged by\n * the Postgres documentation).\n *\n * Zero logic in particular often does not fit into the category of general\n * application logic; for potentially long-running operations like migrations\n * and background cleanup, the statement timeout should be disabled to prevent\n * these operations from timing out.\n */\nexport function disableStatementTimeout(sql: PostgresTransaction) {\n void sql`SET LOCAL statement_timeout = 0;`.execute();\n}\n\nexport const typeNameByOID: Record<number, string> = Object.freeze(\n Object.fromEntries(\n Object.entries(OID).map(([name, oid]) => [\n oid,\n name.startsWith('_') ? `${name.substring(1)}[]` : name,\n ]),\n ),\n);\n"],"mappings":";;;;;;;AAkBA,SAAgB,oBAAoB,WAA2B;AAG7D,aAAY,UAAU,QAAQ,KAAK,IAAI;CACvC,MAAM,iBAAiB,UAAU,SAAS,IAAI;CAC9C,MAAM,eAAe,iBACjB,UAAU,YAAY,IAAI,GAC1B,UAAU,QAAQ,KAAK,UAAU,QAAQ,IAAI,CAAC;CAClD,MAAM,iBACJ,iBAAiB,KAAK,KAAA,IAAY,UAAU,UAAU,aAAa;CACrE,MAAM,qBACH,iBAAiB,KAAK,YAAY,UAAU,UAAU,GAAG,aAAa,IACvE;AAEF,KAAI;EAGF,MAAM,WAAW,IAAI,YAAY,kBAAkB,CAAC,aAAa;EAGjE,MAAM,MAFS,OAAO,WAAW,SAAW,GAC9B,OAAO,WAAW,SAAW,GACd;AAG7B,MAAI,gBAAgB;GAClB,MAAM,CAAC,OAAO,WAAW,eAAe,MAAM,IAAI;GAGlD,MAAM,gBADJ,KAAK,IAAI,OAAO,MAAM,CAAC,GAAG,MAAM,UAAU,OAAO,QAAQ,GAAG,MAChC,KAAK;AAInC,UAAO,iBAAiB,MAAM,eAAe,MAAM;;AAErD,SAAO;UACA,GAAG;AACV,QAAM,IAAI,MAAM,iBAAiB,aAAa,EAAC,OAAO,GAAE,CAAC;;;AAI7D,SAAS,mBAAmB,KAAsB;AAChD,SAAQ,OAAO,KAAf;EACE,KAAK,SACH,QAAO;EACT,KAAK;AACH,OAAI,OAAO,U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YAAY,YACrB,OAAM,EAAC,oBAAoB,OAAM;KAEjC,OAAM;CAIR,MAAM,qBAAqB,QAAQ,KAAQ,IAAQ;AAEnD,QAAO,SAAS,eAAe;EAC7B,GAAG,mBAAmB,KAAK;EAC3B;GACC,iBAAiB;EAClB;EACA,GAAG;EACJ,CAAC;;AAkBiD,OAAO,OAC1D,OAAO,YACL,OAAO,QAAQ,IAAI,CAAC,KAAK,CAAC,MAAM,SAAS,CACvC,KACA,KAAK,WAAW,IAAI,GAAG,GAAG,KAAK,UAAU,EAAE,CAAC,MAAM,KACnD,CAAC,CACH,CACF"}
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * The current version of Zero.
4
4
  */
5
- var version = "1.0.0";
5
+ var version = "1.0.1-canary.0";
6
6
  //#endregion
7
7
  export { version };
8
8
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocicorp/zero",
3
- "version": "1.0.0",
3
+ "version": "1.0.1-canary.0",
4
4
  "description": "Zero is a web framework for serverless web development.",
5
5
  "author": "Rocicorp, Inc.",
6
6
  "repository": {
@@ -1,22 +0,0 @@
1
- import type { LogContext } from '@rocicorp/logger';
2
- import type { LiteAndZqlSpec } from '../../zero-cache/src/db/specs.ts';
3
- import type { AnalyzeQueryResult } from '../../zero-protocol/src/analyze-query-result.ts';
4
- import type { AST } from '../../zero-protocol/src/ast.ts';
5
- import type { ClientSchema } from '../../zero-protocol/src/client-schema.ts';
6
- import type { PermissionsConfig } from '../../zero-schema/src/compiled-permissions.ts';
7
- import type { NameMapper } from '../../zero-schema/src/name-mapper.ts';
8
- import { type BuilderDelegate } from '../../zql/src/builder/builder.ts';
9
- import type { Database } from '../../zqlite/src/db.ts';
10
- export type RunAstOptions = {
11
- applyPermissions?: boolean | undefined;
12
- authData?: string | undefined;
13
- clientToServerMapper?: NameMapper | undefined;
14
- db: Database;
15
- host: BuilderDelegate;
16
- permissions?: PermissionsConfig | undefined;
17
- syncedRows?: boolean | undefined;
18
- tableSpecs: Map<string, LiteAndZqlSpec>;
19
- vendedRows?: boolean | undefined;
20
- };
21
- export declare function runAst(lc: LogContext, clientSchema: ClientSchema, ast: AST, isTransformed: boolean, options: RunAstOptions): Promise<AnalyzeQueryResult>;
22
- //# sourceMappingURL=run-ast.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"run-ast.d.ts","sourceRoot":"","sources":["../../../../analyze-query/src/run-ast.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAQjD,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,kCAAkC,CAAC;AAErE,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,iDAAiD,CAAC;AACxF,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,gCAAgC,CAAC;AAExD,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,0CAA0C,CAAC;AAG3E,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,+CAA+C,CAAC;AACrF,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,sCAAsC,CAAC;AACrE,OAAO,EAEL,KAAK,eAAe,EACrB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,wBAAwB,CAAC;AAErD,MAAM,MAAM,aAAa,GAAG;IAC1B,gBAAgB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACvC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,oBAAoB,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;IAC9C,EAAE,EAAE,QAAQ,CAAC;IACb,IAAI,EAAE,eAAe,CAAC;IACtB,WAAW,CAAC,EAAE,iBAAiB,GAAG,SAAS,CAAC;IAC5C,UAAU,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACjC,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACxC,UAAU,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAClC,CAAC;AAEF,wBAAsB,MAAM,CAC1B,EAAE,EAAE,UAAU,EACd,YAAY,EAAE,YAAY,EAC1B,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,OAAO,EACtB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,kBAAkB,CAAC,CAyG7B"}
@@ -1,75 +0,0 @@
1
- import { assert } from "../../shared/src/asserts.js";
2
- import { must } from "../../shared/src/must.js";
3
- import { mapAST } from "../../zero-protocol/src/ast.js";
4
- import { hashOfAST } from "../../zero-protocol/src/query-hash.js";
5
- import { buildPipeline } from "../../zql/src/builder/builder.js";
6
- import { computeZqlSpecs } from "../../zero-cache/src/db/lite-tables.js";
7
- import { astToZQL } from "../../ast-to-zql/src/ast-to-zql.js";
8
- import { formatOutput } from "../../ast-to-zql/src/format.js";
9
- import { transformAndHashQuery } from "../../zero-cache/src/auth/read-authorizer.js";
10
- import { hydrate } from "../../zero-cache/src/services/view-syncer/pipeline-driver.js";
11
- //#region ../analyze-query/src/run-ast.ts
12
- async function runAst(lc, clientSchema, ast, isTransformed, options) {
13
- const { clientToServerMapper, permissions, host, db } = options;
14
- const result = {
15
- warnings: [],
16
- syncedRows: void 0,
17
- syncedRowCount: 0,
18
- start: 0,
19
- end: 0,
20
- afterPermissions: void 0,
21
- readRows: void 0,
22
- readRowCountsByQuery: {},
23
- readRowCount: void 0
24
- };
25
- if (!isTransformed) ast = mapAST(ast, must(clientToServerMapper));
26
- if (options.applyPermissions) {
27
- result.warnings.push("Permissions are deprecated and will be removed in an upcoming release. See: https://zero.rocicorp.dev/docs/auth.");
28
- const authData = options.authData ? JSON.parse(options.authData) : {};
29
- if (!options.authData) result.warnings.push("No auth data provided. Permission rules will compare to `NULL` wherever an auth data field is referenced.");
30
- const auth = {
31
- type: "jwt",
32
- raw: "",
33
- decoded: authData
34
- };
35
- ast = transformAndHashQuery(lc, "clientGroupIDForAnalyze", ast, must(permissions, "Permissions are required when applyPermissions is true"), auth, false).transformedAst;
36
- result.afterPermissions = await formatOutput(ast.table + astToZQL(ast));
37
- }
38
- const pipeline = buildPipeline(ast, host, "query-id");
39
- const start = performance.now();
40
- let syncedRowCount = 0;
41
- const rowsByTable = {};
42
- const seenByTable = /* @__PURE__ */ new Set();
43
- const tableSpecs = computeZqlSpecs(lc, db, { includeBackfillingColumns: false });
44
- for (const rowChange of hydrate(pipeline, hashOfAST(ast), clientSchema, tableSpecs)) {
45
- if (rowChange === "yield") continue;
46
- assert(rowChange.type === "add", () => `Expected rowChange type 'add', got '${rowChange.type}'`);
47
- let rows = rowsByTable[rowChange.table];
48
- const s = rowChange.table + "." + JSON.stringify(rowChange.row);
49
- if (seenByTable.has(s)) continue;
50
- syncedRowCount++;
51
- seenByTable.add(s);
52
- if (options.syncedRows) {
53
- if (!rows) {
54
- rows = [];
55
- rowsByTable[rowChange.table] = rows;
56
- }
57
- rows.push(rowChange.row);
58
- }
59
- }
60
- const end = performance.now();
61
- if (options.syncedRows) result.syncedRows = rowsByTable;
62
- result.start = start;
63
- result.end = end;
64
- result.syncedRowCount = syncedRowCount;
65
- result.readRowCountsByQuery = host.debug?.getVendedRowCounts() ?? {};
66
- let readRowCount = 0;
67
- for (const c of Object.values(result.readRowCountsByQuery)) for (const v of Object.values(c)) readRowCount += v;
68
- result.readRowCount = readRowCount;
69
- if (options.vendedRows) result.readRows = host.debug?.getVendedRows();
70
- return result;
71
- }
72
- //#endregion
73
- export { runAst };
74
-
75
- //# sourceMappingURL=run-ast.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"run-ast.js","names":[],"sources":["../../../../analyze-query/src/run-ast.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {astToZQL} from '../../ast-to-zql/src/ast-to-zql.ts';\nimport {formatOutput} from '../../ast-to-zql/src/format.ts';\nimport {assert} from '../../shared/src/asserts.ts';\nimport {must} from '../../shared/src/must.ts';\nimport type {JWTAuth} from '../../zero-cache/src/auth/auth.ts';\nimport {transformAndHashQuery} from '../../zero-cache/src/auth/read-authorizer.ts';\nimport {computeZqlSpecs} from '../../zero-cache/src/db/lite-tables.ts';\nimport type {LiteAndZqlSpec} from '../../zero-cache/src/db/specs.ts';\nimport {hydrate} from '../../zero-cache/src/services/view-syncer/pipeline-driver.ts';\nimport type {AnalyzeQueryResult} from '../../zero-protocol/src/analyze-query-result.ts';\nimport type {AST} from '../../zero-protocol/src/ast.ts';\nimport {mapAST} from '../../zero-protocol/src/ast.ts';\nimport type {ClientSchema} from '../../zero-protocol/src/client-schema.ts';\nimport type {Row} from '../../zero-protocol/src/data.ts';\nimport {hashOfAST} from '../../zero-protocol/src/query-hash.ts';\nimport type {PermissionsConfig} from '../../zero-schema/src/compiled-permissions.ts';\nimport type {NameMapper} from '../../zero-schema/src/name-mapper.ts';\nimport {\n buildPipeline,\n type BuilderDelegate,\n} from '../../zql/src/builder/builder.ts';\nimport type {Database} from '../../zqlite/src/db.ts';\n\nexport type RunAstOptions = {\n applyPermissions?: boolean | undefined;\n authData?: string | undefined;\n clientToServerMapper?: NameMapper | undefined;\n db: Database;\n host: BuilderDelegate;\n permissions?: PermissionsConfig | undefined;\n syncedRows?: boolean | undefined;\n tableSpecs: Map<string, LiteAndZqlSpec>;\n vendedRows?: boolean | undefined;\n};\n\nexport async function runAst(\n lc: LogContext,\n clientSchema: ClientSchema,\n ast: AST,\n isTransformed: boolean,\n options: RunAstOptions,\n): Promise<AnalyzeQueryResult> {\n const {clientToServerMapper, permissions, host, db} = options;\n const result: AnalyzeQueryResult = {\n warnings: [],\n syncedRows: undefined,\n syncedRowCount: 0,\n start: 0,\n end: 0,\n afterPermissions: undefined,\n readRows: undefined,\n readRowCountsByQuery: {},\n readRowCount: undefined,\n };\n\n if (!isTransformed) {\n // map the AST to server names if not already transformed\n ast = mapAST(ast, must(clientToServerMapper));\n }\n if (options.applyPermissions) {\n result.warnings.push(\n 'Permissions are deprecated and will be removed in an upcoming release. See: https://zero.rocicorp.dev/docs/auth.',\n );\n\n const authData = options.authData ? JSON.parse(options.authData) : {};\n if (!options.authData) {\n result.warnings.push(\n 'No auth data provided. Permission rules will compare to `NULL` wherever an auth data field is referenced.',\n );\n }\n const auth: JWTAuth = {type: 'jwt', raw: '', decoded: authData};\n ast = transformAndHashQuery(\n lc,\n 'clientGroupIDForAnalyze',\n ast,\n must(\n permissions,\n 'Permissions are required when applyPermissions is true',\n ),\n auth,\n false,\n ).transformedAst;\n result.afterPermissions = await formatOutput(ast.table + astToZQL(ast));\n }\n const pipeline = buildPipeline(ast, host, 'query-id');\n\n const start = performance.now();\n\n let syncedRowCount = 0;\n const rowsByTable: Record<string, Row[]> = {};\n const seenByTable: Set<string> = new Set();\n const tableSpecs = computeZqlSpecs(lc, db, {\n includeBackfillingColumns: false,\n });\n for (const rowChange of hydrate(\n pipeline,\n hashOfAST(ast),\n clientSchema,\n tableSpecs,\n )) {\n if (rowChange === 'yield') {\n continue;\n }\n assert(\n rowChange.type === 'add',\n () => `Expected rowChange type 'add', got '${rowChange.type}'`,\n );\n\n let rows: Row[] = rowsByTable[rowChange.table];\n const s = rowChange.table + '.' + JSON.stringify(rowChange.row);\n if (seenByTable.has(s)) {\n continue; // skip duplicates\n }\n syncedRowCount++;\n seenByTable.add(s);\n if (options.syncedRows) {\n if (!rows) {\n rows = [];\n rowsByTable[rowChange.table] = rows;\n }\n rows.push(rowChange.row);\n }\n }\n\n const end = performance.now();\n if (options.syncedRows) {\n result.syncedRows = rowsByTable;\n }\n result.start = start;\n result.end = end;\n\n // Always include the count of synced and vended rows.\n result.syncedRowCount = syncedRowCount;\n result.readRowCountsByQuery = host.debug?.getVendedRowCounts() ?? {};\n let readRowCount = 0;\n for (const c of Object.values(result.readRowCountsByQuery)) {\n for (const v of Object.values(c)) {\n readRowCount += v;\n }\n }\n result.readRowCount = readRowCount;\n\n if (options.vendedRows) {\n result.readRows = host.debug?.getVendedRows();\n }\n return result;\n}\n"],"mappings":";;;;;;;;;;;AAoCA,eAAsB,OACpB,IACA,c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