@vite-hub/webhooks 0.4.1 → 0.5.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.
@@ -1,7 +1,7 @@
1
- import { t as webhooksErrors } from "./errors-1y5fWGlX.js";
2
- import { t as isRecordOrFunction } from "./object-BJb1W_q5.js";
1
+ import { n as isRecordOrFunction } from "./object-CAK6UYR5.js";
2
+ import { t as webhooksErrors } from "./errors-DT8uyCh7.js";
3
3
  import * as v from "valibot";
4
- //#region ../../node_modules/.pnpm/nostics@1.4.0/node_modules/nostics/dist/index.mjs
4
+ // ../../node_modules/.pnpm/nostics@1.4.0/node_modules/nostics/dist/index.mjs
5
5
  /**
6
6
  * Transforms a value or a function that returns a value to a value.
7
7
  *
@@ -103,8 +103,7 @@ function defineDiagnostics(options) {
103
103
  }
104
104
  return result;
105
105
  }
106
- //#endregion
107
- //#region ../internal/src/error-diagnostics.ts
106
+ // ../internal/src/error-diagnostics.ts
108
107
  const dynamicError = { why: ({ message }) => message === void 0 ? "" : String(message) };
109
108
  const internalErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
110
109
  docsBase: "https://vitehub.dev/docs/development/diagnostics",
@@ -130,7 +129,7 @@ const internalErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
130
129
  why: ({ message }) => message === void 0 ? "" : String(message)
131
130
  },
132
131
  INTERNAL_R0022: {
133
- fix: "Send input that matches the schema of the operation. `vitehub <namespace> <command> --help` shows the fields.",
132
+ fix: "Send input that matches the schema of the operation. `vitehub <namespace> <command> --help` shows the fields, and the list_operations tool of the ViteHub MCP gives the input schema of each operation.",
134
133
  why: ({ message }) => message === void 0 ? "" : String(message)
135
134
  },
136
135
  INTERNAL_R0024: {
@@ -349,6 +348,10 @@ const internalErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
349
348
  why: ({ owner }) => `[vitehub] ${owner} extends tuple requires [preset, options] with an options object.`,
350
349
  fix: "Set `extends` to a preset, or to a two-item tuple `[preset, { ...options }]` whose second item is a plain object."
351
350
  },
351
+ INTERNAL_RUNTIME_PACKAGE_VERSION_CONFLICT: {
352
+ why: ({ first, name, second }) => `[vitehub] One server function needs ${name} from two installations: ${first} and ${second}.`,
353
+ fix: "Install one version of the package that the message names, for example with `pnpm dedupe` or an `overrides` entry in `package.json`, then build again."
354
+ },
352
355
  INTERNAL_SECRET_PURPOSE_MISSING: {
353
356
  why: "[vitehub] A derived secret requires a purpose.",
354
357
  fix: "Pass a non-empty purpose to `deriveSecret()`, such as `deriveSecret(\"my-module:session\")`."
@@ -357,6 +360,10 @@ const internalErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
357
360
  why: ({ minimum }) => `[vitehub] VITEHUB_SECRET must be at least ${minimum} characters. Generate one with \`openssl rand -base64 32\`.`,
358
361
  fix: "Set `VITEHUB_SECRET` to the output of `openssl rand -base64 32`, which has 44 characters."
359
362
  },
363
+ INTERNAL_SITE_TOKEN_TOO_LONG: {
364
+ why: ({ length, maximum }) => `The Site access token is ${length} characters, and an app accepts ${maximum}.`,
365
+ fix: "Open the app with a shorter path, or give the person and the project shorter names, so that the Site access token fits."
366
+ },
360
367
  INTERNAL_SQLITE_ARGUMENT_INVALID: {
361
368
  why: ({ value }) => value === "undefined" ? "undefined cannot be passed as argument to the database" : "Only finite numbers (not Infinity or NaN) can be passed as arguments",
362
369
  fix: "Pass query arguments as finite numbers, strings, booleans, bigints, dates, byte arrays, or null. Replace undefined with null, and check numbers with `Number.isFinite()` before the query."
@@ -371,8 +378,7 @@ const internalErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
371
378
  }
372
379
  }
373
380
  });
374
- //#endregion
375
- //#region ../internal/src/standard-schema.ts
381
+ // ../internal/src/standard-schema.ts
376
382
  function readOwn(value, key) {
377
383
  if (value === null || Object(value) !== value) return;
378
384
  try {
@@ -401,8 +407,7 @@ function standardJsonSchema(schema, direction, target = "draft-2020-12", library
401
407
  return;
402
408
  }
403
409
  }
404
- //#endregion
405
- //#region ../../node_modules/.pnpm/@valibot+to-json-schema@1.8.0_valibot@1.5.0_typescript@6.0.3_/node_modules/@valibot/to-json-schema/dist/index.mjs
410
+ // ../../node_modules/.pnpm/@valibot+to-json-schema@1.8.0_valibot@1.5.0_typescript@6.0.3_/node_modules/@valibot/to-json-schema/dist/index.mjs
406
411
  /**
407
412
  * Adds an error message to the errors array.
408
413
  *
@@ -1084,8 +1089,7 @@ function toJsonSchema(schema, config) {
1084
1089
  if (context.referenceMap.size) jsonSchema.$defs = context.definitions;
1085
1090
  return jsonSchema;
1086
1091
  }
1087
- //#endregion
1088
- //#region ../internal/src/valibot.ts
1092
+ // ../internal/src/valibot.ts
1089
1093
  const valibotTargets = /* @__PURE__ */ new Set([
1090
1094
  "draft-07",
1091
1095
  "draft-2020-12",
@@ -1126,8 +1130,7 @@ function withValibotJsonSchema(schema, options = {}) {
1126
1130
  }
1127
1131
  };
1128
1132
  }
