import { withMappedErrors } from "./internal/error-mapping.js"; import type { NapiVolumeHandle, NapiVolumeInfo, } from "./internal/napi.js"; import { VolumeFs } from "./volume-fs.js"; const READ_ONLY_MSG = "VolumeHandle is read-only — fetch a live handle via Volume.get(name) for lifecycle methods."; export class VolumeHandle { private readonly inner: NapiVolumeHandle | NapiVolumeInfo; readonly name: string; readonly kind: string; readonly quotaMib: number | null; readonly usedBytes: number; readonly capacityBytes: number | null; readonly diskFormat: string | null; readonly diskFstype: string | null; readonly labels: ReadonlyArray; readonly createdAt: Date | null; /** @internal */ constructor(inner: NapiVolumeHandle | NapiVolumeInfo) { this.inner = inner; this.name = inner.name; this.kind = inner.kind; this.quotaMib = typeof inner.quotaMib === "number" ? inner.quotaMib : null; this.usedBytes = inner.usedBytes; this.capacityBytes = typeof inner.capacityBytes === "number" ? inner.capacityBytes : null; this.diskFormat = typeof inner.diskFormat === "string" ? inner.diskFormat : null; this.diskFstype = typeof inner.diskFstype === "string" ? inner.diskFstype : null; this.labels = Object.entries(inner.labels); this.createdAt = typeof inner.createdAt === "number" ? new Date(inner.createdAt) : null; } async remove(): Promise { if (typeof (this.inner as NapiVolumeHandle).remove !== "function") { throw new Error(READ_ONLY_MSG); } await withMappedErrors(() => (this.inner as NapiVolumeHandle).remove()); } /** Host-side filesystem operations on this volume's directory. */ fs(): VolumeFs { if (typeof (this.inner as NapiVolumeHandle).fs !== "function") { throw new Error(READ_ONLY_MSG); } return new VolumeFs((this.inner as NapiVolumeHandle).fs()); } } /** @internal */ export function volumeInfoToHandle(info: NapiVolumeInfo): VolumeHandle { return new VolumeHandle(info); }