apache-autoindex-parse
Version:
parse apache's autoindex html files
37 lines (34 loc) • 971 B
JavaScript
import { parse } from './chunk-PJG4LOSV.mjs';
// src/traverse.ts
async function traverse(rootUrl, options) {
try {
const res = await fetch(rootUrl, {
headers: {
"User-Agent": "github.com/apache-autoindex-parse",
...options?.extraHeaders
},
signal: options?.abortSignal
});
if (!res.ok) {
throw new Error(`failed to fetch directory listing from ${rootUrl}: ${res.status} ${res.statusText}`);
}
const html = await res.text();
const root = parse(html, options?.format);
if (!root) return null;
await Promise.all(
root.children.map(async (entry) => {
if (entry.type === "directory") {
const childUrl = new URL(entry.path, rootUrl).href;
const child = await traverse(childUrl, options);
if (child) {
entry.children = child.children;
}
}
})
);
return root;
} catch {
return null;
}
}
export { traverse };