gemi 0.4.11 → 0.4.13

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 (41) hide show
  1. package/dist/app/App.d.ts +3 -0
  2. package/dist/app/App.d.ts.map +1 -1
  3. package/dist/app/createFlatApiRoutes.d.ts +4 -2
  4. package/dist/app/createFlatApiRoutes.d.ts.map +1 -1
  5. package/dist/app/index.js +186 -182
  6. package/dist/bin/index.js +495 -65
  7. package/dist/client/index.d.ts +1 -0
  8. package/dist/client/index.d.ts.map +1 -1
  9. package/dist/client/index.js +37 -9
  10. package/dist/client/rpc.d.ts +3 -0
  11. package/dist/client/rpc.d.ts.map +1 -0
  12. package/dist/client/useMutation.d.ts +22 -6
  13. package/dist/client/useMutation.d.ts.map +1 -1
  14. package/dist/client/useQuery.d.ts +14 -4
  15. package/dist/client/useQuery.d.ts.map +1 -1
  16. package/dist/email/EmailServiceProvider.d.ts +8 -0
  17. package/dist/email/EmailServiceProvider.d.ts.map +1 -0
  18. package/dist/email/index.js +89 -44
  19. package/dist/email/providers.d.ts +5 -0
  20. package/dist/email/providers.d.ts.map +1 -0
  21. package/dist/email/types.d.ts +15 -0
  22. package/dist/email/types.d.ts.map +1 -0
  23. package/dist/http/ApiRouter.d.ts +39 -42
  24. package/dist/http/ApiRouter.d.ts.map +1 -1
  25. package/dist/http/Controller.d.ts +5 -4
  26. package/dist/http/Controller.d.ts.map +1 -1
  27. package/dist/http/HttpRequest.d.ts +19 -5
  28. package/dist/http/HttpRequest.d.ts.map +1 -1
  29. package/dist/http/Router.d.ts +1 -1
  30. package/dist/http/Router.d.ts.map +1 -1
  31. package/dist/http/index.d.ts +1 -1
  32. package/dist/http/index.d.ts.map +1 -1
  33. package/dist/http/index.js +159 -157
  34. package/dist/internal/type-utils.d.ts +8 -0
  35. package/dist/internal/type-utils.d.ts.map +1 -0
  36. package/dist/kernel/Kernel.d.ts +11 -0
  37. package/dist/kernel/Kernel.d.ts.map +1 -0
  38. package/dist/kernel/index.d.ts +2 -0
  39. package/dist/kernel/index.d.ts.map +1 -0
  40. package/dist/server/index.js +460 -30
  41. package/package.json +7 -3
package/dist/app/App.d.ts CHANGED
@@ -5,6 +5,7 @@ import type { Plugin } from "./Plugin";
5
5
  import type { Middleware } from "../http/Middleware";
6
6
  import type { ServerWebSocket } from "bun";
7
7
  import { ComponentType } from "react";
