@crvouga/mockingbird-runtime 0.6.0 → 0.7.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.
@@ -144,6 +144,25 @@ type UpstreamOptions = {
144
144
  rewriteAuth?: (request: Request) => Request | Promise<Request>;
145
145
  };
146
146
 
147
+ /** An admission quota applied on the namespace clock. */
148
+ type FaultLimit = {
149
+ requests: number;
150
+ per: Duration;
151
+ key?: "namespace" | "credential";
152
+ window?: "fixed" | "sliding";
153
+ };
154
+ type LimitState = {
155
+ used: number;
156
+ windowStart: number | null;
157
+ windowResetsAt: number | null;
158
+ };
159
+ type LimitBucket = {
160
+ start: number;
161
+ used: number;
162
+ } | {
163
+ arrivals: number[];
164
+ };
165
+
147
166
  /**
148
167
  * Seeded pseudo-random numbers, so anything a mock invents — ids, jitter, which
149
168
  * request a percentage fault hits — is reproducible from a seed.
@@ -211,6 +230,8 @@ type FaultRule = {
211
230
  * rules added through `POST /__admin/faults` default to the calling namespace.
212
231
  */
213
232
  namespace?: string;
233
+ /** Admit at most this many matching requests per window. */
234
+ limit?: FaultLimit;
214
235
  /** Let this many matching requests through first. */
215
236
  after?: number;
216
237
  /** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
@@ -302,13 +323,17 @@ type FaultCandidate = {
302
323
  * `undefined` for a body that is missing or not JSON. It must leave the request readable.
303
324
  */
304
325
  readJson?: () => Promise<unknown>;
326
+ /** The service-specific credential, if it has one. */
327
+ credential?: () => string | undefined;
305
328
  /** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
306
329
  now?: number;
330
+ /** Read the clock again after asynchronous request matching. */
331
+ clockNow?: () => number;
307
332
  /** How to spend a delay for this request. Default: the registry's own sleep. */
308
333
  wait?: (ms: number) => Promise<void>;
309
334
  };
310
335
  /** A stored rule and how often it was consulted. */
