threadforge 0.1.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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +152 -0
  3. package/bin/forge.js +1050 -0
  4. package/bin/host-commands.js +344 -0
  5. package/bin/platform-commands.js +570 -0
  6. package/package.json +71 -0
  7. package/shared/auth.js +475 -0
  8. package/src/core/DirectMessageBus.js +364 -0
  9. package/src/core/EndpointResolver.js +247 -0
  10. package/src/core/ForgeContext.js +2227 -0
  11. package/src/core/ForgeHost.js +122 -0
  12. package/src/core/ForgePlatform.js +145 -0
  13. package/src/core/Ingress.js +768 -0
  14. package/src/core/Interceptors.js +420 -0
  15. package/src/core/MessageBus.js +310 -0
  16. package/src/core/Prometheus.js +305 -0
  17. package/src/core/RequestContext.js +413 -0
  18. package/src/core/RoutingStrategy.js +316 -0
  19. package/src/core/Supervisor.js +1306 -0
  20. package/src/core/ThreadAllocator.js +196 -0
  21. package/src/core/WorkerChannelManager.js +879 -0
  22. package/src/core/config.js +624 -0
  23. package/src/core/host-config.js +311 -0
  24. package/src/core/network-utils.js +166 -0
  25. package/src/core/platform-config.js +308 -0
  26. package/src/decorators/ServiceProxy.js +899 -0
  27. package/src/decorators/index.js +571 -0
  28. package/src/deploy/NginxGenerator.js +865 -0
  29. package/src/deploy/PlatformManifestGenerator.js +96 -0
  30. package/src/deploy/RouteManifestGenerator.js +112 -0
  31. package/src/deploy/index.js +984 -0
  32. package/src/frontend/FrontendDevLifecycle.js +65 -0
  33. package/src/frontend/FrontendPluginOrchestrator.js +187 -0
  34. package/src/frontend/SiteResolver.js +63 -0
  35. package/src/frontend/StaticMountRegistry.js +90 -0
  36. package/src/frontend/index.js +5 -0
  37. package/src/frontend/plugins/index.js +2 -0
  38. package/src/frontend/plugins/viteFrontend.js +79 -0
  39. package/src/frontend/types.js +35 -0
  40. package/src/index.js +56 -0
  41. package/src/internals.js +31 -0
  42. package/src/plugins/PluginManager.js +537 -0
  43. package/src/plugins/ScopedPostgres.js +192 -0
  44. package/src/plugins/ScopedRedis.js +142 -0
  45. package/src/plugins/index.js +1729 -0
  46. package/src/registry/ServiceRegistry.js +796 -0
  47. package/src/scaling/ScaleAdvisor.js +442 -0
  48. package/src/services/Service.js +195 -0
  49. package/src/services/worker-bootstrap.js +676 -0
  50. package/src/templates/auth-service.js +65 -0
  51. package/src/templates/identity-service.js +75 -0
