@crvouga/mockingbird-service-posthog 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.
@@ -4,18 +4,32 @@ import {
4
4
  createRuntime,
5
5
  normalizeBootControls,
6
6
  normalizeControls,
7
+ normalizeIdBase,
7
8
  ownedTransport,
8
9
  parseDuration,
9
10
  parseFaultHeader,
10
11
  resolveAdminPrefix,
11
12
  restoreClock,
12
13
  specsFromState
13
- } from "./chunk-YTVQ24KD.js";
14
+ } from "./chunk-2ZTMN4SX.js";
14
15
 
15
16
  // ../../adapters/node/dist/cli.js
16
17
  import { readFile as readFile5 } from "node:fs/promises";
17
18
  import { parseArgs } from "node:util";
18
19
 
20
+ // ../../adapters/node/dist/boot-settings.js
21
+ var checkSettings = (target, settings, label) => {
22
+ if (!target.parseSettings)
23
+ throw new Error(`${label}: ${target.name} takes no boot settings`);
24
+ try {
25
+ return target.parseSettings(settings);
26
+ } catch (error) {
27
+ throw new Error(`${label}: ${error instanceof Error ? error.message : String(error)}`, {
28
+ cause: error
29
+ });
30
+ }
31
+ };
32
+
19
33
  // ../../adapters/node/dist/fleet.js
20
34
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
21
35
  import { createHash as createHash4, randomUUID as randomUUID4 } from "node:crypto";
