-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.ts
162 lines (142 loc) · 4.53 KB
/
index.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import type { Fetch } from "@marko/run";
import baseAdapter from "@marko/run/adapter";
import type { Adapter, AdapterConfig, Route } from "@marko/run/vite";
import { getAvailablePort, loadEnv, spawnServer } from "@marko/run/vite";
import fs from "fs/promises";
import { createServer } from "http";
import type { AddressInfo } from "net";
import path from "path";
import createStaticServe from "serve-static";
import { Pool } from "undici";
import { fileURLToPath } from "url";
import createCrawler from "./crawler";
const __dirname = fileURLToPath(path.dirname(import.meta.url));
const defaultEntry = path.join(__dirname, "default-entry");
export interface Options {
urls?: string[] | ((routes: Route[]) => string[] | Promise<string[]>);
}
function noop() {}
export default function staticAdapter(options: Options = {}): Adapter {
const { startDev } = baseAdapter();
let adapterConfig!: AdapterConfig;
return {
name: "static-adapter",
configure(config) {
adapterConfig = config;
},
async pluginOptions() {
return {
trailingSlashes: "RedirectWith",
};
},
async getEntryFile() {
return defaultEntry;
},
startDev(entry, config, options) {
return startDev!(
entry === defaultEntry ? undefined : entry,
config,
options,
);
},
async startPreview(_entry, options) {
const { dir, port, envFile } = options;
envFile && (await loadEnv(envFile));
const staticServe = createStaticServe(path.join(dir, "public"), {
index: "index.html",
});
const server = await createServer((req, res) =>
staticServe(req, res, noop),
);
return new Promise((resolve) => {
const listener = server.listen(port, () => {
const address = listener.address() as AddressInfo;
console.log(
`Preview server started: http://localhost:${address.port}`,
);
resolve({
port: address.port,
close() {
listener.close();
},
});
});
});
},
async buildEnd(_config, routes, builtEntries, sourceEntries) {
const { envFile } = adapterConfig;
const pathsToVisit: string[] = [];
for (const route of routes) {
for (const path of route.paths) {
if (!path.params || !Object.keys(path.params).length) {
pathsToVisit.push(path.path);
}
}
}
if (typeof options.urls === "function") {
pathsToVisit.push(...(await options.urls(routes)));
} else if (options.urls) {
pathsToVisit.push(...options.urls);
}
const defaultEntry = await this.getEntryFile!();
if (sourceEntries[0] === defaultEntry) {
envFile && (await loadEnv(envFile));
const fetch: Fetch = (await import(builtEntries[0])).fetch;
const crawler = createCrawler(
async (request) => {
const response = await fetch(request, {});
return response || new Response(null, { status: 404 });
},
{
out: path.join(path.dirname(builtEntries[0]), "public"),
},
);
await crawler.crawl(pathsToVisit);
} else {
const port = await getAvailablePort();
const origin = `http://localhost:${port}`;
const client = new Pool(origin);
const server = await spawnServer(
`node ${builtEntries[0]}`,
[],
port,
envFile,
);
const crawler = createCrawler(
async (request) => {
const url = new URL(request.url, origin);
const headers: Record<string, string> = {};
request.headers.forEach((value, key) => {
headers[key] = value;
});
const responseData = await client.request({
path: url.pathname + url.search,
method: request.method as any,
signal: request.signal,
headers,
});
return new Response(responseData.body as any, {
status: responseData.statusCode,
headers: responseData.headers as Record<string, string>,
});
},
{
origin,
},
);
try {
await crawler.crawl(pathsToVisit);
} finally {
await client.close();
server.close();
}
}
for (const file of builtEntries) {
await fs.rm(file, { maxRetries: 5 }).catch(() => {});
}
},
typeInfo() {
return "{}";
},
};
}