@crvouga/mockingbird-runtime 0.2.1 → 0.3.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/README.md +15 -1
- package/dist/adapter-node.d.ts +38 -11
- package/dist/adapter-node.js +82 -174
- package/dist/adapter-node.js.map +4 -4
- package/dist/{chunk-4BRVNPWR.js → chunk-KD6FHYQP.js} +6 -6
- package/dist/{chunk-4BRVNPWR.js.map → chunk-KD6FHYQP.js.map} +2 -2
- package/dist/{chunk-LL6NKAJO.js → chunk-M4HUKBLC.js} +3 -2
- package/dist/chunk-M4HUKBLC.js.map +7 -0
- package/dist/chunk-RI2UQCHP.js +21 -0
- package/dist/chunk-RI2UQCHP.js.map +7 -0
- package/dist/{chunk-2Y25NPCV.js → chunk-U36J5GCB.js} +290 -1
- package/dist/{chunk-2Y25NPCV.js.map → chunk-U36J5GCB.js.map} +3 -3
- package/dist/{chunk-RO7FHXO3.js → chunk-ZQBZSSSP.js} +3 -3
- package/dist/core.d.ts +86 -2
- package/dist/core.js +23 -1
- package/dist/http-provider.js +7 -7
- package/dist/http-provider.js.map +1 -1
- package/dist/mqtt-broker/node.js +4 -4
- package/dist/mqtt-broker.js +4 -4
- package/dist/preview/artifacts.d.ts +4 -9
- package/dist/preview/artifacts.js +4 -16
- package/dist/preview/artifacts.js.map +2 -2
- package/dist/preview/bridge.d.ts +8 -8
- package/dist/preview/bridge.js +1 -1
- package/dist/preview/harness.d.ts +1 -1
- package/dist/preview/harness.js +10 -56
- package/dist/preview/harness.js.map +2 -2
- package/dist/preview/host.d.ts +4 -9
- package/dist/preview/host.js +30 -37
- package/dist/preview/host.js.map +2 -2
- package/dist/service/admin.js +2 -2
- package/dist/service.d.ts +3 -1
- package/dist/service.js +6 -4
- package/dist/ui.d.ts +1 -37
- package/dist/ui.js +2 -701
- package/dist/ui.js.map +3 -3
- package/package.json +7 -6
- package/dist/chunk-EBGYPQPA.js +0 -198
- package/dist/chunk-EBGYPQPA.js.map +0 -7
- package/dist/chunk-LL6NKAJO.js.map +0 -7
- /package/dist/{chunk-RO7FHXO3.js.map → chunk-ZQBZSSSP.js.map} +0 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @crvouga/mockingbird-runtime
|
|
2
2
|
|
|
3
|
-
> Local emulators. Real API contracts. Part of [Mockingbird](https://github.com/crvouga/mockingbird).
|
|
3
|
+
> Local emulators. Real API contracts. Part of [Mockingbird](https://github.com/crvouga-organization/mockingbird).
|
|
4
4
|
|
|
5
5
|
The runtime every Mockingbird emulator service is built on: the router, the `/__admin` API and
|
|
6
6
|
page, snapshots, faults, the clock, and the HTTP and OpenAPI helpers. Each
|
|
@@ -42,6 +42,20 @@ To leave the admin page's client out of a single-file application bundle, mark i
|
|
|
42
42
|
(`esbuild --external:./admin-ui.js`); `/__admin/ui` then answers 503 and everything else works.
|
|
43
43
|
|
|
44
44
|
|
|
45
|
+
## Canonical preview package and compatibility
|
|
46
|
+
|
|
47
|
+
Worker hosting, the MessagePort bridge, artifact loading and the Node HTTP adapter are implemented
|
|
48
|
+
once in [`@crvouga/mockingbird-preview`](../adapters/preview/README.md). The existing runtime
|
|
49
|
+
`preview/*` exports remain supported compatibility adapters; they delegate to that package and
|
|
50
|
+
preserve their public signatures, first generation `1`, promise-shaped `ready`, explicit cookie
|
|
51
|
+
headers and error messages. New consumers can use the canonical package directly for URL policies,
|
|
52
|
+
opaque errors by default, SharedWorker identities, bounded artifacts and static deployment helpers.
|
|
53
|
+
|
|
54
|
+
The artifact formats intentionally differ: legacy `{version: 1}` manifests identify decoded
|
|
55
|
+
JavaScript bytes, while canonical `{format: 1}` manifests identify the downloaded bytes and include
|
|
56
|
+
commit, size and encoding. One bounded loader understands both. Never reinterpret a legacy gzip
|
|
57
|
+
hash as a canonical packed-byte hash; generate a new manifest when migrating.
|
|
58
|
+
|
|
45
59
|
## System harness for application previews
|
|
46
60
|
|
|
47
61
|
```ts
|
package/dist/adapter-node.d.ts
CHANGED
|
@@ -137,6 +137,12 @@ type UpstreamState = {
|
|
|
137
137
|
mode: "emulated" | "forward";
|
|
138
138
|
baseUrl: string;
|
|
139
139
|
};
|
|
140
|
+
type UpstreamOptions = {
|
|
141
|
+
fetch: (request: Request) => Response | Promise<Response>;
|
|
142
|
+
baseUrl: string;
|
|
143
|
+
mode?: "emulated" | "forward";
|
|
144
|
+
rewriteAuth?: (request: Request) => Request | Promise<Request>;
|
|
145
|
+
};
|
|
140
146
|
|
|
141
147
|
/**
|
|
142
148
|
* Seeded pseudo-random numbers, so anything a mock invents — ids, jitter, which
|
|
@@ -1034,6 +1040,16 @@ declare function diffArchives(left: unknown, right: unknown, options?: {
|
|
|
1034
1040
|
}[];
|
|
1035
1041
|
};
|
|
1036
1042
|
|
|
1043
|
+
/**
|
|
1044
|
+
* Anything that can answer a Fetch `Request` with a `Response`.
|
|
1045
|
+
*
|
|
1046
|
+
* Every Mockingbird service implements this, and every runtime adapter consumes it.
|
|
1047
|
+
* It is the only contract shared across the whole graph.
|
|
1048
|
+
*/
|
|
1049
|
+
interface FetchAPI {
|
|
1050
|
+
fetch(request: Request): Promise<Response>;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1037
1053
|
/** Public readiness contract, usable before workspace packages have been built. */
|
|
1038
1054
|
type EndpointManifest = {
|
|
1039
1055
|
version: 1;
|
|
@@ -1604,6 +1620,7 @@ type FleetChild = {
|
|
|
1604
1620
|
collection?: string;
|
|
1605
1621
|
}): Promise<unknown>;
|
|
1606
1622
|
configure?(namespace: string, controls: Record<string, unknown>): Promise<unknown>;
|
|
1623
|
+
upstream?(namespace: string, settings?: Record<string, unknown>): Promise<Response>;
|
|
1607
1624
|
unmatched?(namespace: string, limit: number): {
|
|
1608
1625
|
entries: unknown[];
|
|
1609
1626
|
truncated: boolean;
|
|
@@ -1624,11 +1641,12 @@ type ProtocolTarget = {
|
|
|
1624
1641
|
};
|
|
1625
1642
|
/** Bundled service declarations contain distinct private runtime classes. The loader boundary
|
|
1626
1643
|
* accepts their opaque runtime and validates the fleet protocol before starting a listener. */
|
|
1627
|
-
type FleetHttpTarget = Pick<ServeTarget, "name" | "defaultPort" | "options" | "packageVersion" | "listening" | "parseSettings" | "maxBodyBytes"> & {
|
|
1644
|
+
type FleetHttpTarget = Pick<ServeTarget, "name" | "defaultPort" | "options" | "packageVersion" | "listening" | "parseSettings" | "maxBodyBytes" | "supportsUpstream"> & {
|
|
1628
1645
|
create(values: Parameters<ServeTarget["create"]>[0], common: CommonServeOptions): unknown;
|
|
1629
1646
|
};
|
|
1630
1647
|
type FleetTarget = FleetHttpTarget | ProtocolTarget;
|
|
1631
1648
|
type FleetOptions = ManagementOptions & {
|
|
1649
|
+
upstreams?: Record<string, Pick<UpstreamOptions, "fetch" | "rewriteAuth">>;
|
|
1632
1650
|
participants?: Record<string, FleetChild>;
|
|
1633
1651
|
load(name: string): Promise<FleetTarget>;
|
|
1634
1652
|
onLog?: CommonServeOptions["onLog"];
|
|
@@ -1674,16 +1692,6 @@ type Fleet = {
|
|
|
1674
1692
|
/** Resolves only after every child and the aggregate control listener are healthy. */
|
|
1675
1693
|
declare function startFleet(config: MockingbirdConfig, options: FleetOptions): Promise<Fleet>;
|
|
1676
1694
|
|
|
1677
|
-
/**
|
|
1678
|
-
* Anything that can answer a Fetch `Request` with a `Response`.
|
|
1679
|
-
*
|
|
1680
|
-
* Every Mockingbird service implements this, and every runtime adapter consumes it.
|
|
1681
|
-
* It is the only contract shared across the whole graph.
|
|
1682
|
-
*/
|
|
1683
|
-
interface FetchAPI {
|
|
1684
|
-
fetch(request: Request): Promise<Response>;
|
|
1685
|
-
}
|
|
1686
|
-
|
|
1687
1695
|
type ListenOptions = {
|
|
1688
1696
|
/** Default `0`: the OS picks a free port. */
|
|
1689
1697
|
port?: number;
|
|
@@ -1727,6 +1735,7 @@ type CliSpec = {
|
|
|
1727
1735
|
declare const runCli: (spec: CliSpec, argv: string[]) => Promise<number>;
|
|
1728
1736
|
type LogFormat = "pretty" | "json" | "off";
|
|
1729
1737
|
type CommonServeOptions = {
|
|
1738
|
+
upstream?: UpstreamOptions;
|
|
1730
1739
|
adminPrefix?: string;
|
|
1731
1740
|
adminKey: string | undefined;
|
|
1732
1741
|
seed: string | undefined;
|
|
@@ -1749,6 +1758,7 @@ type CommonServeOptions = {
|
|
|
1749
1758
|
* `serveTarget`, which is also how `serve --config` finds services by name.
|
|
1750
1759
|
*/
|
|
1751
1760
|
type ServeTarget = {
|
|
1761
|
+
supportsUpstream?: boolean;
|
|
1752
1762
|
packageVersion?: string;
|
|
1753
1763
|
name: string;
|
|
1754
1764
|
defaultPort: number;
|
|
@@ -1789,6 +1799,10 @@ declare const resolveServeSettings: (target: Pick<ServeTarget, "name" | "parseSe
|
|
|
1789
1799
|
declare const resolveServeControls: (values: CliValues, env?: Record<string, string | undefined>, base?: unknown) => Promise<MockControls | undefined>;
|
|
1790
1800
|
/** A service entry in `mockingbird.json`. */
|
|
1791
1801
|
type ConfigService = {
|
|
1802
|
+
upstream?: {
|
|
1803
|
+
baseUrl: string;
|
|
1804
|
+
mode?: "emulated" | "forward";
|
|
1805
|
+
};
|
|
1792
1806
|
/** Stable config key is the instance identity; target defaults to that key. */
|
|
1793
1807
|
target?: string;
|
|
1794
1808
|
publicOrigin?: string;
|
|
@@ -2093,6 +2107,19 @@ declare const fleetConfigSchema: {
|
|
|
2093
2107
|
publicPath: {
|
|
2094
2108
|
type: string;
|
|
2095
2109
|
};
|
|
2110
|
+
upstream: {
|
|
2111
|
+
type: string;
|
|
2112
|
+
additionalProperties: boolean;
|
|
2113
|
+
required: string[];
|
|
2114
|
+
properties: {
|
|
2115
|
+
baseUrl: {
|
|
2116
|
+
type: string;
|
|
2117
|
+
};
|
|
2118
|
+
mode: {
|
|
2119
|
+
enum: string[];
|
|
2120
|
+
};
|
|
2121
|
+
};
|
|
2122
|
+
};
|
|
2096
2123
|
};
|
|
2097
2124
|
};
|
|
2098
2125
|
idBase: {
|
package/dist/adapter-node.js
CHANGED
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
normalizeIdBase,
|
|
3
3
|
ownedTransport,
|
|
4
4
|
signV4
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-KD6FHYQP.js";
|
|
6
6
|
import "./chunk-2KDYLZXC.js";
|
|
7
7
|
import {
|
|
8
8
|
createClock,
|
|
@@ -11,9 +11,21 @@ import {
|
|
|
11
11
|
parseDuration,
|
|
12
12
|
parseFaultHeader,
|
|
13
13
|
resolveAdminPrefix,
|
|
14
|
-
restoreClock
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
restoreClock,
|
|
15
|
+
upstreamRequest
|
|
16
|
+
} from "./chunk-M4HUKBLC.js";
|
|
17
|
+
import {
|
|
18
|
+
assertArchiveCompatibility,
|
|
19
|
+
assertCompatibilityTags,
|
|
20
|
+
canonical,
|
|
21
|
+
diffArchives,
|
|
22
|
+
digest,
|
|
23
|
+
inspectArchive,
|
|
24
|
+
migrateApplicationArchive,
|
|
25
|
+
sealArchive,
|
|
26
|
+
validateArchive,
|
|
27
|
+
validateCompatibilityTags
|
|
28
|
+
} from "./chunk-U36J5GCB.js";
|
|
17
29
|
import "./chunk-EQLY2YOA.js";
|
|
18
30
|
import "./chunk-HU6IOAFC.js";
|
|
19
31
|
import "./chunk-4SAK6MQT.js";
|
|
@@ -37,170 +49,12 @@ var checkSettings = (target, settings, label) => {
|
|
|
37
49
|
|
|
38
50
|
// ../adapters/node/dist/fleet.js
|
|
39
51
|
import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
|
|
40
|
-
import { createHash as
|
|
52
|
+
import { createHash as createHash3, randomUUID as randomUUID6 } from "node:crypto";
|
|
41
53
|
import { chmod, readFile as readFile4, rename as rename3, rm as rm3, writeFile as writeFile3 } from "node:fs/promises";
|
|
42
54
|
|
|
43
55
|
// ../adapters/node/dist/fleet-management.js
|
|
44
56
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
45
|
-
import { createHash as
|
|
46
|
-
|
|
47
|
-
// ../adapters/node/dist/fleet-archive.js
|
|
48
|
-
import { createHash } from "node:crypto";
|
|
49
|
-
function validateCompatibilityTags(tags) {
|
|
50
|
-
if (!tags || typeof tags !== "object" || Array.isArray(tags))
|
|
51
|
-
throw new Error("invalid archive compatibility tags");
|
|
52
|
-
const entries = Object.entries(tags);
|
|
53
|
-
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)))
|
|
54
|
-
throw new Error("invalid archive compatibility tags");
|
|
55
|
-
}
|
|
56
|
-
function assertCompatibilityTags(tags, policy) {
|
|
57
|
-
if (tags !== void 0)
|
|
58
|
-
validateCompatibilityTags(tags);
|
|
59
|
-
if (!policy)
|
|
60
|
-
return;
|
|
61
|
-
validateCompatibilityTags(policy.tags);
|
|
62
|
-
if (policy.missing !== void 0 && !["allow", "reject"].includes(policy.missing))
|
|
63
|
-
throw new Error("invalid missing compatibility policy");
|
|
64
|
-
if (tags === void 0 && policy.missing === "allow")
|
|
65
|
-
return;
|
|
66
|
-
if (canonical(tags) !== canonical(policy.tags))
|
|
67
|
-
throw new Error("application archive compatibility mismatch");
|
|
68
|
-
}
|
|
69
|
-
function assertArchiveCompatibility(input, policy) {
|
|
70
|
-
const archive = validateArchive(input);
|
|
71
|
-
assertCompatibilityTags(archive.compatibility, policy);
|
|
72
|
-
return archive;
|
|
73
|
-
}
|
|
74
|
-
async function migrateApplicationArchive(input, migration) {
|
|
75
|
-
const source = assertArchiveCompatibility(input, migration.from);
|
|
76
|
-
validateCompatibilityTags(migration.to);
|
|
77
|
-
const transformed = await migration.transform(structuredClone(source));
|
|
78
|
-
const { sha256: _sha, ...content } = transformed;
|
|
79
|
-
return validateArchive(sealArchive({ ...content, compatibility: structuredClone(migration.to) }));
|
|
80
|
-
}
|
|
81
|
-
var canonical = (value) => {
|
|
82
|
-
if (Array.isArray(value))
|
|
83
|
-
return `[${value.map(canonical).join(",")}]`;
|
|
84
|
-
if (value !== null && typeof value === "object")
|
|
85
|
-
return `{${Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
|
|
86
|
-
return JSON.stringify(value);
|
|
87
|
-
};
|
|
88
|
-
var digest = (value) => createHash("sha256").update(canonical(value)).digest("hex");
|
|
89
|
-
var sealArchive = (content) => ({
|
|
90
|
-
...content,
|
|
91
|
-
sha256: digest(content)
|
|
92
|
-
});
|
|
93
|
-
function validateArchive(value) {
|
|
94
|
-
if (!value || typeof value !== "object")
|
|
95
|
-
throw new Error("expected fleet archive");
|
|
96
|
-
const doc = value;
|
|
97
|
-
const { sha256, ...content } = doc;
|
|
98
|
-
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))
|
|
99
|
-
throw new Error("unsupported or corrupt fleet archive");
|
|
100
|
-
if (doc.compatibility !== void 0)
|
|
101
|
-
validateCompatibilityTags(doc.compatibility);
|
|
102
|
-
for (const [namespace, entry] of Object.entries(doc.namespaces)) {
|
|
103
|
-
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)
|
|
104
|
-
throw new Error("invalid archive namespace");
|
|
105
|
-
for (const [name, child] of Object.entries(entry.services))
|
|
106
|
-
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name) || !child || child.version !== 1 || !["http", "postgres", "redis", "external"].includes(child.protocol))
|
|
107
|
-
throw new Error("unsupported archive service/version");
|
|
108
|
-
}
|
|
109
|
-
return doc;
|
|
110
|
-
}
|
|
111
|
-
function inspectArchive(input) {
|
|
112
|
-
const archive = validateArchive(input);
|
|
113
|
-
return {
|
|
114
|
-
format: archive.format,
|
|
115
|
-
createdAt: archive.createdAt,
|
|
116
|
-
sha256: archive.sha256,
|
|
117
|
-
...archive.compatibility ? { compatibility: archive.compatibility } : {},
|
|
118
|
-
namespaces: Object.fromEntries(Object.entries(archive.namespaces).map(([name, ns]) => [
|
|
119
|
-
name,
|
|
120
|
-
{
|
|
121
|
-
clock: ns.clock,
|
|
122
|
-
services: Object.fromEntries(Object.entries(ns.services).map(([service, state]) => [
|
|
123
|
-
service,
|
|
124
|
-
{
|
|
125
|
-
protocol: state.protocol,
|
|
126
|
-
version: state.version,
|
|
127
|
-
bytes: Buffer.byteLength(canonical(state.state))
|
|
128
|
-
}
|
|
129
|
-
]))
|
|
130
|
-
}
|
|
131
|
-
]))
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
function diffArchives(left, right, options = {}) {
|
|
135
|
-
const a = validateArchive(left), b = validateArchive(right);
|
|
136
|
-
const limit = Math.min(1e3, Math.max(1, options.limit ?? 100));
|
|
137
|
-
const changes = [];
|
|
138
|
-
let total = 0;
|
|
139
|
-
const walk = (namespace, service, path, before, after) => {
|
|
140
|
-
if (canonical(before) === canonical(after))
|
|
141
|
-
return;
|
|
142
|
-
if (before && after && typeof before === "object" && typeof after === "object" && !Array.isArray(before) && !Array.isArray(after)) {
|
|
143
|
-
for (const key of /* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)]))
|
|
144
|
-
walk(namespace, service, `${path}/${key}`, before[key], after[key]);
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
total++;
|
|
148
|
-
if (changes.length < limit)
|
|
149
|
-
changes.push({
|
|
150
|
-
namespace,
|
|
151
|
-
service,
|
|
152
|
-
path,
|
|
153
|
-
kind: before === void 0 ? "added" : after === void 0 ? "removed" : "changed",
|
|
154
|
-
...options.values ? { before, after } : {}
|
|
155
|
-
});
|
|
156
|
-
};
|
|
157
|
-
for (const ns of /* @__PURE__ */ new Set([...Object.keys(a.namespaces), ...Object.keys(b.namespaces)])) {
|
|
158
|
-
if (options.namespace !== void 0 && options.namespace !== ns)
|
|
159
|
-
continue;
|
|
160
|
-
const an = a.namespaces[ns], bn = b.namespaces[ns];
|
|
161
|
-
walk(ns, "$clock", "/clock", an?.clock, bn?.clock);
|
|
162
|
-
for (const service of /* @__PURE__ */ new Set([
|
|
163
|
-
...Object.keys(an?.services ?? {}),
|
|
164
|
-
...Object.keys(bn?.services ?? {})
|
|
165
|
-
])) {
|
|
166
|
-
if (options.service !== void 0 && options.service !== service)
|
|
167
|
-
continue;
|
|
168
|
-
const x = an?.services[service], y = bn?.services[service];
|
|
169
|
-
if (x && y && (x.protocol !== y.protocol || x.version !== y.version))
|
|
170
|
-
throw new Error("incompatible snapshot comparison");
|
|
171
|
-
const normalize = (state) => {
|
|
172
|
-
if (state && typeof state === "object") {
|
|
173
|
-
const obj = state;
|
|
174
|
-
if (obj.format === "mockingbird.postgres/v1" && obj.inventory) {
|
|
175
|
-
if (options.values)
|
|
176
|
-
throw new Error("raw PostgreSQL row diff is unsupported; use bounded namespace inspection");
|
|
177
|
-
const { databases: _databases, ...metadata } = obj;
|
|
178
|
-
return metadata;
|
|
179
|
-
}
|
|
180
|
-
if (obj.$redis === "map" && Array.isArray(obj.value))
|
|
181
|
-
return Object.fromEntries(obj.value.map((pair) => [
|
|
182
|
-
typeof pair[0] === "string" ? pair[0] : canonical(pair[0]),
|
|
183
|
-
normalize(pair[1])
|
|
184
|
-
]));
|
|
185
|
-
if (Array.isArray(state))
|
|
186
|
-
return Object.fromEntries(state.map((value, index) => [String(index), normalize(value)]));
|
|
187
|
-
}
|
|
188
|
-
if (state && typeof state === "object" && Array.isArray(state.records)) {
|
|
189
|
-
const { records, ...rest } = state;
|
|
190
|
-
return {
|
|
191
|
-
...rest,
|
|
192
|
-
records: Object.fromEntries(records.map((r) => [`${r.collection}/${r.id}`, r]))
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
if (state && typeof state === "object" && !Array.isArray(state))
|
|
196
|
-
return Object.fromEntries(Object.entries(state).map(([k, v]) => [k, normalize(v)]));
|
|
197
|
-
return state;
|
|
198
|
-
};
|
|
199
|
-
walk(ns, service, "/state", normalize(x?.state), normalize(y?.state));
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return { format: "mockingbird.fleet-diff/v1", total, truncated: total > changes.length, changes };
|
|
203
|
-
}
|
|
57
|
+
import { createHash as createHash2, randomBytes, randomUUID as randomUUID5 } from "node:crypto";
|
|
204
58
|
|
|
205
59
|
// ../adapters/node/dist/fleet-bindings.js
|
|
206
60
|
var object = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
@@ -1006,8 +860,8 @@ async function reconcileOperation(record2, repository) {
|
|
|
1006
860
|
}
|
|
1007
861
|
|
|
1008
862
|
// ../adapters/node/dist/fleet-observations.js
|
|
1009
|
-
import { createHash
|
|
1010
|
-
var hash = (text) =>
|
|
863
|
+
import { createHash, randomUUID as randomUUID4 } from "node:crypto";
|
|
864
|
+
var hash = (text) => createHash("sha256").update(text).digest("hex");
|
|
1011
865
|
var boundedText = async (stream, max) => {
|
|
1012
866
|
if (!stream)
|
|
1013
867
|
return "";
|
|
@@ -1543,6 +1397,7 @@ var FleetTimeController = class {
|
|
|
1543
1397
|
};
|
|
1544
1398
|
|
|
1545
1399
|
// ../adapters/node/dist/fleet-management.js
|
|
1400
|
+
var bootArchive = (value) => !!value && typeof value === "object" && "format" in value && value.format === "mockingbird.boot/v1";
|
|
1546
1401
|
var object2 = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
1547
1402
|
var fleetError = (status, message, extra = {}) => Response.json({
|
|
1548
1403
|
...extra,
|
|
@@ -1661,7 +1516,7 @@ var FleetManagement = class {
|
|
|
1661
1516
|
async finishRequest(service, namespace, _request, response, log, token) {
|
|
1662
1517
|
if (this.requestPath(service, _request).admin)
|
|
1663
1518
|
return;
|
|
1664
|
-
const pathFingerprint =
|
|
1519
|
+
const pathFingerprint = createHash2("sha256").update(log.path).digest("hex");
|
|
1665
1520
|
this.observations.record({
|
|
1666
1521
|
namespace,
|
|
1667
1522
|
service,
|
|
@@ -1938,6 +1793,8 @@ data: ${JSON.stringify(data)}
|
|
|
1938
1793
|
["GET", "/namespaces/{namespace}/snapshots/{id}"],
|
|
1939
1794
|
["DELETE", "/namespaces/{namespace}/snapshots/{id}"],
|
|
1940
1795
|
["POST", "/namespaces/{namespace}/snapshots/{id}/restore"],
|
|
1796
|
+
["GET", "/services/{service}/upstream"],
|
|
1797
|
+
["PUT", "/services/{service}/upstream"],
|
|
1941
1798
|
["GET", "/metrics"],
|
|
1942
1799
|
["GET", "/clock"],
|
|
1943
1800
|
["POST", "/clock"],
|
|
@@ -2376,6 +2233,18 @@ data: ${JSON.stringify(data)}
|
|
|
2376
2233
|
resolution: "restored-before-state"
|
|
2377
2234
|
});
|
|
2378
2235
|
}
|
|
2236
|
+
const upstreamService = /^\/services\/([a-z0-9-]+)\/upstream$/.exec(route)?.[1];
|
|
2237
|
+
if (upstreamService && ["GET", "PUT"].includes(method)) {
|
|
2238
|
+
if (!admin)
|
|
2239
|
+
return fleetError(403, "upstream controls require administrator scope");
|
|
2240
|
+
const child = this.ctx.children.get(upstreamService);
|
|
2241
|
+
if (!child)
|
|
2242
|
+
return fleetError(404, "unknown service");
|
|
2243
|
+
if (!child.upstream)
|
|
2244
|
+
return fleetError(409, "upstream forwarding is not supported by service");
|
|
2245
|
+
checked(body, ["mode", "baseUrl"]);
|
|
2246
|
+
return child.upstream(namespace, method === "PUT" ? body : void 0);
|
|
2247
|
+
}
|
|
2379
2248
|
if (route === "/telemetry" && method === "GET")
|
|
2380
2249
|
return admin ? Response.json(this.instrumentation.snapshot()) : fleetError(403, "operator telemetry requires administrator scope");
|
|
2381
2250
|
if (route === "/audit" && method === "GET")
|
|
@@ -3074,9 +2943,10 @@ data: ${JSON.stringify(data)}
|
|
|
3074
2943
|
for (const [namespace, ns] of entries)
|
|
3075
2944
|
for (const [name, state] of Object.entries(ns.services)) {
|
|
3076
2945
|
const child = this.ctx.children.get(name);
|
|
3077
|
-
if (!child || child.protocol !== state.protocol || !child.importCheckpoint || (state.target ?? name) !== (child.targetName ?? name))
|
|
2946
|
+
if (!child || child.protocol !== state.protocol || !child.importCheckpoint || (state.target ?? name) !== (child.targetName ?? name) || state.packageVersion !== void 0 && child.packageVersion !== void 0 && state.packageVersion !== child.packageVersion)
|
|
3078
2947
|
throw new Error(`unsupported archive service ${name}`);
|
|
3079
|
-
|
|
2948
|
+
if (!bootArchive(state.state))
|
|
2949
|
+
await child.importCheckpoint(namespace, state.state);
|
|
3080
2950
|
}
|
|
3081
2951
|
const rollback = [];
|
|
3082
2952
|
const created = [];
|
|
@@ -3093,7 +2963,10 @@ data: ${JSON.stringify(data)}
|
|
|
3093
2963
|
const oldClock = this.ctx.clock(namespace).state();
|
|
3094
2964
|
const response = await this.ctx.transaction(namespace, selected, async (name, child) => {
|
|
3095
2965
|
values.set(name, await child.checkpoint(namespace));
|
|
3096
|
-
|
|
2966
|
+
if (bootArchive(ns.services[name]?.state))
|
|
2967
|
+
await child.reset(namespace);
|
|
2968
|
+
else
|
|
2969
|
+
await child.restore(namespace, await child.importCheckpoint?.(namespace, ns.services[name]?.state));
|
|
3097
2970
|
}, true);
|
|
3098
2971
|
if (!response.ok)
|
|
3099
2972
|
throw new Error(`restore ${namespace} failed: ${canonical(await response.json())}`);
|
|
@@ -3506,6 +3379,19 @@ var fleet_config_schema_default = {
|
|
|
3506
3379
|
},
|
|
3507
3380
|
publicPath: {
|
|
3508
3381
|
type: "string"
|
|
3382
|
+
},
|
|
3383
|
+
upstream: {
|
|
3384
|
+
type: "object",
|
|
3385
|
+
additionalProperties: false,
|
|
3386
|
+
required: ["baseUrl"],
|
|
3387
|
+
properties: {
|
|
3388
|
+
baseUrl: {
|
|
3389
|
+
type: "string"
|
|
3390
|
+
},
|
|
3391
|
+
mode: {
|
|
3392
|
+
enum: ["emulated", "forward"]
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3509
3395
|
}
|
|
3510
3396
|
}
|
|
3511
3397
|
},
|
|
@@ -3715,6 +3601,16 @@ async function preflightFleet(config, options) {
|
|
|
3715
3601
|
throw new Error("settings: only HTTP services take boot settings");
|
|
3716
3602
|
checkSettings(target, entry.settings, "settings");
|
|
3717
3603
|
}
|
|
3604
|
+
if (entry.upstream) {
|
|
3605
|
+
if (!("create" in target) || target.supportsUpstream !== true)
|
|
3606
|
+
throw new Error("upstream forwarding is not supported by target");
|
|
3607
|
+
if (!options.upstreams?.[name]?.fetch)
|
|
3608
|
+
throw new Error("upstream requires a host-injected fetch");
|
|
3609
|
+
const targetUrl = new URL(entry.upstream.baseUrl);
|
|
3610
|
+
if (!["http:", "https:"].includes(targetUrl.protocol) || targetUrl.username || targetUrl.password || targetUrl.search || targetUrl.hash)
|
|
3611
|
+
throw new Error("invalid upstream baseUrl");
|
|
3612
|
+
upstreamRequest(new Request("http://fixture.invalid/"), entry.upstream.baseUrl);
|
|
3613
|
+
}
|
|
3718
3614
|
const port = entry.port ?? target.defaultPort;
|
|
3719
3615
|
if (port !== 0) {
|
|
3720
3616
|
const key = `${entry.host ?? "127.0.0.1"}:${port}`;
|
|
@@ -3750,7 +3646,7 @@ async function preflightFleet(config, options) {
|
|
|
3750
3646
|
break;
|
|
3751
3647
|
}
|
|
3752
3648
|
}
|
|
3753
|
-
targets.set(name, { ...target, packageVersion: packageVersion ?? "0.
|
|
3649
|
+
targets.set(name, { ...target, packageVersion: packageVersion ?? "0.3.0" });
|
|
3754
3650
|
} catch (error) {
|
|
3755
3651
|
throw new Error(`service ${name}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
3756
3652
|
}
|
|
@@ -4077,9 +3973,10 @@ var removeOwned = async (path, id) => {
|
|
|
4077
3973
|
throw error;
|
|
4078
3974
|
}
|
|
4079
3975
|
};
|
|
4080
|
-
var httpChild = async (target, entry, options, admission, generation, privateAccess, observer) => {
|
|
3976
|
+
var httpChild = async (name, target, entry, options, admission, generation, privateAccess, observer) => {
|
|
4081
3977
|
const requestLog = new AsyncLocalStorage2();
|
|
4082
3978
|
const created = await target.create(entry.options ?? {}, {
|
|
3979
|
+
...entry.upstream && options.upstreams?.[name] ? { upstream: { ...entry.upstream, ...options.upstreams[name] } } : {},
|
|
4083
3980
|
adminPrefix: resolveAdminPrefix(entry.adminPrefix),
|
|
4084
3981
|
adminKey: entry.adminKey,
|
|
4085
3982
|
seed: entry.seed,
|
|
@@ -4255,7 +4152,7 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
|
|
|
4255
4152
|
return {
|
|
4256
4153
|
entries: entries.slice(0, limit).map(({ method, path, count }) => ({
|
|
4257
4154
|
method,
|
|
4258
|
-
pathFingerprint:
|
|
4155
|
+
pathFingerprint: createHash3("sha256").update(path).digest("hex"),
|
|
4259
4156
|
count
|
|
4260
4157
|
})),
|
|
4261
4158
|
truncated: entries.length > limit
|
|
@@ -4272,6 +4169,17 @@ var httpChild = async (target, entry, options, admission, generation, privateAcc
|
|
|
4272
4169
|
runtime.instance(namespace);
|
|
4273
4170
|
},
|
|
4274
4171
|
deleteNamespace: (namespace) => runtime.dropNamespace(namespace),
|
|
4172
|
+
async upstream(namespace, settings) {
|
|
4173
|
+
return runtime.fetch(new Request(`http://fleet.internal${resolveAdminPrefix(entry.adminPrefix)}/upstream`, {
|
|
4174
|
+
method: settings ? "PUT" : "GET",
|
|
4175
|
+
headers: {
|
|
4176
|
+
"content-type": "application/json",
|
|
4177
|
+
"x-mockingbird-namespace": namespace,
|
|
4178
|
+
...entry.adminKey ? { "x-mockingbird-admin-key": entry.adminKey } : {}
|
|
4179
|
+
},
|
|
4180
|
+
...settings ? { body: JSON.stringify(settings) } : {}
|
|
4181
|
+
}));
|
|
4182
|
+
},
|
|
4275
4183
|
async configure(namespace, controls) {
|
|
4276
4184
|
return runtime.configure(controls, namespace);
|
|
4277
4185
|
},
|
|
@@ -4708,7 +4616,7 @@ async function startFleet(config, options) {
|
|
|
4708
4616
|
const child = "start" in target ? await target.start({
|
|
4709
4617
|
...entry,
|
|
4710
4618
|
...config.adminControls !== void 0 || entry.adminControls !== void 0 ? { adminControls: { ...config.adminControls, ...entry.adminControls } } : {}
|
|
4711
|
-
}) : await httpChild(target, {
|
|
4619
|
+
}) : await httpChild(name, target, {
|
|
4712
4620
|
...entry,
|
|
4713
4621
|
adminPrefix: resolveAdminPrefix(entry.adminPrefix ?? adminPrefix),
|
|
4714
4622
|
...adminKey !== void 0 ? { adminKey } : {},
|