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,
|
||||
type JSXConfig,
|
||||
} from "./esbuild.ts";
|
||||
export { AotSnapshot } from "./aot_snapshot.ts";
|
||||
export interface Builder {
|
||||
build(): Promise<BuildSnapshot>;
|
||||
}
|
||||
@@ -14,7 +15,13 @@ export interface BuildSnapshot {
|
||||
|
||||
/** For a given file, return it's contents.
|
||||
* @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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user