feat(parcel-sync): server-side eTerra session + auto-connect on UAT typing
- Add session-store.ts: global singleton for shared eTerra session state with job tracking (registerJob/unregisterJob/getRunningJobs) - Add GET/POST /api/eterra/session: connect/disconnect with job-running guard - Export routes: credential fallback chain (body > session > env vars), register/unregister active jobs for disconnect protection - Login route: also creates server-side session - ConnectionPill: session-aware display with job count, no credential form - Auto-connect: triggers on first UAT keystroke via autoConnectAttempted ref - Session polling: all clients poll GET /api/eterra/session every 30s - Multi-client: any browser sees shared connection state
This commit is contained in:
@@ -8,6 +8,11 @@ import {
|
||||
clearProgress,
|
||||
setProgress,
|
||||
} from "@/modules/parcel-sync/services/progress-store";
|
||||
import {
|
||||
getSessionCredentials,
|
||||
registerJob,
|
||||
unregisterJob,
|
||||
} from "@/modules/parcel-sync/services/session-store";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
@@ -21,8 +26,14 @@ type ExportLayerRequest = {
|
||||
};
|
||||
|
||||
const validate = (body: ExportLayerRequest) => {
|
||||
const username = String(body.username ?? "").trim();
|
||||
const password = String(body.password ?? "").trim();
|
||||
// Priority: request body > session store > env vars
|
||||
const session = getSessionCredentials();
|
||||
const username = String(
|
||||
body.username || session?.username || process.env.ETERRA_USERNAME || "",
|
||||
).trim();
|
||||
const password = String(
|
||||
body.password || session?.password || process.env.ETERRA_PASSWORD || "",
|
||||
).trim();
|
||||
const siruta = String(body.siruta ?? "").trim();
|
||||
const layerId = String(body.layerId ?? "").trim();
|
||||
const jobId = body.jobId ? String(body.jobId).trim() : undefined;
|
||||
@@ -122,6 +133,7 @@ export async function POST(req: Request) {
|
||||
const body = (await req.json()) as ExportLayerRequest;
|
||||
const validated = validate(body);
|
||||
jobId = validated.jobId;
|
||||
if (jobId) registerJob(jobId);
|
||||
pushProgress();
|
||||
|
||||
const layer = findLayerById(validated.layerId);
|
||||
@@ -245,6 +257,7 @@ export async function POST(req: Request) {
|
||||
pushProgress();
|
||||
scheduleClear(jobId);
|
||||
|
||||
if (jobId) unregisterJob(jobId);
|
||||
const filename = `eterra_uat_${validated.siruta}_${layer.name}.gpkg`;
|
||||
return new Response(new Uint8Array(gpkg), {
|
||||
headers: {
|
||||
@@ -260,6 +273,7 @@ export async function POST(req: Request) {
|
||||
note = undefined;
|
||||
pushProgress();
|
||||
scheduleClear(jobId);
|
||||
if (jobId) unregisterJob(jobId);
|
||||
const lower = errMessage.toLowerCase();
|
||||
const statusCode =
|
||||
lower.includes("login failed") || lower.includes("session") ? 401 : 400;
|
||||
|
||||
Reference in New Issue
Block a user