54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import type {
|
|
BrowserDataObservation,
|
|
BrowserDataObserver,
|
|
BrowserDataFailureCode,
|
|
BrowserDataOperation,
|
|
} from "../../application/ports/browser-file-storage/shared.ts";
|
|
import { observeBrowserData } from "../browser-file-storage/result.ts";
|
|
|
|
type BrowserFileObservation = Readonly<{
|
|
operation: BrowserDataOperation;
|
|
outcome: "SUCCESS" | "DISMISSED" | "FAILED";
|
|
failureCode?: BrowserDataFailureCode;
|
|
byteBucket?: BrowserDataObservation["byteBucket"];
|
|
}>;
|
|
|
|
/**
|
|
* File adapters use the platform BrowserDataObserver as the telemetry SSOT.
|
|
* The helper below is the sole mapper from file-local dismissal semantics.
|
|
*/
|
|
export type BrowserFileObserver = BrowserDataObserver;
|
|
|
|
export function byteBucket(
|
|
byteLength: number | null,
|
|
): BrowserFileObservation["byteBucket"] | undefined {
|
|
if (byteLength === null || !Number.isSafeInteger(byteLength) || byteLength < 0) {
|
|
return undefined;
|
|
}
|
|
if (byteLength === 0) return "ZERO";
|
|
if (byteLength < 1_048_576) return "LT1MIB";
|
|
if (byteLength < 10_485_760) return "1_TO_9MIB";
|
|
if (byteLength < 104_857_600) return "10_TO_99MIB";
|
|
return "GTE100MIB";
|
|
}
|
|
|
|
export function observeBrowserFile(
|
|
observer: BrowserFileObserver | undefined,
|
|
observation: BrowserFileObservation,
|
|
): void {
|
|
observeBrowserData(
|
|
observer,
|
|
Object.freeze({
|
|
operation: observation.operation,
|
|
outcome:
|
|
observation.outcome === "FAILED" ? "FAILED" : "SUCCEEDED",
|
|
...(observation.failureCode
|
|
? { failureCode: observation.failureCode }
|
|
: {}),
|
|
...(observation.byteBucket
|
|
? { byteBucket: observation.byteBucket }
|
|
: {}),
|
|
}),
|
|
);
|
|
}
|