UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

60 lines • 2.63 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 { nowIso } from "../../utils/dates.js"; import { norm } from "../../utils/path.js"; import { activePipe } from "../dev/device.js"; import { checkCapacity, checkDestParents, checkWriteTarget, lookupError } from "./dest.js"; import { makeAbortError } from "../../utils/abort.js"; /** * Copy one key onto another. A live pipe at either end is used as one: a * pipe source is read through its reader and a pipe destination written * into, so no bytes land in a row no reader sees (GNU: `cp <(printf x) f` * and `cp f >(cat)` both deliver). Mirrors Python's copy. */ export async function copy(accessor, src, dst, signal) { const s = norm(src.mountPath); const d = norm(dst.mountPath); checkDestParents(accessor, dst, d); const source = activePipe(accessor, s); const target = activePipe(accessor, d); if (source !== null && target !== null) { for await (const chunk of source.stream(src, signal)) await target.write(chunk, dst, signal); return; } let data; if (source !== null) { data = await source.read(src, null, signal); // A caller that gave up (`timeout`) stores nothing. if (signal?.aborted === true) throw makeAbortError(signal); // The copy held no names while it waited on the pipe, so another job may // have moved or replaced the destination meanwhile: check it again, as a // write would, before storing. checkDestParents(accessor, dst, d); } else data = accessor.store.files.get(s); if (data === undefined) throw lookupError(accessor, src, s); if (target !== null) { await target.write(data, dst, signal); return; } checkWriteTarget(accessor, dst, d); checkCapacity(accessor, dst, d, data.byteLength); accessor.store.files.set(d, data); accessor.store.modified.set(d, nowIso()); } //# sourceMappingURL=copy.js.map