@marko/run 0.0.1-beta1 → 0.0.1-beta10

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.
Files changed (45) hide show
  1. package/README.md +385 -131
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/adapter/default-entry.mjs +14 -5
  4. package/dist/adapter/dev-server.d.ts +1 -1
  5. package/dist/adapter/index.cjs +34 -10
  6. package/dist/adapter/index.d.ts +1 -0
  7. package/dist/adapter/index.js +34 -10
  8. package/dist/adapter/middleware.cjs +237 -0
  9. package/dist/adapter/middleware.d.ts +55 -0
  10. package/dist/adapter/middleware.js +209 -0
  11. package/dist/adapter/polyfill.d.ts +6 -0
  12. package/dist/cli/default.config.mjs +2 -2
  13. package/dist/cli/index.mjs +90 -59
  14. package/dist/runtime/index.cjs +0 -16
  15. package/dist/runtime/index.d.ts +21 -2
  16. package/dist/runtime/index.js +0 -7
  17. package/dist/runtime/internal.cjs +160 -0
  18. package/dist/runtime/internal.d.ts +11 -0
  19. package/dist/runtime/internal.js +126 -0
  20. package/dist/runtime/router.cjs +12 -10
  21. package/dist/runtime/router.d.ts +3 -4
  22. package/dist/runtime/router.js +9 -7
  23. package/dist/runtime/types.d.ts +39 -16
  24. package/dist/vite/codegen/index.d.ts +4 -3
  25. package/dist/vite/codegen/writer.d.ts +1 -1
  26. package/dist/vite/constants.d.ts +3 -2
  27. package/dist/vite/index.cjs +715 -312
  28. package/dist/vite/index.d.ts +4 -3
  29. package/dist/vite/index.js +712 -312
  30. package/dist/vite/types.d.ts +14 -7
  31. package/dist/vite/utils/config.d.ts +5 -3
  32. package/dist/vite/utils/route.d.ts +1 -1
  33. package/dist/vite/utils/server.d.ts +3 -1
  34. package/package.json +39 -17
  35. package/dist/adapter/server-old.d.ts +0 -3
  36. package/dist/adapter/server.d.ts +0 -6
  37. package/dist/adapters/node/index.d.ts +0 -5
  38. package/dist/adapters/node/server.d.ts +0 -3
  39. package/dist/adapters/static/crawler.d.ts +0 -9
  40. package/dist/adapters/static/default-entry.mjs +0 -1
  41. package/dist/adapters/static/index.cjs +0 -371
  42. package/dist/adapters/static/index.d.ts +0 -5
  43. package/dist/adapters/static/index.js +0 -341
  44. package/dist/adapters/static/server.d.ts +0 -3
  45. package/dist/runtime/request.d.ts +0 -4
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/runtime/internal.ts
21
+ var internal_exports = {};
22
+ __export(internal_exports, {
23
+ NotHandled: () => NotHandled,
24
+ NotMatched: () => NotMatched,
25
+ call: () => call,
26
+ compose: () => compose,
27
+ createInput: () => createInput,
28
+ noContent: () => noContent,
29
+ normalize: () => normalize,
30
+ notHandled: () => notHandled,
31
+ notMatched: () => notMatched,
32
+ pageResponse: () => pageResponse
33
+ });
34
+ module.exports = __toCommonJS(internal_exports);
35
+ var pageResponseInit = {
36
+ status: 200,
37
+ headers: { "content-type": "text/html;charset=UTF-8" }
38
+ };
39
+ function pageResponse(template, input) {
40
+ return new Response(template.stream(input), pageResponseInit);
41
+ }
42
+ var NotHandled = Symbol();
43
+ var NotMatched = Symbol();
44
+ globalThis.MarkoRun ?? (globalThis.MarkoRun = {
45
+ NotHandled,
46
+ NotMatched,
47
+ route(handler) {
48
+ return handler;
49
+ }
50
+ });
51
+ function createInput(context) {
52
+ let existing;
53
+ return (data) => {
54
+ existing ?? (existing = {
55
+ $global: context
56
+ });
57
+ return data ? Object.assign(existing, data) : existing;
58
+ };
59
+ }
60
+ async function call(handler, next, context) {
61
+ let response;
62
+ if (process.env.NODE_ENV !== "production") {
63
+ let nextCallCount = 0;
64
+ let didThrow = false;
65
+ try {
66
+ response = await handler(context, () => {
67
+ nextCallCount++;
68
+ return next();
69
+ });
70
+ } catch (error) {
71
+ didThrow = true;
72
+ if (error instanceof Response) {
73
+ return error;
74
+ }
75
+ throw error;
76
+ } finally {
77
+ if (!response && !didThrow && nextCallCount > 0) {
78
+ console.warn(
79
+ `Handler '${handler.name}' called its next function but no response was returned. This will cause the next function to be called again which is wasteful. Either return or throw the result of calling \`next\`, return or throw a new Response object or finally \`throw null\` to skip handling the request`
80
+ );
81
+ } else if (nextCallCount > 1) {
82
+ console.warn(
83
+ `Handler '${handler.name}' called its next function more than once. Make sure this is intentional because it is inefficient.`
84
+ );
85
+ }
86
+ }
87
+ } else {
88
+ try {
89
+ response = await handler(context, next);
90
+ } catch (error) {
91
+ if (error == null) {
92
+ throw NotHandled;
93
+ } else if (error instanceof Response) {
94
+ return error;
95
+ }
96
+ throw error;
97
+ }
98
+ }
99
+ if (response === null) {
100
+ throw NotMatched;
101
+ }
102
+ return response || next();
103
+ }
104
+ function compose(handlers) {
105
+ const len = handlers.length;
106
+ if (!len) {
107
+ return (_context, next) => next();
108
+ } else if (len === 1) {
109
+ return handlers[0];
110
+ }
111
+ return (context, next) => {
112
+ let i = 0;
113
+ return function nextHandler() {
114
+ return i < len ? call(handlers[i++], nextHandler, context) : next();
115
+ }();
116
+ };
117
+ }
118
+ function normalize(obj) {
119
+ if (typeof obj === "function") {
120
+ return obj;
121
+ } else if (Array.isArray(obj)) {
122
+ return compose(obj);
123
+ } else if (obj instanceof Promise) {
124
+ const promise = obj.then((value) => {
125
+ fn = Array.isArray(value) ? compose(value) : value;
126
+ });
127
+ let fn = async (context, next) => {
128
+ await promise;
129
+ return fn(context, next);
130
+ };
131
+ return (context, next) => fn(context, next);
132
+ }
133
+ throw new Error(
134
+ `Invalid handler - expected function, array or Promise but received ${obj}`
135
+ );
136
+ }
137
+ function noContent() {
138
+ return new Response(null, {
139
+ status: 204
140
+ });
141
+ }
142
+ function notHandled() {
143
+ throw null;
144
+ }
145
+ function notMatched() {
146
+ return null;
147
+ }
148
+ // Annotate the CommonJS export names for ESM import in node:
149
+ 0 && (module.exports = {
150
+ NotHandled,
151
+ NotMatched,
152
+ call,
153
+ compose,
154
+ createInput,
155
+ noContent,
156
+ normalize,
157
+ notHandled,
158
+ notMatched,
159
+ pageResponse
160
+ });
@@ -0,0 +1,11 @@
1
+ import type { InputObject, NextFunction, Route, Context, RouteHandler } from "./types";
2
+ export declare function pageResponse(template: any, input: Record<PropertyKey, unknown>): Response;
3
+ export declare const NotHandled: typeof MarkoRun.NotHandled;
4
+ export declare const NotMatched: typeof MarkoRun.NotMatched;
5
+ export declare function createInput(context: Context): (data: InputObject) => InputObject;
6
+ export declare function call(handler: RouteHandler<Route>, next: NextFunction, context: Context): Promise<Response>;
7
+ export declare function compose(handlers: RouteHandler[]): RouteHandler;
8
+ export declare function normalize(obj: RouteHandler | RouteHandler[] | Promise<RouteHandler | RouteHandler[]>): RouteHandler;
9
+ export declare function noContent(): Response;
10
+ export declare function notHandled(): void;
11
+ export declare function notMatched(): null;
@@ -0,0 +1,126 @@
1
+ // src/runtime/internal.ts
2
+ var pageResponseInit = {
3
+ status: 200,
4
+ headers: { "content-type": "text/html;charset=UTF-8" }
5
+ };
6
+ function pageResponse(template, input) {
7
+ return new Response(template.stream(input), pageResponseInit);
8
+ }
9
+ var NotHandled = Symbol();
10
+ var NotMatched = Symbol();
11
+ globalThis.MarkoRun ?? (globalThis.MarkoRun = {
12
+ NotHandled,
13
+ NotMatched,
14
+ route(handler) {
15
+ return handler;
16
+ }
17
+ });
18
+ function createInput(context) {
19
+ let existing;
20
+ return (data) => {
21
+ existing ?? (existing = {
22
+ $global: context
23
+ });
24
+ return data ? Object.assign(existing, data) : existing;
25
+ };
26
+ }
27
+ async function call(handler, next, context) {
28
+ let response;
29
+ if (process.env.NODE_ENV !== "production") {
30
+ let nextCallCount = 0;
31
+ let didThrow = false;
32
+ try {
33
+ response = await handler(context, () => {
34
+ nextCallCount++;
35
+ return next();
36
+ });
37
+ } catch (error) {
38
+ didThrow = true;
39
+ if (error instanceof Response) {
40
+ return error;
41
+ }
42
+ throw error;
43
+ } finally {
44
+ if (!response && !didThrow && nextCallCount > 0) {
45
+ console.warn(
46
+ `Handler '${handler.name}' called its next function but no response was returned. This will cause the next function to be called again which is wasteful. Either return or throw the result of calling \`next\`, return or throw a new Response object or finally \`throw null\` to skip handling the request`
47
+ );
48
+ } else if (nextCallCount > 1) {
49
+ console.warn(
50
+ `Handler '${handler.name}' called its next function more than once. Make sure this is intentional because it is inefficient.`
51
+ );
52
+ }
53
+ }
54
+ } else {
55
+ try {
56
+ response = await handler(context, next);
57
+ } catch (error) {
58
+ if (error == null) {
59
+ throw NotHandled;
60
+ } else if (error instanceof Response) {
61
+ return error;
62
+ }
63
+ throw error;
64
+ }
65
+ }
66
+ if (response === null) {
67
+ throw NotMatched;
68
+ }
69
+ return response || next();
70
+ }
71
+ function compose(handlers) {
72
+ const len = handlers.length;
73
+ if (!len) {
74
+ return (_context, next) => next();
75
+ } else if (len === 1) {
76
+ return handlers[0];
77
+ }
78
+ return (context, next) => {
79
+ let i = 0;
80
+ return function nextHandler() {
81
+ return i < len ? call(handlers[i++], nextHandler, context) : next();
82
+ }();
83
+ };
84
+ }
85
+ function normalize(obj) {
86
+ if (typeof obj === "function") {
87
+ return obj;
88
+ } else if (Array.isArray(obj)) {
89
+ return compose(obj);
90
+ } else if (obj instanceof Promise) {
91
+ const promise = obj.then((value) => {
92
+ fn = Array.isArray(value) ? compose(value) : value;
93
+ });
94
+ let fn = async (context, next) => {
95
+ await promise;
96
+ return fn(context, next);
97
+ };
98
+ return (context, next) => fn(context, next);
99
+ }
100
+ throw new Error(
101
+ `Invalid handler - expected function, array or Promise but received ${obj}`
102
+ );
103
+ }
104
+ function noContent() {
105
+ return new Response(null, {
106
+ status: 204
107
+ });
108
+ }
109
+ function notHandled() {
110
+ throw null;
111
+ }
112
+ function notMatched() {
113
+ return null;
114
+ }
115
+ export {
116
+ NotHandled,
117
+ NotMatched,
118
+ call,
119
+ compose,
120
+ createInput,
121
+ noContent,
122
+ normalize,
123
+ notHandled,
124
+ notMatched,
125
+ pageResponse
126
+ };
@@ -20,20 +20,22 @@ 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
- getMatchedRoute: () => getMatchedRoute,
24
- handler: () => handler,
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() {
29
- throw new Error("This should have been replaced by the @marko/run plugin at build/dev time");
29
+ throw new Error(
30
+ "This should have been replaced by the @marko/run plugin at build/dev time"
31
+ );
30
32
  }
