@marko/run 0.0.1-beta1 → 0.0.1-beta3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter/default-entry.mjs +9 -3
- package/dist/adapter/dev-server.d.ts +1 -1
- package/dist/adapter/index.cjs +23 -8
- package/dist/adapter/index.js +22 -7
- package/dist/adapter/middleware.cjs +200 -0
- package/dist/adapter/middleware.d.ts +59 -0
- package/dist/adapter/middleware.js +169 -0
- package/dist/cli/default.config.mjs +1 -1
- package/dist/cli/index.mjs +45 -24
- package/dist/runtime/index.cjs +0 -16
- package/dist/runtime/index.d.ts +10 -2
- package/dist/runtime/index.js +0 -7
- package/dist/runtime/internal.cjs +148 -0
- package/dist/runtime/internal.d.ts +10 -0
- package/dist/runtime/internal.js +115 -0
- package/dist/runtime/router.cjs +6 -6
- package/dist/runtime/router.d.ts +4 -4
- package/dist/runtime/router.js +4 -4
- package/dist/runtime/types.d.ts +31 -16
- package/dist/runtime/utils.d.ts +3 -0
- package/dist/vite/codegen/index.d.ts +4 -3
- package/dist/vite/codegen/writer.d.ts +1 -1
- package/dist/vite/constants.d.ts +3 -2
- package/dist/vite/index.cjs +542 -275
- package/dist/vite/index.d.ts +4 -3
- package/dist/vite/index.js +539 -275
- package/dist/vite/types.d.ts +9 -7
- package/dist/vite/utils/config.d.ts +2 -2
- package/dist/vite/utils/server.d.ts +3 -1
- package/package.json +18 -6
- package/dist/adapter/server-old.d.ts +0 -3
- package/dist/adapter/server.d.ts +0 -6
- package/dist/adapters/node/index.d.ts +0 -5
- package/dist/adapters/node/server.d.ts +0 -3
- package/dist/adapters/static/crawler.d.ts +0 -9
- package/dist/adapters/static/default-entry.mjs +0 -1
- package/dist/adapters/static/index.cjs +0 -371
- package/dist/adapters/static/index.d.ts +0 -5
- package/dist/adapters/static/index.js +0 -341
- package/dist/adapters/static/server.d.ts +0 -3
- package/dist/runtime/request.d.ts +0 -4
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
// src/adapters/static/index.ts
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
|
-
import { Pool } from "undici";
|
|
5
|
-
|
|
6
|
-
// src/adapters/static/crawler.ts
|
|
7
|
-
import fs from "fs";
|
|
8
|
-
import nodePath from "path";
|
|
9
|
-
import { WritableStream as Parser } from "htmlparser2/lib/WritableStream";
|
|
10
|
-
var noop = () => {
|
|
11
|
-
};
|
|
12
|
-
var ignoredRels = /* @__PURE__ */ new Set(["nofollow", "enclosure", "external"]);
|
|
13
|
-
var contentType = "text/html";
|
|
14
|
-
function createCrawler(makeRequest, opts = {}) {
|
|
15
|
-
const origin = opts.origin || `http://localhost`;
|
|
16
|
-
const out = nodePath.resolve(opts.out || "dist");
|
|
17
|
-
const notFoundPath = resolvePath(opts.notFoundPath || "/404/", origin);
|
|
18
|
-
let seen;
|
|
19
|
-
let queue;
|
|
20
|
-
let pending;
|
|
21
|
-
async function visit(path2) {
|
|
22
|
-
var _a, _b;
|
|
23
|
-
const parser = new Parser({
|
|
24
|
-
onopentag(name, attrs) {
|
|
25
|
-
const href = resolveHref(name, attrs);
|
|
26
|
-
const path3 = href && resolvePath(href, origin);
|
|
27
|
-
if (path3 !== void 0 && !seen.has(path3)) {
|
|
28
|
-
seen.add(path3);
|
|
29
|
-
queue.push(visit(path3));
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
const dirname = nodePath.join(out, path2);
|
|
34
|
-
const abortController = new AbortController();
|
|
35
|
-
let fsWriter;
|
|
36
|
-
try {
|
|
37
|
-
const url = new URL(path2, origin);
|
|
38
|
-
const req = new Request(url, {
|
|
39
|
-
method: "GET",
|
|
40
|
-
signal: abortController.signal,
|
|
41
|
-
headers: { accept: contentType }
|
|
42
|
-
});
|
|
43
|
-
const res = await makeRequest(req);
|
|
44
|
-
if (!((_a = res.headers.get("content-type")) == null ? void 0 : _a.includes(contentType))) {
|
|
45
|
-
abortController.abort();
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
let redirect;
|
|
49
|
-
switch (res.status) {
|
|
50
|
-
case 200:
|
|
51
|
-
break;
|
|
52
|
-
case 404:
|
|
53
|
-
if (path2 !== notFoundPath) {
|
|
54
|
-
redirect = origin + notFoundPath;
|
|
55
|
-
}
|
|
56
|
-
break;
|
|
57
|
-
case 301: {
|
|
58
|
-
redirect = res.headers.get("location");
|
|
59
|
-
const redirectPath = resolvePath(redirect, origin);
|
|
60
|
-
if (redirectPath && !seen.has(redirectPath)) {
|
|
61
|
-
seen.add(redirectPath);
|
|
62
|
-
queue.push(visit(redirectPath));
|
|
63
|
-
}
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
default: {
|
|
67
|
-
abortController.abort();
|
|
68
|
-
console.warn(`Status code ${res.status} was while crawling: '${path2}'`);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
await fs.promises.mkdir(dirname, { recursive: true }).catch(noop);
|
|
73
|
-
fsWriter = fs.createWriteStream(nodePath.join(dirname, "index.html"));
|
|
74
|
-
if (redirect) {
|
|
75
|
-
abortController.abort();
|
|
76
|
-
fsWriter.write(
|
|
77
|
-
`<!DOCTYPE html><meta http-equiv=Refresh content="0;url=${redirect.replace(
|
|
78
|
-
/"/g,
|
|
79
|
-
"("
|
|
80
|
-
)}">`
|
|
81
|
-
);
|
|
82
|
-
} else {
|
|
83
|
-
const writable = new WritableStream({
|
|
84
|
-
write(data) {
|
|
85
|
-
fsWriter.write(data);
|
|
86
|
-
parser.write(data);
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
await ((_b = res.body) == null ? void 0 : _b.pipeTo(writable));
|
|
90
|
-
}
|
|
91
|
-
} finally {
|
|
92
|
-
fsWriter == null ? void 0 : fsWriter.end();
|
|
93
|
-
parser.end();
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return {
|
|
97
|
-
async crawl(paths = ["/"]) {
|
|
98
|
-
if (pending) {
|
|
99
|
-
await pending;
|
|
100
|
-
}
|
|
101
|
-
const startPaths = paths.map((path2) => resolvePath(path2, origin)).concat(notFoundPath);
|
|
102
|
-
seen = new Set(startPaths);
|
|
103
|
-
try {
|
|
104
|
-
queue = startPaths.map(visit);
|
|
105
|
-
while (queue.length) {
|
|
106
|
-
pending = Promise.all(queue);
|
|
107
|
-
queue = [];
|
|
108
|
-
await pending;
|
|
109
|
-
}
|
|
110
|
-
} finally {
|
|
111
|
-
pending = void 0;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
function resolveHref(tagName, attrs) {
|
|
117
|
-
switch (tagName) {
|
|
118
|
-
case "a":
|
|
119
|
-
if (attrs.href && !(attrs.download || ignoredRels.has(attrs.rel))) {
|
|
120
|
-
return attrs.href;
|
|
121
|
-
}
|
|
122
|
-
break;
|
|
123
|
-
case "link":
|
|
124
|
-
if (attrs.href) {
|
|
125
|
-
switch (attrs.rel) {
|
|
126
|
-
case "alternate":
|
|
127
|
-
case "author":
|
|
128
|
-
case "canonical":
|
|
129
|
-
case "help":
|
|
130
|
-
case "license":
|
|
131
|
-
case "next":
|
|
132
|
-
case "prefetch":
|
|
133
|
-
case "prerender":
|
|
134
|
-
case "prev":
|
|
135
|
-
case "search":
|
|
136
|
-
case "tag":
|
|
137
|
-
return attrs.href;
|
|
138
|
-
case "preload":
|
|
139
|
-
if (attrs.as === "document") {
|
|
140
|
-
return attrs.href;
|
|
141
|
-
}
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
break;
|
|
146
|
-
case "iframe":
|
|
147
|
-
return attrs.src;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
function resolvePath(href, origin) {
|
|
151
|
-
const url = new URL(href, origin);
|
|
152
|
-
if (url.origin === origin) {
|
|
153
|
-
let { pathname } = url;
|
|
154
|
-
const lastChar = pathname.length - 1;
|
|
155
|
-
if (pathname[lastChar] !== "/") {
|
|
156
|
-
pathname += "/";
|
|
157
|
-
}
|
|
158
|
-
return pathname + url.search;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// src/adapters/static/index.ts
|
|
163
|
-
import fs2 from "fs/promises";
|
|
164
|
-
|
|
165
|
-
// src/vite/utils/server.ts
|
|
166
|
-
import net from "net";
|
|
167
|
-
import cp from "child_process";
|
|
168
|
-
async function spawnServer(cmd, port = 0, wait = 3e4) {
|
|
169
|
-
if (port <= 0) {
|
|
170
|
-
port = await getAvailablePort();
|
|
171
|
-
}
|
|
172
|
-
const proc = cp.spawn(cmd, {
|
|
173
|
-
shell: true,
|
|
174
|
-
stdio: "inherit",
|
|
175
|
-
windowsHide: true,
|
|
176
|
-
env: { ...process.env, PORT: `${port}` }
|
|
177
|
-
});
|
|
178
|
-
const close = () => {
|
|
179
|
-
proc.unref();
|
|
180
|
-
proc.kill();
|
|
181
|
-
};
|
|
182
|
-
let remaining = wait > 0 ? wait : Infinity;
|
|
183
|
-
while (!await isPortInUse(port)) {
|
|
184
|
-
if (remaining >= 100) {
|
|
185
|
-
remaining -= 100;
|
|
186
|
-
await sleep(100);
|
|
187
|
-
} else {
|
|
188
|
-
close();
|
|
189
|
-
throw new Error(
|
|
190
|
-
`site-write: timeout while wating for server to start on port "${port}".`
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
return close;
|
|
195
|
-
}
|
|
196
|
-
async function isPortInUse(port) {
|
|
197
|
-
return new Promise((resolve) => {
|
|
198
|
-
const connection = net.connect(port).setNoDelay(true).setKeepAlive(false).on("error", () => done(false)).on("connect", () => done(true));
|
|
199
|
-
function done(connected) {
|
|
200
|
-
connection.end();
|
|
201
|
-
resolve(connected);
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
async function getAvailablePort() {
|
|
206
|
-
return new Promise((resolve) => {
|
|
207
|
-
const server = net.createServer().listen(0, () => {
|
|
208
|
-
const { port } = server.address();
|
|
209
|
-
server.close(() => resolve(port));
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
function sleep(ms) {
|
|
214
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// src/adapters/static/server.ts
|
|
218
|
-
import createStaticServe from "serve-static";
|
|
219
|
-
import { createServer as createHttpServer } from "http";
|
|
220
|
-
import { createServer as createDevServer } from "vite";
|
|
221
|
-
import { createMiddleware } from "@hattip/adapter-node";
|
|
222
|
-
function noop2() {
|
|
223
|
-
}
|
|
224
|
-
async function server_default(dir) {
|
|
225
|
-
if (dir) {
|
|
226
|
-
const staticServe = createStaticServe(dir, {
|
|
227
|
-
index: "index.html",
|
|
228
|
-
immutable: true,
|
|
229
|
-
maxAge: "365 days"
|
|
230
|
-
});
|
|
231
|
-
return await createHttpServer((req, res) => staticServe(req, res, noop2));
|
|
232
|
-
}
|
|
233
|
-
const devServer = await createDevServer({
|
|
234
|
-
appType: "custom",
|
|
235
|
-
server: { middlewareMode: true }
|
|
236
|
-
});
|
|
237
|
-
let handler;
|
|
238
|
-
let middleware;
|
|
239
|
-
return devServer.middlewares.use(async (req, res, next) => {
|
|
240
|
-
try {
|
|
241
|
-
const module = await devServer.ssrLoadModule("@marko/run");
|
|
242
|
-
if (module.handler !== handler) {
|
|
243
|
-
handler = module.handler;
|
|
244
|
-
middleware = createMiddleware(handler);
|
|
245
|
-
}
|
|
246
|
-
await middleware(req, res, next);
|
|
247
|
-
} catch (err) {
|
|
248
|
-
if (err instanceof Error) {
|
|
249
|
-
devServer.ssrFixStacktrace(err);
|
|
250
|
-
}
|
|
251
|
-
return next(err);
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// src/adapters/static/index.ts
|
|
257
|
-
var __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
258
|
-
function staticAdapter(_options = {}) {
|
|
259
|
-
return {
|
|
260
|
-
name: "static-adapter",
|
|
261
|
-
async config(_options2) {
|
|
262
|
-
return {
|
|
263
|
-
codegen: {
|
|
264
|
-
trailingSlashes: "RedirectWith"
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
},
|
|
268
|
-
async getEntryFile() {
|
|
269
|
-
return path.join(__dirname, "default-entry");
|
|
270
|
-
},
|
|
271
|
-
async startServer(port, dir) {
|
|
272
|
-
const server = await server_default(dir);
|
|
273
|
-
server.on("error", (err) => {
|
|
274
|
-
console.error(err);
|
|
275
|
-
process.exit(1);
|
|
276
|
-
});
|
|
277
|
-
return new Promise((resolve) => {
|
|
278
|
-
const listener = server.listen(port, () => {
|
|
279
|
-
const address = listener.address();
|
|
280
|
-
console.log(`Server started: http://localhost:${address.port}`);
|
|
281
|
-
resolve();
|
|
282
|
-
});
|
|
283
|
-
});
|
|
284
|
-
},
|
|
285
|
-
async buildEnd(routes, builtEntries, sourceEntries) {
|
|
286
|
-
var _a;
|
|
287
|
-
const pathsToVisit = [];
|
|
288
|
-
for (const route of routes) {
|
|
289
|
-
if (!((_a = route.params) == null ? void 0 : _a.length)) {
|
|
290
|
-
pathsToVisit.push(route.path);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
const defaultEntry = await this.getEntryFile();
|
|
294
|
-
if (sourceEntries[0] === defaultEntry) {
|
|
295
|
-
const { router } = await import(builtEntries[0]);
|
|
296
|
-
const crawler = createCrawler(router, {});
|
|
297
|
-
await crawler.crawl(pathsToVisit);
|
|
298
|
-
} else {
|
|
299
|
-
const port = await getAvailablePort();
|
|
300
|
-
const origin = `http://localhost:${port}`;
|
|
301
|
-
const client = new Pool(origin);
|
|
302
|
-
const closeServer = await spawnServer(`node ${builtEntries[0]}`, port);
|
|
303
|
-
const crawler = createCrawler(
|
|
304
|
-
async (request) => {
|
|
305
|
-
const url = new URL(request.url);
|
|
306
|
-
const headers = {};
|
|
307
|
-
request.headers.forEach((value, key) => {
|
|
308
|
-
headers[key] = value;
|
|
309
|
-
});
|
|
310
|
-
const responseData = await client.request({
|
|
311
|
-
path: url.pathname + url.search,
|
|
312
|
-
method: request.method,
|
|
313
|
-
signal: request.signal,
|
|
314
|
-
headers
|
|
315
|
-
});
|
|
316
|
-
return new Response(responseData.body, {
|
|
317
|
-
status: responseData.statusCode,
|
|
318
|
-
headers: responseData.headers
|
|
319
|
-
});
|
|
320
|
-
},
|
|
321
|
-
{
|
|
322
|
-
origin
|
|
323
|
-
}
|
|
324
|
-
);
|
|
325
|
-
try {
|
|
326
|
-
await crawler.crawl(pathsToVisit);
|
|
327
|
-
} finally {
|
|
328
|
-
await client.close();
|
|
329
|
-
closeServer();
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
for (const file of builtEntries) {
|
|
333
|
-
await fs2.rm(file, { maxRetries: 5 }).catch(() => {
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
export {
|
|
340
|
-
staticAdapter as default
|
|
341
|
-
};
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import type { ServerResponse, IncomingMessage } from "http";
|
|
3
|
-
export declare function createWebRequest<NodeRequest extends IncomingMessage = IncomingMessage>(nodeRequest: NodeRequest, url: URL): Request;
|
|
4
|
-
export declare function applyWebResponse<NodeRequest extends IncomingMessage = IncomingMessage>(nodeResponse: ServerResponse<NodeRequest>, webResponse: Response): Promise<void>;
|