From 4befa679e68b524743a3ac12e72c3bfcec6e8222 Mon Sep 17 00:00:00 2001 From: Manel-Romero Date: Mon, 16 Nov 2026 11:52:18 +0100 Subject: [PATCH 1/6] chore: start cloud logs integration (#677) From c632d322ce111c2ec642f4ff54c8c50b1c31d941 Mon Sep 17 00:00:00 2001 From: Javier Ribal del Rio Date: Sun, 15 Nov 2026 09:32:33 +0100 Subject: [PATCH 2/6] fix(electron-app): update contact email --- electron-app/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/electron-app/package.json b/electron-app/package.json index 2549992da..4630c1d7c 100644 --- a/electron-app/package.json +++ b/electron-app/package.json @@ -6,7 +6,7 @@ "type": "module", "author": { "name": "Hyperloop UPV", - "email": "partners@hyperloopupv.com" + "email": "software@hyperloopupv.com" }, "contributors": [ { From a99132a8c18b832efdb1110375ff8ee9c5f3a7e2 Mon Sep 17 00:00:00 2001 From: Javier Ribal del Rio Date: Sun, 15 Nov 2026 09:42:53 +0100 Subject: [PATCH 3/6] fix(testing-view): remove unused blcu electron api from env --- frontend/testing-view/src/vite-end.d.ts | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/frontend/testing-view/src/vite-end.d.ts b/frontend/testing-view/src/vite-end.d.ts index 63e4a4c8f..c92d01c51 100644 --- a/frontend/testing-view/src/vite-end.d.ts +++ b/frontend/testing-view/src/vite-end.d.ts @@ -9,21 +9,6 @@ interface ElectronAPI { selectFolder: () => Promise; openFolder: (path: string) => Promise; restartBackend: () => Promise; - blcuSelectFile?: () => Promise; - blcuUpload?: (request: { - host: string; - port: number; - remote_filename: string; - local_path: string; - }) => Promise<{ ok: boolean; message: string }>; - blcuDownload?: (request: { - host: string; - port: number; - remote_filename: string; - local_path: string; - }) => Promise<{ ok: boolean; message: string }>; - blcuHealth?: () => Promise<{ status: string }>; - blcuLogs?: (tail?: number) => Promise<{ lines: string[]; line_count: number }>; } declare global { From 027b2708a9c968b111c0894008ddf8cc34117747 Mon Sep 17 00:00:00 2001 From: Javier Ribal del Rio Date: Sun, 15 Nov 2026 09:47:36 +0100 Subject: [PATCH 4/6] feat(electron-app): cloud logs uploder and downloader --- electron-app/README.md | 56 ++++ electron-app/package.json | 4 +- electron-app/preload.js | 17 ++ electron-app/src/cloudLogs/README.md | 112 ++++++++ .../src/cloudLogs/__tests__/storage.test.js | 211 +++++++++++++++ electron-app/src/cloudLogs/archive.js | 129 +++++++++ electron-app/src/cloudLogs/client.js | 50 ++++ electron-app/src/cloudLogs/credentials.js | 89 +++++++ electron-app/src/cloudLogs/handlers.js | 246 ++++++++++++++++++ electron-app/src/cloudLogs/sessions.js | 105 ++++++++ electron-app/src/ipc/handlers.js | 57 ++-- .../frontend-kit/kit/src/core/cloudLogs.ts | 58 +++++ frontend/frontend-kit/kit/src/core/index.ts | 1 + pnpm-lock.yaml | 6 + 14 files changed, 1109 insertions(+), 32 deletions(-) create mode 100644 electron-app/src/cloudLogs/README.md create mode 100644 electron-app/src/cloudLogs/__tests__/storage.test.js create mode 100644 electron-app/src/cloudLogs/archive.js create mode 100644 electron-app/src/cloudLogs/client.js create mode 100644 electron-app/src/cloudLogs/credentials.js create mode 100644 electron-app/src/cloudLogs/handlers.js create mode 100644 electron-app/src/cloudLogs/sessions.js create mode 100644 frontend/frontend-kit/kit/src/core/cloudLogs.ts diff --git a/electron-app/README.md b/electron-app/README.md index a2b9d108e..2065c00a2 100644 --- a/electron-app/README.md +++ b/electron-app/README.md @@ -108,6 +108,62 @@ sudo ifconfig lo0 alias 127.0.0.9 up - **Mode Selector**: html5 file to chose the mode of the app: testing, competition or flashing. Placed at `renderer/mode-selector` +## Cloud Logs + +Cloud Logs is a desktop capability shared by Logging View and Testing View. The +renderer calls the narrow `window.electronAPI.cloudLogs` interface exposed from +`preload.js`; it does not access the filesystem, ZIP library, credentials, or +remote API directly. The IPC handlers in `src/cloudLogs/handlers.js` validate +that requests come from the local Logging or Testing window and keep local paths +inside the Electron main process. + +The local folder format is `/logger///`. +Cloud uploads operate on one recording at a time. A recording ZIP contains the +root `logger_settings.json` and the supported CSV subfolders (`data`, `order`, +`protections`/`protection`, and `state`). Launcher traces and unrelated files +are excluded. The date attached to a cloud record comes from the recording's +`logger_settings.json`, not from the time it was uploaded. Users must confirm +that recording has stopped; Electron checks that its files did not change while +it compressed the ZIP and reports compression progress to the requesting view. + +Downloaded archives are checked and extracted into +`/logger/cloud-logs//`. Extraction uses a staging +directory and rejects unsafe paths, links, duplicate entries, unsupported files, +encrypted ZIPs, and archives over the configured file and byte limits. Logging +View opens a downloaded recording through the same loader it uses for local +folders, so analysis works offline after download. `cloud-logs/` is created on +first use, even when the Go backend is not running. + +The shared typed renderer contract is exported as `CloudLogsAPI` from +`@hyperloopupv/frontend-kit/core`. Testing View already has that type on its +Electron bridge, so it can call the same service without implementing another +network or filesystem client. The React implementation currently lives in +`frontend/logging-view/src/components/cloudLogs/CloudLogsDialog.tsx`. + +Credentials are entered in the Cloud Logs dialog and managed by Electron in +`src/cloudLogs/credentials.js`. The user name and encrypted key are stored in +`/cloud-logs.json` when OS secure storage is available. If +secure storage is unavailable, Electron keeps the key in memory until exit. The +key is never sent back to the renderer; the renderer only receives the user, +whether a key is configured, and whether storage is persistent. + +### Connecting the Cloud Logs API + +The remote API is intentionally not assumed. Its only integration boundary is +`src/cloudLogs/client.js`; the four functions there currently reject with a +clear not-connected error. Implement `listLogs`, `autocompleteTags`, `uploadLog`, +and `downloadLog` there after confirming endpoint URLs, authentication format, +pagination, multipart field names, and response shapes with the API owner. +Normalize returned records to `{ id, user, notes, tags, date }`, stream ZIPs to +and from disk, enforce download limits while streaming, and sanitize errors so +they never include credentials. The request and security details are recorded in +`src/cloudLogs/README.md` next to the service implementation. + +While the client remains a placeholder, local session discovery, ZIP creation, +credential management, download validation, and the UI are usable, but browsing +cloud records, tag suggestions, uploads, and downloads report that the API is +not connected. There are no mocked cloud results or false success states. + ## Dependencies - `electron` - Desktop application framework diff --git a/electron-app/package.json b/electron-app/package.json index 4630c1d7c..09e5f17d4 100644 --- a/electron-app/package.json +++ b/electron-app/package.json @@ -66,9 +66,11 @@ "dependencies": { "@iarna/toml": "^2.2.5", "ansi-to-html": "^0.7.2", + "archiver": "5.3.2", "electron-store": "^11.0.2", "electron-updater": "^6.7.3", - "picocolors": "^1.1.1" + "picocolors": "^1.1.1", + "yauzl": "2.10.0" }, "devDependencies": { "@vitest/coverage-v8": "^4.0.18", diff --git a/electron-app/preload.js b/electron-app/preload.js index a4d5debf6..d4d202c87 100644 --- a/electron-app/preload.js +++ b/electron-app/preload.js @@ -24,6 +24,23 @@ const { contextBridge, ipcRenderer } = require("electron"); * await window.electronAPI.saveConfig({ database: { host: "localhost" } }); */ contextBridge.exposeInMainWorld("electronAPI", { + cloudLogs: { + getCredentialsStatus: () => ipcRenderer.invoke("cloud-logs:credentials-status"), + setCredentials: (input) => ipcRenderer.invoke("cloud-logs:credentials-save", input), + clearCredentials: () => ipcRenderer.invoke("cloud-logs:credentials-clear"), + getLocalSessions: () => ipcRenderer.invoke("cloud-logs:local-sessions"), + selectSession: () => ipcRenderer.invoke("cloud-logs:select-session"), + list: (cursor) => ipcRenderer.invoke("cloud-logs:list", cursor), + autocompleteTags: (query) => ipcRenderer.invoke("cloud-logs:tags", query), + upload: (input) => ipcRenderer.invoke("cloud-logs:upload", input), + download: (input) => ipcRenderer.invoke("cloud-logs:download", input), + getDownloads: () => ipcRenderer.invoke("cloud-logs:downloads"), + onProgress: (callback) => { + const listener = (_event, progress) => callback(progress); + ipcRenderer.on("cloud-logs:progress", listener); + return () => ipcRenderer.removeListener("cloud-logs:progress", listener); + }, + }, // Get the currently loaded view name getCurrentView: () => ipcRenderer.invoke("get-current-view"), // Switch to a different view by name, optionally passing query params diff --git a/electron-app/src/cloudLogs/README.md b/electron-app/src/cloudLogs/README.md new file mode 100644 index 000000000..6326b1d9f --- /dev/null +++ b/electron-app/src/cloudLogs/README.md @@ -0,0 +1,112 @@ +# Cloud Logs integration + +Desktop service shared by Logging View and Testing View via +`window.electronAPI.cloudLogs`. The typed contract is exported from +`@hyperloopupv/frontend-kit/core` (`CloudLogsAPI`). React never imports Node code. + +## File map + +| File | Responsibility | Change it when… | +| --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | +| `client.js` | The remote HTTP adapter. Defines list, tag suggestions, upload, and ZIP download functions; currently these are explicit not-connected placeholders. | Implementing API endpoints, authentication, pagination, request fields, or response mapping. | +| `credentials.js` | Reads, writes, encrypts, and clears the Cloud Logs user and API key. Only credential status is returned to the renderer. | Changing credential persistence, secure-storage policy, or validation. | +| `sessions.js` | Resolves the configured `logger/` root, creates `cloud-logs/`, scans local launch folders, and validates a recording using its `logger_settings.json`. | Changing how backend recordings are laid out or what qualifies as a session. | +| `archive.js` | Zips every regular file inside one recording and safely extracts downloaded archives. Reports compression and extraction progress. | Changing archive layout, ZIP safety checks, limits, or progress. | +| `handlers.js` | Connects renderer IPC calls to credentials, session discovery, archive operations, and the remote adapter. Checks the calling window, owns local path grants, scopes progress, and removes temporary files. | Adding renderer operations, changing authorization, or coordinating uploads and downloads. | +| `__tests__/storage.test.js` | Tests local session discovery, ZIP round trips and safety, arbitrary logger subfolders, and credential storage using temporary files. It does not call the remote API. | Adding regression coverage for session storage, ZIP handling, or credentials. | +| `README.md` | This module guide and API handoff contract. | Updating module behavior or integration instructions. | + +Related bridge files live outside this directory: `electron-app/preload.js` +exposes `window.electronAPI.cloudLogs`; `frontend/frontend-kit/kit/src/core/cloudLogs.ts` +defines the shared TypeScript contract; and +`frontend/logging-view/src/components/cloudLogs/CloudLogsDialog.tsx` implements +the Logging View dialog. Testing View can use the same preload bridge and types. + +## Remote API: teammate handoff + +**Only `client.js` is a placeholder.** Implement its four async functions: + +| Function | Input | Result | +| ------------------ | ------------------------------------------- | ---------------------------------------------- | +| `listLogs` | credentials, optional cursor | `{ items: CloudLog[], nextCursor?: string }` | +| `autocompleteTags` | credentials, query (including empty string) | `string[]` | +| `uploadLog` | credentials, archivePath, metadata | confirmed `CloudLog` | +| `downloadLog` | credentials, id, destination, maxBytes | resolves after ZIP is fully written and closed | + +`CloudLog` has `{ id, user, notes, tags: string[], date }`. Normalize the remote +API's response to this contract; for example map a `description` field to `notes`. +The log date is the original `logger_settings.json.date`, not the upload date. +It may have the backend's `YYYY-MM-DDTHH-mm-ss` format; document any conversion +required by the remote API without inventing a timezone for these local dates. + +Supply the endpoint URLs and authentication header/form fields according to the +real API contract; none are assumed here. Validate responses, bound request time, +and return useful sanitized errors (no API key, headers or raw request dumps). +Stream files instead of loading entire ZIPs into RAM. Enforce `maxBytes` while +downloading, even without Content-Length. Never forward authentication headers +to unrelated download/redirect origins. Do not blindly retry uploads unless the +API supports idempotency. + +The stubs deliberately reject with an explicit "API is not connected" error. +There are no fake tags, cloud records or successful uploads. With configured +credentials, pressing upload really compresses the session and reports progress, +then reaches the upload stub. Temporary ZIPs are removed even on failure. + +## Local service (implemented) + +- Local discovery: `/logger///`. +- Native folder selection also permits recordings outside that root. The main + process gives the renderer an opaque token scoped to its window, not a generic + "upload any path" API. +- One recording per ZIP. `logger_settings.json` stays at the ZIP root, and every + regular file inside that recording is included, even in unrecognized or future + subfolders such as `orders/`, `protections/`, and `other/`. The parent app + launch folder is never archived. The local `cloud-log.json` download marker is + excluded. Upload only stopped recordings; compression rejects files whose + size/mtime or membership changes during archiving. +- Upload ZIPs are created as `session.zip` inside an OS temporary directory named + `cloud-logs-upload-*` (under Node's `os.tmpdir()`). Electron deletes that + directory after the upload completes or fails; the ZIP is not retained. +- `logger/cloud-logs/` is created on first use, also with the Go backend stopped. + Paths resolve against the same backend working directory and `logging_path`. + `others/` remains inside each app launch, not beside the launch folders. +- Downloads extract into a private `.download-*` staging directory. After + validation they are renamed into `cloud-logs//`. Hashing the + ID avoids platform-invalid names and collisions from sanitizing IDs. +- The downloaded ZIP is temporarily named `session.zip` inside that staging + directory. After extraction succeeds or fails, Electron removes the staging + directory and ZIP; only the validated extracted session is kept. +- `cloud-log.json` stores the original ID and date beside the extracted session. + Repeated downloads reuse a valid local copy. Staging files are removed on error; + incomplete directories are excluded from the offline library. +- Extraction rejects traversal, symlinks, encrypted archives, duplicate file + entries and oversized archives. It accepts arbitrary regular files in a + downloaded recording so future logger subfolders remain usable. Limits: 4 GiB + and 10,000 entries. +- Progress is scoped by requestId and sent only to the requesting window. + Compression and extraction report file progress; network phases are + indeterminate. One transfer at a time across the desktop app. The dialog stays + open during transfers; closing the application still interrupts them. + +## Credentials + +`/cloud-logs.json` holds the user and encrypted API key. With +no secure OS storage (including Linux `basic_text`/`unknown`), only the user is +persisted; the key stays in process memory. No key is returned to React. Blank +key on save preserves the current key; removing credentials requires the +explicit Remove action. Credentials updates are serialized across windows. + +## UI and future consumers + +Logging View's sidebar opens Library / Upload / Credentials. Tags are fetched +with a debounced API search. Recording date is read-only and comes from the +selected recording. Downloads open through the existing session-folder loader, +so CSV parsing, charts and recent sessions keep the same behavior. In a browser +without Electron the dialog explains that desktop integration is required. + +Testing View can call the same bridge; its global interface already includes +`cloudLogs`. No backend lifecycle changes are required for cloud operations. + +Run `pnpm --dir electron-app test -- src/cloudLogs/__tests__` and the Logging +View build/lint. The tests use real temporary files and ZIPs, mocked credentials +storage and no remote network. diff --git a/electron-app/src/cloudLogs/__tests__/storage.test.js b/electron-app/src/cloudLogs/__tests__/storage.test.js new file mode 100644 index 000000000..eb9707079 --- /dev/null +++ b/electron-app/src/cloudLogs/__tests__/storage.test.js @@ -0,0 +1,211 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { + mkdtemp, + mkdir, + readFile, + writeFile, + rm, + symlink, +} from "node:fs/promises"; +import { createWriteStream } from "node:fs"; +import { pipeline } from "node:stream/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import archiver from "archiver"; + +const env = vi.hoisted(() => ({ root: "", secure: true, locked: false })); +vi.mock("electron", () => ({ + app: { getPath: () => env.root }, + safeStorage: { + isEncryptionAvailable: () => env.secure, + getSelectedStorageBackend: () => + env.secure ? "gnome_libsecret" : "basic_text", + encryptString: (value) => Buffer.from(`encrypted:${value}`), + decryptString: (value) => { + if (env.locked) throw new Error("locked"); + return value.toString().slice(10); + }, + }, +})); +vi.mock("../../utils/paths.js", () => ({ + getBackendWorkingDir: () => env.root, +})); +vi.mock("../../config/configInstance.js", () => ({ + readConfig: async () => ({ logging: { logging_path: "." } }), +})); + +import { compressSession, extractSession } from "../archive.js"; +import { + findLocalSessions, + getCloudRoot, + inspectSession, +} from "../sessions.js"; +import { + clearCredentials, + getCredentials, + getCredentialsStatus, + setCredentials, +} from "../credentials.js"; + +let session; +const settings = { + date: "2026-11-15T12-30-00", + time_unit: "us", + adj_commit_hash: "abc123", +}; +beforeEach(async () => { + env.root = await mkdtemp(path.join(tmpdir(), "cloud-logs-test-")); + env.secure = true; + env.locked = false; + session = path.join(env.root, "logger", "2026-11-15T12-00-00", settings.date); + await mkdir(path.join(session, "data", "BCU"), { recursive: true }); + await mkdir(path.join(session, "protections")); + await writeFile( + path.join(session, "logger_settings.json"), + JSON.stringify(settings), + ); + await writeFile( + path.join(session, "data", "BCU", "speed.csv"), + "0,BCU,backend,4\n1000,BCU,backend,5\n", + ); + await writeFile( + path.join(session, "protections", "BCU.csv"), + "0,BCU,backend,FAULT\n", + ); +}); +afterEach(async () => { + await clearCredentials(); + await rm(env.root, { recursive: true, force: true }); +}); + +async function zipEntries(entries, name = "input.zip") { + const dest = path.join(env.root, name); + const archive = archiver("zip"); + const output = pipeline(archive, createWriteStream(dest)); + for (const [file, content] of entries) + archive.append(content, { name: file }); + await Promise.all([archive.finalize(), output]); + return dest; +} + +describe("session archives", () => { + it("round-trips every session file, including unfamiliar subfolders", async () => { + await writeFile(path.join(session, "config.toml"), 'api_key="private"'); + await mkdir(path.join(session, "other", "metadata"), { recursive: true }); + await writeFile( + path.join(session, "other", "metadata", "notes.txt"), + "reserved logger data", + ); + const zip = path.join(env.root, "session.zip"); + const progress = vi.fn(); + const original = await compressSession(session, zip, progress); + const extracted = path.join(env.root, "extracted"); + const result = await extractSession(zip, extracted, vi.fn()); + expect(result.date).toBe(settings.date); + expect(result.fileCount).toBe(5); + expect(result.bytes).toBe(original.bytes); + expect(progress).toHaveBeenCalledWith(5, 5); + expect( + await readFile(path.join(extracted, "data/BCU/speed.csv"), "utf8"), + ).toContain("1000,BCU,backend,5"); + expect( + await readFile(path.join(extracted, "other/metadata/notes.txt"), "utf8"), + ).toBe("reserved logger data"); + expect( + await readFile(path.join(extracted, "config.toml"), "utf8"), + ).toContain("private"); + }); + it("discovers recordings underneath launches, excluding cloud downloads", async () => { + const cloud = await getCloudRoot(); + await mkdir(path.join(cloud, "cached")); + const sessions = await findLocalSessions(); + expect(sessions).toHaveLength(1); + expect(sessions[0].date).toBe(settings.date); + await expect(inspectSession(path.dirname(session))).rejects.toThrow( + "Select a recording", + ); + }); + it("refuses symbolic links in a local recording", async () => { + await symlink( + path.join(session, "logger_settings.json"), + path.join(session, "data", "link"), + ); + await expect(inspectSession(session)).rejects.toThrow("symbolic links"); + }); + it("refuses a recording that changes while compressing", async () => { + let changed = false; + const { appendFileSync } = await import("node:fs"); + await expect( + compressSession(session, path.join(env.root, "changing.zip"), () => { + if (!changed) { + changed = true; + appendFileSync( + path.join(session, "data/BCU/speed.csv"), + "2000,BCU,backend,6\n", + ); + } + }), + ).rejects.toThrow("changed during compression"); + }); + it("rejects zip traversal without creating files outside the target", async () => { + const zip = await zipEntries([["aa/escape.csv", "bad"]]); + const bytes = await readFile(zip); + const from = Buffer.from("aa/escape.csv"); + let index = bytes.indexOf(from); + while (index !== -1) { + bytes.set(Buffer.from("../escape.csv"), index); + index = bytes.indexOf(from, index + from.length); + } + await writeFile(zip, bytes); + await expect( + extractSession(zip, path.join(env.root, "out"), vi.fn()), + ).rejects.toThrow(); + await expect(readFile(path.join(env.root, "escape.csv"))).rejects.toThrow(); + }); + it("rejects duplicate entries instead of overwriting", async () => { + const zip = await zipEntries([ + ["logger_settings.json", "{}"], + ["logger_settings.json", "{}"], + ]); + await expect( + extractSession(zip, path.join(env.root, "out"), vi.fn()), + ).rejects.toThrow(); + }); +}); + +describe("credentials", () => { + it("returns only credential status to the UI and preserves a blank replacement", async () => { + expect(await setCredentials({ user: "alice", apiKey: "secret" })).toEqual({ + user: "alice", + hasApiKey: true, + persistent: true, + }); + expect( + await readFile(path.join(env.root, "cloud-logs.json"), "utf8"), + ).not.toContain("secret"); + await setCredentials({ user: "bob", apiKey: "" }); + expect(await getCredentials()).toEqual({ user: "bob", apiKey: "secret" }); + await clearCredentials(); + expect((await getCredentialsStatus()).hasApiKey).toBe(false); + }); + it("keeps the key only in memory when secure storage is unavailable", async () => { + env.secure = false; + expect( + (await setCredentials({ user: "alice", apiKey: "memory-only" })) + .persistent, + ).toBe(false); + expect(await readFile(path.join(env.root, "cloud-logs.json"), "utf8")).toBe( + '{"user":"alice"}', + ); + expect((await getCredentials()).apiKey).toBe("memory-only"); + }); + it("allows replacing an old key that cannot be decrypted", async () => { + await setCredentials({ user: "alice", apiKey: "old" }); + env.locked = true; + await expect( + setCredentials({ user: "alice", apiKey: "new" }), + ).resolves.toMatchObject({ hasApiKey: true }); + env.locked = false; + expect((await getCredentials()).apiKey).toBe("new"); + }); +}); diff --git a/electron-app/src/cloudLogs/archive.js b/electron-app/src/cloudLogs/archive.js new file mode 100644 index 000000000..bc2cfa19b --- /dev/null +++ b/electron-app/src/cloudLogs/archive.js @@ -0,0 +1,129 @@ +import archiver from "archiver"; +import yauzl from "yauzl"; +import { createWriteStream } from "node:fs"; +import { mkdir } from "node:fs/promises"; +import { pipeline } from "node:stream/promises"; +import { Transform } from "node:stream"; +import path from "node:path"; +import { + inspectSession, + isSessionFile, + MAX_BYTES, + MAX_FILES, +} from "./sessions.js"; + +export async function compressSession(dir, destination, onProgress) { + const before = await inspectSession(dir); + const archive = archiver("zip", { zlib: { level: 6 } }); + archive.on("progress", ({ entries }) => + onProgress(entries.processed, before.fileCount), + ); + // Missing/unreadable files must fail the upload, never silently omit data. + archive.on("warning", (error) => archive.destroy(error)); + const output = pipeline( + archive, + createWriteStream(destination, { flags: "wx" }), + ); + for (const file of before.files) + archive.file(path.join(dir, file.name), { name: file.name }); + try { + await Promise.all([archive.finalize(), output]); + } catch (error) { + archive.abort(); + throw error; + } + const after = await inspectSession(dir); + if (JSON.stringify(before.files) !== JSON.stringify(after.files)) { + throw new Error( + "The recording changed during compression. Stop logging before uploading.", + ); + } + return before; +} + +/** Extract only session files into a fresh private directory. No links or traversal. */ +export async function extractSession(archivePath, destination, onProgress) { + await mkdir(destination, { recursive: true }); + await new Promise((resolve, reject) => { + yauzl.open( + archivePath, + { lazyEntries: true, autoClose: true }, + (error, zip) => { + if (error) return reject(error); + let entries = 0; + let bytes = 0; + let declaredBytes = 0; + const seen = new Set(); + const fail = (err) => { + zip.close(); + reject(err); + }; + zip.on("error", fail); + zip.on("end", resolve); + zip.on("entry", (entry) => { + void (async () => { + const name = entry.fileName; + const mode = (entry.externalFileAttributes >>> 16) & 0o170000; + if ( + ++entries > MAX_FILES || + name.startsWith("/") || + /[\\:]/.test(name) || + name.split("/").some((p) => p === ".." || p === ".") || + (mode && mode !== 0o100000 && mode !== 0o040000) || + entry.generalPurposeBitFlag & 1 + ) { + throw new Error( + "Archive contains an unsafe or unsupported entry.", + ); + } + if (name.endsWith("/")) { + zip.readEntry(); + return; + } + if (!isSessionFile(name)) + throw new Error( + "Archive contains files outside a logging session.", + ); + declaredBytes += entry.uncompressedSize; + if (declaredBytes > MAX_BYTES) + throw new Error("Extracted session exceeds 4 GiB."); + const target = path.resolve(destination, name); + const key = + process.platform === "win32" ? target.toLowerCase() : target; + if (seen.has(key)) + throw new Error("Archive contains duplicate file entries."); + seen.add(key); + if (!target.startsWith(path.resolve(destination) + path.sep)) + throw new Error("Unsafe archive path."); + await mkdir(path.dirname(target), { recursive: true }); + const input = await new Promise((res, rej) => + zip.openReadStream(entry, (err, stream) => + err ? rej(err) : res(stream), + ), + ); + const limit = new Transform({ + transform(chunk, _encoding, done) { + bytes += chunk.length; + done( + bytes > MAX_BYTES + ? new Error("Extracted session exceeds 4 GiB.") + : null, + chunk, + ); + }, + }); + const output = createWriteStream(target, { flags: "wx" }); + input.once("error", fail); + limit.once("error", fail); + output.once("error", fail); + await pipeline(input, limit, output); + onProgress(entries, zip.entryCount); + zip.readEntry(); + })().catch(fail); + }); + zip.readEntry(); + }, + ); + }); + return inspectSession(destination); +} diff --git a/electron-app/src/cloudLogs/client.js b/electron-app/src/cloudLogs/client.js new file mode 100644 index 000000000..8176026e5 --- /dev/null +++ b/electron-app/src/cloudLogs/client.js @@ -0,0 +1,50 @@ +/** + * Integration boundary for Cloud Logs. Replace these four functions with HTTP + * calls once the remote API contract is available. Never log credentials. + * See README.md for normalized return values, archive layout and error handling. + */ +function pending() { + throw new Error( + "Cloud Logs API is not connected yet. The remote adapter still needs to be implemented.", + ); +} + +/** @returns {Promise<{items: Array<{id:string,user:string,notes:string,tags:string[],date:string}>,nextCursor?:string}>} */ +export async function listLogs({ credentials, cursor }) { + void credentials; + void cursor; + return pending(); +} + +/** @returns {Promise} API-backed suggestions, never invented locally. */ +export async function autocompleteTags({ credentials, query }) { + void credentials; + void query; + return pending(); +} + +/** + * Stream archivePath as ZIP. Metadata = {user, notes, tags, date}; authenticate + * with credentials.apiKey. Resolve only when the server confirms persistence. + * @returns {Promise<{id:string,user:string,notes:string,tags:string[],date:string}>} + */ +export async function uploadLog({ credentials, archivePath, metadata }) { + void credentials; + void archivePath; + void metadata; + return pending(); +} + +/** + * Download id to destination (a temporary ZIP path). Resolve AFTER closing the + * file. Enforce maxBytes while streaming, including if Content-Length is absent. + * Only send credentials to the trusted API, never arbitrary redirect hosts. + * @returns {Promise} + */ +export async function downloadLog({ credentials, id, destination, maxBytes }) { + void credentials; + void id; + void destination; + void maxBytes; + return pending(); +} diff --git a/electron-app/src/cloudLogs/credentials.js b/electron-app/src/cloudLogs/credentials.js new file mode 100644 index 000000000..3b1596ea2 --- /dev/null +++ b/electron-app/src/cloudLogs/credentials.js @@ -0,0 +1,89 @@ +import { app, safeStorage } from "electron"; +import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; +import path from "node:path"; + +let memoryKey = ""; + +function canPersist() { + return ( + safeStorage.isEncryptionAvailable() && + (process.platform !== "linux" || + !["basic_text", "unknown"].includes( + safeStorage.getSelectedStorageBackend(), + )) + ); +} + +const configPath = () => path.join(app.getPath("userData"), "cloud-logs.json"); + +async function readSettings() { + try { + return JSON.parse(await readFile(configPath(), "utf8")); + } catch (error) { + if (error.code === "ENOENT") return {}; + throw new Error("Could not read Cloud Logs credentials."); + } +} + +async function writeSettings(value) { + await mkdir(path.dirname(configPath()), { recursive: true }); + const temporary = `${configPath()}.tmp`; + await writeFile(temporary, JSON.stringify(value), { mode: 0o600 }); + await rename(temporary, configPath()); +} + +export async function getCredentials() { + const stored = await readSettings(); + let apiKey = memoryKey; + if (!apiKey && stored.encryptedKey && canPersist()) { + try { + apiKey = safeStorage.decryptString( + Buffer.from(stored.encryptedKey, "base64"), + ); + } catch { + throw new Error( + "The saved API key cannot be unlocked. Replace it in Credentials.", + ); + } + } + return { user: stored.user || "", apiKey }; +} + +export async function getCredentialsStatus() { + const stored = await readSettings(); + return { + user: stored.user || "", + hasApiKey: !!(memoryKey || stored.encryptedKey), + persistent: canPersist(), + }; +} + +export async function setCredentials({ user, apiKey }) { + if ( + typeof user !== "string" || + !user.trim() || + user.length > 200 || + (apiKey !== undefined && + (typeof apiKey !== "string" || apiKey.length > 8192)) + ) { + throw new Error("Enter a valid user and API key."); + } + // Blank means preserve. A replacement can also recover an unreadable old key. + const key = apiKey?.trim() || (await getCredentials()).apiKey; + if (!key) throw new Error("Enter an API key."); + const persistent = canPersist(); + await writeSettings({ + user: user.trim(), + encryptedKey: persistent + ? safeStorage.encryptString(key).toString("base64") + : undefined, + }); + memoryKey = persistent ? "" : key; + return getCredentialsStatus(); +} + +export async function clearCredentials() { + await writeSettings({}); + memoryKey = ""; + return getCredentialsStatus(); +} diff --git a/electron-app/src/cloudLogs/handlers.js b/electron-app/src/cloudLogs/handlers.js new file mode 100644 index 000000000..b4de25994 --- /dev/null +++ b/electron-app/src/cloudLogs/handlers.js @@ -0,0 +1,246 @@ +import { BrowserWindow, dialog, ipcMain } from "electron"; +import { createHash, randomUUID } from "node:crypto"; +import { + mkdtemp, + readdir, + readFile, + writeFile, + rename, + rm, + stat, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { pathToFileURL } from "node:url"; +import { getAppPath } from "../utils/paths.js"; +import * as credentials from "./credentials.js"; +import * as client from "./client.js"; +import { compressSession, extractSession } from "./archive.js"; +import { + findLocalSessions, + getCloudRoot, + getLogsRoot, + inspectSession, + MAX_BYTES, +} from "./sessions.js"; + +const allowedURLs = ["logging-view", "testing-view"].map( + (view) => + pathToFileURL(path.join(getAppPath(), "renderer", view, "index.html")).href, +); + +function assertSender(event) { + const url = event.senderFrame?.url?.split(/[?#]/)[0]; + if ( + event.senderFrame !== event.sender.mainFrame || + !allowedURLs.includes(url) + ) + throw new Error( + "Cloud Logs is only available to local Logging and Testing views.", + ); +} + +function text(value, max = 500) { + if (typeof value !== "string" || !value.trim() || value.length > max) + throw new Error("Invalid Cloud Logs request."); + return value; +} + +export function setupCloudLogsHandlers() { + const selected = new Map(); + let transferRunning = false; + let credentialsQueue = Promise.resolve(); + const handle = (name, fn) => + ipcMain.handle(`cloud-logs:${name}`, (event, input) => { + assertSender(event); + return fn(event, input); + }); + const register = (event, session, dir) => { + const id = randomUUID(); + selected.set(id, { owner: event.sender.id, dir }); + // Bound stale folder grants from repeated discovery/pickers. + if (selected.size > 2000) selected.delete(selected.keys().next().value); + const { files, ...summary } = session; + void files; + return { ...summary, id }; + }; + const auth = async () => { + await credentialsQueue; + const value = await credentials.getCredentials(); + if (!value.user || !value.apiKey) + throw new Error("Configure your user and API key in Credentials first."); + return value; + }; + const changeCredentials = (fn) => { + const result = credentialsQueue.then(fn); + credentialsQueue = result.catch(() => {}); + return result; + }; + const transfer = async (event, input, fn) => { + text(input?.requestId, 100); + if (transferRunning) + throw new Error("Another Cloud Logs transfer is in progress."); + transferRunning = true; + const report = (phase, completed, total) => { + if (!event.sender.isDestroyed()) + event.sender.send("cloud-logs:progress", { + requestId: input.requestId, + phase, + completed, + total, + }); + }; + try { + return await fn(report); + } finally { + transferRunning = false; + } + }; + + handle("credentials-status", () => + credentialsQueue.then(() => credentials.getCredentialsStatus()), + ); + handle("credentials-save", (_event, input) => + changeCredentials(() => credentials.setCredentials(input)), + ); + handle("credentials-clear", () => + changeCredentials(() => credentials.clearCredentials()), + ); + handle("local-sessions", async (event) => { + await getCloudRoot(); + const sessions = await findLocalSessions(); + return sessions.map(({ dir, ...session }) => register(event, session, dir)); + }); + handle("select-session", async (event) => { + const result = await dialog.showOpenDialog( + BrowserWindow.fromWebContents(event.sender), + { + title: "Select a completed logging session", + defaultPath: await getLogsRoot(), + properties: ["openDirectory"], + }, + ); + if (result.canceled) return null; + const dir = result.filePaths[0]; + return register(event, await inspectSession(dir), dir); + }); + handle("list", async (_event, cursor) => + client.listLogs({ + credentials: await auth(), + cursor: cursor === undefined ? undefined : text(cursor), + }), + ); + handle("tags", async (_event, query) => { + if (typeof query !== "string" || query.length > 200) + throw new Error("Invalid tag query."); + return client.autocompleteTags({ credentials: await auth(), query }); + }); + handle("upload", (event, input) => + transfer(event, input, async (report) => { + const grant = selected.get(input.sessionId); + if (!grant || grant.owner !== event.sender.id) + throw new Error("Select the session again before uploading."); + if ( + typeof input.notes !== "string" || + input.notes.length > 10000 || + !Array.isArray(input.tags) || + input.tags.length > 30 + ) + throw new Error("Invalid notes or tags."); + const tags = [...new Set(input.tags.map((tag) => text(tag, 100)))]; + const account = await auth(); + const temp = await mkdtemp(path.join(tmpdir(), "cloud-logs-upload-")); + try { + report("compressing", 0); + const archivePath = path.join(temp, "session.zip"); + const session = await compressSession( + grant.dir, + archivePath, + (n, total) => report("compressing", n, total), + ); + report("uploading"); + return await client.uploadLog({ + credentials: account, + archivePath, + metadata: { + user: account.user, + notes: input.notes.trim(), + tags, + date: session.date, + }, + }); + } finally { + await rm(temp, { recursive: true, force: true }); + } + }), + ); + handle("download", (event, input) => + transfer(event, input, async (report) => { + const id = text(input.id); + const account = await auth(); + const root = await getCloudRoot(); + const destination = path.join( + root, + createHash("sha256").update(id).digest("hex"), + ); + try { + const info = JSON.parse( + await readFile(path.join(destination, "cloud-log.json"), "utf8"), + ); + if (info.id !== id) + throw new Error("Downloaded session identity mismatch."); + const session = await inspectSession(destination); + return { id, path: destination, date: session.date }; + } catch (error) { + if (error.code !== "ENOENT") throw error; + } + // Staging on the destination filesystem makes the final rename atomic. + const temp = await mkdtemp(path.join(root, ".download-")); + try { + const archivePath = path.join(temp, "session.zip"); + report("downloading"); + await client.downloadLog({ + credentials: account, + id, + destination: archivePath, + maxBytes: MAX_BYTES, + }); + if ((await stat(archivePath)).size > MAX_BYTES) + throw new Error("Downloaded archive exceeds 4 GiB."); + report("extracting", 0); + const extracted = path.join(temp, "session"); + const session = await extractSession( + archivePath, + extracted, + (n, total) => report("extracting", n, total), + ); + await writeFile( + path.join(extracted, "cloud-log.json"), + JSON.stringify({ id, date: session.date }), + ); + await rename(extracted, destination); + return { id, path: destination, date: session.date }; + } finally { + await rm(temp, { recursive: true, force: true }); + } + }), + ); + handle("downloads", async () => { + const root = await getCloudRoot(); + const items = []; + for (const entry of await readdir(root, { withFileTypes: true })) { + if (!entry.isDirectory() || !/^[a-f0-9]{64}$/.test(entry.name)) continue; + const dir = path.join(root, entry.name); + try { + const info = JSON.parse( + await readFile(path.join(dir, "cloud-log.json"), "utf8"), + ); + const session = await inspectSession(dir); + items.push({ id: text(info.id), date: session.date, path: dir }); + } catch { + /* Do not expose partial or invalid downloads. */ + } + } + return items.sort((a, b) => b.date.localeCompare(a.date)); + }); +} diff --git a/electron-app/src/cloudLogs/sessions.js b/electron-app/src/cloudLogs/sessions.js new file mode 100644 index 000000000..1212c8b4c --- /dev/null +++ b/electron-app/src/cloudLogs/sessions.js @@ -0,0 +1,105 @@ +import { lstat, readdir, readFile, mkdir } from "node:fs/promises"; +import path from "node:path"; +import { readConfig } from "../config/configInstance.js"; +import { getBackendWorkingDir } from "../utils/paths.js"; + +export const MAX_BYTES = 4 * 1024 ** 3; +export const MAX_FILES = 10000; +// Cloud archives keep every regular file below a session root, including files +// from future backend logger subfolders that this app does not know yet. +export const isSessionFile = (name) => + name === "logger_settings.json" || + (!name.startsWith("cloud-log.json") && !name.startsWith("../")); + +export async function getLogsRoot() { + const config = await readConfig(); + return path.resolve( + getBackendWorkingDir(), + config.logging?.logging_path || ".", + "logger", + ); +} + +export async function getCloudRoot() { + const root = path.join(await getLogsRoot(), "cloud-logs"); + await mkdir(root, { recursive: true }); + if ((await lstat(root)).isSymbolicLink()) + throw new Error("Cloud Logs folder cannot be a symbolic link."); + return root; +} + +/** Validate one recording, not the parent app launch. Refuse symlinks. */ +export async function inspectSession(dir) { + if (!(await lstat(dir)).isDirectory()) + throw new Error("Select a logging session folder."); + const files = []; + let bytes = 0; + async function walk(relative = "", depth = 0) { + if (depth > 8) throw new Error("Session directory is too deeply nested."); + for (const entry of await readdir(path.join(dir, relative), { + withFileTypes: true, + })) { + if (entry.isSymbolicLink()) + throw new Error("Session folders cannot contain symbolic links."); + const name = relative ? `${relative}/${entry.name}` : entry.name; + if (entry.isDirectory()) { + await walk(name, depth + 1); + } else if (entry.isFile() && name !== "cloud-log.json") { + const stats = await lstat(path.join(dir, name)); + bytes += stats.size; + files.push({ name, size: stats.size, mtimeMs: stats.mtimeMs }); + if (bytes > MAX_BYTES || files.length > MAX_FILES) + throw new Error("Session exceeds the 4 GiB / 10,000 file limit."); + } + } + } + await walk(); + if (!files.some((f) => f.name === "logger_settings.json")) + throw new Error( + "Select a recording containing logger_settings.json, not the app launch folder.", + ); + const settings = JSON.parse( + await readFile(path.join(dir, "logger_settings.json"), "utf8"), + ); + if ( + typeof settings.date !== "string" || + !/^\d{4}-\d{2}-\d{2}T\d{2}[-:]\d{2}[-:]\d{2}/.test(settings.date) + ) + throw new Error("Session has no valid recording date."); + return { + name: path.basename(dir), + launch: path.basename(path.dirname(dir)), + date: settings.date, + bytes, + fileCount: files.length, + files: files.sort((a, b) => a.name.localeCompare(b.name)), + }; +} + +export async function findLocalSessions() { + const root = await getLogsRoot(); + let launches; + try { + launches = await readdir(root, { withFileTypes: true }); + } catch (error) { + if (error.code === "ENOENT") return []; + throw error; + } + const sessions = []; + for (const launch of launches) { + if (!launch.isDirectory() || !/^\d{4}-\d{2}-\d{2}T/.test(launch.name)) + continue; + for (const entry of await readdir(path.join(root, launch.name), { + withFileTypes: true, + })) { + if (!entry.isDirectory() || entry.name === "others") continue; + const dir = path.join(root, launch.name, entry.name); + try { + sessions.push({ dir, ...(await inspectSession(dir)) }); + } catch { + /* Incomplete/empty recordings are not upload candidates. */ + } + } + } + return sessions.sort((a, b) => b.date.localeCompare(a.date)); +} diff --git a/electron-app/src/ipc/handlers.js b/electron-app/src/ipc/handlers.js index f16fce3f7..9f01402a3 100644 --- a/electron-app/src/ipc/handlers.js +++ b/electron-app/src/ipc/handlers.js @@ -18,12 +18,11 @@ import { readConfig, writeConfig, } from "../config/configInstance.js"; -import { - getBackendWorkingDir, - restartBackend, -} from "../processes/backend.js"; +import { getBackendWorkingDir, restartBackend } from "../processes/backend.js"; import { logger } from "../utils/logger.js"; import { getAppPath } from "../utils/paths.js"; +import { setupCloudLogsHandlers } from "../cloudLogs/handlers.js"; +import { getLogsRoot } from "../cloudLogs/sessions.js"; import { getCurrentView, getMainWindow, @@ -39,6 +38,7 @@ import { * setupIpcHandlers(); */ function setupIpcHandlers() { + setupCloudLogsHandlers(); /** * @event get-current-view * @description Returns the identifier of the currently loaded view. @@ -56,7 +56,10 @@ function setupIpcHandlers() { await restartBackend(); } catch (error) { logger.electron.error("Failed to restart backend:", error); - dialog.showErrorBox("Restart Failed", `Could not restart backend:\n\n${error.message}`); + dialog.showErrorBox( + "Restart Failed", + `Could not restart backend:\n\n${error.message}`, + ); } finally { loadView("testing-view"); } @@ -109,7 +112,7 @@ function setupIpcHandlers() { ]; const rendererDir = join(getAppPath(), "renderer"); return ALL_VIEWS.filter(({ mode }) => - fs.existsSync(join(rendererDir, `${mode}-view`)) + fs.existsSync(join(rendererDir, `${mode}-view`)), ); }); @@ -127,8 +130,6 @@ function setupIpcHandlers() { return view; }); - - /** * @event save-config * @async @@ -196,6 +197,7 @@ function setupIpcHandlers() { const mainWindow = getMainWindow(); const result = await dialog.showOpenDialog(mainWindow, { title: "Select Logging Folder", + defaultPath: await getLogsRoot(), properties: ["openDirectory"], }); @@ -247,36 +249,29 @@ function setupIpcHandlers() { } }; - // A session is /logger_settings.json, /data//*.csv and - // /order/order.csv — exactly what the renderer's openSession reads. - // Walking only those three shapes keeps a huge parent folder from being - // scanned, and Dirent.isDirectory()/isFile() skip symlinks. + // A recording is identified by logger_settings.json. Walk its contents so + // future logger subfolders are preserved and their CSVs can be read locally. const SESSION_FILE_LIMIT = 4000; const listSessionEntries = async (dir) => { const entries = []; - if (fs.existsSync(join(dir, "logger_settings.json"))) { - entries.push("logger_settings.json"); - } - if (fs.existsSync(join(dir, "order", "order.csv"))) { - entries.push("order/order.csv"); - } - let boards = []; - try { - boards = await readdir(join(dir, "data"), { withFileTypes: true }); - } catch { - return entries; - } - for (const board of boards) { - if (!board.isDirectory()) continue; - const files = await readdir(join(dir, "data", board.name), { + const walk = async (relative = "") => { + const children = await readdir(join(dir, relative), { withFileTypes: true, }); - for (const file of files) { - if (!file.isFile() || !file.name.endsWith(".csv")) continue; - entries.push(`data/${board.name}/${file.name}`); - if (entries.length >= SESSION_FILE_LIMIT) return entries; + for (const child of children) { + if (child.isSymbolicLink()) continue; + const name = relative ? join(relative, child.name) : child.name; + if (child.isDirectory()) await walk(name); + else if (child.isFile() && name !== "cloud-log.json") + entries.push(name.split(sep).join("/")); + if (entries.length >= SESSION_FILE_LIMIT) return; } + }; + try { + await walk(); + } catch (error) { + if (error.code !== "ENOENT") throw error; } return entries; }; diff --git a/frontend/frontend-kit/kit/src/core/cloudLogs.ts b/frontend/frontend-kit/kit/src/core/cloudLogs.ts new file mode 100644 index 000000000..1b194b009 --- /dev/null +++ b/frontend/frontend-kit/kit/src/core/cloudLogs.ts @@ -0,0 +1,58 @@ +/** Shared desktop contract. HTTP and filesystem implementations live in Electron. */ +export interface CloudLog { + id: string; + user: string; + notes: string; + tags: string[]; + date: string; +} + +export interface LocalLogSession { + id: string; + name: string; + launch: string; + date: string; + fileCount: number; + bytes: number; +} + +export interface DownloadedLog { + id: string; + path: string; + date: string; +} + +export interface CloudCredentialsStatus { + user: string; + hasApiKey: boolean; + persistent: boolean; +} + +export interface CloudLogProgress { + requestId: string; + phase: "compressing" | "uploading" | "downloading" | "extracting"; + completed?: number; + total?: number; +} + +export interface CloudLogsAPI { + getCredentialsStatus(): Promise; + setCredentials(input: { + user: string; + apiKey?: string; + }): Promise; + clearCredentials(): Promise; + getLocalSessions(): Promise; + selectSession(): Promise; + list(cursor?: string): Promise<{ items: CloudLog[]; nextCursor?: string }>; + autocompleteTags(query: string): Promise; + upload(input: { + sessionId: string; + notes: string; + tags: string[]; + requestId: string; + }): Promise; + download(input: { id: string; requestId: string }): Promise; + getDownloads(): Promise; + onProgress(callback: (progress: CloudLogProgress) => void): () => void; +} diff --git a/frontend/frontend-kit/kit/src/core/index.ts b/frontend/frontend-kit/kit/src/core/index.ts index 005c4cefa..4315aca3e 100644 --- a/frontend/frontend-kit/kit/src/core/index.ts +++ b/frontend/frontend-kit/kit/src/core/index.ts @@ -3,3 +3,4 @@ export * from "./loggerColors"; export * from "./minMaxDownsample"; export * from "./types"; export * from "./websocket"; +export type * from "./cloudLogs"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0df48307a..3b227886f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -48,6 +48,9 @@ importers: ansi-to-html: specifier: ^0.7.2 version: 0.7.2 + archiver: + specifier: 5.3.2 + version: 5.3.2 electron-store: specifier: ^11.0.2 version: 11.0.2 @@ -57,6 +60,9 @@ importers: picocolors: specifier: ^1.1.1 version: 1.1.1 + yauzl: + specifier: 2.10.0 + version: 2.10.0 devDependencies: '@vitest/coverage-v8': specifier: ^4.0.18 From 589d5a14c78ae672fd8e0c46c06dd80f36b3b405 Mon Sep 17 00:00:00 2001 From: Javier Ribal del Rio Date: Sun, 15 Nov 2026 09:48:55 +0100 Subject: [PATCH 5/6] wip(logging-view,session): cloud logs integration --- .../components/cloudLogs/CloudLogsDialog.tsx | 825 ++++++++++++++++++ .../src/components/sidebar/AppSidebar.tsx | 2 + frontend/logging-view/src/vite-env.d.ts | 2 + 3 files changed, 829 insertions(+) create mode 100644 frontend/logging-view/src/components/cloudLogs/CloudLogsDialog.tsx diff --git a/frontend/logging-view/src/components/cloudLogs/CloudLogsDialog.tsx b/frontend/logging-view/src/components/cloudLogs/CloudLogsDialog.tsx new file mode 100644 index 000000000..8bea9cf7a --- /dev/null +++ b/frontend/logging-view/src/components/cloudLogs/CloudLogsDialog.tsx @@ -0,0 +1,825 @@ +import { useEffect, useRef, useState } from "react"; +import { + Button, + Checkbox, + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, + Input, + Label, + SidebarMenuButton, + Tabs, + TabsContent, + TabsList, + TabsTrigger, + Textarea, +} from "@hyperloopupv/frontend-kit/components"; +import { + Database, + FileDown, + FolderOpen, + RefreshCw, + Upload, + X, +} from "@hyperloopupv/frontend-kit/icons"; +import type { + CloudCredentialsStatus, + CloudLog, + CloudLogProgress, + CloudLogsAPI, + DownloadedLog, + LocalLogSession, +} from "@hyperloopupv/frontend-kit/core"; +import { useSessionFolder } from "../../hooks/useSessionFolder"; + +const failureMessage = (error: unknown) => + (error instanceof Error ? error.message : String(error)).replace( + /^Error invoking remote method '[^']+': (?:Error: )?/, + "", + ); +const dateLabel = (date: string) => + date.replace("T", " · ").replace(/(\d{2})-(\d{2})-(\d{2})$/, "$1:$2:$3"); +const sizeLabel = (bytes: number) => `${(bytes / 1024 ** 2).toFixed(1)} MB`; +const phases = { + compressing: "Compressing session…", + uploading: "Uploading ZIP…", + downloading: "Downloading ZIP…", + extracting: "Extracting session…", +}; + +function Notice({ + children, + error = false, +}: { + children: React.ReactNode; + error?: boolean; +}) { + return ( +

+ {children} +

+ ); +} + +function CredentialsPanel({ + api, + status, + onSaved, +}: { + api: CloudLogsAPI; + status: CloudCredentialsStatus | null; + onSaved: (status: CloudCredentialsStatus) => void; +}) { + const [user, setUser] = useState(status?.user ?? ""); + const [key, setKey] = useState(""); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(""); + const [message, setMessage] = useState(""); + const [confirmClear, setConfirmClear] = useState(false); + useEffect(() => setUser(status?.user ?? ""), [status?.user]); + + const save = async (clear = false) => { + setBusy(true); + setError(""); + setMessage(""); + try { + const next = clear + ? await api.clearCredentials() + : await api.setCredentials({ user, apiKey: key }); + setKey(""); + setConfirmClear(false); + onSaved(next); + setMessage( + clear + ? "Credentials removed." + : "Credentials saved. They are shared by Logging View and Testing View.", + ); + } catch (err) { + setError(failureMessage(err)); + } finally { + setBusy(false); + } + }; + + return ( +
{ + event.preventDefault(); + void save(); + }} + > +

+ Set the account used to access Cloud Logs and identify your uploads. +

+
+ + setUser(e.target.value)} + placeholder="Your user name" + /> +
+
+ + setKey(e.target.value)} + placeholder={ + status?.hasApiKey + ? "Key configured — leave blank to keep it" + : "Enter your API key" + } + /> +

