slashbin-ingest-kit 0.0.0-stage → 1.3.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 +24 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/ingest-signer.d.ts +36 -0
- package/dist/ingest-signer.js +48 -0
- package/package.json +35 -4
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# slashbin-ingest-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
For your ingest service, when a source has no usable webhook and your code posts events into Slashbin. This package is code you run in your own service. It is not a hosted Slashbin service.
|
|
4
|
+
|
|
5
|
+
## IngestSigner
|
|
6
|
+
|
|
7
|
+
Signs the raw body of a POST to `/api/ingest/:projectId` to your project's signature contract. Every value comes from the project's Console signature settings; the signer guesses none of them, because a wrong guess is a bare 401.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { IngestSigner } from "slashbin-ingest-kit";
|
|
11
|
+
|
|
12
|
+
// Throws IngestSignerError("missing-key") with no secret, and ("invalid-config") without headerName or encoding.
|
|
13
|
+
const signer = new IngestSigner({
|
|
14
|
+
secret: process.env.YOUR_SIGNING_KEY!, // you read the env var; the kit does not
|
|
15
|
+
headerName: "x-your-signature", // the project's signatureHeader
|
|
16
|
+
encoding: "hex", // the project's signatureEncoding
|
|
17
|
+
prefix: "sha256=", // the project's signaturePrefix, "" if none
|
|
18
|
+
// algorithm: "sha256" (default) | "sha1" | "sha512"
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const { headers, body } = signer.sign(Buffer.from(JSON.stringify(event)));
|
|
22
|
+
await fetch(url, { method: "POST", headers: { "content-type": "application/json", ...headers }, body });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`sign` returns the same bytes it was given: send exactly those, never re-serialize, since the gateway verifies the raw body. There is no unsigned path. Sending, retries and progress are outside this signer.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { IngestSigner, IngestSignerError } from "./ingest-signer.js";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
type Algorithm = "sha256" | "sha1" | "sha512";
|
|
2
|
+
type Encoding = "hex" | "base64";
|
|
3
|
+
/** Every value comes from the project's Console signature settings; the signer never guesses one. */
|
|
4
|
+
export interface IngestSignerConfig {
|
|
5
|
+
/** Non-empty. An empty or missing secret throws `missing-key`. */
|
|
6
|
+
secret: string;
|
|
7
|
+
/** The project's `signatureHeader`. Required: the gateway has no default. */
|
|
8
|
+
headerName: string;
|
|
9
|
+
/** The project's `signatureAlgorithm`. Defaults to "sha256", as the gateway does. */
|
|
10
|
+
algorithm?: Algorithm;
|
|
11
|
+
/** The project's `signatureEncoding`. Required: guessing it is a bare 401. */
|
|
12
|
+
encoding: Encoding;
|
|
13
|
+
/** The project's `signaturePrefix`, for example "sha256=". Defaults to "". */
|
|
14
|
+
prefix?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface SignedRequest {
|
|
17
|
+
/** Exactly `{ [headerName]: prefix + digest }`. */
|
|
18
|
+
headers: Record<string, string>;
|
|
19
|
+
/** The same bytes passed in, never re-serialized: the gateway verifies the raw body. */
|
|
20
|
+
body: Buffer;
|
|
21
|
+
}
|
|
22
|
+
export declare class IngestSignerError extends Error {
|
|
23
|
+
readonly code: "missing-key" | "invalid-config";
|
|
24
|
+
constructor(code: "missing-key" | "invalid-config", message: string);
|
|
25
|
+
}
|
|
26
|
+
/** Signs the raw body of a POST to `/api/ingest/:projectId`. It never returns an unsigned request. */
|
|
27
|
+
export declare class IngestSigner {
|
|
28
|
+
private readonly secret;
|
|
29
|
+
private readonly headerName;
|
|
30
|
+
private readonly algorithm;
|
|
31
|
+
private readonly encoding;
|
|
32
|
+
private readonly prefix;
|
|
33
|
+
constructor(cfg: IngestSignerConfig);
|
|
34
|
+
sign(rawBody: Buffer): SignedRequest;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createHmac } from "node:crypto";
|
|
2
|
+
const ALGORITHMS = ["sha256", "sha1", "sha512"];
|
|
3
|
+
const ENCODINGS = ["hex", "base64"];
|
|
4
|
+
export class IngestSignerError extends Error {
|
|
5
|
+
code;
|
|
6
|
+
constructor(code, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.name = "IngestSignerError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** Signs the raw body of a POST to `/api/ingest/:projectId`. It never returns an unsigned request. */
|
|
13
|
+
export class IngestSigner {
|
|
14
|
+
secret;
|
|
15
|
+
headerName;
|
|
16
|
+
algorithm;
|
|
17
|
+
encoding;
|
|
18
|
+
prefix;
|
|
19
|
+
constructor(cfg) {
|
|
20
|
+
if (typeof cfg?.secret !== "string" || cfg.secret === "") {
|
|
21
|
+
throw new IngestSignerError("missing-key", "No signing secret: set the project's signing key before sending to Slashbin.");
|
|
22
|
+
}
|
|
23
|
+
this.secret = cfg.secret;
|
|
24
|
+
this.headerName = requireNonBlank(cfg.headerName, "headerName");
|
|
25
|
+
this.encoding = requireOneOf(cfg.encoding, ENCODINGS, "encoding");
|
|
26
|
+
this.algorithm = cfg.algorithm === undefined ? "sha256" : requireOneOf(cfg.algorithm, ALGORITHMS, "algorithm");
|
|
27
|
+
this.prefix = cfg.prefix ?? "";
|
|
28
|
+
if (typeof this.prefix !== "string") {
|
|
29
|
+
throw new IngestSignerError("invalid-config", "prefix must be a string.");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
sign(rawBody) {
|
|
33
|
+
const digest = createHmac(this.algorithm, this.secret).update(rawBody).digest(this.encoding);
|
|
34
|
+
return { headers: { [this.headerName]: this.prefix + digest }, body: rawBody };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function requireNonBlank(value, name) {
|
|
38
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
39
|
+
throw new IngestSignerError("invalid-config", `${name} is required: copy it from the project's Console signature settings.`);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
function requireOneOf(value, allowed, name) {
|
|
44
|
+
if (typeof value !== "string" || !allowed.includes(value)) {
|
|
45
|
+
throw new IngestSignerError("invalid-config", `${name} must be one of ${allowed.join(", ")}; got ${JSON.stringify(value)}.`);
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "slashbin-ingest-kit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "Ingest-side kit for your ingest service: signs the raw body of a POST into Slashbin to the project's signature contract, and refuses to sign without a key.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/slashbin-io/slashbin_kits.git",
|
|
9
|
+
"directory": "packages/ingest-kit"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/slashbin-io/slashbin_kits/tree/main/packages/ingest-kit#readme",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"check": "tsc --noEmit",
|
|
29
|
+
"build": "tsc",
|
|
30
|
+
"test": "vitest run"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^22.0.0",
|
|
34
|
+
"typescript": "^5.6.0",
|
|
35
|
+
"vitest": "^3.0.0"
|
|
36
|
+
}
|
|
37
|
+
}
|