@syncular/core 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/README.md +5 -19
  2. package/dist/blob-ref.d.ts +21 -0
  3. package/dist/blob-ref.js +97 -0
  4. package/dist/bytes.d.ts +45 -0
  5. package/dist/bytes.js +242 -0
  6. package/dist/canonical-json.d.ts +10 -0
  7. package/dist/canonical-json.js +18 -0
  8. package/dist/constants.d.ts +5 -0
  9. package/dist/constants.js +5 -0
  10. package/dist/crypto.d.ts +60 -0
  11. package/dist/crypto.js +251 -0
  12. package/dist/errors.d.ts +14 -0
  13. package/dist/errors.js +17 -0
  14. package/dist/frames.d.ts +24 -0
  15. package/dist/frames.js +42 -0
  16. package/dist/index.d.ts +18 -26
  17. package/dist/index.js +18 -43
  18. package/dist/message.d.ts +157 -0
  19. package/dist/message.js +786 -0
  20. package/dist/realtime.d.ts +92 -0
  21. package/dist/realtime.js +222 -0
  22. package/dist/render.d.ts +22 -0
  23. package/dist/render.js +273 -0
  24. package/dist/row-codec.d.ts +39 -0
  25. package/dist/row-codec.js +176 -0
  26. package/dist/segment.d.ts +18 -0
  27. package/dist/segment.js +105 -0
  28. package/dist/stream.d.ts +20 -0
  29. package/dist/stream.js +102 -0
  30. package/package.json +7 -59
  31. package/src/blob-ref.ts +135 -0
  32. package/src/bytes.ts +301 -0
  33. package/src/canonical-json.ts +20 -0
  34. package/src/constants.ts +7 -0
  35. package/src/crypto.ts +335 -0
  36. package/src/errors.ts +18 -0
  37. package/src/frames.ts +47 -0
  38. package/src/index.ts +18 -43
  39. package/src/message.ts +1024 -0
  40. package/src/realtime.ts +340 -0
  41. package/src/render.ts +325 -0
  42. package/src/row-codec.ts +265 -0
  43. package/src/segment.ts +150 -0
  44. package/src/stream.ts +128 -0
  45. package/dist/blobs.d.ts +0 -153
  46. package/dist/blobs.d.ts.map +0 -1
  47. package/dist/blobs.js +0 -47
  48. package/dist/blobs.js.map +0 -1
  49. package/dist/column-codecs.d.ts +0 -59
  50. package/dist/column-codecs.d.ts.map +0 -1
  51. package/dist/column-codecs.js +0 -159
  52. package/dist/column-codecs.js.map +0 -1
  53. package/dist/conflict.d.ts +0 -22
  54. package/dist/conflict.d.ts.map +0 -1
  55. package/dist/conflict.js +0 -81
  56. package/dist/conflict.js.map +0 -1
  57. package/dist/database.d.ts +0 -7
  58. package/dist/database.d.ts.map +0 -1
  59. package/dist/database.js +0 -14
  60. package/dist/database.js.map +0 -1
  61. package/dist/dialect.d.ts +0 -7
  62. package/dist/dialect.d.ts.map +0 -1
  63. package/dist/dialect.js +0 -2
  64. package/dist/dialect.js.map +0 -1
  65. package/dist/error-responses.d.ts +0 -393
  66. package/dist/error-responses.d.ts.map +0 -1
  67. package/dist/error-responses.js +0 -393
  68. package/dist/error-responses.js.map +0 -1
  69. package/dist/http/api-client.d.ts +0 -11
  70. package/dist/http/api-client.d.ts.map +0 -1
  71. package/dist/http/api-client.js +0 -30
  72. package/dist/http/api-client.js.map +0 -1
  73. package/dist/http/blob.d.ts +0 -6
  74. package/dist/http/blob.d.ts.map +0 -1
  75. package/dist/http/blob.js +0 -46
  76. package/dist/http/blob.js.map +0 -1
  77. package/dist/http/generated/api.d.ts +0 -5241
  78. package/dist/http/generated/api.d.ts.map +0 -1
  79. package/dist/http/generated/api.js +0 -6
  80. package/dist/http/generated/api.js.map +0 -1
  81. package/dist/http/index.d.ts +0 -18
  82. package/dist/http/index.d.ts.map +0 -1
  83. package/dist/http/index.js +0 -212
  84. package/dist/http/index.js.map +0 -1
  85. package/dist/http/shared.d.ts +0 -45
  86. package/dist/http/shared.d.ts.map +0 -1
  87. package/dist/http/shared.js +0 -122
  88. package/dist/http/shared.js.map +0 -1
  89. package/dist/http/transport-client.d.ts +0 -31
  90. package/dist/http/transport-client.d.ts.map +0 -1
  91. package/dist/http/transport-client.js +0 -193
  92. package/dist/http/transport-client.js.map +0 -1
  93. package/dist/index.d.ts.map +0 -1
  94. package/dist/index.js.map +0 -1
  95. package/dist/kysely-column-codecs.d.ts +0 -31
  96. package/dist/kysely-column-codecs.d.ts.map +0 -1
  97. package/dist/kysely-column-codecs.js +0 -452
  98. package/dist/kysely-column-codecs.js.map +0 -1
  99. package/dist/logger.d.ts +0 -29
  100. package/dist/logger.d.ts.map +0 -1
  101. package/dist/logger.js +0 -26
  102. package/dist/logger.js.map +0 -1
  103. package/dist/proxy/index.d.ts +0 -54
  104. package/dist/proxy/index.d.ts.map +0 -1
  105. package/dist/proxy/index.js +0 -7
  106. package/dist/proxy/index.js.map +0 -1
  107. package/dist/schemas/blobs.d.ts +0 -82
  108. package/dist/schemas/blobs.d.ts.map +0 -1
  109. package/dist/schemas/blobs.js +0 -64
  110. package/dist/schemas/blobs.js.map +0 -1
  111. package/dist/schemas/common.d.ts +0 -100
  112. package/dist/schemas/common.d.ts.map +0 -1
  113. package/dist/schemas/common.js +0 -64
  114. package/dist/schemas/common.js.map +0 -1
  115. package/dist/schemas/index.d.ts +0 -7
  116. package/dist/schemas/index.d.ts.map +0 -1
  117. package/dist/schemas/index.js +0 -7
  118. package/dist/schemas/index.js.map +0 -1
  119. package/dist/schemas/sync.d.ts +0 -1065
  120. package/dist/schemas/sync.d.ts.map +0 -1
  121. package/dist/schemas/sync.js +0 -361
  122. package/dist/schemas/sync.js.map +0 -1
  123. package/dist/scopes/index.d.ts +0 -113
  124. package/dist/scopes/index.d.ts.map +0 -1
  125. package/dist/scopes/index.js +0 -103
  126. package/dist/scopes/index.js.map +0 -1
  127. package/dist/sentry.d.ts +0 -31
  128. package/dist/sentry.d.ts.map +0 -1
  129. package/dist/sentry.js +0 -147
  130. package/dist/sentry.js.map +0 -1
  131. package/dist/snapshot-chunks.d.ts +0 -110
  132. package/dist/snapshot-chunks.d.ts.map +0 -1
  133. package/dist/snapshot-chunks.js +0 -682
  134. package/dist/snapshot-chunks.js.map +0 -1
  135. package/dist/sync-packs.d.ts +0 -33
  136. package/dist/sync-packs.d.ts.map +0 -1
  137. package/dist/sync-packs.js +0 -821
  138. package/dist/sync-packs.js.map +0 -1
  139. package/dist/telemetry.d.ts +0 -114
  140. package/dist/telemetry.d.ts.map +0 -1
  141. package/dist/telemetry.js +0 -114
  142. package/dist/telemetry.js.map +0 -1
  143. package/dist/transforms.d.ts +0 -146
  144. package/dist/transforms.d.ts.map +0 -1
  145. package/dist/transforms.js +0 -155
  146. package/dist/transforms.js.map +0 -1
  147. package/dist/types.d.ts +0 -210
  148. package/dist/types.d.ts.map +0 -1
  149. package/dist/types.js +0 -20
  150. package/dist/types.js.map +0 -1
  151. package/dist/utils/bytes.d.ts +0 -4
  152. package/dist/utils/bytes.d.ts.map +0 -1
  153. package/dist/utils/bytes.js +0 -66
  154. package/dist/utils/bytes.js.map +0 -1
  155. package/dist/utils/compression.d.ts +0 -22
  156. package/dist/utils/compression.d.ts.map +0 -1
  157. package/dist/utils/compression.js +0 -129
  158. package/dist/utils/compression.js.map +0 -1
  159. package/dist/utils/crypto.d.ts +0 -13
  160. package/dist/utils/crypto.d.ts.map +0 -1
  161. package/dist/utils/crypto.js +0 -107
  162. package/dist/utils/crypto.js.map +0 -1
  163. package/dist/utils/id.d.ts +0 -3
  164. package/dist/utils/id.d.ts.map +0 -1
  165. package/dist/utils/id.js +0 -25
  166. package/dist/utils/id.js.map +0 -1
  167. package/dist/utils/index.d.ts +0 -10
  168. package/dist/utils/index.d.ts.map +0 -1
  169. package/dist/utils/index.js +0 -10
  170. package/dist/utils/index.js.map +0 -1
  171. package/dist/utils/internal-runtime.d.ts +0 -16
  172. package/dist/utils/internal-runtime.d.ts.map +0 -1
  173. package/dist/utils/internal-runtime.js +0 -14
  174. package/dist/utils/internal-runtime.js.map +0 -1
  175. package/dist/utils/object.d.ts +0 -2
  176. package/dist/utils/object.d.ts.map +0 -1
  177. package/dist/utils/object.js +0 -4
  178. package/dist/utils/object.js.map +0 -1
  179. package/dist/utils/realtime-change-index.d.ts +0 -9
  180. package/dist/utils/realtime-change-index.d.ts.map +0 -1
  181. package/dist/utils/realtime-change-index.js +0 -36
  182. package/dist/utils/realtime-change-index.js.map +0 -1
  183. package/dist/utils/realtime-connection-registry.d.ts +0 -40
  184. package/dist/utils/realtime-connection-registry.d.ts.map +0 -1
  185. package/dist/utils/realtime-connection-registry.js +0 -237
  186. package/dist/utils/realtime-connection-registry.js.map +0 -1
  187. package/dist/utils/table-registry.d.ts +0 -10
  188. package/dist/utils/table-registry.d.ts.map +0 -1
  189. package/dist/utils/table-registry.js +0 -54
  190. package/dist/utils/table-registry.js.map +0 -1
  191. package/dist/utils/url.d.ts +0 -3
  192. package/dist/utils/url.d.ts.map +0 -1
  193. package/dist/utils/url.js +0 -20
  194. package/dist/utils/url.js.map +0 -1
  195. package/src/blobs.ts +0 -219
  196. package/src/column-codecs.ts +0 -262
  197. package/src/conflict.ts +0 -92
  198. package/src/database.ts +0 -21
  199. package/src/dialect.ts +0 -8
  200. package/src/error-responses.ts +0 -423
  201. package/src/http/api-client.ts +0 -39
  202. package/src/http/blob.ts +0 -95
  203. package/src/http/generated/api.ts +0 -5241
  204. package/src/http/index.ts +0 -365
  205. package/src/http/shared.ts +0 -220
  206. package/src/http/transport-client.ts +0 -314
  207. package/src/kysely-column-codecs.ts +0 -585
  208. package/src/logger.ts +0 -38
  209. package/src/proxy/index.ts +0 -57
  210. package/src/schemas/blobs.ts +0 -110
  211. package/src/schemas/common.ts +0 -89
  212. package/src/schemas/index.ts +0 -7
  213. package/src/schemas/sync.ts +0 -517
  214. package/src/scopes/index.ts +0 -237
  215. package/src/sentry.ts +0 -200
  216. package/src/snapshot-chunks.ts +0 -964
  217. package/src/sync-packs.ts +0 -1096
  218. package/src/telemetry.ts +0 -243
  219. package/src/transforms.ts +0 -256
  220. package/src/types.ts +0 -267
  221. package/src/utils/bytes.ts +0 -75
  222. package/src/utils/compression.ts +0 -187
  223. package/src/utils/crypto.ts +0 -146
  224. package/src/utils/id.ts +0 -34
  225. package/src/utils/index.ts +0 -9
  226. package/src/utils/internal-runtime.ts +0 -39
  227. package/src/utils/object.ts +0 -3
  228. package/src/utils/realtime-change-index.ts +0 -44
  229. package/src/utils/realtime-connection-registry.ts +0 -286
  230. package/src/utils/table-registry.ts +0 -67
  231. package/src/utils/url.ts +0 -29
@@ -1,682 +0,0 @@
1
- /**
2
- * @syncular/core - Snapshot chunk encoding helpers
3
- */
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
- }
14
- export const SYNC_SNAPSHOT_CHUNK_COMPRESSION = 'gzip';
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;
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
- ]));
37
- function normalizeRowJson(row) {
38
- const serialized = JSON.stringify(row);
39
- return serialized === undefined ? 'null' : serialized;
40
- }
41
- function utf8ByteLength(value) {
42
- return snapshotTextEncoder.encode(value).length;
43
- }
44
- function appendStringField(parts, name, value) {
45
- parts.push(`${name}:s:${utf8ByteLength(value)}:${value}`);
46
- }
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);
94
- }
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);
173
- }
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');
337
- const 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
- }
681
- }
682
- //# sourceMappingURL=snapshot-chunks.js.map