@@ -2665,6 +2679,9 @@ var fleet_config_schema_default = {
2665
2679
  adminControls: {
2666
2680
  $ref: "#/$defs/controls"
2667
2681
  },
2682
+ idBase: {
2683
+ $ref: "#/$defs/idBase"
2684
+ },
2668
2685
  namespace: {
2669
2686
  type: "string",
2670
2687
  pattern: "^[A-Za-z0-9_.-]{1,64}$"
@@ -2754,6 +2771,12 @@ var fleet_config_schema_default = {
2754
2771
  adminControls: {
2755
2772
  $ref: "#/$defs/controls"
2756
2773
  },
2774
+ idBase: {
2775
+ $ref: "#/$defs/idBase"
2776
+ },
2777
+ settings: {
2778
+ type: "object"
2779
+ },
2757
2780
  fixtures: {
2758
2781
  type: "object"
2759
2782
  },
@@ -2800,6 +2823,18 @@ var fleet_config_schema_default = {
2800
2823
  }
2801
2824
  }
2802
2825
  },
2826
+ idBase: {
2827
+ anyOf: [
2828
+ {
2829
+ type: "integer",
2830
+ minimum: 0
2831
+ },
2832
+ {
2833
+ type: "string",
2834
+ pattern: "^(boot|[0-9]+)$"
2835
+ }
2836
+ ]
2837
+ },
2803
2838
  controls: {
2804
2839
  type: "object",
2805
2840
  additionalProperties: false,
@@ -2949,6 +2984,8 @@ async function preflightFleet(config, options) {
2949
2984
  throw new Error("protected archive policy requires a protected repository; configure its protector before startup");
2950
2985
  resolveAdminPrefix(config.adminPrefix);
2951
2986
  normalizeBootControls(config.adminControls);
2987
+ if (config.idBase !== void 0)
2988
+ normalizeIdBase(config.idBase, "idBase");
2952
2989
  const targets = /* @__PURE__ */ new Map();
2953
2990
  const ports = /* @__PURE__ */ new Set();
2954
2991
  for (const [name, entry] of Object.entries(config.services)) {
@@ -2980,6 +3017,13 @@ async function preflightFleet(config, options) {
2980
3017
  throw new Error("invalid protocol namespace selector");
2981
3018
  }
2982
3019
  }
3020
+ if (entry.idBase !== void 0)
3021
+ normalizeIdBase(entry.idBase, "idBase");
3022
+ if (entry.settings !== void 0) {
3023
+ if (protocol !== "http" || !("create" in target))
3024
+ throw new Error("settings: only HTTP services take boot settings");
3025
+ checkSettings(target, entry.settings, "settings");
3026
+ }
2983
3027
  const port = entry.port ?? target.defaultPort;
2984
3028
  if (port !== 0) {
2985
3029
  const key = `${entry.host ?? "127.0.0.1"}:${port}`;
@@ -3015,7 +3059,7 @@ async function preflightFleet(config, options) {
3015
3059
  break;
3016
3060
  }
3017
3061
  }
3018
- targets.set(name, { ...target, packageVersion: packageVersion ?? "3.6.0" });
3062
+ targets.set(name, { ...target, packageVersion: packageVersion ?? "3.8.0" });
3019
3063
  } catch (error) {
3020
3064
  throw new Error(`service ${name}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
3021
3065
  }
@@ -3241,7 +3285,11 @@ var serve = async (api, options = {}) => {
3241
3285
  // ../../adapters/node/dist/listen.js
3242
3286
  var listen = async (api, options = {}) => {
3243
3287
  const host = options.host ?? "127.0.0.1";
3244
- const server = await serve(api, { port: options.port ?? 0, host });
3288
+ const server = await serve(api, {
3289
+ port: options.port ?? 0,
3290
+ host,
3291
+ ...options.maxBodyBytes !== void 0 ? { maxBodyBytes: options.maxBodyBytes } : {}
3292
+ });
3245
3293
  const address = server.address();
3246
3294
  const port = typeof address === "object" && address !== null ? address.port : options.port ?? 0;
3247
3295
  const shown = host.includes(":") ? `[${host}]` : host;
@@ -3335,6 +3383,7 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
3335
3383
  options.onLog?.(entry2);
3336
3384
  },
3337
3385
  ...entry.adminControls !== void 0 ? { adminControls: entry.adminControls } : {},
3386
+ ...entry.settings !== void 0 ? { settings: checkSettings(target, entry.settings, "settings") } : {},
3338
3387
  ...entry.fixtures !== void 0 ? { fixtures: entry.fixtures } : {},
3339
3388
  ...(advertisedHttpBase(entry) ?? entry.baseUrl) !== void 0 ? { baseUrl: advertisedHttpBase(entry) ?? entry.baseUrl } : {}
3340
3389
  });
@@ -3353,6 +3402,8 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
3353
3402
  ].some((method) => typeof created[method] !== "function"))
3354
3403
  throw new Error("HTTP target does not implement the fleet runtime protocol; build/install matching supported service versions");
3355
3404
  const runtime = created;
3405
+ if (entry.idBase !== void 0)
3406
+ runtime.setIdBase?.(entry.idBase);
3356
3407
  const unobserve = runtime.webhooks?.observeDeliveries(observer.delivery);
3357
3408
  if (options.outbound && runtime.webhooks)
3358
3409
  runtime.webhooks.setTransportPolicy(ownedTransport(options.outbound));
@@ -3437,7 +3488,8 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
3437
3488
  try {
3438
3489
  server = await listen(api, {
3439
3490
  port: entry.port ?? target.defaultPort,
3440
- host: entry.host ?? "127.0.0.1"
3491
+ host: entry.host ?? "127.0.0.1",
3492
+ ...target.maxBodyBytes !== void 0 ? { maxBodyBytes: target.maxBodyBytes } : {}
3441
3493
  });
3442
3494
  target.listening?.(server);
3443
3495
  } catch (error) {
@@ -3951,6 +4003,7 @@ async function startFleet(config, options) {
3951
4003
  ...entry,
3952
4004
  adminPrefix: resolveAdminPrefix(entry.adminPrefix ?? adminPrefix),
3953
4005
  ...adminKey !== void 0 ? { adminKey } : {},
4006
+ ...(entry.idBase ?? config.idBase) !== void 0 ? { idBase: entry.idBase ?? config.idBase } : {},
3954
4007
  // Fleet-wide controls, with the service's own keys replacing them.
3955
4008
  ...config.adminControls !== void 0 || entry.adminControls !== void 0 ? { adminControls: { ...config.adminControls, ...entry.adminControls } } : {}
3956
4009
  }, options, (namespace, request) => management?.admit(namespace, request), (namespace) => management?.generation(namespace), (namespace, request) => management?.privateRequest(namespace, request) ?? false, {
@@ -4635,6 +4688,16 @@ var COMMON_SERVE_OPTIONS = {
4635
4688
  value: "<seed>",
4636
4689
  description: "Seed for every random choice (env MOCKINGBIRD_SEED)"
4637
4690
  },
4691
+ "id-base": {
4692
+ type: "string",
4693
+ value: "<n|boot>",
4694
+ 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)"
4695
+ },
4696
+ settings: {
4697
+ type: "string",
4698
+ value: "<json|file>",
4699
+ 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)"
4700
+ },
4638
4701
  "admin-controls": {
4639
4702
  type: "string",
4640
4703
  value: "<json|file>",
@@ -4729,19 +4792,40 @@ var readJsonFile = async (path, label) => {
4729
4792
  }
4730
4793
  };
4731
4794
  var isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4795
+ var readJsonOption = async (source, label) => {
4796
+ const trimmed = source.trim();
4797
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
4798
+ try {
4799
+ return JSON.parse(trimmed);
4800
+ } catch {
4801
+ throw new Error(`${label}: not valid JSON`);
4802
+ }
4803
+ }
4804
+ return readJsonFile(source, label);
4805
+ };
4806
+ var resolveServeIdBase = (values, env = process.env) => {
4807
+ const source = asString(values["id-base"]) ?? env.MOCKINGBIRD_ID_BASE;
4808
+ if (source === void 0 || source.trim() === "")
4809
+ return void 0;
4810
+ normalizeIdBase(source.trim(), "--id-base");
4811
+ return source.trim();
4812
+ };
4813
+ var resolveServeSettings = async (target, values, env = process.env) => {
4814
+ const flag = asString(values.settings);
4815
+ const source = flag ?? env.MOCKINGBIRD_SETTINGS;
4816
+ if (source === void 0 || source.trim() === "")
4817
+ return void 0;
4818
+ const label = flag !== void 0 ? "--settings" : "MOCKINGBIRD_SETTINGS";
4819
+ const parsed = await readJsonOption(source, label);
4820
+ if (!isObject2(parsed))
4821
+ throw new Error(`${label}: expected a JSON object`);
4822
+ return checkSettings(target, parsed, label);
4823
+ };
4732
4824
  var resolveServeControls = async (values, env = process.env, base) => {
4733
4825
  const source = asString(values["admin-controls"]) ?? env.MOCKINGBIRD_ADMIN_CONTROLS;
4734
4826
  let document = isObject2(base) ? { ...base } : {};
4735
4827
  if (source !== void 0 && source.trim() !== "") {
4736
- let parsed;
4737
- if (source.trim().startsWith("{")) {
4738
- try {
4739
- parsed = JSON.parse(source);
4740
- } catch {
4741
- throw new Error("--admin-controls: not valid JSON");
4742
- }
4743
- } else
4744
- parsed = await readJsonFile(source, "--admin-controls");
4828
+ const parsed = source.trim().startsWith("{") ? await readJsonOption(source, "--admin-controls") : await readJsonFile(source, "--admin-controls");
4745
4829
  if (!isObject2(parsed))
4746
4830
  throw new Error("--admin-controls: expected a JSON object");
4747
4831
  document = { ...document, ...parsed };
@@ -4791,9 +4875,16 @@ var start = async (target, values, config) => {
4791
4875
  adminKey: config.adminKey,
4792
4876
  seed: config.seed,
4793
4877
  onLog: formatLog(config.log),
4878
+ ...config.settings !== void 0 ? { settings: config.settings } : {},
4794
4879
  ...config.adminControls !== void 0 ? { adminControls: config.adminControls } : {}
4795
4880
  });
4796
- const listening = await listen(runtime, { port: config.port, host: config.host });
4881
+ if (config.idBase !== void 0)
4882
+ runtime.setIdBase?.(config.idBase);
4883
+ const listening = await listen(runtime, {
4884
+ port: config.port,
4885
+ host: config.host,
4886
+ ...target.maxBodyBytes !== void 0 ? { maxBodyBytes: target.maxBodyBytes } : {}
4887
+ });
4797
4888
  target.listening?.(listening);
4798
4889
  console.log(`${target.name} emulator listening on ${listening.url}`);
4799
4890
  console.log(`${target.name} health: GET ${listening.url}${adminPrefix}/health`);
@@ -4838,6 +4929,11 @@ var serveCommand = (target) => ({
4838
4929
  const adminControls = await resolveServeControls(values, process.env, config.adminControls);
4839
4930
  if (adminControls !== void 0)
4840
4931
  config.adminControls = adminControls;
4932
+ if (values.settings !== void 0)
4933
+ throw new Error("--settings serves a single service; give each service its own `settings` in the config");
4934
+ const idBase = config.idBase ?? resolveServeIdBase(values);
4935
+ if (idBase !== void 0)
4936
+ config.idBase = idBase;
4841
4937
  const fleet = await startFleet(config, {
4842
4938
  load: (name) => loadTarget(name, target),
4843
4939
  onLog: formatLog(config.log ?? log),
@@ -4867,8 +4963,12 @@ var serveCommand = (target) => ({
4867
4963
  let listening;
4868
4964
  try {
4869
4965
  const adminControls = await resolveServeControls(values);
4966
+ const idBase = resolveServeIdBase(values);
4967
+ const settings = await resolveServeSettings(target, values);
4870
4968
  listening = await start(target, values, {
4871
4969
  ...adminControls !== void 0 ? { adminControls } : {},
4970
+ ...idBase !== void 0 ? { idBase } : {},
4971
+ ...settings !== void 0 ? { settings } : {},
4872
4972
  adminPrefix: resolveAdminPrefix(asString(values["admin-prefix"]) ?? process.env.MOCKINGBIRD_ADMIN_PREFIX),
4873
4973
  port: port === void 0 ? target.defaultPort : Number.parseInt(port, 10),
4874
4974
  host: asString(values.host) ?? "127.0.0.1",
@@ -4938,4 +5038,4 @@ export {
4938
5038
  createServer2 as createServer,
4939
5039
  serveTarget
4940
5040
  };
4941
- //# sourceMappingURL=chunk-GW65C4DF.js.map
5041
+ //# sourceMappingURL=chunk-OE5OZXOW.js.map