@@ -0,0 +1,308 @@
1
+ /**
2
+ * Platform Mode Configuration
3
+ *
4
+ * Transforms a user-facing `platform.apps` config into ForgeHost's
5
+ * internal `projects` format. Platform Mode is a config transformation
6
+ * layer on top of ForgeHost — it reuses resolveHostConfig() as the engine.
7
+ *
8
+ * Usage:
9
+ *
10
+ * import { definePlatform } from 'threadforge';
11
+ *
12
+ * export default definePlatform({
13
+ * platform: {
14
+ * globalAuth: true,
15
+ * apps: {
16
+ * coolapp: {
17
+ * domains: ['coolapp.io'],
18
+ * services: ['coolapp-api'],
19
+ * schema: 'coolapp',
20
+ * },
21
+ * },
22
+ * },
23
+ * plugins: [redis(), postgres()],
24
+ * identity: { entry: './services/identity.js', type: 'edge', port: 3001 },
25
+ * auth: { entry: './services/auth.js', type: 'edge', port: 3002 },
26
+ * 'coolapp-api': { entry: './apps/coolapp/api.js', type: 'edge', port: 4001 },
27
+ * });
28
+ */
29
+
30
+ import { defineServices } from "./config.js";
31
+ import { resolveHostConfig } from "./host-config.js";
32
+
33
+ const APP_ID_RE = /^[a-z][a-z0-9_-]*$/;
34
+ const BASE_PATH_RE = /^\/[^\s?#]*$/;
35
+
36
+ function isPlainObject(value) {
37
+ return Boolean(value && typeof value === "object" && !Array.isArray(value));
38
+ }
39
+
40
+ function normalizeFrontendConfig(appId, frontend) {
41
+ if (!isPlainObject(frontend)) {
42
+ throw new Error(`App "${appId}" frontend must be an object`);
43
+ }
44
+
45
+ if (typeof frontend.plugin !== "string" || frontend.plugin.trim() === "") {
46
+ throw new Error(`App "${appId}" frontend.plugin must be a non-empty string`);
47
+ }
48
+
49
+ if (typeof frontend.root !== "string" || frontend.root.trim() === "") {
50
+ throw new Error(`App "${appId}" frontend.root must be a non-empty string`);
51
+ }
52
+
53
+ if (frontend.outDir !== undefined && (typeof frontend.outDir !== "string" || frontend.outDir.trim() === "")) {
54
+ throw new Error(`App "${appId}" frontend.outDir must be a non-empty string when provided`);
55
+ }
56
+
57
+ if (frontend.basePath !== undefined) {
58
+ if (typeof frontend.basePath !== "string" || !BASE_PATH_RE.test(frontend.basePath)) {
59
+ throw new Error(
60
+ `App "${appId}" frontend.basePath must start with "/" and cannot include query strings or fragments`,
61
+ );
62
+ }
63
+ }
64
+
65
+ if (frontend.spaFallback !== undefined && typeof frontend.spaFallback !== "boolean") {
66
+ throw new Error(`App "${appId}" frontend.spaFallback must be a boolean when provided`);
67
+ }
68
+
69
+ if (frontend.env !== undefined && !isPlainObject(frontend.env)) {
70
+ throw new Error(`App "${appId}" frontend.env must be an object when provided`);
71
+ }
72
+ if (frontend.options !== undefined && !isPlainObject(frontend.options)) {
73
+ throw new Error(`App "${appId}" frontend.options must be an object when provided`);
74
+ }
75
+
76
+ return {
77
+ plugin: frontend.plugin,
78
+ root: frontend.root,
79
+ outDir: frontend.outDir ?? `.threadforge/build/${appId}`,
80
+ basePath: frontend.basePath ?? "/",
81
+ spaFallback: frontend.spaFallback ?? true,
82
+ env: frontend.env ?? {},
83
+ options: frontend.options ?? {},
84
+ };
85
+ }
86
+
87
+ /**
88
+ * Normalize an app's domain(s) to an array.
89
+ * Accepts either `domains: [...]` or `domain: '...'`.
90
+ *
91
+ * @param {{ domains?: string[], domain?: string }} app
92
+ * @returns {string[]}
93
+ */
94
+ export function resolveAppDomains(app) {
95
+ if (app.domains) return app.domains;
96
+ if (app.domain) return [app.domain];
97
+ return [];
98
+ }
99
+
100
+ /**
101
+ * Validate the platform config shape.
102
+ *
103
+ * @param {Object} config - Raw platform config
104
+ * @throws {Error} On invalid config
105
+ */
106
+ export function validatePlatformConfig(config) {
107
+ if (!config || typeof config !== "object") {
108
+ throw new Error("definePlatform() requires a config object");
109
+ }
110
+
111
+ if (!config.platform || typeof config.platform !== "object") {
112
+ throw new Error("definePlatform() requires a platform block");
113
+ }
114
+
115
+ const { apps } = config.platform;
116
+ if (!apps || typeof apps !== "object") {
117
+ throw new Error("platform.apps must be a non-empty object");
118
+ }
119
+
120
+ const appIds = Object.keys(apps);
121
+ if (appIds.length === 0) {
122
+ throw new Error("platform.apps must contain at least one app");
123
+ }
124
+
125
+ const schemas = new Set();
126
+
127
+ for (const appId of appIds) {
128
+ if (!APP_ID_RE.test(appId)) {
129
+ throw new Error(
130
+ `Invalid app ID "${appId}": must start with a lowercase letter and contain only lowercase letters, digits, hyphens, and underscores`,
131
+ );
132
+ }
133
+
134
+ const app = apps[appId];
135
+ if (!app || typeof app !== "object") {
136
+ throw new Error(`App "${appId}" must be an object`);
137
+ }
138
+
139
+ if (!app.domains && !app.domain) {
140
+ throw new Error(`App "${appId}" must have a domains array`);
141
+ }
142
+
143
+ const domains = resolveAppDomains(app);
144
+ if (domains.length === 0) {
145
+ throw new Error(`App "${appId}" must have at least one domain`);
146
+ }
147
+
148
+ if (!app.services && !app.config) {
149
+ throw new Error(`App "${appId}" must have either services or config`);
150
+ }
151
+
152
+ if (app.frontend) {
153
+ normalizeFrontendConfig(appId, app.frontend);
154
+ }
155
+
156
+ const schema = app.schema ?? appId;
157
+ if (schemas.has(schema)) {
158
+ throw new Error(`Duplicate schema "${schema}" — each app must have a unique schema`);
159
+ }
160
+ schemas.add(schema);
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Transform platform config into ForgeHost's internal projects format.
166
+ *
167
+ * Maps platform.apps → hostConfig.projects, preserving custom schemas
168
+ * and auto-injecting identity+auth when globalAuth is enabled.
169
+ *
170
+ * @param {Object} config - Validated platform config
171
+ * @returns {Object} ForgeHost-compatible config
172
+ */
173
+ export function transformToHostConfig(config) {
174
+ const { platform } = config;
175
+ const apps = platform.apps;
176
+ const globalAuth = platform.globalAuth ?? false;
177
+
178
+ // Collect all service names claimed by apps
179
+ const claimedServices = new Set();
180
+ for (const app of Object.values(apps)) {
181
+ for (const svcName of app.services ?? []) {
182
+ claimedServices.add(svcName);
183
+ }
184
+ }
185
+
186
+ // Build shared services: any service definition in config that is not
187
+ // claimed by an app and not a reserved top-level key
188
+ const shared = {};
189
+ for (const [key, value] of Object.entries(config)) {
190
+ if (key === "platform" || key === "plugins") continue;
191
+ if (claimedServices.has(key)) continue;
192
+ if (typeof value !== "object" || value === null) continue;
193
+ if (!value.entry && !value.type) continue;
194
+ shared[key] = value;
195
+ }
196
+
197
+ // Auto-inject identity + auth as shared services when globalAuth is enabled
198
+ if (globalAuth) {
199
+ if (!shared.identity) {
200
+ shared.identity = {
201
+ entry: "./shared/identity.js",
202
+ type: "edge",
203
+ port: 3001,
204
+ };
205
+ }
206
+ if (!shared.auth) {
207
+ shared.auth = {
208
+ entry: "./shared/auth.js",
209
+ type: "edge",
210
+ port: 3002,
211
+ };
212
+ }
213
+ }
214
+
215
+ // Build projects map from apps
216
+ const projects = {};
217
+ for (const [appId, app] of Object.entries(apps)) {
218
+ const domains = resolveAppDomains(app);
219
+
220
+ // Build inline service map from claimed service names
221
+ const inlineServices = {};
222
+ for (const svcName of app.services ?? []) {
223
+ const svcConfig = config[svcName];
224
+ if (svcConfig) {
225
+ inlineServices[svcName] = svcConfig;
226
+ }
227
+ }
228
+
229
+ const hasInlineServices = Object.keys(inlineServices).length > 0;
230
+ const frontend = app.frontend ? normalizeFrontendConfig(appId, app.frontend) : null;
231
+ if (frontend && app.static) {
232
+ console.warn(
233
+ `[ThreadForge] App "${appId}" defines both "frontend" and legacy "static". ` +
234
+ `Using frontend plugin config and ignoring "static".`,
235
+ );
236
+ }
237
+
238
+ projects[appId] = {
239
+ domain: domains[0],
240
+ domains,
241
+ schema: app.schema ?? appId,
242
+ static: frontend ? null : (app.static ?? null),
243
+ frontend,
244
+ services: hasInlineServices ? inlineServices : undefined,
245
+ config: app.config,
246
+ };
247
+ }
248
+
249
+ return {
250
+ domain: platform.domain ?? null,
251
+ basePort: platform.basePort ?? 3200,
252
+ metricsPort: platform.metricsPort ?? 9090,
253
+ projects,
254
+ shared,
255
+ plugins: config.plugins ?? [],
256
+ frontendPlugins: config.frontendPlugins ?? [],
257
+ };
258
+ }
259
+
260
+ /**
261
+ * Define a Platform Mode config.
262
+ *
263
+ * Validates the config and returns a marked config object.
264
+ *
265
+ * @param {Object} config - Platform config
266
+ * @returns {Object} Validated config marked with _isPlatformConfig
267
+ */
268
+ export function definePlatform(config) {
269
+ validatePlatformConfig(config);
270
+
271
+ return {
272
+ _isPlatformConfig: true,
273
+ _raw: config,
274
+ platform: config.platform,
275
+ plugins: config.plugins ?? [],
276
+ frontendPlugins: config.frontendPlugins ?? [],
277
+ };
278
+ }
279
+
280
+ /**
281
+ * Resolve a platform config fully: transform → resolveHostConfig → defineServices.
282
+ *
283
+ * @param {Object} config - Output from definePlatform() or raw platform config
284
+ * @returns {Object} Fully resolved config ready for Supervisor
285
+ */
286
+ export async function resolveAndDefinePlatform(config) {
287
+ const raw = config._isPlatformConfig ? config._raw : config;
288
+ validatePlatformConfig(raw);
289
+
290
+ const hostConfig = transformToHostConfig(raw);
291
+ const resolved = await resolveHostConfig(hostConfig);
292
+
293
+ const result = defineServices(resolved.services, {
294
+ plugins: resolved.plugins,
295
+ frontendPlugins: resolved.frontendPlugins,
296
+ sites: resolved.sites,
297
+ metricsPort: resolved.metricsPort,
298
+ });
299
+
300
+ result._isPlatformMode = true;
301
+ result._isHostMode = true;
302
+ result._hostMeta = resolved.hostMeta;
303
+ result._hostDomain = resolved.domain;
304
+ result._sites = resolved.sites;
305
+ result._platformConfig = raw.platform;
306
+
307
+ return result;
308
+ }