@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
52 lines • 2.34 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 { FileStat, FileType } from "../../types.js";
import { enoent } from "../../errors/fs.js";
import { rstripSlash } from "../../utils/slash.js";
import { perAccessor } from "../hierarchy/bind.js";
import { makeStat as hierarchyStat } from "../hierarchy/stat.js";
import { tableOf } from "./scope.js";
function nameOf(spec) {
const last = rstripSlash(spec.virtual).split('/').pop();
return last === undefined || last === '' ? '/' : last;
}
/**
* Build a store's stat: a group proves its table, a row its bytes.
*
* A row a listing names is answered by the mount from that listing, which
* carries its rendered size; one no listing names is rendered here, so the
* size is exact either way.
*/
export function makeStat(tree, read) {
async function tableGuard(accessor, match, virtual) {
const table = tableOf(tree.pinned(accessor), match);
if (!(await tree.tableExists(accessor, table)))
throw enoent(virtual);
}
async function statRow(accessor, match, path) {
await tableGuard(accessor, match, path.virtual);
return new FileStat({
name: nameOf(path),
size: (await read(accessor, path)).length,
type: FileType.FILE,
content: match.scope?.filetype ?? null,
});
}
const overrides = {};
for (const kind of Object.keys(tree.readers))
overrides[kind] = statRow;
const statFor = perAccessor((accessor) => hierarchyStat(tree.detect(accessor), { guards: { group: tableGuard }, overrides }));
return (accessor, path) => statFor(accessor)(accessor, path);
}
//# sourceMappingURL=stat.js.map