@delali/sirannon-db 0.1.3 → 0.1.5

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 (51) hide show
  1. package/README.md +655 -80
  2. package/dist/backup-scheduler/index.d.ts +3 -0
  3. package/dist/backup-scheduler/index.mjs +2 -0
  4. package/dist/change-tracker-CFTQ9TSn.d.ts +89 -0
  5. package/dist/chunk-3MCMONVP.mjs +115 -0
  6. package/dist/chunk-74UN4DIE.mjs +14 -0
  7. package/dist/chunk-ER7ODTDA.mjs +23 -0
  8. package/dist/chunk-FB2U2Q3Y.mjs +21 -0
  9. package/dist/chunk-GS7T5YMI.mjs +51 -0
  10. package/dist/chunk-O7BHI3CF.mjs +90 -0
  11. package/dist/chunk-PXKAKK2V.mjs +124 -0
  12. package/dist/chunk-UTO3ZAFS.mjs +514 -0
  13. package/dist/chunk-UVMVN3OT.mjs +111 -0
  14. package/dist/client/index.d.ts +137 -44
  15. package/dist/client/index.mjs +726 -26
  16. package/dist/core/index.d.ts +32 -241
  17. package/dist/core/index.mjs +294 -568
  18. package/dist/database-BVY1GqE7.d.ts +95 -0
  19. package/dist/driver/better-sqlite3.d.ts +8 -0
  20. package/dist/driver/better-sqlite3.mjs +63 -0
  21. package/dist/driver/bun.mjs +61 -0
  22. package/dist/driver/expo.mjs +55 -0
  23. package/dist/driver/node.d.ts +8 -0
  24. package/dist/driver/node.mjs +60 -0
  25. package/dist/driver/wa-sqlite.d.ts +34 -0
  26. package/dist/driver/wa-sqlite.mjs +141 -0
  27. package/dist/errors-C00ed08Q.d.ts +101 -0
  28. package/dist/file-migrations/index.d.ts +16 -0
  29. package/dist/file-migrations/index.mjs +128 -0
  30. package/dist/index-CLdNrcPz.d.ts +16 -0
  31. package/dist/replication/coordinator/etcd.d.ts +44 -0
  32. package/dist/replication/coordinator/etcd.mjs +650 -0
  33. package/dist/replication/index.d.ts +491 -0
  34. package/dist/replication/index.mjs +3784 -0
  35. package/dist/server/index.d.ts +121 -54
  36. package/dist/server/index.mjs +347 -114
  37. package/dist/sirannon-Cd-lK6T0.d.ts +31 -0
  38. package/dist/transport/grpc.d.ts +316 -0
  39. package/dist/transport/grpc.mjs +3341 -0
  40. package/dist/transport/memory.d.ts +221 -0
  41. package/dist/transport/memory.mjs +337 -0
  42. package/dist/types-B2byqt0B.d.ts +273 -0
  43. package/dist/types-BEu1I_9_.d.ts +139 -0
  44. package/dist/types-BFSsG77t.d.ts +29 -0
  45. package/dist/types-BeozgNPr.d.ts +26 -0
  46. package/dist/{types-DArCObcu.d.ts → types-D-74JiXb.d.ts} +80 -1
  47. package/dist/vfs-INWQ5DTE.mjs +2 -0
  48. package/package.json +106 -11
  49. package/dist/chunk-VI4UP4RR.mjs +0 -417
  50. package/dist/protocol-BX1H-_Mz.d.ts +0 -104
  51. package/dist/sirannon-BJ8Yd1Uf.d.ts +0 -148
