@tagma/trigger-webhook 0.1.4 → 0.1.5
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 -21
- package/README.md +79 -79
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +288 -280
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Tagma
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tagma
|
|
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
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
# @tagma/trigger-webhook
|
|
2
|
-
|
|
3
|
-
HTTP webhook trigger plugin for [@tagma/sdk](https://www.npmjs.com/package/@tagma/sdk).
|
|
4
|
-
|
|
5
|
-
Pauses a task until a POST request arrives on a configured local HTTP listener. Optional HMAC-SHA256 signature validation protects against unauthenticated callers.
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
bun add @tagma/trigger-webhook
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
```yaml
|
|
16
|
-
pipeline:
|
|
17
|
-
name: deploy-on-demand
|
|
18
|
-
plugins:
|
|
19
|
-
- '@tagma/trigger-webhook'
|
|
20
|
-
tracks:
|
|
21
|
-
- id: deploy
|
|
22
|
-
name: Deploy
|
|
23
|
-
driver: claude-code
|
|
24
|
-
tasks:
|
|
25
|
-
- id: wait-signal
|
|
26
|
-
name: Wait for deploy signal
|
|
27
|
-
trigger:
|
|
28
|
-
type: webhook
|
|
29
|
-
port: 8787
|
|
30
|
-
path: /hooks/deploy
|
|
31
|
-
secret_env: TAGMA_WEBHOOK_SECRET
|
|
32
|
-
timeout: 30m
|
|
33
|
-
prompt: 'Run the deploy playbook for the staging environment'
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
Then fire the webhook from anywhere (CI, Slack, cron, another pipeline):
|
|
37
|
-
|
|
38
|
-
```bash
|
|
39
|
-
BODY='{"env":"staging","sha":"abc123"}'
|
|
40
|
-
SIG="sha256=$(printf %s "$BODY" | openssl dgst -sha256 -hmac "$TAGMA_WEBHOOK_SECRET" | awk '{print $2}')"
|
|
41
|
-
curl -X POST http://localhost:8787/hooks/deploy \
|
|
42
|
-
-H "content-type: application/json" \
|
|
43
|
-
-H "x-tagma-signature: $SIG" \
|
|
44
|
-
-d "$BODY"
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
Or load it programmatically:
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
import { bootstrapBuiltins, loadPlugins } from '@tagma/sdk';
|
|
51
|
-
|
|
52
|
-
bootstrapBuiltins();
|
|
53
|
-
await loadPlugins(['@tagma/trigger-webhook']);
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## Config
|
|
57
|
-
|
|
58
|
-
| Field | Type | Default | Notes |
|
|
59
|
-
| ------------ | -------- | ------------ | ------------------------------------------------------------------------------------------- |
|
|
60
|
-
| `port` | number | _(required)_ | TCP port to listen on (1-65535) |
|
|
61
|
-
| `path` | string | `/webhook` | URL path to match; must start with `/` |
|
|
62
|
-
| `secret_env` | string | _(none)_ | Env var holding the HMAC-SHA256 secret. When set, requests must include `x-tagma-signature` |
|
|
63
|
-
| `timeout` | duration | _(forever)_ | Max wait time; omit for unbounded wait |
|
|
64
|
-
|
|
65
|
-
## Behavior
|
|
66
|
-
|
|
67
|
-
- A single `Bun.serve` listener is created per unique `(port, path)` pair and shared across all tasks that watch it. Multiple tasks on the same endpoint form a FIFO waiter queue — the next POST wakes one waiter.
|
|
68
|
-
- Signature header format: `x-tagma-signature: sha256=<hex>`, HMAC-SHA256 of the raw request body using the secret. Verification is constant-time.
|
|
69
|
-
- JSON bodies (`content-type: application/json`) are parsed and handed to the task as the trigger payload; other bodies are passed through as raw strings.
|
|
70
|
-
- A POST arriving while no task is waiting is rejected with `409 no waiting task` so the caller can retry once a pipeline is up.
|
|
71
|
-
- The listener outlives the task — it's a per-process singleton, not cleaned up between pipeline runs. Restart the host to free the port.
|
|
72
|
-
|
|
73
|
-
## Security
|
|
74
|
-
|
|
75
|
-
Always set `secret_env` in production. Without it, anyone who can reach the port can fire the trigger. Bind the host to `127.0.0.1` at the OS level if you only need local callers.
|
|
76
|
-
|
|
77
|
-
## License
|
|
78
|
-
|
|
79
|
-
MIT
|
|
1
|
+
# @tagma/trigger-webhook
|
|
2
|
+
|
|
3
|
+
HTTP webhook trigger plugin for [@tagma/sdk](https://www.npmjs.com/package/@tagma/sdk).
|
|
4
|
+
|
|
5
|
+
Pauses a task until a POST request arrives on a configured local HTTP listener. Optional HMAC-SHA256 signature validation protects against unauthenticated callers.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bun add @tagma/trigger-webhook
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
pipeline:
|
|
17
|
+
name: deploy-on-demand
|
|
18
|
+
plugins:
|
|
19
|
+
- '@tagma/trigger-webhook'
|
|
20
|
+
tracks:
|
|
21
|
+
- id: deploy
|
|
22
|
+
name: Deploy
|
|
23
|
+
driver: claude-code
|
|
24
|
+
tasks:
|
|
25
|
+
- id: wait-signal
|
|
26
|
+
name: Wait for deploy signal
|
|
27
|
+
trigger:
|
|
28
|
+
type: webhook
|
|
29
|
+
port: 8787
|
|
30
|
+
path: /hooks/deploy
|
|
31
|
+
secret_env: TAGMA_WEBHOOK_SECRET
|
|
32
|
+
timeout: 30m
|
|
33
|
+
prompt: 'Run the deploy playbook for the staging environment'
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Then fire the webhook from anywhere (CI, Slack, cron, another pipeline):
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
BODY='{"env":"staging","sha":"abc123"}'
|
|
40
|
+
SIG="sha256=$(printf %s "$BODY" | openssl dgst -sha256 -hmac "$TAGMA_WEBHOOK_SECRET" | awk '{print $2}')"
|
|
41
|
+
curl -X POST http://localhost:8787/hooks/deploy \
|
|
42
|
+
-H "content-type: application/json" \
|
|
43
|
+
-H "x-tagma-signature: $SIG" \
|
|
44
|
+
-d "$BODY"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Or load it programmatically:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { bootstrapBuiltins, loadPlugins } from '@tagma/sdk';
|
|
51
|
+
|
|
52
|
+
bootstrapBuiltins();
|
|
53
|
+
await loadPlugins(['@tagma/trigger-webhook']);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Config
|
|
57
|
+
|
|
58
|
+
| Field | Type | Default | Notes |
|
|
59
|
+
| ------------ | -------- | ------------ | ------------------------------------------------------------------------------------------- |
|
|
60
|
+
| `port` | number | _(required)_ | TCP port to listen on (1-65535) |
|
|
61
|
+
| `path` | string | `/webhook` | URL path to match; must start with `/` |
|
|
62
|
+
| `secret_env` | string | _(none)_ | Env var holding the HMAC-SHA256 secret. When set, requests must include `x-tagma-signature` |
|
|
63
|
+
| `timeout` | duration | _(forever)_ | Max wait time; omit for unbounded wait |
|
|
64
|
+
|
|
65
|
+
## Behavior
|
|
66
|
+
|
|
67
|
+
- A single `Bun.serve` listener is created per unique `(port, path)` pair and shared across all tasks that watch it. Multiple tasks on the same endpoint form a FIFO waiter queue — the next POST wakes one waiter.
|
|
68
|
+
- Signature header format: `x-tagma-signature: sha256=<hex>`, HMAC-SHA256 of the raw request body using the secret. Verification is constant-time.
|
|
69
|
+
- JSON bodies (`content-type: application/json`) are parsed and handed to the task as the trigger payload; other bodies are passed through as raw strings.
|
|
70
|
+
- A POST arriving while no task is waiting is rejected with `409 no waiting task` so the caller can retry once a pipeline is up.
|
|
71
|
+
- The listener outlives the task — it's a per-process singleton, not cleaned up between pipeline runs. Restart the host to free the port.
|
|
72
|
+
|
|
73
|
+
## Security
|
|
74
|
+
|
|
75
|
+
Always set `secret_env` in production. Without it, anyone who can reach the port can fire the trigger. Bind the host to `127.0.0.1` at the OS level if you only need local callers.
|
|
76
|
+
|
|
77
|
+
## License
|
|
78
|
+
|
|
79
|
+
MIT
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EAAE,aAAa,EAAkB,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EAAE,aAAa,EAAkB,MAAM,cAAc,CAAC;AA0IlE,QAAA,MAAM,cAAc,EAAE,aAwHrB,CAAC;AAGF,eAAO,MAAM,cAAc,aAAa,CAAC;AACzC,eAAO,MAAM,UAAU,YAAY,CAAC;AACpC,eAAe,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -99,15 +99,23 @@ function ensureServer(port, path, secretEnv, hostname) {
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
// Decode JSON if possible so downstream tasks get structured data.
|
|
102
|
-
//
|
|
102
|
+
//
|
|
103
|
+
// D22: when the sender explicitly declared `application/json`, a parse
|
|
104
|
+
// failure is a contract violation — silently falling through to the
|
|
105
|
+
// raw-string payload meant downstream tasks that did `payload.field`
|
|
106
|
+
// crashed later with "Cannot read properties of undefined", far away
|
|
107
|
+
// from the actual cause. Return 400 so the sender sees the problem
|
|
108
|
+
// up front. Bodies without a JSON content-type still pass through
|
|
109
|
+
// untouched, which preserves the "any POST body" escape hatch.
|
|
103
110
|
let payload = rawBody;
|
|
104
111
|
const contentType = req.headers.get('content-type') ?? '';
|
|
105
112
|
if (contentType.includes('application/json')) {
|
|
106
113
|
try {
|
|
107
114
|
payload = JSON.parse(rawBody);
|
|
108
115
|
}
|
|
109
|
-
catch {
|
|
110
|
-
|
|
116
|
+
catch (err) {
|
|
117
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
118
|
+
return new Response(`invalid JSON body: ${msg}`, { status: 400 });
|
|
111
119
|
}
|
|
112
120
|
}
|
|
113
121
|
const waiter = state.waiters.shift();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,iCAAiC;AACjC,EAAE;AACF,0EAA0E;AAC1E,0EAA0E;AAC1E,4EAA4E;AAC5E,0CAA0C;AAC1C,EAAE;AACF,4EAA4E;AAC5E,4EAA4E;AAC5E,4EAA4E;AAC5E,gCAAgC;AAChC,EAAE;AACF,0BAA0B;AAC1B,wCAAwC;AACxC,YAAY;AACZ,eAAe;AACf,uBAAuB;AACvB,qBAAqB;AACrB,4BAA4B;AAC5B,yBAAyB;AACzB,kCAAkC;AAClC,+CAA+C;AAC/C,2BAA2B;AAG3B,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAW1D,sEAAsE;AACtE,uEAAuE;AACvE,uCAAuC;AACvC,MAAM,OAAO,GAAG,IAAI,GAAG,EAA8B,CAAC;AAEtD,SAAS,SAAS,CAAC,IAAY,EAAE,IAAY,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AAI,EAAE,IAAI,EAAE,CAAC,CAAC;YAE9D,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;gBAClB,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;oBACtB,IAAI,OAAO;wBAAE,OAAO;oBACpB,OAAO,GAAG,IAAI,CAAC;oBACf,OAAO,EAAE,CAAC;oBACV,aAAa,CACX,IAAI,KAAK,CACP,wCAAwC,IAAI,GAAG,IAAI,WAAW,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CACvF,CACF,CAAC;gBACJ,CAAC,EAAE,SAAS,CAAC,CAAC;YAChB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAC;AAEF,0CAA0C;AAC1C,MAAM,CAAC,MAAM,cAAc,GAAG,UAAU,CAAC;AACzC,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,CAAC;AACpC,eAAe,cAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tagma/trigger-webhook",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "HTTP webhook trigger plugin for tagma-sdk pipelines",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"prepublishOnly": "bun run build"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"@tagma/types": "0.2.
|
|
51
|
+
"@tagma/types": "0.2.8"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"bun-types": "^1.3.11",
|
package/src/index.ts
CHANGED
|
@@ -1,280 +1,288 @@
|
|
|
1
|
-
// ═══ Webhook Trigger Plugin ═══
|
|
2
|
-
//
|
|
3
|
-
// Spins up a Bun.serve HTTP listener and resolves the trigger when a POST
|
|
4
|
-
// arrives on the configured path. Optional HMAC-SHA256 validation via the
|
|
5
|
-
// `x-tagma-signature: sha256=<hex>` header protects against unauthenticated
|
|
6
|
-
// callers when the secret env var is set.
|
|
7
|
-
//
|
|
8
|
-
// Multiple tasks sharing the same `(port, path)` pair hit the same listener
|
|
9
|
-
// instance and are enqueued as waiters — the next inbound request wakes one
|
|
10
|
-
// waiter (FIFO). This keeps the plugin lightweight even when a pipeline has
|
|
11
|
-
// many webhook-triggered tasks.
|
|
12
|
-
//
|
|
13
|
-
// Usage in pipeline.yaml:
|
|
14
|
-
// plugins: ["@tagma/trigger-webhook"]
|
|
15
|
-
// tracks:
|
|
16
|
-
// - tasks:
|
|
17
|
-
// - id: deploy
|
|
18
|
-
// trigger:
|
|
19
|
-
// type: webhook
|
|
20
|
-
// port: 8787
|
|
21
|
-
// path: /hooks/deploy
|
|
22
|
-
// secret_env: TAGMA_WEBHOOK_SECRET
|
|
23
|
-
// timeout: 10m
|
|
24
|
-
|
|
25
|
-
import type { TriggerPlugin, TriggerContext } from '@tagma/types';
|
|
26
|
-
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
27
|
-
|
|
28
|
-
type Resolver = (payload: unknown) => void;
|
|
29
|
-
|
|
30
|
-
interface WebhookServerState {
|
|
31
|
-
readonly port: number;
|
|
32
|
-
readonly path: string;
|
|
33
|
-
readonly secretEnv: string | undefined;
|
|
34
|
-
readonly waiters: Resolver[];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Module-level so concurrent tasks on the same (port, path) share one
|
|
38
|
-
// listener. Bun.serve refuses to bind a port twice, so this sharing is
|
|
39
|
-
// required — not just an optimization.
|
|
40
|
-
const servers = new Map<string, WebhookServerState>();
|
|
41
|
-
|
|
42
|
-
function serverKey(port: number, path: string, hostname: string): string {
|
|
43
|
-
return `${hostname}::${port}::${path}`;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const DEFAULT_WEBHOOK_HOST = '127.0.0.1';
|
|
47
|
-
|
|
48
|
-
function parseDurationSafe(raw: unknown, fallback: number): number {
|
|
49
|
-
if (raw == null) return fallback;
|
|
50
|
-
const str = String(raw).trim();
|
|
51
|
-
const m = str.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
52
|
-
if (!m) return fallback;
|
|
53
|
-
const n = Number(m[1]);
|
|
54
|
-
switch (m[2]) {
|
|
55
|
-
case 'ms':
|
|
56
|
-
return n;
|
|
57
|
-
case 'm':
|
|
58
|
-
return n * 60_000;
|
|
59
|
-
case 'h':
|
|
60
|
-
return n * 3_600_000;
|
|
61
|
-
case 's':
|
|
62
|
-
default:
|
|
63
|
-
return n * 1000;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function verifySignature(rawBody: string, header: string, secret: string): boolean {
|
|
68
|
-
const expected = 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex');
|
|
69
|
-
const a = Buffer.from(header);
|
|
70
|
-
const b = Buffer.from(expected);
|
|
71
|
-
if (a.length !== b.length) return false;
|
|
72
|
-
return timingSafeEqual(a, b);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function ensureServer(
|
|
76
|
-
port: number,
|
|
77
|
-
path: string,
|
|
78
|
-
secretEnv: string | undefined,
|
|
79
|
-
hostname: string,
|
|
80
|
-
): WebhookServerState {
|
|
81
|
-
const key = serverKey(port, path, hostname);
|
|
82
|
-
const existing = servers.get(key);
|
|
83
|
-
if (existing) return existing;
|
|
84
|
-
|
|
85
|
-
const state: WebhookServerState = {
|
|
86
|
-
port,
|
|
87
|
-
path,
|
|
88
|
-
secretEnv,
|
|
89
|
-
waiters: [],
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
Bun.serve({
|
|
93
|
-
port,
|
|
94
|
-
// Default to loopback so the webhook endpoint is not reachable from the
|
|
95
|
-
// LAN unless the user explicitly sets `host` in their YAML (e.g. for
|
|
96
|
-
// container/WSL scenarios). Without this, Bun.serve listens on all
|
|
97
|
-
// interfaces and a secret-less webhook would accept traffic from any
|
|
98
|
-
// network-adjacent caller.
|
|
99
|
-
hostname,
|
|
100
|
-
async fetch(req) {
|
|
101
|
-
const url = new URL(req.url);
|
|
102
|
-
if (url.pathname !== path) {
|
|
103
|
-
return new Response('not found', { status: 404 });
|
|
104
|
-
}
|
|
105
|
-
if (req.method !== 'POST') {
|
|
106
|
-
return new Response('method not allowed', { status: 405 });
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const rawBody = await req.text();
|
|
110
|
-
|
|
111
|
-
// HMAC gate — bypassed when no secret env is configured, which is
|
|
112
|
-
// acceptable for loopback dev but should always be set in production.
|
|
113
|
-
const secret = state.secretEnv ? process.env[state.secretEnv] : undefined;
|
|
114
|
-
if (state.secretEnv) {
|
|
115
|
-
if (!secret) {
|
|
116
|
-
return new Response(`env var ${state.secretEnv} not set`, { status: 500 });
|
|
117
|
-
}
|
|
118
|
-
const sigHeader = req.headers.get('x-tagma-signature') ?? '';
|
|
119
|
-
if (!verifySignature(rawBody, sigHeader, secret)) {
|
|
120
|
-
return new Response('invalid signature', { status: 401 });
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Decode JSON if possible so downstream tasks get structured data.
|
|
125
|
-
//
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
settled
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
1
|
+
// ═══ Webhook Trigger Plugin ═══
|
|
2
|
+
//
|
|
3
|
+
// Spins up a Bun.serve HTTP listener and resolves the trigger when a POST
|
|
4
|
+
// arrives on the configured path. Optional HMAC-SHA256 validation via the
|
|
5
|
+
// `x-tagma-signature: sha256=<hex>` header protects against unauthenticated
|
|
6
|
+
// callers when the secret env var is set.
|
|
7
|
+
//
|
|
8
|
+
// Multiple tasks sharing the same `(port, path)` pair hit the same listener
|
|
9
|
+
// instance and are enqueued as waiters — the next inbound request wakes one
|
|
10
|
+
// waiter (FIFO). This keeps the plugin lightweight even when a pipeline has
|
|
11
|
+
// many webhook-triggered tasks.
|
|
12
|
+
//
|
|
13
|
+
// Usage in pipeline.yaml:
|
|
14
|
+
// plugins: ["@tagma/trigger-webhook"]
|
|
15
|
+
// tracks:
|
|
16
|
+
// - tasks:
|
|
17
|
+
// - id: deploy
|
|
18
|
+
// trigger:
|
|
19
|
+
// type: webhook
|
|
20
|
+
// port: 8787
|
|
21
|
+
// path: /hooks/deploy
|
|
22
|
+
// secret_env: TAGMA_WEBHOOK_SECRET
|
|
23
|
+
// timeout: 10m
|
|
24
|
+
|
|
25
|
+
import type { TriggerPlugin, TriggerContext } from '@tagma/types';
|
|
26
|
+
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
27
|
+
|
|
28
|
+
type Resolver = (payload: unknown) => void;
|
|
29
|
+
|
|
30
|
+
interface WebhookServerState {
|
|
31
|
+
readonly port: number;
|
|
32
|
+
readonly path: string;
|
|
33
|
+
readonly secretEnv: string | undefined;
|
|
34
|
+
readonly waiters: Resolver[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Module-level so concurrent tasks on the same (port, path) share one
|
|
38
|
+
// listener. Bun.serve refuses to bind a port twice, so this sharing is
|
|
39
|
+
// required — not just an optimization.
|
|
40
|
+
const servers = new Map<string, WebhookServerState>();
|
|
41
|
+
|
|
42
|
+
function serverKey(port: number, path: string, hostname: string): string {
|
|
43
|
+
return `${hostname}::${port}::${path}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const DEFAULT_WEBHOOK_HOST = '127.0.0.1';
|
|
47
|
+
|
|
48
|
+
function parseDurationSafe(raw: unknown, fallback: number): number {
|
|
49
|
+
if (raw == null) return fallback;
|
|
50
|
+
const str = String(raw).trim();
|
|
51
|
+
const m = str.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
52
|
+
if (!m) return fallback;
|
|
53
|
+
const n = Number(m[1]);
|
|
54
|
+
switch (m[2]) {
|
|
55
|
+
case 'ms':
|
|
56
|
+
return n;
|
|
57
|
+
case 'm':
|
|
58
|
+
return n * 60_000;
|
|
59
|
+
case 'h':
|
|
60
|
+
return n * 3_600_000;
|
|
61
|
+
case 's':
|
|
62
|
+
default:
|
|
63
|
+
return n * 1000;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function verifySignature(rawBody: string, header: string, secret: string): boolean {
|
|
68
|
+
const expected = 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex');
|
|
69
|
+
const a = Buffer.from(header);
|
|
70
|
+
const b = Buffer.from(expected);
|
|
71
|
+
if (a.length !== b.length) return false;
|
|
72
|
+
return timingSafeEqual(a, b);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function ensureServer(
|
|
76
|
+
port: number,
|
|
77
|
+
path: string,
|
|
78
|
+
secretEnv: string | undefined,
|
|
79
|
+
hostname: string,
|
|
80
|
+
): WebhookServerState {
|
|
81
|
+
const key = serverKey(port, path, hostname);
|
|
82
|
+
const existing = servers.get(key);
|
|
83
|
+
if (existing) return existing;
|
|
84
|
+
|
|
85
|
+
const state: WebhookServerState = {
|
|
86
|
+
port,
|
|
87
|
+
path,
|
|
88
|
+
secretEnv,
|
|
89
|
+
waiters: [],
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
Bun.serve({
|
|
93
|
+
port,
|
|
94
|
+
// Default to loopback so the webhook endpoint is not reachable from the
|
|
95
|
+
// LAN unless the user explicitly sets `host` in their YAML (e.g. for
|
|
96
|
+
// container/WSL scenarios). Without this, Bun.serve listens on all
|
|
97
|
+
// interfaces and a secret-less webhook would accept traffic from any
|
|
98
|
+
// network-adjacent caller.
|
|
99
|
+
hostname,
|
|
100
|
+
async fetch(req) {
|
|
101
|
+
const url = new URL(req.url);
|
|
102
|
+
if (url.pathname !== path) {
|
|
103
|
+
return new Response('not found', { status: 404 });
|
|
104
|
+
}
|
|
105
|
+
if (req.method !== 'POST') {
|
|
106
|
+
return new Response('method not allowed', { status: 405 });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const rawBody = await req.text();
|
|
110
|
+
|
|
111
|
+
// HMAC gate — bypassed when no secret env is configured, which is
|
|
112
|
+
// acceptable for loopback dev but should always be set in production.
|
|
113
|
+
const secret = state.secretEnv ? process.env[state.secretEnv] : undefined;
|
|
114
|
+
if (state.secretEnv) {
|
|
115
|
+
if (!secret) {
|
|
116
|
+
return new Response(`env var ${state.secretEnv} not set`, { status: 500 });
|
|
117
|
+
}
|
|
118
|
+
const sigHeader = req.headers.get('x-tagma-signature') ?? '';
|
|
119
|
+
if (!verifySignature(rawBody, sigHeader, secret)) {
|
|
120
|
+
return new Response('invalid signature', { status: 401 });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Decode JSON if possible so downstream tasks get structured data.
|
|
125
|
+
//
|
|
126
|
+
// D22: when the sender explicitly declared `application/json`, a parse
|
|
127
|
+
// failure is a contract violation — silently falling through to the
|
|
128
|
+
// raw-string payload meant downstream tasks that did `payload.field`
|
|
129
|
+
// crashed later with "Cannot read properties of undefined", far away
|
|
130
|
+
// from the actual cause. Return 400 so the sender sees the problem
|
|
131
|
+
// up front. Bodies without a JSON content-type still pass through
|
|
132
|
+
// untouched, which preserves the "any POST body" escape hatch.
|
|
133
|
+
let payload: unknown = rawBody;
|
|
134
|
+
const contentType = req.headers.get('content-type') ?? '';
|
|
135
|
+
if (contentType.includes('application/json')) {
|
|
136
|
+
try {
|
|
137
|
+
payload = JSON.parse(rawBody);
|
|
138
|
+
} catch (err) {
|
|
139
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
140
|
+
return new Response(`invalid JSON body: ${msg}`, { status: 400 });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const waiter = state.waiters.shift();
|
|
145
|
+
if (waiter) {
|
|
146
|
+
waiter(payload);
|
|
147
|
+
return new Response('ok', { status: 202 });
|
|
148
|
+
}
|
|
149
|
+
// No task is currently waiting on this endpoint — reject loudly so
|
|
150
|
+
// callers know the webhook wasn't actually consumed by a pipeline.
|
|
151
|
+
return new Response('no waiting task', { status: 409 });
|
|
152
|
+
},
|
|
153
|
+
error(err) {
|
|
154
|
+
console.error(`[webhook] server error on :${port}${path}:`, err);
|
|
155
|
+
return new Response('internal error', { status: 500 });
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
servers.set(key, state);
|
|
160
|
+
return state;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const WebhookTrigger: TriggerPlugin = {
|
|
164
|
+
name: 'webhook',
|
|
165
|
+
schema: {
|
|
166
|
+
description: 'Wait for an HTTP POST to arrive on a local listener before the task runs.',
|
|
167
|
+
fields: {
|
|
168
|
+
port: {
|
|
169
|
+
type: 'number',
|
|
170
|
+
required: true,
|
|
171
|
+
default: 8787,
|
|
172
|
+
min: 1,
|
|
173
|
+
max: 65535,
|
|
174
|
+
description: 'TCP port to listen on.',
|
|
175
|
+
placeholder: '8787',
|
|
176
|
+
},
|
|
177
|
+
path: {
|
|
178
|
+
type: 'string',
|
|
179
|
+
required: true,
|
|
180
|
+
default: '/webhook',
|
|
181
|
+
description: 'URL path to match (e.g. /hooks/deploy).',
|
|
182
|
+
placeholder: '/webhook',
|
|
183
|
+
},
|
|
184
|
+
secret_env: {
|
|
185
|
+
type: 'string',
|
|
186
|
+
description:
|
|
187
|
+
'Env var containing the HMAC-SHA256 secret. When set, POSTs must include an x-tagma-signature: sha256=<hex> header computed over the raw body.',
|
|
188
|
+
placeholder: 'TAGMA_WEBHOOK_SECRET',
|
|
189
|
+
},
|
|
190
|
+
host: {
|
|
191
|
+
type: 'string',
|
|
192
|
+
default: DEFAULT_WEBHOOK_HOST,
|
|
193
|
+
description:
|
|
194
|
+
'Interface to bind on. Defaults to 127.0.0.1 so the webhook is only reachable from the local machine. Set to 0.0.0.0 to accept LAN traffic (only do this when secret_env is also set).',
|
|
195
|
+
placeholder: DEFAULT_WEBHOOK_HOST,
|
|
196
|
+
},
|
|
197
|
+
timeout: {
|
|
198
|
+
type: 'duration',
|
|
199
|
+
description: 'Maximum wait time (e.g. 10m). Omit or 0 to wait indefinitely.',
|
|
200
|
+
placeholder: '10m',
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
|
|
205
|
+
watch(config: Record<string, unknown>, ctx: TriggerContext): Promise<unknown> {
|
|
206
|
+
const rawPort = config.port;
|
|
207
|
+
const port = typeof rawPort === 'number' ? rawPort : Number(rawPort);
|
|
208
|
+
if (!Number.isFinite(port) || port < 1 || port > 65535) {
|
|
209
|
+
throw new Error(`webhook trigger: "port" must be 1-65535, got ${String(rawPort)}`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const path = (config.path as string | undefined) ?? '/webhook';
|
|
213
|
+
if (!path.startsWith('/')) {
|
|
214
|
+
throw new Error(`webhook trigger: "path" must start with "/", got ${path}`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const secretEnv = config.secret_env as string | undefined;
|
|
218
|
+
const rawHost = config.host;
|
|
219
|
+
const hostname =
|
|
220
|
+
typeof rawHost === 'string' && rawHost.trim().length > 0
|
|
221
|
+
? rawHost.trim()
|
|
222
|
+
: DEFAULT_WEBHOOK_HOST;
|
|
223
|
+
const timeoutMs = config.timeout != null ? parseDurationSafe(config.timeout, 0) : 0;
|
|
224
|
+
|
|
225
|
+
if (hostname !== DEFAULT_WEBHOOK_HOST && hostname !== 'localhost' && !secretEnv) {
|
|
226
|
+
// Loud warning — the user has opted into a non-loopback bind without
|
|
227
|
+
// HMAC. Refuse by default rather than silently expose the endpoint to
|
|
228
|
+
// the network.
|
|
229
|
+
throw new Error(
|
|
230
|
+
`webhook trigger: binding to "${hostname}" without secret_env is refused — set secret_env to enable HMAC authentication before exposing to non-loopback interfaces`,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const state = ensureServer(port, path, secretEnv, hostname);
|
|
235
|
+
|
|
236
|
+
return new Promise<unknown>((resolvePromise, rejectPromise) => {
|
|
237
|
+
if (ctx.signal.aborted) {
|
|
238
|
+
rejectPromise(new Error('Pipeline aborted'));
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
let settled = false;
|
|
243
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
244
|
+
|
|
245
|
+
const onFire: Resolver = (payload) => {
|
|
246
|
+
if (settled) return;
|
|
247
|
+
settled = true;
|
|
248
|
+
cleanup();
|
|
249
|
+
resolvePromise(payload);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const onAbort = (): void => {
|
|
253
|
+
if (settled) return;
|
|
254
|
+
settled = true;
|
|
255
|
+
cleanup();
|
|
256
|
+
rejectPromise(new Error('Pipeline aborted'));
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const cleanup = (): void => {
|
|
260
|
+
if (timer) clearTimeout(timer);
|
|
261
|
+
ctx.signal.removeEventListener('abort', onAbort);
|
|
262
|
+
const idx = state.waiters.indexOf(onFire);
|
|
263
|
+
if (idx !== -1) state.waiters.splice(idx, 1);
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
state.waiters.push(onFire);
|
|
267
|
+
ctx.signal.addEventListener('abort', onAbort, { once: true });
|
|
268
|
+
|
|
269
|
+
if (timeoutMs > 0) {
|
|
270
|
+
timer = setTimeout(() => {
|
|
271
|
+
if (settled) return;
|
|
272
|
+
settled = true;
|
|
273
|
+
cleanup();
|
|
274
|
+
rejectPromise(
|
|
275
|
+
new Error(
|
|
276
|
+
`webhook trigger timeout: no POST on :${port}${path} within ${String(config.timeout)}`,
|
|
277
|
+
),
|
|
278
|
+
);
|
|
279
|
+
}, timeoutMs);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
// ═══ Plugin self-description exports ═══
|
|
286
|
+
export const pluginCategory = 'triggers';
|
|
287
|
+
export const pluginType = 'webhook';
|
|
288
|
+
export default WebhookTrigger;
|