@cypsela/browser-source
Version:
W3C's FileSystemEntry and FileSystemHandle as a sources for @helia/unixfs
50 lines (49 loc) • 1.56 kB
JavaScript
import blobToIt from "blob-to-it";
// https://github.com/ipfs/helia/blob/28a7091260fda1f711b93318084a65ff3d2f3f8a/packages/unixfs/src/utils/to-mtime.ts#L47-L52
function msToMtime(ms) {
const secs = Math.floor(ms / 1000);
return {
secs: BigInt(secs),
nsecs: (ms - secs * 1000) * 1000,
};
}
function isDirectory(item, getKind) {
return getKind(item) === "directory";
}
function isFile(item, getKind) {
return getKind(item) === "file";
}
export async function* browserFsItemSource(item, getEntries, getFile, getKind, options = {}) {
if (options.hidden === false && item.name.startsWith(".")) {
return;
}
const path = (options.prefix ?? "") + item.name;
if (isDirectory(item, getKind)) {
if (!options.onlyFiles) {
yield {
path,
content: undefined,
mode: options.mode,
mtime: options.mtime,
};
}
for await (const entry of getEntries(item)) {
options.prefix = path + "/";
yield* browserFsItemSource(entry, getEntries, getFile, getKind, options);
}
return;
}
if (isFile(item, getKind)) {
const file = await getFile(item);
yield {
path,
content: blobToIt(file),
mode: options.mode,
mtime: options.preserveMtime
? msToMtime(file.lastModified)
: options.mtime,
};
return;
}
throw new TypeError("Unsupported filesystem kind.");
}