Swadhin Biswas feat: implement mervelas cli engine, built-in tools, and reactive UI · 4/3/2026
0358986
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import type * as https from 'https'
import { Agent as HttpsAgent } from 'https'
import memoize from 'lodash-es/memoize.js'
import type * as tls from 'tls'
import type * as undici from 'undici'
import { getCACertificates } from './caCerts.js'
import { logForDebugging } from './debug.js'
import { getFsImplementation } from './fsOperations.js'
export type MTLSConfig = {
cert?: string
key?: string
passphrase?: string
}
export type TLSConfig = MTLSConfig & {
ca?: string | string[] | Buffer
}
/**
* Get mTLS configuration from environment variables
*/
export const getMTLSConfig = memoize((): MTLSConfig | undefined => {
const config: MTLSConfig = {}
// Note: NODE_EXTRA_CA_CERTS is automatically handled by Node.js at runtime
// We don't need to manually load it - Node.js appends it to the built-in CAs automatically
// Client certificate
if (process.env.CLAUDE_CODE_CLIENT_CERT) {
try {
config.cert = getFsImplementation().readFileSync(
process.env.CLAUDE_CODE_CLIENT_CERT,
{ encoding: 'utf8' },
)
logForDebugging(
'mTLS: Loaded client certificate from CLAUDE_CODE_CLIENT_CERT',
)
} catch (error) {
logForDebugging(`mTLS: Failed to load client certificate: ${error}`, {
level: 'error',
})
}
}
// Client key
if (process.env.CLAUDE_CODE_CLIENT_KEY) {
try {
config.key = getFsImplementation().readFileSync(
process.env.CLAUDE_CODE_CLIENT_KEY,
{ encoding: 'utf8' },
)
logForDebugging('mTLS: Loaded client key from CLAUDE_CODE_CLIENT_KEY')
} catch (error) {
logForDebugging(`mTLS: Failed to load client key: ${error}`, {
level: 'error',
})
}
}
// Key passphrase
if (process.env.CLAUDE_CODE_CLIENT_KEY_PASSPHRASE) {
config.passphrase = process.env.CLAUDE_CODE_CLIENT_KEY_PASSPHRASE
logForDebugging('mTLS: Using client key passphrase')
}
// Only return config if at least one option is set
if (Object.keys(config).length === 0) {
return undefined
}
return config
})
/**
* Create an HTTPS agent with mTLS configuration
*/
export const getMTLSAgent = memoize((): HttpsAgent | undefined => {
const mtlsConfig = getMTLSConfig()
const caCerts = getCACertificates()
if (!mtlsConfig && !caCerts) {
return undefined
}
const agentOptions: https.AgentOptions = {
...mtlsConfig,
...(caCerts && { ca: caCerts }),
// Enable keep-alive for better performance
keepAlive: true,
}
logForDebugging('mTLS: Creating HTTPS agent with custom certificates')
return new HttpsAgent(agentOptions)
})
/**
* Get TLS options for WebSocket connections
*/
export function getWebSocketTLSOptions(): tls.ConnectionOptions | undefined {
const mtlsConfig = getMTLSConfig()
const caCerts = getCACertificates()
if (!mtlsConfig && !caCerts) {
return undefined
}
return {
...mtlsConfig,
...(caCerts && { ca: caCerts }),
}
}
/**
* Get fetch options with TLS configuration (mTLS + CA certs) for undici
*/
export function getTLSFetchOptions(): {
tls?: TLSConfig
dispatcher?: undici.Dispatcher
} {
const mtlsConfig = getMTLSConfig()
const caCerts = getCACertificates()
if (!mtlsConfig && !caCerts) {
return {}
}
const tlsConfig: TLSConfig = {
...mtlsConfig,
...(caCerts && { ca: caCerts }),
}
if (typeof Bun !== 'undefined') {
return { tls: tlsConfig }
}
logForDebugging('TLS: Created undici agent with custom certificates')
// Create a custom undici Agent with TLS options. Lazy-required so that
// the ~1.5MB undici package is only loaded when mTLS/CA certs are configured.
// eslint-disable-next-line @typescript-eslint/no-require-imports
const undiciMod = require('undici') as typeof undici
const agent = new undiciMod.Agent({
connect: {
cert: tlsConfig.cert,
key: tlsConfig.key,
passphrase: tlsConfig.passphrase,
...(tlsConfig.ca && { ca: tlsConfig.ca }),
},
pipelining: 1,
})
return { dispatcher: agent }
}
/**
* Clear the mTLS configuration cache.
*/
export function clearMTLSCache(): void {
getMTLSConfig.cache.clear?.()
getMTLSAgent.cache.clear?.()
logForDebugging('Cleared mTLS configuration cache')
}
/**
* Configure global Node.js TLS settings
*/
export function configureGlobalMTLS(): void {
const mtlsConfig = getMTLSConfig()
if (!mtlsConfig) {
return
}
// NODE_EXTRA_CA_CERTS is automatically handled by Node.js at runtime
if (process.env.NODE_EXTRA_CA_CERTS) {
logForDebugging(
'NODE_EXTRA_CA_CERTS detected - Node.js will automatically append to built-in CAs',
)
}
}