@zukhruf/fencing 0.3.19 → 0.3.20
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 +2 -0
- package/dist/file-token-source.js +6 -1
- package/dist/file-token-source.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,6 +112,8 @@ const token = await tokens.next('product:42');
|
|
|
112
112
|
|
|
113
113
|
`FileTokenSource` makes the directory if it is absent. It keeps the counter of each key as decimal text, and it replaces the file in one step and syncs it to the disk before it gives the token. On Linux and macOS, a power loss cannot make it give one token two times. The name of the file comes from the key with `safeFileName` of `@zukhruf/fs`. Processes that use earlier versions of `@zukhruf/mutex` find the same files.
|
|
114
114
|
|
|
115
|
+
A counter file can hold text that is not decimal digits. An example is the empty file that `@zukhruf/mutex` 0.2.0 or earlier can leave after a power loss, because those versions did not sync the file to the disk. Then `next` rejects with a `SyntaxError` that names the file, and the file stays as it is. Thus the tokens of a key never start again at 1, and the fenced resource never accepts the writes of a stale holder.
|
|
116
|
+
|
|
115
117
|
`EpochTokenSource` puts the epoch in the high 32 bits and the counter in the low 32 bits, as ZooKeeper does in its `zxid`. Each token of a newer epoch is higher than each token of an older epoch. A new leader thus outranks all grants of the leader before it. The epoch must be from 0 to 2^31 − 1, so that each token fits a signed 64-bit integer. One epoch gives at most 2^32 − 1 tokens. After that, `next` throws a `RangeError`.
|
|
116
118
|
|
|
117
119
|
## A fenced lease
|
|
@@ -22,13 +22,18 @@ export class FileTokenSource {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
async function readCounter(path) {
|
|
25
|
+
let text;
|
|
25
26
|
try {
|
|
26
|
-
|
|
27
|
+
text = await readFile(path, 'utf8');
|
|
27
28
|
}
|
|
28
29
|
catch (error) {
|
|
29
30
|
if (isErrno(error, 'ENOENT'))
|
|
30
31
|
return 0n;
|
|
31
32
|
throw error;
|
|
32
33
|
}
|
|
34
|
+
const counter = FencingToken.parse(text);
|
|
35
|
+
if (counter === null)
|
|
36
|
+
throw new SyntaxError(`The fencing counter ${path} holds ${JSON.stringify(text)}, not decimal digits.`);
|
|
37
|
+
return counter.value;
|
|
33
38
|
}
|
|
34
39
|
//# sourceMappingURL=file-token-source.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-token-source.js","sourceRoot":"","sources":["../src/file-token-source.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EACL,iBAAiB,EACjB,YAAY,EACZ,OAAO,EACP,YAAY,GACb,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD;;;;GAIG;AACH,MAAM,OAAO,eAAe;IACjB,UAAU,CAAS;IAE5B,YAAY,SAAiB;QAC3B,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,GAAW;QACpB,MAAM,KAAK,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAClD,MAAM,IAAI,GAAG,YAAY,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,GAAG,iBAAiB,CAAC,CAAC;QACpE,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,IAAI,QAAQ,CAAC,CAAC;QACpD,MAAM,KAAK,GAAG,CAAC,MAAM,WAAW,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;QAC7C,MAAM,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC3C,OAAO,IAAI,YAAY,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;CACF;AAED,KAAK,UAAU,WAAW,CAAC,IAAY;IACrC,IAAI,CAAC;QACH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AAE,OAAO,EAAE,CAAC;QACxC,MAAM,KAAK,CAAC;IACd,CAAC;IACD,MAAM,OAAO,GAAG,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACzC,IAAI,OAAO,KAAK,IAAI;QAClB,MAAM,IAAI,WAAW,CACnB,uBAAuB,IAAI,UAAU,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,uBAAuB,CACjF,CAAC;IACJ,OAAO,OAAO,CAAC,KAAK,CAAC;AACvB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/fencing",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.20",
|
|
4
4
|
"description": "Fencing tokens: a number that grows with each grant, so a protected resource refuses the writes of a stale holder. Token sources that count in memory, in files, or by epoch, and the fenced lease that carries a token.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|