@syncular/core 0.0.6-96 → 0.1.1

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 (156) hide show
  1. package/dist/blobs.d.ts +14 -7
  2. package/dist/blobs.d.ts.map +1 -1
  3. package/dist/blobs.js.map +1 -1
  4. package/dist/column-codecs.d.ts +3 -0
  5. package/dist/column-codecs.d.ts.map +1 -1
  6. package/dist/column-codecs.js +32 -4
  7. package/dist/column-codecs.js.map +1 -1
  8. package/dist/conflict.js.map +1 -1
  9. package/dist/database.js.map +1 -1
  10. package/dist/error-responses.d.ts +393 -0
  11. package/dist/error-responses.d.ts.map +1 -0
  12. package/dist/error-responses.js +393 -0
  13. package/dist/error-responses.js.map +1 -0
  14. package/dist/http/api-client.d.ts +11 -0
  15. package/dist/http/api-client.d.ts.map +1 -0
  16. package/dist/http/api-client.js +30 -0
  17. package/dist/http/api-client.js.map +1 -0
  18. package/dist/http/blob.d.ts +6 -0
  19. package/dist/http/blob.d.ts.map +1 -0
  20. package/dist/http/blob.js +46 -0
  21. package/dist/http/blob.js.map +1 -0
  22. package/dist/http/generated/api.d.ts +5241 -0
  23. package/dist/http/generated/api.d.ts.map +1 -0
  24. package/dist/http/generated/api.js +6 -0
  25. package/dist/http/generated/api.js.map +1 -0
  26. package/dist/http/index.d.ts +18 -0
  27. package/dist/http/index.d.ts.map +1 -0
  28. package/dist/http/index.js +212 -0
  29. package/dist/http/index.js.map +1 -0
  30. package/dist/http/shared.d.ts +45 -0
  31. package/dist/http/shared.d.ts.map +1 -0
  32. package/dist/http/shared.js +122 -0
  33. package/dist/http/shared.js.map +1 -0
  34. package/dist/http/transport-client.d.ts +31 -0
  35. package/dist/http/transport-client.d.ts.map +1 -0
  36. package/dist/http/transport-client.js +193 -0
  37. package/dist/http/transport-client.js.map +1 -0
  38. package/dist/index.d.ts +2 -0
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +4 -0
  41. package/dist/index.js.map +1 -1
  42. package/dist/kysely-column-codecs.d.ts +17 -1
  43. package/dist/kysely-column-codecs.d.ts.map +1 -1
  44. package/dist/kysely-column-codecs.js +56 -70
  45. package/dist/kysely-column-codecs.js.map +1 -1
  46. package/dist/logger.js.map +1 -1
  47. package/dist/schemas/blobs.d.ts +7 -1
  48. package/dist/schemas/blobs.d.ts.map +1 -1
  49. package/dist/schemas/blobs.js +1 -0
  50. package/dist/schemas/blobs.js.map +1 -1
  51. package/dist/schemas/common.d.ts +73 -1
  52. package/dist/schemas/common.d.ts.map +1 -1
  53. package/dist/schemas/common.js +38 -0
  54. package/dist/schemas/common.js.map +1 -1
  55. package/dist/schemas/sync.d.ts +712 -38
  56. package/dist/schemas/sync.d.ts.map +1 -1
  57. package/dist/schemas/sync.js +209 -5
  58. package/dist/schemas/sync.js.map +1 -1
  59. package/dist/scopes/index.d.ts +5 -0
  60. package/dist/scopes/index.d.ts.map +1 -1
  61. package/dist/scopes/index.js +27 -0
  62. package/dist/scopes/index.js.map +1 -1
  63. package/dist/sentry.d.ts +31 -0
  64. package/dist/sentry.d.ts.map +1 -0
  65. package/dist/sentry.js +147 -0
  66. package/dist/sentry.js.map +1 -0
  67. package/dist/snapshot-chunks.d.ts +104 -20
  68. package/dist/snapshot-chunks.d.ts.map +1 -1
  69. package/dist/snapshot-chunks.js +665 -72
  70. package/dist/snapshot-chunks.js.map +1 -1
  71. package/dist/sync-packs.d.ts +33 -0
  72. package/dist/sync-packs.d.ts.map +1 -0
  73. package/dist/sync-packs.js +821 -0
  74. package/dist/sync-packs.js.map +1 -0
  75. package/dist/telemetry.d.ts.map +1 -1
  76. package/dist/telemetry.js +3 -2
  77. package/dist/telemetry.js.map +1 -1
  78. package/dist/transforms.js.map +1 -1
  79. package/dist/types.d.ts +48 -7
  80. package/dist/types.d.ts.map +1 -1
  81. package/dist/types.js.map +1 -1
  82. package/dist/utils/bytes.d.ts +4 -0
  83. package/dist/utils/bytes.d.ts.map +1 -0
  84. package/dist/utils/bytes.js +66 -0
  85. package/dist/utils/bytes.js.map +1 -0
  86. package/dist/utils/compression.d.ts +11 -3
  87. package/dist/utils/compression.d.ts.map +1 -1
  88. package/dist/utils/compression.js +102 -50
  89. package/dist/utils/compression.js.map +1 -1
  90. package/dist/utils/crypto.d.ts +7 -1
  91. package/dist/utils/crypto.d.ts.map +1 -1
  92. package/dist/utils/crypto.js +89 -7
  93. package/dist/utils/crypto.js.map +1 -1
  94. package/dist/utils/id.d.ts.map +1 -1
  95. package/dist/utils/id.js +4 -1
  96. package/dist/utils/id.js.map +1 -1
  97. package/dist/utils/index.d.ts +5 -0
  98. package/dist/utils/index.d.ts.map +1 -1
  99. package/dist/utils/index.js +5 -0
  100. package/dist/utils/index.js.map +1 -1
  101. package/dist/utils/internal-runtime.d.ts +16 -0
  102. package/dist/utils/internal-runtime.d.ts.map +1 -0
  103. package/dist/utils/internal-runtime.js +14 -0
  104. package/dist/utils/internal-runtime.js.map +1 -0
  105. package/dist/utils/object.js.map +1 -1
  106. package/dist/utils/realtime-change-index.d.ts +9 -0
  107. package/dist/utils/realtime-change-index.d.ts.map +1 -0
  108. package/dist/utils/realtime-change-index.js +36 -0
  109. package/dist/utils/realtime-change-index.js.map +1 -0
  110. package/dist/utils/realtime-connection-registry.d.ts +40 -0
  111. package/dist/utils/realtime-connection-registry.d.ts.map +1 -0
  112. package/dist/utils/realtime-connection-registry.js +237 -0
  113. package/dist/utils/realtime-connection-registry.js.map +1 -0
  114. package/dist/utils/table-registry.d.ts +10 -0
  115. package/dist/utils/table-registry.d.ts.map +1 -0
  116. package/dist/utils/table-registry.js +54 -0
  117. package/dist/utils/table-registry.js.map +1 -0
  118. package/dist/utils/url.d.ts +3 -0
  119. package/dist/utils/url.d.ts.map +1 -0
  120. package/dist/utils/url.js +20 -0
  121. package/dist/utils/url.js.map +1 -0
  122. package/package.json +51 -6
  123. package/src/blobs.ts +24 -7
  124. package/src/column-codecs.ts +36 -8
  125. package/src/error-responses.ts +423 -0
  126. package/src/http/api-client.ts +39 -0
  127. package/src/http/blob.ts +95 -0
  128. package/src/http/generated/api.ts +5241 -0
  129. package/src/http/index.ts +365 -0
  130. package/src/http/shared.ts +220 -0
  131. package/src/http/transport-client.ts +314 -0
  132. package/src/index.ts +4 -0
  133. package/src/kysely-column-codecs.ts +81 -86
  134. package/src/schemas/blobs.ts +10 -1
  135. package/src/schemas/common.ts +44 -0
  136. package/src/schemas/sync.ts +298 -7
  137. package/src/scopes/index.ts +42 -0
  138. package/src/sentry.ts +200 -0
  139. package/src/snapshot-chunks.ts +921 -69
  140. package/src/sync-packs.ts +1096 -0
  141. package/src/telemetry.ts +7 -2
  142. package/src/types.ts +63 -8
  143. package/src/utils/bytes.ts +75 -0
  144. package/src/utils/compression.ts +147 -51
  145. package/src/utils/crypto.ts +120 -7
  146. package/src/utils/id.ts +11 -1
  147. package/src/utils/index.ts +5 -0
  148. package/src/utils/internal-runtime.ts +39 -0
  149. package/src/utils/realtime-change-index.ts +44 -0
  150. package/src/utils/realtime-connection-registry.ts +286 -0
  151. package/src/utils/table-registry.ts +67 -0
  152. package/src/utils/url.ts +29 -0
  153. package/src/__tests__/compression.test.ts +0 -54
  154. package/src/__tests__/conflict.test.ts +0 -325
  155. package/src/__tests__/telemetry.test.ts +0 -170
  156. package/src/__tests__/utils.test.ts +0 -27
