@ruifung/codemode-bridge 1.0.7 → 1.0.8

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.
@@ -4,12 +4,13 @@
4
4
  * Implements subcommands for managing and running the bridge
5
5
  */
6
6
  import chalk from "chalk";
7
- import * as fs from "fs";
8
- import * as path from "path";
7
+ import * as fs from "node:fs";
8
+ import * as path from "node:path";
9
9
  import { loadConfig, saveConfig, addServer, removeServer, updateServer, getServer, listServers, validateServer, getConfigFilePath, } from "./config-manager.js";
10
10
  import { startCodeModeBridgeServer } from "../mcp/server.js";
11
11
  import { getServerConfig } from "../mcp/config.js";
12
12
  import { initializeLogger, logInfo, logError, flushStderrBuffer } from "../utils/logger.js";
13
+ import { getRuntimeName } from "../utils/env.js";
13
14
  import { tokenPersistence } from "../mcp/token-persistence.js";
14
15
  import { MCPClient } from "../mcp/mcp-client.js";
15
16
  /**
@@ -21,6 +22,7 @@ export async function runServer(configPath, servers, debug, executor) {
21
22
  initializeLogger(debug);
22
23
  console.error(chalk.cyan("\nšŸš€ Code Mode Bridge"));
23
24
  console.error(chalk.cyan("====================\n"));
25
+ logInfo(`Runtime: ${getRuntimeName()}`, { component: 'CLI' });
24
26
  // Load the bridge configuration
25
27
  const bridgeConfig = loadConfig(configPath);
26
28
  logInfo(`Loaded config from: ${getConfigFilePath(configPath)}`, { component: 'CLI' });
@@ -4,8 +4,8 @@
4
4
  * Manages the bridge configuration stored in .config/codemode-bridge/mcp.json
5
5
  * Provides utilities to load, save, and manipulate the configuration
6
6
  */
7
- import * as fs from "fs";
8
- import * as path from "path";
7
+ import * as fs from "node:fs";
8
+ import * as path from "node:path";
9
9
  /**
10
10
  * Get the default config directory for the current platform
11
11
  */
package/dist/cli/index.js CHANGED
@@ -19,7 +19,7 @@
19
19
  import { Command } from "commander";
20
20
  import { runServer, listServersCommand, showServerCommand, addServerCommand, removeServerCommand, editServerCommand, configInfoCommand, authLoginCommand, authLogoutCommand, authListCommand, } from "./commands.js";
21
21
  import { getConfigFilePath } from "./config-manager.js";
22
- import * as fs from "fs";
22
+ import * as fs from "node:fs";
23
23
  const pkg = JSON.parse(fs.readFileSync(new URL("../../package.json", import.meta.url), "utf-8"));
24
24
  const defaultConfigPath = getConfigFilePath();
25
25
  const program = new Command();
@@ -34,7 +34,7 @@ program
34
34
  .option("-c, --config <path>", `Path to mcp.json configuration file (default: ${defaultConfigPath})`)
35
35
  .option("-s, --servers <names>", "Comma-separated list of servers to connect to")
36
36
  .option("-d, --debug", "Enable debug logging")