31
- var handler = notImplemented;
32
- var router = notImplemented;
33
- var getMatchedRoute = notImplemented;
33
+ var fetch = notImplemented;
34
+ var match = notImplemented;
35
+ var invoke = notImplemented;
34
36
  // Annotate the CommonJS export names for ESM import in node:
35
37
  0 && (module.exports = {
36
- getMatchedRoute,
37
- handler,
38
- router
38
+ fetch,
39
+ invoke,
40
+ match
39
41
  });
@@ -1,4 +1,3 @@
1
- import type { Handler, RouteMatcher, Router } from './types';
2
- export declare const handler: Handler;
3
- export declare const router: Router;
4
- export declare const getMatchedRoute: RouteMatcher;
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>;
@@ -1,12 +1,14 @@
1
1
  // src/runtime/router.ts
2
2
  function notImplemented() {
3
- throw new Error("This should have been replaced by the @marko/run plugin at build/dev time");
3
+ throw new Error(
4
+ "This should have been replaced by the @marko/run plugin at build/dev time"
5
+ );
4
6
  }
5
- var handler = notImplemented;
6
- var router = notImplemented;
7
- var getMatchedRoute = notImplemented;
7
+ var fetch = notImplemented;
8
+ var match = notImplemented;
9
+ var invoke = notImplemented;
8
10
  export {
9
- getMatchedRoute,
10
- handler,
11
- router
11
+ fetch,
12
+ invoke,
13
+ match
12
14
  };
