@acid-cat/cli 0.18.0 → 0.20.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 +35 -5
- package/conformance/fixture.acidcat.json +96 -0
- package/dist/conform-LH9KgQ9-.js +5045 -0
- package/dist/index.d.ts +164 -4
- package/dist/index.js +96 -4
- package/dist/main.d.ts +1 -2
- package/dist/main.js +1090 -7
- package/package.json +4 -3
- package/dist/catalog.d.ts +0 -12
- package/dist/catalog.js +0 -149
- package/dist/conform/check.d.ts +0 -38
- package/dist/conform/check.js +0 -144
- package/dist/conform/cli.d.ts +0 -4
- package/dist/conform/cli.js +0 -721
- package/dist/conform/http.d.ts +0 -5
- package/dist/conform/http.js +0 -297
- package/dist/conform/index.d.ts +0 -63
- package/dist/conform/index.js +0 -58
- package/dist/conform/mcp-stdio.d.ts +0 -6
- package/dist/conform/mcp-stdio.js +0 -291
- package/dist/conform/mcp.d.ts +0 -46
- package/dist/conform/mcp.js +0 -752
- package/dist/conform/poll.d.ts +0 -13
- package/dist/conform/poll.js +0 -295
- package/dist/conform/properties.d.ts +0 -12
- package/dist/conform/properties.js +0 -155
- package/dist/conform/serve.d.ts +0 -12
- package/dist/conform/serve.js +0 -93
- package/dist/conform/server-card.d.ts +0 -5
- package/dist/conform/server-card.js +0 -142
- package/dist/conform/sse.d.ts +0 -9
- package/dist/conform/sse.js +0 -53
- package/dist/conform/webhook.d.ts +0 -30
- package/dist/conform/webhook.js +0 -481
- package/dist/conform/webmcp.d.ts +0 -52
- package/dist/conform/webmcp.js +0 -232
- package/dist/json/catalog.schema.json.js +0 -170
- package/dist/json/fixture.acidcat.json.js +0 -396
- package/dist/json/package.json.js +0 -3
- package/dist/json/server-card.schema.json.js +0 -281
- package/dist/json.d.ts +0 -11
- package/dist/json.js +0 -13
package/dist/conform/webhook.js
DELETED
|
@@ -1,481 +0,0 @@
|
|
|
1
|
-
import { isObject, memberOf, parseJson } from "../json.js";
|
|
2
|
-
import { expect, expectEqual, expectOccurrence, until } from "./check.js";
|
|
3
|
-
import { count, eventRequests, httpChannel } from "./mcp.js";
|
|
4
|
-
import { MCP_WEBHOOK_DEFAULTS, isOccurrence, webhookSignature, webhookSubscriptionId } from "@acid-cat/spec-ts";
|
|
5
|
-
import { randomBytes, randomUUID } from "node:crypto";
|
|
6
|
-
import { createServer } from "node:https";
|
|
7
|
-
// src/conform/webhook.ts
|
|
8
|
-
const RULE = "spec/surfaces/mcp.md#webhook";
|
|
9
|
-
const REFRESH_BEFORE_PRECISION_MS = 1;
|
|
10
|
-
const isVerification = (request) => memberOf(request.body, "type") === "verification";
|
|
11
|
-
async function startReceiver(settings) {
|
|
12
|
-
const received = /* @__PURE__ */ new Map();
|
|
13
|
-
const answerers = /* @__PURE__ */ new Map();
|
|
14
|
-
const server = createServer({
|
|
15
|
-
cert: settings.cert,
|
|
16
|
-
key: settings.key
|
|
17
|
-
}, (request, response) => {
|
|
18
|
-
const chunks = [];
|
|
19
|
-
request.on("data", (chunk) => chunks.push(chunk));
|
|
20
|
-
request.on("end", () => {
|
|
21
|
-
const path = request.url ?? "/";
|
|
22
|
-
const text = Buffer.concat(chunks).toString("utf8");
|
|
23
|
-
let body;
|
|
24
|
-
try {
|
|
25
|
-
body = parseJson(text);
|
|
26
|
-
} catch {
|
|
27
|
-
body = void 0;
|
|
28
|
-
}
|
|
29
|
-
const each = {
|
|
30
|
-
headers: request.headers,
|
|
31
|
-
text,
|
|
32
|
-
body
|
|
33
|
-
};
|
|
34
|
-
const list = received.get(path) ?? [];
|
|
35
|
-
list.push(each);
|
|
36
|
-
received.set(path, list);
|
|
37
|
-
const index = list.filter((one) => !isVerification(one)).length - 1;
|
|
38
|
-
const verification = {
|
|
39
|
-
status: 200,
|
|
40
|
-
body: JSON.stringify({ challenge: memberOf(body, "challenge") })
|
|
41
|
-
};
|
|
42
|
-
const chosen = answerers.get(path)?.(each, index) ?? (isVerification(each) ? verification : { status: 200 });
|
|
43
|
-
response.writeHead(chosen.status, { "Content-Type": "application/json" }).end(chosen.body ?? "");
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
47
|
-
const address = server.address();
|
|
48
|
-
const port = typeof address === "object" && address !== null ? address.port : 0;
|
|
49
|
-
return {
|
|
50
|
-
url: (answer) => {
|
|
51
|
-
const path = `/hook/${randomUUID()}`;
|
|
52
|
-
if (answer !== void 0) answerers.set(path, answer);
|
|
53
|
-
return {
|
|
54
|
-
url: `https://127.0.0.1:${port}${path}`,
|
|
55
|
-
received: () => received.get(path) ?? []
|
|
56
|
-
};
|
|
57
|
-
},
|
|
58
|
-
close: () => new Promise((resolve) => {
|
|
59
|
-
server.closeAllConnections();
|
|
60
|
-
server.close(() => resolve());
|
|
61
|
-
})
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
const newSecret = () => `whsec_${randomBytes(32).toString("base64")}`;
|
|
65
|
-
function header(request, name) {
|
|
66
|
-
const value = request.headers[name];
|
|
67
|
-
expect(typeof value === "string", `the request must have the header ${name}, got ${JSON.stringify(request.headers)}`);
|
|
68
|
-
return value;
|
|
69
|
-
}
|
|
70
|
-
async function expectSignedWith(signingKeys, request, subscription, secrets, type) {
|
|
71
|
-
expect(header(request, "content-type").startsWith("application/json"), `Content-Type must be application/json, got ${header(request, "content-type")}`);
|
|
72
|
-
const id = header(request, "webhook-id");
|
|
73
|
-
const pattern = type === void 0 ? /^msg_[A-Za-z0-9]+$/ : new RegExp(`^msg_${type}_[A-Za-z0-9]+$`);
|
|
74
|
-
expect(pattern.test(id), `webhook-id must match ${pattern}, got ${JSON.stringify(id)}`);
|
|
75
|
-
const timestampText = header(request, "webhook-timestamp");
|
|
76
|
-
const timestamp = Number(timestampText);
|
|
77
|
-
expect(/^\d+$/.test(timestampText) && Math.abs(timestamp - Date.now() / 1e3) <= 300, `webhook-timestamp must be the time in seconds, got ${JSON.stringify(timestampText)}`);
|
|
78
|
-
expectEqual(header(request, "x-mcp-subscription-id"), subscription, "X-MCP-Subscription-Id");
|
|
79
|
-
const signatures = header(request, "webhook-signature").split(" ");
|
|
80
|
-
expectEqual(signatures.length, secrets.length + signingKeys.length, `the number of signatures, one v1 for each secret and one v1a for each signing key of the Server Card, in ${JSON.stringify(signatures)}`);
|
|
81
|
-
const symmetric = signatures.slice(0, secrets.length);
|
|
82
|
-
for (const secret of secrets) {
|
|
83
|
-
const expected = await webhookSignature([secret], id, timestamp, request.text);
|
|
84
|
-
expect(symmetric.includes(expected), `webhook-signature must start with the v1 signature ${expected} of each secret, got ${JSON.stringify(signatures)}`);
|
|
85
|
-
}
|
|
86
|
-
await expectServerSignatures(signingKeys, signatures.slice(secrets.length), `${id}.${timestamp}.${request.text}`);
|
|
87
|
-
return id;
|
|
88
|
-
}
|
|
89
|
-
async function expectServerSignatures(signingKeys, asymmetric, signed) {
|
|
90
|
-
expect(asymmetric.every((each) => each.startsWith("v1a,")), `the signatures after the v1 signatures must be v1a signatures, got ${JSON.stringify(asymmetric)}`);
|
|
91
|
-
for (const [index, key] of signingKeys.entries()) {
|
|
92
|
-
const x = key.x;
|
|
93
|
-
expect(typeof x === "string", `the signing key ${JSON.stringify(key.kid)} must have x`);
|
|
94
|
-
const publicKey = await crypto.subtle.importKey("jwk", {
|
|
95
|
-
kty: "OKP",
|
|
96
|
-
crv: "Ed25519",
|
|
97
|
-
x
|
|
98
|
-
}, "Ed25519", false, ["verify"]);
|
|
99
|
-
const signature = Buffer.from((asymmetric[index] ?? "").slice(4), "base64");
|
|
100
|
-
const verified = await crypto.subtle.verify("Ed25519", publicKey, signature, new TextEncoder().encode(signed));
|
|
101
|
-
expect(verified, `the v1a signature ${index + 1} must verify with the signing key ${JSON.stringify(key.kid)} of the Server Card`);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
function expectDelivered(request, name) {
|
|
105
|
-
expect(isObject(request.body) && !("truncated" in request.body), `the body must be an occurrence without truncated, got ${request.text}`);
|
|
106
|
-
return expectOccurrence(request.body, name);
|
|
107
|
-
}
|
|
108
|
-
async function checkWebhook(checks, url, settings, signingKeys) {
|
|
109
|
-
let receiver;
|
|
110
|
-
try {
|
|
111
|
-
receiver = await startReceiver(settings);
|
|
112
|
-
} catch (error) {
|
|
113
|
-
await checks.run("conform starts an HTTPS receiver with the certificate and the key", RULE, () => {
|
|
114
|
-
throw error instanceof Error ? error : new Error(String(error));
|
|
115
|
-
});
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
try {
|
|
119
|
-
await checkWithReceiver(checks, url, settings, receiver, signingKeys);
|
|
120
|
-
} finally {
|
|
121
|
-
await receiver.close();
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
async function checkWithReceiver(checks, url, settings, receiver, signingKeys) {
|
|
125
|
-
const expectSigned = (request, subscription, secrets, type) => expectSignedWith(signingKeys, request, subscription, secrets, type);
|
|
126
|
-
const principal = settings.principal;
|
|
127
|
-
const channel = httpChannel(url, { Authorization: `Bearer ${principal}` });
|
|
128
|
-
const anonymous = httpChannel(url);
|
|
129
|
-
const { errorOf, watching, signalKey } = eventRequests(channel);
|
|
130
|
-
const subscribeParams = (name, input, hook, secret, extra = {}) => ({
|
|
131
|
-
name,
|
|
132
|
-
arguments: input,
|
|
133
|
-
delivery: {
|
|
134
|
-
mode: "webhook",
|
|
135
|
-
url: hook,
|
|
136
|
-
secret
|
|
137
|
-
},
|
|
138
|
-
...extra
|
|
139
|
-
});
|
|
140
|
-
const unsubscribeParams = (name, input, hook) => ({
|
|
141
|
-
name,
|
|
142
|
-
arguments: input,
|
|
143
|
-
delivery: { url: hook }
|
|
144
|
-
});
|
|
145
|
-
const resultOf = (message, method) => {
|
|
146
|
-
const result = memberOf(message, "result");
|
|
147
|
-
expect(isObject(result), `${method} must give a result, got ${JSON.stringify(message)}`);
|
|
148
|
-
return result;
|
|
149
|
-
};
|
|
150
|
-
const main = {
|
|
151
|
-
channel,
|
|
152
|
-
principal
|
|
153
|
-
};
|
|
154
|
-
const subscribe = async (name, input, hook, secret, signal, extra = {}, client = main) => {
|
|
155
|
-
const sent = Date.now();
|
|
156
|
-
const result = resultOf(await client.channel.request("events/subscribe", subscribeParams(name, input, hook, secret, extra), signal), "events/subscribe");
|
|
157
|
-
expectEqual(result.resultType, "complete", "resultType");
|
|
158
|
-
expectEqual(result.id, await webhookSubscriptionId(client.principal, hook, name, input), "the subscription id");
|
|
159
|
-
const refreshBefore = result.refreshBefore;
|
|
160
|
-
expect(typeof refreshBefore === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/.test(refreshBefore), `refreshBefore must be an RFC 3339 date and time, got ${JSON.stringify(refreshBefore)}`);
|
|
161
|
-
expect(Date.parse(refreshBefore) > sent, `refreshBefore must be after the request, got ${refreshBefore}`);
|
|
162
|
-
expect(result.cursor === null || typeof result.cursor === "string", `cursor must be the watermark or null, got ${JSON.stringify(result.cursor)}`);
|
|
163
|
-
expectEqual(result.truncated, false, "truncated");
|
|
164
|
-
const status = result.deliveryStatus;
|
|
165
|
-
expectEqual(memberOf(status, "active"), true, "deliveryStatus.active");
|
|
166
|
-
const last = memberOf(status, "lastDeliveryAt");
|
|
167
|
-
expect(last === null || typeof last === "string", `deliveryStatus.lastDeliveryAt must be a date or null, got ${JSON.stringify(last)}`);
|
|
168
|
-
expect(isObject(status) && "lastError" in status, `deliveryStatus must have lastError, got ${JSON.stringify(status)}`);
|
|
169
|
-
return {
|
|
170
|
-
id: String(result.id),
|
|
171
|
-
refreshBefore: Date.parse(refreshBefore)
|
|
172
|
-
};
|
|
173
|
-
};
|
|
174
|
-
const unsubscribe = (name, input, hook, signal, client = main) => client.channel.request("events/unsubscribe", unsubscribeParams(name, input, hook), signal);
|
|
175
|
-
const cleanup = (name, input, hook, client = main) => unsubscribe(name, input, hook, AbortSignal.timeout(checks.deadline), client).catch(() => void 0);
|
|
176
|
-
const deliveries = (hook) => hook.received().filter((each) => !isVerification(each));
|
|
177
|
-
const atLeast = (hook, number, signal) => until(`the receiver gets ${number} deliveries`, async () => deliveries(hook), (list) => list.length >= number, signal);
|
|
178
|
-
const watchedKey = (key) => (keys) => keys.includes(key);
|
|
179
|
-
const minTtlMs = settings.minTtlMs ?? MCP_WEBHOOK_DEFAULTS.minTtlMs;
|
|
180
|
-
const maxTtlMs = settings.maxTtlMs ?? MCP_WEBHOOK_DEFAULTS.maxTtlMs;
|
|
181
|
-
if (maxTtlMs <= checks.deadline) {
|
|
182
|
-
await checks.run("the maximum lifetime of a subscription is longer than the deadline", RULE, () => {
|
|
183
|
-
throw new Error(`conform tests webhook delivery only when the maximum lifetime is longer than the deadline of ${checks.deadline} ms, got ${maxTtlMs}`);
|
|
184
|
-
});
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
await checks.run("events/subscribe and events/unsubscribe with no principal are Forbidden", RULE, async (signal) => {
|
|
188
|
-
const hook = receiver.url();
|
|
189
|
-
const input = { key: randomUUID() };
|
|
190
|
-
const subscribed = errorOf(await anonymous.request("events/subscribe", subscribeParams("fixture.watch", input, hook.url, newSecret()), signal));
|
|
191
|
-
expectEqual(subscribed.code, -32024, "the JSON-RPC error code of events/subscribe");
|
|
192
|
-
const unsubscribed = errorOf(await anonymous.request("events/unsubscribe", unsubscribeParams("fixture.watch", input, hook.url), signal));
|
|
193
|
-
expectEqual(unsubscribed.code, -32024, "the JSON-RPC error code of events/unsubscribe");
|
|
194
|
-
expectEqual(hook.received().length, 0, "the requests that the receiver got");
|
|
195
|
-
});
|
|
196
|
-
await checks.run("a URL that is not https, or a secret that is not valid, is the error -32602", RULE, async (signal) => {
|
|
197
|
-
const hook = receiver.url();
|
|
198
|
-
const http = hook.url.replace(/^https:/, "http:");
|
|
199
|
-
for (const [what, params] of [
|
|
200
|
-
["a URL that is not https", subscribeParams("fixture.watch", { key: randomUUID() }, http, newSecret())],
|
|
201
|
-
["a secret of 16 bytes", subscribeParams("fixture.watch", { key: randomUUID() }, hook.url, `whsec_${randomBytes(16).toString("base64")}`)],
|
|
202
|
-
["a secret with no prefix", subscribeParams("fixture.watch", { key: randomUUID() }, hook.url, randomBytes(32).toString("base64"))]
|
|
203
|
-
]) expectEqual(errorOf(await channel.request("events/subscribe", params, signal)).code, -32602, `the JSON-RPC error code for ${what}`);
|
|
204
|
-
expectEqual(hook.received().length, 0, "the requests that the receiver got");
|
|
205
|
-
});
|
|
206
|
-
await checks.run("the server verifies a new URL with a challenge, then answers with the subscription", RULE, async (signal) => {
|
|
207
|
-
const hook = receiver.url();
|
|
208
|
-
const input = {
|
|
209
|
-
count: 0,
|
|
210
|
-
key: randomUUID()
|
|
211
|
-
};
|
|
212
|
-
const secret = newSecret();
|
|
213
|
-
try {
|
|
214
|
-
const { id } = await subscribe("fixture.ticks", input, hook.url, secret, signal);
|
|
215
|
-
const [verification] = hook.received();
|
|
216
|
-
expect(verification !== void 0 && isVerification(verification), `the first request must be the verification, got ${JSON.stringify(verification?.body)}`);
|
|
217
|
-
expect(typeof memberOf(verification.body, "challenge") === "string", `the verification must have a challenge, got ${verification.text}`);
|
|
218
|
-
await expectSigned(verification, id, [secret], "verification");
|
|
219
|
-
} finally {
|
|
220
|
-
await cleanup("fixture.ticks", input, hook.url);
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
await checks.run("an endpoint that does not answer the challenge is CallbackEndpointError, and the subscription does not start", RULE, async (signal) => {
|
|
224
|
-
const hook = receiver.url((request) => isVerification(request) ? {
|
|
225
|
-
status: 200,
|
|
226
|
-
body: JSON.stringify({ challenge: "wrong" })
|
|
227
|
-
} : void 0);
|
|
228
|
-
const input = { key: randomUUID() };
|
|
229
|
-
const error = errorOf(await channel.request("events/subscribe", subscribeParams("fixture.watch", input, hook.url, newSecret()), signal));
|
|
230
|
-
expectEqual(error.code, -32027, "the JSON-RPC error code");
|
|
231
|
-
expectEqual(memberOf(error.data, "reason"), "challenge_failed", "data.reason");
|
|
232
|
-
await until("the key is not in fixture.watching", () => watching(signal), (keys) => !keys.includes(input.key), signal);
|
|
233
|
-
expectEqual(hook.received().length, 1, "the requests that the receiver got: only the verification");
|
|
234
|
-
});
|
|
235
|
-
await checks.run("each occurrence is one signed POST, in order, whose cursor is the watermark", RULE, async (signal) => {
|
|
236
|
-
const hook = receiver.url();
|
|
237
|
-
const input = {
|
|
238
|
-
count: 3,
|
|
239
|
-
key: randomUUID()
|
|
240
|
-
};
|
|
241
|
-
const secret = newSecret();
|
|
242
|
-
try {
|
|
243
|
-
const { id } = await subscribe("fixture.ticks", input, hook.url, secret, signal);
|
|
244
|
-
const list = await atLeast(hook, 3, signal);
|
|
245
|
-
const ids = /* @__PURE__ */ new Set();
|
|
246
|
-
for (const [index, request] of list.entries()) {
|
|
247
|
-
const n = index + 1;
|
|
248
|
-
ids.add(await expectSigned(request, id, [secret]));
|
|
249
|
-
const occurrence = expectDelivered(request, "fixture.ticks");
|
|
250
|
-
expectEqual([
|
|
251
|
-
occurrence.data,
|
|
252
|
-
occurrence.eventId,
|
|
253
|
-
occurrence.cursor
|
|
254
|
-
], [
|
|
255
|
-
{ n },
|
|
256
|
-
String(n),
|
|
257
|
-
String(n)
|
|
258
|
-
], "the data, the eventId and the cursor");
|
|
259
|
-
}
|
|
260
|
-
expectEqual(ids.size, 3, "the number of different webhook-id headers");
|
|
261
|
-
} finally {
|
|
262
|
-
await cleanup("fixture.ticks", input, hook.url);
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
await checks.run("a subscription gets the things that happen; a renewal grants a new lifetime and signs with both secrets; events/unsubscribe ends it", RULE, async (signal) => {
|
|
266
|
-
const hook = receiver.url();
|
|
267
|
-
const input = { key: randomUUID() };
|
|
268
|
-
const [first, second] = [newSecret(), newSecret()];
|
|
269
|
-
try {
|
|
270
|
-
const started = await subscribe("fixture.watch", input, hook.url, first, signal, { ttlMs: checks.deadline });
|
|
271
|
-
await until("fixture.watching gives the key", () => watching(signal), watchedKey(input.key), signal);
|
|
272
|
-
expectEqual(await signalKey(input.key, "a", signal), 1, "the subscriptions that fixture.signal reached");
|
|
273
|
-
const [a] = await atLeast(hook, 1, signal);
|
|
274
|
-
if (a === void 0) throw new Error("the receiver got no delivery");
|
|
275
|
-
await expectSigned(a, started.id, [first]);
|
|
276
|
-
const occurrence = expectDelivered(a, "fixture.watch");
|
|
277
|
-
expectEqual(occurrence.data, {
|
|
278
|
-
...input,
|
|
279
|
-
value: "a"
|
|
280
|
-
}, "the data");
|
|
281
|
-
expect(!("cursor" in occurrence), `with no cursor of the provider, the delivery has no cursor, got ${a.text}`);
|
|
282
|
-
const renewed = await subscribe("fixture.watch", input, hook.url, second, signal);
|
|
283
|
-
expectEqual(renewed.id, started.id, "the subscription id of the renewal");
|
|
284
|
-
expect(renewed.refreshBefore > started.refreshBefore, "a renewal with no ttlMs, which grants the maximum lifetime, must end after the first grant of a lifetime of the deadline");
|
|
285
|
-
expectEqual(count(await watching(signal), input.key), 1, "the subscriptions of the key after the renewal");
|
|
286
|
-
expectEqual(await signalKey(input.key, "b", signal), 1, "the subscriptions that fixture.signal reached after the renewal");
|
|
287
|
-
const [, b] = await atLeast(hook, 2, signal);
|
|
288
|
-
if (b === void 0) throw new Error("the receiver got no second delivery");
|
|
289
|
-
await expectSigned(b, started.id, [second, first]);
|
|
290
|
-
expectEqual(memberOf(resultOf(await unsubscribe("fixture.watch", input, hook.url, signal), "events/unsubscribe"), "resultType"), "complete", "the resultType of events/unsubscribe");
|
|
291
|
-
await until("the key leaves fixture.watching", () => watching(signal), (keys) => !keys.includes(input.key), signal);
|
|
292
|
-
const again = errorOf(await unsubscribe("fixture.watch", input, hook.url, signal));
|
|
293
|
-
expectEqual(again.code, -32023, "the JSON-RPC error code of a second events/unsubscribe");
|
|
294
|
-
expectEqual(memberOf(again.data, "kind"), "subscription", "data.kind");
|
|
295
|
-
} finally {
|
|
296
|
-
await cleanup("fixture.watch", input, hook.url);
|
|
297
|
-
}
|
|
298
|
-
});
|
|
299
|
-
await checks.run("a delivery that gets a status that is not 2xx is sent again, with the same webhook-id", RULE, async (signal) => {
|
|
300
|
-
const hook = receiver.url((request, index) => !isVerification(request) && index === 0 ? { status: 500 } : void 0);
|
|
301
|
-
const input = { key: randomUUID() };
|
|
302
|
-
const secret = newSecret();
|
|
303
|
-
try {
|
|
304
|
-
const { id } = await subscribe("fixture.watch", input, hook.url, secret, signal);
|
|
305
|
-
await until("fixture.watching gives the key", () => watching(signal), watchedKey(input.key), signal);
|
|
306
|
-
await signalKey(input.key, "again", signal);
|
|
307
|
-
const [failed, retried] = await atLeast(hook, 2, signal);
|
|
308
|
-
if (failed === void 0 || retried === void 0) throw new Error("the receiver got no retry");
|
|
309
|
-
const firstId = await expectSigned(failed, id, [secret]);
|
|
310
|
-
expectEqual(await expectSigned(retried, id, [secret]), firstId, "the webhook-id of the second attempt");
|
|
311
|
-
expectEqual(retried.text, failed.text, "the body of the second attempt");
|
|
312
|
-
} finally {
|
|
313
|
-
await cleanup("fixture.watch", input, hook.url);
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
await checks.run("a 410 abandons the delivery at once, and the next occurrence follows", RULE, async (signal) => {
|
|
317
|
-
const hook = receiver.url((request, index) => !isVerification(request) && index === 0 ? { status: 410 } : void 0);
|
|
318
|
-
const input = { key: randomUUID() };
|
|
319
|
-
try {
|
|
320
|
-
await subscribe("fixture.watch", input, hook.url, newSecret(), signal);
|
|
321
|
-
await until("fixture.watching gives the key", () => watching(signal), watchedKey(input.key), signal);
|
|
322
|
-
await signalKey(input.key, "gone", signal);
|
|
323
|
-
await atLeast(hook, 1, signal);
|
|
324
|
-
await signalKey(input.key, "next", signal);
|
|
325
|
-
const [gone, next] = await atLeast(hook, 2, signal);
|
|
326
|
-
if (gone === void 0 || next === void 0) throw new Error("the receiver got no second delivery");
|
|
327
|
-
expectEqual(expectDelivered(gone, "fixture.watch").data, {
|
|
328
|
-
...input,
|
|
329
|
-
value: "gone"
|
|
330
|
-
}, "the data of the first delivery");
|
|
331
|
-
expectEqual(expectDelivered(next, "fixture.watch").data, {
|
|
332
|
-
...input,
|
|
333
|
-
value: "next"
|
|
334
|
-
}, "the data of the second delivery");
|
|
335
|
-
} finally {
|
|
336
|
-
await cleanup("fixture.watch", input, hook.url);
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
await checks.run("a defect after an occurrence sends the control message terminated", RULE, async (signal) => {
|
|
340
|
-
const hook = receiver.url();
|
|
341
|
-
const secret = newSecret();
|
|
342
|
-
try {
|
|
343
|
-
const { id } = await subscribe("fixture.broken", {}, hook.url, secret, signal);
|
|
344
|
-
const [occurrence, terminated] = await atLeast(hook, 2, signal);
|
|
345
|
-
if (occurrence === void 0 || terminated === void 0) throw new Error("the receiver got no control message");
|
|
346
|
-
expectEqual(expectDelivered(occurrence, "fixture.broken").data, { n: 1 }, "the data of the occurrence");
|
|
347
|
-
expect(!isOccurrence(terminated.body), `the second request must be a control message, got ${terminated.text}`);
|
|
348
|
-
expectEqual(memberOf(terminated.body, "type"), "terminated", "the type of the control message");
|
|
349
|
-
const error = memberOf(terminated.body, "error");
|
|
350
|
-
expectEqual(memberOf(error, "code"), -32603, "the error code of the control message");
|
|
351
|
-
expect(typeof memberOf(error, "message") === "string" && !/\n\s+at /.test(String(memberOf(error, "message"))), "the message must be text with no stack trace");
|
|
352
|
-
await expectSigned(terminated, id, [secret], "terminated");
|
|
353
|
-
} finally {
|
|
354
|
-
await cleanup("fixture.broken", {}, hook.url);
|
|
355
|
-
}
|
|
356
|
-
});
|
|
357
|
-
await checks.run("before an occurrence after a gap, the server sends the control message gap with the watermark", RULE, async (signal) => {
|
|
358
|
-
const hook = receiver.url();
|
|
359
|
-
const input = {
|
|
360
|
-
count: 3,
|
|
361
|
-
gap: 2,
|
|
362
|
-
key: randomUUID()
|
|
363
|
-
};
|
|
364
|
-
const secret = newSecret();
|
|
365
|
-
try {
|
|
366
|
-
const { id } = await subscribe("fixture.ticks", input, hook.url, secret, signal);
|
|
367
|
-
const [first, gap, second, third] = await atLeast(hook, 4, signal);
|
|
368
|
-
if (first === void 0 || gap === void 0 || second === void 0 || third === void 0) throw new Error("the receiver got less than four requests");
|
|
369
|
-
expectEqual(expectDelivered(first, "fixture.ticks").data, { n: 1 }, "the data of the first delivery");
|
|
370
|
-
expectEqual(gap.body, {
|
|
371
|
-
type: "gap",
|
|
372
|
-
cursor: "1"
|
|
373
|
-
}, "the control message gap, with the watermark after the first delivery");
|
|
374
|
-
await expectSigned(gap, id, [secret], "gap");
|
|
375
|
-
const after = expectDelivered(second, "fixture.ticks");
|
|
376
|
-
expectEqual([after.data, after.cursor], [{ n: 2 }, "2"], "the data and the cursor of the delivery after the gap");
|
|
377
|
-
expectEqual(expectDelivered(third, "fixture.ticks").data, { n: 3 }, "the data of the last delivery");
|
|
378
|
-
} finally {
|
|
379
|
-
await cleanup("fixture.ticks", input, hook.url);
|
|
380
|
-
}
|
|
381
|
-
});
|
|
382
|
-
await checks.run("the server grants ttlMs between the minimum and the maximum lifetime, and the maximum with no ttlMs", RULE, async (signal) => {
|
|
383
|
-
const hook = receiver.url();
|
|
384
|
-
const cases = [
|
|
385
|
-
[
|
|
386
|
-
"a ttlMs below the minimum",
|
|
387
|
-
{ ttlMs: Math.floor(minTtlMs / 2) },
|
|
388
|
-
minTtlMs
|
|
389
|
-
],
|
|
390
|
-
[
|
|
391
|
-
"a ttlMs above the maximum",
|
|
392
|
-
{ ttlMs: maxTtlMs * 2 },
|
|
393
|
-
maxTtlMs
|
|
394
|
-
],
|
|
395
|
-
[
|
|
396
|
-
"ttlMs: null",
|
|
397
|
-
{ ttlMs: null },
|
|
398
|
-
maxTtlMs
|
|
399
|
-
],
|
|
400
|
-
[
|
|
401
|
-
"no ttlMs",
|
|
402
|
-
{},
|
|
403
|
-
maxTtlMs
|
|
404
|
-
]
|
|
405
|
-
];
|
|
406
|
-
for (const [what, extra, granted] of cases) {
|
|
407
|
-
const input = { key: randomUUID() };
|
|
408
|
-
try {
|
|
409
|
-
const before = Date.now();
|
|
410
|
-
const { refreshBefore } = await subscribe("fixture.watch", input, hook.url, newSecret(), signal, extra);
|
|
411
|
-
const after = Date.now();
|
|
412
|
-
expect(refreshBefore >= before + granted - REFRESH_BEFORE_PRECISION_MS && refreshBefore <= after + granted + REFRESH_BEFORE_PRECISION_MS, `for ${what}, refreshBefore must be ${granted} ms after the request, got ${refreshBefore - before} ms after its start`);
|
|
413
|
-
} finally {
|
|
414
|
-
await cleanup("fixture.watch", input, hook.url);
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
});
|
|
418
|
-
if (settings.minTtlMs !== void 0) await checks.run("a subscription with no renewal ends at the end of its lifetime, and gets no more deliveries", RULE, async (signal) => {
|
|
419
|
-
const hook = receiver.url();
|
|
420
|
-
const input = { key: randomUUID() };
|
|
421
|
-
try {
|
|
422
|
-
expect(minTtlMs < checks.deadline, `conform tests the end of a lifetime only when the minimum lifetime is shorter than the deadline of ${checks.deadline} ms, got ${minTtlMs}`);
|
|
423
|
-
const secret = newSecret();
|
|
424
|
-
let renewedAt = Date.now();
|
|
425
|
-
await until("fixture.watching gives the key, while each events/subscribe renews the subscription", async () => {
|
|
426
|
-
renewedAt = Date.now();
|
|
427
|
-
await subscribe("fixture.watch", input, hook.url, secret, signal, { ttlMs: minTtlMs });
|
|
428
|
-
return watching(signal);
|
|
429
|
-
}, watchedKey(input.key), signal);
|
|
430
|
-
await until("the lifetime ends, and the key leaves fixture.watching", () => watching(signal), (keys) => !keys.includes(input.key), signal);
|
|
431
|
-
const elapsed = Date.now() - renewedAt;
|
|
432
|
-
expect(elapsed >= minTtlMs, `the subscription ended ${elapsed} ms after the last events/subscribe, before its lifetime of ${minTtlMs} ms`);
|
|
433
|
-
expectEqual(await signalKey(input.key, "late", signal), 0, "the subscriptions that fixture.signal reached after the end");
|
|
434
|
-
expectEqual(deliveries(hook).length, 0, "the deliveries that the receiver got");
|
|
435
|
-
const ended = errorOf(await unsubscribe("fixture.watch", input, hook.url, signal));
|
|
436
|
-
expectEqual([ended.code, memberOf(ended.data, "kind")], [-32023, "subscription"], "the error of events/unsubscribe after the end");
|
|
437
|
-
} finally {
|
|
438
|
-
await cleanup("fixture.watch", input, hook.url);
|
|
439
|
-
}
|
|
440
|
-
});
|
|
441
|
-
if (settings.maxSubscriptions !== void 0) {
|
|
442
|
-
const max = settings.maxSubscriptions;
|
|
443
|
-
const other = settings.otherPrincipal;
|
|
444
|
-
const name = `a new subscription over the limit of a principal is ResourceExhausted, and a renewal still works${other === void 0 ? "" : ", and another principal can still subscribe"}`;
|
|
445
|
-
await checks.run(name, RULE, async (signal) => {
|
|
446
|
-
const client = other === void 0 ? main : {
|
|
447
|
-
channel: httpChannel(url, { Authorization: `Bearer ${other}` }),
|
|
448
|
-
principal: other
|
|
449
|
-
};
|
|
450
|
-
const hook = receiver.url();
|
|
451
|
-
const created = [];
|
|
452
|
-
const extra = { key: randomUUID() };
|
|
453
|
-
try {
|
|
454
|
-
let refused;
|
|
455
|
-
while (refused === void 0 && created.length <= max) {
|
|
456
|
-
const input = { key: randomUUID() };
|
|
457
|
-
const message = await client.channel.request("events/subscribe", subscribeParams("fixture.watch", input, hook.url, newSecret()), signal);
|
|
458
|
-
if (memberOf(message, "error") === void 0) {
|
|
459
|
-
resultOf(message, "events/subscribe");
|
|
460
|
-
created.push(input);
|
|
461
|
-
} else refused = errorOf(message);
|
|
462
|
-
}
|
|
463
|
-
if (refused === void 0) throw new Error(`the server granted ${created.length} subscriptions to one principal, over the limit of ${max}`);
|
|
464
|
-
expectEqual(refused.code, -32025, "the JSON-RPC error code");
|
|
465
|
-
expectEqual(refused.data, {
|
|
466
|
-
limit: "subscriptions",
|
|
467
|
-
max
|
|
468
|
-
}, "data");
|
|
469
|
-
if (other !== void 0) expectEqual(created.length, max, "the subscriptions that the server granted before the limit");
|
|
470
|
-
const [first] = created;
|
|
471
|
-
if (first === void 0) throw new Error("the principal had no free subscription, so the check cannot renew one");
|
|
472
|
-
await subscribe("fixture.watch", first, hook.url, newSecret(), signal, {}, client);
|
|
473
|
-
if (other !== void 0) await subscribe("fixture.watch", extra, hook.url, newSecret(), signal);
|
|
474
|
-
} finally {
|
|
475
|
-
for (const input of created) await cleanup("fixture.watch", input, hook.url, client);
|
|
476
|
-
await cleanup("fixture.watch", extra, hook.url);
|
|
477
|
-
}
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
export { checkWebhook };
|
package/dist/conform/webmcp.d.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { Checks } from "./check.js";
|
|
2
|
-
export interface RecordedTool {
|
|
3
|
-
readonly name: string;
|
|
4
|
-
readonly description: string;
|
|
5
|
-
readonly inputSchema: unknown;
|
|
6
|
-
}
|
|
7
|
-
export interface HostTool extends RecordedTool {
|
|
8
|
-
execute(input: unknown, options: {
|
|
9
|
-
readonly signal: AbortSignal;
|
|
10
|
-
}): Promise<unknown>;
|
|
11
|
-
}
|
|
12
|
-
export type ToolCall = {
|
|
13
|
-
readonly resolved: unknown;
|
|
14
|
-
} | {
|
|
15
|
-
readonly rejected: {
|
|
16
|
-
readonly name: string;
|
|
17
|
-
readonly kind: unknown;
|
|
18
|
-
readonly message: string;
|
|
19
|
-
};
|
|
20
|
-
} | {
|
|
21
|
-
readonly missing: true;
|
|
22
|
-
};
|
|
23
|
-
export interface TestHost {
|
|
24
|
-
readonly modelContext: {
|
|
25
|
-
registerTool(tool: HostTool, options: {
|
|
26
|
-
readonly signal: AbortSignal;
|
|
27
|
-
}): void;
|
|
28
|
-
};
|
|
29
|
-
tools(): RecordedTool[];
|
|
30
|
-
call(name: string, input: unknown, id?: string): Promise<ToolCall>;
|
|
31
|
-
cancel(id: string): void;
|
|
32
|
-
}
|
|
33
|
-
/** The function uses no imports and no outer variables, because the browser driver sends its source text to the page. */
|
|
34
|
-
export declare function createTestHost(): TestHost;
|
|
35
|
-
export interface WebMcpDriver {
|
|
36
|
-
tools(signal: AbortSignal): Promise<RecordedTool[]>;
|
|
37
|
-
call(name: string, input: unknown, options?: {
|
|
38
|
-
readonly signal?: AbortSignal;
|
|
39
|
-
readonly cancel?: AbortSignal;
|
|
40
|
-
}): Promise<ToolCall>;
|
|
41
|
-
close(): Promise<void>;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Install the implementation on a host in the same process. Return a function that removes it, or nothing. The
|
|
45
|
-
* implementation serves the `app` target of the fixture, with its switch on.
|
|
46
|
-
*/
|
|
47
|
-
export type WebMcpInstall = (modelContext: TestHost["modelContext"]) => void | (() => void | Promise<void>) | Promise<void | (() => void | Promise<void>)>;
|
|
48
|
-
export declare function processDriver(install: WebMcpInstall): Promise<WebMcpDriver>;
|
|
49
|
-
export declare function browserDriver(url: string): Promise<WebMcpDriver>;
|
|
50
|
-
export declare function checkWebMcp(driver: WebMcpDriver, options: {
|
|
51
|
-
readonly deadline: number;
|
|
52
|
-
}): Promise<Checks>;
|