tally-ttl 0.5.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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/cjs/index.js +80 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +58 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 PhoneCloud
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# tally-ttl
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TallyTTL = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* TallyTTL counts induvidual tallies per given ID with a time-to-live on a per-tally basis.
|
|
6
|
+
*/
|
|
7
|
+
class TallyTTL {
|
|
8
|
+
constructor(config = {}) {
|
|
9
|
+
this.store = {};
|
|
10
|
+
const { defaultTtl = 60, cleanupSeconds = 120000 } = config;
|
|
11
|
+
if (!Number.isFinite(defaultTtl) || defaultTtl <= 0) {
|
|
12
|
+
throw new Error("defaultTtlSeconds must be a positive finite number");
|
|
13
|
+
}
|
|
14
|
+
this.defaultTtl = defaultTtl;
|
|
15
|
+
this.cleanupInterval = setInterval(this.cleanup, cleanupSeconds);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Increment the tally for a given id and return the current count.
|
|
19
|
+
* @param id The identifier to tally.
|
|
20
|
+
* @param ttlSeconds Optional per-call TTL in seconds. Overrides constructor default when provided.
|
|
21
|
+
* @returns The count after incrementing.
|
|
22
|
+
*/
|
|
23
|
+
tally(id, ttlSeconds) {
|
|
24
|
+
if (typeof id !== "string" || id.length === 0) {
|
|
25
|
+
throw new Error("id must be a non-empty string");
|
|
26
|
+
}
|
|
27
|
+
const ttl = this.resolveTtl(ttlSeconds);
|
|
28
|
+
const expiresAt = String(Date.now() + ttl * 1000);
|
|
29
|
+
this.store[id] = this.store[id] || {};
|
|
30
|
+
this.store[id][expiresAt] = (this.store[id][expiresAt] || 0) + 1;
|
|
31
|
+
}
|
|
32
|
+
increment(id) {
|
|
33
|
+
this.tally(id);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get the current count without mutating. Returns 0 if missing or expired.
|
|
37
|
+
*/
|
|
38
|
+
get(id) {
|
|
39
|
+
const now = Date.now();
|
|
40
|
+
let count = 0;
|
|
41
|
+
if (this.store[id]) {
|
|
42
|
+
for (const t of Object.keys(this.store[id])) {
|
|
43
|
+
if (Number(t) > now) {
|
|
44
|
+
count += this.store[id][t] || 0;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return count;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Manually clear an id's tally.
|
|
52
|
+
*/
|
|
53
|
+
clear(id) {
|
|
54
|
+
delete this.store[id];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Remove all expired entries. This is optional; entries are lazily reset on access.
|
|
58
|
+
*/
|
|
59
|
+
cleanup() {
|
|
60
|
+
const now = Date.now();
|
|
61
|
+
for (const id of Object.keys(this.store)) {
|
|
62
|
+
if (this.store[id]) {
|
|
63
|
+
for (const t of Object.keys(this.store[id])) {
|
|
64
|
+
if (Number(t) <= now)
|
|
65
|
+
delete this.store[id][t];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
resolveTtl(ttlSeconds) {
|
|
71
|
+
if (ttlSeconds === undefined)
|
|
72
|
+
return this.defaultTtl;
|
|
73
|
+
if (!Number.isFinite(ttlSeconds) || ttlSeconds <= 0) {
|
|
74
|
+
throw new Error("TallyTTL: ttlSeconds must be a positive finite number when provided");
|
|
75
|
+
}
|
|
76
|
+
return ttlSeconds;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.TallyTTL = TallyTTL;
|
|
80
|
+
exports.default = TallyTTL;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type TallyTTLConfig = {
|
|
2
|
+
defaultTtl?: number;
|
|
3
|
+
cleanupSeconds?: number;
|
|
4
|
+
};
|
|
5
|
+
export declare class TallyTTL {
|
|
6
|
+
private readonly defaultTtl;
|
|
7
|
+
private readonly store;
|
|
8
|
+
cleanupInterval: number;
|
|
9
|
+
constructor(config?: TallyTTLConfig);
|
|
10
|
+
tally(id: string, ttlSeconds?: number): void;
|
|
11
|
+
increment(id: string): void;
|
|
12
|
+
get(id: string): number;
|
|
13
|
+
clear(id: string): void;
|
|
14
|
+
cleanup(): void;
|
|
15
|
+
private resolveTtl;
|
|
16
|
+
}
|
|
17
|
+
export default TallyTTL;
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAKF,qBAAa,QAAQ;IACpB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAIf;IAEP,eAAe,EAAE,MAAM,CAAC;gBAEZ,MAAM,GAAE,cAAmB;IAgBvC,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAWrC,SAAS,CAAC,EAAE,EAAE,MAAM;IAOpB,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM;IAiBvB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAOvB,OAAO,IAAI,IAAI;IAWf,OAAO,CAAC,UAAU;CAOlB;AAED,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export class TallyTTL {
|
|
2
|
+
constructor(config = {}) {
|
|
3
|
+
this.store = {};
|
|
4
|
+
const { defaultTtl = 60, cleanupSeconds = 120000 } = config;
|
|
5
|
+
if (!Number.isFinite(defaultTtl) || defaultTtl <= 0) {
|
|
6
|
+
throw new Error("defaultTtlSeconds must be a positive finite number");
|
|
7
|
+
}
|
|
8
|
+
this.defaultTtl = defaultTtl;
|
|
9
|
+
this.cleanupInterval = setInterval(this.cleanup, cleanupSeconds);
|
|
10
|
+
}
|
|
11
|
+
tally(id, ttlSeconds) {
|
|
12
|
+
if (typeof id !== "string" || id.length === 0) {
|
|
13
|
+
throw new Error("id must be a non-empty string");
|
|
14
|
+
}
|
|
15
|
+
const ttl = this.resolveTtl(ttlSeconds);
|
|
16
|
+
const expiresAt = String(Date.now() + ttl * 1000);
|
|
17
|
+
this.store[id] = this.store[id] || {};
|
|
18
|
+
this.store[id][expiresAt] = (this.store[id][expiresAt] || 0) + 1;
|
|
19
|
+
}
|
|
20
|
+
increment(id) {
|
|
21
|
+
this.tally(id);
|
|
22
|
+
}
|
|
23
|
+
get(id) {
|
|
24
|
+
const now = Date.now();
|
|
25
|
+
let count = 0;
|
|
26
|
+
if (this.store[id]) {
|
|
27
|
+
for (const t of Object.keys(this.store[id])) {
|
|
28
|
+
if (Number(t) > now) {
|
|
29
|
+
count += this.store[id][t] || 0;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return count;
|
|
34
|
+
}
|
|
35
|
+
clear(id) {
|
|
36
|
+
delete this.store[id];
|
|
37
|
+
}
|
|
38
|
+
cleanup() {
|
|
39
|
+
const now = Date.now();
|
|
40
|
+
for (const id of Object.keys(this.store)) {
|
|
41
|
+
if (this.store[id]) {
|
|
42
|
+
for (const t of Object.keys(this.store[id])) {
|
|
43
|
+
if (Number(t) <= now)
|
|
44
|
+
delete this.store[id][t];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
resolveTtl(ttlSeconds) {
|
|
50
|
+
if (ttlSeconds === undefined)
|
|
51
|
+
return this.defaultTtl;
|
|
52
|
+
if (!Number.isFinite(ttlSeconds) || ttlSeconds <= 0) {
|
|
53
|
+
throw new Error("TallyTTL: ttlSeconds must be a positive finite number when provided");
|
|
54
|
+
}
|
|
55
|
+
return ttlSeconds;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export default TallyTTL;
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tally-ttl",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "A simple library to tally items with a configurable per-tally TTL",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tally",
|
|
7
|
+
"counter",
|
|
8
|
+
"ttl",
|
|
9
|
+
"increment",
|
|
10
|
+
"expiring",
|
|
11
|
+
"auto-expire"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/mattmacadams/tally-ttl#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/mattmacadams/tally-ttl/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/mattmacadams/tally-ttl.git"
|
|
20
|
+
},
|
|
21
|
+
"license": "CC0-1.0",
|
|
22
|
+
"author": "Matt MacAdams",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"types": "./dist/esm/index.d.ts",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
27
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
28
|
+
"build": "rm -rf dist/* && npm run build:esm && npm run build:cjs",
|
|
29
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
30
|
+
},
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/esm/index.d.ts",
|
|
34
|
+
"import": "./dist/esm/index.js",
|
|
35
|
+
"require": "./dist/cjs/index.cjs"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"sideEffects": false,
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"prettier": "^3.7.4",
|
|
46
|
+
"typescript": "^5.9.3"
|
|
47
|
+
}
|
|
48
|
+
}
|