+ {status?.persistent + ? "Stored encrypted on this computer. The saved key is never displayed." + : "Secure storage is unavailable. Your API key will only be kept until the app closes."} +

+
+ {error && {error}} + {message && {message}} +
+ + {status?.hasApiKey && ( + + )} +
+ {confirmClear && ( +
+ Remove the saved user and API key from this computer? + +
+ )} +
+ ); +} + +function UploadPanel({ + api, + ready, + busy, + onUpload, +}: { + api: CloudLogsAPI; + ready: boolean; + busy: boolean; + onUpload: ( + session: LocalLogSession, + notes: string, + tags: string[], + ) => Promise; +}) { + const [sessions, setSessions] = useState([]); + const [selected, setSelected] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(""); + const [notes, setNotes] = useState(""); + const [tags, setTags] = useState([]); + const [query, setQuery] = useState(""); + const [suggestions, setSuggestions] = useState([]); + const [tagError, setTagError] = useState(""); + const [tagBusy, setTagBusy] = useState(false); + const [stopped, setStopped] = useState(false); + const refresh = async () => { + setLoading(true); + setError(""); + try { + setSessions(await api.getLocalSessions()); + } catch (err) { + setError(failureMessage(err)); + } finally { + setLoading(false); + } + }; + useEffect(() => { + void refresh(); + }, [api]); // eslint-disable-line react-hooks/exhaustive-deps + useEffect(() => { + if (!ready) return; + let current = true; + setTagBusy(true); + setTagError(""); + setSuggestions([]); + const timer = setTimeout(() => { + api + .autocompleteTags(query) + .then((items) => { + if (current) setSuggestions([...new Set(items)]); + }) + .catch((err) => { + if (current) setTagError(failureMessage(err)); + }) + .finally(() => { + if (current) setTagBusy(false); + }); + }, 300); + return () => { + current = false; + clearTimeout(timer); + }; + }, [api, query, ready]); + const choose = (session: LocalLogSession | null) => { + if (session) { + setSelected(session); + setStopped(false); + } + }; + const browse = async () => { + setLoading(true); + setError(""); + try { + choose(await api.selectSession()); + } catch (err) { + setError(failureMessage(err)); + } finally { + setLoading(false); + } + }; + + return ( +
{ + e.preventDefault(); + if (selected) await onUpload(selected, notes, tags); + }} + > +

+ Share one completed recording. Its CSV files and session settings are + compressed into a ZIP before upload. +

+
+
+ + +
+ + + {!loading && !sessions.length && ( +

+ No completed recordings found in the configured logger folder. You + can browse to another session. +

+ )} +
+ {selected && ( +
+

{selected.name}

+

Launch: {selected.launch}

+

Log date: {dateLabel(selected.date)}

+

+ {selected.fileCount} files · {sizeLabel(selected.bytes)} before + compression +

+
+ )} +
+
+ +