@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
70 lines • 3.19 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 { stripSlash } from "../../utils/slash.js";
import { FileStat, FileType } from "../../types.js";
import { DropboxApiError } from "./client.js";
import { listFolder } from "./api.js";
import { checkPath, spelledOtherwise } from "./paths.js";
import { dropboxPathFromKey, statFromEntry } from "./readdir.js";
import { enoent } from "../../errors/fs.js";
// Each existing component must agree with the exact names of a listing.
async function statFromApi(accessor, path) {
const entry = await checkPath(accessor, path);
if (entry === null || spelledOtherwise(path, entry))
throw enoent(path.virtual);
return statFromEntry(entry);
}
/** What is at `path`, asked by its Dropbox path. Mirrors Python's `stat`. */
export async function stat(accessor, path) {
if (stripSlash(path.vfsPath) === '')
return new FileStat({ name: '/', type: FileType.DIRECTORY });
return statFromApi(accessor, path);
}
/**
* Every entry under a folder from one recursive `list_folder`, each as `stat`
* answers it from a listing. Dropbox matches paths without case, so each
* entry's place comes from its lowercased path and its name from the entry,
* as a listing of its folder names it. A folder Dropbox refuses to list (a
* missing path, a file) resolves null, so the walk's readdir reports it.
* Mirrors Python's `tree`.
*/
export async function tree(accessor, path) {
await checkPath(accessor, path);
const folder = dropboxPathFromKey(accessor.rootPath, stripSlash(path.mountPath));
let entries;
try {
entries = await listFolder(accessor.tokenManager, folder, { recursive: true });
}
catch (err) {
if (err instanceof DropboxApiError && err.status === 409)
return null;
throw err;
}
const base = folder.toLowerCase().replace(/\/+$/, '');
const depthOf = (e) => (e.path_lower ?? '').split('/').length;
const places = new Map([[base, '']]);
const found = new Map();
for (const entry of [...entries].sort((a, b) => depthOf(a) - depthOf(b))) {
const lower = entry.path_lower ?? '';
const parent = lower.slice(0, Math.max(lower.lastIndexOf('/'), 0));
const above = places.get(parent);
if (lower === base || above === undefined)
continue;
const rel = above === '' ? entry.name : `${above}/${entry.name}`;
places.set(lower, rel);
found.set(rel, statFromEntry(entry));
}
return found;
}
//# sourceMappingURL=stat.js.map