@andco/protocol 0.0.9 → 0.0.11

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/dist/webhooks.js CHANGED
@@ -131,23 +131,23 @@ function isEvent(value) {
131
131
  * ```
132
132
  */
133
133
  function isGrantEvent(event) {
134
- return ANDCO_WEBHOOK_GRANT_EVENT_TYPES.includes(event.type);
134
+ return ANDCO_WEBHOOK_GRANT_EVENT_TYPES.includes(event["type"]);
135
135
  }
136
136
  /** Whether a lifecycle event's payload has the shape its type promises; other events pass. */
137
137
  function hasLifecyclePayload(event) {
138
138
  if (!isGrantEvent(event))
139
139
  return true;
140
- const payload = event.payload;
140
+ const payload = event["payload"];
141
141
  const named = typeof payload["grant_id"] === "string" &&
142
142
  typeof payload["client_id"] === "string" &&
143
143
  typeof payload["resource"] === "string";
144
144
  if (!named)
145
145
  return false;
146
- if (event.type === "grant.revoked") {
146
+ if (event["type"] === "grant.revoked") {
147
147
  return ((payload["reason"] === "revoked_by_person" || payload["reason"] === "revoked_by_client") &&
148
148
  typeof payload["revoked_at"] === "string");
149
149
  }
150
- else if (event.type === "grant.expired") {
150
+ else if (event["type"] === "grant.expired") {
151
151
  return typeof payload["expires_at"] === "string";
152
152
  }
153
153
  else {
@@ -177,8 +177,8 @@ function hasLifecyclePayload(event) {
177
177
  *
178
178
  * export async function POST(request: Request) {
179
179
  * const event = await verify(request);
180
- * if (event.type === "deposit.completed") await markPaid(event.payload["intent_id"] as string);
181
- * if (andcoWebhooks.isGrantEvent(event) && event.type !== "grant.updated") await forgetTokens(event.payload.grant_id);
180
+ * if (event["type"] === "deposit.completed") await markPaid(event["payload"]["intent_id"] as string);
181
+ * if (andcoWebhooks.isGrantEvent(event) && event["type"] !== "grant.updated") await forgetTokens(event["payload"]["grant_id"]);
182
182
  * return new Response(null, { status: 204 });
183
183
  * }
184
184
  * ```
@@ -195,6 +195,7 @@ function verifier(options = {}) {
195
195
  const tolerance = options.toleranceSeconds ?? ANDCO_WEBHOOK_TOLERANCE_SECONDS;
196
196
  const now = options.now ?? (() => Math.floor(Date.now() / 1000));
197
197
  const ttl = options.keysTtlMs ?? ANDCO_WEBHOOK_KEYS_TTL_MS;
198
+ const log = options.log ?? (() => { });
198
199
  let keys = new Map();
199
200
  let loading;
200
201
  let loadedAt = Number.NEGATIVE_INFINITY;
@@ -211,19 +212,19 @@ function verifier(options = {}) {
211
212
  });
212
213
  }
213
214
  const document = response.ok ? await response.json().catch(() => null) : null;
214
- const entries = document?.keys;
215
+ const entries = document?.["keys"];
215
216
  if (!Array.isArray(entries)) {
216
217
  throw new AndcoWebhookError(ANDCO_WEBHOOK_ERROR_CODES.JWKS_UNAVAILABLE, `${jwksUrl} answered ${response.status} without a JWKS`);
217
218
  }
218
219
  // Replace, never merge: a kid Andco withdrew must stop verifying.
219
220
  const next = new Map();
220
221
  for (const entry of entries) {
221
- if (entry?.kty !== "OKP" || entry.crv !== "Ed25519" || typeof entry.x !== "string")
222
+ if (entry?.["kty"] !== "OKP" || entry["crv"] !== "Ed25519" || typeof entry["x"] !== "string")
222
223
  continue;
223
- if (typeof entry.kid !== "string" || next.has(entry.kid))
224
+ if (typeof entry["kid"] !== "string" || next.has(entry["kid"]))
224
225
  continue;
225
- const kid = entry.kid;
226
- const x = entry.x;
226
+ const kid = entry["kid"];
227
+ const x = entry["x"];
227
228
  const previous = keys.get(kid);
228
229
  if (previous && previous.x === x) {
229
230
  next.set(kid, previous);
@@ -240,7 +241,7 @@ function verifier(options = {}) {
240
241
  }
241
242
  keys = next;
242
243
  loadedAt = Date.now();
243
- console.debug("[andcoWebhooks.verifier] loaded %s webhook keys from %s", keys.size, jwksUrl);
244
+ log("[andcoWebhooks.verifier] loaded %s webhook keys from %s", keys.size, jwksUrl);
244
245
  }
245
246
  async function keyFor(kid) {
246
247
  const cached = keys.get(kid);
@@ -260,7 +261,7 @@ function verifier(options = {}) {
260
261
  catch (error) {
261
262
  if (!cached)
262
263
  throw error;
263
- console.debug("[andcoWebhooks.verifier] reload failed, verifying %s with its cached key: %o", kid, error);
264
+ log("[andcoWebhooks.verifier] reload failed, verifying %s with its cached key: %o", kid, error);
264
265
  return cached.key;
265
266
  }
266
267
  const loaded = keys.get(kid);
@@ -296,7 +297,7 @@ function verifier(options = {}) {
296
297
  const content = new TextEncoder().encode(ANDCO_WEBHOOK_SIGNED_CONTENT(eventId, timestamp, body));
297
298
  const valid = await crypto.subtle.verify({ name: "Ed25519" }, key, bytes, content);
298
299
  if (!valid) {
299
- console.debug("[andcoWebhooks.verify] signature mismatch for event %s and key %s", eventId, kid);
300
+ log("[andcoWebhooks.verify] signature mismatch for event %s and key %s", eventId, kid);
300
301
  throw new AndcoWebhookError(ANDCO_WEBHOOK_ERROR_CODES.INVALID_SIGNATURE);
301
302
  }
302
303
  let event;
@@ -306,10 +307,10 @@ function verifier(options = {}) {
306
307
  catch {
307
308
  event = null;
308
309
  }
309
- if (!isEvent(event) || event.id !== eventId)
310
+ if (!isEvent(event) || event["id"] !== eventId)
310
311
  throw new AndcoWebhookError(ANDCO_WEBHOOK_ERROR_CODES.INVALID_PAYLOAD);
311
312
  if (!hasLifecyclePayload(event)) {
312
- console.debug("[andcoWebhooks.verify] %s %s has a payload that does not match its type", event.type, eventId);
313
+ log("[andcoWebhooks.verify] %s %s has a payload that does not match its type", event["type"], eventId);
313
314
  throw new AndcoWebhookError(ANDCO_WEBHOOK_ERROR_CODES.INVALID_PAYLOAD);
314
315
  }
315
316
  return event;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andco/protocol",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "andco": {
5
5
  "release": 1780358400
6
6
  },
@@ -75,7 +75,7 @@
75
75
  "generate:openapi": "openapi-typescript openapi/oauth-api.yaml -o src/transport/generated/oauth-api.ts && biome format --write src/transport/generated/oauth-api.ts",
76
76
  "build": "pnpm run clean && pnpm run sync-release && tsc",
77
77
  "dev": "tsc --watch --preserveWatchOutput",
78
- "check-types": "tsc --noEmit",
78
+ "check-types": "tsc -p tsconfig.test.json",
79
79
  "lint": "tsc --noEmit",
80
80
  "test": "vitest run"
81
81
  }