@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
59 lines • 2.57 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 { apiRequest, apiStream } from "../api/client.js";
import { fileIdName } from "../../utils/naming.js";
import { windowFor } from "../../utils/ranges.js";
import { ApiError } from "../api/errors.js";
/**
* Whether a day's `files` directory lists an attachment.
*
* Tombstoned (deleted) and access-restricted attachment payloads carry an id
* but no download URL and no byte size; read() ENOENTs on them, so listing
* them would surface phantom files with unknown sizes. Mirrors the slack guard.
*/
export function isListed(att) {
return Boolean(att.id) && Boolean(att.url) && att.size !== undefined && att.size !== null;
}
/**
* Construct a stable VFS filename for a Discord attachment, of shape
* `<stem>__<att-id>.<ext>`, named after `filename` and else `title` (see
* `fileIdName`).
*/
export function fileBlobName(att) {
return fileIdName(att.id, att.filename, att.title);
}
function downloadError(response) {
return new ApiError(`discord download_file failed: ${String(response.status)} ${response.statusText}`, response.status);
}
// Passes the window rather than a prepared header so the answer can be checked
// against it: a CDN is free to ignore Range and reply 200 with the whole file,
// which apiRequest's byte reader then trims.
export async function downloadFile(url, offset = 0, size = null, timeout) {
const window = windowFor(offset, size);
return (await apiRequest('GET', url, {
errorOf: downloadError,
timeout,
read: 'bytes',
...(window !== undefined ? { window } : {}),
}));
}
/** Stream an attachment with bounded reads and response cleanup. */
export function downloadFileStream(url, signal, timeout) {
return apiStream(url, {
errorOf: downloadError,
timeout,
...(signal === undefined ? {} : { signal }),
});
}
//# sourceMappingURL=files.js.map