37
- .option("-e, --executor <type>", "Executor type (isolated-vm, container, vm2)")
37
+ .option("-e, --executor <type>", "Executor type (isolated-vm, container, deno, vm2)")
38
38
  .action(async (options) => {
39
39
  const servers = options.servers ? options.servers.split(",").map((s) => s.trim()) : undefined;
40
40
  await runServer(options.config, servers, options.debug, options.executor);
@@ -18,8 +18,12 @@ import type { Executor, ExecuteResult } from '@cloudflare/codemode';
18
18
  export interface ContainerExecutorOptions {
19
19
  /** Execution timeout per call in ms (default 30000) */
20
20
  timeout?: number;
21
- /** Container image (default 'node:22-slim') */
21
+ /** Container image (default 'node:24-slim') */
22
22
  image?: string;
23
+ /** Container user (default based on image) */
24
+ user?: string;
25
+ /** Container command to run the runner (default based on image) */
26
+ command?: string[];
23
27
  /** Container runtime command — 'docker' | 'podman' | auto-detect */
24
28
  runtime?: string;
25
29
  /** Memory limit (default '256m') */
@@ -30,6 +34,8 @@ export interface ContainerExecutorOptions {
30
34
  export declare class ContainerExecutor implements Executor {
31
35
  private runtime;
32
36
  private image;
37
+ private containerUser;
38
+ private containerCommand;
33
39
  private timeout;
34
40
  private memoryLimit;
35
41
  private cpuLimit;
@@ -20,6 +20,7 @@ import { createInterface } from 'node:readline';
20
20
  import { fileURLToPath } from 'node:url';
21
21
  import { dirname, join } from 'node:path';
22
22
  import { wrapCode } from './wrap-code.js';
23
+ import { isBun, isDeno } from '../utils/env.js';
23
24
  // ── Runtime detection ───────────────────────────────────────────────
24
25
  function detectRuntime(requested) {
25
26
  if (requested)
@@ -67,12 +68,34 @@ export class ContainerExecutor {
67
68
  this.pendingExecution = null;
68
69
  this.initPromise = null;
69
70
  this.timeout = options.timeout ?? 30000;
70
- this.image = options.image ?? 'node:24-slim';
71
71
  this.memoryLimit = options.memoryLimit ?? '256m';
72
72
  this.cpuLimit = options.cpuLimit ?? 1.0;
73
73
  this.runtime = detectRuntime(options.runtime);
74
+ // Platform-specific defaults
75
+ if (options.image) {
76
+ this.image = options.image;
77
+ this.containerUser = options.user ?? '1000';
78
+ this.containerCommand = options.command ?? ['node'];
79
+ }
80
+ else if (isBun()) {
81
+ this.image = 'oven/bun:debian';
82
+ this.containerUser = options.user ?? '1000'; // bun user is 1000
83
+ this.containerCommand = ['bun', 'run'];
84
+ }
85
+ else if (isDeno()) {
86
+ this.image = 'denoland/deno:debian';
87
+ this.containerUser = options.user ?? '1000'; // deno:debian has no 'deno' user by default
88
+ this.containerCommand = ['deno', 'run', '-A'];
89
+ }
90
+ else {
91
+ this.image = 'node:24-slim';
92
+ this.containerUser = options.user ?? '1000'; // node user is 1000
93
+ this.containerCommand = ['node'];
94
+ }
74
95
  // Start initialization immediately to create the container before the first execution.
75
- // Errors are caught and logged, but will also be thrown when execute() awaits this.init().
96
+ if (process.env.DEBUG || process.env.NODE_ENV === 'test') {
97
+ console.log(`[ContainerExecutor] Using image: ${this.image}, user: ${this.containerUser}, command: ${this.containerCommand.join(' ')}`);
98
+ }
76
99
  this.init().catch(err => {
77
100
  process.stderr.write(`[container-executor] Immediate initialization failed: ${err.message}\n`);
78
101
  });
@@ -145,7 +168,7 @@ export class ContainerExecutor {
145
168
  '--read-only', // immutable rootfs
146
169
  '--tmpfs', '/tmp:rw,noexec,nosuid,size=64m', // writable /tmp
147
170
  '--cap-drop=ALL', // drop all capabilities
148
- '--user', 'node', // run as non-root user
171
+ '--user', this.containerUser,
149
172
  '--memory', this.memoryLimit,
150
173
  `--cpus=${this.cpuLimit}`,
151
174
  '--pids-limit=64', // limit process spawning
@@ -153,7 +176,7 @@ export class ContainerExecutor {
153
176
  '-v', `${scripts.worker}:/app/container-worker.mjs:ro`, // mount worker script
154
177
  '-w', '/app',
155
178
  this.image,
156
- 'node', '/app/container-runner.mjs',
179
+ ...this.containerCommand, '/app/container-runner.mjs',
157
180
  ];
158
181
  this.process = spawn(this.runtime, args, {
159
182
  stdio: ['pipe', 'pipe', 'pipe'],
@@ -0,0 +1,26 @@
1
+ import type { Executor, ExecuteResult } from '@cloudflare/codemode';
2
+ export interface DenoExecutorOptions {
3
+ /** Execution timeout per call in ms (default 30000) */
4
+ timeout?: number;
5
+ /** Deno executable path — 'deno' | auto-detect */
6
+ denoPath?: string;
7
+ }
8
+ export declare class DenoExecutor implements Executor {
9
+ private denoPath;
10
+ private timeout;
11
+ private process;
12
+ private readline;
13
+ private ready;
14
+ private readyResolve;
15
+ private pendingExecution;
16
+ private initPromise;
17
+ constructor(options?: DenoExecutorOptions);
18
+ private init;
19
+ private _init;
20
+ private handleMessage;
21
+ private handleToolCall;
22
+ private send;
23
+ execute(code: string, fns: Record<string, (...args: unknown[]) => Promise<unknown>>): Promise<ExecuteResult>;
24
+ dispose(): void;
25
+ }
26
+ export declare function createDenoExecutor(options?: DenoExecutorOptions): DenoExecutor;
@@ -0,0 +1,251 @@
1
+ import { spawn, execSync } from 'node:child_process';
2
+ import { createInterface } from 'node:readline';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { dirname, join } from 'node:path';
5
+ import { wrapCode } from './wrap-code.js';
6
+ // ── Runtime detection ───────────────────────────────────────────────
7
+ function detectDeno(requested) {
8
+ if (requested)
9
+ return requested;
10
+ // If we are already running on Deno, use the current executable path
11
+ if (typeof globalThis.Deno?.execPath === 'function') {
12
+ return globalThis.Deno.execPath();
13
+ }
14
+ // Try 'deno' command in PATH
15
+ try {
16
+ execSync('deno --version', { stdio: 'ignore' });
17
+ return 'deno';
18
+ }
19
+ catch {
20
+ // not available
21
+ }
22
+ throw new Error('Deno executable not found. Install Deno or set DENO_PATH environment variable.');
23
+ }
24
+ // ── Resolve runner script path ──────────────────────────────────────
25
+ function getScriptPaths() {
26
+ let dir;
27
+ try {
28
+ dir = dirname(fileURLToPath(import.meta.url));
29
+ }
30
+ catch {
31
+ dir = __dirname;
32
+ }
33
+ return {
34
+ runner: join(dir, 'container-runner.mjs'),
35
+ worker: join(dir, 'container-worker.mjs'),
36
+ };
37
+ }
38
+ // ── DenoExecutor ────────────────────────────────────────────────────
39
+ export class DenoExecutor {
40
+ constructor(options = {}) {
41
+ this.process = null;
42
+ this.readline = null;
43
+ this.ready = false;
44
+ this.readyResolve = null;
45
+ this.pendingExecution = null;
46
+ this.initPromise = null;
47
+ this.timeout = options.timeout ?? 30000;
48
+ this.denoPath = detectDeno(options.denoPath);
49
+ this.init().catch(err => {
50
+ process.stderr.write(`[deno-executor] Immediate initialization failed: ${err.message}\n`);
51
+ });
52
+ }
53
+ async init() {
54
+ if (this.ready)
55
+ return;
56
+ if (this.initPromise)
57
+ return this.initPromise;
58
+ this.initPromise = this._init();
59
+ return this.initPromise;
60
+ }
61
+ async _init() {
62
+ const scripts = getScriptPaths();
63
+ // Start a long-lived Deno process with restricted permissions
64
+ const args = [
65
+ 'run',
66
+ '--no-prompt',
67
+ '--no-config',
68
+ '--no-npm',
69
+ '--no-remote',
70
+ '--allow-read=' + scripts.runner + ',' + scripts.worker,
71
+ '--deny-net',
72
+ '--deny-write',
73
+ '--deny-run',
74
+ '--deny-env',
75
+ '--deny-sys',
76
+ '--deny-ffi',
77
+ ];
78
+ args.push(scripts.runner);
79
+ this.process = spawn(this.denoPath, args, {
80
+ stdio: ['pipe', 'pipe', 'pipe'],
81
+ });
82
+ this.process.stderr?.on('data', (data) => {
83
+ process.stderr.write(`[deno] ${data.toString()}`);
84
+ });
85
+ this.process.on('exit', (code) => {
86
+ this.ready = false;
87
+ if (this.pendingExecution) {
88
+ clearTimeout(this.pendingExecution.timeoutHandle);
89
+ this.pendingExecution.reject(new Error(`Deno process exited unexpectedly with code ${code}`));
90
+ this.pendingExecution = null;
91
+ }
92
+ });
93
+ this.readline = createInterface({
94
+ input: this.process.stdout,
95
+ terminal: false,
96
+ });
97
+ this.readline.on('line', (line) => this.handleMessage(line));
98
+ await new Promise((resolve, reject) => {
99
+ const timeout = setTimeout(() => {
100
+ reject(new Error('Deno process failed to become ready within 10s'));
101
+ }, 10000);
102
+ this.readyResolve = () => {
103
+ clearTimeout(timeout);
104
+ this.ready = true;
105
+ this.readyResolve = null;
106
+ resolve();
107
+ };
108
+ this.process.on('error', (err) => {
109
+ clearTimeout(timeout);
110
+ this.readyResolve = null;
111
+ reject(new Error(`Failed to start Deno: ${err.message}`));
112
+ });
113
+ });
114
+ }
115
+ handleMessage(line) {
116
+ if (!line.trim())
117
+ return;
118
+ let msg;
119
+ try {
120
+ msg = JSON.parse(line);
121
+ }
122
+ catch {
123
+ process.stderr.write(`[deno-executor] bad JSON from deno: ${line}\n`);
124
+ return;
125
+ }
126
+ switch (msg.type) {
127
+ case 'tool-call':
128
+ this.handleToolCall(msg);
129
+ break;
130
+ case 'result':
131
+ if (this.pendingExecution && this.pendingExecution.id === msg.id) {
132
+ clearTimeout(this.pendingExecution.timeoutHandle);
133
+ this.pendingExecution.resolve({
134
+ result: msg.result,
135
+ logs: msg.logs,
136
+ });
137
+ this.pendingExecution = null;
138
+ }
139
+ break;
140
+ case 'error':
141
+ if (this.pendingExecution && this.pendingExecution.id === msg.id) {
142
+ clearTimeout(this.pendingExecution.timeoutHandle);
143
+ this.pendingExecution.resolve({
144
+ result: undefined,
145
+ error: msg.error,
146
+ logs: msg.logs,
147
+ });
148
+ this.pendingExecution = null;
149
+ }
150
+ break;
151
+ case 'ready':
152
+ if (this.readyResolve) {
153
+ this.readyResolve();
154
+ }
155
+ break;
156
+ }
157
+ }
158
+ async handleToolCall(msg) {
159
+ if (!this.pendingExecution) {
160
+ this.send({ type: 'tool-error', id: msg.id, error: 'No active execution context' });
161
+ return;
162
+ }
163
+ const fns = this.pendingExecution.fns;
164
+ const fn = fns[msg.name];
165
+ if (!fn) {
166
+ this.send({
167
+ type: 'tool-error',
168
+ id: msg.id,
169
+ error: `Tool '${msg.name}' not found. Available tools: ${Object.keys(fns).join(', ')}`,
170
+ });
171
+ return;
172
+ }
173
+ try {
174
+ const args = msg.args;
175
+ const result = await fn(...(Array.isArray(args) ? args : [args]));
176
+ this.send({ type: 'tool-result', id: msg.id, result });
177
+ }
178
+ catch (err) {
179
+ this.send({
180
+ type: 'tool-error',
181
+ id: msg.id,
182
+ error: err instanceof Error ? err.message : String(err),
183
+ });
184
+ }
185
+ }
186
+ send(msg) {
187
+ if (!this.process?.stdin?.writable) {
188
+ throw new Error('Deno stdin is not writable');
189
+ }
190
+ this.process.stdin.write(JSON.stringify(msg) + '\n');
191
+ }
192
+ async execute(code, fns) {
193
+ await this.init();
194
+ if (this.pendingExecution) {
195
+ return {
196
+ result: undefined,
197
+ error: 'Another execution is already in progress',
198
+ };
199
+ }
200
+ const id = `exec-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
201
+ const wrappedCode = wrapCode(code);
202
+ return new Promise((resolve, reject) => {
203
+ const timeoutHandle = setTimeout(() => {
204
+ if (this.pendingExecution?.id === id) {
205
+ this.pendingExecution = null;
206
+ resolve({
207
+ result: undefined,
208
+ error: `Code execution timeout after ${this.timeout}ms`,
209
+ });
210
+ }
211
+ }, this.timeout);
212
+ this.pendingExecution = { id, resolve, reject, fns, timeoutHandle };
213
+ this.send({ type: 'execute', id, code: wrappedCode });
214
+ });
215
+ }
216
+ dispose() {
217
+ if (this.pendingExecution) {
218
+ clearTimeout(this.pendingExecution.timeoutHandle);
219
+ this.pendingExecution.reject(new Error('Executor disposed'));
220
+ this.pendingExecution = null;
221
+ }
222
+ if (this.process?.stdin?.writable) {
223
+ try {
224
+ this.send({ type: 'shutdown' });
225
+ }
226
+ catch {
227
+ // ignore
228
+ }
229
+ }
230
+ if (this.readline) {
231
+ this.readline.close();
232
+ this.readline = null;
233
+ }
234
+ if (this.process) {
235
+ this.process.kill('SIGTERM');
236
+ const proc = this.process;
237
+ setTimeout(() => {
238
+ try {
239
+ proc.kill('SIGKILL');
240
+ }
241
+ catch { /* already dead */ }
242
+ }, 2000);
243
+ this.process = null;
244
+ }
245
+ this.ready = false;
246
+ this.initPromise = null;
247
+ }
248
+ }
249
+ export function createDenoExecutor(options) {
250
+ return new DenoExecutor(options);
251
+ }
@@ -1,4 +1,5 @@
1
1
  import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
2
+ import { getRuntimeName } from '../utils/env.js';
2
3
  export function createExecutorTestSuite(name, createExecutor, options) {
3
4
  const skipSet = new Set(options?.skipTests ?? []);
4
5
  /** Use it.skip for tests in the skip list, it otherwise */
@@ -11,6 +12,7 @@ export function createExecutorTestSuite(name, createExecutor, options) {
11
12
  }
12
13
  };
13
14
  describe(`Executor: ${name}`, () => {
15
+ console.log(`[Test] Runtime: ${getRuntimeName()}`);
14
16
  let executor;
15
17
  beforeAll(async () => {
16
18
  executor = createExecutor();
@@ -335,7 +337,7 @@ export function createExecutorTestSuite(name, createExecutor, options) {
335
337
  });
336
338
  describe('Isolation & Safety', () => {
337
339
  testOrSkip('should not allow access to require', async () => {
338
- const result = await executor.execute('return require("fs");', {});
340
+ const result = await executor.execute('return require("node:fs");', {});
339
341
  expect(result.error).toBeDefined();
340
342
  });
341
343
  testOrSkip('should not allow process access', async () => {
@@ -361,7 +363,7 @@ export function createExecutorTestSuite(name, createExecutor, options) {
361
363
  return 'network allowed via fetch';
362
364
  }
363
365
  if (typeof require === 'function') {
364
- const http = require('http');
366
+ const http = require('node:http');
365
367
  await new Promise((resolve, reject) => {
366
368
  http.get('http://example.com', resolve).on('error', reject);
367
369
  });
@@ -389,7 +391,7 @@ export function createExecutorTestSuite(name, createExecutor, options) {
389
391
  if (typeof require === 'function') {
390
392
  // Try net.Socket (TCP)
391
393
  try {
392
- const net = require('net');
394
+ const net = require('node:net');
393
395
  await new Promise((resolve, reject) => {
394
396
  const sock = new net.Socket();
395
397
  sock.setTimeout(2000);
@@ -402,7 +404,7 @@ export function createExecutorTestSuite(name, createExecutor, options) {
402
404
 
403
405
  // Try dgram (UDP)
404
406
  try {
405
- const dgram = require('dgram');
407
+ const dgram = require('node:dgram');
406
408
  const sock = dgram.createSocket('udp4');
407
409
  await new Promise((resolve, reject) => {
408
410
  sock.on('error', reject);
@@ -416,7 +418,7 @@ export function createExecutorTestSuite(name, createExecutor, options) {
416
418
 
417
419
  // Try tls.connect
418
420
  try {
419
- const tls = require('tls');
421
+ const tls = require('node:tls');
420
422
  await new Promise((resolve, reject) => {
421
423
  const sock = tls.connect(443, '1.1.1.1', {}, resolve);
422
424
  sock.on('error', reject);
@@ -426,7 +428,7 @@ export function createExecutorTestSuite(name, createExecutor, options) {
426
428
 
427
429
  // Try dns.resolve
428
430
  try {
429
- const dns = require('dns');
431
+ const dns = require('node:dns');
430
432
  await new Promise((resolve, reject) => {
431
433
  dns.resolve('example.com', (err, addresses) => {
432
434
  err ? reject(err) : resolve(addresses);
@@ -529,7 +531,7 @@ export function createExecutorTestSuite(name, createExecutor, options) {
529
531
  testOrSkip('should block dynamic import', async () => {
530
532
  const result = await executor.execute(`
531
533
  try {
532
- const m = await import('fs');
534
+ const m = await import('node:fs');
533
535
  return 'import allowed';
534
536
  } catch (e) {
535
537
  return 'blocked';
@@ -2,8 +2,8 @@
2
2
  * Config Loader - Load MCP server configurations from files
3
3
  * Supports VS Code's mcp.json format and other config files
4
4
  */
5
- import * as fs from "fs";
6
- import * as path from "path";
5
+ import * as fs from "node:fs";
6
+ import * as path from "node:path";
7
7
  /**
8
8
  * Load MCP server configurations from VS Code's mcp.json file
9
9
  * Default location: ~/.config/Code/User/mcp.json (Linux/Mac) or
@@ -26,6 +26,7 @@ import { createCodeTool } from '@cloudflare/codemode/ai';
26
26
  import { z } from 'zod';
27
27
  import { adaptAISDKToolToMCP } from './mcp-adapter.js';
28
28
  import { jsonSchemaToZod } from './server.js';
29
+ import { getRuntimeName } from '../utils/env.js';
29
30
  // ── Helpers ─────────────────────────────────────────────────────────────────
30
31
  /**
31
32
  * Create a mock upstream MCP server with test tools and connect an MCP client
@@ -141,6 +142,7 @@ export function createE2EBridgeTestSuite(executorName, createExecutor, options)
141
142
  }
142
143
  };
143
144
  describe(`E2E Bridge Pipeline [${executorName}]`, () => {
145
+ console.log(`[Test] Runtime: ${getRuntimeName()}`);
144
146
  let upstreamState;
145
147
  let bridgeState;
146
148
  let client;
@@ -378,10 +380,10 @@ export function createE2EBridgeTestSuite(executorName, createExecutor, options)
378
380
  testOrSkip('should not allow require access', async () => {
379
381
  const response = await client.callTool({
380
382
  name: 'eval',
381
- arguments: { code: 'async () => { return require("fs"); }' },
383
+ arguments: { code: 'async () => { return require("node:fs"); }' },
382
384
  });
383
385
  const text = response.content?.[0]?.text || '';
384
- expect(text.toLowerCase()).toMatch(/error|not defined|not allowed/);
386
+ expect(text.toLowerCase()).toMatch(/error|not defined|not allowed|requires .* access|could not be cloned/);
385
387
  });
386
388
  testOrSkip('should not allow process access', async () => {
387
389
  const response = await client.callTool({
@@ -389,7 +391,7 @@ export function createE2EBridgeTestSuite(executorName, createExecutor, options)
389
391
  arguments: { code: 'async () => { return process.env; }' },
390
392
  });
391
393
  const text = response.content?.[0]?.text || '';
392
- expect(text.toLowerCase()).toMatch(/error|not defined|not allowed/);
394
+ expect(text.toLowerCase()).toMatch(/error|not defined|not allowed|requires .* access|could not be cloned/);
393
395
  });
394
396
  testOrSkip('should isolate state between executions', async () => {
395
397
  await callCodemode(client, 'async () => { globalThis.__test = 123; return "set"; }');
@@ -3,7 +3,7 @@
3
3
  * Selects the best available executor (isolated-vm → container → vm2)
4
4
  */
5
5
  import type { Executor } from "@cloudflare/codemode";
6
- export type ExecutorType = 'isolated-vm' | 'container' | 'vm2';
6
+ export type ExecutorType = 'isolated-vm' | 'container' | 'deno' | 'vm2';
7
7
  /**
8
8
  * Metadata about the executor that was created.
9
9
  */
@@ -4,11 +4,27 @@
4
4
  */
5
5
  import { execFileSync } from "node:child_process";
6
6
  import { logInfo, logDebug } from "../utils/logger.js";
7
+ import { isNode, isDeno, isBun, getNodeMajorVersion } from "../utils/env.js";
7
8
  // ── Availability checks (cached) ────────────────────────────────────
8
9
  let _isolatedVmAvailable = null;
10
+ async function isDenoAvailable() {
11
+ // Only allow Deno executor if running on Deno
12
+ return isDeno();
13
+ }
9
14
  async function isIsolatedVmAvailable() {
10
15
  if (_isolatedVmAvailable !== null)
11
16
  return _isolatedVmAvailable;
17
+ // isolated-vm is a native module specifically built for Node.js.
18
+ // While Bun and Deno provide compatibility layers, they often fail or
19
+ // exhibit unstable behavior with native Node modules like isolated-vm.
20
+ // Additionally, isolated-vm only supports LTS (even-numbered) Node.js versions.
21
+ const majorVersion = getNodeMajorVersion();
22
+ const isEvenVersion = majorVersion > 0 && majorVersion % 2 === 0;
23
+ if (!isNode() || !isEvenVersion) {
24
+ logDebug('isolated-vm is only supported on LTS (even-numbered) versions of native Node.js (not Bun, Deno, or odd-numbered Node versions)', { component: 'Executor' });
25
+ _isolatedVmAvailable = false;
26
+ return false;
27
+ }
12
28
  logDebug('Checking isolated-vm availability...', { component: 'Executor' });
13
29
  try {
14
30
  // We run the check in a separate process because isolated-vm can cause
@@ -60,8 +76,17 @@ async function isContainerRuntimeAvailable() {
60
76
  // ── Executor registry (sorted by preference, lowest first) ─────────
61
77
  const executorRegistry = [
62
78
  {
63
- type: 'isolated-vm',
79
+ type: 'deno',
64
80
  preference: 0,
81
+ isAvailable: isDenoAvailable,
82
+ async create(timeout) {
83
+ const { createDenoExecutor } = await import('../executor/deno-executor.js');
84
+ return createDenoExecutor({ timeout });
85
+ },
86
+ },
87
+ {
88
+ type: 'isolated-vm',
89
+ preference: 1,
65
90
  isAvailable: isIsolatedVmAvailable,
66
91
  async create(timeout) {
67
92
  const { createIsolatedVmExecutor } = await import('../executor/isolated-vm-executor.js');
@@ -70,7 +95,7 @@ const executorRegistry = [
70
95
  },
71
96
  {
72
97
  type: 'container',
73
- preference: 1,
98
+ preference: 2,
74
99
  isAvailable: isContainerRuntimeAvailable,
75
100
  async create(timeout) {
76
101
  const { createContainerExecutor } = await import('../executor/container-executor.js');
@@ -79,8 +104,12 @@ const executorRegistry = [
79
104
  },
80
105
  {
81
106
  type: 'vm2',
82
- preference: 2,
107
+ preference: 3,
83
108
  isAvailable: async () => {
109
+ // vm2 is fundamentally broken on Bun (prototype freezing issues)
110
+ // and Node.js built-in 'node:vm' is not safe for untrusted code.
111
+ if (isBun())
112
+ return false;
84
113
  try {
85
114
  await import('vm2');
86
115
  return true;
@@ -6,8 +6,8 @@
6
6
  * to our loopback server with the authorization code, which we capture and use
7
7
  * to complete the authorization flow.
8
8
  */
9
- import { createServer } from 'http';
10
- import { URL } from 'url';
9
+ import { createServer } from 'node:http';
10
+ import { URL } from 'node:url';
11
11
  /**
12
12
  * Loopback HTTP server that listens for OAuth2 redirect callbacks
13
13
  */
@@ -4,9 +4,9 @@
4
4
  * Persists OAuth tokens and client information to disk for reuse across sessions.
5
5
  * Stored in ~/.config/codemode-bridge/mcp-tokens.json
6
6
  */
7
- import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
8
- import { join } from 'path';
9
- import { homedir } from 'os';
7
+ import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'node:fs';
8
+ import { join } from 'node:path';
9
+ import { homedir } from 'node:os';
10
10
  /**
11
11
  * Manages OAuth token storage for MCP server connections
12
12
  */
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Environment detection utilities
3
+ */
4
+ /**
5
+ * Returns true if the current runtime is Node.js
6
+ */
7
+ export declare function isNode(): boolean;
8
+ /**
9
+ * Returns the Node.js major version, or 0 if not running on Node.js
10
+ */
11
+ export declare function getNodeMajorVersion(): number;
12
+ /**
13
+ * Returns true if the current runtime is Bun
14
+ */
15
+ export declare function isBun(): boolean;
16
+ /**
17
+ * Returns true if the current runtime is Deno
18
+ */
19
+ export declare function isDeno(): boolean;
20
+ /**
21
+ * Returns the name of the current runtime (Node.js, Bun, or Deno)
22
+ */
23
+ export declare function getRuntimeName(): string;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Environment detection utilities
3
+ */
4
+ /**
5
+ * Returns true if the current runtime is Node.js
6
+ */
7
+ export function isNode() {
8
+ return (typeof process !== 'undefined' &&
9
+ !!process.versions?.node &&
10
+ !isBun() &&
11
+ !isDeno());
12
+ }
13
+ /**
14
+ * Returns the Node.js major version, or 0 if not running on Node.js
15
+ */
16
+ export function getNodeMajorVersion() {
17
+ if (typeof process === 'undefined' || !process.versions?.node) {
18
+ return 0;
19
+ }
20
+ return parseInt(process.versions.node.split('.')[0], 10);
21
+ }
22
+ /**
23
+ * Returns true if the current runtime is Bun
24
+ */
25
+ export function isBun() {
26
+ return (typeof process !== 'undefined' &&
27
+ !!process.versions?.bun);
28
+ }
29
+ /**
30
+ * Returns true if the current runtime is Deno
31
+ */
32
+ export function isDeno() {
33
+ return ((typeof globalThis !== 'undefined' && !!globalThis.Deno) ||
34
+ (typeof process !== 'undefined' && !!process.versions?.deno));
35
+ }
36
+ /**
37
+ * Returns the name of the current runtime (Node.js, Bun, or Deno)
38
+ */
39
+ export function getRuntimeName() {
40
+ if (isBun())
41
+ return "Bun";
42
+ if (isDeno())
43
+ return "Deno";
44
+ if (isNode())
45
+ return "Node.js";
46
+ return "Unknown";
47
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruifung/codemode-bridge",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "MCP bridge that connects to upstream MCP servers and exposes tools via a single codemode tool for orchestration",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -15,7 +15,7 @@
15
15
  "prepare": "npm run build",
16
16
  "dev": "tsx src/cli/index.ts",
17
17
  "test": "vitest",
18
- "test:e2e": "vitest run src/mcp/e2e-bridge-runner.test.ts"
18
+ "test:e2e": "vitest run src/mcp/e2e-bridge-runner.test.ts --reporter=verbose"
19
19
  },
20
20
  "keywords": [
21
21
  "mcp",