@supscale/sculdoc-client 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 +84 -0
- package/dist/client.d.ts +78 -0
- package/dist/client.js +146 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +57 -0
- package/dist/errors.js +108 -0
- package/dist/errors.js.map +1 -0
- package/dist/generated/api.d.ts +4322 -0
- package/dist/generated/api.js +2 -0
- package/dist/generated/api.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/webhooks.d.ts +14 -0
- package/dist/webhooks.js +40 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/generated/api.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAE5B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const SIGNATURE_HEADER = "x-sculdoc-signature";
|
|
2
|
+
export type WebhookEventType = 'job.completed' | 'job.failed' | 'job.lost' | 'version.published' | 'version.deprecated';
|
|
3
|
+
export interface WebhookEvent<T = Record<string, unknown>> {
|
|
4
|
+
id: string;
|
|
5
|
+
type: WebhookEventType;
|
|
6
|
+
createdAt: string;
|
|
7
|
+
organizationId: string;
|
|
8
|
+
data: T;
|
|
9
|
+
}
|
|
10
|
+
export interface VerifyOptions {
|
|
11
|
+
toleranceSeconds?: number;
|
|
12
|
+
now?: number;
|
|
13
|
+
}
|
|
14
|
+
export declare function verifyWebhook<T = Record<string, unknown>>(secret: string, signature: string | null | undefined, body: string | Uint8Array, o?: VerifyOptions): Promise<WebhookEvent<T>>;
|
package/dist/webhooks.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Receiving webhooks and job callbacks: each delivery is a POST of a JSON envelope signed with HMAC-SHA256 over
|
|
2
|
+
// `<timestamp>.<raw body>` (`X-Sculdoc-Signature: t=<unix seconds>,v1=<hex>`). Check the signature on the raw body
|
|
3
|
+
// (before any JSON parsing), refuse old timestamps (replays) and dedupe on the event id: deliveries are retried.
|
|
4
|
+
import { WebhookSignatureError } from './errors.js';
|
|
5
|
+
export const SIGNATURE_HEADER = 'x-sculdoc-signature';
|
|
6
|
+
const hex = (bytes) => [...new Uint8Array(bytes)].map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
7
|
+
function equal(a, b) {
|
|
8
|
+
if (a.length !== b.length)
|
|
9
|
+
return false;
|
|
10
|
+
let diff = 0;
|
|
11
|
+
for (let i = 0; i < a.length; i++)
|
|
12
|
+
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
13
|
+
return diff === 0;
|
|
14
|
+
}
|
|
15
|
+
// Verifies a delivery and returns its event. `secret`: the `whsec_…` secret of the subscription, or the callback
|
|
16
|
+
// secret of the organization for the `callbackUrl` of a job. Throws WebhookSignatureError.
|
|
17
|
+
export async function verifyWebhook(secret, signature, body, o = {}) {
|
|
18
|
+
const fields = new Map((signature ?? '').split(',').map((part) => {
|
|
19
|
+
const i = part.indexOf('=');
|
|
20
|
+
return [part.slice(0, i).trim(), part.slice(i + 1).trim()];
|
|
21
|
+
}));
|
|
22
|
+
const t = Number(fields.get('t'));
|
|
23
|
+
const v1 = fields.get('v1');
|
|
24
|
+
if (!Number.isInteger(t) || !v1)
|
|
25
|
+
throw new WebhookSignatureError('malformed signature header');
|
|
26
|
+
const now = o.now ?? Math.floor(Date.now() / 1000);
|
|
27
|
+
if (Math.abs(now - t) > (o.toleranceSeconds ?? 300))
|
|
28
|
+
throw new WebhookSignatureError('timestamp out of tolerance');
|
|
29
|
+
const raw = typeof body === 'string' ? body : new TextDecoder().decode(body);
|
|
30
|
+
const encoder = new TextEncoder();
|
|
31
|
+
const key = await crypto.subtle.importKey('raw', encoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, [
|
|
32
|
+
'sign',
|
|
33
|
+
]);
|
|
34
|
+
const expected = hex(await crypto.subtle.sign('HMAC', key, encoder.encode(`${t}.${raw}`)));
|
|
35
|
+
if (!equal(expected, v1.toLowerCase()))
|
|
36
|
+
throw new WebhookSignatureError('signature mismatch');
|
|
37
|
+
// signed by the API with the shared secret: the envelope has the documented shape
|
|
38
|
+
return JSON.parse(raw);
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=webhooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../src/webhooks.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,mHAAmH;AACnH,iHAAiH;AACjH,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEpD,MAAM,CAAC,MAAM,gBAAgB,GAAG,qBAAqB,CAAC;AAiBtD,MAAM,GAAG,GAAG,CAAC,KAAkB,EAAU,EAAE,CAC1C,CAAC,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAEjF,SAAS,KAAK,CAAC,CAAS,EAAE,CAAS;IAClC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACxC,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE;Q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AC,CAAC;IACnH,MAAM,GAAG,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC7E,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;IAClC,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE;QAClH,MAAM;KACN,CAAC,CAAC;IACH,MAAM,QAAQ,GAAG,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAC3F,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,CAAC,WAAW,EAAE,CAAC;QAAE,MAAM,IAAI,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;IAC9F,kFAAkF;IAClF,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAoB,CAAC;AAC3C,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@supscale/sculdoc-client",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Client of the Sculdoc API: renders, jobs, contracts of template versions, webhooks",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=20"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/Supscale/sculdoc.git",
|
|
23
|
+
"directory": "clients/typescript"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"registry": "https://registry.npmjs.org",
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc -p tsconfig.json",
|
|
31
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit",
|
|
32
|
+
"test": "vitest run"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^24.13.5",
|
|
36
|
+
"typescript": "^5",
|
|
37
|
+
"vitest": "^3"
|
|
38
|
+
},
|
|
39
|
+
"license": "UNLICENSED"
|
|
40
|
+
}
|