@backloghq/opslog 0.1.2 → 0.1.4

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/README.md CHANGED
@@ -103,6 +103,8 @@ await store.batch(() => { // Multiple ops, single disk write
103
103
  });
104
104
  ```
105
105
 
106
+ Empty batches (no `set`/`delete` calls) are no-ops — no I/O is performed.
107
+
106
108
  ### History
107
109
 
108
110
  ```typescript
package/dist/store.js CHANGED
@@ -46,7 +46,18 @@ export class Store {
46
46
  }
47
47
  else {
48
48
  // Load existing state
49
- const { records, version: storedVersion } = await loadSnapshot(dir, manifest.currentSnapshot);
49
+ let snapshotData;
50
+ try {
51
+ snapshotData = await loadSnapshot(dir, manifest.currentSnapshot);
52
+ }
53
+ catch (err) {
54
+ const isNotFound = err instanceof Error && "code" in err && err.code === "ENOENT";
55
+ if (isNotFound) {
56
+ throw new Error(`Snapshot file not found: ${manifest.currentSnapshot}. The data directory may be corrupted.`, { cause: err });
57
+ }
58
+ throw err;
59
+ }
60
+ const { records, version: storedVersion } = snapshotData;
50
61
  this.records = records;
51
62
  this.version = storedVersion;
52
63
  this.activeOpsPath = manifest.activeOps;
@@ -154,6 +165,7 @@ export class Store {
154
165
  this.batchOps = [];
155
166
  try {
156
167
  fn();
168
+ // Empty batches are no-ops — no I/O if fn() didn't call set/delete
157
169
  if (this.batchOps.length > 0) {
158
170
  await appendOps(join(this.dir, this.activeOpsPath), this.batchOps);
159
171
  this.ops.push(...this.batchOps);
@@ -245,7 +257,7 @@ export class Store {
245
257
  }
246
258
  async loadArchive(segment) {
247
259
  this.ensureOpen();
248
- const segmentPath = this.archiveSegments.find((s) => s.includes(segment));
260
+ const segmentPath = this.archiveSegments.find((s) => s === `archive/archive-${segment}.json`) || this.archiveSegments.find((s) => s.includes(segment));
249
261
  if (!segmentPath)
250
262
  throw new Error(`Archive segment '${segment}' not found`);
251
263
  return loadArchiveSegment(this.dir, segmentPath);
package/dist/validate.js CHANGED
@@ -3,13 +3,24 @@ export function validateOp(raw) {
3
3
  throw new Error("Invalid operation: not an object");
4
4
  }
5
5
  const obj = raw;
6
- if (typeof obj.ts !== "string")
7
- throw new Error("Invalid operation: missing ts");
8
- if (obj.op !== "set" && obj.op !== "delete") {
9
- throw new Error(`Invalid operation: unknown op "${obj.op}"`);
6
+ if (typeof obj.ts !== "string" || obj.ts.length === 0)
7
+ throw new Error("Invalid operation: ts must be a non-empty string");
8
+ if (typeof obj.op !== "string" || (obj.op !== "set" && obj.op !== "delete")) {
9
+ throw new Error(`Invalid operation: op must be "set" or "delete", got "${obj.op}"`);
10
10
  }
11
- if (typeof obj.id !== "string")
12
- throw new Error("Invalid operation: missing id");
11
+ if (typeof obj.id !== "string" || obj.id.length === 0)
12
+ throw new Error("Invalid operation: id must be a non-empty string");
13
+ if (!("prev" in obj))
14
+ throw new Error("Invalid operation: missing prev");
15
+ if (obj.prev !== null && (typeof obj.prev !== "object" || Array.isArray(obj.prev))) {
16
+ throw new Error("Invalid operation: prev must be an object or null");
17
+ }
18
+ if (obj.op === "set" && (!("data" in obj) || obj.data === null))
19
+ throw new Error("Invalid operation: set op must have non-null data");
20
+ if (obj.op === "delete" && obj.prev === null)
21
+ throw new Error("Invalid operation: delete op must have non-null prev");
22
+ if (obj.op === "delete" && "data" in obj)
23
+ throw new Error("Invalid operation: delete op must not have data field");
13
24
  return raw;
14
25
  }
15
26
  export function validateManifest(raw) {
@@ -17,28 +28,33 @@ export function validateManifest(raw) {
17
28
  throw new Error("Invalid manifest: not an object");
18
29
  }
19
30
  const obj = raw;
20
- if (typeof obj.version !== "number")
21
- throw new Error("Invalid manifest: missing version");
22
- if (typeof obj.currentSnapshot !== "string")
31
+ if (typeof obj.version !== "number" || !Number.isFinite(obj.version) || !Number.isInteger(obj.version) || obj.version < 1) {
32
+ throw new Error("Invalid manifest: version must be a positive finite integer");
33
+ }
34
+ if (typeof obj.currentSnapshot !== "string" || obj.currentSnapshot.length === 0)
23
35
  throw new Error("Invalid manifest: missing currentSnapshot");
24
- if (typeof obj.activeOps !== "string")
36
+ if (typeof obj.activeOps !== "string" || obj.activeOps.length === 0)
25
37
  throw new Error("Invalid manifest: missing activeOps");
26
38
  if (!Array.isArray(obj.archiveSegments))
27
39
  throw new Error("Invalid manifest: archiveSegments must be an array");
40
+ for (const seg of obj.archiveSegments) {
41
+ if (typeof seg !== "string" || seg.length === 0)
42
+ throw new Error("Invalid manifest: archiveSegments entries must be non-empty strings");
43
+ }
28
44
  if (typeof obj.stats !== "object" || obj.stats === null || Array.isArray(obj.stats)) {
29
45
  throw new Error("Invalid manifest: missing stats");
30
46
  }
31
47
  const stats = obj.stats;
32
- if (typeof stats.activeRecords !== "number")
33
- throw new Error("Invalid manifest: stats.activeRecords must be a number");
34
- if (typeof stats.archivedRecords !== "number")
35
- throw new Error("Invalid manifest: stats.archivedRecords must be a number");
36
- if (typeof stats.opsCount !== "number")
37
- throw new Error("Invalid manifest: stats.opsCount must be a number");
38
- if (typeof stats.created !== "string")
39
- throw new Error("Invalid manifest: stats.created must be a string");
40
- if (typeof stats.lastCheckpoint !== "string")
41
- throw new Error("Invalid manifest: stats.lastCheckpoint must be a string");
48
+ if (typeof stats.activeRecords !== "number" || !Number.isFinite(stats.activeRecords) || !Number.isInteger(stats.activeRecords) || stats.activeRecords < 0)
49
+ throw new Error("Invalid manifest: stats.activeRecords must be a non-negative integer");
50
+ if (typeof stats.archivedRecords !== "number" || !Number.isFinite(stats.archivedRecords) || !Number.isInteger(stats.archivedRecords) || stats.archivedRecords < 0)
51
+ throw new Error("Invalid manifest: stats.archivedRecords must be a non-negative integer");
52
+ if (typeof stats.opsCount !== "number" || !Number.isFinite(stats.opsCount) || !Number.isInteger(stats.opsCount) || stats.opsCount < 0)
53
+ throw new Error("Invalid manifest: stats.opsCount must be a non-negative integer");
54
+ if (typeof stats.created !== "string" || stats.created.length === 0)
55
+ throw new Error("Invalid manifest: stats.created must be a non-empty string");
56
+ if (typeof stats.lastCheckpoint !== "string" || stats.lastCheckpoint.length === 0)
57
+ throw new Error("Invalid manifest: stats.lastCheckpoint must be a non-empty string");
42
58
  return raw;
43
59
  }
44
60
  export function validateSnapshot(raw) {
@@ -46,8 +62,11 @@ export function validateSnapshot(raw) {
46
62
  throw new Error("Invalid snapshot: not an object");
47
63
  }
48
64
  const obj = raw;
49
- if (typeof obj.version !== "number")
50
- throw new Error("Invalid snapshot: missing version");
65
+ if (typeof obj.version !== "number" || !Number.isFinite(obj.version) || !Number.isInteger(obj.version) || obj.version < 1) {
66
+ throw new Error("Invalid snapshot: version must be a positive finite integer");
67
+ }
68
+ if (typeof obj.timestamp !== "string" || obj.timestamp.length === 0)
69
+ throw new Error("Invalid snapshot: timestamp must be a non-empty string");
51
70
  if (typeof obj.records !== "object" || obj.records === null || Array.isArray(obj.records)) {
52
71
  throw new Error("Invalid snapshot: records must be an object");
53
72
  }
@@ -58,12 +77,13 @@ export function validateArchiveSegment(raw) {
58
77
  throw new Error("Invalid archive segment: not an object");
59
78
  }
60
79
  const obj = raw;
61
- if (typeof obj.version !== "number")
62
- throw new Error("Invalid archive segment: missing version");
63
- if (typeof obj.period !== "string")
64
- throw new Error("Invalid archive segment: missing period");
65
- if (typeof obj.timestamp !== "string")
66
- throw new Error("Invalid archive segment: missing timestamp");
80
+ if (typeof obj.version !== "number" || !Number.isFinite(obj.version) || !Number.isInteger(obj.version) || obj.version < 1) {
81
+ throw new Error("Invalid archive segment: version must be a positive finite integer");
82
+ }
83
+ if (typeof obj.period !== "string" || obj.period.length === 0)
84
+ throw new Error("Invalid archive segment: period must be a non-empty string");
85
+ if (typeof obj.timestamp !== "string" || obj.timestamp.length === 0)
86
+ throw new Error("Invalid archive segment: timestamp must be a non-empty string");
67
87
  if (typeof obj.records !== "object" || obj.records === null || Array.isArray(obj.records)) {
68
88
  throw new Error("Invalid archive segment: records must be an object");
69
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backloghq/opslog",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Embedded event-sourced document store. Append-only operation log with immutable snapshots, zero native dependencies.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",