fix: stream static files from disk (#1734)
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
import type { BuildSnapshot } from "./mod.ts";
|
||||||
|
|
||||||
|
export class AotSnapshot implements BuildSnapshot {
|
||||||
|
#files: Map<string, string>;
|
||||||
|
#dependencies: Map<string, string[]>;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
files: Map<string, string>,
|
||||||
|
dependencies: Map<string, string[]>,
|
||||||
|
) {
|
||||||
|
this.#files = files;
|
||||||
|
this.#dependencies = dependencies;
|
||||||
|
}
|
||||||
|
|
||||||
|
get paths(): string[] {
|
||||||
|
return Array.from(this.#files.keys());
|
||||||
|
}
|
||||||
|
|
||||||
|
async read(path: string): Promise<ReadableStream<Uint8Array> | null> {
|
||||||
|
const filePath = this.#files.get(path);
|
||||||
|
if (filePath !== undefined) {
|
||||||
|
try {
|
||||||
|
const file = await Deno.open(filePath, { read: true });
|
||||||
|
return file.readable;
|
||||||
|
} catch (_err) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler will turn this into a 404
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies(path: string): string[] {
|
||||||
|
return this.#dependencies.get(path) ?? [];
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
-1
@@ -4,6 +4,7 @@ export {
|
|||||||
EsbuildSnapshot,
|
EsbuildSnapshot,
|
||||||
type JSXConfig,
|
type JSXConfig,
|
||||||
} from "./esbuild.ts";
|
} from "./esbuild.ts";
|
||||||
|
export { AotSnapshot } from "./aot_snapshot.ts";
|
||||||
export interface Builder {
|
export interface Builder {
|
||||||
build(): Promise<BuildSnapshot>;
|
build(): Promise<BuildSnapshot>;
|
||||||
}
|
}
|
||||||
@@ -14,7 +15,13 @@ export interface BuildSnapshot {
|
|||||||
|
|
||||||
/** For a given file, return it's contents.
|
/** For a given file, return it's contents.
|
||||||
* @throws If the file is not contained in this snapshot. */
|
* @throws If the file is not contained in this snapshot. */
|
||||||
read(path: string): ReadableStream<Uint8Array> | Uint8Array | null;
|
read(
|
||||||
|
path: string,
|
||||||
|
):
|
||||||
|
| ReadableStream<Uint8Array>
|
||||||
|
| Uint8Array
|
||||||
|
| null
|
||||||
|
| Promise<ReadableStream<Uint8Array> | Uint8Array | null>;
|
||||||
|
|
||||||
/** For a given entrypoint, return it's list of dependencies.
|
/** For a given entrypoint, return it's list of dependencies.
|
||||||
*
|
*
|
||||||
|
|||||||
+2
-2
@@ -26,8 +26,8 @@ export async function build(
|
|||||||
const snapshot = await ctx.buildSnapshot();
|
const snapshot = await ctx.buildSnapshot();
|
||||||
|
|
||||||
// Write output files to disk
|
// Write output files to disk
|
||||||
await Promise.all(snapshot.paths.map((fileName) => {
|
await Promise.all(snapshot.paths.map(async (fileName) => {
|
||||||
const data = snapshot.read(fileName);
|
const data = await snapshot.read(fileName);
|
||||||
if (data === null) return;
|
if (data === null) return;
|
||||||
|
|
||||||
return Deno.writeFile(join(outDir, fileName), data);
|
return Deno.writeFile(join(outDir, fileName), data);
|
||||||
|
|||||||
@@ -49,11 +49,11 @@ import {
|
|||||||
} from "../runtime/csp.ts";
|
} from "../runtime/csp.ts";
|
||||||
import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts";
|
import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts";
|
||||||
import {
|
import {
|
||||||
|
AotSnapshot,
|
||||||
Builder,
|
Builder,
|
||||||
BuildSnapshot,
|
BuildSnapshot,
|
||||||
BuildSnapshotJson,
|
BuildSnapshotJson,
|
||||||
EsbuildBuilder,
|
EsbuildBuilder,
|
||||||
EsbuildSnapshot,
|
|
||||||
JSXConfig,
|
JSXConfig,
|
||||||
} from "../build/mod.ts";
|
} from "../build/mod.ts";
|
||||||
import { InternalRoute } from "./router.ts";
|
import { InternalRoute } from "./router.ts";
|
||||||
@@ -184,14 +184,13 @@ export class ServerContext {
|
|||||||
Object.entries(json.files),
|
Object.entries(json.files),
|
||||||
);
|
);
|
||||||
|
|
||||||
const files = new Map();
|
const files = new Map<string, string>();
|
||||||
const names = Object.keys(json);
|
Object.keys(json.files).forEach((name) => {
|
||||||
await Promise.all(names.map(async (name) => {
|
|
||||||
const filePath = join(snapshotDirPath, name);
|
const filePath = join(snapshotDirPath, name);
|
||||||
files.set(name, await Deno.readFile(filePath));
|
files.set(name, filePath);
|
||||||
}));
|
});
|
||||||
|
|
||||||
snapshot = new EsbuildSnapshot(files, dependencies);
|
snapshot = new AotSnapshot(files, dependencies);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!(err instanceof Deno.errors.NotFound)) {
|
if (!(err instanceof Deno.errors.NotFound)) {
|
||||||
@@ -994,7 +993,7 @@ export class ServerContext {
|
|||||||
#bundleAssetRoute = (): router.MatchHandler => {
|
#bundleAssetRoute = (): router.MatchHandler => {
|
||||||
return async (_req, _ctx, params) => {
|
return async (_req, _ctx, params) => {
|
||||||
const snapshot = await this.buildSnapshot();
|
const snapshot = await this.buildSnapshot();
|
||||||
const contents = snapshot.read(params.path);
|
const contents = await snapshot.read(params.path);
|
||||||
if (!contents) return new Response(null, { status: 404 });
|
if (!contents) return new Response(null, { status: 404 });
|
||||||
|
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
|
|||||||
Reference in New Issue
Block a user