@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,260 @@
|
|
|
1
|
+
import {
|
|
2
|
+
checkCloudflareApiReachability,
|
|
3
|
+
checkCloudflaredAvailable,
|
|
4
|
+
inspectManagedLocalCloudflareConfig,
|
|
5
|
+
normalizeCloudflareTunnelHostname,
|
|
6
|
+
startCloudflareManagedLocalTunnel,
|
|
7
|
+
startCloudflareManagedRemoteTunnel,
|
|
8
|
+
startCloudflareQuickTunnel,
|
|
9
|
+
} from '../../cloudflare-tunnel.js';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
TUNNEL_INTENT_EPHEMERAL_PUBLIC,
|
|
13
|
+
TUNNEL_INTENT_PERSISTENT_PUBLIC,
|
|
14
|
+
TUNNEL_MODE_MANAGED_LOCAL,
|
|
15
|
+
TUNNEL_MODE_MANAGED_REMOTE,
|
|
16
|
+
TUNNEL_MODE_QUICK,
|
|
17
|
+
TUNNEL_PROVIDER_CLOUDFLARE,
|
|
18
|
+
TunnelServiceError,
|
|
19
|
+
} from '../types.js';
|
|
20
|
+
|
|
21
|
+
export const cloudflareTunnelProviderCapabilities = {
|
|
22
|
+
provider: TUNNEL_PROVIDER_CLOUDFLARE,
|
|
23
|
+
defaults: {
|
|
24
|
+
mode: TUNNEL_MODE_QUICK,
|
|
25
|
+
optionDefaults: {},
|
|
26
|
+
},
|
|
27
|
+
modes: [
|
|
28
|
+
{
|
|
29
|
+
key: TUNNEL_MODE_QUICK,
|
|
30
|
+
label: 'Quick Tunnel',
|
|
31
|
+
intent: TUNNEL_INTENT_EPHEMERAL_PUBLIC,
|
|
32
|
+
requires: [],
|
|
33
|
+
supports: ['sessionTTL'],
|
|
34
|
+
stability: 'ga',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
key: TUNNEL_MODE_MANAGED_REMOTE,
|
|
38
|
+
label: 'Managed Remote Tunnel',
|
|
39
|
+
intent: TUNNEL_INTENT_PERSISTENT_PUBLIC,
|
|
40
|
+
requires: ['token', 'hostname'],
|
|
41
|
+
supports: ['customDomain', 'sessionTTL'],
|
|
42
|
+
stability: 'ga',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
key: TUNNEL_MODE_MANAGED_LOCAL,
|
|
46
|
+
label: 'Managed Local Tunnel',
|
|
47
|
+
intent: TUNNEL_INTENT_PERSISTENT_PUBLIC,
|
|
48
|
+
requires: [],
|
|
49
|
+
supports: ['configFile', 'customDomain', 'sessionTTL'],
|
|
50
|
+
stability: 'ga',
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export function createCloudflareTunnelProvider() {
|
|
56
|
+
const validateTokenShape = (value) => {
|
|
57
|
+
if (typeof value !== 'string') {
|
|
58
|
+
return { ok: false, detail: 'Managed remote token is missing.' };
|
|
59
|
+
}
|
|
60
|
+
const trimmed = value.trim();
|
|
61
|
+
if (!trimmed) {
|
|
62
|
+
return { ok: false, detail: 'Managed remote token is missing.' };
|
|
63
|
+
}
|
|
64
|
+
if (/\s/.test(trimmed)) {
|
|
65
|
+
return { ok: false, detail: 'Managed remote token has whitespace; provide the raw token value.' };
|
|
66
|
+
}
|
|
67
|
+
return { ok: true, detail: 'Managed remote token looks valid.' };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const createModeSummary = (checks) => {
|
|
71
|
+
const failures = checks.filter((entry) => entry.status === 'fail').length;
|
|
72
|
+
const warnings = checks.filter((entry) => entry.status === 'warn').length;
|
|
73
|
+
return {
|
|
74
|
+
ready: failures === 0,
|
|
75
|
+
failures,
|
|
76
|
+
warnings,
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const describeMode = ({ mode, checks }) => {
|
|
81
|
+
const summary = createModeSummary(checks);
|
|
82
|
+
const blockers = checks
|
|
83
|
+
.filter((entry) => entry.status === 'fail' && entry.id !== 'startup_readiness')
|
|
84
|
+
.map((entry) => entry.detail || entry.label || entry.id);
|
|
85
|
+
return {
|
|
86
|
+
mode,
|
|
87
|
+
checks,
|
|
88
|
+
summary,
|
|
89
|
+
ready: summary.ready,
|
|
90
|
+
blockers,
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
id: TUNNEL_PROVIDER_CLOUDFLARE,
|
|
96
|
+
capabilities: cloudflareTunnelProviderCapabilities,
|
|
97
|
+
checkAvailability: async () => {
|
|
98
|
+
const result = await checkCloudflaredAvailable();
|
|
99
|
+
if (result.available) {
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
...result,
|
|
104
|
+
message: 'cloudflared is not installed. Install it with: brew install cloudflared',
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
diagnose: async (request = {}) => {
|
|
108
|
+
const dependency = await checkCloudflaredAvailable();
|
|
109
|
+
const network = await checkCloudflareApiReachability();
|
|
110
|
+
|
|
111
|
+
const providerChecks = [
|
|
112
|
+
{
|
|
113
|
+
id: 'dependency',
|
|
114
|
+
label: 'cloudflared installed',
|
|
115
|
+
status: dependency.available ? 'pass' : 'fail',
|
|
116
|
+
detail: dependency.available
|
|
117
|
+
? (dependency.version || dependency.path || 'cloudflared available')
|
|
118
|
+
: 'cloudflared is not installed. Install it with: brew install cloudflared',
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: 'network',
|
|
122
|
+
label: 'Cloudflare API reachable',
|
|
123
|
+
status: network.reachable ? 'pass' : 'fail',
|
|
124
|
+
detail: network.reachable
|
|
125
|
+
? (network.status ? `HTTP ${network.status}` : 'Reachable')
|
|
126
|
+
: (network.error || 'Could not reach api.trycloudflare.com'),
|
|
127
|
+
},
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
const startupReady = dependency.available && network.reachable;
|
|
131
|
+
const startupDetail = startupReady
|
|
132
|
+
? 'Provider dependency and network checks passed.'
|
|
133
|
+
: 'Resolve provider checks before starting tunnels.';
|
|
134
|
+
|
|
135
|
+
const quickChecks = [
|
|
136
|
+
{
|
|
137
|
+
id: 'startup_readiness',
|
|
138
|
+
label: 'Provider startup readiness',
|
|
139
|
+
status: startupReady ? 'pass' : 'fail',
|
|
140
|
+
detail: startupDetail,
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
id: 'quick_mode_prerequisites',
|
|
144
|
+
label: 'Quick tunnel prerequisites',
|
|
145
|
+
status: network.reachable ? 'pass' : 'fail',
|
|
146
|
+
detail: network.reachable
|
|
147
|
+
? 'Cloudflare edge is reachable for quick tunnels.'
|
|
148
|
+
: 'Cloudflare edge is not reachable for quick tunnels.',
|
|
149
|
+
},
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
const managedLocalInspection = inspectManagedLocalCloudflareConfig({
|
|
153
|
+
configPath: request.configPath,
|
|
154
|
+
hostname: request.hostname,
|
|
155
|
+
});
|
|
156
|
+
const managedLocalChecks = [
|
|
157
|
+
{
|
|
158
|
+
id: 'startup_readiness',
|
|
159
|
+
label: 'Provider startup readiness',
|
|
160
|
+
status: startupReady ? 'pass' : 'fail',
|
|
161
|
+
detail: startupDetail,
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
id: 'managed_local_config',
|
|
165
|
+
label: 'Managed local config',
|
|
166
|
+
status: managedLocalInspection.ok ? 'pass' : 'fail',
|
|
167
|
+
detail: managedLocalInspection.ok
|
|
168
|
+
? `${managedLocalInspection.effectiveConfigPath}${managedLocalInspection.resolvedHostname ? ` (${managedLocalInspection.resolvedHostname})` : ''}`
|
|
169
|
+
: managedLocalInspection.error,
|
|
170
|
+
},
|
|
171
|
+
];
|
|
172
|
+
|
|
173
|
+
const normalizedHost = normalizeCloudflareTunnelHostname(request.hostname);
|
|
174
|
+
const hostnameMissing = !normalizedHost;
|
|
175
|
+
const remoteTokenValidation = validateTokenShape(request.token);
|
|
176
|
+
const tokenMissing = typeof request.token !== 'string' || request.token.trim().length === 0;
|
|
177
|
+
const hasSavedManagedRemoteProfile = request.hasSavedManagedRemoteProfile === true;
|
|
178
|
+
const tokenProvided = request.tokenProvided === true;
|
|
179
|
+
const hostnameProvided = request.hostnameProvided === true;
|
|
180
|
+
const hasExplicitManagedRemoteInput = tokenProvided || hostnameProvided;
|
|
181
|
+
const canUseSavedProfileForHostname = !hasExplicitManagedRemoteInput && hostnameMissing && hasSavedManagedRemoteProfile;
|
|
182
|
+
const canUseSavedProfileForToken = !hasExplicitManagedRemoteInput && tokenMissing && hasSavedManagedRemoteProfile;
|
|
183
|
+
const savedProfileReadyDetail = 'at least one saved profile present';
|
|
184
|
+
const managedRemoteChecks = [
|
|
185
|
+
{
|
|
186
|
+
id: 'startup_readiness',
|
|
187
|
+
label: 'Provider startup readiness',
|
|
188
|
+
status: startupReady ? 'pass' : 'fail',
|
|
189
|
+
detail: startupDetail,
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
id: 'managed_remote_hostname',
|
|
193
|
+
label: 'Managed remote hostname',
|
|
194
|
+
status: normalizedHost || canUseSavedProfileForHostname ? 'pass' : 'fail',
|
|
195
|
+
detail: normalizedHost
|
|
196
|
+
? normalizedHost
|
|
197
|
+
: canUseSavedProfileForHostname
|
|
198
|
+
? savedProfileReadyDetail
|
|
199
|
+
: 'Managed remote hostname is required (use --hostname).',
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
id: 'managed_remote_token',
|
|
203
|
+
label: 'Managed remote token',
|
|
204
|
+
status: remoteTokenValidation.ok || canUseSavedProfileForToken ? 'pass' : 'fail',
|
|
205
|
+
detail: canUseSavedProfileForToken
|
|
206
|
+
? savedProfileReadyDetail
|
|
207
|
+
: remoteTokenValidation.detail,
|
|
208
|
+
},
|
|
209
|
+
];
|
|
210
|
+
|
|
211
|
+
const allModes = [
|
|
212
|
+
describeMode({ mode: TUNNEL_MODE_QUICK, checks: quickChecks }),
|
|
213
|
+
describeMode({ mode: TUNNEL_MODE_MANAGED_REMOTE, checks: managedRemoteChecks }),
|
|
214
|
+
describeMode({ mode: TUNNEL_MODE_MANAGED_LOCAL, checks: managedLocalChecks }),
|
|
215
|
+
];
|
|
216
|
+
|
|
217
|
+
const modeFilter = typeof request.mode === 'string' && request.mode.trim().length > 0
|
|
218
|
+
? request.mode.trim().toLowerCase()
|
|
219
|
+
: null;
|
|
220
|
+
const modes = modeFilter ? allModes.filter((entry) => entry.mode === modeFilter) : allModes;
|
|
221
|
+
|
|
222
|
+
return {
|
|
223
|
+
providerChecks,
|
|
224
|
+
modes,
|
|
225
|
+
};
|
|
226
|
+
},
|
|
227
|
+
start: async (request, context = {}) => {
|
|
228
|
+
if (request.mode === TUNNEL_MODE_MANAGED_REMOTE) {
|
|
229
|
+
return startCloudflareManagedRemoteTunnel({
|
|
230
|
+
token: request.token,
|
|
231
|
+
hostname: request.hostname,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (request.mode === TUNNEL_MODE_MANAGED_LOCAL) {
|
|
236
|
+
return startCloudflareManagedLocalTunnel({
|
|
237
|
+
configPath: request.configPath,
|
|
238
|
+
hostname: request.hostname,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (!context.originUrl) {
|
|
243
|
+
throw new TunnelServiceError('validation_error', 'originUrl is required for quick tunnel mode');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return startCloudflareQuickTunnel({
|
|
247
|
+
originUrl: context.originUrl,
|
|
248
|
+
port: context.activePort,
|
|
249
|
+
});
|
|
250
|
+
},
|
|
251
|
+
stop: (controller) => {
|
|
252
|
+
controller?.stop?.();
|
|
253
|
+
},
|
|
254
|
+
resolvePublicUrl: (controller) => controller?.getPublicUrl?.() ?? null,
|
|
255
|
+
getMetadata: (controller) => ({
|
|
256
|
+
configPath: controller?.getEffectiveConfigPath?.() ?? null,
|
|
257
|
+
resolvedHostname: controller?.getResolvedHostname?.() ?? null,
|
|
258
|
+
}),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const REQUIRED_PROVIDER_METHODS = ['start', 'stop', 'checkAvailability', 'resolvePublicUrl'];
|
|
2
|
+
|
|
3
|
+
export function createTunnelProviderRegistry(initialProviders = []) {
|
|
4
|
+
const providers = new Map();
|
|
5
|
+
let sealed = false;
|
|
6
|
+
|
|
7
|
+
const register = (provider) => {
|
|
8
|
+
if (sealed) {
|
|
9
|
+
throw new Error('Tunnel provider registry is sealed; no further registrations allowed');
|
|
10
|
+
}
|
|
11
|
+
if (!provider || typeof provider.id !== 'string' || provider.id.trim().length === 0) {
|
|
12
|
+
throw new Error('Tunnel provider must define a non-empty id');
|
|
13
|
+
}
|
|
14
|
+
for (const method of REQUIRED_PROVIDER_METHODS) {
|
|
15
|
+
if (typeof provider[method] !== 'function') {
|
|
16
|
+
throw new Error(`Tunnel provider '${provider.id}' must implement ${method}()`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const key = provider.id.trim().toLowerCase();
|
|
20
|
+
if (providers.has(key)) {
|
|
21
|
+
throw new Error(`Tunnel provider '${key}' is already registered`);
|
|
22
|
+
}
|
|
23
|
+
providers.set(key, provider);
|
|
24
|
+
return provider;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const get = (providerId) => {
|
|
28
|
+
if (typeof providerId !== 'string' || providerId.trim().length === 0) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return providers.get(providerId.trim().toLowerCase()) ?? null;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const list = () => Array.from(providers.values());
|
|
35
|
+
|
|
36
|
+
const listCapabilities = () => list().map((provider) => ({ ...provider.capabilities }));
|
|
37
|
+
|
|
38
|
+
for (const provider of initialProviders) {
|
|
39
|
+
register(provider);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const seal = () => { sealed = true; };
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
register,
|
|
46
|
+
get,
|
|
47
|
+
list,
|
|
48
|
+
listCapabilities,
|
|
49
|
+
seal,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import os from 'os';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
export const TUNNEL_PROVIDER_CLOUDFLARE = 'cloudflare';
|
|
5
|
+
|
|
6
|
+
export const TUNNEL_MODE_QUICK = 'quick';
|
|
7
|
+
export const TUNNEL_MODE_MANAGED_REMOTE = 'managed-remote';
|
|
8
|
+
export const TUNNEL_MODE_MANAGED_LOCAL = 'managed-local';
|
|
9
|
+
|
|
10
|
+
export const TUNNEL_INTENT_EPHEMERAL_PUBLIC = 'ephemeral-public';
|
|
11
|
+
export const TUNNEL_INTENT_PERSISTENT_PUBLIC = 'persistent-public';
|
|
12
|
+
export const TUNNEL_INTENT_PRIVATE_NETWORK = 'private-network';
|
|
13
|
+
|
|
14
|
+
const SUPPORTED_TUNNEL_INTENTS = new Set([
|
|
15
|
+
TUNNEL_INTENT_EPHEMERAL_PUBLIC,
|
|
16
|
+
TUNNEL_INTENT_PERSISTENT_PUBLIC,
|
|
17
|
+
TUNNEL_INTENT_PRIVATE_NETWORK,
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
const SUPPORTED_TUNNEL_MODES = new Set([
|
|
21
|
+
TUNNEL_MODE_QUICK,
|
|
22
|
+
TUNNEL_MODE_MANAGED_REMOTE,
|
|
23
|
+
TUNNEL_MODE_MANAGED_LOCAL,
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
export class TunnelServiceError extends Error {
|
|
27
|
+
constructor(code, message, details = null) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = 'TunnelServiceError';
|
|
30
|
+
this.code = code;
|
|
31
|
+
this.details = details;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const SUPPORTED_TUNNEL_PROVIDERS = new Set([
|
|
36
|
+
TUNNEL_PROVIDER_CLOUDFLARE,
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
export function normalizeTunnelProvider(value) {
|
|
40
|
+
if (typeof value !== 'string') {
|
|
41
|
+
return TUNNEL_PROVIDER_CLOUDFLARE;
|
|
42
|
+
}
|
|
43
|
+
const provider = value.trim().toLowerCase();
|
|
44
|
+
if (!provider || !SUPPORTED_TUNNEL_PROVIDERS.has(provider)) {
|
|
45
|
+
return TUNNEL_PROVIDER_CLOUDFLARE;
|
|
46
|
+
}
|
|
47
|
+
return provider;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function normalizeTunnelMode(value) {
|
|
51
|
+
if (typeof value !== 'string') {
|
|
52
|
+
return TUNNEL_MODE_QUICK;
|
|
53
|
+
}
|
|
54
|
+
const mode = value.trim().toLowerCase();
|
|
55
|
+
if (!mode) {
|
|
56
|
+
return TUNNEL_MODE_QUICK;
|
|
57
|
+
}
|
|
58
|
+
if (mode === TUNNEL_MODE_QUICK) {
|
|
59
|
+
return TUNNEL_MODE_QUICK;
|
|
60
|
+
}
|
|
61
|
+
if (mode === TUNNEL_MODE_MANAGED_REMOTE) {
|
|
62
|
+
return TUNNEL_MODE_MANAGED_REMOTE;
|
|
63
|
+
}
|
|
64
|
+
if (mode === TUNNEL_MODE_MANAGED_LOCAL) {
|
|
65
|
+
return TUNNEL_MODE_MANAGED_LOCAL;
|
|
66
|
+
}
|
|
67
|
+
return TUNNEL_MODE_QUICK;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function normalizeTunnelIntent(value) {
|
|
71
|
+
if (typeof value !== 'string') {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const intent = value.trim().toLowerCase();
|
|
75
|
+
if (!intent || !SUPPORTED_TUNNEL_INTENTS.has(intent)) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
return intent;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function modeIntentFallback(mode) {
|
|
82
|
+
if (mode === TUNNEL_MODE_QUICK) {
|
|
83
|
+
return TUNNEL_INTENT_EPHEMERAL_PUBLIC;
|
|
84
|
+
}
|
|
85
|
+
if (mode === TUNNEL_MODE_MANAGED_REMOTE || mode === TUNNEL_MODE_MANAGED_LOCAL) {
|
|
86
|
+
return TUNNEL_INTENT_PERSISTENT_PUBLIC;
|
|
87
|
+
}
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function normalizeTunnelModeForRequest(value) {
|
|
92
|
+
if (typeof value === 'string') {
|
|
93
|
+
const mode = value.trim().toLowerCase();
|
|
94
|
+
if (mode === TUNNEL_MODE_QUICK || mode === TUNNEL_MODE_MANAGED_REMOTE || mode === TUNNEL_MODE_MANAGED_LOCAL) {
|
|
95
|
+
return mode;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return TUNNEL_MODE_QUICK;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function normalizeOptionalPath(value) {
|
|
102
|
+
if (value === null) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
if (typeof value !== 'string') {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
const trimmed = value.trim();
|
|
109
|
+
if (!trimmed) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
let resolved;
|
|
113
|
+
if (trimmed === '~') {
|
|
114
|
+
resolved = os.homedir();
|
|
115
|
+
} else if (trimmed.startsWith('~/') || trimmed.startsWith('~\\')) {
|
|
116
|
+
resolved = path.join(os.homedir(), trimmed.slice(2));
|
|
117
|
+
} else {
|
|
118
|
+
resolved = path.resolve(trimmed);
|
|
119
|
+
}
|
|
120
|
+
const home = os.homedir();
|
|
121
|
+
if (resolved !== home && !resolved.startsWith(home + path.sep)) {
|
|
122
|
+
throw new TunnelServiceError(
|
|
123
|
+
'validation_error',
|
|
124
|
+
`Config path must be within the home directory (${home}). Got: ${resolved}`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
return resolved;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function isSupportedTunnelMode(mode) {
|
|
131
|
+
return SUPPORTED_TUNNEL_MODES.has(mode);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function normalizeTunnelStartRequest(input = {}, defaults = {}) {
|
|
135
|
+
const provider = normalizeTunnelProvider(input.provider ?? defaults.provider);
|
|
136
|
+
const mode = normalizeTunnelModeForRequest(input.mode ?? defaults.mode);
|
|
137
|
+
const explicitIntent = normalizeTunnelIntent(input.intent ?? defaults.intent);
|
|
138
|
+
const intent = explicitIntent ?? modeIntentFallback(mode);
|
|
139
|
+
const configPathValue = Object.prototype.hasOwnProperty.call(input, 'configPath')
|
|
140
|
+
? input.configPath
|
|
141
|
+
: defaults.configPath;
|
|
142
|
+
const configPath = normalizeOptionalPath(configPathValue);
|
|
143
|
+
|
|
144
|
+
const token = typeof (input.token ?? defaults.token) === 'string'
|
|
145
|
+
? (input.token ?? defaults.token).trim()
|
|
146
|
+
: '';
|
|
147
|
+
|
|
148
|
+
const hostname = typeof (input.hostname ?? defaults.hostname) === 'string'
|
|
149
|
+
? (input.hostname ?? defaults.hostname).trim().toLowerCase()
|
|
150
|
+
: '';
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
provider,
|
|
154
|
+
mode,
|
|
155
|
+
intent,
|
|
156
|
+
configPath,
|
|
157
|
+
token,
|
|
158
|
+
hostname,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function validateTunnelStartRequest(request, capabilities) {
|
|
163
|
+
if (!request || typeof request !== 'object') {
|
|
164
|
+
throw new TunnelServiceError('validation_error', 'Tunnel start request must be an object');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!request.provider) {
|
|
168
|
+
throw new TunnelServiceError('validation_error', 'Tunnel provider is required');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (!isSupportedTunnelMode(request.mode)) {
|
|
172
|
+
throw new TunnelServiceError('mode_unsupported', `Unsupported tunnel mode: ${request.mode}`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (!capabilities || capabilities.provider !== request.provider) {
|
|
176
|
+
throw new TunnelServiceError('provider_unsupported', `Unsupported tunnel provider: ${request.provider}`);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (!Array.isArray(capabilities.modes)) {
|
|
180
|
+
throw new TunnelServiceError('mode_unsupported', `Provider '${request.provider}' does not declare tunnel modes`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const modeDescriptor = capabilities.modes.find((entry) => entry?.key === request.mode);
|
|
184
|
+
if (!modeDescriptor) {
|
|
185
|
+
throw new TunnelServiceError('mode_unsupported', `Provider '${request.provider}' does not support mode '${request.mode}'`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (typeof request.intent === 'string' && request.intent.length > 0) {
|
|
189
|
+
if (!SUPPORTED_TUNNEL_INTENTS.has(request.intent)) {
|
|
190
|
+
throw new TunnelServiceError('validation_error', `Unsupported tunnel intent: ${request.intent}`);
|
|
191
|
+
}
|
|
192
|
+
if (modeDescriptor.intent !== request.intent) {
|
|
193
|
+
throw new TunnelServiceError(
|
|
194
|
+
'validation_error',
|
|
195
|
+
`Tunnel intent '${request.intent}' does not match mode '${request.mode}' (expected '${modeDescriptor.intent}')`
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const requiredFields = Array.isArray(modeDescriptor.requires) ? modeDescriptor.requires : [];
|
|
201
|
+
|
|
202
|
+
if (requiredFields.includes('token')) {
|
|
203
|
+
if (!request.token) {
|
|
204
|
+
throw new TunnelServiceError('validation_error', 'Managed remote tunnel token is required');
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (requiredFields.includes('hostname')) {
|
|
209
|
+
if (!request.hostname) {
|
|
210
|
+
throw new TunnelServiceError('validation_error', 'Managed remote tunnel hostname is required');
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (requiredFields.includes('configPath')) {
|
|
215
|
+
if (request.configPath === undefined || request.configPath === null || request.configPath === '') {
|
|
216
|
+
throw new TunnelServiceError('validation_error', `Mode '${request.mode}' requires a configPath`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export function createLruMap(maxEntries, onEvict) {
|
|
2
|
+
const map = new Map();
|
|
3
|
+
const accessOrder = new Map();
|
|
4
|
+
let accessCounter = 0;
|
|
5
|
+
|
|
6
|
+
const touch = (key) => {
|
|
7
|
+
accessOrder.set(key, accessCounter++);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const evictIfNeeded = () => {
|
|
11
|
+
if (map.size <= maxEntries) return;
|
|
12
|
+
|
|
13
|
+
let oldestKey = null;
|
|
14
|
+
let oldestAccess = Infinity;
|
|
15
|
+
|
|
16
|
+
for (const [key, access] of accessOrder) {
|
|
17
|
+
if (access < oldestAccess) {
|
|
18
|
+
oldestAccess = access;
|
|
19
|
+
oldestKey = key;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (oldestKey !== null) {
|
|
24
|
+
const value = map.get(oldestKey);
|
|
25
|
+
map.delete(oldestKey);
|
|
26
|
+
accessOrder.delete(oldestKey);
|
|
27
|
+
if (onEvict && value !== undefined) {
|
|
28
|
+
onEvict(oldestKey, value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
get: (key) => {
|
|
35
|
+
const value = map.get(key);
|
|
36
|
+
if (value !== undefined) {
|
|
37
|
+
touch(key);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
set: (key, value) => {
|
|
43
|
+
if (map.has(key)) {
|
|
44
|
+
map.set(key, value);
|
|
45
|
+
touch(key);
|
|
46
|
+
} else {
|
|
47
|
+
evictIfNeeded();
|
|
48
|
+
map.set(key, value);
|
|
49
|
+
touch(key);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
delete: (key) => {
|
|
54
|
+
const deleted = map.delete(key);
|
|
55
|
+
accessOrder.delete(key);
|
|
56
|
+
return deleted;
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
has: (key) => {
|
|
60
|
+
return map.has(key);
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
clear: () => {
|
|
64
|
+
map.clear();
|
|
65
|
+
accessOrder.clear();
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
get size() {
|
|
69
|
+
return map.size;
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
entries: () => {
|
|
73
|
+
return map.entries();
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
keys: () => {
|
|
77
|
+
return map.keys();
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
values: () => {
|
|
81
|
+
return map.values();
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const EVICTION_WARNING_THRESHOLD = 10;
|
|
87
|
+
const EVICTION_WINDOW_MS = 60000;
|
|
88
|
+
|
|
89
|
+
export function createLruMapWithWarnings(maxEntries, mapName) {
|
|
90
|
+
let evictionCount = 0;
|
|
91
|
+
let lastWarning = 0;
|
|
92
|
+
|
|
93
|
+
const lruMap = createLruMap(maxEntries, () => {
|
|
94
|
+
evictionCount++;
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
|
|
97
|
+
if (evictionCount >= EVICTION_WARNING_THRESHOLD && now - lastWarning > EVICTION_WINDOW_MS) {
|
|
98
|
+
console.warn(`[LRU] ${mapName}: ${evictionCount} evictions in the last minute. Consider increasing maxEntries (current: ${maxEntries})`);
|
|
99
|
+
lastWarning = now;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
...lruMap,
|
|
105
|
+
getEvictionStats: () => ({ count: evictionCount, lastWarning }),
|
|
106
|
+
};
|
|
107
|
+
}
|