mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
49 lines
2.0 KiB
TypeScript
49 lines
2.0 KiB
TypeScript
import { LocalDriver } from './drivers/local.driver';
|
|
import { S3Driver } from './drivers/s3.driver';
|
|
import { StorageBackendError, describeError, type StorageDriver } from './storage.types';
|
|
import type { StorageBackend } from '@trek/shared';
|
|
|
|
import { randomUUID } from 'node:crypto';
|
|
import { Readable } from 'node:stream';
|
|
|
|
/**
|
|
* Test-connection probes (spec: POST /api/admin/storage/test). Drivers are
|
|
* EPHEMERAL — constructed per probe, never registered, never touching
|
|
* registry state. A mirror is probed per target (primary and each replica
|
|
* individually) because MirrorDriver hides replica failures by design.
|
|
*
|
|
* Residue: a local probe may leave an empty `trek-probe/` directory in the
|
|
* target root. It is inert — not a category prefix, invisible to lists
|
|
* (objects only), and outside every retention pattern.
|
|
*/
|
|
|
|
export interface ProbeTargetResult {
|
|
name: string;
|
|
ok: boolean;
|
|
error?: string;
|
|
}
|
|
|
|
/** put → stat → delete of a unique probe key; any throw becomes the target's error. */
|
|
export async function probeDriver(name: string, driver: StorageDriver): Promise<ProbeTargetResult> {
|
|
const key = `trek-probe/${randomUUID()}`;
|
|
try {
|
|
await driver.put(key, Readable.from('trek storage probe'));
|
|
const stat = await driver.stat(key);
|
|
if (!stat) throw new StorageBackendError('probe object vanished between put and stat');
|
|
await driver.delete(key);
|
|
return { name, ok: true };
|
|
} catch (err) {
|
|
return { name, ok: false, error: describeError(err) };
|
|
}
|
|
}
|
|
|
|
/** A non-mirror backend definition (secrets already decrypted) → throwaway driver. */
|
|
export function ephemeralDriverFor(backend: Extract<StorageBackend, { type: 'local' | 's3' }>): StorageDriver {
|
|
if (backend.type === 'local') {
|
|
const driver = new LocalDriver({ id: `probe-${backend.name}`, root: backend.options.root });
|
|
driver.init({ ensurePrefixes: [], cleanSpool: false });
|
|
return driver;
|
|
}
|
|
return new S3Driver({ id: `probe-${backend.name}`, ...backend.options });
|
|
}
|