@crvouga/mockingbird-service-wholescripts 3.6.0 → 3.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.
@@ -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.7.0" });
3017
3067
  } catch (error) {
3018
3068
  throw new Error(`service ${name}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
3019
3069
  }
@@ -3333,6 +3383,7 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
3333
3383
  options.onLog?.(entry2);
3334
3384
  },
3335
3385
  ...entry.adminControls !== void 0 ? { adminControls: entry.adminControls } : {},
3386
+ ...entry.settings !== void 0 ? { settings: checkSettings(target, entry.settings, "settings") } : {},
3336
3387
  ...entry.fixtures !== void 0 ? { fixtures: entry.fixtures } : {},
3337
3388
  ...(advertisedHttpBase(entry) ?? entry.baseUrl) !== void 0 ? { baseUrl: advertisedHttpBase(entry) ?? entry.baseUrl } : {}
3338
3389
  });
@@ -3351,6 +3402,8 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
3351
3402
  ].some((method) => typeof created[method] !== "function"))
3352
3403
  throw new Error("HTTP target does not implement the fleet runtime protocol; build/install matching supported service versions");
3353
3404
  const runtime = created;
3405
+ if (entry.idBase !== void 0)
3406
+ runtime.setIdBase?.(entry.idBase);
3354
3407
  const unobserve = runtime.webhooks?.observeDeliveries(observer.delivery);
3355
3408
  if (options.outbound && runtime.webhooks)
3356
3409
  runtime.webhooks.setTransportPolicy(ownedTransport(options.outbound));
@@ -3949,6 +4002,7 @@ async function startFleet(config, options) {
3949
4002
  ...entry,
3950
4003
  adminPrefix: resolveAdminPrefix(entry.adminPrefix ?? adminPrefix),
3951
4004
  ...adminKey !== void 0 ? { adminKey } : {},
4005
+ ...(entry.idBase ?? config.idBase) !== void 0 ? { idBase: entry.idBase ?? config.idBase } : {},
3952
4006
  // Fleet-wide controls, with the service's own keys replacing them.
3953
4007
  ...config.adminControls !== void 0 || entry.adminControls !== void 0 ? { adminControls: { ...config.adminControls, ...entry.adminControls } } : {}
3954
4008
  }, options, (namespace, request) => management?.admit(namespace, request), (namespace) => management?.generation(namespace), (namespace, request) => management?.privateRequest(namespace, request) ?? false, {
@@ -4633,6 +4687,16 @@ var COMMON_SERVE_OPTIONS = {
4633
4687
  value: "<seed>",
4634
4688
  description: "Seed for every random choice (env MOCKINGBIRD_SEED)"
4635
4689
  },
4690
+ "id-base": {
4691
+ type: "string",
4692
+ value: "<n|boot>",
4693
+ 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)"
4694
+ },
4695
+ settings: {
4696
+ type: "string",
4697
+ value: "<json|file>",
4698
+ 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)"
4699
+ },
4636
4700
  "admin-controls": {
4637
4701
  type: "string",
4638
4702
  value: "<json|file>",
@@ -4727,19 +4791,40 @@ var readJsonFile = async (path, label) => {
4727
4791
  }
4728
4792
  };
4729
4793
  var isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4794
+ var readJsonOption = async (source, label) => {
4795
+ const trimmed = source.trim();
4796
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
4797
+ try {
4798
+ return JSON.parse(trimmed);
4799
+ } catch {
4800
+ throw new Error(`${label}: not valid JSON`);
4801
+ }
4802
+ }
4803
+ return readJsonFile(source, label);
4804
+ };
4805
+ var resolveServeIdBase = (values, env = process.env) => {
4806
+ const source = asString(values["id-base"]) ?? env.MOCKINGBIRD_ID_BASE;
4807
+ if (source === void 0 || source.trim() === "")
4808
+ return void 0;
4809
+ normalizeIdBase(source.trim(), "--id-base");
4810
+ return source.trim();
4811
+ };
4812
+ var resolveServeSettings = async (target, values, env = process.env) => {
4813
+ const flag = asString(values.settings);
4814
+ const source = flag ?? env.MOCKINGBIRD_SETTINGS;
4815
+ if (source === void 0 || source.trim() === "")
4816
+ return void 0;
4817
+ const label = flag !== void 0 ? "--settings" : "MOCKINGBIRD_SETTINGS";
4818
+ const parsed = await readJsonOption(source, label);
4819
+ if (!isObject2(parsed))
4820
+ throw new Error(`${label}: expected a JSON object`);
4821
+ return checkSettings(target, parsed, label);
4822
+ };
4730
4823
  var resolveServeControls = async (values, env = process.env, base) => {
4731
4824
  const source = asString(values["admin-controls"]) ?? env.MOCKINGBIRD_ADMIN_CONTROLS;
4732
4825
  let document = isObject2(base) ? { ...base } : {};
4733
4826
  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");
4827
+ const parsed = source.trim().startsWith("{") ? await readJsonOption(source, "--admin-controls") : await readJsonFile(source, "--admin-controls");
4743
4828
  if (!isObject2(parsed))
4744
4829
  throw new Error("--admin-controls: expected a JSON object");
4745
4830
  document = { ...document, ...parsed };
@@ -4789,8 +4874,11 @@ var start = async (target, values, config) => {
4789
4874
  adminKey: config.adminKey,
4790
4875
  seed: config.seed,
4791
4876
  onLog: formatLog(config.log),
4877
+ ...config.settings !== void 0 ? { settings: config.settings } : {},
4792
4878
  ...config.adminControls !== void 0 ? { adminControls: config.adminControls } : {}
4793
4879
  });
4880
+ if (config.idBase !== void 0)
4881
+ runtime.setIdBase?.(config.idBase);
4794
4882
  const listening = await listen(runtime, { port: config.port, host: config.host });
4795
4883
  target.listening?.(listening);
4796
4884
  console.log(`${target.name} emulator listening on ${listening.url}`);
@@ -4836,6 +4924,11 @@ var serveCommand = (target) => ({
4836
4924
  const adminControls = await resolveServeControls(values, process.env, config.adminControls);
4837
4925
  if (adminControls !== void 0)
4838
4926
  config.adminControls = adminControls;
4927
+ if (values.settings !== void 0)
4928
+ throw new Error("--settings serves a single service; give each service its own `settings` in the config");
4929
+ const idBase = config.idBase ?? resolveServeIdBase(values);
4930
+ if (idBase !== void 0)
4931
+ config.idBase = idBase;
4839
4932
  const fleet = await startFleet(config, {
4840
4933
  load: (name) => loadTarget(name, target),
4841
4934
  onLog: formatLog(config.log ?? log),
@@ -4865,8 +4958,12 @@ var serveCommand = (target) => ({
4865
4958
  let listening;
4866
4959
  try {
4867
4960
  const adminControls = await resolveServeControls(values);
4961
+ const idBase = resolveServeIdBase(values);
4962
+ const settings = await resolveServeSettings(target, values);
4868
4963
  listening = await start(target, values, {
4869
4964
  ...adminControls !== void 0 ? { adminControls } : {},
4965
+ ...idBase !== void 0 ? { idBase } : {},
4966
+ ...settings !== void 0 ? { settings } : {},
4870
4967
  adminPrefix: resolveAdminPrefix(asString(values["admin-prefix"]) ?? process.env.MOCKINGBIRD_ADMIN_PREFIX),
4871
4968
  port: port === void 0 ? target.defaultPort : Number.parseInt(port, 10),
4872
4969
  host: asString(values.host) ?? "127.0.0.1",
@@ -4901,6 +4998,20 @@ var createServer2 = async (options = {}) => {
4901
4998
  };
4902
4999
  };
4903
5000
  var text = (value) => typeof value === "string" ? value : void 0;
5001
+ var loadCatalogFile = (path, mode = "merge") => {
5002
+ let raw;
5003
+ try {
5004
+ raw = readFileSync(path, "utf8");
5005
+ } catch {
5006
+ throw new Error(`wholescripts catalog not found: ${path}`);
5007
+ }
5008
+ try {
5009
+ const parsed = parseCatalogDocument(JSON.parse(raw));
5010
+ return mode === "replace" ? { products: [], medPaxPills: [], privateLabelCartons: [], ...parsed } : mergeCatalog(DEFAULT_CATALOG, parsed);
5011
+ } catch (error) {
5012
+ throw new Error(`wholescripts catalog ${path}: ${error.message}`);
5013
+ }
5014
+ };
4904
5015
  var serveTarget = {
4905
5016
  name: "wholescripts",
4906
5017
  defaultPort: DEFAULT_PORT,
@@ -4919,6 +5030,16 @@ var serveTarget = {
4919
5030
  type: "string",
4920
5031
  value: "<ms:Status,Status,\u2026>",
4921
5032
  description: 'Walk every new order along a path, e.g. "2000:Processing,Complete"'
5033
+ },
5034
+ catalog: {
5035
+ type: "string",
5036
+ value: "<file.json>",
5037
+ 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"
5038
+ },
5039
+ "catalog-mode": {
5040
+ type: "string",
5041
+ value: "<merge|replace>",
5042
+ description: "With --catalog: merge onto the default catalog (default) or use the file as the whole catalog"
4922
5043
  }
4923
5044
  },
4924
5045
  create: (values, common) => {
@@ -4927,8 +5048,15 @@ var serveTarget = {
4927
5048
  const auto = text(values["auto-advance"]);
4928
5049
  const plan = auto ? /^(\d+):(.+)$/.exec(auto) : null;
4929
5050
  if (auto && !plan) throw new Error('--auto-advance must look like "2000:Processing,Complete"');
5051
+ const catalogPath = text(values.catalog);
5052
+ const mode = text(values["catalog-mode"]);
5053
+ if (mode !== void 0 && mode !== "merge" && mode !== "replace")
5054
+ throw new Error('--catalog-mode must be "merge" or "replace"');
5055
+ if (mode !== void 0 && catalogPath === void 0)
5056
+ throw new Error("--catalog-mode needs --catalog");
4930
5057
  return createRuntime({
4931
5058
  tickMs: 100,
5059
+ ...catalogPath ? { catalog: loadCatalogFile(catalogPath, mode) } : {},
4932
5060
  settings: {
4933
5061
  ...username && password ? { accounts: [{ username, password }] } : {},
4934
5062
  ...plan ? {
@@ -4956,6 +5084,7 @@ export {
4956
5084
  serveCommand,
4957
5085
  DEFAULT_PORT,
4958
5086
  createServer2 as createServer,
5087
+ loadCatalogFile,
4959
5088
  serveTarget
4960
5089
  };
4961
- //# sourceMappingURL=chunk-6T4MXYKN.js.map
5090
+ //# sourceMappingURL=chunk-R57VJDWN.js.map