@cypsela/browser-source
Version:
W3C's FileSystemEntry and FileSystemHandle as a sources for @helia/unixfs
23 lines (22 loc) • 903 B
JavaScript
import { browserFsItemSource } from "./util.js";
// https://wicg.github.io/entries-api/#example-ee6569f7 in typescript
export async function* getDirEntryEntries(dirEntry) {
const reader = dirEntry.createReader();
const getNextBatch = () => new Promise((resolve, reject) => reader.readEntries(resolve, reject));
let entries;
do {
entries = await getNextBatch();
for (const entry of entries) {
yield entry;
}
} while (entries.length > 0); // loop until batch is empty
}
export async function getFileEntryFile(fileEntry) {
return new Promise((resolve, reject) => fileEntry.file(resolve, reject));
}
export function getFsEntryKind(entry) {
return entry.isDirectory ? "directory" : "file";
}
export function fsEntrySource(entry, options) {
return browserFsItemSource(entry, getDirEntryEntries, getFileEntryFile, getFsEntryKind, options);
}