@marko/run 0.0.1-beta7 → 0.0.1-beta9
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/README.md +306 -134
- package/dist/.tsbuildinfo +1 -0
- package/dist/adapter/dev-server.d.ts +1 -1
- package/dist/adapter/index.cjs +13 -173
- package/dist/adapter/index.js +13 -173
- package/dist/adapter/middleware.cjs +46 -8
- package/dist/adapter/middleware.d.ts +3 -3
- package/dist/adapter/middleware.js +46 -8
- package/dist/adapter/polyfill.d.ts +5 -0
- package/dist/cli/default.config.mjs +2 -2
- package/dist/cli/index.mjs +2 -2
- package/dist/runtime/index.d.ts +8 -8
- package/dist/runtime/internal.cjs +11 -2
- package/dist/runtime/internal.d.ts +6 -5
- package/dist/runtime/internal.js +9 -1
- package/dist/runtime/types.d.ts +3 -3
- package/dist/vite/index.cjs +24 -35
- package/dist/vite/index.js +24 -35
- package/package.json +19 -9
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { ViteDevServer } from "vite";
|
|
2
|
-
import {
|
|
2
|
+
import type { NodeMiddleware } from "./middleware";
|
|
3
3
|
export declare function createViteDevMiddleware<T>(devServer: ViteDevServer, load: (prev: T | undefined) => Promise<T>, factory: (value: T) => NodeMiddleware): NodeMiddleware;
|
|
4
4
|
export declare function createDevServer(configFile?: string): Promise<import("vite").Connect.Server>;
|
package/dist/adapter/index.cjs
CHANGED
|
@@ -36,176 +36,6 @@ var import_url = require("url");
|
|
|
36
36
|
|
|
37
37
|
// src/adapter/dev-server.ts
|
|
38
38
|
var import_vite = require("vite");
|
|
39
|
-
|
|
40
|
-
// src/adapter/polyfill.ts
|
|
41
|
-
var import_web = require("stream/web");
|
|
42
|
-
var import_crypto = require("crypto");
|
|
43
|
-
var import_undici = require("undici");
|
|
44
|
-
var globals = {
|
|
45
|
-
crypto: import_crypto.webcrypto,
|
|
46
|
-
fetch: import_undici.fetch,
|
|
47
|
-
Response: import_undici.Response,
|
|
48
|
-
Request: import_undici.Request,
|
|
49
|
-
Headers: import_undici.Headers,
|
|
50
|
-
ReadableStream: import_web.ReadableStream,
|
|
51
|
-
TransformStream: import_web.TransformStream,
|
|
52
|
-
WritableStream: import_web.WritableStream,
|
|
53
|
-
FormData: import_undici.FormData,
|
|
54
|
-
File: import_undici.File
|
|
55
|
-
};
|
|
56
|
-
function installPolyfills() {
|
|
57
|
-
for (const name in globals) {
|
|
58
|
-
Object.defineProperty(globalThis, name, {
|
|
59
|
-
enumerable: true,
|
|
60
|
-
configurable: true,
|
|
61
|
-
writable: true,
|
|
62
|
-
value: globals[name]
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// src/adapter/middleware.ts
|
|
68
|
-
installPolyfills();
|
|
69
|
-
function getForwardedHeader(req, name) {
|
|
70
|
-
const value = req.headers["x-forwarded-" + name];
|
|
71
|
-
if (value) {
|
|
72
|
-
if (typeof value === "string") {
|
|
73
|
-
const index = value.indexOf(",");
|
|
74
|
-
return index < 0 ? value : value.slice(0, index);
|
|
75
|
-
}
|
|
76
|
-
return value[0];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
function getOrigin(req, protocol, host, trustProxy) {
|
|
80
|
-
var _a;
|
|
81
|
-
protocol ?? (protocol = req.protocol || trustProxy && getForwardedHeader(req, "proto") || ((_a = req.socket) == null ? void 0 : _a.encrypted) && "https" || "http");
|
|
82
|
-
host ?? (host = trustProxy && getForwardedHeader(req, "host") || req.headers.host);
|
|
83
|
-
if (!host) {
|
|
84
|
-
if (process.env.NODE_ENV !== "production") {
|
|
85
|
-
host = "localhost";
|
|
86
|
-
console.warn(
|
|
87
|
-
`Could not automatically determine the origin host, using 'localhost'. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
|
|
88
|
-
);
|
|
89
|
-
} else {
|
|
90
|
-
throw new Error(
|
|
91
|
-
`Could not automatically determine the origin host. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return `${protocol}://${host}`;
|
|
96
|
-
}
|
|
97
|
-
function createMiddleware(fetch2, options = {}) {
|
|
98
|
-
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
99
|
-
let { origin = process.env.ORIGIN } = options;
|
|
100
|
-
let protocol;
|
|
101
|
-
let host;
|
|
102
|
-
if (origin) {
|
|
103
|
-
({ protocol, host } = new URL(origin));
|
|
104
|
-
protocol = protocol.slice(0, -1);
|
|
105
|
-
}
|
|
106
|
-
return async (req, res, next) => {
|
|
107
|
-
origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
|
|
108
|
-
const url = new URL(req.url, origin);
|
|
109
|
-
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
110
|
-
const headers = req.headers;
|
|
111
|
-
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
112
|
-
start(controller) {
|
|
113
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
114
|
-
req.on("end", () => controller.close());
|
|
115
|
-
req.on("error", (err) => controller.error(err));
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
const request = new Request(url, {
|
|
119
|
-
method: req.method,
|
|
120
|
-
headers,
|
|
121
|
-
body,
|
|
122
|
-
duplex: "half"
|
|
123
|
-
});
|
|
124
|
-
const response = await fetch2(request, {
|
|
125
|
-
ip,
|
|
126
|
-
request: req,
|
|
127
|
-
response: res,
|
|
128
|
-
setCookie(cookie) {
|
|
129
|
-
res.appendHeader("set-cookie", cookie);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
if (!response) {
|
|
133
|
-
if (next) {
|
|
134
|
-
next();
|
|
135
|
-
} else {
|
|
136
|
-
res.statusCode = 404;
|
|
137
|
-
res.setHeader("content-length", "0");
|
|
138
|
-
res.end();
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
res.statusCode = response.status;
|
|
144
|
-
for (const [key, value] of response.headers) {
|
|
145
|
-
if (key === "set-cookie") {
|
|
146
|
-
let sepIndex = value.indexOf(",") + 1;
|
|
147
|
-
if (!sepIndex) {
|
|
148
|
-
res.setHeader(key, value);
|
|
149
|
-
} else {
|
|
150
|
-
let index = 0;
|
|
151
|
-
do {
|
|
152
|
-
res.appendHeader(key, value.slice(index, sepIndex - 1));
|
|
153
|
-
index = sepIndex;
|
|
154
|
-
sepIndex = value.indexOf(",", sepIndex) + 1;
|
|
155
|
-
} while (sepIndex);
|
|
156
|
-
res.appendHeader(key, value.slice(index));
|
|
157
|
-
}
|
|
158
|
-
} else {
|
|
159
|
-
res.setHeader(key, value);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
if (!response.body) {
|
|
163
|
-
if (!response.headers.has("content-length")) {
|
|
164
|
-
res.setHeader("content-length", "0");
|
|
165
|
-
}
|
|
166
|
-
res.end();
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
const reader = response.body.getReader();
|
|
170
|
-
if (res.destroyed) {
|
|
171
|
-
reader.cancel();
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
res.on("close", cancel);
|
|
175
|
-
res.on("error", cancel);
|
|
176
|
-
write();
|
|
177
|
-
function cancel(error) {
|
|
178
|
-
res.off("close", cancel);
|
|
179
|
-
res.off("error", cancel);
|
|
180
|
-
reader.cancel(error).catch(() => {
|
|
181
|
-
});
|
|
182
|
-
error && res.destroy(error);
|
|
183
|
-
}
|
|
184
|
-
async function write() {
|
|
185
|
-
try {
|
|
186
|
-
while (true) {
|
|
187
|
-
const { done, value } = await reader.read();
|
|
188
|
-
if (done) {
|
|
189
|
-
res.end();
|
|
190
|
-
return;
|
|
191
|
-
} else if (!res.write(value)) {
|
|
192
|
-
res.once("drain", write);
|
|
193
|
-
return;
|
|
194
|
-
} else if (res.flush) {
|
|
195
|
-
res.flush();
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
} catch (err) {
|
|
199
|
-
const error = err instanceof Error ? err : new Error("Error while writing to node response", {
|
|
200
|
-
cause: err
|
|
201
|
-
});
|
|
202
|
-
cancel(error);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// src/adapter/dev-server.ts
|
|
209
39
|
function createViteDevMiddleware(devServer, load, factory) {
|
|
210
40
|
let value;
|
|
211
41
|
let middleware;
|
|
@@ -218,10 +48,13 @@ function createViteDevMiddleware(devServer, load, factory) {
|
|
|
218
48
|
}
|
|
219
49
|
await middleware(req, res, next);
|
|
220
50
|
} catch (err) {
|
|
51
|
+
res.statusCode = 500;
|
|
221
52
|
if (err instanceof Error) {
|
|
222
53
|
devServer.ssrFixStacktrace(err);
|
|
54
|
+
res.end(err.stack);
|
|
55
|
+
} else {
|
|
56
|
+
res.end();
|
|
223
57
|
}
|
|
224
|
-
return next == null ? void 0 : next();
|
|
225
58
|
}
|
|
226
59
|
};
|
|
227
60
|
}
|
|
@@ -229,12 +62,19 @@ async function createDevServer(configFile) {
|
|
|
229
62
|
const devServer = await (0, import_vite.createServer)({
|
|
230
63
|
configFile,
|
|
231
64
|
appType: "custom",
|
|
232
|
-
server: { middlewareMode: true }
|
|
65
|
+
server: { middlewareMode: true },
|
|
66
|
+
resolve: {
|
|
67
|
+
dedupe: ["marko"],
|
|
68
|
+
conditions: ["worker"]
|
|
69
|
+
}
|
|
233
70
|
});
|
|
71
|
+
const { createMiddleware } = await devServer.ssrLoadModule(
|
|
72
|
+
"@marko/run/adapter/middleware"
|
|
73
|
+
);
|
|
234
74
|
const middleware = createViteDevMiddleware(
|
|
235
75
|
devServer,
|
|
236
76
|
async () => await devServer.ssrLoadModule("@marko/run/router"),
|
|
237
|
-
(module2) => createMiddleware(module2.fetch)
|
|
77
|
+
(module2) => createMiddleware(module2.fetch, { devServer })
|
|
238
78
|
);
|
|
239
79
|
return devServer.middlewares.use(middleware);
|
|
240
80
|
}
|
package/dist/adapter/index.js
CHANGED
|
@@ -4,176 +4,6 @@ import { fileURLToPath } from "url";
|
|
|
4
4
|
|
|
5
5
|
// src/adapter/dev-server.ts
|
|
6
6
|
import { createServer } from "vite";
|
|
7
|
-
|
|
8
|
-
// src/adapter/polyfill.ts
|
|
9
|
-
import { ReadableStream as ReadableStream2, TransformStream, WritableStream } from "stream/web";
|
|
10
|
-
import { webcrypto as crypto } from "crypto";
|
|
11
|
-
import { fetch, Response, Request as Request2, Headers, FormData, File } from "undici";
|
|
12
|
-
var globals = {
|
|
13
|
-
crypto,
|
|
14
|
-
fetch,
|
|
15
|
-
Response,
|
|
16
|
-
Request: Request2,
|
|
17
|
-
Headers,
|
|
18
|
-
ReadableStream: ReadableStream2,
|
|
19
|
-
TransformStream,
|
|
20
|
-
WritableStream,
|
|
21
|
-
FormData,
|
|
22
|
-
File
|
|
23
|
-
};
|
|
24
|
-
function installPolyfills() {
|
|
25
|
-
for (const name in globals) {
|
|
26
|
-
Object.defineProperty(globalThis, name, {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
configurable: true,
|
|
29
|
-
writable: true,
|
|
30
|
-
value: globals[name]
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// src/adapter/middleware.ts
|
|
36
|
-
installPolyfills();
|
|
37
|
-
function getForwardedHeader(req, name) {
|
|
38
|
-
const value = req.headers["x-forwarded-" + name];
|
|
39
|
-
if (value) {
|
|
40
|
-
if (typeof value === "string") {
|
|
41
|
-
const index = value.indexOf(",");
|
|
42
|
-
return index < 0 ? value : value.slice(0, index);
|
|
43
|
-
}
|
|
44
|
-
return value[0];
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function getOrigin(req, protocol, host, trustProxy) {
|
|
48
|
-
var _a;
|
|
49
|
-
protocol ?? (protocol = req.protocol || trustProxy && getForwardedHeader(req, "proto") || ((_a = req.socket) == null ? void 0 : _a.encrypted) && "https" || "http");
|
|
50
|
-
host ?? (host = trustProxy && getForwardedHeader(req, "host") || req.headers.host);
|
|
51
|
-
if (!host) {
|
|
52
|
-
if (process.env.NODE_ENV !== "production") {
|
|
53
|
-
host = "localhost";
|
|
54
|
-
console.warn(
|
|
55
|
-
`Could not automatically determine the origin host, using 'localhost'. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
|
|
56
|
-
);
|
|
57
|
-
} else {
|
|
58
|
-
throw new Error(
|
|
59
|
-
`Could not automatically determine the origin host. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return `${protocol}://${host}`;
|
|
64
|
-
}
|
|
65
|
-
function createMiddleware(fetch2, options = {}) {
|
|
66
|
-
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
67
|
-
let { origin = process.env.ORIGIN } = options;
|
|
68
|
-
let protocol;
|
|
69
|
-
let host;
|
|
70
|
-
if (origin) {
|
|
71
|
-
({ protocol, host } = new URL(origin));
|
|
72
|
-
protocol = protocol.slice(0, -1);
|
|
73
|
-
}
|
|
74
|
-
return async (req, res, next) => {
|
|
75
|
-
origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
|
|
76
|
-
const url = new URL(req.url, origin);
|
|
77
|
-
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
78
|
-
const headers = req.headers;
|
|
79
|
-
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
80
|
-
start(controller) {
|
|
81
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
82
|
-
req.on("end", () => controller.close());
|
|
83
|
-
req.on("error", (err) => controller.error(err));
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
const request = new Request(url, {
|
|
87
|
-
method: req.method,
|
|
88
|
-
headers,
|
|
89
|
-
body,
|
|
90
|
-
duplex: "half"
|
|
91
|
-
});
|
|
92
|
-
const response = await fetch2(request, {
|
|
93
|
-
ip,
|
|
94
|
-
request: req,
|
|
95
|
-
response: res,
|
|
96
|
-
setCookie(cookie) {
|
|
97
|
-
res.appendHeader("set-cookie", cookie);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
if (!response) {
|
|
101
|
-
if (next) {
|
|
102
|
-
next();
|
|
103
|
-
} else {
|
|
104
|
-
res.statusCode = 404;
|
|
105
|
-
res.setHeader("content-length", "0");
|
|
106
|
-
res.end();
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
res.statusCode = response.status;
|
|
112
|
-
for (const [key, value] of response.headers) {
|
|
113
|
-
if (key === "set-cookie") {
|
|
114
|
-
let sepIndex = value.indexOf(",") + 1;
|
|
115
|
-
if (!sepIndex) {
|
|
116
|
-
res.setHeader(key, value);
|
|
117
|
-
} else {
|
|
118
|
-
let index = 0;
|
|
119
|
-
do {
|
|
120
|
-
res.appendHeader(key, value.slice(index, sepIndex - 1));
|
|
121
|
-
index = sepIndex;
|
|
122
|
-
sepIndex = value.indexOf(",", sepIndex) + 1;
|
|
123
|
-
} while (sepIndex);
|
|
124
|
-
res.appendHeader(key, value.slice(index));
|
|
125
|
-
}
|
|
126
|
-
} else {
|
|
127
|
-
res.setHeader(key, value);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (!response.body) {
|
|
131
|
-
if (!response.headers.has("content-length")) {
|
|
132
|
-
res.setHeader("content-length", "0");
|
|
133
|
-
}
|
|
134
|
-
res.end();
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
const reader = response.body.getReader();
|
|
138
|
-
if (res.destroyed) {
|
|
139
|
-
reader.cancel();
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
res.on("close", cancel);
|
|
143
|
-
res.on("error", cancel);
|
|
144
|
-
write();
|
|
145
|
-
function cancel(error) {
|
|
146
|
-
res.off("close", cancel);
|
|
147
|
-
res.off("error", cancel);
|
|
148
|
-
reader.cancel(error).catch(() => {
|
|
149
|
-
});
|
|
150
|
-
error && res.destroy(error);
|
|
151
|
-
}
|
|
152
|
-
async function write() {
|
|
153
|
-
try {
|
|
154
|
-
while (true) {
|
|
155
|
-
const { done, value } = await reader.read();
|
|
156
|
-
if (done) {
|
|
157
|
-
res.end();
|
|
158
|
-
return;
|
|
159
|
-
} else if (!res.write(value)) {
|
|
160
|
-
res.once("drain", write);
|
|
161
|
-
return;
|
|
162
|
-
} else if (res.flush) {
|
|
163
|
-
res.flush();
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
} catch (err) {
|
|
167
|
-
const error = err instanceof Error ? err : new Error("Error while writing to node response", {
|
|
168
|
-
cause: err
|
|
169
|
-
});
|
|
170
|
-
cancel(error);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// src/adapter/dev-server.ts
|
|
177
7
|
function createViteDevMiddleware(devServer, load, factory) {
|
|
178
8
|
let value;
|
|
179
9
|
let middleware;
|
|
@@ -186,10 +16,13 @@ function createViteDevMiddleware(devServer, load, factory) {
|
|
|
186
16
|
}
|
|
187
17
|
await middleware(req, res, next);
|
|
188
18
|
} catch (err) {
|
|
19
|
+
res.statusCode = 500;
|
|
189
20
|
if (err instanceof Error) {
|
|
190
21
|
devServer.ssrFixStacktrace(err);
|
|
22
|
+
res.end(err.stack);
|
|
23
|
+
} else {
|
|
24
|
+
res.end();
|
|
191
25
|
}
|
|
192
|
-
return next == null ? void 0 : next();
|
|
193
26
|
}
|
|
194
27
|
};
|
|
195
28
|
}
|
|
@@ -197,12 +30,19 @@ async function createDevServer(configFile) {
|
|
|
197
30
|
const devServer = await createServer({
|
|
198
31
|
configFile,
|
|
199
32
|
appType: "custom",
|
|
200
|
-
server: { middlewareMode: true }
|
|
33
|
+
server: { middlewareMode: true },
|
|
34
|
+
resolve: {
|
|
35
|
+
dedupe: ["marko"],
|
|
36
|
+
conditions: ["worker"]
|
|
37
|
+
}
|
|
201
38
|
});
|
|
39
|
+
const { createMiddleware } = await devServer.ssrLoadModule(
|
|
40
|
+
"@marko/run/adapter/middleware"
|
|
41
|
+
);
|
|
202
42
|
const middleware = createViteDevMiddleware(
|
|
203
43
|
devServer,
|
|
204
44
|
async () => await devServer.ssrLoadModule("@marko/run/router"),
|
|
205
|
-
(module) => createMiddleware(module.fetch)
|
|
45
|
+
(module) => createMiddleware(module.fetch, { devServer })
|
|
206
46
|
);
|
|
207
47
|
return devServer.middlewares.use(middleware);
|
|
208
48
|
}
|
|
@@ -29,6 +29,7 @@ module.exports = __toCommonJS(middleware_exports);
|
|
|
29
29
|
var import_web = require("stream/web");
|
|
30
30
|
var import_crypto = require("crypto");
|
|
31
31
|
var import_undici = require("undici");
|
|
32
|
+
var import_http = require("http");
|
|
32
33
|
var globals = {
|
|
33
34
|
crypto: import_crypto.webcrypto,
|
|
34
35
|
fetch: import_undici.fetch,
|
|
@@ -41,14 +42,41 @@ var globals = {
|
|
|
41
42
|
FormData: import_undici.FormData,
|
|
42
43
|
File: import_undici.File
|
|
43
44
|
};
|
|
45
|
+
if (typeof import_http.OutgoingMessage.prototype.appendHeader !== "function") {
|
|
46
|
+
const messageHeaders = /* @__PURE__ */ new WeakMap();
|
|
47
|
+
import_http.OutgoingMessage.prototype.appendHeader = function(name, value) {
|
|
48
|
+
let headers = messageHeaders.get(this);
|
|
49
|
+
if (!headers) {
|
|
50
|
+
headers = /* @__PURE__ */ new Map();
|
|
51
|
+
messageHeaders.set(this, headers);
|
|
52
|
+
}
|
|
53
|
+
const key = name.toLowerCase();
|
|
54
|
+
let existing = headers.get(key);
|
|
55
|
+
if (existing) {
|
|
56
|
+
if (Array.isArray(existing)) {
|
|
57
|
+
existing.push(value);
|
|
58
|
+
} else {
|
|
59
|
+
existing = [existing, value];
|
|
60
|
+
}
|
|
61
|
+
this.setHeader(name, existing);
|
|
62
|
+
headers.set(key, existing);
|
|
63
|
+
} else {
|
|
64
|
+
this.setHeader(name, value);
|
|
65
|
+
headers.set(key, value);
|
|
66
|
+
}
|
|
67
|
+
return this;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
44
70
|
function installPolyfills() {
|
|
45
71
|
for (const name in globals) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
72
|
+
if (!(name in globalThis)) {
|
|
73
|
+
Object.defineProperty(globalThis, name, {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: globals[name]
|
|
78
|
+
});
|
|
79
|
+
}
|
|
52
80
|
}
|
|
53
81
|
}
|
|
54
82
|
|
|
@@ -83,7 +111,7 @@ function getOrigin(req, protocol, host, trustProxy) {
|
|
|
83
111
|
return `${protocol}://${host}`;
|
|
84
112
|
}
|
|
85
113
|
function createMiddleware(fetch2, options = {}) {
|
|
86
|
-
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
114
|
+
const { trustProxy = process.env.TRUST_PROXY === "1", devServer } = options;
|
|
87
115
|
let { origin = process.env.ORIGIN } = options;
|
|
88
116
|
let protocol;
|
|
89
117
|
let host;
|
|
@@ -167,7 +195,17 @@ function createMiddleware(fetch2, options = {}) {
|
|
|
167
195
|
res.off("error", cancel);
|
|
168
196
|
reader.cancel(error).catch(() => {
|
|
169
197
|
});
|
|
170
|
-
|
|
198
|
+
if (error) {
|
|
199
|
+
if (process.env.NODE_ENV !== "production" && devServer) {
|
|
200
|
+
res.end();
|
|
201
|
+
devServer.ws.send({
|
|
202
|
+
type: "error",
|
|
203
|
+
err: { message: error.message, stack: error.stack || "" }
|
|
204
|
+
});
|
|
205
|
+
} else {
|
|
206
|
+
res.destroy(error);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
171
209
|
}
|
|
172
210
|
async function write() {
|
|
173
211
|
try {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import type { Fetch } from "../runtime";
|
|
2
3
|
import type { IncomingMessage, ServerResponse } from "http";
|
|
4
|
+
import type { ViteDevServer } from "vite";
|
|
3
5
|
declare module "net" {
|
|
4
6
|
interface Socket {
|
|
5
7
|
encrypted?: boolean;
|
|
@@ -10,9 +12,6 @@ declare module "http" {
|
|
|
10
12
|
ip?: string;
|
|
11
13
|
protocol?: string;
|
|
12
14
|
}
|
|
13
|
-
interface ServerResponse {
|
|
14
|
-
appendHeader(key: string, value: string | string[]): this;
|
|
15
|
-
}
|
|
16
15
|
}
|
|
17
16
|
export interface NodePlatformInfo {
|
|
18
17
|
ip: string;
|
|
@@ -46,6 +45,7 @@ export interface NodeAdapterOptions {
|
|
|
46
45
|
* is set to `1`, otherwise false.
|
|
47
46
|
*/
|
|
48
47
|
trustProxy?: boolean;
|
|
48
|
+
devServer?: ViteDevServer;
|
|
49
49
|
}
|
|
50
50
|
export declare function getOrigin(req: IncomingMessage, protocol?: string, host?: string, trustProxy?: boolean): string;
|
|
51
51
|
/**
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { ReadableStream as ReadableStream2, TransformStream, WritableStream } from "stream/web";
|
|
3
3
|
import { webcrypto as crypto } from "crypto";
|
|
4
4
|
import { fetch, Response, Request as Request2, Headers, FormData, File } from "undici";
|
|
5
|
+
import { OutgoingMessage } from "http";
|
|
5
6
|
var globals = {
|
|
6
7
|
crypto,
|
|
7
8
|
fetch,
|
|
@@ -14,14 +15,41 @@ var globals = {
|
|
|
14
15
|
FormData,
|
|
15
16
|
File
|
|
16
17
|
};
|
|
18
|
+
if (typeof OutgoingMessage.prototype.appendHeader !== "function") {
|
|
19
|
+
const messageHeaders = /* @__PURE__ */ new WeakMap();
|
|
20
|
+
OutgoingMessage.prototype.appendHeader = function(name, value) {
|
|
21
|
+
let headers = messageHeaders.get(this);
|
|
22
|
+
if (!headers) {
|
|
23
|
+
headers = /* @__PURE__ */ new Map();
|
|
24
|
+
messageHeaders.set(this, headers);
|
|
25
|
+
}
|
|
26
|
+
const key = name.toLowerCase();
|
|
27
|
+
let existing = headers.get(key);
|
|
28
|
+
if (existing) {
|
|
29
|
+
if (Array.isArray(existing)) {
|
|
30
|
+
existing.push(value);
|
|
31
|
+
} else {
|
|
32
|
+
existing = [existing, value];
|
|
33
|
+
}
|
|
34
|
+
this.setHeader(name, existing);
|
|
35
|
+
headers.set(key, existing);
|
|
36
|
+
} else {
|
|
37
|
+
this.setHeader(name, value);
|
|
38
|
+
headers.set(key, value);
|
|
39
|
+
}
|
|
40
|
+
return this;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
17
43
|
function installPolyfills() {
|
|
18
44
|
for (const name in globals) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
45
|
+
if (!(name in globalThis)) {
|
|
46
|
+
Object.defineProperty(globalThis, name, {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
writable: true,
|
|
50
|
+
value: globals[name]
|
|
51
|
+
});
|
|
52
|
+
}
|
|
25
53
|
}
|
|
26
54
|
}
|
|
27
55
|
|
|
@@ -56,7 +84,7 @@ function getOrigin(req, protocol, host, trustProxy) {
|
|
|
56
84
|
return `${protocol}://${host}`;
|
|
57
85
|
}
|
|
58
86
|
function createMiddleware(fetch2, options = {}) {
|
|
59
|
-
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
87
|
+
const { trustProxy = process.env.TRUST_PROXY === "1", devServer } = options;
|
|
60
88
|
let { origin = process.env.ORIGIN } = options;
|
|
61
89
|
let protocol;
|
|
62
90
|
let host;
|
|
@@ -140,7 +168,17 @@ function createMiddleware(fetch2, options = {}) {
|
|
|
140
168
|
res.off("error", cancel);
|
|
141
169
|
reader.cancel(error).catch(() => {
|
|
142
170
|
});
|
|
143
|
-
|
|
171
|
+
if (error) {
|
|
172
|
+
if (process.env.NODE_ENV !== "production" && devServer) {
|
|
173
|
+
res.end();
|
|
174
|
+
devServer.ws.send({
|
|
175
|
+
type: "error",
|
|
176
|
+
err: { message: error.message, stack: error.stack || "" }
|
|
177
|
+
});
|
|
178
|
+
} else {
|
|
179
|
+
res.destroy(error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
144
182
|
}
|
|
145
183
|
async function write() {
|
|
146
184
|
try {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { defineConfig } from "vite";
|
|
2
2
|
import marko from "@marko/run/vite";
|
|
3
|
-
import
|
|
3
|
+
import nodeAdapter from "@marko/run/adapter";
|
|
4
4
|
|
|
5
5
|
export default defineConfig({
|
|
6
|
-
plugins: [marko({ adapter:
|
|
6
|
+
plugins: [marko({ adapter: nodeAdapter() })]
|
|
7
7
|
});
|
package/dist/cli/index.mjs
CHANGED
|
@@ -98,12 +98,12 @@ var defaultPort = +process.env.PORT || 3e3;
|
|
|
98
98
|
var defaultConfigFileBases = ["serve.config", "vite.config"];
|
|
99
99
|
var defaultConfigFileExts = [".js", ".cjs", ".mjs", ".ts", ".mts"];
|
|
100
100
|
var prog = sade("marko-run").version("0.0.1").option("-c, --config", `Provide path to a Vite config file (by default looks for a file starting with ${defaultConfigFileBases.join(" or ")} with one of these extensions: ${defaultConfigFileExts.join(", ")})`).option("-e, --env", "Provide path to a dotenv file");
|
|
101
|
-
prog.command("
|
|
101
|
+
prog.command("preview [entry]").describe("Start a production-like server for already-built app files").option("-o, --output", "Directory to serve files from, and write asset files to if `--build` (default: )").option("-p, --port", "Port the server should listen on (defaults: `$PORT` env variable or 3000)").option("-f, --file", "Output file to start").action(async (entry, opts) => {
|
|
102
102
|
const config2 = await getViteConfig(cwd, opts.config);
|
|
103
103
|
await build(entry, config2, opts.output, false, opts.env);
|
|
104
104
|
await preview(opts.entry, config2, opts.port, opts.output, opts.env);
|
|
105
105
|
});
|
|
106
|
-
prog.command("dev [entry]").describe("Start development server in watch mode").option("-p, --port", "Port the dev server should listen on (defaults: 'preview.port' in config, or `$PORT` env variable, or 3000)").example("dev --config vite.config.js").action(async (entry, opts) => {
|
|
106
|
+
prog.command("dev [entry]", "", { default: true }).describe("Start development server in watch mode").option("-p, --port", "Port the dev server should listen on (defaults: 'preview.port' in config, or `$PORT` env variable, or 3000)").example("dev --config vite.config.js").action(async (entry, opts) => {
|
|
107
107
|
const cmd = opts._.length ? `${entry} ${opts._.join(" ")}` : entry ? `node ${entry}` : void 0;
|
|
108
108
|
const config2 = await getViteConfig(cwd, opts.config);
|
|
109
109
|
await dev(cmd, config2, opts.port, opts.env);
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import type { HandlerLike, ParamsObject, Route,
|
|
1
|
+
import type { HandlerLike, ParamsObject, Route as AnyRoute, Context as AnyContext } from "./types";
|
|
2
2
|
declare global {
|
|
3
3
|
namespace Marko {
|
|
4
|
-
interface Global extends MarkoRun.
|
|
4
|
+
interface Global extends MarkoRun.Context {
|
|
5
5
|
}
|
|
6
6
|
interface Out {
|
|
7
7
|
global: Global;
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
namespace MarkoRun {
|
|
11
|
-
const NotHandled: symbol;
|
|
12
|
-
const NotMatched: symbol;
|
|
13
|
-
interface
|
|
11
|
+
const NotHandled: unique symbol;
|
|
12
|
+
const NotMatched: unique symbol;
|
|
13
|
+
interface Route extends AnyRoute {
|
|
14
14
|
}
|
|
15
|
-
interface
|
|
15
|
+
interface Context extends AnyContext<Route> {
|
|
16
16
|
}
|
|
17
|
-
type Handler<Params extends ParamsObject = {}, Meta = unknown> = HandlerLike<
|
|
17
|
+
type Handler<Params extends ParamsObject = {}, Meta = unknown> = HandlerLike<AnyRoute<Params, Meta, string>>;
|
|
18
18
|
function route<Params extends ParamsObject = {}, Meta = unknown>(handler: Handler<Params, Meta>): typeof handler;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
export type { Fetch, HandlerLike, InputObject, Invoke, Match, NextFunction, PathTemplate, Route,
|
|
21
|
+
export type { Fetch, HandlerLike, InputObject, Invoke, Match, NextFunction, PathTemplate, Route, Context, ContextExtensions, RouteHandler, RouteWithHandler, RuntimeModule, ValidateHref, ValidatePath, } from "./types";
|