sliftutils 0.66.0 → 0.67.0

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/index.d.ts CHANGED
@@ -986,6 +986,7 @@ declare module "sliftutils/storage/TransactionStorage" {
986
986
  watchResync(callback: () => void): void;
987
987
  private init;
988
988
  private getCurrentChunk;
989
+ private updateDiskFileTimestamp;
989
990
  private onAddToChunk;
990
991
  get(key: string): Promise<Buffer | undefined>;
991
992
  set(key: string, value: Buffer): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sliftutils",
3
- "version": "0.66.0",
3
+ "version": "0.67.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -22,6 +22,7 @@ export declare class TransactionStorage implements IStorage<Buffer> {
22
22
  watchResync(callback: () => void): void;
23
23
  private init;
24
24
  private getCurrentChunk;
25
+ private updateDiskFileTimestamp;
25
26
  private onAddToChunk;
26
27
  get(key: string): Promise<Buffer | undefined>;
27
28
  set(key: string, value: Buffer): Promise<void>;
@@ -78,7 +78,8 @@ function getNextChunkPath(): string {
78
78
 
79
79
  export class TransactionStorage implements IStorage<Buffer> {
80
80
  public cache: Map<string, TransactionEntry> = new Map();
81
- private diskFiles: Set<string> = new Set();
81
+ // Maps file name to our last known write timestamp
82
+ private diskFiles: Map<string, number> = new Map();
82
83
  private currentChunk: {
83
84
  path: string;
84
85
  size: number;
@@ -123,10 +124,14 @@ export class TransactionStorage implements IStorage<Buffer> {
123
124
  size: 0,
124
125
  timeCreated: Date.now()
125
126
  };
126
- this.diskFiles.add(this.currentChunk.path);
127
+ // Timestamp will be updated after actual write
128
+ this.diskFiles.set(this.currentChunk.path, 0);
127
129
  }
128
130
  return this.currentChunk.path;
129
131
  }
132
+ private updateDiskFileTimestamp(file: string): void {
133
+ this.diskFiles.set(file, Date.now());
134
+ }
130
135
  private onAddToChunk(size: number): void {
131
136
  if (!this.currentChunk) return;
132
137
  this.currentChunk.size += size;
@@ -222,6 +227,7 @@ export class TransactionStorage implements IStorage<Buffer> {
222
227
  }
223
228
  let content = chunk.buffer;
224
229
  await this.rawStorage.append(file, content);
230
+ this.updateDiskFileTimestamp(file);
225
231
  this.onAddToChunk(content.length);
226
232
  }
227
233
 
@@ -255,8 +261,18 @@ export class TransactionStorage implements IStorage<Buffer> {
255
261
  const anyChanges = async () => {
256
262
  let keys = await this.rawStorage.getKeys();
257
263
  let diskFiles = keys.filter(key => key.endsWith(CHUNK_EXT));
258
- let hasNew = diskFiles.some(file => !this.diskFiles.has(file));
259
- return hasNew;
264
+ // Check if any known files have been modified externally (disk timestamp newer than ours)
265
+ for (let file of diskFiles) {
266
+ let ourTimestamp = this.diskFiles.get(file);
267
+ if (ourTimestamp === undefined) {
268
+ return true;
269
+ }
270
+ let info = await this.rawStorage.getInfo(file);
271
+ if (info && info.lastModified > ourTimestamp) {
272
+ return true;
273
+ }
274
+ }
275
+ return false;
260
276
  };
261
277
  if (!await anyChanges()) return;
262
278
 
@@ -278,7 +294,12 @@ export class TransactionStorage implements IStorage<Buffer> {
278
294
  let time = Date.now();
279
295
  const keys = await this.rawStorage.getKeys();
280
296
  const transactionFiles = keys.filter(key => key.endsWith(CHUNK_EXT));
281
- this.diskFiles = new Set(transactionFiles);
297
+ // Populate diskFiles with actual timestamps from disk
298
+ this.diskFiles = new Map();
299
+ for (let file of transactionFiles) {
300
+ let info = await this.rawStorage.getInfo(file);
301
+ this.diskFiles.set(file, info?.lastModified ?? Date.now());
302
+ }
282
303
 
283
304
  let entryList: TransactionEntry[][] = [];
284
305
  for (let file of transactionFiles) {
@@ -568,6 +589,7 @@ export class TransactionStorage implements IStorage<Buffer> {
568
589
  }
569
590
  let buffer = Buffer.concat([headerBuffer, content]);
570
591
  await this.rawStorage.set(file, buffer);
592
+ this.updateDiskFileTimestamp(file);
571
593
  let verified = await this.rawStorage.get(file);
572
594
  if (!verified?.equals(buffer)) {
573
595
  console.error(`Failed to verify transaction file ${file} in ${this.debugName}`);