@rocicorp/zero 1.2.0-canary.0 → 1.2.0-canary.10

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 (163) hide show
  1. package/out/ast-to-zql/src/format.d.ts.map +1 -1
  2. package/out/ast-to-zql/src/format.js +6 -6
  3. package/out/ast-to-zql/src/format.js.map +1 -1
  4. package/out/shared/src/bigint-json.d.ts.map +1 -1
  5. package/out/shared/src/bigint-json.js.map +1 -1
  6. package/out/shared/src/btree-set.d.ts.map +1 -1
  7. package/out/shared/src/btree-set.js +73 -41
  8. package/out/shared/src/btree-set.js.map +1 -1
  9. package/out/z2s/src/sql.d.ts +2 -2
  10. package/out/z2s/src/sql.d.ts.map +1 -1
  11. package/out/z2s/src/sql.js +2 -3
  12. package/out/z2s/src/sql.js.map +1 -1
  13. package/out/zero/package.js +6 -6
  14. package/out/zero/package.js.map +1 -1
  15. package/out/zero-cache/src/auth/load-permissions.d.ts +2 -2
  16. package/out/zero-cache/src/auth/load-permissions.d.ts.map +1 -1
  17. package/out/zero-cache/src/auth/load-permissions.js.map +1 -1
  18. package/out/zero-cache/src/config/zero-config.d.ts +9 -1
  19. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  20. package/out/zero-cache/src/config/zero-config.js +21 -3
  21. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  22. package/out/zero-cache/src/custom/fetch.d.ts +1 -1
  23. package/out/zero-cache/src/custom/fetch.d.ts.map +1 -1
  24. package/out/zero-cache/src/custom-queries/transform-query.d.ts.map +1 -1
  25. package/out/zero-cache/src/custom-queries/transform-query.js +4 -1
  26. package/out/zero-cache/src/custom-queries/transform-query.js.map +1 -1
  27. package/out/zero-cache/src/db/run-transaction.d.ts.map +1 -1
  28. package/out/zero-cache/src/db/run-transaction.js +2 -2
  29. package/out/zero-cache/src/db/run-transaction.js.map +1 -1
  30. package/out/zero-cache/src/db/transaction-pool.d.ts.map +1 -1
  31. package/out/zero-cache/src/db/transaction-pool.js.map +1 -1
  32. package/out/zero-cache/src/observability/metrics.d.ts +1 -1
  33. package/out/zero-cache/src/observability/metrics.d.ts.map +1 -1
  34. package/out/zero-cache/src/observability/metrics.js.map +1 -1
  35. package/out/zero-cache/src/server/anonymous-otel-start.d.ts.map +1 -1
  36. package/out/zero-cache/src/server/anonymous-otel-start.js +6 -1
  37. package/out/zero-cache/src/server/anonymous-otel-start.js.map +1 -1
  38. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  39. package/out/zero-cache/src/server/change-streamer.js +3 -1
  40. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  41. package/out/zero-cache/src/server/logging.d.ts.map +1 -1
  42. package/out/zero-cache/src/server/logging.js +9 -1
  43. package/out/zero-cache/src/server/logging.js.map +1 -1
  44. package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
  45. package/out/zero-cache/src/server/replicator.js +28 -1
  46. package/out/zero-cache/src/server/replicator.js.map +1 -1
  47. package/out/zero-cache/src/services/change-source/change-source.d.ts +1 -1
  48. package/out/zero-cache/src/services/change-source/change-source.d.ts.map +1 -1
  49. package/out/zero-cache/src/services/change-source/common/replica-schema.d.ts.map +1 -1
  50. package/out/zero-cache/src/services/change-source/common/replica-schema.js +13 -1
  51. package/out/zero-cache/src/services/change-source/common/replica-schema.js.map +1 -1
  52. package/out/zero-cache/src/services/change-source/custom/change-source.js +4 -4
  53. package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
  54. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  55. package/out/zero-cache/src/services/change-source/pg/change-source.js +65 -22
  56. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  57. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js +1 -1
  58. package/out/zero-cache/src/services/change-streamer/backup-monitor.d.ts +1 -1
  59. package/out/zero-cache/src/services/change-streamer/backup-monitor.d.ts.map +1 -1
  60. package/out/zero-cache/src/services/change-streamer/backup-monitor.js +31 -1
  61. package/out/zero-cache/src/services/change-streamer/backup-monitor.js.map +1 -1
  62. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +4 -4
  63. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  64. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +2 -3
  65. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  66. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +4 -0
  67. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  68. package/out/zero-cache/src/services/change-streamer/change-streamer.js +9 -1
  69. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  70. package/out/zero-cache/src/services/change-streamer/storer.d.ts.map +1 -1
  71. package/out/zero-cache/src/services/change-streamer/storer.js +1 -1
  72. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  73. package/out/zero-cache/src/services/life-cycle.d.ts +1 -0
  74. package/out/zero-cache/src/services/life-cycle.d.ts.map +1 -1
  75. package/out/zero-cache/src/services/life-cycle.js +2 -2
  76. package/out/zero-cache/src/services/life-cycle.js.map +1 -1
  77. package/out/zero-cache/src/services/litestream/commands.d.ts.map +1 -1
  78. package/out/zero-cache/src/services/litestream/commands.js +5 -5
  79. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  80. package/out/zero-cache/src/services/mutagen/pusher.d.ts +2 -2
  81. package/out/zero-cache/src/services/mutagen/pusher.d.ts.map +1 -1
  82. package/out/zero-cache/src/services/mutagen/pusher.js +7 -4
  83. package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
  84. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts.map +1 -1
  85. package/out/zero-cache/src/services/replicator/incremental-sync.js +6 -3
  86. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  87. package/out/zero-cache/src/services/replicator/schema/column-metadata.d.ts +1 -1
  88. package/out/zero-cache/src/services/replicator/schema/column-metadata.d.ts.map +1 -1
  89. package/out/zero-cache/src/services/replicator/schema/column-metadata.js.map +1 -1
  90. package/out/zero-cache/src/services/replicator/schema/replication-state.d.ts.map +1 -1
  91. package/out/zero-cache/src/services/replicator/schema/replication-state.js +6 -3
  92. package/out/zero-cache/src/services/replicator/schema/replication-state.js.map +1 -1
  93. package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
  94. package/out/zero-cache/src/services/view-syncer/cvr.js +8 -6
  95. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  96. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +1 -1
  97. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  98. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  99. package/out/zero-cache/src/services/view-syncer/row-record-cache.d.ts.map +1 -1
  100. package/out/zero-cache/src/services/view-syncer/row-record-cache.js +13 -7
  101. package/out/zero-cache/src/services/view-syncer/row-record-cache.js.map +1 -1
  102. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +1 -1
  103. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
  104. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  105. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  106. package/out/zero-cache/src/services/view-syncer/view-syncer.js +32 -13
  107. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  108. package/out/zero-cache/src/types/pg.d.ts +1 -0
  109. package/out/zero-cache/src/types/pg.d.ts.map +1 -1
  110. package/out/zero-cache/src/types/pg.js +20 -9
  111. package/out/zero-cache/src/types/pg.js.map +1 -1
  112. package/out/zero-cache/src/workers/connection.js +2 -2
  113. package/out/zero-cache/src/workers/connection.js.map +1 -1
  114. package/out/zero-cache/src/workers/replicator.d.ts +5 -2
  115. package/out/zero-cache/src/workers/replicator.d.ts.map +1 -1
  116. package/out/zero-cache/src/workers/replicator.js +10 -6
  117. package/out/zero-cache/src/workers/replicator.js.map +1 -1
  118. package/out/zero-client/src/client/version.js +1 -1
  119. package/out/zero-client/src/client/zero-poke-handler.d.ts.map +1 -1
  120. package/out/zero-client/src/client/zero-poke-handler.js +6 -2
  121. package/out/zero-client/src/client/zero-poke-handler.js.map +1 -1
  122. package/out/zero-protocol/src/application-error.d.ts +1 -1
  123. package/out/zero-protocol/src/application-error.d.ts.map +1 -1
  124. package/out/zero-protocol/src/application-error.js.map +1 -1
  125. package/out/zero-types/src/name-mapper.d.ts +1 -0
  126. package/out/zero-types/src/name-mapper.d.ts.map +1 -1
  127. package/out/zero-types/src/name-mapper.js +3 -0
  128. package/out/zero-types/src/name-mapper.js.map +1 -1
  129. package/out/zql/src/builder/builder.d.ts.map +1 -1
  130. package/out/zql/src/builder/builder.js +5 -15
  131. package/out/zql/src/builder/builder.js.map +1 -1
  132. package/out/zql/src/ivm/memory-source.d.ts +1 -1
  133. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  134. package/out/zql/src/ivm/memory-source.js +2 -2
  135. package/out/zql/src/ivm/memory-source.js.map +1 -1
  136. package/out/zql/src/ivm/take.d.ts.map +1 -1
  137. package/out/zql/src/ivm/take.js +2 -2
  138. package/out/zql/src/ivm/take.js.map +1 -1
  139. package/out/zql/src/ivm/view-apply-change.d.ts.map +1 -1
  140. package/out/zql/src/ivm/view-apply-change.js +34 -26
  141. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  142. package/out/zql/src/planner/planner-debug.d.ts.map +1 -1
  143. package/out/zql/src/planner/planner-debug.js.map +1 -1
  144. package/out/zql/src/query/expression.d.ts +1 -1
  145. package/out/zql/src/query/expression.d.ts.map +1 -1
  146. package/out/zql/src/query/expression.js.map +1 -1
  147. package/out/zql/src/query/query.d.ts +1 -2
  148. package/out/zql/src/query/query.d.ts.map +1 -1
  149. package/out/zqlite/src/db.d.ts.map +1 -1
  150. package/out/zqlite/src/db.js +1 -1
  151. package/out/zqlite/src/db.js.map +1 -1
  152. package/out/zqlite/src/internal/sql.d.ts +2 -2
  153. package/out/zqlite/src/internal/sql.d.ts.map +1 -1
  154. package/out/zqlite/src/internal/sql.js +1 -2
  155. package/out/zqlite/src/internal/sql.js.map +1 -1
  156. package/out/zqlite/src/table-source.d.ts.map +1 -1
  157. package/out/zqlite/src/table-source.js +7 -11
  158. package/out/zqlite/src/table-source.js.map +1 -1
  159. package/package.json +6 -6
  160. package/out/zql/src/ivm/cap.d.ts +0 -32
  161. package/out/zql/src/ivm/cap.d.ts.map +0 -1
  162. package/out/zql/src/ivm/cap.js +0 -226
  163. package/out/zql/src/ivm/cap.js.map +0 -1
@@ -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 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,MA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,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;AACH,OAAG,OAAO,EAAE;AACZ;GACF,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// Matches: YEAR-MM-DD HH:MM:SS[.fraction][+-TZ[:MM]][ BC]\nconst pgTimestampRe =\n /^(\\d+)-(\\d{2}-\\d{2}) (\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?)([+-]\\d{1,2}(?::\\d{2})?)?(?: BC)?$/;\n\n// exported for testing.\nexport function timestampToFpMillis(timestamp: string): number {\n const match = timestamp.match(pgTimestampRe);\n if (!match) {\n throw new Error(`Error parsing ${timestamp}`);\n }\n\n const [, yearStr, monthDay, time, tz] = match;\n const bc = timestamp.endsWith(' BC');\n\n let year = Number(yearStr);\n if (bc) {\n // Postgres: 1 BC = JS year 0, 2 BC = JS year -1, N BC = -(N-1)\n year = -(year - 1);\n }\n\n // Format year as ISO 8601 expanded year if needed.\n // https://tc39.es/ecma262/#sec-expanded-years\n let isoYear: string;\n if (year >= 0 && year <= 9999) {\n isoYear = String(year).padStart(4, '0');\n } else if (year >= 0) {\n isoYear = '+' + String(year).padStart(6, '0');\n } else {\n isoYear = '-' + String(Math.abs(year)).padStart(6, '0');\n }\n\n // Build a UTC ISO string so PreciseDate returns microsecond precision.\n const utcString = `${isoYear}-${monthDay}T${time}Z`;\n\n try {\n const fullTime = new PreciseDate(utcString).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. We passed local time as UTC,\n // so a positive offset means we need to subtract, and vice versa.\n if (tz) {\n const positiveOffset = tz.startsWith('+');\n const [hours, minutes] = tz.split(':');\n const offset =\n Math.abs(Number(hours)) * 60 + (minutes ? Number(minutes) : 0);\n const offsetMillis = offset * 60 * 1_000;\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\nexport function isPostgresError(e: unknown, ...codes: [string, ...string[]]) {\n return e instanceof postgres.PostgresError && codes.includes(e.code);\n}\n"],"mappings":";;;;;;;AAkBA,IAAM,gBACJ;AAGF,SAAgB,oBAAoB,WAA2B;CAC7D,MAAM,QAAQ,UAAU,MAAM,cAAc;AAC5C,KAAI,CAAC,MACH,OAAM,IAAI,MAAM,iBAAiB,YAAY;CAG/C,MAAM,GAAG,SAAS,UAAU,MAAM,MAAM;CACxC,MAAM,KAAK,UAAU,SAAS,MAAM;CAEpC,IAAI,OAAO,OAAO,QAAQ;AAC1B,KAAI,GAEF,QAAO,EAAE,OAAO;CAKlB,IAAI;AACJ,KAAI,QAAQ,KAAK,QAAQ,KACvB,WAAU,OAAO,KAAK,CAAC,SAAS,GAAG,IAAI;UAC9B,QAAQ,EACjB,WAAU,MAAM,OAAO,KAAK,CAAC,SAAS,GAAG,IAAI;KAE7C,WAAU,MAAM,OAAO,KAAK,IAAI,KAAK,CAAC,CAAC,SAAS,GAAG,IAAI;CAIzD,MAAM,YAAY,GAAG,QAAQ,GAAG,SAAS,GAAG,KAAK;AAEjD,KAAI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,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;AACH,OAAG,OAAO,EAAE;AACZ;GACF,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;AAED,SAAgB,gBAAgB,GAAY,GAAG,OAA8B;AAC3E,QAAO,aAAa,SAAS,iBAAiB,MAAM,SAAS,EAAE,KAAK"}
@@ -6,7 +6,7 @@ import { isProtocolError } from "../../../zero-protocol/src/error.js";
6
6
  import "../../../zero-protocol/src/protocol-version.js";
7
7
  import { ProtocolErrorWithLevel, getLogLevel, wrapWithProtocolError } from "../types/error-with-level.js";
8
8
  import { upstreamSchema } from "../../../zero-protocol/src/up.js";
9
- import WebSocket, { createWebSocketStream } from "ws";
9
+ import WebSocket$1, { createWebSocketStream } from "ws";
10
10
  import { Readable, Writable, pipeline } from "node:stream";
11
11
  //#region ../zero-cache/src/workers/connection.ts
12
12
  var DOWNSTREAM_MSG_INTERVAL_MS = 6e3;
@@ -180,7 +180,7 @@ var Connection = class {
180
180
  }
181
181
  };
