idempotency-store 0.0.0-stage → 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.
package/index.d.ts ADDED
File without changes
package/index.js ADDED
@@ -0,0 +1,69 @@
1
+ class IdempotencyStore {
2
+ /**
3
+ * @param {object} [options]
4
+ * @param {number} [options.ttlMs=86400000] - Time to live in milliseconds (default: 24 hours)
5
+ * @param {number} [options.cleanupIntervalMs=60000] - How often to clean up expired keys (default: 1 min)
6
+ */
7
+ constructor(options = {}) {
8
+ this.ttlMs = options.ttlMs || 24 * 60 * 60 * 1000; // 24 hours
9
+ this.store = new Map();
10
+
11
+ // Background cleanup to prevent memory leaks
12
+ this.cleanupInterval = setInterval(() => {
13
+ this.cleanup();
14
+ }, options.cleanupIntervalMs || 60 * 1000);
15
+
16
+ // Don't let the interval keep the Node process alive if the app stops
17
+ if (this.cleanupInterval.unref) {
18
+ this.cleanupInterval.unref();
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Check if an ID exists, and if not, mark it as processed.
24
+ * @param {string} id - The unique event ID (e.g., from the webhook header)
25
+ * @returns {boolean} - Returns true if this is the FIRST time seeing the ID. False if it's a duplicate.
26
+ */
27
+ process(id) {
28
+ if (!id) {
29
+ throw new Error('ID is required');
30
+ }
31
+
32
+ const now = Date.now();
33
+
34
+ if (this.store.has(id)) {
35
+ const expiresAt = this.store.get(id);
36
+ // Check if it's actually expired right now
37
+ if (now > expiresAt) {
38
+ this.store.delete(id); // It's expired, remove it and let it process
39
+ } else {
40
+ return false; // It's still valid, so it's a duplicate
41
+ }
42
+ }
43
+
44
+ // First time seeing it (or it expired). Save it with a new expiration time.
45
+ this.store.set(id, now + this.ttlMs);
46
+ return true;
47
+ }
48
+
49
+ /**
50
+ * Manually remove expired keys
51
+ */
52
+ cleanup() {
53
+ const now = Date.now();
54
+ for (const [id, expiresAt] of this.store.entries()) {
55
+ if (now > expiresAt) {
56
+ this.store.delete(id);
57
+ }
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Stop the background cleanup (useful for tests or shutting down)
63
+ */
64
+ stop() {
65
+ clearInterval(this.cleanupInterval);
66
+ }
67
+ }
68
+
69
+ module.exports = { IdempotencyStore };
package/package.json CHANGED
@@ -1,6 +1,23 @@
1
1
  {
2
2
  "name": "idempotency-store",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A zero-dependency idempotency store to prevent duplicate webhook processing.",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "files": [
8
+ "index.js",
9
+ "index.d.ts"
10
+ ],
11
+ "scripts": {
12
+ "test": "node test.js"
13
+ },
14
+ "keywords": [
15
+ "idempotency",
16
+ "webhook",
17
+ "payment",
18
+ "cache",
19
+ "stripe"
20
+ ],
21
+ "author": "Deepasri Kanagaraj",
22
+ "license": "MIT"
23
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.