Created
May 9, 2019 13:56
-
-
Save ewingson/e1e6c78a50c55b736684ebc3b19b23a8 to your computer and use it in GitHub Desktop.
glob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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