@logbrew/sdk 0.1.20 → 0.1.22

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 (2) hide show
  1. package/core.cjs +24 -7
  2. package/package.json +1 -1
package/core.cjs CHANGED
@@ -1194,6 +1194,11 @@ function loadStoredEvents({
1194
1194
  return { events, queuedEventBytes, serializedEventBytes, serializedEvents };
1195
1195
  }
1196
1196
 
1197
+ function structuralJson(value) {
1198
+ return JSON.stringify(value, (_, entry) => entry && !Array.isArray(entry) && typeof entry === "object"
1199
+ ? Object.fromEntries(Object.entries(entry).sort(([left], [right]) => left.localeCompare(right))) : entry);
1200
+ }
1201
+
1197
1202
  function normalizeStoredRecord(record) {
1198
1203
  try {
1199
1204
  if (!record || Array.isArray(record) || typeof record !== "object") {
@@ -1212,19 +1217,31 @@ function normalizeStoredRecord(record) {
1212
1217
  if (!event.attributes || Array.isArray(event.attributes) || typeof event.attributes !== "object") {
1213
1218
  throw invalidStoredRecord();
1214
1219
  }
1220
+ if (JSON.stringify(event) !== serializedEvent || utf8ByteLength(serializedEvent) !== eventBytes) {
1221
+ throw invalidStoredRecord();
1222
+ }
1223
+ const recoveredAttributes = event.type === "issue" && event.attributes.level === "fatal"
1224
+ ? { ...event.attributes, level: "critical" }
1225
+ : event.attributes;
1226
+ const context = recoveredAttributes.context;
1227
+ const recoveredEvent = context && !Array.isArray(context) && typeof context === "object"
1228
+ && context.schemaVersion === undefined
1229
+ ? { ...event, attributes: { ...recoveredAttributes, context: { schemaVersion: 1, ...context } } }
1230
+ : { ...event, attributes: recoveredAttributes };
1215
1231
  const normalizedEvent = {
1216
- type: event.type,
1217
- id: event.id,
1218
- timestamp: event.timestamp,
1219
- attributes: validator(event.attributes)
1232
+ type: recoveredEvent.type,
1233
+ id: recoveredEvent.id,
1234
+ timestamp: recoveredEvent.timestamp,
1235
+ attributes: validator(recoveredEvent.attributes)
1220
1236
  };
1221
- if (JSON.stringify(normalizedEvent) !== serializedEvent || utf8ByteLength(serializedEvent) !== eventBytes) {
1237
+ const normalizedSerializedEvent = JSON.stringify(normalizedEvent);
1238
+ if (structuralJson(normalizedEvent) !== structuralJson(recoveredEvent)) {
1222
1239
  throw invalidStoredRecord();
1223
1240
  }
1224
1241
  return {
1225
1242
  event: cloneEvent(normalizedEvent),
1226
- eventBytes,
1227
- serializedEvent
1243
+ eventBytes: utf8ByteLength(normalizedSerializedEvent),
1244
+ serializedEvent: normalizedSerializedEvent
1228
1245
  };
1229
1246
  } catch (error) {
1230
1247
  if (error instanceof SdkError && error.code === "persistence_error") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logbrew/sdk",
3
- "version": "0.1.20",
3
+ "version": "0.1.22",
4
4
  "description": "Public LogBrew JavaScript SDK for building, validating, and flushing event batches.",
5
5
  "type": "module",
6
6
  "main": "./index.cjs",