@condition-sh/runner 2026.9.1
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 +5 -0
- package/index.js +210 -0
- package/package.json +25 -0
- package/types/client.d.ts +17 -0
- package/types/index.d.ts +5 -0
- package/types/server.d.ts +7 -0
- package/types/signature.d.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rad Soft, Inc.
|
|
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
package/index.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// packages/runner/src/server.ts
|
|
2
|
+
import { describeProject, inboxAddress } from "@condition-sh/core";
|
|
3
|
+
|
|
4
|
+
// packages/runner/src/signature.ts
|
|
5
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
6
|
+
var TIMESTAMP_HEADER = "x-condition-timestamp";
|
|
7
|
+
var SIGNATURE_HEADER = "x-condition-signature";
|
|
8
|
+
var windowSeconds = 300;
|
|
9
|
+
function payload(timestamp, method, route, body) {
|
|
10
|
+
return `${timestamp}
|
|
11
|
+
${method.toUpperCase()}
|
|
12
|
+
${route}
|
|
13
|
+
${body}`;
|
|
14
|
+
}
|
|
15
|
+
function sign(secret, method, route, body, now = Date.now()) {
|
|
16
|
+
const timestamp = String(Math.floor(now / 1000));
|
|
17
|
+
const signature = createHmac("sha256", secret).update(payload(timestamp, method, route, body)).digest("hex");
|
|
18
|
+
return { [TIMESTAMP_HEADER]: timestamp, [SIGNATURE_HEADER]: signature };
|
|
19
|
+
}
|
|
20
|
+
function verifySignature(secret, headers, method, route, body, now = Date.now()) {
|
|
21
|
+
const timestamp = headers.get(TIMESTAMP_HEADER) ?? "";
|
|
22
|
+
const signature = headers.get(SIGNATURE_HEADER) ?? "";
|
|
23
|
+
if (!/^\d{1,12}$/.test(timestamp) || !/^[0-9a-f]{64}$/.test(signature))
|
|
24
|
+
return false;
|
|
25
|
+
if (Math.abs(now / 1000 - Number(timestamp)) > windowSeconds)
|
|
26
|
+
return false;
|
|
27
|
+
const expected = createHmac("sha256", secret).update(payload(timestamp, method, route, body)).digest();
|
|
28
|
+
return timingSafeEqual(expected, Buffer.from(signature, "hex"));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// packages/runner/src/server.ts
|
|
32
|
+
var runPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
33
|
+
var routePattern = /\/(scenarios|present|runs|runs\/([0-9a-f-]{36})\/(verify|login|cleanup))$/;
|
|
34
|
+
|
|
35
|
+
class RunnerError extends Error {
|
|
36
|
+
status;
|
|
37
|
+
constructor(status, message) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.status = status;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
var json = (value, status = 200) => Response.json(value, { status, headers: { "cache-control": "no-store" } });
|
|
43
|
+
function lookup(project, body) {
|
|
44
|
+
const scenario = typeof body.scenario === "string" ? project.scenarios[body.scenario] : undefined;
|
|
45
|
+
if (!scenario)
|
|
46
|
+
throw new RunnerError(404, `unknown scenario ${String(body.scenario)}`);
|
|
47
|
+
return scenario;
|
|
48
|
+
}
|
|
49
|
+
async function perform(project, route, body) {
|
|
50
|
+
const [, name, runId, step] = route;
|
|
51
|
+
if (name === "present") {
|
|
52
|
+
const scenario2 = lookup(project, body);
|
|
53
|
+
const hook = body.kind === "input" ? scenario2.presentInput : scenario2.present;
|
|
54
|
+
return { presented: hook ? await hook(body.value) ?? null : null };
|
|
55
|
+
}
|
|
56
|
+
const scenario = lookup(project, body);
|
|
57
|
+
const id = runId ?? body.runId;
|
|
58
|
+
if (typeof id !== "string" || !runPattern.test(id))
|
|
59
|
+
throw new RunnerError(400, "invalid run ID");
|
|
60
|
+
const inboxDomain = typeof body.inboxDomain === "string" ? body.inboxDomain : undefined;
|
|
61
|
+
const context = {
|
|
62
|
+
runId: id,
|
|
63
|
+
input: body.input ?? {},
|
|
64
|
+
inbox: inboxDomain ? { address: (actor) => inboxAddress(actor, id, inboxDomain) } : undefined
|
|
65
|
+
};
|
|
66
|
+
switch (step) {
|
|
67
|
+
case undefined:
|
|
68
|
+
return { output: await scenario.prepare(context) ?? null };
|
|
69
|
+
case "verify":
|
|
70
|
+
await scenario.verify({ ...context, output: body.output });
|
|
71
|
+
return { verified: true };
|
|
72
|
+
case "cleanup":
|
|
73
|
+
await scenario.cleanup({ ...context, output: body.output ?? null });
|
|
74
|
+
return { cleaned: true };
|
|
75
|
+
default: {
|
|
76
|
+
if (!scenario.login)
|
|
77
|
+
throw new RunnerError(404, `scenario ${String(body.scenario)} does not provide login`);
|
|
78
|
+
if (typeof body.actor !== "string")
|
|
79
|
+
throw new RunnerError(400, "actor is required");
|
|
80
|
+
return { login: await scenario.login({ ...context, output: body.output, actor: body.actor }) };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function createRunner(options) {
|
|
85
|
+
if (!options.secret || options.secret.length < 32)
|
|
86
|
+
throw new Error("runner secret must be at least 32 characters");
|
|
87
|
+
return async (request) => {
|
|
88
|
+
if (!options.enabled)
|
|
89
|
+
return json({ error: "not found" }, 404);
|
|
90
|
+
const route = new URL(request.url).pathname.match(routePattern);
|
|
91
|
+
const expected = route?.[1] === "scenarios" ? "GET" : "POST";
|
|
92
|
+
if (!route || request.method !== expected)
|
|
93
|
+
return json({ error: "not found" }, 404);
|
|
94
|
+
const raw = await request.text();
|
|
95
|
+
if (!verifySignature(options.secret, request.headers, request.method, route[0], raw))
|
|
96
|
+
return json({ error: "invalid signature" }, 401);
|
|
97
|
+
if (route[1] === "scenarios") {
|
|
98
|
+
const { project, scenarios } = describeProject(options.project);
|
|
99
|
+
return json({ project, scenarios });
|
|
100
|
+
}
|
|
101
|
+
let body;
|
|
102
|
+
try {
|
|
103
|
+
body = JSON.parse(raw);
|
|
104
|
+
} catch {
|
|
105
|
+
return json({ error: "invalid JSON" }, 400);
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
return json(await perform(options.project, route, body ?? {}));
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (error instanceof RunnerError)
|
|
111
|
+
return json({ error: error.message }, error.status);
|
|
112
|
+
return json({ error: error instanceof Error ? error.message : String(error) }, 422);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// packages/runner/src/client.ts
|
|
117
|
+
import { defineProject } from "@condition-sh/core";
|
|
118
|
+
function runnerUrl(value) {
|
|
119
|
+
const url = new URL(value);
|
|
120
|
+
const local = url.hostname === "localhost" || url.hostname === "127.0.0.1";
|
|
121
|
+
if (url.protocol !== "https:" && !(local && url.protocol === "http:"))
|
|
122
|
+
throw new Error("runner URL must use HTTPS");
|
|
123
|
+
if (url.username || url.password || url.search || url.hash)
|
|
124
|
+
throw new Error("runner URL must be a plain origin and path");
|
|
125
|
+
return url;
|
|
126
|
+
}
|
|
127
|
+
var notSent = /ENOTFOUND|EAI_AGAIN|ECONNREFUSED|ConnectionRefused/;
|
|
128
|
+
function neverSent(error) {
|
|
129
|
+
const { code, message } = error;
|
|
130
|
+
return notSent.test(`${code ?? ""} ${message ?? ""}`);
|
|
131
|
+
}
|
|
132
|
+
async function send(target, init, attempts = 3) {
|
|
133
|
+
for (let attempt = 1;; attempt++) {
|
|
134
|
+
try {
|
|
135
|
+
return await fetch(target, { ...init, signal: AbortSignal.timeout(120000) });
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (attempt >= attempts || !neverSent(error))
|
|
138
|
+
throw error;
|
|
139
|
+
await new Promise((resolve) => setTimeout(resolve, 250 * attempt));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function call({ url, secret }, method, route, body) {
|
|
144
|
+
const raw = body === undefined ? "" : JSON.stringify(body);
|
|
145
|
+
const base = runnerUrl(url);
|
|
146
|
+
const target = new URL(`${base.pathname.replace(/\/$/, "")}${route}`, base);
|
|
147
|
+
let response;
|
|
148
|
+
try {
|
|
149
|
+
response = await send(target, {
|
|
150
|
+
method,
|
|
151
|
+
redirect: "error",
|
|
152
|
+
headers: { "content-type": "application/json", ...sign(secret, method, route, raw) },
|
|
153
|
+
...method === "GET" ? {} : { body: raw }
|
|
154
|
+
});
|
|
155
|
+
} catch (error) {
|
|
156
|
+
throw new Error(`runner unreachable: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
157
|
+
}
|
|
158
|
+
const result = await response.json().catch(() => ({}));
|
|
159
|
+
if (!response.ok)
|
|
160
|
+
throw new Error(`runner ${route.split("/").pop()}: ${response.status} ${result.error ?? "request failed"}`);
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
async function fetchScenarios(connection) {
|
|
164
|
+
const result = await call(connection, "GET", "/scenarios");
|
|
165
|
+
if (!Array.isArray(result.scenarios))
|
|
166
|
+
throw new Error("runner returned no scenario list");
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
function remoteScenario(connection, { name, description, input, actors }, inboxDomain) {
|
|
170
|
+
const present = (kind) => async (value) => (await call(connection, "POST", "/present", { scenario: name, kind, value })).presented;
|
|
171
|
+
const mail = (inbox) => inbox && inboxDomain ? { inboxDomain } : {};
|
|
172
|
+
return {
|
|
173
|
+
description,
|
|
174
|
+
...input ? { input } : {},
|
|
175
|
+
...actors ? { actors } : {},
|
|
176
|
+
async prepare({ runId, input: input2, inbox }) {
|
|
177
|
+
return (await call(connection, "POST", "/runs", { runId, scenario: name, input: input2, ...mail(inbox) })).output;
|
|
178
|
+
},
|
|
179
|
+
async verify({ runId, input: input2, output, inbox }) {
|
|
180
|
+
await call(connection, "POST", `/runs/${runId}/verify`, { scenario: name, input: input2, output, ...mail(inbox) });
|
|
181
|
+
},
|
|
182
|
+
async cleanup({ runId, input: input2, output, inbox }) {
|
|
183
|
+
await call(connection, "POST", `/runs/${runId}/cleanup`, { scenario: name, input: input2, output, ...mail(inbox) });
|
|
184
|
+
},
|
|
185
|
+
async login({ runId, input: input2, output, actor, inbox }) {
|
|
186
|
+
return (await call(connection, "POST", `/runs/${runId}/login`, { scenario: name, input: input2, output, actor, ...mail(inbox) })).login;
|
|
187
|
+
},
|
|
188
|
+
present: present("output"),
|
|
189
|
+
presentInput: present("input")
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function remoteProject(options) {
|
|
193
|
+
runnerUrl(options.url);
|
|
194
|
+
return defineProject({
|
|
195
|
+
name: options.name,
|
|
196
|
+
inbox: options.inbox,
|
|
197
|
+
scenarios: Object.fromEntries(options.scenarios.map((scenario) => [
|
|
198
|
+
scenario.name,
|
|
199
|
+
remoteScenario(options, scenario, options.inbox?.domain)
|
|
200
|
+
]))
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
export {
|
|
204
|
+
verifySignature,
|
|
205
|
+
sign,
|
|
206
|
+
runnerUrl,
|
|
207
|
+
remoteProject,
|
|
208
|
+
fetchScenarios,
|
|
209
|
+
createRunner
|
|
210
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@condition-sh/runner",
|
|
3
|
+
"version": "2026.9.1",
|
|
4
|
+
"description": "Mount a signed Condition runner inside your app.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Rad-Soft/condition.git",
|
|
10
|
+
"directory": "packages/runner"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://condition.sh",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./types/index.d.ts",
|
|
16
|
+
"default": "./index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@condition-sh/core": "^2026.9.1"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Project, type RemoteInbox, type ScenarioSummary } from "@condition-sh/core";
|
|
2
|
+
export type RemoteScenario = ScenarioSummary;
|
|
3
|
+
type Connection = {
|
|
4
|
+
url: string;
|
|
5
|
+
secret: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function runnerUrl(value: string): URL;
|
|
8
|
+
export declare function fetchScenarios(connection: Connection): Promise<{
|
|
9
|
+
project: string;
|
|
10
|
+
scenarios: RemoteScenario[];
|
|
11
|
+
}>;
|
|
12
|
+
export declare function remoteProject(options: Connection & {
|
|
13
|
+
name: string;
|
|
14
|
+
scenarios: RemoteScenario[];
|
|
15
|
+
inbox?: RemoteInbox;
|
|
16
|
+
}): Project;
|
|
17
|
+
export {};
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createRunner } from "./server.js";
|
|
2
|
+
export type { RunnerOptions } from "./server.js";
|
|
3
|
+
export { fetchScenarios, remoteProject, runnerUrl } from "./client.js";
|
|
4
|
+
export type { RemoteScenario } from "./client.js";
|
|
5
|
+
export { sign, verifySignature } from "./signature.js";
|