@mapl/web 0.3.0-alpha.2 → 0.3.0-beta.1

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 CHANGED
@@ -13,7 +13,12 @@ const app = router(
13
13
  [ layer.attach('id', () => performance.now()) ],
14
14
 
15
15
  // Routes
16
- [ handle.get('/path', (c) => c.id) ],
16
+ [
17
+ handle.get('/path', (c) => c.id, {
18
+ // Response wrapper
19
+ type: handle.text
20
+ })
21
+ ],
17
22
 
18
23
  // Subrouters
19
24
  { '/api': api }
@@ -85,7 +90,7 @@ const input = await rolldown({
85
90
  module: true,
86
91
  mangle: false,
87
92
  compress: {
88
- // passes should be at least 2
93
+ // passes should be at least 2, recommend 3 - 5
89
94
  passes: 3,
90
95
  },
91
96
  }),
@@ -96,4 +101,4 @@ await input.write({
96
101
  inlineDynamicImports: true,
97
102
  });
98
103
  ```
99
- As of rn only `terser` can DCE `@mapl/web` well.
104
+ As of rn only `terser` can DCE `@mapl/web` patterns.
package/compiler/aot.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { RouterTag } from "../core/index.js";
2
2
  import "../core/context.js";
3
+ export declare const hydrateRouter: (router: RouterTag) => void;
3
4
  declare const _default: (router: RouterTag) => void;
4
5
  export default _default;
package/compiler/aot.js CHANGED
@@ -1 +1 @@
1
- import{hydrateDependency,setHooks}from"@mapl/framework";import{injectExternalDependency,markExported}from"runtime-compiler";import"../core/context.js";import{countParams}from"@mapl/router/path";export default router=>{setHooks({compileHandler:(handler,_,_1,scope)=>{let fn=handler[2];injectExternalDependency(fn);handler[3]?.type?.(``,scope[1]||fn.length>countParams(handler[1]))},compileErrorHandler:(_,fn,dat,scope)=>{injectExternalDependency(fn);dat?.type?.(``,scope[1]||fn.length>1);return``}});hydrateDependency(router,[false,false,,``,false],``);markExported()};
1
+ import{hydrateDependency,setCompileErrorHandlerHook,setCompileHandlerHook}from"@mapl/framework";import{injectExternalDependency,markExported}from"runtime-compiler";import"../core/context.js";import{countParams}from"@mapl/router/path";export let hydrateRouter=router=>{setCompileHandlerHook((handler,_,_1,scope)=>{let fn=handler[2];injectExternalDependency(fn);handler[3]?.type?.(``,scope[1]||fn.length>countParams(handler[1]))});setCompileErrorHandlerHook((_,fn,dat,scope)=>{injectExternalDependency(fn);dat?.type?.(``,scope[1]||fn.length>1);return``});hydrateDependency(router,[false,false,,``,false],``)};export default router=>{hydrateRouter(router);markExported()};
@@ -0,0 +1,2 @@
1
+ import aot from "../aot.js";
2
+ export default aot;
@@ -0,0 +1 @@
1
+ import aot from"../aot.js";export default aot;
@@ -0,0 +1,8 @@
1
+ import type { RouterTag } from "../../core/index.js";
2
+ import { type CompiledDependency } from "runtime-compiler";
3
+ import type { FetchFn } from "../../core/utils.js";
4
+ export type BunRoutes = Record<string, Record<string, FetchFn> | FetchFn>;
5
+ export declare const compileToString: (router: RouterTag) => string;
6
+ export declare const compileToExportedDependency: (router: RouterTag) => CompiledDependency<BunRoutes>;
7
+ export declare const compileToHandler: (router: RouterTag) => Promise<BunRoutes>;
8
+ export declare const compileToHandlerSync: (router: RouterTag) => BunRoutes;
@@ -0,0 +1 @@
1
+ import{compileGroup,AsyncFunction,contextInit,setContextInit,setCompileErrorHandlerHook,setCompileHandlerHook}from"@mapl/framework";import{countParams}from"@mapl/router/path";import{exportDependency,getDependency,injectDependency,injectExternalDependency}from"runtime-compiler";import{evaluate,evaluateSync}from"runtime-compiler/jit";import{compileErrorHandler,compileReturn,paramArgs,RES400}from"../jit.js";import{insertRoute,resetRouter,routerToString}from"./router.js";let compileToState=router=>{resetRouter();setCompileHandlerHook((handler,prevContent,path,scope)=>{let fn=handler[2];let call=injectExternalDependency(fn)+`(`;let paramCount=countParams(handler[1]);paramCount>0&&(call+=paramArgs[paramCount]);if(fn.length>paramCount){call+=paramCount===0?`c`:`,c`;if(!scope[1]){insertRoute(handler[0],path,prevContent+contextInit+compileReturn(handler[3],fn instanceof AsyncFunction,scope[0],true,call+`)`)+(scope[0]?`})()`:``));return}}insertRoute(handler[0],path,prevContent+compileReturn(handler[3],fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)+(scope[0]?`})()`:``))});setCompileErrorHandlerHook(compileErrorHandler);setContextInit(`let h=[],c={status:200,req:r,server:s,headers:h};`);compileGroup(router,[false,false,,`return `+RES400,false],``,``)};export let compileToString=router=>{compileToState(router);return routerToString()};export let compileToExportedDependency=router=>exportDependency(injectDependency(compileToString(router)));export let compileToHandler=async router=>{let id=compileToExportedDependency(router);await evaluate();return getDependency(id)};export let compileToHandlerSync=router=>{let id=compileToExportedDependency(router);evaluateSync();return getDependency(id)};
@@ -0,0 +1,3 @@
1
+ export declare const resetRouter: () => void;
2
+ export declare const insertRoute: (method: string, path: string, content: string) => void;
3
+ export declare const routerToString: () => string;
@@ -0,0 +1 @@
1
+ import{injectDependency}from"runtime-compiler";import{RES404}from"../jit.js";let ROUTES;export let resetRouter=()=>{ROUTES={}};export let insertRoute=(method,path,content)=>{let isWildcard=path.endsWith(`**`);let i=0;let bunPattern=isWildcard?path.slice(-2).replace(/\*/g,()=>`:q`+ i++)+`*`:path.replace(/\*/g,()=>`:q`+ i++);let str=`(r,s)=>{`;if(i>0){str+=`let {q0`;for(let j=1,l=i-(isWildcard?1:0);j<l;j++)str+=`q,`+j;str+=`}=r.params`+(isWildcard?`,q`+i+`=r.params["*"];`:`;`)}(ROUTES[bunPattern]??={})[method]=str+content+`}`;if(method!==``&&method!==`GET`&&method!==`HEAD`&&method!==`OPTIONS`&&method!==`DELETE`&&method!==`PATCH`&&method!==`POST`&&method!==`PUT`)ROUTES[bunPattern][``]??=RES404;if(isWildcard)(ROUTES[bunPattern===`/*`?`/`:bunPattern.slice(0,-3)]??={})[method]??=RES404};export let routerToString=()=>{let str=`{`;for(let pattern in ROUTES){str+=`"`+pattern+`":`;let methods=ROUTES[pattern];let allMethods=methods[``];if(allMethods==null){str+=`{`;for(let method in methods)str+=method+`:`+methods[method]+`,`;str+=`},`}else if(Object.keys(methods).length===1)str+=methods[``]+`,`;else{str+=`(r,s)=>`;for(let method in methods)if(method!==``){let fn=methods[method];str+=`r.method==="`+method+`"?`+(fn.startsWith(`(r,s)=>`)?injectDependency(fn)+`(r,s)`:fn)+`:`}str+=(allMethods.startsWith(`(r,s)=>`)?injectDependency(allMethods)+`(r,s)`:allMethods)+`,`}}return str+`}`};
package/compiler/jit.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import type { RouterTag } from "../core/index.js";
2
2
  import { type CompiledDependency } from "runtime-compiler";
3
+ import type { FetchFn } from "../core/utils.js";
3
4
  export declare const RES404: string;
4
5
  export declare const RES400: string;
6
+ export declare const paramArgs: string[];
5
7
  export declare const compileToString: (router: RouterTag) => string;
6
- export declare const compileToExportedDependency: (router: RouterTag) => CompiledDependency<(req: Request) => any>;
7
- export declare const compileToHandler: (router: RouterTag) => Promise<(req: Request) => any>;
8
- export declare const compileToHandlerSync: (router: RouterTag) => ((req: Request) => any);
8
+ export declare const compileToExportedDependency: (router: RouterTag) => CompiledDependency<FetchFn>;
9
+ export declare const compileToHandler: (router: RouterTag) => Promise<FetchFn>;
10
+ export declare const compileToHandlerSync: (router: RouterTag) => FetchFn;
package/compiler/jit.js CHANGED
@@ -1 +1 @@
1
- import{compileGroup,createArgSet,AsyncFunction,setHooks,contextInit,setContextInit}from"@mapl/framework";import compile from"@mapl/router/method/compiler";import{countParams}from"@mapl/router/path";import{insertItem}from"@mapl/router/method";import{exportDependency,getDependency,injectDependency,injectExternalDependency}from"runtime-compiler";import{evaluate,evaluateSync}from"runtime-compiler/jit";let URL_ROUTER;export let RES404=injectDependency(`new Response(null,{status:404})`);export let RES400=injectDependency(`new Response(null,{status:400})`);let paramArgs=createArgSet(new Array(16).fill(0).map((_1,i)=>`q`+i));let compileReturn=(dat,fnAsync,scopeAsync,contextCreated,result)=>{let res=dat?.type;if(res==null)return`return `+result;let str=res(fnAsync?`await `+result:result,contextCreated);return fnAsync&&!scopeAsync?`return (async()=>{`+str+`})()`:str};let compileToState=router=>{URL_ROUTER={};setHooks({compileHandler:(handler,prevContent,path,scope)=>{let fn=handler[2];let call=injectExternalDependency(fn)+`(`;let paramCount=countParams(handler[1]);paramCount>0&&(call+=paramArgs[paramCount]);if(fn.length>paramCount){call+=paramCount===0?`c`:`,c`;if(!scope[1]){insertItem(URL_ROUTER,handler[0],path,prevContent+contextInit+compileReturn(handler[3],fn instanceof AsyncFunction,scope[0],true,call+`)`)+(scope[0]?`})()`:``));return}}insertItem(URL_ROUTER,handler[0],path,prevContent+compileReturn(handler[3],fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)+(scope[0]?`})()`:``))},compileErrorHandler:(input,fn,dat,scope)=>{let call=injectExternalDependency(fn)+`(`+input;if(fn.length>1){call+=`,c`;if(!scope[1])return contextInit+compileReturn(dat,fn instanceof AsyncFunction,scope[0],true,call+`)`)}return compileReturn(dat,fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)}});setContextInit(`let h=[],c={status:200,req:r,headers:h};`);compileGroup(router,[false,false,,`return `+RES400,false],``,``)};export let compileToString=router=>{compileToState(router);return`(r)=>{`+compile(URL_ROUTER,`r.method`,`let u=r.url,s=u.indexOf("/",12)+1,e=u.indexOf("?",s),p=e===-1?u.slice(s):u.slice(s,e);`,1)+`return `+RES404+`}`};export let compileToExportedDependency=router=>exportDependency(injectDependency(compileToString(router)));export let compileToHandler=async router=>{let id=compileToExportedDependency(router);await evaluate();return getDependency(id)};export let compileToHandlerSync=router=>{let id=compileToExportedDependency(router);evaluateSync();return getDependency(id)};
1
+ import{compileGroup,createArgSet,AsyncFunction,contextInit,setContextInit,setCompileErrorHandlerHook,setCompileHandlerHook}from"@mapl/framework";import compile from"@mapl/router/method/compiler";import{countParams}from"@mapl/router/path";import{insertItem}from"@mapl/router/method";import{exportDependency,getDependency,injectDependency,injectExternalDependency}from"runtime-compiler";import{evaluate,evaluateSync}from"runtime-compiler/jit";let URL_ROUTER;export let RES404=injectDependency(`new Response(null,{status:404})`);export let RES400=injectDependency(`new Response(null,{status:400})`);export let paramArgs=createArgSet(new Array(16).fill(0).map((_1,i)=>`q`+i));export let compileReturn=(dat,fnAsync,scopeAsync,contextCreated,result)=>{let res=dat?.type;if(res==null)return`return `+result;let str=res(fnAsync?`await `+result:result,contextCreated);return fnAsync&&!scopeAsync?`return (async()=>{`+str+`})()`:str};export let compileErrorHandler=(input,fn,dat,scope)=>{let call=injectExternalDependency(fn)+`(`+input;if(fn.length>1){call+=`,c`;if(!scope[1])return contextInit+compileReturn(dat,fn instanceof AsyncFunction,scope[0],true,call+`)`)}return compileReturn(dat,fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)};let compileToState=router=>{URL_ROUTER={};setCompileHandlerHook((handler,prevContent,path,scope)=>{let fn=handler[2];let call=injectExternalDependency(fn)+`(`;let paramCount=countParams(handler[1]);paramCount>0&&(call+=paramArgs[paramCount]);if(fn.length>paramCount){call+=paramCount===0?`c`:`,c`;if(!scope[1]){insertItem(URL_ROUTER,handler[0],path,prevContent+contextInit+compileReturn(handler[3],fn instanceof AsyncFunction,scope[0],true,call+`)`)+(scope[0]?`})()`:``));return}}insertItem(URL_ROUTER,handler[0],path,prevContent+compileReturn(handler[3],fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)+(scope[0]?`})()`:``))});setCompileErrorHandlerHook(compileErrorHandler);setContextInit(`let h=[],c={status:200,req:r,headers:h};`);compileGroup(router,[false,false,,`return `+RES400,false],``,``)};export let compileToString=router=>{compileToState(router);return`(r)=>{`+compile(URL_ROUTER,`r.method`,`let u=r.url,s=u.indexOf("/",12)+1,e=u.indexOf("?",s),p=e===-1?u.slice(s):u.slice(s,e);`,1)+`return `+RES404+`}`};export let compileToExportedDependency=router=>exportDependency(injectDependency(compileToString(router)));export let compileToHandler=async router=>{let id=compileToExportedDependency(router);await evaluate();return getDependency(id)};export let compileToHandlerSync=router=>{let id=compileToExportedDependency(router);evaluateSync();return getDependency(id)};
package/constants.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "@mapl/framework/constants";
2
2
  export declare const HEADERS: string;
3
- export declare const CTX_INIT: string;
3
+ export declare const SERVER_INFO: string;
4
+ export declare const BUN_FN_ARGS: string;
5
+ export declare const BUN_FN_START: string;
4
6
  export declare const PARSE_PATH: string;
package/constants.js CHANGED
@@ -1 +1 @@
1
- import{CTX,PATH,REQ,URL,PATH_START,PATH_END}from"@mapl/framework/constants";export*from"@mapl/framework/constants";export let HEADERS=`h`;export let CTX_INIT=`let `+HEADERS+`=[],`+CTX+`={status:200,req:`+REQ+`,headers:`+HEADERS+`};`;export let PARSE_PATH=`let `+URL+`=`+REQ+`.url,`+PATH_START+`=`+URL+`.indexOf("/",12)+1,`+PATH_END+`=`+URL+`.indexOf("?",`+PATH_START+`),`+PATH+`=`+PATH_END+`===-1?`+URL+`.slice(`+PATH_START+`):`+URL+`.slice(`+PATH_START+`,`+PATH_END+`);`;
1
+ import{PATH,REQ,URL,PATH_START,PATH_END}from"@mapl/framework/constants";export*from"@mapl/framework/constants";export let HEADERS=`h`;export let SERVER_INFO=`s`;export let BUN_FN_ARGS=`(`+REQ+`,`+SERVER_INFO+`)`;export let BUN_FN_START=BUN_FN_ARGS+`=>`;export let PARSE_PATH=`let `+URL+`=`+REQ+`.url,`+PATH_START+`=`+URL+`.indexOf("/",12)+1,`+PATH_END+`=`+URL+`.indexOf("?",`+PATH_START+`),`+PATH+`=`+PATH_END+`===-1?`+URL+`.slice(`+PATH_START+`):`+URL+`.slice(`+PATH_START+`,`+PATH_END+`);`;
package/core/handler.js CHANGED
@@ -1 +1 @@
1
- import{noOp}from"./utils.js";import{isHydrating}from"runtime-compiler/config";import{injectDependency}from"runtime-compiler";export let json=isHydrating?noOp:(()=>{let jsonHeader=injectDependency(`["content-type","application/json"]`);let jsonOptions=injectDependency(`{headers:[`+jsonHeader+`]}`);return(res,hasContext)=>hasContext?`h.push(`+jsonHeader+`);return new Response(JSON.stringify(`+res+`),c)`:`return new Response(JSON.stringify(`+res+`),`+jsonOptions+`)`})();export let html=isHydrating?noOp:(()=>{let htmlHeader=injectDependency(`["content-type","text/html"]`);let htmlOptions=injectDependency(`{headers:[`+htmlHeader+`]}`);return(res,hasContext)=>hasContext?`h.push(`+htmlHeader+`);return new Response(`+res+`,c)`:`return new Response(`+res+`,`+htmlOptions+`)`})();export let text=isHydrating?noOp:(res,hasContext)=>`return new Response(`+res+(hasContext?`,c)`:`)`);export let error=(r,f,dat)=>{r[2]=[f,dat];return r};export let route=(method,path,handler,dat)=>[method,path,handler,dat];export let any=(path,handler,dat)=>[``,path,handler,dat];export let get=(path,handler,dat)=>[`GET`,path,handler,dat];export let post=(path,handler,dat)=>[`POST`,path,handler,dat];export let put=(path,handler,dat)=>[`PUT`,path,handler,dat];export let del=(path,handler,dat)=>[`DELETE`,path,handler,dat];export let patch=(path,handler,dat)=>[`PATCH`,path,handler,dat];export let options=(path,handler,dat)=>[`OPTIONS`,path,handler,dat];export let trace=(path,handler,dat)=>[`TRACE`,path,handler,dat];
1
+ import{noOp}from"./utils.js";import{isHydrating}from"runtime-compiler/config";import{injectDependency,lazyDependency}from"runtime-compiler";let JSON_HEADER=isHydrating?noOp:lazyDependency(injectDependency,`["content-type","application/json"]`);let JSON_OPTIONS=isHydrating?noOp:lazyDependency(injectDependency,`{headers:[`+JSON_HEADER()+`]}`);export let json=isHydrating?noOp:(res,hasContext)=>hasContext?`h.push(`+JSON_HEADER()+`);return new Response(JSON.stringify(`+res+`),c)`:`return new Response(JSON.stringify(`+res+`),`+JSON_OPTIONS()+`)`;let HTML_HEADER=isHydrating?noOp:lazyDependency(injectDependency,`["content-type","application/json"]`);let HTML_OPTIONS=isHydrating?noOp:lazyDependency(injectDependency,`{headers:[`+JSON_HEADER()+`]}`);export let html=isHydrating?noOp:(res,hasContext)=>hasContext?`h.push(`+HTML_HEADER()+`);return new Response(`+res+`,c)`:`return new Response(`+res+`,`+HTML_OPTIONS()+`)`;export let text=isHydrating?noOp:(res,hasContext)=>`return new Response(`+res+(hasContext?`,c)`:`)`);export let error=(r,f,dat)=>{r[2]=[f,dat];return r};export let route=(method,path,handler,dat)=>[method,path,handler,dat];export let any=(path,handler,dat)=>[``,path,handler,dat];export let get=(path,handler,dat)=>[`GET`,path,handler,dat];export let post=(path,handler,dat)=>[`POST`,path,handler,dat];export let put=(path,handler,dat)=>[`PUT`,path,handler,dat];export let del=(path,handler,dat)=>[`DELETE`,path,handler,dat];export let patch=(path,handler,dat)=>[`PATCH`,path,handler,dat];export let options=(path,handler,dat)=>[`OPTIONS`,path,handler,dat];export let trace=(path,handler,dat)=>[`TRACE`,path,handler,dat];
package/core/utils.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
2
2
  export type AwaitedReturn<U extends (...a: any[]) => any> = Awaited<ReturnType<U>>;
3
3
  export type RequestMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "TRACE" | (string & {});
4
+ export type FetchFn = (req: Request) => any;
4
5
  export declare const noOp: () => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/web",
3
- "version": "0.3.0-alpha.2",
3
+ "version": "0.3.0-beta.1",
4
4
  "description": "A compiled web framework for all runtimes",
5
5
  "keywords": ["fast", "lightweight", "cross-runtime", "framework", "web", "backend"],
6
6
  "repository": {
@@ -12,26 +12,29 @@
12
12
  "main": "./index.js",
13
13
  "types": "./index.d.ts",
14
14
  "dependencies": {
15
- "@mapl/framework": "^0.4.7",
15
+ "@mapl/framework": "^0.5.2",
16
16
  "@mapl/router": "^0.6.2",
17
17
  "@safe-std/error": "^1.0.1",
18
- "runtime-compiler": "^1.0.11"
18
+ "runtime-compiler": "^1.2.4"
19
19
  },
20
20
  "optionalDependencies": {
21
21
  "secure-headers": "^0.0.6"
22
22
  },
23
23
  "exports": {
24
24
  "./constants": "./constants.js",
25
- "./core/utils": "./core/utils.js",
26
25
  ".": "./index.js",
27
- "./core/context": "./core/context.js",
28
- "./compiler/aot": "./compiler/aot.js",
29
- "./core/middleware": "./core/middleware.js",
30
- "./utils/cors": "./utils/cors.js",
31
- "./core": "./core/index.js",
32
26
  "./core/handler": "./core/handler.js",
33
- "./compiler/jit": "./compiler/jit.js",
27
+ "./core/utils": "./core/utils.js",
28
+ "./core": "./core/index.js",
29
+ "./core/middleware": "./core/middleware.js",
34
30
  "./utils/static-headers": "./utils/static-headers.js",
35
- "./utils/secure-headers": "./utils/secure-headers.js"
31
+ "./core/context": "./core/context.js",
32
+ "./utils/secure-headers": "./utils/secure-headers.js",
33
+ "./compiler/bun/jit": "./compiler/bun/jit.js",
34
+ "./compiler/bun/aot": "./compiler/bun/aot.js",
35
+ "./compiler/bun/router": "./compiler/bun/router.js",
36
+ "./utils/cors": "./utils/cors.js",
37
+ "./compiler/aot": "./compiler/aot.js",
38
+ "./compiler/jit": "./compiler/jit.js"
36
39
  }
37
40
  }