@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
52 lines • 2.24 kB
JavaScript
// ========= 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 { enotsup } from "../../errors/fs.js";
import { writeCondition } from "../../cache/context.js";
import { OwnRead } from "../../cache/types.js";
import { sha1Hex } from "../../utils/hash.js";
import { downloadFile } from "./api.js";
import { liveOf } from "./fingerprint.js";
import { liveItemOf } from "./resolve.js";
import { replace, write } from "./write.js";
/**
* Resize a file by rewriting it whole. Emptying reads nothing and carries the
* agent's version; a resize holds the sha1 of the bytes it downloaded, or none
* for a file without one. Mirrors Python's `truncate`.
*/
export async function truncate(accessor, path, length, noCreate = false, handed = {}) {
if (noCreate)
throw enotsup('box', 'truncate --no-create', path);
if (length === 0) {
await write(accessor, path, new Uint8Array(0), handed);
return;
}
const item = await liveItemOf(accessor, path, handed.entry);
const live = liveOf(item);
let data = new Uint8Array(0);
let own = OwnRead.ABSENT;
if (item !== null && live !== null) {
data = await downloadFile(accessor.tokenManager, item.id);
own = live.content !== null ? await sha1Hex(data) : OwnRead.UNKNOWN;
}
let next;
if (length <= data.length) {
next = data.slice(0, length);
}
else {
next = new Uint8Array(length);
next.set(data);
}
await replace(accessor, { path, body: next, condition: await writeCondition(path, 'put', own) }, handed);
}
//# sourceMappingURL=truncate.js.map