@crvouga/mockingbird-service-redis 2.5.0 → 2.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.
- package/dist/{chunk-LHUY4G3I.js → chunk-OJI5WTP5.js} +12 -1
- package/dist/chunk-OJI5WTP5.js.map +7 -0
- package/dist/cli.js +314 -50
- package/dist/cli.js.map +4 -4
- package/dist/server.d.ts +18 -0
- package/dist/server.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-LHUY4G3I.js.map +0 -7
package/dist/cli.js
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
createClock,
|
|
5
5
|
normalizeBootControls,
|
|
6
6
|
normalizeControls,
|
|
7
|
+
normalizeIdBase,
|
|
7
8
|
ownedTransport,
|
|
8
9
|
parseDuration,
|
|
9
10
|
parseFaultHeader,
|
|
@@ -11,18 +12,28 @@ import {
|
|
|
11
12
|
restoreClock,
|
|
12
13
|
serve,
|
|
13
14
|
serveTarget
|
|
14
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-OJI5WTP5.js";
|
|
15
16
|
import {
|
|
16
17
|
createRedis
|
|
17
18
|
} from "./chunk-Z3XQGVTX.js";
|
|
18
19
|
|
|
19
20
|
// ../../adapters/node/dist/cli.js
|
|
20
21
|
import { readFile as readFile5 } from "node:fs/promises";
|
|
21
|
-
import { createRequire as createRequire3 } from "node:module";
|
|
22
|
-
import { resolve as resolve3 } from "node:path";
|
|
23
|
-
import { pathToFileURL } from "node:url";
|
|
24
22
|
import { parseArgs } from "node:util";
|
|
25
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
|
+
|
|
26
37
|
// ../../adapters/node/dist/fleet.js
|
|
27
38
|
import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
|
|
28
39
|
import { createHash as createHash4, randomUUID as randomUUID4 } from "node:crypto";
|
|
@@ -34,6 +45,31 @@ import { createHash as createHash3, randomBytes, randomUUID as randomUUID3 } fro
|
|
|
34
45
|
|
|
35
46
|
// ../../adapters/node/dist/fleet-archive.js
|
|
36
47
|
import { createHash } from "node:crypto";
|
|
48
|
+
function validateCompatibilityTags(tags) {
|
|
49
|
+
if (!tags || typeof tags !== "object" || Array.isArray(tags))
|
|
50
|
+
throw new Error("invalid archive compatibility tags");
|
|
51
|
+
const entries = Object.entries(tags);
|
|
52
|
+
if (entries.length > 32 || entries.some(([key, value]) => !/^[A-Za-z0-9_.-]{1,64}$/.test(key) || typeof value !== "string" || value.length < 1 || value.length > 256 || [...value].some((character) => character.charCodeAt(0) < 32 || character.charCodeAt(0) === 127)))
|
|
53
|
+
throw new Error("invalid archive compatibility tags");
|
|
54
|
+
}
|
|
55
|
+
function assertCompatibilityTags(tags, policy) {
|
|
56
|
+
if (tags !== void 0)
|
|
57
|
+
validateCompatibilityTags(tags);
|
|
58
|
+
if (!policy)
|
|
59
|
+
return;
|
|
60
|
+
validateCompatibilityTags(policy.tags);
|
|
61
|
+
if (policy.missing !== void 0 && !["allow", "reject"].includes(policy.missing))
|
|
62
|
+
throw new Error("invalid missing compatibility policy");
|
|
63
|
+
if (tags === void 0 && policy.missing === "allow")
|
|
64
|
+
return;
|
|
65
|
+
if (canonical(tags) !== canonical(policy.tags))
|
|
66
|
+
throw new Error("application archive compatibility mismatch");
|
|
67
|
+
}
|
|
68
|
+
function assertArchiveCompatibility(input, policy) {
|
|
69
|
+
const archive = validateArchive(input);
|
|
70
|
+
assertCompatibilityTags(archive.compatibility, policy);
|
|
71
|
+
return archive;
|
|
72
|
+
}
|
|
37
73
|
var canonical = (value) => {
|
|
38
74
|
if (Array.isArray(value))
|
|
39
75
|
return `[${value.map(canonical).join(",")}]`;
|
|
@@ -53,6 +89,8 @@ function validateArchive(value) {
|
|
|
53
89
|
const { sha256, ...content } = doc;
|
|
54
90
|
if (doc.format !== "mockingbird.fleet/v1" || typeof doc.sourceId !== "string" || typeof doc.createdAt !== "string" || !doc.namespaces || typeof doc.namespaces !== "object" || Array.isArray(doc.namespaces) || sha256 !== digest(content))
|
|
55
91
|
throw new Error("unsupported or corrupt fleet archive");
|
|
92
|
+
if (doc.compatibility !== void 0)
|
|
93
|
+
validateCompatibilityTags(doc.compatibility);
|
|
56
94
|
for (const [namespace, entry] of Object.entries(doc.namespaces)) {
|
|
57
95
|
if (!/^[A-Za-z0-9_.-]{1,64}$/.test(namespace) || !entry || !Number.isFinite(entry.clock?.now) || typeof entry.clock?.frozen !== "boolean" || !entry.services || !Object.keys(entry.services).length)
|
|
58
96
|
throw new Error("invalid archive namespace");
|
|
@@ -68,6 +106,7 @@ function inspectArchive(input) {
|
|
|
68
106
|
format: archive.format,
|
|
69
107
|
createdAt: archive.createdAt,
|
|
70
108
|
sha256: archive.sha256,
|
|
109
|
+
...archive.compatibility ? { compatibility: archive.compatibility } : {},
|
|
71
110
|
namespaces: Object.fromEntries(Object.entries(archive.namespaces).map(([name, ns]) => [
|
|
72
111
|
name,
|
|
73
112
|
{
|
|
@@ -710,11 +749,28 @@ var ingressSupport = {
|
|
|
710
749
|
streaming: true,
|
|
711
750
|
signing: "vendor request signing is not used; webhook signatures remain payload based"
|
|
712
751
|
},
|
|
752
|
+
cognito: {
|
|
753
|
+
publicOrigin: true,
|
|
754
|
+
pathPrefix: true,
|
|
755
|
+
namespaces: "path-or-header",
|
|
756
|
+
streaming: true,
|
|
757
|
+
signing: "JWT issuer and JWKS use the configured public namespace; SigV4 verification is not modelled"
|
|
758
|
+
},
|
|
759
|
+
medplum: {
|
|
760
|
+
publicOrigin: true,
|
|
761
|
+
pathPrefix: true,
|
|
762
|
+
namespaces: "path-or-header",
|
|
763
|
+
streaming: true,
|
|
764
|
+
signing: "FHIR, OAuth and storage URLs use the configured public namespace"
|
|
765
|
+
},
|
|
713
766
|
other: {
|
|
714
767
|
publicOrigin: false,
|
|
715
768
|
reason: "host/path-sensitive signing and hosted URL rewriting are not certified; use a direct endpoint"
|
|
716
769
|
}
|
|
717
770
|
};
|
|
771
|
+
function supportsPublicIngress(service) {
|
|
772
|
+
return service === "stripe" || service === "cognito" || service === "medplum";
|
|
773
|
+
}
|
|
718
774
|
function advertisedHttpBase(entry) {
|
|
719
775
|
if (entry.publicPath !== void 0 && !entry.publicOrigin)
|
|
720
776
|
throw new Error("publicPath requires publicOrigin");
|
|
@@ -1253,7 +1309,7 @@ data: ${JSON.stringify(data)}
|
|
|
1253
1309
|
{
|
|
1254
1310
|
protocol: c.protocol,
|
|
1255
1311
|
coverage: c.coverage,
|
|
1256
|
-
ingress: c.targetName
|
|
1312
|
+
ingress: supportsPublicIngress(c.targetName ?? "") ? ingressSupport[c.targetName] : ingressSupport.other,
|
|
1257
1313
|
target: c.targetName,
|
|
1258
1314
|
outboundPolicy: c.outboundPolicy ?? "unsupported",
|
|
1259
1315
|
version: 1,
|
|
@@ -2337,11 +2393,12 @@ data: ${JSON.stringify(data)}
|
|
|
2337
2393
|
format: "mockingbird.fleet/v1",
|
|
2338
2394
|
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2339
2395
|
sourceId: this.ctx.id,
|
|
2396
|
+
...this.ctx.options.archiveCompatibility ? { compatibility: structuredClone(this.ctx.options.archiveCompatibility.tags) } : {},
|
|
2340
2397
|
namespaces: contents
|
|
2341
2398
|
});
|
|
2342
2399
|
}
|
|
2343
2400
|
async restoreArchive(input, selectedNamespace) {
|
|
2344
|
-
const archive =
|
|
2401
|
+
const archive = assertArchiveCompatibility(input, this.ctx.options.archiveCompatibility);
|
|
2345
2402
|
const entries = Object.entries(archive.namespaces).filter(([ns]) => selectedNamespace === void 0 || ns === selectedNamespace);
|
|
2346
2403
|
if (!entries.length)
|
|
2347
2404
|
throw new Error("selected namespace absent from archive");
|
|
@@ -2626,6 +2683,9 @@ var fleet_config_schema_default = {
|
|
|
2626
2683
|
adminControls: {
|
|
2627
2684
|
$ref: "#/$defs/controls"
|
|
2628
2685
|
},
|
|
2686
|
+
idBase: {
|
|
2687
|
+
$ref: "#/$defs/idBase"
|
|
2688
|
+
},
|
|
2629
2689
|
namespace: {
|
|
2630
2690
|
type: "string",
|
|
2631
2691
|
pattern: "^[A-Za-z0-9_.-]{1,64}$"
|
|
@@ -2715,6 +2775,12 @@ var fleet_config_schema_default = {
|
|
|
2715
2775
|
adminControls: {
|
|
2716
2776
|
$ref: "#/$defs/controls"
|
|
2717
2777
|
},
|
|
2778
|
+
idBase: {
|
|
2779
|
+
$ref: "#/$defs/idBase"
|
|
2780
|
+
},
|
|
2781
|
+
settings: {
|
|
2782
|
+
type: "object"
|
|
2783
|
+
},
|
|
2718
2784
|
fixtures: {
|
|
2719
2785
|
type: "object"
|
|
2720
2786
|
},
|
|
@@ -2761,6 +2827,18 @@ var fleet_config_schema_default = {
|
|
|
2761
2827
|
}
|
|
2762
2828
|
}
|
|
2763
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
|
+
},
|
|
2764
2842
|
controls: {
|
|
2765
2843
|
type: "object",
|
|
2766
2844
|
additionalProperties: false,
|
|
@@ -2910,6 +2988,8 @@ async function preflightFleet(config, options) {
|
|
|
2910
2988
|
throw new Error("protected archive policy requires a protected repository; configure its protector before startup");
|
|
2911
2989
|
resolveAdminPrefix(config.adminPrefix);
|
|
2912
2990
|
normalizeBootControls(config.adminControls);
|
|
2991
|
+
if (config.idBase !== void 0)
|
|
2992
|
+
normalizeIdBase(config.idBase, "idBase");
|
|
2913
2993
|
const targets = /* @__PURE__ */ new Map();
|
|
2914
2994
|
const ports = /* @__PURE__ */ new Set();
|
|
2915
2995
|
for (const [name, entry] of Object.entries(config.services)) {
|
|
@@ -2925,7 +3005,7 @@ async function preflightFleet(config, options) {
|
|
|
2925
3005
|
if (entry.target !== void 0 && !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(entry.target))
|
|
2926
3006
|
throw new Error("target must name a supported service suffix");
|
|
2927
3007
|
const service = entry.target ?? name;
|
|
2928
|
-
if (advertisedHttpBase(entry) && service
|
|
3008
|
+
if (advertisedHttpBase(entry) && !supportsPublicIngress(service))
|
|
2929
3009
|
throw new Error(`public ingress unsupported for ${service}; use a direct endpoint to preserve signed targets`);
|
|
2930
3010
|
const target = await options.load(service);
|
|
2931
3011
|
const protocol = "protocol" in target ? target.protocol : "http";
|
|
@@ -2941,6 +3021,13 @@ async function preflightFleet(config, options) {
|
|
|
2941
3021
|
throw new Error("invalid protocol namespace selector");
|
|
2942
3022
|
}
|
|
2943
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
|
+
}
|
|
2944
3031
|
const port2 = entry.port ?? target.defaultPort;
|
|
2945
3032
|
if (port2 !== 0) {
|
|
2946
3033
|
const key = `${entry.host ?? "127.0.0.1"}:${port2}`;
|
|
@@ -2976,7 +3063,7 @@ async function preflightFleet(config, options) {
|
|
|
2976
3063
|
break;
|
|
2977
3064
|
}
|
|
2978
3065
|
}
|
|
2979
|
-
targets.set(name, { ...target, packageVersion: packageVersion ?? "2.
|
|
3066
|
+
targets.set(name, { ...target, packageVersion: packageVersion ?? "2.7.0" });
|
|
2980
3067
|
} catch (error) {
|
|
2981
3068
|
throw new Error(`service ${name}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
2982
3069
|
}
|
|
@@ -3020,6 +3107,11 @@ async function preflightFleet(config, options) {
|
|
|
3020
3107
|
return ordered;
|
|
3021
3108
|
}
|
|
3022
3109
|
function validateFleetPolicies(options) {
|
|
3110
|
+
if (options.archiveCompatibility) {
|
|
3111
|
+
validateCompatibilityTags(options.archiveCompatibility.tags);
|
|
3112
|
+
if (options.archiveCompatibility.missing !== void 0 && !["allow", "reject"].includes(options.archiveCompatibility.missing))
|
|
3113
|
+
throw new Error("archiveCompatibility.missing must be allow or reject");
|
|
3114
|
+
}
|
|
3023
3115
|
if (options.journal && !options.repository)
|
|
3024
3116
|
throw new Error("durable operation journal requires an archive repository");
|
|
3025
3117
|
if (options.cors) {
|
|
@@ -3291,6 +3383,7 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
|
|
|
3291
3383
|
options.onLog?.(entry2);
|
|
3292
3384
|
},
|
|
3293
3385
|
...entry.adminControls !== void 0 ? { adminControls: entry.adminControls } : {},
|
|
3386
|
+
...entry.settings !== void 0 ? { settings: checkSettings(target, entry.settings, "settings") } : {},
|
|
3294
3387
|
...entry.fixtures !== void 0 ? { fixtures: entry.fixtures } : {},
|
|
3295
3388
|
...(advertisedHttpBase(entry) ?? entry.baseUrl) !== void 0 ? { baseUrl: advertisedHttpBase(entry) ?? entry.baseUrl } : {}
|
|
3296
3389
|
});
|
|
@@ -3309,6 +3402,8 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
|
|
|
3309
3402
|
].some((method) => typeof created[method] !== "function"))
|
|
3310
3403
|
throw new Error("HTTP target does not implement the fleet runtime protocol; build/install matching supported service versions");
|
|
3311
3404
|
const runtime = created;
|
|
3405
|
+
if (entry.idBase !== void 0)
|
|
3406
|
+
runtime.setIdBase?.(entry.idBase);
|
|
3312
3407
|
const unobserve = runtime.webhooks?.observeDeliveries(observer.delivery);
|
|
3313
3408
|
if (options.outbound && runtime.webhooks)
|
|
3314
3409
|
runtime.webhooks.setTransportPolicy(ownedTransport(options.outbound));
|
|
@@ -3907,6 +4002,7 @@ async function startFleet(config, options) {
|
|
|
3907
4002
|
...entry,
|
|
3908
4003
|
adminPrefix: resolveAdminPrefix(entry.adminPrefix ?? adminPrefix),
|
|
3909
4004
|
...adminKey !== void 0 ? { adminKey } : {},
|
|
4005
|
+
...(entry.idBase ?? config.idBase) !== void 0 ? { idBase: entry.idBase ?? config.idBase } : {},
|
|
3910
4006
|
// Fleet-wide controls, with the service's own keys replacing them.
|
|
3911
4007
|
...config.adminControls !== void 0 || entry.adminControls !== void 0 ? { adminControls: { ...config.adminControls, ...entry.adminControls } } : {}
|
|
3912
4008
|
}, options, (namespace, request) => management?.admit(namespace, request), (namespace) => management?.generation(namespace), (namespace, request) => management?.privateRequest(namespace, request) ?? false, {
|
|
@@ -4012,10 +4108,44 @@ async function startFleet(config, options) {
|
|
|
4012
4108
|
}
|
|
4013
4109
|
}
|
|
4014
4110
|
|
|
4015
|
-
// ../../adapters/node/dist/fleet-
|
|
4016
|
-
import { readFile as readFile4 } from "node:fs/promises";
|
|
4111
|
+
// ../../adapters/node/dist/fleet-loader.js
|
|
4017
4112
|
import { createRequire as createRequire2 } from "node:module";
|
|
4018
4113
|
import { resolve as resolve2 } from "node:path";
|
|
4114
|
+
import { pathToFileURL } from "node:url";
|
|
4115
|
+
var suffix = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
4116
|
+
function createInstalledServiceLoader(options) {
|
|
4117
|
+
if (!options.projectRoot)
|
|
4118
|
+
throw new Error("consumer projectRoot required");
|
|
4119
|
+
const allowed = new Set(options.services);
|
|
4120
|
+
for (const name of allowed)
|
|
4121
|
+
if (!suffix.test(name))
|
|
4122
|
+
throw new Error("invalid service suffix");
|
|
4123
|
+
const require2 = createRequire2(resolve2(options.projectRoot, "package.json"));
|
|
4124
|
+
const targets = new Map((options.targets ?? []).map((target) => [target.name, target]));
|
|
4125
|
+
return async (name) => {
|
|
4126
|
+
if (!suffix.test(name) || !allowed.has(name))
|
|
4127
|
+
throw new Error("service is not declared in loader allowlist");
|
|
4128
|
+
const own = targets.get(name);
|
|
4129
|
+
if (own)
|
|
4130
|
+
return own;
|
|
4131
|
+
const specifier = `@crvouga/mockingbird-service-${name}/server`;
|
|
4132
|
+
let location;
|
|
4133
|
+
try {
|
|
4134
|
+
location = require2.resolve(specifier);
|
|
4135
|
+
} catch {
|
|
4136
|
+
throw new Error(`cannot load service "${name}": install @crvouga/mockingbird-service-${name} in the consumer project; its /server export is required`);
|
|
4137
|
+
}
|
|
4138
|
+
const mod = await import(pathToFileURL(location).href);
|
|
4139
|
+
if (!mod.serveTarget || mod.serveTarget.name !== name || ("protocol" in mod.serveTarget ? typeof mod.serveTarget.start !== "function" : typeof mod.serveTarget.create !== "function"))
|
|
4140
|
+
throw new Error(`${specifier} exports no matching serveTarget`);
|
|
4141
|
+
return mod.serveTarget;
|
|
4142
|
+
};
|
|
4143
|
+
}
|
|
4144
|
+
|
|
4145
|
+
// ../../adapters/node/dist/fleet-tools.js
|
|
4146
|
+
import { readFile as readFile4 } from "node:fs/promises";
|
|
4147
|
+
import { createRequire as createRequire3 } from "node:module";
|
|
4148
|
+
import { resolve as resolve3 } from "node:path";
|
|
4019
4149
|
|
|
4020
4150
|
// ../../adapters/node/dist/fleet-client.js
|
|
4021
4151
|
import { randomUUID as randomUUID5 } from "node:crypto";
|
|
@@ -4029,6 +4159,14 @@ var FleetClientError = class extends Error {
|
|
|
4029
4159
|
this.details = details;
|
|
4030
4160
|
}
|
|
4031
4161
|
};
|
|
4162
|
+
var FleetClientResponseError = class extends FleetClientError {
|
|
4163
|
+
code;
|
|
4164
|
+
constructor(code, status, origin) {
|
|
4165
|
+
super(status, { code, body: "omitted", ...origin ? { origin } : {} });
|
|
4166
|
+
this.code = code;
|
|
4167
|
+
this.name = "FleetClientResponseError";
|
|
4168
|
+
}
|
|
4169
|
+
};
|
|
4032
4170
|
var FleetClient = class {
|
|
4033
4171
|
base;
|
|
4034
4172
|
options;
|
|
@@ -4041,7 +4179,14 @@ var FleetClient = class {
|
|
|
4041
4179
|
if (url.username || url.password)
|
|
4042
4180
|
throw new Error("fleet client URL must not contain credentials");
|
|
4043
4181
|
url.searchParams.set("namespace", options.namespace ?? this.options.namespace ?? "default");
|
|
4182
|
+
const limit = options.maxResponseBytes ?? this.options.maxResponseBytes ?? 64 * 1024 * 1024;
|
|
4183
|
+
if (!Number.isSafeInteger(limit) || limit < 1)
|
|
4184
|
+
throw new Error("invalid response byte limit");
|
|
4185
|
+
const deadline = AbortSignal.timeout(this.options.timeoutMs ?? 3e4);
|
|
4186
|
+
const signal = options.signal ? AbortSignal.any([options.signal, deadline]) : deadline;
|
|
4187
|
+
signal.throwIfAborted();
|
|
4044
4188
|
const response = await (this.options.fetch ?? fetch)(url, {
|
|
4189
|
+
redirect: "manual",
|
|
4045
4190
|
method: options.method ?? (options.body === void 0 ? "GET" : "POST"),
|
|
4046
4191
|
headers: {
|
|
4047
4192
|
"content-type": "application/json",
|
|
@@ -4049,11 +4194,71 @@ var FleetClient = class {
|
|
|
4049
4194
|
...options.idempotencyKey ? { "idempotency-key": options.idempotencyKey } : {}
|
|
4050
4195
|
},
|
|
4051
4196
|
...options.body !== void 0 ? { body: JSON.stringify(options.body) } : {},
|
|
4052
|
-
signal
|
|
4197
|
+
signal
|
|
4053
4198
|
});
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
throw new
|
|
4199
|
+
if (response.status >= 300 && response.status < 400) {
|
|
4200
|
+
await response.body?.cancel();
|
|
4201
|
+
throw new FleetClientResponseError("redirect", response.status, url.origin);
|
|
4202
|
+
}
|
|
4203
|
+
if (Number(response.headers.get("content-length")) > limit) {
|
|
4204
|
+
await response.body?.cancel();
|
|
4205
|
+
throw new FleetClientResponseError("response_too_large", response.status);
|
|
4206
|
+
}
|
|
4207
|
+
const reader = response.body?.getReader();
|
|
4208
|
+
const chunks = [];
|
|
4209
|
+
let size = 0;
|
|
4210
|
+
const abort = () => {
|
|
4211
|
+
void reader?.cancel(signal.reason).catch(() => {
|
|
4212
|
+
});
|
|
4213
|
+
};
|
|
4214
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
4215
|
+
try {
|
|
4216
|
+
if (reader)
|
|
4217
|
+
while (true) {
|
|
4218
|
+
signal.throwIfAborted();
|
|
4219
|
+
const { done, value } = await reader.read();
|
|
4220
|
+
signal.throwIfAborted();
|
|
4221
|
+
if (done)
|
|
4222
|
+
break;
|
|
4223
|
+
size += value.byteLength;
|
|
4224
|
+
if (size > limit) {
|
|
4225
|
+
await reader.cancel();
|
|
4226
|
+
throw new FleetClientResponseError("response_too_large", response.status);
|
|
4227
|
+
}
|
|
4228
|
+
chunks.push(value);
|
|
4229
|
+
}
|
|
4230
|
+
} finally {
|
|
4231
|
+
signal.removeEventListener("abort", abort);
|
|
4232
|
+
if (signal.aborted)
|
|
4233
|
+
await reader?.cancel(signal.reason).catch(() => {
|
|
4234
|
+
});
|
|
4235
|
+
reader?.releaseLock();
|
|
4236
|
+
}
|
|
4237
|
+
const bytes = new Uint8Array(size);
|
|
4238
|
+
let offset = 0;
|
|
4239
|
+
for (const chunk of chunks) {
|
|
4240
|
+
bytes.set(chunk, offset);
|
|
4241
|
+
offset += chunk.byteLength;
|
|
4242
|
+
}
|
|
4243
|
+
const text = new TextDecoder().decode(bytes);
|
|
4244
|
+
let data;
|
|
4245
|
+
try {
|
|
4246
|
+
data = text.length ? JSON.parse(text) : void 0;
|
|
4247
|
+
} catch {
|
|
4248
|
+
if (response.ok)
|
|
4249
|
+
throw new FleetClientResponseError("invalid_json", response.status);
|
|
4250
|
+
}
|
|
4251
|
+
if (!response.ok) {
|
|
4252
|
+
const error = data && typeof data === "object" ? data.error : void 0;
|
|
4253
|
+
const detail = { body: "omitted" };
|
|
4254
|
+
if (error && typeof error === "object")
|
|
4255
|
+
for (const key of ["type", "code"]) {
|
|
4256
|
+
const value = error[key];
|
|
4257
|
+
if (typeof value === "string" && /^[A-Za-z0-9_.-]{1,128}$/.test(value))
|
|
4258
|
+
detail[key] = value;
|
|
4259
|
+
}
|
|
4260
|
+
throw new FleetClientError(response.status, detail);
|
|
4261
|
+
}
|
|
4057
4262
|
return data;
|
|
4058
4263
|
}
|
|
4059
4264
|
discover() {
|
|
@@ -4119,17 +4324,52 @@ function renderConnections(manifest, privateManifest, namespace, mapping) {
|
|
|
4119
4324
|
const child = manifest.services[entry.service], secret = privateManifest.services[entry.service];
|
|
4120
4325
|
if (!child || !secret)
|
|
4121
4326
|
throw new Error(`missing service mapping ${entry.service}`);
|
|
4327
|
+
let endpoint;
|
|
4122
4328
|
if (child.protocol === "http") {
|
|
4123
4329
|
const template = child.namespaces.path;
|
|
4124
4330
|
if (typeof template !== "string")
|
|
4125
4331
|
throw new Error(`missing HTTP namespace mapping ${entry.service}`);
|
|
4126
|
-
|
|
4332
|
+
endpoint = new URL(template.replace("{name}", encodeURIComponent(namespace)), secret.url).href;
|
|
4127
4333
|
} else {
|
|
4128
|
-
const
|
|
4129
|
-
if (!
|
|
4334
|
+
const selected = secret.namespaces?.[namespace];
|
|
4335
|
+
if (!selected)
|
|
4130
4336
|
throw new Error(`missing namespace mapping ${namespace}/${entry.service}`);
|
|
4131
|
-
|
|
4337
|
+
endpoint = selected;
|
|
4338
|
+
}
|
|
4339
|
+
const url = new URL(endpoint);
|
|
4340
|
+
const field = entry.field ?? "url";
|
|
4341
|
+
let value;
|
|
4342
|
+
switch (field) {
|
|
4343
|
+
case "url":
|
|
4344
|
+
value = endpoint;
|
|
4345
|
+
break;
|
|
4346
|
+
case "hostname":
|
|
4347
|
+
value = url.hostname.replace(/^\[|\]$/g, "");
|
|
4348
|
+
break;
|
|
4349
|
+
case "port":
|
|
4350
|
+
value = url.port;
|
|
4351
|
+
break;
|
|
4352
|
+
case "username":
|
|
4353
|
+
value = decodeURIComponent(url.username);
|
|
4354
|
+
break;
|
|
4355
|
+
case "password":
|
|
4356
|
+
value = decodeURIComponent(url.password);
|
|
4357
|
+
break;
|
|
4358
|
+
case "pathname":
|
|
4359
|
+
value = decodeURIComponent(url.pathname);
|
|
4360
|
+
break;
|
|
4361
|
+
case "database":
|
|
4362
|
+
value = decodeURIComponent(url.pathname.replace(/^\//, ""));
|
|
4363
|
+
break;
|
|
4364
|
+
default:
|
|
4365
|
+
throw new Error("unsupported connection field");
|
|
4132
4366
|
}
|
|
4367
|
+
if (!value) {
|
|
4368
|
+
if (typeof entry.fallback !== "string")
|
|
4369
|
+
throw new Error(`missing connection component ${field}`);
|
|
4370
|
+
value = entry.fallback;
|
|
4371
|
+
}
|
|
4372
|
+
result[variable] = value;
|
|
4133
4373
|
}
|
|
4134
4374
|
return result;
|
|
4135
4375
|
}
|
|
@@ -4153,7 +4393,7 @@ async function doctorFleet(config, options, probes = []) {
|
|
|
4153
4393
|
const plan = [];
|
|
4154
4394
|
try {
|
|
4155
4395
|
const targets = await preflightFleet(config, options);
|
|
4156
|
-
const require2 =
|
|
4396
|
+
const require2 = createRequire3(resolve3(process.cwd(), "package.json"));
|
|
4157
4397
|
for (const [service, target] of targets) {
|
|
4158
4398
|
let version = "development";
|
|
4159
4399
|
try {
|
|
@@ -4208,7 +4448,7 @@ async function doctorFleet(config, options, probes = []) {
|
|
|
4208
4448
|
};
|
|
4209
4449
|
}
|
|
4210
4450
|
async function rehearseFleet(config, options, input, probes = [], timeoutMs = 3e4) {
|
|
4211
|
-
const archive =
|
|
4451
|
+
const archive = assertArchiveCompatibility(input, options.archiveCompatibility);
|
|
4212
4452
|
const startedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
4213
4453
|
const isolated = {
|
|
4214
4454
|
...config,
|
|
@@ -4220,7 +4460,7 @@ async function rehearseFleet(config, options, input, probes = [], timeoutMs = 3e
|
|
|
4220
4460
|
};
|
|
4221
4461
|
let fleet;
|
|
4222
4462
|
try {
|
|
4223
|
-
fleet = await startFleet(isolated,
|
|
4463
|
+
fleet = await startFleet(isolated, options);
|
|
4224
4464
|
const client = new FleetClient(fleet.manifest.adminBase, {
|
|
4225
4465
|
...config.adminKey ? { adminKey: config.adminKey } : {}
|
|
4226
4466
|
});
|
|
@@ -4447,6 +4687,16 @@ var COMMON_SERVE_OPTIONS = {
|
|
|
4447
4687
|
value: "<seed>",
|
|
4448
4688
|
description: "Seed for every random choice (env MOCKINGBIRD_SEED)"
|
|
4449
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
|
+
},
|
|
4450
4700
|
"admin-controls": {
|
|
4451
4701
|
type: "string",
|
|
4452
4702
|
value: "<json|file>",
|
|
@@ -4541,19 +4791,40 @@ var readJsonFile = async (path, label) => {
|
|
|
4541
4791
|
}
|
|
4542
4792
|
};
|
|
4543
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
|
+
};
|
|
4544
4823
|
var resolveServeControls = async (values, env = process.env, base) => {
|
|
4545
4824
|
const source = asString(values["admin-controls"]) ?? env.MOCKINGBIRD_ADMIN_CONTROLS;
|
|
4546
4825
|
let document = isObject2(base) ? { ...base } : {};
|
|
4547
4826
|
if (source !== void 0 && source.trim() !== "") {
|
|
4548
|
-
|
|
4549
|
-
if (source.trim().startsWith("{")) {
|
|
4550
|
-
try {
|
|
4551
|
-
parsed = JSON.parse(source);
|
|
4552
|
-
} catch {
|
|
4553
|
-
throw new Error("--admin-controls: not valid JSON");
|
|
4554
|
-
}
|
|
4555
|
-
} else
|
|
4556
|
-
parsed = await readJsonFile(source, "--admin-controls");
|
|
4827
|
+
const parsed = source.trim().startsWith("{") ? await readJsonOption(source, "--admin-controls") : await readJsonFile(source, "--admin-controls");
|
|
4557
4828
|
if (!isObject2(parsed))
|
|
4558
4829
|
throw new Error("--admin-controls: expected a JSON object");
|
|
4559
4830
|
document = { ...document, ...parsed };
|
|
@@ -4595,26 +4866,7 @@ var resolveServeControls = async (values, env = process.env, base) => {
|
|
|
4595
4866
|
normalizeBootControls(document, "adminControls");
|
|
4596
4867
|
return document;
|
|
4597
4868
|
};
|
|
4598
|
-
var loadTarget =
|
|
4599
|
-
if (name === own.name)
|
|
4600
|
-
return own;
|
|
4601
|
-
const specifier = `@crvouga/mockingbird-service-${name}/server`;
|
|
4602
|
-
try {
|
|
4603
|
-
let mod;
|
|
4604
|
-
try {
|
|
4605
|
-
mod = await import(specifier);
|
|
4606
|
-
} catch {
|
|
4607
|
-
const fromProject = createRequire3(resolve3(process.cwd(), "package.json"));
|
|
4608
|
-
mod = await import(pathToFileURL(fromProject.resolve(specifier)).href);
|
|
4609
|
-
}
|
|
4610
|
-
if (!mod.serveTarget)
|
|
4611
|
-
throw new Error(`${specifier} exports no serveTarget`);
|
|
4612
|
-
return mod.serveTarget;
|
|
4613
|
-
} catch (error) {
|
|
4614
|
-
const reason = error instanceof Error ? error.message : String(error);
|
|
4615
|
-
throw new Error(`cannot load service "${name}": ${reason}. Install @crvouga/mockingbird-service-${name}.`);
|
|
4616
|
-
}
|
|
4617
|
-
};
|
|
4869
|
+
var loadTarget = (name, own) => createInstalledServiceLoader({ projectRoot: process.cwd(), services: [name], targets: [own] })(name);
|
|
4618
4870
|
var start = async (target, values, config) => {
|
|
4619
4871
|
const adminPrefix = resolveAdminPrefix(config.adminPrefix);
|
|
4620
4872
|
const runtime = await target.create(values, {
|
|
@@ -4622,8 +4874,11 @@ var start = async (target, values, config) => {
|
|
|
4622
4874
|
adminKey: config.adminKey,
|
|
4623
4875
|
seed: config.seed,
|
|
4624
4876
|
onLog: formatLog(config.log),
|
|
4877
|
+
...config.settings !== void 0 ? { settings: config.settings } : {},
|
|
4625
4878
|
...config.adminControls !== void 0 ? { adminControls: config.adminControls } : {}
|
|
4626
4879
|
});
|
|
4880
|
+
if (config.idBase !== void 0)
|
|
4881
|
+
runtime.setIdBase?.(config.idBase);
|
|
4627
4882
|
const listening2 = await listen(runtime, { port: config.port, host: config.host });
|
|
4628
4883
|
target.listening?.(listening2);
|
|
4629
4884
|
console.log(`${target.name} emulator listening on ${listening2.url}`);
|
|
@@ -4669,6 +4924,11 @@ var serveCommand = (target) => ({
|
|
|
4669
4924
|
const adminControls = await resolveServeControls(values, process.env, config.adminControls);
|
|
4670
4925
|
if (adminControls !== void 0)
|
|
4671
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;
|
|
4672
4932
|
const fleet = await startFleet(config, {
|
|
4673
4933
|
load: (name) => loadTarget(name, target),
|
|
4674
4934
|
onLog: formatLog(config.log ?? log),
|
|
@@ -4698,8 +4958,12 @@ var serveCommand = (target) => ({
|
|
|
4698
4958
|
let listening2;
|
|
4699
4959
|
try {
|
|
4700
4960
|
const adminControls = await resolveServeControls(values);
|
|
4961
|
+
const idBase = resolveServeIdBase(values);
|
|
4962
|
+
const settings = await resolveServeSettings(target, values);
|
|
4701
4963
|
listening2 = await start(target, values, {
|
|
4702
4964
|
...adminControls !== void 0 ? { adminControls } : {},
|
|
4965
|
+
...idBase !== void 0 ? { idBase } : {},
|
|
4966
|
+
...settings !== void 0 ? { settings } : {},
|
|
4703
4967
|
adminPrefix: resolveAdminPrefix(asString(values["admin-prefix"]) ?? process.env.MOCKINGBIRD_ADMIN_PREFIX),
|
|
4704
4968
|
port: port2 === void 0 ? target.defaultPort : Number.parseInt(port2, 10),
|
|
4705
4969
|
host: asString(values.host) ?? "127.0.0.1",
|