@omen.foundation/node-microservice-runtime 0.1.11 → 0.1.12

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
@@ -65,7 +65,7 @@ function mapPinoLevelToBeamableLevel(level) {
65
65
  return 'Info';
66
66
  }
67
67
  }
68
- function createBeamableLogFormatter() {
68
+ function createBeamableLogFormatter(serviceName, qualifiedServiceName) {
69
69
  return new node_stream_1.Transform({
70
70
  objectMode: false,
71
71
  transform(chunk, _encoding, callback) {
@@ -113,6 +113,12 @@ function createBeamableLogFormatter() {
113
113
  contextFields.service = pinoLog.service;
114
114
  if (pinoLog.component)
115
115
  contextFields.component = pinoLog.component;
116
+ if (serviceName) {
117
+ contextFields.serviceName = serviceName;
118
+ }
119
+ if (qualifiedServiceName) {
120
+ contextFields.qualifiedServiceName = qualifiedServiceName;
121
+ }
116
122
  const standardPinoFields = ['level', 'time', 'pid', 'hostname', 'name', 'msg', 'err', 'v', 'cid', 'pid', 'routingKey', 'sdkVersionExecution', 'service', 'component'];
117
123
  for (const [key, value] of Object.entries(pinoLog)) {
118
124
  if (!standardPinoFields.includes(key) && value !== undefined && value !== null) {
@@ -122,6 +128,54 @@ function createBeamableLogFormatter() {
122
128
  if (Object.keys(contextFields).length > 0) {
123
129
  beamableLog.__c = contextFields;
124
130
  }
131
+ const otelFields = {};
132
+ const resourceAttributes = {};
133
+ if (qualifiedServiceName) {
134
+ resourceAttributes['service.namespace'] = qualifiedServiceName;
135
+ }
136
+ if (serviceName) {
137
+ resourceAttributes['service.name'] = serviceName;
138
+ }
139
+ if (pinoLog.cid) {
140
+ resourceAttributes['beam.cid'] = String(pinoLog.cid);
141
+ }
142
+ if (pinoLog.pid) {
143
+ resourceAttributes['beam.pid'] = String(pinoLog.pid);
144
+ }
145
+ if (pinoLog.routingKey) {
146
+ resourceAttributes['beam.routing_key'] = String(pinoLog.routingKey);
147
+ }
148
+ const logAttributes = {};
149
+ if (pinoLog.component) {
150
+ logAttributes['component'] = String(pinoLog.component);
151
+ }
152
+ if (pinoLog.err) {
153
+ const err = pinoLog.err;
154
+ if (err.message)
155
+ logAttributes['exception.message'] = String(err.message);
156
+ if (err.stack)
157
+ logAttributes['exception.stacktrace'] = String(err.stack);
158
+ if (err.type)
159
+ logAttributes['exception.type'] = String(err.type);
160
+ }
161
+ const severityTextMap = {
162
+ 'Debug': 'Debug',
163
+ 'Info': 'Information',
164
+ 'Warning': 'Warning',
165
+ 'Error': 'Error',
166
+ 'Fatal': 'Critical',
167
+ };
168
+ const severityText = severityTextMap[level] || 'Information';
169
+ otelFields['Timestamp'] = timestamp;
170
+ otelFields['SeverityText'] = severityText;
171
+ otelFields['Body'] = messageParts.length > 0 ? messageParts.join(' ') : 'No message';
172
+ if (Object.keys(resourceAttributes).length > 0) {
173
+ otelFields['ResourceAttributes'] = resourceAttributes;
174
+ }
175
+ if (Object.keys(logAttributes).length > 0) {
176
+ otelFields['LogAttributes'] = logAttributes;
177
+ }
178
+ Object.assign(beamableLog, otelFields);
125
179
  const output = JSON.stringify(beamableLog) + '\n';
126
180
  callback(null, Buffer.from(output, 'utf8'));
127
181
  }
@@ -148,6 +202,8 @@ function createLogger(env, options = {}) {
148
202
  pid: env.pid,
149
203
  routingKey: (_c = env.routingKey) !== null && _c !== void 0 ? _c : null,
150
204
  sdkVersionExecution: env.sdkVersionExecution,
205
+ serviceName: options.serviceName,
206
+ qualifiedServiceName: options.qualifiedServiceName,
151
207
  },
152
208
  redact: {
153
209
  paths: ['secret', 'refreshToken'],
@@ -157,7 +213,7 @@ function createLogger(env, options = {}) {
157
213
  };
158
214
  if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {
159
215
  if (!usePrettyLogs) {
160
- const beamableFormatter = createBeamableLogFormatter();
216
+ const beamableFormatter = createBeamableLogFormatter(options.serviceName, options.qualifiedServiceName);
161
217
  beamableFormatter.pipe(process.stdout);
162
218
  return (0, pino_1.default)(pinoOptions, beamableFormatter);
163
219
  }
@@ -180,7 +236,7 @@ function createLogger(env, options = {}) {
180
236
  }
181
237
  const resolvedDestination = configuredDestination === 'temp' ? (0, env_js_1.ensureWritableTempDirectory)() : configuredDestination;
182
238
  if (!usePrettyLogs) {
183
- const beamableFormatter = createBeamableLogFormatter();
239
+ const beamableFormatter = createBeamableLogFormatter(options.serviceName, options.qualifiedServiceName);
184
240
  const fileStream = (0, pino_1.destination)({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
185
241
  beamableFormatter.pipe(fileStream);
186
242
  return (0, pino_1.default)(pinoOptions, beamableFormatter);
package/dist/logger.d.ts CHANGED
@@ -3,6 +3,8 @@ import type { EnvironmentConfig } from './types.js';
3
3
  interface LoggerFactoryOptions {
4
4
  name?: string;
5
5
  destinationPath?: string;
6
+ serviceName?: string;
7
+ qualifiedServiceName?: string;
6
8
  }
7
9
  export declare function createLogger(env: EnvironmentConfig, options?: LoggerFactoryOptions): Logger;
8
10
  export {};
@@ -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;AA2BpD,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;AA2BpD,UAAU,oBAAoB;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B;AAiMD,wBAAgB,YAAY,CAAC,GAAG,EAAE,iBAAiB,EAAE,OAAO,GAAE,oBAAyB,GAAG,MAAM,CAsE/F"}
package/dist/logger.js CHANGED
@@ -49,9 +49,10 @@ function mapPinoLevelToBeamableLevel(level) {
49
49
  /**
50
50
  * Creates a transform stream that converts Pino JSON logs to Beamable's expected format.
51
51
  * Beamable expects logs with __t (timestamp), __l (level), and __m (message) fields.
52
+ * Also includes OpenTelemetry-compatible fields for ClickHouse compatibility.
52
53
  * Pino writes JSON strings (one per line) to the stream.
53
54
  */
54
- function createBeamableLogFormatter() {
55
+ function createBeamableLogFormatter(serviceName, qualifiedServiceName) {
55
56
  return new Transform({
56
57
  objectMode: false, // Pino writes strings, not objects
57
58
  transform(chunk, _encoding, callback) {
@@ -91,7 +92,7 @@ function createBeamableLogFormatter() {
91
92
  const errStack = err.stack ? `\n${err.stack}` : '';
92
93
  messageParts.push(`${errMsg}${errStack}`);
93
94
  }
94
- // Build the Beamable log format
95
+ // Build the Beamable log format (for CloudWatch Logs Insights)
95
96
  const beamableLog = {
96
97
  __t: timestamp,
97
98
  __l: level,
@@ -110,6 +111,13 @@ function createBeamableLogFormatter() {
110
111
  contextFields.service = pinoLog.service;
111
112
  if (pinoLog.component)
112
113
  contextFields.component = pinoLog.component;
114
+ // Include service name in context for CloudWatch filtering
115
+ if (serviceName) {
116
+ contextFields.serviceName = serviceName;
117
+ }
118
+ if (qualifiedServiceName) {
119
+ contextFields.qualifiedServiceName = qualifiedServiceName;
120
+ }
113
121
  // Include any other fields that aren't standard Pino fields
114
122
  const standardPinoFields = ['level', 'time', 'pid', 'hostname', 'name', 'msg', 'err', 'v', 'cid', 'pid', 'routingKey', 'sdkVersionExecution', 'service', 'component'];
115
123
  for (const [key, value] of Object.entries(pinoLog)) {
@@ -121,6 +129,67 @@ function createBeamableLogFormatter() {
121
129
  if (Object.keys(contextFields).length > 0) {
122
130
  beamableLog.__c = contextFields;
123
131
  }
132
+ // Add OpenTelemetry-compatible fields for ClickHouse compatibility
133
+ // These fields allow an OpenTelemetry collector to parse and forward logs to ClickHouse
134
+ // The Portal's realm-level logs page queries ClickHouse's otel_logs table
135
+ const otelFields = {};
136
+ // ResourceAttributes - service identification (for ClickHouse filtering)
137
+ const resourceAttributes = {};
138
+ if (qualifiedServiceName) {
139
+ // ClickHouse expects service.namespace to match the service name format
140
+ resourceAttributes['service.namespace'] = qualifiedServiceName;
141
+ }
142
+ if (serviceName) {
143
+ resourceAttributes['service.name'] = serviceName;
144
+ }
145
+ if (pinoLog.cid) {
146
+ resourceAttributes['beam.cid'] = String(pinoLog.cid);
147
+ }
148
+ if (pinoLog.pid) {
149
+ resourceAttributes['beam.pid'] = String(pinoLog.pid);
150
+ }
151
+ if (pinoLog.routingKey) {
152
+ resourceAttributes['beam.routing_key'] = String(pinoLog.routingKey);
153
+ }
154
+ // LogAttributes - log-specific attributes
155
+ const logAttributes = {};
156
+ if (pinoLog.component) {
157
+ logAttributes['component'] = String(pinoLog.component);
158
+ }
159
+ if (pinoLog.err) {
160
+ const err = pinoLog.err;
161
+ if (err.message)
162
+ logAttributes['exception.message'] = String(err.message);
163
+ if (err.stack)
164
+ logAttributes['exception.stacktrace'] = String(err.stack);
165
+ if (err.type)
166
+ logAttributes['exception.type'] = String(err.type);
167
+ }
168
+ // Map Beamable log level to OpenTelemetry SeverityText
169
+ // Beamable: Debug, Info, Warning, Error, Fatal
170
+ // OpenTelemetry: Trace, Debug, Info, Warn, Error, Fatal, Unspecified
171
+ const severityTextMap = {
172
+ 'Debug': 'Debug',
173
+ 'Info': 'Information',
174
+ 'Warning': 'Warning',
175
+ 'Error': 'Error',
176
+ 'Fatal': 'Critical',
177
+ };
178
+ const severityText = severityTextMap[level] || 'Information';
179
+ // Add OpenTelemetry fields to the log
180
+ // These are in addition to the Beamable format, so both systems can parse the logs
181
+ otelFields['Timestamp'] = timestamp; // OpenTelemetry timestamp format
182
+ otelFields['SeverityText'] = severityText;
183
+ otelFields['Body'] = messageParts.length > 0 ? messageParts.join(' ') : 'No message';
184
+ if (Object.keys(resourceAttributes).length > 0) {
185
+ otelFields['ResourceAttributes'] = resourceAttributes;
186
+ }
187
+ if (Object.keys(logAttributes).length > 0) {
188
+ otelFields['LogAttributes'] = logAttributes;
189
+ }
190
+ // Merge OpenTelemetry fields into the log (for ClickHouse compatibility)
191
+ // An OpenTelemetry collector can parse these fields and forward to ClickHouse
192
+ Object.assign(beamableLog, otelFields);
124
193
  // Output as a single-line JSON string (required for CloudWatch)
125
194
  const output = JSON.stringify(beamableLog) + '\n';
126
195
  callback(null, Buffer.from(output, 'utf8'));
@@ -148,6 +217,9 @@ export function createLogger(env, options = {}) {
148
217
  pid: env.pid,
149
218
  routingKey: env.routingKey ?? null,
150
219
  sdkVersionExecution: env.sdkVersionExecution,
220
+ // Include service name in base fields for filtering
221
+ serviceName: options.serviceName,
222
+ qualifiedServiceName: options.qualifiedServiceName,
151
223
  },
152
224
  redact: {
153
225
  paths: ['secret', 'refreshToken'],
@@ -161,7 +233,8 @@ export function createLogger(env, options = {}) {
161
233
  if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {
162
234
  if (!usePrettyLogs) {
163
235
  // Deployed/remote: Use Beamable JSON format for log collection
164
- const beamableFormatter = createBeamableLogFormatter();
236
+ // Include OpenTelemetry fields for ClickHouse compatibility
237
+ const beamableFormatter = createBeamableLogFormatter(options.serviceName, options.qualifiedServiceName);
165
238
  beamableFormatter.pipe(process.stdout);
166
239
  return pino(pinoOptions, beamableFormatter);
167
240
  }
@@ -194,7 +267,7 @@ export function createLogger(env, options = {}) {
194
267
  // For file logging: Use Beamable format if not local, default Pino format if local
195
268
  const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;
196
269
  if (!usePrettyLogs) {
197
- const beamableFormatter = createBeamableLogFormatter();
270
+ const beamableFormatter = createBeamableLogFormatter(options.serviceName, options.qualifiedServiceName);
198
271
  const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
199
272
  beamableFormatter.pipe(fileStream);
200
273
  return pino(pinoOptions, beamableFormatter);
@@ -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;;;;;GAKG;AACH,SAAS,mBAAmB;IAC1B,mCAAmC;IACnC,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAC;AACzE,CAAC;AAOD;;;;GAIG;AACH,SAAS,2BAA2B,CAAC,KAAa;IAChD,QAAQ,KAAK,EAAE,CAAC;QACd,KAAK,EAAE,EAAE,QAAQ;YACf,OAAO,OAAO,CAAC;QACjB,KAAK,EAAE,EAAE,QAAQ;YACf,OAAO,OAAO,CAAC;QACjB,KAAK,EAAE,EAAE,OAAO;YACd,OAAO,MAAM,CAAC;QAChB,KAAK,EAAE,E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AC;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 * Determines if we should use pretty logs (local dev) or raw JSON logs (deployed).\r\n * \r\n * Simple check: If IS_LOCAL=1 is set in environment, use pretty logs.\r\n * Otherwise, use raw Beamable JSON format for log collection.\r\n */\r\nfunction shouldUsePrettyLogs(): boolean {\r\n // Check for explicit IS_LOCAL flag\r\n return process.env.IS_LOCAL === '1' || process.env.IS_LOCAL === 'true';\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 usePrettyLogs = shouldUsePrettyLogs();\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 (!usePrettyLogs) {\r\n // Deployed/remote: Use Beamable JSON format for log 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 if not local, default Pino format if local\r\n const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;\r\n if (!usePrettyLogs) {\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","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,MA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BAC9B,QAAQ,EAAE,IAAI;oBACd,aAAa,EAAE,YAAY;oBAC3B,MAAM,EAAE,cAAc;oBACtB,UAAU,EAAE,KAAK;iBAClB,CAAC,CAAC;gBACH,kCAAkC;gBAClC,OAAO,IAAI,CAAC,WAAW,EAAE,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,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,iBAAiB,GAAG,0BAA0B,CAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAC;QACxG,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 * Determines if we should use pretty logs (local dev) or raw JSON logs (deployed).\r\n * \r\n * Simple check: If IS_LOCAL=1 is set in environment, use pretty logs.\r\n * Otherwise, use raw Beamable JSON format for log collection.\r\n */\r\nfunction shouldUsePrettyLogs(): boolean {\r\n // Check for explicit IS_LOCAL flag\r\n return process.env.IS_LOCAL === '1' || process.env.IS_LOCAL === 'true';\r\n}\r\n\r\ninterface LoggerFactoryOptions {\r\n name?: string;\r\n destinationPath?: string;\r\n serviceName?: string; // Service name for log filtering (e.g., \"ExampleNodeService\")\r\n qualifiedServiceName?: string; // Full qualified service name (e.g., \"micro_ExampleNodeService\")\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 * Also includes OpenTelemetry-compatible fields for ClickHouse compatibility.\r\n * Pino writes JSON strings (one per line) to the stream.\r\n */\r\nfunction createBeamableLogFormatter(serviceName?: string, qualifiedServiceName?: string): 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 (for CloudWatch Logs Insights)\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 service name in context for CloudWatch filtering\r\n if (serviceName) {\r\n contextFields.serviceName = serviceName;\r\n }\r\n if (qualifiedServiceName) {\r\n contextFields.qualifiedServiceName = qualifiedServiceName;\r\n }\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 // Add OpenTelemetry-compatible fields for ClickHouse compatibility\r\n // These fields allow an OpenTelemetry collector to parse and forward logs to ClickHouse\r\n // The Portal's realm-level logs page queries ClickHouse's otel_logs table\r\n const otelFields: Record<string, unknown> = {};\r\n \r\n // ResourceAttributes - service identification (for ClickHouse filtering)\r\n const resourceAttributes: Record<string, unknown> = {};\r\n if (qualifiedServiceName) {\r\n // ClickHouse expects service.namespace to match the service name format\r\n resourceAttributes['service.namespace'] = qualifiedServiceName;\r\n }\r\n if (serviceName) {\r\n resourceAttributes['service.name'] = serviceName;\r\n }\r\n if (pinoLog.cid) {\r\n resourceAttributes['beam.cid'] = String(pinoLog.cid);\r\n }\r\n if (pinoLog.pid) {\r\n resourceAttributes['beam.pid'] = String(pinoLog.pid);\r\n }\r\n if (pinoLog.routingKey) {\r\n resourceAttributes['beam.routing_key'] = String(pinoLog.routingKey);\r\n }\r\n \r\n // LogAttributes - log-specific attributes\r\n const logAttributes: Record<string, unknown> = {};\r\n if (pinoLog.component) {\r\n logAttributes['component'] = String(pinoLog.component);\r\n }\r\n if (pinoLog.err) {\r\n const err = pinoLog.err;\r\n if (err.message) logAttributes['exception.message'] = String(err.message);\r\n if (err.stack) logAttributes['exception.stacktrace'] = String(err.stack);\r\n if (err.type) logAttributes['exception.type'] = String(err.type);\r\n }\r\n \r\n // Map Beamable log level to OpenTelemetry SeverityText\r\n // Beamable: Debug, Info, Warning, Error, Fatal\r\n // OpenTelemetry: Trace, Debug, Info, Warn, Error, Fatal, Unspecified\r\n const severityTextMap: Record<string, string> = {\r\n 'Debug': 'Debug',\r\n 'Info': 'Information',\r\n 'Warning': 'Warning',\r\n 'Error': 'Error',\r\n 'Fatal': 'Critical',\r\n };\r\n const severityText = severityTextMap[level] || 'Information';\r\n \r\n // Add OpenTelemetry fields to the log\r\n // These are in addition to the Beamable format, so both systems can parse the logs\r\n otelFields['Timestamp'] = timestamp; // OpenTelemetry timestamp format\r\n otelFields['SeverityText'] = severityText;\r\n otelFields['Body'] = messageParts.length > 0 ? messageParts.join(' ') : 'No message';\r\n if (Object.keys(resourceAttributes).length > 0) {\r\n otelFields['ResourceAttributes'] = resourceAttributes;\r\n }\r\n if (Object.keys(logAttributes).length > 0) {\r\n otelFields['LogAttributes'] = logAttributes;\r\n }\r\n \r\n // Merge OpenTelemetry fields into the log (for ClickHouse compatibility)\r\n // An OpenTelemetry collector can parse these fields and forward to ClickHouse\r\n Object.assign(beamableLog, otelFields);\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 usePrettyLogs = shouldUsePrettyLogs();\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 // Include service name in base fields for filtering\r\n serviceName: options.serviceName,\r\n qualifiedServiceName: options.qualifiedServiceName,\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 (!usePrettyLogs) {\r\n // Deployed/remote: Use Beamable JSON format for log collection\r\n // Include OpenTelemetry fields for ClickHouse compatibility\r\n const beamableFormatter = createBeamableLogFormatter(options.serviceName, options.qualifiedServiceName);\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 if not local, default Pino format if local\r\n const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;\r\n if (!usePrettyLogs) {\r\n const beamableFormatter = createBeamableLogFormatter(options.serviceName, options.qualifiedServiceName);\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
@@ -23,12 +23,18 @@ class MicroserviceRuntime {
23
23
  this.microServiceId = (0, node_crypto_1.randomUUID)();
24
24
  this.isReady = false;
25
25
  this.env = env !== null && env !== void 0 ? env : (0, env_js_1.loadEnvironmentConfig)();
26
- this.logger = (0, logger_js_1.createLogger)(this.env, { name: 'beamable-node-microservice' });
27
- this.serviceManager = new services_js_1.BeamableServiceManager(this.env, this.logger);
28
26
  const registered = (0, decorators_js_1.listRegisteredServices)();
29
27
  if (registered.length === 0) {
30
28
  throw new Error('No microservices registered. Use the @Microservice decorator to register at least one class.');
31
29
  }
30
+ const primaryService = registered[0];
31
+ const qualifiedServiceName = `micro_${primaryService.qualifiedName}`;
32
+ this.logger = (0, logger_js_1.createLogger)(this.env, {
33
+ name: 'beamable-node-microservice',
34
+ serviceName: primaryService.name,
35
+ qualifiedServiceName: qualifiedServiceName,
36
+ });
37
+ this.serviceManager = new services_js_1.BeamableServiceManager(this.env, this.logger);
32
38
  this.services = registered.map((definition) => {
33
39
  const instance = new definition.ctor();
34
40
  const configureHandlers = (0, decorators_js_1.getConfigureServicesHandlers)(definition.ctor);
@@ -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,m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M,EAC1C,OAAO,GAAE,sBAA2B,GACnC,OAAO,CA4BT"}
package/dist/runtime.js CHANGED
@@ -27,12 +27,20 @@ export class MicroserviceRuntime {
27
27
  isReady = false;
28
28
  constructor(env) {
29
29
  this.env = env ?? loadEnvironmentConfig();
30
- this.logger = createLogger(this.env, { name: 'beamable-node-microservice' });
31
- this.serviceManager = new BeamableServiceManager(this.env, this.logger);
30
+ // Get registered services first to extract service name for logger
32
31
  const registered = listRegisteredServices();
33
32
  if (registered.length === 0) {
34
33
  throw new Error('No microservices registered. Use the @Microservice decorator to register at least one class.');
35
34
  }
35
+ // Use the first service's name for the main logger (for CloudWatch filtering and ClickHouse compatibility)
36
+ const primaryService = registered[0];
37
+ const qualifiedServiceName = `micro_${primaryService.qualifiedName}`;
38
+ this.logger = createLogger(this.env, {
39
+ name: 'beamable-node-microservice',
40
+ serviceName: primaryService.name,
41
+ qualifiedServiceName: qualifiedServiceName,
42
+ });
43
+ this.serviceManager = new BeamableServiceManager(this.env, this.logger);
36
44
  this.services = registered.map((definition) => {
37
45
  const instance = new definition.ctor();
38
46
  const configureHandlers = getConfigureServicesHandlers(definition.ctor);
@@ -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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E,KAAK,CAAC,CAAC;IACrB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACvB,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAC3B,MAAM,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 // Discovery broadcaster only runs in local development (not in containers)\r\n // This allows the portal to detect that the service is running locally\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 when IS_LOCAL=1 is set\r\n // In deployed containers, we want only JSON logs for proper log collection\r\n if (process.env.IS_LOCAL !== '1' && process.env.IS_LOCAL !== 'true') {\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 // Only output debug logs when IS_LOCAL=1 is set\r\n if (process.env.IS_LOCAL === '1' || process.env.IS_LOCAL === 'true') {\r\n console.error(...args);\r\n }\r\n}\r\n\r\n/**\r\n * Determines if we're running in a deployed container.\r\n * Used for service registration logic (routing key handling, discovery broadcaster, etc.)\r\n * \r\n * Note: For log formatting, use IS_LOCAL env var instead.\r\n */\r\nfunction isRunningInContainer(): boolean {\r\n // Check for Docker container\r\n try {\r\n const fs = require('fs');\r\n if (fs.existsSync('/.dockerenv')) {\r\n return true;\r\n }\r\n } catch {\r\n // fs might not be available\r\n }\r\n \r\n // Check for Docker container hostname pattern (12 hex chars)\r\n const hostname = process.env.HOSTNAME || '';\r\n if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {\r\n return true;\r\n }\r\n \r\n // Explicit container indicators\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 // 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,EAA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;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 \r\n // Get registered services first to extract service name for logger\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 \r\n // Use the first service's name for the main logger (for CloudWatch filtering and ClickHouse compatibility)\r\n const primaryService = registered[0];\r\n const qualifiedServiceName = `micro_${primaryService.qualifiedName}`;\r\n \r\n this.logger = createLogger(this.env, {\r\n name: 'beamable-node-microservice',\r\n serviceName: primaryService.name,\r\n qualifiedServiceName: qualifiedServiceName,\r\n });\r\n this.serviceManager = new BeamableServiceManager(this.env, this.logger);\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 // Discovery broadcaster only runs in local development (not in containers)\r\n // This allows the portal to detect that the service is running locally\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 when IS_LOCAL=1 is set\r\n // In deployed containers, we want only JSON logs for proper log collection\r\n if (process.env.IS_LOCAL !== '1' && process.env.IS_LOCAL !== 'true') {\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 // Only output debug logs when IS_LOCAL=1 is set\r\n if (process.env.IS_LOCAL === '1' || process.env.IS_LOCAL === 'true') {\r\n console.error(...args);\r\n }\r\n}\r\n\r\n/**\r\n * Determines if we're running in a deployed container.\r\n * Used for service registration logic (routing key handling, discovery broadcaster, etc.)\r\n * \r\n * Note: For log formatting, use IS_LOCAL env var instead.\r\n */\r\nfunction isRunningInContainer(): boolean {\r\n // Check for Docker container\r\n try {\r\n const fs = require('fs');\r\n if (fs.existsSync('/.dockerenv')) {\r\n return true;\r\n }\r\n } catch {\r\n // fs might not be available\r\n }\r\n \r\n // Check for Docker container hostname pattern (12 hex chars)\r\n const hostname = process.env.HOSTNAME || '';\r\n if (hostname && /^[a-f0-9]{12}$/i.test(hostname)) {\r\n return true;\r\n }\r\n \r\n // Explicit container indicators\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 // 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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omen.foundation/node-microservice-runtime",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
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
@@ -32,6 +32,8 @@ function shouldUsePrettyLogs(): boolean {
32
32
  interface LoggerFactoryOptions {
33
33
  name?: string;
34
34
  destinationPath?: string;
35
+ serviceName?: string; // Service name for log filtering (e.g., "ExampleNodeService")
36
+ qualifiedServiceName?: string; // Full qualified service name (e.g., "micro_ExampleNodeService")
35
37
  }
36
38
 
37
39
  /**
@@ -61,9 +63,10 @@ function mapPinoLevelToBeamableLevel(level: number): string {
61
63
  /**
62
64
  * Creates a transform stream that converts Pino JSON logs to Beamable's expected format.
63
65
  * Beamable expects logs with __t (timestamp), __l (level), and __m (message) fields.
66
+ * Also includes OpenTelemetry-compatible fields for ClickHouse compatibility.
64
67
  * Pino writes JSON strings (one per line) to the stream.
65
68
  */
66
- function createBeamableLogFormatter(): Transform {
69
+ function createBeamableLogFormatter(serviceName?: string, qualifiedServiceName?: string): Transform {
67
70
  return new Transform({
68
71
  objectMode: false, // Pino writes strings, not objects
69
72
  transform(chunk: Buffer, _encoding, callback) {
@@ -107,7 +110,7 @@ function createBeamableLogFormatter(): Transform {
107
110
  messageParts.push(`${errMsg}${errStack}`);
108
111
  }
109
112
 
110
- // Build the Beamable log format
113
+ // Build the Beamable log format (for CloudWatch Logs Insights)
111
114
  const beamableLog: Record<string, unknown> = {
112
115
  __t: timestamp,
113
116
  __l: level,
@@ -123,6 +126,14 @@ function createBeamableLogFormatter(): Transform {
123
126
  if (pinoLog.service) contextFields.service = pinoLog.service;
124
127
  if (pinoLog.component) contextFields.component = pinoLog.component;
125
128
 
129
+ // Include service name in context for CloudWatch filtering
130
+ if (serviceName) {
131
+ contextFields.serviceName = serviceName;
132
+ }
133
+ if (qualifiedServiceName) {
134
+ contextFields.qualifiedServiceName = qualifiedServiceName;
135
+ }
136
+
126
137
  // Include any other fields that aren't standard Pino fields
127
138
  const standardPinoFields = ['level', 'time', 'pid', 'hostname', 'name', 'msg', 'err', 'v', 'cid', 'pid', 'routingKey', 'sdkVersionExecution', 'service', 'component'];
128
139
  for (const [key, value] of Object.entries(pinoLog)) {
@@ -136,6 +147,70 @@ function createBeamableLogFormatter(): Transform {
136
147
  beamableLog.__c = contextFields;
137
148
  }
138
149
 
150
+ // Add OpenTelemetry-compatible fields for ClickHouse compatibility
151
+ // These fields allow an OpenTelemetry collector to parse and forward logs to ClickHouse
152
+ // The Portal's realm-level logs page queries ClickHouse's otel_logs table
153
+ const otelFields: Record<string, unknown> = {};
154
+
155
+ // ResourceAttributes - service identification (for ClickHouse filtering)
156
+ const resourceAttributes: Record<string, unknown> = {};
157
+ if (qualifiedServiceName) {
158
+ // ClickHouse expects service.namespace to match the service name format
159
+ resourceAttributes['service.namespace'] = qualifiedServiceName;
160
+ }
161
+ if (serviceName) {
162
+ resourceAttributes['service.name'] = serviceName;
163
+ }
164
+ if (pinoLog.cid) {
165
+ resourceAttributes['beam.cid'] = String(pinoLog.cid);
166
+ }
167
+ if (pinoLog.pid) {
168
+ resourceAttributes['beam.pid'] = String(pinoLog.pid);
169
+ }
170
+ if (pinoLog.routingKey) {
171
+ resourceAttributes['beam.routing_key'] = String(pinoLog.routingKey);
172
+ }
173
+
174
+ // LogAttributes - log-specific attributes
175
+ const logAttributes: Record<string, unknown> = {};
176
+ if (pinoLog.component) {
177
+ logAttributes['component'] = String(pinoLog.component);
178
+ }
179
+ if (pinoLog.err) {
180
+ const err = pinoLog.err;
181
+ if (err.message) logAttributes['exception.message'] = String(err.message);
182
+ if (err.stack) logAttributes['exception.stacktrace'] = String(err.stack);
183
+ if (err.type) logAttributes['exception.type'] = String(err.type);
184
+ }
185
+
186
+ // Map Beamable log level to OpenTelemetry SeverityText
187
+ // Beamable: Debug, Info, Warning, Error, Fatal
188
+ // OpenTelemetry: Trace, Debug, Info, Warn, Error, Fatal, Unspecified
189
+ const severityTextMap: Record<string, string> = {
190
+ 'Debug': 'Debug',
191
+ 'Info': 'Information',
192
+ 'Warning': 'Warning',
193
+ 'Error': 'Error',
194
+ 'Fatal': 'Critical',
195
+ };
196
+ const severityText = severityTextMap[level] || 'Information';
197
+
198
+ // Add OpenTelemetry fields to the log
199
+ // These are in addition to the Beamable format, so both systems can parse the logs
200
+ otelFields['Timestamp'] = timestamp; // OpenTelemetry timestamp format
201
+ otelFields['SeverityText'] = severityText;
202
+ otelFields['Body'] = messageParts.length > 0 ? messageParts.join(' ') : 'No message';
203
+ if (Object.keys(resourceAttributes).length > 0) {
204
+ otelFields['ResourceAttributes'] = resourceAttributes;
205
+ }
206
+ if (Object.keys(logAttributes).length > 0) {
207
+ otelFields['LogAttributes'] = logAttributes;
208
+ }
209
+
210
+ // Merge OpenTelemetry fields into the log (for ClickHouse compatibility)
211
+ // An OpenTelemetry collector can parse these fields and forward to ClickHouse
212
+ Object.assign(beamableLog, otelFields);
213
+
139
214
  // Output as a single-line JSON string (required for CloudWatch)
140
215
  const output = JSON.stringify(beamableLog) + '\n';
141
216
  callback(null, Buffer.from(output, 'utf8'));
@@ -164,6 +239,9 @@ export function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptio
164
239
  pid: env.pid,
165
240
  routingKey: env.routingKey ?? null,
166
241
  sdkVersionExecution: env.sdkVersionExecution,
242
+ // Include service name in base fields for filtering
243
+ serviceName: options.serviceName,
244
+ qualifiedServiceName: options.qualifiedServiceName,
167
245
  },
168
246
  redact: {
169
247
  paths: ['secret', 'refreshToken'],
@@ -178,7 +256,8 @@ export function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptio
178
256
  if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {
179
257
  if (!usePrettyLogs) {
180
258
  // Deployed/remote: Use Beamable JSON format for log collection
181
- const beamableFormatter = createBeamableLogFormatter();
259
+ // Include OpenTelemetry fields for ClickHouse compatibility
260
+ const beamableFormatter = createBeamableLogFormatter(options.serviceName, options.qualifiedServiceName);
182
261
  beamableFormatter.pipe(process.stdout);
183
262
  return pino(pinoOptions, beamableFormatter);
184
263
  } else {
@@ -210,7 +289,7 @@ export function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptio
210
289
  // For file logging: Use Beamable format if not local, default Pino format if local
211
290
  const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;
212
291
  if (!usePrettyLogs) {
213
- const beamableFormatter = createBeamableLogFormatter();
292
+ const beamableFormatter = createBeamableLogFormatter(options.serviceName, options.qualifiedServiceName);
214
293
  const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
215
294
  beamableFormatter.pipe(fileStream as unknown as NodeJS.WritableStream);
216
295
  return pino(pinoOptions, beamableFormatter);
package/src/runtime.ts CHANGED
@@ -58,13 +58,24 @@ export class MicroserviceRuntime {
58
58
 
59
59
  constructor(env?: EnvironmentConfig) {
60
60
  this.env = env ?? loadEnvironmentConfig();
61
- this.logger = createLogger(this.env, { name: 'beamable-node-microservice' });
62
- this.serviceManager = new BeamableServiceManager(this.env, this.logger);
63
-
61
+
62
+ // Get registered services first to extract service name for logger
64
63
  const registered = listRegisteredServices();
65
64
  if (registered.length === 0) {
66
65
  throw new Error('No microservices registered. Use the @Microservice decorator to register at least one class.');
67
66
  }
67
+
68
+ // Use the first service's name for the main logger (for CloudWatch filtering and ClickHouse compatibility)
69
+ const primaryService = registered[0];
70
+ const qualifiedServiceName = `micro_${primaryService.qualifiedName}`;
71
+
72
+ this.logger = createLogger(this.env, {
73
+ name: 'beamable-node-microservice',
74
+ serviceName: primaryService.name,
75
+ qualifiedServiceName: qualifiedServiceName,
76
+ });
77
+ this.serviceManager = new BeamableServiceManager(this.env, this.logger);
78
+
68
79
  this.services = registered.map((definition) => {
69
80
  const instance = new definition.ctor() as Record<string, unknown>;
70
81
  const configureHandlers = getConfigureServicesHandlers(definition.ctor);