@@ -0,0 +1,3341 @@
1
+ import { TransportError } from '../chunk-UVMVN3OT.mjs';
2
+ import '../chunk-O7BHI3CF.mjs';
3
+ import { makeGenericClientConstructor, Metadata, Server, status, ServerCredentials, credentials } from '@grpc/grpc-js';
4
+ import { BinaryWriter, BinaryReader } from '@bufbuild/protobuf/wire';
5
+ import { readFileSync } from 'fs';
6
+ import { HealthImplementation } from 'grpc-health-check';
7
+
8
+ // src/transport/grpc/codec.ts
9
+ function toColumnValue(value) {
10
+ if (value === null || value === void 0) {
11
+ return { nullValue: true };
12
+ }
13
+ if (typeof value === "string") {
14
+ return { stringValue: value };
15
+ }
16
+ if (typeof value === "bigint") {
17
+ return { intValue: value };
18
+ }
19
+ if (typeof value === "number") {
20
+ if (Number.isInteger(value)) {
21
+ return { intValue: BigInt(value) };
22
+ }
23
+ return { floatValue: value };
24
+ }
25
+ if (typeof value === "boolean") {
26
+ return { boolValue: value };
27
+ }
28
+ if (value instanceof Uint8Array || Buffer.isBuffer(value)) {
29
+ return { blobValue: Buffer.from(value) };
30
+ }
31
+ throw new TypeError(
32
+ `Unsupported value type for ColumnValue conversion: ${typeof value} (${Object.prototype.toString.call(value)})`
33
+ );
34
+ }
35
+ function fromColumnValue(cv) {
36
+ if (cv.nullValue !== void 0) return null;
37
+ if (cv.stringValue !== void 0) return cv.stringValue;
38
+ if (cv.intValue !== void 0) return cv.intValue;
39
+ if (cv.floatValue !== void 0) return cv.floatValue;
40
+ if (cv.blobValue !== void 0) return cv.blobValue;
41
+ if (cv.boolValue !== void 0) return cv.boolValue;
42
+ return null;
43
+ }
44
+ function toRowData(record) {
45
+ if (record === null || record === void 0) return void 0;
46
+ const fields = {};
47
+ for (const [key, value] of Object.entries(record)) {
48
+ fields[key] = toColumnValue(value);
49
+ }
50
+ return { fields };
51
+ }
52
+ function fromRowData(rowData) {
53
+ if (rowData === void 0) return null;
54
+ const result = {};
55
+ for (const [key, cv] of Object.entries(rowData.fields)) {
56
+ result[key] = fromColumnValue(cv);
57
+ }
58
+ return result;
59
+ }
60
+ function toProtoChange(change) {
61
+ return {
62
+ table: change.table,
63
+ operation: change.operation,
64
+ rowId: change.rowId,
65
+ primaryKey: toRowData(change.primaryKey),
66
+ hlc: change.hlc,
67
+ txId: change.txId,
68
+ nodeId: change.nodeId,
69
+ newData: toRowData(change.newData),
70
+ oldData: toRowData(change.oldData),
71
+ ddlStatement: change.ddlStatement ?? ""
72
+ };
73
+ }
74
+ function fromProtoChange(proto) {
75
+ return {
76
+ table: proto.table,
77
+ operation: proto.operation,
78
+ rowId: proto.rowId,
79
+ primaryKey: fromRowData(proto.primaryKey) ?? {},
80
+ hlc: proto.hlc,
81
+ txId: proto.txId,
82
+ nodeId: proto.nodeId,
83
+ newData: fromRowData(proto.newData),
84
+ oldData: fromRowData(proto.oldData),
85
+ ddlStatement: proto.ddlStatement || void 0
86
+ };
87
+ }
88
+ function toBatchPayload(batch) {
89
+ return {
90
+ sourceNodeId: batch.sourceNodeId,
91
+ batchId: batch.batchId,
92
+ fromSeq: batch.fromSeq,
93
+ toSeq: batch.toSeq,
94
+ hlcRange: { min: batch.hlcRange.min, max: batch.hlcRange.max },
95
+ changes: batch.changes.map(toProtoChange),
96
+ checksum: batch.checksum,
97
+ groupId: batch.groupId ?? "",
98
+ primaryTerm: batch.primaryTerm ?? 0n
99
+ };
100
+ }
101
+ function fromBatchPayload(payload) {
102
+ return {
103
+ sourceNodeId: payload.sourceNodeId,
104
+ batchId: payload.batchId,
105
+ fromSeq: payload.fromSeq,
106
+ toSeq: payload.toSeq,
107
+ hlcRange: {
108
+ min: payload.hlcRange?.min ?? "",
109
+ max: payload.hlcRange?.max ?? ""
110
+ },
111
+ changes: payload.changes.map(fromProtoChange),
112
+ checksum: payload.checksum,
113
+ groupId: payload.groupId || void 0,
114
+ primaryTerm: payload.primaryTerm === 0n ? void 0 : payload.primaryTerm
115
+ };
116
+ }
117
+ function toAckPayload(ack) {
118
+ return {
119
+ batchId: ack.batchId,
120
+ ackedSeq: ack.ackedSeq,
121
+ nodeId: ack.nodeId,
122
+ groupId: ack.groupId ?? "",
123
+ primaryTerm: ack.primaryTerm ?? 0n
124
+ };
125
+ }
126
+ function fromAckPayload(payload) {
127
+ return {
128
+ batchId: payload.batchId,
129
+ ackedSeq: payload.ackedSeq,
130
+ nodeId: payload.nodeId,
131
+ groupId: payload.groupId || void 0,
132
+ primaryTerm: payload.primaryTerm === 0n ? void 0 : payload.primaryTerm
133
+ };
134
+ }
135
+ function toSyncRequestPayload(req) {
136
+ return {
137
+ requestId: req.requestId,
138
+ joinerNodeId: req.joinerNodeId,
139
+ completedTables: req.completedTables,
140
+ groupId: req.groupId ?? "",
141
+ primaryTerm: req.primaryTerm ?? 0n
142
+ };
143
+ }
144
+ function fromSyncRequestPayload(p) {
145
+ return {
146
+ requestId: p.requestId,
147
+ joinerNodeId: p.joinerNodeId,
148
+ completedTables: p.completedTables,
149
+ groupId: p.groupId || void 0,
150
+ primaryTerm: p.primaryTerm === 0n ? void 0 : p.primaryTerm
151
+ };
152
+ }
153
+ function toSyncBatchPayload(batch) {
154
+ return {
155
+ requestId: batch.requestId,
156
+ table: batch.table,
157
+ batchIndex: batch.batchIndex,
158
+ rows: batch.rows.map((row) => toRowData(row) ?? { fields: {} }),
159
+ schema: batch.schema ?? [],
160
+ checksum: batch.checksum,
161
+ isLastBatchForTable: batch.isLastBatchForTable,
162
+ groupId: batch.groupId ?? "",
163
+ primaryTerm: batch.primaryTerm ?? 0n
164
+ };
165
+ }
166
+ function fromSyncBatchPayload(p) {
167
+ return {
168
+ requestId: p.requestId,
169
+ table: p.table,
170
+ batchIndex: p.batchIndex,
171
+ rows: p.rows.map((r) => fromRowData(r) ?? {}),
172
+ schema: p.schema.length > 0 ? p.schema : void 0,
173
+ checksum: p.checksum,
174
+ isLastBatchForTable: p.isLastBatchForTable,
175
+ groupId: p.groupId || void 0,
176
+ primaryTerm: p.primaryTerm === 0n ? void 0 : p.primaryTerm
177
+ };
178
+ }
179
+ function toSyncCompletePayload(complete) {
180
+ return {
181
+ requestId: complete.requestId,
182
+ snapshotSeq: complete.snapshotSeq,
183
+ manifests: complete.manifests.map((m) => ({
184
+ table: m.table,
185
+ rowCount: m.rowCount,
186
+ pkHash: m.pkHash
187
+ })),
188
+ groupId: complete.groupId ?? "",
189
+ primaryTerm: complete.primaryTerm ?? 0n
190
+ };
191
+ }
192
+ function fromSyncCompletePayload(p) {
193
+ return {
194
+ requestId: p.requestId,
195
+ snapshotSeq: p.snapshotSeq,
196
+ manifests: p.manifests.map(
197
+ (m) => ({
198
+ table: m.table,
199
+ rowCount: m.rowCount,
200
+ pkHash: m.pkHash
201
+ })
202
+ ),
203
+ groupId: p.groupId || void 0,
204
+ primaryTerm: p.primaryTerm === 0n ? void 0 : p.primaryTerm
205
+ };
206
+ }
207
+ function toSyncAckPayload(ack) {
208
+ return {
209
+ requestId: ack.requestId,
210
+ joinerNodeId: ack.joinerNodeId,
211
+ table: ack.table,
212
+ batchIndex: ack.batchIndex,
213
+ success: ack.success,
214
+ error: ack.error ?? "",
215
+ groupId: ack.groupId ?? "",
216
+ primaryTerm: ack.primaryTerm ?? 0n
217
+ };
218
+ }
219
+ function fromSyncAckPayload(p) {
220
+ return {
221
+ requestId: p.requestId,
222
+ joinerNodeId: p.joinerNodeId,
223
+ table: p.table,
224
+ batchIndex: p.batchIndex,
225
+ success: p.success,
226
+ error: p.error || void 0,
227
+ groupId: p.groupId || void 0,
228
+ primaryTerm: p.primaryTerm === 0n ? void 0 : p.primaryTerm
229
+ };
230
+ }
231
+ function toForwardRequest(req) {
232
+ const statements = req.statements.map((s) => {
233
+ const namedParams = {};
234
+ const positionalParams = [];
235
+ if (s.params !== void 0 && s.params !== null) {
236
+ if (Array.isArray(s.params)) {
237
+ for (const v of s.params) {
238
+ positionalParams.push(toColumnValue(v));
239
+ }
240
+ } else {
241
+ for (const [k, v] of Object.entries(s.params)) {
242
+ namedParams[k] = toColumnValue(v);
243
+ }
244
+ }
245
+ }
246
+ return { sql: s.sql, namedParams, positionalParams };
247
+ });
248
+ return { requestId: req.requestId, statements, groupId: req.groupId ?? "", primaryTerm: req.primaryTerm ?? 0n };
249
+ }
250
+ function fromForwardRequest(proto) {
251
+ const statements = proto.statements.map((s) => {
252
+ const hasNamed = Object.keys(s.namedParams).length > 0;
253
+ const hasPositional = s.positionalParams.length > 0;
254
+ let params;
255
+ if (hasNamed) {
256
+ const named = {};
257
+ for (const [k, cv] of Object.entries(s.namedParams)) {
258
+ named[k] = fromColumnValue(cv);
259
+ }
260
+ params = named;
261
+ } else if (hasPositional) {
262
+ params = s.positionalParams.map(fromColumnValue);
263
+ }
264
+ return { sql: s.sql, params };
265
+ });
266
+ return {
267
+ requestId: proto.requestId,
268
+ statements,
269
+ groupId: proto.groupId || void 0,
270
+ primaryTerm: proto.primaryTerm === 0n ? void 0 : proto.primaryTerm
271
+ };
272
+ }
273
+ function createBaseColumnValue() {
274
+ return {
275
+ nullValue: void 0,
276
+ stringValue: void 0,
277
+ intValue: void 0,
278
+ floatValue: void 0,
279
+ blobValue: void 0,
280
+ boolValue: void 0
281
+ };
282
+ }
283
+ var ColumnValue = {
284
+ encode(message, writer = new BinaryWriter()) {
285
+ if (message.nullValue !== void 0) {
286
+ writer.uint32(8).bool(message.nullValue);
287
+ }
288
+ if (message.stringValue !== void 0) {
289
+ writer.uint32(18).string(message.stringValue);
290
+ }
291
+ if (message.intValue !== void 0) {
292
+ if (BigInt.asIntN(64, message.intValue) !== message.intValue) {
293
+ throw new globalThis.Error("value provided for field message.intValue of type int64 too large");
294
+ }
295
+ writer.uint32(24).int64(message.intValue);
296
+ }
297
+ if (message.floatValue !== void 0) {
298
+ writer.uint32(33).double(message.floatValue);
299
+ }
300
+ if (message.blobValue !== void 0) {
301
+ writer.uint32(42).bytes(message.blobValue);
302
+ }
303
+ if (message.boolValue !== void 0) {
304
+ writer.uint32(48).bool(message.boolValue);
305
+ }
306
+ return writer;
307
+ },
308
+ decode(input, length) {
309
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
310
+ const end = length === void 0 ? reader.len : reader.pos + length;
311
+ const message = createBaseColumnValue();
312
+ while (reader.pos < end) {
313
+ const tag = reader.uint32();
314
+ switch (tag >>> 3) {
315
+ case 1: {
316
+ if (tag !== 8) {
317
+ break;
318
+ }
319
+ message.nullValue = reader.bool();
320
+ continue;
321
+ }
322
+ case 2: {
323
+ if (tag !== 18) {
324
+ break;
325
+ }
326
+ message.stringValue = reader.string();
327
+ continue;
328
+ }
329
+ case 3: {
330
+ if (tag !== 24) {
331
+ break;
332
+ }
333
+ message.intValue = reader.int64();
334
+ continue;
335
+ }
336
+ case 4: {
337
+ if (tag !== 33) {
338
+ break;
339
+ }
340
+ message.floatValue = reader.double();
341
+ continue;
342
+ }
343
+ case 5: {
344
+ if (tag !== 42) {
345
+ break;
346
+ }
347
+ message.blobValue = Buffer.from(reader.bytes());
348
+ continue;
349
+ }
350
+ case 6: {
351
+ if (tag !== 48) {
352
+ break;
353
+ }
354
+ message.boolValue = reader.bool();
355
+ continue;
356
+ }
357
+ }
358
+ if ((tag & 7) === 4 || tag === 0) {
359
+ break;
360
+ }
361
+ reader.skip(tag & 7);
362
+ }
363
+ return message;
364
+ },
365
+ fromJSON(object) {
366
+ return {
367
+ nullValue: isSet(object.nullValue) ? globalThis.Boolean(object.nullValue) : isSet(object.null_value) ? globalThis.Boolean(object.null_value) : void 0,
368
+ stringValue: isSet(object.stringValue) ? globalThis.String(object.stringValue) : isSet(object.string_value) ? globalThis.String(object.string_value) : void 0,
369
+ intValue: isSet(object.intValue) ? BigInt(object.intValue) : isSet(object.int_value) ? BigInt(object.int_value) : void 0,
370
+ floatValue: isSet(object.floatValue) ? globalThis.Number(object.floatValue) : isSet(object.float_value) ? globalThis.Number(object.float_value) : void 0,
371
+ blobValue: isSet(object.blobValue) ? Buffer.from(bytesFromBase64(object.blobValue)) : isSet(object.blob_value) ? Buffer.from(bytesFromBase64(object.blob_value)) : void 0,
372
+ boolValue: isSet(object.boolValue) ? globalThis.Boolean(object.boolValue) : isSet(object.bool_value) ? globalThis.Boolean(object.bool_value) : void 0
373
+ };
374
+ },
375
+ toJSON(message) {
376
+ const obj = {};
377
+ if (message.nullValue !== void 0) {
378
+ obj.nullValue = message.nullValue;
379
+ }
380
+ if (message.stringValue !== void 0) {
381
+ obj.stringValue = message.stringValue;
382
+ }
383
+ if (message.intValue !== void 0) {
384
+ obj.intValue = message.intValue.toString();
385
+ }
386
+ if (message.floatValue !== void 0) {
387
+ obj.floatValue = message.floatValue;
388
+ }
389
+ if (message.blobValue !== void 0) {
390
+ obj.blobValue = base64FromBytes(message.blobValue);
391
+ }
392
+ if (message.boolValue !== void 0) {
393
+ obj.boolValue = message.boolValue;
394
+ }
395
+ return obj;
396
+ },
397
+ create(base) {
398
+ return ColumnValue.fromPartial(base ?? {});
399
+ },
400
+ fromPartial(object) {
401
+ const message = createBaseColumnValue();
402
+ message.nullValue = object.nullValue ?? void 0;
403
+ message.stringValue = object.stringValue ?? void 0;
404
+ message.intValue = object.intValue ?? void 0;
405
+ message.floatValue = object.floatValue ?? void 0;
406
+ message.blobValue = object.blobValue ?? void 0;
407
+ message.boolValue = object.boolValue ?? void 0;
408
+ return message;
409
+ }
410
+ };
411
+ function createBaseRowData() {
412
+ return { fields: {} };
413
+ }
414
+ var RowData = {
415
+ encode(message, writer = new BinaryWriter()) {
416
+ globalThis.Object.entries(message.fields).forEach(([key, value]) => {
417
+ RowData_FieldsEntry.encode({ key, value }, writer.uint32(10).fork()).join();
418
+ });
419
+ return writer;
420
+ },
421
+ decode(input, length) {
422
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
423
+ const end = length === void 0 ? reader.len : reader.pos + length;
424
+ const message = createBaseRowData();
425
+ while (reader.pos < end) {
426
+ const tag = reader.uint32();
427
+ switch (tag >>> 3) {
428
+ case 1: {
429
+ if (tag !== 10) {
430
+ break;
431
+ }
432
+ const entry1 = RowData_FieldsEntry.decode(reader, reader.uint32());
433
+ if (entry1.value !== void 0) {
434
+ message.fields[entry1.key] = entry1.value;
435
+ }
436
+ continue;
437
+ }
438
+ }
439
+ if ((tag & 7) === 4 || tag === 0) {
440
+ break;
441
+ }
442
+ reader.skip(tag & 7);
443
+ }
444
+ return message;
445
+ },
446
+ fromJSON(object) {
447
+ return {
448
+ fields: isObject(object.fields) ? globalThis.Object.entries(object.fields).reduce(
449
+ (acc, [key, value]) => {
450
+ acc[key] = ColumnValue.fromJSON(value);
451
+ return acc;
452
+ },
453
+ {}
454
+ ) : {}
455
+ };
456
+ },
457
+ toJSON(message) {
458
+ const obj = {};
459
+ if (message.fields) {
460
+ const entries = globalThis.Object.entries(message.fields);
461
+ if (entries.length > 0) {
462
+ obj.fields = {};
463
+ entries.forEach(([k, v]) => {
464
+ obj.fields[k] = ColumnValue.toJSON(v);
465
+ });
466
+ }
467
+ }
468
+ return obj;
469
+ },
470
+ create(base) {
471
+ return RowData.fromPartial(base ?? {});
472
+ },
473
+ fromPartial(object) {
474
+ const message = createBaseRowData();
475
+ message.fields = globalThis.Object.entries(object.fields ?? {}).reduce(
476
+ (acc, [key, value]) => {
477
+ if (value !== void 0) {
478
+ acc[key] = ColumnValue.fromPartial(value);
479
+ }
480
+ return acc;
481
+ },
482
+ {}
483
+ );
484
+ return message;
485
+ }
486
+ };
487
+ function createBaseRowData_FieldsEntry() {
488
+ return { key: "", value: void 0 };
489
+ }
490
+ var RowData_FieldsEntry = {
491
+ encode(message, writer = new BinaryWriter()) {
492
+ if (message.key !== "") {
493
+ writer.uint32(10).string(message.key);
494
+ }
495
+ if (message.value !== void 0) {
496
+ ColumnValue.encode(message.value, writer.uint32(18).fork()).join();
497
+ }
498
+ return writer;
499
+ },
500
+ decode(input, length) {
501
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
502
+ const end = length === void 0 ? reader.len : reader.pos + length;
503
+ const message = createBaseRowData_FieldsEntry();
504
+ while (reader.pos < end) {
505
+ const tag = reader.uint32();
506
+ switch (tag >>> 3) {
507
+ case 1: {
508
+ if (tag !== 10) {
509
+ break;
510
+ }
511
+ message.key = reader.string();
512
+ continue;
513
+ }
514
+ case 2: {
515
+ if (tag !== 18) {
516
+ break;
517
+ }
518
+ message.value = ColumnValue.decode(reader, reader.uint32());
519
+ continue;
520
+ }
521
+ }
522
+ if ((tag & 7) === 4 || tag === 0) {
523
+ break;
524
+ }
525
+ reader.skip(tag & 7);
526
+ }
527
+ return message;
528
+ },
529
+ fromJSON(object) {
530
+ return {
531
+ key: isSet(object.key) ? globalThis.String(object.key) : "",
532
+ value: isSet(object.value) ? ColumnValue.fromJSON(object.value) : void 0
533
+ };
534
+ },
535
+ toJSON(message) {
536
+ const obj = {};
537
+ if (message.key !== "") {
538
+ obj.key = message.key;
539
+ }
540
+ if (message.value !== void 0) {
541
+ obj.value = ColumnValue.toJSON(message.value);
542
+ }
543
+ return obj;
544
+ },
545
+ create(base) {
546
+ return RowData_FieldsEntry.fromPartial(base ?? {});
547
+ },
548
+ fromPartial(object) {
549
+ const message = createBaseRowData_FieldsEntry();
550
+ message.key = object.key ?? "";
551
+ message.value = object.value !== void 0 && object.value !== null ? ColumnValue.fromPartial(object.value) : void 0;
552
+ return message;
553
+ }
554
+ };
555
+ function createBaseHlcRange() {
556
+ return { min: "", max: "" };
557
+ }
558
+ var HlcRange = {
559
+ encode(message, writer = new BinaryWriter()) {
560
+ if (message.min !== "") {
561
+ writer.uint32(10).string(message.min);
562
+ }
563
+ if (message.max !== "") {
564
+ writer.uint32(18).string(message.max);
565
+ }
566
+ return writer;
567
+ },
568
+ decode(input, length) {
569
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
570
+ const end = length === void 0 ? reader.len : reader.pos + length;
571
+ const message = createBaseHlcRange();
572
+ while (reader.pos < end) {
573
+ const tag = reader.uint32();
574
+ switch (tag >>> 3) {
575
+ case 1: {
576
+ if (tag !== 10) {
577
+ break;
578
+ }
579
+ message.min = reader.string();
580
+ continue;
581
+ }
582
+ case 2: {
583
+ if (tag !== 18) {
584
+ break;
585
+ }
586
+ message.max = reader.string();
587
+ continue;
588
+ }
589
+ }
590
+ if ((tag & 7) === 4 || tag === 0) {
591
+ break;
592
+ }
593
+ reader.skip(tag & 7);
594
+ }
595
+ return message;
596
+ },
597
+ fromJSON(object) {
598
+ return {
599
+ min: isSet(object.min) ? globalThis.String(object.min) : "",
600
+ max: isSet(object.max) ? globalThis.String(object.max) : ""
601
+ };
602
+ },
603
+ toJSON(message) {
604
+ const obj = {};
605
+ if (message.min !== "") {
606
+ obj.min = message.min;
607
+ }
608
+ if (message.max !== "") {
609
+ obj.max = message.max;
610
+ }
611
+ return obj;
612
+ },
613
+ create(base) {
614
+ return HlcRange.fromPartial(base ?? {});
615
+ },
616
+ fromPartial(object) {
617
+ const message = createBaseHlcRange();
618
+ message.min = object.min ?? "";
619
+ message.max = object.max ?? "";
620
+ return message;
621
+ }
622
+ };
623
+ function createBaseReplicationChange() {
624
+ return {
625
+ table: "",
626
+ operation: "",
627
+ rowId: "",
628
+ primaryKey: void 0,
629
+ hlc: "",
630
+ txId: "",
631
+ nodeId: "",
632
+ newData: void 0,
633
+ oldData: void 0,
634
+ ddlStatement: ""
635
+ };
636
+ }
637
+ var ReplicationChange = {
638
+ encode(message, writer = new BinaryWriter()) {
639
+ if (message.table !== "") {
640
+ writer.uint32(10).string(message.table);
641
+ }
642
+ if (message.operation !== "") {
643
+ writer.uint32(18).string(message.operation);
644
+ }
645
+ if (message.rowId !== "") {
646
+ writer.uint32(26).string(message.rowId);
647
+ }
648
+ if (message.primaryKey !== void 0) {
649
+ RowData.encode(message.primaryKey, writer.uint32(34).fork()).join();
650
+ }
651
+ if (message.hlc !== "") {
652
+ writer.uint32(42).string(message.hlc);
653
+ }
654
+ if (message.txId !== "") {
655
+ writer.uint32(50).string(message.txId);
656
+ }
657
+ if (message.nodeId !== "") {
658
+ writer.uint32(58).string(message.nodeId);
659
+ }
660
+ if (message.newData !== void 0) {
661
+ RowData.encode(message.newData, writer.uint32(66).fork()).join();
662
+ }
663
+ if (message.oldData !== void 0) {
664
+ RowData.encode(message.oldData, writer.uint32(74).fork()).join();
665
+ }
666
+ if (message.ddlStatement !== "") {
667
+ writer.uint32(82).string(message.ddlStatement);
668
+ }
669
+ return writer;
670
+ },
671
+ decode(input, length) {
672
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
673
+ const end = length === void 0 ? reader.len : reader.pos + length;
674
+ const message = createBaseReplicationChange();
675
+ while (reader.pos < end) {
676
+ const tag = reader.uint32();
677
+ switch (tag >>> 3) {
678
+ case 1: {
679
+ if (tag !== 10) {
680
+ break;
681
+ }
682
+ message.table = reader.string();
683
+ continue;
684
+ }
685
+ case 2: {
686
+ if (tag !== 18) {
687
+ break;
688
+ }
689
+ message.operation = reader.string();
690
+ continue;
691
+ }
692
+ case 3: {
693
+ if (tag !== 26) {
694
+ break;
695
+ }
696
+ message.rowId = reader.string();
697
+ continue;
698
+ }
699
+ case 4: {
700
+ if (tag !== 34) {
701
+ break;
702
+ }
703
+ message.primaryKey = RowData.decode(reader, reader.uint32());
704
+ continue;
705
+ }
706
+ case 5: {
707
+ if (tag !== 42) {
708
+ break;
709
+ }
710
+ message.hlc = reader.string();
711
+ continue;
712
+ }
713
+ case 6: {
714
+ if (tag !== 50) {
715
+ break;
716
+ }
717
+ message.txId = reader.string();
718
+ continue;
719
+ }
720
+ case 7: {
721
+ if (tag !== 58) {
722
+ break;
723
+ }
724
+ message.nodeId = reader.string();
725
+ continue;
726
+ }
727
+ case 8: {
728
+ if (tag !== 66) {
729
+ break;
730
+ }
731
+ message.newData = RowData.decode(reader, reader.uint32());
732
+ continue;
733
+ }
734
+ case 9: {
735
+ if (tag !== 74) {
736
+ break;
737
+ }
738
+ message.oldData = RowData.decode(reader, reader.uint32());
739
+ continue;
740
+ }
741
+ case 10: {
742
+ if (tag !== 82) {
743
+ break;
744
+ }
745
+ message.ddlStatement = reader.string();
746
+ continue;
747
+ }
748
+ }
749
+ if ((tag & 7) === 4 || tag === 0) {
750
+ break;
751
+ }
752
+ reader.skip(tag & 7);
753
+ }
754
+ return message;
755
+ },
756
+ fromJSON(object) {
757
+ return {
758
+ table: isSet(object.table) ? globalThis.String(object.table) : "",
759
+ operation: isSet(object.operation) ? globalThis.String(object.operation) : "",
760
+ rowId: isSet(object.rowId) ? globalThis.String(object.rowId) : isSet(object.row_id) ? globalThis.String(object.row_id) : "",
761
+ primaryKey: isSet(object.primaryKey) ? RowData.fromJSON(object.primaryKey) : isSet(object.primary_key) ? RowData.fromJSON(object.primary_key) : void 0,
762
+ hlc: isSet(object.hlc) ? globalThis.String(object.hlc) : "",
763
+ txId: isSet(object.txId) ? globalThis.String(object.txId) : isSet(object.tx_id) ? globalThis.String(object.tx_id) : "",
764
+ nodeId: isSet(object.nodeId) ? globalThis.String(object.nodeId) : isSet(object.node_id) ? globalThis.String(object.node_id) : "",
765
+ newData: isSet(object.newData) ? RowData.fromJSON(object.newData) : isSet(object.new_data) ? RowData.fromJSON(object.new_data) : void 0,
766
+ oldData: isSet(object.oldData) ? RowData.fromJSON(object.oldData) : isSet(object.old_data) ? RowData.fromJSON(object.old_data) : void 0,
767
+ ddlStatement: isSet(object.ddlStatement) ? globalThis.String(object.ddlStatement) : isSet(object.ddl_statement) ? globalThis.String(object.ddl_statement) : ""
768
+ };
769
+ },
770
+ toJSON(message) {
771
+ const obj = {};
772
+ if (message.table !== "") {
773
+ obj.table = message.table;
774
+ }
775
+ if (message.operation !== "") {
776
+ obj.operation = message.operation;
777
+ }
778
+ if (message.rowId !== "") {
779
+ obj.rowId = message.rowId;
780
+ }
781
+ if (message.primaryKey !== void 0) {
782
+ obj.primaryKey = RowData.toJSON(message.primaryKey);
783
+ }
784
+ if (message.hlc !== "") {
785
+ obj.hlc = message.hlc;
786
+ }
787
+ if (message.txId !== "") {
788
+ obj.txId = message.txId;
789
+ }
790
+ if (message.nodeId !== "") {
791
+ obj.nodeId = message.nodeId;
792
+ }
793
+ if (message.newData !== void 0) {
794
+ obj.newData = RowData.toJSON(message.newData);
795
+ }
796
+ if (message.oldData !== void 0) {
797
+ obj.oldData = RowData.toJSON(message.oldData);
798
+ }
799
+ if (message.ddlStatement !== "") {
800
+ obj.ddlStatement = message.ddlStatement;
801
+ }
802
+ return obj;
803
+ },
804
+ create(base) {
805
+ return ReplicationChange.fromPartial(base ?? {});
806
+ },
807
+ fromPartial(object) {
808
+ const message = createBaseReplicationChange();
809
+ message.table = object.table ?? "";
810
+ message.operation = object.operation ?? "";
811
+ message.rowId = object.rowId ?? "";
812
+ message.primaryKey = object.primaryKey !== void 0 && object.primaryKey !== null ? RowData.fromPartial(object.primaryKey) : void 0;
813
+ message.hlc = object.hlc ?? "";
814
+ message.txId = object.txId ?? "";
815
+ message.nodeId = object.nodeId ?? "";
816
+ message.newData = object.newData !== void 0 && object.newData !== null ? RowData.fromPartial(object.newData) : void 0;
817
+ message.oldData = object.oldData !== void 0 && object.oldData !== null ? RowData.fromPartial(object.oldData) : void 0;
818
+ message.ddlStatement = object.ddlStatement ?? "";
819
+ return message;
820
+ }
821
+ };
822
+ function createBaseBatchPayload() {
823
+ return {
824
+ sourceNodeId: "",
825
+ batchId: "",
826
+ fromSeq: 0n,
827
+ toSeq: 0n,
828
+ hlcRange: void 0,
829
+ changes: [],
830
+ checksum: "",
831
+ groupId: "",
832
+ primaryTerm: 0n
833
+ };
834
+ }
835
+ var BatchPayload = {
836
+ encode(message, writer = new BinaryWriter()) {
837
+ if (message.sourceNodeId !== "") {
838
+ writer.uint32(10).string(message.sourceNodeId);
839
+ }
840
+ if (message.batchId !== "") {
841
+ writer.uint32(18).string(message.batchId);
842
+ }
843
+ if (message.fromSeq !== 0n) {
844
+ if (BigInt.asIntN(64, message.fromSeq) !== message.fromSeq) {
845
+ throw new globalThis.Error("value provided for field message.fromSeq of type int64 too large");
846
+ }
847
+ writer.uint32(24).int64(message.fromSeq);
848
+ }
849
+ if (message.toSeq !== 0n) {
850
+ if (BigInt.asIntN(64, message.toSeq) !== message.toSeq) {
851
+ throw new globalThis.Error("value provided for field message.toSeq of type int64 too large");
852
+ }
853
+ writer.uint32(32).int64(message.toSeq);
854
+ }
855
+ if (message.hlcRange !== void 0) {
856
+ HlcRange.encode(message.hlcRange, writer.uint32(42).fork()).join();
857
+ }
858
+ for (const v of message.changes) {
859
+ ReplicationChange.encode(v, writer.uint32(50).fork()).join();
860
+ }
861
+ if (message.checksum !== "") {
862
+ writer.uint32(58).string(message.checksum);
863
+ }
864
+ if (message.groupId !== "") {
865
+ writer.uint32(66).string(message.groupId);
866
+ }
867
+ if (message.primaryTerm !== 0n) {
868
+ if (BigInt.asIntN(64, message.primaryTerm) !== message.primaryTerm) {
869
+ throw new globalThis.Error("value provided for field message.primaryTerm of type int64 too large");
870
+ }
871
+ writer.uint32(72).int64(message.primaryTerm);
872
+ }
873
+ return writer;
874
+ },
875
+ decode(input, length) {
876
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
877
+ const end = length === void 0 ? reader.len : reader.pos + length;
878
+ const message = createBaseBatchPayload();
879
+ while (reader.pos < end) {
880
+ const tag = reader.uint32();
881
+ switch (tag >>> 3) {
882
+ case 1: {
883
+ if (tag !== 10) {
884
+ break;
885
+ }
886
+ message.sourceNodeId = reader.string();
887
+ continue;
888
+ }
889
+ case 2: {
890
+ if (tag !== 18) {
891
+ break;
892
+ }
893
+ message.batchId = reader.string();
894
+ continue;
895
+ }
896
+ case 3: {
897
+ if (tag !== 24) {
898
+ break;
899
+ }
900
+ message.fromSeq = reader.int64();
901
+ continue;
902
+ }
903
+ case 4: {
904
+ if (tag !== 32) {
905
+ break;
906
+ }
907
+ message.toSeq = reader.int64();
908
+ continue;
909
+ }
910
+ case 5: {
911
+ if (tag !== 42) {
912
+ break;
913
+ }
914
+ message.hlcRange = HlcRange.decode(reader, reader.uint32());
915
+ continue;
916
+ }
917
+ case 6: {
918
+ if (tag !== 50) {
919
+ break;
920
+ }
921
+ message.changes.push(ReplicationChange.decode(reader, reader.uint32()));
922
+ continue;
923
+ }
924
+ case 7: {
925
+ if (tag !== 58) {
926
+ break;
927
+ }
928
+ message.checksum = reader.string();
929
+ continue;
930
+ }
931
+ case 8: {
932
+ if (tag !== 66) {
933
+ break;
934
+ }
935
+ message.groupId = reader.string();
936
+ continue;
937
+ }
938
+ case 9: {
939
+ if (tag !== 72) {
940
+ break;
941
+ }
942
+ message.primaryTerm = reader.int64();
943
+ continue;
944
+ }
945
+ }
946
+ if ((tag & 7) === 4 || tag === 0) {
947
+ break;
948
+ }
949
+ reader.skip(tag & 7);
950
+ }
951
+ return message;
952
+ },
953
+ fromJSON(object) {
954
+ return {
955
+ sourceNodeId: isSet(object.sourceNodeId) ? globalThis.String(object.sourceNodeId) : isSet(object.source_node_id) ? globalThis.String(object.source_node_id) : "",
956
+ batchId: isSet(object.batchId) ? globalThis.String(object.batchId) : isSet(object.batch_id) ? globalThis.String(object.batch_id) : "",
957
+ fromSeq: isSet(object.fromSeq) ? BigInt(object.fromSeq) : isSet(object.from_seq) ? BigInt(object.from_seq) : 0n,
958
+ toSeq: isSet(object.toSeq) ? BigInt(object.toSeq) : isSet(object.to_seq) ? BigInt(object.to_seq) : 0n,
959
+ hlcRange: isSet(object.hlcRange) ? HlcRange.fromJSON(object.hlcRange) : isSet(object.hlc_range) ? HlcRange.fromJSON(object.hlc_range) : void 0,
960
+ changes: globalThis.Array.isArray(object?.changes) ? object.changes.map((e) => ReplicationChange.fromJSON(e)) : [],
961
+ checksum: isSet(object.checksum) ? globalThis.String(object.checksum) : "",
962
+ groupId: isSet(object.groupId) ? globalThis.String(object.groupId) : isSet(object.group_id) ? globalThis.String(object.group_id) : "",
963
+ primaryTerm: isSet(object.primaryTerm) ? BigInt(object.primaryTerm) : isSet(object.primary_term) ? BigInt(object.primary_term) : 0n
964
+ };
965
+ },
966
+ toJSON(message) {
967
+ const obj = {};
968
+ if (message.sourceNodeId !== "") {
969
+ obj.sourceNodeId = message.sourceNodeId;
970
+ }
971
+ if (message.batchId !== "") {
972
+ obj.batchId = message.batchId;
973
+ }
974
+ if (message.fromSeq !== 0n) {
975
+ obj.fromSeq = message.fromSeq.toString();
976
+ }
977
+ if (message.toSeq !== 0n) {
978
+ obj.toSeq = message.toSeq.toString();
979
+ }
980
+ if (message.hlcRange !== void 0) {
981
+ obj.hlcRange = HlcRange.toJSON(message.hlcRange);
982
+ }
983
+ if (message.changes?.length) {
984
+ obj.changes = message.changes.map((e) => ReplicationChange.toJSON(e));
985
+ }
986
+ if (message.checksum !== "") {
987
+ obj.checksum = message.checksum;
988
+ }
989
+ if (message.groupId !== "") {
990
+ obj.groupId = message.groupId;
991
+ }
992
+ if (message.primaryTerm !== 0n) {
993
+ obj.primaryTerm = message.primaryTerm.toString();
994
+ }
995
+ return obj;
996
+ },
997
+ create(base) {
998
+ return BatchPayload.fromPartial(base ?? {});
999
+ },
1000
+ fromPartial(object) {
1001
+ const message = createBaseBatchPayload();
1002
+ message.sourceNodeId = object.sourceNodeId ?? "";
1003
+ message.batchId = object.batchId ?? "";
1004
+ message.fromSeq = object.fromSeq ?? 0n;
1005
+ message.toSeq = object.toSeq ?? 0n;
1006
+ message.hlcRange = object.hlcRange !== void 0 && object.hlcRange !== null ? HlcRange.fromPartial(object.hlcRange) : void 0;
1007
+ message.changes = object.changes?.map((e) => ReplicationChange.fromPartial(e)) || [];
1008
+ message.checksum = object.checksum ?? "";
1009
+ message.groupId = object.groupId ?? "";
1010
+ message.primaryTerm = object.primaryTerm ?? 0n;
1011
+ return message;
1012
+ }
1013
+ };
1014
+ function createBaseAckPayload() {
1015
+ return { batchId: "", ackedSeq: 0n, nodeId: "", groupId: "", primaryTerm: 0n };
1016
+ }
1017
+ var AckPayload = {
1018
+ encode(message, writer = new BinaryWriter()) {
1019
+ if (message.batchId !== "") {
1020
+ writer.uint32(10).string(message.batchId);
1021
+ }
1022
+ if (message.ackedSeq !== 0n) {
1023
+ if (BigInt.asIntN(64, message.ackedSeq) !== message.ackedSeq) {
1024
+ throw new globalThis.Error("value provided for field message.ackedSeq of type int64 too large");
1025
+ }
1026
+ writer.uint32(16).int64(message.ackedSeq);
1027
+ }
1028
+ if (message.nodeId !== "") {
1029
+ writer.uint32(26).string(message.nodeId);
1030
+ }
1031
+ if (message.groupId !== "") {
1032
+ writer.uint32(34).string(message.groupId);
1033
+ }
1034
+ if (message.primaryTerm !== 0n) {
1035
+ if (BigInt.asIntN(64, message.primaryTerm) !== message.primaryTerm) {
1036
+ throw new globalThis.Error("value provided for field message.primaryTerm of type int64 too large");
1037
+ }
1038
+ writer.uint32(40).int64(message.primaryTerm);
1039
+ }
1040
+ return writer;
1041
+ },
1042
+ decode(input, length) {
1043
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1044
+ const end = length === void 0 ? reader.len : reader.pos + length;
1045
+ const message = createBaseAckPayload();
1046
+ while (reader.pos < end) {
1047
+ const tag = reader.uint32();
1048
+ switch (tag >>> 3) {
1049
+ case 1: {
1050
+ if (tag !== 10) {
1051
+ break;
1052
+ }
1053
+ message.batchId = reader.string();
1054
+ continue;
1055
+ }
1056
+ case 2: {
1057
+ if (tag !== 16) {
1058
+ break;
1059
+ }
1060
+ message.ackedSeq = reader.int64();
1061
+ continue;
1062
+ }
1063
+ case 3: {
1064
+ if (tag !== 26) {
1065
+ break;
1066
+ }
1067
+ message.nodeId = reader.string();
1068
+ continue;
1069
+ }
1070
+ case 4: {
1071
+ if (tag !== 34) {
1072
+ break;
1073
+ }
1074
+ message.groupId = reader.string();
1075
+ continue;
1076
+ }
1077
+ case 5: {
1078
+ if (tag !== 40) {
1079
+ break;
1080
+ }
1081
+ message.primaryTerm = reader.int64();
1082
+ continue;
1083
+ }
1084
+ }
1085
+ if ((tag & 7) === 4 || tag === 0) {
1086
+ break;
1087
+ }
1088
+ reader.skip(tag & 7);
1089
+ }
1090
+ return message;
1091
+ },
1092
+ fromJSON(object) {
1093
+ return {
1094
+ batchId: isSet(object.batchId) ? globalThis.String(object.batchId) : isSet(object.batch_id) ? globalThis.String(object.batch_id) : "",
1095
+ ackedSeq: isSet(object.ackedSeq) ? BigInt(object.ackedSeq) : isSet(object.acked_seq) ? BigInt(object.acked_seq) : 0n,
1096
+ nodeId: isSet(object.nodeId) ? globalThis.String(object.nodeId) : isSet(object.node_id) ? globalThis.String(object.node_id) : "",
1097
+ groupId: isSet(object.groupId) ? globalThis.String(object.groupId) : isSet(object.group_id) ? globalThis.String(object.group_id) : "",
1098
+ primaryTerm: isSet(object.primaryTerm) ? BigInt(object.primaryTerm) : isSet(object.primary_term) ? BigInt(object.primary_term) : 0n
1099
+ };
1100
+ },
1101
+ toJSON(message) {
1102
+ const obj = {};
1103
+ if (message.batchId !== "") {
1104
+ obj.batchId = message.batchId;
1105
+ }
1106
+ if (message.ackedSeq !== 0n) {
1107
+ obj.ackedSeq = message.ackedSeq.toString();
1108
+ }
1109
+ if (message.nodeId !== "") {
1110
+ obj.nodeId = message.nodeId;
1111
+ }
1112
+ if (message.groupId !== "") {
1113
+ obj.groupId = message.groupId;
1114
+ }
1115
+ if (message.primaryTerm !== 0n) {
1116
+ obj.primaryTerm = message.primaryTerm.toString();
1117
+ }
1118
+ return obj;
1119
+ },
1120
+ create(base) {
1121
+ return AckPayload.fromPartial(base ?? {});
1122
+ },
1123
+ fromPartial(object) {
1124
+ const message = createBaseAckPayload();
1125
+ message.batchId = object.batchId ?? "";
1126
+ message.ackedSeq = object.ackedSeq ?? 0n;
1127
+ message.nodeId = object.nodeId ?? "";
1128
+ message.groupId = object.groupId ?? "";
1129
+ message.primaryTerm = object.primaryTerm ?? 0n;
1130
+ return message;
1131
+ }
1132
+ };
1133
+ function createBaseHello() {
1134
+ return { nodeId: "", role: "", groupId: "", primaryTerm: 0n, protocolVersion: "" };
1135
+ }
1136
+ var Hello = {
1137
+ encode(message, writer = new BinaryWriter()) {
1138
+ if (message.nodeId !== "") {
1139
+ writer.uint32(10).string(message.nodeId);
1140
+ }
1141
+ if (message.role !== "") {
1142
+ writer.uint32(18).string(message.role);
1143
+ }
1144
+ if (message.groupId !== "") {
1145
+ writer.uint32(26).string(message.groupId);
1146
+ }
1147
+ if (message.primaryTerm !== 0n) {
1148
+ if (BigInt.asIntN(64, message.primaryTerm) !== message.primaryTerm) {
1149
+ throw new globalThis.Error("value provided for field message.primaryTerm of type int64 too large");
1150
+ }
1151
+ writer.uint32(32).int64(message.primaryTerm);
1152
+ }
1153
+ if (message.protocolVersion !== "") {
1154
+ writer.uint32(42).string(message.protocolVersion);
1155
+ }
1156
+ return writer;
1157
+ },
1158
+ decode(input, length) {
1159
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1160
+ const end = length === void 0 ? reader.len : reader.pos + length;
1161
+ const message = createBaseHello();
1162
+ while (reader.pos < end) {
1163
+ const tag = reader.uint32();
1164
+ switch (tag >>> 3) {
1165
+ case 1: {
1166
+ if (tag !== 10) {
1167
+ break;
1168
+ }
1169
+ message.nodeId = reader.string();
1170
+ continue;
1171
+ }
1172
+ case 2: {
1173
+ if (tag !== 18) {
1174
+ break;
1175
+ }
1176
+ message.role = reader.string();
1177
+ continue;
1178
+ }
1179
+ case 3: {
1180
+ if (tag !== 26) {
1181
+ break;
1182
+ }
1183
+ message.groupId = reader.string();
1184
+ continue;
1185
+ }
1186
+ case 4: {
1187
+ if (tag !== 32) {
1188
+ break;
1189
+ }
1190
+ message.primaryTerm = reader.int64();
1191
+ continue;
1192
+ }
1193
+ case 5: {
1194
+ if (tag !== 42) {
1195
+ break;
1196
+ }
1197
+ message.protocolVersion = reader.string();
1198
+ continue;
1199
+ }
1200
+ }
1201
+ if ((tag & 7) === 4 || tag === 0) {
1202
+ break;
1203
+ }
1204
+ reader.skip(tag & 7);
1205
+ }
1206
+ return message;
1207
+ },
1208
+ fromJSON(object) {
1209
+ return {
1210
+ nodeId: isSet(object.nodeId) ? globalThis.String(object.nodeId) : isSet(object.node_id) ? globalThis.String(object.node_id) : "",
1211
+ role: isSet(object.role) ? globalThis.String(object.role) : "",
1212
+ groupId: isSet(object.groupId) ? globalThis.String(object.groupId) : isSet(object.group_id) ? globalThis.String(object.group_id) : "",
1213
+ primaryTerm: isSet(object.primaryTerm) ? BigInt(object.primaryTerm) : isSet(object.primary_term) ? BigInt(object.primary_term) : 0n,
1214
+ protocolVersion: isSet(object.protocolVersion) ? globalThis.String(object.protocolVersion) : isSet(object.protocol_version) ? globalThis.String(object.protocol_version) : ""
1215
+ };
1216
+ },
1217
+ toJSON(message) {
1218
+ const obj = {};
1219
+ if (message.nodeId !== "") {
1220
+ obj.nodeId = message.nodeId;
1221
+ }
1222
+ if (message.role !== "") {
1223
+ obj.role = message.role;
1224
+ }
1225
+ if (message.groupId !== "") {
1226
+ obj.groupId = message.groupId;
1227
+ }
1228
+ if (message.primaryTerm !== 0n) {
1229
+ obj.primaryTerm = message.primaryTerm.toString();
1230
+ }
1231
+ if (message.protocolVersion !== "") {
1232
+ obj.protocolVersion = message.protocolVersion;
1233
+ }
1234
+ return obj;
1235
+ },
1236
+ create(base) {
1237
+ return Hello.fromPartial(base ?? {});
1238
+ },
1239
+ fromPartial(object) {
1240
+ const message = createBaseHello();
1241
+ message.nodeId = object.nodeId ?? "";
1242
+ message.role = object.role ?? "";
1243
+ message.groupId = object.groupId ?? "";
1244
+ message.primaryTerm = object.primaryTerm ?? 0n;
1245
+ message.protocolVersion = object.protocolVersion ?? "";
1246
+ return message;
1247
+ }
1248
+ };
1249
+ function createBaseReplicationMessage() {
1250
+ return { hello: void 0, batch: void 0, ack: void 0 };
1251
+ }
1252
+ var ReplicationMessage = {
1253
+ encode(message, writer = new BinaryWriter()) {
1254
+ if (message.hello !== void 0) {
1255
+ Hello.encode(message.hello, writer.uint32(10).fork()).join();
1256
+ }
1257
+ if (message.batch !== void 0) {
1258
+ BatchPayload.encode(message.batch, writer.uint32(18).fork()).join();
1259
+ }
1260
+ if (message.ack !== void 0) {
1261
+ AckPayload.encode(message.ack, writer.uint32(26).fork()).join();
1262
+ }
1263
+ return writer;
1264
+ },
1265
+ decode(input, length) {
1266
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1267
+ const end = length === void 0 ? reader.len : reader.pos + length;
1268
+ const message = createBaseReplicationMessage();
1269
+ while (reader.pos < end) {
1270
+ const tag = reader.uint32();
1271
+ switch (tag >>> 3) {
1272
+ case 1: {
1273
+ if (tag !== 10) {
1274
+ break;
1275
+ }
1276
+ message.hello = Hello.decode(reader, reader.uint32());
1277
+ continue;
1278
+ }
1279
+ case 2: {
1280
+ if (tag !== 18) {
1281
+ break;
1282
+ }
1283
+ message.batch = BatchPayload.decode(reader, reader.uint32());
1284
+ continue;
1285
+ }
1286
+ case 3: {
1287
+ if (tag !== 26) {
1288
+ break;
1289
+ }
1290
+ message.ack = AckPayload.decode(reader, reader.uint32());
1291
+ continue;
1292
+ }
1293
+ }
1294
+ if ((tag & 7) === 4 || tag === 0) {
1295
+ break;
1296
+ }
1297
+ reader.skip(tag & 7);
1298
+ }
1299
+ return message;
1300
+ },
1301
+ fromJSON(object) {
1302
+ return {
1303
+ hello: isSet(object.hello) ? Hello.fromJSON(object.hello) : void 0,
1304
+ batch: isSet(object.batch) ? BatchPayload.fromJSON(object.batch) : void 0,
1305
+ ack: isSet(object.ack) ? AckPayload.fromJSON(object.ack) : void 0
1306
+ };
1307
+ },
1308
+ toJSON(message) {
1309
+ const obj = {};
1310
+ if (message.hello !== void 0) {
1311
+ obj.hello = Hello.toJSON(message.hello);
1312
+ }
1313
+ if (message.batch !== void 0) {
1314
+ obj.batch = BatchPayload.toJSON(message.batch);
1315
+ }
1316
+ if (message.ack !== void 0) {
1317
+ obj.ack = AckPayload.toJSON(message.ack);
1318
+ }
1319
+ return obj;
1320
+ },
1321
+ create(base) {
1322
+ return ReplicationMessage.fromPartial(base ?? {});
1323
+ },
1324
+ fromPartial(object) {
1325
+ const message = createBaseReplicationMessage();
1326
+ message.hello = object.hello !== void 0 && object.hello !== null ? Hello.fromPartial(object.hello) : void 0;
1327
+ message.batch = object.batch !== void 0 && object.batch !== null ? BatchPayload.fromPartial(object.batch) : void 0;
1328
+ message.ack = object.ack !== void 0 && object.ack !== null ? AckPayload.fromPartial(object.ack) : void 0;
1329
+ return message;
1330
+ }
1331
+ };
1332
+ function createBaseSyncRequestPayload() {
1333
+ return { requestId: "", joinerNodeId: "", completedTables: [], groupId: "", primaryTerm: 0n };
1334
+ }
1335
+ var SyncRequestPayload = {
1336
+ encode(message, writer = new BinaryWriter()) {
1337
+ if (message.requestId !== "") {
1338
+ writer.uint32(10).string(message.requestId);
1339
+ }
1340
+ if (message.joinerNodeId !== "") {
1341
+ writer.uint32(18).string(message.joinerNodeId);
1342
+ }
1343
+ for (const v of message.completedTables) {
1344
+ writer.uint32(26).string(v);
1345
+ }
1346
+ if (message.groupId !== "") {
1347
+ writer.uint32(34).string(message.groupId);
1348
+ }
1349
+ if (message.primaryTerm !== 0n) {
1350
+ if (BigInt.asIntN(64, message.primaryTerm) !== message.primaryTerm) {
1351
+ throw new globalThis.Error("value provided for field message.primaryTerm of type int64 too large");
1352
+ }
1353
+ writer.uint32(40).int64(message.primaryTerm);
1354
+ }
1355
+ return writer;
1356
+ },
1357
+ decode(input, length) {
1358
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1359
+ const end = length === void 0 ? reader.len : reader.pos + length;
1360
+ const message = createBaseSyncRequestPayload();
1361
+ while (reader.pos < end) {
1362
+ const tag = reader.uint32();
1363
+ switch (tag >>> 3) {
1364
+ case 1: {
1365
+ if (tag !== 10) {
1366
+ break;
1367
+ }
1368
+ message.requestId = reader.string();
1369
+ continue;
1370
+ }
1371
+ case 2: {
1372
+ if (tag !== 18) {
1373
+ break;
1374
+ }
1375
+ message.joinerNodeId = reader.string();
1376
+ continue;
1377
+ }
1378
+ case 3: {
1379
+ if (tag !== 26) {
1380
+ break;
1381
+ }
1382
+ message.completedTables.push(reader.string());
1383
+ continue;
1384
+ }
1385
+ case 4: {
1386
+ if (tag !== 34) {
1387
+ break;
1388
+ }
1389
+ message.groupId = reader.string();
1390
+ continue;
1391
+ }
1392
+ case 5: {
1393
+ if (tag !== 40) {
1394
+ break;
1395
+ }
1396
+ message.primaryTerm = reader.int64();
1397
+ continue;
1398
+ }
1399
+ }
1400
+ if ((tag & 7) === 4 || tag === 0) {
1401
+ break;
1402
+ }
1403
+ reader.skip(tag & 7);
1404
+ }
1405
+ return message;
1406
+ },
1407
+ fromJSON(object) {
1408
+ return {
1409
+ requestId: isSet(object.requestId) ? globalThis.String(object.requestId) : isSet(object.request_id) ? globalThis.String(object.request_id) : "",
1410
+ joinerNodeId: isSet(object.joinerNodeId) ? globalThis.String(object.joinerNodeId) : isSet(object.joiner_node_id) ? globalThis.String(object.joiner_node_id) : "",
1411
+ completedTables: globalThis.Array.isArray(object?.completedTables) ? object.completedTables.map((e) => globalThis.String(e)) : globalThis.Array.isArray(object?.completed_tables) ? object.completed_tables.map((e) => globalThis.String(e)) : [],
1412
+ groupId: isSet(object.groupId) ? globalThis.String(object.groupId) : isSet(object.group_id) ? globalThis.String(object.group_id) : "",
1413
+ primaryTerm: isSet(object.primaryTerm) ? BigInt(object.primaryTerm) : isSet(object.primary_term) ? BigInt(object.primary_term) : 0n
1414
+ };
1415
+ },
1416
+ toJSON(message) {
1417
+ const obj = {};
1418
+ if (message.requestId !== "") {
1419
+ obj.requestId = message.requestId;
1420
+ }
1421
+ if (message.joinerNodeId !== "") {
1422
+ obj.joinerNodeId = message.joinerNodeId;
1423
+ }
1424
+ if (message.completedTables?.length) {
1425
+ obj.completedTables = message.completedTables;
1426
+ }
1427
+ if (message.groupId !== "") {
1428
+ obj.groupId = message.groupId;
1429
+ }
1430
+ if (message.primaryTerm !== 0n) {
1431
+ obj.primaryTerm = message.primaryTerm.toString();
1432
+ }
1433
+ return obj;
1434
+ },
1435
+ create(base) {
1436
+ return SyncRequestPayload.fromPartial(base ?? {});
1437
+ },
1438
+ fromPartial(object) {
1439
+ const message = createBaseSyncRequestPayload();
1440
+ message.requestId = object.requestId ?? "";
1441
+ message.joinerNodeId = object.joinerNodeId ?? "";
1442
+ message.completedTables = object.completedTables?.map((e) => e) || [];
1443
+ message.groupId = object.groupId ?? "";
1444
+ message.primaryTerm = object.primaryTerm ?? 0n;
1445
+ return message;
1446
+ }
1447
+ };
1448
+ function createBaseSyncBatchPayload() {
1449
+ return {
1450
+ requestId: "",
1451
+ table: "",
1452
+ batchIndex: 0,
1453
+ rows: [],
1454
+ schema: [],
1455
+ checksum: "",
1456
+ isLastBatchForTable: false,
1457
+ groupId: "",
1458
+ primaryTerm: 0n
1459
+ };
1460
+ }
1461
+ var SyncBatchPayload = {
1462
+ encode(message, writer = new BinaryWriter()) {
1463
+ if (message.requestId !== "") {
1464
+ writer.uint32(10).string(message.requestId);
1465
+ }
1466
+ if (message.table !== "") {
1467
+ writer.uint32(18).string(message.table);
1468
+ }
1469
+ if (message.batchIndex !== 0) {
1470
+ writer.uint32(24).int32(message.batchIndex);
1471
+ }
1472
+ for (const v of message.rows) {
1473
+ RowData.encode(v, writer.uint32(34).fork()).join();
1474
+ }
1475
+ for (const v of message.schema) {
1476
+ writer.uint32(42).string(v);
1477
+ }
1478
+ if (message.checksum !== "") {
1479
+ writer.uint32(50).string(message.checksum);
1480
+ }
1481
+ if (message.isLastBatchForTable !== false) {
1482
+ writer.uint32(56).bool(message.isLastBatchForTable);
1483
+ }
1484
+ if (message.groupId !== "") {
1485
+ writer.uint32(66).string(message.groupId);
1486
+ }
1487
+ if (message.primaryTerm !== 0n) {
1488
+ if (BigInt.asIntN(64, message.primaryTerm) !== message.primaryTerm) {
1489
+ throw new globalThis.Error("value provided for field message.primaryTerm of type int64 too large");
1490
+ }
1491
+ writer.uint32(72).int64(message.primaryTerm);
1492
+ }
1493
+ return writer;
1494
+ },
1495
+ decode(input, length) {
1496
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1497
+ const end = length === void 0 ? reader.len : reader.pos + length;
1498
+ const message = createBaseSyncBatchPayload();
1499
+ while (reader.pos < end) {
1500
+ const tag = reader.uint32();
1501
+ switch (tag >>> 3) {
1502
+ case 1: {
1503
+ if (tag !== 10) {
1504
+ break;
1505
+ }
1506
+ message.requestId = reader.string();
1507
+ continue;
1508
+ }
1509
+ case 2: {
1510
+ if (tag !== 18) {
1511
+ break;
1512
+ }
1513
+ message.table = reader.string();
1514
+ continue;
1515
+ }
1516
+ case 3: {
1517
+ if (tag !== 24) {
1518
+ break;
1519
+ }
1520
+ message.batchIndex = reader.int32();
1521
+ continue;
1522
+ }
1523
+ case 4: {
1524
+ if (tag !== 34) {
1525
+ break;
1526
+ }
1527
+ message.rows.push(RowData.decode(reader, reader.uint32()));
1528
+ continue;
1529
+ }
1530
+ case 5: {
1531
+ if (tag !== 42) {
1532
+ break;
1533
+ }
1534
+ message.schema.push(reader.string());
1535
+ continue;
1536
+ }
1537
+ case 6: {
1538
+ if (tag !== 50) {
1539
+ break;
1540
+ }
1541
+ message.checksum = reader.string();
1542
+ continue;
1543
+ }
1544
+ case 7: {
1545
+ if (tag !== 56) {
1546
+ break;
1547
+ }
1548
+ message.isLastBatchForTable = reader.bool();
1549
+ continue;
1550
+ }
1551
+ case 8: {
1552
+ if (tag !== 66) {
1553
+ break;
1554
+ }
1555
+ message.groupId = reader.string();
1556
+ continue;
1557
+ }
1558
+ case 9: {
1559
+ if (tag !== 72) {
1560
+ break;
1561
+ }
1562
+ message.primaryTerm = reader.int64();
1563
+ continue;
1564
+ }
1565
+ }
1566
+ if ((tag & 7) === 4 || tag === 0) {
1567
+ break;
1568
+ }
1569
+ reader.skip(tag & 7);
1570
+ }
1571
+ return message;
1572
+ },
1573
+ fromJSON(object) {
1574
+ return {
1575
+ requestId: isSet(object.requestId) ? globalThis.String(object.requestId) : isSet(object.request_id) ? globalThis.String(object.request_id) : "",
1576
+ table: isSet(object.table) ? globalThis.String(object.table) : "",
1577
+ batchIndex: isSet(object.batchIndex) ? globalThis.Number(object.batchIndex) : isSet(object.batch_index) ? globalThis.Number(object.batch_index) : 0,
1578
+ rows: globalThis.Array.isArray(object?.rows) ? object.rows.map((e) => RowData.fromJSON(e)) : [],
1579
+ schema: globalThis.Array.isArray(object?.schema) ? object.schema.map((e) => globalThis.String(e)) : [],
1580
+ checksum: isSet(object.checksum) ? globalThis.String(object.checksum) : "",
1581
+ isLastBatchForTable: isSet(object.isLastBatchForTable) ? globalThis.Boolean(object.isLastBatchForTable) : isSet(object.is_last_batch_for_table) ? globalThis.Boolean(object.is_last_batch_for_table) : false,
1582
+ groupId: isSet(object.groupId) ? globalThis.String(object.groupId) : isSet(object.group_id) ? globalThis.String(object.group_id) : "",
1583
+ primaryTerm: isSet(object.primaryTerm) ? BigInt(object.primaryTerm) : isSet(object.primary_term) ? BigInt(object.primary_term) : 0n
1584
+ };
1585
+ },
1586
+ toJSON(message) {
1587
+ const obj = {};
1588
+ if (message.requestId !== "") {
1589
+ obj.requestId = message.requestId;
1590
+ }
1591
+ if (message.table !== "") {
1592
+ obj.table = message.table;
1593
+ }
1594
+ if (message.batchIndex !== 0) {
1595
+ obj.batchIndex = Math.round(message.batchIndex);
1596
+ }
1597
+ if (message.rows?.length) {
1598
+ obj.rows = message.rows.map((e) => RowData.toJSON(e));
1599
+ }
1600
+ if (message.schema?.length) {
1601
+ obj.schema = message.schema;
1602
+ }
1603
+ if (message.checksum !== "") {
1604
+ obj.checksum = message.checksum;
1605
+ }
1606
+ if (message.isLastBatchForTable !== false) {
1607
+ obj.isLastBatchForTable = message.isLastBatchForTable;
1608
+ }
1609
+ if (message.groupId !== "") {
1610
+ obj.groupId = message.groupId;
1611
+ }
1612
+ if (message.primaryTerm !== 0n) {
1613
+ obj.primaryTerm = message.primaryTerm.toString();
1614
+ }
1615
+ return obj;
1616
+ },
1617
+ create(base) {
1618
+ return SyncBatchPayload.fromPartial(base ?? {});
1619
+ },
1620
+ fromPartial(object) {
1621
+ const message = createBaseSyncBatchPayload();
1622
+ message.requestId = object.requestId ?? "";
1623
+ message.table = object.table ?? "";
1624
+ message.batchIndex = object.batchIndex ?? 0;
1625
+ message.rows = object.rows?.map((e) => RowData.fromPartial(e)) || [];
1626
+ message.schema = object.schema?.map((e) => e) || [];
1627
+ message.checksum = object.checksum ?? "";
1628
+ message.isLastBatchForTable = object.isLastBatchForTable ?? false;
1629
+ message.groupId = object.groupId ?? "";
1630
+ message.primaryTerm = object.primaryTerm ?? 0n;
1631
+ return message;
1632
+ }
1633
+ };
1634
+ function createBaseSyncCompletePayload() {
1635
+ return { requestId: "", snapshotSeq: 0n, manifests: [], groupId: "", primaryTerm: 0n };
1636
+ }
1637
+ var SyncCompletePayload = {
1638
+ encode(message, writer = new BinaryWriter()) {
1639
+ if (message.requestId !== "") {
1640
+ writer.uint32(10).string(message.requestId);
1641
+ }
1642
+ if (message.snapshotSeq !== 0n) {
1643
+ if (BigInt.asIntN(64, message.snapshotSeq) !== message.snapshotSeq) {
1644
+ throw new globalThis.Error("value provided for field message.snapshotSeq of type int64 too large");
1645
+ }
1646
+ writer.uint32(16).int64(message.snapshotSeq);
1647
+ }
1648
+ for (const v of message.manifests) {
1649
+ SyncTableManifest.encode(v, writer.uint32(26).fork()).join();
1650
+ }
1651
+ if (message.groupId !== "") {
1652
+ writer.uint32(34).string(message.groupId);
1653
+ }
1654
+ if (message.primaryTerm !== 0n) {
1655
+ if (BigInt.asIntN(64, message.primaryTerm) !== message.primaryTerm) {
1656
+ throw new globalThis.Error("value provided for field message.primaryTerm of type int64 too large");
1657
+ }
1658
+ writer.uint32(40).int64(message.primaryTerm);
1659
+ }
1660
+ return writer;
1661
+ },
1662
+ decode(input, length) {
1663
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1664
+ const end = length === void 0 ? reader.len : reader.pos + length;
1665
+ const message = createBaseSyncCompletePayload();
1666
+ while (reader.pos < end) {
1667
+ const tag = reader.uint32();
1668
+ switch (tag >>> 3) {
1669
+ case 1: {
1670
+ if (tag !== 10) {
1671
+ break;
1672
+ }
1673
+ message.requestId = reader.string();
1674
+ continue;
1675
+ }
1676
+ case 2: {
1677
+ if (tag !== 16) {
1678
+ break;
1679
+ }
1680
+ message.snapshotSeq = reader.int64();
1681
+ continue;
1682
+ }
1683
+ case 3: {
1684
+ if (tag !== 26) {
1685
+ break;
1686
+ }
1687
+ message.manifests.push(SyncTableManifest.decode(reader, reader.uint32()));
1688
+ continue;
1689
+ }
1690
+ case 4: {
1691
+ if (tag !== 34) {
1692
+ break;
1693
+ }
1694
+ message.groupId = reader.string();
1695
+ continue;
1696
+ }
1697
+ case 5: {
1698
+ if (tag !== 40) {
1699
+ break;
1700
+ }
1701
+ message.primaryTerm = reader.int64();
1702
+ continue;
1703
+ }
1704
+ }
1705
+ if ((tag & 7) === 4 || tag === 0) {
1706
+ break;
1707
+ }
1708
+ reader.skip(tag & 7);
1709
+ }
1710
+ return message;
1711
+ },
1712
+ fromJSON(object) {
1713
+ return {
1714
+ requestId: isSet(object.requestId) ? globalThis.String(object.requestId) : isSet(object.request_id) ? globalThis.String(object.request_id) : "",
1715
+ snapshotSeq: isSet(object.snapshotSeq) ? BigInt(object.snapshotSeq) : isSet(object.snapshot_seq) ? BigInt(object.snapshot_seq) : 0n,
1716
+ manifests: globalThis.Array.isArray(object?.manifests) ? object.manifests.map((e) => SyncTableManifest.fromJSON(e)) : [],
1717
+ groupId: isSet(object.groupId) ? globalThis.String(object.groupId) : isSet(object.group_id) ? globalThis.String(object.group_id) : "",
1718
+ primaryTerm: isSet(object.primaryTerm) ? BigInt(object.primaryTerm) : isSet(object.primary_term) ? BigInt(object.primary_term) : 0n
1719
+ };
1720
+ },
1721
+ toJSON(message) {
1722
+ const obj = {};
1723
+ if (message.requestId !== "") {
1724
+ obj.requestId = message.requestId;
1725
+ }
1726
+ if (message.snapshotSeq !== 0n) {
1727
+ obj.snapshotSeq = message.snapshotSeq.toString();
1728
+ }
1729
+ if (message.manifests?.length) {
1730
+ obj.manifests = message.manifests.map((e) => SyncTableManifest.toJSON(e));
1731
+ }
1732
+ if (message.groupId !== "") {
1733
+ obj.groupId = message.groupId;
1734
+ }
1735
+ if (message.primaryTerm !== 0n) {
1736
+ obj.primaryTerm = message.primaryTerm.toString();
1737
+ }
1738
+ return obj;
1739
+ },
1740
+ create(base) {
1741
+ return SyncCompletePayload.fromPartial(base ?? {});
1742
+ },
1743
+ fromPartial(object) {
1744
+ const message = createBaseSyncCompletePayload();
1745
+ message.requestId = object.requestId ?? "";
1746
+ message.snapshotSeq = object.snapshotSeq ?? 0n;
1747
+ message.manifests = object.manifests?.map((e) => SyncTableManifest.fromPartial(e)) || [];
1748
+ message.groupId = object.groupId ?? "";
1749
+ message.primaryTerm = object.primaryTerm ?? 0n;
1750
+ return message;
1751
+ }
1752
+ };
1753
+ function createBaseSyncTableManifest() {
1754
+ return { table: "", rowCount: 0, pkHash: "" };
1755
+ }
1756
+ var SyncTableManifest = {
1757
+ encode(message, writer = new BinaryWriter()) {
1758
+ if (message.table !== "") {
1759
+ writer.uint32(10).string(message.table);
1760
+ }
1761
+ if (message.rowCount !== 0) {
1762
+ writer.uint32(16).int32(message.rowCount);
1763
+ }
1764
+ if (message.pkHash !== "") {
1765
+ writer.uint32(26).string(message.pkHash);
1766
+ }
1767
+ return writer;
1768
+ },
1769
+ decode(input, length) {
1770
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1771
+ const end = length === void 0 ? reader.len : reader.pos + length;
1772
+ const message = createBaseSyncTableManifest();
1773
+ while (reader.pos < end) {
1774
+ const tag = reader.uint32();
1775
+ switch (tag >>> 3) {
1776
+ case 1: {
1777
+ if (tag !== 10) {
1778
+ break;
1779
+ }
1780
+ message.table = reader.string();
1781
+ continue;
1782
+ }
1783
+ case 2: {
1784
+ if (tag !== 16) {
1785
+ break;
1786
+ }
1787
+ message.rowCount = reader.int32();
1788
+ continue;
1789
+ }
1790
+ case 3: {
1791
+ if (tag !== 26) {
1792
+ break;
1793
+ }
1794
+ message.pkHash = reader.string();
1795
+ continue;
1796
+ }
1797
+ }
1798
+ if ((tag & 7) === 4 || tag === 0) {
1799
+ break;
1800
+ }
1801
+ reader.skip(tag & 7);
1802
+ }
1803
+ return message;
1804
+ },
1805
+ fromJSON(object) {
1806
+ return {
1807
+ table: isSet(object.table) ? globalThis.String(object.table) : "",
1808
+ rowCount: isSet(object.rowCount) ? globalThis.Number(object.rowCount) : isSet(object.row_count) ? globalThis.Number(object.row_count) : 0,
1809
+ pkHash: isSet(object.pkHash) ? globalThis.String(object.pkHash) : isSet(object.pk_hash) ? globalThis.String(object.pk_hash) : ""
1810
+ };
1811
+ },
1812
+ toJSON(message) {
1813
+ const obj = {};
1814
+ if (message.table !== "") {
1815
+ obj.table = message.table;
1816
+ }
1817
+ if (message.rowCount !== 0) {
1818
+ obj.rowCount = Math.round(message.rowCount);
1819
+ }
1820
+ if (message.pkHash !== "") {
1821
+ obj.pkHash = message.pkHash;
1822
+ }
1823
+ return obj;
1824
+ },
1825
+ create(base) {
1826
+ return SyncTableManifest.fromPartial(base ?? {});
1827
+ },
1828
+ fromPartial(object) {
1829
+ const message = createBaseSyncTableManifest();
1830
+ message.table = object.table ?? "";
1831
+ message.rowCount = object.rowCount ?? 0;
1832
+ message.pkHash = object.pkHash ?? "";
1833
+ return message;
1834
+ }
1835
+ };
1836
+ function createBaseSyncAckPayload() {
1837
+ return {
1838
+ requestId: "",
1839
+ joinerNodeId: "",
1840
+ table: "",
1841
+ batchIndex: 0,
1842
+ success: false,
1843
+ error: "",
1844
+ groupId: "",
1845
+ primaryTerm: 0n
1846
+ };
1847
+ }
1848
+ var SyncAckPayload = {
1849
+ encode(message, writer = new BinaryWriter()) {
1850
+ if (message.requestId !== "") {
1851
+ writer.uint32(10).string(message.requestId);
1852
+ }
1853
+ if (message.joinerNodeId !== "") {
1854
+ writer.uint32(18).string(message.joinerNodeId);
1855
+ }
1856
+ if (message.table !== "") {
1857
+ writer.uint32(26).string(message.table);
1858
+ }
1859
+ if (message.batchIndex !== 0) {
1860
+ writer.uint32(32).int32(message.batchIndex);
1861
+ }
1862
+ if (message.success !== false) {
1863
+ writer.uint32(40).bool(message.success);
1864
+ }
1865
+ if (message.error !== "") {
1866
+ writer.uint32(50).string(message.error);
1867
+ }
1868
+ if (message.groupId !== "") {
1869
+ writer.uint32(58).string(message.groupId);
1870
+ }
1871
+ if (message.primaryTerm !== 0n) {
1872
+ if (BigInt.asIntN(64, message.primaryTerm) !== message.primaryTerm) {
1873
+ throw new globalThis.Error("value provided for field message.primaryTerm of type int64 too large");
1874
+ }
1875
+ writer.uint32(64).int64(message.primaryTerm);
1876
+ }
1877
+ return writer;
1878
+ },
1879
+ decode(input, length) {
1880
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1881
+ const end = length === void 0 ? reader.len : reader.pos + length;
1882
+ const message = createBaseSyncAckPayload();
1883
+ while (reader.pos < end) {
1884
+ const tag = reader.uint32();
1885
+ switch (tag >>> 3) {
1886
+ case 1: {
1887
+ if (tag !== 10) {
1888
+ break;
1889
+ }
1890
+ message.requestId = reader.string();
1891
+ continue;
1892
+ }
1893
+ case 2: {
1894
+ if (tag !== 18) {
1895
+ break;
1896
+ }
1897
+ message.joinerNodeId = reader.string();
1898
+ continue;
1899
+ }
1900
+ case 3: {
1901
+ if (tag !== 26) {
1902
+ break;
1903
+ }
1904
+ message.table = reader.string();
1905
+ continue;
1906
+ }
1907
+ case 4: {
1908
+ if (tag !== 32) {
1909
+ break;
1910
+ }
1911
+ message.batchIndex = reader.int32();
1912
+ continue;
1913
+ }
1914
+ case 5: {
1915
+ if (tag !== 40) {
1916
+ break;
1917
+ }
1918
+ message.success = reader.bool();
1919
+ continue;
1920
+ }
1921
+ case 6: {
1922
+ if (tag !== 50) {
1923
+ break;
1924
+ }
1925
+ message.error = reader.string();
1926
+ continue;
1927
+ }
1928
+ case 7: {
1929
+ if (tag !== 58) {
1930
+ break;
1931
+ }
1932
+ message.groupId = reader.string();
1933
+ continue;
1934
+ }
1935
+ case 8: {
1936
+ if (tag !== 64) {
1937
+ break;
1938
+ }
1939
+ message.primaryTerm = reader.int64();
1940
+ continue;
1941
+ }
1942
+ }
1943
+ if ((tag & 7) === 4 || tag === 0) {
1944
+ break;
1945
+ }
1946
+ reader.skip(tag & 7);
1947
+ }
1948
+ return message;
1949
+ },
1950
+ fromJSON(object) {
1951
+ return {
1952
+ requestId: isSet(object.requestId) ? globalThis.String(object.requestId) : isSet(object.request_id) ? globalThis.String(object.request_id) : "",
1953
+ joinerNodeId: isSet(object.joinerNodeId) ? globalThis.String(object.joinerNodeId) : isSet(object.joiner_node_id) ? globalThis.String(object.joiner_node_id) : "",
1954
+ table: isSet(object.table) ? globalThis.String(object.table) : "",
1955
+ batchIndex: isSet(object.batchIndex) ? globalThis.Number(object.batchIndex) : isSet(object.batch_index) ? globalThis.Number(object.batch_index) : 0,
1956
+ success: isSet(object.success) ? globalThis.Boolean(object.success) : false,
1957
+ error: isSet(object.error) ? globalThis.String(object.error) : "",
1958
+ groupId: isSet(object.groupId) ? globalThis.String(object.groupId) : isSet(object.group_id) ? globalThis.String(object.group_id) : "",
1959
+ primaryTerm: isSet(object.primaryTerm) ? BigInt(object.primaryTerm) : isSet(object.primary_term) ? BigInt(object.primary_term) : 0n
1960
+ };
1961
+ },
1962
+ toJSON(message) {
1963
+ const obj = {};
1964
+ if (message.requestId !== "") {
1965
+ obj.requestId = message.requestId;
1966
+ }
1967
+ if (message.joinerNodeId !== "") {
1968
+ obj.joinerNodeId = message.joinerNodeId;
1969
+ }
1970
+ if (message.table !== "") {
1971
+ obj.table = message.table;
1972
+ }
1973
+ if (message.batchIndex !== 0) {
1974
+ obj.batchIndex = Math.round(message.batchIndex);
1975
+ }
1976
+ if (message.success !== false) {
1977
+ obj.success = message.success;
1978
+ }
1979
+ if (message.error !== "") {
1980
+ obj.error = message.error;
1981
+ }
1982
+ if (message.groupId !== "") {
1983
+ obj.groupId = message.groupId;
1984
+ }
1985
+ if (message.primaryTerm !== 0n) {
1986
+ obj.primaryTerm = message.primaryTerm.toString();
1987
+ }
1988
+ return obj;
1989
+ },
1990
+ create(base) {
1991
+ return SyncAckPayload.fromPartial(base ?? {});
1992
+ },
1993
+ fromPartial(object) {
1994
+ const message = createBaseSyncAckPayload();
1995
+ message.requestId = object.requestId ?? "";
1996
+ message.joinerNodeId = object.joinerNodeId ?? "";
1997
+ message.table = object.table ?? "";
1998
+ message.batchIndex = object.batchIndex ?? 0;
1999
+ message.success = object.success ?? false;
2000
+ message.error = object.error ?? "";
2001
+ message.groupId = object.groupId ?? "";
2002
+ message.primaryTerm = object.primaryTerm ?? 0n;
2003
+ return message;
2004
+ }
2005
+ };
2006
+ function createBaseSyncMessage() {
2007
+ return {
2008
+ hello: void 0,
2009
+ syncRequest: void 0,
2010
+ syncBatch: void 0,
2011
+ syncComplete: void 0,
2012
+ syncAck: void 0
2013
+ };
2014
+ }
2015
+ var SyncMessage = {
2016
+ encode(message, writer = new BinaryWriter()) {
2017
+ if (message.hello !== void 0) {
2018
+ Hello.encode(message.hello, writer.uint32(10).fork()).join();
2019
+ }
2020
+ if (message.syncRequest !== void 0) {
2021
+ SyncRequestPayload.encode(message.syncRequest, writer.uint32(18).fork()).join();
2022
+ }
2023
+ if (message.syncBatch !== void 0) {
2024
+ SyncBatchPayload.encode(message.syncBatch, writer.uint32(26).fork()).join();
2025
+ }
2026
+ if (message.syncComplete !== void 0) {
2027
+ SyncCompletePayload.encode(message.syncComplete, writer.uint32(34).fork()).join();
2028
+ }
2029
+ if (message.syncAck !== void 0) {
2030
+ SyncAckPayload.encode(message.syncAck, writer.uint32(42).fork()).join();
2031
+ }
2032
+ return writer;
2033
+ },
2034
+ decode(input, length) {
2035
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
2036
+ const end = length === void 0 ? reader.len : reader.pos + length;
2037
+ const message = createBaseSyncMessage();
2038
+ while (reader.pos < end) {
2039
+ const tag = reader.uint32();
2040
+ switch (tag >>> 3) {
2041
+ case 1: {
2042
+ if (tag !== 10) {
2043
+ break;
2044
+ }
2045
+ message.hello = Hello.decode(reader, reader.uint32());
2046
+ continue;
2047
+ }
2048
+ case 2: {
2049
+ if (tag !== 18) {
2050
+ break;
2051
+ }
2052
+ message.syncRequest = SyncRequestPayload.decode(reader, reader.uint32());
2053
+ continue;
2054
+ }
2055
+ case 3: {
2056
+ if (tag !== 26) {
2057
+ break;
2058
+ }
2059
+ message.syncBatch = SyncBatchPayload.decode(reader, reader.uint32());
2060
+ continue;
2061
+ }
2062
+ case 4: {
2063
+ if (tag !== 34) {
2064
+ break;
2065
+ }
2066
+ message.syncComplete = SyncCompletePayload.decode(reader, reader.uint32());
2067
+ continue;
2068
+ }
2069
+ case 5: {
2070
+ if (tag !== 42) {
2071
+ break;
2072
+ }
2073
+ message.syncAck = SyncAckPayload.decode(reader, reader.uint32());
2074
+ continue;
2075
+ }
2076
+ }
2077
+ if ((tag & 7) === 4 || tag === 0) {
2078
+ break;
2079
+ }
2080
+ reader.skip(tag & 7);
2081
+ }
2082
+ return message;
2083
+ },
2084
+ fromJSON(object) {
2085
+ return {
2086
+ hello: isSet(object.hello) ? Hello.fromJSON(object.hello) : void 0,
2087
+ syncRequest: isSet(object.syncRequest) ? SyncRequestPayload.fromJSON(object.syncRequest) : isSet(object.sync_request) ? SyncRequestPayload.fromJSON(object.sync_request) : void 0,
2088
+ syncBatch: isSet(object.syncBatch) ? SyncBatchPayload.fromJSON(object.syncBatch) : isSet(object.sync_batch) ? SyncBatchPayload.fromJSON(object.sync_batch) : void 0,
2089
+ syncComplete: isSet(object.syncComplete) ? SyncCompletePayload.fromJSON(object.syncComplete) : isSet(object.sync_complete) ? SyncCompletePayload.fromJSON(object.sync_complete) : void 0,
2090
+ syncAck: isSet(object.syncAck) ? SyncAckPayload.fromJSON(object.syncAck) : isSet(object.sync_ack) ? SyncAckPayload.fromJSON(object.sync_ack) : void 0
2091
+ };
2092
+ },
2093
+ toJSON(message) {
2094
+ const obj = {};
2095
+ if (message.hello !== void 0) {
2096
+ obj.hello = Hello.toJSON(message.hello);
2097
+ }
2098
+ if (message.syncRequest !== void 0) {
2099
+ obj.syncRequest = SyncRequestPayload.toJSON(message.syncRequest);
2100
+ }
2101
+ if (message.syncBatch !== void 0) {
2102
+ obj.syncBatch = SyncBatchPayload.toJSON(message.syncBatch);
2103
+ }
2104
+ if (message.syncComplete !== void 0) {
2105
+ obj.syncComplete = SyncCompletePayload.toJSON(message.syncComplete);
2106
+ }
2107
+ if (message.syncAck !== void 0) {
2108
+ obj.syncAck = SyncAckPayload.toJSON(message.syncAck);
2109
+ }
2110
+ return obj;
2111
+ },
2112
+ create(base) {
2113
+ return SyncMessage.fromPartial(base ?? {});
2114
+ },
2115
+ fromPartial(object) {
2116
+ const message = createBaseSyncMessage();
2117
+ message.hello = object.hello !== void 0 && object.hello !== null ? Hello.fromPartial(object.hello) : void 0;
2118
+ message.syncRequest = object.syncRequest !== void 0 && object.syncRequest !== null ? SyncRequestPayload.fromPartial(object.syncRequest) : void 0;
2119
+ message.syncBatch = object.syncBatch !== void 0 && object.syncBatch !== null ? SyncBatchPayload.fromPartial(object.syncBatch) : void 0;
2120
+ message.syncComplete = object.syncComplete !== void 0 && object.syncComplete !== null ? SyncCompletePayload.fromPartial(object.syncComplete) : void 0;
2121
+ message.syncAck = object.syncAck !== void 0 && object.syncAck !== null ? SyncAckPayload.fromPartial(object.syncAck) : void 0;
2122
+ return message;
2123
+ }
2124
+ };
2125
+ function createBaseStatement() {
2126
+ return { sql: "", namedParams: {}, positionalParams: [] };
2127
+ }
2128
+ var Statement = {
2129
+ encode(message, writer = new BinaryWriter()) {
2130
+ if (message.sql !== "") {
2131
+ writer.uint32(10).string(message.sql);
2132
+ }
2133
+ globalThis.Object.entries(message.namedParams).forEach(([key, value]) => {
2134
+ Statement_NamedParamsEntry.encode({ key, value }, writer.uint32(18).fork()).join();
2135
+ });
2136
+ for (const v of message.positionalParams) {
2137
+ ColumnValue.encode(v, writer.uint32(26).fork()).join();
2138
+ }
2139
+ return writer;
2140
+ },
2141
+ decode(input, length) {
2142
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
2143
+ const end = length === void 0 ? reader.len : reader.pos + length;
2144
+ const message = createBaseStatement();
2145
+ while (reader.pos < end) {
2146
+ const tag = reader.uint32();
2147
+ switch (tag >>> 3) {
2148
+ case 1: {
2149
+ if (tag !== 10) {
2150
+ break;
2151
+ }
2152
+ message.sql = reader.string();
2153
+ continue;
2154
+ }
2155
+ case 2: {
2156
+ if (tag !== 18) {
2157
+ break;
2158
+ }
2159
+ const entry2 = Statement_NamedParamsEntry.decode(reader, reader.uint32());
2160
+ if (entry2.value !== void 0) {
2161
+ message.namedParams[entry2.key] = entry2.value;
2162
+ }
2163
+ continue;
2164
+ }
2165
+ case 3: {
2166
+ if (tag !== 26) {
2167
+ break;
2168
+ }
2169
+ message.positionalParams.push(ColumnValue.decode(reader, reader.uint32()));
2170
+ continue;
2171
+ }
2172
+ }
2173
+ if ((tag & 7) === 4 || tag === 0) {
2174
+ break;
2175
+ }
2176
+ reader.skip(tag & 7);
2177
+ }
2178
+ return message;
2179
+ },
2180
+ fromJSON(object) {
2181
+ return {
2182
+ sql: isSet(object.sql) ? globalThis.String(object.sql) : "",
2183
+ namedParams: isObject(object.namedParams) ? globalThis.Object.entries(object.namedParams).reduce(
2184
+ (acc, [key, value]) => {
2185
+ acc[key] = ColumnValue.fromJSON(value);
2186
+ return acc;
2187
+ },
2188
+ {}
2189
+ ) : isObject(object.named_params) ? globalThis.Object.entries(object.named_params).reduce(
2190
+ (acc, [key, value]) => {
2191
+ acc[key] = ColumnValue.fromJSON(value);
2192
+ return acc;
2193
+ },
2194
+ {}
2195
+ ) : {},
2196
+ positionalParams: globalThis.Array.isArray(object?.positionalParams) ? object.positionalParams.map((e) => ColumnValue.fromJSON(e)) : globalThis.Array.isArray(object?.positional_params) ? object.positional_params.map((e) => ColumnValue.fromJSON(e)) : []
2197
+ };
2198
+ },
2199
+ toJSON(message) {
2200
+ const obj = {};
2201
+ if (message.sql !== "") {
2202
+ obj.sql = message.sql;
2203
+ }
2204
+ if (message.namedParams) {
2205
+ const entries = globalThis.Object.entries(message.namedParams);
2206
+ if (entries.length > 0) {
2207
+ obj.namedParams = {};
2208
+ entries.forEach(([k, v]) => {
2209
+ obj.namedParams[k] = ColumnValue.toJSON(v);
2210
+ });
2211
+ }
2212
+ }
2213
+ if (message.positionalParams?.length) {
2214
+ obj.positionalParams = message.positionalParams.map((e) => ColumnValue.toJSON(e));
2215
+ }
2216
+ return obj;
2217
+ },
2218
+ create(base) {
2219
+ return Statement.fromPartial(base ?? {});
2220
+ },
2221
+ fromPartial(object) {
2222
+ const message = createBaseStatement();
2223
+ message.sql = object.sql ?? "";
2224
+ message.namedParams = globalThis.Object.entries(object.namedParams ?? {}).reduce(
2225
+ (acc, [key, value]) => {
2226
+ if (value !== void 0) {
2227
+ acc[key] = ColumnValue.fromPartial(value);
2228
+ }
2229
+ return acc;
2230
+ },
2231
+ {}
2232
+ );
2233
+ message.positionalParams = object.positionalParams?.map((e) => ColumnValue.fromPartial(e)) || [];
2234
+ return message;
2235
+ }
2236
+ };
2237
+ function createBaseStatement_NamedParamsEntry() {
2238
+ return { key: "", value: void 0 };
2239
+ }
2240
+ var Statement_NamedParamsEntry = {
2241
+ encode(message, writer = new BinaryWriter()) {
2242
+ if (message.key !== "") {
2243
+ writer.uint32(10).string(message.key);
2244
+ }
2245
+ if (message.value !== void 0) {
2246
+ ColumnValue.encode(message.value, writer.uint32(18).fork()).join();
2247
+ }
2248
+ return writer;
2249
+ },
2250
+ decode(input, length) {
2251
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
2252
+ const end = length === void 0 ? reader.len : reader.pos + length;
2253
+ const message = createBaseStatement_NamedParamsEntry();
2254
+ while (reader.pos < end) {
2255
+ const tag = reader.uint32();
2256
+ switch (tag >>> 3) {
2257
+ case 1: {
2258
+ if (tag !== 10) {
2259
+ break;
2260
+ }
2261
+ message.key = reader.string();
2262
+ continue;
2263
+ }
2264
+ case 2: {
2265
+ if (tag !== 18) {
2266
+ break;
2267
+ }
2268
+ message.value = ColumnValue.decode(reader, reader.uint32());
2269
+ continue;
2270
+ }
2271
+ }
2272
+ if ((tag & 7) === 4 || tag === 0) {
2273
+ break;
2274
+ }
2275
+ reader.skip(tag & 7);
2276
+ }
2277
+ return message;
2278
+ },
2279
+ fromJSON(object) {
2280
+ return {
2281
+ key: isSet(object.key) ? globalThis.String(object.key) : "",
2282
+ value: isSet(object.value) ? ColumnValue.fromJSON(object.value) : void 0
2283
+ };
2284
+ },
2285
+ toJSON(message) {
2286
+ const obj = {};
2287
+ if (message.key !== "") {
2288
+ obj.key = message.key;
2289
+ }
2290
+ if (message.value !== void 0) {
2291
+ obj.value = ColumnValue.toJSON(message.value);
2292
+ }
2293
+ return obj;
2294
+ },
2295
+ create(base) {
2296
+ return Statement_NamedParamsEntry.fromPartial(base ?? {});
2297
+ },
2298
+ fromPartial(object) {
2299
+ const message = createBaseStatement_NamedParamsEntry();
2300
+ message.key = object.key ?? "";
2301
+ message.value = object.value !== void 0 && object.value !== null ? ColumnValue.fromPartial(object.value) : void 0;
2302
+ return message;
2303
+ }
2304
+ };
2305
+ function createBaseForwardRequest() {
2306
+ return { requestId: "", statements: [], groupId: "", primaryTerm: 0n };
2307
+ }
2308
+ var ForwardRequest = {
2309
+ encode(message, writer = new BinaryWriter()) {
2310
+ if (message.requestId !== "") {
2311
+ writer.uint32(10).string(message.requestId);
2312
+ }
2313
+ for (const v of message.statements) {
2314
+ Statement.encode(v, writer.uint32(18).fork()).join();
2315
+ }
2316
+ if (message.groupId !== "") {
2317
+ writer.uint32(26).string(message.groupId);
2318
+ }
2319
+ if (message.primaryTerm !== 0n) {
2320
+ if (BigInt.asIntN(64, message.primaryTerm) !== message.primaryTerm) {
2321
+ throw new globalThis.Error("value provided for field message.primaryTerm of type int64 too large");
2322
+ }
2323
+ writer.uint32(32).int64(message.primaryTerm);
2324
+ }
2325
+ return writer;
2326
+ },
2327
+ decode(input, length) {
2328
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
2329
+ const end = length === void 0 ? reader.len : reader.pos + length;
2330
+ const message = createBaseForwardRequest();
2331
+ while (reader.pos < end) {
2332
+ const tag = reader.uint32();
2333
+ switch (tag >>> 3) {
2334
+ case 1: {
2335
+ if (tag !== 10) {
2336
+ break;
2337
+ }
2338
+ message.requestId = reader.string();
2339
+ continue;
2340
+ }
2341
+ case 2: {
2342
+ if (tag !== 18) {
2343
+ break;
2344
+ }
2345
+ message.statements.push(Statement.decode(reader, reader.uint32()));
2346
+ continue;
2347
+ }
2348
+ case 3: {
2349
+ if (tag !== 26) {
2350
+ break;
2351
+ }
2352
+ message.groupId = reader.string();
2353
+ continue;
2354
+ }
2355
+ case 4: {
2356
+ if (tag !== 32) {
2357
+ break;
2358
+ }
2359
+ message.primaryTerm = reader.int64();
2360
+ continue;
2361
+ }
2362
+ }
2363
+ if ((tag & 7) === 4 || tag === 0) {
2364
+ break;
2365
+ }
2366
+ reader.skip(tag & 7);
2367
+ }
2368
+ return message;
2369
+ },
2370
+ fromJSON(object) {
2371
+ return {
2372
+ requestId: isSet(object.requestId) ? globalThis.String(object.requestId) : isSet(object.request_id) ? globalThis.String(object.request_id) : "",
2373
+ statements: globalThis.Array.isArray(object?.statements) ? object.statements.map((e) => Statement.fromJSON(e)) : [],
2374
+ groupId: isSet(object.groupId) ? globalThis.String(object.groupId) : isSet(object.group_id) ? globalThis.String(object.group_id) : "",
2375
+ primaryTerm: isSet(object.primaryTerm) ? BigInt(object.primaryTerm) : isSet(object.primary_term) ? BigInt(object.primary_term) : 0n
2376
+ };
2377
+ },
2378
+ toJSON(message) {
2379
+ const obj = {};
2380
+ if (message.requestId !== "") {
2381
+ obj.requestId = message.requestId;
2382
+ }
2383
+ if (message.statements?.length) {
2384
+ obj.statements = message.statements.map((e) => Statement.toJSON(e));
2385
+ }
2386
+ if (message.groupId !== "") {
2387
+ obj.groupId = message.groupId;
2388
+ }
2389
+ if (message.primaryTerm !== 0n) {
2390
+ obj.primaryTerm = message.primaryTerm.toString();
2391
+ }
2392
+ return obj;
2393
+ },
2394
+ create(base) {
2395
+ return ForwardRequest.fromPartial(base ?? {});
2396
+ },
2397
+ fromPartial(object) {
2398
+ const message = createBaseForwardRequest();
2399
+ message.requestId = object.requestId ?? "";
2400
+ message.statements = object.statements?.map((e) => Statement.fromPartial(e)) || [];
2401
+ message.groupId = object.groupId ?? "";
2402
+ message.primaryTerm = object.primaryTerm ?? 0n;
2403
+ return message;
2404
+ }
2405
+ };
2406
+ function createBaseStatementResult() {
2407
+ return { changes: 0, lastInsertRowId: 0n };
2408
+ }
2409
+ var StatementResult = {
2410
+ encode(message, writer = new BinaryWriter()) {
2411
+ if (message.changes !== 0) {
2412
+ writer.uint32(8).int32(message.changes);
2413
+ }
2414
+ if (message.lastInsertRowId !== 0n) {
2415
+ if (BigInt.asIntN(64, message.lastInsertRowId) !== message.lastInsertRowId) {
2416
+ throw new globalThis.Error("value provided for field message.lastInsertRowId of type int64 too large");
2417
+ }
2418
+ writer.uint32(16).int64(message.lastInsertRowId);
2419
+ }
2420
+ return writer;
2421
+ },
2422
+ decode(input, length) {
2423
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
2424
+ const end = length === void 0 ? reader.len : reader.pos + length;
2425
+ const message = createBaseStatementResult();
2426
+ while (reader.pos < end) {
2427
+ const tag = reader.uint32();
2428
+ switch (tag >>> 3) {
2429
+ case 1: {
2430
+ if (tag !== 8) {
2431
+ break;
2432
+ }
2433
+ message.changes = reader.int32();
2434
+ continue;
2435
+ }
2436
+ case 2: {
2437
+ if (tag !== 16) {
2438
+ break;
2439
+ }
2440
+ message.lastInsertRowId = reader.int64();
2441
+ continue;
2442
+ }
2443
+ }
2444
+ if ((tag & 7) === 4 || tag === 0) {
2445
+ break;
2446
+ }
2447
+ reader.skip(tag & 7);
2448
+ }
2449
+ return message;
2450
+ },
2451
+ fromJSON(object) {
2452
+ return {
2453
+ changes: isSet(object.changes) ? globalThis.Number(object.changes) : 0,
2454
+ lastInsertRowId: isSet(object.lastInsertRowId) ? BigInt(object.lastInsertRowId) : isSet(object.last_insert_row_id) ? BigInt(object.last_insert_row_id) : 0n
2455
+ };
2456
+ },
2457
+ toJSON(message) {
2458
+ const obj = {};
2459
+ if (message.changes !== 0) {
2460
+ obj.changes = Math.round(message.changes);
2461
+ }
2462
+ if (message.lastInsertRowId !== 0n) {
2463
+ obj.lastInsertRowId = message.lastInsertRowId.toString();
2464
+ }
2465
+ return obj;
2466
+ },
2467
+ create(base) {
2468
+ return StatementResult.fromPartial(base ?? {});
2469
+ },
2470
+ fromPartial(object) {
2471
+ const message = createBaseStatementResult();
2472
+ message.changes = object.changes ?? 0;
2473
+ message.lastInsertRowId = object.lastInsertRowId ?? 0n;
2474
+ return message;
2475
+ }
2476
+ };
2477
+ function createBaseForwardResponse() {
2478
+ return { requestId: "", results: [], error: "", groupId: "", primaryTerm: 0n };
2479
+ }
2480
+ var ForwardResponse = {
2481
+ encode(message, writer = new BinaryWriter()) {
2482
+ if (message.requestId !== "") {
2483
+ writer.uint32(10).string(message.requestId);
2484
+ }
2485
+ for (const v of message.results) {
2486
+ StatementResult.encode(v, writer.uint32(18).fork()).join();
2487
+ }
2488
+ if (message.error !== "") {
2489
+ writer.uint32(26).string(message.error);
2490
+ }
2491
+ if (message.groupId !== "") {
2492
+ writer.uint32(34).string(message.groupId);
2493
+ }
2494
+ if (message.primaryTerm !== 0n) {
2495
+ if (BigInt.asIntN(64, message.primaryTerm) !== message.primaryTerm) {
2496
+ throw new globalThis.Error("value provided for field message.primaryTerm of type int64 too large");
2497
+ }
2498
+ writer.uint32(40).int64(message.primaryTerm);
2499
+ }
2500
+ return writer;
2501
+ },
2502
+ decode(input, length) {
2503
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
2504
+ const end = length === void 0 ? reader.len : reader.pos + length;
2505
+ const message = createBaseForwardResponse();
2506
+ while (reader.pos < end) {
2507
+ const tag = reader.uint32();
2508
+ switch (tag >>> 3) {
2509
+ case 1: {
2510
+ if (tag !== 10) {
2511
+ break;
2512
+ }
2513
+ message.requestId = reader.string();
2514
+ continue;
2515
+ }
2516
+ case 2: {
2517
+ if (tag !== 18) {
2518
+ break;
2519
+ }
2520
+ message.results.push(StatementResult.decode(reader, reader.uint32()));
2521
+ continue;
2522
+ }
2523
+ case 3: {
2524
+ if (tag !== 26) {
2525
+ break;
2526
+ }
2527
+ message.error = reader.string();
2528
+ continue;
2529
+ }
2530
+ case 4: {
2531
+ if (tag !== 34) {
2532
+ break;
2533
+ }
2534
+ message.groupId = reader.string();
2535
+ continue;
2536
+ }
2537
+ case 5: {
2538
+ if (tag !== 40) {
2539
+ break;
2540
+ }
2541
+ message.primaryTerm = reader.int64();
2542
+ continue;
2543
+ }
2544
+ }
2545
+ if ((tag & 7) === 4 || tag === 0) {
2546
+ break;
2547
+ }
2548
+ reader.skip(tag & 7);
2549
+ }
2550
+ return message;
2551
+ },
2552
+ fromJSON(object) {
2553
+ return {
2554
+ requestId: isSet(object.requestId) ? globalThis.String(object.requestId) : isSet(object.request_id) ? globalThis.String(object.request_id) : "",
2555
+ results: globalThis.Array.isArray(object?.results) ? object.results.map((e) => StatementResult.fromJSON(e)) : [],
2556
+ error: isSet(object.error) ? globalThis.String(object.error) : "",
2557
+ groupId: isSet(object.groupId) ? globalThis.String(object.groupId) : isSet(object.group_id) ? globalThis.String(object.group_id) : "",
2558
+ primaryTerm: isSet(object.primaryTerm) ? BigInt(object.primaryTerm) : isSet(object.primary_term) ? BigInt(object.primary_term) : 0n
2559
+ };
2560
+ },
2561
+ toJSON(message) {
2562
+ const obj = {};
2563
+ if (message.requestId !== "") {
2564
+ obj.requestId = message.requestId;
2565
+ }
2566
+ if (message.results?.length) {
2567
+ obj.results = message.results.map((e) => StatementResult.toJSON(e));
2568
+ }
2569
+ if (message.error !== "") {
2570
+ obj.error = message.error;
2571
+ }
2572
+ if (message.groupId !== "") {
2573
+ obj.groupId = message.groupId;
2574
+ }
2575
+ if (message.primaryTerm !== 0n) {
2576
+ obj.primaryTerm = message.primaryTerm.toString();
2577
+ }
2578
+ return obj;
2579
+ },
2580
+ create(base) {
2581
+ return ForwardResponse.fromPartial(base ?? {});
2582
+ },
2583
+ fromPartial(object) {
2584
+ const message = createBaseForwardResponse();
2585
+ message.requestId = object.requestId ?? "";
2586
+ message.results = object.results?.map((e) => StatementResult.fromPartial(e)) || [];
2587
+ message.error = object.error ?? "";
2588
+ message.groupId = object.groupId ?? "";
2589
+ message.primaryTerm = object.primaryTerm ?? 0n;
2590
+ return message;
2591
+ }
2592
+ };
2593
+ var ReplicationService = {
2594
+ replicate: {
2595
+ path: "/sirannon.replication.v1.Replication/Replicate",
2596
+ requestStream: true,
2597
+ responseStream: true,
2598
+ requestSerialize: (value) => Buffer.from(ReplicationMessage.encode(value).finish()),
2599
+ requestDeserialize: (value) => ReplicationMessage.decode(value),
2600
+ responseSerialize: (value) => Buffer.from(ReplicationMessage.encode(value).finish()),
2601
+ responseDeserialize: (value) => ReplicationMessage.decode(value)
2602
+ },
2603
+ sync: {
2604
+ path: "/sirannon.replication.v1.Replication/Sync",
2605
+ requestStream: true,
2606
+ responseStream: true,
2607
+ requestSerialize: (value) => Buffer.from(SyncMessage.encode(value).finish()),
2608
+ requestDeserialize: (value) => SyncMessage.decode(value),
2609
+ responseSerialize: (value) => Buffer.from(SyncMessage.encode(value).finish()),
2610
+ responseDeserialize: (value) => SyncMessage.decode(value)
2611
+ },
2612
+ forward: {
2613
+ path: "/sirannon.replication.v1.Replication/Forward",
2614
+ requestStream: false,
2615
+ responseStream: false,
2616
+ requestSerialize: (value) => Buffer.from(ForwardRequest.encode(value).finish()),
2617
+ requestDeserialize: (value) => ForwardRequest.decode(value),
2618
+ responseSerialize: (value) => Buffer.from(ForwardResponse.encode(value).finish()),
2619
+ responseDeserialize: (value) => ForwardResponse.decode(value)
2620
+ }
2621
+ };
2622
+ var ReplicationClient = makeGenericClientConstructor(
2623
+ ReplicationService,
2624
+ "sirannon.replication.v1.Replication"
2625
+ );
2626
+ function bytesFromBase64(b64) {
2627
+ return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
2628
+ }
2629
+ function base64FromBytes(arr) {
2630
+ return globalThis.Buffer.from(arr).toString("base64");
2631
+ }
2632
+ function isObject(value) {
2633
+ return typeof value === "object" && value !== null;
2634
+ }
2635
+ function isSet(value) {
2636
+ return value !== null && value !== void 0;
2637
+ }
2638
+ function buildServerCreds(options) {
2639
+ if (options.insecure) {
2640
+ return ServerCredentials.createInsecure();
2641
+ }
2642
+ if (!options.tlsCert || !options.tlsKey) {
2643
+ throw new TransportError("TLS certificate and key paths are required when insecure mode is disabled");
2644
+ }
2645
+ const cert = readFileSync(options.tlsCert);
2646
+ const key = readFileSync(options.tlsKey);
2647
+ const ca = options.tlsCaCert ? readFileSync(options.tlsCaCert) : null;
2648
+ return ServerCredentials.createSsl(ca, [{ private_key: key, cert_chain: cert }], ca !== null);
2649
+ }
2650
+ function buildChannelCreds(options) {
2651
+ if (options.insecure) {
2652
+ return credentials.createInsecure();
2653
+ }
2654
+ if (!options.tlsCert || !options.tlsKey) {
2655
+ throw new TransportError("TLS certificate and key paths are required when insecure mode is disabled");
2656
+ }
2657
+ const cert = readFileSync(options.tlsCert);
2658
+ const key = readFileSync(options.tlsKey);
2659
+ const ca = options.tlsCaCert ? readFileSync(options.tlsCaCert) : null;
2660
+ return credentials.createSsl(ca, key, cert);
2661
+ }
2662
+
2663
+ // src/transport/grpc/peer-streams.ts
2664
+ function registerPeer(connectedPeers, peerConnectedHandler, nodeId, role, metadata = {}) {
2665
+ if (connectedPeers.has(nodeId)) return;
2666
+ const peerInfo = {
2667
+ id: nodeId,
2668
+ groupId: metadata.groupId,
2669
+ role,
2670
+ primaryTerm: metadata.primaryTerm,
2671
+ protocolVersion: metadata.protocolVersion,
2672
+ joinedAt: Date.now(),
2673
+ lastSeenAt: Date.now(),
2674
+ lastAckedSeq: 0n
2675
+ };
2676
+ connectedPeers.set(nodeId, peerInfo);
2677
+ peerConnectedHandler?.(peerInfo);
2678
+ }
2679
+ function removePeer(connectedPeers, serverPeerStreams, peerDisconnectedHandler, nodeId) {
2680
+ if (!connectedPeers.has(nodeId)) return;
2681
+ connectedPeers.delete(nodeId);
2682
+ serverPeerStreams.delete(nodeId);
2683
+ peerDisconnectedHandler?.(nodeId);
2684
+ }
2685
+
2686
+ // src/transport/grpc/client-streams.ts
2687
+ async function connectToEndpoint(t, endpoint) {
2688
+ const channelCreds = buildChannelCreds(t.options);
2689
+ const clientOpts = {};
2690
+ const client = new ReplicationClient(endpoint, channelCreds, clientOpts);
2691
+ const replicateStream = client.replicate();
2692
+ const syncStream = client.sync();
2693
+ const entry = {
2694
+ client,
2695
+ replicateStream,
2696
+ syncStream
2697
+ };
2698
+ replicateStream.write({
2699
+ hello: {
2700
+ nodeId: t.localNodeId,
2701
+ role: t.localRole,
2702
+ groupId: t.localGroupId ?? "",
2703
+ primaryTerm: t.localPrimaryTerm ?? 0n,
2704
+ protocolVersion: t.localProtocolVersion ?? ""
2705
+ }
2706
+ });
2707
+ syncStream.write({
2708
+ hello: {
2709
+ nodeId: t.localNodeId,
2710
+ role: t.localRole,
2711
+ groupId: t.localGroupId ?? "",
2712
+ primaryTerm: t.localPrimaryTerm ?? 0n,
2713
+ protocolVersion: t.localProtocolVersion ?? ""
2714
+ }
2715
+ });
2716
+ let replicatePeerId = null;
2717
+ replicateStream.on("data", (msg) => {
2718
+ if (replicatePeerId === null) {
2719
+ if (!msg.hello) {
2720
+ replicateStream.cancel();
2721
+ return;
2722
+ }
2723
+ replicatePeerId = msg.hello.nodeId;
2724
+ const peerRole = msg.hello.role;
2725
+ t.clientPeerStreams.set(replicatePeerId, entry);
2726
+ registerPeer(t.connectedPeers, t.peerConnectedHandler, replicatePeerId, peerRole, {
2727
+ groupId: msg.hello.groupId || void 0,
2728
+ primaryTerm: msg.hello.primaryTerm === 0n ? void 0 : msg.hello.primaryTerm,
2729
+ protocolVersion: msg.hello.protocolVersion || void 0
2730
+ });
2731
+ return;
2732
+ }
2733
+ if (msg.batch) {
2734
+ t.batchHandler?.(fromBatchPayload(msg.batch), replicatePeerId).catch(() => {
2735
+ });
2736
+ } else if (msg.ack) {
2737
+ t.ackHandler?.(fromAckPayload(msg.ack), replicatePeerId);
2738
+ }
2739
+ });
2740
+ replicateStream.on("end", () => {
2741
+ detachReplicate(t, replicatePeerId);
2742
+ });
2743
+ replicateStream.on("error", () => {
2744
+ detachReplicate(t, replicatePeerId);
2745
+ });
2746
+ let syncPeerId = null;
2747
+ syncStream.on("data", (msg) => {
2748
+ if (syncPeerId === null) {
2749
+ if (!msg.hello) {
2750
+ syncStream.cancel();
2751
+ return;
2752
+ }
2753
+ syncPeerId = msg.hello.nodeId;
2754
+ return;
2755
+ }
2756
+ if (msg.syncRequest) {
2757
+ t.syncRequestHandler?.(fromSyncRequestPayload(msg.syncRequest), syncPeerId).catch(() => {
2758
+ });
2759
+ } else if (msg.syncBatch) {
2760
+ t.syncBatchHandler?.(fromSyncBatchPayload(msg.syncBatch), syncPeerId).catch(() => {
2761
+ });
2762
+ } else if (msg.syncComplete) {
2763
+ t.syncCompleteHandler?.(fromSyncCompletePayload(msg.syncComplete), syncPeerId).catch(() => {
2764
+ });
2765
+ } else if (msg.syncAck) {
2766
+ t.syncAckHandler?.(fromSyncAckPayload(msg.syncAck), syncPeerId);
2767
+ }
2768
+ });
2769
+ syncStream.on("end", () => {
2770
+ detachSync(t, syncPeerId);
2771
+ });
2772
+ syncStream.on("error", () => {
2773
+ detachSync(t, syncPeerId);
2774
+ });
2775
+ }
2776
+ function detachReplicate(t, peerId) {
2777
+ if (!peerId) return;
2778
+ const e = t.clientPeerStreams.get(peerId);
2779
+ if (!e) return;
2780
+ e.replicateStream = null;
2781
+ if (!e.syncStream) {
2782
+ t.clientPeerStreams.delete(peerId);
2783
+ removePeer(t.connectedPeers, t.serverPeerStreams, t.peerDisconnectedHandler, peerId);
2784
+ }
2785
+ }
2786
+ function detachSync(t, peerId) {
2787
+ if (!peerId) return;
2788
+ const e = t.clientPeerStreams.get(peerId);
2789
+ if (!e) return;
2790
+ e.syncStream = null;
2791
+ if (!e.replicateStream) {
2792
+ t.clientPeerStreams.delete(peerId);
2793
+ removePeer(t.connectedPeers, t.serverPeerStreams, t.peerDisconnectedHandler, peerId);
2794
+ }
2795
+ }
2796
+
2797
+ // src/transport/grpc/options.ts
2798
+ var DEFAULT_FORWARD_DEADLINE_MS = 3e4;
2799
+ var SERVICE_NAME = "sirannon.replication.v1.Replication";
2800
+ async function startServer(t) {
2801
+ const server = new Server();
2802
+ t.server = server;
2803
+ const healthImpl = new HealthImplementation({
2804
+ [SERVICE_NAME]: "SERVING",
2805
+ "": "SERVING"
2806
+ });
2807
+ healthImpl.addToServer(server);
2808
+ t.healthImpl = healthImpl;
2809
+ server.addService(ReplicationService, {
2810
+ replicate: (call) => {
2811
+ handleServerReplicateStream(t, call);
2812
+ },
2813
+ sync: (call) => {
2814
+ handleServerSyncStream(t, call);
2815
+ },
2816
+ forward: (call, callback) => {
2817
+ handleForwardCall(t, call, callback);
2818
+ }
2819
+ });
2820
+ const host = t.options.host ?? "0.0.0.0";
2821
+ const port = t.options.port ?? 0;
2822
+ const bindAddress = `${host}:${port}`;
2823
+ const serverCreds = buildServerCreds(t.options);
2824
+ await new Promise((resolve, reject) => {
2825
+ server.bindAsync(bindAddress, serverCreds, (err, assignedPort) => {
2826
+ if (err) {
2827
+ reject(new TransportError(`Failed to bind gRPC server: ${err.message}`));
2828
+ return;
2829
+ }
2830
+ t.boundPort = assignedPort;
2831
+ resolve();
2832
+ });
2833
+ });
2834
+ }
2835
+ function handleServerReplicateStream(t, call) {
2836
+ let peerId = null;
2837
+ let helloSent = false;
2838
+ call.on("data", (msg) => {
2839
+ if (peerId === null) {
2840
+ if (!msg.hello) {
2841
+ call.destroy(new Error("First message must be Hello"));
2842
+ return;
2843
+ }
2844
+ if (!t.validateTlsIdentity(call, msg.hello.nodeId)) {
2845
+ call.destroy(new Error(`TLS certificate CN does not match claimed nodeId '${msg.hello.nodeId}'`));
2846
+ return;
2847
+ }
2848
+ peerId = msg.hello.nodeId;
2849
+ const peerRole = msg.hello.role;
2850
+ const existing = t.serverPeerStreams.get(peerId);
2851
+ if (existing?.replicateStream) {
2852
+ existing.replicateStream.end();
2853
+ }
2854
+ const ps = t.serverPeerStreams.get(peerId) ?? {
2855
+ replicateStream: null,
2856
+ syncStream: null
2857
+ };
2858
+ ps.replicateStream = call;
2859
+ t.serverPeerStreams.set(peerId, ps);
2860
+ if (!helloSent) {
2861
+ call.write({
2862
+ hello: {
2863
+ nodeId: t.localNodeId,
2864
+ role: t.localRole,
2865
+ groupId: t.localGroupId ?? "",
2866
+ primaryTerm: t.localPrimaryTerm ?? 0n,
2867
+ protocolVersion: t.localProtocolVersion ?? ""
2868
+ }
2869
+ });
2870
+ helloSent = true;
2871
+ }
2872
+ registerPeer(t.connectedPeers, t.peerConnectedHandler, peerId, peerRole, {
2873
+ groupId: msg.hello.groupId || void 0,
2874
+ primaryTerm: msg.hello.primaryTerm === 0n ? void 0 : msg.hello.primaryTerm,
2875
+ protocolVersion: msg.hello.protocolVersion || void 0
2876
+ });
2877
+ return;
2878
+ }
2879
+ if (msg.batch) {
2880
+ const batch = fromBatchPayload(msg.batch);
2881
+ t.batchHandler?.(batch, peerId).catch(() => {
2882
+ });
2883
+ } else if (msg.ack) {
2884
+ const ack = fromAckPayload(msg.ack);
2885
+ t.ackHandler?.(ack, peerId);
2886
+ }
2887
+ });
2888
+ call.on("end", () => {
2889
+ call.end();
2890
+ if (peerId) {
2891
+ const ps = t.serverPeerStreams.get(peerId);
2892
+ if (ps?.replicateStream === call) {
2893
+ ps.replicateStream = null;
2894
+ }
2895
+ if (!ps?.syncStream) {
2896
+ removePeer(t.connectedPeers, t.serverPeerStreams, t.peerDisconnectedHandler, peerId);
2897
+ }
2898
+ }
2899
+ });
2900
+ call.on("error", () => {
2901
+ if (peerId) {
2902
+ const ps = t.serverPeerStreams.get(peerId);
2903
+ if (ps?.replicateStream === call) {
2904
+ ps.replicateStream = null;
2905
+ }
2906
+ if (!ps?.syncStream) {
2907
+ removePeer(t.connectedPeers, t.serverPeerStreams, t.peerDisconnectedHandler, peerId);
2908
+ }
2909
+ }
2910
+ });
2911
+ }
2912
+ function handleServerSyncStream(t, call) {
2913
+ let peerId = null;
2914
+ let helloSent = false;
2915
+ call.on("data", (msg) => {
2916
+ if (peerId === null) {
2917
+ if (!msg.hello) {
2918
+ call.destroy(new Error("First message must be Hello"));
2919
+ return;
2920
+ }
2921
+ if (!t.validateTlsIdentity(call, msg.hello.nodeId)) {
2922
+ call.destroy(new Error(`TLS certificate CN does not match claimed nodeId '${msg.hello.nodeId}'`));
2923
+ return;
2924
+ }
2925
+ peerId = msg.hello.nodeId;
2926
+ const peerRole = msg.hello.role;
2927
+ const existing = t.serverPeerStreams.get(peerId);
2928
+ if (existing?.syncStream) {
2929
+ existing.syncStream.end();
2930
+ }
2931
+ const ps = t.serverPeerStreams.get(peerId) ?? {
2932
+ replicateStream: null,
2933
+ syncStream: null
2934
+ };
2935
+ ps.syncStream = call;
2936
+ t.serverPeerStreams.set(peerId, ps);
2937
+ if (!helloSent) {
2938
+ call.write({
2939
+ hello: {
2940
+ nodeId: t.localNodeId,
2941
+ role: t.localRole,
2942
+ groupId: t.localGroupId ?? "",
2943
+ primaryTerm: t.localPrimaryTerm ?? 0n,
2944
+ protocolVersion: t.localProtocolVersion ?? ""
2945
+ }
2946
+ });
2947
+ helloSent = true;
2948
+ }
2949
+ registerPeer(t.connectedPeers, t.peerConnectedHandler, peerId, peerRole, {
2950
+ groupId: msg.hello.groupId || void 0,
2951
+ primaryTerm: msg.hello.primaryTerm === 0n ? void 0 : msg.hello.primaryTerm,
2952
+ protocolVersion: msg.hello.protocolVersion || void 0
2953
+ });
2954
+ return;
2955
+ }
2956
+ if (msg.syncRequest) {
2957
+ t.syncRequestHandler?.(fromSyncRequestPayload(msg.syncRequest), peerId).catch(() => {
2958
+ });
2959
+ } else if (msg.syncBatch) {
2960
+ t.syncBatchHandler?.(fromSyncBatchPayload(msg.syncBatch), peerId).catch(() => {
2961
+ });
2962
+ } else if (msg.syncComplete) {
2963
+ t.syncCompleteHandler?.(fromSyncCompletePayload(msg.syncComplete), peerId).catch(() => {
2964
+ });
2965
+ } else if (msg.syncAck) {
2966
+ t.syncAckHandler?.(fromSyncAckPayload(msg.syncAck), peerId);
2967
+ }
2968
+ });
2969
+ call.on("end", () => {
2970
+ call.end();
2971
+ if (peerId) {
2972
+ const ps = t.serverPeerStreams.get(peerId);
2973
+ if (ps?.syncStream === call) {
2974
+ ps.syncStream = null;
2975
+ }
2976
+ if (!ps?.replicateStream) {
2977
+ removePeer(t.connectedPeers, t.serverPeerStreams, t.peerDisconnectedHandler, peerId);
2978
+ }
2979
+ }
2980
+ });
2981
+ call.on("error", () => {
2982
+ if (peerId) {
2983
+ const ps = t.serverPeerStreams.get(peerId);
2984
+ if (ps?.syncStream === call) {
2985
+ ps.syncStream = null;
2986
+ }
2987
+ if (!ps?.replicateStream) {
2988
+ removePeer(t.connectedPeers, t.serverPeerStreams, t.peerDisconnectedHandler, peerId);
2989
+ }
2990
+ }
2991
+ });
2992
+ }
2993
+ function handleForwardCall(t, call, callback) {
2994
+ if (!t.forwardHandler) {
2995
+ callback({
2996
+ code: status.UNIMPLEMENTED,
2997
+ message: "Forward handler not registered"
2998
+ });
2999
+ return;
3000
+ }
3001
+ const peerId = t.resolveForwardPeerId(call);
3002
+ if (!peerId) {
3003
+ callback({
3004
+ code: status.UNAUTHENTICATED,
3005
+ message: "Forward request from unidentified peer"
3006
+ });
3007
+ return;
3008
+ }
3009
+ const appReq = fromForwardRequest(call.request);
3010
+ t.forwardHandler(appReq, peerId).then((result) => {
3011
+ callback(null, {
3012
+ requestId: result.requestId,
3013
+ results: result.results.map((r) => ({
3014
+ changes: r.changes,
3015
+ lastInsertRowId: BigInt(typeof r.lastInsertRowId === "string" ? r.lastInsertRowId : r.lastInsertRowId)
3016
+ })),
3017
+ error: "",
3018
+ groupId: result.groupId ?? "",
3019
+ primaryTerm: result.primaryTerm ?? 0n
3020
+ });
3021
+ }).catch((err) => {
3022
+ callback(null, {
3023
+ requestId: call.request.requestId,
3024
+ results: [],
3025
+ error: err.message,
3026
+ groupId: call.request.groupId ?? "",
3027
+ primaryTerm: call.request.primaryTerm ?? 0n
3028
+ });
3029
+ });
3030
+ }
3031
+
3032
+ // src/transport/grpc/stream-util.ts
3033
+ function writeWithBackpressure(stream, message) {
3034
+ return new Promise((resolve, reject) => {
3035
+ const ok = stream.write(message);
3036
+ if (ok) {
3037
+ resolve();
3038
+ return;
3039
+ }
3040
+ const onDrain = () => {
3041
+ stream.removeListener("error", onError);
3042
+ resolve();
3043
+ };
3044
+ const onError = (...args) => {
3045
+ stream.removeListener("drain", onDrain);
3046
+ const err = args[0];
3047
+ reject(err instanceof Error ? err : new Error(String(err)));
3048
+ };
3049
+ stream.once("drain", onDrain);
3050
+ stream.once("error", onError);
3051
+ });
3052
+ }
3053
+
3054
+ // src/transport/grpc/transport.ts
3055
+ var GrpcReplicationTransport = class {
3056
+ options;
3057
+ localNodeId = "";
3058
+ localRole = "replica";
3059
+ localGroupId;
3060
+ localPrimaryTerm;
3061
+ localProtocolVersion;
3062
+ connected = false;
3063
+ server = null;
3064
+ boundPort = 0;
3065
+ healthImpl = null;
3066
+ connectedPeers = /* @__PURE__ */ new Map();
3067
+ serverPeerStreams = /* @__PURE__ */ new Map();
3068
+ clientPeerStreams = /* @__PURE__ */ new Map();
3069
+ batchHandler = null;
3070
+ ackHandler = null;
3071
+ forwardHandler = null;
3072
+ peerConnectedHandler = null;
3073
+ peerDisconnectedHandler = null;
3074
+ syncRequestHandler = null;
3075
+ syncBatchHandler = null;
3076
+ syncCompleteHandler = null;
3077
+ syncAckHandler = null;
3078
+ constructor(options = {}) {
3079
+ this.options = options;
3080
+ }
3081
+ getPort() {
3082
+ return this.boundPort;
3083
+ }
3084
+ async connect(localNodeId, config) {
3085
+ if (this.connected) {
3086
+ throw new TransportError("Transport is already connected");
3087
+ }
3088
+ if (!localNodeId || typeof localNodeId !== "string") {
3089
+ throw new TransportError("localNodeId must be a non-empty string");
3090
+ }
3091
+ this.localNodeId = localNodeId;
3092
+ this.localRole = config.localRole ?? "replica";
3093
+ this.localGroupId = config.groupId;
3094
+ this.localPrimaryTerm = config.primaryTerm;
3095
+ this.localProtocolVersion = config.protocolVersion;
3096
+ this.connected = true;
3097
+ if (this.localRole === "primary" || config.groupId) {
3098
+ await startServer(this);
3099
+ }
3100
+ if (config.endpoints && config.endpoints.length > 0) {
3101
+ for (const endpoint of config.endpoints) {
3102
+ await connectToEndpoint(this, endpoint);
3103
+ }
3104
+ }
3105
+ }
3106
+ async disconnect() {
3107
+ if (!this.connected) return;
3108
+ this.connected = false;
3109
+ for (const [peerId, entry] of this.clientPeerStreams) {
3110
+ entry.replicateStream?.cancel();
3111
+ entry.syncStream?.cancel();
3112
+ entry.client.close();
3113
+ this.clientPeerStreams.delete(peerId);
3114
+ }
3115
+ for (const [peerId, ps] of this.serverPeerStreams) {
3116
+ ps.replicateStream?.end();
3117
+ ps.syncStream?.end();
3118
+ this.serverPeerStreams.delete(peerId);
3119
+ }
3120
+ const peerIds = [...this.connectedPeers.keys()];
3121
+ this.connectedPeers.clear();
3122
+ for (const peerId of peerIds) {
3123
+ this.peerDisconnectedHandler?.(peerId);
3124
+ }
3125
+ if (this.healthImpl) {
3126
+ this.healthImpl.setStatus(SERVICE_NAME, "NOT_SERVING");
3127
+ }
3128
+ if (this.server) {
3129
+ await new Promise((resolve) => {
3130
+ const srv = this.server;
3131
+ if (!srv) {
3132
+ resolve();
3133
+ return;
3134
+ }
3135
+ srv.tryShutdown((err) => {
3136
+ if (err) {
3137
+ srv.forceShutdown();
3138
+ }
3139
+ resolve();
3140
+ });
3141
+ });
3142
+ this.server = null;
3143
+ }
3144
+ }
3145
+ async send(peerId, batch) {
3146
+ this.ensureConnected();
3147
+ const stream = this.getReplicateWriteStream(peerId);
3148
+ if (!stream) {
3149
+ throw new TransportError(`Peer '${peerId}' is not connected`);
3150
+ }
3151
+ const msg = { batch: toBatchPayload(batch) };
3152
+ await writeWithBackpressure(stream, msg);
3153
+ }
3154
+ async broadcast(batch) {
3155
+ this.ensureConnected();
3156
+ const msg = { batch: toBatchPayload(batch) };
3157
+ const promises = [];
3158
+ for (const [peerId] of this.connectedPeers) {
3159
+ const stream = this.getReplicateWriteStream(peerId);
3160
+ if (stream) {
3161
+ promises.push(writeWithBackpressure(stream, msg));
3162
+ }
3163
+ }
3164
+ await Promise.all(promises);
3165
+ }
3166
+ async sendAck(peerId, ack) {
3167
+ this.ensureConnected();
3168
+ const stream = this.getReplicateWriteStream(peerId);
3169
+ if (!stream) {
3170
+ throw new TransportError(`Peer '${peerId}' is not connected`);
3171
+ }
3172
+ const msg = { ack: toAckPayload(ack) };
3173
+ await writeWithBackpressure(stream, msg);
3174
+ }
3175
+ async forward(peerId, request) {
3176
+ this.ensureConnected();
3177
+ const clientEntry = this.clientPeerStreams.get(peerId);
3178
+ if (!clientEntry) {
3179
+ throw new TransportError(`Peer '${peerId}' is not connected`);
3180
+ }
3181
+ const protoReq = toForwardRequest(request);
3182
+ const deadlineMs = this.options.forwardDeadlineMs ?? DEFAULT_FORWARD_DEADLINE_MS;
3183
+ const deadline = new Date(Date.now() + deadlineMs);
3184
+ return new Promise((resolve, reject) => {
3185
+ clientEntry.client.forward(
3186
+ protoReq,
3187
+ new Metadata(),
3188
+ { deadline },
3189
+ (err, response) => {
3190
+ if (err) {
3191
+ reject(new TransportError(`Forward RPC failed: ${err.message}`));
3192
+ return;
3193
+ }
3194
+ if (!response) {
3195
+ reject(new TransportError("Forward RPC returned empty response"));
3196
+ return;
3197
+ }
3198
+ if (response.error) {
3199
+ reject(new TransportError(`Forward RPC error: ${response.error}`));
3200
+ return;
3201
+ }
3202
+ resolve({
3203
+ requestId: response.requestId,
3204
+ results: response.results.map((r) => ({
3205
+ changes: r.changes,
3206
+ lastInsertRowId: Number(r.lastInsertRowId)
3207
+ })),
3208
+ groupId: response.groupId || void 0,
3209
+ primaryTerm: response.primaryTerm === 0n ? void 0 : response.primaryTerm
3210
+ });
3211
+ }
3212
+ );
3213
+ });
3214
+ }
3215
+ async requestSync(peerId, request) {
3216
+ this.ensureConnected();
3217
+ const stream = this.getSyncWriteStream(peerId);
3218
+ if (!stream) {
3219
+ throw new TransportError(`Peer '${peerId}' is not connected`);
3220
+ }
3221
+ const msg = { syncRequest: toSyncRequestPayload(request) };
3222
+ await writeWithBackpressure(stream, msg);
3223
+ }
3224
+ async sendSyncBatch(peerId, batch) {
3225
+ this.ensureConnected();
3226
+ const stream = this.getSyncWriteStream(peerId);
3227
+ if (!stream) {
3228
+ throw new TransportError(`Peer '${peerId}' is not connected`);
3229
+ }
3230
+ const msg = { syncBatch: toSyncBatchPayload(batch) };
3231
+ await writeWithBackpressure(stream, msg);
3232
+ }
3233
+ async sendSyncComplete(peerId, complete) {
3234
+ this.ensureConnected();
3235
+ const stream = this.getSyncWriteStream(peerId);
3236
+ if (!stream) {
3237
+ throw new TransportError(`Peer '${peerId}' is not connected`);
3238
+ }
3239
+ const msg = { syncComplete: toSyncCompletePayload(complete) };
3240
+ await writeWithBackpressure(stream, msg);
3241
+ }
3242
+ async sendSyncAck(peerId, ack) {
3243
+ this.ensureConnected();
3244
+ const stream = this.getSyncWriteStream(peerId);
3245
+ if (!stream) {
3246
+ throw new TransportError(`Peer '${peerId}' is not connected`);
3247
+ }
3248
+ const msg = { syncAck: toSyncAckPayload(ack) };
3249
+ await writeWithBackpressure(stream, msg);
3250
+ }
3251
+ onBatchReceived(handler) {
3252
+ this.batchHandler = handler;
3253
+ }
3254
+ onAckReceived(handler) {
3255
+ this.ackHandler = handler;
3256
+ }
3257
+ onForwardReceived(handler) {
3258
+ this.forwardHandler = handler;
3259
+ }
3260
+ onSyncRequested(handler) {
3261
+ this.syncRequestHandler = handler;
3262
+ }
3263
+ onSyncBatchReceived(handler) {
3264
+ this.syncBatchHandler = handler;
3265
+ }
3266
+ onSyncCompleteReceived(handler) {
3267
+ this.syncCompleteHandler = handler;
3268
+ }
3269
+ onSyncAckReceived(handler) {
3270
+ this.syncAckHandler = handler;
3271
+ }
3272
+ onPeerConnected(handler) {
3273
+ this.peerConnectedHandler = handler;
3274
+ }
3275
+ onPeerDisconnected(handler) {
3276
+ this.peerDisconnectedHandler = handler;
3277
+ }
3278
+ peers() {
3279
+ return this.connectedPeers;
3280
+ }
3281
+ extractTlsCN(call) {
3282
+ if (this.options.insecure) return null;
3283
+ if (!this.options.tlsCaCert) return null;
3284
+ const authCtx = call.getAuthContext();
3285
+ if (!authCtx) return null;
3286
+ const peerCert = authCtx.sslPeerCertificate;
3287
+ if (!peerCert) return null;
3288
+ const certCN = peerCert.subject?.CN;
3289
+ if (!certCN) return null;
3290
+ if (Array.isArray(certCN)) return certCN[0] ?? null;
3291
+ return certCN;
3292
+ }
3293
+ validateTlsIdentity(call, claimedNodeId) {
3294
+ if (this.options.insecure) return true;
3295
+ if (!this.options.tlsCaCert) return true;
3296
+ const cn = this.extractTlsCN(call);
3297
+ return cn === claimedNodeId;
3298
+ }
3299
+ resolveForwardPeerId(call) {
3300
+ const cn = this.extractTlsCN(call);
3301
+ if (cn) {
3302
+ return this.connectedPeers.has(cn) ? cn : null;
3303
+ }
3304
+ if (!this.options.insecure) return null;
3305
+ const peerAddr = call.getPeer();
3306
+ for (const [, entry] of this.serverPeerStreams) {
3307
+ if (entry.replicateStream?.getPeer() === peerAddr) {
3308
+ const streamPeerId = this.findPeerIdForStream(entry);
3309
+ if (streamPeerId) return streamPeerId;
3310
+ }
3311
+ }
3312
+ return null;
3313
+ }
3314
+ findPeerIdForStream(entry) {
3315
+ for (const [peerId, ps] of this.serverPeerStreams) {
3316
+ if (ps === entry) return peerId;
3317
+ }
3318
+ return null;
3319
+ }
3320
+ ensureConnected() {
3321
+ if (!this.connected) {
3322
+ throw new TransportError("Transport is not connected");
3323
+ }
3324
+ }
3325
+ getReplicateWriteStream(peerId) {
3326
+ const serverStream = this.serverPeerStreams.get(peerId);
3327
+ if (serverStream?.replicateStream) return serverStream.replicateStream;
3328
+ const clientEntry = this.clientPeerStreams.get(peerId);
3329
+ if (clientEntry?.replicateStream) return clientEntry.replicateStream;
3330
+ return null;
3331
+ }
3332
+ getSyncWriteStream(peerId) {
3333
+ const serverStream = this.serverPeerStreams.get(peerId);
3334
+ if (serverStream?.syncStream) return serverStream.syncStream;
3335
+ const clientEntry = this.clientPeerStreams.get(peerId);
3336
+ if (clientEntry?.syncStream) return clientEntry.syncStream;
3337
+ return null;
3338
+ }
3339
+ };
3340
+
3341
+ export { GrpcReplicationTransport, fromColumnValue, toColumnValue };