@crvouga/mockingbird-service-wholescripts 3.6.0 → 3.8.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.
@@ -1,19 +1,39 @@
1
1
  import {
2
+ DEFAULT_CATALOG,
2
3
  createClock,
3
4
  createRuntime,
5
+ mergeCatalog,
4
6
  normalizeBootControls,
5
7
  normalizeControls,
8
+ normalizeIdBase,
6
9
  ownedTransport,
10
+ parseCatalogDocument,
7
11
  parseDuration,
8
12
  parseFaultHeader,
9
13
  resolveAdminPrefix,
10
14
  restoreClock
11
- } from "./chunk-T6P2TKQO.js";
15
+ } from "./chunk-4BLJLSNN.js";
16
+
17
+ // src/server.ts
18
+ import { readFileSync } from "node:fs";
12
19
 
13
20
  // ../../adapters/node/dist/cli.js
14
21
  import { readFile as readFile5 } from "node:fs/promises";
15
22
  import { parseArgs } from "node:util";
16
23
 
24
+ // ../../adapters/node/dist/boot-settings.js
25
+ var checkSettings = (target, settings, label) => {
26
+ if (!target.parseSettings)
27
+ throw new Error(`${label}: ${target.name} takes no boot settings`);
28
+ try {
29
+ return target.parseSettings(settings);
30
+ } catch (error) {
31
+ throw new Error(`${label}: ${error instanceof Error ? error.message : String(error)}`, {
32
+ cause: error
33
+ });
34
+ }
35
+ };
36
+
17
37
  // ../../adapters/node/dist/fleet.js
18
38
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
19
39
  import { createHash as createHash4, randomUUID as randomUUID4 } from "node:crypto";
