beardos-toolbox 0.1.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.
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Minimal in-memory singleton for per-ID timestamp gating.
3
+ * - Stores only last-allowed timestamps (ms since epoch) per id.
4
+ * - Call check(id) to check & update atomically:
5
+ * - If first time or elapsed >= ttlMs -> sets now and returns true
6
+ * - Otherwise returns false (unchanged)
7
+ * - Pass your own `now` when batching or testing.
8
+ */
9
+ export declare class TimestampGate {
10
+ private static _instance;
11
+ private readonly store;
12
+ private readonly defaultTTL;
13
+ private constructor();
14
+ /** Get the single shared instance. */
15
+ static get instance(): TimestampGate;
16
+ /**
17
+ * Check if `id` is allowed based on `ttl`. If allowed, persist `now`.
18
+ * @param {string} id Unique key to gate.
19
+ * @param {number} now Optional timestamp (ms). Defaults to Date.now() for minimal overhead.
20
+ * @param {number?} ttl Time-to-live in milliseconds.
21
+ * @returns true if allowed (and stored), false otherwise.
22
+ */
23
+ check(id: string, now?: number, ttl?: number): boolean;
24
+ /**
25
+ * Peek the last-allowed timestamp (ms) for `id`.
26
+ * @returns number | undefined
27
+ */
28
+ get(id: string): number | undefined;
29
+ /**
30
+ * Manually set a last-allowed timestamp (ms) for `id`.
31
+ * Useful for backfilling/imports.
32
+ */
33
+ set(id: string, timestampMs: number): void;
34
+ /** Remove a single id. */
35
+ delete(id: string): void;
36
+ /** Drop everything (helps if you shard or rotate caches). */
37
+ clear(): void;
38
+ /**
39
+ * Optional: prune entries whose last timestamp is older than `cutoffMs`.
40
+ * Keeps memory lean if there is other operations that run on a schedules
41
+ * or with enough frequency to effectively
42
+ */
43
+ pruneFrom(cutoffMs: number): void;
44
+ /** Current number of tracked ids (for diagnostics). */
45
+ size(): number;
46
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "beardos-toolbox",
3
+ "version": "0.1.0",
4
+ "description": "Handy scripts and libs that I don't want to write again but don't need their own individual repos",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "node build.mjs",
21
+ "test": "node --import tsx --test **/*.test.ts",
22
+ "test:watch": "node --import tsx --test --watch **/*.test.ts",
23
+ "prepublishOnly": "npm run build"
24
+ },
25
+ "peerDependencies": {
26
+ "@aws-sdk/client-s3": ">=3"
27
+ },
28
+ "peerDependenciesMeta": {
29
+ "@aws-sdk/client-s3": {
30
+ "optional": true
31
+ }
32
+ },
33
+ "devDependencies": {
34
+ "@aws-sdk/client-s3": "^3.0.0",
35
+ "@types/node": "^22.0.0",
36
+ "esbuild": "^0.25.0",
37
+ "tsx": "^4.21.0",
38
+ "typescript": "^5.0.0"
39
+ },
40
+ "keywords": [],
41
+ "author": "Aaron Toomey <aaron@inkiebeard.codes>",
42
+ "license": "MIT"
43
+ }