everything-dev 1.6.0 → 1.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/api-contract.cjs +55 -8
- package/dist/api-contract.cjs.map +1 -1
- package/dist/api-contract.mjs +55 -8
- package/dist/api-contract.mjs.map +1 -1
- package/dist/app.cjs +26 -2
- package/dist/app.cjs.map +1 -1
- package/dist/app.mjs +27 -3
- package/dist/app.mjs.map +1 -1
- package/dist/cli/init.cjs +4 -4
- package/dist/cli/init.cjs.map +1 -1
- package/dist/cli/init.mjs +4 -4
- package/dist/cli/init.mjs.map +1 -1
- package/dist/cli/sync.cjs +2 -2
- package/dist/cli/sync.cjs.map +1 -1
- package/dist/cli/sync.mjs +2 -2
- package/dist/cli/sync.mjs.map +1 -1
- package/dist/cli.cjs +0 -1
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.mjs +0 -1
- package/dist/cli.mjs.map +1 -1
- package/dist/components/streaming-view.cjs +0 -18
- package/dist/components/streaming-view.cjs.map +1 -1
- package/dist/components/streaming-view.mjs +0 -18
- package/dist/components/streaming-view.mjs.map +1 -1
- package/dist/config.cjs +21 -5
- package/dist/config.cjs.map +1 -1
- package/dist/config.d.cts +2 -1
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.mts +2 -1
- package/dist/config.d.mts.map +1 -1
- package/dist/config.mjs +21 -6
- package/dist/config.mjs.map +1 -1
- package/dist/contract.cjs +8 -1
- package/dist/contract.cjs.map +1 -1
- package/dist/contract.d.cts +44 -8
- package/dist/contract.d.cts.map +1 -1
- package/dist/contract.d.mts +44 -8
- package/dist/contract.d.mts.map +1 -1
- package/dist/contract.meta.cjs +1 -1
- package/dist/contract.meta.cjs.map +1 -1
- package/dist/contract.meta.d.cts +1 -1
- package/dist/contract.meta.d.mts +1 -1
- package/dist/contract.meta.mjs +1 -1
- package/dist/contract.meta.mjs.map +1 -1
- package/dist/contract.mjs +8 -1
- package/dist/contract.mjs.map +1 -1
- package/dist/dev-session.cjs +51 -66
- package/dist/dev-session.cjs.map +1 -1
- package/dist/dev-session.mjs +52 -67
- package/dist/dev-session.mjs.map +1 -1
- package/dist/fastkv.cjs +56 -0
- package/dist/fastkv.cjs.map +1 -1
- package/dist/fastkv.d.cts +45 -1
- package/dist/fastkv.d.cts.map +1 -1
- package/dist/fastkv.d.mts +45 -1
- package/dist/fastkv.d.mts.map +1 -1
- package/dist/fastkv.mjs +54 -1
- package/dist/fastkv.mjs.map +1 -1
- package/dist/host.cjs +1 -1
- package/dist/host.cjs.map +1 -1
- package/dist/host.mjs +1 -1
- package/dist/host.mjs.map +1 -1
- package/dist/index.cjs +4 -0
- package/dist/index.d.cts +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +3 -3
- package/dist/near-cli.cjs +1 -1
- package/dist/near-cli.mjs +1 -1
- package/dist/orchestrator.cjs +55 -20
- package/dist/orchestrator.cjs.map +1 -1
- package/dist/orchestrator.d.cts +5 -4
- package/dist/orchestrator.d.cts.map +1 -1
- package/dist/orchestrator.d.mts +5 -4
- package/dist/orchestrator.d.mts.map +1 -1
- package/dist/orchestrator.mjs +55 -20
- package/dist/orchestrator.mjs.map +1 -1
- package/dist/plugin.cjs +135 -9
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +49 -8
- package/dist/plugin.d.cts.map +1 -1
- package/dist/plugin.d.mts +49 -8
- package/dist/plugin.d.mts.map +1 -1
- package/dist/plugin.mjs +137 -11
- package/dist/plugin.mjs.map +1 -1
- package/dist/types.cjs +15 -5
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +60 -9
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +60 -9
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +15 -5
- package/dist/types.mjs.map +1 -1
- package/package.json +2 -2
- package/src/api-contract.ts +88 -9
- package/src/app.ts +55 -7
- package/src/cli/init.ts +6 -6
- package/src/cli/sync.ts +5 -3
- package/src/cli.ts +0 -1
- package/src/components/streaming-view.ts +0 -20
- package/src/config.ts +39 -23
- package/src/contract.meta.ts +4 -1
- package/src/contract.ts +7 -0
- package/src/dev-session.ts +85 -83
- package/src/fastkv.ts +95 -0
- package/src/host.ts +1 -1
- package/src/orchestrator.ts +61 -31
- package/src/plugin.ts +202 -5
- package/src/types.ts +38 -4
package/dist/api-contract.cjs
CHANGED
|
@@ -41,6 +41,13 @@ function localApiContractSource(configDir) {
|
|
|
41
41
|
sourceFilePath: (0, node_path.join)(configDir, "api", "src", "contract.ts")
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
|
+
function localAuthContractSource(configDir) {
|
|
45
|
+
return {
|
|
46
|
+
key: "auth",
|
|
47
|
+
importName: "authContract",
|
|
48
|
+
sourceFilePath: (0, node_path.join)(configDir, "plugins", "auth", "src", "contract.ts")
|
|
49
|
+
};
|
|
50
|
+
}
|
|
44
51
|
async function remoteContractSource(opts) {
|
|
45
52
|
const manifest = await fetchApiPluginManifest(opts.baseUrl);
|
|
46
53
|
if (!manifest.contract) throw new Error(`Plugin manifest for ${manifest.plugin.name} does not advertise contract types`);
|
|
@@ -69,6 +76,15 @@ async function resolveContractSource(opts) {
|
|
|
69
76
|
};
|
|
70
77
|
if (!opts.baseUrl) return localApiContractSource(opts.configDir);
|
|
71
78
|
}
|
|
79
|
+
if (opts.key === "auth" && opts.localSourceFactory && (!opts.source || !("localPath" in opts.source) || opts.source.localPath)) {
|
|
80
|
+
const localPath = opts.source && "localPath" in opts.source ? opts.source.localPath : void 0;
|
|
81
|
+
if (localPath) return {
|
|
82
|
+
key: opts.key,
|
|
83
|
+
importName: "authContract",
|
|
84
|
+
sourceFilePath: (0, node_path.join)(localPath, "src", "contract.ts")
|
|
85
|
+
};
|
|
86
|
+
if (!opts.baseUrl) return opts.localSourceFactory(opts.configDir);
|
|
87
|
+
}
|
|
72
88
|
if (opts.source && "localPath" in opts.source && opts.source.localPath) return {
|
|
73
89
|
key: opts.key,
|
|
74
90
|
importName: `${sanitizeIdentifier(opts.key)}Contract`,
|
|
@@ -82,7 +98,7 @@ async function resolveContractSource(opts) {
|
|
|
82
98
|
generatedSubdir: opts.generatedSubdir
|
|
83
99
|
});
|
|
84
100
|
}
|
|
85
|
-
function
|
|
101
|
+
function writeGeneratedFiles(opts) {
|
|
86
102
|
const baseSource = opts.sources.find((source) => source.key === "api");
|
|
87
103
|
const pluginSources = opts.pluginKeys.map((key) => opts.sources.find((entry) => entry.key === key)).filter((source) => Boolean(source));
|
|
88
104
|
if (!baseSource) throw new Error("API contract source is required to generate the aggregate contract");
|
|
@@ -93,13 +109,16 @@ function writeAggregateContractFile(opts) {
|
|
|
93
109
|
uiLines.push(`import type { ContractType as ${source.importName} } from "${importPath}";`);
|
|
94
110
|
}
|
|
95
111
|
uiLines.push("");
|
|
96
|
-
|
|
112
|
+
const compositeParts = [];
|
|
113
|
+
if (opts.authSource) compositeParts.push(`auth: ${opts.authSource.importName}`);
|
|
114
|
+
for (const source of pluginSources) {
|
|
115
|
+
const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key) ? source.key : JSON.stringify(source.key);
|
|
116
|
+
compositeParts.push(`${key}: ${source.importName}`);
|
|
117
|
+
}
|
|
118
|
+
if (compositeParts.length === 0) uiLines.push(`export type ApiContract = ${baseSource.importName};`);
|
|
97
119
|
else {
|
|
98
120
|
uiLines.push(`export type ApiContract = ${baseSource.importName} & {`);
|
|
99
|
-
for (const
|
|
100
|
-
const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key) ? source.key : JSON.stringify(source.key);
|
|
101
|
-
uiLines.push(` ${key}: ${source.importName};`);
|
|
102
|
-
}
|
|
121
|
+
for (const part of compositeParts) uiLines.push(` ${part};`);
|
|
103
122
|
uiLines.push("};");
|
|
104
123
|
}
|
|
105
124
|
(0, node_fs.mkdirSync)((0, node_path.dirname)(uiContractPath), { recursive: true });
|
|
@@ -124,6 +143,18 @@ function writeAggregateContractFile(opts) {
|
|
|
124
143
|
}
|
|
125
144
|
(0, node_fs.mkdirSync)((0, node_path.dirname)(pluginsClientPath), { recursive: true });
|
|
126
145
|
writeFileIfChanged(pluginsClientPath, `${pluginsClientLines.join("\n")}\n`);
|
|
146
|
+
if (opts.authSource) {
|
|
147
|
+
const authClientPath = (0, node_path.join)(opts.configDir, "api", "src", "auth-client.gen.ts");
|
|
148
|
+
const authClientLines = [];
|
|
149
|
+
const importPath = toImportPath(authClientPath, opts.authSource.sourceFilePath);
|
|
150
|
+
authClientLines.push(`import type { ContractType as ${opts.authSource.importName} } from "${importPath}";`);
|
|
151
|
+
authClientLines.push("import type { ContractRouterClient, AnyContractRouter } from \"@orpc/contract\";");
|
|
152
|
+
authClientLines.push("type ClientFactory<C extends AnyContractRouter> = (context?: Record<string, unknown>) => ContractRouterClient<C>;");
|
|
153
|
+
authClientLines.push("");
|
|
154
|
+
authClientLines.push(`export type AuthClient = ClientFactory<${opts.authSource.importName}>;`);
|
|
155
|
+
(0, node_fs.mkdirSync)((0, node_path.dirname)(authClientPath), { recursive: true });
|
|
156
|
+
writeFileIfChanged(authClientPath, `${authClientLines.join("\n")}\n`);
|
|
157
|
+
}
|
|
127
158
|
return uiContractPath;
|
|
128
159
|
}
|
|
129
160
|
async function syncApiContractBridge(opts) {
|
|
@@ -132,6 +163,7 @@ async function syncApiContractBridge(opts) {
|
|
|
132
163
|
const sources = [];
|
|
133
164
|
let manifest = null;
|
|
134
165
|
let generatedPath = null;
|
|
166
|
+
let authSource = null;
|
|
135
167
|
const baseSource = await resolveContractSource({
|
|
136
168
|
configDir: opts.configDir,
|
|
137
169
|
runtimeDir,
|
|
@@ -141,6 +173,19 @@ async function syncApiContractBridge(opts) {
|
|
|
141
173
|
generatedSubdir: "api"
|
|
142
174
|
});
|
|
143
175
|
sources.push(baseSource);
|
|
176
|
+
if (opts.runtimeConfig.auth) {
|
|
177
|
+
authSource = await resolveContractSource({
|
|
178
|
+
configDir: opts.configDir,
|
|
179
|
+
runtimeDir,
|
|
180
|
+
key: "auth",
|
|
181
|
+
source: opts.runtimeConfig.auth,
|
|
182
|
+
baseUrl: opts.runtimeConfig.auth.url,
|
|
183
|
+
generatedSubdir: "auth",
|
|
184
|
+
localSourceFactory: localAuthContractSource
|
|
185
|
+
});
|
|
186
|
+
sources.push(authSource);
|
|
187
|
+
if (authSource.generatedPath) generatedPath = authSource.generatedPath;
|
|
188
|
+
}
|
|
144
189
|
for (const [key, plugin] of pluginEntries) {
|
|
145
190
|
const source = await resolveContractSource({
|
|
146
191
|
configDir: opts.configDir,
|
|
@@ -153,10 +198,12 @@ async function syncApiContractBridge(opts) {
|
|
|
153
198
|
sources.push(source);
|
|
154
199
|
if (source.generatedPath) generatedPath = source.generatedPath;
|
|
155
200
|
}
|
|
156
|
-
|
|
201
|
+
const allPluginKeys = pluginEntries.map(([key]) => key);
|
|
202
|
+
writeGeneratedFiles({
|
|
157
203
|
configDir: opts.configDir,
|
|
158
204
|
sources,
|
|
159
|
-
pluginKeys:
|
|
205
|
+
pluginKeys: allPluginKeys,
|
|
206
|
+
authSource
|
|
160
207
|
});
|
|
161
208
|
if (opts.runtimeConfig.api.source !== "local") manifest = await fetchApiPluginManifest(opts.apiBaseUrl);
|
|
162
209
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-contract.cjs","names":[],"sources":["../src/api-contract.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport { mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join, relative } from \"node:path\";\nimport type { RuntimeConfig, RuntimePluginConfig } from \"./types\";\n\nexport interface ApiPluginManifest {\n schemaVersion: 1;\n kind: \"every-plugin/manifest\";\n plugin: {\n name: string;\n version: string;\n };\n runtime: {\n remoteEntry: string;\n };\n contract?: {\n kind: \"orpc\";\n types: {\n path: string;\n exportName: string;\n typeName: string;\n sha256?: string;\n };\n };\n}\n\ninterface ContractSource {\n key: string;\n importName: string;\n sourceFilePath: string;\n generatedPath?: string;\n}\n\nfunction sha256(input: string): string {\n return createHash(\"sha256\").update(input).digest(\"hex\");\n}\n\nfunction trimTrailingSlash(input: string): string {\n return input.replace(/\\/$/, \"\");\n}\n\nfunction sanitizeIdentifier(input: string): string {\n return input.replace(/[^A-Za-z0-9_]/g, \"_\").replace(/^[^A-Za-z_]+/, \"_\");\n}\n\nfunction toImportPath(fromFile: string, targetFile: string): string {\n const rel = relative(dirname(fromFile), targetFile).replace(/\\\\/g, \"/\");\n return rel.startsWith(\".\") ? rel : `./${rel}`;\n}\n\nfunction writeFileIfChanged(filePath: string, content: string) {\n try {\n if (readFileSync(filePath, \"utf8\") === content) return false;\n } catch {\n // file does not exist yet\n }\n\n writeFileSync(filePath, content);\n return true;\n}\n\nfunction getApiPluginManifestUrl(apiBaseUrl: string): string {\n return `${trimTrailingSlash(apiBaseUrl)}/plugin.manifest.json`;\n}\n\nasync function fetchApiPluginManifest(apiBaseUrl: string): Promise<ApiPluginManifest> {\n const response = await fetch(getApiPluginManifestUrl(apiBaseUrl));\n if (!response.ok) {\n throw new Error(\n `Failed to fetch API plugin manifest: ${response.status} ${response.statusText}`,\n );\n }\n\n const manifest = (await response.json()) as ApiPluginManifest;\n if (manifest.schemaVersion !== 1 || manifest.kind !== \"every-plugin/manifest\") {\n throw new Error(\"Unsupported API plugin manifest format\");\n }\n\n return manifest;\n}\n\nfunction localApiContractSource(configDir: string): ContractSource {\n const sourcePath = join(configDir, \"api\", \"src\", \"contract.ts\");\n return {\n key: \"api\",\n importName: \"BaseApiContract\",\n sourceFilePath: sourcePath,\n };\n}\n\nasync function remoteContractSource(opts: {\n configDir: string;\n runtimeDir: string;\n name: string;\n baseUrl: string;\n generatedSubdir: string;\n}): Promise<ContractSource> {\n const manifest = await fetchApiPluginManifest(opts.baseUrl);\n if (!manifest.contract) {\n throw new Error(\n `Plugin manifest for ${manifest.plugin.name} does not advertise contract types`,\n );\n }\n\n const contractUrl = `${trimTrailingSlash(opts.baseUrl)}/${manifest.contract.types.path.replace(/^\\.\\//, \"\")}`;\n const contractResponse = await fetch(contractUrl);\n if (!contractResponse.ok) {\n throw new Error(\n `Failed to fetch contract types: ${contractResponse.status} ${contractResponse.statusText}`,\n );\n }\n\n const contractTypes = await contractResponse.text();\n if (manifest.contract.types.sha256 && manifest.contract.types.sha256 !== sha256(contractTypes)) {\n throw new Error(\"Fetched contract types failed checksum verification\");\n }\n\n const generatedPath = join(opts.runtimeDir, opts.generatedSubdir, \"contract.d.ts\");\n mkdirSync(dirname(generatedPath), { recursive: true });\n writeFileIfChanged(generatedPath, contractTypes);\n\n return {\n key: opts.name,\n importName: `${sanitizeIdentifier(opts.name)}Contract`,\n sourceFilePath: generatedPath,\n generatedPath,\n };\n}\n\nasync function resolveContractSource(opts: {\n configDir: string;\n runtimeDir: string;\n key: string;\n source: RuntimePluginConfig | { url: string; localPath?: string; name: string } | null;\n baseUrl: string;\n generatedSubdir: string;\n}): Promise<ContractSource> {\n if (\n opts.key === \"api\" &&\n (!opts.source || !(\"localPath\" in opts.source) || opts.source.localPath)\n ) {\n const localPath = opts.source && \"localPath\" in opts.source ? opts.source.localPath : undefined;\n if (localPath) {\n return {\n key: opts.key,\n importName: \"BaseApiContract\",\n sourceFilePath: join(localPath, \"src\", \"contract.ts\"),\n };\n }\n\n if (!opts.baseUrl) {\n return localApiContractSource(opts.configDir);\n }\n }\n\n if (opts.source && \"localPath\" in opts.source && opts.source.localPath) {\n return {\n key: opts.key,\n importName: `${sanitizeIdentifier(opts.key)}Contract`,\n sourceFilePath: join(opts.source.localPath, \"src\", \"contract.ts\"),\n };\n }\n\n return remoteContractSource({\n configDir: opts.configDir,\n runtimeDir: opts.runtimeDir,\n name: opts.key,\n baseUrl: opts.baseUrl,\n generatedSubdir: opts.generatedSubdir,\n });\n}\n\nfunction writeAggregateContractFile(opts: {\n configDir: string;\n sources: ContractSource[];\n pluginKeys: string[];\n}) {\n const baseSource = opts.sources.find((source) => source.key === \"api\");\n const pluginSources = opts.pluginKeys\n .map((key) => opts.sources.find((entry) => entry.key === key))\n .filter((source): source is ContractSource => Boolean(source));\n\n if (!baseSource) {\n throw new Error(\"API contract source is required to generate the aggregate contract\");\n }\n\n // --- Generate ui/src/api-contract.gen.ts ---\n const uiContractPath = join(opts.configDir, \"ui\", \"src\", \"api-contract.gen.ts\");\n const uiLines: string[] = [];\n\n for (const source of opts.sources) {\n const importPath = toImportPath(uiContractPath, source.sourceFilePath);\n uiLines.push(`import type { ContractType as ${source.importName} } from \"${importPath}\";`);\n }\n\n uiLines.push(\"\");\n if (pluginSources.length === 0) {\n uiLines.push(`export type ApiContract = ${baseSource.importName};`);\n } else {\n uiLines.push(`export type ApiContract = ${baseSource.importName} & {`);\n for (const source of pluginSources) {\n const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key)\n ? source.key\n : JSON.stringify(source.key);\n uiLines.push(` ${key}: ${source.importName};`);\n }\n uiLines.push(\"};\");\n }\n mkdirSync(dirname(uiContractPath), { recursive: true });\n writeFileIfChanged(uiContractPath, `${uiLines.join(\"\\n\")}\\n`);\n\n // --- Generate api/src/plugins-client.gen.ts ---\n const pluginsClientPath = join(opts.configDir, \"api\", \"src\", \"plugins-client.gen.ts\");\n const pluginsClientLines: string[] = [];\n\n for (const source of pluginSources) {\n const importPath = toImportPath(pluginsClientPath, source.sourceFilePath);\n pluginsClientLines.push(\n `import type { ContractType as ${source.importName} } from \"${importPath}\";`,\n );\n }\n\n pluginsClientLines.push(\n 'import type { ContractRouterClient, AnyContractRouter } from \"@orpc/contract\";',\n );\n pluginsClientLines.push(\n \"type ClientFactory<C extends AnyContractRouter> = (context?: Record<string, unknown>) => ContractRouterClient<C>;\",\n );\n pluginsClientLines.push(\"\");\n\n if (pluginSources.length === 0) {\n pluginsClientLines.push(\"export type PluginsClient = Record<string, never>;\");\n } else {\n pluginsClientLines.push(\"export type PluginsClient = {\");\n for (const source of pluginSources) {\n const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key)\n ? source.key\n : JSON.stringify(source.key);\n pluginsClientLines.push(` ${key}: ClientFactory<${source.importName}>;`);\n }\n pluginsClientLines.push(\"};\");\n }\n\n mkdirSync(dirname(pluginsClientPath), { recursive: true });\n writeFileIfChanged(pluginsClientPath, `${pluginsClientLines.join(\"\\n\")}\\n`);\n\n return uiContractPath;\n}\n\nexport async function syncApiContractBridge(opts: {\n configDir: string;\n runtimeConfig: RuntimeConfig;\n apiBaseUrl: string;\n}): Promise<{\n bridgePath: string;\n generatedPath: string | null;\n manifest: ApiPluginManifest | null;\n source: \"local\" | \"remote\";\n}> {\n const runtimeDir = join(opts.configDir, \".bos\", \"generated\");\n const pluginEntries = Object.entries(opts.runtimeConfig.plugins ?? {}).sort(([a], [b]) =>\n a.localeCompare(b),\n );\n const sources: ContractSource[] = [];\n let manifest: ApiPluginManifest | null = null;\n let generatedPath: string | null = null;\n\n const baseSource = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key: \"api\",\n source: opts.runtimeConfig.api,\n baseUrl: opts.apiBaseUrl,\n generatedSubdir: \"api\",\n });\n sources.push(baseSource);\n\n for (const [key, plugin] of pluginEntries) {\n const source = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key,\n source: plugin,\n baseUrl: plugin.url,\n generatedSubdir: `plugins/${key}`,\n });\n sources.push(source);\n if (source.generatedPath) {\n generatedPath = source.generatedPath;\n }\n }\n\n writeAggregateContractFile({\n configDir: opts.configDir,\n sources,\n pluginKeys: pluginEntries.map(([key]) => key),\n });\n\n if (opts.runtimeConfig.api.source !== \"local\") {\n manifest = await fetchApiPluginManifest(opts.apiBaseUrl);\n }\n\n return {\n bridgePath: join(opts.configDir, \"ui\", \"src\", \"api-contract.gen.ts\"),\n generatedPath,\n manifest,\n source: opts.runtimeConfig.api.source,\n };\n}\n"],"mappings":";;;;;;AAiCA,SAAS,OAAO,OAAuB;AACrC,oCAAkB,SAAS,CAAC,OAAO,MAAM,CAAC,OAAO,MAAM;;AAGzD,SAAS,kBAAkB,OAAuB;AAChD,QAAO,MAAM,QAAQ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sources":["../src/api-contract.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport { mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join, relative } from \"node:path\";\nimport type { RuntimeConfig, RuntimePluginConfig } from \"./types\";\n\nexport interface ApiPluginManifest {\n schemaVersion: 1;\n kind: \"every-plugin/manifest\";\n plugin: {\n name: string;\n version: string;\n };\n runtime: {\n remoteEntry: string;\n };\n contract?: {\n kind: \"orpc\";\n types: {\n path: string;\n exportName: string;\n typeName: string;\n sha256?: string;\n };\n };\n}\n\ninterface ContractSource {\n key: string;\n importName: string;\n sourceFilePath: string;\n generatedPath?: string;\n}\n\nfunction sha256(input: string): string {\n return createHash(\"sha256\").update(input).digest(\"hex\");\n}\n\nfunction trimTrailingSlash(input: string): string {\n return input.replace(/\\/$/, \"\");\n}\n\nfunction sanitizeIdentifier(input: string): string {\n return input.replace(/[^A-Za-z0-9_]/g, \"_\").replace(/^[^A-Za-z_]+/, \"_\");\n}\n\nfunction toImportPath(fromFile: string, targetFile: string): string {\n const rel = relative(dirname(fromFile), targetFile).replace(/\\\\/g, \"/\");\n return rel.startsWith(\".\") ? rel : `./${rel}`;\n}\n\nfunction writeFileIfChanged(filePath: string, content: string) {\n try {\n if (readFileSync(filePath, \"utf8\") === content) return false;\n } catch {\n // file does not exist yet\n }\n\n writeFileSync(filePath, content);\n return true;\n}\n\nfunction getApiPluginManifestUrl(apiBaseUrl: string): string {\n return `${trimTrailingSlash(apiBaseUrl)}/plugin.manifest.json`;\n}\n\nasync function fetchApiPluginManifest(apiBaseUrl: string): Promise<ApiPluginManifest> {\n const response = await fetch(getApiPluginManifestUrl(apiBaseUrl));\n if (!response.ok) {\n throw new Error(\n `Failed to fetch API plugin manifest: ${response.status} ${response.statusText}`,\n );\n }\n\n const manifest = (await response.json()) as ApiPluginManifest;\n if (manifest.schemaVersion !== 1 || manifest.kind !== \"every-plugin/manifest\") {\n throw new Error(\"Unsupported API plugin manifest format\");\n }\n\n return manifest;\n}\n\nfunction localApiContractSource(configDir: string): ContractSource {\n const sourcePath = join(configDir, \"api\", \"src\", \"contract.ts\");\n return {\n key: \"api\",\n importName: \"BaseApiContract\",\n sourceFilePath: sourcePath,\n };\n}\n\nfunction localAuthContractSource(configDir: string): ContractSource {\n const sourcePath = join(configDir, \"plugins\", \"auth\", \"src\", \"contract.ts\");\n return {\n key: \"auth\",\n importName: \"authContract\",\n sourceFilePath: sourcePath,\n };\n}\n\nasync function remoteContractSource(opts: {\n configDir: string;\n runtimeDir: string;\n name: string;\n baseUrl: string;\n generatedSubdir: string;\n}): Promise<ContractSource> {\n const manifest = await fetchApiPluginManifest(opts.baseUrl);\n if (!manifest.contract) {\n throw new Error(\n `Plugin manifest for ${manifest.plugin.name} does not advertise contract types`,\n );\n }\n\n const contractUrl = `${trimTrailingSlash(opts.baseUrl)}/${manifest.contract.types.path.replace(/^\\.\\//, \"\")}`;\n const contractResponse = await fetch(contractUrl);\n if (!contractResponse.ok) {\n throw new Error(\n `Failed to fetch contract types: ${contractResponse.status} ${contractResponse.statusText}`,\n );\n }\n\n const contractTypes = await contractResponse.text();\n if (manifest.contract.types.sha256 && manifest.contract.types.sha256 !== sha256(contractTypes)) {\n throw new Error(\"Fetched contract types failed checksum verification\");\n }\n\n const generatedPath = join(opts.runtimeDir, opts.generatedSubdir, \"contract.d.ts\");\n mkdirSync(dirname(generatedPath), { recursive: true });\n writeFileIfChanged(generatedPath, contractTypes);\n\n return {\n key: opts.name,\n importName: `${sanitizeIdentifier(opts.name)}Contract`,\n sourceFilePath: generatedPath,\n generatedPath,\n };\n}\n\nasync function resolveContractSource(opts: {\n configDir: string;\n runtimeDir: string;\n key: string;\n source: RuntimePluginConfig | { url: string; localPath?: string; name: string } | null;\n baseUrl: string;\n generatedSubdir: string;\n localSourceFactory?: (configDir: string) => ContractSource;\n}): Promise<ContractSource> {\n if (\n opts.key === \"api\" &&\n (!opts.source || !(\"localPath\" in opts.source) || opts.source.localPath)\n ) {\n const localPath = opts.source && \"localPath\" in opts.source ? opts.source.localPath : undefined;\n if (localPath) {\n return {\n key: opts.key,\n importName: \"BaseApiContract\",\n sourceFilePath: join(localPath, \"src\", \"contract.ts\"),\n };\n }\n\n if (!opts.baseUrl) {\n return localApiContractSource(opts.configDir);\n }\n }\n\n if (\n opts.key === \"auth\" &&\n opts.localSourceFactory &&\n (!opts.source || !(\"localPath\" in opts.source) || opts.source.localPath)\n ) {\n const localPath = opts.source && \"localPath\" in opts.source ? opts.source.localPath : undefined;\n if (localPath) {\n return {\n key: opts.key,\n importName: \"authContract\",\n sourceFilePath: join(localPath, \"src\", \"contract.ts\"),\n };\n }\n\n if (!opts.baseUrl) {\n return opts.localSourceFactory(opts.configDir);\n }\n }\n\n if (opts.source && \"localPath\" in opts.source && opts.source.localPath) {\n return {\n key: opts.key,\n importName: `${sanitizeIdentifier(opts.key)}Contract`,\n sourceFilePath: join(opts.source.localPath, \"src\", \"contract.ts\"),\n };\n }\n\n return remoteContractSource({\n configDir: opts.configDir,\n runtimeDir: opts.runtimeDir,\n name: opts.key,\n baseUrl: opts.baseUrl,\n generatedSubdir: opts.generatedSubdir,\n });\n}\n\nfunction writeGeneratedFiles(opts: {\n configDir: string;\n sources: ContractSource[];\n pluginKeys: string[];\n authSource: ContractSource | null;\n}) {\n const baseSource = opts.sources.find((source) => source.key === \"api\");\n const pluginSources = opts.pluginKeys\n .map((key) => opts.sources.find((entry) => entry.key === key))\n .filter((source): source is ContractSource => Boolean(source));\n\n if (!baseSource) {\n throw new Error(\"API contract source is required to generate the aggregate contract\");\n }\n\n // --- Generate ui/src/api-contract.gen.ts ---\n const uiContractPath = join(opts.configDir, \"ui\", \"src\", \"api-contract.gen.ts\");\n const uiLines: string[] = [];\n\n for (const source of opts.sources) {\n const importPath = toImportPath(uiContractPath, source.sourceFilePath);\n uiLines.push(`import type { ContractType as ${source.importName} } from \"${importPath}\";`);\n }\n\n uiLines.push(\"\");\n\n const compositeParts: string[] = [];\n if (opts.authSource) {\n compositeParts.push(`auth: ${opts.authSource.importName}`);\n }\n for (const source of pluginSources) {\n const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key) ? source.key : JSON.stringify(source.key);\n compositeParts.push(`${key}: ${source.importName}`);\n }\n\n if (compositeParts.length === 0) {\n uiLines.push(`export type ApiContract = ${baseSource.importName};`);\n } else {\n uiLines.push(`export type ApiContract = ${baseSource.importName} & {`);\n for (const part of compositeParts) {\n uiLines.push(` ${part};`);\n }\n uiLines.push(\"};\");\n }\n mkdirSync(dirname(uiContractPath), { recursive: true });\n writeFileIfChanged(uiContractPath, `${uiLines.join(\"\\n\")}\\n`);\n\n // --- Generate api/src/plugins-client.gen.ts ---\n const pluginsClientPath = join(opts.configDir, \"api\", \"src\", \"plugins-client.gen.ts\");\n const pluginsClientLines: string[] = [];\n\n for (const source of pluginSources) {\n const importPath = toImportPath(pluginsClientPath, source.sourceFilePath);\n pluginsClientLines.push(\n `import type { ContractType as ${source.importName} } from \"${importPath}\";`,\n );\n }\n\n pluginsClientLines.push(\n 'import type { ContractRouterClient, AnyContractRouter } from \"@orpc/contract\";',\n );\n pluginsClientLines.push(\n \"type ClientFactory<C extends AnyContractRouter> = (context?: Record<string, unknown>) => ContractRouterClient<C>;\",\n );\n pluginsClientLines.push(\"\");\n\n if (pluginSources.length === 0) {\n pluginsClientLines.push(\"export type PluginsClient = Record<string, never>;\");\n } else {\n pluginsClientLines.push(\"export type PluginsClient = {\");\n for (const source of pluginSources) {\n const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key)\n ? source.key\n : JSON.stringify(source.key);\n pluginsClientLines.push(` ${key}: ClientFactory<${source.importName}>;`);\n }\n pluginsClientLines.push(\"};\");\n }\n\n mkdirSync(dirname(pluginsClientPath), { recursive: true });\n writeFileIfChanged(pluginsClientPath, `${pluginsClientLines.join(\"\\n\")}\\n`);\n\n // --- Generate api/src/auth-client.gen.ts ---\n if (opts.authSource) {\n const authClientPath = join(opts.configDir, \"api\", \"src\", \"auth-client.gen.ts\");\n const authClientLines: string[] = [];\n\n const importPath = toImportPath(authClientPath, opts.authSource.sourceFilePath);\n authClientLines.push(\n `import type { ContractType as ${opts.authSource.importName} } from \"${importPath}\";`,\n );\n authClientLines.push(\n 'import type { ContractRouterClient, AnyContractRouter } from \"@orpc/contract\";',\n );\n authClientLines.push(\n \"type ClientFactory<C extends AnyContractRouter> = (context?: Record<string, unknown>) => ContractRouterClient<C>;\",\n );\n authClientLines.push(\"\");\n authClientLines.push(`export type AuthClient = ClientFactory<${opts.authSource.importName}>;`);\n\n mkdirSync(dirname(authClientPath), { recursive: true });\n writeFileIfChanged(authClientPath, `${authClientLines.join(\"\\n\")}\\n`);\n }\n\n return uiContractPath;\n}\n\nexport async function syncApiContractBridge(opts: {\n configDir: string;\n runtimeConfig: RuntimeConfig;\n apiBaseUrl: string;\n}): Promise<{\n bridgePath: string;\n generatedPath: string | null;\n manifest: ApiPluginManifest | null;\n source: \"local\" | \"remote\";\n}> {\n const runtimeDir = join(opts.configDir, \".bos\", \"generated\");\n const pluginEntries = Object.entries(opts.runtimeConfig.plugins ?? {}).sort(([a], [b]) =>\n a.localeCompare(b),\n );\n const sources: ContractSource[] = [];\n let manifest: ApiPluginManifest | null = null;\n let generatedPath: string | null = null;\n let authSource: ContractSource | null = null;\n\n const baseSource = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key: \"api\",\n source: opts.runtimeConfig.api,\n baseUrl: opts.apiBaseUrl,\n generatedSubdir: \"api\",\n });\n sources.push(baseSource);\n\n if (opts.runtimeConfig.auth) {\n authSource = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key: \"auth\",\n source: opts.runtimeConfig.auth,\n baseUrl: opts.runtimeConfig.auth.url,\n generatedSubdir: \"auth\",\n localSourceFactory: localAuthContractSource,\n });\n sources.push(authSource);\n if (authSource.generatedPath) {\n generatedPath = authSource.generatedPath;\n }\n }\n\n for (const [key, plugin] of pluginEntries) {\n const source = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key,\n source: plugin,\n baseUrl: plugin.url,\n generatedSubdir: `plugins/${key}`,\n });\n sources.push(source);\n if (source.generatedPath) {\n generatedPath = source.generatedPath;\n }\n }\n\n const allPluginKeys = pluginEntries.map(([key]) => key);\n\n writeGeneratedFiles({\n configDir: opts.configDir,\n sources,\n pluginKeys: allPluginKeys,\n authSource,\n });\n\n if (opts.runtimeConfig.api.source !== \"local\") {\n manifest = await fetchApiPluginManifest(opts.apiBaseUrl);\n }\n\n return {\n bridgePath: join(opts.configDir, \"ui\", \"src\", \"api-contract.gen.ts\"),\n generatedPath,\n manifest,\n source: opts.runtimeConfig.api.source,\n };\n}\n"],"mappings":";;;;;;AAiCA,SAAS,OAAO,OAAuB;AACrC,oCAAkB,SAAS,CAAC,OAAO,MAAM,CAAC,OAAO,MAAM;;AAGzD,SAAS,kBAAkB,OAAuB;AAChD,QAAO,MAAM,QAAQ,OAAO,GAAG;;AAGjC,SAAS,mBAAmB,OAAuB;AACjD,QAAO,MAAM,QAAQ,kBAAkB,IAAI,CAAC,QAAQ,gBAAgB,IAAI;;AAG1E,SAAS,aAAa,UAAkB,YAA4B;CAClE,MAAM,qDAAuB,SAAS,EAAE,WAAW,CAAC,QAAQ,OAAO,IAAI;AACvE,QAAO,IAAI,WAAW,IAAI,GAAG,MAAM,KAAK;;AAG1C,SAAS,mBAAmB,UAAkB,SAAiB;AAC7D,KAAI;AACF,gCAAiB,UAAU,OAAO,KAAK,QAAS,QAAO;SACjD;AAIR,4BAAc,UAAU,QAAQ;AAChC,QAAO;;AAGT,SAAS,wBAAwB,YAA4B;AAC3D,QAAO,GAAG,kBAAkB,WAAW,CAAC;;AAG1C,eAAe,uBAAuB,YAAgD;CACpF,MAAM,WAAW,MAAM,MAAM,wBAAwB,WAAW,CAAC;AACjE,KAAI,CAAC,SAAS,GACZ,OAAM,IAAI,MACR,wCAAwC,SAAS,OAAO,GAAG,SAAS,aACrE;CAGH,MAAM,WAAY,MAAM,SAAS,MA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AK;GACjC,iBAAiB;GACjB,oBAAoB;GACrB,CAAC;AACF,UAAQ,KAAK,WAAW;AACxB,MAAI,WAAW,cACb,iBAAgB,WAAW;;AAI/B,MAAK,MAAM,CAAC,KAAK,WAAW,eAAe;EACzC,MAAM,SAAS,MAAM,sBAAsB;GACzC,WAAW,KAAK;GAChB;GACA;GACA,QAAQ;GACR,SAAS,OAAO;GAChB,iBAAiB,WAAW;GAC7B,CAAC;AACF,UAAQ,KAAK,OAAO;AACpB,MAAI,OAAO,cACT,iBAAgB,OAAO;;CAI3B,MAAM,gBAAgB,cAAc,KAAK,CAAC,SAAS,IAAI;AAEvD,qBAAoB;EAClB,WAAW,KAAK;EAChB;EACA,YAAY;EACZ;EACD,CAAC;AAEF,KAAI,KAAK,cAAc,IAAI,WAAW,QACpC,YAAW,MAAM,uBAAuB,KAAK,WAAW;AAG1D,QAAO;EACL,gCAAiB,KAAK,WAAW,MAAM,OAAO,sBAAsB;EACpE;EACA;EACA,QAAQ,KAAK,cAAc,IAAI;EAChC"}
|
package/dist/api-contract.mjs
CHANGED
|
@@ -40,6 +40,13 @@ function localApiContractSource(configDir) {
|
|
|
40
40
|
sourceFilePath: join(configDir, "api", "src", "contract.ts")
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
+
function localAuthContractSource(configDir) {
|
|
44
|
+
return {
|
|
45
|
+
key: "auth",
|
|
46
|
+
importName: "authContract",
|
|
47
|
+
sourceFilePath: join(configDir, "plugins", "auth", "src", "contract.ts")
|
|
48
|
+
};
|
|
49
|
+
}
|
|
43
50
|
async function remoteContractSource(opts) {
|
|
44
51
|
const manifest = await fetchApiPluginManifest(opts.baseUrl);
|
|
45
52
|
if (!manifest.contract) throw new Error(`Plugin manifest for ${manifest.plugin.name} does not advertise contract types`);
|
|
@@ -68,6 +75,15 @@ async function resolveContractSource(opts) {
|
|
|
68
75
|
};
|
|
69
76
|
if (!opts.baseUrl) return localApiContractSource(opts.configDir);
|
|
70
77
|
}
|
|
78
|
+
if (opts.key === "auth" && opts.localSourceFactory && (!opts.source || !("localPath" in opts.source) || opts.source.localPath)) {
|
|
79
|
+
const localPath = opts.source && "localPath" in opts.source ? opts.source.localPath : void 0;
|
|
80
|
+
if (localPath) return {
|
|
81
|
+
key: opts.key,
|
|
82
|
+
importName: "authContract",
|
|
83
|
+
sourceFilePath: join(localPath, "src", "contract.ts")
|
|
84
|
+
};
|
|
85
|
+
if (!opts.baseUrl) return opts.localSourceFactory(opts.configDir);
|
|
86
|
+
}
|
|
71
87
|
if (opts.source && "localPath" in opts.source && opts.source.localPath) return {
|
|
72
88
|
key: opts.key,
|
|
73
89
|
importName: `${sanitizeIdentifier(opts.key)}Contract`,
|
|
@@ -81,7 +97,7 @@ async function resolveContractSource(opts) {
|
|
|
81
97
|
generatedSubdir: opts.generatedSubdir
|
|
82
98
|
});
|
|
83
99
|
}
|
|
84
|
-
function
|
|
100
|
+
function writeGeneratedFiles(opts) {
|
|
85
101
|
const baseSource = opts.sources.find((source) => source.key === "api");
|
|
86
102
|
const pluginSources = opts.pluginKeys.map((key) => opts.sources.find((entry) => entry.key === key)).filter((source) => Boolean(source));
|
|
87
103
|
if (!baseSource) throw new Error("API contract source is required to generate the aggregate contract");
|
|
@@ -92,13 +108,16 @@ function writeAggregateContractFile(opts) {
|
|
|
92
108
|
uiLines.push(`import type { ContractType as ${source.importName} } from "${importPath}";`);
|
|
93
109
|
}
|
|
94
110
|
uiLines.push("");
|
|
95
|
-
|
|
111
|
+
const compositeParts = [];
|
|
112
|
+
if (opts.authSource) compositeParts.push(`auth: ${opts.authSource.importName}`);
|
|
113
|
+
for (const source of pluginSources) {
|
|
114
|
+
const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key) ? source.key : JSON.stringify(source.key);
|
|
115
|
+
compositeParts.push(`${key}: ${source.importName}`);
|
|
116
|
+
}
|
|
117
|
+
if (compositeParts.length === 0) uiLines.push(`export type ApiContract = ${baseSource.importName};`);
|
|
96
118
|
else {
|
|
97
119
|
uiLines.push(`export type ApiContract = ${baseSource.importName} & {`);
|
|
98
|
-
for (const
|
|
99
|
-
const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key) ? source.key : JSON.stringify(source.key);
|
|
100
|
-
uiLines.push(` ${key}: ${source.importName};`);
|
|
101
|
-
}
|
|
120
|
+
for (const part of compositeParts) uiLines.push(` ${part};`);
|
|
102
121
|
uiLines.push("};");
|
|
103
122
|
}
|
|
104
123
|
mkdirSync(dirname(uiContractPath), { recursive: true });
|
|
@@ -123,6 +142,18 @@ function writeAggregateContractFile(opts) {
|
|
|
123
142
|
}
|
|
124
143
|
mkdirSync(dirname(pluginsClientPath), { recursive: true });
|
|
125
144
|
writeFileIfChanged(pluginsClientPath, `${pluginsClientLines.join("\n")}\n`);
|
|
145
|
+
if (opts.authSource) {
|
|
146
|
+
const authClientPath = join(opts.configDir, "api", "src", "auth-client.gen.ts");
|
|
147
|
+
const authClientLines = [];
|
|
148
|
+
const importPath = toImportPath(authClientPath, opts.authSource.sourceFilePath);
|
|
149
|
+
authClientLines.push(`import type { ContractType as ${opts.authSource.importName} } from "${importPath}";`);
|
|
150
|
+
authClientLines.push("import type { ContractRouterClient, AnyContractRouter } from \"@orpc/contract\";");
|
|
151
|
+
authClientLines.push("type ClientFactory<C extends AnyContractRouter> = (context?: Record<string, unknown>) => ContractRouterClient<C>;");
|
|
152
|
+
authClientLines.push("");
|
|
153
|
+
authClientLines.push(`export type AuthClient = ClientFactory<${opts.authSource.importName}>;`);
|
|
154
|
+
mkdirSync(dirname(authClientPath), { recursive: true });
|
|
155
|
+
writeFileIfChanged(authClientPath, `${authClientLines.join("\n")}\n`);
|
|
156
|
+
}
|
|
126
157
|
return uiContractPath;
|
|
127
158
|
}
|
|
128
159
|
async function syncApiContractBridge(opts) {
|
|
@@ -131,6 +162,7 @@ async function syncApiContractBridge(opts) {
|
|
|
131
162
|
const sources = [];
|
|
132
163
|
let manifest = null;
|
|
133
164
|
let generatedPath = null;
|
|
165
|
+
let authSource = null;
|
|
134
166
|
const baseSource = await resolveContractSource({
|
|
135
167
|
configDir: opts.configDir,
|
|
136
168
|
runtimeDir,
|
|
@@ -140,6 +172,19 @@ async function syncApiContractBridge(opts) {
|
|
|
140
172
|
generatedSubdir: "api"
|
|
141
173
|
});
|
|
142
174
|
sources.push(baseSource);
|
|
175
|
+
if (opts.runtimeConfig.auth) {
|
|
176
|
+
authSource = await resolveContractSource({
|
|
177
|
+
configDir: opts.configDir,
|
|
178
|
+
runtimeDir,
|
|
179
|
+
key: "auth",
|
|
180
|
+
source: opts.runtimeConfig.auth,
|
|
181
|
+
baseUrl: opts.runtimeConfig.auth.url,
|
|
182
|
+
generatedSubdir: "auth",
|
|
183
|
+
localSourceFactory: localAuthContractSource
|
|
184
|
+
});
|
|
185
|
+
sources.push(authSource);
|
|
186
|
+
if (authSource.generatedPath) generatedPath = authSource.generatedPath;
|
|
187
|
+
}
|
|
143
188
|
for (const [key, plugin] of pluginEntries) {
|
|
144
189
|
const source = await resolveContractSource({
|
|
145
190
|
configDir: opts.configDir,
|
|
@@ -152,10 +197,12 @@ async function syncApiContractBridge(opts) {
|
|
|
152
197
|
sources.push(source);
|
|
153
198
|
if (source.generatedPath) generatedPath = source.generatedPath;
|
|
154
199
|
}
|
|
155
|
-
|
|
200
|
+
const allPluginKeys = pluginEntries.map(([key]) => key);
|
|
201
|
+
writeGeneratedFiles({
|
|
156
202
|
configDir: opts.configDir,
|
|
157
203
|
sources,
|
|
158
|
-
pluginKeys:
|
|
204
|
+
pluginKeys: allPluginKeys,
|
|
205
|
+
authSource
|
|
159
206
|
});
|
|
160
207
|
if (opts.runtimeConfig.api.source !== "local") manifest = await fetchApiPluginManifest(opts.apiBaseUrl);
|
|
161
208
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-contract.mjs","names":[],"sources":["../src/api-contract.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport { mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join, relative } from \"node:path\";\nimport type { RuntimeConfig, RuntimePluginConfig } from \"./types\";\n\nexport interface ApiPluginManifest {\n schemaVersion: 1;\n kind: \"every-plugin/manifest\";\n plugin: {\n name: string;\n version: string;\n };\n runtime: {\n remoteEntry: string;\n };\n contract?: {\n kind: \"orpc\";\n types: {\n path: string;\n exportName: string;\n typeName: string;\n sha256?: string;\n };\n };\n}\n\ninterface ContractSource {\n key: string;\n importName: string;\n sourceFilePath: string;\n generatedPath?: string;\n}\n\nfunction sha256(input: string): string {\n return createHash(\"sha256\").update(input).digest(\"hex\");\n}\n\nfunction trimTrailingSlash(input: string): string {\n return input.replace(/\\/$/, \"\");\n}\n\nfunction sanitizeIdentifier(input: string): string {\n return input.replace(/[^A-Za-z0-9_]/g, \"_\").replace(/^[^A-Za-z_]+/, \"_\");\n}\n\nfunction toImportPath(fromFile: string, targetFile: string): string {\n const rel = relative(dirname(fromFile), targetFile).replace(/\\\\/g, \"/\");\n return rel.startsWith(\".\") ? rel : `./${rel}`;\n}\n\nfunction writeFileIfChanged(filePath: string, content: string) {\n try {\n if (readFileSync(filePath, \"utf8\") === content) return false;\n } catch {\n // file does not exist yet\n }\n\n writeFileSync(filePath, content);\n return true;\n}\n\nfunction getApiPluginManifestUrl(apiBaseUrl: string): string {\n return `${trimTrailingSlash(apiBaseUrl)}/plugin.manifest.json`;\n}\n\nasync function fetchApiPluginManifest(apiBaseUrl: string): Promise<ApiPluginManifest> {\n const response = await fetch(getApiPluginManifestUrl(apiBaseUrl));\n if (!response.ok) {\n throw new Error(\n `Failed to fetch API plugin manifest: ${response.status} ${response.statusText}`,\n );\n }\n\n const manifest = (await response.json()) as ApiPluginManifest;\n if (manifest.schemaVersion !== 1 || manifest.kind !== \"every-plugin/manifest\") {\n throw new Error(\"Unsupported API plugin manifest format\");\n }\n\n return manifest;\n}\n\nfunction localApiContractSource(configDir: string): ContractSource {\n const sourcePath = join(configDir, \"api\", \"src\", \"contract.ts\");\n return {\n key: \"api\",\n importName: \"BaseApiContract\",\n sourceFilePath: sourcePath,\n };\n}\n\nasync function remoteContractSource(opts: {\n configDir: string;\n runtimeDir: string;\n name: string;\n baseUrl: string;\n generatedSubdir: string;\n}): Promise<ContractSource> {\n const manifest = await fetchApiPluginManifest(opts.baseUrl);\n if (!manifest.contract) {\n throw new Error(\n `Plugin manifest for ${manifest.plugin.name} does not advertise contract types`,\n );\n }\n\n const contractUrl = `${trimTrailingSlash(opts.baseUrl)}/${manifest.contract.types.path.replace(/^\\.\\//, \"\")}`;\n const contractResponse = await fetch(contractUrl);\n if (!contractResponse.ok) {\n throw new Error(\n `Failed to fetch contract types: ${contractResponse.status} ${contractResponse.statusText}`,\n );\n }\n\n const contractTypes = await contractResponse.text();\n if (manifest.contract.types.sha256 && manifest.contract.types.sha256 !== sha256(contractTypes)) {\n throw new Error(\"Fetched contract types failed checksum verification\");\n }\n\n const generatedPath = join(opts.runtimeDir, opts.generatedSubdir, \"contract.d.ts\");\n mkdirSync(dirname(generatedPath), { recursive: true });\n writeFileIfChanged(generatedPath, contractTypes);\n\n return {\n key: opts.name,\n importName: `${sanitizeIdentifier(opts.name)}Contract`,\n sourceFilePath: generatedPath,\n generatedPath,\n };\n}\n\nasync function resolveContractSource(opts: {\n configDir: string;\n runtimeDir: string;\n key: string;\n source: RuntimePluginConfig | { url: string; localPath?: string; name: string } | null;\n baseUrl: string;\n generatedSubdir: string;\n}): Promise<ContractSource> {\n if (\n opts.key === \"api\" &&\n (!opts.source || !(\"localPath\" in opts.source) || opts.source.localPath)\n ) {\n const localPath = opts.source && \"localPath\" in opts.source ? opts.source.localPath : undefined;\n if (localPath) {\n return {\n key: opts.key,\n importName: \"BaseApiContract\",\n sourceFilePath: join(localPath, \"src\", \"contract.ts\"),\n };\n }\n\n if (!opts.baseUrl) {\n return localApiContractSource(opts.configDir);\n }\n }\n\n if (opts.source && \"localPath\" in opts.source && opts.source.localPath) {\n return {\n key: opts.key,\n importName: `${sanitizeIdentifier(opts.key)}Contract`,\n sourceFilePath: join(opts.source.localPath, \"src\", \"contract.ts\"),\n };\n }\n\n return remoteContractSource({\n configDir: opts.configDir,\n runtimeDir: opts.runtimeDir,\n name: opts.key,\n baseUrl: opts.baseUrl,\n generatedSubdir: opts.generatedSubdir,\n });\n}\n\nfunction writeAggregateContractFile(opts: {\n configDir: string;\n sources: ContractSource[];\n pluginKeys: string[];\n}) {\n const baseSource = opts.sources.find((source) => source.key === \"api\");\n const pluginSources = opts.pluginKeys\n .map((key) => opts.sources.find((entry) => entry.key === key))\n .filter((source): source is ContractSource => Boolean(source));\n\n if (!baseSource) {\n throw new Error(\"API contract source is required to generate the aggregate contract\");\n }\n\n // --- Generate ui/src/api-contract.gen.ts ---\n const uiContractPath = join(opts.configDir, \"ui\", \"src\", \"api-contract.gen.ts\");\n const uiLines: string[] = [];\n\n for (const source of opts.sources) {\n const importPath = toImportPath(uiContractPath, source.sourceFilePath);\n uiLines.push(`import type { ContractType as ${source.importName} } from \"${importPath}\";`);\n }\n\n uiLines.push(\"\");\n if (pluginSources.length === 0) {\n uiLines.push(`export type ApiContract = ${baseSource.importName};`);\n } else {\n uiLines.push(`export type ApiContract = ${baseSource.importName} & {`);\n for (const source of pluginSources) {\n const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key)\n ? source.key\n : JSON.stringify(source.key);\n uiLines.push(` ${key}: ${source.importName};`);\n }\n uiLines.push(\"};\");\n }\n mkdirSync(dirname(uiContractPath), { recursive: true });\n writeFileIfChanged(uiContractPath, `${uiLines.join(\"\\n\")}\\n`);\n\n // --- Generate api/src/plugins-client.gen.ts ---\n const pluginsClientPath = join(opts.configDir, \"api\", \"src\", \"plugins-client.gen.ts\");\n const pluginsClientLines: string[] = [];\n\n for (const source of pluginSources) {\n const importPath = toImportPath(pluginsClientPath, source.sourceFilePath);\n pluginsClientLines.push(\n `import type { ContractType as ${source.importName} } from \"${importPath}\";`,\n );\n }\n\n pluginsClientLines.push(\n 'import type { ContractRouterClient, AnyContractRouter } from \"@orpc/contract\";',\n );\n pluginsClientLines.push(\n \"type ClientFactory<C extends AnyContractRouter> = (context?: Record<string, unknown>) => ContractRouterClient<C>;\",\n );\n pluginsClientLines.push(\"\");\n\n if (pluginSources.length === 0) {\n pluginsClientLines.push(\"export type PluginsClient = Record<string, never>;\");\n } else {\n pluginsClientLines.push(\"export type PluginsClient = {\");\n for (const source of pluginSources) {\n const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key)\n ? source.key\n : JSON.stringify(source.key);\n pluginsClientLines.push(` ${key}: ClientFactory<${source.importName}>;`);\n }\n pluginsClientLines.push(\"};\");\n }\n\n mkdirSync(dirname(pluginsClientPath), { recursive: true });\n writeFileIfChanged(pluginsClientPath, `${pluginsClientLines.join(\"\\n\")}\\n`);\n\n return uiContractPath;\n}\n\nexport async function syncApiContractBridge(opts: {\n configDir: string;\n runtimeConfig: RuntimeConfig;\n apiBaseUrl: string;\n}): Promise<{\n bridgePath: string;\n generatedPath: string | null;\n manifest: ApiPluginManifest | null;\n source: \"local\" | \"remote\";\n}> {\n const runtimeDir = join(opts.configDir, \".bos\", \"generated\");\n const pluginEntries = Object.entries(opts.runtimeConfig.plugins ?? {}).sort(([a], [b]) =>\n a.localeCompare(b),\n );\n const sources: ContractSource[] = [];\n let manifest: ApiPluginManifest | null = null;\n let generatedPath: string | null = null;\n\n const baseSource = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key: \"api\",\n source: opts.runtimeConfig.api,\n baseUrl: opts.apiBaseUrl,\n generatedSubdir: \"api\",\n });\n sources.push(baseSource);\n\n for (const [key, plugin] of pluginEntries) {\n const source = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key,\n source: plugin,\n baseUrl: plugin.url,\n generatedSubdir: `plugins/${key}`,\n });\n sources.push(source);\n if (source.generatedPath) {\n generatedPath = source.generatedPath;\n }\n }\n\n writeAggregateContractFile({\n configDir: opts.configDir,\n sources,\n pluginKeys: pluginEntries.map(([key]) => key),\n });\n\n if (opts.runtimeConfig.api.source !== \"local\") {\n manifest = await fetchApiPluginManifest(opts.apiBaseUrl);\n }\n\n return {\n bridgePath: join(opts.configDir, \"ui\", \"src\", \"api-contract.gen.ts\"),\n generatedPath,\n manifest,\n source: opts.runtimeConfig.api.source,\n };\n}\n"],"mappings":";;;;;AAiCA,SAAS,OAAO,OAAuB;AACrC,QAAO,WAAW,SAAS,CAAC,OAAO,MAAM,CAAC,OAAO,MAAM;;AAGzD,SAAS,kBAAkB,OAAuB;AAChD,QAAO,MAAM,QAAQ,OAAO,GAAG;;AAGjC,SAAS,mBAAmB,OAAuB;AACjD,QAAO,MAAM,QAAQ,kBAAkB,IAAI,CAAC,QAAQ,gBAAgB,IAAI;;AAG1E,SAAS,aAAa,UAAkB,YAA4B;CAClE,MAAM,MAAM,SAAS,QAAQ,SAAS,EAAE,WAAW,CAAC,QAAQ,OAAO,IAAI;AACvE,QAAO,IAAI,WAAW,IAAI,GAAG,MAAM,KAAK;;AAG1C,SAAS,mBAAmB,UAAkB,SAAiB;AAC7D,KAAI;AACF,MAAI,aAAa,UAAU,OAAO,KAAK,QAAS,QAAO;SACjD;AAIR,eAAc,UAAU,QAAQ;AAChC,QAAO;;AAGT,SAAS,wBAAwB,YAA4B;AAC3D,QAAO,GAAG,kBAAkB,WAAW,CAAC;;AAG1C,eAAe,uBAAuB,YAAgD;CACpF,MAAM,WAAW,MAAM,MAAM,wBAAwB,WAAW,CAAC;AACjE,KAAI,CAAC,SAAS,GACZ,OAAM,IAAI,MACR,wCAAwC,SAAS,OAAO,GAAG,SAAS,aACrE;CAGH,MAAM,WAAY,MAAM,SAAS,MAAM;AACvC,KAAI,SAAS,kBAAkB,KAAK,SAAS,SAAS,wBACpD,OAAM,IAAI,MAAM,yCAAyC;AAG3D,QAAO;;AAGT,SAAS,uBAAuB,WAAmC;AAEjE,QAAO;EACL,KAAK;EACL,YAAY;EACZ,gBAJiB,KAAK,WAAW,OAAO,OAAO,cAAc;EAK9D;;AAGH,eAA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,WAAW;GAC7B,CAAC;AACF,UAAQ,KAAK,OAAO;AACpB,MAAI,OAAO,cACT,iBAAgB,OAAO;;AAI3B,4BAA2B;EACzB,WAAW,KAAK;EAChB;EACA,YAAY,cAAc,KAAK,CAAC,SAAS,IAAI;EAC9C,CAAC;AAEF,KAAI,KAAK,cAAc,IAAI,WAAW,QACpC,YAAW,MAAM,uBAAuB,KAAK,WAAW;AAG1D,QAAO;EACL,YAAY,KAAK,KAAK,WAAW,MAAM,OAAO,sBAAsB;EACpE;EACA;EACA,QAAQ,KAAK,cAAc,IAAI;EAChC"}
|
|
1
|
+
{"version":3,"file":"api-contract.mjs","names":[],"sources":["../src/api-contract.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport { mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join, relative } from \"node:path\";\nimport type { RuntimeConfig, RuntimePluginConfig } from \"./types\";\n\nexport interface ApiPluginManifest {\n schemaVersion: 1;\n kind: \"every-plugin/manifest\";\n plugin: {\n name: string;\n version: string;\n };\n runtime: {\n remoteEntry: string;\n };\n contract?: {\n kind: \"orpc\";\n types: {\n path: string;\n exportName: string;\n typeName: string;\n sha256?: string;\n };\n };\n}\n\ninterface ContractSource {\n key: string;\n importName: string;\n sourceFilePath: string;\n generatedPath?: string;\n}\n\nfunction sha256(input: string): string {\n return createHash(\"sha256\").update(input).digest(\"hex\");\n}\n\nfunction trimTrailingSlash(input: string): string {\n return input.replace(/\\/$/, \"\");\n}\n\nfunction sanitizeIdentifier(input: string): string {\n return input.replace(/[^A-Za-z0-9_]/g, \"_\").replace(/^[^A-Za-z_]+/, \"_\");\n}\n\nfunction toImportPath(fromFile: string, targetFile: string): string {\n const rel = relative(dirname(fromFile), targetFile).replace(/\\\\/g, \"/\");\n return rel.startsWith(\".\") ? rel : `./${rel}`;\n}\n\nfunction writeFileIfChanged(filePath: string, content: string) {\n try {\n if (readFileSync(filePath, \"utf8\") === content) return false;\n } catch {\n // file does not exist yet\n }\n\n writeFileSync(filePath, content);\n return true;\n}\n\nfunction getApiPluginManifestUrl(apiBaseUrl: string): string {\n return `${trimTrailingSlash(apiBaseUrl)}/plugin.manifest.json`;\n}\n\nasync function fetchApiPluginManifest(apiBaseUrl: string): Promise<ApiPluginManifest> {\n const response = await fetch(getApiPluginManifestUrl(apiBaseUrl));\n if (!response.ok) {\n throw new Error(\n `Failed to fetch API plugin manifest: ${response.status} ${response.statusText}`,\n );\n }\n\n const manifest = (await response.json()) as ApiPluginManifest;\n if (manifest.schemaVersion !== 1 || manifest.kind !== \"every-plugin/manifest\") {\n throw new Error(\"Unsupported API plugin manifest format\");\n }\n\n return manifest;\n}\n\nfunction localApiContractSource(configDir: string): ContractSource {\n const sourcePath = join(configDir, \"api\", \"src\", \"contract.ts\");\n return {\n key: \"api\",\n importName: \"BaseApiContract\",\n sourceFilePath: sourcePath,\n };\n}\n\nfunction localAuthContractSource(configDir: string): ContractSource {\n const sourcePath = join(configDir, \"plugins\", \"auth\", \"src\", \"contract.ts\");\n return {\n key: \"auth\",\n importName: \"authContract\",\n sourceFilePath: sourcePath,\n };\n}\n\nasync function remoteContractSource(opts: {\n configDir: string;\n runtimeDir: string;\n name: string;\n baseUrl: string;\n generatedSubdir: string;\n}): Promise<ContractSource> {\n const manifest = await fetchApiPluginManifest(opts.baseUrl);\n if (!manifest.contract) {\n throw new Error(\n `Plugin manifest for ${manifest.plugin.name} does not advertise contract types`,\n );\n }\n\n const contractUrl = `${trimTrailingSlash(opts.baseUrl)}/${manifest.contract.types.path.replace(/^\\.\\//, \"\")}`;\n const contractResponse = await fetch(contractUrl);\n if (!contractResponse.ok) {\n throw new Error(\n `Failed to fetch contract types: ${contractResponse.status} ${contractResponse.statusText}`,\n );\n }\n\n const contractTypes = await contractResponse.text();\n if (manifest.contract.types.sha256 && manifest.contract.types.sha256 !== sha256(contractTypes)) {\n throw new Error(\"Fetched contract types failed checksum verification\");\n }\n\n const generatedPath = join(opts.runtimeDir, opts.generatedSubdir, \"contract.d.ts\");\n mkdirSync(dirname(generatedPath), { recursive: true });\n writeFileIfChanged(generatedPath, contractTypes);\n\n return {\n key: opts.name,\n importName: `${sanitizeIdentifier(opts.name)}Contract`,\n sourceFilePath: generatedPath,\n generatedPath,\n };\n}\n\nasync function resolveContractSource(opts: {\n configDir: string;\n runtimeDir: string;\n key: string;\n source: RuntimePluginConfig | { url: string; localPath?: string; name: string } | null;\n baseUrl: string;\n generatedSubdir: string;\n localSourceFactory?: (configDir: string) => ContractSource;\n}): Promise<ContractSource> {\n if (\n opts.key === \"api\" &&\n (!opts.source || !(\"localPath\" in opts.source) || opts.source.localPath)\n ) {\n const localPath = opts.source && \"localPath\" in opts.source ? opts.source.localPath : undefined;\n if (localPath) {\n return {\n key: opts.key,\n importName: \"BaseApiContract\",\n sourceFilePath: join(localPath, \"src\", \"contract.ts\"),\n };\n }\n\n if (!opts.baseUrl) {\n return localApiContractSource(opts.configDir);\n }\n }\n\n if (\n opts.key === \"auth\" &&\n opts.localSourceFactory &&\n (!opts.source || !(\"localPath\" in opts.source) || opts.source.localPath)\n ) {\n const localPath = opts.source && \"localPath\" in opts.source ? opts.source.localPath : undefined;\n if (localPath) {\n return {\n key: opts.key,\n importName: \"authContract\",\n sourceFilePath: join(localPath, \"src\", \"contract.ts\"),\n };\n }\n\n if (!opts.baseUrl) {\n return opts.localSourceFactory(opts.configDir);\n }\n }\n\n if (opts.source && \"localPath\" in opts.source && opts.source.localPath) {\n return {\n key: opts.key,\n importName: `${sanitizeIdentifier(opts.key)}Contract`,\n sourceFilePath: join(opts.source.localPath, \"src\", \"contract.ts\"),\n };\n }\n\n return remoteContractSource({\n configDir: opts.configDir,\n runtimeDir: opts.runtimeDir,\n name: opts.key,\n baseUrl: opts.baseUrl,\n generatedSubdir: opts.generatedSubdir,\n });\n}\n\nfunction writeGeneratedFiles(opts: {\n configDir: string;\n sources: ContractSource[];\n pluginKeys: string[];\n authSource: ContractSource | null;\n}) {\n const baseSource = opts.sources.find((source) => source.key === \"api\");\n const pluginSources = opts.pluginKeys\n .map((key) => opts.sources.find((entry) => entry.key === key))\n .filter((source): source is ContractSource => Boolean(source));\n\n if (!baseSource) {\n throw new Error(\"API contract source is required to generate the aggregate contract\");\n }\n\n // --- Generate ui/src/api-contract.gen.ts ---\n const uiContractPath = join(opts.configDir, \"ui\", \"src\", \"api-contract.gen.ts\");\n const uiLines: string[] = [];\n\n for (const source of opts.sources) {\n const importPath = toImportPath(uiContractPath, source.sourceFilePath);\n uiLines.push(`import type { ContractType as ${source.importName} } from \"${importPath}\";`);\n }\n\n uiLines.push(\"\");\n\n const compositeParts: string[] = [];\n if (opts.authSource) {\n compositeParts.push(`auth: ${opts.authSource.importName}`);\n }\n for (const source of pluginSources) {\n const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key) ? source.key : JSON.stringify(source.key);\n compositeParts.push(`${key}: ${source.importName}`);\n }\n\n if (compositeParts.length === 0) {\n uiLines.push(`export type ApiContract = ${baseSource.importName};`);\n } else {\n uiLines.push(`export type ApiContract = ${baseSource.importName} & {`);\n for (const part of compositeParts) {\n uiLines.push(` ${part};`);\n }\n uiLines.push(\"};\");\n }\n mkdirSync(dirname(uiContractPath), { recursive: true });\n writeFileIfChanged(uiContractPath, `${uiLines.join(\"\\n\")}\\n`);\n\n // --- Generate api/src/plugins-client.gen.ts ---\n const pluginsClientPath = join(opts.configDir, \"api\", \"src\", \"plugins-client.gen.ts\");\n const pluginsClientLines: string[] = [];\n\n for (const source of pluginSources) {\n const importPath = toImportPath(pluginsClientPath, source.sourceFilePath);\n pluginsClientLines.push(\n `import type { ContractType as ${source.importName} } from \"${importPath}\";`,\n );\n }\n\n pluginsClientLines.push(\n 'import type { ContractRouterClient, AnyContractRouter } from \"@orpc/contract\";',\n );\n pluginsClientLines.push(\n \"type ClientFactory<C extends AnyContractRouter> = (context?: Record<string, unknown>) => ContractRouterClient<C>;\",\n );\n pluginsClientLines.push(\"\");\n\n if (pluginSources.length === 0) {\n pluginsClientLines.push(\"export type PluginsClient = Record<string, never>;\");\n } else {\n pluginsClientLines.push(\"export type PluginsClient = {\");\n for (const source of pluginSources) {\n const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key)\n ? source.key\n : JSON.stringify(source.key);\n pluginsClientLines.push(` ${key}: ClientFactory<${source.importName}>;`);\n }\n pluginsClientLines.push(\"};\");\n }\n\n mkdirSync(dirname(pluginsClientPath), { recursive: true });\n writeFileIfChanged(pluginsClientPath, `${pluginsClientLines.join(\"\\n\")}\\n`);\n\n // --- Generate api/src/auth-client.gen.ts ---\n if (opts.authSource) {\n const authClientPath = join(opts.configDir, \"api\", \"src\", \"auth-client.gen.ts\");\n const authClientLines: string[] = [];\n\n const importPath = toImportPath(authClientPath, opts.authSource.sourceFilePath);\n authClientLines.push(\n `import type { ContractType as ${opts.authSource.importName} } from \"${importPath}\";`,\n );\n authClientLines.push(\n 'import type { ContractRouterClient, AnyContractRouter } from \"@orpc/contract\";',\n );\n authClientLines.push(\n \"type ClientFactory<C extends AnyContractRouter> = (context?: Record<string, unknown>) => ContractRouterClient<C>;\",\n );\n authClientLines.push(\"\");\n authClientLines.push(`export type AuthClient = ClientFactory<${opts.authSource.importName}>;`);\n\n mkdirSync(dirname(authClientPath), { recursive: true });\n writeFileIfChanged(authClientPath, `${authClientLines.join(\"\\n\")}\\n`);\n }\n\n return uiContractPath;\n}\n\nexport async function syncApiContractBridge(opts: {\n configDir: string;\n runtimeConfig: RuntimeConfig;\n apiBaseUrl: string;\n}): Promise<{\n bridgePath: string;\n generatedPath: string | null;\n manifest: ApiPluginManifest | null;\n source: \"local\" | \"remote\";\n}> {\n const runtimeDir = join(opts.configDir, \".bos\", \"generated\");\n const pluginEntries = Object.entries(opts.runtimeConfig.plugins ?? {}).sort(([a], [b]) =>\n a.localeCompare(b),\n );\n const sources: ContractSource[] = [];\n let manifest: ApiPluginManifest | null = null;\n let generatedPath: string | null = null;\n let authSource: ContractSource | null = null;\n\n const baseSource = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key: \"api\",\n source: opts.runtimeConfig.api,\n baseUrl: opts.apiBaseUrl,\n generatedSubdir: \"api\",\n });\n sources.push(baseSource);\n\n if (opts.runtimeConfig.auth) {\n authSource = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key: \"auth\",\n source: opts.runtimeConfig.auth,\n baseUrl: opts.runtimeConfig.auth.url,\n generatedSubdir: \"auth\",\n localSourceFactory: localAuthContractSource,\n });\n sources.push(authSource);\n if (authSource.generatedPath) {\n generatedPath = authSource.generatedPath;\n }\n }\n\n for (const [key, plugin] of pluginEntries) {\n const source = await resolveContractSource({\n configDir: opts.configDir,\n runtimeDir,\n key,\n source: plugin,\n baseUrl: plugin.url,\n generatedSubdir: `plugins/${key}`,\n });\n sources.push(source);\n if (source.generatedPath) {\n generatedPath = source.generatedPath;\n }\n }\n\n const allPluginKeys = pluginEntries.map(([key]) => key);\n\n writeGeneratedFiles({\n configDir: opts.configDir,\n sources,\n pluginKeys: allPluginKeys,\n authSource,\n });\n\n if (opts.runtimeConfig.api.source !== \"local\") {\n manifest = await fetchApiPluginManifest(opts.apiBaseUrl);\n }\n\n return {\n bridgePath: join(opts.configDir, \"ui\", \"src\", \"api-contract.gen.ts\"),\n generatedPath,\n manifest,\n source: opts.runtimeConfig.api.source,\n };\n}\n"],"mappings":";;;;;AAiCA,SAAS,OAAO,OAAuB;AACrC,QAAO,WAAW,SAAS,CAAC,OAAO,MAAM,CAAC,OAAO,MAAM;;AAGzD,SAAS,kBAAkB,OAAuB;AAChD,QAAO,MAAM,QAAQ,OAAO,GAAG;;AAGjC,SAAS,mBAAmB,OAAuB;AACjD,QAAO,MAAM,QAAQ,kBAAkB,IAAI,CAAC,QAAQ,gBAAgB,IAAI;;AAG1E,SAAS,aAAa,UAAkB,YAA4B;CAClE,MAAM,MAAM,SAAS,QAAQ,SAAS,EAAE,WAAW,CAAC,QAAQ,OAAO,IAAI;AACvE,QAAO,IAAI,WAAW,IAAI,GAAG,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 3e3;
|
|
12
12
|
const DEFAULT_UI_PORT = 3002;
|
|
13
13
|
const DEFAULT_API_PORT = 3014;
|
|
14
|
+
const DEFAULT_AUTH_PORT = 3020;
|
|
14
15
|
const DEFAULT_PLUGIN_PORT_START = 3021;
|
|
15
16
|
function detectLocalPackages(bosConfig, runtimeConfig) {
|
|
16
17
|
const packages = [];
|
|
@@ -23,18 +24,24 @@ function detectLocalPackages(bosConfig, runtimeConfig) {
|
|
|
23
24
|
if (hostLocalPath && (0, node_fs.existsSync)((0, node_path.join)(hostLocalPath, "package.json"))) packages.push("host");
|
|
24
25
|
else if ((0, node_fs.existsSync)((0, node_path.join)(configDir, "host", "package.json"))) packages.push("host");
|
|
25
26
|
for (const [pluginId, pluginConfig] of Object.entries(runtimeConfig?.plugins ?? {})) if (pluginConfig.localPath && (0, node_fs.existsSync)((0, node_path.join)(pluginConfig.localPath, "package.json"))) packages.push(`plugin:${pluginId}`);
|
|
27
|
+
const authLocalPath = runtimeConfig?.auth?.localPath ?? require_config.resolveLocalDevelopmentPath(bosConfig?.app.auth?.development, configDir);
|
|
28
|
+
if (authLocalPath && (0, node_fs.existsSync)((0, node_path.join)(authLocalPath, "package.json"))) packages.push("auth");
|
|
26
29
|
return packages;
|
|
27
30
|
}
|
|
28
31
|
function buildRuntimeConfig(bosConfig, options) {
|
|
29
32
|
const configDir = require_config.getProjectRoot();
|
|
30
33
|
const uiConfig = bosConfig.app.ui;
|
|
31
34
|
const apiConfig = bosConfig.app.api;
|
|
35
|
+
const authConfig = bosConfig.app.auth;
|
|
32
36
|
const uiSource = options.uiSource ?? "local";
|
|
33
37
|
const apiSource = options.apiSource ?? "local";
|
|
38
|
+
const authSource = options.authSource ?? "local";
|
|
34
39
|
const uiLocalPath = require_config.resolveLocalDevelopmentPath(uiConfig.development, configDir);
|
|
35
40
|
const apiLocalPath = require_config.resolveLocalDevelopmentPath(apiConfig.development, configDir);
|
|
41
|
+
const authLocalPath = authConfig ? require_config.resolveLocalDevelopmentPath(authConfig.development, configDir) : null;
|
|
36
42
|
const uiLocalUrl = !uiLocalPath && uiConfig.development && !require_config.isLocalDevelopmentTarget(uiConfig.development) ? uiConfig.development : "";
|
|
37
43
|
const apiLocalUrl = !apiLocalPath && apiConfig.development && !require_config.isLocalDevelopmentTarget(apiConfig.development) ? apiConfig.development : "";
|
|
44
|
+
const authLocalUrl = authConfig && !authLocalPath && authConfig.development && !require_config.isLocalDevelopmentTarget(authConfig.development) ? authConfig.development : "";
|
|
38
45
|
return {
|
|
39
46
|
env: options.env ?? "development",
|
|
40
47
|
account: bosConfig.account,
|
|
@@ -50,7 +57,7 @@ function buildRuntimeConfig(bosConfig, options) {
|
|
|
50
57
|
port: uiSource === "local" && uiLocalUrl ? require_config.parsePort(uiLocalUrl) : void 0,
|
|
51
58
|
ssrUrl: uiSource === "remote" ? uiConfig.ssr : void 0,
|
|
52
59
|
ssrIntegrity: uiSource === "remote" ? uiConfig.ssrIntegrity : void 0,
|
|
53
|
-
integrity: uiSource === "remote" ? uiConfig.
|
|
60
|
+
integrity: uiSource === "remote" ? uiConfig.integrity : void 0,
|
|
54
61
|
source: uiSource === "local" ? uiLocalPath ? "local" : "remote" : "remote"
|
|
55
62
|
} : {
|
|
56
63
|
name: "ui",
|
|
@@ -68,13 +75,25 @@ function buildRuntimeConfig(bosConfig, options) {
|
|
|
68
75
|
proxy: options.proxy ?? apiConfig.proxy,
|
|
69
76
|
variables: apiConfig.variables,
|
|
70
77
|
secrets: apiConfig.secrets,
|
|
71
|
-
integrity: apiSource === "remote" ? apiConfig.
|
|
78
|
+
integrity: apiSource === "remote" ? apiConfig.integrity : void 0
|
|
72
79
|
} : {
|
|
73
80
|
name: "api",
|
|
74
81
|
url: "",
|
|
75
82
|
entry: "/mf-manifest.json",
|
|
76
83
|
source: apiSource
|
|
77
84
|
},
|
|
85
|
+
auth: authConfig ? {
|
|
86
|
+
name: require_config.resolvePluginRuntimeName(void 0, authSource === "local" ? authLocalPath ?? void 0 : void 0, authConfig.name),
|
|
87
|
+
url: authSource === "remote" ? authConfig.production ?? "" : authLocalUrl,
|
|
88
|
+
entry: authSource === "remote" ? `${authConfig.production ?? ""}/mf-manifest.json` : authLocalUrl ? `${authLocalUrl}/mf-manifest.json` : "/mf-manifest.json",
|
|
89
|
+
localPath: authSource === "local" ? authLocalPath ?? void 0 : void 0,
|
|
90
|
+
port: authSource === "local" && authLocalUrl ? require_config.parsePort(authLocalUrl) : void 0,
|
|
91
|
+
source: authSource === "local" ? authLocalPath ? "local" : "remote" : "remote",
|
|
92
|
+
proxy: authConfig.proxy,
|
|
93
|
+
variables: authConfig.variables,
|
|
94
|
+
secrets: authConfig.secrets,
|
|
95
|
+
integrity: authSource === "remote" ? authConfig.integrity : void 0
|
|
96
|
+
} : void 0,
|
|
78
97
|
plugins: options.plugins
|
|
79
98
|
};
|
|
80
99
|
}
|
|
@@ -122,6 +141,7 @@ async function prepareDevelopmentRuntimeConfig(runtimeConfig, options) {
|
|
|
122
141
|
hostUrl: `http://localhost:${hostPort}`,
|
|
123
142
|
ui: { ...runtimeConfig.ui },
|
|
124
143
|
api: { ...runtimeConfig.api },
|
|
144
|
+
auth: runtimeConfig.auth ? { ...runtimeConfig.auth } : void 0,
|
|
125
145
|
plugins: runtimeConfig.plugins ? { ...runtimeConfig.plugins } : void 0
|
|
126
146
|
};
|
|
127
147
|
if (next.ui.source === "local" && next.ui.localPath) {
|
|
@@ -146,6 +166,10 @@ async function prepareDevelopmentRuntimeConfig(runtimeConfig, options) {
|
|
|
146
166
|
pluginBasePort = pluginPort + 1;
|
|
147
167
|
}
|
|
148
168
|
}
|
|
169
|
+
if (next.auth?.source === "local" && next.auth.localPath) {
|
|
170
|
+
const authPort = await pickAvailablePort(next.auth.port ?? DEFAULT_AUTH_PORT, usedPorts);
|
|
171
|
+
next.auth = withLocalRuntimeUrl(next.auth, authPort);
|
|
172
|
+
}
|
|
149
173
|
return next;
|
|
150
174
|
}
|
|
151
175
|
|
package/dist/app.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.cjs","names":["getProjectRoot","resolveLocalDevelopmentPath","isLocalDevelopmentTarget","getNetworkIdForAccount","parsePort"],"sources":["../src/app.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { createConnection } from \"node:net\";\nimport { join } from \"node:path\";\nimport { Effect } from \"effect\";\nimport {\n getProjectRoot,\n isLocalDevelopmentTarget,\n parsePort,\n resolveLocalDevelopmentPath,\n} from \"./config\";\nimport { getNetworkIdForAccount } from \"./network\";\nimport { makeDevProcess, type ProcessCallbacks, type ProcessHandle } from \"./orchestrator\";\nimport type { ProcessRegistry } from \"./process-registry\";\nimport type { BosConfig, RuntimeConfig, RuntimePluginConfig } from \"./types\";\n\nexport interface AppOrchestrator {\n packages: string[];\n env: Record<string, string>;\n description: string;\n bosConfig: BosConfig;\n runtimeConfig: RuntimeConfig;\n port?: number;\n interactive?: boolean;\n}\n\nconst STARTUP_ORDER = [\"ui-ssr\", \"ui\", \"api\", \"plugin\", \"host-build\", \"host\"];\nconst DEFAULT_HOST_PORT = 3000;\nconst DEFAULT_UI_PORT = 3002;\nconst DEFAULT_API_PORT = 3014;\nconst DEFAULT_PLUGIN_PORT_START = 3021;\n\nconst sortByOrder = (packages: string[]): string[] => {\n return [...packages].sort((a, b) => {\n const aIdx = a.startsWith(\"plugin:\")\n ? STARTUP_ORDER.indexOf(\"plugin\")\n : STARTUP_ORDER.indexOf(a);\n const bIdx = b.startsWith(\"plugin:\")\n ? STARTUP_ORDER.indexOf(\"plugin\")\n : STARTUP_ORDER.indexOf(b);\n if (aIdx === -1 && bIdx === -1) return 0;\n if (aIdx === -1) return 1;\n if (bIdx === -1) return -1;\n return aIdx - bIdx;\n });\n};\n\n// Note: log filtering and persistence lives at the CLI layer.\n\nexport interface DevServersHandle {\n handles: ProcessHandle[];\n shutdown: Effect.Effect<void>;\n}\n\nexport const startDevServers = (\n orchestrator: AppOrchestrator,\n callbacks: ProcessCallbacks,\n registry?: ProcessRegistry,\n) => {\n const run = Effect.gen(function* () {\n const orderedPackages = sortByOrder(orchestrator.packages);\n const handles: ProcessHandle[] = [];\n\n const startProcess = (pkg: string) => {\n const portOverride = pkg === \"host\" ? orchestrator.port : undefined;\n return makeDevProcess(\n pkg,\n orchestrator.env,\n callbacks,\n portOverride,\n orchestrator.bosConfig,\n orchestrator.runtimeConfig,\n registry,\n );\n };\n\n const startGroup = (packages: string[]) =>\n Effect.forEach(packages, startProcess, { concurrency: \"unbounded\" });\n\n const awaitReady = (pkg: string, handle: ProcessHandle) =>\n Effect.race(\n handle.waitForReady,\n Effect.sleep(\"30 seconds\").pipe(\n Effect.andThen(\n Effect.sync(() => {\n callbacks.onLog(pkg, \"Timeout waiting for ready, continuing...\", true);\n }),\n ),\n ),\n );\n\n const nonHostPackages = orderedPackages.filter((pkg) => pkg !== \"host\");\n const hostPackages = orderedPackages.filter((pkg) => pkg === \"host\");\n\n const nonHostHandles = yield* startGroup(nonHostPackages);\n handles.push(...nonHostHandles);\n\n yield* Effect.forEach(\n nonHostHandles.map((handle, index) => ({\n handle,\n pkg: nonHostPackages[index] ?? handle.name,\n })),\n ({ handle, pkg }) => awaitReady(pkg, handle),\n { concurrency: \"unbounded\" },\n );\n\n const hostHandles = yield* startGroup(hostPackages);\n handles.push(...hostHandles);\n\n yield* Effect.forEach(\n hostHandles.map((handle, index) => ({ handle, pkg: hostPackages[index] ?? handle.name })),\n ({ handle, pkg }) => awaitReady(pkg, handle),\n { concurrency: \"unbounded\" },\n );\n\n const shutdown = Effect.gen(function* () {\n const reversed = [...handles].reverse();\n for (const handle of reversed) {\n yield* Effect.tryPromise({\n try: () => handle.kill(),\n catch: () => null,\n }).pipe(Effect.ignore);\n }\n });\n\n return { handles, shutdown } satisfies DevServersHandle;\n });\n\n return run;\n};\n\nexport function detectLocalPackages(\n bosConfig?: BosConfig,\n runtimeConfig?: RuntimeConfig,\n): string[] {\n const packages: string[] = [];\n const configDir = getProjectRoot();\n\n const uiLocalPath =\n runtimeConfig?.ui.localPath ??\n resolveLocalDevelopmentPath(bosConfig?.app.ui.development, configDir);\n if (uiLocalPath && existsSync(join(uiLocalPath, \"package.json\"))) {\n packages.push(\"ui\");\n }\n\n const apiLocalPath =\n runtimeConfig?.api.localPath ??\n resolveLocalDevelopmentPath(bosConfig?.app.api.development, configDir);\n if (apiLocalPath && existsSync(join(apiLocalPath, \"package.json\"))) {\n packages.push(\"api\");\n }\n\n const hostLocalPath = resolveLocalDevelopmentPath(bosConfig?.app.host.development, configDir);\n if (hostLocalPath && existsSync(join(hostLocalPath, \"package.json\"))) {\n packages.push(\"host\");\n } else if (existsSync(join(configDir, \"host\", \"package.json\"))) {\n packages.push(\"host\");\n }\n\n for (const [pluginId, pluginConfig] of Object.entries(runtimeConfig?.plugins ?? {})) {\n if (pluginConfig.localPath && existsSync(join(pluginConfig.localPath, \"package.json\"))) {\n packages.push(`plugin:${pluginId}`);\n }\n }\n\n return packages;\n}\n\nexport function buildRuntimeConfig(\n bosConfig: BosConfig,\n options: {\n uiSource?: \"local\" | \"remote\";\n apiSource?: \"local\" | \"remote\";\n hostUrl: string;\n proxy?: string;\n env?: \"development\" | \"production\";\n plugins?: Record<string, RuntimePluginConfig>;\n },\n): RuntimeConfig {\n const configDir = getProjectRoot();\n const uiConfig = bosConfig.app.ui;\n const apiConfig = bosConfig.app.api;\n const uiSource = options.uiSource ?? \"local\";\n const apiSource = options.apiSource ?? \"local\";\n const uiLocalPath = resolveLocalDevelopmentPath(uiConfig.development, configDir);\n const apiLocalPath = resolveLocalDevelopmentPath(apiConfig.development, configDir);\n const uiLocalUrl =\n !uiLocalPath && uiConfig.development && !isLocalDevelopmentTarget(uiConfig.development)\n ? uiConfig.development\n : \"\";\n const apiLocalUrl =\n !apiLocalPath && apiConfig.development && !isLocalDevelopmentTarget(apiConfig.development)\n ? apiConfig.development\n : \"\";\n\n return {\n env: options.env ?? \"development\",\n account: bosConfig.account,\n domain: bosConfig.domain,\n networkId: getNetworkIdForAccount(bosConfig.account),\n hostUrl: options.hostUrl,\n shared: bosConfig.shared,\n ui: uiConfig\n ? {\n name: uiConfig.name,\n url: uiSource === \"remote\" ? (uiConfig.production ?? \"\") : uiLocalUrl,\n entry:\n uiSource === \"remote\"\n ? `${uiConfig.production ?? \"\"}/mf-manifest.json`\n : uiLocalUrl\n ? `${uiLocalUrl}/mf-manifest.json`\n : \"/mf-manifest.json\",\n localPath: uiSource === \"local\" ? (uiLocalPath ?? undefined) : undefined,\n port: uiSource === \"local\" && uiLocalUrl ? parsePort(uiLocalUrl) : undefined,\n ssrUrl: uiSource === \"remote\" ? uiConfig.ssr : undefined,\n ssrIntegrity: uiSource === \"remote\" ? uiConfig.ssrIntegrity : undefined,\n integrity: uiSource === \"remote\" ? uiConfig.productionIntegrity : undefined,\n source: uiSource === \"local\" ? (uiLocalPath ? \"local\" : \"remote\") : \"remote\",\n }\n : {\n name: \"ui\",\n url: \"\",\n entry: \"/mf-manifest.json\",\n source: uiSource,\n },\n api: apiConfig\n ? {\n name: apiConfig.name,\n url: apiSource === \"remote\" ? (apiConfig.production ?? \"\") : apiLocalUrl,\n entry:\n apiSource === \"remote\"\n ? `${apiConfig.production ?? \"\"}/mf-manifest.json`\n : apiLocalUrl\n ? `${apiLocalUrl}/mf-manifest.json`\n : \"/mf-manifest.json\",\n localPath: apiSource === \"local\" ? (apiLocalPath ?? undefined) : undefined,\n port: apiSource === \"local\" && apiLocalUrl ? parsePort(apiLocalUrl) : undefined,\n source: apiSource === \"local\" ? (apiLocalPath ? \"local\" : \"remote\") : \"remote\",\n proxy: options.proxy ?? apiConfig.proxy,\n variables: apiConfig.variables,\n secrets: apiConfig.secrets,\n integrity: apiSource === \"remote\" ? apiConfig.productionIntegrity : undefined,\n }\n : {\n name: \"api\",\n url: \"\",\n entry: \"/mf-manifest.json\",\n source: apiSource,\n },\n plugins: options.plugins,\n };\n}\n\nfunction probeTcpOpen(port: number, timeoutMs = 250): Promise<boolean> {\n return new Promise((resolve) => {\n const socket = createConnection({ host: \"127.0.0.1\", port });\n const timer = setTimeout(() => {\n socket.destroy();\n resolve(false);\n }, timeoutMs);\n\n socket.once(\"connect\", () => {\n clearTimeout(timer);\n socket.destroy();\n resolve(true);\n });\n\n socket.once(\"error\", () => {\n clearTimeout(timer);\n resolve(false);\n });\n });\n}\n\nasync function pickAvailablePort(preferred: number, usedPorts: Set<number>): Promise<number> {\n let port = preferred;\n while (usedPorts.has(port) || (await probeTcpOpen(port))) {\n port += 1;\n }\n usedPorts.add(port);\n return port;\n}\n\nfunction withLocalRuntimeUrl<\n T extends { url: string; entry: string; port?: number; localPath?: string },\n>(entry: T, port: number): T {\n const url = `http://localhost:${port}`;\n return {\n ...entry,\n url,\n entry: `${url}/mf-manifest.json`,\n port,\n };\n}\n\nexport async function prepareDevelopmentRuntimeConfig(\n runtimeConfig: RuntimeConfig,\n options?: { hostPort?: number; ssr?: boolean },\n): Promise<RuntimeConfig> {\n const usedPorts = new Set<number>();\n const hostPort = await pickAvailablePort(\n options?.hostPort ??\n (runtimeConfig.hostUrl ? parsePort(runtimeConfig.hostUrl) : DEFAULT_HOST_PORT),\n usedPorts,\n );\n\n const next: RuntimeConfig = {\n ...runtimeConfig,\n hostUrl: `http://localhost:${hostPort}`,\n ui: { ...runtimeConfig.ui },\n api: { ...runtimeConfig.api },\n plugins: runtimeConfig.plugins ? { ...runtimeConfig.plugins } : undefined,\n };\n\n if (next.ui.source === \"local\" && next.ui.localPath) {\n const uiPort = await pickAvailablePort(next.ui.port ?? DEFAULT_UI_PORT, usedPorts);\n next.ui = withLocalRuntimeUrl(next.ui, uiPort);\n if (options?.ssr) {\n const ssrPort = await pickAvailablePort(uiPort + 1, usedPorts);\n next.ui.ssrUrl = `http://localhost:${ssrPort}`;\n } else {\n next.ui.ssrUrl = undefined;\n }\n }\n\n if (next.api.source === \"local\" && next.api.localPath) {\n const apiPort = await pickAvailablePort(next.api.port ?? DEFAULT_API_PORT, usedPorts);\n next.api = withLocalRuntimeUrl(next.api, apiPort);\n }\n\n if (next.plugins) {\n const entries = Object.entries(next.plugins).sort(([a], [b]) => a.localeCompare(b));\n let pluginBasePort = DEFAULT_PLUGIN_PORT_START;\n\n for (const [pluginId, plugin] of entries) {\n if (plugin.source !== \"local\" || !plugin.localPath) {\n continue;\n }\n\n const pluginPort = await pickAvailablePort(plugin.port ?? pluginBasePort, usedPorts);\n next.plugins[pluginId] = withLocalRuntimeUrl(plugin, pluginPort);\n pluginBasePort = pluginPort + 1;\n }\n }\n\n return next;\n}\n"],"mappings":";;;;;;;;;;AA0BA,MAAM,oBAAoB;AAC1B,MAAM,kBAAkB;AACxB,MAAM,mBAAmB;AACzB,MAAM,4BAA4B;AAqGlC,SAAgB,oBACd,WACA,eACU;CACV,MAAM,WAAqB,EAAE;CAC7B,MAAM,YAAYA,+BAAgB;CAElC,MAAM,cACJ,eAAe,GAAG,aAClBC,2CAA4B,WAAW,IAAI,GAAG,aAAa,UAAU;AACvE,KAAI,2DAA+B,aAAa,eAAe,CAAC,CAC9D,UAAS,KAAK,KAAK;CAGrB,MAAM,eACJ,eAAe,IAAI,aACnBA,2CAA4B,WAAW,IAAI,IAAI,aAAa,UAAU;AACxE,KAAI,4DAAgC,cAAc,eAAe,CAAC,CAChE,UAAS,KAAK,MAAM;CAGtB,MAAM,gBAAgBA,2CAA4B,WAAW,IAAI,KAAK,aAAa,UAAU;AAC7F,KAAI,6DAAiC,eAAe,eAAe,CAAC,CAClE,UAAS,KAAK,OAAO;sDACI,WAAW,QAAQ,eAAe,CAAC,CAC5D,UAAS,KAAK,OAAO;AAGvB,MAAK,MAAM,CAAC,UAAU,iBAAiB,OAAO,QAAQ,eAAe,WAAW,EAAE,CAAC,CACjF,KAAI,aAAa,yDAA6B,aAAa,WAAW,eAAe,CAAC,CACpF,UAAS,KAAK,UAAU,WAAW;AAIvC,QAAO;;AAGT,SAAgB,mBACd,WACA,SAQe;CACf,MAAM,YAAYD,+BAAgB;CAClC,MAAM,WAAW,UAAU,IAAI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AAG,WAAW;EACnD,MAAM,SAAS,MAAM,kBAAkB,KAAK,GAAG,QAAQ,iBAAiB,UAAU;AAClF,OAAK,KAAK,oBAAoB,KAAK,IAAI,OAAO;AAC9C,MAAI,SAAS,KAAK;GAChB,MAAM,UAAU,MAAM,kBAAkB,SAAS,GAAG,UAAU;AAC9D,QAAK,GAAG,SAAS,oBAAoB;QAErC,MAAK,GAAG,SAAS;;AAIrB,KAAI,KAAK,IAAI,WAAW,WAAW,KAAK,IAAI,WAAW;EACrD,MAAM,UAAU,MAAM,kBAAkB,KAAK,IAAI,QAAQ,kBAAkB,UAAU;AACrF,OAAK,MAAM,oBAAoB,KAAK,KAAK,QAAQ;;AAGnD,KAAI,KAAK,SAAS;EAChB,MAAM,UAAU,OAAO,QAAQ,KAAK,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,cAAc,EAAE,CAAC;EACnF,IAAI,iBAAiB;AAErB,OAAK,MAAM,CAAC,UAAU,WAAW,SAAS;AACxC,OAAI,OAAO,WAAW,WAAW,CAAC,OAAO,UACvC;GAGF,MAAM,aAAa,MAAM,kBAAkB,OAAO,QAAQ,gBAAgB,UAAU;AACpF,QAAK,QAAQ,YAAY,oBAAoB,QAAQ,WAAW;AAChE,oBAAiB,aAAa;;;AAIlC,QAAO"}
|
|
1
|
+
{"version":3,"file":"app.cjs","names":["getProjectRoot","resolveLocalDevelopmentPath","isLocalDevelopmentTarget","getNetworkIdForAccount","parsePort","resolvePluginRuntimeName"],"sources":["../src/app.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { createConnection } from \"node:net\";\nimport { join } from \"node:path\";\nimport { Effect } from \"effect\";\nimport {\n getProjectRoot,\n isLocalDevelopmentTarget,\n parsePort,\n resolveLocalDevelopmentPath,\n resolvePluginRuntimeName,\n} from \"./config\";\nimport { getNetworkIdForAccount } from \"./network\";\nimport { makeDevProcess, type ProcessCallbacks, type ProcessHandle } from \"./orchestrator\";\nimport type { ProcessRegistry } from \"./process-registry\";\nimport type { BosConfig, RuntimeConfig, RuntimePluginConfig } from \"./types\";\n\nexport interface AppOrchestrator {\n packages: string[];\n env: Record<string, string>;\n description: string;\n bosConfig: BosConfig;\n runtimeConfig: RuntimeConfig;\n port?: number;\n interactive?: boolean;\n}\n\nconst STARTUP_ORDER = [\"ui-ssr\", \"ui\", \"auth\", \"api\", \"plugin\", \"host-build\", \"host\"];\nconst DEFAULT_HOST_PORT = 3000;\nconst DEFAULT_UI_PORT = 3002;\nconst DEFAULT_API_PORT = 3014;\nconst DEFAULT_AUTH_PORT = 3020;\nconst DEFAULT_PLUGIN_PORT_START = 3021;\n\nconst sortByOrder = (packages: string[]): string[] => {\n return [...packages].sort((a, b) => {\n const aIdx = a.startsWith(\"plugin:\")\n ? STARTUP_ORDER.indexOf(\"plugin\")\n : STARTUP_ORDER.indexOf(a);\n const bIdx = b.startsWith(\"plugin:\")\n ? STARTUP_ORDER.indexOf(\"plugin\")\n : STARTUP_ORDER.indexOf(b);\n if (aIdx === -1 && bIdx === -1) return 0;\n if (aIdx === -1) return 1;\n if (bIdx === -1) return -1;\n return aIdx - bIdx;\n });\n};\n\n// Note: log filtering and persistence lives at the CLI layer.\n\nexport interface DevServersHandle {\n handles: ProcessHandle[];\n shutdown: Effect.Effect<void>;\n}\n\nexport const startDevServers = (\n orchestrator: AppOrchestrator,\n callbacks: ProcessCallbacks,\n registry?: ProcessRegistry,\n) => {\n const run = Effect.gen(function* () {\n const orderedPackages = sortByOrder(orchestrator.packages);\n const handles: ProcessHandle[] = [];\n\n const startProcess = (pkg: string) => {\n const portOverride = pkg === \"host\" ? orchestrator.port : undefined;\n return makeDevProcess(\n pkg,\n orchestrator.env,\n callbacks,\n portOverride,\n orchestrator.bosConfig,\n orchestrator.runtimeConfig,\n registry,\n );\n };\n\n const startGroup = (packages: string[]) =>\n Effect.forEach(packages, startProcess, { concurrency: \"unbounded\" });\n\n const awaitReady = (pkg: string, handle: ProcessHandle) =>\n Effect.race(\n handle.waitForReady,\n Effect.sleep(\"30 seconds\").pipe(\n Effect.andThen(\n Effect.sync(() => {\n callbacks.onLog(pkg, \"Timeout waiting for ready, continuing...\", true);\n }),\n ),\n ),\n );\n\n const nonHostPackages = orderedPackages.filter((pkg) => pkg !== \"host\");\n const hostPackages = orderedPackages.filter((pkg) => pkg === \"host\");\n\n const nonHostHandles = yield* startGroup(nonHostPackages);\n handles.push(...nonHostHandles);\n\n yield* Effect.forEach(\n nonHostHandles.map((handle, index) => ({\n handle,\n pkg: nonHostPackages[index] ?? handle.name,\n })),\n ({ handle, pkg }) => awaitReady(pkg, handle),\n { concurrency: \"unbounded\" },\n );\n\n const hostHandles = yield* startGroup(hostPackages);\n handles.push(...hostHandles);\n\n yield* Effect.forEach(\n hostHandles.map((handle, index) => ({ handle, pkg: hostPackages[index] ?? handle.name })),\n ({ handle, pkg }) => awaitReady(pkg, handle),\n { concurrency: \"unbounded\" },\n );\n\n const shutdown = Effect.gen(function* () {\n const reversed = [...handles].reverse();\n for (const handle of reversed) {\n yield* handle.kill.pipe(Effect.ignore);\n }\n });\n\n return { handles, shutdown } satisfies DevServersHandle;\n });\n\n return run;\n};\n\nexport function detectLocalPackages(\n bosConfig?: BosConfig,\n runtimeConfig?: RuntimeConfig,\n): string[] {\n const packages: string[] = [];\n const configDir = getProjectRoot();\n\n const uiLocalPath =\n runtimeConfig?.ui.localPath ??\n resolveLocalDevelopmentPath(bosConfig?.app.ui.development, configDir);\n if (uiLocalPath && existsSync(join(uiLocalPath, \"package.json\"))) {\n packages.push(\"ui\");\n }\n\n const apiLocalPath =\n runtimeConfig?.api.localPath ??\n resolveLocalDevelopmentPath(bosConfig?.app.api.development, configDir);\n if (apiLocalPath && existsSync(join(apiLocalPath, \"package.json\"))) {\n packages.push(\"api\");\n }\n\n const hostLocalPath = resolveLocalDevelopmentPath(bosConfig?.app.host.development, configDir);\n if (hostLocalPath && existsSync(join(hostLocalPath, \"package.json\"))) {\n packages.push(\"host\");\n } else if (existsSync(join(configDir, \"host\", \"package.json\"))) {\n packages.push(\"host\");\n }\n\n for (const [pluginId, pluginConfig] of Object.entries(runtimeConfig?.plugins ?? {})) {\n if (pluginConfig.localPath && existsSync(join(pluginConfig.localPath, \"package.json\"))) {\n packages.push(`plugin:${pluginId}`);\n }\n }\n\n const authLocalPath =\n runtimeConfig?.auth?.localPath ??\n resolveLocalDevelopmentPath(bosConfig?.app.auth?.development, configDir);\n if (authLocalPath && existsSync(join(authLocalPath, \"package.json\"))) {\n packages.push(\"auth\");\n }\n\n return packages;\n}\n\nexport function buildRuntimeConfig(\n bosConfig: BosConfig,\n options: {\n uiSource?: \"local\" | \"remote\";\n apiSource?: \"local\" | \"remote\";\n authSource?: \"local\" | \"remote\";\n hostUrl: string;\n proxy?: string;\n env?: \"development\" | \"production\";\n plugins?: Record<string, RuntimePluginConfig>;\n },\n): RuntimeConfig {\n const configDir = getProjectRoot();\n const uiConfig = bosConfig.app.ui;\n const apiConfig = bosConfig.app.api;\n const authConfig = bosConfig.app.auth;\n const uiSource = options.uiSource ?? \"local\";\n const apiSource = options.apiSource ?? \"local\";\n const authSource = options.authSource ?? \"local\";\n const uiLocalPath = resolveLocalDevelopmentPath(uiConfig.development, configDir);\n const apiLocalPath = resolveLocalDevelopmentPath(apiConfig.development, configDir);\n const authLocalPath = authConfig\n ? resolveLocalDevelopmentPath(authConfig.development, configDir)\n : null;\n const uiLocalUrl =\n !uiLocalPath && uiConfig.development && !isLocalDevelopmentTarget(uiConfig.development)\n ? uiConfig.development\n : \"\";\n const apiLocalUrl =\n !apiLocalPath && apiConfig.development && !isLocalDevelopmentTarget(apiConfig.development)\n ? apiConfig.development\n : \"\";\n const authLocalUrl =\n authConfig &&\n !authLocalPath &&\n authConfig.development &&\n !isLocalDevelopmentTarget(authConfig.development)\n ? authConfig.development\n : \"\";\n\n return {\n env: options.env ?? \"development\",\n account: bosConfig.account,\n domain: bosConfig.domain,\n networkId: getNetworkIdForAccount(bosConfig.account),\n hostUrl: options.hostUrl,\n shared: bosConfig.shared,\n ui: uiConfig\n ? {\n name: uiConfig.name,\n url: uiSource === \"remote\" ? (uiConfig.production ?? \"\") : uiLocalUrl,\n entry:\n uiSource === \"remote\"\n ? `${uiConfig.production ?? \"\"}/mf-manifest.json`\n : uiLocalUrl\n ? `${uiLocalUrl}/mf-manifest.json`\n : \"/mf-manifest.json\",\n localPath: uiSource === \"local\" ? (uiLocalPath ?? undefined) : undefined,\n port: uiSource === \"local\" && uiLocalUrl ? parsePort(uiLocalUrl) : undefined,\n ssrUrl: uiSource === \"remote\" ? uiConfig.ssr : undefined,\n ssrIntegrity: uiSource === \"remote\" ? uiConfig.ssrIntegrity : undefined,\n integrity: uiSource === \"remote\" ? uiConfig.integrity : undefined,\n source: uiSource === \"local\" ? (uiLocalPath ? \"local\" : \"remote\") : \"remote\",\n }\n : {\n name: \"ui\",\n url: \"\",\n entry: \"/mf-manifest.json\",\n source: uiSource,\n },\n api: apiConfig\n ? {\n name: apiConfig.name,\n url: apiSource === \"remote\" ? (apiConfig.production ?? \"\") : apiLocalUrl,\n entry:\n apiSource === \"remote\"\n ? `${apiConfig.production ?? \"\"}/mf-manifest.json`\n : apiLocalUrl\n ? `${apiLocalUrl}/mf-manifest.json`\n : \"/mf-manifest.json\",\n localPath: apiSource === \"local\" ? (apiLocalPath ?? undefined) : undefined,\n port: apiSource === \"local\" && apiLocalUrl ? parsePort(apiLocalUrl) : undefined,\n source: apiSource === \"local\" ? (apiLocalPath ? \"local\" : \"remote\") : \"remote\",\n proxy: options.proxy ?? apiConfig.proxy,\n variables: apiConfig.variables,\n secrets: apiConfig.secrets,\n integrity: apiSource === \"remote\" ? apiConfig.integrity : undefined,\n }\n : {\n name: \"api\",\n url: \"\",\n entry: \"/mf-manifest.json\",\n source: apiSource,\n },\n auth: authConfig\n ? {\n name: resolvePluginRuntimeName(\n undefined,\n authSource === \"local\" ? (authLocalPath ?? undefined) : undefined,\n authConfig.name,\n ),\n url: authSource === \"remote\" ? (authConfig.production ?? \"\") : authLocalUrl,\n entry:\n authSource === \"remote\"\n ? `${authConfig.production ?? \"\"}/mf-manifest.json`\n : authLocalUrl\n ? `${authLocalUrl}/mf-manifest.json`\n : \"/mf-manifest.json\",\n localPath: authSource === \"local\" ? (authLocalPath ?? undefined) : undefined,\n port: authSource === \"local\" && authLocalUrl ? parsePort(authLocalUrl) : undefined,\n source: authSource === \"local\" ? (authLocalPath ? \"local\" : \"remote\") : \"remote\",\n proxy: authConfig.proxy,\n variables: authConfig.variables,\n secrets: authConfig.secrets,\n integrity: authSource === \"remote\" ? authConfig.integrity : undefined,\n }\n : undefined,\n plugins: options.plugins,\n };\n}\n\nfunction probeTcpOpen(port: number, timeoutMs = 250): Promise<boolean> {\n return new Promise((resolve) => {\n const socket = createConnection({ host: \"127.0.0.1\", port });\n const timer = setTimeout(() => {\n socket.destroy();\n resolve(false);\n }, timeoutMs);\n\n socket.once(\"connect\", () => {\n clearTimeout(timer);\n socket.destroy();\n resolve(true);\n });\n\n socket.once(\"error\", () => {\n clearTimeout(timer);\n resolve(false);\n });\n });\n}\n\nasync function pickAvailablePort(preferred: number, usedPorts: Set<number>): Promise<number> {\n let port = preferred;\n while (usedPorts.has(port) || (await probeTcpOpen(port))) {\n port += 1;\n }\n usedPorts.add(port);\n return port;\n}\n\nfunction withLocalRuntimeUrl<\n T extends { url: string; entry: string; port?: number; localPath?: string },\n>(entry: T, port: number): T {\n const url = `http://localhost:${port}`;\n return {\n ...entry,\n url,\n entry: `${url}/mf-manifest.json`,\n port,\n };\n}\n\nexport async function prepareDevelopmentRuntimeConfig(\n runtimeConfig: RuntimeConfig,\n options?: { hostPort?: number; ssr?: boolean },\n): Promise<RuntimeConfig> {\n const usedPorts = new Set<number>();\n const hostPort = await pickAvailablePort(\n options?.hostPort ??\n (runtimeConfig.hostUrl ? parsePort(runtimeConfig.hostUrl) : DEFAULT_HOST_PORT),\n usedPorts,\n );\n\n const next: RuntimeConfig = {\n ...runtimeConfig,\n hostUrl: `http://localhost:${hostPort}`,\n ui: { ...runtimeConfig.ui },\n api: { ...runtimeConfig.api },\n auth: runtimeConfig.auth ? { ...runtimeConfig.auth } : undefined,\n plugins: runtimeConfig.plugins ? { ...runtimeConfig.plugins } : undefined,\n };\n\n if (next.ui.source === \"local\" && next.ui.localPath) {\n const uiPort = await pickAvailablePort(next.ui.port ?? DEFAULT_UI_PORT, usedPorts);\n next.ui = withLocalRuntimeUrl(next.ui, uiPort);\n if (options?.ssr) {\n const ssrPort = await pickAvailablePort(uiPort + 1, usedPorts);\n next.ui.ssrUrl = `http://localhost:${ssrPort}`;\n } else {\n next.ui.ssrUrl = undefined;\n }\n }\n\n if (next.api.source === \"local\" && next.api.localPath) {\n const apiPort = await pickAvailablePort(next.api.port ?? DEFAULT_API_PORT, usedPorts);\n next.api = withLocalRuntimeUrl(next.api, apiPort);\n }\n\n if (next.plugins) {\n const entries = Object.entries(next.plugins).sort(([a], [b]) => a.localeCompare(b));\n let pluginBasePort = DEFAULT_PLUGIN_PORT_START;\n\n for (const [pluginId, plugin] of entries) {\n if (plugin.source !== \"local\" || !plugin.localPath) {\n continue;\n }\n\n const pluginPort = await pickAvailablePort(plugin.port ?? pluginBasePort, usedPorts);\n next.plugins[pluginId] = withLocalRuntimeUrl(plugin, pluginPort);\n pluginBasePort = pluginPort + 1;\n }\n }\n\n if (next.auth?.source === \"local\" && next.auth.localPath) {\n const authPort = await pickAvailablePort(next.auth.port ?? DEFAULT_AUTH_PORT, usedPorts);\n next.auth = withLocalRuntimeUrl(next.auth, authPort);\n }\n\n return next;\n}\n"],"mappings":";;;;;;;;;;AA2BA,MAAM,oBAAoB;AAC1B,MAAM,kBAAkB;AACxB,MAAM,mBAAmB;AACzB,MAAM,oBAAoB;AAC1B,MAAM,4BAA4B;AAkGlC,SAAgB,oBACd,WACA,eACU;CACV,MAAM,WAAqB,EAAE;CAC7B,MAAM,YAAYA,+BAAgB;CAElC,MAAM,cACJ,eAAe,GAAG,aAClBC,2CAA4B,WAAW,IAAI,GA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