A comprehensive collection of utilities for building and testing applications in Val Town Projects.
readFile
- Read files from Val Town valslistFiles
- List all files in a valfetchTranspiledJavaScript
- Fetch and transpile TypeScript/JavaScriptparseVal
- Extract metadata from Val Town URLsserveFile
- Serve files with proper content typesgetContentType
- Determine MIME types for filesstaticHTTPServer
- Create a static file servertestServer
- Create a test runner with HTML/SVG output
Utilities for reading and managing files in Val Town Projects.
Reads the contents of a file from the current val.
Signature:
Example:
import { readFile } from "https://esm.town/v/std/utils/index.ts";
const content = await readFile("/README.md", import.meta.url);
console.log(content);
Lists all files in the current val with their metadata.
Signature:
Example:
import { listFiles } from "https://esm.town/v/std/utils/index.ts";
const files = await listFiles(import.meta.url);
files.forEach((file) => console.log(file.path));
Fetches and transpiles TypeScript/JavaScript code from esm.town URLs.
Signature:
Example:
import { fetchTranspiledJavaScript } from "https://esm.town/v/std/utils/index.ts";
const code = await fetchTranspiledJavaScript(
"https://esm.town/v/std/utils@85-main/index.ts",
);
console.log(code);
Notes:
- Paths can be specified with or without leading slashes
- TypeScript files are automatically transpiled to JavaScript
Extract metadata about the currently running Val.
Parses Val Town metadata from an import.meta.url.
Signature:
Example:
import { parseVal } from "https://esm.town/v/std/utils/index.ts";
const val = parseVal(import.meta.url);
console.log(val);
// Output: { owner: "username", name: "valname", ... }
Prior Art:
HTTP utilities for serving val files with proper content types.
Serves a file from the val as an HTTP Response with the correct Content-Type header.
Signature:
Example:
import { serveFile } from "https://esm.town/v/std/utils/index.ts";
// In a Hono app
app.get("/assets/*", async (c) => {
return await serveFile(c.req.path, import.meta.url);
});
Determines the appropriate MIME type for a file based on its extension.
Signature:
Example:
import { getContentType } from "https://esm.town/v/std/utils/index.ts";
console.log(getContentType("index.html")); // "text/html; charset=utf-8"
console.log(getContentType("script.ts")); // "text/javascript"
console.log(getContentType("data.json")); // "application/json; charset=utf-8"
Features:
- TypeScript/TSX/JSX files are served as
text/javascript