@logbrew/sdk 0.1.22 → 0.1.24
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.
- package/core.cjs +26 -8
- package/package.json +1 -1
package/core.cjs
CHANGED
|
@@ -334,14 +334,7 @@ class LogBrewClient {
|
|
|
334
334
|
this.eventStore = resolvedEventStore;
|
|
335
335
|
let recovered;
|
|
336
336
|
try {
|
|
337
|
-
recovered = loadStoredEvents(
|
|
338
|
-
batchPrefixBytes: this.batchPrefixBytes,
|
|
339
|
-
batchSuffixBytes: this.batchSuffixBytes,
|
|
340
|
-
eventStore: resolvedEventStore,
|
|
341
|
-
maxBatchBytes,
|
|
342
|
-
maxQueueBytes,
|
|
343
|
-
maxQueueSize
|
|
344
|
-
});
|
|
337
|
+
recovered = loadStoredEvents(this);
|
|
345
338
|
} catch (error) {
|
|
346
339
|
if (ownsEventStore) {
|
|
347
340
|
try {
|
|
@@ -822,6 +815,7 @@ class LogBrewClient {
|
|
|
822
815
|
}
|
|
823
816
|
|
|
824
817
|
async #flushSnapshot(transport) {
|
|
818
|
+
this.#synchronizeEventStore();
|
|
825
819
|
let remainingEvents = this.events.length;
|
|
826
820
|
if (remainingEvents === 0) {
|
|
827
821
|
return { statusCode: 204, attempts: 0, batches: 0 };
|
|
@@ -851,15 +845,31 @@ class LogBrewClient {
|
|
|
851
845
|
return { statusCode, attempts, batches };
|
|
852
846
|
}
|
|
853
847
|
|
|
848
|
+
#synchronizeEventStore() {
|
|
849
|
+
if (!this.eventStore) return;
|
|
850
|
+
const recovered = loadStoredEvents(this);
|
|
851
|
+
if (!isOrderedSubsequence(this.serializedEvents, recovered.serializedEvents))
|
|
852
|
+
throw new SdkError("persistence_error", "event store lost a queued record");
|
|
853
|
+
this.events = recovered.events;
|
|
854
|
+
this.serializedEvents = recovered.serializedEvents;
|
|
855
|
+
this.serializedEventBytes = recovered.serializedEventBytes;
|
|
856
|
+
this.queuedEventBytes = recovered.queuedEventBytes;
|
|
857
|
+
}
|
|
858
|
+
|
|
854
859
|
#nextBatch(maxEvents) {
|
|
855
860
|
let bodyBytes = this.batchPrefixBytes + this.batchSuffixBytes;
|
|
856
861
|
let eventsCount = 0;
|
|
862
|
+
const eventIds = new Set();
|
|
857
863
|
for (let index = 0; index < maxEvents; index += 1) {
|
|
864
|
+
if (eventIds.has(this.events[index].id)) {
|
|
865
|
+
break;
|
|
866
|
+
}
|
|
858
867
|
const separatorBytes = eventsCount === 0 ? 0 : 1;
|
|
859
868
|
const nextBodyBytes = bodyBytes + separatorBytes + this.serializedEventBytes[index];
|
|
860
869
|
if (nextBodyBytes > this.maxBatchBytes) {
|
|
861
870
|
break;
|
|
862
871
|
}
|
|
872
|
+
eventIds.add(this.events[index].id);
|
|
863
873
|
bodyBytes = nextBodyBytes;
|
|
864
874
|
eventsCount += 1;
|
|
865
875
|
}
|
|
@@ -1194,6 +1204,14 @@ function loadStoredEvents({
|
|
|
1194
1204
|
return { events, queuedEventBytes, serializedEventBytes, serializedEvents };
|
|
1195
1205
|
}
|
|
1196
1206
|
|
|
1207
|
+
function isOrderedSubsequence(expected, actual) {
|
|
1208
|
+
let index = 0;
|
|
1209
|
+
for (const value of actual) {
|
|
1210
|
+
index += value === expected[index] ? 1 : 0;
|
|
1211
|
+
}
|
|
1212
|
+
return index === expected.length;
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1197
1215
|
function structuralJson(value) {
|
|
1198
1216
|
return JSON.stringify(value, (_, entry) => entry && !Array.isArray(entry) && typeof entry === "object"
|
|
1199
1217
|
? Object.fromEntries(Object.entries(entry).sort(([left], [right]) => left.localeCompare(right))) : entry);
|