-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
test-adapter.js
101 lines (95 loc) · 2.49 KB
/
test-adapter.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { viteID } from '../dist/core/util.js';
/**
*
* @returns {import('../src/@types/astro').AstroIntegration}
*/
export default function (
{
provideAddress = true,
extendAdapter,
setEntryPoints = undefined,
setMiddlewareEntryPoint = undefined,
setRoutes = undefined,
} = {
provideAddress: true,
}
) {
return {
name: 'my-ssr-adapter',
hooks: {
'astro:config:setup': ({ updateConfig }) => {
updateConfig({
vite: {
plugins: [
{
resolveId(id) {
if (id === '@my-ssr') {
return id;
} else if (id === 'astro/app') {
const viteId = viteID(new URL('../dist/core/app/index.js', import.meta.url));
return viteId;
}
},
load(id) {
if (id === '@my-ssr') {
return `
import { App } from 'astro/app';
import fs from 'fs';
class MyApp extends App {
#manifest = null;
constructor(manifest, streaming) {
super(manifest, streaming);
this.#manifest = manifest;
}
async render(request, routeData, locals) {
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);
}
${provideAddress ? `request[Symbol.for('astro.clientAddress')] = '0.0.0.0';` : ''}
return super.render(request, routeData, locals);
}
}
export function createExports(manifest) {
return {
manifest,
createApp: (streaming) => new MyApp(manifest, streaming)
};
}
`;
}
},
},
],
},
});
},
'astro:config:done': ({ setAdapter }) => {
setAdapter({
name: 'my-ssr-adapter',
serverEntrypoint: '@my-ssr',
exports: ['manifest', 'createApp'],
supportedAstroFeatures: {
serverOutput: "stable"
},
...extendAdapter,
});
},
'astro:build:ssr': ({ entryPoints, middlewareEntryPoint }) => {
if (setEntryPoints) {
setEntryPoints(entryPoints);
}
if (setMiddlewareEntryPoint) {
setMiddlewareEntryPoint(middlewareEntryPoint);
}
},
'astro:build:done': ({ routes }) => {
if (setRoutes) {
setRoutes(routes);
}
},
},
};
}