@omen.foundation/node-microservice-runtime 0.1.8 → 0.1.9

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/logger.cjs CHANGED
@@ -44,19 +44,28 @@ function getRequire() {
44
44
  }
45
45
  return (0, node_module_1.createRequire)(import.meta.url);
46
46
  }
47
- function isRunningInContainer(env) {
47
+ function isRunningInContainer() {
48
+ try {
49
+ const fs = require('fs');
50
+ if (fs.existsSync('/.dockerenv')) {
51
+ return true;
52
+ }
53
+ }
54
+ catch {
55
+ }
56
+ const hostname = process.env.HOSTNAME || '';
57
+ if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {
58
+ return true;
59
+ }
48
60
  if (process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
49
61
  process.env.CONTAINER === 'beamable' ||
50
62
  !!process.env.ECS_CONTAINER_METADATA_URI ||
51
63
  !!process.env.KUBERNETES_SERVICE_HOST) {
52
64
  return true;
53
65
  }
54
- const hasRoutingKey = (env === null || env === void 0 ? void 0 : env.routingKey) || process.env.NAME_PREFIX || process.env.ROUTING_KEY;
55
- if (hasRoutingKey) {
56
- return false;
57
- }
58
66
  const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
59
- if (hasBeamableEnvVars) {
67
+ const hasExplicitRoutingKey = !!(process.env.NAME_PREFIX || process.env.ROUTING_KEY);
68
+ if (hasBeamableEnvVars && !hasExplicitRoutingKey) {
60
69
  return true;
61
70
  }
62
71
  return false;
@@ -153,7 +162,7 @@ function createBeamableLogFormatter() {
153
162
  function createLogger(env, options = {}) {
154
163
  var _a, _b, _c;
155
164
  const configuredDestination = (_a = options.destinationPath) !== null && _a !== void 0 ? _a : process.env.LOG_PATH;
156
- const inContainer = isRunningInContainer(env);
165
+ const inContainer = isRunningInContainer();
157
166
  const pinoOptions = {
158
167
  name: (_b = options.name) !== null && _b !== void 0 ? _b : 'beamable-node-runtime',
159
168
  level: env.logLevel,
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAa,EAAe,KAAK,MAAM,EAAsB,MAAM,MAAM,CAAC;AAI1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAyDpD,UAAU,oBAAoB;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAwHD,wBAAgB,YAAY,CAAC,GAAG,EAAE,iBAAiB,EAAE,OAAO,GAAE,oBAAyB,GAAG,MAAM,CAkE/F"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAa,EAAe,KAAK,MAAM,EAAsB,MAAM,MAAM,CAAC;AAI1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AA0EpD,UAAU,oBAAoB;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAwHD,wBAAgB,YAAY,CAAC,GAAG,EAAE,iBAAiB,EAAE,OAAO,GAAE,oBAAyB,GAAG,MAAM,CAkE/F"}
package/dist/logger.js CHANGED
@@ -18,34 +18,50 @@ function getRequire() {
18
18
  * This is used to determine log format: pretty for local dev, JSON for containers.
19
19
  *
20
20
  * Key indicators we're in a container:
21
- * - Has CID, PID, HOST, SECRET (Beamable env vars)
22
- * - NO routing key (deployed services don't use routing keys)
23
- * - OR explicit container indicators (DOTNET_RUNNING_IN_CONTAINER, ECS, K8s)
21
+ * - Docker: .dockerenv file exists, or HOSTNAME matches container pattern
22
+ * - Explicit container indicators (DOTNET_RUNNING_IN_CONTAINER, ECS, K8s)
23
+ * - Has CID, PID, HOST, SECRET (Beamable env vars) AND no routing key from env vars
24
24
  *
25
25
  * Key indicators we're running locally:
26
- * - Has routing key (local dev always has a routing key)
27
26
  * - Running via tsx/node directly (not in Docker)
27
+ * - No container indicators present
28
28
  */
29
- function isRunningInContainer(env) {
30
- // Explicit container indicators (highest priority)
29
+ function isRunningInContainer() {
30
+ // Check for Docker container first (most reliable indicator)
31
+ // Docker creates /.dockerenv file in containers
32
+ try {
33
+ const fs = require('fs');
34
+ if (fs.existsSync('/.dockerenv')) {
35
+ return true; // Definitely in Docker
36
+ }
37
+ }
38
+ catch {
39
+ // fs might not be available, continue with other checks
40
+ }
41
+ // Check for Docker container hostname pattern
42
+ // Docker containers often have hostnames that are just hex strings (12 chars)
43
+ const hostname = process.env.HOSTNAME || '';
44
+ if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {
45
+ // This looks like a Docker container ID as hostname
46
+ return true;
47
+ }
48
+ // Explicit container indicators (highest priority after Docker checks)
31
49
  if (process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
32
50
  process.env.CONTAINER === 'beamable' ||
33
51
  !!process.env.ECS_CONTAINER_METADATA_URI ||
34
52
  !!process.env.KUBERNETES_SERVICE_HOST) {
35
53
  return true;
36
54
  }
37
- // If we have a routing key (from env object or process.env), we're definitely running locally
38
- // Local dev always has a routing key (either set explicitly or auto-generated)
39
- const hasRoutingKey = env?.routingKey || process.env.NAME_PREFIX || process.env.ROUTING_KEY;
40
- if (hasRoutingKey) {
41
- return false; // Local development
42
- }
43
- // If we have Beamable env vars but NO routing key, we're in a deployed container
55
+ // If we have Beamable env vars AND no routing key from process.env (not auto-generated)
56
+ // This indicates a deployed container (Beamable sets these but not routing key)
44
57
  const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
45
- if (hasBeamableEnvVars) {
46
- return true; // Deployed container (has env vars but no routing key)
58
+ const hasExplicitRoutingKey = !!(process.env.NAME_PREFIX || process.env.ROUTING_KEY);
59
+ if (hasBeamableEnvVars && !hasExplicitRoutingKey) {
60
+ // We have Beamable env vars but no explicit routing key from env
61
+ // This means we're in a deployed container (routing key would be auto-generated, but we're in container)
62
+ return true;
47
63
  }
48
- // Default: assume local development
64
+ // Default: assume local development (npm run dev)
49
65
  return false;
50
66
  }
51
67
  /**
@@ -164,7 +180,7 @@ function createBeamableLogFormatter() {
164
180
  }
165
181
  export function createLogger(env, options = {}) {
166
182
  const configuredDestination = options.destinationPath ?? process.env.LOG_PATH;
167
- const inContainer = isRunningInContainer(env);
183
+ const inContainer = isRunningInContainer();
168
184
  const pinoOptions = {
169
185
  name: options.name ?? 'beamable-node-runtime',
170
186
  level: env.logLevel,
@@ -1 +1 @@
1
- {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,EAAE,EAAE,WAAW,EAAmC,MAAM,MAAM,CAAC;AAC1E,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,2BAA2B,EAAE,MAAM,UAAU,CAAC;AAKvD,SAAS,UAAU;IACjB,sDAAsD;IACtD,IAAI,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,OAAO,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QAC1E,qCAAqC;QACrC,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,uDAAuD;IACvD,yEAAyE;IACzE,4EAA4E;IAC5E,OAAO,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAS,oBAAoB,CAAC,GAAuB;IACnD,mDAAmD;IACnD,IACE,OAAO,CAAC,GAAG,CAAC,2BAA2B,KAAK,MAAM;QAClD,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,UAAU;QACpC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,0BAA0B;QACxC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,EACrC,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,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gBACP,yEAAyE;gBACzE,4EAA4E;gBAC5E,OAAO,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;YAC3C,CAAC;QACH,CAAC;IACH,CAAC;IAED,mFAAmF;IACnF,MAAM,mBAAmB,GAAG,qBAAqB,KAAK,MAAM,CAAC,CAAC,CAAC,2BAA2B,EAAE,CAAC,CAAC,CAAC,qBAAqB,CAAC;IACrH,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,iBAAiB,GAAG,0BAA0B,EAAE,CAAC;QACvD,MAAM,UAAU,GAAG,WAAW,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QACtG,iBAAiB,CAAC,IAAI,CAAC,UAA8C,CAAC,CAAC;QACvE,OAAO,IAAI,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;IAC9C,CAAC;SAAM,CAAC;QACN,MAAM,UAAU,GAAG,WAAW,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QACtG,OAAO,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACvC,CAAC;AACH,CAAC","sourcesContent":["import pino, { destination, type Logger, type LoggerOptions } from 'pino';\r\nimport { Transform } from 'node:stream';\r\nimport { createRequire } from 'node:module';\r\nimport { ensureWritableTempDirectory } from './env.js';\r\nimport type { EnvironmentConfig } from './types.js';\r\n\r\n// Helper to get require function that works in both CJS and ESM\r\ndeclare const require: any;\r\nfunction getRequire(): any {\r\n // Check if we're in CJS context (require.main exists)\r\n if (typeof require !== 'undefined' && typeof require.main !== 'undefined') {\r\n // CJS context - use require directly\r\n return require;\r\n }\r\n // ESM context - use createRequire with import.meta.url\r\n // TypeScript will complain in CJS builds, but this code only runs in ESM\r\n // @ts-ignore - import.meta is ESM-only, TypeScript error in CJS is expected\r\n return createRequire(import.meta.url);\r\n}\r\n\r\n/**\r\n * Detects if we're running in a container (deployed environment).\r\n * This is used to determine log format: pretty for local dev, JSON for containers.\r\n * \r\n * Key indicators we're in a container:\r\n * - Has CID, PID, HOST, SECRET (Beamable env vars)\r\n * - NO routing key (deployed services don't use routing keys)\r\n * - OR explicit container indicators (DOTNET_RUNNING_IN_CONTAINER, ECS, K8s)\r\n * \r\n * Key indicators we're running locally:\r\n * - Has routing key (local dev always has a routing key)\r\n * - Running via tsx/node directly (not in Docker)\r\n */\r\nfunction isRunningInContainer(env?: EnvironmentConfig): boolean {\r\n // Explicit container indicators (highest priority)\r\n if (\r\n process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||\r\n process.env.CONTAINER === 'beamable' ||\r\n !!process.env.ECS_CONTAINER_METADATA_URI ||\r\n !!process.env.KUBERNETES_SERVICE_HOST\r\n ) {\r\n return true;\r\n }\r\n \r\n // If we have a routing key (from env object or process.env), we're definitely running locally\r\n // Local dev always has a routing key (either set explicitly or auto-generated)\r\n const hasRoutingKey = env?.routingKey || process.env.NAME_PREFIX || process.env.ROUTING_KEY;\r\n if (hasRoutingKey) {\r\n return false; // Local development\r\n }\r\n \r\n // If we have Beamable env vars but NO routing key, we're in a deployed container\r\n const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);\r\n if (hasBeamableEnvVars) {\r\n return true; // Deployed container (has env vars but no routing key)\r\n }\r\n \r\n // Default: assume local development\r\n return false;\r\n}\r\n\r\ninterface LoggerFactoryOptions {\r\n name?: string;\r\n destinationPath?: string;\r\n}\r\n\r\n/**\r\n * Maps Pino log levels to Beamable log levels\r\n * Pino levels: 10=trace, 20=debug, 30=info, 40=warn, 50=error, 60=fatal\r\n * Beamable levels: Debug, Info, Warning, Error, Fatal\r\n */\r\nfunction mapPinoLevelToBeamableLevel(level: number): string {\r\n switch (level) {\r\n case 10: // trace\r\n return 'Debug';\r\n case 20: // debug\r\n return 'Debug';\r\n case 30: // info\r\n return 'Info';\r\n case 40: // warn\r\n return 'Warning';\r\n case 50: // error\r\n return 'Error';\r\n case 60: // fatal\r\n return 'Fatal';\r\n default:\r\n return 'Info';\r\n }\r\n}\r\n\r\n/**\r\n * Creates a transform stream that converts Pino JSON logs to Beamable's expected format.\r\n * Beamable expects logs with __t (timestamp), __l (level), and __m (message) fields.\r\n * Pino writes JSON strings (one per line) to the stream.\r\n */\r\nfunction createBeamableLogFormatter(): Transform {\r\n return new Transform({\r\n objectMode: false, // Pino writes strings, not objects\r\n transform(chunk: Buffer, _encoding, callback) {\r\n try {\r\n const line = chunk.toString();\r\n // Skip empty lines\r\n if (!line.trim()) {\r\n callback();\r\n return;\r\n }\r\n \r\n // Parse Pino's JSON log line\r\n const pinoLog = JSON.parse(line);\r\n \r\n // Extract timestamp - Pino uses 'time' field (ISO 8601 string or milliseconds)\r\n // Convert to ISO 8601 string for Beamable\r\n let timestamp: string;\r\n if (typeof pinoLog.time === 'string') {\r\n timestamp = pinoLog.time;\r\n } else if (typeof pinoLog.time === 'number') {\r\n timestamp = new Date(pinoLog.time).toISOString();\r\n } else {\r\n timestamp = new Date().toISOString();\r\n }\r\n \r\n // Map Pino level to Beamable level\r\n const level = mapPinoLevelToBeamableLevel(pinoLog.level);\r\n \r\n // Build the message - combine msg with any additional fields\r\n // Pino's 'msg' field contains the log message\r\n const messageParts: string[] = [];\r\n if (pinoLog.msg) {\r\n messageParts.push(pinoLog.msg);\r\n }\r\n \r\n // Include error information if present\r\n if (pinoLog.err) {\r\n const err = pinoLog.err;\r\n const errMsg = err.message || err.msg || 'Error';\r\n const errStack = err.stack ? `\\n${err.stack}` : '';\r\n messageParts.push(`${errMsg}${errStack}`);\r\n }\r\n \r\n // Build the Beamable log format\r\n const beamableLog: Record<string, unknown> = {\r\n __t: timestamp,\r\n __l: level,\r\n __m: messageParts.length > 0 ? messageParts.join(' ') : 'No message',\r\n };\r\n \r\n // Include additional context fields that might be useful\r\n // These are included in the message object but not as top-level fields\r\n const contextFields: Record<string, unknown> = {};\r\n if (pinoLog.cid) contextFields.cid = pinoLog.cid;\r\n if (pinoLog.pid) contextFields.pid = pinoLog.pid;\r\n if (pinoLog.routingKey) contextFields.routingKey = pinoLog.routingKey;\r\n if (pinoLog.service) contextFields.service = pinoLog.service;\r\n if (pinoLog.component) contextFields.component = pinoLog.component;\r\n \r\n // Include any other fields that aren't standard Pino fields\r\n const standardPinoFields = ['level', 'time', 'pid', 'hostname', 'name', 'msg', 'err', 'v', 'cid', 'pid', 'routingKey', 'sdkVersionExecution', 'service', 'component'];\r\n for (const [key, value] of Object.entries(pinoLog)) {\r\n if (!standardPinoFields.includes(key) && value !== undefined && value !== null) {\r\n contextFields[key] = value;\r\n }\r\n }\r\n \r\n // If there are context fields, include them in the log\r\n if (Object.keys(contextFields).length > 0) {\r\n beamableLog.__c = contextFields;\r\n }\r\n \r\n // Output as a single-line JSON string (required for CloudWatch)\r\n const output = JSON.stringify(beamableLog) + '\\n';\r\n callback(null, Buffer.from(output, 'utf8'));\r\n } catch (error) {\r\n // If parsing fails, output a fallback log entry\r\n const fallbackLog = {\r\n __t: new Date().toISOString(),\r\n __l: 'Error',\r\n __m: `Failed to parse log entry: ${chunk.toString().substring(0, 200)}`,\r\n };\r\n callback(null, Buffer.from(JSON.stringify(fallbackLog) + '\\n', 'utf8'));\r\n }\r\n },\r\n });\r\n}\r\n\r\nexport function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptions = {}): Logger {\r\n const configuredDestination = options.destinationPath ?? process.env.LOG_PATH;\r\n const inContainer = isRunningInContainer(env);\r\n\r\n const pinoOptions: LoggerOptions = {\r\n name: options.name ?? 'beamable-node-runtime',\r\n level: env.logLevel,\r\n base: {\r\n cid: env.cid,\r\n pid: env.pid,\r\n routingKey: env.routingKey ?? null,\r\n sdkVersionExecution: env.sdkVersionExecution,\r\n },\r\n redact: {\r\n paths: ['secret', 'refreshToken'],\r\n censor: '***',\r\n },\r\n // Use timestamp in milliseconds (Pino default) for accurate conversion\r\n timestamp: pino.stdTimeFunctions.isoTime,\r\n };\r\n\r\n // For deployed services, always log to stdout so container orchestrator can collect logs\r\n // For local development, log to stdout unless a specific file path is provided\r\n if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {\r\n if (inContainer) {\r\n // In containers: Use Beamable JSON format for CloudWatch collection\r\n const beamableFormatter = createBeamableLogFormatter();\r\n beamableFormatter.pipe(process.stdout);\r\n return pino(pinoOptions, beamableFormatter);\r\n } else {\r\n // Local development: Use Pino's pretty printing for human-readable logs\r\n // Try to use pino-pretty if available (optional dependency)\r\n // If not available, fall back to default Pino JSON output\r\n try {\r\n // Check if pino-pretty is available\r\n // Use getRequire() which handles both CJS and ESM contexts\r\n const requireFn = getRequire();\r\n const pinoPretty = requireFn('pino-pretty');\r\n // Create a pretty stream with formatting options\r\n const prettyStream = pinoPretty({\r\n colorize: true,\r\n translateTime: 'HH:MM:ss.l',\r\n ignore: 'pid,hostname',\r\n singleLine: false,\r\n });\r\n // Use pino with the pretty stream\r\n return pino(pinoOptions, prettyStream);\r\n } catch {\r\n // pino-pretty not available, use default Pino output (JSON but readable)\r\n // This is expected if pino-pretty isn't installed, so we silently fall back\r\n return pino(pinoOptions, process.stdout);\r\n }\r\n }\r\n }\r\n\r\n // For file logging: Use Beamable format in containers, default Pino format locally\r\n const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;\r\n if (inContainer) {\r\n const beamableFormatter = createBeamableLogFormatter();\r\n const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });\r\n beamableFormatter.pipe(fileStream as unknown as NodeJS.WritableStream);\r\n return pino(pinoOptions, beamableFormatter);\r\n } else {\r\n const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });\r\n return pino(pinoOptions, fileStream);\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"logger.js","sourceRo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,YAAY,CAAC,CAAC;YACzC,CAAC;YAAC,MAAM,CAAC;gBACP,yEAAyE;gBACzE,4EAA4E;gBAC5E,OAAO,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;YAC3C,CAAC;QACH,CAAC;IACH,CAAC;IAED,mFAAmF;IACnF,MAAM,mBAAmB,GAAG,qBAAqB,KAAK,MAAM,CAAC,CAAC,CAAC,2BAA2B,EAAE,CAAC,CAAC,CAAC,qBAAqB,CAAC;IACrH,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,iBAAiB,GAAG,0BAA0B,EAAE,CAAC;QACvD,MAAM,UAAU,GAAG,WAAW,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QACtG,iBAAiB,CAAC,IAAI,CAAC,UAA8C,CAAC,CAAC;QACvE,OAAO,IAAI,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;IAC9C,CAAC;SAAM,CAAC;QACN,MAAM,UAAU,GAAG,WAAW,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QACtG,OAAO,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACvC,CAAC;AACH,CAAC","sourcesContent":["import pino, { destination, type Logger, type LoggerOptions } from 'pino';\r\nimport { Transform } from 'node:stream';\r\nimport { createRequire } from 'node:module';\r\nimport { ensureWritableTempDirectory } from './env.js';\r\nimport type { EnvironmentConfig } from './types.js';\r\n\r\n// Helper to get require function that works in both CJS and ESM\r\ndeclare const require: any;\r\nfunction getRequire(): any {\r\n // Check if we're in CJS context (require.main exists)\r\n if (typeof require !== 'undefined' && typeof require.main !== 'undefined') {\r\n // CJS context - use require directly\r\n return require;\r\n }\r\n // ESM context - use createRequire with import.meta.url\r\n // TypeScript will complain in CJS builds, but this code only runs in ESM\r\n // @ts-ignore - import.meta is ESM-only, TypeScript error in CJS is expected\r\n return createRequire(import.meta.url);\r\n}\r\n\r\n/**\r\n * Detects if we're running in a container (deployed environment).\r\n * This is used to determine log format: pretty for local dev, JSON for containers.\r\n * \r\n * Key indicators we're in a container:\r\n * - Docker: .dockerenv file exists, or HOSTNAME matches container pattern\r\n * - Explicit container indicators (DOTNET_RUNNING_IN_CONTAINER, ECS, K8s)\r\n * - Has CID, PID, HOST, SECRET (Beamable env vars) AND no routing key from env vars\r\n * \r\n * Key indicators we're running locally:\r\n * - Running via tsx/node directly (not in Docker)\r\n * - No container indicators present\r\n */\r\nfunction isRunningInContainer(): boolean {\r\n // Check for Docker container first (most reliable indicator)\r\n // Docker creates /.dockerenv file in containers\r\n try {\r\n const fs = require('fs');\r\n if (fs.existsSync('/.dockerenv')) {\r\n return true; // Definitely in Docker\r\n }\r\n } catch {\r\n // fs might not be available, continue with other checks\r\n }\r\n \r\n // Check for Docker container hostname pattern\r\n // Docker containers often have hostnames that are just hex strings (12 chars)\r\n const hostname = process.env.HOSTNAME || '';\r\n if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {\r\n // This looks like a Docker container ID as hostname\r\n return true;\r\n }\r\n \r\n // Explicit container indicators (highest priority after Docker checks)\r\n if (\r\n process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||\r\n process.env.CONTAINER === 'beamable' ||\r\n !!process.env.ECS_CONTAINER_METADATA_URI ||\r\n !!process.env.KUBERNETES_SERVICE_HOST\r\n ) {\r\n return true;\r\n }\r\n \r\n // If we have Beamable env vars AND no routing key from process.env (not auto-generated)\r\n // This indicates a deployed container (Beamable sets these but not routing key)\r\n const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);\r\n const hasExplicitRoutingKey = !!(process.env.NAME_PREFIX || process.env.ROUTING_KEY);\r\n \r\n if (hasBeamableEnvVars && !hasExplicitRoutingKey) {\r\n // We have Beamable env vars but no explicit routing key from env\r\n // This means we're in a deployed container (routing key would be auto-generated, but we're in container)\r\n return true;\r\n }\r\n \r\n // Default: assume local development (npm run dev)\r\n return false;\r\n}\r\n\r\ninterface LoggerFactoryOptions {\r\n name?: string;\r\n destinationPath?: string;\r\n}\r\n\r\n/**\r\n * Maps Pino log levels to Beamable log levels\r\n * Pino levels: 10=trace, 20=debug, 30=info, 40=warn, 50=error, 60=fatal\r\n * Beamable levels: Debug, Info, Warning, Error, Fatal\r\n */\r\nfunction mapPinoLevelToBeamableLevel(level: number): string {\r\n switch (level) {\r\n case 10: // trace\r\n return 'Debug';\r\n case 20: // debug\r\n return 'Debug';\r\n case 30: // info\r\n return 'Info';\r\n case 40: // warn\r\n return 'Warning';\r\n case 50: // error\r\n return 'Error';\r\n case 60: // fatal\r\n return 'Fatal';\r\n default:\r\n return 'Info';\r\n }\r\n}\r\n\r\n/**\r\n * Creates a transform stream that converts Pino JSON logs to Beamable's expected format.\r\n * Beamable expects logs with __t (timestamp), __l (level), and __m (message) fields.\r\n * Pino writes JSON strings (one per line) to the stream.\r\n */\r\nfunction createBeamableLogFormatter(): Transform {\r\n return new Transform({\r\n objectMode: false, // Pino writes strings, not objects\r\n transform(chunk: Buffer, _encoding, callback) {\r\n try {\r\n const line = chunk.toString();\r\n // Skip empty lines\r\n if (!line.trim()) {\r\n callback();\r\n return;\r\n }\r\n \r\n // Parse Pino's JSON log line\r\n const pinoLog = JSON.parse(line);\r\n \r\n // Extract timestamp - Pino uses 'time' field (ISO 8601 string or milliseconds)\r\n // Convert to ISO 8601 string for Beamable\r\n let timestamp: string;\r\n if (typeof pinoLog.time === 'string') {\r\n timestamp = pinoLog.time;\r\n } else if (typeof pinoLog.time === 'number') {\r\n timestamp = new Date(pinoLog.time).toISOString();\r\n } else {\r\n timestamp = new Date().toISOString();\r\n }\r\n \r\n // Map Pino level to Beamable level\r\n const level = mapPinoLevelToBeamableLevel(pinoLog.level);\r\n \r\n // Build the message - combine msg with any additional fields\r\n // Pino's 'msg' field contains the log message\r\n const messageParts: string[] = [];\r\n if (pinoLog.msg) {\r\n messageParts.push(pinoLog.msg);\r\n }\r\n \r\n // Include error information if present\r\n if (pinoLog.err) {\r\n const err = pinoLog.err;\r\n const errMsg = err.message || err.msg || 'Error';\r\n const errStack = err.stack ? `\\n${err.stack}` : '';\r\n messageParts.push(`${errMsg}${errStack}`);\r\n }\r\n \r\n // Build the Beamable log format\r\n const beamableLog: Record<string, unknown> = {\r\n __t: timestamp,\r\n __l: level,\r\n __m: messageParts.length > 0 ? messageParts.join(' ') : 'No message',\r\n };\r\n \r\n // Include additional context fields that might be useful\r\n // These are included in the message object but not as top-level fields\r\n const contextFields: Record<string, unknown> = {};\r\n if (pinoLog.cid) contextFields.cid = pinoLog.cid;\r\n if (pinoLog.pid) contextFields.pid = pinoLog.pid;\r\n if (pinoLog.routingKey) contextFields.routingKey = pinoLog.routingKey;\r\n if (pinoLog.service) contextFields.service = pinoLog.service;\r\n if (pinoLog.component) contextFields.component = pinoLog.component;\r\n \r\n // Include any other fields that aren't standard Pino fields\r\n const standardPinoFields = ['level', 'time', 'pid', 'hostname', 'name', 'msg', 'err', 'v', 'cid', 'pid', 'routingKey', 'sdkVersionExecution', 'service', 'component'];\r\n for (const [key, value] of Object.entries(pinoLog)) {\r\n if (!standardPinoFields.includes(key) && value !== undefined && value !== null) {\r\n contextFields[key] = value;\r\n }\r\n }\r\n \r\n // If there are context fields, include them in the log\r\n if (Object.keys(contextFields).length > 0) {\r\n beamableLog.__c = contextFields;\r\n }\r\n \r\n // Output as a single-line JSON string (required for CloudWatch)\r\n const output = JSON.stringify(beamableLog) + '\\n';\r\n callback(null, Buffer.from(output, 'utf8'));\r\n } catch (error) {\r\n // If parsing fails, output a fallback log entry\r\n const fallbackLog = {\r\n __t: new Date().toISOString(),\r\n __l: 'Error',\r\n __m: `Failed to parse log entry: ${chunk.toString().substring(0, 200)}`,\r\n };\r\n callback(null, Buffer.from(JSON.stringify(fallbackLog) + '\\n', 'utf8'));\r\n }\r\n },\r\n });\r\n}\r\n\r\nexport function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptions = {}): Logger {\r\n const configuredDestination = options.destinationPath ?? process.env.LOG_PATH;\r\n const inContainer = isRunningInContainer();\r\n\r\n const pinoOptions: LoggerOptions = {\r\n name: options.name ?? 'beamable-node-runtime',\r\n level: env.logLevel,\r\n base: {\r\n cid: env.cid,\r\n pid: env.pid,\r\n routingKey: env.routingKey ?? null,\r\n sdkVersionExecution: env.sdkVersionExecution,\r\n },\r\n redact: {\r\n paths: ['secret', 'refreshToken'],\r\n censor: '***',\r\n },\r\n // Use timestamp in milliseconds (Pino default) for accurate conversion\r\n timestamp: pino.stdTimeFunctions.isoTime,\r\n };\r\n\r\n // For deployed services, always log to stdout so container orchestrator can collect logs\r\n // For local development, log to stdout unless a specific file path is provided\r\n if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {\r\n if (inContainer) {\r\n // In containers: Use Beamable JSON format for CloudWatch collection\r\n const beamableFormatter = createBeamableLogFormatter();\r\n beamableFormatter.pipe(process.stdout);\r\n return pino(pinoOptions, beamableFormatter);\r\n } else {\r\n // Local development: Use Pino's pretty printing for human-readable logs\r\n // Try to use pino-pretty if available (optional dependency)\r\n // If not available, fall back to default Pino JSON output\r\n try {\r\n // Check if pino-pretty is available\r\n // Use getRequire() which handles both CJS and ESM contexts\r\n const requireFn = getRequire();\r\n const pinoPretty = requireFn('pino-pretty');\r\n // Create a pretty stream with formatting options\r\n const prettyStream = pinoPretty({\r\n colorize: true,\r\n translateTime: 'HH:MM:ss.l',\r\n ignore: 'pid,hostname',\r\n singleLine: false,\r\n });\r\n // Use pino with the pretty stream\r\n return pino(pinoOptions, prettyStream);\r\n } catch {\r\n // pino-pretty not available, use default Pino output (JSON but readable)\r\n // This is expected if pino-pretty isn't installed, so we silently fall back\r\n return pino(pinoOptions, process.stdout);\r\n }\r\n }\r\n }\r\n\r\n // For file logging: Use Beamable format in containers, default Pino format locally\r\n const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;\r\n if (inContainer) {\r\n const beamableFormatter = createBeamableLogFormatter();\r\n const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });\r\n beamableFormatter.pipe(fileStream as unknown as NodeJS.WritableStream);\r\n return pino(pinoOptions, beamableFormatter);\r\n } else {\r\n const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });\r\n return pino(pinoOptions, fileStream);\r\n }\r\n}\r\n"]}
package/dist/runtime.cjs CHANGED
@@ -54,7 +54,7 @@ class MicroserviceRuntime {
54
54
  this.requester = new requester_js_1.GatewayRequester(this.webSocket, this.logger);
55
55
  this.authManager = new auth_js_1.AuthManager(this.env, this.requester);
56
56
  this.requester.on('event', (envelope) => this.handleEvent(envelope));
57
- if (!isRunningInContainer(this.env) && this.services.length > 0) {
57
+ if (!isRunningInContainer() && this.services.length > 0) {
58
58
  this.discovery = new discovery_js_1.DiscoveryBroadcaster({
59
59
  env: this.env,
60
60
  serviceName: this.services[0].definition.name,
@@ -163,7 +163,7 @@ class MicroserviceRuntime {
163
163
  throw new Error('Unexpected missing service definition during registration.');
164
164
  }
165
165
  const options = (_b = (0, decorators_js_1.getServiceOptions)(primary.ctor)) !== null && _b !== void 0 ? _b : {};
166
- const isDeployed = isRunningInContainer(this.env);
166
+ const isDeployed = isRunningInContainer();
167
167
  const request = {
168
168
  type: 'basic',
169
169
  name: primary.qualifiedName,
@@ -531,7 +531,7 @@ class MicroserviceRuntime {
531
531
  if (!service) {
532
532
  return;
533
533
  }
534
- if (isRunningInContainer(this.env)) {
534
+ if (isRunningInContainer()) {
535
535
  return;
536
536
  }
537
537
  const docsUrl = buildDocsPortalUrl(this.env, service.definition);
@@ -608,19 +608,28 @@ function debugLog(...args) {
608
608
  console.error(...args);
609
609
  }
610
610
  }
611
- function isRunningInContainer(env) {
611
+ function isRunningInContainer() {
612
+ try {
613
+ const fs = require('fs');
614
+ if (fs.existsSync('/.dockerenv')) {
615
+ return true;
616
+ }
617
+ }
618
+ catch {
619
+ }
620
+ const hostname = process.env.HOSTNAME || '';
621
+ if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {
622
+ return true;
623
+ }
612
624
  if (process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
613
625
  process.env.CONTAINER === 'beamable' ||
614
626
  !!process.env.ECS_CONTAINER_METADATA_URI ||
615
627
  !!process.env.KUBERNETES_SERVICE_HOST) {
616
628
  return true;
617
629
  }
618
- const hasRoutingKey = (env === null || env === void 0 ? void 0 : env.routingKey) || process.env.NAME_PREFIX || process.env.ROUTING_KEY;
619
- if (hasRoutingKey) {
620
- return false;
621
- }
622
630
  const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
623
- if (hasBeamableEnvVars) {
631
+ const hasExplicitRoutingKey = !!(process.env.NAME_PREFIX || process.env.ROUTING_KEY);
632
+ if (hasBeamableEnvVars && !hasExplicitRoutingKey) {
624
633
  return true;
625
634
  }
626
635
  return false;
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACV,iBAAiB,EAOlB,MAAM,YAAY,CAAC;AA2BpB,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAoB;IACxC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoB;IAC7C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAoB;IAC9C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAmB;IAC7C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;IAC1C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAuB;IAClD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;IAC/C,OAA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AO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAuB;IAClD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;IAC/C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAyB;IACxD,OAAO,CAAC,iBAAiB,CAAC,CAAS;IACnC,OAAO,CAAC,OAAO,CAAkB;gBAErB,GAAG,CAAC,EAAE,iBAAiB;IA8C7B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA6DtB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;YASjB,sBAAsB;YA2CtB,qBAAqB;YAarB,eAAe;YA+Gf,WAAW;IA8BzB,OAAO,CAAC,gBAAgB;IAkFxB,OAAO,CAAC,kBAAkB;YAUZ,QAAQ;IAsDtB,OAAO,CAAC,wBAAwB;YAmBlB,mBAAmB;YAiBnB,sBAAsB;YAStB,wBAAwB;YAoBxB,mBAAmB;IA0GjC,OAAO,CAAC,kBAAkB;IAgB1B,OAAO,CAAC,gBAAgB;YA+BV,6BAA6B;IA6B3C,OAAO,CAAC,kBAAkB;CAK3B;AAuHD,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAoDrD;AAED,UAAU,sBAAsB;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,4CAA4C,CAC1D,SAAS,GAAE,OAAO,CAAC,iBAAiB,CAAM,EAC1C,OAAO,GAAE,sBAA2B,GACnC,OAAO,CA4BT"}
package/dist/runtime.js CHANGED
@@ -58,7 +58,7 @@ export class MicroserviceRuntime {
58
58
  this.requester = new GatewayRequester(this.webSocket, this.logger);
59
59
  this.authManager = new AuthManager(this.env, this.requester);
60
60
  this.requester.on('event', (envelope) => this.handleEvent(envelope));
61
- if (!isRunningInContainer(this.env) && this.services.length > 0) {
61
+ if (!isRunningInContainer() && this.services.length > 0) {
62
62
  this.discovery = new DiscoveryBroadcaster({
63
63
  env: this.env,
64
64
  serviceName: this.services[0].definition.name,
@@ -202,7 +202,7 @@ export class MicroserviceRuntime {
202
202
  // The gateway's ServiceIdentity.fullNameNoType lowercases the service name when creating bindings,
203
203
  // but the registration request should use the original case (as C# does)
204
204
  // This ensures the service name in the registration matches what the portal expects
205
- const isDeployed = isRunningInContainer(this.env);
205
+ const isDeployed = isRunningInContainer();
206
206
  // For deployed services, routingKey should be null/undefined (None in Scala)
207
207
  // For local dev, routingKey should be the actual routing key string
208
208
  // The backend expects Option[String] = None for deployed services
@@ -609,7 +609,7 @@ export class MicroserviceRuntime {
609
609
  }
610
610
  // Only print helpful URLs in local development (not in containers)
611
611
  // In containers, we want only JSON logs for proper log collection
612
- if (isRunningInContainer(this.env)) {
612
+ if (isRunningInContainer()) {
613
613
  return;
614
614
  }
615
615
  const docsUrl = buildDocsPortalUrl(this.env, service.definition);
@@ -689,26 +689,42 @@ function debugLog(...args) {
689
689
  console.error(...args);
690
690
  }
691
691
  }
692
- function isRunningInContainer(env) {
693
- // Explicit container indicators (highest priority)
692
+ function isRunningInContainer() {
693
+ // Check for Docker container first (most reliable indicator)
694
+ // Docker creates /.dockerenv file in containers
695
+ try {
696
+ const fs = require('fs');
697
+ if (fs.existsSync('/.dockerenv')) {
698
+ return true; // Definitely in Docker
699
+ }
700
+ }
701
+ catch {
702
+ // fs might not be available, continue with other checks
703
+ }
704
+ // Check for Docker container hostname pattern
705
+ // Docker containers often have hostnames that are just hex strings (12 chars)
706
+ const hostname = process.env.HOSTNAME || '';
707
+ if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {
708
+ // This looks like a Docker container ID as hostname
709
+ return true;
710
+ }
711
+ // Explicit container indicators (highest priority after Docker checks)
694
712
  if (process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
695
713
  process.env.CONTAINER === 'beamable' ||
696
714
  !!process.env.ECS_CONTAINER_METADATA_URI ||
697
715
  !!process.env.KUBERNETES_SERVICE_HOST) {
698
716
  return true;
699
717
  }
700
- // If we have a routing key (from env object or process.env), we're definitely running locally
701
- // Local dev always has a routing key (either set explicitly or auto-generated)
702
- const hasRoutingKey = env?.routingKey || process.env.NAME_PREFIX || process.env.ROUTING_KEY;
703
- if (hasRoutingKey) {
704
- return false; // Local development
705
- }
706
- // If we have Beamable env vars but NO routing key, we're in a deployed container
718
+ // If we have Beamable env vars AND no routing key from process.env (not auto-generated)
719
+ // This indicates a deployed container (Beamable sets these but not routing key)
707
720
  const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
708
- if (hasBeamableEnvVars) {
709
- return true; // Deployed container (has env vars but no routing key)
721
+ const hasExplicitRoutingKey = !!(process.env.NAME_PREFIX || process.env.ROUTING_KEY);
722
+ if (hasBeamableEnvVars && !hasExplicitRoutingKey) {
723
+ // We have Beamable env vars but no explicit routing key from env
724
+ // This means we're in a deployed container (routing key would be auto-generated, but we're in container)
725
+ return true;
710
726
  }
711
- // Default: assume local development
727
+ // Default: assume local development (npm run dev)
712
728
  return false;
713
729
  }
714
730
  function normalizeScopes(scopes) {
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,4BAA4B,EAAE,6BAA6B,EAAE,MAAM,iBAAiB,CAAC;AACzI,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,E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AM,CAAC,cAAc,EAAE,eAAe,CAAC,CAAC;IACxC,gEAAgE;IAChE,+EAA+E;IAC/E,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC;QAClD,MAAM,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;IACtC,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QAC/C,MAAM,IAAI,KAAK,CAAC,4DAA4D,CAAC,CAAC;IAChF,CAAC;IAED,MAAM,IAAI,GAAG,qBAAqB,EAAE,CAAC;IAErC,OAAO;QACL,GAAG,IAAI;QACP,GAAG,MAAM;QACT,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,IAAI,CAAC,UAAU;KACjD,CAAC;AACJ,CAAC","sourcesContent":["import { randomUUID } from 'node:crypto';\r\nimport { BeamableWebSocket } from './websocket.js';\r\nimport { GatewayRequester } from './requester.js';\r\nimport { AuthManager } from './auth.js';\r\nimport { createLogger } from './logger.js';\r\nimport { loadEnvironmentConfig } from './env.js';\r\nimport { listRegisteredServices, getServiceOptions, getConfigureServicesHandlers, getInitializeServicesHandlers } from './decorators.js';\r\nimport { generateOpenApiDocument } from './docs.js';\r\nimport { DiscoveryBroadcaster } from './discovery.js';\r\nimport { BeamableRuntimeError, MissingScopesError, UnauthorizedUserError, UnknownRouteError } from './errors.js';\r\nimport type {\r\n EnvironmentConfig,\r\n RequestContext,\r\n ServiceDefinition,\r\n ServiceCallableMetadata,\r\n GatewayResponse,\r\n WebsocketEventEnvelope,\r\n ServiceAccess,\r\n} from './types.js';\r\nimport type { Logger } from 'pino';\r\nimport { BeamableServiceManager } from './services.js';\r\nimport {\r\n DependencyBuilder,\r\n LOGGER_TOKEN,\r\n ENVIRONMENT_CONFIG_TOKEN,\r\n REQUEST_CONTEXT_TOKEN,\r\n BEAMABLE_SERVICES_TOKEN,\r\n ServiceProvider,\r\n MutableDependencyScope,\r\n} from './dependency.js';\r\nimport { hostToHttpUrl, hostToPortalUrl } from './utils/urls.js';\r\nimport { FederationRegistry, getFederationComponents, getFederatedInventoryMetadata } from './federation.js';\r\nimport type { FederatedRequestContext } from './federation.js';\r\nimport { createServer, type Server } from 'node:http';\r\n\r\ninterface ServiceInstance {\r\n definition: ServiceDefinition;\r\n instance: Record<string, unknown>;\r\n configureHandlers: ReturnType<typeof getConfigureServicesHandlers>;\r\n initializeHandlers: ReturnType<typeof getInitializeServicesHandlers>;\r\n provider?: ServiceProvider;\r\n logger: Logger;\r\n federationRegistry: FederationRegistry;\r\n}\r\n\r\nexport class MicroserviceRuntime {\r\n private readonly env: EnvironmentConfig;\r\n private readonly logger: Logger;\r\n private readonly services: ServiceInstance[];\r\n private readonly webSocket: BeamableWebSocket;\r\n private readonly requester: GatewayRequester;\r\n private readonly authManager: AuthManager;\r\n private readonly discovery?: DiscoveryBroadcaster;\r\n private readonly microServiceId = randomUUID();\r\n private readonly serviceManager: BeamableServiceManager;\r\n private healthCheckServer?: Server;\r\n private isReady: boolean = false;\r\n\r\n constructor(env?: EnvironmentConfig) {\r\n this.env = env ?? loadEnvironmentConfig();\r\n this.logger = createLogger(this.env, { name: 'beamable-node-microservice' });\r\n this.serviceManager = new BeamableServiceManager(this.env, this.logger);\r\n\r\n const registered = listRegisteredServices();\r\n if (registered.length === 0) {\r\n throw new Error('No microservices registered. Use the @Microservice decorator to register at least one class.');\r\n }\r\n this.services = registered.map((definition) => {\r\n const instance = new definition.ctor() as Record<string, unknown>;\r\n const configureHandlers = getConfigureServicesHandlers(definition.ctor);\r\n const initializeHandlers = getInitializeServicesHandlers(definition.ctor);\r\n const logger = this.logger.child({ service: definition.name });\r\n const federationRegistry = new FederationRegistry(logger);\r\n const decoratedFederations = getFederationComponents(definition.ctor);\r\n for (const component of decoratedFederations) {\r\n federationRegistry.register(component);\r\n }\r\n const inventoryMetadata = getFederatedInventoryMetadata(definition.ctor);\r\n if (inventoryMetadata.length > 0) {\r\n federationRegistry.registerInventoryHandlers(instance, inventoryMetadata);\r\n }\r\n this.serviceManager.registerFederationRegistry(definition.name, federationRegistry);\r\n return { definition, instance, configureHandlers, initializeHandlers, logger, federationRegistry };\r\n });\r\n\r\n const socketUrl = this.env.host.endsWith('/socket') ? this.env.host : `${this.env.host}/socket`;\r\n this.webSocket = new BeamableWebSocket({ url: socketUrl, logger: this.logger });\r\n this.webSocket.on('message', (payload) => {\r\n this.logger.debug({ payload }, 'Runtime observed websocket frame.');\r\n });\r\n this.requester = new GatewayRequester(this.webSocket, this.logger);\r\n this.authManager = new AuthManager(this.env, this.requester);\r\n this.requester.on('event', (envelope) => this.handleEvent(envelope));\r\n\r\n if (!isRunningInContainer(this.env) && this.services.length > 0) {\r\n this.discovery = new DiscoveryBroadcaster({\r\n env: this.env,\r\n serviceName: this.services[0].definition.name,\r\n routingKey: this.env.routingKey,\r\n logger: this.logger.child({ component: 'DiscoveryBroadcaster' }),\r\n });\r\n }\r\n }\r\n\r\n async start(): Promise<void> {\r\n // Immediate console output for container logs (before logger is ready)\r\n debugLog('[BEAMABLE-NODE] MicroserviceRuntime.start() called');\r\n debugLog(`[BEAMABLE-NODE] Service count: ${this.services.length}`);\r\n \r\n this.printHelpfulUrls(this.services[0]);\r\n this.logger.info('Starting Beamable Node microservice runtime.');\r\n \r\n // Start health check server FIRST - this is critical for container health checks\r\n // Even if startup fails, the health check server must be running so we can debug\r\n debugLog('[BEAMABLE-NODE] Starting health check server...');\r\n await this.startHealthCheckServer();\r\n debugLog('[BEAMABLE-NODE] Health check server started');\r\n \r\n try {\r\n this.logger.info('Connecting to Beamable gateway...');\r\n await this.webSocket.connect();\r\n await new Promise((resolve) => setTimeout(resolve, 250));\r\n \r\n this.logger.info('Authenticating with Beamable...');\r\n await this.authManager.authenticate();\r\n \r\n this.logger.info('Initializing Beamable SDK services...');\r\n await this.serviceManager.initialize();\r\n \r\n this.logger.info('Initializing dependency providers...');\r\n await this.initializeDependencyProviders();\r\n \r\n this.logger.info('Registering service with gateway...');\r\n await this.registerService();\r\n \r\n this.logger.info('Starting discovery broadcaster...');\r\n await this.discovery?.start();\r\n \r\n // Mark as ready only after service is fully registered and discovery is started\r\n this.isReady = true;\r\n this.logger.info('Beamable microservice runtime is ready to accept traffic.');\r\n } catch (error) {\r\n // Log the error with full context but don't crash - health check server is running\r\n // This allows the container to stay alive so we can debug the issue\r\n // Debug output for local development only (in containers, logger handles this)\r\n debugLog('[BEAMABLE-NODE] FATAL ERROR during startup:');\r\n debugLog(`[BEAMABLE-NODE] Error message: ${error instanceof Error ? error.message : String(error)}`);\r\n debugLog(`[BEAMABLE-NODE] Error stack: ${error instanceof Error ? error.stack : 'No stack trace'}`);\r\n debugLog(`[BEAMABLE-NODE] isReady: ${this.isReady}`);\r\n \r\n this.logger.error(\r\n { \r\n err: error,\r\n errorMessage: error instanceof Error ? error.message : String(error),\r\n errorStack: error instanceof Error ? error.stack : undefined,\r\n isReady: this.isReady,\r\n },\r\n 'Failed to fully initialize microservice runtime. Health check will continue to return 503 until initialization completes.'\r\n );\r\n // DON'T re-throw - keep process alive so health check can show 503\r\n // This allows us to see the error in logs\r\n this.isReady = false;\r\n }\r\n }\r\n\r\n async shutdown(): Promise<void> {\r\n this.logger.info('Shutting down microservice runtime.');\r\n this.isReady = false; // Mark as not ready during shutdown\r\n this.discovery?.stop();\r\n await this.stopHealthCheckServer();\r\n this.requester.dispose();\r\n await this.webSocket.close();\r\n }\r\n\r\n private async startHealthCheckServer(): Promise<void> {\r\n // For deployed services, always start health check server (required for container health checks)\r\n // For local development, only start if healthPort is explicitly set\r\n // IMPORTANT: Always default to 6565 if HEALTH_PORT env var is not set, as this is the standard port\r\n const healthPort = this.env.healthPort || 6565;\r\n \r\n // Always start health check server if we have a valid port\r\n // The container orchestrator expects this endpoint to be available\r\n if (!healthPort || healthPort === 0) {\r\n // Health check server not needed (local development without explicit port)\r\n this.logger.debug('Health check server skipped (no healthPort set)');\r\n return;\r\n }\r\n\r\n this.healthCheckServer = createServer((req, res) => {\r\n if (req.url === '/health' && req.method === 'GET') {\r\n // Only return success if service is fully ready (registered and accepting traffic)\r\n if (this.isReady) {\r\n res.writeHead(200, { 'Content-Type': 'text/plain' });\r\n res.end('responsive');\r\n } else {\r\n // Service is still starting up\r\n res.writeHead(503, { 'Content-Type': 'text/plain' });\r\n res.end('Service Unavailable');\r\n }\r\n } else {\r\n res.writeHead(404, { 'Content-Type': 'text/plain' });\r\n res.end('Not Found');\r\n }\r\n });\r\n\r\n return new Promise((resolve, reject) => {\r\n this.healthCheckServer!.listen(healthPort, '0.0.0.0', () => {\r\n this.logger.info({ port: healthPort }, 'Health check server started on port');\r\n resolve();\r\n });\r\n this.healthCheckServer!.on('error', (err) => {\r\n this.logger.error({ err, port: healthPort }, 'Failed to start health check server');\r\n reject(err);\r\n });\r\n });\r\n }\r\n\r\n private async stopHealthCheckServer(): Promise<void> {\r\n if (!this.healthCheckServer) {\r\n return;\r\n }\r\n\r\n return new Promise((resolve) => {\r\n this.healthCheckServer!.close(() => {\r\n this.logger.info('Health check server stopped');\r\n resolve();\r\n });\r\n });\r\n }\r\n\r\n private async registerService(): Promise<void> {\r\n const primary = this.services[0]?.definition;\r\n if (!primary) {\r\n throw new Error('Unexpected missing service definition during registration.');\r\n }\r\n const options = getServiceOptions(primary.ctor) ?? {};\r\n // Match C# exactly: use qualifiedName (preserves case) for name field.\r\n // The gateway lowercases service names when creating bindings, but uses the original case\r\n // from the registration request when constructing routing key lookups.\r\n // This ensures the routing key format matches what the gateway expects.\r\n // The gateway's binding lookup behavior:\r\n // - Gateway error shows: \"No binding found for service ...micro_examplenodeservice.basic\"\r\n // - This means the gateway lowercases the service name for binding storage/lookup\r\n // - Portal sends requests with mixed case in URL and routing key header\r\n // - The gateway lowercases the URL path for binding lookup, which should work\r\n // - But we need to register with the format the gateway expects for the binding key\r\n // - The gateway constructs binding key as: {cid}.{pid}.{lowercaseServiceName}.{type}\r\n // - So we register with lowercase to match what the gateway stores in the binding\r\n // - The portal will still send mixed case, and the gateway will lowercase it for lookup\r\n // Register with mixed-case qualifiedName to match C# behavior\r\n // The gateway will lowercase the service name when creating the binding key,\r\n // but the registration request should use the original case (as C# does)\r\n // This ensures the service name in the registration matches what the portal expects\r\n // Register with mixed-case qualifiedName to match C# behavior\r\n // The gateway's ServiceIdentity.fullNameNoType lowercases the service name when creating bindings,\r\n // but the registration request should use the original case (as C# does)\r\n // This ensures the service name in the registration matches what the portal expects\r\n const isDeployed = isRunningInContainer(this.env);\r\n \r\n // For deployed services, routingKey should be null/undefined (None in Scala)\r\n // For local dev, routingKey should be the actual routing key string\r\n // The backend expects Option[String] = None for deployed services\r\n // Note: microServiceId is not part of SocketSessionProviderRegisterRequest, so we don't include it\r\n // All fields except 'type' are Option[T] in Scala, so undefined fields will be omitted from JSON\r\n // This matches C# behavior where null/None fields are not serialized\r\n const request: Record<string, unknown> = {\r\n type: 'basic',\r\n name: primary.qualifiedName, // Use mixed-case as C# does - gateway handles lowercasing for binding storage\r\n beamoName: primary.name,\r\n };\r\n \r\n // Only include routingKey and startedById if they have values (for local dev)\r\n // For deployed services, these should be omitted (undefined) to match None in Scala\r\n if (!isDeployed && this.env.routingKey) {\r\n request.routingKey = this.env.routingKey;\r\n }\r\n if (!isDeployed && this.env.accountId) {\r\n request.startedById = this.env.accountId;\r\n }\r\n\r\n // Log registration request to match C# behavior exactly\r\n // Also log the actual JSON that will be sent (undefined fields will be omitted)\r\n const serializedRequest = JSON.stringify(request);\r\n this.logger.debug(\r\n {\r\n request: {\r\n type: request.type,\r\n name: request.name,\r\n beamoName: request.beamoName,\r\n routingKey: request.routingKey,\r\n startedById: request.startedById,\r\n },\r\n serializedJson: serializedRequest,\r\n isDeployed,\r\n },\r\n 'Registering service provider with gateway.',\r\n );\r\n\r\n try {\r\n await this.requester.request('post', 'gateway/provider', request);\r\n this.logger.info({ serviceName: primary.qualifiedName }, 'Service provider registered successfully.');\r\n \r\n // After registration, the gateway's BasicServiceProvider.start() is called asynchronously\r\n // This triggers afterRabbitInit() -> setupDirectServiceCommunication() -> scheduleServiceBindingCheck()\r\n // The first updateBindings() call happens immediately (0.millisecond delay)\r\n // We wait a bit to allow the gateway to set up the HTTP binding and call updateBindings()\r\n // This is especially important for deployed services where the gateway needs to establish\r\n // the external host binding before bindings can be created\r\n if (isDeployed) {\r\n this.logger.debug('Waiting for gateway to establish bindings after registration...');\r\n await new Promise((resolve) => setTimeout(resolve, 2000)); // 2 second wait for deployed services\r\n this.logger.debug('Wait complete, gateway should have established bindings by now.');\r\n }\r\n } catch (error) {\r\n this.logger.error(\r\n {\r\n err: error,\r\n request,\r\n serviceName: primary.qualifiedName,\r\n errorMessage: error instanceof Error ? error.message : String(error),\r\n },\r\n 'Failed to register service provider with gateway. This will prevent the service from receiving requests.'\r\n );\r\n throw error; // Re-throw so startup fails and we can see the error\r\n }\r\n\r\n if (options.disableAllBeamableEvents) {\r\n this.logger.info('Beamable events disabled by configuration.');\r\n } else {\r\n const eventRequest = {\r\n type: 'event',\r\n evtWhitelist: ['content.manifest', 'realm.config', 'logging.context'],\r\n };\r\n try {\r\n await this.requester.request('post', 'gateway/provider', eventRequest);\r\n } catch (error) {\r\n this.logger.warn({ err: error }, 'Failed to register event provider. Continuing without events.');\r\n }\r\n }\r\n }\r\n\r\n private async handleEvent(envelope: WebsocketEventEnvelope): Promise<void> {\r\n try {\r\n if (!envelope.path) {\r\n this.logger.debug({ envelope }, 'Ignoring websocket event without path.');\r\n return;\r\n }\r\n\r\n if (envelope.path.startsWith('event/')) {\r\n await this.requester.acknowledge(envelope.id);\r\n return;\r\n }\r\n\r\n const context = this.toRequestContext(envelope);\r\n await this.dispatch(context);\r\n } catch (error) {\r\n const err = error instanceof Error ? error : new Error(String(error));\r\n this.logger.error({ err, envelope }, 'Failed to handle websocket event.');\r\n const status = this.resolveErrorStatus(err);\r\n const response: GatewayResponse = {\r\n id: envelope.id,\r\n status,\r\n body: {\r\n error: err.name,\r\n message: err.message,\r\n },\r\n };\r\n await this.requester.sendResponse(response);\r\n }\r\n }\r\n\r\n private toRequestContext(envelope: WebsocketEventEnvelope): RequestContext {\r\n const path = envelope.path ?? '';\r\n const method = envelope.method ?? 'post';\r\n const userId = typeof envelope.from === 'number' ? envelope.from : 0;\r\n const headers = envelope.headers ?? {};\r\n \r\n // Extract scopes from envelope.scopes array\r\n // Note: X-DE-SCOPE header contains CID.PID, not scope values\r\n // The gateway sends scopes in envelope.scopes array\r\n // For admin endpoints, the gateway may not send scopes in the envelope,\r\n // so we infer admin scope from the path if it's an admin route\r\n const envelopeScopes = envelope.scopes ?? [];\r\n let scopes = normalizeScopes(envelopeScopes);\r\n \r\n // If this is an admin endpoint and no scopes are provided, infer admin scope\r\n // The gateway may not always send scopes for admin routes\r\n const pathLower = path.toLowerCase();\r\n if (pathLower.includes('/admin/') && scopes.size === 0) {\r\n scopes = normalizeScopes(['admin']);\r\n }\r\n\r\n let body: Record<string, unknown> | undefined;\r\n if (envelope.body && typeof envelope.body === 'string') {\r\n try {\r\n body = JSON.parse(envelope.body) as Record<string, unknown>;\r\n } catch (error) {\r\n this.logger.warn({ err: error, raw: envelope.body }, 'Failed to parse body string.');\r\n }\r\n } else if (envelope.body && typeof envelope.body === 'object') {\r\n body = envelope.body as Record<string, unknown>;\r\n }\r\n\r\n let payload: unknown;\r\n if (body && typeof body === 'object' && 'payload' in body) {\r\n const rawPayload = (body as Record<string, unknown>).payload;\r\n if (typeof rawPayload === 'string') {\r\n try {\r\n payload = JSON.parse(rawPayload) as unknown[];\r\n } catch (error) {\r\n this.logger.warn({ err: error }, 'Failed to parse payload JSON.');\r\n payload = rawPayload;\r\n }\r\n } else {\r\n payload = rawPayload;\r\n }\r\n }\r\n\r\n const targetService = this.findServiceForPath(path);\r\n const services = this.serviceManager.createFacade(userId, scopes, targetService?.definition.name);\r\n const provider = this.createRequestScope(path, targetService);\r\n\r\n const context: RequestContext = {\r\n id: envelope.id,\r\n path,\r\n method,\r\n status: envelope.status ?? 0,\r\n userId,\r\n payload,\r\n body,\r\n scopes,\r\n headers,\r\n cid: this.env.cid,\r\n pid: this.env.pid,\r\n services,\r\n throwIfCancelled: () => {},\r\n isCancelled: () => false,\r\n hasScopes: (...requiredScopes: string[]) => requiredScopes.every((scope) => scopeSetHas(scopes, scope)),\r\n requireScopes: (...requiredScopes: string[]) => {\r\n const missingScopes = requiredScopes.filter((scope) => !scopeSetHas(scopes, scope));\r\n if (missingScopes.length > 0) {\r\n throw new MissingScopesError(missingScopes);\r\n }\r\n },\r\n provider,\r\n };\r\n\r\n provider.setInstance(REQUEST_CONTEXT_TOKEN, context);\r\n provider.setInstance(BEAMABLE_SERVICES_TOKEN, services);\r\n\r\n return context;\r\n }\r\n\r\n private findServiceForPath(path: string): ServiceInstance | undefined {\r\n // Gateway sends paths with lowercase service names, so we need case-insensitive matching\r\n // Match by comparing lowercase versions to handle gateway's lowercase path format\r\n const pathLower = path.toLowerCase();\r\n return this.services.find((service) => {\r\n const qualifiedNameLower = service.definition.qualifiedName.toLowerCase();\r\n return pathLower.startsWith(`${qualifiedNameLower}/`);\r\n });\r\n }\r\n\r\n private async dispatch(ctx: RequestContext): Promise<void> {\r\n const service = this.findServiceForPath(ctx.path);\r\n if (!service) {\r\n throw new UnknownRouteError(ctx.path);\r\n }\r\n\r\n if (await this.tryHandleFederationRoute(ctx, service)) {\r\n return;\r\n }\r\n\r\n if (await this.tryHandleAdminRoute(ctx, service)) {\r\n return;\r\n }\r\n\r\n // Extract route from path - handle case-insensitive path matching\r\n const pathLower = ctx.path.toLowerCase();\r\n const qualifiedNameLower = service.definition.qualifiedName.toLowerCase();\r\n const route = pathLower.substring(qualifiedNameLower.length + 1);\r\n const metadata = service.definition.callables.get(route);\r\n if (!metadata) {\r\n throw new UnknownRouteError(ctx.path);\r\n }\r\n\r\n // For server and admin access, allow userId: 0 if the appropriate scope is present\r\n // For client access, always require userId > 0\r\n if (metadata.requireAuth && ctx.userId <= 0) {\r\n if (metadata.access === 'server' && scopeSetHas(ctx.scopes, 'server')) {\r\n // Server callables with server scope don't need a user ID\r\n } else if (metadata.access === 'admin' && scopeSetHas(ctx.scopes, 'admin')) {\r\n // Admin callables with admin scope don't need a user ID\r\n } else {\r\n throw new UnauthorizedUserError(route);\r\n }\r\n }\r\n\r\n enforceAccess(metadata.access, ctx);\r\n\r\n if (metadata.requiredScopes.length > 0) {\r\n const missing = metadata.requiredScopes.filter((scope) => !scopeSetHas(ctx.scopes, scope));\r\n if (missing.length > 0) {\r\n throw new MissingScopesError(missing);\r\n }\r\n }\r\n\r\n const handler = service.instance[metadata.displayName];\r\n if (typeof handler !== 'function') {\r\n throw new Error(`Callable ${metadata.displayName} is not a function on service ${service.definition.name}.`);\r\n }\r\n\r\n const args = this.buildInvocationArguments(handler as (...args: unknown[]) => unknown, ctx);\r\n const result = await Promise.resolve((handler as (...args: unknown[]) => unknown).apply(service.instance, args));\r\n await this.sendSuccessResponse(ctx, metadata, result);\r\n }\r\n\r\n private buildInvocationArguments(\r\n handler: (...args: unknown[]) => unknown,\r\n ctx: RequestContext,\r\n ): unknown[] {\r\n const payload = Array.isArray(ctx.payload)\r\n ? ctx.payload\r\n : typeof ctx.payload === 'string'\r\n ? [ctx.payload]\r\n : ctx.payload === undefined\r\n ? []\r\n : [ctx.payload];\r\n\r\n const expectedParams = handler.length;\r\n if (expectedParams === payload.length + 1) {\r\n return [ctx, ...payload];\r\n }\r\n return payload;\r\n }\r\n\r\n private async sendSuccessResponse(ctx: RequestContext, metadata: ServiceCallableMetadata, result: unknown): Promise<void> {\r\n let body: unknown;\r\n if (metadata.useLegacySerialization) {\r\n const serialized = typeof result === 'string' ? result : JSON.stringify(result ?? null);\r\n body = { payload: serialized };\r\n } else {\r\n body = result ?? null;\r\n }\r\n\r\n const response: GatewayResponse = {\r\n id: ctx.id,\r\n status: 200,\r\n body,\r\n };\r\n await this.requester.sendResponse(response);\r\n }\r\n\r\n private async sendFederationResponse(ctx: RequestContext, result: unknown): Promise<void> {\r\n const response: GatewayResponse = {\r\n id: ctx.id,\r\n status: 200,\r\n body: result ?? null,\r\n };\r\n await this.requester.sendResponse(response);\r\n }\r\n\r\n private async tryHandleFederationRoute(ctx: RequestContext, service: ServiceInstance): Promise<boolean> {\r\n // Gateway sends paths with lowercase service names, so we need case-insensitive matching\r\n const pathLower = ctx.path.toLowerCase();\r\n const qualifiedNameLower = service.definition.qualifiedName.toLowerCase();\r\n const prefixLower = `${qualifiedNameLower}/`;\r\n if (!pathLower.startsWith(prefixLower)) {\r\n return false;\r\n }\r\n // Extract relative path - use lowercase length since gateway sends lowercase paths\r\n const relativePath = ctx.path.substring(qualifiedNameLower.length + 1);\r\n const handler = service.federationRegistry.resolve(relativePath);\r\n if (!handler) {\r\n return false;\r\n }\r\n\r\n const result = await handler.invoke(ctx as FederatedRequestContext);\r\n await this.sendFederationResponse(ctx, result);\r\n return true;\r\n }\r\n\r\n private async tryHandleAdminRoute(ctx: RequestContext, service: ServiceInstance): Promise<boolean> {\r\n // Gateway sends paths with lowercase service names, so we need case-insensitive matching\r\n // Check if path starts with the admin prefix (case-insensitive)\r\n const pathLower = ctx.path.toLowerCase();\r\n const adminPrefixLower = `${service.definition.qualifiedName.toLowerCase()}/admin/`;\r\n if (!pathLower.startsWith(adminPrefixLower)) {\r\n return false;\r\n }\r\n\r\n const options = getServiceOptions(service.definition.ctor) ?? {};\r\n\r\n const action = pathLower.substring(adminPrefixLower.length);\r\n const requiresAdmin = action === 'metadata' || action === 'docs';\r\n if (requiresAdmin && !scopeSetHas(ctx.scopes, 'admin')) {\r\n // For portal requests to admin endpoints, the gateway may not send scopes\r\n // The X-DE-SCOPE header contains CID.PID, not scope values\r\n // If this is a portal request (has X-DE-SCOPE header), grant admin scope\r\n const hasPortalHeader = ctx.headers['X-DE-SCOPE'] || ctx.headers['x-de-scope'];\r\n if (hasPortalHeader) {\r\n // Grant admin scope for portal requests to admin endpoints\r\n ctx.scopes.add('admin');\r\n } else {\r\n throw new MissingScopesError(['admin']);\r\n }\r\n }\r\n\r\n switch (action) {\r\n case 'health':\r\n case 'healthcheck':\r\n await this.requester.sendResponse({ id: ctx.id, status: 200, body: 'responsive' });\r\n return true;\r\n case 'metadata':\r\n case 'docs':\r\n break;\r\n default:\r\n if (!scopeSetHas(ctx.scopes, 'admin')) {\r\n throw new MissingScopesError(['admin']);\r\n }\r\n throw new UnknownRouteError(ctx.path);\r\n }\r\n\r\n if (action === 'metadata') {\r\n const federatedComponents = service.federationRegistry\r\n .list()\r\n .map((component) => ({\r\n federationNamespace: component.federationNamespace,\r\n federationType: component.federationType,\r\n }));\r\n\r\n await this.requester.sendResponse({\r\n id: ctx.id,\r\n status: 200,\r\n body: {\r\n serviceName: service.definition.name,\r\n sdkVersion: this.env.sdkVersionExecution,\r\n sdkBaseBuildVersion: this.env.sdkVersionExecution,\r\n sdkExecutionVersion: this.env.sdkVersionExecution,\r\n useLegacySerialization: Boolean(options.useLegacySerialization),\r\n disableAllBeamableEvents: Boolean(options.disableAllBeamableEvents),\r\n enableEagerContentLoading: false,\r\n instanceId: this.microServiceId,\r\n routingKey: this.env.routingKey ?? '',\r\n federatedComponents,\r\n },\r\n });\r\n return true;\r\n }\r\n\r\n if (action === 'docs') {\r\n try {\r\n const document = generateOpenApiDocument(\r\n {\r\n qualifiedName: service.definition.qualifiedName,\r\n name: service.definition.name,\r\n callables: Array.from(service.definition.callables.values()).map((callable) => ({\r\n name: callable.displayName,\r\n route: callable.route,\r\n metadata: callable,\r\n handler: typeof service.instance[callable.displayName] === 'function'\r\n ? (service.instance[callable.displayName] as (...args: unknown[]) => unknown)\r\n : undefined,\r\n })),\r\n },\r\n this.env,\r\n );\r\n\r\n // Ensure document is valid (not empty)\r\n if (!document || Object.keys(document).length === 0) {\r\n this.logger.warn({ serviceName: service.definition.name }, 'Generated OpenAPI document is empty');\r\n }\r\n\r\n await this.requester.sendResponse({\r\n id: ctx.id,\r\n status: 200,\r\n body: document,\r\n });\r\n return true;\r\n } catch (error) {\r\n this.logger.error({ err: error, serviceName: service.definition.name }, 'Failed to generate or send docs');\r\n throw error;\r\n }\r\n }\r\n\r\n return false;\r\n }\r\n\r\n private resolveErrorStatus(error: Error): number {\r\n if (error instanceof UnauthorizedUserError) {\r\n return 401;\r\n }\r\n if (error instanceof MissingScopesError) {\r\n return 403;\r\n }\r\n if (error instanceof UnknownRouteError) {\r\n return 404;\r\n }\r\n if (error instanceof BeamableRuntimeError) {\r\n return 500;\r\n }\r\n return 500;\r\n }\r\n\r\n private printHelpfulUrls(service?: ServiceInstance): void {\r\n if (!service) {\r\n return;\r\n }\r\n\r\n // Only print helpful URLs in local development (not in containers)\r\n // In containers, we want only JSON logs for proper log collection\r\n if (isRunningInContainer(this.env)) {\r\n return;\r\n }\r\n\r\n const docsUrl = buildDocsPortalUrl(this.env, service.definition);\r\n const endpointBase = buildPostmanBaseUrl(this.env, service.definition);\r\n\r\n const green = (text: string) => `\\x1b[32m${text}\\x1b[0m`;\r\n const yellow = (text: string) => `\\x1b[33m${text}\\x1b[0m`;\r\n\r\n const bannerLines = [\r\n '',\r\n yellow('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'),\r\n ` ${green('Beamable Node microservice ready:')} ${green(service.definition.name)}`,\r\n yellow(' Quick shortcuts'),\r\n ` ${yellow('Docs:')} ${docsUrl}`,\r\n ` ${yellow('Endpoint:')} ${endpointBase}`,\r\n yellow('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'),\r\n '',\r\n ];\r\n\r\n process.stdout.write(`${bannerLines.join('\\n')}`);\r\n }\r\n\r\n private async initializeDependencyProviders(): Promise<void> {\r\n for (const service of this.services) {\r\n const builder = new DependencyBuilder();\r\n builder.tryAddSingletonInstance(LOGGER_TOKEN, service.logger);\r\n builder.tryAddSingletonInstance(ENVIRONMENT_CONFIG_TOKEN, this.env);\r\n builder.tryAddSingletonInstance(BeamableServiceManager, this.serviceManager);\r\n builder.tryAddSingletonInstance(service.definition.ctor, service.instance);\r\n builder.tryAddSingletonInstance(FederationRegistry, service.federationRegistry);\r\n\r\n for (const handler of service.configureHandlers) {\r\n await handler(builder);\r\n }\r\n\r\n const provider = builder.build();\r\n service.provider = provider;\r\n\r\n for (const handler of service.initializeHandlers) {\r\n await handler(provider);\r\n }\r\n\r\n Object.defineProperty(service.instance, 'provider', {\r\n value: provider,\r\n enumerable: false,\r\n configurable: false,\r\n writable: false,\r\n });\r\n }\r\n }\r\n\r\n private createRequestScope(path: string, service?: ServiceInstance): MutableDependencyScope {\r\n const targetService = service ?? this.findServiceForPath(path);\r\n const provider = targetService?.provider ?? new DependencyBuilder().build();\r\n return provider.createScope();\r\n }\r\n}\r\n\r\nfunction enforceAccess(access: ServiceAccess, ctx: RequestContext): void {\r\n switch (access) {\r\n case 'client':\r\n if (ctx.userId <= 0) {\r\n throw new UnauthorizedUserError(ctx.path);\r\n }\r\n break;\r\n case 'server':\r\n if (!scopeSetHas(ctx.scopes, 'server')) {\r\n throw new MissingScopesError(['server']);\r\n }\r\n break;\r\n case 'admin':\r\n if (!scopeSetHas(ctx.scopes, 'admin')) {\r\n throw new MissingScopesError(['admin']);\r\n }\r\n break;\r\n default:\r\n break;\r\n }\r\n}\r\n\r\n/**\r\n * Conditionally output to console.error only when running locally (not in container).\r\n * In containers, we want only Pino JSON logs to stdout for proper log collection.\r\n */\r\nfunction debugLog(...args: unknown[]): void {\r\n // Check process.env directly since this is a standalone function\r\n if (!isRunningInContainer()) {\r\n console.error(...args);\r\n }\r\n}\r\n\r\nfunction isRunningInContainer(env?: EnvironmentConfig): boolean {\r\n // Explicit container indicators (highest priority)\r\n if (\r\n process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||\r\n process.env.CONTAINER === 'beamable' ||\r\n !!process.env.ECS_CONTAINER_METADATA_URI ||\r\n !!process.env.KUBERNETES_SERVICE_HOST\r\n ) {\r\n return true;\r\n }\r\n \r\n // If we have a routing key (from env object or process.env), we're definitely running locally\r\n // Local dev always has a routing key (either set explicitly or auto-generated)\r\n const hasRoutingKey = env?.routingKey || process.env.NAME_PREFIX || process.env.ROUTING_KEY;\r\n if (hasRoutingKey) {\r\n return false; // Local development\r\n }\r\n \r\n // If we have Beamable env vars but NO routing key, we're in a deployed container\r\n const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);\r\n if (hasBeamableEnvVars) {\r\n return true; // Deployed container (has env vars but no routing key)\r\n }\r\n \r\n // Default: assume local development\r\n return false;\r\n}\r\n\r\nfunction normalizeScopes(scopes: Array<unknown>): Set<string> {\r\n const normalized = new Set<string>();\r\n for (const scope of scopes) {\r\n if (typeof scope !== 'string' || scope.trim().length === 0) {\r\n continue;\r\n }\r\n normalized.add(scope.trim().toLowerCase());\r\n }\r\n return normalized;\r\n}\r\n\r\nfunction scopeSetHas(scopes: Set<string>, scope: string): boolean {\r\n const normalized = scope.trim().toLowerCase();\r\n if (normalized.length === 0) {\r\n return false;\r\n }\r\n return scopes.has('*') || scopes.has(normalized);\r\n}\r\n\r\nfunction buildPostmanBaseUrl(env: EnvironmentConfig, service: ServiceDefinition): string {\r\n const httpHost = hostToHttpUrl(env.host);\r\n const routingKeyPart = env.routingKey ? env.routingKey : '';\r\n return `${httpHost}/basic/${env.cid}.${env.pid}.${routingKeyPart}${service.qualifiedName}/`;\r\n}\r\n\r\nfunction buildDocsPortalUrl(env: EnvironmentConfig, service: ServiceDefinition): string {\r\n const portalHost = hostToPortalUrl(hostToHttpUrl(env.host));\r\n const queryParams: Record<string, string> = { srcTool: 'node-runtime' };\r\n if (env.routingKey) {\r\n queryParams.routingKey = env.routingKey;\r\n }\r\n const query = new URLSearchParams(queryParams);\r\n if (env.refreshToken) {\r\n query.set('refresh_token', env.refreshToken);\r\n }\r\n const beamoId = service.name;\r\n return `${portalHost}/${env.cid}/games/${env.pid}/realms/${env.pid}/microservices/micro_${beamoId}/docs?${query.toString()}`;\r\n}\r\n\r\nexport async function runMicroservice(): Promise<void> {\r\n // Immediate console output to verify process is starting (local dev only)\r\n // In containers, we rely on Pino logger for all output\r\n debugLog('[BEAMABLE-NODE] Starting microservice...');\r\n debugLog(`[BEAMABLE-NODE] Node version: ${process.version}`);\r\n debugLog(`[BEAMABLE-NODE] Working directory: ${process.cwd()}`);\r\n debugLog(`[BEAMABLE-NODE] Environment: ${JSON.stringify({\r\n NODE_ENV: process.env.NODE_ENV,\r\n CID: process.env.CID ? 'SET' : 'NOT SET',\r\n PID: process.env.PID ? 'SET' : 'NOT SET',\r\n HOST: process.env.HOST ? 'SET' : 'NOT SET',\r\n SECRET: process.env.SECRET ? 'SET' : 'NOT SET',\r\n })}`);\r\n if (process.env.BEAMABLE_SKIP_RUNTIME === 'true') {\r\n return;\r\n }\r\n const runtime = new MicroserviceRuntime();\r\n \r\n // Handle uncaught errors - log them but don't crash immediately\r\n // This allows the health check server to keep running so we can debug\r\n // In containers, errors will be logged by the logger in the runtime\r\n // Locally, use console.error for visibility\r\n process.on('uncaughtException', (error) => {\r\n // In containers, the logger will handle this; locally, show in console\r\n debugLog('Uncaught Exception:', error);\r\n // Don't exit - let the health check server keep running\r\n });\r\n \r\n process.on('unhandledRejection', (reason, promise) => {\r\n // In containers, the logger will handle this; locally, show in console\r\n debugLog('Unhandled Rejection at:', promise, 'reason:', reason);\r\n // Don't exit - let the health check server keep running\r\n });\r\n \r\n try {\r\n await runtime.start();\r\n } catch (error) {\r\n // Log the error but don't exit - health check server is running\r\n // This allows the container to stay alive so we can see what went wrong\r\n // In containers, the logger already logged it in start(); locally, also use console.error\r\n debugLog('Failed to start microservice runtime:', error);\r\n // Keep the process alive so health check can continue\r\n // The health check will return 503 until isReady is true\r\n }\r\n \r\n const shutdownSignals: NodeJS.Signals[] = ['SIGTERM', 'SIGINT'];\r\n shutdownSignals.forEach((signal) => {\r\n process.once(signal, async () => {\r\n await runtime.shutdown();\r\n process.exit(0);\r\n });\r\n });\r\n}\r\n\r\ninterface GenerateOpenApiOptions {\r\n serviceName?: string;\r\n}\r\n\r\nexport function generateOpenApiDocumentForRegisteredServices(\r\n overrides: Partial<EnvironmentConfig> = {},\r\n options: GenerateOpenApiOptions = {},\r\n): unknown {\r\n const services = listRegisteredServices();\r\n if (services.length === 0) {\r\n throw new Error('No microservices registered. Import your service module before generating documentation.');\r\n }\r\n\r\n const baseEnv = buildEnvironmentConfig(overrides);\r\n const primary = options.serviceName\r\n ? services.find((svc) => svc.name === options.serviceName || svc.qualifiedName === options.serviceName)\r\n : services[0];\r\n\r\n if (!primary) {\r\n throw new Error(`No registered microservice matched '${options.serviceName}'.`);\r\n }\r\n\r\n return generateOpenApiDocument(\r\n {\r\n qualifiedName: primary.qualifiedName,\r\n name: primary.name,\r\n callables: Array.from(primary.callables.entries()).map(([displayName, metadata]) => ({\r\n name: displayName,\r\n route: metadata.route,\r\n metadata,\r\n handler: undefined,\r\n })),\r\n },\r\n baseEnv,\r\n );\r\n}\r\n\r\nfunction buildEnvironmentConfig(overrides: Partial<EnvironmentConfig>): EnvironmentConfig {\r\n const merged: Partial<EnvironmentConfig> = { ...overrides };\r\n\r\n const ensure = (key: keyof EnvironmentConfig, fallbackEnv?: string) => {\r\n if (merged[key] !== undefined) {\r\n return;\r\n }\r\n if (fallbackEnv && process.env[fallbackEnv]) {\r\n merged[key] = process.env[fallbackEnv] as never;\r\n }\r\n };\r\n\r\n ensure('cid', 'CID');\r\n ensure('pid', 'PID');\r\n ensure('host', 'HOST');\r\n ensure('secret', 'SECRET');\r\n ensure('refreshToken', 'REFRESH_TOKEN');\r\n // Routing key is optional for deployed services (in containers)\r\n // It will be resolved to empty string if not provided and running in container\r\n if (!merged.routingKey && !isRunningInContainer()) {\r\n ensure('routingKey', 'NAME_PREFIX');\r\n }\r\n\r\n if (!merged.cid || !merged.pid || !merged.host) {\r\n throw new Error('CID, PID, and HOST are required to generate documentation.');\r\n }\r\n\r\n const base = loadEnvironmentConfig();\r\n\r\n return {\r\n ...base,\r\n ...merged,\r\n routingKey: merged.routingKey ?? base.routingKey,\r\n };\r\n}\r\n"]}
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,4BAA4B,EAAE,6BAA6B,EAAE,MAAM,iBAAiB,CAAC;AACzI,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAWjH,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EACL,iBAAiB,EACjB,YAAY,EACZ,wBAAwB,EACxB,qBAAqB,EACrB,uBAAuB,GAGxB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,6BAA6B,EAAE,MAAM,iBAAiB,CAAC;AAE7G,OAAO,EAAE,YAAY,EAAe,MAAM,WAAW,CAAC;AAYtD,MAAM,OAAO,mBAAmB;IACb,GAAG,CAAoB;IACvB,MAAM,CAAS;IACf,QAAQ,CAAoB;IAC5B,SAAS,CAAoB;IAC7B,SAAS,CAAmB;IAC5B,WAAW,CAA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;IACtC,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QAC/C,MAAM,IAAI,KAAK,CAAC,4DAA4D,CAAC,CAAC;IAChF,CAAC;IAED,MAAM,IAAI,GAAG,qBAAqB,EAAE,CAAC;IAErC,OAAO;QACL,GAAG,IAAI;QACP,GAAG,MAAM;QACT,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,IAAI,CAAC,UAAU;KACjD,CAAC;AACJ,CAAC","sourcesContent":["import { randomUUID } from 'node:crypto';\r\nimport { BeamableWebSocket } from './websocket.js';\r\nimport { GatewayRequester } from './requester.js';\r\nimport { AuthManager } from './auth.js';\r\nimport { createLogger } from './logger.js';\r\nimport { loadEnvironmentConfig } from './env.js';\r\nimport { listRegisteredServices, getServiceOptions, getConfigureServicesHandlers, getInitializeServicesHandlers } from './decorators.js';\r\nimport { generateOpenApiDocument } from './docs.js';\r\nimport { DiscoveryBroadcaster } from './discovery.js';\r\nimport { BeamableRuntimeError, MissingScopesError, UnauthorizedUserError, UnknownRouteError } from './errors.js';\r\nimport type {\r\n EnvironmentConfig,\r\n RequestContext,\r\n ServiceDefinition,\r\n ServiceCallableMetadata,\r\n GatewayResponse,\r\n WebsocketEventEnvelope,\r\n ServiceAccess,\r\n} from './types.js';\r\nimport type { Logger } from 'pino';\r\nimport { BeamableServiceManager } from './services.js';\r\nimport {\r\n DependencyBuilder,\r\n LOGGER_TOKEN,\r\n ENVIRONMENT_CONFIG_TOKEN,\r\n REQUEST_CONTEXT_TOKEN,\r\n BEAMABLE_SERVICES_TOKEN,\r\n ServiceProvider,\r\n MutableDependencyScope,\r\n} from './dependency.js';\r\nimport { hostToHttpUrl, hostToPortalUrl } from './utils/urls.js';\r\nimport { FederationRegistry, getFederationComponents, getFederatedInventoryMetadata } from './federation.js';\r\nimport type { FederatedRequestContext } from './federation.js';\r\nimport { createServer, type Server } from 'node:http';\r\n\r\ninterface ServiceInstance {\r\n definition: ServiceDefinition;\r\n instance: Record<string, unknown>;\r\n configureHandlers: ReturnType<typeof getConfigureServicesHandlers>;\r\n initializeHandlers: ReturnType<typeof getInitializeServicesHandlers>;\r\n provider?: ServiceProvider;\r\n logger: Logger;\r\n federationRegistry: FederationRegistry;\r\n}\r\n\r\nexport class MicroserviceRuntime {\r\n private readonly env: EnvironmentConfig;\r\n private readonly logger: Logger;\r\n private readonly services: ServiceInstance[];\r\n private readonly webSocket: BeamableWebSocket;\r\n private readonly requester: GatewayRequester;\r\n private readonly authManager: AuthManager;\r\n private readonly discovery?: DiscoveryBroadcaster;\r\n private readonly microServiceId = randomUUID();\r\n private readonly serviceManager: BeamableServiceManager;\r\n private healthCheckServer?: Server;\r\n private isReady: boolean = false;\r\n\r\n constructor(env?: EnvironmentConfig) {\r\n this.env = env ?? loadEnvironmentConfig();\r\n this.logger = createLogger(this.env, { name: 'beamable-node-microservice' });\r\n this.serviceManager = new BeamableServiceManager(this.env, this.logger);\r\n\r\n const registered = listRegisteredServices();\r\n if (registered.length === 0) {\r\n throw new Error('No microservices registered. Use the @Microservice decorator to register at least one class.');\r\n }\r\n this.services = registered.map((definition) => {\r\n const instance = new definition.ctor() as Record<string, unknown>;\r\n const configureHandlers = getConfigureServicesHandlers(definition.ctor);\r\n const initializeHandlers = getInitializeServicesHandlers(definition.ctor);\r\n const logger = this.logger.child({ service: definition.name });\r\n const federationRegistry = new FederationRegistry(logger);\r\n const decoratedFederations = getFederationComponents(definition.ctor);\r\n for (const component of decoratedFederations) {\r\n federationRegistry.register(component);\r\n }\r\n const inventoryMetadata = getFederatedInventoryMetadata(definition.ctor);\r\n if (inventoryMetadata.length > 0) {\r\n federationRegistry.registerInventoryHandlers(instance, inventoryMetadata);\r\n }\r\n this.serviceManager.registerFederationRegistry(definition.name, federationRegistry);\r\n return { definition, instance, configureHandlers, initializeHandlers, logger, federationRegistry };\r\n });\r\n\r\n const socketUrl = this.env.host.endsWith('/socket') ? this.env.host : `${this.env.host}/socket`;\r\n this.webSocket = new BeamableWebSocket({ url: socketUrl, logger: this.logger });\r\n this.webSocket.on('message', (payload) => {\r\n this.logger.debug({ payload }, 'Runtime observed websocket frame.');\r\n });\r\n this.requester = new GatewayRequester(this.webSocket, this.logger);\r\n this.authManager = new AuthManager(this.env, this.requester);\r\n this.requester.on('event', (envelope) => this.handleEvent(envelope));\r\n\r\n if (!isRunningInContainer() && this.services.length > 0) {\r\n this.discovery = new DiscoveryBroadcaster({\r\n env: this.env,\r\n serviceName: this.services[0].definition.name,\r\n routingKey: this.env.routingKey,\r\n logger: this.logger.child({ component: 'DiscoveryBroadcaster' }),\r\n });\r\n }\r\n }\r\n\r\n async start(): Promise<void> {\r\n // Immediate console output for container logs (before logger is ready)\r\n debugLog('[BEAMABLE-NODE] MicroserviceRuntime.start() called');\r\n debugLog(`[BEAMABLE-NODE] Service count: ${this.services.length}`);\r\n \r\n this.printHelpfulUrls(this.services[0]);\r\n this.logger.info('Starting Beamable Node microservice runtime.');\r\n \r\n // Start health check server FIRST - this is critical for container health checks\r\n // Even if startup fails, the health check server must be running so we can debug\r\n debugLog('[BEAMABLE-NODE] Starting health check server...');\r\n await this.startHealthCheckServer();\r\n debugLog('[BEAMABLE-NODE] Health check server started');\r\n \r\n try {\r\n this.logger.info('Connecting to Beamable gateway...');\r\n await this.webSocket.connect();\r\n await new Promise((resolve) => setTimeout(resolve, 250));\r\n \r\n this.logger.info('Authenticating with Beamable...');\r\n await this.authManager.authenticate();\r\n \r\n this.logger.info('Initializing Beamable SDK services...');\r\n await this.serviceManager.initialize();\r\n \r\n this.logger.info('Initializing dependency providers...');\r\n await this.initializeDependencyProviders();\r\n \r\n this.logger.info('Registering service with gateway...');\r\n await this.registerService();\r\n \r\n this.logger.info('Starting discovery broadcaster...');\r\n await this.discovery?.start();\r\n \r\n // Mark as ready only after service is fully registered and discovery is started\r\n this.isReady = true;\r\n this.logger.info('Beamable microservice runtime is ready to accept traffic.');\r\n } catch (error) {\r\n // Log the error with full context but don't crash - health check server is running\r\n // This allows the container to stay alive so we can debug the issue\r\n // Debug output for local development only (in containers, logger handles this)\r\n debugLog('[BEAMABLE-NODE] FATAL ERROR during startup:');\r\n debugLog(`[BEAMABLE-NODE] Error message: ${error instanceof Error ? error.message : String(error)}`);\r\n debugLog(`[BEAMABLE-NODE] Error stack: ${error instanceof Error ? error.stack : 'No stack trace'}`);\r\n debugLog(`[BEAMABLE-NODE] isReady: ${this.isReady}`);\r\n \r\n this.logger.error(\r\n { \r\n err: error,\r\n errorMessage: error instanceof Error ? error.message : String(error),\r\n errorStack: error instanceof Error ? error.stack : undefined,\r\n isReady: this.isReady,\r\n },\r\n 'Failed to fully initialize microservice runtime. Health check will continue to return 503 until initialization completes.'\r\n );\r\n // DON'T re-throw - keep process alive so health check can show 503\r\n // This allows us to see the error in logs\r\n this.isReady = false;\r\n }\r\n }\r\n\r\n async shutdown(): Promise<void> {\r\n this.logger.info('Shutting down microservice runtime.');\r\n this.isReady = false; // Mark as not ready during shutdown\r\n this.discovery?.stop();\r\n await this.stopHealthCheckServer();\r\n this.requester.dispose();\r\n await this.webSocket.close();\r\n }\r\n\r\n private async startHealthCheckServer(): Promise<void> {\r\n // For deployed services, always start health check server (required for container health checks)\r\n // For local development, only start if healthPort is explicitly set\r\n // IMPORTANT: Always default to 6565 if HEALTH_PORT env var is not set, as this is the standard port\r\n const healthPort = this.env.healthPort || 6565;\r\n \r\n // Always start health check server if we have a valid port\r\n // The container orchestrator expects this endpoint to be available\r\n if (!healthPort || healthPort === 0) {\r\n // Health check server not needed (local development without explicit port)\r\n this.logger.debug('Health check server skipped (no healthPort set)');\r\n return;\r\n }\r\n\r\n this.healthCheckServer = createServer((req, res) => {\r\n if (req.url === '/health' && req.method === 'GET') {\r\n // Only return success if service is fully ready (registered and accepting traffic)\r\n if (this.isReady) {\r\n res.writeHead(200, { 'Content-Type': 'text/plain' });\r\n res.end('responsive');\r\n } else {\r\n // Service is still starting up\r\n res.writeHead(503, { 'Content-Type': 'text/plain' });\r\n res.end('Service Unavailable');\r\n }\r\n } else {\r\n res.writeHead(404, { 'Content-Type': 'text/plain' });\r\n res.end('Not Found');\r\n }\r\n });\r\n\r\n return new Promise((resolve, reject) => {\r\n this.healthCheckServer!.listen(healthPort, '0.0.0.0', () => {\r\n this.logger.info({ port: healthPort }, 'Health check server started on port');\r\n resolve();\r\n });\r\n this.healthCheckServer!.on('error', (err) => {\r\n this.logger.error({ err, port: healthPort }, 'Failed to start health check server');\r\n reject(err);\r\n });\r\n });\r\n }\r\n\r\n private async stopHealthCheckServer(): Promise<void> {\r\n if (!this.healthCheckServer) {\r\n return;\r\n }\r\n\r\n return new Promise((resolve) => {\r\n this.healthCheckServer!.close(() => {\r\n this.logger.info('Health check server stopped');\r\n resolve();\r\n });\r\n });\r\n }\r\n\r\n private async registerService(): Promise<void> {\r\n const primary = this.services[0]?.definition;\r\n if (!primary) {\r\n throw new Error('Unexpected missing service definition during registration.');\r\n }\r\n const options = getServiceOptions(primary.ctor) ?? {};\r\n // Match C# exactly: use qualifiedName (preserves case) for name field.\r\n // The gateway lowercases service names when creating bindings, but uses the original case\r\n // from the registration request when constructing routing key lookups.\r\n // This ensures the routing key format matches what the gateway expects.\r\n // The gateway's binding lookup behavior:\r\n // - Gateway error shows: \"No binding found for service ...micro_examplenodeservice.basic\"\r\n // - This means the gateway lowercases the service name for binding storage/lookup\r\n // - Portal sends requests with mixed case in URL and routing key header\r\n // - The gateway lowercases the URL path for binding lookup, which should work\r\n // - But we need to register with the format the gateway expects for the binding key\r\n // - The gateway constructs binding key as: {cid}.{pid}.{lowercaseServiceName}.{type}\r\n // - So we register with lowercase to match what the gateway stores in the binding\r\n // - The portal will still send mixed case, and the gateway will lowercase it for lookup\r\n // Register with mixed-case qualifiedName to match C# behavior\r\n // The gateway will lowercase the service name when creating the binding key,\r\n // but the registration request should use the original case (as C# does)\r\n // This ensures the service name in the registration matches what the portal expects\r\n // Register with mixed-case qualifiedName to match C# behavior\r\n // The gateway's ServiceIdentity.fullNameNoType lowercases the service name when creating bindings,\r\n // but the registration request should use the original case (as C# does)\r\n // This ensures the service name in the registration matches what the portal expects\r\n const isDeployed = isRunningInContainer();\r\n \r\n // For deployed services, routingKey should be null/undefined (None in Scala)\r\n // For local dev, routingKey should be the actual routing key string\r\n // The backend expects Option[String] = None for deployed services\r\n // Note: microServiceId is not part of SocketSessionProviderRegisterRequest, so we don't include it\r\n // All fields except 'type' are Option[T] in Scala, so undefined fields will be omitted from JSON\r\n // This matches C# behavior where null/None fields are not serialized\r\n const request: Record<string, unknown> = {\r\n type: 'basic',\r\n name: primary.qualifiedName, // Use mixed-case as C# does - gateway handles lowercasing for binding storage\r\n beamoName: primary.name,\r\n };\r\n \r\n // Only include routingKey and startedById if they have values (for local dev)\r\n // For deployed services, these should be omitted (undefined) to match None in Scala\r\n if (!isDeployed && this.env.routingKey) {\r\n request.routingKey = this.env.routingKey;\r\n }\r\n if (!isDeployed && this.env.accountId) {\r\n request.startedById = this.env.accountId;\r\n }\r\n\r\n // Log registration request to match C# behavior exactly\r\n // Also log the actual JSON that will be sent (undefined fields will be omitted)\r\n const serializedRequest = JSON.stringify(request);\r\n this.logger.debug(\r\n {\r\n request: {\r\n type: request.type,\r\n name: request.name,\r\n beamoName: request.beamoName,\r\n routingKey: request.routingKey,\r\n startedById: request.startedById,\r\n },\r\n serializedJson: serializedRequest,\r\n isDeployed,\r\n },\r\n 'Registering service provider with gateway.',\r\n );\r\n\r\n try {\r\n await this.requester.request('post', 'gateway/provider', request);\r\n this.logger.info({ serviceName: primary.qualifiedName }, 'Service provider registered successfully.');\r\n \r\n // After registration, the gateway's BasicServiceProvider.start() is called asynchronously\r\n // This triggers afterRabbitInit() -> setupDirectServiceCommunication() -> scheduleServiceBindingCheck()\r\n // The first updateBindings() call happens immediately (0.millisecond delay)\r\n // We wait a bit to allow the gateway to set up the HTTP binding and call updateBindings()\r\n // This is especially important for deployed services where the gateway needs to establish\r\n // the external host binding before bindings can be created\r\n if (isDeployed) {\r\n this.logger.debug('Waiting for gateway to establish bindings after registration...');\r\n await new Promise((resolve) => setTimeout(resolve, 2000)); // 2 second wait for deployed services\r\n this.logger.debug('Wait complete, gateway should have established bindings by now.');\r\n }\r\n } catch (error) {\r\n this.logger.error(\r\n {\r\n err: error,\r\n request,\r\n serviceName: primary.qualifiedName,\r\n errorMessage: error instanceof Error ? error.message : String(error),\r\n },\r\n 'Failed to register service provider with gateway. This will prevent the service from receiving requests.'\r\n );\r\n throw error; // Re-throw so startup fails and we can see the error\r\n }\r\n\r\n if (options.disableAllBeamableEvents) {\r\n this.logger.info('Beamable events disabled by configuration.');\r\n } else {\r\n const eventRequest = {\r\n type: 'event',\r\n evtWhitelist: ['content.manifest', 'realm.config', 'logging.context'],\r\n };\r\n try {\r\n await this.requester.request('post', 'gateway/provider', eventRequest);\r\n } catch (error) {\r\n this.logger.warn({ err: error }, 'Failed to register event provider. Continuing without events.');\r\n }\r\n }\r\n }\r\n\r\n private async handleEvent(envelope: WebsocketEventEnvelope): Promise<void> {\r\n try {\r\n if (!envelope.path) {\r\n this.logger.debug({ envelope }, 'Ignoring websocket event without path.');\r\n return;\r\n }\r\n\r\n if (envelope.path.startsWith('event/')) {\r\n await this.requester.acknowledge(envelope.id);\r\n return;\r\n }\r\n\r\n const context = this.toRequestContext(envelope);\r\n await this.dispatch(context);\r\n } catch (error) {\r\n const err = error instanceof Error ? error : new Error(String(error));\r\n this.logger.error({ err, envelope }, 'Failed to handle websocket event.');\r\n const status = this.resolveErrorStatus(err);\r\n const response: GatewayResponse = {\r\n id: envelope.id,\r\n status,\r\n body: {\r\n error: err.name,\r\n message: err.message,\r\n },\r\n };\r\n await this.requester.sendResponse(response);\r\n }\r\n }\r\n\r\n private toRequestContext(envelope: WebsocketEventEnvelope): RequestContext {\r\n const path = envelope.path ?? '';\r\n const method = envelope.method ?? 'post';\r\n const userId = typeof envelope.from === 'number' ? envelope.from : 0;\r\n const headers = envelope.headers ?? {};\r\n \r\n // Extract scopes from envelope.scopes array\r\n // Note: X-DE-SCOPE header contains CID.PID, not scope values\r\n // The gateway sends scopes in envelope.scopes array\r\n // For admin endpoints, the gateway may not send scopes in the envelope,\r\n // so we infer admin scope from the path if it's an admin route\r\n const envelopeScopes = envelope.scopes ?? [];\r\n let scopes = normalizeScopes(envelopeScopes);\r\n \r\n // If this is an admin endpoint and no scopes are provided, infer admin scope\r\n // The gateway may not always send scopes for admin routes\r\n const pathLower = path.toLowerCase();\r\n if (pathLower.includes('/admin/') && scopes.size === 0) {\r\n scopes = normalizeScopes(['admin']);\r\n }\r\n\r\n let body: Record<string, unknown> | undefined;\r\n if (envelope.body && typeof envelope.body === 'string') {\r\n try {\r\n body = JSON.parse(envelope.body) as Record<string, unknown>;\r\n } catch (error) {\r\n this.logger.warn({ err: error, raw: envelope.body }, 'Failed to parse body string.');\r\n }\r\n } else if (envelope.body && typeof envelope.body === 'object') {\r\n body = envelope.body as Record<string, unknown>;\r\n }\r\n\r\n let payload: unknown;\r\n if (body && typeof body === 'object' && 'payload' in body) {\r\n const rawPayload = (body as Record<string, unknown>).payload;\r\n if (typeof rawPayload === 'string') {\r\n try {\r\n payload = JSON.parse(rawPayload) as unknown[];\r\n } catch (error) {\r\n this.logger.warn({ err: error }, 'Failed to parse payload JSON.');\r\n payload = rawPayload;\r\n }\r\n } else {\r\n payload = rawPayload;\r\n }\r\n }\r\n\r\n const targetService = this.findServiceForPath(path);\r\n const services = this.serviceManager.createFacade(userId, scopes, targetService?.definition.name);\r\n const provider = this.createRequestScope(path, targetService);\r\n\r\n const context: RequestContext = {\r\n id: envelope.id,\r\n path,\r\n method,\r\n status: envelope.status ?? 0,\r\n userId,\r\n payload,\r\n body,\r\n scopes,\r\n headers,\r\n cid: this.env.cid,\r\n pid: this.env.pid,\r\n services,\r\n throwIfCancelled: () => {},\r\n isCancelled: () => false,\r\n hasScopes: (...requiredScopes: string[]) => requiredScopes.every((scope) => scopeSetHas(scopes, scope)),\r\n requireScopes: (...requiredScopes: string[]) => {\r\n const missingScopes = requiredScopes.filter((scope) => !scopeSetHas(scopes, scope));\r\n if (missingScopes.length > 0) {\r\n throw new MissingScopesError(missingScopes);\r\n }\r\n },\r\n provider,\r\n };\r\n\r\n provider.setInstance(REQUEST_CONTEXT_TOKEN, context);\r\n provider.setInstance(BEAMABLE_SERVICES_TOKEN, services);\r\n\r\n return context;\r\n }\r\n\r\n private findServiceForPath(path: string): ServiceInstance | undefined {\r\n // Gateway sends paths with lowercase service names, so we need case-insensitive matching\r\n // Match by comparing lowercase versions to handle gateway's lowercase path format\r\n const pathLower = path.toLowerCase();\r\n return this.services.find((service) => {\r\n const qualifiedNameLower = service.definition.qualifiedName.toLowerCase();\r\n return pathLower.startsWith(`${qualifiedNameLower}/`);\r\n });\r\n }\r\n\r\n private async dispatch(ctx: RequestContext): Promise<void> {\r\n const service = this.findServiceForPath(ctx.path);\r\n if (!service) {\r\n throw new UnknownRouteError(ctx.path);\r\n }\r\n\r\n if (await this.tryHandleFederationRoute(ctx, service)) {\r\n return;\r\n }\r\n\r\n if (await this.tryHandleAdminRoute(ctx, service)) {\r\n return;\r\n }\r\n\r\n // Extract route from path - handle case-insensitive path matching\r\n const pathLower = ctx.path.toLowerCase();\r\n const qualifiedNameLower = service.definition.qualifiedName.toLowerCase();\r\n const route = pathLower.substring(qualifiedNameLower.length + 1);\r\n const metadata = service.definition.callables.get(route);\r\n if (!metadata) {\r\n throw new UnknownRouteError(ctx.path);\r\n }\r\n\r\n // For server and admin access, allow userId: 0 if the appropriate scope is present\r\n // For client access, always require userId > 0\r\n if (metadata.requireAuth && ctx.userId <= 0) {\r\n if (metadata.access === 'server' && scopeSetHas(ctx.scopes, 'server')) {\r\n // Server callables with server scope don't need a user ID\r\n } else if (metadata.access === 'admin' && scopeSetHas(ctx.scopes, 'admin')) {\r\n // Admin callables with admin scope don't need a user ID\r\n } else {\r\n throw new UnauthorizedUserError(route);\r\n }\r\n }\r\n\r\n enforceAccess(metadata.access, ctx);\r\n\r\n if (metadata.requiredScopes.length > 0) {\r\n const missing = metadata.requiredScopes.filter((scope) => !scopeSetHas(ctx.scopes, scope));\r\n if (missing.length > 0) {\r\n throw new MissingScopesError(missing);\r\n }\r\n }\r\n\r\n const handler = service.instance[metadata.displayName];\r\n if (typeof handler !== 'function') {\r\n throw new Error(`Callable ${metadata.displayName} is not a function on service ${service.definition.name}.`);\r\n }\r\n\r\n const args = this.buildInvocationArguments(handler as (...args: unknown[]) => unknown, ctx);\r\n const result = await Promise.resolve((handler as (...args: unknown[]) => unknown).apply(service.instance, args));\r\n await this.sendSuccessResponse(ctx, metadata, result);\r\n }\r\n\r\n private buildInvocationArguments(\r\n handler: (...args: unknown[]) => unknown,\r\n ctx: RequestContext,\r\n ): unknown[] {\r\n const payload = Array.isArray(ctx.payload)\r\n ? ctx.payload\r\n : typeof ctx.payload === 'string'\r\n ? [ctx.payload]\r\n : ctx.payload === undefined\r\n ? []\r\n : [ctx.payload];\r\n\r\n const expectedParams = handler.length;\r\n if (expectedParams === payload.length + 1) {\r\n return [ctx, ...payload];\r\n }\r\n return payload;\r\n }\r\n\r\n private async sendSuccessResponse(ctx: RequestContext, metadata: ServiceCallableMetadata, result: unknown): Promise<void> {\r\n let body: unknown;\r\n if (metadata.useLegacySerialization) {\r\n const serialized = typeof result === 'string' ? result : JSON.stringify(result ?? null);\r\n body = { payload: serialized };\r\n } else {\r\n body = result ?? null;\r\n }\r\n\r\n const response: GatewayResponse = {\r\n id: ctx.id,\r\n status: 200,\r\n body,\r\n };\r\n await this.requester.sendResponse(response);\r\n }\r\n\r\n private async sendFederationResponse(ctx: RequestContext, result: unknown): Promise<void> {\r\n const response: GatewayResponse = {\r\n id: ctx.id,\r\n status: 200,\r\n body: result ?? null,\r\n };\r\n await this.requester.sendResponse(response);\r\n }\r\n\r\n private async tryHandleFederationRoute(ctx: RequestContext, service: ServiceInstance): Promise<boolean> {\r\n // Gateway sends paths with lowercase service names, so we need case-insensitive matching\r\n const pathLower = ctx.path.toLowerCase();\r\n const qualifiedNameLower = service.definition.qualifiedName.toLowerCase();\r\n const prefixLower = `${qualifiedNameLower}/`;\r\n if (!pathLower.startsWith(prefixLower)) {\r\n return false;\r\n }\r\n // Extract relative path - use lowercase length since gateway sends lowercase paths\r\n const relativePath = ctx.path.substring(qualifiedNameLower.length + 1);\r\n const handler = service.federationRegistry.resolve(relativePath);\r\n if (!handler) {\r\n return false;\r\n }\r\n\r\n const result = await handler.invoke(ctx as FederatedRequestContext);\r\n await this.sendFederationResponse(ctx, result);\r\n return true;\r\n }\r\n\r\n private async tryHandleAdminRoute(ctx: RequestContext, service: ServiceInstance): Promise<boolean> {\r\n // Gateway sends paths with lowercase service names, so we need case-insensitive matching\r\n // Check if path starts with the admin prefix (case-insensitive)\r\n const pathLower = ctx.path.toLowerCase();\r\n const adminPrefixLower = `${service.definition.qualifiedName.toLowerCase()}/admin/`;\r\n if (!pathLower.startsWith(adminPrefixLower)) {\r\n return false;\r\n }\r\n\r\n const options = getServiceOptions(service.definition.ctor) ?? {};\r\n\r\n const action = pathLower.substring(adminPrefixLower.length);\r\n const requiresAdmin = action === 'metadata' || action === 'docs';\r\n if (requiresAdmin && !scopeSetHas(ctx.scopes, 'admin')) {\r\n // For portal requests to admin endpoints, the gateway may not send scopes\r\n // The X-DE-SCOPE header contains CID.PID, not scope values\r\n // If this is a portal request (has X-DE-SCOPE header), grant admin scope\r\n const hasPortalHeader = ctx.headers['X-DE-SCOPE'] || ctx.headers['x-de-scope'];\r\n if (hasPortalHeader) {\r\n // Grant admin scope for portal requests to admin endpoints\r\n ctx.scopes.add('admin');\r\n } else {\r\n throw new MissingScopesError(['admin']);\r\n }\r\n }\r\n\r\n switch (action) {\r\n case 'health':\r\n case 'healthcheck':\r\n await this.requester.sendResponse({ id: ctx.id, status: 200, body: 'responsive' });\r\n return true;\r\n case 'metadata':\r\n case 'docs':\r\n break;\r\n default:\r\n if (!scopeSetHas(ctx.scopes, 'admin')) {\r\n throw new MissingScopesError(['admin']);\r\n }\r\n throw new UnknownRouteError(ctx.path);\r\n }\r\n\r\n if (action === 'metadata') {\r\n const federatedComponents = service.federationRegistry\r\n .list()\r\n .map((component) => ({\r\n federationNamespace: component.federationNamespace,\r\n federationType: component.federationType,\r\n }));\r\n\r\n await this.requester.sendResponse({\r\n id: ctx.id,\r\n status: 200,\r\n body: {\r\n serviceName: service.definition.name,\r\n sdkVersion: this.env.sdkVersionExecution,\r\n sdkBaseBuildVersion: this.env.sdkVersionExecution,\r\n sdkExecutionVersion: this.env.sdkVersionExecution,\r\n useLegacySerialization: Boolean(options.useLegacySerialization),\r\n disableAllBeamableEvents: Boolean(options.disableAllBeamableEvents),\r\n enableEagerContentLoading: false,\r\n instanceId: this.microServiceId,\r\n routingKey: this.env.routingKey ?? '',\r\n federatedComponents,\r\n },\r\n });\r\n return true;\r\n }\r\n\r\n if (action === 'docs') {\r\n try {\r\n const document = generateOpenApiDocument(\r\n {\r\n qualifiedName: service.definition.qualifiedName,\r\n name: service.definition.name,\r\n callables: Array.from(service.definition.callables.values()).map((callable) => ({\r\n name: callable.displayName,\r\n route: callable.route,\r\n metadata: callable,\r\n handler: typeof service.instance[callable.displayName] === 'function'\r\n ? (service.instance[callable.displayName] as (...args: unknown[]) => unknown)\r\n : undefined,\r\n })),\r\n },\r\n this.env,\r\n );\r\n\r\n // Ensure document is valid (not empty)\r\n if (!document || Object.keys(document).length === 0) {\r\n this.logger.warn({ serviceName: service.definition.name }, 'Generated OpenAPI document is empty');\r\n }\r\n\r\n await this.requester.sendResponse({\r\n id: ctx.id,\r\n status: 200,\r\n body: document,\r\n });\r\n return true;\r\n } catch (error) {\r\n this.logger.error({ err: error, serviceName: service.definition.name }, 'Failed to generate or send docs');\r\n throw error;\r\n }\r\n }\r\n\r\n return false;\r\n }\r\n\r\n private resolveErrorStatus(error: Error): number {\r\n if (error instanceof UnauthorizedUserError) {\r\n return 401;\r\n }\r\n if (error instanceof MissingScopesError) {\r\n return 403;\r\n }\r\n if (error instanceof UnknownRouteError) {\r\n return 404;\r\n }\r\n if (error instanceof BeamableRuntimeError) {\r\n return 500;\r\n }\r\n return 500;\r\n }\r\n\r\n private printHelpfulUrls(service?: ServiceInstance): void {\r\n if (!service) {\r\n return;\r\n }\r\n\r\n // Only print helpful URLs in local development (not in containers)\r\n // In containers, we want only JSON logs for proper log collection\r\n if (isRunningInContainer()) {\r\n return;\r\n }\r\n\r\n const docsUrl = buildDocsPortalUrl(this.env, service.definition);\r\n const endpointBase = buildPostmanBaseUrl(this.env, service.definition);\r\n\r\n const green = (text: string) => `\\x1b[32m${text}\\x1b[0m`;\r\n const yellow = (text: string) => `\\x1b[33m${text}\\x1b[0m`;\r\n\r\n const bannerLines = [\r\n '',\r\n yellow('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'),\r\n ` ${green('Beamable Node microservice ready:')} ${green(service.definition.name)}`,\r\n yellow(' Quick shortcuts'),\r\n ` ${yellow('Docs:')} ${docsUrl}`,\r\n ` ${yellow('Endpoint:')} ${endpointBase}`,\r\n yellow('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'),\r\n '',\r\n ];\r\n\r\n process.stdout.write(`${bannerLines.join('\\n')}`);\r\n }\r\n\r\n private async initializeDependencyProviders(): Promise<void> {\r\n for (const service of this.services) {\r\n const builder = new DependencyBuilder();\r\n builder.tryAddSingletonInstance(LOGGER_TOKEN, service.logger);\r\n builder.tryAddSingletonInstance(ENVIRONMENT_CONFIG_TOKEN, this.env);\r\n builder.tryAddSingletonInstance(BeamableServiceManager, this.serviceManager);\r\n builder.tryAddSingletonInstance(service.definition.ctor, service.instance);\r\n builder.tryAddSingletonInstance(FederationRegistry, service.federationRegistry);\r\n\r\n for (const handler of service.configureHandlers) {\r\n await handler(builder);\r\n }\r\n\r\n const provider = builder.build();\r\n service.provider = provider;\r\n\r\n for (const handler of service.initializeHandlers) {\r\n await handler(provider);\r\n }\r\n\r\n Object.defineProperty(service.instance, 'provider', {\r\n value: provider,\r\n enumerable: false,\r\n configurable: false,\r\n writable: false,\r\n });\r\n }\r\n }\r\n\r\n private createRequestScope(path: string, service?: ServiceInstance): MutableDependencyScope {\r\n const targetService = service ?? this.findServiceForPath(path);\r\n const provider = targetService?.provider ?? new DependencyBuilder().build();\r\n return provider.createScope();\r\n }\r\n}\r\n\r\nfunction enforceAccess(access: ServiceAccess, ctx: RequestContext): void {\r\n switch (access) {\r\n case 'client':\r\n if (ctx.userId <= 0) {\r\n throw new UnauthorizedUserError(ctx.path);\r\n }\r\n break;\r\n case 'server':\r\n if (!scopeSetHas(ctx.scopes, 'server')) {\r\n throw new MissingScopesError(['server']);\r\n }\r\n break;\r\n case 'admin':\r\n if (!scopeSetHas(ctx.scopes, 'admin')) {\r\n throw new MissingScopesError(['admin']);\r\n }\r\n break;\r\n default:\r\n break;\r\n }\r\n}\r\n\r\n/**\r\n * Conditionally output to console.error only when running locally (not in container).\r\n * In containers, we want only Pino JSON logs to stdout for proper log collection.\r\n */\r\nfunction debugLog(...args: unknown[]): void {\r\n // Check process.env directly since this is a standalone function\r\n if (!isRunningInContainer()) {\r\n console.error(...args);\r\n }\r\n}\r\n\r\nfunction isRunningInContainer(): boolean {\r\n // Check for Docker container first (most reliable indicator)\r\n // Docker creates /.dockerenv file in containers\r\n try {\r\n const fs = require('fs');\r\n if (fs.existsSync('/.dockerenv')) {\r\n return true; // Definitely in Docker\r\n }\r\n } catch {\r\n // fs might not be available, continue with other checks\r\n }\r\n \r\n // Check for Docker container hostname pattern\r\n // Docker containers often have hostnames that are just hex strings (12 chars)\r\n const hostname = process.env.HOSTNAME || '';\r\n if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {\r\n // This looks like a Docker container ID as hostname\r\n return true;\r\n }\r\n \r\n // Explicit container indicators (highest priority after Docker checks)\r\n if (\r\n process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||\r\n process.env.CONTAINER === 'beamable' ||\r\n !!process.env.ECS_CONTAINER_METADATA_URI ||\r\n !!process.env.KUBERNETES_SERVICE_HOST\r\n ) {\r\n return true;\r\n }\r\n \r\n // If we have Beamable env vars AND no routing key from process.env (not auto-generated)\r\n // This indicates a deployed container (Beamable sets these but not routing key)\r\n const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);\r\n const hasExplicitRoutingKey = !!(process.env.NAME_PREFIX || process.env.ROUTING_KEY);\r\n \r\n if (hasBeamableEnvVars && !hasExplicitRoutingKey) {\r\n // We have Beamable env vars but no explicit routing key from env\r\n // This means we're in a deployed container (routing key would be auto-generated, but we're in container)\r\n return true;\r\n }\r\n \r\n // Default: assume local development (npm run dev)\r\n return false;\r\n}\r\n\r\nfunction normalizeScopes(scopes: Array<unknown>): Set<string> {\r\n const normalized = new Set<string>();\r\n for (const scope of scopes) {\r\n if (typeof scope !== 'string' || scope.trim().length === 0) {\r\n continue;\r\n }\r\n normalized.add(scope.trim().toLowerCase());\r\n }\r\n return normalized;\r\n}\r\n\r\nfunction scopeSetHas(scopes: Set<string>, scope: string): boolean {\r\n const normalized = scope.trim().toLowerCase();\r\n if (normalized.length === 0) {\r\n return false;\r\n }\r\n return scopes.has('*') || scopes.has(normalized);\r\n}\r\n\r\nfunction buildPostmanBaseUrl(env: EnvironmentConfig, service: ServiceDefinition): string {\r\n const httpHost = hostToHttpUrl(env.host);\r\n const routingKeyPart = env.routingKey ? env.routingKey : '';\r\n return `${httpHost}/basic/${env.cid}.${env.pid}.${routingKeyPart}${service.qualifiedName}/`;\r\n}\r\n\r\nfunction buildDocsPortalUrl(env: EnvironmentConfig, service: ServiceDefinition): string {\r\n const portalHost = hostToPortalUrl(hostToHttpUrl(env.host));\r\n const queryParams: Record<string, string> = { srcTool: 'node-runtime' };\r\n if (env.routingKey) {\r\n queryParams.routingKey = env.routingKey;\r\n }\r\n const query = new URLSearchParams(queryParams);\r\n if (env.refreshToken) {\r\n query.set('refresh_token', env.refreshToken);\r\n }\r\n const beamoId = service.name;\r\n return `${portalHost}/${env.cid}/games/${env.pid}/realms/${env.pid}/microservices/micro_${beamoId}/docs?${query.toString()}`;\r\n}\r\n\r\nexport async function runMicroservice(): Promise<void> {\r\n // Immediate console output to verify process is starting (local dev only)\r\n // In containers, we rely on Pino logger for all output\r\n debugLog('[BEAMABLE-NODE] Starting microservice...');\r\n debugLog(`[BEAMABLE-NODE] Node version: ${process.version}`);\r\n debugLog(`[BEAMABLE-NODE] Working directory: ${process.cwd()}`);\r\n debugLog(`[BEAMABLE-NODE] Environment: ${JSON.stringify({\r\n NODE_ENV: process.env.NODE_ENV,\r\n CID: process.env.CID ? 'SET' : 'NOT SET',\r\n PID: process.env.PID ? 'SET' : 'NOT SET',\r\n HOST: process.env.HOST ? 'SET' : 'NOT SET',\r\n SECRET: process.env.SECRET ? 'SET' : 'NOT SET',\r\n })}`);\r\n if (process.env.BEAMABLE_SKIP_RUNTIME === 'true') {\r\n return;\r\n }\r\n const runtime = new MicroserviceRuntime();\r\n \r\n // Handle uncaught errors - log them but don't crash immediately\r\n // This allows the health check server to keep running so we can debug\r\n // In containers, errors will be logged by the logger in the runtime\r\n // Locally, use console.error for visibility\r\n process.on('uncaughtException', (error) => {\r\n // In containers, the logger will handle this; locally, show in console\r\n debugLog('Uncaught Exception:', error);\r\n // Don't exit - let the health check server keep running\r\n });\r\n \r\n process.on('unhandledRejection', (reason, promise) => {\r\n // In containers, the logger will handle this; locally, show in console\r\n debugLog('Unhandled Rejection at:', promise, 'reason:', reason);\r\n // Don't exit - let the health check server keep running\r\n });\r\n \r\n try {\r\n await runtime.start();\r\n } catch (error) {\r\n // Log the error but don't exit - health check server is running\r\n // This allows the container to stay alive so we can see what went wrong\r\n // In containers, the logger already logged it in start(); locally, also use console.error\r\n debugLog('Failed to start microservice runtime:', error);\r\n // Keep the process alive so health check can continue\r\n // The health check will return 503 until isReady is true\r\n }\r\n \r\n const shutdownSignals: NodeJS.Signals[] = ['SIGTERM', 'SIGINT'];\r\n shutdownSignals.forEach((signal) => {\r\n process.once(signal, async () => {\r\n await runtime.shutdown();\r\n process.exit(0);\r\n });\r\n });\r\n}\r\n\r\ninterface GenerateOpenApiOptions {\r\n serviceName?: string;\r\n}\r\n\r\nexport function generateOpenApiDocumentForRegisteredServices(\r\n overrides: Partial<EnvironmentConfig> = {},\r\n options: GenerateOpenApiOptions = {},\r\n): unknown {\r\n const services = listRegisteredServices();\r\n if (services.length === 0) {\r\n throw new Error('No microservices registered. Import your service module before generating documentation.');\r\n }\r\n\r\n const baseEnv = buildEnvironmentConfig(overrides);\r\n const primary = options.serviceName\r\n ? services.find((svc) => svc.name === options.serviceName || svc.qualifiedName === options.serviceName)\r\n : services[0];\r\n\r\n if (!primary) {\r\n throw new Error(`No registered microservice matched '${options.serviceName}'.`);\r\n }\r\n\r\n return generateOpenApiDocument(\r\n {\r\n qualifiedName: primary.qualifiedName,\r\n name: primary.name,\r\n callables: Array.from(primary.callables.entries()).map(([displayName, metadata]) => ({\r\n name: displayName,\r\n route: metadata.route,\r\n metadata,\r\n handler: undefined,\r\n })),\r\n },\r\n baseEnv,\r\n );\r\n}\r\n\r\nfunction buildEnvironmentConfig(overrides: Partial<EnvironmentConfig>): EnvironmentConfig {\r\n const merged: Partial<EnvironmentConfig> = { ...overrides };\r\n\r\n const ensure = (key: keyof EnvironmentConfig, fallbackEnv?: string) => {\r\n if (merged[key] !== undefined) {\r\n return;\r\n }\r\n if (fallbackEnv && process.env[fallbackEnv]) {\r\n merged[key] = process.env[fallbackEnv] as never;\r\n }\r\n };\r\n\r\n ensure('cid', 'CID');\r\n ensure('pid', 'PID');\r\n ensure('host', 'HOST');\r\n ensure('secret', 'SECRET');\r\n ensure('refreshToken', 'REFRESH_TOKEN');\r\n // Routing key is optional for deployed services (in containers)\r\n // It will be resolved to empty string if not provided and running in container\r\n if (!merged.routingKey && !isRunningInContainer()) {\r\n ensure('routingKey', 'NAME_PREFIX');\r\n }\r\n\r\n if (!merged.cid || !merged.pid || !merged.host) {\r\n throw new Error('CID, PID, and HOST are required to generate documentation.');\r\n }\r\n\r\n const base = loadEnvironmentConfig();\r\n\r\n return {\r\n ...base,\r\n ...merged,\r\n routingKey: merged.routingKey ?? base.routingKey,\r\n };\r\n}\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omen.foundation/node-microservice-runtime",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Beamable microservice runtime for Node.js/TypeScript services.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/logger.ts CHANGED
@@ -23,16 +23,35 @@ function getRequire(): any {
23
23
  * This is used to determine log format: pretty for local dev, JSON for containers.
24
24
  *
25
25
  * Key indicators we're in a container:
26
- * - Has CID, PID, HOST, SECRET (Beamable env vars)
27
- * - NO routing key (deployed services don't use routing keys)
28
- * - OR explicit container indicators (DOTNET_RUNNING_IN_CONTAINER, ECS, K8s)
26
+ * - Docker: .dockerenv file exists, or HOSTNAME matches container pattern
27
+ * - Explicit container indicators (DOTNET_RUNNING_IN_CONTAINER, ECS, K8s)
28
+ * - Has CID, PID, HOST, SECRET (Beamable env vars) AND no routing key from env vars
29
29
  *
30
30
  * Key indicators we're running locally:
31
- * - Has routing key (local dev always has a routing key)
32
31
  * - Running via tsx/node directly (not in Docker)
32
+ * - No container indicators present
33
33
  */
34
- function isRunningInContainer(env?: EnvironmentConfig): boolean {
35
- // Explicit container indicators (highest priority)
34
+ function isRunningInContainer(): boolean {
35
+ // Check for Docker container first (most reliable indicator)
36
+ // Docker creates /.dockerenv file in containers
37
+ try {
38
+ const fs = require('fs');
39
+ if (fs.existsSync('/.dockerenv')) {
40
+ return true; // Definitely in Docker
41
+ }
42
+ } catch {
43
+ // fs might not be available, continue with other checks
44
+ }
45
+
46
+ // Check for Docker container hostname pattern
47
+ // Docker containers often have hostnames that are just hex strings (12 chars)
48
+ const hostname = process.env.HOSTNAME || '';
49
+ if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {
50
+ // This looks like a Docker container ID as hostname
51
+ return true;
52
+ }
53
+
54
+ // Explicit container indicators (highest priority after Docker checks)
36
55
  if (
37
56
  process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
38
57
  process.env.CONTAINER === 'beamable' ||
@@ -42,20 +61,18 @@ function isRunningInContainer(env?: EnvironmentConfig): boolean {
42
61
  return true;
43
62
  }
44
63
 
45
- // If we have a routing key (from env object or process.env), we're definitely running locally
46
- // Local dev always has a routing key (either set explicitly or auto-generated)
47
- const hasRoutingKey = env?.routingKey || process.env.NAME_PREFIX || process.env.ROUTING_KEY;
48
- if (hasRoutingKey) {
49
- return false; // Local development
50
- }
51
-
52
- // If we have Beamable env vars but NO routing key, we're in a deployed container
64
+ // If we have Beamable env vars AND no routing key from process.env (not auto-generated)
65
+ // This indicates a deployed container (Beamable sets these but not routing key)
53
66
  const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
54
- if (hasBeamableEnvVars) {
55
- return true; // Deployed container (has env vars but no routing key)
67
+ const hasExplicitRoutingKey = !!(process.env.NAME_PREFIX || process.env.ROUTING_KEY);
68
+
69
+ if (hasBeamableEnvVars && !hasExplicitRoutingKey) {
70
+ // We have Beamable env vars but no explicit routing key from env
71
+ // This means we're in a deployed container (routing key would be auto-generated, but we're in container)
72
+ return true;
56
73
  }
57
74
 
58
- // Default: assume local development
75
+ // Default: assume local development (npm run dev)
59
76
  return false;
60
77
  }
61
78
 
@@ -184,7 +201,7 @@ function createBeamableLogFormatter(): Transform {
184
201
 
185
202
  export function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptions = {}): Logger {
186
203
  const configuredDestination = options.destinationPath ?? process.env.LOG_PATH;
187
- const inContainer = isRunningInContainer(env);
204
+ const inContainer = isRunningInContainer();
188
205
 
189
206
  const pinoOptions: LoggerOptions = {
190
207
  name: options.name ?? 'beamable-node-runtime',
package/src/runtime.ts CHANGED
@@ -92,7 +92,7 @@ export class MicroserviceRuntime {
92
92
  this.authManager = new AuthManager(this.env, this.requester);
93
93
  this.requester.on('event', (envelope) => this.handleEvent(envelope));
94
94
 
95
- if (!isRunningInContainer(this.env) && this.services.length > 0) {
95
+ if (!isRunningInContainer() && this.services.length > 0) {
96
96
  this.discovery = new DiscoveryBroadcaster({
97
97
  env: this.env,
98
98
  serviceName: this.services[0].definition.name,
@@ -255,7 +255,7 @@ export class MicroserviceRuntime {
255
255
  // The gateway's ServiceIdentity.fullNameNoType lowercases the service name when creating bindings,
256
256
  // but the registration request should use the original case (as C# does)
257
257
  // This ensures the service name in the registration matches what the portal expects
258
- const isDeployed = isRunningInContainer(this.env);
258
+ const isDeployed = isRunningInContainer();
259
259
 
260
260
  // For deployed services, routingKey should be null/undefined (None in Scala)
261
261
  // For local dev, routingKey should be the actual routing key string
@@ -709,7 +709,7 @@ export class MicroserviceRuntime {
709
709
 
710
710
  // Only print helpful URLs in local development (not in containers)
711
711
  // In containers, we want only JSON logs for proper log collection
712
- if (isRunningInContainer(this.env)) {
712
+ if (isRunningInContainer()) {
713
713
  return;
714
714
  }
715
715
 
@@ -802,8 +802,27 @@ function debugLog(...args: unknown[]): void {
802
802
  }
803
803
  }
804
804
 
805
- function isRunningInContainer(env?: EnvironmentConfig): boolean {
806
- // Explicit container indicators (highest priority)
805
+ function isRunningInContainer(): boolean {
806
+ // Check for Docker container first (most reliable indicator)
807
+ // Docker creates /.dockerenv file in containers
808
+ try {
809
+ const fs = require('fs');
810
+ if (fs.existsSync('/.dockerenv')) {
811
+ return true; // Definitely in Docker
812
+ }
813
+ } catch {
814
+ // fs might not be available, continue with other checks
815
+ }
816
+
817
+ // Check for Docker container hostname pattern
818
+ // Docker containers often have hostnames that are just hex strings (12 chars)
819
+ const hostname = process.env.HOSTNAME || '';
820
+ if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {
821
+ // This looks like a Docker container ID as hostname
822
+ return true;
823
+ }
824
+
825
+ // Explicit container indicators (highest priority after Docker checks)
807
826
  if (
808
827
  process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
809
828
  process.env.CONTAINER === 'beamable' ||
@@ -813,20 +832,18 @@ function isRunningInContainer(env?: EnvironmentConfig): boolean {
813
832
  return true;
814
833
  }
815
834
 
816
- // If we have a routing key (from env object or process.env), we're definitely running locally
817
- // Local dev always has a routing key (either set explicitly or auto-generated)
818
- const hasRoutingKey = env?.routingKey || process.env.NAME_PREFIX || process.env.ROUTING_KEY;
819
- if (hasRoutingKey) {
820
- return false; // Local development
821
- }
822
-
823
- // If we have Beamable env vars but NO routing key, we're in a deployed container
835
+ // If we have Beamable env vars AND no routing key from process.env (not auto-generated)
836
+ // This indicates a deployed container (Beamable sets these but not routing key)
824
837
  const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
825
- if (hasBeamableEnvVars) {
826
- return true; // Deployed container (has env vars but no routing key)
838
+ const hasExplicitRoutingKey = !!(process.env.NAME_PREFIX || process.env.ROUTING_KEY);
839
+
840
+ if (hasBeamableEnvVars && !hasExplicitRoutingKey) {
841
+ // We have Beamable env vars but no explicit routing key from env
842
+ // This means we're in a deployed container (routing key would be auto-generated, but we're in container)
843
+ return true;
827
844
  }
828
845
 
829
- // Default: assume local development
846
+ // Default: assume local development (npm run dev)
830
847
  return false;
831
848
  }
832
849