Readme

Serve prefixed blobs.

Usage

Create valimport { serveBlobs } from "https://esm.town/v/pomdtr/serve_blobs" export default serveBlobs({ root: "public/" })

All your blobs prefixed by public/ will be publicly accessible.

Ex: Go to https://pomdtr-public.web.val.run/example.json to view the blob public/example.json from my account.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as path from "jsr:@std/path";
import { blob } from "https://esm.town/v/std/blob?v=12";
import { ValTownBlobNotFoundError } from "https://esm.town/v/std/ValTownBlobNotFoundError";
import mime from "npm:mime";
export function serveBlobs({
root,
mimes = {},
}: {
root: string;
mimes?: Record<string, string>;
}) {
return async (req: Request) => {
const url = new URL(req.url);
const key = path.join(
root,
url.pathname == "/" ? "index.html" : url.pathname
);
try {
const res = await blob.get(key);
const extension = key.split(".").pop();
return new Response(res.body, {
headers: {
"Content-Type": mimes[extension] || mime.getType(key),
},
});
} catch (e) {
if (e instanceof ValTownBlobNotFoundError) {
return new Response(null, {
status: 404,
});
}
return new Response(null, {
status: 500,
});
}
};
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.