summaryrefslogtreecommitdiff
path: root/benchmark/packages/adapter/src/server.ts
blob: ca69fe28f26228f069f99607a9f86666f21316b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import * as fs from 'node:fs';
import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import { applyPolyfills } from 'astro/app/node';

applyPolyfills();

class MyApp extends App {
	#manifest: SSRManifest | undefined;
	#streaming: boolean;
	constructor(manifest: SSRManifest, streaming = false) {
		super(manifest, streaming);
		this.#manifest = manifest;
		this.#streaming = streaming;
	}

	async render(request: Request) {
		const url = new URL(request.url);
		if (this.#manifest?.assets.has(url.pathname)) {
			const filePath = new URL('../../client/' + this.removeBase(url.pathname), import.meta.url);
			const data = await fs.promises.readFile(filePath);
			return new Response(data);
		}

		return super.render(request);
	}
}

export function createExports(manifest: SSRManifest) {
	return {
		manifest,
		createApp: (streaming: boolean) => new MyApp(manifest, streaming),
	};
}