@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.
@@ -1,12 +1,32 @@
1
- // src/adapter/middleware.ts
2
- import * as webStream from "stream/web";
3
- import installCrypto from "@hattip/polyfills/crypto";
4
- installCrypto();
5
- for (const key of Object.keys(webStream)) {
6
- if (!(key in global)) {
7
- global[key] = webStream[key];
1
+ // src/adapter/polyfill.ts
2
+ import { ReadableStream as ReadableStream2, TransformStream, WritableStream } from "stream/web";
3
+ import { webcrypto as crypto } from "crypto";
4
+ import { fetch, Response, Request as Request2, Headers, FormData, File } from "undici";
5
+ var globals = {
6
+ crypto,
7
+ fetch,
8
+ Response,
9
+ Request: Request2,
10
+ Headers,
11
+ ReadableStream: ReadableStream2,
12
+ TransformStream,
13
+ WritableStream,
14
+ FormData,
15
+ File
16
+ };
17
+ function installPolyfills() {
18
+ for (const name in globals) {
19
+ Object.defineProperty(globalThis, name, {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: globals[name]
24
+ });
8
25
  }
9
26
  }
27
+
28
+ // src/adapter/middleware.ts
29
+ installPolyfills();
10
30
  function getForwardedHeader(req, name) {
11
31
  const value = req.headers["x-forwarded-" + name];
12
32
  if (value) {
@@ -35,7 +55,7 @@ function getOrigin(req, protocol, host, trustProxy) {
35
55
  }
36
56
  return `${protocol}://${host}`;
37
57
  }
38
- function createMiddleware(router, options = {}) {
58
+ function createMiddleware(fetch2, options = {}) {
39
59
  const { trustProxy = process.env.TRUST_PROXY === "1" } = options;
40
60
  let { origin = process.env.ORIGIN } = options;
41
61
  let protocol;
@@ -48,45 +68,28 @@ function createMiddleware(router, options = {}) {
48
68
  origin ?? (origin = getOrigin(req, protocol, host, trustProxy));
49
69
  const url = new URL(req.url, origin);
50
70
  const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
51
- const requestContext = {
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, {
52
80
  method: req.method,
53
- url,
54
- platform: {
55
- ip,
56
- request: req,
57
- response: res,
58
- setCookie(cookie) {
59
- res.appendHeader("set-cookie", cookie);
60
- }
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);
61
91
  }
62
- };
63
- Object.defineProperty(requestContext, "request", {
64
- get() {
65
- const headers = req.headers;
66
- const body = req.method === "GET" || req.method === "HEAD" ? void 0 : req.socket ? req : new ReadableStream({
67
- start(controller) {
68
- req.on("data", (chunk) => controller.enqueue(chunk));
69
- req.on("end", () => controller.close());
70
- req.on("error", (err) => controller.error(err));
71
- }
72
- });
73
- const request = new Request(url, {
74
- method: req.method,
75
- headers,
76
- body,
77
- duplex: "half"
78
- });
79
- Object.defineProperty(this, "request", {
80
- value: request,
81
- enumerable: true,
82
- configurable: true
83
- });
84
- return request;
85
- },
86
- enumerable: true,
87
- configurable: true
88
92
  });
89
- const response = await router(requestContext);
90
93
  if (!response) {
91
94
  if (next) {
92
95
  next();
@@ -163,6 +166,6 @@ function createMiddleware(router, options = {}) {
163
166
  };
164
167
  }
165
168
  export {
166
- createMiddleware as default,
169
+ createMiddleware,
167
170
  getOrigin
168
171
  };
@@ -0,0 +1 @@
1
+ export declare function installPolyfills(): void;
@@ -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
- context: RouteContext;
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, InvokeRoute, MatchRoute, NextFunction, PathTemplate, RequestContext, Route, RouteContext, RouteContextExtensions, RouteHandler, RouteWithHandler, Router, ValidateHref, ValidatePath, } from "./types";
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
- RequestNotHandled: () => RequestNotHandled,
24
- RequestNotMatched: () => RequestNotMatched,
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
- globalThis.MarkoRun ?? (globalThis.MarkoRun = {});
35
- globalThis.MarkoRun.route = (handler) => handler;
36
- var RequestNotHandled = Symbol();
37
- var RequestNotMatched = Symbol();
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 RequestNotHandled;
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 RequestNotMatched;
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
- RequestNotHandled,
140
- RequestNotMatched,
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 RequestNotHandled: unique symbol;
3
- export declare const RequestNotMatched: unique symbol;
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;
@@ -1,15 +1,18 @@
1
1
  // src/runtime/internal.ts
2
- globalThis.MarkoRun ?? (globalThis.MarkoRun = {});
3
- globalThis.MarkoRun.route = (handler) => handler;
4
- var RequestNotHandled = Symbol();
5
- var RequestNotMatched = Symbol();
2
+ var NotHandled = Symbol();
3
+ var NotMatched = Symbol();
4
+ globalThis.MarkoRun ?? (globalThis.MarkoRun = {
5
+ NotHandled,
6
+ NotMatched,
7
+ route(handler) {
8
+ return handler;
9
+ }
10
+ });
6
11
  function createInput(context) {
7
12
  let existing;
8
13
  return (data) => {
9
14
  existing ?? (existing = {
10
- $global: {
11
- context
12
- }
15
+ $global: context
13
16
  });
14
17
  return data ? Object.assign(existing, data) : existing;
15
18
  };
@@ -46,7 +49,7 @@ async function call(handler, next, context) {
46
49
  response = await handler(context, next);
47
50
  } catch (error) {
48
51
  if (error == null) {
49
- throw RequestNotHandled;
52
+ throw NotHandled;
50
53
  } else if (error instanceof Response) {
51
54
  return error;
52
55
  }
@@ -54,7 +57,7 @@ async function call(handler, next, context) {
54
57
  }
55
58
  }
56
59
  if (response === null) {
57
- throw RequestNotMatched;
60
+ throw NotMatched;
58
61
  }
59
62
  return response || next();
60
63
  }
@@ -103,8 +106,8 @@ function notMatched() {
103
106
  return null;
104
107
  }
105
108
  export {
106
- RequestNotHandled,
107
- RequestNotMatched,
109
+ NotHandled,
110
+ NotMatched,
108
111
  call,
109
112
  compose,
110
113
  createInput,
@@ -20,9 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/runtime/router.ts
21
21
  var router_exports = {};
22
22
  __export(router_exports, {
23
- invokeRoute: () => invokeRoute,
24
- matchRoute: () => matchRoute,
25
- router: () => router
23
+ fetch: () => fetch,
24
+ invoke: () => invoke,
25
+ match: () => match
26
26
  });
27
27
  module.exports = __toCommonJS(router_exports);
28
28
  function notImplemented() {
@@ -30,12 +30,12 @@ function notImplemented() {
30
30
  "This should have been replaced by the @marko/run plugin at build/dev time"
31
31
  );
32
32
  }
33
- var router = notImplemented;
34
- var matchRoute = notImplemented;
35
- var invokeRoute = notImplemented;
33
+ var fetch = notImplemented;
34
+ var match = notImplemented;
35
+ var invoke = notImplemented;
36
36
  // Annotate the CommonJS export names for ESM import in node:
37
37
  0 && (module.exports = {
38
- invokeRoute,
39
- matchRoute,
40
- router
38
+ fetch,
39
+ invoke,
40
+ match
41
41
  });
@@ -1,4 +1,3 @@
1
- import type { MatchRoute, RequestContext, Route } from "./types";
2
- export declare const router: <T>(context: RequestContext<T>) => Promise<Response | void>;
3
- export declare const matchRoute: MatchRoute;
4
- export declare const invokeRoute: <T>(route: Route | null, context: RequestContext<T>) => Promise<Response | void>;
1
+ export declare const fetch: <Platform = unknown>(request: Request, platform: Platform) => Promise<void | Response>;
2
+ export declare const match: (method: string, pathname: string) => import("./types").RouteWithHandler<{}, unknown, string> | null;
3
+ export declare const invoke: <Platform = unknown>(route: import("./types").RouteWithHandler<{}, unknown, string> | null, request: Request, platform: Platform) => Promise<void | Response>;
@@ -4,11 +4,11 @@ function notImplemented() {
4
4
  "This should have been replaced by the @marko/run plugin at build/dev time"
5
5
  );
6
6
  }
7
- var router = notImplemented;
8
- var matchRoute = notImplemented;
9
- var invokeRoute = notImplemented;
7
+ var fetch = notImplemented;
8
+ var match = notImplemented;
9
+ var invoke = notImplemented;
10
10
  export {
11
- invokeRoute,
12
- matchRoute,
13
- router
11
+ fetch,
12
+ invoke,
13
+ match
14
14
  };
@@ -7,20 +7,17 @@ export interface RouteContextExtensions {
7
7
  }
8
8
  export declare type ParamsObject = Record<string, string>;
9
9
  export declare type InputObject = Record<PropertyKey, any>;
10
- export interface RequestContext<T = unknown> {
10
+ export declare type RouteContext<Platform = unknown, TRoute extends Route = Route> = TRoute extends any ? Combine<RouteContextExtensions & Readonly<{
11
11
  url: URL;
12
- method: string;
13
12
  request: Request;
14
- platform: T;
15
- }
16
- export declare type RouteContext<TRoute extends Route = Route> = TRoute extends any ? Combine<RouteContextExtensions & Readonly<RequestContext & {
17
13
  route: TRoute["path"];
18
14
  params: TRoute["params"];
19
15
  meta: TRoute["meta"];
16
+ platform: Platform;
20
17
  }>> : never;
21
18
  export declare type NextFunction = () => Awaitable<Response>;
22
19
  export declare type HandlerLike<TRoute extends Route = Route> = Awaitable<OneOrMany<RouteHandler<TRoute>>>;
23
- export declare type RouteHandler<TRoute extends Route = Route> = (context: RouteContext<TRoute>, next: NextFunction) => Awaitable<Response | null | void>;
20
+ export declare type RouteHandler<TRoute extends Route = Route> = (context: RouteContext<unknown, TRoute>, next: NextFunction) => Awaitable<Response | null | void>;
24
21
  export interface Route<Params extends ParamsObject = {}, Meta = unknown, Path extends string = string> {
25
22
  path: Path;
26
23
  params: Params;
@@ -29,9 +26,14 @@ export interface Route<Params extends ParamsObject = {}, Meta = unknown, Path ex
29
26
  export interface RouteWithHandler<Params extends ParamsObject = {}, Meta = unknown, Path extends string = string> extends Route<Params, Meta, Path> {
30
27
  handler: RouteHandler<this>;
31
28
  }
32
- export declare type MatchRoute = (method: string, pathname: string) => RouteWithHandler | null;
33
- export declare type Router<T = unknown> = (context: RequestContext<T>) => Promise<Response | void>;
34
- export declare type InvokeRoute<T = unknown> = (route: Route | null, context: RequestContext<T>) => Promise<Response | void>;
29
+ export declare type Fetch<Platform = unknown> = (request: Request, platform: Platform) => Promise<Response | void>;
30
+ export declare type Match = (method: string, pathname: string) => RouteWithHandler | null;
31
+ export declare type Invoke = <Platform = unknown>(route: RouteWithHandler | null, request: Request, platform: Platform) => Promise<Response | void>;
32
+ export interface RuntimeModule {
33
+ fetch: <Platform = unknown>(request: Request, platform: Platform) => Promise<Response | void>;
34
+ match: (method: string, pathname: string) => RouteWithHandler | null;
35
+ invoke: <Platform = unknown>(route: RouteWithHandler | null, request: Request, platform: Platform) => Promise<Response | void>;
36
+ }
35
37
  declare type Member<T, U> = T extends T ? (U extends T ? T : never) : never;
36
38
  declare type Segments<T extends string, Acc extends string[] = []> = T extends "" ? Acc : T extends `${infer Left}/${infer Rest}` ? Segments<Rest, [...Acc, Left]> : [...Acc, T];
37
39
  declare type GTE<A extends any[], B extends any[]> = A["length"] extends B["length"] ? 1 : A extends [infer _Ha, ...infer Ta] ? B extends [infer _Hb, ...infer Tb] ? GTE<Ta, Tb> : 1 : 0;
@@ -3,4 +3,4 @@ export declare function renderRouteTemplate(route: Route): string;
3
3
  export declare function renderRouteEntry(route: Route): string;
4
4
  export declare function renderRouter(routes: BuiltRoutes, options?: RouterOptions): string;
5
5
  export declare function renderMiddleware(middleware: RoutableFile[]): string;
6
- export declare function renderRouteTypeInfo(routes: BuiltRoutes, pathPrefix?: string): string;
6
+ export declare function renderRouteTypeInfo(routes: BuiltRoutes, pathPrefix?: string, adapterTypes?: string): string;