@@ -1,22 +1,45 @@
1
- import type { AdapterRequestContext } from '@hattip/core';
2
- export interface RouteContext<Params extends Record<string, string> = {}, Meta = unknown> {
3
- request: Request;
1
+ declare type Awaitable<T> = Promise<T> | T;
2
+ declare type OneOrMany<T> = T | T[];
3
+ declare type Combine<T> = T extends object ? {
4
+ [P in keyof T]: T[P];
5
+ } : T;
6
+ export interface ContextExtensions {
7
+ }
8
+ export declare type ParamsObject = Record<string, string>;
9
+ export declare type InputObject = Record<PropertyKey, any>;
10
+ export declare type Context<Platform = unknown, TRoute extends Route = Route> = TRoute extends any ? Combine<ContextExtensions & Readonly<{
4
11
  url: URL;
12
+ request: Request;
13
+ route: TRoute["path"];
14
+ params: TRoute["params"];
15
+ meta: TRoute["meta"];
16
+ platform: Platform;
17
+ }>> : never;
18
+ export declare type NextFunction = () => Awaitable<Response>;
19
+ export declare type HandlerLike<TRoute extends Route = Route> = Awaitable<OneOrMany<RouteHandler<TRoute>>>;
20
+ export declare type RouteHandler<TRoute extends Route = Route> = (context: Context<unknown, TRoute>, next: NextFunction) => Awaitable<Response | null | void>;
21
+ export interface Route<Params extends ParamsObject = {}, Meta = unknown, Path extends string = string> {
22
+ path: Path;
5
23
  params: Params;
6
24
  meta: Meta;
7
- error?: unknown;
8
25
  }
9
- export declare type RouteHandler<Params extends Record<string, string> = {}, Meta = unknown> = (context: RouteContext<Params, Meta>) => Promise<Response>;
10
- export interface MatchedRoute<Params extends Record<string, string> = {}, Meta = unknown> {
11
- handler: RouteHandler;
12
- params: Params;
13
- meta: Meta;
14
- invoke: Router;
26
+ export interface RouteWithHandler<Params extends ParamsObject = {}, Meta = unknown, Path extends string = string> extends Route<Params, Meta, Path> {
27
+ handler: RouteHandler<this>;
15
28
  }
16
- export declare type Router = (request: Request) => Promise<Response>;
17
- export declare type RouteMatcher = (method: string, url: URL) => MatchedRoute | null;
18
- export declare type Handler = (context: AdapterRequestContext) => Promise<Response>;
19
- export interface Runtime {
20
- router: Router;
21
- getMatchedRoute: RouteMatcher;
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>;
22
36
  }
37
+ declare type Member<T, U> = T extends T ? (U extends T ? T : never) : never;
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];
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;
40
+ declare type MatchSegments<A extends string, B extends string> = A extends `${infer P}/${string}*` ? 1 extends GTE<Segments<B>, Segments<P>> ? `${P}/${string}` : never : Segments<B>["length"] extends Segments<A>["length"] ? A : never;
41
+ declare type PathPattern<T extends string> = T extends `${infer Left}/\${${string}}/${infer Rest}` ? PathPattern<`${Left}/${string}/${Rest}`> : T extends `${infer Left}/\${...${string}}` ? PathPattern<`${Left}/${string}*`> : T extends `${infer Left}/\${${string}}` ? PathPattern<`${Left}/${string}`> : T;
42
+ export declare type PathTemplate<Path extends string> = Path extends `${infer Left}/\${${string}}/${infer Rest}` ? PathTemplate<`${Left}/${string}/${Rest}`> : Path extends `${infer Left}/\${${string}}` ? PathTemplate<`${Left}/${string}`> : Path;
43
+ export declare type ValidatePath<Paths extends string, Path extends string> = Paths | (Path extends `/${string}` ? MatchSegments<Member<PathPattern<Paths>, Path>, Path> : Path);
44
+ export declare type ValidateHref<Paths extends string, Href extends string> = Href extends `${infer P}#${infer H}?${infer Q}` ? `${ValidatePath<Paths, P>}#${H}?${Q}` : Href extends `${infer P}?${infer Q}` ? `${ValidatePath<Paths, P>}?${Q}` : Href extends `${infer P}#${infer H}` ? `${ValidatePath<Paths, P>}#${H}` : ValidatePath<Paths, Href>;
45
+ export {};
@@ -1,5 +1,6 @@
1
- import type { Route, BuiltRoutes, CodegenOptions } from "../types";
2
- export declare const DefaultCodegenOptions: CodegenOptions;
1
+ import type { Route, BuiltRoutes, RoutableFile, RouterOptions } from "../types";
3
2
  export declare function renderRouteTemplate(route: Route): string;
4
3
  export declare function renderRouteEntry(route: Route): string;
5
- export declare function renderRouter(routes: BuiltRoutes, options?: CodegenOptions): string;
4
+ export declare function renderRouter(routes: BuiltRoutes, options?: RouterOptions): string;
5
+ export declare function renderMiddleware(middleware: RoutableFile[]): string;
6
+ export declare function renderRouteTypeInfo(routes: BuiltRoutes, pathPrefix?: string, adapterTypes?: string): string;
@@ -2,7 +2,7 @@ export interface Writer {
2
2
  indent: number;
3
3
  branch(name: string): Writer;
4
4
  join(closeBranches?: boolean): void;
5
- write(data: string): Writer;
5
+ write(data: string, indent?: boolean): Writer;
6
6
  writeLines(...lines: string[]): Writer;
7
7
  writeBlockStart(data: string): Writer;
8
8
  writeBlockEnd(data: string): Writer;
@@ -1,7 +1,8 @@
1
1
  declare type ValuesOf<T> = T[keyof T];
2
- export declare const markoServeFilePrefix = "__marko-serve__";
3
- export declare const virtualFilePrefix = "virtual:marko-serve";
2
+ export declare const markoRunFilePrefix = "__marko-run__";
3
+ export declare const virtualFilePrefix = "virtual:marko-run";
4
4
  export declare const virtualRoutesPrefix: string;
5
+ export declare const virtualRuntimePrefix: string;
5
6
  export declare const httpVerbs: readonly ["get", "post", "put", "delete"];
6
7
  export declare const serverEntryQuery = "?marko-server-entry";
7
8
  export declare const browserEntryQuery = "?marko-browser-entry";