@marko/run 0.0.1-beta5 → 0.0.1-beta7
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 +32 -32
- package/dist/adapter/default-entry.mjs +8 -5
- package/dist/adapter/index.cjs +51 -48
- package/dist/adapter/index.js +51 -48
- package/dist/adapter/middleware.cjs +50 -50
- package/dist/adapter/middleware.d.ts +2 -2
- package/dist/adapter/middleware.js +48 -45
- package/dist/adapter/polyfill.d.ts +1 -0
- package/dist/runtime/index.d.ts +7 -3
- package/dist/runtime/internal.cjs +16 -13
- package/dist/runtime/internal.d.ts +2 -2
- package/dist/runtime/internal.js +14 -11
- package/dist/runtime/router.cjs +9 -9
- package/dist/runtime/router.d.ts +3 -4
- package/dist/runtime/router.js +6 -6
- package/dist/runtime/types.d.ts +11 -9
- package/dist/vite/codegen/index.d.ts +1 -1
- package/dist/vite/index.cjs +82 -57
- package/dist/vite/index.js +82 -57
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
> **Warning**
|
|
2
|
+
> This project is in BETA - use at your own peril, but please do provide helpful feedback.
|
|
3
|
+
|
|
1
4
|
<div align="center">
|
|
2
5
|
<!-- Logo -->
|
|
3
6
|
<h1>
|
|
@@ -51,9 +54,9 @@ The package provides a command line tool `marko-run` which can be run using scri
|
|
|
51
54
|
> npx marko-run build
|
|
52
55
|
```
|
|
53
56
|
|
|
54
|
-
**`
|
|
57
|
+
**`serve`** - Create a production build and serve
|
|
55
58
|
```bash
|
|
56
|
-
> npx marko-run
|
|
59
|
+
> npx marko-run serve
|
|
57
60
|
```
|
|
58
61
|
or (default command)
|
|
59
62
|
```bash
|
|
@@ -76,38 +79,30 @@ export default defineConfig({
|
|
|
76
79
|
|
|
77
80
|
## Adapters
|
|
78
81
|
|
|
79
|
-
|
|
82
|
+
<!-- TODO: link to existing adapters>
|
|
83
|
+
|
|
84
|
+
<!-- *🎗 TODO: provide a quick overview* -->
|
|
80
85
|
|
|
81
86
|
## Runtime
|
|
82
87
|
|
|
83
88
|
Generally, when using an adapter, this runtime will be abstracted away.
|
|
84
89
|
|
|
90
|
+
<!-- TODO: Add examples -->
|
|
91
|
+
<!-- TODO: Split fetch and match + invoke in two sections and explain why you might use one or the other -->
|
|
92
|
+
|
|
85
93
|
```ts
|
|
86
94
|
import { router, matchRoute, invokeRoute } from '@marko/run/router`;
|
|
87
95
|
```
|
|
88
96
|
|
|
89
|
-
### `
|
|
97
|
+
### `fetch`
|
|
90
98
|
|
|
91
99
|
```ts
|
|
92
|
-
|
|
93
|
-
url: URL;
|
|
94
|
-
method: string;
|
|
95
|
-
request: Request;
|
|
96
|
-
platform: T;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async function router(context: RequestContext) => Promise<Response | void>;
|
|
100
|
+
async function fetch<T>(request: Request, platform: T) => Promise<Response | void>;
|
|
100
101
|
```
|
|
101
102
|
|
|
102
|
-
This asynchronous function takes a
|
|
103
|
+
This asynchronous function takes a [WHATWG `Request` object](https://fetch.spec.whatwg.org/#request-class) object and an object containing any platform specific data you may want access to and returns the [WHATWG `Response` object](https://fetch.spec.whatwg.org/#response-class) from executing any matched route files or undefined if the request was explicitly not handled. If no route matches the requested path, a `404` status code response will be returned. If an error occurs a `500` status code response will be returned.
|
|
103
104
|
|
|
104
|
-
|
|
105
|
-
- `url`: The URL reprensting the requested resource
|
|
106
|
-
- `method`: The HTTP method used
|
|
107
|
-
- `request`: [WHATWG `Request` object](https://fetch.spec.whatwg.org/#request-class)
|
|
108
|
-
- `platform`: An object containing any platform-specific data (eg Node request/response) and will vary between adapters.
|
|
109
|
-
|
|
110
|
-
### `matchRoute`
|
|
105
|
+
### `match`
|
|
111
106
|
|
|
112
107
|
```ts
|
|
113
108
|
interface interface Route {
|
|
@@ -115,7 +110,7 @@ interface interface Route {
|
|
|
115
110
|
meta: unknown;
|
|
116
111
|
}
|
|
117
112
|
|
|
118
|
-
function
|
|
113
|
+
function match(method: string, pathname: string) => Route | null;
|
|
119
114
|
```
|
|
120
115
|
|
|
121
116
|
This synchronous function takes an HTTP method and path name, then returns an object representing the best match — or `null` if no match is found.
|
|
@@ -123,20 +118,20 @@ This synchronous function takes an HTTP method and path name, then returns an ob
|
|
|
123
118
|
- `params` - a `{ key: value }` collection of any path parameters for the route
|
|
124
119
|
- `meta` - metadata for the route
|
|
125
120
|
|
|
126
|
-
### `
|
|
121
|
+
### `invoke`
|
|
127
122
|
|
|
128
123
|
```ts
|
|
129
|
-
function
|
|
124
|
+
async function invoke<T>(route: Route, request: Request, platform: T) => Promise<Response | void>;
|
|
130
125
|
```
|
|
131
|
-
This asynchronous function takes a route object returned by [
|
|
126
|
+
This asynchronous function takes a route object returned by [match](#match) the request and platform data and returns a response in the same way the [fetch](#fetch) does.
|
|
132
127
|
|
|
133
128
|
|
|
134
129
|
|
|
135
130
|
## File-based Routing
|
|
136
131
|
|
|
137
|
-
### Nested Routing
|
|
132
|
+
<!-- ### Nested Routing
|
|
138
133
|
|
|
139
|
-
*🎗 TODO: provide a quick overview*
|
|
134
|
+
*🎗 TODO: provide a quick overview* -->
|
|
140
135
|
|
|
141
136
|
### Routes Directory
|
|
142
137
|
|
|
@@ -276,6 +271,8 @@ Responses with this page will have a `500` status code.
|
|
|
276
271
|
|
|
277
272
|
### Execution Order
|
|
278
273
|
|
|
274
|
+
<!-- TODO: add file tree and update flow-chart with file names -->
|
|
275
|
+
|
|
279
276
|
For a matched route, the routable files execute in the following order:
|
|
280
277
|
|
|
281
278
|
1. Middlewares from root-most to leaf-most
|
|
@@ -315,16 +312,15 @@ Within the _routes directory_, the directory structure will determine the path t
|
|
|
315
312
|
/projects
|
|
316
313
|
```
|
|
317
314
|
|
|
318
|
-
2. **Pathless directories** - These directories do **not** contribute their name to the route's served path. Directory names that start with an underscore (`_`)
|
|
315
|
+
2. **Pathless directories** - These directories do **not** contribute their name to the route's served path. Directory names that start with an underscore (`_`) will be a pathless directory.
|
|
319
316
|
|
|
320
317
|
Examples:
|
|
321
318
|
```
|
|
322
319
|
/_users
|
|
323
320
|
/_public
|
|
324
|
-
/index
|
|
325
321
|
```
|
|
326
322
|
|
|
327
|
-
3. **Dynamic directories** - These directories introduce a dynamic parameter to the route's served path and will match any value at that segment. Any directory name that starts with a single dollar sign (`$`) will be a dynamic directory, and the remaining directory name will be the parameter at runtime. If the directory name is exactly
|
|
323
|
+
3. **Dynamic directories** - These directories introduce a dynamic parameter to the route's served path and will match any value at that segment. Any directory name that starts with a single dollar sign (`$`) will be a dynamic directory, and the remaining directory name will be the parameter at runtime. If the directory name is exactly `$`, the parameter will not be captured but it will be matched.
|
|
328
324
|
|
|
329
325
|
Examples:
|
|
330
326
|
```
|
|
@@ -333,7 +329,7 @@ Within the _routes directory_, the directory structure will determine the path t
|
|
|
333
329
|
/$
|
|
334
330
|
```
|
|
335
331
|
|
|
336
|
-
4. **Catch-all directories** - These directories are similar to dynamic directories and introduce a dynamic parameter, but instead of matching a single path segment, they match to the end of the path. Any directory that starts with two dollar signs (`$$`) will be a catch-all directory, and the remaining directory name will be the parameter at runtime. In the case of a directory named
|
|
332
|
+
4. **Catch-all directories** - These directories are similar to dynamic directories and introduce a dynamic parameter, but instead of matching a single path segment, they match to the end of the path. Any directory that starts with two dollar signs (`$$`) will be a catch-all directory, and the remaining directory name will be the parameter at runtime. In the case of a directory named `$$`, the parameter name will not be captured but it will match. Catch-all directories can be used to make `404` Not Found routes at any level, including the root.
|
|
337
333
|
|
|
338
334
|
Because catch-all directories match any path segment and consume the rest of the path, you cannot nest route files in them and no further directories will be traversed.
|
|
339
335
|
|
|
@@ -344,6 +340,10 @@ Within the _routes directory_, the directory structure will determine the path t
|
|
|
344
340
|
/$$
|
|
345
341
|
```
|
|
346
342
|
|
|
347
|
-
### Match Ranking
|
|
343
|
+
<!-- ### Match Ranking
|
|
344
|
+
|
|
345
|
+
*TODO: Write some things* -->
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
## TypeScript
|
|
348
349
|
|
|
349
|
-
*TODO: Write some things*
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import createStaticServe from "serve-static";
|
|
2
2
|
import compression from "compression";
|
|
3
3
|
import { createServer } from "http";
|
|
4
|
-
import createMiddleware from "@marko/run/adapter/middleware";
|
|
5
|
-
import {
|
|
4
|
+
import { createMiddleware } from "@marko/run/adapter/middleware";
|
|
5
|
+
import { fetch } from "@marko/run/router";
|
|
6
|
+
import { dirname } from 'path';
|
|
7
|
+
import { fileURLToPath } from 'url';
|
|
8
|
+
|
|
9
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
6
10
|
|
|
7
11
|
const { PORT = 3456 } = process.env;
|
|
8
12
|
|
|
9
|
-
const
|
|
10
|
-
const middleware = createMiddleware(router);
|
|
13
|
+
const middleware = createMiddleware(fetch);
|
|
11
14
|
const compress = compression({
|
|
12
15
|
threshold: 500,
|
|
13
16
|
});
|
|
14
|
-
const staticServe = createStaticServe(
|
|
17
|
+
const staticServe = createStaticServe(__dirname, {
|
|
15
18
|
index: false,
|
|
16
19
|
immutable: true,
|
|
17
20
|
maxAge: "365 days",
|
package/dist/adapter/index.cjs
CHANGED
|
@@ -37,15 +37,35 @@ var import_url = require("url");
|
|
|
37
37
|
// src/adapter/dev-server.ts
|
|
38
38
|
var import_vite = require("vite");
|
|
39
39
|
|
|
40
|
-
// src/adapter/
|
|
41
|
-
var
|
|
42
|
-
var import_crypto =
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
});
|
|
47
64
|
}
|
|
48
65
|
}
|
|
66
|
+
|
|
67
|
+
// src/adapter/middleware.ts
|
|
68
|
+
installPolyfills();
|
|
49
69
|
function getForwardedHeader(req, name) {
|
|
50
70
|
const value = req.headers["x-forwarded-" + name];
|
|
51
71
|
if (value) {
|
|
@@ -74,7 +94,7 @@ function getOrigin(req, protocol, host, trustProxy) {
|
|
|
74
94
|
}
|
|
75
95
|
return `${protocol}://${host}`;
|
|
76
96
|
}
|
|
77
|
-
function createMiddleware(
|
|
97
|
+
function createMiddleware(fetch2, options = {}) {
|
|
78
98
|
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
79
99
|
let { origin = process.env.ORIGIN } = options;
|
|
80
100
|
let protocol;
|
|
@@ -87,45 +107,28 @@ function createMiddleware(router, options = {}) {
|
|
|
87
107
|
origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
|
|
88
108
|
const url = new URL(req.url, origin);
|
|
89
109
|
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
90
|
-
const
|
|
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, {
|
|
91
119
|
method: req.method,
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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);
|
|
100
130
|
}
|
|
101
|
-
};
|
|
102
|
-
Object.defineProperty(requestContext, "request", {
|
|
103
|
-
get() {
|
|
104
|
-
const headers = req.headers;
|
|
105
|
-
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
106
|
-
start(controller) {
|
|
107
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
108
|
-
req.on("end", () => controller.close());
|
|
109
|
-
req.on("error", (err) => controller.error(err));
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
const request = new Request(url, {
|
|
113
|
-
method: req.method,
|
|
114
|
-
headers,
|
|
115
|
-
body,
|
|
116
|
-
duplex: "half"
|
|
117
|
-
});
|
|
118
|
-
Object.defineProperty(this, "request", {
|
|
119
|
-
value: request,
|
|
120
|
-
enumerable: true,
|
|
121
|
-
configurable: true
|
|
122
|
-
});
|
|
123
|
-
return request;
|
|
124
|
-
},
|
|
125
|
-
enumerable: true,
|
|
126
|
-
configurable: true
|
|
127
131
|
});
|
|
128
|
-
const response = await router(requestContext);
|
|
129
132
|
if (!response) {
|
|
130
133
|
if (next) {
|
|
131
134
|
next();
|
|
@@ -230,8 +233,8 @@ async function createDevServer(configFile) {
|
|
|
230
233
|
});
|
|
231
234
|
const middleware = createViteDevMiddleware(
|
|
232
235
|
devServer,
|
|
233
|
-
async () =>
|
|
234
|
-
createMiddleware
|
|
236
|
+
async () => await devServer.ssrLoadModule("@marko/run/router"),
|
|
237
|
+
(module2) => createMiddleware(module2.fetch)
|
|
235
238
|
);
|
|
236
239
|
return devServer.middlewares.use(middleware);
|
|
237
240
|
}
|
|
@@ -327,8 +330,8 @@ function adapter() {
|
|
|
327
330
|
});
|
|
328
331
|
});
|
|
329
332
|
},
|
|
330
|
-
async startPreview(
|
|
331
|
-
const server = await spawnServer(`node ${entry}`, port, envFile
|
|
333
|
+
async startPreview(_dir, entry, port, envFile) {
|
|
334
|
+
const server = await spawnServer(`node ${entry}`, port, envFile);
|
|
332
335
|
console.log(`Preview server started: http://localhost:${server.port}`);
|
|
333
336
|
}
|
|
334
337
|
};
|
package/dist/adapter/index.js
CHANGED
|
@@ -5,15 +5,35 @@ import { fileURLToPath } from "url";
|
|
|
5
5
|
// src/adapter/dev-server.ts
|
|
6
6
|
import { createServer } from "vite";
|
|
7
7
|
|
|
8
|
-
// src/adapter/
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
+
});
|
|
15
32
|
}
|
|
16
33
|
}
|
|
34
|
+
|
|
35
|
+
// src/adapter/middleware.ts
|
|
36
|
+
installPolyfills();
|
|
17
37
|
function getForwardedHeader(req, name) {
|
|
18
38
|
const value = req.headers["x-forwarded-" + name];
|
|
19
39
|
if (value) {
|
|
@@ -42,7 +62,7 @@ function getOrigin(req, protocol, host, trustProxy) {
|
|
|
42
62
|
}
|
|
43
63
|
return `${protocol}://${host}`;
|
|
44
64
|
}
|
|
45
|
-
function createMiddleware(
|
|
65
|
+
function createMiddleware(fetch2, options = {}) {
|
|
46
66
|
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
47
67
|
let { origin = process.env.ORIGIN } = options;
|
|
48
68
|
let protocol;
|
|
@@ -55,45 +75,28 @@ function createMiddleware(router, options = {}) {
|
|
|
55
75
|
origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
|
|
56
76
|
const url = new URL(req.url, origin);
|
|
57
77
|
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
58
|
-
const
|
|
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, {
|
|
59
87
|
method: req.method,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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);
|
|
68
98
|
}
|
|
69
|
-
};
|
|
70
|
-
Object.defineProperty(requestContext, "request", {
|
|
71
|
-
get() {
|
|
72
|
-
const headers = req.headers;
|
|
73
|
-
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
74
|
-
start(controller) {
|
|
75
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
76
|
-
req.on("end", () => controller.close());
|
|
77
|
-
req.on("error", (err) => controller.error(err));
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
const request = new Request(url, {
|
|
81
|
-
method: req.method,
|
|
82
|
-
headers,
|
|
83
|
-
body,
|
|
84
|
-
duplex: "half"
|
|
85
|
-
});
|
|
86
|
-
Object.defineProperty(this, "request", {
|
|
87
|
-
value: request,
|
|
88
|
-
enumerable: true,
|
|
89
|
-
configurable: true
|
|
90
|
-
});
|
|
91
|
-
return request;
|
|
92
|
-
},
|
|
93
|
-
enumerable: true,
|
|
94
|
-
configurable: true
|
|
95
99
|
});
|
|
96
|
-
const response = await router(requestContext);
|
|
97
100
|
if (!response) {
|
|
98
101
|
if (next) {
|
|
99
102
|
next();
|
|
@@ -198,8 +201,8 @@ async function createDevServer(configFile) {
|
|
|
198
201
|
});
|
|
199
202
|
const middleware = createViteDevMiddleware(
|
|
200
203
|
devServer,
|
|
201
|
-
async () =>
|
|
202
|
-
createMiddleware
|
|
204
|
+
async () => await devServer.ssrLoadModule("@marko/run/router"),
|
|
205
|
+
(module) => createMiddleware(module.fetch)
|
|
203
206
|
);
|
|
204
207
|
return devServer.middlewares.use(middleware);
|
|
205
208
|
}
|
|
@@ -294,8 +297,8 @@ function adapter() {
|
|
|
294
297
|
});
|
|
295
298
|
});
|
|
296
299
|
},
|
|
297
|
-
async startPreview(
|
|
298
|
-
const server = await spawnServer(`node ${entry}`, port, envFile
|
|
300
|
+
async startPreview(_dir, entry, port, envFile) {
|
|
301
|
+
const server = await spawnServer(`node ${entry}`, port, envFile);
|
|
299
302
|
console.log(`Preview server started: http://localhost:${server.port}`);
|
|
300
303
|
}
|
|
301
304
|
};
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,27 +15,45 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
-
mod
|
|
23
|
-
));
|
|
24
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
19
|
|
|
26
20
|
// src/adapter/middleware.ts
|
|
27
21
|
var middleware_exports = {};
|
|
28
22
|
__export(middleware_exports, {
|
|
29
|
-
|
|
23
|
+
createMiddleware: () => createMiddleware,
|
|
30
24
|
getOrigin: () => getOrigin
|
|
31
25
|
});
|
|
32
26
|
module.exports = __toCommonJS(middleware_exports);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
27
|
+
|
|
28
|
+
// src/adapter/polyfill.ts
|
|
29
|
+
var import_web = require("stream/web");
|
|
30
|
+
var import_crypto = require("crypto");
|
|
31
|
+
var import_undici = require("undici");
|
|
32
|
+
var globals = {
|
|
33
|
+
crypto: import_crypto.webcrypto,
|
|
34
|
+
fetch: import_undici.fetch,
|
|
35
|
+
Response: import_undici.Response,
|
|
36
|
+
Request: import_undici.Request,
|
|
37
|
+
Headers: import_undici.Headers,
|
|
38
|
+
ReadableStream: import_web.ReadableStream,
|
|
39
|
+
TransformStream: import_web.TransformStream,
|
|
40
|
+
WritableStream: import_web.WritableStream,
|
|
41
|
+
FormData: import_undici.FormData,
|
|
42
|
+
File: import_undici.File
|
|
43
|
+
};
|
|
44
|
+
function installPolyfills() {
|
|
45
|
+
for (const name in globals) {
|
|
46
|
+
Object.defineProperty(globalThis, name, {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
writable: true,
|
|
50
|
+
value: globals[name]
|
|
51
|
+
});
|
|
39
52
|
}
|
|
40
53
|
}
|
|
54
|
+
|
|
55
|
+
// src/adapter/middleware.ts
|
|
56
|
+
installPolyfills();
|
|
41
57
|
function getForwardedHeader(req, name) {
|
|
42
58
|
const value = req.headers["x-forwarded-" + name];
|
|
43
59
|
if (value) {
|
|
@@ -66,7 +82,7 @@ function getOrigin(req, protocol, host, trustProxy) {
|
|
|
66
82
|
}
|
|
67
83
|
return `${protocol}://${host}`;
|
|
68
84
|
}
|
|
69
|
-
function createMiddleware(
|
|
85
|
+
function createMiddleware(fetch2, options = {}) {
|
|
70
86
|
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
71
87
|
let { origin = process.env.ORIGIN } = options;
|
|
72
88
|
let protocol;
|
|
@@ -79,45 +95,28 @@ function createMiddleware(router, options = {}) {
|
|
|
79
95
|
origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
|
|
80
96
|
const url = new URL(req.url, origin);
|
|
81
97
|
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
82
|
-
const
|
|
98
|
+
const headers = req.headers;
|
|
99
|
+
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
100
|
+
start(controller) {
|
|
101
|
+
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
102
|
+
req.on("end", () => controller.close());
|
|
103
|
+
req.on("error", (err) => controller.error(err));
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
const request = new Request(url, {
|
|
83
107
|
method: req.method,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
108
|
+
headers,
|
|
109
|
+
body,
|
|
110
|
+
duplex: "half"
|
|
111
|
+
});
|
|
112
|
+
const response = await fetch2(request, {
|
|
113
|
+
ip,
|
|
114
|
+
request: req,
|
|
115
|
+
response: res,
|
|
116
|
+
setCookie(cookie) {
|
|
117
|
+
res.appendHeader("set-cookie", cookie);
|
|
92
118
|
}
|
|
93
|
-
};
|
|
94
|
-
Object.defineProperty(requestContext, "request", {
|
|
95
|
-
get() {
|
|
96
|
-
const headers = req.headers;
|
|
97
|
-
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
98
|
-
start(controller) {
|
|
99
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
100
|
-
req.on("end", () => controller.close());
|
|
101
|
-
req.on("error", (err) => controller.error(err));
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
const request = new Request(url, {
|
|
105
|
-
method: req.method,
|
|
106
|
-
headers,
|
|
107
|
-
body,
|
|
108
|
-
duplex: "half"
|
|
109
|
-
});
|
|
110
|
-
Object.defineProperty(this, "request", {
|
|
111
|
-
value: request,
|
|
112
|
-
enumerable: true,
|
|
113
|
-
configurable: true
|
|
114
|
-
});
|
|
115
|
-
return request;
|
|
116
|
-
},
|
|
117
|
-
enumerable: true,
|
|
118
|
-
configurable: true
|
|
119
119
|
});
|
|
120
|
-
const response = await router(requestContext);
|
|
121
120
|
if (!response) {
|
|
122
121
|
if (next) {
|
|
123
122
|
next();
|
|
@@ -195,5 +194,6 @@ function createMiddleware(router, options = {}) {
|
|
|
195
194
|
}
|
|
196
195
|
// Annotate the CommonJS export names for ESM import in node:
|
|
197
196
|
0 && (module.exports = {
|
|
197
|
+
createMiddleware,
|
|
198
198
|
getOrigin
|
|
199
199
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Fetch } from "../runtime";
|
|
2
2
|
import type { IncomingMessage, ServerResponse } from "http";
|
|
3
3
|
declare module "net" {
|
|
4
4
|
interface Socket {
|
|
@@ -52,4 +52,4 @@ export declare function getOrigin(req: IncomingMessage, protocol?: string, host?
|
|
|
52
52
|
* Creates a request handler to be passed to http.createServer() or used as a
|
|
53
53
|
* middleware in Connect-style frameworks like Express.
|
|
54
54
|
*/
|
|
55
|
-
export
|
|
55
|
+
export declare function createMiddleware(fetch: Fetch<NodePlatformInfo>, options?: NodeAdapterOptions): NodeMiddleware;
|