@powerhousedao/ph-run 0.0.0-stage → 6.2.3-dev.101
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/LICENSE +661 -0
- package/dist/classic-args-BYccyIIC.mjs +36 -0
- package/dist/classic-args-BYccyIIC.mjs.map +1 -0
- package/dist/classic-args-Dm0_3O5p.d.mts +194 -0
- package/dist/classic-args-Dm0_3O5p.d.mts.map +1 -0
- package/dist/cli.d.mts +2 -0
- package/dist/cli.mjs +32 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/connect-plugin-configs-CPRA8hQJ.mjs +50 -0
- package/dist/connect-plugin-configs-CPRA8hQJ.mjs.map +1 -0
- package/dist/connect-serve-1WAi0KJL.mjs +201 -0
- package/dist/connect-serve-1WAi0KJL.mjs.map +1 -0
- package/dist/connect-serve-COWXf_41.d.mts +64 -0
- package/dist/connect-serve-COWXf_41.d.mts.map +1 -0
- package/dist/member-config-Bs3yOT6y.mjs +161 -0
- package/dist/member-config-Bs3yOT6y.mjs.map +1 -0
- package/dist/runners/connect.d.mts +1271 -0
- package/dist/runners/connect.d.mts.map +1 -0
- package/dist/runners/connect.mjs +273 -0
- package/dist/runners/connect.mjs.map +1 -0
- package/dist/runners/member-config.d.mts +796 -0
- package/dist/runners/member-config.d.mts.map +1 -0
- package/dist/runners/member-config.mjs +4 -0
- package/dist/runners/switchboard.d.mts +1754 -0
- package/dist/runners/switchboard.d.mts.map +1 -0
- package/dist/runners/switchboard.mjs +714 -0
- package/dist/runners/switchboard.mjs.map +1 -0
- package/dist/services/classic-args.d.mts +3 -0
- package/dist/services/classic-args.mjs +4 -0
- package/dist/services/connect-serve.d.mts +3 -0
- package/dist/services/connect-serve.mjs +4 -0
- package/dist/services/connect-server.d.mts +26 -0
- package/dist/services/connect-server.d.mts.map +1 -0
- package/dist/services/connect-server.mjs +219 -0
- package/dist/services/connect-server.mjs.map +1 -0
- package/dist/services/run/start-run.d.mts +29 -0
- package/dist/services/run/start-run.d.mts.map +1 -0
- package/dist/services/run/start-run.mjs +4 -0
- package/dist/services/switchboard.d.mts +79 -0
- package/dist/services/switchboard.d.mts.map +1 -0
- package/dist/services/switchboard.mjs +191 -0
- package/dist/services/switchboard.mjs.map +1 -0
- package/dist/start-run-DWn2cVyi.mjs +125 -0
- package/dist/start-run-DWn2cVyi.mjs.map +1 -0
- package/dist/superseded-config-CpHrCuNq.mjs +53 -0
- package/dist/superseded-config-CpHrCuNq.mjs.map +1 -0
- package/package.json +93 -3
- package/README.md +0 -3
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { t as ConnectPreviewArgs } from "./classic-args-Dm0_3O5p.mjs";
|
|
2
|
+
import { PHConnectRuntimeConfig } from "@powerhousedao/shared/clis";
|
|
3
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
4
|
+
//#region src/services/connect-serve.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Runtime shaping for a served (built) Connect, desktop shape: the compound
|
|
7
|
+
* projects the live switchboard's drive into the connect member's config, and
|
|
8
|
+
* this override makes the STATIC dist serve it — the built
|
|
9
|
+
* powerhouse.config.json is baked, so without this the drive injection never
|
|
10
|
+
* reaches the SPA. `cspRegistrySync` additionally patches the baked CSP's
|
|
11
|
+
* script-src with the runtime registry (the build bakes the CSP for the
|
|
12
|
+
* registry it was built against; setting the registry at run time breaks that
|
|
13
|
+
* coupling and every package import gets blocked otherwise).
|
|
14
|
+
*/
|
|
15
|
+
interface ConnectServeOverride {
|
|
16
|
+
connect?: PHConnectRuntimeConfig;
|
|
17
|
+
cspRegistrySync?: boolean;
|
|
18
|
+
/** Overrides the baked top-level packageRegistryUrl in the served config. */
|
|
19
|
+
packageRegistryUrl?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The env contract a supervisor can drive the preview with — the same
|
|
23
|
+
* variables the Docker entrypoint and clint's service manager already speak:
|
|
24
|
+
* PH_CONNECT_DEFAULT_DRIVES_URL (comma-separated drive urls, injected into
|
|
25
|
+
* drives.defaultDrives), PH_CONNECT_DRIVES_PRESERVE_STRATEGY, and
|
|
26
|
+
* PH_CONNECT_PACKAGES_REGISTRY (runtime registry: served config + baked-CSP
|
|
27
|
+
* patch). Lets `ph connect preview` replace hand-rolled static servers
|
|
28
|
+
* (clint's connect-server, the ph-win sidecar) without losing injection.
|
|
29
|
+
*/
|
|
30
|
+
declare function overrideFromEnv(env?: Record<string, string | undefined>): ConnectServeOverride | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Append the registry origin to the baked CSP's script-src. The attribute
|
|
33
|
+
* value is HTML-escaped (every CSP keyword is `'`-quoted, which contains a
|
|
34
|
+
* semicolon), so decode before touching directives and re-escape after —
|
|
35
|
+
* ported from the ph-win-desktop sidecar, which this path replaces.
|
|
36
|
+
*/
|
|
37
|
+
declare function patchCspScriptSrc(html: string, registryUrl: string): string;
|
|
38
|
+
declare function resolvePreviewBase(argBase: string | undefined, outDirAbs: string): string | undefined;
|
|
39
|
+
declare function stripBase(pathname: string, base: string): string;
|
|
40
|
+
/** The steps both Connect servers run, in the order they run them. */
|
|
41
|
+
type ConnectMiddleware = (req: IncomingMessage, res: ServerResponse, next: () => void) => void;
|
|
42
|
+
declare function previewCacheControl(pathname: string, search: string): string | undefined;
|
|
43
|
+
declare function cacheHeaders(outDir: string, base: string): ConnectMiddleware;
|
|
44
|
+
type HtmlTransform = (html: string) => string;
|
|
45
|
+
/** The dist's shell for the served config: its composition's variant and its app name. */
|
|
46
|
+
declare function shellSwapFor(outDir: string, override: ConnectServeOverride, base?: string): HtmlTransform | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Whether a request on a proxying server belongs to the switchboard: its
|
|
49
|
+
* paths go to the proxy, untouched, except a browser navigation under `/d/`
|
|
50
|
+
* (Connect's own drive routes), as switchboardProxy splits them.
|
|
51
|
+
*/
|
|
52
|
+
declare function isProxiedRequest(urlPath: string, accept: string | undefined): boolean;
|
|
53
|
+
declare function serveOverrideStep(outDir: string, override: ConnectServeOverride, base?: string, swapShell?: HtmlTransform, proxied?: boolean): ConnectMiddleware;
|
|
54
|
+
/** Extensions whose files have `.gz` siblings. */
|
|
55
|
+
declare const PRECOMPRESS_EXTENSIONS: readonly [".wasm", ".data"];
|
|
56
|
+
declare function precompressedAssets(outDir: string, base?: string): ConnectMiddleware;
|
|
57
|
+
/** Throws, naming the missing file and the build, when `outDirAbs` holds no build. */
|
|
58
|
+
declare function assertConnectBuild(outDirArg: string, outDirAbs: string, dirname: string): void;
|
|
59
|
+
/** The served base: `--base`, else what the dist or the project was built with. */
|
|
60
|
+
declare function connectBaseOf(args: Pick<ConnectPreviewArgs, "connectBasePath">, outDirAbs: string, dirname: string): string;
|
|
61
|
+
//#endregion
|
|
62
|
+
export { cacheHeaders as a, overrideFromEnv as c, previewCacheControl as d, resolvePreviewBase as f, stripBase as h, assertConnectBuild as i, patchCspScriptSrc as l, shellSwapFor as m, ConnectServeOverride as n, connectBaseOf as o, serveOverrideStep as p, PRECOMPRESS_EXTENSIONS as r, isProxiedRequest as s, ConnectMiddleware as t, precompressedAssets as u };
|
|
63
|
+
export {};
|
|
64
|
+
//# sourceMappingURL=connect-serve-COWXf_41.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-serve-COWXf_41.d.mts","names":[],"sources":["../src/services/connect-serve.ts"],"mappings":";;;;;;;;;;;;;;UAmCiB;EACf,UAAU;EACV;;EAEA;;;;;;;;;;;iBAYc,gBACd,MAAK,qCACJ;;;;;;;iBAsDa,kBAAkB,cAAc;iBAkBhC,mBACd,6BACA;iBAkBc,UAAU,kBAAkB;;KAOhC,qBACV,KAAK,iBACL,KAAK,gBACL;iBAQc,oBACd,kBACA;iBAec,aAAa,gBAAgB,eAAe;KAcvD,iBAAiB;;iBAGN,aACd,gBACA,UAAU,sBACV,gBACC;;;;;;iBA6Ba,iBAAiB,iBAAiB;iBAKlC,kBACd,gBACA,UAAU,sBACV,eACA,YAAY,eACZ,oBACC;;cA+CU;iBAQG,oBACd,gBACA,gBACC;;iBAqCa,mBACd,mBACA,mBACA;;iBA4Bc,cACd,MAAM,KAAK,wCACX,mBACA"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="b275fe92-4897-57b6-a224-4881b6d414db")}catch(e){}}();
|
|
3
|
+
import { assertProtocolConsistency, primaryBackend, protocolEnv, reactorBlockToSwitchboardConfig } from "@powerhousedao/host-core";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import { mergeSwitchboardConfig, switchboardConfigLayerSchema } from "@powerhousedao/switchboard/config";
|
|
7
|
+
import { envNamesBeforeProjectEnv, isDotenvDisabled } from "@powerhousedao/shared/clis/project-env";
|
|
8
|
+
import dotenv from "dotenv";
|
|
9
|
+
//#region src/runners/member-config.ts
|
|
10
|
+
function readDotenv(cwd) {
|
|
11
|
+
try {
|
|
12
|
+
return dotenv.parse(fs.readFileSync(path.join(cwd, ".env")));
|
|
13
|
+
} catch {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const shellNamesByEnv = /* @__PURE__ */ new WeakMap();
|
|
18
|
+
/** Records which names of `env` the shell set, before a `.env` loads into it. */
|
|
19
|
+
function rememberShellNames(env, names) {
|
|
20
|
+
shellNamesByEnv.set(env, new Set(names));
|
|
21
|
+
}
|
|
22
|
+
function shellNamesOf(env) {
|
|
23
|
+
let names = shellNamesByEnv.get(env);
|
|
24
|
+
if (!names) {
|
|
25
|
+
names = (env === process.env ? envNamesBeforeProjectEnv() : void 0) ?? new Set(Object.keys(env));
|
|
26
|
+
shellNamesByEnv.set(env, names);
|
|
27
|
+
}
|
|
28
|
+
return names;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Splits `env` into the shell's values and the `.env` the switchboard server
|
|
32
|
+
* loads from `cwd` (the same file). A name the shell set stays the shell's,
|
|
33
|
+
* whatever the file says.
|
|
34
|
+
*/
|
|
35
|
+
function memberEnvOf(env, cwd = process.cwd()) {
|
|
36
|
+
const file = isDotenvDisabled({ ...env }) ? {} : readDotenv(cwd);
|
|
37
|
+
const shellNames = shellNamesOf(env);
|
|
38
|
+
const shell = {};
|
|
39
|
+
for (const [name, value] of Object.entries(env)) if (file[name] === void 0 || shellNames.has(name)) shell[name] = value;
|
|
40
|
+
const fromFile = {};
|
|
41
|
+
for (const [name, value] of Object.entries(file)) if (!shellNames.has(name)) fromFile[name] = value;
|
|
42
|
+
return {
|
|
43
|
+
shell,
|
|
44
|
+
dotenv: fromFile
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** The env vars the block sets, which a `.env` value does not override. */
|
|
48
|
+
function blockEnvNames(reactor) {
|
|
49
|
+
const block = reactorBlockToSwitchboardConfig(reactor);
|
|
50
|
+
return [
|
|
51
|
+
...Object.keys(protocolEnv(reactor?.protocol)),
|
|
52
|
+
...block.packages?.dynamic ? ["DYNAMIC_MODEL_LOADING"] : [],
|
|
53
|
+
...block.features ? ["PH_WORKFLOWS_ENABLED"] : []
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
/** `config.switchboard`: a member's own switchboard config. The omitted keys
|
|
57
|
+
* live in the reactor block (storage, protocol, dynamic packages) or the
|
|
58
|
+
* runner (port, dev verb, drives, plugins), or are process-wide or not JSON. */
|
|
59
|
+
const memberSwitchboardConfigSchema = switchboardConfigLayerSchema([
|
|
60
|
+
"logLevel",
|
|
61
|
+
"server.port",
|
|
62
|
+
"server.dev",
|
|
63
|
+
"storage.reactor",
|
|
64
|
+
"storage.readModel",
|
|
65
|
+
"protocol",
|
|
66
|
+
"telemetry",
|
|
67
|
+
"packages.dynamic",
|
|
68
|
+
"drive",
|
|
69
|
+
"remoteDrives",
|
|
70
|
+
"features.pluginHost",
|
|
71
|
+
"processorConfig"
|
|
72
|
+
]);
|
|
73
|
+
function isPlainObject(value) {
|
|
74
|
+
return typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
|
|
75
|
+
}
|
|
76
|
+
/** The dotted keys both layers set. */
|
|
77
|
+
function overlappingKeys(a, b, path = "") {
|
|
78
|
+
return Object.entries(b).flatMap(([key, value]) => {
|
|
79
|
+
const other = a[key];
|
|
80
|
+
if (value === void 0 || other === void 0) return [];
|
|
81
|
+
const at = path ? `${path}.${key}` : key;
|
|
82
|
+
return isPlainObject(value) && isPlainObject(other) ? overlappingKeys(other, value, at) : [at];
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/** Each `config.switchboard` key the reactor block already sets. */
|
|
86
|
+
function reactorBlockConflicts(member) {
|
|
87
|
+
const own = member.config?.switchboard;
|
|
88
|
+
if (!own) return [];
|
|
89
|
+
return overlappingKeys(reactorBlockToSwitchboardConfig(member.reactor), own).map((key) => `config.switchboard.${key} is also set by the reactor block; set it in one place.`);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The deprecation notice for a block whose `readModel.url` still names the
|
|
93
|
+
* reactor store too: nothing else names the store, as it did when
|
|
94
|
+
* readModel.url rode `dbPath`.
|
|
95
|
+
*/
|
|
96
|
+
function readModelAsReactorStore(reactor, env, hostFileLabel) {
|
|
97
|
+
const storage = reactorBlockToSwitchboardConfig(reactor).storage;
|
|
98
|
+
const url = storage?.readModel?.url;
|
|
99
|
+
if (!url || storage.reactor || env.PH_REACTOR_DATABASE_URL !== void 0 || env.PH_SWITCHBOARD_DATABASE_URL !== void 0) return;
|
|
100
|
+
return {
|
|
101
|
+
url,
|
|
102
|
+
warning: `${hostFileLabel}: reactor readModel.url (${url}) also holds the reactor store, because no storage names one. This is deprecated: set the backend's storage explicitly (the same Postgres url, or a path dataDir).`
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The member's switchboard config under the runner's own arguments: its
|
|
107
|
+
* reactor block, then `config.switchboard`, then the env over both, mapped
|
|
108
|
+
* once as the server read it (the shell over the `.env`, whose values for the
|
|
109
|
+
* block's own vars are dropped). The block keeps the precedence it had as
|
|
110
|
+
* set-if-absent env and `dbPath`: only a shell PH_REACTOR_DATABASE_URL
|
|
111
|
+
* overrides its store, and no env var overrides its read model. Writes nothing.
|
|
112
|
+
*/
|
|
113
|
+
async function memberSwitchboardConfig(member, env, options) {
|
|
114
|
+
const { switchboardConfigFromEnv } = await import("@powerhousedao/switchboard/config/env");
|
|
115
|
+
const block = reactorBlockToSwitchboardConfig(member.reactor);
|
|
116
|
+
const legacy = readModelAsReactorStore(member.reactor, env.shell, "");
|
|
117
|
+
if (legacy) block.storage = {
|
|
118
|
+
...block.storage,
|
|
119
|
+
reactor: { url: legacy.url }
|
|
120
|
+
};
|
|
121
|
+
const dotenvValues = { ...env.dotenv };
|
|
122
|
+
for (const name of blockEnvNames(member.reactor)) delete dotenvValues[name];
|
|
123
|
+
const fromEnv = switchboardConfigFromEnv({
|
|
124
|
+
...dotenvValues,
|
|
125
|
+
...env.shell
|
|
126
|
+
}, { logger: options.logger });
|
|
127
|
+
const storage = { ...fromEnv.storage };
|
|
128
|
+
if (block.storage?.reactor && env.shell.PH_REACTOR_DATABASE_URL === void 0) delete storage.reactor;
|
|
129
|
+
if (block.storage?.readModel) delete storage.readModel;
|
|
130
|
+
return mergeSwitchboardConfig(options.defaults ?? {}, block, member.config?.switchboard ?? {}, {
|
|
131
|
+
...fromEnv,
|
|
132
|
+
storage
|
|
133
|
+
}, options.pluginHost ? { features: { pluginHost: true } } : {});
|
|
134
|
+
}
|
|
135
|
+
/** A Postgres store no url names: the server would use a folder instead. */
|
|
136
|
+
function missingPostgresUrl(reactor, config, hostFileLabel) {
|
|
137
|
+
if (primaryBackend(reactor)?.storage?.kind !== "postgres") return void 0;
|
|
138
|
+
if (config.storage?.reactor?.url) return void 0;
|
|
139
|
+
return `${hostFileLabel}: the reactor's storage is Postgres, but nothing names the database. Set the backend's storage.url, or PH_REACTOR_DATABASE_URL.`;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Fails when the operator's env contradicts the member's protocol block, then
|
|
143
|
+
* creates the block's path-storage dir (the server's own mkdir is not
|
|
144
|
+
* recursive).
|
|
145
|
+
*/
|
|
146
|
+
function prepareMemberStorage(reactor, env, hostFileLabel) {
|
|
147
|
+
assertProtocolConsistency(reactor?.protocol, { ...env }, hostFileLabel);
|
|
148
|
+
const storage = primaryBackend(reactor)?.storage;
|
|
149
|
+
if (storage?.kind !== "path" || !storage.dataDir) return;
|
|
150
|
+
if (/\$\{(userData|projectRoot)\}/.test(storage.dataDir)) throw new Error(`${hostFileLabel}: storage.dataDir "${storage.dataDir}" was not resolved; resolve the host (resolveHostOutputs) before starting it.`);
|
|
151
|
+
fs.mkdirSync(storage.dataDir, { recursive: true });
|
|
152
|
+
}
|
|
153
|
+
/** The verb owns dev mode; `runner.dev` is the host-file spelling of it. */
|
|
154
|
+
function switchboardDevMode(devVerb, runner) {
|
|
155
|
+
return devVerb || runner.dev === true;
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
export { overlappingKeys as a, readModelAsReactorStore as c, missingPostgresUrl as i, rememberShellNames as l, memberSwitchboardConfig as n, prepareMemberStorage as o, memberSwitchboardConfigSchema as r, reactorBlockConflicts as s, memberEnvOf as t, switchboardDevMode as u };
|
|
159
|
+
|
|
160
|
+
//# sourceMappingURL=member-config-Bs3yOT6y.mjs.map
|
|
161
|
+
//# debugId=b275fe92-4897-57b6-a224-4881b6d414db
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"member-config-Bs3yOT6y.mjs","sources":["../src/runners/member-config.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport path from \"node:path\";\nimport {\n assertProtocolConsistency,\n primaryBackend,\n protocolEnv,\n reactorBlockToSwitchboardConfig,\n type ReactorConfig,\n} from \"@powerhousedao/host-core\";\nimport {\n mergeSwitchboardConfig,\n switchboardConfigLayerSchema,\n type SwitchboardConfigInput,\n} from \"@powerhousedao/switchboard/config\";\nimport {\n envNamesBeforeProjectEnv,\n isDotenvDisabled,\n} from \"@powerhousedao/shared/clis/project-env\";\nimport type { ILogger } from \"document-model\";\nimport dotenv from \"dotenv\";\n\ntype Env = Readonly<Record<string, string | undefined>>;\n\n/** The operator's env, split by where each value came from. */\nexport interface MemberEnv {\n /** Set by the shell: wins over the host file. */\n shell: Env;\n /** Only in the project `.env`: the host file's keys win over these. */\n dotenv: Env;\n}\n\n// The server's dotenv.config() ignores a file it cannot read.\nfunction readDotenv(cwd: string): Record<string, string> {\n try {\n return dotenv.parse(fs.readFileSync(path.join(cwd, \".env\")));\n } catch {\n return {};\n }\n}\n\nconst shellNamesByEnv = new WeakMap<object, ReadonlySet<string>>();\n\n/** Records which names of `env` the shell set, before a `.env` loads into it. */\nexport function rememberShellNames(env: Env, names: Iterable<string>): void {\n shellNamesByEnv.set(env, new Set(names));\n}\n\n// Taken the first time an env is seen: before the first member's server\n// import loads the .env into it, and reused for every later member.\nfunction shellNamesOf(env: Env): ReadonlySet<string> {\n let names = shellNamesByEnv.get(env);\n if (!names) {\n names =\n (env === process.env ? envNamesBeforeProjectEnv() : undefined) ??\n new Set(Object.keys(env));\n shellNamesByEnv.set(env, names);\n }\n return names;\n}\n\n/**\n * Splits `env` into the shell's values and the `.env` the switchboard server\n * loads from `cwd` (the same file). A name the shell set stays the shell's,\n * whatever the file says.\n */\nexport function memberEnvOf(env: Env, cwd: string = process.cwd()): MemberEnv {\n const file = isDotenvDisabled({ ...env }) ? {} : readDotenv(cwd);\n const shellNames = shellNamesOf(env);\n const shell: Record<string, string | undefined> = {};\n for (const [name, value] of Object.entries(env)) {\n if (file[name] === undefined || shellNames.has(name)) shell[name] = value;\n }\n const fromFile: Record<string, string> = {};\n for (const [name, value] of Object.entries(file)) {\n if (!shellNames.has(name)) fromFile[name] = value;\n }\n return { shell, dotenv: fromFile };\n}\n\n/** The env vars the block sets, which a `.env` value does not override. */\nfunction blockEnvNames(reactor: ReactorConfig | undefined): string[] {\n const block = reactorBlockToSwitchboardConfig(reactor);\n return [\n ...Object.keys(protocolEnv(reactor?.protocol)),\n ...(block.packages?.dynamic ? [\"DYNAMIC_MODEL_LOADING\"] : []),\n ...(block.features ? [\"PH_WORKFLOWS_ENABLED\"] : []),\n ];\n}\n\n/** `config.switchboard`: a member's own switchboard config. The omitted keys\n * live in the reactor block (storage, protocol, dynamic packages) or the\n * runner (port, dev verb, drives, plugins), or are process-wide or not JSON. */\nexport const memberSwitchboardConfigSchema = switchboardConfigLayerSchema([\n \"logLevel\",\n \"server.port\",\n \"server.dev\",\n \"storage.reactor\",\n \"storage.readModel\",\n \"protocol\",\n \"telemetry\",\n \"packages.dynamic\",\n \"drive\",\n \"remoteDrives\",\n \"features.pluginHost\",\n \"processorConfig\",\n]);\n\nexport interface SwitchboardMember {\n reactor?: ReactorConfig;\n config?: { switchboard?: SwitchboardConfigInput };\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n return (\n typeof value === \"object\" &&\n value !== null &&\n Object.getPrototypeOf(value) === Object.prototype\n );\n}\n\n/** The dotted keys both layers set. */\nexport function overlappingKeys(\n a: Record<string, unknown>,\n b: Record<string, unknown>,\n path = \"\",\n): string[] {\n return Object.entries(b).flatMap(([key, value]) => {\n const other = a[key];\n if (value === undefined || other === undefined) return [];\n const at = path ? `${path}.${key}` : key;\n return isPlainObject(value) && isPlainObject(other)\n ? overlappingKeys(other, value, at)\n : [at];\n });\n}\n\n/** Each `config.switchboard` key the reactor block already sets. */\nexport function reactorBlockConflicts(member: SwitchboardMember): string[] {\n const own = member.config?.switchboard;\n if (!own) return [];\n return overlappingKeys(\n reactorBlockToSwitchboardConfig(member.reactor) as Record<string, unknown>,\n own as Record<string, unknown>,\n ).map(\n (key) =>\n `config.switchboard.${key} is also set by the reactor block; set it in one place.`,\n );\n}\n\n/**\n * The deprecation notice for a block whose `readModel.url` still names the\n * reactor store too: nothing else names the store, as it did when\n * readModel.url rode `dbPath`.\n */\nexport function readModelAsReactorStore(\n reactor: ReactorConfig | undefined,\n env: Env,\n hostFileLabel: string,\n): { url: string; warning: string } | undefined {\n const storage = reactorBlockToSwitchboardConfig(reactor).storage;\n const url = storage?.readModel?.url;\n if (\n !url ||\n storage.reactor ||\n env.PH_REACTOR_DATABASE_URL !== undefined ||\n env.PH_SWITCHBOARD_DATABASE_URL !== undefined\n ) {\n return undefined;\n }\n return {\n url,\n warning:\n `${hostFileLabel}: reactor readModel.url (${url}) also holds the reactor store, because no storage names one. ` +\n \"This is deprecated: set the backend's storage explicitly (the same Postgres url, or a path dataDir).\",\n };\n}\n\n/**\n * The member's switchboard config under the runner's own arguments: its\n * reactor block, then `config.switchboard`, then the env over both, mapped\n * once as the server read it (the shell over the `.env`, whose values for the\n * block's own vars are dropped). The block keeps the precedence it had as\n * set-if-absent env and `dbPath`: only a shell PH_REACTOR_DATABASE_URL\n * overrides its store, and no env var overrides its read model. Writes nothing.\n */\nexport async function memberSwitchboardConfig(\n member: SwitchboardMember,\n env: MemberEnv,\n options: {\n pluginHost: boolean;\n logger?: ILogger;\n /** Under everything else: values no flag or host file set. */\n defaults?: SwitchboardConfigInput;\n },\n): Promise<SwitchboardConfigInput> {\n // Lazy: the env mapping loads reactor-api, which the runner module must not.\n const { switchboardConfigFromEnv } =\n await import(\"@powerhousedao/switchboard/config/env\");\n const block = reactorBlockToSwitchboardConfig(member.reactor);\n const legacy = readModelAsReactorStore(member.reactor, env.shell, \"\");\n if (legacy) {\n block.storage = { ...block.storage, reactor: { url: legacy.url } };\n }\n // One env, as the server read it: the .env under the block's own vars,\n // the shell over both.\n const dotenvValues: Record<string, string | undefined> = { ...env.dotenv };\n for (const name of blockEnvNames(member.reactor)) delete dotenvValues[name];\n const fromEnv = switchboardConfigFromEnv(\n { ...dotenvValues, ...env.shell },\n { logger: options.logger },\n );\n const storage = { ...fromEnv.storage };\n if (\n block.storage?.reactor &&\n env.shell.PH_REACTOR_DATABASE_URL === undefined\n ) {\n delete storage.reactor;\n }\n if (block.storage?.readModel) delete storage.readModel;\n return mergeSwitchboardConfig(\n options.defaults ?? {},\n block,\n member.config?.switchboard ?? {},\n { ...fromEnv, storage },\n // A declared composition turns the plugin host on, whatever the env says.\n options.pluginHost ? { features: { pluginHost: true } } : {},\n );\n}\n\n/** A Postgres store no url names: the server would use a folder instead. */\nexport function missingPostgresUrl(\n reactor: ReactorConfig | undefined,\n config: SwitchboardConfigInput,\n hostFileLabel: string,\n): string | undefined {\n if (primaryBackend(reactor)?.storage?.kind !== \"postgres\") return undefined;\n if (config.storage?.reactor?.url) return undefined;\n return `${hostFileLabel}: the reactor's storage is Postgres, but nothing names the database. Set the backend's storage.url, or PH_REACTOR_DATABASE_URL.`;\n}\n\n/**\n * Fails when the operator's env contradicts the member's protocol block, then\n * creates the block's path-storage dir (the server's own mkdir is not\n * recursive).\n */\nexport function prepareMemberStorage(\n reactor: ReactorConfig | undefined,\n env: Env,\n hostFileLabel: string,\n): void {\n assertProtocolConsistency(reactor?.protocol, { ...env }, hostFileLabel);\n const storage = primaryBackend(reactor)?.storage;\n if (storage?.kind !== \"path\" || !storage.dataDir) return;\n // `ph run` resolves `$env` and the `${userData}` sugar before the verb;\n // an unresolved one here would mkdir a literal \"${userData}\" folder.\n if (/\\$\\{(userData|projectRoot)\\}/.test(storage.dataDir)) {\n throw new Error(\n `${hostFileLabel}: storage.dataDir \"${storage.dataDir}\" was not resolved; resolve the host (resolveHostOutputs) before starting it.`,\n );\n }\n fs.mkdirSync(storage.dataDir, { recursive: true });\n}\n\n/** The verb owns dev mode; `runner.dev` is the host-file spelling of it. */\nexport function switchboardDevMode(\n devVerb: boolean,\n runner: { dev?: boolean },\n): boolean {\n return devVerb || runner.dev === true;\n}\n"],"names":[],"mappings":";;;;;;;;;AAgCA,SAAS,WAAW,KAAqC;CACvD,IAAI;EACF,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