UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

69 lines • 2.6 kB
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= import { record, startOp } from "../../observe/context.js"; import { VFSName } from "../../types.js"; import { ByteSpool } from "../../io/spool.js"; import { getDirectoryMetadata, getMetadata, MAX_UPLOAD_SIZE, upload } from "./client.js"; import { parentPath } from "./_helpers.js"; import { isNotFound } from "./errors.js"; import { backendPath } from "./path.js"; import { efbig, enoent, enotdir } from "../../errors/fs.js"; async function ensureParentDirectory(accessor, remoteParent, virtualTarget) { try { await getDirectoryMetadata(accessor, remoteParent); return; } catch (exc) { if (!isNotFound(exc)) throw exc; } try { await getMetadata(accessor, remoteParent); } catch (exc) { if (isNotFound(exc)) throw enoent(virtualTarget); throw exc; } throw enotdir(virtualTarget); } /** * Write a file in one PUT streamed from a spool, which knows its length; one * past the PUT's cap is refused before any request. Mirrors Python's `write`. */ export async function write(accessor, path, data) { const remoteParent = backendPath(accessor.config, parentPath(path)); const remotePath = backendPath(accessor.config, path); const spool = new ByteSpool(); try { await spool.fill(data); if (spool.size > MAX_UPLOAD_SIZE) throw efbig(path.virtual); const timer = startOp(); await ensureParentDirectory(accessor, remoteParent, path.virtual); try { await upload(accessor, remotePath, spool, true); } catch (exc) { if (isNotFound(exc)) throw enoent(path.virtual); throw exc; } record('write', path.virtual, VFSName.DATABRICKS_VOLUME, spool.size, timer); } finally { await spool.close(); } } //# sourceMappingURL=write.js.map