@niq.run/worker-sdk 0.11.0 → 0.11.1
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/dist/durable.d.ts +8 -1
- package/dist/durable.d.ts.map +1 -1
- package/dist/durable.js +13 -1
- package/dist/durable.js.map +1 -1
- package/package.json +1 -1
package/dist/durable.d.ts
CHANGED
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
export interface DurableStore {
|
|
13
13
|
/** Return the last saved text, or undefined when nothing was ever saved. */
|
|
14
14
|
load(): Promise<string | undefined>;
|
|
15
|
-
/** Replace the saved text. Implementations must be atomic (no torn reads)
|
|
15
|
+
/** Replace the saved text. Implementations must be atomic (no torn reads),
|
|
16
|
+
* and concurrent saves must land in call order so the newest snapshot wins. */
|
|
16
17
|
save(data: string): Promise<void>;
|
|
17
18
|
}
|
|
18
19
|
/**
|
|
@@ -26,15 +27,21 @@ export interface DurableStore {
|
|
|
26
27
|
*
|
|
27
28
|
* Writes go to a temporary sibling and are renamed into place, so a crash
|
|
28
29
|
* mid-write leaves the previous state intact rather than a truncated file.
|
|
30
|
+
* Saves are also serialized: a worker that saves on every handled event must
|
|
31
|
+
* not lose the newer snapshot to a slower older write, so each save is chained
|
|
32
|
+
* after the previous one and lands in call order.
|
|
29
33
|
*/
|
|
30
34
|
export declare class FileDurableStore implements DurableStore {
|
|
31
35
|
readonly dir: string;
|
|
32
36
|
readonly fileName: string;
|
|
37
|
+
/** Tail of the write chain; keeps saves applied in call order. */
|
|
38
|
+
private queue;
|
|
33
39
|
constructor(dir: string, fileName?: string);
|
|
34
40
|
/** Absolute-or-relative path of the state file this store owns. */
|
|
35
41
|
get path(): string;
|
|
36
42
|
load(): Promise<string | undefined>;
|
|
37
43
|
save(data: string): Promise<void>;
|
|
44
|
+
private writeNow;
|
|
38
45
|
}
|
|
39
46
|
/**
|
|
40
47
|
* A durable store that keeps the value in memory only.
|
package/dist/durable.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durable.d.ts","sourceRoot":"","sources":["../src/durable.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;GAUG;AACH,MAAM,WAAW,YAAY;IAC3B,4EAA4E;IAC5E,IAAI,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IACpC
|
|
1
|
+
{"version":3,"file":"durable.d.ts","sourceRoot":"","sources":["../src/durable.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;GAUG;AACH,MAAM,WAAW,YAAY;IAC3B,4EAA4E;IAC5E,IAAI,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IACpC;mFAC+E;IAC/E,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACnC;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,gBAAiB,YAAW,YAAY;IACnD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B,kEAAkE;IAClE,OAAO,CAAC,KAAK,CAAoC;gBAErC,GAAG,EAAE,MAAM,EAAE,QAAQ,SAAe;IAKhD,mEAAmE;IACnE,IAAI,IAAI,IAAI,MAAM,CAEjB;IAEK,IAAI,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IASzC,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAQnB,QAAQ;CAOvB;AAED;;;;;;GAMG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IACrD,OAAO,CAAC,KAAK,CAAC,CAAS;IAEvB,IAAI,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAInC,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAIlC"}
|
package/dist/durable.js
CHANGED
|
@@ -11,10 +11,15 @@ import { dirname, join } from "node:path";
|
|
|
11
11
|
*
|
|
12
12
|
* Writes go to a temporary sibling and are renamed into place, so a crash
|
|
13
13
|
* mid-write leaves the previous state intact rather than a truncated file.
|
|
14
|
+
* Saves are also serialized: a worker that saves on every handled event must
|
|
15
|
+
* not lose the newer snapshot to a slower older write, so each save is chained
|
|
16
|
+
* after the previous one and lands in call order.
|
|
14
17
|
*/
|
|
15
18
|
export class FileDurableStore {
|
|
16
19
|
dir;
|
|
17
20
|
fileName;
|
|
21
|
+
/** Tail of the write chain; keeps saves applied in call order. */
|
|
22
|
+
queue = Promise.resolve();
|
|
18
23
|
constructor(dir, fileName = "state.json") {
|
|
19
24
|
this.dir = dir;
|
|
20
25
|
this.fileName = fileName;
|
|
@@ -33,7 +38,14 @@ export class FileDurableStore {
|
|
|
33
38
|
throw err;
|
|
34
39
|
}
|
|
35
40
|
}
|
|
36
|
-
|
|
41
|
+
save(data) {
|
|
42
|
+
// Chain onto the previous write so the last call wins, not the last to
|
|
43
|
+
// finish. A failed write must not poison the chain for later saves.
|
|
44
|
+
const write = this.queue.then(() => this.writeNow(data));
|
|
45
|
+
this.queue = write.catch(() => { });
|
|
46
|
+
return write;
|
|
47
|
+
}
|
|
48
|
+
async writeNow(data) {
|
|
37
49
|
await mkdir(dirname(this.path), { recursive: true });
|
|
38
50
|
// A per-write suffix: two concurrent saves must not share a temp file.
|
|
39
51
|
const tmp = `${this.path}.tmp-${process.pid}-${Math.random().toString(36).slice(2)}`;
|
package/dist/durable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durable.js","sourceRoot":"","sources":["../src/durable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"durable.js","sourceRoot":"","sources":["../src/durable.ts"],"names":[],"mappings":"AAAA,OAAO,EAA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