@logbrew/sdk 0.1.22 → 0.1.23
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 +21 -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,6 +845,17 @@ 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;
|
|
@@ -1194,6 +1199,14 @@ function loadStoredEvents({
|
|
|
1194
1199
|
return { events, queuedEventBytes, serializedEventBytes, serializedEvents };
|
|
1195
1200
|
}
|
|
1196
1201
|
|
|
1202
|
+
function isOrderedSubsequence(expected, actual) {
|
|
1203
|
+
let index = 0;
|
|
1204
|
+
for (const value of actual) {
|
|
1205
|
+
index += value === expected[index] ? 1 : 0;
|
|
1206
|
+
}
|
|
1207
|
+
return index === expected.length;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1197
1210
|
function structuralJson(value) {
|
|
1198
1211
|
return JSON.stringify(value, (_, entry) => entry && !Array.isArray(entry) && typeof entry === "object"
|
|
1199
1212
|
? Object.fromEntries(Object.entries(entry).sort(([left], [right]) => left.localeCompare(right))) : entry);
|