webhook-sequence-guard 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.
Files changed (4) hide show
  1. package/README.md +12 -2
  2. package/index.d.ts +12 -0
  3. package/index.js +61 -0
  4. package/package.json +24 -4
package/README.md CHANGED
@@ -1,3 +1,13 @@
1
- # Temporary Holding Version
1
+ # webhook-sequence-guard
2
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.
3
+ A lightweight state machine to reject out-of-order webhooks.
4
+
5
+ ## The Problem
6
+ When payment providers (like Stripe) send webhooks, they can arrive out of order due to network delays. You might receive `order.shipped` **before** `order.created`. If you blindly apply these updates, your database state will go backwards, breaking your application.
7
+
8
+ ## The Solution
9
+ This package enforces a strict state machine. You define the allowed transitions (e.g., `created -> paid -> shipped`). If a webhook arrives that tries to go backwards, it safely rejects it.
10
+
11
+ ## Install
12
+ ```bash
13
+ npm install webhook-sequence-guard
package/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export interface TransitionResult {
2
+ valid: boolean;
3
+ reason?: string;
4
+ }
5
+
6
+ export type StateRules = Record<string, string[]>;
7
+
8
+ export class WebhookSequenceGuard {
9
+ constructor(rules: StateRules);
10
+ transition(entityId: string, newState: string): TransitionResult;
11
+ getState(entityId: string): string | undefined;
12
+ }
package/index.js ADDED
@@ -0,0 +1,61 @@
1
+ class WebhookSequenceGuard {
2
+ /**
3
+ * @param {object} rules - The state machine rules.
4
+ * Example: { created: ['paid', 'cancelled'], paid: ['shipped'], shipped: ['delivered'] }
5
+ */
6
+ constructor(rules) {
7
+ this.rules = rules;
8
+ this.states = new Map();
9
+ }
10
+
11
+ /**
12
+ * Checks if a transition is valid and updates the state.
13
+ * @param {string} entityId - The ID of the order/user/etc.
14
+ * @param {string} newState - The incoming state from the webhook.
15
+ * @returns {object} - { valid: boolean, reason?: string }
16
+ */
17
+ transition(entityId, newState) {
18
+ if (!entityId || !newState) {
19
+ throw new Error('entityId and newState are required');
20
+ }
21
+
22
+ const currentState = this.states.get(entityId);
23
+
24
+ // 1. First time seeing this entity (e.g., 'created' arrives first)
25
+ if (!currentState) {
26
+ if (!this.rules[newState]) {
27
+ this.states.set(entityId, newState);
28
+ return { valid: true };
29
+ }
30
+ this.states.set(entityId, newState);
31
+ return { valid: true };
32
+ }
33
+
34
+ // 2. Same state arriving again (e.g., duplicate 'paid' webhook)
35
+ if (currentState === newState) {
36
+ return { valid: false, reason: `Already in state: ${newState}` };
37
+ }
38
+
39
+ // 3. Check if the transition is allowed by the rules
40
+ const allowedNextStates = this.rules[currentState] || [];
41
+
42
+ if (allowedNextStates.includes(newState)) {
43
+ this.states.set(entityId, newState);
44
+ return { valid: true };
45
+ } else {
46
+ return {
47
+ valid: false,
48
+ reason: `Invalid transition: ${currentState} -> ${newState}`
49
+ };
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Get the current state of an entity
55
+ */
56
+ getState(entityId) {
57
+ return this.states.get(entityId);
58
+ }
59
+ }
60
+
61
+ module.exports = { WebhookSequenceGuard };
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "webhook-sequence-guard",
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 lightweight state machine to reject out-of-order webhooks.",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "files": [
8
+ "index.js",
9
+ "index.d.ts"
10
+ ],
11
+ "scripts": {
12
+ "test": "vitest run"
13
+ },
14
+ "keywords": [
15
+ "webhook",
16
+ "state-machine",
17
+ "payment",
18
+ "sequence",
19
+ "out-of-order"
20
+ ],
21
+ "author": "Deepasri Kanagaraj",
22
+ "license": "MIT",
23
+ "devDependencies": {
24
+ "vitest": "^5.1.2"
25
+ }
26
+ }