This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
38 additions
and
19 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
contentType, | ||
lookup, | ||
} from "https://deno.land/x/media_types@v2.9.0/mod.ts"; | ||
|
||
import type { RequestEvent } from "../types.d.ts"; | ||
|
||
/** | ||
* A fetch handler that returns local files that are requested by the worker. | ||
* | ||
* This is useful when the worker is attempting to access static assets from | ||
* its repo using `import.meta.url` as the base for a fetch request. | ||
*/ | ||
export async function fileFetchHandler(evt: RequestEvent) { | ||
const url = new URL(evt.request.url); | ||
if (url.protocol === "file:") { | ||
const stat = await Deno.stat(url); | ||
let response: Response; | ||
if (stat.isFile) { | ||
const ct = contentType(lookup(evt.request.url) ?? "") ?? "text/plain"; | ||
const body = await Deno.readFile(url); | ||
response = new Response(body, { | ||
headers: { | ||
"content-type": ct, | ||
}, | ||
}); | ||
} else { | ||
response = new Response(null, { | ||
status: 404, | ||
statusText: "Not Found", | ||
}); | ||
} | ||
evt.respondWith(response); | ||
} | ||
} |
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