@crvouga/mockingbird-runtime 0.5.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.
@@ -0,0 +1,83 @@
1
+ import {
2
+ Collection,
3
+ IdSequence,
4
+ bodyIssues,
5
+ createRuntime,
6
+ createService,
7
+ defineOperations,
8
+ jsonRes
9
+ } from "./chunk-CIJF35LX.js";
10
+ import "./chunk-2KDYLZXC.js";
11
+ import "./chunk-MZMSHIAI.js";
12
+ import "./chunk-U36J5GCB.js";
13
+ import "./chunk-EQLY2YOA.js";
14
+ import "./chunk-HU6IOAFC.js";
15
+ import "./chunk-4SAK6MQT.js";
16
+
17
+ // src/authoring.ts
18
+ function defineHttpService(definition) {
19
+ const {
20
+ name,
21
+ version,
22
+ defaultPort = 0,
23
+ document,
24
+ handlers,
25
+ before,
26
+ unsupported,
27
+ notFound = () => Response.json({ error: "not_found" }, { status: 404 }),
28
+ onError = () => Response.json({ error: "internal_error" }, { status: 500 }),
29
+ ...runtimeOptions
30
+ } = definition;
31
+ if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name))
32
+ throw new Error("expected a lowercase hyphenated service name");
33
+ if (!version.trim()) throw new Error("service version is required");
34
+ if (!Number.isInteger(defaultPort) || defaultPort < 0 || defaultPort > 65535)
35
+ throw new Error("defaultPort must be 0-65535");
36
+ const boot = (options = {}) => createRuntime({
37
+ ...options,
38
+ ...runtimeOptions,
39
+ name,
40
+ version,
41
+ document,
42
+ create(context) {
43
+ return createService({
44
+ sqlite: context.sqlite,
45
+ namespace: context.namespace,
46
+ now: context.clock.now,
47
+ document,
48
+ handlers: handlers(context),
49
+ notFound,
50
+ onError,
51
+ ...before && { before },
52
+ ...unsupported && { unsupported }
53
+ });
54
+ }
55
+ });
56
+ return {
57
+ createRuntime: boot,
58
+ serveTarget: {
59
+ name,
60
+ packageVersion: version,
61
+ defaultPort,
62
+ create(_values, common) {
63
+ const { adminKey, adminPrefix, seed, adminControls, onLog } = common;
64
+ return boot({
65
+ ...adminKey !== void 0 && { adminKey },
66
+ ...adminPrefix !== void 0 && { adminPrefix },
67
+ ...seed !== void 0 && { seed },
68
+ ...adminControls !== void 0 && { adminControls },
69
+ ...onLog && { onLog }
70
+ });
71
+ }
72
+ }
73
+ };
74
+ }
75
+ export {
76
+ Collection,
77
+ IdSequence,
78
+ bodyIssues,
79
+ defineHttpService,
80
+ defineOperations,
81
+ jsonRes as jsonResponse
82
+ };
83
+ //# sourceMappingURL=authoring.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/authoring.ts"],
4
+ "sourcesContent": ["import {\n createRuntime,\n createService,\n type InstanceContext,\n type MockCreateOptions,\n type OperationHandlers,\n type RuntimeOptions,\n type Service,\n type ServiceOptions,\n type ServiceRuntime,\n} from \"@crvouga/mockingbird-service\"\n\nexport type { InstanceContext, OperationHandlers } from \"@crvouga/mockingbird-service\"\nexport {\n bodyIssues,\n Collection,\n defineOperations,\n IdSequence,\n jsonResponse,\n} from \"@crvouga/mockingbird-service\"\n\nexport type HttpServiceDefinition = Pick<\n RuntimeOptions<Service>,\n \"name\" | \"presets\" | \"credential\" | \"state\" | \"admin\"\n> &\n Pick<ServiceOptions, \"document\" | \"before\" | \"unsupported\"> &\n Partial<Pick<ServiceOptions, \"onError\" | \"notFound\">> & {\n version: string\n defaultPort?: number\n handlers(context: InstanceContext): OperationHandlers\n }\n\nexport type HttpRuntimeFactory = (\n options?: MockCreateOptions & Pick<RuntimeOptions<Service>, \"onLog\">,\n) => ServiceRuntime<Service>\n\n/** Structural subset of the fleet boot options consumed by HTTP services. */\ntype HttpBootControls = {\n [K in \"adminKey\" | \"adminPrefix\" | \"seed\" | \"adminControls\" | \"onLog\"]?:\n | RuntimeOptions<Service>[K]\n | undefined\n}\n\nexport interface HttpServeTarget {\n name: string\n packageVersion: string\n defaultPort: number\n create(values: unknown, common: HttpBootControls): ServiceRuntime<Service>\n}\n\n/** Define once for Fetch, a Node listener, or a fleet. Each boot owns its storage and clock. */\nexport function defineHttpService(definition: HttpServiceDefinition): {\n createRuntime: HttpRuntimeFactory\n serveTarget: HttpServeTarget\n} {\n const {\n name,\n version,\n defaultPort = 0,\n document,\n handlers,\n before,\n unsupported,\n notFound = () => Response.json({ error: \"not_found\" }, { status: 404 }),\n onError = () => Response.json({ error: \"internal_error\" }, { status: 500 }),\n ...runtimeOptions\n } = definition\n if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name))\n throw new Error(\"expected a lowercase hyphenated service name\")\n if (!version.trim()) throw new Error(\"service version is required\")\n if (!Number.isInteger(defaultPort) || defaultPort < 0 || defaultPort > 65535)\n throw new Error(\"defaultPort must be 0-65535\")\n const boot = (\n options: MockCreateOptions & Pick<RuntimeOptions<Service>, \"onLog\"> = {},\n ): ServiceRuntime<Service> =>\n createRuntime({\n ...options,\n ...runtimeOptions,\n name,\n version,\n document,\n create(context) {\n return createService({\n sqlite: context.sqlite,\n namespace: context.namespace,\n now: context.clock.now,\n document,\n handlers: handlers(context),\n notFound,\n onError,\n ...(before && { before }),\n ...(unsupported && { unsupported }),\n })\n },\n })\n return {\n createRuntime: boot,\n serveTarget: {\n name,\n packageVersion: version,\n defaultPort,\n create(_values: unknown, common: HttpBootControls) {\n const { adminKey, adminPrefix, seed, adminControls, onLog } = common\n return boot({\n ...(adminKey !== undefined && { adminKey }),\n ...(adminPrefix !== undefined && { adminPrefix }),\n ...(seed !== undefined && { seed }),\n ...(adminControls !== undefined && { adminControls }),\n ...(onLog && { onLog }),\n })\n },\n },\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;AAmDO,SAAS,kBAAkB,YAGhC;AACA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW,MAAM,SAAS,KAAK,EAAE,OAAO,YAAY,GAAG,EAAE,QAAQ,IAAI,CAAC;A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
+ "names": []
7
+ }
@@ -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.5.0";
5903
- var PACKAGE_VERSION = true ? "0.5.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