@marko/run 0.0.1-beta6 → 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 +24 -41
- package/dist/adapter/index.js +24 -41
- package/dist/adapter/middleware.cjs +22 -38
- package/dist/adapter/middleware.d.ts +2 -2
- package/dist/adapter/middleware.js +21 -38
- 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 +1 -1
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
|
@@ -94,7 +94,7 @@ function getOrigin(req, protocol, host, trustProxy) {
|
|
|
94
94
|
}
|
|
95
95
|
return `${protocol}://${host}`;
|
|
96
96
|
}
|
|
97
|
-
function createMiddleware(
|
|
97
|
+
function createMiddleware(fetch2, options = {}) {
|
|
98
98
|
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
99
99
|
let { origin = process.env.ORIGIN } = options;
|
|
100
100
|
let protocol;
|
|
@@ -107,45 +107,28 @@ function createMiddleware(router, options = {}) {
|
|
|
107
107
|
origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
|
|
108
108
|
const url = new URL(req.url, origin);
|
|
109
109
|
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
110
|
-
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, {
|
|
111
119
|
method: req.method,
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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);
|
|
120
130
|
}
|
|
121
|
-
};
|
|
122
|
-
Object.defineProperty(requestContext, "request", {
|
|
123
|
-
get() {
|
|
124
|
-
const headers = req.headers;
|
|
125
|
-
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
126
|
-
start(controller) {
|
|
127
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
128
|
-
req.on("end", () => controller.close());
|
|
129
|
-
req.on("error", (err) => controller.error(err));
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
const request = new Request(url, {
|
|
133
|
-
method: req.method,
|
|
134
|
-
headers,
|
|
135
|
-
body,
|
|
136
|
-
duplex: "half"
|
|
137
|
-
});
|
|
138
|
-
Object.defineProperty(this, "request", {
|
|
139
|
-
value: request,
|
|
140
|
-
enumerable: true,
|
|
141
|
-
configurable: true
|
|
142
|
-
});
|
|
143
|
-
return request;
|
|
144
|
-
},
|
|
145
|
-
enumerable: true,
|
|
146
|
-
configurable: true
|
|
147
131
|
});
|
|
148
|
-
const response = await router(requestContext);
|
|
149
132
|
if (!response) {
|
|
150
133
|
if (next) {
|
|
151
134
|
next();
|
|
@@ -250,8 +233,8 @@ async function createDevServer(configFile) {
|
|
|
250
233
|
});
|
|
251
234
|
const middleware = createViteDevMiddleware(
|
|
252
235
|
devServer,
|
|
253
|
-
async () =>
|
|
254
|
-
createMiddleware
|
|
236
|
+
async () => await devServer.ssrLoadModule("@marko/run/router"),
|
|
237
|
+
(module2) => createMiddleware(module2.fetch)
|
|
255
238
|
);
|
|
256
239
|
return devServer.middlewares.use(middleware);
|
|
257
240
|
}
|
|
@@ -347,8 +330,8 @@ function adapter() {
|
|
|
347
330
|
});
|
|
348
331
|
});
|
|
349
332
|
},
|
|
350
|
-
async startPreview(
|
|
351
|
-
const server = await spawnServer(`node ${entry}`, port, envFile
|
|
333
|
+
async startPreview(_dir, entry, port, envFile) {
|
|
334
|
+
const server = await spawnServer(`node ${entry}`, port, envFile);
|
|
352
335
|
console.log(`Preview server started: http://localhost:${server.port}`);
|
|
353
336
|
}
|
|
354
337
|
};
|
package/dist/adapter/index.js
CHANGED
|
@@ -62,7 +62,7 @@ function getOrigin(req, protocol, host, trustProxy) {
|
|
|
62
62
|
}
|
|
63
63
|
return `${protocol}://${host}`;
|
|
64
64
|
}
|
|
65
|
-
function createMiddleware(
|
|
65
|
+
function createMiddleware(fetch2, options = {}) {
|
|
66
66
|
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
67
67
|
let { origin = process.env.ORIGIN } = options;
|
|
68
68
|
let protocol;
|
|
@@ -75,45 +75,28 @@ function createMiddleware(router, options = {}) {
|
|
|
75
75
|
origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
|
|
76
76
|
const url = new URL(req.url, origin);
|
|
77
77
|
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
78
|
-
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, {
|
|
79
87
|
method: req.method,
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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);
|
|
88
98
|
}
|
|
89
|
-
};
|
|
90
|
-
Object.defineProperty(requestContext, "request", {
|
|
91
|
-
get() {
|
|
92
|
-
const headers = req.headers;
|
|
93
|
-
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
94
|
-
start(controller) {
|
|
95
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
96
|
-
req.on("end", () => controller.close());
|
|
97
|
-
req.on("error", (err) => controller.error(err));
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
const request = new Request(url, {
|
|
101
|
-
method: req.method,
|
|
102
|
-
headers,
|
|
103
|
-
body,
|
|
104
|
-
duplex: "half"
|
|
105
|
-
});
|
|
106
|
-
Object.defineProperty(this, "request", {
|
|
107
|
-
value: request,
|
|
108
|
-
enumerable: true,
|
|
109
|
-
configurable: true
|
|
110
|
-
});
|
|
111
|
-
return request;
|
|
112
|
-
},
|
|
113
|
-
enumerable: true,
|
|
114
|
-
configurable: true
|
|
115
99
|
});
|
|
116
|
-
const response = await router(requestContext);
|
|
117
100
|
if (!response) {
|
|
118
101
|
if (next) {
|
|
119
102
|
next();
|
|
@@ -218,8 +201,8 @@ async function createDevServer(configFile) {
|
|
|
218
201
|
});
|
|
219
202
|
const middleware = createViteDevMiddleware(
|
|
220
203
|
devServer,
|
|
221
|
-
async () =>
|
|
222
|
-
createMiddleware
|
|
204
|
+
async () => await devServer.ssrLoadModule("@marko/run/router"),
|
|
205
|
+
(module) => createMiddleware(module.fetch)
|
|
223
206
|
);
|
|
224
207
|
return devServer.middlewares.use(middleware);
|
|
225
208
|
}
|
|
@@ -314,8 +297,8 @@ function adapter() {
|
|
|
314
297
|
});
|
|
315
298
|
});
|
|
316
299
|
},
|
|
317
|
-
async startPreview(
|
|
318
|
-
const server = await spawnServer(`node ${entry}`, port, envFile
|
|
300
|
+
async startPreview(_dir, entry, port, envFile) {
|
|
301
|
+
const server = await spawnServer(`node ${entry}`, port, envFile);
|
|
319
302
|
console.log(`Preview server started: http://localhost:${server.port}`);
|
|
320
303
|
}
|
|
321
304
|
};
|
|
@@ -20,7 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/adapter/middleware.ts
|
|
21
21
|
var middleware_exports = {};
|
|
22
22
|
__export(middleware_exports, {
|
|
23
|
-
|
|
23
|
+
createMiddleware: () => createMiddleware,
|
|
24
24
|
getOrigin: () => getOrigin
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(middleware_exports);
|
|
@@ -82,7 +82,7 @@ function getOrigin(req, protocol, host, trustProxy) {
|
|
|
82
82
|
}
|
|
83
83
|
return `${protocol}://${host}`;
|
|
84
84
|
}
|
|
85
|
-
function createMiddleware(
|
|
85
|
+
function createMiddleware(fetch2, options = {}) {
|
|
86
86
|
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
87
87
|
let { origin = process.env.ORIGIN } = options;
|
|
88
88
|
let protocol;
|
|
@@ -95,45 +95,28 @@ function createMiddleware(router, options = {}) {
|
|
|
95
95
|
origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
|
|
96
96
|
const url = new URL(req.url, origin);
|
|
97
97
|
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
98
|
-
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, {
|
|
99
107
|
method: req.method,
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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);
|
|
108
118
|
}
|
|
109
|
-
};
|
|
110
|
-
Object.defineProperty(requestContext, "request", {
|
|
111
|
-
get() {
|
|
112
|
-
const headers = req.headers;
|
|
113
|
-
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
114
|
-
start(controller) {
|
|
115
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
116
|
-
req.on("end", () => controller.close());
|
|
117
|
-
req.on("error", (err) => controller.error(err));
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
const request = new Request(url, {
|
|
121
|
-
method: req.method,
|
|
122
|
-
headers,
|
|
123
|
-
body,
|
|
124
|
-
duplex: "half"
|
|
125
|
-
});
|
|
126
|
-
Object.defineProperty(this, "request", {
|
|
127
|
-
value: request,
|
|
128
|
-
enumerable: true,
|
|
129
|
-
configurable: true
|
|
130
|
-
});
|
|
131
|
-
return request;
|
|
132
|
-
},
|
|
133
|
-
enumerable: true,
|
|
134
|
-
configurable: true
|
|
135
119
|
});
|
|
136
|
-
const response = await router(requestContext);
|
|
137
120
|
if (!response) {
|
|
138
121
|
if (next) {
|
|
139
122
|
next();
|
|
@@ -211,5 +194,6 @@ function createMiddleware(router, options = {}) {
|
|
|
211
194
|
}
|
|
212
195
|
// Annotate the CommonJS export names for ESM import in node:
|
|
213
196
|
0 && (module.exports = {
|
|
197
|
+
createMiddleware,
|
|
214
198
|
getOrigin
|
|
215
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;
|
|
@@ -55,7 +55,7 @@ function getOrigin(req, protocol, host, trustProxy) {
|
|
|
55
55
|
}
|
|
56
56
|
return `${protocol}://${host}`;
|
|
57
57
|
}
|
|
58
|
-
function createMiddleware(
|
|
58
|
+
function createMiddleware(fetch2, options = {}) {
|
|
59
59
|
const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
|
|
60
60
|
let { origin = process.env.ORIGIN } = options;
|
|
61
61
|
let protocol;
|
|
@@ -68,45 +68,28 @@ function createMiddleware(router, options = {}) {
|
|
|
68
68
|
origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
|
|
69
69
|
const url = new URL(req.url, origin);
|
|
70
70
|
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
71
|
-
const
|
|
71
|
+
const headers = req.headers;
|
|
72
|
+
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
73
|
+
start(controller) {
|
|
74
|
+
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
75
|
+
req.on("end", () => controller.close());
|
|
76
|
+
req.on("error", (err) => controller.error(err));
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
const request = new Request(url, {
|
|
72
80
|
method: req.method,
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
headers,
|
|
82
|
+
body,
|
|
83
|
+
duplex: "half"
|
|
84
|
+
});
|
|
85
|
+
const response = await fetch2(request, {
|
|
86
|
+
ip,
|
|
87
|
+
request: req,
|
|
88
|
+
response: res,
|
|
89
|
+
setCookie(cookie) {
|
|
90
|
+
res.appendHeader("set-cookie", cookie);
|
|
81
91
|
}
|
|
82
|
-
};
|
|
83
|
-
Object.defineProperty(requestContext, "request", {
|
|
84
|
-
get() {
|
|
85
|
-
const headers = req.headers;
|
|
86
|
-
const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
|
|
87
|
-
start(controller) {
|
|
88
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
89
|
-
req.on("end", () => controller.close());
|
|
90
|
-
req.on("error", (err) => controller.error(err));
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
const request = new Request(url, {
|
|
94
|
-
method: req.method,
|
|
95
|
-
headers,
|
|
96
|
-
body,
|
|
97
|
-
duplex: "half"
|
|
98
|
-
});
|
|
99
|
-
Object.defineProperty(this, "request", {
|
|
100
|
-
value: request,
|
|
101
|
-
enumerable: true,
|
|
102
|
-
configurable: true
|
|
103
|
-
});
|
|
104
|
-
return request;
|
|
105
|
-
},
|
|
106
|
-
enumerable: true,
|
|
107
|
-
configurable: true
|
|
108
92
|
});
|
|
109
|
-
const response = await router(requestContext);
|
|
110
93
|
if (!response) {
|
|
111
94
|
if (next) {
|
|
112
95
|
next();
|
|
@@ -183,6 +166,6 @@ function createMiddleware(router, options = {}) {
|
|
|
183
166
|
};
|
|
184
167
|
}
|
|
185
168
|
export {
|
|
186
|
-
createMiddleware
|
|
169
|
+
createMiddleware,
|
|
187
170
|
getOrigin
|
|
188
171
|
};
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import type { HandlerLike, ParamsObject, Route, RouteContext } from "./types";
|
|
2
2
|
declare global {
|
|
3
3
|
namespace Marko {
|
|
4
|
-
interface Global {
|
|
5
|
-
|
|
4
|
+
interface Global extends MarkoRun.CurrentContext {
|
|
5
|
+
}
|
|
6
|
+
interface Out {
|
|
7
|
+
global: Global;
|
|
6
8
|
}
|
|
7
9
|
}
|
|
8
10
|
namespace MarkoRun {
|
|
11
|
+
const NotHandled: symbol;
|
|
12
|
+
const NotMatched: symbol;
|
|
9
13
|
interface CurrentRoute extends Route {
|
|
10
14
|
}
|
|
11
15
|
interface CurrentContext extends RouteContext<CurrentRoute> {
|
|
@@ -14,4 +18,4 @@ declare global {
|
|
|
14
18
|
function route<Params extends ParamsObject = {}, Meta = unknown>(handler: Handler<Params, Meta>): typeof handler;
|
|
15
19
|
}
|
|
16
20
|
}
|
|
17
|
-
export type { HandlerLike, InputObject,
|
|
21
|
+
export type { Fetch, HandlerLike, InputObject, Invoke, Match, NextFunction, PathTemplate, Route, RouteContext, RouteContextExtensions, RouteHandler, RouteWithHandler, RuntimeModule, ValidateHref, ValidatePath, } from "./types";
|
|
@@ -20,8 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/runtime/internal.ts
|
|
21
21
|
var internal_exports = {};
|
|
22
22
|
__export(internal_exports, {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
NotHandled: () => NotHandled,
|
|
24
|
+
NotMatched: () => NotMatched,
|
|
25
25
|
call: () => call,
|
|
26
26
|
compose: () => compose,
|
|
27
27
|
createInput: () => createInput,
|
|
@@ -31,17 +31,20 @@ __export(internal_exports, {
|
|
|
31
31
|
notMatched: () => notMatched
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(internal_exports);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
var NotHandled = Symbol();
|
|
35
|
+
var NotMatched = Symbol();
|
|
36
|
+
globalThis.MarkoRun ?? (globalThis.MarkoRun = {
|
|
37
|
+
NotHandled,
|
|
38
|
+
NotMatched,
|
|
39
|
+
route(handler) {
|
|
40
|
+
return handler;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
38
43
|
function createInput(context) {
|
|
39
44
|
let existing;
|
|
40
45
|
return (data) => {
|
|
41
46
|
existing ?? (existing = {
|
|
42
|
-
$global:
|
|
43
|
-
context
|
|
44
|
-
}
|
|
47
|
+
$global: context
|
|
45
48
|
});
|
|
46
49
|
return data ? Object.assign(existing, data) : existing;
|
|
47
50
|
};
|
|
@@ -78,7 +81,7 @@ async function call(handler, next, context) {
|
|
|
78
81
|
response = await handler(context, next);
|
|
79
82
|
} catch (error) {
|
|
80
83
|
if (error == null) {
|
|
81
|
-
throw
|
|
84
|
+
throw NotHandled;
|
|
82
85
|
} else if (error instanceof Response) {
|
|
83
86
|
return error;
|
|
84
87
|
}
|
|
@@ -86,7 +89,7 @@ async function call(handler, next, context) {
|
|
|
86
89
|
}
|
|
87
90
|
}
|
|
88
91
|
if (response === null) {
|
|
89
|
-
throw
|
|
92
|
+
throw NotMatched;
|
|
90
93
|
}
|
|
91
94
|
return response || next();
|
|
92
95
|
}
|
|
@@ -136,8 +139,8 @@ function notMatched() {
|
|
|
136
139
|
}
|
|
137
140
|
// Annotate the CommonJS export names for ESM import in node:
|
|
138
141
|
0 && (module.exports = {
|
|
139
|
-
|
|
140
|
-
|
|
142
|
+
NotHandled,
|
|
143
|
+
NotMatched,
|
|
141
144
|
call,
|
|
142
145
|
compose,
|
|
143
146
|
createInput,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { InputObject, NextFunction, Route, RouteContext, RouteHandler } from "./types";
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const
|
|
2
|
+
export declare const NotHandled: unique symbol;
|
|
3
|
+
export declare const NotMatched: unique symbol;
|
|
4
4
|
export declare function createInput(context: RouteContext): (data: InputObject) => InputObject;
|
|
5
5
|
export declare function call(handler: RouteHandler<Route>, next: NextFunction, context: RouteContext): Promise<Response>;
|
|
6
6
|
export declare function compose(handlers: RouteHandler[]): RouteHandler;
|