@@ -1,89 +1,682 @@
1
1
  /**
2
2
  * @syncular/core - Snapshot chunk encoding helpers
3
3
  */
4
- export const SYNC_SNAPSHOT_CHUNK_ENCODING = 'json-row-frame-v1';
4
+ import { sha256Hex } from './utils/crypto.js';
5
+ export const SYNC_SNAPSHOT_CHUNK_ENCODING_BINARY_TABLE_V1 = 'binary-table-v1';
6
+ export const SYNC_SNAPSHOT_CHUNK_ENCODINGS = [
7
+ SYNC_SNAPSHOT_CHUNK_ENCODING_BINARY_TABLE_V1,
8
+ ];
9
+ export const SYNC_SNAPSHOT_CHUNK_ENCODING = SYNC_SNAPSHOT_CHUNK_ENCODING_BINARY_TABLE_V1;
10
+ export function isSyncSnapshotChunkEncoding(value) {
11
+ return (typeof value === 'string' &&
12
+ SYNC_SNAPSHOT_CHUNK_ENCODINGS.includes(value));
13
+ }
5
14
  export const SYNC_SNAPSHOT_CHUNK_COMPRESSION = 'gzip';
6
- const SNAPSHOT_ROW_FRAME_MAGIC = new Uint8Array([0x53, 0x52, 0x46, 0x31]); // "SRF1"
7
- const FRAME_LENGTH_BYTES = 4;
15
+ export const SYNC_SNAPSHOT_ARTIFACT_COMPRESSION_NONE = 'none';
16
+ export const SYNC_SCOPED_SNAPSHOT_ARTIFACT_MANIFEST_VERSION = 1;
17
+ export const SYNC_SCOPED_SNAPSHOT_ARTIFACT_KIND_SQLITE_V1 = 'sqlite-snapshot-v1';
18
+ const SNAPSHOT_BINARY_TABLE_MAGIC = new Uint8Array([0x53, 0x42, 0x54, 0x31]); // "SBT1"
19
+ const BINARY_TABLE_VERSION = 1;
20
+ const BINARY_TABLE_FLAG_NONE = 0;
21
+ const BINARY_COLUMN_FLAG_NULLABLE = 1;
8
22
  const MAX_FRAME_BYTE_LENGTH = 0xffff_ffff;
