Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewingson/e1e6c78a50c55b736684ebc3b19b23a8 to your computer and use it in GitHub Desktop.
Save ewingson/e1e6c78a50c55b736684ebc3b19b23a8 to your computer and use it in GitHub Desktop.
glob
import { Request, Response } from "@opennetwork/http-representation";
import { getType, notAccepted } from "./combine";
import { Fetcher } from "./fetcher";
import { readdir } from "./readdir";
import { combineForResponse } from "./combine";
export type SimpleGlobOptions = {
fetch: Fetcher
};
export async function simpleGlob(request: Request, options: SimpleGlobOptions): Promise<Response> {
const url = new URL(request.url);
if (!url.pathname.endsWith("*")) {
return undefined;
}
const possibleRDFType = getType(request);
if (!possibleRDFType) {
return notAccepted();
}
const directory = url.pathname.substr(0, url.pathname.lastIndexOf("/") + 1);
const allEntries = await readdir(request, directory, options.fetch);
const base = url.pathname.substr(0, directory.length).replace(/\*$/, "").toLowerCase();
const matchedEntries = base ? (
allEntries
.filter(entry => entry.toLowerCase().startsWith(base))
) : allEntries; // If no base, then we must be just asking for "*", aka, everything, so bypass the filter
return combineForResponse(request, matchedEntries, options.fetch);
}
export function createSimpleGlob(options: SimpleGlobOptions): Fetcher {
return (request: Request) => simpleGlob(request, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment