slashbin-receiver-kit 1.1.1 → 1.2.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/README.md +9 -0
- package/dist/claim-store.d.ts +6 -0
- package/dist/claim-store.js +1 -0
- package/dist/event-id-claim.d.ts +24 -0
- package/dist/event-id-claim.js +38 -0
- package/dist/in-memory-claim-store.d.ts +6 -0
- package/dist/in-memory-claim-store.js +18 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -74,3 +74,12 @@ export const rotating = new ReceiverSignatureVerifier({
|
|
|
74
74
|
Source and issues: <https://github.com/slashbin-io/slashbin_kits>
|
|
75
75
|
|
|
76
76
|
MIT licence.
|
|
77
|
+
|
|
78
|
+
## Event ID claim
|
|
79
|
+
|
|
80
|
+
`EventIdClaim` reads the event ID from `slashbin_job_id` in the body, else the `x-slashbin-job-id` header, and claims `(source, event ID)` through a `ClaimStore` that you implement over your own storage. `InMemoryClaimStore` is a non-durable test double.
|
|
81
|
+
|
|
82
|
+
- A blank or whitespace-only ID counts as absent: `claim()` returns `refused` and the store is not called. Respond `400 { "error": "Missing event id" }`.
|
|
83
|
+
- A non-blank ID is stored exactly as sent.
|
|
84
|
+
- `claimed`: enqueue, and call `release(eventId)` if the enqueue fails so the sender's retry enqueues once. `duplicate`: do not enqueue; respond 202.
|
|
85
|
+
- A `ClaimStore` error rejects `claim()`. It is never reported as a duplicate; respond 500 so the sender retries.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export interface ClaimStore {
|
|
2
|
+
/** Atomic. true = newly claimed; false = (source, eventId) already present. Rejects on a storage error; never returns false for one. */
|
|
3
|
+
tryClaim(source: string, eventId: string): Promise<boolean>;
|
|
4
|
+
/** Removes the claim. Resolves whether or not it was present. Rejects only on a storage error. */
|
|
5
|
+
release(source: string, eventId: string): Promise<void>;
|
|
6
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ClaimStore } from "./claim-store.js";
|
|
2
|
+
import type { IncomingHeaders } from "./receiver-signature-verifier.js";
|
|
3
|
+
export interface EventIdClaimOptions {
|
|
4
|
+
/** The source slug, e.g. "shopify"; part of the claim key. */
|
|
5
|
+
source: string;
|
|
6
|
+
}
|
|
7
|
+
export type ClaimOutcome = {
|
|
8
|
+
kind: "claimed";
|
|
9
|
+
eventId: string;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "duplicate";
|
|
12
|
+
eventId: string;
|
|
13
|
+
} | {
|
|
14
|
+
kind: "refused";
|
|
15
|
+
reason: "missing-event-id";
|
|
16
|
+
};
|
|
17
|
+
export declare class EventIdClaim {
|
|
18
|
+
private readonly store;
|
|
19
|
+
private readonly opts;
|
|
20
|
+
constructor(store: ClaimStore, opts: EventIdClaimOptions);
|
|
21
|
+
extract(body: unknown, headers: IncomingHeaders): string | null;
|
|
22
|
+
claim(body: unknown, headers: IncomingHeaders): Promise<ClaimOutcome>;
|
|
23
|
+
release(eventId: string): Promise<void>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// ID sources match Slashbin's delivery:
|
|
2
|
+
// body slashbin_job_id (the signed envelope, so it wins)
|
|
3
|
+
// header x-slashbin-job-id
|
|
4
|
+
const BODY_FIELD = "slashbin_job_id";
|
|
5
|
+
const HEADER_NAME = "x-slashbin-job-id";
|
|
6
|
+
export class EventIdClaim {
|
|
7
|
+
store;
|
|
8
|
+
opts;
|
|
9
|
+
constructor(store, opts) {
|
|
10
|
+
this.store = store;
|
|
11
|
+
this.opts = opts;
|
|
12
|
+
}
|
|
13
|
+
// Pure; no store call. A non-blank ID is returned exactly as sent: trimming could merge two jobs.
|
|
14
|
+
extract(body, headers) {
|
|
15
|
+
const fromBody = isRecord(body) ? body[BODY_FIELD] : undefined;
|
|
16
|
+
const fromHeader = headers[HEADER_NAME];
|
|
17
|
+
return nonBlank(fromBody) ?? nonBlank(Array.isArray(fromHeader) ? fromHeader[0] : fromHeader);
|
|
18
|
+
}
|
|
19
|
+
// A ClaimStore rejection rejects this promise; it is never reported as a duplicate, so the
|
|
20
|
+
// caller returns 500 and the sender retries.
|
|
21
|
+
async claim(body, headers) {
|
|
22
|
+
const eventId = this.extract(body, headers);
|
|
23
|
+
if (eventId === null)
|
|
24
|
+
return { kind: "refused", reason: "missing-event-id" };
|
|
25
|
+
const claimed = await this.store.tryClaim(this.opts.source, eventId);
|
|
26
|
+
return { kind: claimed ? "claimed" : "duplicate", eventId };
|
|
27
|
+
}
|
|
28
|
+
// Idempotent. Resolves when the claim is absent.
|
|
29
|
+
release(eventId) {
|
|
30
|
+
return this.store.release(this.opts.source, eventId);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function isRecord(value) {
|
|
34
|
+
return typeof value === "object" && value !== null;
|
|
35
|
+
}
|
|
36
|
+
function nonBlank(value) {
|
|
37
|
+
return typeof value === "string" && value.trim() !== "" ? value : null;
|
|
38
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ClaimStore } from "./claim-store.js";
|
|
2
|
+
export declare class InMemoryClaimStore implements ClaimStore {
|
|
3
|
+
private readonly claims;
|
|
4
|
+
tryClaim(source: string, eventId: string): Promise<boolean>;
|
|
5
|
+
release(source: string, eventId: string): Promise<void>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Test double with the same semantics as a real ClaimStore. Not durable: a restart forgets every claim.
|
|
2
|
+
export class InMemoryClaimStore {
|
|
3
|
+
claims = new Set();
|
|
4
|
+
async tryClaim(source, eventId) {
|
|
5
|
+
const key = claimKey(source, eventId);
|
|
6
|
+
if (this.claims.has(key))
|
|
7
|
+
return false;
|
|
8
|
+
this.claims.add(key);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
async release(source, eventId) {
|
|
12
|
+
this.claims.delete(claimKey(source, eventId));
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
// JSON keeps (source, eventId) pairs distinct whatever characters either part holds.
|
|
16
|
+
function claimKey(source, eventId) {
|
|
17
|
+
return JSON.stringify([source, eventId]);
|
|
18
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
export { ReceiverSignatureVerifier } from "./receiver-signature-verifier.js";
|
|
2
2
|
export type { IncomingHeaders, ReceiverSignatureVerifierOptions, VerifyResult, } from "./receiver-signature-verifier.js";
|
|
3
3
|
export { slashbinSignatureMiddleware } from "./slashbin-signature-middleware.js";
|
|
4
|
+
export { EventIdClaim } from "./event-id-claim.js";
|
|
5
|
+
export type { ClaimOutcome, EventIdClaimOptions } from "./event-id-claim.js";
|
|
6
|
+
export type { ClaimStore } from "./claim-store.js";
|
|
7
|
+
export { InMemoryClaimStore } from "./in-memory-claim-store.js";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export { ReceiverSignatureVerifier } from "./receiver-signature-verifier.js";
|
|
2
2
|
export { slashbinSignatureMiddleware } from "./slashbin-signature-middleware.js";
|
|
3
|
+
export { EventIdClaim } from "./event-id-claim.js";
|
|
4
|
+
export { InMemoryClaimStore } from "./in-memory-claim-store.js";
|
package/package.json
CHANGED