8
+ import { Kernel } from "../kernel";
8
9
  interface RenderParams {
9
10
  styles: string[];
10
11
  manifest: Record<string, any>;
@@ -17,6 +18,7 @@ interface AppParams {
17
18
  plugins?: (new () => Plugin)[];
18
19
  middlewareAliases?: Record<string, new () => Middleware>;
19
20
  root: ComponentType;
21
+ kernel: new () => Kernel;
20
22
  }
21
23
  export declare class App {
22
24
  private renderParams;
@@ -32,6 +34,7 @@ export declare class App {
32
34
  private apiRouter;
33
35
  private viewRouter;
34
36
  private Root;
37
+ private kernel;
35
38
  constructor(params: AppParams);
36
39
  private prepare;
37
40
  getComponentTree(): ComponentTree;
@@ -1 +1 @@
1
- {"version":3,"file":"App.d.ts","sourceRoot":"","sources":["../../app/App.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACjE,OAAO,EAAE,UAAU,EAAsB,MAAM,oBAAoB,CAAC;AAKpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAIrD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACvC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,KAAK,CAAC;AAU3C,OAAO,EAAE,aAAa,EAA2B,MAAM,OAAO,CAAC;AAK/D,UAAU,YAAY;IACpB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9B,cAAc,EAAE,MAAM,C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ACpB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,aAAa,CAAgB;IAC9B,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,UAAU,CAAC,CAAM;IAC7D,UAAU,SAAK;IACtB,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,IAAI,CAAgB;IAC5B,OAAO,CAAC,MAAM,CAAS;gBAEX,MAAM,EAAE,SAAS;IAW7B,OAAO,CAAC,OAAO;IAkCR,gBAAgB;IAIhB,eAAe,CAAC,MAAM,EAAE,YAAY;YAI7B,eAAe;IAqC7B,OAAO,CAAC,aAAa;IAmCf,gBAAgB,CAAC,GAAG,EAAE,OAAO;IAyF7B,iBAAiB,CAAC,GAAG,EAAE,OAAO;IAmL9B,KAAK,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC;IAY5C,OAAO,CAAC,sBAAsB,CAGtB;IACR,OAAO,CAAC,mBAAmB,CAEzB;IACF,OAAO,CAAC,oBAAoB,CAM1B;IAEF,SAAS;sBAdH,eAAe,WACV,MAAM,GAAG,MAAM;mBAES,eAAe;oBAI5C,eAAe,QACb,MAAM,UACJ,MAAM;MASd;CACH"}
@@ -1,8 +1,10 @@
1
1
  import { Middleware } from "../http";
2
- import { ApiRouteChildren, ApiRouteExec } from "../http/ApiRouter";
2
+ import { ApiRoutes } from "../http/ApiRouter";
3
3
  import { RouterMiddleware } from "../http/Router";
4
- export declare function createFlatApiRoutes(routes: ApiRouteChildren): Record<string, Record<string, {
4
+ type ApiRouteExec = any;
5
+ export declare function createFlatApiRoutes(routes: ApiRoutes): Record<string, Record<string, {
5
6
  exec: ApiRouteExec;
6
7
  middleware: (string | (new () => Middleware) | RouterMiddleware)[];
7
8
  }>>;
9
+ export {};
8
10
  //# sourceMappingURL=createFlatApiRoutes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createFlatApiRoutes.d.ts","sourceRoot":"","sources":["../../app/createFlatApiRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,gBAAgB;UAM9C,YAAY;gBACN,CAAC,MAAM,GAAG,CAAC,UAAU,UAAU,CAAC,GAAG,gBAAgB,CAAC,EAAE;IAoDzE"}
1
+ {"version":3,"file":"createFlatApiRoutes.d.ts","sourceRoot":"","sources":["../../app/createFlatApiRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAgB,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD,KAAK,YAAY,GAAG,GAAG,CAAC;AAExB,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,SAAS;UAMvC,YAAY;gBACN,CAAC,MAAM,GAAG,CAAC,UAAU,UAAU,CAAC,GAAG,gBAAgB,CAAC,EAAE;IAyDzE"}
package/dist/app/index.js CHANGED
@@ -491,11 +491,148 @@ var require_dist = __commonJS((exports) => {
491
491
 
492
492
  // http/Controller.ts
493
493
  class Controller {
494
- app;
495
494
  requests = {};
496
495
  static kind = "controller";
497
- constructor(app) {
498
- this.app = app;
496
+ constructor() {
497
+ }
498
+ }
499
+ // internal/isConstructor.ts
500
+ function isConstructor(value) {
501
+ return typeof value === "function" && value.prototype !== undefined;
502
+ }
503
+
504
+ // http/ApiRouter.ts
505
+ var isController = function(candidate) {
506
+ return isConstructor(candidate);
507
+ };
508
+
509
+ class RouteHandler {
510
+ method;
511
+ handler;
512
+ methodName;
513
+ middlewares = [];
514
+ constructor(method, handler, methodName) {
515
+ this.method = method;
516
+ this.handler = handler;
517
+ this.methodName = methodName;
518
+ }
519
+ run(req) {
520
+ let httpRequest = req;
521
+ if (isController(this.handler)) {
522
+ const controller = new this.handler;
523
+ const handler = controller[this.methodName].bind(controller);
524
+ httpRequest = controller.requests[this.methodName] ? new controller.requests[this.methodName](req.rawRequest, req.params) : httpRequest;
525
+ return handler(httpRequest);
526
+ } else {
527
+ return this.handler(req);
528
+ }
529
+ }
530
+ middleware(middlewareList) {
531
+ this.middlewares = middlewareList;
532
+ return this;
533
+ }
534
+ }
535
+
536
+ class ApiRouter {
537
+ routes = {};
538
+ middlewares = [];
539
+ middleware(_req) {
540
+ }
541
+ get(handler, methodName) {
542
+ return new RouteHandler("GET", handler, methodName);
543
+ }
544
+ post(handler, methodName) {
545
+ return new RouteHandler("POST", handler, methodName);
546
+ }
547
+ put(handler, methodName) {
548
+ return new RouteHandler("PUT", handler, methodName);
549
+ }
550
+ patch(handler, methodName) {
551
+ return new RouteHandler("PATCH", handler, methodName);
552
+ }
553
+ delete(handler, methodName) {
554
+ return new RouteHandler("DELETE", handler, methodName);
555
+ }
556
+ }
557
+ // http/ViewRouter.ts
558
+ class ViewRouter {
559
+ routes = {};
560
+ middlewares = [];
561
+ middleware(req) {
562
+ }
563
+ layout(viewPath, handler, children) {
564
+ function prepare(middlewares = []) {
565
+ let _children = children ?? {};
566
+ if (handler && handler.constructor === Object) {
567
+ _children = handler;
568
+ }
569
+ return {
570
+ exec: async (req, params, app) => {
571
+ let _handler = () => Promise.resolve({
572
+ data: { [viewPath]: {} },
573
+ headers: {},
574
+ head: {}
575
+ });
576
+ if (typeof handler === "function") {
577
+ _handler = handler;
578
+ }
579
+ if (Array.isArray(handler)) {
580
+ const [controller, methodName] = handler;
581
+ const instance = new controller(app);
582
+ _handler = instance[methodName].bind(instance);
583
+ }
584
+ const data = await _handler(req, params);
585
+ return { [viewPath]: data };
586
+ },
587
+ children: _children,
588
+ viewPath,
589
+ middlewares,
590
+ kind: "layout"
591
+ };
592
+ }
593
+ return {
594
+ prepare,
595
+ middleware: (middlewares) => ({
596
+ prepare: () => prepare(middlewares)
597
+ })
598
+ };
599
+ }
600
+ view(viewPath, handler, children) {
601
+ function prepare(middlewares = []) {
602
+ let _children = children ?? {};
603
+ if (handler && handler.constructor === Object) {
604
+ _children = handler;
605
+ }
606
+ return {
607
+ exec: async (req, params, app) => {
608
+ let _handler = () => Promise.resolve({
609
+ data: { [viewPath]: {} },
610
+ headers: {},
611
+ head: {}
612
+ });
613
+ if (typeof handler === "function") {
614
+ _handler = handler;
615
+ }
616
+ if (Array.isArray(handler)) {
617
+ const [controller, methodName] = handler;
618
+ const instance = new controller(app);
619
+ _handler = instance[methodName].bind(instance);
620
+ }
621
+ const data = await _handler(req, params);
622
+ return { [viewPath]: data };
623
+ },
624
+ children: _children,
625
+ viewPath,
626
+ middlewares,
627
+ kind: "view"
628
+ };
629
+ }
630
+ return {
631
+ prepare,
632
+ middleware: (middlewares) => ({
633
+ prepare: () => prepare(middlewares)
634
+ })
635
+ };
499
636
  }
500
637
  }
501
638
  // http/Error.ts
@@ -555,7 +692,6 @@ class ValidationError extends RequestBreakerError {
555
692
  };
556
693
  }
557
694
  }
558
-
559
695
  // http/HttpRequest.ts
560
696
  var validate = function(ruleName) {
561
697
  const [rule, param] = ruleName.split(":");
@@ -593,12 +729,22 @@ var validate = function(ruleName) {
593
729
  }
594
730
  };
595
731
 
596
- class Input extends Map {
597
- constructor() {
598
- super();
732
+ class Input {
733
+ data;
734
+ constructor(data) {
735
+ this.data = data;
736
+ }
737
+ get(key) {
738
+ return this.data[key];
739
+ }
740
+ set(key, value) {
741
+ this.data[key] = value;
742
+ }
743
+ has(key) {
744
+ return this.data[key] !== undefined;
599
745
  }
600
746
  toJSON() {
601
- return Object.fromEntries(this);
747
+ return this.data;
602
748
  }
603
749
  }
604
750
 
@@ -606,8 +752,10 @@ class HttpRequest {
606
752
  rawRequest;
607
753
  headers;
608
754
  cookies;
609
- schema = {};
610
- constructor(req) {
755
+ schema;
756
+ params;
757
+ constructor(req, params) {
758
+ this.params = params;
611
759
  this.rawRequest = req;
612
760
  this.headers = req.headers;
613
761
  const cookie = this.rawRequest.headers.get("Cookie");
@@ -622,7 +770,7 @@ class HttpRequest {
622
770
  this.cookies = cookies;
623
771
  }
624
772
  async parseBody() {
625
- const inputMap = new Input;
773
+ const inputMap = new Input({});
626
774
  if (this.rawRequest.headers.get("Content-Type") === "application/json") {
627
775
  const body = await this.rawRequest.json();
628
776
  for (const [key, value] of Object.entries(body)) {
@@ -702,152 +850,6 @@ class HttpRequest {
702
850
  throw "not implemented";
703
851
  }
704
852
  }
705
-
706
- // http/ApiRouter.ts
707
- var isController = function(controller) {
708
- return "kind" in controller && controller.kind === "controller";
709
- };
710
-
711
- class ApiRouter {
712
- routes = {};
713
- middlewares = [];
714
- constructor() {
715
- }
716
- middleware(_req) {
717
- }
718
- handleRequest(controller, methodName) {
719
- return (method) => {
720
- const prepare = (middleware = []) => {
721
- return {
722
- middleware,
723
- method,
724
- exec: async (req, params, app) => {
725
- let handler = (_req, params2) => Promise.resolve({});
726
- let httpRequest = new HttpRequest(req.rawRequest);
727
- if (isController(controller)) {
728
- const controllerInstance = new controller(app);
729
- const Req = controllerInstance.requests[methodName] ?? HttpRequest;
730
- httpRequest = new Req(req.rawRequest);
731
- handler = controllerInstance[methodName].bind(controllerInstance);
732
- } else if (typeof controller === "function") {
733
- handler = (req2) => controller(req2);
734
- }
735
- return await handler(httpRequest, params);
736
- }
737
- };
738
- };
739
- return {
740
- prepare,
741
- middleware: (middlware) => {
742
- return {
743
- prepare: () => prepare(middlware)
744
- };
745
- }
746
- };
747
- };
748
- }
749
- get(controller, methodName) {
750
- const handler = this.handleRequest(controller, methodName);
751
- return handler("get");
752
- }
753
- post(controller, methodName) {
754
- const handler = this.handleRequest(controller, methodName);
755
- return handler("post");
756
- }
757
- put(controller, methodName) {
758
- const handler = this.handleRequest(controller, methodName);
759
- return handler("put");
760
- }
761
- delete(controller, methodName) {
762
- const handler = this.handleRequest(controller, methodName);
763
- return handler("delete");
764
- }
765
- patch(controller, methodName) {
766
- const handler = this.handleRequest(controller, methodName);
767
- return handler("patch");
768
- }
769
- }
770
- // http/ViewRouter.ts
771
- class ViewRouter {
772
- routes = {};
773
- middlewares = [];
774
- middleware(req) {
775
- }
776
- layout(viewPath, handler, children) {
777
- function prepare(middlewares = []) {
778
- let _children = children ?? {};
779
- if (handler && handler.constructor === Object) {
780
- _children = handler;
781
- }
782
- return {
783
- exec: async (req, params, app) => {
784
- let _handler = () => Promise.resolve({
785
- data: { [viewPath]: {} },
786
- headers: {},
787
- head: {}
788
- });
789
- if (typeof handler === "function") {
790
- _handler = handler;
791
- }
792
- if (Array.isArray(handler)) {
793
- const [controller, methodName] = handler;
794
- const instance = new controller(app);
795
- _handler = instance[methodName].bind(instance);
796
- }
797
- const data = await _handler(req, params);
798
- return { [viewPath]: data };
799
- },
800
- children: _children,
801
- viewPath,
802
- middlewares,
803
- kind: "layout"
804
- };
805
- }
806
- return {
807
- prepare,
808
- middleware: (middlewares) => ({
809
- prepare: () => prepare(middlewares)
810
- })
811
- };
812
- }
813
- view(viewPath, handler, children) {
814
- function prepare(middlewares = []) {
815
- let _children = children ?? {};
816
- if (handler && handler.constructor === Object) {
817
- _children = handler;
818
- }
819
- return {
820
- exec: async (req, params, app) => {
821
- let _handler = () => Promise.resolve({
822
- data: { [viewPath]: {} },
823
- headers: {},
824
- head: {}
825
- });
826
- if (typeof handler === "function") {
827
- _handler = handler;
828
- }
829
- if (Array.isArray(handler)) {
830
- const [controller, methodName] = handler;
831
- const instance = new controller(app);
832
- _handler = instance[methodName].bind(instance);
833
- }
834
- const data = await _handler(req, params);
835
- return { [viewPath]: data };
836
- },
837
- children: _children,
838
- viewPath,
839
- middlewares,
840
- kind: "view"
841
- };
842
- }
843
- return {
844
- prepare,
845
- middleware: (middlewares) => ({
846
- prepare: () => prepare(middlewares)
847
- })
848
- };
849
- }
850
- }
851
853
  // http/Middleware.ts
852
854
  class Middleware {
853
855
  async run(_req, ctx) {
@@ -1791,29 +1793,34 @@ function createRouteManifest(routes) {
1791
1793
  // app/createFlatApiRoutes.ts
1792
1794
  function createFlatApiRoutes(routes) {
1793
1795
  const flatApiRoutes = {};
1794
- for (const [rootPath, apiConfigOrApiRouter] of Object.entries(routes)) {
1795
- if ("prepare" in apiConfigOrApiRouter) {
1796
+ for (const [rootPath, apiRouteHandlerOrApiRouter] of Object.entries(routes)) {
1797
+ if (apiRouteHandlerOrApiRouter instanceof RouteHandler) {
1798
+ const routeHandler = apiRouteHandlerOrApiRouter;
1796
1799
  if (!flatApiRoutes[rootPath]) {
1797
1800
  flatApiRoutes[rootPath] = {};
1798
1801
  }
1799
- const { exec, method, middleware } = apiConfigOrApiRouter.prepare();
1802
+ const method = routeHandler.method;
1803
+ const middleware = routeHandler.middlewares;
1804
+ const exec = routeHandler.run.bind(routeHandler);
1800
1805
  flatApiRoutes[rootPath][method] = {
1801
1806
  exec,
1802
1807
  middleware: [...middleware]
1803
1808
  };
1804
- } else if (Array.isArray(apiConfigOrApiRouter)) {
1805
- for (const apiConfig of apiConfigOrApiRouter) {
1809
+ } else if (Array.isArray(apiRouteHandlerOrApiRouter)) {
1810
+ for (const routeHandler of apiRouteHandlerOrApiRouter) {
1806
1811
  if (!flatApiRoutes[rootPath]) {
1807
1812
  flatApiRoutes[rootPath] = {};
1808
1813
  }
1809
- const { exec, method, middleware } = apiConfig.prepare();
1814
+ const method = routeHandler.method;
1815
+ const middleware = routeHandler.middlewares;
1816
+ const exec = routeHandler.run;
1810
1817
  flatApiRoutes[rootPath][method] = {
1811
1818
  exec,
1812
1819
  middleware
1813
1820
  };
1814
1821
  }
1815
1822
  } else {
1816
- const router = new apiConfigOrApiRouter;
1823
+ const router = new apiRouteHandlerOrApiRouter;
1817
1824
  const result = createFlatApiRoutes(router.routes);
1818
1825
  for (const [path, handlers] of Object.entries(result)) {
1819
1826
  const subPath = path === "/" ? "" : path;
@@ -1841,13 +1848,6 @@ function createFlatApiRoutes(routes) {
1841
1848
  // app/App.ts
1842
1849
  import {renderToReadableStream} from "react-dom/server.browser";
1843
1850
  import {createElement, Fragment} from "react";
1844
-
1845
- // internal/isConstructor.ts
1846
- function isConstructor(value) {
1847
- return typeof value === "function" && value.prototype !== undefined;
1848
- }
1849
-
1850
- // app/App.ts
1851
1851
  class App {
1852
1852
  renderParams = {
1853
1853
  styles: [],
@@ -1867,11 +1867,13 @@ class App {
1867
1867
  apiRouter;
1868
1868
  viewRouter;
1869
1869
  Root;
1870
+ kernel;
1870
1871
  constructor(params) {
1871
1872
  this.params = params;
1872
1873
  this.apiRouter = params.apiRouter;
1873
1874
  this.viewRouter = params.viewRouter;
1874
1875
  this.Root = params.root;
1876
+ this.kernel = new params.kernel;
1875
1877
  this.prepare();
1876
1878
  this.appId = generateETag(Date.now());
1877
1879
  }
@@ -1929,7 +1931,7 @@ class App {
1929
1931
  const reqWithMiddlewares = this.runMiddleware(middlewares);
1930
1932
  const reqCtx = new Map;
1931
1933
  const data = await requestContext.run(reqCtx, async () => {
1932
- await reqWithMiddlewares(new HttpRequest(req), reqCtx);
1934
+ await reqWithMiddlewares(new HttpRequest(req, params), reqCtx);
1933
1935
  return await Promise.all(handlers.map((fn) => fn(req, params, this)));
1934
1936
  });
1935
1937
  return {
@@ -1970,15 +1972,15 @@ class App {
1970
1972
  const pattern = new me({ pathname: path });
1971
1973
  if (pattern.test({ pathname: apiPath })) {
1972
1974
  const params = pattern.exec({ pathname: apiPath })?.pathname.groups;
1973
- if (!handler[req.method.toLowerCase()]) {
1975
+ if (!handler[req.method]) {
1974
1976
  return new Response(JSON.stringify({ error: { message: "Not found" } }));
1975
1977
  }
1976
- const exec = handler[req.method.toLowerCase()].exec;
1977
- const middlewares = handler[req.method.toLowerCase()].middleware;
1978
+ const exec = handler[req.method].exec;
1979
+ const middlewares = handler[req.method].middleware;
1978
1980
  const reqWithMiddlewares = this.runMiddleware(middlewares);
1979
1981
  const reqCtx = new Map;
1980
1982
  return await requestContext.run(reqCtx, async () => {
1981
- const httpRequest = new HttpRequest(req);
1983
+ const httpRequest = new HttpRequest(req, params);
1982
1984
  let handler2 = exec ? () => exec(httpRequest, params, this) : () => Promise.resolve({});
1983
1985
  try {
1984
1986
  await reqWithMiddlewares(httpRequest, reqCtx);
@@ -2056,7 +2058,7 @@ class App {
2056
2058
  const reqWithMiddlewares = this.runMiddleware(middlewares);
2057
2059
  const reqCtx = new Map;
2058
2060
  const data2 = await requestContext.run(reqCtx, async () => {
2059
- await reqWithMiddlewares(new HttpRequest(req), reqCtx);
2061
+ await reqWithMiddlewares(new HttpRequest(req, params2), reqCtx);
2060
2062
  return await Promise.all(handlers.map((fn) => fn(req, params2, this)));
2061
2063
  });
2062
2064
  pageData = {
@@ -2179,11 +2181,13 @@ class App {
2179
2181
  }
2180
2182
  async fetch(req) {
2181
2183
  const url = new URL(req.url);
2182
- if (url.pathname.startsWith("/api")) {
2183
- return await this.handleApiRequest(req);
2184
- } else {
2185
- return await this.handleViewRequest(req);
2186
- }
2184
+ return this.kernel.run(async () => {
2185
+ if (url.pathname.startsWith("/api")) {
2186
+ return await this.handleApiRequest(req);
2187
+ } else {
2188
+ return await this.handleViewRequest(req);
2189
+ }
2190
+ });
2187
2191
  }
2188
2192
  handleWebSocketMessage = (ws, message) => {
2189
2193
  };