@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
72 lines • 2.65 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 { enoent } from "../../errors/fs.js";
import { FileStat, FileType } from "../../types.js";
import { toIsoZ } from "../../utils/dates.js";
import { contentTypeForPath } from "../../utils/filetype.js";
import { stripSlash } from "../../utils/slash.js";
import { getDirectoryMetadata, getMetadata } from "./client.js";
import { isNotFound } from "./errors.js";
import { backendPath } from "./path.js";
function nameFromBackendPath(remotePath) {
const stripped = stripSlash(remotePath);
return stripped.split('/').pop() ?? stripped;
}
function modifiedToIso(value) {
if (value === null || value === '')
return null;
const parsed = new Date(value);
if (Number.isNaN(parsed.getTime()))
return value;
return toIsoZ(parsed);
}
async function directoryStatOrRaise(accessor, remotePath, path) {
try {
await getDirectoryMetadata(accessor, remotePath);
}
catch (exc) {
if (isNotFound(exc))
throw enoent(path.virtual);
throw exc;
}
return new FileStat({ name: nameFromBackendPath(remotePath), type: FileType.DIRECTORY });
}
export async function stat(accessor, path) {
const stripped = stripSlash(path.mountPath);
if (stripped === '') {
return new FileStat({ name: '/', type: FileType.DIRECTORY });
}
const remotePath = backendPath(accessor.config, path);
let metadata;
try {
metadata = await getMetadata(accessor, remotePath);
}
catch (exc) {
if (isNotFound(exc)) {
return directoryStatOrRaise(accessor, remotePath, path);
}
throw exc;
}
const name = nameFromBackendPath(remotePath);
const size = metadata.contentLength;
const modified = modifiedToIso(metadata.lastModified);
return new FileStat({
name,
size,
modified,
type: FileType.FILE,
content: contentTypeForPath(name),
});
}
//# sourceMappingURL=stat.js.map