311
- type FaultListing = FaultRule & {
336
+ type FaultListing = FaultRule & Partial<LimitState> & {
312
337
  /** Faults left before the rule retires. `null` when it has no `count`. */
313
338
  remaining: number | null;
314
339
  /** Times the rule faulted a request. */
@@ -324,6 +349,7 @@ type FaultSnapshot = {
324
349
  */
325
350
  rules: (FaultListing & {
326
351
  position?: number;
352
+ limitBuckets?: [string, LimitBucket][];
327
353
  })[];
328
354
  /** How many `fault_<n>` ids the namespace has handed out. */
329
355
  nextId?: number;
@@ -803,6 +829,8 @@ type WebhookEndpoint = {
803
829
  tags?: Record<string, string>;
804
830
  /** The public URL the receiver verifies signatures against (Twilio), when it differs. */
805
831
  signUrl?: string;
832
+ /** Disable automatic retry attempts for this endpoint. Explicit replay remains available. */
833
+ retry?: false;
806
834
  headers?: Record<string, string>;
807
835
  };
808
836
  type WebhookMessage = {
@@ -853,6 +881,8 @@ type PublishInput = {
853
881
  tags?: Record<string, string>;
854
882
  /** Message-specific delivery headers, captured as part of durable message state. */
855
883
  headers?: Record<string, string>;
884
+ /** Additional delivery endpoint for this message only. */
885
+ endpoint?: WebhookEndpoint;
856
886
  /** Message id; generated when omitted. */
857
887
  id?: string;
858
888
  };
@@ -2,7 +2,7 @@ import {
2
2
  normalizeIdBase,
3
3
  ownedTransport,
4
4
  signV4
5
- } from "./chunk-BK5KDNUE.js";
5
+ } from "./chunk-CIJF35LX.js";
6
6
  import "./chunk-2KDYLZXC.js";
7
7
  import {
8
8
  createClock,
@@ -13,7 +13,7 @@ import {
13
13
  resolveAdminPrefix,
14
14
  restoreClock,
15
15
  upstreamRequest
16
- } from "./chunk-GAIPLMRY.js";
16
+ } from "./chunk-MZMSHIAI.js";
17
17
  import {
18
18
  assertArchiveCompatibility,
19
19
  assertCompatibilityTags,
@@ -3651,7 +3651,7 @@ async function preflightFleet(config, options) {
3651
3651
  break;
3652
3652
  }
3653
3653
  }
3654
- targets.set(name, { ...target, packageVersion: packageVersion ?? "0.6.0" });
3654
+ targets.set(name, { ...target, packageVersion: packageVersion ?? "0.7.0" });
3655
3655
  } catch (error) {
3656
3656
  throw new Error(`service ${name}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
3657
3657
  }
@@ -144,6 +144,25 @@ type UpstreamOptions = {
144
144
  rewriteAuth?: (request: Request) => Request | Promise<Request>;
145
145
  };
146
146
 
147
+ /** An admission quota applied on the namespace clock. */
148
+ type FaultLimit = {
149
+ requests: number;
150
+ per: Duration;
151
+ key?: "namespace" | "credential";
152
+ window?: "fixed" | "sliding";
153
+ };
154
+ type LimitState = {
155
+ used: number;
156
+ windowStart: number | null;
157
+ windowResetsAt: number | null;
158
+ };
159
+ type LimitBucket = {
160
+ start: number;
161
+ used: number;
162
+ } | {
163
+ arrivals: number[];
164
+ };
165
+
147
166
  /**
148
167
  * Seeded pseudo-random numbers, so anything a mock invents — ids, jitter, which
149
168
  * request a percentage fault hits — is reproducible from a seed.
@@ -211,6 +230,8 @@ type FaultRule = {
211
230
  * rules added through `POST /__admin/faults` default to the calling namespace.
212
231
  */
213
232
  namespace?: string;
233
+ /** Admit at most this many matching requests per window. */
234
+ limit?: FaultLimit;
214
235
  /** Let this many matching requests through first. */
215
236
  after?: number;
216
237
  /** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
@@ -315,13 +336,17 @@ type FaultCandidate = {
315
336
  * `undefined` for a body that is missing or not JSON. It must leave the request readable.
316
337
  */
317
338
  readJson?: () => Promise<unknown>;
339
+ /** The service-specific credential, if it has one. */
340
+ credential?: () => string | undefined;
318
341
  /** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
319
342
  now?: number;
343
+ /** Read the clock again after asynchronous request matching. */
344
+ clockNow?: () => number;
320
345
  /** How to spend a delay for this request. Default: the registry's own sleep. */
321
346
  wait?: (ms: number) => Promise<void>;
322
347
  };
323
348
  /** A stored rule and how often it was consulted. */
324
- type FaultListing = FaultRule & {
349
+ type FaultListing = FaultRule & Partial<LimitState> & {
325
350
  /** Faults left before the rule retires. `null` when it has no `count`. */
326
351
  remaining: number | null;
327
352
  /** Times the rule faulted a request. */
@@ -337,6 +362,7 @@ type FaultSnapshot = {
337
362
  */
338
363
  rules: (FaultListing & {
339
364
  position?: number;
365
+ limitBuckets?: [string, LimitBucket][];
340
366
  })[];
341
367
  /** How many `fault_<n>` ids the namespace has handed out. */
342
368
  nextId?: number;
@@ -1252,6 +1278,8 @@ type WebhookEndpoint = {
1252
1278
  tags?: Record<string, string>;
1253
1279
  /** The public URL the receiver verifies signatures against (Twilio), when it differs. */
1254
1280
  signUrl?: string;
1281
+ /** Disable automatic retry attempts for this endpoint. Explicit replay remains available. */
1282
+ retry?: false;
1255
1283
  headers?: Record<string, string>;
1256
1284
  };
1257
1285
  type WebhookMessage = {
@@ -1302,6 +1330,8 @@ type PublishInput = {
1302
1330
  tags?: Record<string, string>;
1303
1331
  /** Message-specific delivery headers, captured as part of durable message state. */
1304
1332
  headers?: Record<string, string>;
1333
+ /** Additional delivery endpoint for this message only. */
1334
+ endpoint?: WebhookEndpoint;
1305
1335
  /** Message id; generated when omitted. */
1306
1336
  id?: string;
1307
1337
  };
@@ -1356,6 +1386,8 @@ type RuntimeOptions<T extends ServiceInstance> = {
1356
1386
  /** Reject unsupported forwarding surfaces before any network request. */
1357
1387
  canForward?: (request: Request) => boolean;
1358
1388
  upstreamRequest?: (request: Request, baseUrl: string) => Request;
1389
+ /** Return an emulated refusal before request fault rules are consumed. Read from a request clone. */
1390
+ beforeFaults?: (instance: T, request: Request) => Response | undefined | Promise<Response | undefined>;
1359
1391
  /** Service name, e.g. `"junction"`. Also the default namespace's storage key. */
1360
1392
  name: string;
1361
1393
  /** Build the instance backing one namespace. Called once per namespace, on first use. */
package/dist/authoring.js CHANGED
@@ -6,9 +6,9 @@ import {
6
6
  createService,
7
7
  defineOperations,
8
8
  jsonRes
9
- } from "./chunk-BK5KDNUE.js";
9
+ } from "./chunk-CIJF35LX.js";
10
10
  import "./chunk-2KDYLZXC.js";
11
- import "./chunk-GAIPLMRY.js";
11
+ import "./chunk-MZMSHIAI.js";
12
12
  import "./chunk-U36J5GCB.js";
13
13
  import "./chunk-EQLY2YOA.js";
14
14
  import "./chunk-HU6IOAFC.js";
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  DEFAULT_NAMESPACE
3
- } from "./chunk-BK5KDNUE.js";
3
+ } from "./chunk-CIJF35LX.js";
4
4
  import {
5
5
  fromBase64,
6
6
  matchNamespacePath
7
- } from "./chunk-GAIPLMRY.js";
7
+ } from "./chunk-MZMSHIAI.js";
8
8
 
9
9
  // ../mqtt/broker/dist/reason.js
10
10
  var ReasonCode = {
@@ -775,4 +775,4 @@ export {
775
775
  connectNamespace,
776
776
  brokerAdminRoutes
777
777
  };
778
- //# sourceMappingURL=chunk-SVG34QFE.js.map
778
+ //# sourceMappingURL=chunk-BKXECY5O.js.map
@@ -14,6 +14,7 @@ import {
14
14
  SNAPSHOT_FORMAT,
15
15
  adminUiRoutes,
16
16
  annotateResponse,
17
+ anyCredential,
17
18
  assertAdminPrefixAvailable,
18
19
  assertAdminUi,
19
20
  assertSnapshotDocument,
@@ -49,7 +50,7 @@ import {
49
50
  toHex,
50
51
  upstreamRequest,
51
52
  upstreamState
52
- } from "./chunk-GAIPLMRY.js";
53
+ } from "./chunk-MZMSHIAI.js";
53
54
  import {
54
55
  Timeline,
55
56
  forwardMountContext,
@@ -5899,8 +5900,8 @@ var stateAdminRoutes = (routes) => {
5899
5900
  };
5900
5901
 
5901
5902
  // ../service/core/dist/version.js
5902
- var UNRELEASED_VERSION = "0.6.0";
5903
- var PACKAGE_VERSION = true ? "0.6.0" : UNRELEASED_VERSION;
5903
+ var UNRELEASED_VERSION = "0.7.0";
5904
+ var PACKAGE_VERSION = true ? "0.7.0" : UNRELEASED_VERSION;
5904
5905
 
5905
5906
  // ../service/core/dist/webhooks.js
5906
5907
  var signers = {
@@ -6046,7 +6047,7 @@ var createWebhookHub = (options) => {
6046
6047
  };
6047
6048
  const schedule = (delivery) => {
6048
6049
  const index = delivery.attempts.length;
6049
- if (index >= delays.length) {
6050
+ if (index >= delays.length || index > 0 && payloads.get(delivery.id)?.endpoint.retry === false) {
6050
6051
  delivery.state = "failed";
6051
6052
  notify(delivery);
6052
6053
  pending.delete(delivery.id);
@@ -6081,8 +6082,11 @@ var createWebhookHub = (options) => {
6081
6082
  pending.set(delivery.id, timer);
6082
6083
  };
6083
6084
  const endpointsFor = (namespace) => [...own.get(namespace) ?? [], ...global];
6084
- const fanOut = (message, state = "pending") => {
6085
- for (const endpoint of endpointsFor(message.namespace)) {
6085
+ const fanOut = (message, state = "pending", extra) => {
6086
+ const endpoints = endpointsFor(message.namespace);
6087
+ if (extra)
6088
+ endpoints.push(extra);
6089
+ for (const endpoint of endpoints) {
6086
6090
  if (!matchesEndpoint(endpoint, message))
6087
6091
  continue;
6088
6092
  const delivery = {
@@ -6134,8 +6138,8 @@ var createWebhookHub = (options) => {
6134
6138
  if (!waiting)
6135
6139
  return;
6136
6140
  held.delete(namespace);
6137
- for (const message of waiting)
6138
- fanOut(message);
6141
+ for (const { message, endpoint } of waiting)
6142
+ fanOut(message, "pending", endpoint);
6139
6143
  };
6140
6144
  const hub = {
6141
6145
  observeDeliveries(listener) {
@@ -6234,18 +6238,19 @@ var createWebhookHub = (options) => {
6234
6238
  if (ofNamespace.length > keep && oldest)
6235
6239
  messages.splice(messages.indexOf(oldest), 1);
6236
6240
  options.onMessage?.(message);
6241
+ const endpoint = input.endpoint && structuredClone(input.endpoint);
6237
6242
  const fault = takeFault(message.namespace);
6238
6243
  if (fault === "drop") {
6239
- fanOut(message, "dropped");
6244
+ fanOut(message, "dropped", endpoint);
6240
6245
  return message;
6241
6246
  }
6242
6247
  if (fault === "reorder") {
6243
- held.set(message.namespace, [...held.get(message.namespace) ?? [], message]);
6248
+ held.set(message.namespace, [...held.get(message.namespace) ?? [], { message, endpoint }]);
6244
6249
  return message;
6245
6250
  }
6246
- fanOut(message);
6251
+ fanOut(message, "pending", endpoint);
6247
6252
  if (fault === "duplicate")
6248
- fanOut(message);
6253
+ fanOut(message, "pending", endpoint);
6249
6254
  releaseHeld(message.namespace);
6250
6255
  return message;
6251
6256
  },
@@ -6361,6 +6366,8 @@ var parseEndpoint = (value) => {
6361
6366
  endpoint.secret = value.secret;
6362
6367
  if (typeof value.signUrl === "string")
6363
6368
  endpoint.signUrl = value.signUrl;
6369
+ if (value.retry === false)
6370
+ endpoint.retry = false;
6364
6371
  const events = value.events ?? value.enabledEvents;
6365
6372
  if (Array.isArray(events))
6366
6373
  endpoint.events = events.map(String);
@@ -7503,6 +7510,7 @@ var createRuntime = (options) => {
7503
7510
  namespace,
7504
7511
  headers: request.headers,
7505
7512
  query: url.searchParams,
7513
+ credential: () => options.credential?.(request) ?? anyCredential(request),
7506
7514
  // Read from a copy, so the service still gets the body. Not JSON: nothing to match.
7507
7515
  readJson: async () => {
7508
7516
  try {
@@ -7513,17 +7521,8 @@ var createRuntime = (options) => {
7513
7521
  },
7514
7522
  wait
7515
7523
  };
7516
- let hits;
7517
- try {
7518
- await wait(latency.take(namespace, operationId) + (requested?.delay ?? 0));
7519
- hits = scripted.length > 0 ? await faults.fire(scripted, candidate) : [];
7520
- if (!hits.some((hit) => hit.drop || hit.response))
7521
- hits.push(...await faults.take({ ...candidate, now: requestClock.now() }));
7522
- } catch (error) {
7523
- log(0);
7524
- throw error;
7525
- }
7526
- const final = hits.find((hit) => hit.drop || hit.response);
7524
+ let hits = [];
7525
+ let final;
7527
7526
  const finish = (response2) => {
7528
7527
  let out = response2;
7529
7528
  const truncate = hits.find((hit) => hit.truncate !== void 0)?.truncate;
@@ -7540,6 +7539,31 @@ var createRuntime = (options) => {
7540
7539
  }
7541
7540
  return stamp(out);
7542
7541
  };
7542
+ const upstream = getUpstream(namespace);
7543
+ const wantsForward = (requested?.upstream ?? upstream?.mode) === "forward";
7544
+ try {
7545
+ await wait(latency.take(namespace, operationId) + (requested?.delay ?? 0));
7546
+ if (!wantsForward && options.beforeFaults) {
7547
+ const early = await options.beforeFaults(instanceFor(storage, namespace), request);
7548
+ if (early) {
7549
+ log(early.status, void 0, early);
7550
+ return finish(early);
7551
+ }
7552
+ }
7553
+ hits = scripted.length > 0 ? await faults.fire(scripted, candidate) : [];
7554
+ if (!hits.some((hit) => hit.drop || hit.response)) {
7555
+ const now = requestClock.now();
7556
+ hits.push(...await faults.take({
7557
+ ...candidate,
7558
+ now,
7559
+ clockNow: requestClock.now
7560
+ }));
7561
+ }
7562
+ } catch (error) {
7563
+ log(0);
7564
+ throw error;
7565
+ }
7566
+ final = hits.find((hit) => hit.drop || hit.response);
7543
7567
  if (final?.drop) {
7544
7568
  log(0, final.id);
7545
7569
  throw new DroppedConnectionError(final.timeout ? "timeout" : "drop");
@@ -7548,8 +7572,6 @@ var createRuntime = (options) => {
7548
7572
  log(final.response.status, final.id);
7549
7573
  return finish(final.response);
7550
7574
  }
7551
- const upstream = getUpstream(namespace);
7552
- const wantsForward = (requested?.upstream ?? upstream?.mode) === "forward";
7553
7575
  if (wantsForward) {
7554
7576
  if (!options.upstream || !upstream)
7555
7577
  return refuse(409, "an upstream fetch and baseUrl are required");
@@ -7935,4 +7957,4 @@ export {
7935
7957
  signV4,
7936
7958
  putObject
7937
7959
  };
7938
- //# sourceMappingURL=chunk-BK5KDNUE.js.map
7960
+ //# sourceMappingURL=chunk-CIJF35LX.js.map