23
+ const snapshotTextEncoder = new TextEncoder();
24
+ const snapshotTextDecoder = new TextDecoder();
25
+ const BINARY_TYPE_TAGS = {
26
+ string: 1,
27
+ integer: 2,
28
+ float: 3,
29
+ boolean: 4,
30
+ json: 5,
31
+ bytes: 6,
32
+ };
33
+ const BINARY_COLUMN_TYPES_BY_TAG = new Map(Object.entries(BINARY_TYPE_TAGS).map(([type, tag]) => [
34
+ tag,
35
+ type,
36
+ ]));
9
37
  function normalizeRowJson(row) {
10
38
  const serialized = JSON.stringify(row);
11
39
  return serialized === undefined ? 'null' : serialized;
12
40
  }
13
- /**
14
- * Encode rows as framed JSON bytes without the format header.
15
- */
16
- export function encodeSnapshotRowFrames(rows) {
17
- const encoder = new TextEncoder();
18
- const payloads = [];
19
- let totalByteLength = 0;
20
- for (const row of rows) {
21
- const payload = encoder.encode(normalizeRowJson(row));
22
- if (payload.length > MAX_FRAME_BYTE_LENGTH) {
23
- throw new Error(`Snapshot row payload exceeds ${MAX_FRAME_BYTE_LENGTH} bytes`);
24
- }
25
- payloads.push(payload);
26
- totalByteLength += FRAME_LENGTH_BYTES + payload.length;
27
- }
28
- const encoded = new Uint8Array(totalByteLength);
29
- const view = new DataView(encoded.buffer, encoded.byteOffset, encoded.length);
30
- let offset = 0;
31
- for (const payload of payloads) {
32
- view.setUint32(offset, payload.length, false);
33
- offset += FRAME_LENGTH_BYTES;
34
- encoded.set(payload, offset);
35
- offset += payload.length;
36
- }
37
- return encoded;
41
+ function utf8ByteLength(value) {
42
+ return snapshotTextEncoder.encode(value).length;
38
43
  }
39
- /**
40
- * Encode rows as framed JSON bytes with a format header.
41
- *
42
- * Format:
43
- * - 4-byte magic header ("SRF1")
44
- * - repeated frames of:
45
- * - 4-byte big-endian payload byte length
46
- * - UTF-8 JSON payload
47
- */
48
- export function encodeSnapshotRows(rows) {
49
- const framedRows = encodeSnapshotRowFrames(rows);
50
- const totalByteLength = SNAPSHOT_ROW_FRAME_MAGIC.length + framedRows.length;
51
- const encoded = new Uint8Array(totalByteLength);
52
- encoded.set(SNAPSHOT_ROW_FRAME_MAGIC, 0);
53
- encoded.set(framedRows, SNAPSHOT_ROW_FRAME_MAGIC.length);
54
- return encoded;
44
+ function appendStringField(parts, name, value) {
45
+ parts.push(`${name}:s:${utf8ByteLength(value)}:${value}`);
55
46
  }
56
- /**
57
- * Decode framed JSON bytes into rows.
58
- */
59
- export function decodeSnapshotRows(bytes) {
60
- if (bytes.length < SNAPSHOT_ROW_FRAME_MAGIC.length) {
61
- throw new Error('Snapshot chunk payload is too small');
47
+ function appendNullableStringField(parts, name, value) {
48
+ if (value === null) {
49
+ parts.push(`${name}:n`);
50
+ return;
51
+ }
52
+ appendStringField(parts, name, value);
53
+ }
54
+ function appendIntField(parts, name, value) {
55
+ if (!Number.isSafeInteger(value)) {
56
+ throw new Error(`Snapshot manifest ${name} must be a safe integer`);
57
+ }
58
+ parts.push(`${name}:i:${value}`);
59
+ }
60
+ function appendBoolField(parts, name, value) {
61
+ parts.push(`${name}:b:${value ? 1 : 0}`);
62
+ }
63
+ export function snapshotScopeDigestFromCacheKey(scopeKey) {
64
+ const marker = ':scope:';
65
+ const markerIndex = scopeKey.lastIndexOf(marker);
66
+ if (markerIndex < 0) {
67
+ throw new Error('Snapshot scope cache key is missing scope digest');
68
+ }
69
+ const digest = scopeKey.slice(markerIndex + marker.length);
70
+ if (!/^[a-f0-9]{64}$/.test(digest)) {
71
+ throw new Error('Snapshot scope cache key has invalid scope digest');
72
+ }
73
+ return digest;
74
+ }
75
+ export function snapshotManifestDigestPayload(manifest) {
76
+ const parts = ['syncular.snapshot-manifest.v1'];
77
+ appendIntField(parts, 'version', manifest.version);
78
+ appendStringField(parts, 'table', manifest.table);
79
+ appendIntField(parts, 'asOfCommitSeq', manifest.asOfCommitSeq);
80
+ appendStringField(parts, 'scopeDigest', manifest.scopeDigest);
81
+ appendNullableStringField(parts, 'rowCursor', manifest.rowCursor);
82
+ appendIntField(parts, 'rowLimit', manifest.rowLimit);
83
+ appendNullableStringField(parts, 'nextRowCursor', manifest.nextRowCursor);
84
+ appendBoolField(parts, 'isFirstPage', manifest.isFirstPage);
85
+ appendBoolField(parts, 'isLastPage', manifest.isLastPage);
86
+ appendIntField(parts, 'chunkCount', manifest.chunks.length);
87
+ for (const [index, chunk] of manifest.chunks.entries()) {
88
+ appendIntField(parts, `chunk.${index}.index`, index);
89
+ appendStringField(parts, `chunk.${index}.id`, chunk.id);
90
+ appendIntField(parts, `chunk.${index}.byteLength`, chunk.byteLength);
91
+ appendStringField(parts, `chunk.${index}.sha256`, chunk.sha256);
92
+ appendStringField(parts, `chunk.${index}.encoding`, chunk.encoding);
93
+ appendStringField(parts, `chunk.${index}.compression`, chunk.compression);
62
94
  }
63
- for (let index = 0; index < SNAPSHOT_ROW_FRAME_MAGIC.length; index += 1) {
64
- const expected = SNAPSHOT_ROW_FRAME_MAGIC[index];
65
- const actual = bytes[index];
66
- if (actual !== expected) {
67
- throw new Error('Unexpected snapshot chunk format');
95
+ return `${parts.join('\n')}\n`;
96
+ }
97
+ export async function createSnapshotManifestDigest(manifest) {
98
+ return sha256Hex(snapshotManifestDigestPayload(manifest));
99
+ }
100
+ export async function createSnapshotManifest(manifest) {
101
+ return {
102
+ ...manifest,
103
+ chunks: [...manifest.chunks],
104
+ digest: await createSnapshotManifestDigest(manifest),
105
+ };
106
+ }
107
+ function normalizedFeatureSet(featureSet) {
108
+ return Array.from(new Set(featureSet)).sort();
109
+ }
110
+ function scopedSnapshotArtifactDigestInput(manifest) {
111
+ return {
112
+ ...manifest,
113
+ featureSet: normalizedFeatureSet(manifest.featureSet),
114
+ };
115
+ }
116
+ export function scopedSnapshotArtifactDigestPayload(manifest) {
117
+ const input = scopedSnapshotArtifactDigestInput(manifest);
118
+ const parts = ['syncular.scoped-snapshot-artifact.v1'];
119
+ appendIntField(parts, 'version', input.version);
120
+ appendStringField(parts, 'artifactKind', input.artifactKind);
121
+ appendStringField(parts, 'partitionId', input.partitionId);
122
+ appendStringField(parts, 'subscriptionId', input.subscriptionId);
123
+ appendStringField(parts, 'table', input.table);
124
+ appendStringField(parts, 'schemaVersion', input.schemaVersion);
125
+ appendIntField(parts, 'asOfCommitSeq', input.asOfCommitSeq);
126
+ appendStringField(parts, 'scopeDigest', input.scopeDigest);
127
+ appendNullableStringField(parts, 'rowCursor', input.rowCursor);
128
+ appendIntField(parts, 'rowLimit', input.rowLimit);
129
+ appendIntField(parts, 'rowCount', input.rowCount);
130
+ appendNullableStringField(parts, 'nextRowCursor', input.nextRowCursor);
131
+ appendBoolField(parts, 'isFirstPage', input.isFirstPage);
132
+ appendBoolField(parts, 'isLastPage', input.isLastPage);
133
+ appendStringField(parts, 'compression', input.compression);
134
+ appendIntField(parts, 'byteLength', input.byteLength);
135
+ appendStringField(parts, 'sha256', input.sha256);
136
+ appendIntField(parts, 'featureCount', input.featureSet.length);
137
+ for (const [index, feature] of input.featureSet.entries()) {
138
+ appendIntField(parts, `feature.${index}.index`, index);
139
+ appendStringField(parts, `feature.${index}.name`, feature);
140
+ }
141
+ return `${parts.join('\n')}\n`;
142
+ }
143
+ export async function createScopedSnapshotArtifactDigest(manifest) {
144
+ return sha256Hex(scopedSnapshotArtifactDigestPayload(manifest));
145
+ }
146
+ export async function createScopedSnapshotArtifactManifest(manifest) {
147
+ const normalized = scopedSnapshotArtifactDigestInput(manifest);
148
+ return {
149
+ ...normalized,
150
+ featureSet: [...normalized.featureSet],
151
+ digest: await createScopedSnapshotArtifactDigest(normalized),
152
+ };
153
+ }
154
+ export function encodeBinarySnapshotTable(table) {
155
+ const writer = new BinarySnapshotWriter(estimateBinarySnapshotTableSize(table));
156
+ const valueWriters = table.columns.map(createBinarySnapshotValueWriter);
157
+ writeBinarySnapshotTableHeader(writer, table.table, table.columns, table.rows.length);
158
+ const nullBitmapBytes = Math.ceil(table.columns.length / 8);
159
+ for (const row of table.rows) {
160
+ const nullBitmapOffset = writer.writeZeroes(nullBitmapBytes);
161
+ for (let index = 0; index < table.columns.length; index += 1) {
162
+ const column = table.columns[index];
163
+ const value = row[column.name];
164
+ if (value == null) {
165
+ if (!column.nullable) {
166
+ throw new Error(`Binary snapshot column ${column.name} is not nullable`);
167
+ }
168
+ const bitmapIndex = nullBitmapOffset + Math.floor(index / 8);
169
+ writer.patchUint8(bitmapIndex, writer.readUint8(bitmapIndex) | (1 << (index % 8)));
170
+ continue;
171
+ }
172
+ valueWriters[index](writer, value);
68
173
  }
69
174
  }
175
+ return writer.toUint8Array();
176
+ }
177
+ function createBinarySnapshotValueWriter(column) {
178
+ const label = `binary snapshot ${column.name}`;
179
+ switch (column.type) {
180
+ case 'string':
181
+ return (writer, value) => {
182
+ writer.writeString32(binarySnapshotString(value, label), label);
183
+ };
184
+ case 'integer':
185
+ return (writer, value) => {
186
+ writer.writeInt64(value, label);
187
+ };
188
+ case 'float':
189
+ return (writer, value) => {
190
+ if (typeof value !== 'number') {
191
+ throw new Error(`${label} expected number`);
192
+ }
193
+ writer.writeFloat64(value);
194
+ };
195
+ case 'boolean':
196
+ return (writer, value) => {
197
+ if (typeof value !== 'boolean') {
198
+ throw new Error(`${label} expected boolean`);
199
+ }
200
+ writer.writeUint8(value ? 1 : 0);
201
+ };
202
+ case 'json':
203
+ return (writer, value) => {
204
+ writer.writeString32(normalizeRowJson(value), label);
205
+ };
206
+ case 'bytes':
207
+ return (writer, value) => {
208
+ const bytes = coerceBinaryBytes(value, column.name);
209
+ writer.writeUint32(bytes.length);
210
+ writer.writeBytes(bytes);
211
+ };
212
+ }
213
+ }
214
+ export class BinarySnapshotTableWriter {
215
+ columns;
216
+ writer;
217
+ nullBitmapBytes;
218
+ expectedRows;
219
+ rowsWritten = 0;
220
+ nullBitmapOffset = null;
221
+ constructor(table, columns, rowCount, initialCapacity) {
222
+ this.columns = columns;
223
+ this.expectedRows = rowCount;
224
+ this.nullBitmapBytes = Math.ceil(columns.length / 8);
225
+ this.writer = new BinarySnapshotWriter(initialCapacity ??
226
+ estimateBinarySnapshotStaticSize(table, columns, rowCount));
227
+ writeBinarySnapshotTableHeader(this.writer, table, columns, rowCount);
228
+ }
229
+ beginRow() {
230
+ if (this.rowsWritten >= this.expectedRows) {
231
+ throw new Error('Binary snapshot writer received too many rows');
232
+ }
233
+ this.nullBitmapOffset = this.writer.writeZeroes(this.nullBitmapBytes);
234
+ this.rowsWritten += 1;
235
+ }
236
+ writeNull(columnIndex) {
237
+ const column = this.column(columnIndex);
238
+ if (!column.nullable) {
239
+ throw new Error(`Binary snapshot column ${column.name} is not nullable`);
240
+ }
241
+ const nullBitmapOffset = this.currentNullBitmapOffset();
242
+ const bitmapIndex = nullBitmapOffset + Math.floor(columnIndex / 8);
243
+ this.writer.patchUint8(bitmapIndex, this.writer.readUint8(bitmapIndex) | (1 << (columnIndex % 8)));
244
+ }
245
+ writeString(value, label) {
246
+ this.currentNullBitmapOffset();
247
+ this.writer.writeString32(binarySnapshotString(value, label), label);
248
+ }
249
+ writeInteger(value, label) {
250
+ this.currentNullBitmapOffset();
251
+ this.writer.writeInt64(value, label);
252
+ }
253
+ writeFloat(value, label) {
254
+ this.currentNullBitmapOffset();
255
+ if (typeof value !== 'number') {
256
+ throw new Error(`${label} expected number`);
257
+ }
258
+ this.writer.writeFloat64(value);
259
+ }
260
+ writeBoolean(value, label) {
261
+ this.currentNullBitmapOffset();
262
+ if (typeof value !== 'boolean') {
263
+ throw new Error(`${label} expected boolean`);
264
+ }
265
+ this.writer.writeUint8(value ? 1 : 0);
266
+ }
267
+ writeJson(value, label) {
268
+ this.currentNullBitmapOffset();
269
+ this.writer.writeString32(normalizeRowJson(value), label);
270
+ }
271
+ writeBytes(value, columnName) {
272
+ this.currentNullBitmapOffset();
273
+ const bytes = coerceBinaryBytes(value, columnName);
274
+ this.writer.writeUint32(bytes.length);
275
+ this.writer.writeBytes(bytes);
276
+ }
277
+ writeValue(columnIndex, value) {
278
+ const column = this.column(columnIndex);
279
+ if (value == null) {
280
+ this.writeNull(columnIndex);
281
+ return;
282
+ }
283
+ this.currentNullBitmapOffset();
284
+ writeBinarySnapshotValue(this.writer, column, value);
285
+ }
286
+ finish() {
287
+ if (this.rowsWritten !== this.expectedRows) {
288
+ throw new Error(`Binary snapshot writer expected ${this.expectedRows} rows, received ${this.rowsWritten}`);
289
+ }
290
+ return this.writer.toUint8Array();
291
+ }
292
+ column(columnIndex) {
293
+ const column = this.columns[columnIndex];
294
+ if (!column) {
295
+ throw new Error(`Binary snapshot column index ${columnIndex} is invalid`);
296
+ }
297
+ return column;
298
+ }
299
+ currentNullBitmapOffset() {
300
+ if (this.nullBitmapOffset == null) {
301
+ throw new Error('Binary snapshot writer has no active row');
302
+ }
303
+ return this.nullBitmapOffset;
304
+ }
305
+ }
306
+ export function decodeBinarySnapshotTable(bytes) {
307
+ const reader = new BinarySnapshotReader(bytes);
308
+ reader.expectMagic(SNAPSHOT_BINARY_TABLE_MAGIC, 'binary snapshot table');
309
+ const version = reader.readUint16('binary snapshot version');
310
+ if (version !== BINARY_TABLE_VERSION) {
311
+ throw new Error(`Unsupported binary snapshot version: ${version}`);
312
+ }
313
+ const flags = reader.readUint16('binary snapshot flags');
314
+ if (flags !== BINARY_TABLE_FLAG_NONE) {
315
+ throw new Error(`Unsupported binary snapshot flags: ${flags}`);
316
+ }
317
+ const table = reader.readString16('binary snapshot table name');
318
+ const columnCount = reader.readUint16('binary snapshot column count');
319
+ const columns = [];
320
+ for (let index = 0; index < columnCount; index += 1) {
321
+ const name = reader.readString16('binary snapshot column name');
322
+ const typeTag = reader.readUint8('binary snapshot column type');
323
+ const type = BINARY_COLUMN_TYPES_BY_TAG.get(typeTag);
324
+ if (!type)
325
+ throw new Error(`Unsupported binary snapshot type tag: ${typeTag}`);
326
+ const columnFlags = reader.readUint8('binary snapshot column flags');
327
+ if (columnFlags & ~BINARY_COLUMN_FLAG_NULLABLE) {
328
+ throw new Error(`Unsupported binary snapshot column flags: ${columnFlags}`);
329
+ }
330
+ columns.push({
331
+ name,
332
+ type,
333
+ ...(columnFlags & BINARY_COLUMN_FLAG_NULLABLE ? { nullable: true } : {}),
334
+ });
335
+ }
336
+ const rowCount = reader.readUint32('binary snapshot row count');
70
337
  const rows = [];
71
- const decoder = new TextDecoder();
72
- const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
73
- let offset = SNAPSHOT_ROW_FRAME_MAGIC.length;
74
- while (offset < bytes.length) {
75
- if (offset + FRAME_LENGTH_BYTES > bytes.length) {
76
- throw new Error('Snapshot chunk payload ended mid-frame header');
77
- }
78
- const payloadLength = view.getUint32(offset, false);
79
- offset += FRAME_LENGTH_BYTES;
80
- if (offset + payloadLength > bytes.length) {
81
- throw new Error('Snapshot chunk payload ended mid-frame body');
82
- }
83
- const payload = bytes.subarray(offset, offset + payloadLength);
84
- offset += payloadLength;
85
- rows.push(JSON.parse(decoder.decode(payload)));
86
- }
87
- return rows;
338
+ const nullBitmapBytes = Math.ceil(columns.length / 8);
339
+ for (let rowIndex = 0; rowIndex < rowCount; rowIndex += 1) {
340
+ const nullBitmap = reader.readBytes(nullBitmapBytes, 'binary snapshot row null bitmap');
341
+ const row = {};
342
+ for (let columnIndex = 0; columnIndex < columns.length; columnIndex += 1) {
343
+ const column = columns[columnIndex];
344
+ const isNull = (nullBitmap[Math.floor(columnIndex / 8)] &
345
+ (1 << (columnIndex % 8))) !==
346
+ 0;
347
+ if (isNull && !column.nullable) {
348
+ throw new Error(`Binary snapshot column ${column.name} is not nullable`);
349
+ }
350
+ row[column.name] = isNull
351
+ ? null
352
+ : reader.readValue(column.type, `binary snapshot ${column.name}`);
353
+ }
354
+ rows.push(row);
355
+ }
356
+ reader.assertDone();
357
+ return { table, columns, rows };
358
+ }
359
+ function writeBinarySnapshotValue(writer, column, value) {
360
+ switch (column.type) {
361
+ case 'string': {
362
+ if (typeof value !== 'string') {
363
+ throw new Error(`Binary snapshot column ${column.name} expected string`);
364
+ }
365
+ writer.writeString32(value, `binary snapshot ${column.name}`);
366
+ return;
367
+ }
368
+ case 'integer':
369
+ writer.writeInt64(value, `binary snapshot ${column.name}`);
370
+ return;
371
+ case 'float': {
372
+ if (typeof value !== 'number') {
373
+ throw new Error(`Binary snapshot column ${column.name} expected number`);
374
+ }
375
+ writer.writeFloat64(value);
376
+ return;
377
+ }
378
+ case 'boolean': {
379
+ if (typeof value !== 'boolean') {
380
+ throw new Error(`Binary snapshot column ${column.name} expected boolean`);
381
+ }
382
+ writer.writeUint8(value ? 1 : 0);
383
+ return;
384
+ }
385
+ case 'json':
386
+ writer.writeString32(normalizeRowJson(value), `binary snapshot ${column.name}`);
387
+ return;
388
+ case 'bytes': {
389
+ const bytes = coerceBinaryBytes(value, column.name);
390
+ writer.writeUint32(bytes.length);
391
+ writer.writeBytes(bytes);
392
+ return;
393
+ }
394
+ }
395
+ }
396
+ function binarySnapshotInt64(value, label) {
397
+ let bigint;
398
+ if (typeof value === 'bigint') {
399
+ bigint = value;
400
+ }
401
+ else if (typeof value === 'number' && Number.isSafeInteger(value)) {
402
+ bigint = BigInt(value);
403
+ }
404
+ else if (typeof value === 'string' && /^-?\d+$/.test(value)) {
405
+ bigint = BigInt(value);
406
+ }
407
+ else {
408
+ throw new Error(`${label} expected a safe integer or bigint`);
409
+ }
410
+ return bigint;
411
+ }
412
+ function binarySnapshotString(value, label) {
413
+ if (typeof value === 'string')
414
+ return value;
415
+ if (value instanceof Date)
416
+ return value.toISOString();
417
+ throw new Error(`${label} expected string`);
418
+ }
419
+ function coerceBinaryBytes(value, columnName) {
420
+ if (value instanceof Uint8Array)
421
+ return value;
422
+ if (value instanceof ArrayBuffer)
423
+ return new Uint8Array(value);
424
+ throw new Error(`Binary snapshot column ${columnName} expected bytes`);
425
+ }
426
+ function assertUint16(value, label) {
427
+ if (!Number.isInteger(value) || value < 0 || value > 0xffff) {
428
+ throw new Error(`${label} exceeds uint16 bounds`);
429
+ }
430
+ }
431
+ function assertUint32(value, label) {
432
+ if (!Number.isInteger(value) || value < 0 || value > MAX_FRAME_BYTE_LENGTH) {
433
+ throw new Error(`${label} exceeds uint32 bounds`);
434
+ }
435
+ }
436
+ function estimateBinarySnapshotTableSize(table) {
437
+ return estimateBinarySnapshotStaticSize(table.table, table.columns, table.rows.length);
438
+ }
439
+ function estimateBinarySnapshotStaticSize(table, columns, rowCount) {
440
+ const columnHeaderBytes = columns.reduce((sum, column) => sum + 4 + column.name.length * 4, 0);
441
+ const rowOverhead = rowCount * Math.max(1, Math.ceil(columns.length / 8));
442
+ const scalarBytes = rowCount * columns.length * 16;
443
+ return (SNAPSHOT_BINARY_TABLE_MAGIC.length +
444
+ 10 +
445
+ table.length * 4 +
446
+ columnHeaderBytes +
447
+ rowOverhead +
448
+ scalarBytes);
449
+ }
450
+ function writeBinarySnapshotTableHeader(writer, table, columns, rowCount) {
451
+ assertUint16(columns.length, 'binary snapshot column count');
452
+ assertUint32(rowCount, 'binary snapshot row count');
453
+ writer.writeBytes(SNAPSHOT_BINARY_TABLE_MAGIC);
454
+ writer.writeUint16(BINARY_TABLE_VERSION);
455
+ writer.writeUint16(BINARY_TABLE_FLAG_NONE);
456
+ writer.writeString16(table, 'binary snapshot table name');
457
+ writer.writeUint16(columns.length);
458
+ for (const column of columns) {
459
+ writer.writeString16(column.name, 'binary snapshot column name');
460
+ writer.writeUint8(BINARY_TYPE_TAGS[column.type]);
461
+ writer.writeUint8(column.nullable ? BINARY_COLUMN_FLAG_NULLABLE : 0);
462
+ }
463
+ writer.writeUint32(rowCount);
464
+ }
465
+ class BinarySnapshotWriter {
466
+ bytes;
467
+ view;
468
+ offset = 0;
469
+ constructor(initialCapacity) {
470
+ this.bytes = new Uint8Array(Math.max(64, initialCapacity));
471
+ this.view = new DataView(this.bytes.buffer, this.bytes.byteOffset, this.bytes.length);
472
+ }
473
+ writeUint8(value) {
474
+ this.ensure(1);
475
+ this.view.setUint8(this.offset, value);
476
+ this.offset += 1;
477
+ }
478
+ writeUint16(value) {
479
+ assertUint16(value, 'binary snapshot uint16');
480
+ this.ensure(2);
481
+ this.view.setUint16(this.offset, value, true);
482
+ this.offset += 2;
483
+ }
484
+ writeUint32(value) {
485
+ assertUint32(value, 'binary snapshot uint32');
486
+ this.ensure(4);
487
+ this.view.setUint32(this.offset, value, true);
488
+ this.offset += 4;
489
+ }
490
+ writeInt64(value, label) {
491
+ this.ensure(8);
492
+ if (typeof value === 'number') {
493
+ if (!Number.isSafeInteger(value)) {
494
+ throw new Error(`${label} expected a safe integer or bigint`);
495
+ }
496
+ if (value >= 0) {
497
+ this.view.setUint32(this.offset, value >>> 0, true);
498
+ this.view.setUint32(this.offset + 4, Math.floor(value / 0x1_0000_0000), true);
499
+ }
500
+ else {
501
+ this.view.setBigInt64(this.offset, BigInt(value), true);
502
+ }
503
+ }
504
+ else {
505
+ this.view.setBigInt64(this.offset, binarySnapshotInt64(value, label), true);
506
+ }
507
+ this.offset += 8;
508
+ }
509
+ writeFloat64(value) {
510
+ this.ensure(8);
511
+ this.view.setFloat64(this.offset, value, true);
512
+ this.offset += 8;
513
+ }
514
+ writeString16(value, label) {
515
+ if (this.writeAsciiString16(value, label))
516
+ return;
517
+ const bytes = snapshotTextEncoder.encode(value);
518
+ assertUint16(bytes.length, label);
519
+ this.writeUint16(bytes.length);
520
+ this.writeBytes(bytes);
521
+ }
522
+ writeString32(value, label) {
523
+ if (this.writeAsciiString32(value, label))
524
+ return;
525
+ const bytes = snapshotTextEncoder.encode(value);
526
+ assertUint32(bytes.length, label);
527
+ this.writeUint32(bytes.length);
528
+ this.writeBytes(bytes);
529
+ }
530
+ writeBytes(bytes) {
531
+ this.ensure(bytes.length);
532
+ this.bytes.set(bytes, this.offset);
533
+ this.offset += bytes.length;
534
+ }
535
+ writeZeroes(length) {
536
+ this.ensure(length);
537
+ const start = this.offset;
538
+ this.bytes.fill(0, start, start + length);
539
+ this.offset += length;
540
+ return start;
541
+ }
542
+ readUint8(offset) {
543
+ return this.view.getUint8(offset);
544
+ }
545
+ patchUint8(offset, value) {
546
+ this.view.setUint8(offset, value);
547
+ }
548
+ toUint8Array() {
549
+ return this.bytes.subarray(0, this.offset);
550
+ }
551
+ ensure(length) {
552
+ const required = this.offset + length;
553
+ if (required <= this.bytes.length)
554
+ return;
555
+ let nextLength = this.bytes.length;
556
+ while (nextLength < required) {
557
+ nextLength *= 2;
558
+ }
559
+ const next = new Uint8Array(nextLength);
560
+ next.set(this.bytes, 0);
561
+ this.bytes = next;
562
+ this.view = new DataView(this.bytes.buffer, this.bytes.byteOffset, this.bytes.length);
563
+ }
564
+ writeAsciiString16(value, label) {
565
+ assertUint16(value.length, label);
566
+ const start = this.offset;
567
+ this.writeUint16(value.length);
568
+ return this.tryWriteAsciiBytes(value, start);
569
+ }
570
+ writeAsciiString32(value, label) {
571
+ assertUint32(value.length, label);
572
+ const start = this.offset;
573
+ this.writeUint32(value.length);
574
+ return this.tryWriteAsciiBytes(value, start);
575
+ }
576
+ tryWriteAsciiBytes(value, rollbackOffset) {
577
+ this.ensure(value.length);
578
+ for (let index = 0; index < value.length; index += 1) {
579
+ const code = value.charCodeAt(index);
580
+ if (code > 0x7f) {
581
+ this.offset = rollbackOffset;
582
+ return false;
583
+ }
584
+ this.bytes[this.offset + index] = code;
585
+ }
586
+ this.offset += value.length;
587
+ return true;
588
+ }
589
+ }
590
+ class BinarySnapshotReader {
591
+ bytes;
592
+ view;
593
+ offset = 0;
594
+ constructor(bytes) {
595
+ this.bytes = bytes;
596
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
597
+ }
598
+ expectMagic(magic, label) {
599
+ const actual = this.readBytes(magic.length, `${label} magic`);
600
+ for (let index = 0; index < magic.length; index += 1) {
601
+ if (actual[index] !== magic[index])
602
+ throw new Error(`Unexpected ${label} magic`);
603
+ }
604
+ }
605
+ readUint8(label) {
606
+ this.require(1, label);
607
+ const value = this.view.getUint8(this.offset);
608
+ this.offset += 1;
609
+ return value;
610
+ }
611
+ readUint16(label) {
612
+ this.require(2, label);
613
+ const value = this.view.getUint16(this.offset, true);
614
+ this.offset += 2;
615
+ return value;
616
+ }
617
+ readUint32(label) {
618
+ this.require(4, label);
619
+ const value = this.view.getUint32(this.offset, true);
620
+ this.offset += 4;
621
+ return value;
622
+ }
623
+ readString16(label) {
624
+ const length = this.readUint16(`${label} length`);
625
+ return snapshotTextDecoder.decode(this.readBytes(length, label));
626
+ }
627
+ readString32(label) {
628
+ const length = this.readUint32(`${label} length`);
629
+ return snapshotTextDecoder.decode(this.readBytes(length, label));
630
+ }
631
+ readBytes(length, label) {
632
+ this.require(length, label);
633
+ const value = this.bytes.subarray(this.offset, this.offset + length);
634
+ this.offset += length;
635
+ return value;
636
+ }
637
+ readValue(type, label) {
638
+ switch (type) {
639
+ case 'string':
640
+ return this.readString32(label);
641
+ case 'integer': {
642
+ this.require(8, label);
643
+ const value = this.view.getBigInt64(this.offset, true);
644
+ this.offset += 8;
645
+ const numberValue = Number(value);
646
+ return Number.isSafeInteger(numberValue) &&
647
+ BigInt(numberValue) === value
648
+ ? numberValue
649
+ : value;
650
+ }
651
+ case 'float': {
652
+ this.require(8, label);
653
+ const value = this.view.getFloat64(this.offset, true);
654
+ this.offset += 8;
655
+ return value;
656
+ }
657
+ case 'boolean': {
658
+ const value = this.readUint8(label);
659
+ if (value !== 0 && value !== 1)
660
+ throw new Error(`${label} expected boolean byte`);
661
+ return value === 1;
662
+ }
663
+ case 'json':
664
+ return JSON.parse(this.readString32(label));
665
+ case 'bytes': {
666
+ const length = this.readUint32(`${label} length`);
667
+ return this.readBytes(length, label);
668
+ }
669
+ }
670
+ }
671
+ assertDone() {
672
+ if (this.offset !== this.bytes.length) {
673
+ throw new Error('Binary snapshot payload has trailing bytes');
674
+ }
675
+ }
676
+ require(length, label) {
677
+ if (this.offset + length > this.bytes.length) {
678
+ throw new Error(`${label} exceeds binary snapshot payload bounds`);
679
+ }
680
+ }
88
681
  }
89
682
  //# sourceMappingURL=snapshot-chunks.js.map