182
182
  function send(lc, ws, data, callback) {
183
- if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(data), callback === "ignore-backpressure" ? void 0 : callback);
183
+ if (ws.readyState === WebSocket$1.OPEN) ws.send(JSON.stringify(data), callback === "ignore-backpressure" ? void 0 : callback);
184
184
  else {
185
185
  lc.debug?.(`Dropping outbound message on ws (state: ${ws.readyState})`, { dropped: data });
186
186
  if (callback !== "ignore-backpressure") callback(new ProtocolErrorWithLevel({
@@ -1 +1 @@
1
- {"version":3,"file":"connection.js","names":["#ws","#wsID","#protocolVersion","#lc","#onClose","#messageHandler","#downstreamMsgTimer","#handleClose","#handleError","#proxyInbound","#maybeSendPong","#closeWithError","#closed","#viewSyncerOutboundStream","#pusherOutboundStream","#handleMessage","#handleMessageResult","#closeWithThrown","#proxyOutbound","#lastDownstreamMsgTime"],"sources":["../../../../../zero-cache/src/workers/connection.ts"],"sourcesContent":["import type {LogContext, LogLevel} from '@rocicorp/logger';\nimport {pipeline, Readable, Writable} from 'node:stream';\nimport type {CloseEvent, Data, ErrorEvent} from 'ws';\nimport WebSocket, {createWebSocketStream} from 'ws';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport * as valita from '../../../shared/src/valita.ts';\nimport type {ConnectedMessage} from '../../../zero-protocol/src/connect.ts';\nimport type {Downstream} from '../../../zero-protocol/src/down.ts';\nimport {ErrorKind} from '../../../zero-protocol/src/error-kind.ts';\nimport type {ErrorBody} from '../../../zero-protocol/src/error.ts';\nimport {\n MIN_SERVER_SUPPORTED_SYNC_PROTOCOL,\n PROTOCOL_VERSION,\n} from '../../../zero-protocol/src/protocol-version.ts';\nimport {upstreamSchema, type Upstream} from '../../../zero-protocol/src/up.ts';\nimport {\n ProtocolErrorWithLevel,\n getLogLevel,\n wrapWithProtocolError,\n} from '../types/error-with-level.ts';\nimport type {Source} from '../types/streams.ts';\nimport type {ConnectParams} from './connect-params.ts';\nimport {\n isProtocolError,\n type ProtocolError,\n} from '../../../zero-protocol/src/error.ts';\nimport {ErrorOrigin} from '../../../zero-protocol/src/error-origin.ts';\n\nexport type HandlerResult =\n | {\n type: 'ok';\n }\n | {\n type: 'fatal';\n error: ErrorBody;\n }\n | {\n type: 'transient';\n errors: ErrorBody[];\n }\n | StreamResult;\n\nexport type StreamResult = {\n type: 'stream';\n source: 'viewSyncer' | 'pusher';\n stream: Source<Downstream>;\n};\n\nexport interface MessageHandler {\n handleMessage(msg: Upstream): Promise<HandlerResult[]>;\n}\n\n// Ensures that a downstream message is sent at least every interval, sending a\n// 'pong' if necessary. This is set to be slightly longer than the client-side\n// PING_INTERVAL of 5 seconds, so that in the common case, 'pong's are sent in\n// response to client-initiated 'ping's. However, if the inbound stream is\n// backed up because a command is taking a long time to process, the pings\n// will be stuck in the queue (i.e. back-pressured), in which case pongs will\n// be manually sent to notify the client of server liveness.\n//\n// This is equivalent to what is done for Postgres keepalives on the\n// replication stream (which can similarly be back-pressured):\n// https://github.com/rocicorp/mono/blob/f98cb369a2dbb15650328859c732db358f187ef0/packages/zero-cache/src/services/change-source/pg/logical-replication/stream.ts#L21\nconst DOWNSTREAM_MSG_INTERVAL_MS = 6_000;\n\n/**\n * Represents a connection between the client and server.\n *\n * Handles incoming messages on the connection and dispatches\n * them to the correct service.\n *\n * Listens to the ViewSyncer and sends messages to the client.\n */\nexport class Connection {\n readonly #ws: WebSocket;\n readonly #wsID: string;\n readonly #protocolVersion: number;\n readonly #lc: LogContext;\n readonly #onClose: () => void;\n readonly #messageHandler: MessageHandler;\n readonly #downstreamMsgTimer: NodeJS.Timeout | undefined;\n\n #viewSyncerOutboundStream: Source<Downstream> | undefined;\n #pusherOutboundStream: Source<Downstream> | undefined;\n #closed = false;\n\n constructor(\n lc: LogContext,\n connectParams: ConnectParams,\n ws: WebSocket,\n messageHandler: MessageHandler,\n onClose: () => void,\n ) {\n const {clientGroupID, clientID, wsID, protocolVersion} = connectParams;\n this.#messageHandler = messageHandler;\n\n this.#ws = ws;\n this.#wsID = wsID;\n this.#protocolVersion = protocolVersion;\n\n this.#lc = lc\n .withContext('connection')\n .withContext('clientID', clientID)\n .withContext('clientGroupID', clientGroupID)\n .withContext('wsID', wsID);\n this.#lc.debug?.('new connection');\n this.#onClose = onClose;\n\n this.#ws.addEventListener('close', this.#handleClose);\n this.#ws.addEventListener('error', this.#handleError);\n\n this.#proxyInbound();\n this.#downstreamMsgTimer = setInterval(\n this.#maybeSendPong,\n DOWNSTREAM_MSG_INTERVAL_MS / 2,\n );\n }\n\n /**\n * Checks the protocol version and errors for unsupported protocols,\n * sending the initial `connected` response on success.\n *\n * This is early in the connection lifecycle because {@link #handleMessage}\n * will only parse messages with schema(s) of supported protocol versions.\n */\n init(): boolean {\n if (\n this.#protocolVersion > PROTOCOL_VERSION ||\n this.#protocolVersion < MIN_SERVER_SUPPORTED_SYNC_PROTOCOL\n ) {\n this.#closeWithError({\n kind: ErrorKind.VersionNotSupported,\n message: `server is at sync protocol v${PROTOCOL_VERSION} and does not support v${\n this.#protocolVersion\n }. The ${\n this.#protocolVersion > PROTOCOL_VERSION ? 'server' : 'client'\n } must be updated to a newer release.`,\n origin: ErrorOrigin.ZeroCache,\n });\n } else {\n const connectedMessage: ConnectedMessage = [\n 'connected',\n {wsid: this.#wsID, timestamp: Date.now()},\n ];\n this.send(connectedMessage, 'ignore-backpressure');\n return true;\n }\n return false;\n }\n\n close(reason: string, ...args: unknown[]) {\n if (this.#closed) {\n return;\n }\n this.#closed = true;\n this.#lc.info?.(`closing connection: ${reason}`, ...args);\n this.#ws.removeEventListener('close', this.#handleClose);\n this.#ws.removeEventListener('error', this.#handleError);\n this.#viewSyncerOutboundStream?.cancel();\n this.#viewSyncerOutboundStream = undefined;\n this.#pusherOutboundStream?.cancel();\n this.#pusherOutboundStream = undefined;\n this.#onClose();\n if (this.#ws.readyState !== this.#ws.CLOSED) {\n this.#ws.close();\n }\n clearTimeout(this.#downstreamMsgTimer);\n\n // spin down services if we have\n // no more client connections for the client group?\n }\n\n handleInitConnection(initConnectionMsg: string) {\n return this.#handleMessage({data: initConnectionMsg});\n }\n\n #handleMessage = async (event: {data: Data}) => {\n const data = event.data.toString();\n if (this.#closed) {\n this.#lc.debug?.('Ignoring message received after closed', data);\n return;\n }\n\n let msg;\n try {\n const value = JSON.parse(data);\n msg = valita.parse(value, upstreamSchema);\n } catch (e) {\n this.#lc.warn?.(`failed to parse message \"${data}\": ${String(e)}`);\n this.#closeWithError(\n {\n kind: ErrorKind.InvalidMessage,\n message: String(e),\n origin: ErrorOrigin.ZeroCache,\n },\n e,\n );\n return;\n }\n\n try {\n const msgType = msg[0];\n if (msgType === 'ping') {\n this.send(['pong', {}], 'ignore-backpressure');\n return;\n }\n\n const result = await this.#messageHandler.handleMessage(msg);\n for (const r of result) {\n this.#handleMessageResult(r);\n }\n } catch (e) {\n this.#closeWithThrown(e);\n }\n };\n\n #handleMessageResult(result: HandlerResult): void {\n switch (result.type) {\n case 'fatal':\n this.#closeWithError(result.error);\n break;\n case 'ok':\n break;\n case 'stream': {\n switch (result.source) {\n case 'viewSyncer':\n assert(\n this.#viewSyncerOutboundStream === undefined,\n 'Outbound stream already set for this connection!',\n );\n this.#viewSyncerOutboundStream = result.stream;\n break;\n case 'pusher':\n assert(\n this.#pusherOutboundStream === undefined,\n 'Outbound stream already set for this connection!',\n );\n this.#pusherOutboundStream = result.stream;\n break;\n }\n this.#proxyOutbound(result.stream);\n break;\n }\n case 'transient': {\n for (const error of result.errors) {\n void this.sendError(error);\n }\n }\n }\n }\n\n #handleClose = (e: CloseEvent) => {\n const {code, reason, wasClean} = e;\n this.close('WebSocket close event', {code, reason, wasClean});\n };\n\n #handleError = (e: ErrorEvent) => {\n this.#lc.error?.('WebSocket error event', e.message, e.error);\n };\n\n #proxyInbound() {\n pipeline(\n createWebSocketStream(this.#ws),\n new Writable({\n write: (data, _encoding, callback) => {\n this.#handleMessage({data}).then(() => callback(), callback);\n },\n }),\n // The done callback is not used, as #handleClose and #handleError,\n // configured on the underlying WebSocket, provide more complete\n // information.\n () => {},\n );\n }\n\n #proxyOutbound(outboundStream: Source<Downstream>) {\n // Note: createWebSocketStream() is avoided here in order to control\n // exception handling with #closeWithThrown(). If the Writable\n // from createWebSocketStream() were instead used, exceptions\n // from the outboundStream result in the Writable closing the\n // the websocket before the error message can be sent.\n pipeline(\n Readable.from(outboundStream),\n new Writable({\n objectMode: true,\n write: (downstream: Downstream, _encoding, callback) =>\n this.send(downstream, callback),\n }),\n e =>\n e\n ? this.#closeWithThrown(e)\n : this.close(`downstream closed by ViewSyncer`),\n );\n }\n\n #closeWithThrown(e: unknown) {\n const errorBody =\n findProtocolError(e)?.errorBody ?? wrapWithProtocolError(e).errorBody;\n\n this.#closeWithError(errorBody, e);\n }\n\n #closeWithError(errorBody: ErrorBody, thrown?: unknown) {\n this.sendError(errorBody, thrown);\n this.close(\n `${errorBody.kind} (${errorBody.origin}): ${errorBody.message}`,\n errorBody,\n );\n }\n\n #lastDownstreamMsgTime = Date.now();\n\n #maybeSendPong = () => {\n if (Date.now() - this.#lastDownstreamMsgTime > DOWNSTREAM_MSG_INTERVAL_MS) {\n this.#lc.debug?.('manually sending pong');\n this.send(['pong', {}], 'ignore-backpressure');\n }\n };\n\n send(\n data: Downstream,\n callback: ((err?: Error | null) => void) | 'ignore-backpressure',\n ) {\n this.#lastDownstreamMsgTime = Date.now();\n return send(this.#lc, this.#ws, data, callback);\n }\n\n sendError(errorBody: ErrorBody, thrown?: unknown) {\n sendError(this.#lc, this.#ws, errorBody, thrown);\n }\n}\n\nexport type WebSocketLike = Pick<WebSocket, 'readyState'> & {\n send(data: string, cb?: (err?: Error) => void): void;\n};\n\n// Exported for testing purposes.\nexport function send(\n lc: LogContext,\n ws: WebSocketLike,\n data: Downstream,\n callback: ((err?: Error | null) => void) | 'ignore-backpressure',\n) {\n if (ws.readyState === WebSocket.OPEN) {\n ws.send(\n JSON.stringify(data),\n callback === 'ignore-backpressure' ? undefined : callback,\n );\n } else {\n lc.debug?.(`Dropping outbound message on ws (state: ${ws.readyState})`, {\n dropped: data,\n });\n if (callback !== 'ignore-backpressure') {\n callback(\n new ProtocolErrorWithLevel(\n {\n kind: ErrorKind.Internal,\n message: 'WebSocket closed',\n origin: ErrorOrigin.ZeroCache,\n },\n 'info',\n ),\n );\n }\n }\n}\n\nexport function sendError(\n lc: LogContext,\n ws: WebSocket,\n errorBody: ErrorBody,\n thrown?: unknown,\n) {\n lc = lc.withContext('errorKind', errorBody.kind);\n\n let logLevel: LogLevel;\n\n // If the thrown error is a ProtocolErrorWithLevel, its explicit logLevel takes precedence\n if (thrown instanceof ProtocolErrorWithLevel) {\n logLevel = thrown.logLevel;\n }\n // Errors with errno or transient socket codes are low-level, transient I/O issues\n // (e.g., EPIPE, ECONNRESET) and should be warnings, not errors\n else if (\n hasErrno(thrown) ||\n hasTransientSocketCode(thrown) ||\n isTransientSocketMessage(errorBody.message)\n ) {\n logLevel = 'warn';\n }\n // Fallback: check errorBody.kind for errors that weren't thrown as ProtocolErrorWithLevel\n else if (\n errorBody.kind === ErrorKind.ClientNotFound ||\n errorBody.kind === ErrorKind.TransformFailed\n ) {\n logLevel = 'warn';\n } else {\n logLevel = thrown ? getLogLevel(thrown) : 'info';\n }\n\n lc[logLevel]?.('Sending error on WebSocket', errorBody, thrown ?? '');\n send(lc, ws, ['error', errorBody], 'ignore-backpressure');\n}\n\nexport function findProtocolError(error: unknown): ProtocolError | undefined {\n if (isProtocolError(error)) {\n return error;\n }\n if (error instanceof Error && error.cause) {\n return findProtocolError(error.cause);\n }\n return undefined;\n}\n\nfunction hasErrno(error: unknown): boolean {\n return Boolean(\n error &&\n typeof error === 'object' &&\n 'errno' in error &&\n typeof (error as {errno: unknown}).errno !== 'undefined',\n );\n}\n\n// System error codes that indicate transient socket conditions.\n// These are checked via the `code` property on errors.\nconst TRANSIENT_SOCKET_ERROR_CODES = new Set([\n 'EPIPE',\n 'ECONNRESET',\n 'ECANCELED',\n]);\n\n// Error messages that indicate transient socket conditions but don't have\n// standard error codes (e.g., WebSocket library errors).\nconst TRANSIENT_SOCKET_MESSAGE_PATTERNS = [\n 'socket was closed while data was being compressed',\n];\n\nfunction hasTransientSocketCode(error: unknown): boolean {\n if (!error || typeof error !== 'object') {\n return false;\n }\n const maybeCode =\n 'code' in error ? String((error as {code?: unknown}).code) : undefined;\n return Boolean(\n maybeCode && TRANSIENT_SOCKET_ERROR_CODES.has(maybeCode.toUpperCase()),\n );\n}\n\nfunction isTransientSocketMessage(message: string | undefined): boolean {\n if (!message) {\n return false;\n }\n const lower = message.toLowerCase();\n return TRANSIENT_SOCKET_MESSAGE_PATTERNS.some(pattern =>\n lower.includes(pattern),\n );\n}\n"],"mappings":";;;;;;;;;;;AA+DA,IAAM,6BAA6B;;;;;;;;;AAUnC,IAAa,aAAb,MAAwB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;CACA;CACA,UAAU;CAEV,YACE,IACA,eACA,IACA,gBACA,SACA;EACA,MAAM,EAAC,eAAe,UAAU,MAAM,oBAAmB;AACzD,QAAA,iBAAuB;AAEvB,QAAA,KAAW;AACX,QAAA,OAAa;AACb,QAAA,kBAAwB;AAExB,QAAA,KAAW,GACR,YAAY,aAAa,CACzB,YAAY,YAAY,SAAS,CACjC,YAAY,iBAAiB,cAAc,CAC3C,YAAY,QAAQ,KAAK;AAC5B,QAAA,GAAS,QAAQ,iBAAiB;AAClC,QAAA,UAAgB;AAEhB,QAAA,GAAS,iBAAiB,SAAS,MAAA,YAAkB;AACrD,QAAA,GAAS,iBAAiB,SAAS,MAAA,YAAkB;AAErD,QAAA,cAAoB;AACpB,QAAA,qBAA2B,YACzB,MAAA,eACA,6BAA6B,EAC9B;;;;;;;;;CAUH,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AS,YAAY,OAAO,GAAG;AAG5C,IAAG,YAAY,8BAA8B,WAAW,UAAU,GAAG;AACrE,MAAK,IAAI,IAAI,CAAC,SAAS,UAAU,EAAE,sBAAsB;;AAG3D,SAAgB,kBAAkB,OAA2C;AAC3E,KAAI,gBAAgB,MAAM,CACxB,QAAO;AAET,KAAI,iBAAiB,SAAS,MAAM,MAClC,QAAO,kBAAkB,MAAM,MAAM;;AAKzC,SAAS,SAAS,OAAyB;AACzC,QAAO,QACL,SACA,OAAO,UAAU,YACjB,WAAW,SACX,OAAQ,MAA2B,UAAU,YAC9C;;AAKH,IAAM,+BAA+B,IAAI,IAAI;CAC3C;CACA;CACA;CACD,CAAC;AAIF,IAAM,oCAAoC,CACxC,oDACD;AAED,SAAS,uBAAuB,OAAyB;AACvD,KAAI,CAAC,SAAS,OAAO,UAAU,SAC7B,QAAO;CAET,MAAM,YACJ,UAAU,QAAQ,OAAQ,MAA2B,KAAK,GAAG,KAAA;AAC/D,QAAO,QACL,aAAa,6BAA6B,IAAI,UAAU,aAAa,CAAC,CACvE;;AAGH,SAAS,yBAAyB,SAAsC;AACtE,KAAI,CAAC,QACH,QAAO;CAET,MAAM,QAAQ,QAAQ,aAAa;AACnC,QAAO,kCAAkC,MAAK,YAC5C,MAAM,SAAS,QAAQ,CACxB"}
1
+ {"version":3,"file":"connection.js","names":["#ws","#wsID","#protocolVersion","#lc","#onClose","#messageHandler","#downstreamMsgTimer","#handleClose","#handleError","#proxyInbound","#maybeSendPong","#closeWithError","#closed","#viewSyncerOutboundStream","#pusherOutboundStream","#handleMessage","#handleMessageResult","#closeWithThrown","#proxyOutbound","#lastDownstreamMsgTime"],"sources":["../../../../../zero-cache/src/workers/connection.ts"],"sourcesContent":["import type {LogContext, LogLevel} from '@rocicorp/logger';\nimport {pipeline, Readable, Writable} from 'node:stream';\nimport type {CloseEvent, Data, ErrorEvent} from 'ws';\nimport WebSocket, {createWebSocketStream} from 'ws';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport * as valita from '../../../shared/src/valita.ts';\nimport type {ConnectedMessage} from '../../../zero-protocol/src/connect.ts';\nimport type {Downstream} from '../../../zero-protocol/src/down.ts';\nimport {ErrorKind} from '../../../zero-protocol/src/error-kind.ts';\nimport type {ErrorBody} from '../../../zero-protocol/src/error.ts';\nimport {\n MIN_SERVER_SUPPORTED_SYNC_PROTOCOL,\n PROTOCOL_VERSION,\n} from '../../../zero-protocol/src/protocol-version.ts';\nimport {upstreamSchema, type Upstream} from '../../../zero-protocol/src/up.ts';\nimport {\n ProtocolErrorWithLevel,\n getLogLevel,\n wrapWithProtocolError,\n} from '../types/error-with-level.ts';\nimport type {Source} from '../types/streams.ts';\nimport type {ConnectParams} from './connect-params.ts';\nimport {\n isProtocolError,\n type ProtocolError,\n} from '../../../zero-protocol/src/error.ts';\nimport {ErrorOrigin} from '../../../zero-protocol/src/error-origin.ts';\n\nexport type HandlerResult =\n | {\n type: 'ok';\n }\n | {\n type: 'fatal';\n error: ErrorBody;\n }\n | {\n type: 'transient';\n errors: ErrorBody[];\n }\n | StreamResult;\n\nexport type StreamResult = {\n type: 'stream';\n source: 'viewSyncer' | 'pusher';\n stream: Source<Downstream>;\n};\n\nexport interface MessageHandler {\n handleMessage(msg: Upstream): Promise<HandlerResult[]>;\n}\n\n// Ensures that a downstream message is sent at least every interval, sending a\n// 'pong' if necessary. This is set to be slightly longer than the client-side\n// PING_INTERVAL of 5 seconds, so that in the common case, 'pong's are sent in\n// response to client-initiated 'ping's. However, if the inbound stream is\n// backed up because a command is taking a long time to process, the pings\n// will be stuck in the queue (i.e. back-pressured), in which case pongs will\n// be manually sent to notify the client of server liveness.\n//\n// This is equivalent to what is done for Postgres keepalives on the\n// replication stream (which can similarly be back-pressured):\n// https://github.com/rocicorp/mono/blob/f98cb369a2dbb15650328859c732db358f187ef0/packages/zero-cache/src/services/change-source/pg/logical-replication/stream.ts#L21\nconst DOWNSTREAM_MSG_INTERVAL_MS = 6_000;\n\n/**\n * Represents a connection between the client and server.\n *\n * Handles incoming messages on the connection and dispatches\n * them to the correct service.\n *\n * Listens to the ViewSyncer and sends messages to the client.\n */\nexport class Connection {\n readonly #ws: WebSocket;\n readonly #wsID: string;\n readonly #protocolVersion: number;\n readonly #lc: LogContext;\n readonly #onClose: () => void;\n readonly #messageHandler: MessageHandler;\n readonly #downstreamMsgTimer: NodeJS.Timeout | undefined;\n\n #viewSyncerOutboundStream: Source<Downstream> | undefined;\n #pusherOutboundStream: Source<Downstream> | undefined;\n #closed = false;\n\n constructor(\n lc: LogContext,\n connectParams: ConnectParams,\n ws: WebSocket,\n messageHandler: MessageHandler,\n onClose: () => void,\n ) {\n const {clientGroupID, clientID, wsID, protocolVersion} = connectParams;\n this.#messageHandler = messageHandler;\n\n this.#ws = ws;\n this.#wsID = wsID;\n this.#protocolVersion = protocolVersion;\n\n this.#lc = lc\n .withContext('connection')\n .withContext('clientID', clientID)\n .withContext('clientGroupID', clientGroupID)\n .withContext('wsID', wsID);\n this.#lc.debug?.('new connection');\n this.#onClose = onClose;\n\n this.#ws.addEventListener('close', this.#handleClose);\n this.#ws.addEventListener('error', this.#handleError);\n\n this.#proxyInbound();\n this.#downstreamMsgTimer = setInterval(\n this.#maybeSendPong,\n DOWNSTREAM_MSG_INTERVAL_MS / 2,\n );\n }\n\n /**\n * Checks the protocol version and errors for unsupported protocols,\n * sending the initial `connected` response on success.\n *\n * This is early in the connection lifecycle because {@link #handleMessage}\n * will only parse messages with schema(s) of supported protocol versions.\n */\n init(): boolean {\n if (\n this.#protocolVersion > PROTOCOL_VERSION ||\n this.#protocolVersion < MIN_SERVER_SUPPORTED_SYNC_PROTOCOL\n ) {\n this.#closeWithError({\n kind: ErrorKind.VersionNotSupported,\n message: `server is at sync protocol v${PROTOCOL_VERSION} and does not support v${\n this.#protocolVersion\n }. The ${\n this.#protocolVersion > PROTOCOL_VERSION ? 'server' : 'client'\n } must be updated to a newer release.`,\n origin: ErrorOrigin.ZeroCache,\n });\n } else {\n const connectedMessage: ConnectedMessage = [\n 'connected',\n {wsid: this.#wsID, timestamp: Date.now()},\n ];\n this.send(connectedMessage, 'ignore-backpressure');\n return true;\n }\n return false;\n }\n\n close(reason: string, ...args: unknown[]) {\n if (this.#closed) {\n return;\n }\n this.#closed = true;\n this.#lc.info?.(`closing connection: ${reason}`, ...args);\n this.#ws.removeEventListener('close', this.#handleClose);\n this.#ws.removeEventListener('error', this.#handleError);\n this.#viewSyncerOutboundStream?.cancel();\n this.#viewSyncerOutboundStream = undefined;\n this.#pusherOutboundStream?.cancel();\n this.#pusherOutboundStream = undefined;\n this.#onClose();\n if (this.#ws.readyState !== this.#ws.CLOSED) {\n this.#ws.close();\n }\n clearTimeout(this.#downstreamMsgTimer);\n\n // spin down services if we have\n // no more client connections for the client group?\n }\n\n handleInitConnection(initConnectionMsg: string) {\n return this.#handleMessage({data: initConnectionMsg});\n }\n\n #handleMessage = async (event: {data: Data}) => {\n const data = event.data.toString();\n if (this.#closed) {\n this.#lc.debug?.('Ignoring message received after closed', data);\n return;\n }\n\n let msg;\n try {\n const value = JSON.parse(data);\n msg = valita.parse(value, upstreamSchema);\n } catch (e) {\n this.#lc.warn?.(`failed to parse message \"${data}\": ${String(e)}`);\n this.#closeWithError(\n {\n kind: ErrorKind.InvalidMessage,\n message: String(e),\n origin: ErrorOrigin.ZeroCache,\n },\n e,\n );\n return;\n }\n\n try {\n const msgType = msg[0];\n if (msgType === 'ping') {\n this.send(['pong', {}], 'ignore-backpressure');\n return;\n }\n\n const result = await this.#messageHandler.handleMessage(msg);\n for (const r of result) {\n this.#handleMessageResult(r);\n }\n } catch (e) {\n this.#closeWithThrown(e);\n }\n };\n\n #handleMessageResult(result: HandlerResult): void {\n switch (result.type) {\n case 'fatal':\n this.#closeWithError(result.error);\n break;\n case 'ok':\n break;\n case 'stream': {\n switch (result.source) {\n case 'viewSyncer':\n assert(\n this.#viewSyncerOutboundStream === undefined,\n 'Outbound stream already set for this connection!',\n );\n this.#viewSyncerOutboundStream = result.stream;\n break;\n case 'pusher':\n assert(\n this.#pusherOutboundStream === undefined,\n 'Outbound stream already set for this connection!',\n );\n this.#pusherOutboundStream = result.stream;\n break;\n }\n this.#proxyOutbound(result.stream);\n break;\n }\n case 'transient': {\n for (const error of result.errors) {\n this.sendError(error);\n }\n }\n }\n }\n\n #handleClose = (e: CloseEvent) => {\n const {code, reason, wasClean} = e;\n this.close('WebSocket close event', {code, reason, wasClean});\n };\n\n #handleError = (e: ErrorEvent) => {\n this.#lc.error?.('WebSocket error event', e.message, e.error);\n };\n\n #proxyInbound() {\n pipeline(\n createWebSocketStream(this.#ws),\n new Writable({\n write: (data, _encoding, callback) => {\n this.#handleMessage({data}).then(() => callback(), callback);\n },\n }),\n // The done callback is not used, as #handleClose and #handleError,\n // configured on the underlying WebSocket, provide more complete\n // information.\n () => {},\n );\n }\n\n #proxyOutbound(outboundStream: Source<Downstream>) {\n // Note: createWebSocketStream() is avoided here in order to control\n // exception handling with #closeWithThrown(). If the Writable\n // from createWebSocketStream() were instead used, exceptions\n // from the outboundStream result in the Writable closing the\n // the websocket before the error message can be sent.\n pipeline(\n Readable.from(outboundStream),\n new Writable({\n objectMode: true,\n write: (downstream: Downstream, _encoding, callback) =>\n this.send(downstream, callback),\n }),\n e =>\n e\n ? this.#closeWithThrown(e)\n : this.close(`downstream closed by ViewSyncer`),\n );\n }\n\n #closeWithThrown(e: unknown) {\n const errorBody =\n findProtocolError(e)?.errorBody ?? wrapWithProtocolError(e).errorBody;\n\n this.#closeWithError(errorBody, e);\n }\n\n #closeWithError(errorBody: ErrorBody, thrown?: unknown) {\n this.sendError(errorBody, thrown);\n this.close(\n `${errorBody.kind} (${errorBody.origin}): ${errorBody.message}`,\n errorBody,\n );\n }\n\n #lastDownstreamMsgTime = Date.now();\n\n #maybeSendPong = () => {\n if (Date.now() - this.#lastDownstreamMsgTime > DOWNSTREAM_MSG_INTERVAL_MS) {\n this.#lc.debug?.('manually sending pong');\n this.send(['pong', {}], 'ignore-backpressure');\n }\n };\n\n send(\n data: Downstream,\n callback: ((err?: Error | null) => void) | 'ignore-backpressure',\n ) {\n this.#lastDownstreamMsgTime = Date.now();\n return send(this.#lc, this.#ws, data, callback);\n }\n\n sendError(errorBody: ErrorBody, thrown?: unknown) {\n sendError(this.#lc, this.#ws, errorBody, thrown);\n }\n}\n\nexport type WebSocketLike = Pick<WebSocket, 'readyState'> & {\n send(data: string, cb?: (err?: Error) => void): void;\n};\n\n// Exported for testing purposes.\nexport function send(\n lc: LogContext,\n ws: WebSocketLike,\n data: Downstream,\n callback: ((err?: Error | null) => void) | 'ignore-backpressure',\n) {\n if (ws.readyState === WebSocket.OPEN) {\n ws.send(\n JSON.stringify(data),\n callback === 'ignore-backpressure' ? undefined : callback,\n );\n } else {\n lc.debug?.(`Dropping outbound message on ws (state: ${ws.readyState})`, {\n dropped: data,\n });\n if (callback !== 'ignore-backpressure') {\n callback(\n new ProtocolErrorWithLevel(\n {\n kind: ErrorKind.Internal,\n message: 'WebSocket closed',\n origin: ErrorOrigin.ZeroCache,\n },\n 'info',\n ),\n );\n }\n }\n}\n\nexport function sendError(\n lc: LogContext,\n ws: WebSocket,\n errorBody: ErrorBody,\n thrown?: unknown,\n) {\n lc = lc.withContext('errorKind', errorBody.kind);\n\n let logLevel: LogLevel;\n\n // If the thrown error is a ProtocolErrorWithLevel, its explicit logLevel takes precedence\n if (thrown instanceof ProtocolErrorWithLevel) {\n logLevel = thrown.logLevel;\n }\n // Errors with errno or transient socket codes are low-level, transient I/O issues\n // (e.g., EPIPE, ECONNRESET) and should be warnings, not errors\n else if (\n hasErrno(thrown) ||\n hasTransientSocketCode(thrown) ||\n isTransientSocketMessage(errorBody.message)\n ) {\n logLevel = 'warn';\n }\n // Fallback: check errorBody.kind for errors that weren't thrown as ProtocolErrorWithLevel\n else if (\n errorBody.kind === ErrorKind.ClientNotFound ||\n errorBody.kind === ErrorKind.TransformFailed\n ) {\n logLevel = 'warn';\n } else {\n logLevel = thrown ? getLogLevel(thrown) : 'info';\n }\n\n lc[logLevel]?.('Sending error on WebSocket', errorBody, thrown ?? '');\n send(lc, ws, ['error', errorBody], 'ignore-backpressure');\n}\n\nexport function findProtocolError(error: unknown): ProtocolError | undefined {\n if (isProtocolError(error)) {\n return error;\n }\n if (error instanceof Error && error.cause) {\n return findProtocolError(error.cause);\n }\n return undefined;\n}\n\nfunction hasErrno(error: unknown): boolean {\n return Boolean(\n error &&\n typeof error === 'object' &&\n 'errno' in error &&\n typeof (error as {errno: unknown}).errno !== 'undefined',\n );\n}\n\n// System error codes that indicate transient socket conditions.\n// These are checked via the `code` property on errors.\nconst TRANSIENT_SOCKET_ERROR_CODES = new Set([\n 'EPIPE',\n 'ECONNRESET',\n 'ECANCELED',\n]);\n\n// Error messages that indicate transient socket conditions but don't have\n// standard error codes (e.g., WebSocket library errors).\nconst TRANSIENT_SOCKET_MESSAGE_PATTERNS = [\n 'socket was closed while data was being compressed',\n];\n\nfunction hasTransientSocketCode(error: unknown): boolean {\n if (!error || typeof error !== 'object') {\n return false;\n }\n const maybeCode =\n 'code' in error ? String((error as {code?: unknown}).code) : undefined;\n return Boolean(\n maybeCode && TRANSIENT_SOCKET_ERROR_CODES.has(maybeCode.toUpperCase()),\n );\n}\n\nfunction isTransientSocketMessage(message: string | undefined): boolean {\n if (!message) {\n return false;\n }\n const lower = message.toLowerCase();\n return TRANSIENT_SOCKET_MESSAGE_PATTERNS.some(pattern =>\n lower.includes(pattern),\n );\n}\n"],"mappings":";;;;;;;;;;;AA+DA,IAAM,6BAA6B;;;;;;;;;AAUnC,IAAa,aAAb,MAAwB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;CACA;CACA,UAAU;CAEV,YACE,IACA,eACA,IACA,gBACA,SACA;EACA,MAAM,EAAC,eAAe,UAAU,MAAM,oBAAmB;AACzD,QAAA,iBAAuB;AAEvB,QAAA,KAAW;AACX,QAAA,OAAa;AACb,QAAA,kBAAwB;AAExB,QAAA,KAAW,GACR,YAAY,aAAa,CACzB,YAAY,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T,EACD,OACD,CACF;;;AAKP,SAAgB,UACd,IACA,IACA,WACA,QACA;AACA,MAAK,GAAG,YAAY,aAAa,UAAU,KAAK;CAEhD,IAAI;AAGJ,KAAI,kBAAkB,uBACpB,YAAW,OAAO;UAKlB,SAAS,OAAO,IAChB,uBAAuB,OAAO,IAC9B,yBAAyB,UAAU,QAAQ,CAE3C,YAAW;UAIX,UAAU,SAAS,oBACnB,UAAU,SAAS,kBAEnB,YAAW;KAEX,YAAW,SAAS,YAAY,OAAO,GAAG;AAG5C,IAAG,YAAY,8BAA8B,WAAW,UAAU,GAAG;AACrE,MAAK,IAAI,IAAI,CAAC,SAAS,UAAU,EAAE,sBAAsB;;AAG3D,SAAgB,kBAAkB,OAA2C;AAC3E,KAAI,gBAAgB,MAAM,CACxB,QAAO;AAET,KAAI,iBAAiB,SAAS,MAAM,MAClC,QAAO,kBAAkB,MAAM,MAAM;;AAKzC,SAAS,SAAS,OAAyB;AACzC,QAAO,QACL,SACA,OAAO,UAAU,YACjB,WAAW,SACX,OAAQ,MAA2B,UAAU,YAC9C;;AAKH,IAAM,+BAA+B,IAAI,IAAI;CAC3C;CACA;CACA;CACD,CAAC;AAIF,IAAM,oCAAoC,CACxC,oDACD;AAED,SAAS,uBAAuB,OAAyB;AACvD,KAAI,CAAC,SAAS,OAAO,UAAU,SAC7B,QAAO;CAET,MAAM,YACJ,UAAU,QAAQ,OAAQ,MAA2B,KAAK,GAAG,KAAA;AAC/D,QAAO,QACL,aAAa,6BAA6B,IAAI,UAAU,aAAa,CAAC,CACvE;;AAGH,SAAS,yBAAyB,SAAsC;AACtE,KAAI,CAAC,QACH,QAAO;CAET,MAAM,QAAQ,QAAQ,aAAa;AACnC,QAAO,kCAAkC,MAAK,YAC5C,MAAM,SAAS,QAAQ,CACxB"}
@@ -1,6 +1,5 @@
1
1
  import type { LogContext } from '@rocicorp/logger';
2
2
  import * as v from '../../../shared/src/valita.ts';
3
- import { Database } from '../../../zqlite/src/db.ts';
4
3
  import type { ReplicaOptions } from '../config/zero-config.ts';
5
4
  import { Notifier } from '../services/replicator/notifier.ts';
6
5
  import type { ReplicaStateNotifier, Replicator } from '../services/replicator/replicator.ts';
@@ -8,6 +7,7 @@ import { type PragmaConfig } from '../services/replicator/write-worker-client.ts
8
7
  import type { Worker } from '../types/processes.ts';
9
8
  export declare const replicaFileModeSchema: v.Type<"backup" | "serving" | "serving-copy">;
10
9
  export type ReplicaFileMode = v.Infer<typeof replicaFileModeSchema>;
10
+ export type WalMode = 'wal' | 'wal2';
11
11
  export declare function replicaFileName(replicaFile: string, mode: ReplicaFileMode): string;
12
12
  /**
13
13
  * Returns the PragmaConfig for a given replica file mode.
@@ -15,7 +15,10 @@ export declare function replicaFileName(replicaFile: string, mode: ReplicaFileMo
15
15
  * the write worker thread to apply the same pragma settings.
16
16
  */
17
17
  export declare function getPragmaConfig(mode: ReplicaFileMode): PragmaConfig;
18
- export declare function setupReplica(lc: LogContext, mode: ReplicaFileMode, replicaOptions: ReplicaOptions): Promise<Database>;
18
+ export declare function setupReplica(lc: LogContext, mode: ReplicaFileMode, replicaOptions: ReplicaOptions): Promise<{
19
+ file: string;
20
+ walMode: WalMode;
21
+ }>;
19
22
  export declare function setUpMessageHandlers(lc: LogContext, replicator: Replicator, parent: Worker): void;
20
23
  export declare function handleSubscriptionsFrom(lc: LogContext, subscriber: Worker, notifier: ReplicaStateNotifier): void;
21
24
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"replicator.d.ts","sourceRoot":"","sources":["../../../../../zero-cache/src/workers/replicator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,CAAC,MAAM,+BAA+B,CAAC;AACnD,OAAO,EAAC,QAAQ,EAAC,MAAM,2BAA2B,CAAC;AACnD,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,0BAA0B,CAAC;AAG7D,OAAO,EAAC,QAAQ,EAAC,MAAM,oCAAoC,CAA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D,OAAO,EAAC,QAAQ,EAAC,MAAM,oCAAoC,CAAC;AAC5D,OAAO,KAAK,EAEV,oBAAoB,EACpB,UAAU,EACX,MAAM,sCAAsC,CAAC;AAK9C,OAAO,EAEL,KAAK,YAAY,EAClB,MAAM,+CAA+C,CAAC;AACvD,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,uBAAuB,CAAC;AAElD,eAAO,MAAM,qBAAqB,+CAIjC,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEpE,MAAM,MAAM,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;AAErC,wBAAgB,eAAe,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,eAAe,UAEzE;AA0FD;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,eAAe,GAAG,YAAY,CAMnE;AAED,wBAAgB,YAAY,CAC1B,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,eAAe,EACrB,cAAc,EAAE,cAAc;UAhGd,MAAM;aAAW,OAAO;GA+HzC;AAED,wBAAgB,oBAAoB,CAClC,EAAE,EAAE,UAAU,EACd,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,MAAM,QAGf;AAID,wBAAgB,uBAAuB,CACrC,EAAE,EAAE,UAAU,EACd,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,oBAAoB,QA8B/B;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,QAAQ,CAM5E;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,QAE1D"}
@@ -13,7 +13,7 @@ function replicaFileName(replicaFile, mode) {
13
13
  }
14
14
  var MILLIS_PER_HOUR = 1e3 * 60 * 60;
15
15
  var MB = 1024 * 1024;
16
- async function connect(lc, { file, vacuumIntervalHours }, walMode, mode) {
16
+ async function prepare(lc, { file, vacuumIntervalHours }, walMode, mode) {
17
17
  const replica = new Database(lc, file);
18
18
  await upgradeReplica(lc, `${mode}-replica`, file);
19
19
  await setJournalMode(lc, replica, "delete");
@@ -42,7 +42,11 @@ async function connect(lc, { file, vacuumIntervalHours }, walMode, mode) {
42
42
  applyPragmas(replica, getPragmaConfig(mode));
43
43
  replica.pragma("optimize = 0x10002");
44
44
  lc.info?.(`optimized ${file}`);
45
- return replica;
45
+ replica.close();
46
+ return {
47
+ file,
48
+ walMode
49
+ };
46
50
  }
47
51
  async function setJournalMode(lc, replica, mode) {
48
52
  lc.info?.(`setting ${replica.name} to ${mode} mode`);
@@ -71,10 +75,10 @@ function getPragmaConfig(mode) {
71
75
  walAutocheckpoint: mode === "backup" ? 0 : void 0
72
76
  };
73
77
  }
74
- async function setupReplica(lc, mode, replicaOptions) {
78
+ function setupReplica(lc, mode, replicaOptions) {
75
79
  lc.info?.(`setting up ${mode} replica`);
76
80
  switch (mode) {
77
- case "backup": return await connect(lc, replicaOptions, "wal", mode);
81
+ case "backup": return prepare(lc, replicaOptions, "wal", mode);
78
82
  case "serving-copy": {
79
83
  const { file } = replicaOptions;
80
84
  const copyLocation = replicaFileName(file, mode);
@@ -85,12 +89,12 @@ async function setupReplica(lc, mode, replicaOptions) {
85
89
  replica.prepare(`VACUUM INTO ?`).run(copyLocation);
86
90
  replica.close();
87
91
  lc.info?.(`finished copy (${Date.now() - start} ms)`);
88
- return connect(lc, {
92
+ return prepare(lc, {
89
93
  ...replicaOptions,
90
94
  file: copyLocation
91
95
  }, "wal2", mode);
92
96
  }
93
- case "serving": return connect(lc, replicaOptions, "wal2", mode);
97
+ case "serving": return prepare(lc, replicaOptions, "wal2", mode);
94
98
  default: throw new Error(`Invalid ReplicaMode ${mode}`);
95
99
  }
96
100
  }
@@ -1 +1 @@
1
- {"version":3,"file":"replicator.js","names":[],"sources":["../../../../../zero-cache/src/workers/replicator.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {sleep} from '../../../shared/src/sleep.ts';\nimport * as v from '../../../shared/src/valita.ts';\nimport {Database} from '../../../zqlite/src/db.ts';\nimport type {ReplicaOptions} from '../config/zero-config.ts';\nimport {deleteLiteDB} from '../db/delete-lite-db.ts';\nimport {upgradeReplica} from '../services/change-source/common/replica-schema.ts';\nimport {Notifier} from '../services/replicator/notifier.ts';\nimport type {\n ReplicaState,\n ReplicaStateNotifier,\n Replicator,\n} from '../services/replicator/replicator.ts';\nimport {\n getAscendingEvents,\n recordEvent,\n} from '../services/replicator/schema/replication-state.ts';\nimport {\n applyPragmas,\n type PragmaConfig,\n} from '../services/replicator/write-worker-client.ts';\nimport type {Worker} from '../types/processes.ts';\n\nexport const replicaFileModeSchema = v.literalUnion(\n 'serving',\n 'serving-copy',\n 'backup',\n);\n\nexport type ReplicaFileMode = v.Infer<typeof replicaFileModeSchema>;\n\nexport function replicaFileName(replicaFile: string, mode: ReplicaFileMode) {\n return mode === 'serving-copy' ? `${replicaFile}-serving-copy` : replicaFile;\n}\n\nconst MILLIS_PER_HOUR = 1000 * 60 * 60;\nconst MB = 1024 * 1024;\n\nasync function connect(\n lc: LogContext,\n {file, vacuumIntervalHours}: ReplicaOptions,\n walMode: 'wal' | 'wal2',\n mode: ReplicaFileMode,\n): Promise<Database> {\n const replica = new Database(lc, file);\n\n // Perform any upgrades to the replica in case the backup is an\n // earlier version.\n await upgradeReplica(lc, `${mode}-replica`, file);\n\n // Start by folding any (e.g. restored) WAL(2) files into the main db.\n await setJournalMode(lc, replica, 'delete');\n\n const [{page_size: pageSize}] = replica.pragma<{page_size: number}>(\n 'page_size',\n );\n const [{page_count: pageCount}] = replica.pragma<{page_count: number}>(\n 'page_count',\n );\n const [{freelist_count: freelistCount}] = replica.pragma<{\n freelist_count: number;\n }>('freelist_count');\n\n const dbSize = ((pageCount * pageSize) / MB).toFixed(2);\n const freelistSize = ((freelistCount * pageSize) / MB).toFixed(2);\n\n // TODO: Consider adding a freelist size or ratio based vacuum trigger.\n lc.info?.(`Size of db ${file}: ${dbSize} MB (${freelistSize} MB freeable)`);\n\n // Check for the VACUUM threshold.\n const events = getAscendingEvents(replica);\n lc.debug?.(`Runtime events for db ${file}`, {events});\n if (vacuumIntervalHours !== undefined) {\n const millisSinceLastEvent =\n Date.now() - (events.at(-1)?.timestamp.getTime() ?? 0);\n if (millisSinceLastEvent / MILLIS_PER_HOUR > vacuumIntervalHours) {\n lc.info?.(`Performing maintenance cleanup on ${file}`);\n const t0 = performance.now();\n replica.unsafeMode(true);\n replica.pragma('journal_mode = OFF');\n replica.exec('VACUUM');\n recordEvent(replica, 'vacuum');\n replica.unsafeMode(false);\n const t1 = performance.now();\n lc.info?.(`VACUUM completed (${t1 - t0} ms)`);\n }\n }\n\n await setJournalMode(lc, replica, walMode);\n\n const pragmas = getPragmaConfig(mode);\n applyPragmas(replica, pragmas);\n\n replica.pragma('optimize = 0x10002');\n lc.info?.(`optimized ${file}`);\n return replica;\n}\n\n// Setting the journal_mode requires an exclusive lock on the replica.\n// Add resilience against random replica reads (for stats, etc.) by\n// retrying if the database is locked. Note that the busy_timeout doesn't\n// work here.\nasync function setJournalMode(\n lc: LogContext,\n replica: Database,\n mode: 'delete' | 'wal' | 'wal2',\n) {\n lc.info?.(`setting ${replica.name} to ${mode} mode`);\n let err: unknown;\n for (let i = 0; i < 5; i++) {\n try {\n replica.pragma(`journal_mode = ${mode}`);\n return;\n } catch (e) {\n lc.warn?.(`error setting journal_mode to ${mode} (attempt ${i + 1})`, e);\n err = e;\n }\n await sleep(500);\n }\n throw err;\n}\n\n/**\n * Returns the PragmaConfig for a given replica file mode.\n * This is used by both the main thread (setupReplica) and\n * the write worker thread to apply the same pragma settings.\n */\nexport function getPragmaConfig(mode: ReplicaFileMode): PragmaConfig {\n return {\n busyTimeout: 30000,\n analysisLimit: 1000,\n walAutocheckpoint: mode === 'backup' ? 0 : undefined,\n };\n}\n\nexport async function setupReplica(\n lc: LogContext,\n mode: ReplicaFileMode,\n replicaOptions: ReplicaOptions,\n): Promise<Database> {\n lc.info?.(`setting up ${mode} replica`);\n\n switch (mode) {\n case 'backup':\n return await connect(lc, replicaOptions, 'wal', mode);\n\n case 'serving-copy': {\n // In 'serving-copy' mode, the original file is being used for 'backup'\n // mode, so we make a copy for servicing sync requests.\n const {file} = replicaOptions;\n const copyLocation = replicaFileName(file, mode);\n deleteLiteDB(copyLocation);\n\n const start = Date.now();\n lc.info?.(`copying ${file} to ${copyLocation}`);\n const replica = new Database(lc, file);\n replica.prepare(`VACUUM INTO ?`).run(copyLocation);\n replica.close();\n lc.info?.(`finished copy (${Date.now() - start} ms)`);\n\n return connect(lc, {...replicaOptions, file: copyLocation}, 'wal2', mode);\n }\n\n case 'serving':\n return connect(lc, replicaOptions, 'wal2', mode);\n\n default:\n throw new Error(`Invalid ReplicaMode ${mode}`);\n }\n}\n\nexport function setUpMessageHandlers(\n lc: LogContext,\n replicator: Replicator,\n parent: Worker,\n) {\n handleSubscriptionsFrom(lc, parent, replicator);\n}\n\ntype Notification = ['notify', ReplicaState];\n\nexport function handleSubscriptionsFrom(\n lc: LogContext,\n subscriber: Worker,\n notifier: ReplicaStateNotifier,\n) {\n subscriber.onMessageType('subscribe', async () => {\n const subscription = notifier.subscribe();\n\n subscriber.on('close', () => {\n lc.debug?.(`closing replication subscription from ${subscriber.pid}`);\n subscription.cancel();\n });\n\n for await (const msg of subscription) {\n try {\n subscriber.send<Notification>(['notify', msg]);\n } catch (e) {\n const log =\n e instanceof Error &&\n 'code' in e &&\n // This can happen in a race condition if the subscribing process\n // is closed before the 'close' message is processed.\n e.code === 'ERR_IPC_CHANNEL_CLOSED'\n ? 'warn'\n : 'error';\n\n lc[log]?.(\n `error sending replicator notification to ${subscriber.pid}: ${String(e)}`,\n e,\n );\n }\n }\n });\n}\n\n/**\n * Creates a Notifier to relay notifications the notifier of another Worker.\n * This does not send the initial subscription message. Use {@link subscribeTo}\n * to initiate the subscription.\n */\nexport function createNotifierFrom(_lc: LogContext, source: Worker): Notifier {\n const notifier = new Notifier();\n source.onMessageType<Notification>('notify', msg =>\n notifier.notifySubscribers(msg),\n );\n return notifier;\n}\n\nexport function subscribeTo(_lc: LogContext, source: Worker) {\n source.send(['subscribe', {}]);\n}\n"],"mappings":";;;;;;;;;AAuBA,IAAa,wBAAwB,aACnC,WACA,gBACA,SACD;AAID,SAAgB,gBAAgB,aAAqB,MAAuB;AAC1E,QAAO,SAAS,iBAAiB,GAAG,YAAY,iBAAiB;;AAGnE,IAAM,kBAAkB,MAAO,KAAK;AACpC,IAAM,KAAK,OAAO;AAElB,eAAe,QACb,IACA,EAAC,MAAM,uBACP,SACA,MACmB;CACnB,MAAM,UAAU,IAAI,SAAS,IAAI,KAAK;AAItC,OAAM,eAAe,IAAI,GAAG,KAAK,WAAW,KAAK;AAGjD,OAAM,eAAe,IAAI,SAAS,SAAS;CAE3C,MAAM,CAAC,EAAC,WAAW,cAAa,QAAQ,OACtC,YACD;CACD,MAAM,CAAC,EAAC,YAAY,eAAc,QAAQ,OACxC,aACD;CACD,MAAM,CAAC,EAAC,gBAAgB,mBAAkB,QAAQ,OAE/C,iBAAiB;CAEpB,MAAM,UAAW,YAAY,WAAY,IAAI,QAAQ,EAAE;CACvD,MAAM,gBAAiB,gBAAgB,WAAY,IAAI,QAAQ,EAAE;AAGjE,IAAG,OAAO,cAAc,KAAK,IAAI,OAAO,OAAO,aAAa,eAAe;CAG3E,MAAM,SAAS,mBAAmB,QAAQ;AAC1C,IAAG,QAAQ,yBAAyB,QAAQ,EAAC,QAAO,CAAC;AACrD,KAAI,wBAAwB,KAAA;OAExB,KAAK,KAAK,IAAI,OAAO,GAAG,GAAG,EAAE,UAAU,SAAS,IAAI,MAC3B,kBAAkB,qBAAqB;AAChE,MAAG,OAAO,qCAAqC,OAAO;GACtD,MAAM,KAAK,YAAY,KAAK;AAC5B,WAAQ,WAAW,KAAK;AACxB,WAAQ,OAAO,qBAAqB;AACpC,WAAQ,KAAK,SAAS;AACtB,eAAY,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AG;IAAgB,MAAM;IAAa,EAAE,QAAQ,KAAK;;EAG3E,KAAK,UACH,QAAO,QAAQ,IAAI,gBAAgB,QAAQ,KAAK;EAElD,QACE,OAAM,IAAI,MAAM,uBAAuB,OAAO;;;AAIpD,SAAgB,qBACd,IACA,YACA,QACA;AACA,yBAAwB,IAAI,QAAQ,WAAW;;AAKjD,SAAgB,wBACd,IACA,YACA,UACA;AACA,YAAW,cAAc,aAAa,YAAY;EAChD,MAAM,eAAe,SAAS,WAAW;AAEzC,aAAW,GAAG,eAAe;AAC3B,MAAG,QAAQ,yCAAyC,WAAW,MAAM;AACrE,gBAAa,QAAQ;IACrB;AAEF,aAAW,MAAM,OAAO,aACtB,KAAI;AACF,cAAW,KAAmB,CAAC,UAAU,IAAI,CAAC;WACvC,GAAG;AAUV,MARE,aAAa,SACb,UAAU,KAGV,EAAE,SAAS,2BACP,SACA,WAGJ,4CAA4C,WAAW,IAAI,IAAI,OAAO,EAAE,IACxE,EACD;;GAGL;;;;;;;AAQJ,SAAgB,mBAAmB,KAAiB,QAA0B;CAC5E,MAAM,WAAW,IAAI,UAAU;AAC/B,QAAO,cAA4B,WAAU,QAC3C,SAAS,kBAAkB,IAAI,CAChC;AACD,QAAO;;AAGT,SAAgB,YAAY,KAAiB,QAAgB;AAC3D,QAAO,KAAK,CAAC,aAAa,EAAE,CAAC,CAAC"}
1
+ {"version":3,"file":"replicator.js","names":[],"sources":["../../../../../zero-cache/src/workers/replicator.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {sleep} from '../../../shared/src/sleep.ts';\nimport * as v from '../../../shared/src/valita.ts';\nimport {Database} from '../../../zqlite/src/db.ts';\nimport type {ReplicaOptions} from '../config/zero-config.ts';\nimport {deleteLiteDB} from '../db/delete-lite-db.ts';\nimport {upgradeReplica} from '../services/change-source/common/replica-schema.ts';\nimport {Notifier} from '../services/replicator/notifier.ts';\nimport type {\n ReplicaState,\n ReplicaStateNotifier,\n Replicator,\n} from '../services/replicator/replicator.ts';\nimport {\n getAscendingEvents,\n recordEvent,\n} from '../services/replicator/schema/replication-state.ts';\nimport {\n applyPragmas,\n type PragmaConfig,\n} from '../services/replicator/write-worker-client.ts';\nimport type {Worker} from '../types/processes.ts';\n\nexport const replicaFileModeSchema = v.literalUnion(\n 'serving',\n 'serving-copy',\n 'backup',\n);\n\nexport type ReplicaFileMode = v.Infer<typeof replicaFileModeSchema>;\n\nexport type WalMode = 'wal' | 'wal2';\n\nexport function replicaFileName(replicaFile: string, mode: ReplicaFileMode) {\n return mode === 'serving-copy' ? `${replicaFile}-serving-copy` : replicaFile;\n}\n\nconst MILLIS_PER_HOUR = 1000 * 60 * 60;\nconst MB = 1024 * 1024;\n\nasync function prepare(\n lc: LogContext,\n {file, vacuumIntervalHours}: ReplicaOptions,\n walMode: WalMode,\n mode: ReplicaFileMode,\n): Promise<{file: string; walMode: WalMode}> {\n const replica = new Database(lc, file);\n\n // Perform any upgrades to the replica in case the backup is an\n // earlier version.\n await upgradeReplica(lc, `${mode}-replica`, file);\n\n // Start by folding any (e.g. restored) WAL(2) files into the main db.\n await setJournalMode(lc, replica, 'delete');\n\n const [{page_size: pageSize}] = replica.pragma<{page_size: number}>(\n 'page_size',\n );\n const [{page_count: pageCount}] = replica.pragma<{page_count: number}>(\n 'page_count',\n );\n const [{freelist_count: freelistCount}] = replica.pragma<{\n freelist_count: number;\n }>('freelist_count');\n\n const dbSize = ((pageCount * pageSize) / MB).toFixed(2);\n const freelistSize = ((freelistCount * pageSize) / MB).toFixed(2);\n\n // TODO: Consider adding a freelist size or ratio based vacuum trigger.\n lc.info?.(`Size of db ${file}: ${dbSize} MB (${freelistSize} MB freeable)`);\n\n // Check for the VACUUM threshold.\n const events = getAscendingEvents(replica);\n lc.debug?.(`Runtime events for db ${file}`, {events});\n if (vacuumIntervalHours !== undefined) {\n const millisSinceLastEvent =\n Date.now() - (events.at(-1)?.timestamp.getTime() ?? 0);\n if (millisSinceLastEvent / MILLIS_PER_HOUR > vacuumIntervalHours) {\n lc.info?.(`Performing maintenance cleanup on ${file}`);\n const t0 = performance.now();\n replica.unsafeMode(true);\n replica.pragma('journal_mode = OFF');\n replica.exec('VACUUM');\n recordEvent(replica, 'vacuum');\n replica.unsafeMode(false);\n const t1 = performance.now();\n lc.info?.(`VACUUM completed (${t1 - t0} ms)`);\n }\n }\n\n await setJournalMode(lc, replica, walMode);\n\n const pragmas = getPragmaConfig(mode);\n applyPragmas(replica, pragmas);\n\n replica.pragma('optimize = 0x10002');\n lc.info?.(`optimized ${file}`);\n replica.close();\n return {file, walMode};\n}\n\n// Setting the journal_mode requires an exclusive lock on the replica.\n// Add resilience against random replica reads (for stats, etc.) by\n// retrying if the database is locked. Note that the busy_timeout doesn't\n// work here.\nasync function setJournalMode(\n lc: LogContext,\n replica: Database,\n mode: 'delete' | 'wal' | 'wal2',\n) {\n lc.info?.(`setting ${replica.name} to ${mode} mode`);\n let err: unknown;\n for (let i = 0; i < 5; i++) {\n try {\n replica.pragma(`journal_mode = ${mode}`);\n return;\n } catch (e) {\n lc.warn?.(`error setting journal_mode to ${mode} (attempt ${i + 1})`, e);\n err = e;\n }\n await sleep(500);\n }\n throw err;\n}\n\n/**\n * Returns the PragmaConfig for a given replica file mode.\n * This is used by both the main thread (setupReplica) and\n * the write worker thread to apply the same pragma settings.\n */\nexport function getPragmaConfig(mode: ReplicaFileMode): PragmaConfig {\n return {\n busyTimeout: 30000,\n analysisLimit: 1000,\n walAutocheckpoint: mode === 'backup' ? 0 : undefined,\n };\n}\n\nexport function setupReplica(\n lc: LogContext,\n mode: ReplicaFileMode,\n replicaOptions: ReplicaOptions,\n) {\n lc.info?.(`setting up ${mode} replica`);\n\n switch (mode) {\n case 'backup':\n return prepare(lc, replicaOptions, 'wal', mode);\n\n case 'serving-copy': {\n // In 'serving-copy' mode, the original file is being used for 'backup'\n // mode, so we make a copy for servicing sync requests.\n const {file} = replicaOptions;\n const copyLocation = replicaFileName(file, mode);\n deleteLiteDB(copyLocation);\n\n const start = Date.now();\n lc.info?.(`copying ${file} to ${copyLocation}`);\n const replica = new Database(lc, file);\n replica.prepare(`VACUUM INTO ?`).run(copyLocation);\n replica.close();\n lc.info?.(`finished copy (${Date.now() - start} ms)`);\n\n return prepare(lc, {...replicaOptions, file: copyLocation}, 'wal2', mode);\n }\n\n case 'serving':\n return prepare(lc, replicaOptions, 'wal2', mode);\n\n default:\n throw new Error(`Invalid ReplicaMode ${mode}`);\n }\n}\n\nexport function setUpMessageHandlers(\n lc: LogContext,\n replicator: Replicator,\n parent: Worker,\n) {\n handleSubscriptionsFrom(lc, parent, replicator);\n}\n\ntype Notification = ['notify', ReplicaState];\n\nexport function handleSubscriptionsFrom(\n lc: LogContext,\n subscriber: Worker,\n notifier: ReplicaStateNotifier,\n) {\n subscriber.onMessageType('subscribe', async () => {\n const subscription = notifier.subscribe();\n\n subscriber.on('close', () => {\n lc.debug?.(`closing replication subscription from ${subscriber.pid}`);\n subscription.cancel();\n });\n\n for await (const msg of subscription) {\n try {\n subscriber.send<Notification>(['notify', msg]);\n } catch (e) {\n const log =\n e instanceof Error &&\n 'code' in e &&\n // This can happen in a race condition if the subscribing process\n // is closed before the 'close' message is processed.\n e.code === 'ERR_IPC_CHANNEL_CLOSED'\n ? 'warn'\n : 'error';\n\n lc[log]?.(\n `error sending replicator notification to ${subscriber.pid}: ${String(e)}`,\n e,\n );\n }\n }\n });\n}\n\n/**\n * Creates a Notifier to relay notifications the notifier of another Worker.\n * This does not send the initial subscription message. Use {@link subscribeTo}\n * to initiate the subscription.\n */\nexport function createNotifierFrom(_lc: LogContext, source: Worker): Notifier {\n const notifier = new Notifier();\n source.onMessageType<Notification>('notify', msg =>\n notifier.notifySubscribers(msg),\n );\n return notifier;\n}\n\nexport function subscribeTo(_lc: LogContext, source: Worker) {\n source.send(['subscribe', {}]);\n}\n"],"mappings":";;;;;;;;;AAuBA,IAAa,wBAAwB,aACnC,WACA,gBACA,SACD;AAMD,SAAgB,gBAAgB,aAAqB,MAAuB;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KjD,SAAgB,wBACd,IACA,YACA,UACA;AACA,YAAW,cAAc,aAAa,YAAY;EAChD,MAAM,eAAe,SAAS,WAAW;AAEzC,aAAW,GAAG,eAAe;AAC3B,MAAG,QAAQ,yCAAyC,WAAW,MAAM;AACrE,gBAAa,QAAQ;IACrB;AAEF,aAAW,MAAM,OAAO,aACtB,KAAI;AACF,cAAW,KAAmB,CAAC,UAAU,IAAI,CAAC;WACvC,GAAG;AAUV,MARE,aAAa,SACb,UAAU,KAGV,EAAE,SAAS,2BACP,SACA,WAGJ,4CAA4C,WAAW,IAAI,IAAI,OAAO,EAAE,IACxE,EACD;;GAGL;;;;;;;AAQJ,SAAgB,mBAAmB,KAAiB,QAA0B;CAC5E,MAAM,WAAW,IAAI,UAAU;AAC/B,QAAO,cAA4B,WAAU,QAC3C,SAAS,kBAAkB,IAAI,CAChC;AACD,QAAO;;AAGT,SAAgB,YAAY,KAAiB,QAAgB;AAC3D,QAAO,KAAK,CAAC,aAAa,EAAE,CAAC,CAAC"}
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * The current version of Zero.
4
4
  */
5
- var version = "1.2.0-canary.0";
5
+ var version = "1.2.0-canary.10";
6
6
  //#endregion
7
7
  export { version };
8
8
 
@@ -1 +1 @@
1
- {"version":3,"file":"zero-poke-handler.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/zero-poke-handler.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EACV,sBAAsB,EACtB,YAAY,EACb,MAAM,iCAAiC,CAAC;AAEzC,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,qCAAqC,CAAC;AAGlE,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,+CAA+C,CAAC;AACjF,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EACZ,aAAa,EACd,MAAM,oCAAoC,CAAC;AAG5C,OAAO,EAEL,KAAK,UAAU,EAChB,MAAM,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D,wBAAgB,kCAAkC,CAChD,EAAE,EAAE,aAAa,GAChB,sBAAsB,CAcxB"}
@@ -142,7 +142,10 @@ function mergePokes(pokeBuffer, schema, serverToClient) {
142
142
  if (pokePart.lastMutationIDChanges) for (const [clientID, lastMutationID] of Object.entries(pokePart.lastMutationIDChanges)) mergedLastMutationIDChanges[clientID] = lastMutationID;
143
143
  if (pokePart.desiredQueriesPatches) for (const [clientID, queriesPatch] of Object.entries(pokePart.desiredQueriesPatches)) for (const op of queriesPatch) mergedPatch.push(queryPatchOpToReplicachePatchOp(op, (hash) => toDesiredQueriesKey(clientID, hash)));
144
144
  if (pokePart.gotQueriesPatch) for (const op of pokePart.gotQueriesPatch) mergedPatch.push(queryPatchOpToReplicachePatchOp(op, toGotQueriesKey));
145
- if (pokePart.rowsPatch) for (const p of pokePart.rowsPatch) mergedPatch.push(rowsPatchOpToReplicachePatchOp(p, schema, serverToClient));
145
+ if (pokePart.rowsPatch) for (const p of pokePart.rowsPatch) {
146
+ const patchOp = rowsPatchOpToReplicachePatchOp(p, schema, serverToClient);
147
+ if (patchOp) mergedPatch.push(patchOp);
148
+ }
146
149
  if (pokePart.mutationsPatch) for (const op of pokePart.mutationsPatch) mergedPatch.push(mutationPatchOpToReplicachePatchOp(op));
147
150
  }
148
151
  }
@@ -187,7 +190,8 @@ function mutationPatchOpToReplicachePatchOp(op) {
187
190
  }
188
191
  function rowsPatchOpToReplicachePatchOp(op, schema, serverToClient) {
189
192
  if (op.op === "clear") return op;
190
- const tableName = serverToClient.tableName(op.tableName, op);
193
+ const tableName = serverToClient.tableNameIfKnown(op.tableName);
194
+ if (!tableName) return;
191
195
  switch (op.op) {
192
196
  case "del": return {
193
197
  op: "del",
@@ -1 +1 @@
1
- {"version":3,"file":"zero-poke-handler.js","names":["#replicachePoke","#onPokeError","#clientID","#lc","#pokeBuffer","#pokeLock","#schema","#serverToClient","#mutationTracker","#receivingPoke","#handlePokeError","#pokePlaybackLoopRunning","#startPlaybackLoop","#clear","#scheduledCallback","#lastScheduledTimestamp","#processPokesForFrame"],"sources":["../../../../../zero-client/src/client/zero-poke-handler.ts"],"sourcesContent":["import {Lock} from '@rocicorp/lock';\nimport type {LogContext} from '@rocicorp/logger';\nimport type {\n PatchOperationInternal,\n PokeInternal,\n} from '../../../replicache/src/impl.ts';\nimport type {PatchOperation} from '../../../replicache/src/patch-operation.ts';\nimport type {ClientID} from '../../../replicache/src/sync/ids.ts';\nimport {unreachable} from '../../../shared/src/asserts.ts';\nimport type {JSONValue} from '../../../shared/src/json.ts';\nimport type {MutationPatch} from '../../../zero-protocol/src/mutations-patch.ts';\nimport type {\n PokeEndBody,\n PokePartBody,\n PokeStartBody,\n} from '../../../zero-protocol/src/poke.ts';\nimport type {QueriesPatchOp} from '../../../zero-protocol/src/queries-patch.ts';\nimport type {RowPatchOp} from '../../../zero-protocol/src/row-patch.ts';\nimport {\n serverToClient,\n type NameMapper,\n} from '../../../zero-schema/src/name-mapper.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport {\n toDesiredQueriesKey,\n toGotQueriesKey,\n toMutationResponseKey,\n toPrimaryKeyString,\n} from './keys.ts';\nimport type {MutationTracker} from './mutation-tracker.ts';\n\ntype PokeAccumulator = {\n readonly pokeStart: PokeStartBody;\n readonly parts: PokePartBody[];\n readonly pokeEnd: PokeEndBody;\n};\n\n/**\n * Handles the multi-part format of zero pokes.\n * As an optimization it also debounces pokes, only poking Replicache with a\n * merged poke at most once per scheduled callback (using setTimeout).\n * This debouncing avoids wastefully computing separate diffs and IVM updates\n * for intermediate states. setTimeout is used instead of requestAnimationFrame\n * to ensure pokes are delivered even when the tab is in the background,\n * enabling notifications (sounds, favicon badges) to work correctly.\n */\nexport class PokeHandler {\n readonly #replicachePoke: (poke: PokeInternal) => Promise<void>;\n readonly #onPokeError: (error: unknown) => void;\n readonly #clientID: ClientID;\n readonly #lc: LogContext;\n #receivingPoke: Omit<PokeAccumulator, 'pokeEnd'> | undefined = undefined;\n readonly #pokeBuffer: PokeAccumulator[] = [];\n #pokePlaybackLoopRunning = false;\n #lastScheduledTimestamp = 0;\n // Serializes calls to this.#replicachePoke otherwise we can cause out of\n // order poke errors.\n readonly #pokeLock = new Lock();\n readonly #schema: Schema;\n readonly #serverToClient: NameMapper;\n readonly #mutationTracker: MutationTracker;\n\n constructor(\n replicachePoke: (poke: PokeInternal) => Promise<void>,\n onPokeError: (error: unknown) => void,\n clientID: ClientID,\n schema: Schema,\n lc: LogContext,\n mutationTracker: MutationTracker,\n ) {\n this.#replicachePoke = replicachePoke;\n this.#onPokeError = onPokeError;\n this.#clientID = clientID;\n this.#schema = schema;\n this.#serverToClient = serverToClient(schema.tables);\n this.#lc = lc.withContext('PokeHandler');\n this.#mutationTracker = mutationTracker;\n }\n\n handlePokeStart(pokeStart: PokeStartBody) {\n if (this.#receivingPoke) {\n this.#handlePokeError(\n `pokeStart ${JSON.stringify(\n pokeStart,\n )} while still receiving ${JSON.stringify(\n this.#receivingPoke.pokeStart,\n )} `,\n );\n return;\n }\n this.#receivingPoke = {\n pokeStart,\n parts: [],\n };\n }\n\n handlePokePart(pokePart: PokePartBody): number | undefined {\n if (pokePart.pokeID !== this.#receivingPoke?.pokeStart.pokeID) {\n this.#handlePokeError(\n `pokePart for ${pokePart.pokeID}, when receiving ${\n this.#receivingPoke?.pokeStart.pokeID\n }`,\n );\n return;\n }\n this.#receivingPoke.parts.push(pokePart);\n return pokePart.lastMutationIDChanges?.[this.#clientID];\n }\n\n handlePokeEnd(pokeEnd: PokeEndBody): void {\n if (pokeEnd.pokeID !== this.#receivingPoke?.pokeStart.pokeID) {\n this.#handlePokeError(\n `pokeEnd for ${pokeEnd.pokeID}, when receiving ${\n this.#receivingPoke?.pokeStart.pokeID\n }`,\n );\n return;\n }\n if (pokeEnd.cancel) {\n this.#receivingPoke = undefined;\n return;\n }\n this.#pokeBuffer.push({...this.#receivingPoke, pokeEnd});\n this.#receivingPoke = undefined;\n if (!this.#pokePlaybackLoopRunning) {\n this.#startPlaybackLoop();\n }\n }\n\n handleDisconnect(): void {\n this.#lc.debug?.('clearing due to disconnect');\n this.#clear();\n }\n\n #startPlaybackLoop() {\n this.#lc.debug?.('starting playback loop');\n this.#pokePlaybackLoopRunning = true;\n setTimeout(this.#scheduledCallback, 0);\n }\n\n #scheduledCallback = async () => {\n const lc = this.#lc.withContext(\n 'scheduledAt',\n Math.floor(performance.now()),\n );\n if (this.#pokeBuffer.length === 0) {\n lc.debug?.('stopping playback loop');\n this.#pokePlaybackLoopRunning = false;\n return;\n }\n setTimeout(this.#scheduledCallback, 0);\n const start = performance.now();\n lc.debug?.(\n 'scheduled callback fired, processing pokes. Since last callback',\n start - this.#lastScheduledTimestamp,\n );\n this.#lastScheduledTimestamp = start;\n await this.#processPokesForFrame(lc);\n lc.debug?.('processing pokes took', performance.now() - start);\n };\n\n #processPokesForFrame(lc: LogContext): Promise<void> {\n return this.#pokeLock.withLock(async () => {\n const now = Date.now();\n lc.debug?.('got poke lock at', now);\n lc.debug?.('merging', this.#pokeBuffer.length);\n try {\n const merged = mergePokes(\n this.#pokeBuffer,\n this.#schema,\n this.#serverToClient,\n );\n this.#pokeBuffer.length = 0;\n if (merged === undefined) {\n lc.debug?.('frame is empty');\n return;\n }\n const start = performance.now();\n lc.debug?.('poking replicache');\n await this.#replicachePoke(merged);\n lc.debug?.('poking replicache took', performance.now() - start);\n\n if (!('error' in merged.pullResponse)) {\n const lmid =\n merged.pullResponse.lastMutationIDChanges[this.#clientID];\n if (lmid !== undefined) {\n this.#mutationTracker.lmidAdvanced(lmid);\n }\n }\n } catch (e) {\n this.#handlePokeError(e);\n }\n });\n }\n\n #handlePokeError(e: unknown) {\n if (String(e).includes('unexpected base cookie for poke')) {\n // This can happen if cookie changes due to refresh from idb due\n // to an update arriving to different tabs in the same\n // client group at very different times. Unusual but possible.\n this.#lc.debug?.('clearing due to', e);\n } else {\n this.#lc.error?.('clearing due to unexpected poke error', e);\n }\n this.#clear();\n this.#onPokeError(e);\n }\n\n #clear() {\n this.#receivingPoke = undefined;\n this.#pokeBuffer.length = 0;\n }\n}\n\nexport function mergePokes(\n pokeBuffer: PokeAccumulator[],\n schema: Schema,\n serverToClient: NameMapper,\n):\n | (PokeInternal & {mutationResults?: MutationPatch[] | undefined})\n | undefined {\n if (pokeBuffer.length === 0) {\n return undefined;\n }\n const {baseCookie} = pokeBuffer[0].pokeStart;\n const lastPoke = pokeBuffer[pokeBuffer.length - 1];\n const {cookie} = lastPoke.pokeEnd;\n const mergedPatch: PatchOperationInternal[] = [];\n const mergedLastMutationIDChanges: Record<string, number> = {};\n const mutationResults: MutationPatch[] = [];\n\n let prevPokeEnd = undefined;\n for (const pokeAccumulator of pokeBuffer) {\n if (\n prevPokeEnd &&\n pokeAccumulator.pokeStart.baseCookie &&\n pokeAccumulator.pokeStart.baseCookie > prevPokeEnd.cookie\n ) {\n throw Error(\n `unexpected cookie gap ${JSON.stringify(prevPokeEnd)} ${JSON.stringify(\n pokeAccumulator.pokeStart,\n )}`,\n );\n }\n prevPokeEnd = pokeAccumulator.pokeEnd;\n for (const pokePart of pokeAccumulator.parts) {\n if (pokePart.lastMutationIDChanges) {\n for (const [clientID, lastMutationID] of Object.entries(\n pokePart.lastMutationIDChanges,\n )) {\n mergedLastMutationIDChanges[clientID] = lastMutationID;\n }\n }\n if (pokePart.desiredQueriesPatches) {\n for (const [clientID, queriesPatch] of Object.entries(\n pokePart.desiredQueriesPatches,\n )) {\n for (const op of queriesPatch) {\n mergedPatch.push(\n queryPatchOpToReplicachePatchOp(op, hash =>\n toDesiredQueriesKey(clientID, hash),\n ),\n );\n }\n }\n }\n if (pokePart.gotQueriesPatch) {\n for (const op of pokePart.gotQueriesPatch) {\n mergedPatch.push(\n queryPatchOpToReplicachePatchOp(op, toGotQueriesKey),\n );\n }\n }\n if (pokePart.rowsPatch) {\n for (const p of pokePart.rowsPatch) {\n mergedPatch.push(\n rowsPatchOpToReplicachePatchOp(p, schema, serverToClient),\n );\n }\n }\n if (pokePart.mutationsPatch) {\n for (const op of pokePart.mutationsPatch) {\n mergedPatch.push(mutationPatchOpToReplicachePatchOp(op));\n }\n }\n }\n }\n const ret: PokeInternal & {mutationResults?: MutationPatch[] | undefined} = {\n baseCookie,\n pullResponse: {\n lastMutationIDChanges: mergedLastMutationIDChanges,\n patch: mergedPatch,\n cookie,\n },\n };\n\n // For backwards compatibility. Because we're strict on our validation,\n // zero-client must be able to parse pokes with this field before we introduce it.\n // So users can update their clients and then start using custom mutators that write responses to the db.\n if (mutationResults.length > 0) {\n ret.mutationResults = mutationResults;\n }\n return ret;\n}\n\nfunction queryPatchOpToReplicachePatchOp(\n op: QueriesPatchOp,\n toKey: (hash: string) => string,\n): PatchOperation {\n switch (op.op) {\n case 'clear':\n return op;\n case 'del':\n return {\n op: 'del',\n key: toKey(op.hash),\n };\n case 'put':\n return {\n op: 'put',\n key: toKey(op.hash),\n value: null,\n };\n default:\n unreachable(op);\n }\n}\n\nexport function mutationPatchOpToReplicachePatchOp(\n op: MutationPatch,\n): PatchOperationInternal {\n switch (op.op) {\n case 'put':\n return {\n op: 'put',\n key: toMutationResponseKey(op.mutation.id),\n value: op.mutation.result,\n };\n case 'del':\n return {\n op: 'del',\n key: toMutationResponseKey(op.id),\n };\n }\n}\n\nfunction rowsPatchOpToReplicachePatchOp(\n op: RowPatchOp,\n schema: Schema,\n serverToClient: NameMapper,\n): PatchOperationInternal {\n if (op.op === 'clear') {\n return op;\n }\n const tableName = serverToClient.tableName(op.tableName, op as JSONValue);\n switch (op.op) {\n case 'del':\n return {\n op: 'del',\n key: toPrimaryKeyString(\n tableName,\n schema.tables[tableName].primaryKey,\n serverToClient.row(op.tableName, op.id),\n ),\n };\n case 'put':\n return {\n op: 'put',\n key: toPrimaryKeyString(\n tableName,\n schema.tables[tableName].primaryKey,\n serverToClient.row(op.tableName, op.value),\n ),\n value: serverToClient.row(op.tableName, op.value),\n };\n case 'update':\n return {\n op: 'update',\n key: toPrimaryKeyString(\n tableName,\n schema.tables[tableName].primaryKey,\n serverToClient.row(op.tableName, op.id),\n ),\n merge: op.merge\n ? serverToClient.row(op.tableName, op.merge)\n : undefined,\n constrain: serverToClient.columns(op.tableName, op.constrain),\n };\n default:\n unreachable(op);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AA8CA,IAAa,cAAb,MAAyB;CACvB;CACA;CACA;CACA;CACA,iBAA+D,KAAA;CAC/D,cAA0C,EAAE;CAC5C,2BAA2B;CAC3B,0BAA0B;CAG1B,YAAqB,IAAI,MAAM;CAC/B;CACA;CACA;CAEA,YACE,gBACA,aACA,UACA,QACA,IACA,iBACA;AACA,QAAA,iBAAuB;AACvB,QAAA,cAAoB;AACpB,QAAA,WAAiB;AACjB,QAAA,SAAe;AACf,QAAA,iBAAuB,eAAe,OAAO,OAAO;AACpD,QAAA,KAAW,GAAG,YAAY,cAAc;AACxC,QAAA,kBAAwB;;CAG1B,gBAAgB,WAA0B;AACxC,MAAI,MAAA,eAAqB;AACvB,SAAA,gBACE,aAAa,KAAK,UAChB,UACD,CAAC,0BAA0B,KAAK,UAC/B,MAAA,cAAoB,UACrB,CAAC,GACH;AACD;;AAEF,QAAA,gBAAsB;GACpB;GACA,OAAO,EAAE;GACV;;CAGH,eAAe,UAA4C;AACzD,MAAI,SAAS,WAAW,MAAA,eAAqB,UAAU,QAAQ;AAC7D,SAAA,gBACE,gBAAgB,SAAS,OAAO,mBAC9B,MAAA,eAAqB,UAAU,SAElC;AACD;;AAEF,QAAA,cAAoB,MAAM,KAAK,SAAS;AACxC,SAAO,SAAS,wBAAwB,MAAA;;CAG1C,cAAc,SAA4B;AACxC,MAAI,QAAQ,WAAW,MAAA,eAAqB,UAAU,QAAQ;AAC5D,SAAA,gBACE,eAAe,QAAQ,OAAO,mBAC5B,MAAA,eAAqB,UAAU,SAElC;AACD;;AAEF,MAAI,QAAQ,QAAQ;AAClB,SAAA,gBAAsB,KAAA;AACtB;;AAEF,QAAA,WAAiB,KAAK;GAAC,GAAG,MAAA;GAAqB;GAAQ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ClD;EACH,KAAK,SACH,QAAO;GACL,IAAI;GACJ,KAAK,mBACH,WACA,OAAO,OAAO,WAAW,YACzB,eAAe,IAAI,GAAG,WAAW,GAAG,GAAG,CACxC;GACD,OAAO,GAAG,QACN,eAAe,IAAI,GAAG,WAAW,GAAG,MAAM,GAC1C,KAAA;GACJ,WAAW,eAAe,QAAQ,GAAG,WAAW,GAAG,UAAU;GAC9D;EACH,QACE,aAAY,GAAG"}
1
+ {"version":3,"file":"zero-poke-handler.js","names":["#replicachePoke","#onPokeError","#clientID","#lc","#pokeBuffer","#pokeLock","#schema","#serverToClient","#mutationTracker","#receivingPoke","#handlePokeError","#pokePlaybackLoopRunning","#startPlaybackLoop","#clear","#scheduledCallback","#lastScheduledTimestamp","#processPokesForFrame"],"sources":["../../../../../zero-client/src/client/zero-poke-handler.ts"],"sourcesContent":["import {Lock} from '@rocicorp/lock';\nimport type {LogContext} from '@rocicorp/logger';\nimport type {\n PatchOperationInternal,\n PokeInternal,\n} from '../../../replicache/src/impl.ts';\nimport type {PatchOperation} from '../../../replicache/src/patch-operation.ts';\nimport type {ClientID} from '../../../replicache/src/sync/ids.ts';\nimport {unreachable} from '../../../shared/src/asserts.ts';\nimport type {MutationPatch} from '../../../zero-protocol/src/mutations-patch.ts';\nimport type {\n PokeEndBody,\n PokePartBody,\n PokeStartBody,\n} from '../../../zero-protocol/src/poke.ts';\nimport type {QueriesPatchOp} from '../../../zero-protocol/src/queries-patch.ts';\nimport type {RowPatchOp} from '../../../zero-protocol/src/row-patch.ts';\nimport {\n serverToClient,\n type NameMapper,\n} from '../../../zero-schema/src/name-mapper.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport {\n toDesiredQueriesKey,\n toGotQueriesKey,\n toMutationResponseKey,\n toPrimaryKeyString,\n} from './keys.ts';\nimport type {MutationTracker} from './mutation-tracker.ts';\n\ntype PokeAccumulator = {\n readonly pokeStart: PokeStartBody;\n readonly parts: PokePartBody[];\n readonly pokeEnd: PokeEndBody;\n};\n\n/**\n * Handles the multi-part format of zero pokes.\n * As an optimization it also debounces pokes, only poking Replicache with a\n * merged poke at most once per scheduled callback (using setTimeout).\n * This debouncing avoids wastefully computing separate diffs and IVM updates\n * for intermediate states. setTimeout is used instead of requestAnimationFrame\n * to ensure pokes are delivered even when the tab is in the background,\n * enabling notifications (sounds, favicon badges) to work correctly.\n */\nexport class PokeHandler {\n readonly #replicachePoke: (poke: PokeInternal) => Promise<void>;\n readonly #onPokeError: (error: unknown) => void;\n readonly #clientID: ClientID;\n readonly #lc: LogContext;\n #receivingPoke: Omit<PokeAccumulator, 'pokeEnd'> | undefined = undefined;\n readonly #pokeBuffer: PokeAccumulator[] = [];\n #pokePlaybackLoopRunning = false;\n #lastScheduledTimestamp = 0;\n // Serializes calls to this.#replicachePoke otherwise we can cause out of\n // order poke errors.\n readonly #pokeLock = new Lock();\n readonly #schema: Schema;\n readonly #serverToClient: NameMapper;\n readonly #mutationTracker: MutationTracker;\n\n constructor(\n replicachePoke: (poke: PokeInternal) => Promise<void>,\n onPokeError: (error: unknown) => void,\n clientID: ClientID,\n schema: Schema,\n lc: LogContext,\n mutationTracker: MutationTracker,\n ) {\n this.#replicachePoke = replicachePoke;\n this.#onPokeError = onPokeError;\n this.#clientID = clientID;\n this.#schema = schema;\n this.#serverToClient = serverToClient(schema.tables);\n this.#lc = lc.withContext('PokeHandler');\n this.#mutationTracker = mutationTracker;\n }\n\n handlePokeStart(pokeStart: PokeStartBody) {\n if (this.#receivingPoke) {\n this.#handlePokeError(\n `pokeStart ${JSON.stringify(\n pokeStart,\n )} while still receiving ${JSON.stringify(\n this.#receivingPoke.pokeStart,\n )} `,\n );\n return;\n }\n this.#receivingPoke = {\n pokeStart,\n parts: [],\n };\n }\n\n handlePokePart(pokePart: PokePartBody): number | undefined {\n if (pokePart.pokeID !== this.#receivingPoke?.pokeStart.pokeID) {\n this.#handlePokeError(\n `pokePart for ${pokePart.pokeID}, when receiving ${\n this.#receivingPoke?.pokeStart.pokeID\n }`,\n );\n return;\n }\n this.#receivingPoke.parts.push(pokePart);\n return pokePart.lastMutationIDChanges?.[this.#clientID];\n }\n\n handlePokeEnd(pokeEnd: PokeEndBody): void {\n if (pokeEnd.pokeID !== this.#receivingPoke?.pokeStart.pokeID) {\n this.#handlePokeError(\n `pokeEnd for ${pokeEnd.pokeID}, when receiving ${\n this.#receivingPoke?.pokeStart.pokeID\n }`,\n );\n return;\n }\n if (pokeEnd.cancel) {\n this.#receivingPoke = undefined;\n return;\n }\n this.#pokeBuffer.push({...this.#receivingPoke, pokeEnd});\n this.#receivingPoke = undefined;\n if (!this.#pokePlaybackLoopRunning) {\n this.#startPlaybackLoop();\n }\n }\n\n handleDisconnect(): void {\n this.#lc.debug?.('clearing due to disconnect');\n this.#clear();\n }\n\n #startPlaybackLoop() {\n this.#lc.debug?.('starting playback loop');\n this.#pokePlaybackLoopRunning = true;\n setTimeout(this.#scheduledCallback, 0);\n }\n\n #scheduledCallback = async () => {\n const lc = this.#lc.withContext(\n 'scheduledAt',\n Math.floor(performance.now()),\n );\n if (this.#pokeBuffer.length === 0) {\n lc.debug?.('stopping playback loop');\n this.#pokePlaybackLoopRunning = false;\n return;\n }\n setTimeout(this.#scheduledCallback, 0);\n const start = performance.now();\n lc.debug?.(\n 'scheduled callback fired, processing pokes. Since last callback',\n start - this.#lastScheduledTimestamp,\n );\n this.#lastScheduledTimestamp = start;\n await this.#processPokesForFrame(lc);\n lc.debug?.('processing pokes took', performance.now() - start);\n };\n\n #processPokesForFrame(lc: LogContext): Promise<void> {\n return this.#pokeLock.withLock(async () => {\n const now = Date.now();\n lc.debug?.('got poke lock at', now);\n lc.debug?.('merging', this.#pokeBuffer.length);\n try {\n const merged = mergePokes(\n this.#pokeBuffer,\n this.#schema,\n this.#serverToClient,\n );\n this.#pokeBuffer.length = 0;\n if (merged === undefined) {\n lc.debug?.('frame is empty');\n return;\n }\n const start = performance.now();\n lc.debug?.('poking replicache');\n await this.#replicachePoke(merged);\n lc.debug?.('poking replicache took', performance.now() - start);\n\n if (!('error' in merged.pullResponse)) {\n const lmid =\n merged.pullResponse.lastMutationIDChanges[this.#clientID];\n if (lmid !== undefined) {\n this.#mutationTracker.lmidAdvanced(lmid);\n }\n }\n } catch (e) {\n this.#handlePokeError(e);\n }\n });\n }\n\n #handlePokeError(e: unknown) {\n if (String(e).includes('unexpected base cookie for poke')) {\n // This can happen if cookie changes due to refresh from idb due\n // to an update arriving to different tabs in the same\n // client group at very different times. Unusual but possible.\n this.#lc.debug?.('clearing due to', e);\n } else {\n this.#lc.error?.('clearing due to unexpected poke error', e);\n }\n this.#clear();\n this.#onPokeError(e);\n }\n\n #clear() {\n this.#receivingPoke = undefined;\n this.#pokeBuffer.length = 0;\n }\n}\n\nexport function mergePokes(\n pokeBuffer: PokeAccumulator[],\n schema: Schema,\n serverToClient: NameMapper,\n):\n | (PokeInternal & {mutationResults?: MutationPatch[] | undefined})\n | undefined {\n if (pokeBuffer.length === 0) {\n return undefined;\n }\n const {baseCookie} = pokeBuffer[0].pokeStart;\n const lastPoke = pokeBuffer[pokeBuffer.length - 1];\n const {cookie} = lastPoke.pokeEnd;\n const mergedPatch: PatchOperationInternal[] = [];\n const mergedLastMutationIDChanges: Record<string, number> = {};\n const mutationResults: MutationPatch[] = [];\n\n let prevPokeEnd = undefined;\n for (const pokeAccumulator of pokeBuffer) {\n if (\n prevPokeEnd &&\n pokeAccumulator.pokeStart.baseCookie &&\n pokeAccumulator.pokeStart.baseCookie > prevPokeEnd.cookie\n ) {\n throw Error(\n `unexpected cookie gap ${JSON.stringify(prevPokeEnd)} ${JSON.stringify(\n pokeAccumulator.pokeStart,\n )}`,\n );\n }\n prevPokeEnd = pokeAccumulator.pokeEnd;\n for (const pokePart of pokeAccumulator.parts) {\n if (pokePart.lastMutationIDChanges) {\n for (const [clientID, lastMutationID] of Object.entries(\n pokePart.lastMutationIDChanges,\n )) {\n mergedLastMutationIDChanges[clientID] = lastMutationID;\n }\n }\n if (pokePart.desiredQueriesPatches) {\n for (const [clientID, queriesPatch] of Object.entries(\n pokePart.desiredQueriesPatches,\n )) {\n for (const op of queriesPatch) {\n mergedPatch.push(\n queryPatchOpToReplicachePatchOp(op, hash =>\n toDesiredQueriesKey(clientID, hash),\n ),\n );\n }\n }\n }\n if (pokePart.gotQueriesPatch) {\n for (const op of pokePart.gotQueriesPatch) {\n mergedPatch.push(\n queryPatchOpToReplicachePatchOp(op, toGotQueriesKey),\n );\n }\n }\n if (pokePart.rowsPatch) {\n for (const p of pokePart.rowsPatch) {\n const patchOp = rowsPatchOpToReplicachePatchOp(\n p,\n schema,\n serverToClient,\n );\n if (patchOp) {\n mergedPatch.push(patchOp);\n }\n }\n }\n if (pokePart.mutationsPatch) {\n for (const op of pokePart.mutationsPatch) {\n mergedPatch.push(mutationPatchOpToReplicachePatchOp(op));\n }\n }\n }\n }\n const ret: PokeInternal & {mutationResults?: MutationPatch[] | undefined} = {\n baseCookie,\n pullResponse: {\n lastMutationIDChanges: mergedLastMutationIDChanges,\n patch: mergedPatch,\n cookie,\n },\n };\n\n // For backwards compatibility. Because we're strict on our validation,\n // zero-client must be able to parse pokes with this field before we introduce it.\n // So users can update their clients and then start using custom mutators that write responses to the db.\n if (mutationResults.length > 0) {\n ret.mutationResults = mutationResults;\n }\n return ret;\n}\n\nfunction queryPatchOpToReplicachePatchOp(\n op: QueriesPatchOp,\n toKey: (hash: string) => string,\n): PatchOperation {\n switch (op.op) {\n case 'clear':\n return op;\n case 'del':\n return {\n op: 'del',\n key: toKey(op.hash),\n };\n case 'put':\n return {\n op: 'put',\n key: toKey(op.hash),\n value: null,\n };\n default:\n unreachable(op);\n }\n}\n\nexport function mutationPatchOpToReplicachePatchOp(\n op: MutationPatch,\n): PatchOperationInternal {\n switch (op.op) {\n case 'put':\n return {\n op: 'put',\n key: toMutationResponseKey(op.mutation.id),\n value: op.mutation.result,\n };\n case 'del':\n return {\n op: 'del',\n key: toMutationResponseKey(op.id),\n };\n }\n}\n\nfunction rowsPatchOpToReplicachePatchOp(\n op: RowPatchOp,\n schema: Schema,\n serverToClient: NameMapper,\n): PatchOperationInternal | undefined {\n if (op.op === 'clear') {\n return op;\n }\n // Skip rows for tables not in the client schema. This can happen when\n // the server-side query AST references tables (e.g. issueNotifications)\n // that are not yet part of the client schema definition.\n const tableName = serverToClient.tableNameIfKnown(op.tableName);\n if (!tableName) {\n return undefined;\n }\n switch (op.op) {\n case 'del':\n return {\n op: 'del',\n key: toPrimaryKeyString(\n tableName,\n schema.tables[tableName].primaryKey,\n serverToClient.row(op.tableName, op.id),\n ),\n };\n case 'put':\n return {\n op: 'put',\n key: toPrimaryKeyString(\n tableName,\n schema.tables[tableName].primaryKey,\n serverToClient.row(op.tableName, op.value),\n ),\n value: serverToClient.row(op.tableName, op.value),\n };\n case 'update':\n return {\n op: 'update',\n key: toPrimaryKeyString(\n tableName,\n schema.tables[tableName].primaryKey,\n serverToClient.row(op.tableName, op.id),\n ),\n merge: op.merge\n ? serverToClient.row(op.tableName, op.merge)\n : undefined,\n constrain: serverToClient.columns(op.tableName, op.constrain),\n };\n default:\n unreachable(op);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AA6CA,IAAa,cAAb,MAAyB;CACvB;CACA;CACA;CACA;CACA,iBAA+D,KAAA;CAC/D,cAA0C,EAAE;CAC5C,2BAA2B;CAC3B,0BAA0B;CAG1B,YAAqB,IAAI,MAAM;CAC/B;CACA;CACA;CAEA,YACE,gBACA,aACA,UACA,QACA,IACA,iBACA;AACA,QAAA,iBAAuB;AACvB,QAAA,cAAoB;AACpB,QAAA,WAAiB;AACjB,QAAA,SAAe;AACf,QAAA,iBAAuB,eAAe,OAAO,OAAO;AACpD,QAAA,KAAW,GAAG,YAAY,cAAc;AACxC,QAAA,kBAAwB;;CAG1B,gBAAgB,WAA0B;AACxC,MAAI,MAAA,eAAqB;AACvB,SAAA,gBACE,aAAa,KAAK,UAChB,UACD,CAAC,0BAA0B,KAAK,UAC/B,MAAA,cAAoB,UACrB,CAAC,G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K,sBAAsB,GAAG,SAAS,GAAG;GAC1C,OAAO,GAAG,SAAS;GACpB;EACH,KAAK,MACH,QAAO;GACL,IAAI;GACJ,KAAK,sBAAsB,GAAG,GAAG;GAClC;;;AAIP,SAAS,+BACP,IACA,QACA,gBACoC;AACpC,KAAI,GAAG,OAAO,QACZ,QAAO;CAKT,MAAM,YAAY,eAAe,iBAAiB,GAAG,UAAU;AAC/D,KAAI,CAAC,UACH;AAEF,SAAQ,GAAG,IAAX;EACE,KAAK,MACH,QAAO;GACL,IAAI;GACJ,KAAK,mBACH,WACA,OAAO,OAAO,WAAW,YACzB,eAAe,IAAI,GAAG,WAAW,GAAG,GAAG,CACxC;GACF;EACH,KAAK,MACH,QAAO;GACL,IAAI;GACJ,KAAK,mBACH,WACA,OAAO,OAAO,WAAW,YACzB,eAAe,IAAI,GAAG,WAAW,GAAG,MAAM,CAC3C;GACD,OAAO,eAAe,IAAI,GAAG,WAAW,GAAG,MAAM;GAClD;EACH,KAAK,SACH,QAAO;GACL,IAAI;GACJ,KAAK,mBACH,WACA,OAAO,OAAO,WAAW,YACzB,eAAe,IAAI,GAAG,WAAW,GAAG,GAAG,CACxC;GACD,OAAO,GAAG,QACN,eAAe,IAAI,GAAG,WAAW,GAAG,MAAM,GAC1C,KAAA;GACJ,WAAW,eAAe,QAAQ,GAAG,WAAW,GAAG,UAAU;GAC9D;EACH,QACE,aAAY,GAAG"}
@@ -18,7 +18,7 @@ export interface ApplicationErrorOptions<T extends ReadonlyJSONValue | undefined
18
18
  */
19
19
  export declare class ApplicationError<const T extends ReadonlyJSONValue | undefined = ReadonlyJSONValue | undefined> extends Error {
20
20
  #private;
21
- constructor(message: string, options?: ApplicationErrorOptions<T> | undefined);
21
+ constructor(message: string, options?: ApplicationErrorOptions<T>);
22
22
  get details(): T;
23
23
  get kind(): 'Application';
24
24
  }
@@ -1 +1 @@
1
- {"version":3,"file":"application-error.d.ts","sourceRoot":"","sources":["../../../../zero-protocol/src/application-error.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEhE;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB,CACtC,CAAC,SAAS,iBAAiB,GAAG,SAAS;IAEvC,OAAO,CAAC,EAAE,CAAC,CAAC;IACZ,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;GAMG;AACH,qBAAa,gBAAgB,CAC3B,KAAK,CAAC,CAAC,SAAS,iBAAiB,GAAG,SAAS,GAAG,iBAAiB,GAAG,SAAS,CAC7E,SAAQ,KAAK;;gBAOX,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,uBAAuB,CAAC,CAAC,CAAC,GAAG,SAAS;IAOlD,IAAI,OAAO,IAAI,CAAC,CAEf;IAED,IAAI,IAAI,IAAI,aAAa,CAExB;CACF;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,gBAAgB,CAE5E;AAED,wBAAgB,wBAAwB,CACtC,CAAC,SAAS,iBAAiB,GAAG,SAAS,GAAG,iBAAiB,GAAG,SAAS,EACvE,KAAK,EAAE,OAAO,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAYrC"}
1
+ {"version":3,"file":"application-error.d.ts","sourceRoot":"","sources":["../../../../zero-protocol/src/application-error.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEhE;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB,CACtC,CAAC,SAAS,iBAAiB,GAAG,SAAS;IAEvC,OAAO,CAAC,EAAE,CAAC,CAAC;IACZ,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;GAMG;AACH,qBAAa,gBAAgB,CAC3B,KAAK,CAAC,CAAC,SAAS,iBAAiB,GAAG,SAAS,GAAG,iBAAiB,GAAG,SAAS,CAC7E,SAAQ,KAAK;;gBAMD,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,uBAAuB,CAAC,CAAC,CAAC;IAMjE,IAAI,OAAO,IAAI,CAAC,CAEf;IAED,IAAI,IAAI,IAAI,aAAa,CAExB;CACF;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,gBAAgB,CAE5E;AAED,wBAAgB,wBAAwB,CACtC,CAAC,SAAS,iBAAiB,GAAG,SAAS,GAAG,iBAAiB,GAAG,SAAS,EACvE,KAAK,EAAE,OAAO,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAYrC"}
@@ -1 +1 @@
1
- {"version":3,"file":"application-error.js","names":["#details"],"sources":["../../../../zero-protocol/src/application-error.ts"],"sourcesContent":["import {getErrorDetails, getErrorMessage} from '../../shared/src/error.ts';\nimport type {ReadonlyJSONValue} from '../../shared/src/json.ts';\n\n/**\n * Options accepted by {@link ApplicationError}.\n *\n * Use these when you need to attach additional context to an error that will\n * be sent back to the client.\n */\nexport interface ApplicationErrorOptions<\n T extends ReadonlyJSONValue | undefined,\n> {\n details?: T;\n cause?: unknown;\n}\n\n/**\n * Error type that application code can throw to surface structured metadata back to\n * the client.\n *\n * Use this when you want to return a descriptive message along with the\n * JSON-serializable `details`.\n */\nexport class ApplicationError<\n const T extends ReadonlyJSONValue | undefined = ReadonlyJSONValue | undefined,\n> extends Error {\n /**\n * This maps onto errors for transform and push app-level failures.\n */\n readonly #details: T;\n\n constructor(\n message: string,\n options?: ApplicationErrorOptions<T> | undefined,\n ) {\n super(message, {cause: options?.cause});\n this.name = 'ApplicationError';\n this.#details = options?.details ?? (undefined as T);\n }\n\n get details(): T {\n return this.#details;\n }\n\n get kind(): 'Application' {\n return 'Application';\n }\n}\n\nexport function isApplicationError(error: unknown): error is ApplicationError {\n return error instanceof ApplicationError;\n}\n\nexport function wrapWithApplicationError<\n T extends ReadonlyJSONValue | undefined = ReadonlyJSONValue | undefined,\n>(error: unknown): ApplicationError<T> {\n if (isApplicationError(error)) {\n return error as ApplicationError<T>;\n }\n\n const message = getErrorMessage(error);\n const details = getErrorDetails(error);\n\n return new ApplicationError<T>(message, {\n cause: error,\n details: details as T,\n });\n}\n"],"mappings":";;;;;;;;;AAuBA,IAAa,mBAAb,cAEU,MAAM;;;;CAId;CAEA,YACE,SACA,SACA;AACA,QAAM,SAAS,EAAC,OAAO,SAAS,OAAM,CAAC;AACvC,OAAK,OAAO;AACZ,QAAA,UAAgB,SAAS,WAAY,KAAA;;CAGvC,IAAI,UAAa;AACf,SAAO,MAAA;;CAGT,IAAI,OAAsB;AACxB,SAAO;;;AAIX,SAAgB,mBAAmB,OAA2C;AAC5E,QAAO,iBAAiB;;AAG1B,SAAgB,yBAEd,OAAqC;AACrC,KAAI,mBAAmB,MAAM,CAC3B,QAAO;AAMT,QAAO,IAAI,iBAHK,gBAAgB,MAAM,EAGE;EACtC,OAAO;EACE,SAJK,gBAAgB,MAAM;EAKrC,CAAC"}
1
+ {"version":3,"file":"application-error.js","names":["#details"],"sources":["../../../../zero-protocol/src/application-error.ts"],"sourcesContent":["import {getErrorDetails, getErrorMessage} from '../../shared/src/error.ts';\nimport type {ReadonlyJSONValue} from '../../shared/src/json.ts';\n\n/**\n * Options accepted by {@link ApplicationError}.\n *\n * Use these when you need to attach additional context to an error that will\n * be sent back to the client.\n */\nexport interface ApplicationErrorOptions<\n T extends ReadonlyJSONValue | undefined,\n> {\n details?: T;\n cause?: unknown;\n}\n\n/**\n * Error type that application code can throw to surface structured metadata back to\n * the client.\n *\n * Use this when you want to return a descriptive message along with the\n * JSON-serializable `details`.\n */\nexport class ApplicationError<\n const T extends ReadonlyJSONValue | undefined = ReadonlyJSONValue | undefined,\n> extends Error {\n /**\n * This maps onto errors for transform and push app-level failures.\n */\n readonly #details: T;\n\n constructor(message: string, options?: ApplicationErrorOptions<T>) {\n super(message, {cause: options?.cause});\n this.name = 'ApplicationError';\n this.#details = options?.details ?? (undefined as T);\n }\n\n get details(): T {\n return this.#details;\n }\n\n get kind(): 'Application' {\n return 'Application';\n }\n}\n\nexport function isApplicationError(error: unknown): error is ApplicationError {\n return error instanceof ApplicationError;\n}\n\nexport function wrapWithApplicationError<\n T extends ReadonlyJSONValue | undefined = ReadonlyJSONValue | undefined,\n>(error: unknown): ApplicationError<T> {\n if (isApplicationError(error)) {\n return error as ApplicationError<T>;\n }\n\n const message = getErrorMessage(error);\n const details = getErrorDetails(error);\n\n return new ApplicationError<T>(message, {\n cause: error,\n details: details as T,\n });\n}\n"],"mappings":";;;;;;;;;AAuBA,IAAa,mBAAb,cAEU,MAAM;;;;CAId;CAEA,YAAY,SAAiB,SAAsC;AACjE,QAAM,SAAS,EAAC,OAAO,SAAS,OAAM,CAAC;AACvC,OAAK,OAAO;AACZ,QAAA,UAAgB,SAAS,WAAY,KAAA;;CAGvC,IAAI,UAAa;AACf,SAAO,MAAA;;CAGT,IAAI,OAAsB;AACxB,SAAO;;;AAIX,SAAgB,mBAAmB,OAA2C;AAC5E,QAAO,iBAAiB;;AAG1B,SAAgB,yBAEd,OAAqC;AACrC,KAAI,mBAAmB,MAAM,CAC3B,QAAO;AAMT,QAAO,IAAI,iBAHK,gBAAgB,MAAM,EAGE;EACtC,OAAO;EACE,SAJK,gBAAgB,MAAM;EAKrC,CAAC"}
@@ -12,6 +12,7 @@ export declare class NameMapper {
12
12
  #private;
13
13
  constructor(tables: Map<string, DestNames>);
14
14
  tableName(src: string, context?: JSONValue): string;
15
+ tableNameIfKnown(src: string): string | undefined;
15
16
  columnName(table: string, src: string, ctx?: JSONValue): string;
16
17
  row<V extends Value>(table: string, row: Readonly<Record<string, V>>): Readonly<Record<string, V>>;
17
18
  columns<Columns extends readonly string[] | undefined>(table: string, cols: Columns): Columns;
@@ -1 +1 @@
1
- {"version":3,"file":"name-mapper.d.ts","sourceRoot":"","sources":["../../../../zero-types/src/name-mapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,SAAS,EAAE,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAI3E,MAAM,MAAM,KAAK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,OAAO,EAAE,WAAW,CAAC;IACrB,cAAc,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,qBAAa,UAAU;;gBAGT,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC;IAc1C,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,GAAG,MAAM;IAInD,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIjD,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,SAAS,GAAG,MAAM;IAY/D,GAAG,CAAC,CAAC,SAAS,KAAK,EACjB,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,GAC/B,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAc9B,OAAO,CAAC,OAAO,SAAS,SAAS,MAAM,EAAE,GAAG,SAAS,EACnD,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,OAAO,GACZ,OAAO;CASX"}
@@ -12,6 +12,9 @@ var NameMapper = class {
12
12
  tableName(src, context) {
13
13
  return this.#getTable(src, context).tableName;
14
14
  }
15
+ tableNameIfKnown(src) {
16
+ return this.#tables.get(src)?.tableName;
17
+ }
15
18
  columnName(table, src, ctx) {
16
19
  const dst = this.#getTable(table, ctx).columns[src];
17
20
  if (!dst) throw new Error(`unknown column "${src}" of "${table}" table ${!ctx ? "" : `in ${JSON.stringify(ctx)}`}`);
@@ -1 +1 @@
1
- {"version":3,"file":"name-mapper.js","names":["#tables","#getTable"],"sources":["../../../../zero-types/src/name-mapper.ts"],"sourcesContent":["import type {JSONValue, ReadonlyJSONValue} from '../../shared/src/json.ts';\n\n// Value type from zero-protocol (JSONValue/ReadonlyJSONValue | undefined)\n// Defined here to avoid circular dependency with zero-protocol\nexport type Value = JSONValue | ReadonlyJSONValue | undefined;\n\nexport type ColumnNames = {[src: string]: string};\n\nexport type DestNames = {\n tableName: string;\n columns: ColumnNames;\n allColumnsSame: boolean;\n};\n\nexport class NameMapper {\n readonly #tables = new Map<string, DestNames>();\n\n constructor(tables: Map<string, DestNames>) {\n this.#tables = tables;\n }\n\n #getTable(src: string, ctx?: JSONValue): DestNames {\n const table = this.#tables.get(src);\n if (!table) {\n throw new Error(\n `unknown table \"${src}\" ${!ctx ? '' : `in ${JSON.stringify(ctx)}`}`,\n );\n }\n return table;\n }\n\n tableName(src: string, context?: JSONValue): string {\n return this.#getTable(src, context).tableName;\n }\n\n columnName(table: string, src: string, ctx?: JSONValue): string {\n const dst = this.#getTable(table, ctx).columns[src];\n if (!dst) {\n throw new Error(\n `unknown column \"${src}\" of \"${table}\" table ${\n !ctx ? '' : `in ${JSON.stringify(ctx)}`\n }`,\n );\n }\n return dst;\n }\n\n row<V extends Value>(\n table: string,\n row: Readonly<Record<string, V>>,\n ): Readonly<Record<string, V>> {\n const dest = this.#getTable(table);\n const {allColumnsSame, columns} = dest;\n if (allColumnsSame) {\n return row;\n }\n const clientRow: Record<string, V> = {};\n for (const col in row) {\n // Note: columns with unknown names simply pass through.\n clientRow[columns[col] ?? col] = row[col];\n }\n return clientRow;\n }\n\n columns<Columns extends readonly string[] | undefined>(\n table: string,\n cols: Columns,\n ): Columns {\n const dest = this.#getTable(table);\n const {allColumnsSame, columns} = dest;\n\n // Note: Columns not defined in the schema simply pass through.\n return cols === undefined || allColumnsSame\n ? cols\n : (cols.map(col => columns[col] ?? col) as unknown as Columns);\n }\n}\n"],"mappings":";AAcA,IAAa,aAAb,MAAwB;CACtB,0BAAmB,IAAI,KAAwB;CAE/C,YAAY,QAAgC;AAC1C,QAAA,SAAe;;CAGjB,UAAU,KAAa,KAA4B;EACjD,MAAM,QAAQ,MAAA,OAAa,IAAI,IAAI;AACnC,MAAI,CAAC,MACH,OAAM,IAAI,MACR,kBAAkB,IAAI,IAAI,CAAC,MAAM,KAAK,MAAM,KAAK,UAAU,IAAI,KAChE;AAEH,SAAO;;CAGT,UAAU,KAAa,SAA6B;AAClD,SAAO,MAAA,SAAe,KAAK,QAAQ,CAAC;;CAGtC,WAAW,OAAe,KAAa,KAAyB;EAC9D,MAAM,MAAM,MAAA,SAAe,OAAO,IAAI,CAAC,QAAQ;AAC/C,MAAI,CAAC,IACH,OAAM,IAAI,MACR,mBAAmB,IAAI,QAAQ,MAAM,UACnC,CAAC,MAAM,KAAK,MAAM,KAAK,UAAU,IAAI,KAExC;AAEH,SAAO;;CAGT,IACE,OACA,KAC6B;EAE7B,MAAM,EAAC,gBAAgB,YADV,MAAA,SAAe,MAAM;AAElC,MAAI,eACF,QAAO;EAET,MAAM,YAA+B,EAAE;AACvC,OAAK,MAAM,OAAO,IAEhB,WAAU,QAAQ,QAAQ,OAAO,IAAI;AAEvC,SAAO;;CAGT,QACE,OACA,MACS;EAET,MAAM,EAAC,gBAAgB,YADV,MAAA,SAAe,MAAM;AAIlC,SAAO,SAAS,KAAA,KAAa,iBACzB,OACC,KAAK,KAAI,QAAO,QAAQ,QAAQ,IAAI"}
1
+ {"version":3,"file":"name-mapper.js","names":["#tables","#getTable"],"sources":["../../../../zero-types/src/name-mapper.ts"],"sourcesContent":["import type {JSONValue, ReadonlyJSONValue} from '../../shared/src/json.ts';\n\n// Value type from zero-protocol (JSONValue/ReadonlyJSONValue | undefined)\n// Defined here to avoid circular dependency with zero-protocol\nexport type Value = JSONValue | ReadonlyJSONValue | undefined;\n\nexport type ColumnNames = {[src: string]: string};\n\nexport type DestNames = {\n tableName: string;\n columns: ColumnNames;\n allColumnsSame: boolean;\n};\n\nexport class NameMapper {\n readonly #tables = new Map<string, DestNames>();\n\n constructor(tables: Map<string, DestNames>) {\n this.#tables = tables;\n }\n\n #getTable(src: string, ctx?: JSONValue): DestNames {\n const table = this.#tables.get(src);\n if (!table) {\n throw new Error(\n `unknown table \"${src}\" ${!ctx ? '' : `in ${JSON.stringify(ctx)}`}`,\n );\n }\n return table;\n }\n\n tableName(src: string, context?: JSONValue): string {\n return this.#getTable(src, context).tableName;\n }\n\n tableNameIfKnown(src: string): string | undefined {\n return this.#tables.get(src)?.tableName;\n }\n\n columnName(table: string, src: string, ctx?: JSONValue): string {\n const dst = this.#getTable(table, ctx).columns[src];\n if (!dst) {\n throw new Error(\n `unknown column \"${src}\" of \"${table}\" table ${\n !ctx ? '' : `in ${JSON.stringify(ctx)}`\n }`,\n );\n }\n return dst;\n }\n\n row<V extends Value>(\n table: string,\n row: Readonly<Record<string, V>>,\n ): Readonly<Record<string, V>> {\n const dest = this.#getTable(table);\n const {allColumnsSame, columns} = dest;\n if (allColumnsSame) {\n return row;\n }\n const clientRow: Record<string, V> = {};\n for (const col in row) {\n // Note: columns with unknown names simply pass through.\n clientRow[columns[col] ?? col] = row[col];\n }\n return clientRow;\n }\n\n columns<Columns extends readonly string[] | undefined>(\n table: string,\n cols: Columns,\n ): Columns {\n const dest = this.#getTable(table);\n const {allColumnsSame, columns} = dest;\n\n // Note: Columns not defined in the schema simply pass through.\n return cols === undefined || allColumnsSame\n ? cols\n : (cols.map(col => columns[col] ?? col) as unknown as Columns);\n }\n}\n"],"mappings":";AAcA,IAAa,aAAb,MAAwB;CACtB,0BAAmB,IAAI,KAAwB;CAE/C,YAAY,QAAgC;AAC1C,QAAA,SAAe;;CAGjB,UAAU,KAAa,KAA4B;EACjD,MAAM,QAAQ,MAAA,OAAa,IAAI,IAAI;AACnC,MAAI,CAAC,MACH,OAAM,IAAI,MACR,kBAAkB,IAAI,IAAI,CAAC,MAAM,KAAK,MAAM,KAAK,UAAU,IAAI,KAChE;AAEH,SAAO;;CAGT,UAAU,KAAa,SAA6B;AAClD,SAAO,MAAA,SAAe,KAAK,QAAQ,CAAC;;CAGtC,iBAAiB,KAAiC;AAChD,SAAO,MAAA,OAAa,IAAI,IAAI,EAAE;;CAGhC,WAAW,OAAe,KAAa,KAAyB;EAC9D,MAAM,MAAM,MAAA,SAAe,OAAO,IAAI,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