@spreadspace/sdk 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/README.md +197 -0
- package/dist/chunk-2JW6MGIK.js +139 -0
- package/dist/chunk-2JW6MGIK.js.map +1 -0
- package/dist/index.cjs +1478 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1004 -0
- package/dist/index.d.ts +1004 -0
- package/dist/index.js +1284 -0
- package/dist/index.js.map +1 -0
- package/dist/webhooks.cjs +165 -0
- package/dist/webhooks.cjs.map +1 -0
- package/dist/webhooks.d.cts +156 -0
- package/dist/webhooks.d.ts +156 -0
- package/dist/webhooks.js +11 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# SpreadSpace TypeScript SDK
|
|
2
|
+
|
|
3
|
+
Official TypeScript SDK for the [SpreadSpace](https://spreadspace.ai) API —
|
|
4
|
+
document ingestion, extraction, and spreads for lending workflows.
|
|
5
|
+
|
|
6
|
+
- Typed error hierarchy, automatic retries with backoff, and idempotency.
|
|
7
|
+
- Cursor pagination as a native `for await ... of` async iterable.
|
|
8
|
+
- First-class helpers for the async-operation (export) and document-upload flows.
|
|
9
|
+
- ESM + CommonJS, full type declarations, zero runtime dependencies.
|
|
10
|
+
|
|
11
|
+
Node `>= 18` (uses the platform `fetch` and Web Crypto).
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm i @spreadspace/sdk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
> Building **from source**? The typed request/response substrate is generated
|
|
20
|
+
> from the public OpenAPI spec into `src/generated/` (gitignored). Run
|
|
21
|
+
> `bash scripts/generate.sh` once before `npm run build` / `tsc`. Consuming the
|
|
22
|
+
> published package needs none of that — `dist/` is shipped prebuilt.
|
|
23
|
+
|
|
24
|
+
## Quickstart
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { SpreadSpace } from '@spreadspace/sdk';
|
|
28
|
+
|
|
29
|
+
// ss_test_ -> isolated sandbox tenant; ss_live_ -> real workspace data. Same
|
|
30
|
+
// base URL. You can also set SPREADSPACE_API_KEY and call `new SpreadSpace()`.
|
|
31
|
+
const client = new SpreadSpace({ apiKey: 'ss_test_...' });
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### List borrowers (auto-paged)
|
|
35
|
+
|
|
36
|
+
List methods return a lazy async iterable that fetches pages on demand and stops
|
|
37
|
+
when the cursor is exhausted (`next_cursor === null`). There is no `has_more` or
|
|
38
|
+
`total` — just iterate.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
for await (const borrower of client.borrowers.list()) {
|
|
42
|
+
console.log(borrower.id);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Filters + page size:
|
|
46
|
+
for await (const b of client.borrowers.list({ intake: true, limit: 50 })) { /* ... */ }
|
|
47
|
+
|
|
48
|
+
// Loans (optionally scoped to a borrower) and jobs iterate the same way:
|
|
49
|
+
for await (const loan of client.loans.list({ borrowerId: 'abc123' })) { /* ... */ }
|
|
50
|
+
for await (const job of client.jobs.list()) { /* ... */ }
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Each `list()` is generic — pass your own row type: `client.borrowers.list<Borrower>()`.
|
|
54
|
+
|
|
55
|
+
### Create an extraction export and wait for it
|
|
56
|
+
|
|
57
|
+
Long-running work is an **async operation**: `create()` enqueues it and resolves
|
|
58
|
+
to a handle; `wait()` polls to a terminal status. Terminal states are
|
|
59
|
+
`succeeded` / `failed` / `cancelled` (note the double-L). A `failed` operation
|
|
60
|
+
rejects with `AsyncOperationError` carrying `errorCode` / `errorMessage`.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
const operation = await client.exports.create({ borrowerId: 'abc123', format: 'xlsx' });
|
|
64
|
+
|
|
65
|
+
// Poll until terminal. Rejects with AsyncOperationError on "failed",
|
|
66
|
+
// AsyncOperationTimeout if it doesn't finish in time.
|
|
67
|
+
const op = await operation.wait({ timeoutMs: 5 * 60_000 });
|
|
68
|
+
|
|
69
|
+
console.log(op.status); // 'succeeded'
|
|
70
|
+
console.log(op.resultUrl); // download link, when present
|
|
71
|
+
|
|
72
|
+
// Cancel a still-running operation (cancelling a terminal op rejects with
|
|
73
|
+
// ConflictError; cancelling an already-cancelled op is an idempotent success):
|
|
74
|
+
await client.asyncOperations.cancel(op.operationId);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Upload a document and wait for processing
|
|
78
|
+
|
|
79
|
+
Upload is a three-step dance the helper handles for you: mint a presigned URL,
|
|
80
|
+
`PUT` the bytes straight to S3, then confirm. Raw bytes never transit a
|
|
81
|
+
SpreadSpace endpoint body. `file` may be a path string, raw bytes, a `Blob` /
|
|
82
|
+
`File`, or a `Readable` / iterable of chunks.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
// wait: true also polls to a terminal job status before resolving.
|
|
86
|
+
const job = await client.documents.upload('./statement.pdf', {
|
|
87
|
+
borrowerId: 'abc123',
|
|
88
|
+
wait: true,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
console.log(job.id);
|
|
92
|
+
|
|
93
|
+
// From bytes / a Blob — fileName + contentType are required there:
|
|
94
|
+
await client.documents.upload(bytes, {
|
|
95
|
+
fileName: 'statement.pdf',
|
|
96
|
+
contentType: 'application/pdf',
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Poll a job's status yourself:
|
|
100
|
+
const status = await client.documents.status(job.id);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Terminal **job** statuses are `COMPLETED` / `FAILED`; `PENDING` / `PROCESSING`
|
|
104
|
+
are in flight. A `FAILED` job rejects `wait()` with `UploadError`. (`REJECTED`
|
|
105
|
+
is a per-*document* outcome, not a job status — a failed job is what surfaces as
|
|
106
|
+
"rejected" in the UI.)
|
|
107
|
+
|
|
108
|
+
> If billing is inactive, minting the presigned URL returns **402** and the SDK
|
|
109
|
+
> throws (rather than hanging) so you see the billing wall immediately.
|
|
110
|
+
|
|
111
|
+
## Errors
|
|
112
|
+
|
|
113
|
+
Every API error maps to a typed error. Match on the class; for the stable machine
|
|
114
|
+
code read `err.type` (the wire `error.type`), never the message. Each error
|
|
115
|
+
carries `requestId` (preferred from the `X-Request-ID` response header) — quote
|
|
116
|
+
it in support tickets.
|
|
117
|
+
|
|
118
|
+
| Error | HTTP |
|
|
119
|
+
|---|---|
|
|
120
|
+
| `InvalidRequestError` | 400 |
|
|
121
|
+
| `AuthenticationError` | 401 |
|
|
122
|
+
| `PermissionError` | 403 |
|
|
123
|
+
| `NotFoundError` | 404 |
|
|
124
|
+
| `ConflictError` | 409 |
|
|
125
|
+
| `RateLimitError` | 429 (honors `Retry-After`) |
|
|
126
|
+
| `ServerError` | 5xx |
|
|
127
|
+
| `NetworkError` | transport failure (no HTTP response) |
|
|
128
|
+
|
|
129
|
+
The HTTP-status errors derive from `SpreadSpaceError`.
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { RateLimitError, SpreadSpaceError } from '@spreadspace/sdk';
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
for await (const b of client.borrowers.list()) { /* ... */ }
|
|
136
|
+
} catch (err) {
|
|
137
|
+
if (err instanceof RateLimitError) {
|
|
138
|
+
console.log(`rate limited; retry after ${err.retryAfter}s (request ${err.requestId})`);
|
|
139
|
+
} else if (err instanceof SpreadSpaceError) {
|
|
140
|
+
console.log(`${err.type}: ${err.message} (request ${err.requestId})`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
`429` and `5xx` (and transport errors) are retried automatically with exponential
|
|
146
|
+
backoff + full jitter, honoring `Retry-After`. Other `4xx` are never retried.
|
|
147
|
+
Tune with `maxRetries` in the constructor.
|
|
148
|
+
|
|
149
|
+
## Idempotency
|
|
150
|
+
|
|
151
|
+
Non-`GET` requests automatically get a generated `Idempotency-Key` (a UUID).
|
|
152
|
+
Supply your own (via the request options on the low-level `request()` escape
|
|
153
|
+
hatch) to safely retry a specific call across restarts, or pass `null` to
|
|
154
|
+
suppress it.
|
|
155
|
+
|
|
156
|
+
## Pinning the API version
|
|
157
|
+
|
|
158
|
+
Every request sends a `SpreadSpace-Version` header. The SDK pins a known-good
|
|
159
|
+
default (`DEFAULT_API_VERSION`); override it globally with `apiVersion` in the
|
|
160
|
+
constructor, or per call via the `apiVersion` option on `list()` and the helpers.
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const client = new SpreadSpace({
|
|
164
|
+
apiKey: 'ss_test_...',
|
|
165
|
+
apiVersion: '2026-05-03',
|
|
166
|
+
maxRetries: 4,
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## ⚠️ Money is a `number` (not exact — read this)
|
|
171
|
+
|
|
172
|
+
Monetary amounts decode to a plain JavaScript `number` (IEEE-754 float64).
|
|
173
|
+
**TypeScript has no decimal type**, so very large amounts or sub-cent arithmetic
|
|
174
|
+
can lose precision — do **not** treat these values as exact decimals, and do not
|
|
175
|
+
sum many of them naively where cents must reconcile.
|
|
176
|
+
|
|
177
|
+
This is a deliberate, temporary stance: the wire currently encodes money as a
|
|
178
|
+
JSON number. A future API change will move money to a **decimal string** for
|
|
179
|
+
lossless transport; that will be a coordinated, breaking change and this SDK will
|
|
180
|
+
adopt it then. For exact arithmetic today, read the raw string from the response
|
|
181
|
+
body yourself, or defer the math to the server-side spread/export. (The .NET SDK
|
|
182
|
+
decodes money as `System.Decimal` and *is* exact — this caveat is TS-specific.)
|
|
183
|
+
|
|
184
|
+
## Escape hatch
|
|
185
|
+
|
|
186
|
+
For any endpoint the resource helpers don't wrap, call the raw transport. It
|
|
187
|
+
applies all the cross-cutting behavior (auth, version header, retries,
|
|
188
|
+
idempotency, error mapping) and resolves to the decoded body (or `undefined` on a
|
|
189
|
+
204):
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
const body = await client.request('GET', '/api/some/endpoint');
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
MIT
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// src/webhooks.ts
|
|
2
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
3
|
+
var DEFAULT_FRESHNESS_TOLERANCE_SECONDS = 5 * 60;
|
|
4
|
+
var VERSION_1_PREFIX = "v1";
|
|
5
|
+
var WebhookSignatureError = class _WebhookSignatureError extends Error {
|
|
6
|
+
cause;
|
|
7
|
+
constructor(message, cause) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "WebhookSignatureError";
|
|
10
|
+
this.cause = cause;
|
|
11
|
+
Object.setPrototypeOf(this, _WebhookSignatureError.prototype);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
function verifyWebhook(rawBody, signature, secret, options) {
|
|
15
|
+
if (typeof signature !== "string" || signature.length === 0) {
|
|
16
|
+
throw new WebhookSignatureError("SpreadSpace-Signature header is missing or empty.");
|
|
17
|
+
}
|
|
18
|
+
if (typeof secret !== "string" || secret.length === 0) {
|
|
19
|
+
throw new WebhookSignatureError("Webhook signing secret is missing or empty.");
|
|
20
|
+
}
|
|
21
|
+
if (rawBody === null || rawBody === void 0) {
|
|
22
|
+
throw new WebhookSignatureError("Webhook raw body is missing.");
|
|
23
|
+
}
|
|
24
|
+
const parsed = parseSignatureHeader(signature);
|
|
25
|
+
if (!parsed) {
|
|
26
|
+
throw new WebhookSignatureError("Webhook signature header is malformed.");
|
|
27
|
+
}
|
|
28
|
+
const { timestamp, hex } = parsed;
|
|
29
|
+
const bodyString = bodyToString(rawBody);
|
|
30
|
+
const expectedHex = computeHmacHex(secret, `${timestamp}.${bodyString}`);
|
|
31
|
+
if (!hexEquals(expectedHex, hex)) {
|
|
32
|
+
throw new WebhookSignatureError("Webhook signature does not match.");
|
|
33
|
+
}
|
|
34
|
+
const tolerance = options?.freshnessTolerance ?? DEFAULT_FRESHNESS_TOLERANCE_SECONDS;
|
|
35
|
+
const now = options?.currentTimestamp ?? Math.floor(Date.now() / 1e3);
|
|
36
|
+
const age = now - timestamp;
|
|
37
|
+
if (age > tolerance || age < -tolerance) {
|
|
38
|
+
throw new WebhookSignatureError(
|
|
39
|
+
`Webhook timestamp is outside the freshness window of ${tolerance}s.`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
function verifyAndParseWebhook(rawBody, signature, secret, options) {
|
|
45
|
+
verifyWebhook(rawBody, signature, secret, options);
|
|
46
|
+
const bodyString = bodyToString(rawBody);
|
|
47
|
+
let parsed;
|
|
48
|
+
try {
|
|
49
|
+
parsed = JSON.parse(bodyString);
|
|
50
|
+
} catch (cause) {
|
|
51
|
+
throw new WebhookSignatureError("Webhook body is not valid JSON.", cause);
|
|
52
|
+
}
|
|
53
|
+
if (!isPlainObject(parsed)) {
|
|
54
|
+
throw new WebhookSignatureError("Webhook body is not a JSON object.");
|
|
55
|
+
}
|
|
56
|
+
const candidate = parsed;
|
|
57
|
+
if (typeof candidate.type !== "string" || candidate.type.length === 0) {
|
|
58
|
+
throw new WebhookSignatureError("Webhook body is missing a `type` field.");
|
|
59
|
+
}
|
|
60
|
+
if (typeof candidate.id !== "string" || candidate.id.length === 0) {
|
|
61
|
+
throw new WebhookSignatureError("Webhook body is missing an `id` field.");
|
|
62
|
+
}
|
|
63
|
+
if (typeof candidate.created !== "number") {
|
|
64
|
+
throw new WebhookSignatureError("Webhook body is missing a numeric `created` field.");
|
|
65
|
+
}
|
|
66
|
+
if (typeof candidate.tenant_id !== "string" || candidate.tenant_id.length === 0) {
|
|
67
|
+
throw new WebhookSignatureError("Webhook body is missing a `tenant_id` field.");
|
|
68
|
+
}
|
|
69
|
+
if (!isPlainObject(candidate.data)) {
|
|
70
|
+
throw new WebhookSignatureError("Webhook body is missing a `data` object.");
|
|
71
|
+
}
|
|
72
|
+
return parsed;
|
|
73
|
+
}
|
|
74
|
+
function parseSignatureHeader(header) {
|
|
75
|
+
const segments = header.split(",").filter((s) => s.length > 0);
|
|
76
|
+
if (segments.length < 2) return null;
|
|
77
|
+
let parsedTs = null;
|
|
78
|
+
let parsedV1 = null;
|
|
79
|
+
for (const raw of segments) {
|
|
80
|
+
const segment = raw.trim();
|
|
81
|
+
const eq = segment.indexOf("=");
|
|
82
|
+
if (eq <= 0 || eq === segment.length - 1) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
const key = segment.slice(0, eq).trim();
|
|
86
|
+
const value = segment.slice(eq + 1).trim();
|
|
87
|
+
if (key === "t") {
|
|
88
|
+
if (parsedTs !== null) return null;
|
|
89
|
+
if (!/^-?\d+$/.test(value)) return null;
|
|
90
|
+
const ts = Number(value);
|
|
91
|
+
if (!Number.isFinite(ts) || !Number.isInteger(ts)) return null;
|
|
92
|
+
parsedTs = ts;
|
|
93
|
+
} else if (key === VERSION_1_PREFIX) {
|
|
94
|
+
if (parsedV1 !== null) return null;
|
|
95
|
+
if (value.length === 0) return null;
|
|
96
|
+
parsedV1 = value;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (parsedTs === null || parsedV1 === null) return null;
|
|
100
|
+
return { timestamp: parsedTs, hex: parsedV1 };
|
|
101
|
+
}
|
|
102
|
+
function computeHmacHex(secret, signedPayload) {
|
|
103
|
+
const hmac = createHmac("sha256", Buffer.from(secret, "utf-8"));
|
|
104
|
+
hmac.update(Buffer.from(signedPayload, "utf-8"));
|
|
105
|
+
return hmac.digest("hex");
|
|
106
|
+
}
|
|
107
|
+
function hexEquals(expectedHex, providedHex) {
|
|
108
|
+
if (expectedHex.length !== providedHex.length) return false;
|
|
109
|
+
let expectedBytes;
|
|
110
|
+
let providedBytes;
|
|
111
|
+
try {
|
|
112
|
+
expectedBytes = Buffer.from(expectedHex, "hex");
|
|
113
|
+
providedBytes = Buffer.from(providedHex, "hex");
|
|
114
|
+
} catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
if (expectedBytes.length !== providedBytes.length) return false;
|
|
118
|
+
if (expectedBytes.length * 2 !== expectedHex.length) return false;
|
|
119
|
+
if (providedBytes.length * 2 !== providedHex.length) return false;
|
|
120
|
+
return timingSafeEqual(
|
|
121
|
+
new Uint8Array(expectedBytes.buffer, expectedBytes.byteOffset, expectedBytes.byteLength),
|
|
122
|
+
new Uint8Array(providedBytes.buffer, providedBytes.byteOffset, providedBytes.byteLength)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
function bodyToString(body) {
|
|
126
|
+
if (typeof body === "string") return body;
|
|
127
|
+
if (Buffer.isBuffer(body)) return body.toString("utf-8");
|
|
128
|
+
return Buffer.from(body).toString("utf-8");
|
|
129
|
+
}
|
|
130
|
+
function isPlainObject(v) {
|
|
131
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export {
|
|
135
|
+
WebhookSignatureError,
|
|
136
|
+
verifyWebhook,
|
|
137
|
+
verifyAndParseWebhook
|
|
138
|
+
};
|
|
139
|
+
//# sourceMappingURL=chunk-2JW6MGIK.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/webhooks.ts"],"sourcesContent":["/**\n * Webhook signature verification + typed event envelopes.\n *\n * Byte-for-byte compatible with the server signer `api/src/Webhooks/WebhookSigner.cs`.\n * Wire format mirrors Stripe's `Stripe-Signature`:\n *\n * SpreadSpace-Signature: t=<unix_seconds>,v1=<hex_hmac_sha256(secret, \"{t}.{body}\")>\n *\n * The verifier:\n * 1. Parses `t=` and `v1=` tokens (tolerant of extra/reordered/unknown pairs to\n * stay forward-compatible with future schemes).\n * 2. Recomputes HMAC-SHA256 over `\"{timestamp}.{rawBody}\"` with the secret.\n * 3. Compares with `crypto.timingSafeEqual` on decoded bytes — never on hex\n * strings (`===` short-circuits and leaks prefix info via timing).\n * 4. Two-sided freshness check: rejects timestamps too far in the past (replay\n * protection) AND too far in the future (attacker-controlled skew).\n *\n * Verification order matches the C# side: HMAC FIRST, then freshness. That way a\n * forged-but-fresh request and a real-but-stale request fail indistinguishably.\n *\n * Tree-shakeable: importable as `@spreadspace/sdk/webhooks` so a webhook-receiver\n * Lambda pulls in only the verifier, not the whole client + resource tree.\n */\n\nimport { createHmac, timingSafeEqual } from 'node:crypto';\n\n/** Default freshness tolerance — matches `WebhookSignatureFormat.DefaultFreshnessTolerance` (300s). */\nconst DEFAULT_FRESHNESS_TOLERANCE_SECONDS = 5 * 60;\n\n/** Wire constant for the v1 HMAC-SHA256 scheme. */\nconst VERSION_1_PREFIX = 'v1';\n\n// ────────────────────────────────────────────────────────────────────────────\n// Webhook event types — discriminated union over the five canonical events.\n//\n// Mirror the C# payload records under `api/src/Webhooks/Events/`. Field names\n// are the exact wire keys (snake_case).\n// ────────────────────────────────────────────────────────────────────────────\n\n/**\n * Canonical webhook event-type identifier strings. Stable wire constants stored\n * verbatim in the envelope `type` field. `*` is a subscription wildcard, never\n * an emitted `type`, so it is deliberately excluded from this union.\n */\nexport type WebhookEventType =\n | 'document.processed'\n | 'document.failed'\n | 'extraction.ready'\n | 'job.completed'\n | 'loan.classified';\n\n/** `document.processed` payload. `extraction_id === document_id` if an extraction phase ran, `null` if classified-only. */\nexport interface DocumentProcessedPayload {\n document_id: string;\n job_id: string;\n borrower_id: string;\n loan_id: string;\n document_type: string;\n processed_at: string;\n extraction_id?: string | null;\n}\n\n/** `document.failed` payload. `reason` mirrors `ExtractedDocument.ErrorMessage`. */\nexport interface DocumentFailedPayload {\n document_id: string;\n job_id: string;\n borrower_id: string;\n loan_id: string;\n reason: string;\n failed_at: string;\n}\n\n/** `extraction.ready` payload. Fired once per doc when extracted line items become queryable. */\nexport interface ExtractionReadyPayload {\n extraction_id: string;\n document_id: string;\n job_id: string;\n borrower_id: string;\n loan_id: string;\n document_type: string;\n ready_at: string;\n}\n\n/** `job.completed` payload. Terminal package event, fired once every doc reaches a terminal state. */\nexport interface JobCompletedPayload {\n job_id: string;\n borrower_id: string;\n loan_id: string;\n document_count_total: number;\n document_count_succeeded: number;\n document_count_failed: number;\n completed_at: string;\n}\n\n/** `loan.classified` payload. Fired once per loan after the classification cascade finishes. */\nexport interface LoanClassifiedPayload {\n loan_id: string;\n borrower_id: string;\n job_id: string;\n document_count: number;\n classified_at: string;\n}\n\n/**\n * Common envelope fields wrapping every event (the bytes that are signed).\n * Property order is pinned server-side via `JsonPropertyOrder`; the signature is\n * over the raw byte stream, so order is immaterial to the verifier but the\n * fields are documented here for completeness. `livemode` is `false` for the\n * sandbox simulator, `true` otherwise.\n */\ninterface WebhookEnvelopeBase {\n id: string;\n created: number;\n tenant_id: string;\n livemode: boolean;\n}\n\n/**\n * Discriminated union over the five canonical webhook events. Switch on `type`\n * to narrow `data` to the matching payload.\n */\nexport type WebhookEvent =\n | (WebhookEnvelopeBase & { type: 'document.processed'; data: DocumentProcessedPayload })\n | (WebhookEnvelopeBase & { type: 'document.failed'; data: DocumentFailedPayload })\n | (WebhookEnvelopeBase & { type: 'extraction.ready'; data: ExtractionReadyPayload })\n | (WebhookEnvelopeBase & { type: 'job.completed'; data: JobCompletedPayload })\n | (WebhookEnvelopeBase & { type: 'loan.classified'; data: LoanClassifiedPayload });\n\n/** Parsed envelope returned by {@link verifyAndParseWebhook}, narrowed on `type`. */\nexport type VerifyAndParseResult = WebhookEvent;\n\n/** Options for {@link verifyWebhook} / {@link verifyAndParseWebhook}. */\nexport interface VerifyWebhookOptions {\n /**\n * Maximum age (and maximum future skew) of the timestamp before rejection.\n * Symmetric — `Math.abs(now - t)` must be ≤ this value. Default 300 seconds.\n */\n freshnessTolerance?: number;\n\n /**\n * Override the \"current time\" (unix seconds) used for the freshness check.\n * Useful in tests; otherwise leave undefined and the verifier reads\n * `Date.now() / 1000`.\n */\n currentTimestamp?: number;\n}\n\n/**\n * Thrown when a webhook signature fails verification or the envelope is\n * structurally malformed. Self-contained — distinct from `SpreadSpaceError`\n * because verifiers don't need the full request/response error machinery.\n */\nexport class WebhookSignatureError extends Error {\n public readonly cause?: unknown;\n\n constructor(message: string, cause?: unknown) {\n super(message);\n this.name = 'WebhookSignatureError';\n this.cause = cause;\n Object.setPrototypeOf(this, WebhookSignatureError.prototype);\n }\n}\n\n/**\n * Verify a `SpreadSpace-Signature` header against a body and signing secret.\n * Throws {@link WebhookSignatureError} on any failure (malformed header, bad\n * HMAC, stale timestamp, wrong secret); returns `true` on success.\n *\n * @param rawBody The exact bytes received on the wire — NOT re-serialized JSON.\n * Any byte-level transformation (re-stringify, gzip decode, encoding change)\n * invalidates the signature.\n * @param signature Contents of the `SpreadSpace-Signature` HTTP header.\n * @param secret The plaintext signing secret (`whsec_...`). Never log this.\n * @param options Optional freshness window + clock injection.\n */\nexport function verifyWebhook(\n rawBody: string | Buffer | Uint8Array,\n signature: string,\n secret: string,\n options?: VerifyWebhookOptions,\n): true {\n if (typeof signature !== 'string' || signature.length === 0) {\n throw new WebhookSignatureError('SpreadSpace-Signature header is missing or empty.');\n }\n if (typeof secret !== 'string' || secret.length === 0) {\n throw new WebhookSignatureError('Webhook signing secret is missing or empty.');\n }\n if (rawBody === null || rawBody === undefined) {\n throw new WebhookSignatureError('Webhook raw body is missing.');\n }\n\n const parsed = parseSignatureHeader(signature);\n if (!parsed) {\n throw new WebhookSignatureError('Webhook signature header is malformed.');\n }\n const { timestamp, hex } = parsed;\n\n // HMAC FIRST. Never leak (via timing or distinct error text) whether a request\n // was a forged signature vs. a real one that aged out — both fail the same way.\n const bodyString = bodyToString(rawBody);\n const expectedHex = computeHmacHex(secret, `${timestamp}.${bodyString}`);\n\n if (!hexEquals(expectedHex, hex)) {\n throw new WebhookSignatureError('Webhook signature does not match.');\n }\n\n // Then freshness. Two-sided: too old OR too far in the future.\n const tolerance = options?.freshnessTolerance ?? DEFAULT_FRESHNESS_TOLERANCE_SECONDS;\n const now = options?.currentTimestamp ?? Math.floor(Date.now() / 1000);\n const age = now - timestamp;\n if (age > tolerance || age < -tolerance) {\n throw new WebhookSignatureError(\n `Webhook timestamp is outside the freshness window of ${tolerance}s.`,\n );\n }\n\n return true;\n}\n\n/**\n * Verify a webhook signature and JSON-parse the body into a typed\n * {@link WebhookEvent} envelope. Throws {@link WebhookSignatureError} on\n * signature failure or invalid JSON. Switch on `event.type` to narrow\n * `event.data` to the matching payload.\n */\nexport function verifyAndParseWebhook(\n rawBody: string | Buffer | Uint8Array,\n signature: string,\n secret: string,\n options?: VerifyWebhookOptions,\n): VerifyAndParseResult {\n verifyWebhook(rawBody, signature, secret, options);\n\n const bodyString = bodyToString(rawBody);\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(bodyString);\n } catch (cause) {\n throw new WebhookSignatureError('Webhook body is not valid JSON.', cause);\n }\n\n if (!isPlainObject(parsed)) {\n throw new WebhookSignatureError('Webhook body is not a JSON object.');\n }\n const candidate = parsed;\n if (typeof candidate.type !== 'string' || candidate.type.length === 0) {\n throw new WebhookSignatureError('Webhook body is missing a `type` field.');\n }\n if (typeof candidate.id !== 'string' || candidate.id.length === 0) {\n throw new WebhookSignatureError('Webhook body is missing an `id` field.');\n }\n if (typeof candidate.created !== 'number') {\n throw new WebhookSignatureError('Webhook body is missing a numeric `created` field.');\n }\n if (typeof candidate.tenant_id !== 'string' || candidate.tenant_id.length === 0) {\n throw new WebhookSignatureError('Webhook body is missing a `tenant_id` field.');\n }\n if (!isPlainObject(candidate.data)) {\n throw new WebhookSignatureError('Webhook body is missing a `data` object.');\n }\n\n return parsed as unknown as VerifyAndParseResult;\n}\n\n// ────────────────────────────────────────────────────────────────────────────\n// Internals\n// ────────────────────────────────────────────────────────────────────────────\n\ninterface ParsedSignature {\n timestamp: number;\n hex: string;\n}\n\n/**\n * Parse a header in the form `t=<long>,v1=<hex>`. Tolerant of whitespace between\n * segments and of additional unrecognized pairs (e.g. a future `v2=...`) — those\n * are silently ignored, keeping the verifier forward-compatible. Duplicate `t=`\n * or `v1=`, an empty value, a missing `=`, or fewer than two segments → reject.\n */\nfunction parseSignatureHeader(header: string): ParsedSignature | null {\n const segments = header.split(',').filter((s) => s.length > 0);\n if (segments.length < 2) return null;\n\n let parsedTs: number | null = null;\n let parsedV1: string | null = null;\n\n for (const raw of segments) {\n const segment = raw.trim();\n const eq = segment.indexOf('=');\n if (eq <= 0 || eq === segment.length - 1) {\n // Missing key, missing value, or `=` at the very end (`v1=`).\n return null;\n }\n\n const key = segment.slice(0, eq).trim();\n const value = segment.slice(eq + 1).trim();\n\n if (key === 't') {\n if (parsedTs !== null) return null; // Duplicate `t=`.\n // Strict integer parse — reject `1.5`, `1e3`, leading `+`, etc. The C#\n // verifier uses `long.TryParse(NumberStyles.Integer, Invariant)`, which\n // accepts an optional leading `-` and digits only. Mirror that.\n if (!/^-?\\d+$/.test(value)) return null;\n const ts = Number(value);\n if (!Number.isFinite(ts) || !Number.isInteger(ts)) return null;\n parsedTs = ts;\n } else if (key === VERSION_1_PREFIX) {\n if (parsedV1 !== null) return null; // Duplicate `v1=`.\n if (value.length === 0) return null;\n parsedV1 = value;\n }\n // Any other key — silently ignored for forward compatibility.\n }\n\n if (parsedTs === null || parsedV1 === null) return null;\n return { timestamp: parsedTs, hex: parsedV1 };\n}\n\n/**\n * Compute lowercase hex HMAC-SHA256 digest. Matches the C# side's\n * `Convert.ToHexStringLower(hmac.ComputeHash(...))` exactly — both UTF-8 encode\n * the key and message.\n */\nfunction computeHmacHex(secret: string, signedPayload: string): string {\n const hmac = createHmac('sha256', Buffer.from(secret, 'utf-8'));\n hmac.update(Buffer.from(signedPayload, 'utf-8'));\n return hmac.digest('hex'); // Node default is lowercase.\n}\n\n/**\n * Constant-time compare of two hex strings. Decode both sides to bytes before\n * `timingSafeEqual` (it requires equal-length inputs). A length mismatch fails\n * fast — length is public (SHA-256 emits 64 hex chars), not timing-sensitive.\n */\nfunction hexEquals(expectedHex: string, providedHex: string): boolean {\n if (expectedHex.length !== providedHex.length) return false;\n\n let expectedBytes: Buffer;\n let providedBytes: Buffer;\n try {\n expectedBytes = Buffer.from(expectedHex, 'hex');\n providedBytes = Buffer.from(providedHex, 'hex');\n } catch {\n return false;\n }\n\n // `Buffer.from(..., 'hex')` silently stops at the first non-hex character,\n // producing a shorter buffer — guard explicitly so invalid hex that truncates\n // to a matching length can't produce a false equality.\n if (expectedBytes.length !== providedBytes.length) return false;\n if (expectedBytes.length * 2 !== expectedHex.length) return false;\n if (providedBytes.length * 2 !== providedHex.length) return false;\n\n return timingSafeEqual(\n new Uint8Array(expectedBytes.buffer, expectedBytes.byteOffset, expectedBytes.byteLength),\n new Uint8Array(providedBytes.buffer, providedBytes.byteOffset, providedBytes.byteLength),\n );\n}\n\nfunction bodyToString(body: string | Buffer | Uint8Array): string {\n if (typeof body === 'string') return body;\n if (Buffer.isBuffer(body)) return body.toString('utf-8');\n return Buffer.from(body).toString('utf-8');\n}\n\nfunction isPlainObject(v: unknown): v is Record<string, unknown> {\n return typeof v === 'object' && v !== null && !Array.isArray(v);\n}\n"],"mappings":";AAwBA,SAAS,YAAY,uBAAuB;AAG5C,IAAM,sCAAsC,IAAI;AAGhD,IAAM,mBAAmB;AA0HlB,IAAM,wBAAN,MAAM,+BAA8B,MAAM;AAAA,EAC/B;AAAA,EAEhB,YAAY,SAAiB,OAAiB;AAC5C,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,QAAQ;AACb,WAAO,eAAe,MAAM,uBAAsB,SAAS;AAAA,EAC7D;AACF;AAcO,SAAS,cACd,SACA,WACA,QACA,SACM;AACN,MAAI,OAAO,cAAc,YAAY,UAAU,WAAW,GAAG;AAC3D,UAAM,IAAI,sBAAsB,mDAAmD;AAAA,EACrF;AACA,MAAI,OAAO,WAAW,YAAY,OAAO,WAAW,GAAG;AACrD,UAAM,IAAI,sBAAsB,6CAA6C;AAAA,EAC/E;AACA,MAAI,YAAY,QAAQ,YAAY,QAAW;AAC7C,UAAM,IAAI,sBAAsB,8BAA8B;AAAA,EAChE;AAEA,QAA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