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 vallistFilesByPath - List files in a specific directory pathhttpEndpoint - Get HTTP endpoint URL for a filefetchTranspiledJavaScript - 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 outputisMain - Check if current file is the main entry pointUtilities for reading and managing files in Val Town Projects.
Reads the contents of a file from the current val.
Signature:
Parameters:
path - The file path to read (with or without leading slash)metaImportUrl - Optional. The import.meta.url to determine which val to read
from. Defaults to Deno.env.get("VALTOWN_ENTRYPOINT")Example:
import { readFile } from "https://esm.town/v/std/utils/index.ts";
// Most common usage - reads from current val
const content = await readFile("/README.md");
console.log(content);
// Explicit val specification
const content2 = await readFile("/README.md", import.meta.url);
console.log(content2);
Lists all files in the current val with their metadata.
Signature:
Parameters:
metaImportUrl - Optional. The import.meta.url to determine which val to list
files from. Defaults to Deno.env.get("VALTOWN_ENTRYPOINT")Example:
import { listFiles } from "https://esm.town/v/std/utils/index.ts";
// Most common usage - lists files from current val
const files = await listFiles();
files.forEach((file) => console.log(file.path));
// Explicit val specification
const files2 = await listFiles(import.meta.url);
files2.forEach((file) => console.log(file.path));
Lists files in a specific directory path within the current val.
Signature:
Parameters:
path - The directory path to list files frommetaImportUrl - Optional. The import.meta.url to determine which val to list
files from. Defaults to Deno.env.get("VALTOWN_ENTRYPOINT")Example:
import { listFilesByPath } from "https://esm.town/v/std/utils/index.ts";
// List files in a specific directory
const frontendFiles = await listFilesByPath("frontend/");
frontendFiles.forEach((file) => console.log(file.path));
Gets the HTTP endpoint URL for a specific file in the val.
Signature:
Parameters:
path - The file path to get the endpoint formetaImportUrl - Optional. The import.meta.url to determine which val the
file belongs to. Defaults to Deno.env.get("VALTOWN_ENTRYPOINT")Example:
import { httpEndpoint } from "https://esm.town/v/std/utils/index.ts";
// Get the HTTP endpoint for a specific file
const endpoint = await httpEndpoint("/api/users.ts");
console.log(endpoint); // Returns the full HTTP URL for the endpoint
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:
metaImportUrl parameters that default to
Deno.env.get("VALTOWN_ENTRYPOINT"), making them easier to use in most casesExtract 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:
Parameters:
path - The file path to servemetaImportUrl - Optional. The import.meta.url to determine which val to
serve from. Defaults to Deno.env.get("VALTOWN_ENTRYPOINT")Example:
import { serveFile } from "https://esm.town/v/std/utils/index.ts";
// In a Hono app - most common usage
app.get("/assets/*", async (c) => {
return await serveFile(c.req.path);
});
// Explicit val specification
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:
text/javascriptCreate a complete static file server with minimal configuration.
Creates a Hono-based HTTP server that serves static files from your val. The
server automatically serves /index.html for root requests and handles all
other file paths.
Signature:
Parameters:
importMetaURL - Optional. The import.meta.url to determine which val to
serve files from. Defaults to Deno.env.get("VALTOWN_ENTRYPOINT")Example:
import { staticHTTPServer } from "https://esm.town/v/std/utils/index.ts";
// Most common usage - serves files from current val
export default staticHTTPServer();
// Explicit val specification
export default staticHTTPServer(import.meta.url);
Features:
/index.html for root path requests (/)Use Cases:
Notes:
/index.html for root requestsUtility for determining if the current file is the main entry point.
Checks if the current file is the main entry point of the val. This is a replacement for import.meta.main which is not available in Val Town's environment.
Signature:
Parameters:
importMetaURL - The import.meta.url of the current fileExample:
import { isMain } from "https://esm.town/v/std/utils/index.ts";
if (isMain(import.meta.url)) {
console.log("This file is being run directly");
// Run main logic here
} else {
console.log("This file is being imported");
// Export functions/classes for use by other files
}
Use Cases:
Notes:
import.meta.url with the VALTOWN_ENTRYPOINT environment variableif __name__ == "__main__": pattern or Node.js's require.main === moduleimport.meta.main, this works reliably in Val Town's serverless environment