@mapl/web 0.5.2 → 0.5.4

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
@@ -2,20 +2,20 @@
2
2
  A low level, fast and type-safe framework.
3
3
 
4
4
  ```ts
5
- import { inject, route, router, send } from '@mapl/web';
5
+ import { inject, router, send } from '@mapl/web';
6
6
  import { compiler, request } from '@mapl/web/generic';
7
7
 
8
- const app = router(
8
+ const app = router.init(
9
9
  [],
10
10
  [
11
- route.get(
11
+ router.get(
12
12
  '/',
13
13
  send.raw((c) => {
14
14
  c.status = 418;
15
15
  return 'Hi';
16
16
  }),
17
17
  ),
18
- route.post(
18
+ router.post(
19
19
  '/body',
20
20
  send.json(
21
21
  inject([request], async (req) => req.json())
@@ -27,5 +27,4 @@ const app = router(
27
27
  export default {
28
28
  fetch: compiler.buildSync(app)(),
29
29
  };
30
-
31
30
  ```
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * @module Global compiler hooks
3
+ *
4
+ * ONLY COMPILER MODULES SHOULD INCLUDE THIS FILE.
5
+ * IF NOT IT BREAKS BUNDLING :(
3
6
  */
4
7
  import type { Route } from "./router.ts";
5
8
  import type { State } from "./state.ts";
@@ -1,4 +1,5 @@
1
1
  import type { State } from "./state.ts";
2
+ import type { RequestMethod } from "../generic/types.ts";
2
3
  /*
3
4
  * From what i've tested closures use 5x more
4
5
  * memory than [fn, ...data] and runs slower too
@@ -10,7 +11,7 @@ export type RouteLayer<Params extends any[]> = [build: (data: any[], state: Stat
10
11
  };
11
12
  export type Route = [method: string, path: string, ...layers: RouteLayer<any[]>[]];
12
13
  export type RegisterRouteFn = <const Path extends string>(path: Path, ...layers: RouteLayer<InferPath<Path>>[]) => Route;
13
- export type RegisterRouteWithMethodFn = <const Path extends string>(method: string, path: Path, ...layers: RouteLayer<InferPath<Path>>[]) => Route;
14
+ export type RegisterRouteWithMethodFn = <const Path extends string>(method: RequestMethod, path: Path, ...layers: RouteLayer<InferPath<Path>>[]) => Route;
14
15
  export type Router = [layers: Layer[], routes: Route[]] | [layers: Layer[], routes: Route[], children: Record<string, Router>];
15
16
  /**
16
17
  * Run a layer
@@ -18,7 +18,7 @@ export declare const hydrateToDependency: (router: Router) => void;
18
18
  * Use in `build` mode.
19
19
  *
20
20
  * @example
21
- * `(${buildToString(app)})(hydrate(app));`
21
+ * `const fetch = (${buildToString(app)})(hydrate(app))();`
22
22
  */
23
23
  export declare const buildToString: (router: Router) => string;
24
24
  /**
@@ -1 +1 @@
1
- import{createRouter,insertItem}from"@mapl/router/method";import compileMethodRouter from"@mapl/router/method/compiler";import{addExtraCode,evaluate,evaluateToString,hydrate as finishHydration,injectDependency}from"runtime-compiler";import{build as buildRouter,hydrate as hydrateRouter}from"../compiler/router.js";import{finalizeReturn}from"../compiler/state.js";import{setHandlerArgs,setRegisterRoute,setRouteParamMap}from"../compiler/globals.js";let methodRouter;export let registerRouteCb=(route,state,prefix,content)=>{insertItem(methodRouter,route[0],prefix+route[1],finalizeReturn(state,content))};let buildWrapper=router=>{setHandlerArgs(`(mr)`);methodRouter=createRouter();setRegisterRoute(registerRouteCb);let paramMap=[``,`q0,`];for(let i=1;i<=8;i++)paramMap.push(`${paramMap[i]}q${i},`);setRouteParamMap(paramMap);buildRouter(router,[false,false],``,``);return`()=>{var mjh=['content-type','application/json'],mjo={headers:mjh},mhh=['content-type','text/html'],mho={headers:mhh},mnf=new Response(null,{status:404});return(mr)=>{${compileMethodRouter(methodRouter,`mr.method`,`let mu=mr.url,s=mu.indexOf('/',10)+1,e=mu.indexOf('?',s),p=e===-1?mu.slice(s):mu.slice(s,e);`,1)}return mnf}}`};export let buildToDependency=router=>injectDependency(buildWrapper(router));export let hydrateToDependency=router=>{hydrateRouter(router,[false,false])};export let buildToString=router=>(addExtraCode(`return`+buildWrapper(router)),evaluateToString());export let build=router=>(addExtraCode(`return`+buildWrapper(router)),evaluate());export let hydrate=router=>(hydrateToDependency(router),finishHydration());
1
+ import{createRouter,insertItem}from"@mapl/router/method";import compileMethodRouter from"@mapl/router/method/compiler";import{addExtraCode,evaluate,evaluateToString,hydrate as finishHydration,injectDependency}from"runtime-compiler";import{build as buildRouter,hydrate as hydrateRouter}from"../compiler/router.js";import{finalizeReturn}from"../compiler/state.js";import{setHandlerArgs,setRegisterRoute,setRouteParamMap}from"../compiler/globals.js";let methodRouter;export let registerRouteCb=(route,state,prefix,content)=>{insertItem(methodRouter,route[0],prefix+route[1],finalizeReturn(state,content))};let buildWrapper=router=>{setHandlerArgs(`(mr)`);methodRouter=createRouter();setRegisterRoute(registerRouteCb);let paramMap=[``,`q0,`];for(let i=1;i<=8;i++)paramMap.push(`${paramMap[i]}q${i},`);setRouteParamMap(paramMap);buildRouter(router,[false,false],``,``);return`()=>{var mjh=['content-type','application/json'],mjo={headers:mjh},mhh=['content-type','text/html'],mho={headers:mhh},mnf=new Response(null,{status:404});return(mr)=>{${compileMethodRouter(methodRouter,`mr.method`,`let mu=mr.url,s=mu.indexOf('/',10)+1,e=mu.indexOf('?',s),p=e===-1?mu.slice(s):mu.slice(s,e);`,0)}return mnf}}`};export let buildToDependency=router=>injectDependency(buildWrapper(router));export let hydrateToDependency=router=>{hydrateRouter(router,[false,false])};export let buildToString=router=>(addExtraCode(`return`+buildWrapper(router)),evaluateToString());export let build=router=>(addExtraCode(`return`+buildWrapper(router)),evaluate());export let hydrate=router=>(hydrateToDependency(router),finishHydration());
@@ -1,4 +1,4 @@
1
- import type { RegisterRouteFn, RegisterRouteWithMethodFn } from "../compiler/router.ts";
1
+ import type { RegisterRouteFn, RegisterRouteWithMethodFn, Router } from "../compiler/router.ts";
2
2
  export declare const get: RegisterRouteFn;
3
3
  export declare const post: RegisterRouteFn;
4
4
  export declare const put: RegisterRouteFn;
@@ -9,3 +9,4 @@ export declare const trace: RegisterRouteFn;
9
9
  export declare const query: RegisterRouteFn;
10
10
  export declare const any: RegisterRouteFn;
11
11
  export declare const def: RegisterRouteWithMethodFn;
12
+ export declare const init: (layers: Router[0], routes: Router[1], children?: Router[2]) => Router;
package/generic/route.js CHANGED
@@ -1 +1 @@
1
- let GET=[`GET`];export let get=(...args)=>GET.concat(args);let POST=[`POST`];export let post=(...args)=>POST.concat(args);let PUT=[`PUT`];export let put=(...args)=>PUT.concat(args);let DEL=[`DELETE`];export let del=(...args)=>DEL.concat(args);let PATCH=[`PATCH`];export let patch=(...args)=>PATCH.concat(args);let OPTIONS=[`OPTIONS`];export let options=(...args)=>OPTIONS.concat(args);let TRACE=[`TRACE`];export let trace=(...args)=>TRACE.concat(args);let QUERY=[`QUERY`];export let query=(...args)=>QUERY.concat(args);let ANY=[``];export let any=(...args)=>ANY.concat(args);export let def=(...args)=>args;
1
+ let GET=[`GET`];export let get=(...args)=>GET.concat(args);let POST=[`POST`];export let post=(...args)=>POST.concat(args);let PUT=[`PUT`];export let put=(...args)=>PUT.concat(args);let DEL=[`DELETE`];export let del=(...args)=>DEL.concat(args);let PATCH=[`PATCH`];export let patch=(...args)=>PATCH.concat(args);let OPTIONS=[`OPTIONS`];export let options=(...args)=>OPTIONS.concat(args);let TRACE=[`TRACE`];export let trace=(...args)=>TRACE.concat(args);let QUERY=[`QUERY`];export let query=(...args)=>QUERY.concat(args);let ANY=[``];export let any=(...args)=>ANY.concat(args);export let def=(...args)=>args;export let init=(layers,routes,children)=>children==null?[layers,routes]:[layers,routes,children];
@@ -4,3 +4,4 @@ export interface Context {
4
4
  headers: Header[];
5
5
  }
6
6
  export type MaybePromise<T> = T | Promise<T>;
7
+ export type RequestMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "TRACE" | (string & {});
@@ -0,0 +1,11 @@
1
+ import type { Header, RequestMethod } from "../types.ts";
2
+ import type { Layer } from "../../compiler/router.ts";
3
+ export type PreflightHeader = Header & {
4
+ "~": 0;
5
+ };
6
+ export declare const allowMethods: (methods: [RequestMethod, RequestMethod, ...RequestMethod[]] | RequestMethod | "*") => PreflightHeader;
7
+ export declare const allowHeaders: (headers: string | string[]) => PreflightHeader;
8
+ export declare const maxAge: (seconds: number) => PreflightHeader;
9
+ export declare const allowCredentials: Header;
10
+ export declare const exposeHeaders: (headers: "*" | (string & {}) | [string, string, ...string[]]) => Header;
11
+ export declare const init: (origins: "*" | (string & {}) | [string, string, ...string[]], preflightHeaders?: [PreflightHeader, ...PreflightHeader[]], headers?: [Header, ...Header[]]) => Layer<any[]>;
@@ -0,0 +1 @@
1
+ import{isHydrating}from"runtime-compiler/config";import{injectDependency}from"runtime-compiler";import{injectArgsList}from"runtime-compiler/utils";export let allowMethods=methods=>[`access-control-allow-methods`,``+methods];export let allowHeaders=headers=>[`access-control-allow-headers`,``+headers];export let maxAge=seconds=>[`access-control-max-age`,``+seconds];export let allowCredentials=[`access-control-allow-credentials`,`true`];export let exposeHeaders=headers=>[`access-control-expose-headers`,``+headers];let hydrateInit=[(_,state)=>(state[1]=true,``),null,null,null];let buildInit=(layer,state)=>{state[1]=true;let origins=layer[1];let preflightHeaders=layer[2];let headers=layer[3]??[];if(origins!==`*`){headers.push([`vary`,`origin`]);if(Array.isArray(origins)){let str=`(r,h)=>{let o=r.headers.get("origin");h.push(["access-control-allow-origin",typeof o==="string"&&o===`+JSON.stringify(origins[1]);for(let i=2;i<origins.length;i++)str+=`||o===`+JSON.stringify(origins[i]);return injectDependency(str+`?o:`+JSON.stringify(origins[0])+`]);`+(headers.length>0?`h.push(`+injectArgsList(headers)+`);`:``)+(preflightHeaders!=null?`r.method==="OPTIONS"&&h.push(`+injectArgsList(preflightHeaders)+`)}`:`}`))+`(mr,mh);`}}headers.push([`access-control-allow-origin`,origins]);let pushHeaders=headers.length>0?`h.push(`+injectArgsList(headers)+`);`:``;return preflightHeaders!=null?injectDependency(`(r,h)=>{`+pushHeaders+`r.method==="OPTIONS"&&h.push(`+injectArgsList(preflightHeaders)+`)}`)+`(mr,mh);`:pushHeaders};export let init=isHydrating?()=>hydrateInit:(origin,preflightHeaders,headers)=>[buildInit,origin,preflightHeaders,headers];
package/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import type { Router } from "./compiler/router.ts";
2
1
  export * as send from "./generic/handlers.ts";
3
- export * as route from "./generic/route.ts";
2
+ export * as router from "./generic/route.ts";
4
3
  export * as layer from "./generic/layers.ts";
5
4
  export * as parser from "./generic/parsers.ts";
6
5
  export { inject } from "runtime-compiler/call";
7
- export declare const router: (layers: Router[0], routes: Router[1], children?: Router[2]) => Router;
6
+ export * as cors from "./generic/utils/cors.ts";
package/index.js CHANGED
@@ -1 +1 @@
1
- export*as send from"./generic/handlers.js";export*as route from"./generic/route.js";export*as layer from"./generic/layers.js";export*as parser from"./generic/parsers.js";export{inject}from"runtime-compiler/call";export let router=(layers,routes,children)=>children==null?[layers,routes]:[layers,routes,children];
1
+ export*as send from"./generic/handlers.js";export*as router from"./generic/route.js";export*as layer from"./generic/layers.js";export*as parser from"./generic/parsers.js";export{inject}from"runtime-compiler/call";export*as cors from"./generic/utils/cors.js";
package/package.json CHANGED
@@ -1,35 +1 @@
1
- {
2
- "name": "@mapl/web",
3
- "version": "0.5.2",
4
- "description": "A compiled web framework for all runtimes",
5
- "keywords": ["fast", "lightweight", "cross-runtime", "framework", "web", "backend"],
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/mapljs/web.git"
9
- },
10
- "license": "MIT",
11
- "type": "module",
12
- "main": "./index.js",
13
- "types": "./index.d.ts",
14
- "dependencies": {
15
- "@mapl/router": "^0.8.1",
16
- "@safe-std/error": "^2.0.4",
17
- "runtime-compiler": "^2.1.2"
18
- },
19
- "exports": {
20
- "./bun": "./bun/index.js",
21
- "./generic/compiler": "./generic/compiler.js",
22
- "./constants": "./constants.js",
23
- "./generic/types": "./generic/types.js",
24
- "./generic/handlers": "./generic/handlers.js",
25
- "./compiler/call": "./compiler/call.js",
26
- "./generic": "./generic/index.js",
27
- ".": "./index.js",
28
- "./compiler/globals": "./compiler/globals.js",
29
- "./generic/parsers": "./generic/parsers.js",
30
- "./compiler/state": "./compiler/state.js",
31
- "./generic/route": "./generic/route.js",
32
- "./compiler/router": "./compiler/router.js",
33
- "./generic/layers": "./generic/layers.js"
34
- }
35
- }
1
+ {"name":"@mapl/web","version":"0.5.4","description":"A compiled web framework for all runtimes","keywords":["fast","lightweight","cross-runtime","framework","web","backend"],"repository":{"type":"git","url":"git+https://github.com/mapljs/web.git"},"license":"MIT","type":"module","main":"./index.js","types":"./index.d.ts","dependencies":{"@mapl/router":"^0.8.1","@safe-std/error":"^2.0.4","runtime-compiler":"^2.1.2"},"exports":{"./constants":"./constants.js","./generic/route":"./generic/route.js","./generic/handlers":"./generic/handlers.js",".":"./index.js","./generic/types":"./generic/types.js","./generic/layers":"./generic/layers.js","./generic/parsers":"./generic/parsers.js","./generic/utils/cors":"./generic/utils/cors.js","./generic/compiler":"./generic/compiler.js","./compiler/call":"./compiler/call.js","./compiler/state":"./compiler/state.js","./generic":"./generic/index.js","./compiler/router":"./compiler/router.js","./compiler/globals":"./compiler/globals.js","./bun":"./bun/index.js"}}