Public
Val Town Utilities
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.

Standard utility library for Val Town. Provides helpers for reading files, serving static content, parsing val metadata, and testing.

📖 Full API documentation

Installation

import { readFile, serveFile, parseVal, listFiles } from "https://esm.town/v/std/utils/index.ts";

Quick start

Deploy a static site (most common)

staticHTTPServer is the fastest way to ship HTML/CSS/JS from a val. Drop an index.html (plus any assets) into your val and:

import { staticHTTPServer } from "https://esm.town/v/std/utils/index.ts"; // Serves index.html at "/", every other file by path, and a /source redirect. export default staticHTTPServer();

This automatically:

  • serves /index.html at /
  • serves any other path as a static file with the correct Content-Type
  • exposes /source, which 302-redirects to the val's source code on Val Town — so visitors can always click through to see how a page was built.

Mostly static, but with a little custom routing

Need an API route or a redirect alongside your static files? Pass a fetch handler as the second argument — it's just the web standard (request) => Response. It's tried first on every request: return a Response to handle it yourself, or return null/undefined to fall through to static serving (and /source):

import { staticHTTPServer } from "https://esm.town/v/std/utils/index.ts"; export default staticHTTPServer(import.meta.url, (req) => { const { pathname } = new URL(req.url); if (pathname === "/api/hello") return Response.json({ hello: "world" }); return null; // defer to the static server (index.html / assets / /source) });

Since it's the fetch standard, any framework's handler works too — e.g. pass Hono's app.fetch (have its notFound return null so unmatched routes fall through):

import { Hono } from "npm:hono@4.11.9"; import { staticHTTPServer } from "https://esm.town/v/std/utils/index.ts"; const app = new Hono(); app.get("/api/hello", (c) => c.json({ hello: "world" })); app.notFound(() => null as any); export default staticHTTPServer(import.meta.url, app.fetch);

Other common tasks

// Read a file from your val const content = await readFile("/README.md"); // Serve static files in a Hono app app.get("/assets/*", (c) => serveFile(c.req.path)); // Get the current val's metadata const val = parseVal(import.meta.url); console.log(val.username, val.name);

Exported functions

FunctionDescription
staticHTTPServerDeploy a static site in one line. Serves /index.html at root, assets by path with automatic immutable caching, and a /source redirect. Optionally pass a fetch handler for custom routing.
readFileRead raw file contents from your val
readFileTranspiledRead transpiled JS (types stripped) for browser use
listFilesList all files in the current val with metadata
listFilesByPathList files in a specific directory
httpEndpointGet the live HTTP URL for a file
emailAddressGet the email address for an email-trigger file
getValIdGet the current val's UUID
fetchTranspiledJavaScriptFetch transpiled JS from any esm.town URL
serveFileServe a file as an HTTP Response with correct Content-Type
serveImmutableFileServe /__immutable/<version>/<path> URLs with immutable browser caching; bare paths 302 into the current version, other versions 404
immutableFileUrlVersion-address a file path for immutable caching (/frontend/index.tsx/__immutable/45/frontend/index.tsx)
getContentTypeGet MIME type for a file path
parseValParse val metadata (username, name, version, branch, links) from import.meta.url
isMainCheck if the current file is the entrypoint
testServerCreate an HTML test runner with pass/fail badges

File overview

FileTypePurpose
index.tsscriptPublic entry point — re-exports all utilities
file.tsscriptFile I/O: read, list, transpile, endpoints, email addresses
serve-file.tsscriptHTTP file serving with MIME type detection
serve-immutable-file.tsscriptVersion-addressed file serving with immutable caching (serveImmutableFile, immutableFileUrl)
parseImportMeta.tsscriptParse import.meta.url into val metadata
is-main.tsscriptEntrypoint detection (import.meta.main replacement)
test.tsxscriptTest runner with HTML output and SVG badges
static.tsxscriptStatic file server (fetch-standard, no framework dependency)
_docs.tsxhttpAuto-generated documentation site at utilities.val.run
_tests.tshttpTest suite endpoint
*.test.tshttpPer-module test files

Development

Tests run as HTTP endpoints. Each source file has a corresponding .test.ts file. The _tests.ts file aggregates all tests and serves a badge at /badge.svg.

The _docs.tsx file uses the TypeScript compiler API to extract JSDoc from source files and render them as an HTML documentation site. Return type overrides for SDK types that can't be resolved by the in-memory compiler are hardcoded in RETURN_TYPE_OVERRIDES — these may need updating if the @valtown/sdk types change.