1129
- //#endregion
1130
- //#region ../internal/src/operation-definition.ts
1133
+ // ../internal/src/operation-definition.ts
1131
1134
  const nameFormat = /^[a-z][a-z0-9-]*\.[a-z][a-z0-9-]*$/;
1132
1135
  const kinds = /* @__PURE__ */ new Set([
1133
1136
  "read",
@@ -1187,16 +1190,21 @@ function isSectionId(section) {
1187
1190
  function operationInputJsonSchema(operation) {
1188
1191
  return standardJsonSchema(operation.input, "input") ?? { type: "object" };
1189
1192
  }
1190
- //#endregion
1191
- //#region src/operations.ts
1193
+ // src/operations.ts
1192
1194
  const runtime = () => import("./runtime.js");
1193
- /** The HTTP status that the CLI, the Console, and Agents report for each code of a failed operation. */
1195
+ const members = () => import("./members-Cr_Nlyg4.js").then((n) => n.l);
1194
1196
  const statuses = {
1195
1197
  WEBHOOKS_BODY_INVALID: 400,
1196
1198
  WEBHOOKS_DATABASE_MISSING: 409,
1197
1199
  WEBHOOKS_DELIVERY_ACTIVE: 409,
1198
1200
  WEBHOOKS_DELIVERY_NOT_FOUND: 404,
1201
+ WEBHOOKS_DELIVERY_NOT_OWNER: 403,
1199
1202
  WEBHOOKS_DELIVERY_NOT_PENDING: 409,
1203
+ WEBHOOKS_ENDPOINT_DISABLED: 409,
1204
+ WEBHOOKS_ENDPOINT_NOT_FOUND: 404,
1205
+ WEBHOOKS_ENDPOINTS_OFF: 409,
1206
+ WEBHOOKS_KEY_INVALID: 409,
1207
+ WEBHOOKS_KEY_UNSET: 409,
1200
1208
  WEBHOOKS_NOT_FOUND: 404,
1201
1209
  WEBHOOKS_NOT_INSTALLED: 409,
1202
1210
  WEBHOOKS_SECRET_UNSET: 409,
@@ -1205,11 +1213,9 @@ const statuses = {
1205
1213
  WEBHOOKS_URL_UNSET: 409,
1206
1214
  WEBHOOKS_WAKE_FAILED: 502
1207
1215
  };
1208
- /** Gives a Webhooks error the status that the surfaces report. Other errors pass unchanged. */
1209
1216
  function failure(error) {
1210
1217
  return webhooksErrors.is(error) ? Object.assign(error, { status: statuses[error.code] ?? 500 }) : error;
1211
1218
  }
1212
- /** Runs a runtime function and throws its error, so the operation fails with the code, `why`, and `fix`. */
1213
1219
  async function unwrap(run) {
1214
1220
  let result;
1215
1221
  try {
@@ -1220,7 +1226,11 @@ async function unwrap(run) {
1220
1226
  if (result[0]) throw failure(result[0]);
1221
1227
  return result[1];
1222
1228
  }
1223
- function defineWebhooksOperation(definition) {
1229
+ /**
1230
+ * Access to the Webhooks section of the Console decides. Most operations never read the signed-in user. Those with
1231
+ * `user: true` do: a member delivery goes out again only for the member who owns its endpoint.
1232
+ */
1233
+ function defineWebhooksOperation(definition, options = {}) {
1224
1234
  return defineOperation({
1225
1235
  ...definition,
1226
1236
  cli: {
@@ -1228,106 +1238,186 @@ function defineWebhooksOperation(definition) {
1228
1238
  ...definition.cli
1229
1239
  },
1230
1240
  section: "webhooks",
1231
- user: false
1241
+ ...options.user ? {} : { user: false }
1232
1242
  });
1233
1243
  }
1244
+ /**
1245
+ * The caller of a member action. A call that the ViteHub Console signs names a Console account, which is not an app
1246
+ * user, so only its email can match the owner. Any other call names an app user by id.
1247
+ */
1248
+ async function memberCaller(context) {
1249
+ const { request, user } = context;
1250
+ if (!user || context.access.via === "cli" || context.surface === "cli") return void 0;
1251
+ if (context.access.via !== "console") return { id: user.id };
1252
+ const email = user.email?.trim().toLowerCase();
1253
+ if (!request || user.emailVerified !== true || !email) return void 0;
1254
+ const id = await (await runtime()).installedWebhooks()?.resolveVerifiedOwner?.(email, request);
1255
+ return id ? {
1256
+ id,
1257
+ verifiedConsoleEmail: email
1258
+ } : void 0;
1259
+ }
1260
+ function endpointChange(endpoint) {
1261
+ return {
1262
+ id: endpoint.id,
1263
+ owner: endpoint.owner,
1264
+ disabledAt: endpoint.disabledAt,
1265
+ disabledBy: endpoint.disabledBy
1266
+ };
1267
+ }
1268
+ /** The name that the Console shows for the person who disabled an endpoint. */
1269
+ function personLabel(context) {
1270
+ const { user } = context;
1271
+ if (!user) return context.surface === "cli" ? "the CLI" : "an admin";
1272
+ return [user.name, user.email].find((value) => typeof value === "string" && value.trim() !== "") ?? user.id;
1273
+ }
1234
1274
  const webhookName = v.pipe(v.string(), v.minLength(1), v.description("The webhook name, the file name of server/webhooks/<name>.ts."));
1235
1275
  const deliveryId = v.pipe(v.string(), v.minLength(1), v.description("The delivery id, such as whd_..."));
1236
1276
  const limit = v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(500), v.description("At most this many, from 1 to 500. Default: 100.")));
1277
+ const list = defineWebhooksOperation({
1278
+ cli: { default: true },
1279
+ description: "List the webhooks of the app: the names of their URL and secret variables, whether each is set, the URL host, the signature, and the debounce. Never the URL path, the query, or the secret.",
1280
+ input: v.strictObject({}),
1281
+ kind: "read",
1282
+ name: "webhooks.list",
1283
+ run: async () => {
1284
+ const { listWebhooks } = await runtime();
1285
+ try {
1286
+ return listWebhooks();
1287
+ } catch (error) {
1288
+ throw failure(error);
1289
+ }
1290
+ }
1291
+ });
1292
+ const deliveries = defineWebhooksOperation({
1293
+ description: "List webhook deliveries, newest first, with their state, attempts, last status, and last error. A member delivery names its endpoint as @<endpoint id>; owner lists the member's deliveries across endpoint reconnections.",
1294
+ input: v.strictObject({
1295
+ webhook: v.optional(v.pipe(v.string(), v.minLength(1), v.description("The webhook name, or @<endpoint id> for a member endpoint."))),
1296
+ owner: v.optional(v.pipe(v.string(), v.minLength(1), v.description("The member who owns the endpoint, such as the Auth user id."))),
1297
+ state: v.optional(v.picklist([
1298
+ "pending",
1299
+ "sending",
1300
+ "sent",
1301
+ "dead",
1302
+ "cancelled"
1303
+ ])),
1304
+ limit,
1305
+ before: v.optional(v.pipe(v.strictObject({
1306
+ createdAt: v.number(),
1307
+ id: v.string()
1308
+ }), v.description("The createdAt and id of the last delivery of the previous page.")))
1309
+ }),
1310
+ kind: "read",
1311
+ name: "webhooks.deliveries",
1312
+ run: async (input) => unwrap(async () => (await runtime()).listWebhookDeliveries(input))
1313
+ });
1314
+ const pending = defineWebhooksOperation({
1315
+ description: "List the pending webhook deliveries, the soonest due first: the debounce timers that wait now.",
1316
+ input: v.strictObject({
1317
+ webhook: v.optional(webhookName),
1318
+ limit
1319
+ }),
1320
+ kind: "read",
1321
+ name: "webhooks.pending",
1322
+ run: async (input) => unwrap(async () => (await runtime()).listPendingWebhookDeliveries(input))
1323
+ });
1324
+ const get = defineWebhooksOperation({
1325
+ cli: { positional: ["id"] },
1326
+ description: "Show one webhook delivery with its body.",
1327
+ input: v.strictObject({ id: deliveryId }),
1328
+ kind: "read",
1329
+ name: "webhooks.get",
1330
+ run: async (input) => unwrap(async () => (await runtime()).getWebhookDelivery(input.id))
1331
+ });
1332
+ const test = defineWebhooksOperation({
1333
+ cli: { positional: ["name"] },
1334
+ description: "Send a test delivery to a webhook now, without its debounce, with the event webhooks.test. The body is a JSON value; the default is { test: true, webhook: <name> }.",
1335
+ input: v.strictObject({
1336
+ name: webhookName,
1337
+ body: v.optional(v.unknown())
1338
+ }),
1339
+ kind: "write",
1340
+ name: "webhooks.test",
1341
+ run: async (input) => unwrap(async () => (await runtime()).testWebhook(input.name, input.body === void 0 ? {} : { body: input.body }))
1342
+ });
1343
+ const flush = defineWebhooksOperation({
1344
+ cli: { positional: ["id"] },
1345
+ description: "Deliver a pending webhook delivery now, without the rest of its debounce. A member delivery goes out only for the member who owns its endpoint.",
1346
+ input: v.strictObject({ id: deliveryId }),
1347
+ kind: "write",
1348
+ name: "webhooks.flush",
1349
+ run: async (input, context) => unwrap(async () => (await runtime()).flushWebhookDelivery(input.id, { resolveCaller: () => memberCaller(context) }))
1350
+ }, { user: true });
1351
+ const cancel = defineWebhooksOperation({
1352
+ cli: { positional: ["id"] },
1353
+ description: "Cancel a pending webhook delivery, so it is never sent.",
1354
+ input: v.strictObject({ id: deliveryId }),
1355
+ kind: "write",
1356
+ name: "webhooks.cancel",
1357
+ run: async (input) => unwrap(async () => (await runtime()).cancelWebhookDelivery(input.id))
1358
+ });
1359
+ const redeliver = defineWebhooksOperation({
1360
+ cli: { positional: ["id"] },
1361
+ description: "Send a sent, dead, or cancelled webhook delivery again now, as a new delivery with the same body and event. A member delivery goes out again only for the member who owns its endpoint; anyone else gets 403.",
1362
+ input: v.strictObject({ id: deliveryId }),
1363
+ kind: "write",
1364
+ name: "webhooks.redeliver",
1365
+ run: async (input, context) => unwrap(async () => (await runtime()).redeliverWebhookDelivery(input.id, { resolveCaller: () => memberCaller(context) }))
1366
+ }, { user: true });
1367
+ const endpointId = v.pipe(v.string(), v.minLength(1), v.description("The member endpoint id, such as whe_..."));
1237
1368
  /**
1238
1369
  * The `webhooks.*` operations. The Console, `vitehub webhooks`, and MCP run them on the app's operation RPC, `POST
1239
1370
  * /_vitehub/rpc`.
1240
1371
  */
1241
1372
  const webhooksOperations = [
1373
+ list,
1374
+ deliveries,
1375
+ pending,
1376
+ get,
1377
+ test,
1378
+ flush,
1379
+ cancel,
1380
+ redeliver,
1242
1381
  defineWebhooksOperation({
1243
- cli: { default: true },
1244
- description: "List the webhooks of the app: the names of their URL and secret variables, whether each is set, the URL host, the signature, and the debounce. Never the URL path, the query, or the secret.",
1245
- input: v.strictObject({}),
1246
- kind: "read",
1247
- name: "webhooks.list",
1248
- run: async () => {
1249
- const { listWebhooks } = await runtime();
1250
- try {
1251
- return listWebhooks();
1252
- } catch (error) {
1253
- throw failure(error);
1254
- }
1255
- }
1256
- }),
1257
- defineWebhooksOperation({
1258
- description: "List webhook deliveries, newest first, with their state, attempts, last status, and last error.",
1382
+ description: "List the member endpoints, where each member's agent gets their sends: the owner, the agent, the URL host, and the newest delivery. Never the URL path, the query, the secret, or the owner's email.",
1259
1383
  input: v.strictObject({
1260
- webhook: v.optional(webhookName),
1261
- state: v.optional(v.picklist([
1262
- "pending",
1263
- "sending",
1264
- "sent",
1265
- "dead",
1266
- "cancelled"
1267
- ])),
1268
- limit,
1269
- before: v.optional(v.pipe(v.strictObject({
1384
+ limit: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(500), v.description("Page size, from 1 to 500. Default: 500."))),
1385
+ after: v.optional(v.pipe(v.strictObject({
1270
1386
  createdAt: v.number(),
1271
- id: v.string()
1272
- }), v.description("The createdAt and id of the last delivery of the previous page.")))
1273
- }),
1274
- kind: "read",
1275
- name: "webhooks.deliveries",
1276
- run: async (input) => unwrap(async () => (await runtime()).listWebhookDeliveries(input))
1277
- }),
1278
- defineWebhooksOperation({
1279
- description: "List the pending webhook deliveries, the soonest due first: the debounce timers that wait now.",
1280
- input: v.strictObject({
1281
- webhook: v.optional(webhookName),
1282
- limit
1387
+ id: endpointId
1388
+ }), v.description("The createdAt and id of the last endpoint of the previous page.")))
1283
1389
  }),
1284
1390
  kind: "read",
1285
- name: "webhooks.pending",
1286
- run: async (input) => unwrap(async () => (await runtime()).listPendingWebhookDeliveries(input))
1287
- }),
1288
- defineWebhooksOperation({
1289
- cli: { positional: ["id"] },
1290
- description: "Show one webhook delivery with its body.",
1291
- input: v.strictObject({ id: deliveryId }),
1292
- kind: "read",
1293
- name: "webhooks.get",
1294
- run: async (input) => unwrap(async () => (await runtime()).getWebhookDelivery(input.id))
1295
- }),
1296
- defineWebhooksOperation({
1297
- cli: { positional: ["name"] },
1298
- description: "Send a test delivery to a webhook now, without its debounce, with the event webhooks.test. The body is a JSON value; the default is { test: true, webhook: <name> }.",
1299
- input: v.strictObject({
1300
- name: webhookName,
1301
- body: v.optional(v.unknown())
1302
- }),
1303
- kind: "write",
1304
- name: "webhooks.test",
1305
- run: async (input) => unwrap(async () => (await runtime()).testWebhook(input.name, input.body === void 0 ? {} : { body: input.body }))
1306
- }),
1307
- defineWebhooksOperation({
1308
- cli: { positional: ["id"] },
1309
- description: "Deliver a pending webhook delivery now, without the rest of its debounce.",
1310
- input: v.strictObject({ id: deliveryId }),
1311
- kind: "write",
1312
- name: "webhooks.flush",
1313
- run: async (input) => unwrap(async () => (await runtime()).flushWebhookDelivery(input.id))
1314
- }),
1391
+ name: "webhooks.endpoints",
1392
+ run: async (input, context) => {
1393
+ const { listMemberEndpoints } = await members();
1394
+ const { ownsMemberEndpoint } = await runtime();
1395
+ const rows = await unwrap(async () => listMemberEndpoints(input));
1396
+ const caller = await memberCaller(context);
1397
+ return rows.map(({ ownerEmail, ...row }) => ({
1398
+ ...row,
1399
+ mine: ownsMemberEndpoint({
1400
+ owner: row.owner,
1401
+ ownerEmail
1402
+ }, caller)
1403
+ }));
1404
+ }
1405
+ }, { user: true }),
1315
1406
  defineWebhooksOperation({
1316
1407
  cli: { positional: ["id"] },
1317
- description: "Cancel a pending webhook delivery, so it is never sent.",
1318
- input: v.strictObject({ id: deliveryId }),
1408
+ description: "Disable a member endpoint, so the member's sends stop until an admin enables it or the member connects again.",
1409
+ input: v.strictObject({ id: endpointId }),
1319
1410
  kind: "write",
1320
- name: "webhooks.cancel",
1321
- run: async (input) => unwrap(async () => (await runtime()).cancelWebhookDelivery(input.id))
1322
- }),
1411
+ name: "webhooks.disable-endpoint",
1412
+ run: async (input, context) => endpointChange(await unwrap(async () => (await members()).disableMemberEndpoint(input.id, { by: personLabel(context) })))
1413
+ }, { user: true }),
1323
1414
  defineWebhooksOperation({
1324
1415
  cli: { positional: ["id"] },
1325
- description: "Send a sent, dead, or cancelled webhook delivery again now, as a new delivery with the same body and event.",
1326
- input: v.strictObject({ id: deliveryId }),
1416
+ description: "Enable a member endpoint that an admin disabled, so the member's sends go out again.",
1417
+ input: v.strictObject({ id: endpointId }),
1327
1418
  kind: "write",
1328
- name: "webhooks.redeliver",
1329
- run: async (input) => unwrap(async () => (await runtime()).redeliverWebhookDelivery(input.id))
1419
+ name: "webhooks.enable-endpoint",
1420
+ run: async (input) => endpointChange(await unwrap(async () => (await members()).enableMemberEndpoint(input.id)))
1330
1421
  })
1331
1422
  ];
1332
- //#endregion
1333
1423
  export { webhooksOperations };
@@ -1,5 +1,3 @@
1
- //#region src/queue-name.d.ts
2
1
  /** The internal Queue that delivers every webhook. Its own module, so the Vite plugin reads it without the runtime. */
3
2
  declare const webhooksQueueName = "vitehub-webhooks";
4
- //#endregion
5
3
  export { webhooksQueueName as t };
@@ -1,5 +1,4 @@
1
- //#region src/queue-name.ts
1
+ // src/queue-name.ts
2
2
  /** The internal Queue that delivers every webhook. Its own module, so the Vite plugin reads it without the runtime. */
3
3
  const webhooksQueueName = "vitehub-webhooks";
4
- //#endregion
5
4
  export { webhooksQueueName as t };