apache-autoindex-parse
Version:
parse apache's autoindex html files
63 lines (61 loc) • 2.29 kB
JavaScript
const require_src = require('./src-DqwlaVzi.cjs');
//#region src/traverse.ts
/**
* Recursively traverses an Apache autoindex directory structure.
*
* This function fetches the HTML content from the provided URL, parses it to extract directory entries,
* and then recursively traverses any subdirectories found.
*
* @param {string} rootUrl - The URL of the Apache autoindex directory to traverse
* @param {TraverseOptions?} options - Optional configuration for the traversal process
* @returns {Promise<TraverseEntry[]>} A promise that resolves to a RootEntry object representing the directory structure, or null if parsing failed
*
* @example
* ```typescript
* import { traverse } from 'apache-autoindex-parse/traverse';
*
* const directoryStructure = await traverse('https://example.com/files');
* ```
*/
async function traverse(rootUrl, options) {
return traverseInternal(rootUrl, "", options);
}
async function traverseInternal(rootUrl, pathPrefix, options) {
try {
const res = await fetch(rootUrl, {
headers: {
"User-Agent": "github.com/luxass/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 rootEntries = require_src.parse(await res.text(), options?.format);
if (!rootEntries) return [];
return await Promise.all(rootEntries.map(async (entry) => {
let fullPath = pathPrefix ? `${pathPrefix}/${entry.path}` : entry.path;
fullPath = require_src.trimTrailingSlash(require_src.trimLeadingSlash(fullPath));
if (entry.type === "file") {
const newFileEntry = {
...entry,
path: fullPath
};
await options?.onFile?.(newFileEntry);
return newFileEntry;
}
const child = await traverseInternal(rootUrl.endsWith("/") ? rootUrl + (entry.path.startsWith("/") ? entry.path.slice(1) : entry.path) : rootUrl + (entry.path.startsWith("/") ? entry.path : `/${entry.path}`), fullPath, options);
entry.name = require_src.trimTrailingSlash(entry.name);
const dirEntry = {
...entry,
path: fullPath,
children: child
};
await options?.onDirectory?.(dirEntry);
return dirEntry;
}));
} catch {
return [];
}
}
//#endregion
exports.traverse = traverse;