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

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