@algochad/archcoder 2.0.2
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/README.md +113 -0
- package/bin/cli-entry.js +55 -0
- package/bin/cli-output.js +145 -0
- package/bin/cli.js +5108 -0
- package/bin/cli.test.js +56 -0
- package/dist/apple-touch-icon-120x120.png +0 -0
- package/dist/apple-touch-icon-152x152.png +0 -0
- package/dist/apple-touch-icon-167x167.png +0 -0
- package/dist/apple-touch-icon-180x180.png +0 -0
- package/dist/apple-touch-icon.png +0 -0
- package/dist/apple-touch-icon.svg +67 -0
- package/dist/assets/MultiRunWindow-BZp3MjJP.js +1 -0
- package/dist/assets/SettingsWindow-DoGYXpX7.js +1 -0
- package/dist/assets/TerminalView-BN7BR5Ff.js +3 -0
- package/dist/assets/TimelineDialog-ZQ33oVQR.js +1 -0
- package/dist/assets/ToolOutputDialog-Blv3pnug.js +16 -0
- package/dist/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
- package/dist/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
- package/dist/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
- package/dist/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
- package/dist/assets/ibm-plex-mono-latin-600-normal-BgSNZQsw.woff2 +0 -0
- package/dist/assets/ibm-plex-mono-latin-600-normal-DWFSQ4vo.woff +0 -0
- package/dist/assets/ibm-plex-sans-latin-400-normal-CDDApCn2.woff2 +0 -0
- package/dist/assets/ibm-plex-sans-latin-400-normal-CYLoc0-x.woff +0 -0
- package/dist/assets/ibm-plex-sans-latin-500-normal-6ng42L7E.woff2 +0 -0
- package/dist/assets/ibm-plex-sans-latin-500-normal-BgVn5rGT.woff +0 -0
- package/dist/assets/ibm-plex-sans-latin-600-normal-Cu4Hd6ag.woff +0 -0
- package/dist/assets/ibm-plex-sans-latin-600-normal-CuJfVYMP.woff2 +0 -0
- package/dist/assets/index-CtCEGYrr.css +1 -0
- package/dist/assets/index-o_d2wtWC.js +48 -0
- package/dist/assets/main-5QGBtzdq.css +1 -0
- package/dist/assets/main-B6oiMU86.js +8033 -0
- package/dist/assets/vendor--DbVqbJpV.css +1 -0
- package/dist/assets/vendor-.bun-HTKwyaEM.js +10086 -0
- package/dist/assets/wasm-CG6Dc4jp.js +1 -0
- package/dist/assets/worker-bqd4RMrj.js +155 -0
- package/dist/favicon-16.png +0 -0
- package/dist/favicon-32.png +0 -0
- package/dist/favicon.png +0 -0
- package/dist/favicon.svg +67 -0
- package/dist/index.html +533 -0
- package/dist/logo-dark-192x192.png +0 -0
- package/dist/logo-dark-512x512.svg +16 -0
- package/dist/logo-light-192x192.png +0 -0
- package/dist/logo-light-512x512.svg +16 -0
- package/dist/pwa-192.png +0 -0
- package/dist/pwa-512.png +0 -0
- package/dist/pwa-maskable-192.png +0 -0
- package/dist/pwa-maskable-512.png +0 -0
- package/dist/site.webmanifest +22 -0
- package/dist/sw.js +1 -0
- package/package.json +107 -0
- package/public/apple-touch-icon-120x120.png +0 -0
- package/public/apple-touch-icon-152x152.png +0 -0
- package/public/apple-touch-icon-167x167.png +0 -0
- package/public/apple-touch-icon-180x180.png +0 -0
- package/public/apple-touch-icon.png +0 -0
- package/public/apple-touch-icon.svg +67 -0
- package/public/favicon-16.png +0 -0
- package/public/favicon-32.png +0 -0
- package/public/favicon.png +0 -0
- package/public/favicon.svg +67 -0
- package/public/logo-dark-192x192.png +0 -0
- package/public/logo-dark-512x512.svg +16 -0
- package/public/logo-light-192x192.png +0 -0
- package/public/logo-light-512x512.svg +16 -0
- package/public/pwa-192.png +0 -0
- package/public/pwa-512.png +0 -0
- package/public/pwa-maskable-192.png +0 -0
- package/public/pwa-maskable-512.png +0 -0
- package/public/site.webmanifest +22 -0
- package/server/TERMINAL_INPUT_WS_PROTOCOL.md +44 -0
- package/server/index.d.ts +37 -0
- package/server/index.js +14694 -0
- package/server/lib/cloudflare-tunnel.js +650 -0
- package/server/lib/git/DOCUMENTATION.md +146 -0
- package/server/lib/git/credentials.js +74 -0
- package/server/lib/git/identity-storage.js +110 -0
- package/server/lib/git/index.js +6 -0
- package/server/lib/git/service.js +3117 -0
- package/server/lib/github/DOCUMENTATION.md +170 -0
- package/server/lib/github/auth.js +307 -0
- package/server/lib/github/device-flow.js +50 -0
- package/server/lib/github/index.js +24 -0
- package/server/lib/github/octokit.js +10 -0
- package/server/lib/github/pr-status.js +478 -0
- package/server/lib/github/repo/index.js +55 -0
- package/server/lib/installer/desktop.js +289 -0
- package/server/lib/installer/download.js +208 -0
- package/server/lib/installer/index.js +45 -0
- package/server/lib/installer/platform.js +100 -0
- package/server/lib/notifications/DOCUMENTATION.md +61 -0
- package/server/lib/notifications/index.js +1 -0
- package/server/lib/notifications/message.js +49 -0
- package/server/lib/notifications/message.test.js +59 -0
- package/server/lib/opencode/DOCUMENTATION.md +59 -0
- package/server/lib/opencode/agents.js +634 -0
- package/server/lib/opencode/auth.js +81 -0
- package/server/lib/opencode/commands.js +339 -0
- package/server/lib/opencode/index.js +66 -0
- package/server/lib/opencode/mcp.js +206 -0
- package/server/lib/opencode/providers.js +96 -0
- package/server/lib/opencode/shared.js +527 -0
- package/server/lib/opencode/skills.js +480 -0
- package/server/lib/opencode/tunnel-auth.js +591 -0
- package/server/lib/opencode/ui-auth.js +510 -0
- package/server/lib/package-manager.js +505 -0
- package/server/lib/quota/DOCUMENTATION.md +55 -0
- package/server/lib/quota/index.js +24 -0
- package/server/lib/quota/providers/claude.js +107 -0
- package/server/lib/quota/providers/codex.js +113 -0
- package/server/lib/quota/providers/copilot.js +165 -0
- package/server/lib/quota/providers/google/api.js +92 -0
- package/server/lib/quota/providers/google/auth.js +108 -0
- package/server/lib/quota/providers/google/index.js +124 -0
- package/server/lib/quota/providers/google/transforms.js +109 -0
- package/server/lib/quota/providers/index.js +152 -0
- package/server/lib/quota/providers/interface.js +55 -0
- package/server/lib/quota/providers/kimi.js +108 -0
- package/server/lib/quota/providers/minimax-cn-coding-plan.js +15 -0
- package/server/lib/quota/providers/minimax-coding-plan.js +15 -0
- package/server/lib/quota/providers/minimax-shared.js +136 -0
- package/server/lib/quota/providers/nanogpt.js +124 -0
- package/server/lib/quota/providers/ollama-cloud.js +112 -0
- package/server/lib/quota/providers/openai.js +91 -0
- package/server/lib/quota/providers/openrouter.js +92 -0
- package/server/lib/quota/providers/zai.js +91 -0
- package/server/lib/quota/utils/auth.js +46 -0
- package/server/lib/quota/utils/formatters.js +76 -0
- package/server/lib/quota/utils/index.js +10 -0
- package/server/lib/quota/utils/transformers.js +55 -0
- package/server/lib/skills-catalog/DOCUMENTATION.md +178 -0
- package/server/lib/skills-catalog/cache.js +32 -0
- package/server/lib/skills-catalog/clawdhub/api.js +158 -0
- package/server/lib/skills-catalog/clawdhub/index.js +30 -0
- package/server/lib/skills-catalog/clawdhub/install.js +238 -0
- package/server/lib/skills-catalog/clawdhub/scan.js +113 -0
- package/server/lib/skills-catalog/curated-sources.js +21 -0
- package/server/lib/skills-catalog/git.js +77 -0
- package/server/lib/skills-catalog/index.js +42 -0
- package/server/lib/skills-catalog/install.js +294 -0
- package/server/lib/skills-catalog/scan.js +221 -0
- package/server/lib/skills-catalog/source.js +85 -0
- package/server/lib/terminal/DOCUMENTATION.md +114 -0
- package/server/lib/terminal/index.js +12 -0
- package/server/lib/terminal/input-ws-protocol.js +66 -0
- package/server/lib/terminal/input-ws-protocol.test.js +138 -0
- package/server/lib/tts/DOCUMENTATION.md +134 -0
- package/server/lib/tts/index.js +16 -0
- package/server/lib/tts/service.js +162 -0
- package/server/lib/tts/summarization.js +171 -0
- package/server/lib/tunnels/index.js +166 -0
- package/server/lib/tunnels/providers/cloudflare.js +260 -0
- package/server/lib/tunnels/registry.js +51 -0
- package/server/lib/tunnels/types.js +219 -0
- package/server/lib/utils/lru.js +107 -0
- package/server/lib/utils/sse.js +121 -0
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
import { spawn, spawnSync } from 'child_process';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import yaml from 'yaml';
|
|
7
|
+
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = path.dirname(__filename);
|
|
10
|
+
|
|
11
|
+
const TRY_CF_URL_REGEX = /https:\/\/[a-z0-9-]+\.trycloudflare\.com/i;
|
|
12
|
+
|
|
13
|
+
const DEFAULT_STARTUP_TIMEOUT_MS = 30000;
|
|
14
|
+
const MANAGED_TUNNEL_STARTUP_TIMEOUT_MS = 20000;
|
|
15
|
+
const MANAGED_TUNNEL_LIVENESS_FALLBACK_MS = 6000;
|
|
16
|
+
const TUNNEL_MODE_QUICK = 'quick';
|
|
17
|
+
const TUNNEL_MODE_MANAGED_REMOTE = 'managed-remote';
|
|
18
|
+
const TUNNEL_MODE_MANAGED_LOCAL = 'managed-local';
|
|
19
|
+
|
|
20
|
+
async function searchPathFor(command) {
|
|
21
|
+
const pathValue = process.env.PATH || '';
|
|
22
|
+
const segments = pathValue.split(path.delimiter).filter(Boolean);
|
|
23
|
+
const WINDOWS_EXTENSIONS = process.platform === 'win32'
|
|
24
|
+
? (process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM')
|
|
25
|
+
.split(';')
|
|
26
|
+
.map((ext) => ext.trim().toLowerCase())
|
|
27
|
+
.filter(Boolean)
|
|
28
|
+
.map((ext) => (ext.startsWith('.') ? ext : `.${ext}`))
|
|
29
|
+
: [''];
|
|
30
|
+
|
|
31
|
+
for (const dir of segments) {
|
|
32
|
+
for (const ext of WINDOWS_EXTENSIONS) {
|
|
33
|
+
const fileName = process.platform === 'win32' ? `${command}${ext}` : command;
|
|
34
|
+
const candidate = path.join(dir, fileName);
|
|
35
|
+
try {
|
|
36
|
+
const stats = fs.statSync(candidate);
|
|
37
|
+
if (stats.isFile()) {
|
|
38
|
+
if (process.platform !== 'win32') {
|
|
39
|
+
try {
|
|
40
|
+
fs.accessSync(candidate, fs.constants.X_OK);
|
|
41
|
+
} catch {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return candidate;
|
|
46
|
+
}
|
|
47
|
+
} catch {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function checkCloudflaredAvailable() {
|
|
56
|
+
const cfPath = await searchPathFor('cloudflared');
|
|
57
|
+
if (cfPath) {
|
|
58
|
+
try {
|
|
59
|
+
const result = spawnSync(cfPath, ['--version'], {
|
|
60
|
+
encoding: 'utf8',
|
|
61
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
62
|
+
windowsHide: true,
|
|
63
|
+
});
|
|
64
|
+
if (result.status === 0) {
|
|
65
|
+
return { available: true, path: cfPath, version: result.stdout.trim() };
|
|
66
|
+
}
|
|
67
|
+
} catch {
|
|
68
|
+
// Ignore
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { available: false, path: null, version: null };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function printCloudflareTunnelInstallHelp() {
|
|
75
|
+
const platform = process.platform;
|
|
76
|
+
let installCmd = '';
|
|
77
|
+
|
|
78
|
+
if (platform === 'darwin') {
|
|
79
|
+
installCmd = 'brew install cloudflared';
|
|
80
|
+
} else if (platform === 'win32') {
|
|
81
|
+
installCmd = 'winget install --id Cloudflare.cloudflared';
|
|
82
|
+
} else {
|
|
83
|
+
installCmd = 'Download from https://github.com/cloudflare/cloudflared/releases';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
console.log(`
|
|
87
|
+
╔══════════════════════════════════════════════════════════════════╗
|
|
88
|
+
║ Cloudflare tunnel requires 'cloudflared' to be installed ║
|
|
89
|
+
╚══════════════════════════════════════════════════════════════════╝
|
|
90
|
+
|
|
91
|
+
Install instructions for your platform:
|
|
92
|
+
|
|
93
|
+
macOS: brew install cloudflared
|
|
94
|
+
Windows: winget install --id Cloudflare.cloudflared
|
|
95
|
+
Linux: Download from https://github.com/cloudflare/cloudflared/releases
|
|
96
|
+
|
|
97
|
+
Or visit: https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflared/downloads/
|
|
98
|
+
`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const spawnCloudflared = (args, envOverrides = {}, resolvedBinaryPath = 'cloudflared') => spawn(resolvedBinaryPath, args, {
|
|
102
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
103
|
+
windowsHide: true,
|
|
104
|
+
env: {
|
|
105
|
+
...process.env,
|
|
106
|
+
CF_TELEMETRY_DISABLE: '1',
|
|
107
|
+
...envOverrides,
|
|
108
|
+
},
|
|
109
|
+
killSignal: 'SIGINT',
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const normalizeHostname = (value) => {
|
|
113
|
+
if (typeof value !== 'string') {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
const trimmed = value.trim();
|
|
117
|
+
if (!trimmed) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const parsed = trimmed.includes('://') ? new URL(trimmed) : new URL(`https://${trimmed}`);
|
|
122
|
+
const hostname = parsed.hostname.trim().toLowerCase();
|
|
123
|
+
if (!hostname || hostname.includes('*')) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
return hostname;
|
|
127
|
+
} catch {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export function normalizeCloudflareTunnelHostname(value) {
|
|
133
|
+
return normalizeHostname(value);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export async function checkCloudflareApiReachability({ fetchImpl = globalThis.fetch, timeoutMs = 5000 } = {}) {
|
|
137
|
+
if (typeof fetchImpl !== 'function') {
|
|
138
|
+
return {
|
|
139
|
+
reachable: false,
|
|
140
|
+
status: null,
|
|
141
|
+
error: 'Fetch API is unavailable in this runtime.',
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const controller = new AbortController();
|
|
146
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
147
|
+
try {
|
|
148
|
+
const response = await fetchImpl('https://api.trycloudflare.com/', {
|
|
149
|
+
method: 'GET',
|
|
150
|
+
signal: controller.signal,
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
reachable: true,
|
|
154
|
+
status: response.status,
|
|
155
|
+
error: null,
|
|
156
|
+
};
|
|
157
|
+
} catch (error) {
|
|
158
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
159
|
+
return {
|
|
160
|
+
reachable: false,
|
|
161
|
+
status: null,
|
|
162
|
+
error: message,
|
|
163
|
+
};
|
|
164
|
+
} finally {
|
|
165
|
+
clearTimeout(timeout);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const READY_LOG_PATTERNS = [
|
|
170
|
+
/registered tunnel connection/i,
|
|
171
|
+
/connection[^\n]*registered/i,
|
|
172
|
+
/starting metrics server/i,
|
|
173
|
+
/connected to edge/i,
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
const MANAGED_LOCAL_CONFIG_MAX_BYTES = 256 * 1024;
|
|
177
|
+
const MANAGED_LOCAL_CONFIG_ALLOWED_EXTENSIONS = new Set(['.yml', '.yaml', '.json']);
|
|
178
|
+
|
|
179
|
+
const FATAL_LOG_PATTERNS = [
|
|
180
|
+
/error parsing.*config/i,
|
|
181
|
+
/failed to .*config/i,
|
|
182
|
+
/invalid token/i,
|
|
183
|
+
/unauthorized/i,
|
|
184
|
+
/credentials file .* not found/i,
|
|
185
|
+
/provided tunnel credentials are invalid/i,
|
|
186
|
+
];
|
|
187
|
+
|
|
188
|
+
function isCloudflaredReadyLogLine(line) {
|
|
189
|
+
if (!line) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
return READY_LOG_PATTERNS.some((pattern) => pattern.test(line));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function isCloudflaredFatalLogLine(line) {
|
|
196
|
+
if (!line) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
return FATAL_LOG_PATTERNS.some((pattern) => pattern.test(line));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function assertReadableFile(filePath, contextLabel) {
|
|
203
|
+
let stats;
|
|
204
|
+
try {
|
|
205
|
+
stats = fs.statSync(filePath);
|
|
206
|
+
} catch {
|
|
207
|
+
throw new Error(`${contextLabel} file was not found. Select a valid cloudflared config file.`);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (!stats.isFile()) {
|
|
211
|
+
throw new Error(`${contextLabel} path is not a file. Select a cloudflared config file.`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const extension = path.extname(filePath).toLowerCase();
|
|
215
|
+
if (!MANAGED_LOCAL_CONFIG_ALLOWED_EXTENSIONS.has(extension)) {
|
|
216
|
+
throw new Error(`${contextLabel} must be a .yml, .yaml, or .json file.`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (stats.size <= 0) {
|
|
220
|
+
throw new Error(`${contextLabel} file is empty.`);
|
|
221
|
+
}
|
|
222
|
+
if (stats.size > MANAGED_LOCAL_CONFIG_MAX_BYTES) {
|
|
223
|
+
throw new Error(`${contextLabel} file is too large (max ${MANAGED_LOCAL_CONFIG_MAX_BYTES} bytes).`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
try {
|
|
227
|
+
fs.accessSync(filePath, fs.constants.R_OK);
|
|
228
|
+
} catch {
|
|
229
|
+
throw new Error(`${contextLabel} file is not readable. Check file permissions and try again.`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function extractHostnameFromCloudflaredConfigDetailed(configPath) {
|
|
234
|
+
if (typeof configPath !== 'string' || configPath.trim().length === 0) {
|
|
235
|
+
return { hostname: null, parseError: null };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
let raw;
|
|
239
|
+
try {
|
|
240
|
+
raw = fs.readFileSync(configPath, 'utf8');
|
|
241
|
+
} catch {
|
|
242
|
+
return {
|
|
243
|
+
hostname: null,
|
|
244
|
+
parseError: new Error('Could not read the managed local tunnel config file. Check that the file exists and is accessible.'),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let parsed;
|
|
249
|
+
try {
|
|
250
|
+
parsed = yaml.parse(raw);
|
|
251
|
+
} catch {
|
|
252
|
+
return {
|
|
253
|
+
hostname: null,
|
|
254
|
+
parseError: new Error('Managed local tunnel config is invalid. Use a valid cloudflared YAML/JSON config file.'),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const ingress = Array.isArray(parsed?.ingress) ? parsed.ingress : [];
|
|
259
|
+
for (const rule of ingress) {
|
|
260
|
+
const hostname = normalizeHostname(rule?.hostname);
|
|
261
|
+
if (hostname) {
|
|
262
|
+
return { hostname, parseError: null };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return { hostname: null, parseError: null };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const extractHostnameFromCloudflaredConfig = (configPath) => {
|
|
270
|
+
return extractHostnameFromCloudflaredConfigDetailed(configPath).hostname;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const getDefaultCloudflaredConfigPath = () => path.join(os.homedir(), '.cloudflared', 'config.yml');
|
|
274
|
+
|
|
275
|
+
export function inspectManagedLocalCloudflareConfig({ configPath, hostname } = {}) {
|
|
276
|
+
const requestedPath = typeof configPath === 'string' ? configPath.trim() : '';
|
|
277
|
+
const effectiveConfigPath = requestedPath || getDefaultCloudflaredConfigPath();
|
|
278
|
+
|
|
279
|
+
try {
|
|
280
|
+
if (requestedPath) {
|
|
281
|
+
assertReadableFile(effectiveConfigPath, 'Managed local tunnel config');
|
|
282
|
+
} else {
|
|
283
|
+
assertReadableFile(effectiveConfigPath, 'Managed local tunnel default config');
|
|
284
|
+
}
|
|
285
|
+
} catch (error) {
|
|
286
|
+
return {
|
|
287
|
+
ok: false,
|
|
288
|
+
effectiveConfigPath,
|
|
289
|
+
resolvedHostname: null,
|
|
290
|
+
error: error instanceof Error ? error.message : String(error),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const configHostnameResult = extractHostnameFromCloudflaredConfigDetailed(effectiveConfigPath);
|
|
295
|
+
if (configHostnameResult.parseError) {
|
|
296
|
+
return {
|
|
297
|
+
ok: false,
|
|
298
|
+
effectiveConfigPath,
|
|
299
|
+
resolvedHostname: null,
|
|
300
|
+
error: configHostnameResult.parseError.message,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const resolvedHostname = normalizeHostname(hostname) || configHostnameResult.hostname;
|
|
305
|
+
if (!resolvedHostname) {
|
|
306
|
+
return {
|
|
307
|
+
ok: false,
|
|
308
|
+
effectiveConfigPath,
|
|
309
|
+
resolvedHostname: null,
|
|
310
|
+
error: 'Managed local tunnel hostname is required (set --hostname or include ingress hostname in config).',
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
ok: true,
|
|
316
|
+
effectiveConfigPath,
|
|
317
|
+
resolvedHostname,
|
|
318
|
+
error: null,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async function waitForManagedTunnelReady(child, { modeLabel }) {
|
|
323
|
+
await new Promise((resolve, reject) => {
|
|
324
|
+
let settled = false;
|
|
325
|
+
let sawOutput = false;
|
|
326
|
+
|
|
327
|
+
const finish = (handler, value) => {
|
|
328
|
+
if (settled) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
settled = true;
|
|
332
|
+
clearTimeout(fallbackTimer);
|
|
333
|
+
clearTimeout(hardTimeout);
|
|
334
|
+
child.stdout?.off('data', onStdout);
|
|
335
|
+
child.stderr?.off('data', onStderr);
|
|
336
|
+
child.off('exit', onExit);
|
|
337
|
+
handler(value);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const inspectChunk = (chunk) => {
|
|
341
|
+
const text = chunk.toString('utf8');
|
|
342
|
+
if (text.trim().length > 0) {
|
|
343
|
+
sawOutput = true;
|
|
344
|
+
}
|
|
345
|
+
const lines = text.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
|
346
|
+
for (const line of lines) {
|
|
347
|
+
if (isCloudflaredReadyLogLine(line)) {
|
|
348
|
+
finish(resolve, null);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (isCloudflaredFatalLogLine(line)) {
|
|
352
|
+
finish(reject, new Error(`Cloudflared failed to start ${modeLabel}: ${line}`));
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const onStdout = (chunk) => {
|
|
359
|
+
inspectChunk(chunk);
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
const onStderr = (chunk) => {
|
|
363
|
+
inspectChunk(chunk);
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const onExit = (code) => {
|
|
367
|
+
finish(reject, new Error(`Cloudflared exited while starting ${modeLabel} (code ${code ?? 'unknown'})`));
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
child.stdout?.on('data', onStdout);
|
|
371
|
+
child.stderr?.on('data', onStderr);
|
|
372
|
+
child.once('exit', onExit);
|
|
373
|
+
|
|
374
|
+
const fallbackTimer = setTimeout(() => {
|
|
375
|
+
if (sawOutput) {
|
|
376
|
+
finish(resolve, null);
|
|
377
|
+
}
|
|
378
|
+
}, MANAGED_TUNNEL_LIVENESS_FALLBACK_MS);
|
|
379
|
+
|
|
380
|
+
const hardTimeout = setTimeout(() => {
|
|
381
|
+
finish(reject, new Error(`Timed out waiting for cloudflared to initialize ${modeLabel}. Check your tunnel config and credentials.`));
|
|
382
|
+
}, MANAGED_TUNNEL_STARTUP_TIMEOUT_MS);
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export async function startCloudflareQuickTunnel({ originUrl }) {
|
|
387
|
+
const cfCheck = await checkCloudflaredAvailable();
|
|
388
|
+
|
|
389
|
+
if (!cfCheck.available) {
|
|
390
|
+
printCloudflareTunnelInstallHelp();
|
|
391
|
+
throw new Error('cloudflared is not installed');
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
console.log(`Using cloudflared: ${cfCheck.path} (${cfCheck.version})`);
|
|
395
|
+
|
|
396
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'archcoder-cf-'));
|
|
397
|
+
|
|
398
|
+
const child = spawnCloudflared(['tunnel', '--url', originUrl], { HOME: tempDir }, cfCheck.path);
|
|
399
|
+
|
|
400
|
+
let publicUrl = null;
|
|
401
|
+
let tunnelReady = false;
|
|
402
|
+
|
|
403
|
+
const onData = (chunk, isStderr) => {
|
|
404
|
+
const text = chunk.toString('utf8');
|
|
405
|
+
|
|
406
|
+
if (!tunnelReady) {
|
|
407
|
+
const match = text.match(TRY_CF_URL_REGEX);
|
|
408
|
+
if (match) {
|
|
409
|
+
publicUrl = match[0];
|
|
410
|
+
tunnelReady = true;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
process.stderr.write(isStderr ? text : '');
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
child.stdout.on('data', (chunk) => onData(chunk, false));
|
|
418
|
+
child.stderr.on('data', (chunk) => onData(chunk, true));
|
|
419
|
+
|
|
420
|
+
child.on('error', (error) => {
|
|
421
|
+
console.error(`Cloudflared error: ${error.message}`);
|
|
422
|
+
cleanupTempDir();
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
const cleanupTempDir = () => {
|
|
426
|
+
try {
|
|
427
|
+
if (fs.existsSync(tempDir)) {
|
|
428
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
429
|
+
}
|
|
430
|
+
} catch {
|
|
431
|
+
// Ignore cleanup errors
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
await new Promise((resolve, reject) => {
|
|
436
|
+
const timeout = setTimeout(() => {
|
|
437
|
+
if (!publicUrl) {
|
|
438
|
+
try { child.kill('SIGINT'); } catch { /* ignore */ }
|
|
439
|
+
cleanupTempDir();
|
|
440
|
+
reject(new Error('Tunnel URL not received within 30 seconds'));
|
|
441
|
+
}
|
|
442
|
+
}, DEFAULT_STARTUP_TIMEOUT_MS);
|
|
443
|
+
|
|
444
|
+
const checkReady = setInterval(() => {
|
|
445
|
+
if (publicUrl) {
|
|
446
|
+
clearTimeout(timeout);
|
|
447
|
+
clearInterval(checkReady);
|
|
448
|
+
resolve(null);
|
|
449
|
+
}
|
|
450
|
+
}, 100);
|
|
451
|
+
|
|
452
|
+
child.on('exit', (code) => {
|
|
453
|
+
clearTimeout(timeout);
|
|
454
|
+
clearInterval(checkReady);
|
|
455
|
+
cleanupTempDir();
|
|
456
|
+
if (code !== null && code !== 0) {
|
|
457
|
+
reject(new Error(`Cloudflared exited with code ${code}`));
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
return {
|
|
463
|
+
mode: TUNNEL_MODE_QUICK,
|
|
464
|
+
stop: () => {
|
|
465
|
+
try {
|
|
466
|
+
child.kill('SIGINT');
|
|
467
|
+
} catch {
|
|
468
|
+
// Ignore
|
|
469
|
+
}
|
|
470
|
+
},
|
|
471
|
+
process: child,
|
|
472
|
+
getPublicUrl: () => publicUrl,
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export async function startCloudflareManagedRemoteTunnel({ token, hostname, tokenFilePath }) {
|
|
477
|
+
const cfCheck = await checkCloudflaredAvailable();
|
|
478
|
+
|
|
479
|
+
if (!cfCheck.available) {
|
|
480
|
+
printCloudflareTunnelInstallHelp();
|
|
481
|
+
throw new Error('cloudflared is not installed');
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const normalizedToken = typeof token === 'string' ? token.trim() : '';
|
|
485
|
+
const normalizedHost = typeof hostname === 'string' ? hostname.trim().toLowerCase() : '';
|
|
486
|
+
|
|
487
|
+
if (!normalizedToken) {
|
|
488
|
+
throw new Error('Managed remote tunnel token is required');
|
|
489
|
+
}
|
|
490
|
+
if (!normalizedHost) {
|
|
491
|
+
throw new Error('Managed remote tunnel hostname is required');
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
let effectiveTokenFilePath = typeof tokenFilePath === 'string' ? tokenFilePath : null;
|
|
495
|
+
let tempTokenFile = null;
|
|
496
|
+
|
|
497
|
+
if (!effectiveTokenFilePath) {
|
|
498
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'archcoder-cf-token-'));
|
|
499
|
+
effectiveTokenFilePath = path.join(tempDir, 'token');
|
|
500
|
+
fs.writeFileSync(effectiveTokenFilePath, normalizedToken, { encoding: 'utf8', mode: 0o600 });
|
|
501
|
+
tempTokenFile = { dir: tempDir, path: effectiveTokenFilePath };
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
const child = spawnCloudflared(['tunnel', 'run', '--token-file', effectiveTokenFilePath], {}, cfCheck.path);
|
|
505
|
+
const publicUrl = `https://${normalizedHost}`;
|
|
506
|
+
|
|
507
|
+
child.stdout.on('data', () => {
|
|
508
|
+
// Keep stream drained, but avoid logging potentially sensitive output.
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
child.stderr.on('data', (chunk) => {
|
|
512
|
+
const text = chunk.toString('utf8');
|
|
513
|
+
process.stderr.write(text);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
const cleanupTempTokenFile = () => {
|
|
517
|
+
if (tempTokenFile) {
|
|
518
|
+
try {
|
|
519
|
+
if (fs.existsSync(tempTokenFile.dir)) {
|
|
520
|
+
fs.rmSync(tempTokenFile.dir, { recursive: true, force: true });
|
|
521
|
+
}
|
|
522
|
+
} catch {
|
|
523
|
+
// Ignore cleanup errors
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
child.on('error', (error) => {
|
|
529
|
+
console.error(`Cloudflared error: ${error.message}`);
|
|
530
|
+
cleanupTempTokenFile();
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
child.on('exit', () => {
|
|
534
|
+
cleanupTempTokenFile();
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
try {
|
|
538
|
+
await waitForManagedTunnelReady(child, { modeLabel: 'managed-remote tunnel' });
|
|
539
|
+
} catch (error) {
|
|
540
|
+
try { child.kill('SIGINT'); } catch { /* ignore */ }
|
|
541
|
+
cleanupTempTokenFile();
|
|
542
|
+
throw error;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return {
|
|
546
|
+
mode: TUNNEL_MODE_MANAGED_REMOTE,
|
|
547
|
+
stop: () => {
|
|
548
|
+
try {
|
|
549
|
+
child.kill('SIGINT');
|
|
550
|
+
} catch {
|
|
551
|
+
// Ignore
|
|
552
|
+
}
|
|
553
|
+
cleanupTempTokenFile();
|
|
554
|
+
},
|
|
555
|
+
process: child,
|
|
556
|
+
getPublicUrl: () => publicUrl,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
export async function startCloudflareManagedLocalTunnel({ configPath, hostname }) {
|
|
561
|
+
const cfCheck = await checkCloudflaredAvailable();
|
|
562
|
+
|
|
563
|
+
if (!cfCheck.available) {
|
|
564
|
+
printCloudflareTunnelInstallHelp();
|
|
565
|
+
throw new Error('cloudflared is not installed');
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
const requestedPath = typeof configPath === 'string' ? configPath.trim() : '';
|
|
569
|
+
const effectiveConfigPath = requestedPath || getDefaultCloudflaredConfigPath();
|
|
570
|
+
|
|
571
|
+
if (requestedPath) {
|
|
572
|
+
assertReadableFile(effectiveConfigPath, 'Managed local tunnel config');
|
|
573
|
+
} else {
|
|
574
|
+
assertReadableFile(effectiveConfigPath, 'Managed local tunnel default config');
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const configHostnameResult = extractHostnameFromCloudflaredConfigDetailed(effectiveConfigPath);
|
|
578
|
+
if (configHostnameResult.parseError) {
|
|
579
|
+
throw configHostnameResult.parseError;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
const resolvedHost = normalizeHostname(hostname) || configHostnameResult.hostname;
|
|
583
|
+
|
|
584
|
+
if (!resolvedHost) {
|
|
585
|
+
throw new Error('Managed local tunnel hostname is required (use --tunnel-hostname or add an ingress hostname to the cloudflared config)');
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const args = ['tunnel'];
|
|
589
|
+
if (requestedPath) {
|
|
590
|
+
args.push('--config', effectiveConfigPath);
|
|
591
|
+
}
|
|
592
|
+
args.push('run');
|
|
593
|
+
|
|
594
|
+
const child = spawnCloudflared(args, {}, cfCheck.path);
|
|
595
|
+
const publicUrl = `https://${resolvedHost}`;
|
|
596
|
+
|
|
597
|
+
child.stdout.on('data', () => {
|
|
598
|
+
// Keep stream drained, but avoid logging potentially sensitive output.
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
child.stderr.on('data', (chunk) => {
|
|
602
|
+
const text = chunk.toString('utf8');
|
|
603
|
+
process.stderr.write(text);
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
child.on('error', (error) => {
|
|
607
|
+
console.error(`Cloudflared error: ${error.message}`);
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
try {
|
|
611
|
+
await waitForManagedTunnelReady(child, { modeLabel: 'managed-local tunnel' });
|
|
612
|
+
} catch (error) {
|
|
613
|
+
try { child.kill('SIGINT'); } catch { /* ignore */ }
|
|
614
|
+
throw error;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
return {
|
|
618
|
+
mode: TUNNEL_MODE_MANAGED_LOCAL,
|
|
619
|
+
stop: () => {
|
|
620
|
+
try {
|
|
621
|
+
child.kill('SIGINT');
|
|
622
|
+
} catch {
|
|
623
|
+
// Ignore
|
|
624
|
+
}
|
|
625
|
+
},
|
|
626
|
+
process: child,
|
|
627
|
+
getPublicUrl: () => publicUrl,
|
|
628
|
+
getResolvedHostname: () => resolvedHost,
|
|
629
|
+
getEffectiveConfigPath: () => effectiveConfigPath,
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export async function startCloudflareTunnel({ originUrl, port }) {
|
|
634
|
+
void port;
|
|
635
|
+
return startCloudflareQuickTunnel({ originUrl });
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
export function printTunnelWarning() {
|
|
639
|
+
console.log(`
|
|
640
|
+
⚠️ Cloudflare Quick Tunnel Limitations:
|
|
641
|
+
|
|
642
|
+
• Maximum 200 concurrent requests
|
|
643
|
+
• Server-Sent Events (SSE) are NOT supported
|
|
644
|
+
• URLs are temporary and will expire when the tunnel stops
|
|
645
|
+
• Password protection is required for tunnel access
|
|
646
|
+
|
|
647
|
+
For production use, set up a managed remote Cloudflare Tunnel:
|
|
648
|
+
https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel/
|
|
649
|
+
`);
|
|
650
|
+
}
|