refactor(auth): standardize session management and security

- Centralize secret retrieval using `getAdminSessionSecret`
- Replace manual session parsing with `verifyRequestAdminSession` utility
- Tighten API validation and improve security headers handling
- Enforce environment variable security for production deployments
This commit is contained in:
Team Dark Devil committed 2026-11-18 05:18:55 +06:00
1 parent 34519c07df
commit 177ed4a844
78 files changed
+6748 -3157

No files matched your search

+2 -4
View File
@@ -9,10 +9,8 @@ import { redirect } from 'next/navigation';
export const dynamic = 'force-dynamic';
export default async function LandingPage() {
if (process.env.DATABASE_URL) {
const isConfigured = await isAdminSetupComplete().catch(() => false);
if (!isConfigured) redirect('/vcon/setup');
}
const isConfigured = await isAdminSetupComplete().catch(() => false);
if (!isConfigured) redirect('/vcon/setup');
return (
<MainLayout>
+102 -79
View File
@@ -1,11 +1,12 @@
import {
createAdminSessionToken,
getAdminSessionSecret,
getSessionCookieOptions,
} from "@/core/auth/adminSession";
import {
authenticateAdminCredentials,
clearAdminLoginAttempts,
consumeAdminLoginAttempt,
verifyAdminCredentials,
} from "@/core/auth/adminUsers";
import { NextRequest, NextResponse } from "next/server";
@@ -13,10 +14,19 @@ const SESSION_COOKIE = "datastack_admin_session";
const SESSION_SECONDS = 8 * 60 * 60;
const REMEMBERED_SESSION_SECONDS = 30 * 24 * 60 * 60;
function extractClientIp(request: NextRequest) {
const forwardedFor = request.headers.get("x-forwarded-for");
if (forwardedFor) {
const first = forwardedFor.split(",")[0]?.trim();
if (first) return first;
}
const realIp = request.headers.get("x-real-ip")?.trim();
if (realIp) return realIp;
return "unknown";
}
export async function POST(request: NextRequest) {
const sessionSecret =
process.env.DATASTACK_ADMIN_SESSION_SECRET ||
"datastack-admin-session-default-secret-32-chars";
const sessionSecret = getAdminSessionSecret();
if (!sessionSecret || sessionSecret.length < 32) {
return NextResponse.json(
@@ -27,86 +37,99 @@ export async function POST(request: NextRequest) {
);
}
let body: Record<string, unknown>;
try {
const body = await request.json();
const email =
typeof body.email === "string" ? body.email.trim().toLowerCase() : "";
const password = typeof body.password === "string" ? body.password : "";
if (!email || password.length === 0 || password.length > 1024) {
return NextResponse.json(
{ error: "Invalid email or password." },
{ status: 401 },
);
}
let throttleDecision;
try {
throttleDecision = await consumeAdminLoginAttempt(email, sessionSecret);
} catch {
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
if (!throttleDecision.allowed) {
return NextResponse.json(
{ error: "Too many sign-in attempts. Try again later." },
{
status: 429,
headers: {
"Retry-After": String(throttleDecision.retryAfterSeconds),
},
},
);
}
let authenticated = false;
try {
authenticated = await verifyAdminCredentials(email, password);
} catch {
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
if (!authenticated) {
return NextResponse.json(
{ error: "Invalid email or password." },
{ status: 401 },
);
}
try {
await clearAdminLoginAttempts(email, sessionSecret);
} catch {
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
const remember = body.remember === true;
const maxAge = remember ? REMEMBERED_SESSION_SECONDS : SESSION_SECONDS;
const expiresAt = Date.now() + maxAge * 1000;
const token = await createAdminSessionToken(
email,
sessionSecret,
expiresAt,
);
const response = NextResponse.json({ success: true });
response.cookies.set(
SESSION_COOKIE,
token,
getSessionCookieOptions(request, maxAge),
);
return response;
const parsed = await request.json();
body =
parsed && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: {};
} catch {
return NextResponse.json(
{ error: "Invalid login request." },
{ status: 400 },
);
}
const email =
typeof body.email === "string" ? body.email.trim().toLowerCase() : "";
const password = typeof body.password === "string" ? body.password : "";
if (!email || password.length === 0 || password.length > 1024) {
return NextResponse.json(
{ error: "Invalid email or password." },
{ status: 401 },
);
}
const clientIp = extractClientIp(request);
let throttleDecision;
try {
throttleDecision = await consumeAdminLoginAttempt(
email,
sessionSecret,
clientIp,
);
} catch {
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
if (!throttleDecision.allowed) {
return NextResponse.json(
{ error: "Too many sign-in attempts. Try again later." },
{
status: 429,
headers: {
"Retry-After": String(throttleDecision.retryAfterSeconds),
},
},
);
}
let authResult: { authenticated: boolean; tokenVersion: number };
try {
authResult = await authenticateAdminCredentials(email, password);
} catch {
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
if (!authResult.authenticated) {
return NextResponse.json(
{ error: "Invalid email or password." },
{ status: 401 },
);
}
try {
await clearAdminLoginAttempts(email, sessionSecret, clientIp);
} catch {
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
const remember = body.remember === true;
const maxAge = remember ? REMEMBERED_SESSION_SECONDS : SESSION_SECONDS;
const expiresAt = Date.now() + maxAge * 1000;
const token = await createAdminSessionToken(
email,
sessionSecret,
expiresAt,
authResult.tokenVersion,
);
const response = NextResponse.json({ success: true });
response.cookies.set(
SESSION_COOKIE,
token,
getSessionCookieOptions(request, maxAge),
);
return response;
}
+54 -35
View File
@@ -1,11 +1,11 @@
import {
createAdminSessionToken,
getAdminSessionPayload,
getSessionCookieOptions,
} from "@/core/auth/adminSession";
import {
getAdminAccountInfo,
updateAdminAccount,
verifyRequestAdminSession,
} from "@/core/auth/adminUsers";
import { NextRequest, NextResponse } from "next/server";
@@ -17,51 +17,69 @@ function validEmail(email: string) {
}
export async function GET(request: NextRequest) {
const sessionSecret =
process.env.DATASTACK_ADMIN_SESSION_SECRET ||
"datastack-admin-session-default-secret-32-chars";
const token = request.cookies.get(SESSION_COOKIE)?.value || "";
const session = await getAdminSessionPayload(token, sessionSecret);
const token = request.cookies.get(SESSION_COOKIE)?.value;
const auth = await verifyRequestAdminSession(token);
if (!session) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: 401 },
);
if (!auth.valid) {
const status = auth.reason === "UNAUTHORIZED" ? 401 : 503;
const message =
auth.reason === "UNAUTHORIZED"
? "Authentication required."
: "Admin authentication is temporarily unavailable.";
return NextResponse.json({ error: message }, { status });
}
const account = await getAdminAccountInfo(session.email);
return NextResponse.json({
profile: {
email: account.email,
role: "Owner",
storageMode: account.storageMode,
createdAt: account.createdAt,
sessionExpiresAt: new Date(session.expiresAt).toISOString(),
},
});
try {
const account = await getAdminAccountInfo(auth.session.email);
return NextResponse.json({
profile: {
email: account.email,
role: "Owner",
storageMode: account.storageMode,
createdAt: account.createdAt,
sessionExpiresAt: new Date(auth.session.expiresAt).toISOString(),
},
});
} catch {
return NextResponse.json(
{ error: "Unable to load admin profile." },
{ status: 503 },
);
}
}
export async function PUT(request: NextRequest) {
const sessionSecret =
process.env.DATASTACK_ADMIN_SESSION_SECRET ||
"datastack-admin-session-default-secret-32-chars";
const token = request.cookies.get(SESSION_COOKIE)?.value || "";
const session = await getAdminSessionPayload(token, sessionSecret);
const token = request.cookies.get(SESSION_COOKIE)?.value;
const auth = await verifyRequestAdminSession(token);
if (!session) {
if (!auth.valid) {
const status = auth.reason === "UNAUTHORIZED" ? 401 : 503;
const message =
auth.reason === "UNAUTHORIZED"
? "Authentication required."
: "Admin authentication is temporarily unavailable.";
return NextResponse.json({ error: message }, { status });
}
let body: Record<string, unknown>;
try {
const parsed = await request.json();
body =
parsed && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: {};
} catch {
return NextResponse.json(
{ error: "Authentication required." },
{ status: 401 },
{ error: "Invalid profile payload." },
{ status: 400 },
);
}
try {
const body = await request.json();
const nextEmail =
typeof body.email === "string"
? body.email.trim().toLowerCase()
: session.email;
: auth.session.email;
const currentPassword =
typeof body.currentPassword === "string" ? body.currentPassword : "";
const newPassword =
@@ -88,7 +106,7 @@ export async function PUT(request: NextRequest) {
);
}
const account = await getAdminAccountInfo(session.email);
const account = await getAdminAccountInfo(auth.session.email);
const result = await updateAdminAccount({
currentEmail: account.email,
currentPassword,
@@ -103,13 +121,14 @@ export async function PUT(request: NextRequest) {
// Preserve remaining session duration (e.g., 30-day "Remember me" session)
const remainingSeconds = Math.max(
SESSION_SECONDS,
Math.ceil((session.expiresAt - Date.now()) / 1000),
Math.ceil((auth.session.expiresAt - Date.now()) / 1000),
);
const expiresAt = Date.now() + remainingSeconds * 1000;
const nextToken = await createAdminSessionToken(
result.email,
sessionSecret,
auth.sessionSecret,
expiresAt,
result.tokenVersion,
);
const response = NextResponse.json({
@@ -133,7 +152,7 @@ export async function PUT(request: NextRequest) {
} catch {
return NextResponse.json(
{ error: "Unable to update profile." },
{ status: 400 },
{ status: 500 },
);
}
}
+17 -4
View File
@@ -1,5 +1,6 @@
import {
createAdminSessionToken,
getAdminSessionSecret,
getSessionCookieOptions,
} from "@/core/auth/adminSession";
import {
@@ -20,9 +21,7 @@ function validEmail(email: string) {
}
export async function POST(request: NextRequest) {
const sessionSecret =
process.env.DATASTACK_ADMIN_SESSION_SECRET ||
"datastack-admin-session-default-secret-32-chars";
const sessionSecret = getAdminSessionSecret();
if (!sessionSecret || sessionSecret.length < 32) {
return NextResponse.json(
{ error: "Admin session configuration is unavailable." },
@@ -30,8 +29,21 @@ export async function POST(request: NextRequest) {
);
}
let body: Record<string, unknown>;
try {
const parsed = await request.json();
body =
parsed && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: {};
} catch {
return NextResponse.json(
{ error: "Invalid setup request payload." },
{ status: 400 },
);
}
try {
const body = await request.json();
const email =
typeof body.email === "string" ? body.email.trim().toLowerCase() : "";
const password = typeof body.password === "string" ? body.password : "";
@@ -74,6 +86,7 @@ export async function POST(request: NextRequest) {
email,
sessionSecret,
Date.now() + SESSION_SECONDS * 1000,
1,
);
const response = NextResponse.json({ success: true });
response.cookies.set(
+46 -4
View File
@@ -1,23 +1,54 @@
import { verifyRequestAdminSession } from "@/core/auth/adminUsers";
import { SDKManager } from "@/core/sdk/sdkManager";
import { NextRequest, NextResponse } from "next/server";
const SESSION_COOKIE = "datastack_admin_session";
export async function GET(
_req: NextRequest,
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
const { id } = await params;
const client = await SDKManager.getClientAsync(id);
if (!client) {
return NextResponse.json({ error: "Client not found" }, { status: 404 });
}
const { secret, token, ...clientSummary } = client;
return NextResponse.json({ client: clientSummary });
void secret;
const includeToken = req.nextUrl.searchParams.get("includeToken") === "true";
return NextResponse.json({
client: {
...clientSummary,
tokenPreview: token ? `${token.slice(0, 12)}...` : "",
...(includeToken ? { token } : {}),
},
});
}
export async function PATCH(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
const { id } = await params;
try {
const body = await req.json();
@@ -46,11 +77,22 @@ export async function PATCH(
}
export async function DELETE(
_req: NextRequest,
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
const { id } = await params;
const deleted = await SDKManager.deleteClientAsync(id);
const purgeFiles = req.nextUrl.searchParams.get("purgeFiles") === "true";
const deleted = await SDKManager.deleteClientAsync(id, { purgeFiles });
if (!deleted) {
return NextResponse.json({ error: "Client not found" }, { status: 404 });
}
+43 -4
View File
@@ -1,12 +1,28 @@
import { verifyRequestAdminSession } from "@/core/auth/adminUsers";
import { SDKManager } from "@/core/sdk/sdkManager";
import { NextRequest, NextResponse } from "next/server";
export async function GET() {
const SESSION_COOKIE = "datastack_admin_session";
export async function GET(req: NextRequest) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
try {
const allClients = await SDKManager.listClientsAsync();
const clients = allClients.map(({ secret, ...client }) => {
const clients = allClients.map(({ secret, token, ...client }) => {
void secret;
return client;
return {
...client,
tokenPreview: token ? `${token.slice(0, 12)}...` : "",
};
});
return NextResponse.json({ clients, count: clients.length });
} catch (err: unknown) {
@@ -17,8 +33,31 @@ export async function GET() {
}
export async function POST(req: NextRequest) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
let body: Record<string, unknown>;
try {
const parsed = await req.json();
body =
parsed && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: {};
} catch {
return NextResponse.json(
{ error: "Invalid request payload." },
{ status: 400 },
);
}
try {
const body = await req.json();
const { clientId, name, rateLimit } = body;
if (!clientId || !name) {
+35 -9
View File
@@ -1,4 +1,6 @@
import { SettingsManager } from "@/core/adminSettings/settingsManager";
import { DataBufferEngine } from "@/core/data/buffer";
import { getInternalIngestionHeaders } from "@/core/Security/security";
import { CSVStorageManager } from "@/core/Storage/csvStorage";
import { parse } from "csv-parse";
import { NextRequest, NextResponse } from "next/server";
@@ -31,18 +33,25 @@ export async function GET(request: NextRequest) {
let total = 0;
const addIfMatching = (event: DataEvent) => {
const eventDate = event.timestamp.slice(0, 10);
const eventDate = CSVStorageManager.extractSafePartitionDate(event.timestamp);
const dedupeKey = `${event.clientId}:${event.eventId}`;
const matchesSearch =
!search ||
event.payload.toLowerCase().includes(search) ||
event.eventId.toLowerCase().includes(search) ||
event.clientId.toLowerCase().includes(search);
if (
(clientId !== "all" && event.clientId !== clientId) ||
(from && eventDate < from) ||
(to && eventDate > to) ||
(search && !event.payload.toLowerCase().includes(search)) ||
eventIds.has(event.eventId)
!matchesSearch ||
eventIds.has(dedupeKey)
) {
return;
}
eventIds.add(event.eventId);
eventIds.add(dedupeKey);
total += 1;
let low = 0;
@@ -109,7 +118,16 @@ export async function GET(request: NextRequest) {
}
}
for (const event of DataBufferEngine.getRecentEvents(200)) {
const settings = await SettingsManager.getSettingsAsync();
const recentWindowLimit = Math.min(
2000,
Math.max(200, settings.batchFlushThreshold || 500),
);
for (const event of await DataBufferEngine.getRecentEventsAsync(
recentWindowLimit,
clientId,
)) {
addIfMatching({ ...event, status: "buffered" });
}
@@ -117,10 +135,18 @@ export async function GET(request: NextRequest) {
process.env.INGESTION_INTERNAL_URL || "http://127.0.0.1:4000"
).replace(/\/$/, "");
try {
const res = await fetch(`${ingestionBaseUrl}/internal/recent?limit=200`, {
cache: "no-store",
signal: AbortSignal.timeout(1000),
});
const qs = new URLSearchParams({ limit: String(recentWindowLimit) });
if (clientId && clientId !== "all") {
qs.set("clientId", clientId);
}
const res = await fetch(
`${ingestionBaseUrl}/internal/recent?${qs.toString()}`,
{
headers: getInternalIngestionHeaders(),
cache: "no-store",
signal: AbortSignal.timeout(1000),
},
);
if (res.ok) {
const data = await res.json();
if (Array.isArray(data.events)) {
+75 -30
View File
@@ -1,9 +1,12 @@
import { SettingsManager } from '@/core/adminSettings/settingsManager';
import { verifyAdminSessionToken } from '@/core/auth/adminSession';
import { verifyRequestAdminSession } from '@/core/auth/adminUsers';
import { AuthService } from '@/core/auth/jwt';
import { DataBufferEngine } from '@/core/data/buffer';
import { SDKManager } from '@/core/sdk/sdkManager';
import { SecurityEngine } from '@/core/Security/security';
import {
SecurityEngine,
getInternalIngestionHeaders,
} from '@/core/Security/security';
import { CSVStorageManager, type DataEvent } from '@/core/Storage/csvStorage';
import { parse } from 'csv-parse';
import { NextRequest, NextResponse } from 'next/server';
@@ -15,14 +18,15 @@ async function getRecentStoredEvents(
limit: number,
clientId?: string,
): Promise<DataEvent[]> {
const files = CSVStorageManager.listAllFiles()
const allFiles = await CSVStorageManager.listAllFilesAsync();
const files = allFiles
.filter((f) => !clientId || clientId === 'all' || f.clientId === clientId)
.slice(0, 5);
const collected: DataEvent[] = [];
for (const file of files) {
const fileEvents: DataEvent[] = [];
const fileTail: DataEvent[] = [];
const parser = createReadStream(file.filePath).pipe(
parse({ columns: true, skip_empty_lines: true, bom: true }),
);
@@ -32,7 +36,10 @@ async function getRecentStoredEvents(
Record<string, string>
>) {
if (!record.eventId) continue;
fileEvents.push({
if (fileTail.length >= limit) {
fileTail.shift();
}
fileTail.push({
clientId: record.clientId || file.clientId,
eventId: record.eventId,
timestamp: record.timestamp || `${file.date}T00:00:00.000Z`,
@@ -43,8 +50,8 @@ async function getRecentStoredEvents(
parser.destroy();
}
if (fileEvents.length > 0) {
collected.push(...fileEvents.slice(-limit));
if (fileTail.length > 0) {
collected.push(...fileTail);
}
}
@@ -55,9 +62,10 @@ export async function GET(req: NextRequest) {
try {
const { searchParams } = new URL(req.url);
const clientId = searchParams.get('clientId') || undefined;
const limit = Math.min(200, Math.max(1, Number(searchParams.get('limit')) || 50));
const limit = Math.min(2000, Math.max(1, Number(searchParams.get('limit')) || 50));
const bufferedEvents = DataBufferEngine.getRecentEvents(limit, clientId);
await SettingsManager.getSettingsAsync();
const bufferedEvents = await DataBufferEngine.getRecentEventsAsync(limit, clientId);
let socketBufferedEvents: DataEvent[] = [];
const ingestionBaseUrl = (
process.env.INGESTION_INTERNAL_URL || 'http://127.0.0.1:4000'
@@ -67,6 +75,7 @@ export async function GET(req: NextRequest) {
const qs = new URLSearchParams({ limit: String(limit) });
if (clientId) qs.set('clientId', clientId);
const res = await fetch(`${ingestionBaseUrl}/internal/recent?${qs.toString()}`, {
headers: getInternalIngestionHeaders(),
cache: 'no-store',
signal: AbortSignal.timeout(1000),
});
@@ -86,8 +95,10 @@ export async function GET(req: NextRequest) {
const merged: DataEvent[] = [];
for (const evt of [...bufferedEvents, ...socketBufferedEvents, ...storedEvents]) {
if (!evt.eventId || seenIds.has(evt.eventId)) continue;
seenIds.add(evt.eventId);
if (!evt.eventId) continue;
const dedupeKey = `${evt.clientId}:${evt.eventId}`;
if (seenIds.has(dedupeKey)) continue;
seenIds.add(dedupeKey);
merged.push(evt);
}
@@ -107,9 +118,6 @@ export async function POST(req: NextRequest) {
// Verify either Client Bearer JWT or Admin Session Cookie
const authHeader = req.headers.get('authorization');
const sessionSecret =
process.env.DATASTACK_ADMIN_SESSION_SECRET ||
'datastack-admin-session-default-secret-32-chars';
const sessionCookie = req.cookies.get(SESSION_COOKIE)?.value;
let tokenClientId: string | null = null;
@@ -128,20 +136,30 @@ export async function POST(req: NextRequest) {
tokenClientId = verified.payload.clientId;
tokenRateLimit = verified.payload.rateLimit;
} else {
const isAdmin = Boolean(
sessionSecret &&
sessionCookie &&
(await verifyAdminSessionToken(sessionCookie, sessionSecret)),
);
if (!isAdmin) {
const auth = await verifyRequestAdminSession(sessionCookie);
if (!auth.valid) {
return NextResponse.json(
{ error: 'Authentication required. Provide a valid Client Bearer JWT or Admin Session.' },
{ status: 401 },
{
error:
auth.reason === 'UNAUTHORIZED'
? 'Authentication required. Provide a valid Client Bearer JWT or Admin Session.'
: 'Admin authentication is temporarily unavailable.',
},
{ status: auth.reason === 'UNAUTHORIZED' ? 401 : 503 },
);
}
}
const body = await req.json();
let body: unknown;
try {
body = await req.json();
} catch {
return NextResponse.json(
{ error: 'Invalid JSON request payload.', code: 'INVALID_JSON' },
{ status: 400 },
);
}
const bodyObj =
body && typeof body === 'object' && !Array.isArray(body)
? (body as Record<string, unknown>)
@@ -154,7 +172,27 @@ export async function POST(req: NextRequest) {
.toLowerCase()
.replace(/[^a-z0-9_-]/g, '_');
if (await SDKManager.isClientDeletedAsync(clientId)) {
return NextResponse.json(
{
error: `CLIENT_REVOKED: Client '${clientId}' has been deleted by administrator.`,
code: 'CLIENT_REVOKED',
},
{ status: 403 },
);
}
const registeredClient = await SDKManager.getClientAsync(clientId);
if (rawBearerToken && !registeredClient) {
return NextResponse.json(
{
error: `CLIENT_REVOKED: Client '${clientId}' registration not found or has been deleted.`,
code: 'CLIENT_REVOKED',
},
{ status: 403 },
);
}
if (registeredClient) {
if (registeredClient.status === 'revoked') {
return NextResponse.json(
@@ -196,6 +234,7 @@ export async function POST(req: NextRequest) {
: null;
if (batchItems !== null) {
const envelopeKeys = new Set(['eventId', 'timestamp', 'data', 'clientId']);
let processed = 0;
let rejected = 0;
let duplicates = 0;
@@ -205,6 +244,8 @@ export async function POST(req: NextRequest) {
const oversizedEventIds: string[] = [];
const errors: string[] = [];
const batchEvents: DataEvent[] = [];
for (const item of batchItems) {
const safeItem =
item && typeof item === 'object' && !Array.isArray(item)
@@ -221,8 +262,10 @@ export async function POST(req: NextRequest) {
? safeItem.timestamp.trim()
: new Date().toISOString();
const itemData =
safeItem.data !== undefined ? safeItem.data : safeItem;
const isEnvelope =
safeItem.data !== undefined &&
Object.keys(safeItem).every((k) => envelopeKeys.has(k));
const itemData = isEnvelope ? safeItem.data : safeItem;
const val = SecurityEngine.validateEvent(
clientId,
@@ -251,18 +294,20 @@ export async function POST(req: NextRequest) {
continue;
}
const event: DataEvent = {
batchEvents.push({
clientId,
eventId: itemEventId,
timestamp: itemTimestamp,
payload:
typeof itemData === 'string' ? itemData : JSON.stringify(itemData),
};
DataBufferEngine.push(event);
});
processed += 1;
}
if (batchEvents.length > 0) {
await DataBufferEngine.pushBatchAsync(batchEvents);
}
if (processed > 0) {
SDKManager.incrementEventCount(clientId, processed);
}
@@ -347,7 +392,7 @@ export async function POST(req: NextRequest) {
payload: typeof data === 'string' ? data : JSON.stringify(data),
};
DataBufferEngine.push(event);
await DataBufferEngine.pushAsync(event);
SDKManager.incrementEventCount(clientId, 1);
return NextResponse.json({ success: true, eventId, event });
+40 -14
View File
@@ -1,4 +1,6 @@
import { SettingsManager } from "@/core/adminSettings/settingsManager";
import { DataBufferEngine } from "@/core/data/buffer";
import { getInternalIngestionHeaders } from "@/core/Security/security";
import {
CSVStorageManager,
type StorageFileMeta,
@@ -6,6 +8,7 @@ import {
import { parse } from "csv-parse";
import { NextRequest, NextResponse } from "next/server";
import { createReadStream } from "node:fs";
import path from "node:path";
type ExportFormat = "csv" | "json";
type CsvRecord = Record<string, string>;
@@ -108,19 +111,26 @@ function streamFile(file: StorageFileMeta) {
export async function GET(request: NextRequest) {
try {
await DataBufferEngine.flush();
const ingestionBaseUrl =
process.env.INGESTION_INTERNAL_URL || "http://127.0.0.1:4000";
try {
await fetch(`${ingestionBaseUrl}/internal/flush`, {
method: "POST",
signal: AbortSignal.timeout(1500),
});
} catch {
// Port 4000 may not be running in isolated single-process dev mode
const { searchParams } = new URL(request.url);
const shouldFlush = searchParams.get("flush") !== "false";
await SettingsManager.getSettingsAsync();
if (shouldFlush) {
await DataBufferEngine.flush();
const ingestionBaseUrl = (
process.env.INGESTION_INTERNAL_URL || "http://127.0.0.1:4000"
).replace(/\/$/, "");
try {
await fetch(`${ingestionBaseUrl}/internal/flush`, {
method: "POST",
headers: getInternalIngestionHeaders(),
signal: AbortSignal.timeout(1500),
});
} catch {
// Port 4000 may not be running in isolated single-process dev mode
}
}
const { searchParams } = new URL(request.url);
const clientId = searchParams.get("clientId") || "all";
const filename = searchParams.get("filename");
const from = searchParams.get("from") || "";
@@ -129,17 +139,33 @@ export async function GET(request: NextRequest) {
searchParams.get("format") === "json" ? "json" : "csv";
const includeHeader = searchParams.get("includeHeader") !== "false";
let files = CSVStorageManager.listAllFiles();
let files = await CSVStorageManager.listAllFilesAsync();
if (clientId !== "all")
files = files.filter((file) => file.clientId === clientId);
if (filename) {
const file = files.find((candidate) => candidate.filename === filename);
if (!file)
const safeFilename = path.basename(filename);
const matches = files.filter(
(candidate) => candidate.filename === safeFilename,
);
if (matches.length === 0) {
return NextResponse.json(
{ error: "CSV file not found" },
{ status: 404 },
);
}
if (matches.length > 1) {
return NextResponse.json(
{
error:
"Ambiguous filename matches multiple clients. Specify clientId.",
code: "AMBIGUOUS_FILENAME",
matchingClients: matches.map((m) => m.clientId),
},
{ status: 400 },
);
}
const file = matches[0];
return new NextResponse(streamFile(file), {
status: 200,
headers: {
+13 -1
View File
@@ -23,7 +23,19 @@ export async function GET(req: NextRequest) {
);
}
const clientId = verified.payload.clientId;
const clientId = String(verified.payload.clientId || '')
.trim()
.toLowerCase();
if (SDKManager.isClientDeleted(clientId)) {
return NextResponse.json(
{
status: 'error',
code: 'CLIENT_REVOKED',
error: `CLIENT_REVOKED: Client '${clientId}' has been deleted by administrator.`,
},
{ status: 403 },
);
}
const registeredClient = await SDKManager.getClientAsync(clientId);
if (registeredClient) {
if (registeredClient.status === 'revoked') {
+176 -4
View File
@@ -1,8 +1,55 @@
import { SettingsManager } from "@/core/adminSettings/settingsManager";
import {
SettingsManager,
type AdminSettings,
} from "@/core/adminSettings/settingsManager";
import { verifyRequestAdminSession } from "@/core/auth/adminUsers";
import { NextRequest, NextResponse } from "next/server";
export async function GET() {
const SESSION_COOKIE = "datastack_admin_session";
const ALLOWED_SETTING_KEYS = new Set([
"maxEventsPerSec",
"maxPayloadSizeKb",
"batchFlushThreshold",
"batchFlushIntervalMs",
"fileRotationMb",
"enableIdempotency",
"autoPruneDays",
"jwtSecret",
]);
function parseBoundedInt(
raw: unknown,
field: string,
min: number,
max: number,
): number {
if (typeof raw !== "number" || !Number.isInteger(raw)) {
throw new Error(`${field} must be an integer between ${min} and ${max}.`);
}
if (raw < min || raw > max) {
throw new Error(`${field} must be between ${min} and ${max}.`);
}
return raw;
}
export async function GET(req: NextRequest) {
try {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{
error:
auth.reason === "UNAUTHORIZED"
? "Authentication required."
: "Admin authentication is temporarily unavailable.",
},
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
const settings = await SettingsManager.getPublicSettingsAsync();
return NextResponse.json({ settings });
} catch (err: unknown) {
@@ -13,8 +60,133 @@ export async function GET() {
export async function POST(req: NextRequest) {
try {
const body = await req.json();
await SettingsManager.updateSettingsAsync(body);
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{
error:
auth.reason === "UNAUTHORIZED"
? "Authentication required."
: "Admin authentication is temporarily unavailable.",
},
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
let rawBody: unknown;
try {
rawBody = await req.json();
} catch {
return NextResponse.json(
{ error: "Invalid JSON request payload.", code: "INVALID_JSON" },
{ status: 400 },
);
}
if (!rawBody || typeof rawBody !== "object" || Array.isArray(rawBody)) {
return NextResponse.json(
{
error: "Settings payload must be a JSON object.",
code: "VALIDATION_ERROR",
},
{ status: 400 },
);
}
const body = rawBody as Record<string, unknown>;
const unknownKeys = Object.keys(body).filter(
(key) => !ALLOWED_SETTING_KEYS.has(key),
);
if (unknownKeys.length > 0) {
return NextResponse.json(
{
error: `Unsupported settings field(s): ${unknownKeys.join(", ")}`,
code: "VALIDATION_ERROR",
},
{ status: 400 },
);
}
const validated: Partial<AdminSettings> = {};
try {
if (body.maxEventsPerSec !== undefined) {
validated.maxEventsPerSec = parseBoundedInt(
body.maxEventsPerSec,
"maxEventsPerSec",
1,
100000,
);
}
if (body.maxPayloadSizeKb !== undefined) {
validated.maxPayloadSizeKb = parseBoundedInt(
body.maxPayloadSizeKb,
"maxPayloadSizeKb",
1,
10240,
);
}
if (body.batchFlushThreshold !== undefined) {
validated.batchFlushThreshold = parseBoundedInt(
body.batchFlushThreshold,
"batchFlushThreshold",
1,
50000,
);
}
if (body.batchFlushIntervalMs !== undefined) {
validated.batchFlushIntervalMs = parseBoundedInt(
body.batchFlushIntervalMs,
"batchFlushIntervalMs",
100,
60000,
);
}
if (body.fileRotationMb !== undefined) {
validated.fileRotationMb = parseBoundedInt(
body.fileRotationMb,
"fileRotationMb",
1,
2048,
);
}
if (body.autoPruneDays !== undefined) {
validated.autoPruneDays = parseBoundedInt(
body.autoPruneDays,
"autoPruneDays",
0,
3650,
);
}
if (body.enableIdempotency !== undefined) {
if (typeof body.enableIdempotency !== "boolean") {
throw new Error("enableIdempotency must be a boolean.");
}
validated.enableIdempotency = body.enableIdempotency;
}
if (body.jwtSecret !== undefined) {
if (typeof body.jwtSecret !== "string" || body.jwtSecret.trim().length < 32) {
throw new Error(
"jwtSecret must be a string with at least 32 characters.",
);
}
validated.jwtSecret = body.jwtSecret.trim();
}
} catch (validationErr: unknown) {
return NextResponse.json(
{
error:
validationErr instanceof Error
? validationErr.message
: "Invalid settings value.",
code: "VALIDATION_ERROR",
},
{ status: 400 },
);
}
await SettingsManager.updateSettingsAsync(validated);
const settings = await SettingsManager.getPublicSettingsAsync();
return NextResponse.json({
settings,
+59 -11
View File
@@ -1,4 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
import { SettingsManager } from '@/core/adminSettings/settingsManager';
import { getInternalIngestionHeaders } from '@/core/Security/security';
import { CSVStorageManager } from '@/core/Storage/csvStorage';
import { DataBufferEngine } from '@/core/data/buffer';
@@ -7,8 +9,11 @@ export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const clientId = searchParams.get('clientId') || undefined;
const stats = CSVStorageManager.getStorageStats();
let files = CSVStorageManager.listAllFiles();
const [stats, allFiles] = await Promise.all([
CSVStorageManager.getStorageStatsAsync(),
CSVStorageManager.listAllFilesAsync(),
]);
let files = allFiles;
if (clientId && clientId !== 'all') {
files = files.filter((f) => f.clientId === clientId);
@@ -23,12 +28,17 @@ export async function GET(req: NextRequest) {
export async function POST(_req: NextRequest) {
try {
// 1. Flush local Next.js DataBufferEngine
await SettingsManager.getSettingsAsync();
// 1. Flush local / shared Next.js DataBufferEngine
const localRes = await DataBufferEngine.flush();
// 2. Also flush Port 4000 Socket.IO ingestion buffer
let remoteCount = 0;
let remoteFiles: string[] = [];
let ingestionFlushed = false;
let warning: string | undefined;
const explicitIngestionUrl = Boolean(process.env.INGESTION_INTERNAL_URL?.trim());
const ingestionBaseUrl = (
process.env.INGESTION_INTERNAL_URL || 'http://127.0.0.1:4000'
).replace(/\/$/, '');
@@ -36,17 +46,53 @@ export async function POST(_req: NextRequest) {
try {
const ingestRes = await fetch(`${ingestionBaseUrl}/internal/flush`, {
method: 'POST',
headers: getInternalIngestionHeaders(),
cache: 'no-store',
signal: AbortSignal.timeout(2500),
});
if (ingestRes.ok) {
const ingestData = await ingestRes.json();
remoteCount = Number(ingestData.flushedCount) || 0;
if (Array.isArray(ingestData.filesUpdated)) {
remoteFiles = ingestData.filesUpdated;
}
if (!ingestRes.ok) {
const errPayload = await ingestRes.json().catch(() => ({}));
const remoteErr =
typeof errPayload?.error === 'string'
? errPayload.error
: `Ingestion flush returned HTTP ${ingestRes.status}`;
return NextResponse.json(
{
success: false,
error: remoteErr,
code: 'INGESTION_FLUSH_FAILED',
flushedCount: localRes.count,
filesUpdated: localRes.files,
ingestionFlushed: false,
ingestionStatus: ingestRes.status,
},
{ status: 502 },
);
}
} catch {
// Port 4000 server may be offline in standalone Next.js dev mode
ingestionFlushed = true;
const ingestData = await ingestRes.json();
remoteCount = Number(ingestData.flushedCount) || 0;
if (Array.isArray(ingestData.filesUpdated)) {
remoteFiles = ingestData.filesUpdated;
}
} catch (err: unknown) {
const errMsg =
err instanceof Error ? err.message : 'Ingestion server unreachable';
if (explicitIngestionUrl) {
return NextResponse.json(
{
success: false,
error: `Ingestion server flush failed: ${errMsg}`,
code: 'INGESTION_UNREACHABLE',
flushedCount: localRes.count,
filesUpdated: localRes.files,
ingestionFlushed: false,
},
{ status: 502 },
);
}
warning = `Port 4000 ingestion server is unreachable (${errMsg}); flushed Next.js buffer only.`;
}
const combinedFiles = Array.from(
@@ -57,6 +103,8 @@ export async function POST(_req: NextRequest) {
success: true,
flushedCount: localRes.count + remoteCount,
filesUpdated: combinedFiles,
ingestionFlushed,
...(warning ? { warning } : {}),
});
} catch (err: unknown) {
const message = err instanceof Error ? err.message : 'Manual flush failed';
+50
View File
@@ -0,0 +1,50 @@
'use client';
import { Button } from '@/components/ui/Button';
import { AlertTriangle, RotateCcw } from 'lucide-react';
import Link from 'next/link';
import { useEffect } from 'react';
export default function RootErrorBoundary({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
useEffect(() => {
console.error('Application error boundary:', error);
}, [error]);
return (
<main className="flex min-h-screen items-center justify-center bg-[var(--bg-main)] px-4 py-8 text-[var(--text)]">
<section className="w-full max-w-[440px] border border-[var(--border-strong)] bg-[var(--surface)] p-5">
<div className="mb-3 flex items-center gap-2 border-b border-[var(--border)] pb-3">
<AlertTriangle className="h-4 w-4 shrink-0 text-[var(--danger)]" aria-hidden="true" />
<h1 className="text-[13px] font-medium uppercase tracking-wider text-[var(--text)]">
Runtime error
</h1>
</div>
<p className="mb-3 text-[12px] leading-relaxed text-[var(--text-muted)]">
An unexpected rendering error occurred. You can retry the view or return to the overview page.
</p>
{error?.message && (
<div className="mb-4 border border-[var(--border)] bg-[var(--bg-main)] px-2.5 py-2 font-mono text-[11px] text-[var(--danger)] break-words">
{error.message}
</div>
)}
<div className="flex flex-wrap items-center justify-end gap-2 border-t border-[var(--border)] pt-3">
<Link href="/">
<Button type="button" size="xs" variant="ghost">
Return home
</Button>
</Link>
<Button type="button" size="xs" variant="primary" onClick={() => reset()}>
<RotateCcw className="h-3 w-3" aria-hidden="true" />
Try again
</Button>
</div>
</section>
</main>
);
}
+25 -6
View File
@@ -1,11 +1,31 @@
import { PwaLifecycle } from '@/components/pwa/PwaLifecycle';
import { ToastProvider } from '@/components/ui/Toast';
import type { Metadata } from 'next';
import type { Metadata, Viewport } from 'next';
import './globals.css';
export const viewport: Viewport = {
themeColor: '#0a0a0b',
width: 'device-width',
initialScale: 1,
};
export const metadata: Metadata = {
title: 'DataStack - Realtime Data Collection Platform',
description: 'Self-hosted universal realtime data collection engine with Socket.IO ingestion, Redis buffer, JWT client auth, CSV batch rotation, and /vcon/ admin panel.',
manifest: '/manifest.json',
appleWebApp: {
capable: true,
statusBarStyle: 'black-translucent',
title: 'DataStack',
},
icons: {
icon: [
{ url: '/icon.svg', type: 'image/svg+xml' },
{ url: '/icon-192.png', sizes: '192x192', type: 'image/png' },
{ url: '/icon-512.png', sizes: '512x512', type: 'image/png' },
],
apple: [{ url: '/apple-touch-icon.png', sizes: '180x180', type: 'image/png' }],
},
openGraph: {
title: 'DataStack - Realtime Data Collection Platform',
description: 'Self-hosted universal realtime data collection engine with Socket.IO ingestion, Redis buffer, JWT client auth, CSV batch rotation, and /vcon/ admin panel.',
@@ -21,12 +41,11 @@ export const metadata: Metadata = {
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" className="dark">
<head>
<link rel="manifest" href="/manifest.json" />
<meta name="theme-color" content="#0a0a0b" />
</head>
<body suppressHydrationWarning className="min-h-screen bg-[var(--bg-main)] text-[var(--text)] antialiased">
<ToastProvider>{children}</ToastProvider>
<ToastProvider>
{children}
<PwaLifecycle />
</ToastProvider>
</body>
</html>
);
+6 -5
View File
@@ -1,5 +1,5 @@
import { MainLayout } from '@/components/layout/MainLayout';
import { Button } from '@/components/ui/Button';
import { buttonClassNames } from '@/components/ui/Button';
import Link from 'next/link';
export default function NotFound() {
@@ -11,10 +11,11 @@ export default function NotFound() {
The requested page or route does not exist on DataStack.
</p>
<div className="flex items-center gap-2 pt-2">
<Link href="/">
<Button variant="secondary" size="sm">
Return Home
</Button>
<Link href="/" className={buttonClassNames({ variant: 'primary', size: 'sm' })}>
Return Home
</Link>
<Link href="/vcon/dashboard" className={buttonClassNames({ variant: 'secondary', size: 'sm' })}>
Open Console
</Link>
</div>
</div>
+17 -4
View File
@@ -23,7 +23,11 @@ export default function ClientsPage() {
const [now, setNow] = useState(() => Date.now());
const fetchClients = useCallback(async () => {
const response = await fetch('/api/clients');
const response = await fetch('/api/clients', { cache: 'no-store' });
if (response.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (!response.ok) throw new Error('Unable to load clients');
const data = await response.json();
setClients(setCached('vcon:clients', data.clients || []));
@@ -49,16 +53,19 @@ export default function ClientsPage() {
!query ||
client.clientId.toLowerCase().includes(query) ||
client.name.toLowerCase().includes(query);
const revoked = client.status === 'revoked';
const online =
client.status === 'active' &&
Boolean(client.lastActive) &&
now - new Date(client.lastActive!).getTime() < 60_000;
const pending = client.status === 'active' && !client.lastActive;
const offline = client.status === 'active' && !online && !pending;
const matchesStatus =
statusFilter === 'all' ||
(statusFilter === 'online' && online) ||
(statusFilter === 'offline' && !online && !pending) ||
(statusFilter === 'pending' && pending);
(statusFilter === 'offline' && offline) ||
(statusFilter === 'pending' && pending) ||
(statusFilter === 'revoked' && revoked);
return matchesSearch && matchesStatus;
});
}, [clients, now, search, statusFilter]);
@@ -72,7 +79,10 @@ export default function ClientsPage() {
const pendingCount = clients.filter(
(client) => client.status === 'active' && !client.lastActive
).length;
const offlineCount = clients.length - onlineCount - pendingCount;
const revokedCount = clients.filter(
(client) => client.status === 'revoked'
).length;
const offlineCount = clients.length - onlineCount - pendingCount - revokedCount;
return (
<PageShell
@@ -90,11 +100,13 @@ export default function ClientsPage() {
}
>
<StatStrip
className="md:grid-cols-5"
items={[
{ label: 'Total', value: clients.length },
{ label: 'Online', value: onlineCount, dot: 'ok' },
{ label: 'Offline', value: offlineCount, dot: 'off' },
{ label: 'Pending', value: pendingCount, dot: 'pending' },
{ label: 'Revoked', value: revokedCount, dot: 'off' },
]}
/>
@@ -109,6 +121,7 @@ export default function ClientsPage() {
online: onlineCount,
offline: offlineCount,
pending: pendingCount,
revoked: revokedCount,
}}
/>
<ClientsTable
+50
View File
@@ -0,0 +1,50 @@
'use client';
import { Button } from '@/components/ui/Button';
import { AlertTriangle, RotateCcw } from 'lucide-react';
import Link from 'next/link';
import { useEffect } from 'react';
export default function VconErrorBoundary({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
useEffect(() => {
console.error('Admin console error boundary:', error);
}, [error]);
return (
<div className="flex min-h-[60vh] items-center justify-center p-4 text-[var(--text)]">
<section className="w-full max-w-[440px] border border-[var(--border-strong)] bg-[var(--surface)] p-5">
<div className="mb-3 flex items-center gap-2 border-b border-[var(--border)] pb-3">
<AlertTriangle className="h-4 w-4 shrink-0 text-[var(--danger)]" aria-hidden="true" />
<h1 className="text-[13px] font-medium uppercase tracking-wider text-[var(--text)]">
Console panel error
</h1>
</div>
<p className="mb-3 text-[12px] leading-relaxed text-[var(--text-muted)]">
This console view encountered an unexpected failure. Retry rendering or navigate back to the dashboard.
</p>
{error?.message && (
<div className="mb-4 border border-[var(--border)] bg-[var(--bg-main)] px-2.5 py-2 font-mono text-[11px] text-[var(--danger)] break-words">
{error.message}
</div>
)}
<div className="flex flex-wrap items-center justify-end gap-2 border-t border-[var(--border)] pt-3">
<Link href="/vcon/dashboard">
<Button type="button" size="xs" variant="ghost">
Dashboard
</Button>
</Link>
<Button type="button" size="xs" variant="primary" onClick={() => reset()}>
<RotateCcw className="h-3 w-3" aria-hidden="true" />
Retry panel
</Button>
</div>
</section>
</div>
);
}
+6 -5
View File
@@ -6,12 +6,13 @@ export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';
export default async function LoginPage() {
if (process.env.DATABASE_URL) {
const isConfigured = await isAdminSetupComplete().catch(() => false);
if (!isConfigured) redirect('/vcon/setup');
}
const isConfigured = await isAdminSetupComplete().catch(() => false);
if (!isConfigured) redirect('/vcon/setup');
const showDemoHint = !process.env.DATABASE_URL && process.env.NODE_ENV !== 'production';
const showDemoHint =
!process.env.DATABASE_URL &&
process.env.NODE_ENV !== 'production' &&
process.env.DATASTACK_ALLOW_DEMO_ADMIN !== 'false';
return <AdminLogin showDemoHint={showDemoHint} />;
}
+96 -21
View File
@@ -11,15 +11,25 @@ import { useToast } from '@/components/ui/Toast';
import { getCached, setCached } from '@/lib/vconCache';
import { useEffect, useState } from 'react';
interface SdkClientItem {
clientId: string;
name: string;
status?: 'active' | 'revoked';
tokenPreview?: string;
token?: string;
}
export default function SdkGuidePage() {
const { success } = useToast();
const [clients, setClients] = useState<any[]>(() =>
getCached<any[]>('vcon:clients', [])
const { success, error } = useToast();
const [clients, setClients] = useState<SdkClientItem[]>(() =>
getCached<SdkClientItem[]>('vcon:clients', [])
);
const [selectedClientId, setSelectedClientId] = useState<string>(() => {
const cached = getCached<any[]>('vcon:clients', []);
return cached[0]?.clientId ?? '';
const cached = getCached<SdkClientItem[]>('vcon:clients', []);
return cached.find((c) => c.status !== 'revoked')?.clientId ?? cached[0]?.clientId ?? '';
});
const [selectedClientToken, setSelectedClientToken] = useState<string>('');
const [loadingToken, setLoadingToken] = useState<boolean>(false);
const [serverUrl, setServerUrl] = useState<string>('http://localhost:4000');
const [apiUrl, setApiUrl] = useState<string>('http://localhost:3000');
@@ -34,33 +44,81 @@ export default function SdkGuidePage() {
async function loadClients() {
try {
const res = await fetch('/api/clients');
const res = await fetch('/api/clients', { cache: 'no-store' });
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (res.ok) {
const data = await res.json();
const list = setCached('vcon:clients', data.clients || []);
const list = setCached<SdkClientItem[]>('vcon:clients', data.clients || []);
setClients(list);
if (list.length > 0) {
setSelectedClientId((prev) =>
list.some((c: any) => c.clientId === prev) ? prev : list[0].clientId
list.some((c) => c.clientId === prev)
? prev
: list.find((c) => c.status !== 'revoked')?.clientId ?? list[0].clientId
);
}
}
} catch {
// ignore
// Retain cached list on transient network error
}
}
loadClients();
void loadClients();
}, []);
useEffect(() => {
if (!selectedClientId) {
setSelectedClientToken('');
return;
}
let cancelled = false;
setLoadingToken(true);
async function fetchToken() {
try {
const res = await fetch(
`/api/clients/${encodeURIComponent(selectedClientId)}?includeToken=true`,
{ cache: 'no-store' }
);
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (res.ok) {
const data = await res.json();
if (!cancelled) {
setSelectedClientToken(String(data?.client?.token || ''));
}
} else if (!cancelled) {
setSelectedClientToken('');
}
} catch {
if (!cancelled) setSelectedClientToken('');
} finally {
if (!cancelled) setLoadingToken(false);
}
}
void fetchToken();
return () => {
cancelled = true;
};
}, [selectedClientId]);
const activeClient =
clients.find((c) => c.clientId === selectedClientId) || clients[0];
const sampleToken = activeClient?.token || 'YOUR_SIGNED_CLIENT_JWT_TOKEN';
const resolvedToken = selectedClientToken || activeClient?.token || '';
const sampleToken = resolvedToken || 'YOUR_SIGNED_CLIENT_JWT_TOKEN';
const configSnippet = `# config.datastack.py — Place in your Python project root
DATASTACK_CONFIG = {
"SERVER_URL": "${serverUrl}",
"API_URL": "${apiUrl}",
"CLIENT_TOKEN": "${sampleToken}",
"CLIENT_ID": "${activeClient?.clientId || ''}",
"AUTO_CONNECT": True,
"ENABLE_HTTP_FALLBACK": True,
"HEARTBEAT_INTERVAL_SEC": 5.0,
@@ -99,9 +157,21 @@ print("SDK Status:", client.status())
client.disconnect()`;
const copySnippet = (text: string, label: string) => {
navigator.clipboard.writeText(text);
success('Copied', `${label} copied`);
const copySnippet = async (text: string, label: string) => {
try {
if (!navigator?.clipboard?.writeText) {
throw new Error('Clipboard API unavailable in this browser context');
}
await navigator.clipboard.writeText(text);
success('Copied', `${label} copied`);
} catch (copyErr) {
error(
'Copy failed',
copyErr instanceof Error
? copyErr.message
: 'Unable to copy to clipboard. Copy manually from the code block.'
);
}
};
return (
@@ -109,12 +179,13 @@ client.disconnect()`;
title="Python SDK"
actions={
<>
{activeClient?.token && (
{resolvedToken && (
<Button
size="xs"
variant="secondary"
icon="Key"
onClick={() => copySnippet(activeClient.token, 'Client JWT token')}
disabled={loadingToken}
onClick={() => void copySnippet(resolvedToken, 'Client JWT token')}
>
Copy Token
</Button>
@@ -123,7 +194,7 @@ client.disconnect()`;
size="xs"
variant="secondary"
icon="FileCode"
onClick={() => copySnippet(configSnippet, 'config.datastack.py')}
onClick={() => void copySnippet(configSnippet, 'config.datastack.py')}
>
Copy Config
</Button>
@@ -131,7 +202,7 @@ client.disconnect()`;
size="xs"
variant="secondary"
icon="Copy"
onClick={() => copySnippet(pythonSnippet, 'Python SDK snippet')}
onClick={() => void copySnippet(pythonSnippet, 'Python SDK snippet')}
>
Copy Code
</Button>
@@ -161,8 +232,9 @@ client.disconnect()`;
size="xs"
variant="ghost"
icon="Copy"
aria-label="Copy pip install command"
onClick={() =>
copySnippet('pip install ./sdk/python/datastack', 'Pip command')
void copySnippet('pip install ./sdk/python/datastack', 'Pip command')
}
/>
</Card>
@@ -176,6 +248,7 @@ client.disconnect()`;
<div className="w-[190px]">
<Select
compact
aria-label="Select SDK client"
value={selectedClientId}
onChange={(e) => setSelectedClientId(e.target.value)}
>
@@ -191,7 +264,8 @@ client.disconnect()`;
size="xs"
variant="ghost"
icon="Copy"
onClick={() => copySnippet(configSnippet, 'config.datastack.py')}
aria-label="Copy config.datastack.py"
onClick={() => void copySnippet(configSnippet, 'config.datastack.py')}
/>
</div>
}
@@ -207,7 +281,8 @@ client.disconnect()`;
size="xs"
variant="ghost"
icon="Copy"
onClick={() => copySnippet(pythonSnippet, 'Python SDK snippet')}
aria-label="Copy Python SDK snippet"
onClick={() => void copySnippet(pythonSnippet, 'Python SDK snippet')}
/>
}
>
+310 -43
View File
@@ -5,20 +5,40 @@ import { Button } from '@/components/ui/Button';
import { CodeBlock } from '@/components/ui/CodeBlock';
import { Select, Textarea } from '@/components/ui/Input';
import { Panel } from '@/components/ui/Panel';
import { Pill } from '@/components/ui/Pill';
import { StatStrip } from '@/components/ui/StatStrip';
import { useToast } from '@/components/ui/Toast';
import { getCached, setCached } from '@/lib/vconCache';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import { io } from 'socket.io-client';
interface SimulatorClient {
clientId: string;
name: string;
status: 'active' | 'revoked';
}
type TransportMode = 'http' | 'websocket';
type DispatchStatus = 'Ready' | 'Accepted' | 'Rejected' | 'Error';
function getSocketOrigin(): string {
const isLocalDev = window.location.port === '3000';
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
const host = isLocalDev ? `${window.location.hostname}:4000` : window.location.host;
return `${protocol}://${host}`;
}
export default function SimulatorPage() {
const { success, error } = useToast();
const [clients, setClients] = useState<any[]>(() =>
getCached<any[]>('vcon:clients', [])
const { success, warning, error } = useToast();
const [clients, setClients] = useState<SimulatorClient[]>(() =>
getCached<SimulatorClient[]>('vcon:clients', [])
);
const [selectedClient, setSelectedClient] = useState<string>(() => {
const cached = getCached<any[]>('vcon:clients', []);
return cached[0]?.clientId ?? '';
const cached = getCached<SimulatorClient[]>('vcon:clients', []);
return cached.find((c) => c.status === 'active')?.clientId ?? cached[0]?.clientId ?? '';
});
const [transport, setTransport] = useState<TransportMode>('http');
const [payloadText, setPayloadText] = useState<string>(
JSON.stringify(
{
@@ -33,30 +53,136 @@ export default function SimulatorPage() {
)
);
const [sending, setSending] = useState(false);
const [lastResponse, setLastResponse] = useState<any>(null);
const [dispatchStatus, setDispatchStatus] = useState<DispatchStatus>('Ready');
const [lastResponse, setLastResponse] = useState<Record<string, unknown> | null>(null);
useEffect(() => {
async function loadClients() {
try {
const res = await fetch('/api/clients');
const res = await fetch('/api/clients', { cache: 'no-store' });
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (res.ok) {
const data = await res.json();
const list = setCached('vcon:clients', data.clients || []);
const list = setCached<SimulatorClient[]>('vcon:clients', data.clients || []);
setClients(list);
if (list.length > 0)
setSelectedClient((prev) => prev || list[0].clientId);
setSelectedClient((prev) => {
if (prev && list.some((c) => c.clientId === prev)) return prev;
return list.find((c) => c.status === 'active')?.clientId ?? list[0]?.clientId ?? '';
});
}
} catch {
// ignore
// Ignore transient failure and retain cached list
}
}
loadClients();
void loadClients();
}, []);
const fetchClientJwt = async (clientId: string): Promise<string> => {
const res = await fetch(`/api/clients/${encodeURIComponent(clientId)}?includeToken=true`, {
cache: 'no-store',
});
if (res.status === 401) {
window.location.assign('/vcon/login');
throw new Error('Authentication required');
}
const data = await res.json();
if (!res.ok || !data?.client?.token) {
throw new Error(data?.error || 'Unable to retrieve client JWT token');
}
return String(data.client.token);
};
const emitSocketEvents = async (
clientId: string,
items: Array<Record<string, unknown>>
): Promise<{ accepted: number; rejected: number; responses: Record<string, unknown>[] }> => {
const token = await fetchClientJwt(clientId);
const socketUrl = getSocketOrigin();
return new Promise((resolve, reject) => {
const socket = io(socketUrl, {
auth: { token },
transports: ['websocket', 'polling'],
reconnection: false,
timeout: 4000,
});
const timer = window.setTimeout(() => {
socket.disconnect();
reject(new Error('WebSocket handshake timed out (is Port 4000 ingestion server running?)'));
}, 4500);
socket.on('connect_error', (connectErr) => {
window.clearTimeout(timer);
socket.disconnect();
reject(new Error(connectErr.message || 'WebSocket authentication failed'));
});
socket.on('connect', () => {
const responses: Record<string, unknown>[] = [];
let accepted = 0;
let rejected = 0;
let settled = 0;
const finishIfDone = () => {
if (settled >= items.length) {
window.clearTimeout(timer);
socket.disconnect();
resolve({ accepted, rejected, responses });
}
};
const onAck = (ack: Record<string, unknown>) => {
responses.push(ack);
if (ack?.status === 'ok' || ack?.status === 'duplicate') {
accepted++;
} else {
rejected++;
}
settled++;
finishIfDone();
};
const onErrorEvent = (errPayload: Record<string, unknown>) => {
responses.push(errPayload);
rejected++;
settled++;
finishIfDone();
};
socket.on('ack', onAck);
socket.on('error_event', onErrorEvent);
items.forEach((data, index) => {
const eventId = `sim_ws_${Date.now()}_${index}`;
socket.emit('telemetry_event', {
clientId,
eventId,
timestamp: new Date().toISOString(),
data,
});
});
});
});
};
const handleSendSingle = async () => {
let parsedData: any;
if (!selectedClient) {
error('No target client', 'Register a client in /vcon/clients first.');
return;
}
let parsedData: Record<string, unknown>;
try {
parsedData = JSON.parse(payloadText);
const raw = JSON.parse(payloadText);
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
error('Invalid payload', 'Payload must be a JSON object.');
return;
}
parsedData = raw as Record<string, unknown>;
} catch {
error('Invalid JSON', 'Fix payload syntax');
return;
@@ -64,54 +190,145 @@ export default function SimulatorPage() {
setSending(true);
try {
if (transport === 'websocket') {
const result = await emitSocketEvents(selectedClient, [parsedData]);
const first = result.responses[0] || {};
setLastResponse({ transport: 'websocket', ...first });
if (result.accepted > 0) {
setDispatchStatus('Accepted');
success('WebSocket Event Accepted', `${String(first.eventId || selectedClient)} acknowledged`);
} else {
setDispatchStatus('Rejected');
error('WebSocket Event Rejected', String(first.message || first.error || 'Rejected by Port 4000'));
}
return;
}
const res = await fetch('/api/events', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
clientId: selectedClient || 'py_telemetry_agent_01',
clientId: selectedClient,
data: parsedData,
}),
});
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
const data = await res.json();
setLastResponse(data);
setLastResponse({ transport: 'http', status: res.status, ...data });
if (res.ok) {
success('Event Accepted', `${data.event.eventId} buffered`);
setDispatchStatus('Accepted');
success('Event Accepted', `${data.event?.eventId || selectedClient} buffered`);
} else {
setDispatchStatus('Rejected');
error('Event Rejected', data.error || 'Server rejected');
}
} catch (err: any) {
error('Error', err.message);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : 'Dispatch error';
setDispatchStatus('Error');
setLastResponse({ transport, error: message });
error('Dispatch failed', message);
} finally {
setSending(false);
}
};
const handleSendBatch = async (count = 10) => {
if (!selectedClient) {
error('No target client', 'Register a client in /vcon/clients first.');
return;
}
const items = Array.from({ length: count }, (_, i) => ({
batch_index: i + 1,
sensor: `sensor_${(i % 3) + 1}`,
reading: +(Math.random() * 100).toFixed(2),
timestamp: new Date().toISOString(),
}));
setSending(true);
try {
if (transport === 'websocket') {
const result = await emitSocketEvents(selectedClient, items);
setLastResponse({
transport: 'websocket',
batchSent: count,
accepted: result.accepted,
rejected: result.rejected,
sample: result.responses.slice(0, 3),
});
if (result.accepted === 0) {
setDispatchStatus('Rejected');
error('WebSocket Batch Rejected', `0/${count} events ingested`);
} else if (result.accepted < count) {
setDispatchStatus('Rejected');
warning('WebSocket Batch Partial', `${result.accepted}/${count} ingested (${result.rejected} rejected)`);
} else {
setDispatchStatus('Accepted');
success('WebSocket Batch Streamed', `${result.accepted}/${count} ingested`);
}
return;
}
let accepted = 0;
for (let i = 0; i < count; i++) {
let rejected = 0;
const errors: string[] = [];
for (const item of items) {
const res = await fetch('/api/events', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
clientId: selectedClient || 'py_telemetry_agent_01',
data: {
batch_index: i + 1,
sensor: `sensor_${(i % 3) + 1}`,
reading: +(Math.random() * 100).toFixed(2),
timestamp: new Date().toISOString(),
},
clientId: selectedClient,
data: item,
}),
});
if (res.ok) accepted++;
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (res.ok) {
accepted++;
} else {
rejected++;
try {
const errJson = await res.json();
if (errJson?.error && errors.length < 3) {
errors.push(String(errJson.error));
}
} catch {
// ignore non-json error body
}
}
}
success('Batch Streamed', `${accepted}/${count} ingested`);
setLastResponse({ success: true, batchSent: count, accepted });
} catch (err: any) {
error('Batch Error', err.message);
setLastResponse({
transport: 'http',
batchSent: count,
accepted,
rejected,
...(errors.length > 0 ? { errors } : {}),
});
if (accepted === 0) {
setDispatchStatus('Rejected');
error('Batch Rejected', `0/${count} ingested${errors[0] ? ` (${errors[0]})` : ''}`);
} else if (accepted < count) {
setDispatchStatus('Rejected');
warning('Batch Partial', `${accepted}/${count} ingested (${rejected} rejected)`);
} else {
setDispatchStatus('Accepted');
success('Batch Streamed', `${accepted}/${count} ingested`);
}
} catch (err: unknown) {
const message = err instanceof Error ? err.message : 'Batch dispatch error';
setDispatchStatus('Error');
setLastResponse({ transport, error: message });
error('Batch Error', message);
} finally {
setSending(false);
}
@@ -126,7 +343,8 @@ export default function SimulatorPage() {
size="xs"
variant="secondary"
loading={sending}
onClick={() => handleSendBatch(10)}
disabled={sending || !selectedClient}
onClick={() => void handleSendBatch(10)}
>
Burst 10
</Button>
@@ -135,44 +353,93 @@ export default function SimulatorPage() {
variant="primary"
icon="Send"
loading={sending}
onClick={handleSendSingle}
disabled={sending || !selectedClient}
onClick={() => void handleSendSingle()}
>
Send Event
</Button>
</>
}
>
{clients.length === 0 && (
<div
role="alert"
className="mb-3 flex flex-wrap items-center justify-between gap-2 border border-[var(--warning)] bg-[var(--warning-soft)] px-3 py-2 text-[11.5px] text-[var(--text)]"
>
<span>No registered SDK clients found. Register a client before dispatching events.</span>
<Link href="/vcon/clients" className="font-medium text-[var(--accent)] underline">
Go to Clients →
</Link>
</div>
)}
<StatStrip
items={[
{
label: 'Target client',
value: selectedClient || 'py_telemetry_agent_01',
dot: 'ok',
value: selectedClient || 'None selected',
dot: selectedClient ? 'ok' : 'off',
},
{
label: 'Ingest transport',
value: transport === 'http' ? 'POST /api/events' : 'WS Port 4000',
},
{ label: 'Ingest route', value: 'POST /api/events' },
{ label: 'Payload format', value: 'JSON' },
{
label: 'Last dispatch',
value: lastResponse ? 'Accepted' : 'Ready',
value: dispatchStatus,
dot:
dispatchStatus === 'Accepted'
? 'ok'
: dispatchStatus === 'Ready'
? undefined
: 'off',
},
]}
/>
<div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
<Panel title="Payload" bodyClassName="flex flex-col gap-2.5 p-3">
<div className="flex flex-wrap items-center justify-between gap-2">
<span className="text-[10px] uppercase tracking-wider text-[var(--text-faint)]">
Transport
</span>
<div className="flex gap-1" role="group" aria-label="Ingest transport">
<Pill
active={transport === 'http'}
onClick={() => setTransport('http')}
>
HTTP /api/events
</Pill>
<Pill
active={transport === 'websocket'}
onClick={() => setTransport('websocket')}
>
WebSocket Port 4000
</Pill>
</div>
</div>
<Select
compact
label="Target client"
value={selectedClient}
disabled={clients.length === 0}
onChange={(e) => setSelectedClient(e.target.value)}
>
{clients.map((c) => (
<option key={c.clientId} value={c.clientId}>
{c.clientId}
</option>
))}
{clients.length === 0 ? (
<option value="">No registered clients</option>
) : (
clients.map((c) => (
<option key={c.clientId} value={c.clientId}>
{c.clientId} ({c.status})
</option>
))
)}
</Select>
<Textarea
label="Event JSON"
rows={7}
value={payloadText}
onChange={(e) => setPayloadText(e.target.value)}