devanvil 0.0.0-stage → 2.1.0
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/LICENSE +21 -0
- package/README.ar.md +113 -0
- package/README.md +272 -2
- package/SECURITY.md +49 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +659 -0
- package/dist/core/contract-drift.d.ts +49 -0
- package/dist/core/contract-drift.js +302 -0
- package/dist/core/converters.d.ts +18 -0
- package/dist/core/converters.js +981 -0
- package/dist/core/cron.d.ts +33 -0
- package/dist/core/cron.js +306 -0
- package/dist/core/crypto.d.ts +59 -0
- package/dist/core/crypto.js +250 -0
- package/dist/core/curl.d.ts +17 -0
- package/dist/core/curl.js +170 -0
- package/dist/core/diff.d.ts +36 -0
- package/dist/core/diff.js +148 -0
- package/dist/core/encoding.d.ts +3 -0
- package/dist/core/encoding.js +143 -0
- package/dist/core/importers.d.ts +65 -0
- package/dist/core/importers.js +361 -0
- package/dist/core/jwt.d.ts +39 -0
- package/dist/core/jwt.js +256 -0
- package/dist/core/llm-tokens.d.ts +38 -0
- package/dist/core/llm-tokens.js +125 -0
- package/dist/core/log-analyzer.d.ts +30 -0
- package/dist/core/log-analyzer.js +155 -0
- package/dist/core/network.d.ts +72 -0
- package/dist/core/network.js +185 -0
- package/dist/core/oauth-protobuf.d.ts +74 -0
- package/dist/core/oauth-protobuf.js +390 -0
- package/dist/core/openapi.d.ts +27 -0
- package/dist/core/openapi.js +257 -0
- package/dist/core/pipeline.d.ts +28 -0
- package/dist/core/pipeline.js +154 -0
- package/dist/core/regex.d.ts +50 -0
- package/dist/core/regex.js +351 -0
- package/dist/core/runner.d.ts +53 -0
- package/dist/core/runner.js +250 -0
- package/dist/core/secrets.d.ts +87 -0
- package/dist/core/secrets.js +599 -0
- package/dist/core/security.d.ts +34 -0
- package/dist/core/security.js +284 -0
- package/dist/core/sql-explain.d.ts +50 -0
- package/dist/core/sql-explain.js +259 -0
- package/dist/core/tls.d.ts +44 -0
- package/dist/core/tls.js +166 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +31 -0
- package/dist/server/app.d.ts +31 -0
- package/dist/server/app.js +348 -0
- package/dist/server/http-proxy.d.ts +55 -0
- package/dist/server/http-proxy.js +364 -0
- package/dist/server/http-utils.d.ts +28 -0
- package/dist/server/http-utils.js +106 -0
- package/dist/server/load-tester.d.ts +53 -0
- package/dist/server/load-tester.js +134 -0
- package/dist/server/mcp.d.ts +35 -0
- package/dist/server/mcp.js +466 -0
- package/dist/server/mock-store.d.ts +38 -0
- package/dist/server/mock-store.js +197 -0
- package/dist/server/plugin-loader.d.ts +60 -0
- package/dist/server/plugin-loader.js +285 -0
- package/dist/server/recorder-store.d.ts +35 -0
- package/dist/server/recorder-store.js +146 -0
- package/dist/server/routes/network-routes.d.ts +3 -0
- package/dist/server/routes/network-routes.js +391 -0
- package/dist/server/routes/tools-routes.d.ts +3 -0
- package/dist/server/routes/tools-routes.js +292 -0
- package/dist/server/routes/workspace-routes.d.ts +3 -0
- package/dist/server/routes/workspace-routes.js +240 -0
- package/dist/server/webhook-store.d.ts +29 -0
- package/dist/server/webhook-store.js +90 -0
- package/dist/server/workspace-store.d.ts +91 -0
- package/dist/server/workspace-store.js +447 -0
- package/dist/server/ws-probe.d.ts +34 -0
- package/dist/server/ws-probe.js +283 -0
- package/dist/ui/app.js +274 -0
- package/dist/ui/favicon.svg +14 -0
- package/dist/ui/index.html +83 -0
- package/dist/ui/logo.png +0 -0
- package/dist/ui/styles.css +694 -0
- package/dist/ui/tools-data.js +704 -0
- package/dist/ui/tools-network.js +727 -0
- package/dist/ui/tools-ops.js +295 -0
- package/dist/ui/tools-pro.js +500 -0
- package/dist/ui/tools-security.js +389 -0
- package/dist/ui/tools-v12.js +745 -0
- package/dist/ui/tools-v13.js +321 -0
- package/dist/ui/tools-v20.js +563 -0
- package/dist/ui/tools-v21.js +395 -0
- package/package.json +75 -4
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { convertData, formatSql, queryJsonPath } from '../../core/converters.js';
|
|
2
|
+
import { describeCron, getNextCronRuns, parseTimestamp } from '../../core/cron.js';
|
|
3
|
+
import { decryptAesGcm, deriveKey, encryptAesGcm, generateApiKey, generateAsymmetricKeyPair, generateNanoId, generatePassword, generateUlid, generateUuidV4, generateUuidV7, hashText, hmacText, } from '../../core/crypto.js';
|
|
4
|
+
import { diffJsonSemantic, diffText } from '../../core/diff.js';
|
|
5
|
+
import { decodeText, encodeText } from '../../core/encoding.js';
|
|
6
|
+
import { decodeJwt, signJwt, verifyJwt } from '../../core/jwt.js';
|
|
7
|
+
import { analyzeLlmText, compactContextText } from '../../core/llm-tokens.js';
|
|
8
|
+
import { analyzeLogs } from '../../core/log-analyzer.js';
|
|
9
|
+
import { calculateCidr, checkColorContrast, HTTP_STATUS_CODES, parseChmod, parseHexColor, } from '../../core/network.js';
|
|
10
|
+
import { analyzeOAuthCallback, decodeProtobufWire, generatePkceBundle } from '../../core/oauth-protobuf.js';
|
|
11
|
+
import { PIPELINE_OPERATIONS, runPipeline } from '../../core/pipeline.js';
|
|
12
|
+
import { REGEX_PRESETS, testRegex } from '../../core/regex.js';
|
|
13
|
+
import { analyzeEnvFile, scanSecrets } from '../../core/secrets.js';
|
|
14
|
+
import { safeJsonParse } from '../../core/security.js';
|
|
15
|
+
import { analyzeSqlExplain } from '../../core/sql-explain.js';
|
|
16
|
+
import { asOptionalNumber, asOptionalString, asString, readJsonBody, sendJson, } from '../http-utils.js';
|
|
17
|
+
export async function handleToolRoutes(method, pathname, urlObj, req, res, ctx) {
|
|
18
|
+
const { denyIframe } = ctx;
|
|
19
|
+
// SQL EXPLAIN ANALYZE Visualizer & Log Analyzer
|
|
20
|
+
if (method === 'POST' && pathname === '/api/sql/explain') {
|
|
21
|
+
const body = await readJsonBody(req);
|
|
22
|
+
sendJson(res, 200, analyzeSqlExplain(asString(body.input)), denyIframe);
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
if (method === 'POST' && pathname === '/api/logs/analyze') {
|
|
26
|
+
const body = await readJsonBody(req);
|
|
27
|
+
sendJson(res, 200, analyzeLogs(asString(body.input)), denyIframe);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
// OAuth 2.0 / OIDC PKCE Studio & Protobuf Binary Wire Inspector
|
|
31
|
+
if (method === 'POST' && pathname === '/api/oauth/pkce') {
|
|
32
|
+
const body = await readJsonBody(req);
|
|
33
|
+
sendJson(res, 200, generatePkceBundle({
|
|
34
|
+
verifierLength: asOptionalNumber(body.verifierLength),
|
|
35
|
+
authorizationEndpoint: asOptionalString(body.authorizationEndpoint),
|
|
36
|
+
clientId: asOptionalString(body.clientId),
|
|
37
|
+
redirectUri: asOptionalString(body.redirectUri),
|
|
38
|
+
scope: asOptionalString(body.scope),
|
|
39
|
+
audience: asOptionalString(body.audience),
|
|
40
|
+
}), denyIframe);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
if (method === 'POST' && pathname === '/api/oauth/analyze-callback') {
|
|
44
|
+
const body = await readJsonBody(req);
|
|
45
|
+
sendJson(res, 200, analyzeOAuthCallback(asString(body.callbackUrl), {
|
|
46
|
+
state: asOptionalString(body.expectedState),
|
|
47
|
+
nonce: asOptionalString(body.expectedNonce),
|
|
48
|
+
codeVerifier: asOptionalString(body.codeVerifier),
|
|
49
|
+
codeChallenge: asOptionalString(body.codeChallenge),
|
|
50
|
+
}), denyIframe);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (method === 'POST' && pathname === '/api/protobuf/decode') {
|
|
54
|
+
const body = await readJsonBody(req);
|
|
55
|
+
const enc = asString(body.encoding, 'auto').toLowerCase();
|
|
56
|
+
sendJson(res, 200, decodeProtobufWire(asString(body.input), {
|
|
57
|
+
encoding: enc === 'hex' || enc === 'base64' ? enc : 'auto',
|
|
58
|
+
protoSchema: asOptionalString(body.protoSchema),
|
|
59
|
+
messageName: asOptionalString(body.messageName),
|
|
60
|
+
}), denyIframe);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
// Secret Scanner & .env Validator
|
|
64
|
+
if (method === 'POST' && pathname === '/api/secrets/scan') {
|
|
65
|
+
const body = await readJsonBody(req);
|
|
66
|
+
sendJson(res, 200, scanSecrets(asString(body.input), {
|
|
67
|
+
checkHighEntropy: body.checkHighEntropy !== false,
|
|
68
|
+
entropyThreshold: asOptionalNumber(body.entropyThreshold),
|
|
69
|
+
}), denyIframe);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (method === 'POST' && pathname === '/api/secrets/env') {
|
|
73
|
+
const body = await readJsonBody(req);
|
|
74
|
+
sendJson(res, 200, analyzeEnvFile(asString(body.env), asOptionalString(body.example)), denyIframe);
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
// LLM Token & Context Budget Studio
|
|
78
|
+
if (method === 'POST' && pathname === '/api/llm/analyze') {
|
|
79
|
+
const body = await readJsonBody(req);
|
|
80
|
+
sendJson(res, 200, analyzeLlmText(asString(body.input)), denyIframe);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
if (method === 'POST' && pathname === '/api/llm/compact') {
|
|
84
|
+
const body = await readJsonBody(req);
|
|
85
|
+
sendJson(res, 200, compactContextText(asString(body.input), {
|
|
86
|
+
stripLineComments: Boolean(body.stripLineComments),
|
|
87
|
+
minifyJson: Boolean(body.minifyJson),
|
|
88
|
+
}), denyIframe);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
// JWT Studio
|
|
92
|
+
if (method === 'POST' && pathname === '/api/jwt/decode') {
|
|
93
|
+
const body = await readJsonBody(req);
|
|
94
|
+
sendJson(res, 200, decodeJwt(asString(body.token)), denyIframe);
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (method === 'POST' && pathname === '/api/jwt/sign') {
|
|
98
|
+
const body = await readJsonBody(req);
|
|
99
|
+
const payload = body.payload && typeof body.payload === 'object' && !Array.isArray(body.payload)
|
|
100
|
+
? body.payload
|
|
101
|
+
: {};
|
|
102
|
+
const token = signJwt(payload, asString(body.secret), {
|
|
103
|
+
alg: asOptionalString(body.alg),
|
|
104
|
+
expiresInSeconds: asOptionalNumber(body.expiresInSeconds),
|
|
105
|
+
});
|
|
106
|
+
sendJson(res, 200, { token, decoded: decodeJwt(token) }, denyIframe);
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
if (method === 'POST' && pathname === '/api/jwt/verify') {
|
|
110
|
+
const body = await readJsonBody(req);
|
|
111
|
+
sendJson(res, 200, verifyJwt(asString(body.token), asString(body.secret), {
|
|
112
|
+
allowedAlgorithms: Array.isArray(body.allowedAlgorithms) ? body.allowedAlgorithms : undefined,
|
|
113
|
+
clockToleranceSec: asOptionalNumber(body.clockToleranceSec),
|
|
114
|
+
}), denyIframe);
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
if (method === 'POST' && pathname === '/api/jwt/keypair') {
|
|
118
|
+
const body = await readJsonBody(req);
|
|
119
|
+
sendJson(res, 200, generateAsymmetricKeyPair(body.type === 'rsa' ? 'rsa' : 'ec'), denyIframe);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
// Data Converters, JSONPath, SQL, Diff
|
|
123
|
+
if (method === 'POST' && pathname === '/api/convert') {
|
|
124
|
+
const body = await readJsonBody(req);
|
|
125
|
+
const output = convertData(asString(body.input), asString(body.from, 'json'), asString(body.to, 'yaml'), {
|
|
126
|
+
indent: asOptionalNumber(body.indent),
|
|
127
|
+
sortKeys: Boolean(body.sortKeys),
|
|
128
|
+
minify: Boolean(body.minify),
|
|
129
|
+
});
|
|
130
|
+
sendJson(res, 200, { output }, denyIframe);
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
if (method === 'POST' && pathname === '/api/jsonpath') {
|
|
134
|
+
const body = await readJsonBody(req);
|
|
135
|
+
const parsed = safeJsonParse(asString(body.input, '{}'));
|
|
136
|
+
const matches = queryJsonPath(parsed, asString(body.expression, '$'));
|
|
137
|
+
sendJson(res, 200, { count: matches.length, matches }, denyIframe);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
if (method === 'POST' && pathname === '/api/sql/format') {
|
|
141
|
+
const body = await readJsonBody(req);
|
|
142
|
+
sendJson(res, 200, { formatted: formatSql(asString(body.sql)) }, denyIframe);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
if (method === 'POST' && pathname === '/api/diff') {
|
|
146
|
+
const body = await readJsonBody(req);
|
|
147
|
+
if (body.mode === 'json') {
|
|
148
|
+
sendJson(res, 200, diffJsonSemantic(asString(body.left, '{}'), asString(body.right, '{}')), denyIframe);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
sendJson(res, 200, diffText(asString(body.left), asString(body.right)), denyIframe);
|
|
152
|
+
}
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
// Regex Lab
|
|
156
|
+
if (method === 'GET' && pathname === '/api/regex/presets') {
|
|
157
|
+
sendJson(res, 200, { presets: REGEX_PRESETS }, denyIframe);
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
if (method === 'POST' && pathname === '/api/regex/test') {
|
|
161
|
+
const body = await readJsonBody(req);
|
|
162
|
+
sendJson(res, 200, testRegex(asString(body.pattern), asString(body.flags, 'g'), asString(body.text), {
|
|
163
|
+
replacement: asOptionalString(body.replacement),
|
|
164
|
+
}), denyIframe);
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
// Crypto, Hashing, Encoding & IDs
|
|
168
|
+
if (method === 'POST' && pathname === '/api/crypto/hash') {
|
|
169
|
+
const body = await readJsonBody(req);
|
|
170
|
+
const algo = asString(body.algorithm, 'sha256');
|
|
171
|
+
const secret = asOptionalString(body.hmacSecret);
|
|
172
|
+
sendJson(res, 200, secret ? hmacText(asString(body.input), secret, algo) : hashText(asString(body.input), algo), denyIframe);
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
if (method === 'POST' && pathname === '/api/crypto/aes') {
|
|
176
|
+
const body = await readJsonBody(req);
|
|
177
|
+
const output = body.mode === 'decrypt'
|
|
178
|
+
? decryptAesGcm(asString(body.input), asString(body.passphrase))
|
|
179
|
+
: encryptAesGcm(asString(body.input), asString(body.passphrase));
|
|
180
|
+
sendJson(res, 200, { output }, denyIframe);
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
if (method === 'POST' && pathname === '/api/crypto/kdf') {
|
|
184
|
+
const body = await readJsonBody(req);
|
|
185
|
+
sendJson(res, 200, deriveKey(asString(body.password), {
|
|
186
|
+
kdf: body.kdf === 'pbkdf2' ? 'pbkdf2' : 'scrypt',
|
|
187
|
+
saltHex: asOptionalString(body.saltHex),
|
|
188
|
+
iterations: asOptionalNumber(body.iterations),
|
|
189
|
+
}), denyIframe);
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
if (method === 'POST' && pathname === '/api/crypto/password') {
|
|
193
|
+
const body = await readJsonBody(req);
|
|
194
|
+
const count = Math.min(Math.max(Math.floor(asOptionalNumber(body.count) ?? 1), 1), 20);
|
|
195
|
+
const items = Array.from({ length: count }, () => generatePassword({
|
|
196
|
+
length: asOptionalNumber(body.length),
|
|
197
|
+
uppercase: body.uppercase !== false,
|
|
198
|
+
lowercase: body.lowercase !== false,
|
|
199
|
+
numbers: body.numbers !== false,
|
|
200
|
+
symbols: body.symbols !== false,
|
|
201
|
+
excludeAmbiguous: Boolean(body.excludeAmbiguous),
|
|
202
|
+
}));
|
|
203
|
+
sendJson(res, 200, { items }, denyIframe);
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
if (method === 'POST' && pathname === '/api/crypto/ids') {
|
|
207
|
+
const body = await readJsonBody(req);
|
|
208
|
+
const count = Math.min(Math.max(Math.floor(asOptionalNumber(body.count) ?? 5), 1), 50);
|
|
209
|
+
const kind = asString(body.kind, 'uuidv7');
|
|
210
|
+
const size = asOptionalNumber(body.size) ?? 21;
|
|
211
|
+
const prefix = asString(body.prefix, 'da_live');
|
|
212
|
+
const ids = Array.from({ length: count }, () => {
|
|
213
|
+
if (kind === 'uuidv4')
|
|
214
|
+
return generateUuidV4();
|
|
215
|
+
if (kind === 'ulid')
|
|
216
|
+
return generateUlid();
|
|
217
|
+
if (kind === 'nanoid')
|
|
218
|
+
return generateNanoId(size);
|
|
219
|
+
if (kind === 'apikey')
|
|
220
|
+
return generateApiKey(prefix);
|
|
221
|
+
return generateUuidV7();
|
|
222
|
+
});
|
|
223
|
+
sendJson(res, 200, { kind, ids }, denyIframe);
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
if (method === 'POST' && pathname === '/api/encode') {
|
|
227
|
+
const body = await readJsonBody(req);
|
|
228
|
+
const codec = asString(body.codec, 'base64');
|
|
229
|
+
const output = body.mode === 'decode' ? decodeText(asString(body.input), codec) : encodeText(asString(body.input), codec);
|
|
230
|
+
sendJson(res, 200, { output }, denyIframe);
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
// Pipeline / Recipes
|
|
234
|
+
if (method === 'GET' && pathname === '/api/pipeline/ops') {
|
|
235
|
+
sendJson(res, 200, { operations: PIPELINE_OPERATIONS }, denyIframe);
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
if (method === 'POST' && pathname === '/api/pipeline/run') {
|
|
239
|
+
const body = await readJsonBody(req);
|
|
240
|
+
if (body.steps !== undefined && !Array.isArray(body.steps)) {
|
|
241
|
+
sendJson(res, 400, { error: 'Pipeline "steps" must be an array' }, denyIframe);
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
sendJson(res, 200, runPipeline(asString(body.input), body.steps ?? []), denyIframe);
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
// Cron & Timestamp
|
|
248
|
+
if (method === 'POST' && pathname === '/api/cron') {
|
|
249
|
+
const body = await readJsonBody(req);
|
|
250
|
+
const expr = asString(body.expression, '*/15 * * * *');
|
|
251
|
+
sendJson(res, 200, { ...getNextCronRuns(expr, asOptionalNumber(body.count) ?? 10), description: describeCron(expr) }, denyIframe);
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
if (method === 'POST' && pathname === '/api/timestamp') {
|
|
255
|
+
const body = await readJsonBody(req);
|
|
256
|
+
sendJson(res, 200, parseTimestamp(asString(body.input, 'now')), denyIframe);
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
// Network, Chmod, Color, HTTP Status
|
|
260
|
+
if (method === 'POST' && pathname === '/api/utils/cidr') {
|
|
261
|
+
const body = await readJsonBody(req);
|
|
262
|
+
sendJson(res, 200, calculateCidr(asString(body.cidr, '192.168.1.0/24')), denyIframe);
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
if (method === 'POST' && pathname === '/api/utils/chmod') {
|
|
266
|
+
const body = await readJsonBody(req);
|
|
267
|
+
sendJson(res, 200, parseChmod(asString(body.mode, '755')), denyIframe);
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
if (method === 'POST' && pathname === '/api/utils/color') {
|
|
271
|
+
const body = await readJsonBody(req);
|
|
272
|
+
const bg = asOptionalString(body.background);
|
|
273
|
+
if (bg) {
|
|
274
|
+
sendJson(res, 200, checkColorContrast(asString(body.foreground, '#FFFFFF'), bg), denyIframe);
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
sendJson(res, 200, parseHexColor(asString(body.foreground, '#F59E0B')), denyIframe);
|
|
278
|
+
}
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
if (method === 'GET' && pathname === '/api/utils/http-statuses') {
|
|
282
|
+
const q = (urlObj.searchParams.get('q') ?? '').trim().toLowerCase();
|
|
283
|
+
const filtered = q
|
|
284
|
+
? HTTP_STATUS_CODES.filter((s) => String(s.code).includes(q) ||
|
|
285
|
+
s.title.toLowerCase().includes(q) ||
|
|
286
|
+
s.description.toLowerCase().includes(q))
|
|
287
|
+
: HTTP_STATUS_CODES;
|
|
288
|
+
sendJson(res, 200, { statuses: filtered }, denyIframe);
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { type RouteContext } from '../http-utils.js';
|
|
3
|
+
export declare function handleWorkspaceAndPluginRoutes(method: string, pathname: string, req: IncomingMessage, res: ServerResponse, ctx: RouteContext): Promise<boolean>;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { importHarArchive, importHttpFile, importPostmanCollection, } from '../../core/importers.js';
|
|
2
|
+
import { formatRunReportAsJunitXml, runCollection } from '../../core/runner.js';
|
|
3
|
+
import { safeJsonParse } from '../../core/security.js';
|
|
4
|
+
import { asOptionalNumber, asOptionalString, asString, asStringMap, readJsonBody, sendJson, } from '../http-utils.js';
|
|
5
|
+
export async function handleWorkspaceAndPluginRoutes(method, pathname, req, res, ctx) {
|
|
6
|
+
const { denyIframe, allowPrivateProxy, mockStore, workspaceStore, pluginLoader } = ctx;
|
|
7
|
+
// Zero-Trust Sandboxed Local Plugins (~/.devanvil/plugins) — Worker Thread + VM isolation + CRUD
|
|
8
|
+
if (method === 'GET' && pathname === '/api/plugins') {
|
|
9
|
+
sendJson(res, 200, {
|
|
10
|
+
pluginsDir: pluginLoader.getPluginsDir(),
|
|
11
|
+
isolation: 'worker_threads+vm',
|
|
12
|
+
plugins: pluginLoader.listPlugins(),
|
|
13
|
+
}, denyIframe);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
if (method === 'POST' && pathname === '/api/plugins/scaffold') {
|
|
17
|
+
const body = await readJsonBody(req);
|
|
18
|
+
const created = pluginLoader.scaffoldPlugin(asString(body.id));
|
|
19
|
+
sendJson(res, 201, { created, plugins: pluginLoader.listPlugins() }, denyIframe);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
const pluginRunMatch = /^\/api\/plugins\/([^/]+)\/run$/.exec(pathname);
|
|
23
|
+
if (method === 'POST' && pluginRunMatch) {
|
|
24
|
+
const pluginId = decodeURIComponent(pluginRunMatch[1]);
|
|
25
|
+
const body = await readJsonBody(req);
|
|
26
|
+
const opts = body.options && typeof body.options === 'object' && !Array.isArray(body.options)
|
|
27
|
+
? body.options
|
|
28
|
+
: {};
|
|
29
|
+
const result = await pluginLoader.runPluginAsync(pluginId, asString(body.input), opts, asOptionalNumber(body.timeoutMs));
|
|
30
|
+
sendJson(res, 200, result, denyIframe);
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
const pluginSourceMatch = /^\/api\/plugins\/([^/]+)\/source$/.exec(pathname);
|
|
34
|
+
if (pluginSourceMatch) {
|
|
35
|
+
const pluginId = decodeURIComponent(pluginSourceMatch[1]);
|
|
36
|
+
if (method === 'GET') {
|
|
37
|
+
sendJson(res, 200, pluginLoader.readPluginSource(pluginId), denyIframe);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
if (method === 'PUT' || method === 'POST') {
|
|
41
|
+
const body = await readJsonBody(req);
|
|
42
|
+
sendJson(res, 200, pluginLoader.savePluginSource(pluginId, asString(body.code)), denyIframe);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const pluginItemMatch = /^\/api\/plugins\/([^/]+)$/.exec(pathname);
|
|
47
|
+
if (method === 'DELETE' && pluginItemMatch) {
|
|
48
|
+
const pluginId = decodeURIComponent(pluginItemMatch[1]);
|
|
49
|
+
const deleted = pluginLoader.deletePlugin(pluginId);
|
|
50
|
+
sendJson(res, deleted ? 200 : 404, { deleted, plugins: pluginLoader.listPlugins() }, denyIframe);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
// Git-Native Workspace, Environments, Collections & Importers
|
|
54
|
+
if (method === 'GET' && pathname === '/api/workspace') {
|
|
55
|
+
sendJson(res, 200, {
|
|
56
|
+
persistent: workspaceStore.isPersistent(),
|
|
57
|
+
filePath: workspaceStore.getFilePath() ?? null,
|
|
58
|
+
localOverridesPath: workspaceStore.getLocalOverridesPath() ?? null,
|
|
59
|
+
secretWarnings: workspaceStore.auditWorkspaceSecrets(),
|
|
60
|
+
workspace: workspaceStore.getSnapshot(mockStore.list()),
|
|
61
|
+
}, denyIframe);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
if (method === 'POST' && pathname === '/api/workspace/export-encrypted') {
|
|
65
|
+
const body = await readJsonBody(req);
|
|
66
|
+
const bundle = workspaceStore.exportEncryptedBundle(asString(body.passphrase), mockStore.list());
|
|
67
|
+
sendJson(res, 200, { bundle }, denyIframe);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
if (method === 'POST' && pathname === '/api/workspace/import-encrypted') {
|
|
71
|
+
const body = await readJsonBody(req);
|
|
72
|
+
const raw = typeof body.bundle === 'string' ? body.bundle : JSON.stringify(body.bundle ?? {});
|
|
73
|
+
const loaded = workspaceStore.importEncryptedBundle(raw, asString(body.passphrase));
|
|
74
|
+
if (loaded.mockRoutes.length > 0) {
|
|
75
|
+
mockStore.importRoutes(loaded.mockRoutes, Boolean(body.replaceMocks));
|
|
76
|
+
}
|
|
77
|
+
sendJson(res, 200, {
|
|
78
|
+
workspace: workspaceStore.getSnapshot(mockStore.list()),
|
|
79
|
+
secretWarnings: workspaceStore.auditWorkspaceSecrets(),
|
|
80
|
+
}, denyIframe);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
if (method === 'POST' && pathname === '/api/workspace/run') {
|
|
84
|
+
const body = await readJsonBody(req);
|
|
85
|
+
const snap = workspaceStore.getSnapshot(mockStore.list());
|
|
86
|
+
const colId = asOptionalString(body.collectionId);
|
|
87
|
+
const col = colId ? snap.collections.find((c) => c.id === colId || c.name === colId) : snap.collections[0];
|
|
88
|
+
if (!col) {
|
|
89
|
+
sendJson(res, 404, { error: 'Collection not found in workspace' }, denyIframe);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
const envId = asOptionalString(body.environmentId);
|
|
93
|
+
const activeEnv = envId
|
|
94
|
+
? snap.environments.find((e) => e.id === envId || e.name === envId)
|
|
95
|
+
: workspaceStore.getActiveEnvironment();
|
|
96
|
+
const report = await runCollection(col, {
|
|
97
|
+
environmentName: activeEnv?.name,
|
|
98
|
+
variables: activeEnv?.variables ?? {},
|
|
99
|
+
processEnv: process.env,
|
|
100
|
+
allowPrivate: allowPrivateProxy,
|
|
101
|
+
timeoutMs: asOptionalNumber(body.timeoutMs),
|
|
102
|
+
stopOnFailure: Boolean(body.stopOnFailure),
|
|
103
|
+
});
|
|
104
|
+
sendJson(res, 200, {
|
|
105
|
+
report,
|
|
106
|
+
...(body.includeJunit === true ? { junitXml: formatRunReportAsJunitXml(report) } : {}),
|
|
107
|
+
}, denyIframe);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
if (method === 'POST' && pathname === '/api/workspace/import') {
|
|
111
|
+
const body = await readJsonBody(req);
|
|
112
|
+
const raw = typeof body.json === 'string' ? body.json : JSON.stringify(body.workspace ?? body);
|
|
113
|
+
const loaded = workspaceStore.importWorkspaceJson(raw);
|
|
114
|
+
if (loaded.mockRoutes.length > 0) {
|
|
115
|
+
mockStore.importRoutes(loaded.mockRoutes, Boolean(body.replaceMocks));
|
|
116
|
+
}
|
|
117
|
+
sendJson(res, 200, {
|
|
118
|
+
workspace: workspaceStore.getSnapshot(mockStore.list()),
|
|
119
|
+
secretWarnings: workspaceStore.auditWorkspaceSecrets(),
|
|
120
|
+
}, denyIframe);
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
if (method === 'POST' && pathname === '/api/workspace/environments') {
|
|
124
|
+
const body = await readJsonBody(req);
|
|
125
|
+
const env = workspaceStore.upsertEnvironment({
|
|
126
|
+
id: asOptionalString(body.id),
|
|
127
|
+
name: asString(body.name),
|
|
128
|
+
variables: asStringMap(body.variables) ?? {},
|
|
129
|
+
});
|
|
130
|
+
sendJson(res, 201, {
|
|
131
|
+
environment: env,
|
|
132
|
+
workspace: workspaceStore.getSnapshot(mockStore.list()),
|
|
133
|
+
secretWarnings: workspaceStore.auditWorkspaceSecrets(),
|
|
134
|
+
}, denyIframe);
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
if (method === 'POST' && pathname === '/api/workspace/environments/active') {
|
|
138
|
+
const body = await readJsonBody(req);
|
|
139
|
+
const env = workspaceStore.setActiveEnvironment(asString(body.id));
|
|
140
|
+
sendJson(res, 200, { activeEnvironment: env }, denyIframe);
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
if (method === 'DELETE' && pathname.startsWith('/api/workspace/environments/')) {
|
|
144
|
+
const id = pathname.slice('/api/workspace/environments/'.length);
|
|
145
|
+
const deleted = workspaceStore.deleteEnvironment(id);
|
|
146
|
+
sendJson(res, deleted ? 200 : 404, { deleted }, denyIframe);
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
// Update or delete a single request inside a collection (Assertions & Request Chaining editor!)
|
|
150
|
+
const reqItemMatch = /^\/api\/workspace\/collections\/([^/]+)\/requests\/([^/]+)$/.exec(pathname);
|
|
151
|
+
if (reqItemMatch) {
|
|
152
|
+
const colId = decodeURIComponent(reqItemMatch[1]);
|
|
153
|
+
const reqId = decodeURIComponent(reqItemMatch[2]);
|
|
154
|
+
if (method === 'PUT' || method === 'PATCH') {
|
|
155
|
+
const body = await readJsonBody(req);
|
|
156
|
+
const updated = workspaceStore.updateRequestInCollection(colId, reqId, {
|
|
157
|
+
name: asOptionalString(body.name),
|
|
158
|
+
method: asOptionalString(body.method),
|
|
159
|
+
url: asOptionalString(body.url),
|
|
160
|
+
headers: body.headers ? asStringMap(body.headers) : undefined,
|
|
161
|
+
body: typeof body.body === 'string' ? body.body : undefined,
|
|
162
|
+
notes: asOptionalString(body.notes),
|
|
163
|
+
assertions: Array.isArray(body.assertions) ? body.assertions : undefined,
|
|
164
|
+
extract: Array.isArray(body.extract) ? body.extract : undefined,
|
|
165
|
+
});
|
|
166
|
+
sendJson(res, 200, { request: updated, workspace: workspaceStore.getSnapshot(mockStore.list()) }, denyIframe);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
if (method === 'DELETE') {
|
|
170
|
+
const deleted = workspaceStore.deleteRequestFromCollection(colId, reqId);
|
|
171
|
+
sendJson(res, deleted ? 200 : 404, { deleted, workspace: workspaceStore.getSnapshot(mockStore.list()) }, denyIframe);
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (method === 'POST' && pathname === '/api/workspace/collections') {
|
|
176
|
+
const body = await readJsonBody(req);
|
|
177
|
+
if (body.request && typeof body.request === 'object') {
|
|
178
|
+
const r = body.request;
|
|
179
|
+
const saved = workspaceStore.saveRequestToCollection(asOptionalString(body.collectionId), {
|
|
180
|
+
name: asString(r.name, `${asString(r.method, 'GET')} ${asString(r.url)}`),
|
|
181
|
+
method: asString(r.method, 'GET'),
|
|
182
|
+
url: asString(r.url),
|
|
183
|
+
headers: asStringMap(r.headers) ?? {},
|
|
184
|
+
body: asOptionalString(r.body),
|
|
185
|
+
assertions: Array.isArray(r.assertions) ? r.assertions : undefined,
|
|
186
|
+
extract: Array.isArray(r.extract) ? r.extract : undefined,
|
|
187
|
+
});
|
|
188
|
+
sendJson(res, 201, { request: saved, workspace: workspaceStore.getSnapshot(mockStore.list()) }, denyIframe);
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
const col = workspaceStore.addCollection(asString(body.name, 'New Collection'), Array.isArray(body.requests) ? body.requests : []);
|
|
192
|
+
sendJson(res, 201, { collection: col, workspace: workspaceStore.getSnapshot(mockStore.list()) }, denyIframe);
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
if (method === 'DELETE' && pathname.startsWith('/api/workspace/collections/')) {
|
|
196
|
+
const id = pathname.slice('/api/workspace/collections/'.length);
|
|
197
|
+
const deleted = workspaceStore.deleteCollection(id);
|
|
198
|
+
sendJson(res, deleted ? 200 : 404, { deleted }, denyIframe);
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
if (method === 'POST' && pathname === '/api/import/collection') {
|
|
202
|
+
const body = await readJsonBody(req);
|
|
203
|
+
const raw = asString(body.input);
|
|
204
|
+
const fmt = asString(body.format, 'auto').toLowerCase();
|
|
205
|
+
let imported;
|
|
206
|
+
if (fmt === 'postman')
|
|
207
|
+
imported = importPostmanCollection(raw);
|
|
208
|
+
else if (fmt === 'har')
|
|
209
|
+
imported = importHarArchive(raw);
|
|
210
|
+
else if (fmt === 'http')
|
|
211
|
+
imported = importHttpFile(raw);
|
|
212
|
+
else {
|
|
213
|
+
const trimmed = raw.trim();
|
|
214
|
+
if (trimmed.startsWith('{')) {
|
|
215
|
+
const probe = safeJsonParse(trimmed);
|
|
216
|
+
imported = probe && 'log' in probe ? importHarArchive(trimmed) : importPostmanCollection(trimmed);
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
imported = importHttpFile(trimmed);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const collection = workspaceStore.addCollection(imported.collectionName, imported.requests);
|
|
223
|
+
let mountedMocksCount = 0;
|
|
224
|
+
if (body.mountMocks === true && imported.mockRoutes.length > 0) {
|
|
225
|
+
const mounted = mockStore.importRoutes(imported.mockRoutes, false);
|
|
226
|
+
mountedMocksCount = mounted.length;
|
|
227
|
+
workspaceStore.syncMocksAndPersist(mockStore.list());
|
|
228
|
+
}
|
|
229
|
+
sendJson(res, 201, {
|
|
230
|
+
format: imported.format,
|
|
231
|
+
collection,
|
|
232
|
+
importedRequestsCount: imported.requests.length,
|
|
233
|
+
availableMocksCount: imported.mockRoutes.length,
|
|
234
|
+
mountedMocksCount,
|
|
235
|
+
warnings: imported.warnings,
|
|
236
|
+
}, denyIframe);
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ServerResponse } from 'node:http';
|
|
2
|
+
export interface WebhookEvent {
|
|
3
|
+
id: string;
|
|
4
|
+
channel: string;
|
|
5
|
+
receivedAt: string;
|
|
6
|
+
method: string;
|
|
7
|
+
path: string;
|
|
8
|
+
query: Record<string, string>;
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
body: string;
|
|
11
|
+
contentType: string;
|
|
12
|
+
sizeBytes: number;
|
|
13
|
+
remoteAddress: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class WebhookStore {
|
|
16
|
+
private channels;
|
|
17
|
+
private listeners;
|
|
18
|
+
capture(input: Omit<WebhookEvent, 'id' | 'receivedAt'>): WebhookEvent;
|
|
19
|
+
list(rawChannel: string): WebhookEvent[];
|
|
20
|
+
listChannels(): Array<{
|
|
21
|
+
channel: string;
|
|
22
|
+
count: number;
|
|
23
|
+
lastReceivedAt?: string;
|
|
24
|
+
}>;
|
|
25
|
+
clear(rawChannel: string): void;
|
|
26
|
+
totalListeners(): number;
|
|
27
|
+
subscribe(rawChannel: string, res: ServerResponse): () => void;
|
|
28
|
+
private broadcast;
|
|
29
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { generateUuidV7 } from '../core/crypto.js';
|
|
2
|
+
import { assertSafeChannelName } from '../core/security.js';
|
|
3
|
+
const MAX_EVENTS_PER_CHANNEL = 100;
|
|
4
|
+
const MAX_CHANNELS = 50;
|
|
5
|
+
const MAX_SSE_CLIENTS = 50;
|
|
6
|
+
export class WebhookStore {
|
|
7
|
+
channels = new Map();
|
|
8
|
+
listeners = new Map();
|
|
9
|
+
capture(input) {
|
|
10
|
+
const channel = assertSafeChannelName(input.channel);
|
|
11
|
+
if (!this.channels.has(channel) && this.channels.size >= MAX_CHANNELS) {
|
|
12
|
+
// Evict oldest channel to stay bounded
|
|
13
|
+
const oldestKey = this.channels.keys().next().value;
|
|
14
|
+
if (oldestKey)
|
|
15
|
+
this.channels.delete(oldestKey);
|
|
16
|
+
}
|
|
17
|
+
const event = {
|
|
18
|
+
...input,
|
|
19
|
+
channel,
|
|
20
|
+
id: generateUuidV7(),
|
|
21
|
+
receivedAt: new Date().toISOString(),
|
|
22
|
+
};
|
|
23
|
+
const list = this.channels.get(channel) ?? [];
|
|
24
|
+
list.unshift(event);
|
|
25
|
+
if (list.length > MAX_EVENTS_PER_CHANNEL) {
|
|
26
|
+
list.length = MAX_EVENTS_PER_CHANNEL;
|
|
27
|
+
}
|
|
28
|
+
this.channels.set(channel, list);
|
|
29
|
+
this.broadcast(channel, 'webhook', event);
|
|
30
|
+
return event;
|
|
31
|
+
}
|
|
32
|
+
list(rawChannel) {
|
|
33
|
+
const channel = assertSafeChannelName(rawChannel);
|
|
34
|
+
return [...(this.channels.get(channel) ?? [])];
|
|
35
|
+
}
|
|
36
|
+
listChannels() {
|
|
37
|
+
return Array.from(this.channels.entries()).map(([channel, events]) => ({
|
|
38
|
+
channel,
|
|
39
|
+
count: events.length,
|
|
40
|
+
...(events[0] ? { lastReceivedAt: events[0].receivedAt } : {}),
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
clear(rawChannel) {
|
|
44
|
+
const channel = assertSafeChannelName(rawChannel);
|
|
45
|
+
this.channels.set(channel, []);
|
|
46
|
+
this.broadcast(channel, 'cleared', { channel });
|
|
47
|
+
}
|
|
48
|
+
totalListeners() {
|
|
49
|
+
let total = 0;
|
|
50
|
+
for (const set of this.listeners.values())
|
|
51
|
+
total += set.size;
|
|
52
|
+
return total;
|
|
53
|
+
}
|
|
54
|
+
subscribe(rawChannel, res) {
|
|
55
|
+
const channel = assertSafeChannelName(rawChannel);
|
|
56
|
+
if (this.totalListeners() >= MAX_SSE_CLIENTS) {
|
|
57
|
+
throw new Error(`Maximum concurrent SSE listeners (${MAX_SSE_CLIENTS}) reached`);
|
|
58
|
+
}
|
|
59
|
+
let set = this.listeners.get(channel);
|
|
60
|
+
if (!set) {
|
|
61
|
+
set = new Set();
|
|
62
|
+
this.listeners.set(channel, set);
|
|
63
|
+
}
|
|
64
|
+
set.add(res);
|
|
65
|
+
const heartbeat = setInterval(() => {
|
|
66
|
+
if (!res.writableEnded) {
|
|
67
|
+
res.write(': heartbeat\n\n');
|
|
68
|
+
}
|
|
69
|
+
}, 20_000);
|
|
70
|
+
heartbeat.unref();
|
|
71
|
+
return () => {
|
|
72
|
+
clearInterval(heartbeat);
|
|
73
|
+
set?.delete(res);
|
|
74
|
+
if (set && set.size === 0) {
|
|
75
|
+
this.listeners.delete(channel);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
broadcast(channel, eventName, payload) {
|
|
80
|
+
const set = this.listeners.get(channel);
|
|
81
|
+
if (!set || set.size === 0)
|
|
82
|
+
return;
|
|
83
|
+
const frame = `event: ${eventName}\ndata: ${JSON.stringify(payload)}\n\n`;
|
|
84
|
+
for (const res of set) {
|
|
85
|
+
if (!res.writableEnded) {
|
|
86
|
+
res.write(frame);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|