@@ -2663,6 +2683,9 @@ var fleet_config_schema_default = {
2663
2683
  adminControls: {
2664
2684
  $ref: "#/$defs/controls"
2665
2685
  },
2686
+ idBase: {
2687
+ $ref: "#/$defs/idBase"
2688
+ },
2666
2689
  namespace: {
2667
2690
  type: "string",
2668
2691
  pattern: "^[A-Za-z0-9_.-]{1,64}$"
@@ -2752,6 +2775,12 @@ var fleet_config_schema_default = {
2752
2775
  adminControls: {
2753
2776
  $ref: "#/$defs/controls"
2754
2777
  },
2778
+ idBase: {
2779
+ $ref: "#/$defs/idBase"
2780
+ },
2781
+ settings: {
2782
+ type: "object"
2783
+ },
2755
2784
  fixtures: {
2756
2785
  type: "object"
2757
2786
  },
@@ -2798,6 +2827,18 @@ var fleet_config_schema_default = {
2798
2827
  }
2799
2828
  }
2800
2829
  },
2830
+ idBase: {
2831
+ anyOf: [
2832
+ {
2833
+ type: "integer",
2834
+ minimum: 0
2835
+ },
2836
+ {
2837
+ type: "string",
2838
+ pattern: "^(boot|[0-9]+)$"
2839
+ }
2840
+ ]
2841
+ },
2801
2842
  controls: {
2802
2843
  type: "object",
2803
2844
  additionalProperties: false,
@@ -2947,6 +2988,8 @@ async function preflightFleet(config, options) {
2947
2988
  throw new Error("protected archive policy requires a protected repository; configure its protector before startup");
2948
2989
  resolveAdminPrefix(config.adminPrefix);
2949
2990
  normalizeBootControls(config.adminControls);
2991
+ if (config.idBase !== void 0)
2992
+ normalizeIdBase(config.idBase, "idBase");
2950
2993
  const targets = /* @__PURE__ */ new Map();
2951
2994
  const ports = /* @__PURE__ */ new Set();
2952
2995
  for (const [name, entry] of Object.entries(config.services)) {
@@ -2978,6 +3021,13 @@ async function preflightFleet(config, options) {
2978
3021
  throw new Error("invalid protocol namespace selector");
2979
3022
  }
2980
3023
  }
3024
+ if (entry.idBase !== void 0)
3025
+ normalizeIdBase(entry.idBase, "idBase");
3026
+ if (entry.settings !== void 0) {
3027
+ if (protocol !== "http" || !("create" in target))
3028
+ throw new Error("settings: only HTTP services take boot settings");
3029
+ checkSettings(target, entry.settings, "settings");
3030
+ }
2981
3031
  const port = entry.port ?? target.defaultPort;
2982
3032
  if (port !== 0) {
2983
3033
  const key = `${entry.host ?? "127.0.0.1"}:${port}`;
@@ -3013,7 +3063,7 @@ async function preflightFleet(config, options) {
3013
3063
  break;
3014
3064
  }
3015
3065
  }
3016
- targets.set(name, { ...target, packageVersion: packageVersion ?? "3.6.0" });
3066
+ targets.set(name, { ...target, packageVersion: packageVersion ?? "3.8.0" });
3017
3067
  } catch (error) {
3018
3068
  throw new Error(`service ${name}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
3019
3069
  }
@@ -3239,7 +3289,11 @@ var serve = async (api, options = {}) => {
3239
3289
  // ../../adapters/node/dist/listen.js
3240
3290
  var listen = async (api, options = {}) => {
3241
3291
  const host = options.host ?? "127.0.0.1";
3242
- const server = await serve(api, { port: options.port ?? 0, host });
3292
+ const server = await serve(api, {
3293
+ port: options.port ?? 0,
3294
+ host,
3295
+ ...options.maxBodyBytes !== void 0 ? { maxBodyBytes: options.maxBodyBytes } : {}
3296
+ });
3243
3297
  const address = server.address();
3244
3298
  const port = typeof address === "object" && address !== null ? address.port : options.port ?? 0;
3245
3299
  const shown = host.includes(":") ? `[${host}]` : host;
@@ -3333,6 +3387,7 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
3333
3387
  options.onLog?.(entry2);
3334
3388
  },
3335
3389
  ...entry.adminControls !== void 0 ? { adminControls: entry.adminControls } : {},
3390
+ ...entry.settings !== void 0 ? { settings: checkSettings(target, entry.settings, "settings") } : {},
3336
3391
  ...entry.fixtures !== void 0 ? { fixtures: entry.fixtures } : {},
3337
3392
  ...(advertisedHttpBase(entry) ?? entry.baseUrl) !== void 0 ? { baseUrl: advertisedHttpBase(entry) ?? entry.baseUrl } : {}
3338
3393
  });
@@ -3351,6 +3406,8 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
3351
3406
  ].some((method) => typeof created[method] !== "function"))
3352
3407
  throw new Error("HTTP target does not implement the fleet runtime protocol; build/install matching supported service versions");
3353
3408
  const runtime = created;
3409
+ if (entry.idBase !== void 0)
3410
+ runtime.setIdBase?.(entry.idBase);
3354
3411
  const unobserve = runtime.webhooks?.observeDeliveries(observer.delivery);
3355
3412
  if (options.outbound && runtime.webhooks)
3356
3413
  runtime.webhooks.setTransportPolicy(ownedTransport(options.outbound));
@@ -3435,7 +3492,8 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
3435
3492
  try {
3436
3493
  server = await listen(api, {
3437
3494
  port: entry.port ?? target.defaultPort,
3438
- host: entry.host ?? "127.0.0.1"
3495
+ host: entry.host ?? "127.0.0.1",
3496
+ ...target.maxBodyBytes !== void 0 ? { maxBodyBytes: target.maxBodyBytes } : {}
3439
3497
  });
3440
3498
  target.listening?.(server);
3441
3499
  } catch (error) {
@@ -3949,6 +4007,7 @@ async function startFleet(config, options) {
3949
4007
  ...entry,
3950
4008
  adminPrefix: resolveAdminPrefix(entry.adminPrefix ?? adminPrefix),
3951
4009
  ...adminKey !== void 0 ? { adminKey } : {},
4010
+ ...(entry.idBase ?? config.idBase) !== void 0 ? { idBase: entry.idBase ?? config.idBase } : {},
3952
4011
  // Fleet-wide controls, with the service's own keys replacing them.
3953
4012
  ...config.adminControls !== void 0 || entry.adminControls !== void 0 ? { adminControls: { ...config.adminControls, ...entry.adminControls } } : {}
3954
4013
  }, options, (namespace, request) => management?.admit(namespace, request), (namespace) => management?.generation(namespace), (namespace, request) => management?.privateRequest(namespace, request) ?? false, {
@@ -4633,6 +4692,16 @@ var COMMON_SERVE_OPTIONS = {
4633
4692
  value: "<seed>",
4634
4693
  description: "Seed for every random choice (env MOCKINGBIRD_SEED)"
4635
4694
  },
4695
+ "id-base": {
4696
+ type: "string",
4697
+ value: "<n|boot>",
4698
+ description: "Start generated ids at this base so a restart does not reissue the last boot's ids; boot derives one from the clock (env MOCKINGBIRD_ID_BASE; default 0, the same ids every boot)"
4699
+ },
4700
+ settings: {
4701
+ type: "string",
4702
+ value: "<json|file>",
4703
+ description: "Service settings to boot with, as JSON or a JSON file; every namespace starts from them (env MOCKINGBIRD_SETTINGS; only for services that declare settings)"
4704
+ },
4636
4705
  "admin-controls": {
4637
4706
  type: "string",
4638
4707
  value: "<json|file>",
@@ -4727,19 +4796,40 @@ var readJsonFile = async (path, label) => {
4727
4796
  }
4728
4797
  };
4729
4798
  var isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4799
+ var readJsonOption = async (source, label) => {
4800
+ const trimmed = source.trim();
4801
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
4802
+ try {
4803
+ return JSON.parse(trimmed);
4804
+ } catch {
4805
+ throw new Error(`${label}: not valid JSON`);
4806
+ }
4807
+ }
4808
+ return readJsonFile(source, label);
4809
+ };
4810
+ var resolveServeIdBase = (values, env = process.env) => {
4811
+ const source = asString(values["id-base"]) ?? env.MOCKINGBIRD_ID_BASE;
4812
+ if (source === void 0 || source.trim() === "")
4813
+ return void 0;
4814
+ normalizeIdBase(source.trim(), "--id-base");
4815
+ return source.trim();
4816
+ };
4817
+ var resolveServeSettings = async (target, values, env = process.env) => {
4818
+ const flag = asString(values.settings);
4819
+ const source = flag ?? env.MOCKINGBIRD_SETTINGS;
4820
+ if (source === void 0 || source.trim() === "")
4821
+ return void 0;
4822
+ const label = flag !== void 0 ? "--settings" : "MOCKINGBIRD_SETTINGS";
4823
+ const parsed = await readJsonOption(source, label);
4824
+ if (!isObject2(parsed))
4825
+ throw new Error(`${label}: expected a JSON object`);
4826
+ return checkSettings(target, parsed, label);
4827
+ };
4730
4828
  var resolveServeControls = async (values, env = process.env, base) => {
4731
4829
  const source = asString(values["admin-controls"]) ?? env.MOCKINGBIRD_ADMIN_CONTROLS;
4732
4830
  let document = isObject2(base) ? { ...base } : {};
4733
4831
  if (source !== void 0 && source.trim() !== "") {
4734
- let parsed;
4735
- if (source.trim().startsWith("{")) {
4736
- try {
4737
- parsed = JSON.parse(source);
4738
- } catch {
4739
- throw new Error("--admin-controls: not valid JSON");
4740
- }
4741
- } else
4742
- parsed = await readJsonFile(source, "--admin-controls");
4832
+ const parsed = source.trim().startsWith("{") ? await readJsonOption(source, "--admin-controls") : await readJsonFile(source, "--admin-controls");
4743
4833
  if (!isObject2(parsed))
4744
4834
  throw new Error("--admin-controls: expected a JSON object");
4745
4835
  document = { ...document, ...parsed };
@@ -4789,9 +4879,16 @@ var start = async (target, values, config) => {
4789
4879
  adminKey: config.adminKey,
4790
4880
  seed: config.seed,
4791
4881
  onLog: formatLog(config.log),
4882
+ ...config.settings !== void 0 ? { settings: config.settings } : {},
4792
4883
  ...config.adminControls !== void 0 ? { adminControls: config.adminControls } : {}
4793
4884
  });
4794
- const listening = await listen(runtime, { port: config.port, host: config.host });
4885
+ if (config.idBase !== void 0)
4886
+ runtime.setIdBase?.(config.idBase);
4887
+ const listening = await listen(runtime, {
4888
+ port: config.port,
4889
+ host: config.host,
4890
+ ...target.maxBodyBytes !== void 0 ? { maxBodyBytes: target.maxBodyBytes } : {}
4891
+ });
4795
4892
  target.listening?.(listening);
4796
4893
  console.log(`${target.name} emulator listening on ${listening.url}`);
4797
4894
  console.log(`${target.name} health: GET ${listening.url}${adminPrefix}/health`);
@@ -4836,6 +4933,11 @@ var serveCommand = (target) => ({
4836
4933
  const adminControls = await resolveServeControls(values, process.env, config.adminControls);
4837
4934
  if (adminControls !== void 0)
4838
4935
  config.adminControls = adminControls;
4936
+ if (values.settings !== void 0)
4937
+ throw new Error("--settings serves a single service; give each service its own `settings` in the config");
4938
+ const idBase = config.idBase ?? resolveServeIdBase(values);
4939
+ if (idBase !== void 0)
4940
+ config.idBase = idBase;
4839
4941
  const fleet = await startFleet(config, {
4840
4942
  load: (name) => loadTarget(name, target),
4841
4943
  onLog: formatLog(config.log ?? log),
@@ -4865,8 +4967,12 @@ var serveCommand = (target) => ({
4865
4967
  let listening;
4866
4968
  try {
4867
4969
  const adminControls = await resolveServeControls(values);
4970
+ const idBase = resolveServeIdBase(values);
4971
+ const settings = await resolveServeSettings(target, values);
4868
4972
  listening = await start(target, values, {
4869
4973
  ...adminControls !== void 0 ? { adminControls } : {},
4974
+ ...idBase !== void 0 ? { idBase } : {},
4975
+ ...settings !== void 0 ? { settings } : {},
4870
4976
  adminPrefix: resolveAdminPrefix(asString(values["admin-prefix"]) ?? process.env.MOCKINGBIRD_ADMIN_PREFIX),
4871
4977
  port: port === void 0 ? target.defaultPort : Number.parseInt(port, 10),
4872
4978
  host: asString(values.host) ?? "127.0.0.1",
@@ -4901,6 +5007,20 @@ var createServer2 = async (options = {}) => {
4901
5007
  };
4902
5008
  };
4903
5009
  var text = (value) => typeof value === "string" ? value : void 0;
5010
+ var loadCatalogFile = (path, mode = "merge") => {
5011
+ let raw;
5012
+ try {
5013
+ raw = readFileSync(path, "utf8");
5014
+ } catch {
5015
+ throw new Error(`wholescripts catalog not found: ${path}`);
5016
+ }
5017
+ try {
5018
+ const parsed = parseCatalogDocument(JSON.parse(raw));
5019
+ return mode === "replace" ? { products: [], medPaxPills: [], privateLabelCartons: [], ...parsed } : mergeCatalog(DEFAULT_CATALOG, parsed);
5020
+ } catch (error) {
5021
+ throw new Error(`wholescripts catalog ${path}: ${error.message}`);
5022
+ }
5023
+ };
4904
5024
  var serveTarget = {
4905
5025
  name: "wholescripts",
4906
5026
  defaultPort: DEFAULT_PORT,
@@ -4919,6 +5039,16 @@ var serveTarget = {
4919
5039
  type: "string",
4920
5040
  value: "<ms:Status,Status,\u2026>",
4921
5041
  description: 'Walk every new order along a path, e.g. "2000:Processing,Complete"'
5042
+ },
5043
+ catalog: {
5044
+ type: "string",
5045
+ value: "<file.json>",
5046
+ description: "Catalog rows every namespace starts with and resets to: {products?, medPaxPills?, privateLabelCartons?} or a bare array of products, merged onto the default catalog by SKU"
5047
+ },
5048
+ "catalog-mode": {
5049
+ type: "string",
5050
+ value: "<merge|replace>",
5051
+ description: "With --catalog: merge onto the default catalog (default) or use the file as the whole catalog"
4922
5052
  }
4923
5053
  },
4924
5054
  create: (values, common) => {
@@ -4927,8 +5057,15 @@ var serveTarget = {
4927
5057
  const auto = text(values["auto-advance"]);
4928
5058
  const plan = auto ? /^(\d+):(.+)$/.exec(auto) : null;
4929
5059
  if (auto && !plan) throw new Error('--auto-advance must look like "2000:Processing,Complete"');
5060
+ const catalogPath = text(values.catalog);
5061
+ const mode = text(values["catalog-mode"]);
5062
+ if (mode !== void 0 && mode !== "merge" && mode !== "replace")
5063
+ throw new Error('--catalog-mode must be "merge" or "replace"');
5064
+ if (mode !== void 0 && catalogPath === void 0)
5065
+ throw new Error("--catalog-mode needs --catalog");
4930
5066
  return createRuntime({
4931
5067
  tickMs: 100,
5068
+ ...catalogPath ? { catalog: loadCatalogFile(catalogPath, mode) } : {},
4932
5069
  settings: {
4933
5070
  ...username && password ? { accounts: [{ username, password }] } : {},
4934
5071
  ...plan ? {
@@ -4956,6 +5093,7 @@ export {
4956
5093
  serveCommand,
4957
5094
  DEFAULT_PORT,
4958
5095
  createServer2 as createServer,
5096
+ loadCatalogFile,
4959
5097
  serveTarget
4960
5098
  };
4961
- //# sourceMappingURL=chunk-6T4MXYKN.js.map
5099
+ //# sourceMappingURL=chunk-NS2QQO5Y.js.map