@ampless/plugin-webhook 0.2.0-alpha.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/LICENSE +21 -0
- package/README.md +103 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +87 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ampless contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @ampless/plugin-webhook
|
|
2
|
+
|
|
3
|
+
POST ampless events to one or more external URLs.
|
|
4
|
+
|
|
5
|
+
> **Pre-release / alpha.** Breaking changes possible in any minor version until v1.0.
|
|
6
|
+
|
|
7
|
+
Runs in the **untrusted** Lambda — it makes outbound HTTPS calls and never touches AWS data, so a compromised webhook receiver can't pivot into your CMS.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @ampless/plugin-webhook@alpha
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Configure
|
|
16
|
+
|
|
17
|
+
In `cms.config.ts`:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import webhookPlugin from '@ampless/plugin-webhook'
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
// ...
|
|
24
|
+
plugins: [
|
|
25
|
+
webhookPlugin({
|
|
26
|
+
endpoints: [
|
|
27
|
+
{
|
|
28
|
+
url: 'https://example.com/hooks/ampless',
|
|
29
|
+
secret: process.env.WEBHOOK_SECRET,
|
|
30
|
+
events: ['content.published', 'content.unpublished', 'content.deleted'],
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
url: 'https://discord.com/api/webhooks/.../...',
|
|
34
|
+
// No secret — Discord doesn't verify signatures
|
|
35
|
+
events: ['content.published'],
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
}),
|
|
39
|
+
],
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
| Option | Default | Notes |
|
|
44
|
+
|---|---|---|
|
|
45
|
+
| `endpoints[].url` | required | HTTPS endpoint to POST to |
|
|
46
|
+
| `endpoints[].secret` | none | When set, body is HMAC-SHA-256 signed; sent in `X-Ampless-Signature` |
|
|
47
|
+
| `endpoints[].events` | all `content.*` | Restrict which event types fire this endpoint |
|
|
48
|
+
| `endpoints[].headers` | `{}` | Extra headers merged into every request |
|
|
49
|
+
| `endpoints[].timeoutMs` | `5000` | Per-request timeout |
|
|
50
|
+
| `url` (top-level) | none | Single-endpoint shortcut, equivalent to `endpoints: [{ url, secret }]` |
|
|
51
|
+
| `secret` (top-level) | none | Pairs with the top-level `url` |
|
|
52
|
+
|
|
53
|
+
## Request shape
|
|
54
|
+
|
|
55
|
+
```http
|
|
56
|
+
POST /hooks/ampless HTTP/1.1
|
|
57
|
+
Host: example.com
|
|
58
|
+
Content-Type: application/json
|
|
59
|
+
X-Ampless-Event: content.published
|
|
60
|
+
X-Ampless-Signature: sha256=<hex>
|
|
61
|
+
|
|
62
|
+
{
|
|
63
|
+
"type": "content.published",
|
|
64
|
+
"payload": {
|
|
65
|
+
"siteId": "default",
|
|
66
|
+
"postId": "post-001",
|
|
67
|
+
"slug": "hello",
|
|
68
|
+
"title": "Hello",
|
|
69
|
+
"status": "published",
|
|
70
|
+
"publishedAt": "2026-04-30T00:00:00.000Z",
|
|
71
|
+
"tags": ["intro"]
|
|
72
|
+
},
|
|
73
|
+
"timestamp": "2026-04-30T00:00:01.000Z"
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Verifying the signature (Node.js)
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { createHmac, timingSafeEqual } from 'node:crypto'
|
|
81
|
+
|
|
82
|
+
function verify(rawBody: string, signatureHeader: string, secret: string): boolean {
|
|
83
|
+
const expected = 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex')
|
|
84
|
+
const a = Buffer.from(expected)
|
|
85
|
+
const b = Buffer.from(signatureHeader)
|
|
86
|
+
if (a.length !== b.length) return false
|
|
87
|
+
return timingSafeEqual(a, b)
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Always compute the HMAC over the **raw request body** before any JSON parsing, and use a constant-time comparison.
|
|
92
|
+
|
|
93
|
+
## Retry behavior
|
|
94
|
+
|
|
95
|
+
When any endpoint returns a non-2xx response (or times out), the plugin throws. The trust-level processor Lambda re-throws to SQS, which retries the message up to 3 times before moving it to the dead-letter queue. Idempotent receivers are recommended — the same event can be delivered more than once.
|
|
96
|
+
|
|
97
|
+
## Events emitted
|
|
98
|
+
|
|
99
|
+
- `content.created` — new post (any status)
|
|
100
|
+
- `content.published` — status went `draft` → `published`, or a published post was inserted
|
|
101
|
+
- `content.unpublished` — status went `published` → `draft`, or a published post was deleted
|
|
102
|
+
- `content.updated` — any MODIFY (also fires alongside published/unpublished on transitions)
|
|
103
|
+
- `content.deleted` — post removed
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { EventType, AmplessPlugin } from 'ampless';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* HMAC SHA-256 signature in the format `sha256=<hex>`. Receivers verify
|
|
5
|
+
* by recomputing the HMAC over the raw request body and constant-time
|
|
6
|
+
* comparing against the header value (same conventions as GitHub
|
|
7
|
+
* webhooks, Stripe, etc.).
|
|
8
|
+
*/
|
|
9
|
+
declare function signPayload(secret: string, body: string): string;
|
|
10
|
+
|
|
11
|
+
interface WebhookEndpoint {
|
|
12
|
+
/** Target URL. Required. */
|
|
13
|
+
url: string;
|
|
14
|
+
/**
|
|
15
|
+
* Optional shared secret. When set, the body is HMAC-SHA-256 signed and
|
|
16
|
+
* the result is sent in the `X-Ampless-Signature` header (format:
|
|
17
|
+
* `sha256=<hex>`). Receivers should constant-time compare against the
|
|
18
|
+
* recomputed HMAC over the raw body to authenticate.
|
|
19
|
+
*/
|
|
20
|
+
secret?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Restrict which events fire this endpoint. Omit to receive all
|
|
23
|
+
* supported event types (currently the content.* family).
|
|
24
|
+
*/
|
|
25
|
+
events?: EventType[];
|
|
26
|
+
/** Extra headers merged into every request to this endpoint. */
|
|
27
|
+
headers?: Record<string, string>;
|
|
28
|
+
/** Per-request timeout in ms. Default 5000. */
|
|
29
|
+
timeoutMs?: number;
|
|
30
|
+
}
|
|
31
|
+
interface WebhookPluginOptions {
|
|
32
|
+
endpoints?: WebhookEndpoint[];
|
|
33
|
+
/** Backwards-compatible single-endpoint shortcut. */
|
|
34
|
+
url?: string;
|
|
35
|
+
secret?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Webhook plugin. Posts a JSON envelope to one or more external URLs
|
|
39
|
+
* whenever a subscribed event fires. Runs in the untrusted Lambda — it
|
|
40
|
+
* needs no AWS data access, only outbound HTTPS.
|
|
41
|
+
*/
|
|
42
|
+
declare function webhookPlugin(options?: WebhookPluginOptions): AmplessPlugin;
|
|
43
|
+
|
|
44
|
+
export { type WebhookEndpoint, type WebhookPluginOptions, webhookPlugin as default, signPayload };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { definePlugin } from "ampless";
|
|
3
|
+
|
|
4
|
+
// src/sign.ts
|
|
5
|
+
import { createHmac } from "crypto";
|
|
6
|
+
function signPayload(secret, body) {
|
|
7
|
+
return "sha256=" + createHmac("sha256", secret).update(body).digest("hex");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// src/index.ts
|
|
11
|
+
var ALL_CONTENT_EVENTS = [
|
|
12
|
+
"content.created",
|
|
13
|
+
"content.updated",
|
|
14
|
+
"content.published",
|
|
15
|
+
"content.unpublished",
|
|
16
|
+
"content.deleted"
|
|
17
|
+
];
|
|
18
|
+
function webhookPlugin(options = {}) {
|
|
19
|
+
const endpoints = normalizeEndpoints(options);
|
|
20
|
+
const subscribed = /* @__PURE__ */ new Set();
|
|
21
|
+
for (const ep of endpoints) {
|
|
22
|
+
const events = ep.events ?? ALL_CONTENT_EVENTS;
|
|
23
|
+
events.forEach((e) => subscribed.add(e));
|
|
24
|
+
}
|
|
25
|
+
const hooks = {};
|
|
26
|
+
for (const eventType of subscribed) {
|
|
27
|
+
hooks[eventType] = async (event) => {
|
|
28
|
+
await dispatch(endpoints, event);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return definePlugin({
|
|
32
|
+
name: "webhook",
|
|
33
|
+
apiVersion: 1,
|
|
34
|
+
trust_level: "untrusted",
|
|
35
|
+
hooks
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function normalizeEndpoints(options) {
|
|
39
|
+
const list = [];
|
|
40
|
+
if (options.url) list.push({ url: options.url, secret: options.secret });
|
|
41
|
+
if (options.endpoints) list.push(...options.endpoints);
|
|
42
|
+
return list;
|
|
43
|
+
}
|
|
44
|
+
async function dispatch(endpoints, event) {
|
|
45
|
+
const body = JSON.stringify({
|
|
46
|
+
type: event.type,
|
|
47
|
+
payload: event.payload,
|
|
48
|
+
timestamp: event.timestamp
|
|
49
|
+
});
|
|
50
|
+
const results = await Promise.allSettled(
|
|
51
|
+
endpoints.filter((ep) => (ep.events ?? ALL_CONTENT_EVENTS).includes(event.type)).map((ep) => postOne(ep, body, event.type))
|
|
52
|
+
);
|
|
53
|
+
const failures = results.filter((r) => r.status === "rejected");
|
|
54
|
+
if (failures.length > 0) {
|
|
55
|
+
const messages = failures.map((f) => String(f.reason)).join("; ");
|
|
56
|
+
throw new Error(`webhook plugin: ${failures.length} endpoint(s) failed \u2014 ${messages}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async function postOne(endpoint, body, eventType) {
|
|
60
|
+
const headers = {
|
|
61
|
+
"Content-Type": "application/json",
|
|
62
|
+
"X-Ampless-Event": eventType,
|
|
63
|
+
...endpoint.headers ?? {}
|
|
64
|
+
};
|
|
65
|
+
if (endpoint.secret) {
|
|
66
|
+
headers["X-Ampless-Signature"] = signPayload(endpoint.secret, body);
|
|
67
|
+
}
|
|
68
|
+
const controller = new AbortController();
|
|
69
|
+
const timer = setTimeout(() => controller.abort(), endpoint.timeoutMs ?? 5e3);
|
|
70
|
+
try {
|
|
71
|
+
const response = await fetch(endpoint.url, {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers,
|
|
74
|
+
body,
|
|
75
|
+
signal: controller.signal
|
|
76
|
+
});
|
|
77
|
+
if (!response.ok) {
|
|
78
|
+
throw new Error(`${endpoint.url} returned ${response.status}`);
|
|
79
|
+
}
|
|
80
|
+
} finally {
|
|
81
|
+
clearTimeout(timer);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
webhookPlugin as default,
|
|
86
|
+
signPayload
|
|
87
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ampless/plugin-webhook",
|
|
3
|
+
"version": "0.2.0-alpha.0",
|
|
4
|
+
"description": "Webhook plugin for ampless — POST events to external URLs",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/heavymoons/ampless.git",
|
|
23
|
+
"directory": "packages/plugin-webhook"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/heavymoons/ampless/tree/main/packages/plugin-webhook#readme",
|
|
26
|
+
"bugs": "https://github.com/heavymoons/ampless/issues",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"ampless": "0.2.0-alpha.0"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"ampless",
|
|
32
|
+
"plugin",
|
|
33
|
+
"webhook"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup",
|
|
37
|
+
"dev": "tsup --watch",
|
|
38
|
+
"lint": "tsc --noEmit",
|
|
39
|
+
"test": "vitest run --passWithNoTests"
|
|
40
|
+
}
|
|
41
|
+
}
|