UNPKG

@hirosystems/clarinet-sdk-browser

Version:

A SDK to interact with Clarity Smart Contracts in the browser

63 lines 2.04 kB
export const defaultFileStore = new Map(); // @ts-ignore globalThis.fsStore = defaultFileStore; function fileArrayToString(bufferArray) { return Array.from(bufferArray) .map((item) => String.fromCharCode(item)) .join(""); } function isValidReadEvent(e) { return typeof e?.path === "string"; } function isValidReadManyEvent(e) { return Array.isArray(e?.paths) && e.paths.every((s) => typeof s === "string"); } function isValidWriteEvent(e) { return (typeof e?.path === "string" && (Array.isArray(e?.content) || e?.content instanceof Uint8Array)); } function exists(event) { if (!isValidReadEvent(event)) throw new Error("invalid read event"); return defaultFileStore.has(event.path); } function readFile(event) { if (!isValidReadEvent(event)) throw new Error("invalid read event"); const content = defaultFileStore.get(event.path) ?? null; return content; } function readFiles(event) { if (!isValidReadManyEvent(event)) throw new Error("invalid read event"); const files = event.paths.map((p) => { try { return defaultFileStore.get(p); } catch (err) { console.warn(err); return null; } }); return Object.fromEntries(files.reduce((acc, f, i) => { if (f === null || f === undefined) return acc; return acc.concat([[event.paths[i], f]]); }, [])); } function writeFile(event) { if (!isValidWriteEvent(event)) throw new Error("invalid write event"); return defaultFileStore.set(event.path, fileArrayToString(Uint8Array.from(event.content))); } export function defaultVfs(action, data) { if (action === "vfs/exists") return exists(data); if (action === "vfs/readFile") return readFile(data); if (action === "vfs/readFiles") return readFiles(data); if (action === "vfs/writeFile") return writeFile(data); throw new Error("invalid vfs action"); } //# sourceMappingURL=defaultVfs.js.map