sliftutils 0.66.0 → 0.68.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
|
@@ -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
|
-
|
|
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
|
-
|
|
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,13 +261,24 @@ 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
|
-
|
|
259
|
-
|
|
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
|
|
|
263
279
|
await fileLockSection(async () => {
|
|
264
280
|
if (!await anyChanges()) return;
|
|
281
|
+
console.log(`Found new changes in collection from other tab. Loading them now. ${this.debugName}`);
|
|
265
282
|
await this.loadAllTransactions();
|
|
266
283
|
});
|
|
267
284
|
}
|
|
@@ -278,7 +295,12 @@ export class TransactionStorage implements IStorage<Buffer> {
|
|
|
278
295
|
let time = Date.now();
|
|
279
296
|
const keys = await this.rawStorage.getKeys();
|
|
280
297
|
const transactionFiles = keys.filter(key => key.endsWith(CHUNK_EXT));
|
|
281
|
-
|
|
298
|
+
// Populate diskFiles with actual timestamps from disk
|
|
299
|
+
this.diskFiles = new Map();
|
|
300
|
+
for (let file of transactionFiles) {
|
|
301
|
+
let info = await this.rawStorage.getInfo(file);
|
|
302
|
+
this.diskFiles.set(file, info?.lastModified ?? Date.now());
|
|
303
|
+
}
|
|
282
304
|
|
|
283
305
|
let entryList: TransactionEntry[][] = [];
|
|
284
306
|
for (let file of transactionFiles) {
|
|
@@ -568,6 +590,7 @@ export class TransactionStorage implements IStorage<Buffer> {
|
|
|
568
590
|
}
|
|
569
591
|
let buffer = Buffer.concat([headerBuffer, content]);
|
|
570
592
|
await this.rawStorage.set(file, buffer);
|
|
593
|
+
this.updateDiskFileTimestamp(file);
|
|
571
594
|
let verified = await this.rawStorage.get(file);
|
|
572
595
|
if (!verified?.equals(buffer)) {
|
|
573
596
|
console.error(`Failed to verify transaction file ${file} in ${this.debugName}`);
|