createDenoAssetReader
Streams only build files listed in the asset manifest using Deno.open, with ETag, HEAD, 304 and Meeko's cache policy.
import { createDenoAssetReader } from "@meekojs/meeko/deno";createDenoAssetReader({ root, assets }) is Deno's public file reader: it responds only to paths in the build's asset manifest, opening the file with Deno.open and streaming it, and returns null for all others, which are never looked up on disk. Headers, HEAD, 304 and the cache policy are shared by all adapters.
Usage#
import { createServerApp } from "@meekojs/meeko/server";
import { createDenoAssetReader, serveDeno } from "@meekojs/meeko/deno";
const app = await createServerApp({
manifest, load, runtime, production: true,
assets: createDenoAssetReader({ root: new URL("./client/", import.meta.url), assets: manifest.assets ?? {} }),
});
await serveDeno(app, { handleSignals: true });Type#
function createDenoAssetReader(options: DenoAssetReaderOptions): AssetReaderinterface DenoAssetReaderOptions {
readonly root: string | URL
readonly assets: AssetManifest
}interface AssetReader {
get(pathname: string, request: Request): Promise<Response | null>
}Parameters#
| Option | Type | Description |
|---|---|---|
root |
string | URL |
Folder containing the manifest's files, as a path or file: URL. |
assets |
AssetManifest |
Public paths with their type, etag, size and immutable. |
Return value#
An AssetReader with get(pathname, request): null for a path absent from assets or a method other than GET and HEAD; 304 if If-None-Match matches, without opening the file; 200 with the file body, content-type, ETag, content-length and cache-control (immutable for one year for hashed files, max-age=0, must-revalidate for the rest).
Examples#
Restricted read permissions#
deno run --allow-net --allow-env --allow-read=.meeko/output/deno .meeko/output/deno/main.jsThe reader only opens files under root, so allowing reads of the output folder is enough.
Stream a large file#
GET /downloads/catalogo.pdf → 200, content-length: 18874368, the body is file.readableIf the client leaves, the stream is canceled and the file is closed.
Common mistakes#
Read permissions for a different folder#
deno run --allow-net --allow-env --allow-read=./public .meeko/output/deno/main.jsThe first request for a listed file fails with Deno's permission error when opening it, and the application responds with 500 and sends it to onError.
deno run --allow-net --allow-env --allow-read=.meeko/output/deno .meeko/output/deno/main.jsRun it in Node#
await createDenoAssetReader({ root, assets }).get("/robots.txt", request); // in NodeError: The Meeko Deno adapter requires the Deno runtime. In Node, use createNodeAssetReader.
const assets = createNodeAssetReader({ root: fileURLToPath(new URL("./client/", import.meta.url)), assets: manifest.assets ?? {} });Notes#
- Security. Paths are looked up in the manifest without decoding: no traversal matches, and the key comes from the build, never from the request.
- Deno only. Importing
@meekojs/meeko/denoin another runtime does not fail; reading a file does.