105 lines
3.6 KiB
JavaScript
105 lines
3.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createSessionRouter = createSessionRouter;
|
|
const express_1 = require("express");
|
|
const ExplorationConfig_1 = require("../../core/ExplorationConfig");
|
|
function createSessionRouter(store) {
|
|
const router = (0, express_1.Router)();
|
|
// POST /api/sessions — start a new exploration
|
|
router.post('/', async (req, res) => {
|
|
const body = req.body;
|
|
const { url, seed = 42 } = body;
|
|
if (!url || typeof url !== 'string') {
|
|
res.status(400).json({ error: 'url is required' });
|
|
return;
|
|
}
|
|
// Enforce concurrent session limit
|
|
const stats = store.getStats();
|
|
const limit = store.getMaxConcurrent();
|
|
if (stats.runningSessions >= limit) {
|
|
res.status(429).json({
|
|
error: 'Max concurrent sessions reached',
|
|
active: stats.runningSessions,
|
|
limit,
|
|
});
|
|
return;
|
|
}
|
|
const config = {
|
|
...ExplorationConfig_1.DEFAULT_EXPLORATION_CONFIG,
|
|
...(body.config ?? {}),
|
|
};
|
|
// If allowedDomains not specified, derive from the target URL
|
|
if (config.allowedDomains.length === 0) {
|
|
try {
|
|
const hostname = new URL(url).hostname;
|
|
config.allowedDomains = [hostname];
|
|
}
|
|
catch {
|
|
// leave empty
|
|
}
|
|
}
|
|
const record = await store.startSession({
|
|
url,
|
|
seed,
|
|
maxStates: config.maxStates,
|
|
explorationConfig: config,
|
|
});
|
|
res.status(201).json({
|
|
sessionId: record.sessionId,
|
|
status: record.status,
|
|
startedAt: record.startedAt,
|
|
});
|
|
});
|
|
// GET /api/sessions — list all sessions
|
|
router.get('/', (_req, res) => {
|
|
const sessions = store.getAllSessions().map((s) => ({
|
|
sessionId: s.sessionId,
|
|
url: s.url,
|
|
status: s.status,
|
|
startedAt: s.startedAt,
|
|
anomaliesFound: s.anomaliesFound,
|
|
statesVisited: s.statesVisited,
|
|
}));
|
|
res.json(sessions);
|
|
});
|
|
// GET /api/sessions/:sessionId — session detail
|
|
router.get('/:sessionId', (req, res) => {
|
|
const record = store.getSession(req.params['sessionId']);
|
|
if (!record) {
|
|
res.status(404).json({ error: 'Session not found' });
|
|
return;
|
|
}
|
|
res.json({
|
|
sessionId: record.sessionId,
|
|
url: record.url,
|
|
status: record.status,
|
|
startedAt: record.startedAt,
|
|
finishedAt: record.finishedAt,
|
|
statesVisited: record.statesVisited,
|
|
anomaliesFound: record.anomaliesFound,
|
|
seed: record.seed,
|
|
});
|
|
});
|
|
// DELETE /api/sessions/:sessionId — stop an active session
|
|
router.delete('/:sessionId', (req, res) => {
|
|
const stopped = store.stopSession(req.params['sessionId']);
|
|
if (!stopped) {
|
|
res.status(404).json({ error: 'Session not found or not running' });
|
|
return;
|
|
}
|
|
res.json({ stopped: true });
|
|
});
|
|
// GET /api/sessions/:sessionId/performance — performance metrics for session
|
|
router.get('/:sessionId/performance', (req, res) => {
|
|
const sessionId = req.params['sessionId'];
|
|
const record = store.getSession(sessionId);
|
|
if (!record) {
|
|
res.status(404).json({ error: 'Session not found' });
|
|
return;
|
|
}
|
|
const metrics = store.getPerformanceMetrics(sessionId);
|
|
res.json(metrics);
|
|
});
|
|
return router;
|
|
}
|