@mapl/web 0.0.5 → 0.0.7

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/constants.d.ts CHANGED
@@ -1,11 +1,7 @@
1
1
  export * from '@mapl/framework/constants';
2
2
  export declare const WEB: string;
3
- export declare const REQ: string;
4
3
  export declare const CTX_FN: string;
5
4
  export declare const CTX_INIT: string;
6
- export declare const URL: string;
7
- export declare const PATH_START: string;
8
- export declare const PATH_END: string;
9
5
  export declare const PARSE_PATH: string;
10
6
  export declare const CHTML: string;
11
7
  export declare const CJSON: string;
package/constants.js CHANGED
@@ -1 +1 @@
1
- import{MAPL,HEADERS,CTX,PATH}from"@mapl/framework/constants";export*from"@mapl/framework/constants";export let WEB=MAPL+"w";export let REQ=WEB+"r";export let CTX_FN=WEB+"ci";export let CTX_INIT="let "+HEADERS+"=[],"+CTX+"="+CTX_FN+"("+REQ+","+HEADERS+");";export let URL=REQ+"u";export let PATH_START=PATH+"s";export let PATH_END=PATH+"e";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+".substring("+PATH_START+","+PATH_END+");";export let CHTML=WEB+"h";export let CJSON=WEB+"j";export let R404=WEB+"nf";export let R400=WEB+"br";export let GLOBALS="let ["+CHTML+","+CJSON+']=["text/html","application/json"].map(c=>["content-type",c]),['+R404+","+R400+"]=[404,400].map(s=>new Response(null,{status:s}));";
1
+ import{MAPL,HEADERS,CTX,PATH,REQ,URL,PATH_START,PATH_END}from"@mapl/framework/constants";export*from"@mapl/framework/constants";export let WEB=MAPL+"w";export let CTX_FN=WEB+"c";export let CTX_INIT="let "+HEADERS+"=[],"+CTX+"="+CTX_FN+"("+REQ+","+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+".substring("+PATH_START+","+PATH_END+");";export let CHTML=WEB+"h";export let CJSON=WEB+"j";export let R404=WEB+"n";export let R400=WEB+"b";export let GLOBALS="let ["+CHTML+","+CJSON+']=["text/html","application/json"].map(c=>["content-type",c]),['+R404+","+R400+"]=[404,400].map(s=>new Response(null,{status:s}));";
package/index.js CHANGED
@@ -1 +1 @@
1
- export{default as router}from"./router";import genericCompile from"./router/compiler/generic";export let compile=(app)=>genericCompile(app,[]);
1
+ export{default as router}from"./router";import genericCompile from"./router/compiler";export let compile=(app)=>genericCompile(app,[]);
@@ -0,0 +1,6 @@
1
+ import { type Err } from 'safe-throw';
2
+ import type { Context } from '../router/context';
3
+ export type TokenParser = (token: string, c: Context) => Promise<any>;
4
+ export type MalformedErr = Err<'Malformed bearer token'>;
5
+ declare const _default: <const T extends TokenParser>(verifier: T) => (c: Parameters<T>[1]) => ReturnType<T> | Promise<MalformedErr>;
6
+ export default _default;
@@ -0,0 +1 @@
1
+ export default (verifier)=>async(c)=>{let tok=c.req.headers.get("Authorization");return typeof tok==="string"&&tok.startsWith("Bearer ")&&tok.length>7?verifier(tok,c):malformedErr};import{err}from"safe-throw";let malformedErr=Promise.resolve(err("Malformed bearer token"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/web",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "A compiled web standard framework",
5
5
  "keywords": [
6
6
  "fast",
@@ -34,8 +34,8 @@
34
34
  "typescript-eslint": "latest"
35
35
  },
36
36
  "dependencies": {
37
- "@mapl/framework": "^0.0.21",
38
- "@mapl/router": "^0.4.10",
37
+ "@mapl/framework": "^0.0.26",
38
+ "@mapl/router": "^0.4.13",
39
39
  "safe-throw": "^0.0.9"
40
40
  }
41
41
  }
@@ -0,0 +1,3 @@
1
+ import type { AnyRouter } from '.';
2
+ declare const _default: (router: AnyRouter) => (req: Request) => any;
3
+ export default _default;
@@ -0,0 +1 @@
1
+ export default (router)=>{let baseRouter={};let dependencies=[];compileGroup(router._,[baseRouter,dependencies,"let mh=[],c=mwc(r,mh);",(fn,dat,path,state,scope)=>{let call="f"+state[1].push(fn)+"(";let paramCount=path[0].length;if(paramCount>0)call+=paramArgs[paramCount];if(fn.length>paramCount){call+=paramCount===0?"c":",c";if(!scope[1])return state[2]+compileReturn(state,dat,isFuncAsync(fn),scope[0],true,call+")")}return compileReturn(state,dat,isFuncAsync(fn),scope[0],scope[1],call+")")},(fn,dat,state,scope)=>{let call="f"+state[1].push(fn)+"(t";if(fn.length>1){call+=",c";if(!scope[1])return state[2]+compileReturn(state,dat,isFuncAsync(fn),scope[0],true,call+")")}return compileReturn(state,dat,isFuncAsync(fn),scope[0],scope[1],call+")")},transformRoute],[false,false,null,"return mwb"],"","");return Function("me","mwc",...dependencies.map((_,i)=>"f"+(i+1)),'let [mwh,mwj]=["text/html","application/json"].map(c=>["content-type",c]),[mwn,mwb]=[404,400].map(s=>new Response(null,{status:s}));return(r)=>{'+compile(baseRouter,o2,"r.method",'let u=r.url,s=u.indexOf("/",12)+1,e=u.indexOf("?",s),p=e===-1?u.slice(s):u.substring(s,e);',1)+"return mwn}")(isErr,createContext,...dependencies)};import{compileGroup,createArgSet,isFuncAsync}from"@mapl/framework";import{o2}from"@mapl/router/tree/compiler";import compile from"@mapl/router/method/compiler";import{transformRoute}from"@mapl/router/transform";import{isErr}from"safe-throw";import{createContext}from"./context";let paramArgs=createArgSet(new Array(16).fill(0).map((_1,i)=>"q"+i));let compileReturn=(state,dat,fnAsync,scopeAsync,contextCreated,result)=>{let typ=dat.type;if(typ==="raw")return"return "+result;if(fnAsync)result="await "+result;let str=typ==null?"return new Response("+result+(contextCreated?",c)":")"):(contextCreated?"":state[2])+"mh.push("+(typ==="json"?"mwj":"mwh")+");return new Response("+(typ==="json"?"JSON.stringify("+result+")":result)+",c)";return fnAsync&&!scopeAsync?"return (async()=>{"+str+"})":str};
@@ -2,8 +2,8 @@ import type { Group } from '@mapl/framework';
2
2
  import type { Err } from 'safe-throw';
3
3
  import type { Context } from './context';
4
4
  export type ErrorFunc<E extends Err = Err, T extends {} = Record<string, any>, Args extends any[] = any[]> = (err: E, c: Context & T, ...args: Args) => any;
5
- export type HandlerFunc<Params extends string[] = string[], T extends {} = Record<string, any>, Args extends any[] = any[]> = (...args: [...params: Params, c: Context & T, ...args: Args]) => any;
6
- export type MiddlewareFunc<T extends {} = Record<string, any>, Args extends any[] = any[]> = (c: Context & T, ...args: Args) => any;
5
+ export type HandlerFunc<Params extends string[] = string[], T extends {} = Record<string, any>> = (...args: [...params: Params, c: Context & T]) => any;
6
+ export type MiddlewareFunc<T extends {} = Record<string, any>> = (c: Context & T) => any;
7
7
  /**
8
8
  * Basic information to compile
9
9
  */
package/router/index.d.ts CHANGED
@@ -1,21 +1,22 @@
1
1
  import type { Err, InferErr, InferResult } from 'safe-throw';
2
2
  import type { InferRoute } from '@mapl/router/transform';
3
+ import { type AwaitedReturn } from './utils';
3
4
  import type { ErrorFunc, HandlerData, HandlerFunc, HandlerGroup, MiddlewareFunc } from './handler';
4
5
  import { type Methods } from './method';
5
- export type RouteRegister<in out Args extends any[], in out State extends {}, in out E extends Err> = {
6
- [K in Methods | 'any']: <Path extends string>(path: Path, handler: HandlerFunc<InferRoute<Path>, State, Args>, ...data: HandlerData[]) => Router<Args, State, E>;
6
+ export type RouteRegister<in out State extends {}, in out E extends Err> = {
7
+ [K in Methods | 'any']: <Path extends string>(path: Path, handler: HandlerFunc<InferRoute<Path>, State>, ...data: HandlerData[]) => Router<State, E>;
7
8
  };
8
- export interface Router<in out Args extends any[], in out State extends {} = {}, in out E extends Err = never> extends RouteRegister<Args, State, E> {
9
+ export interface Router<in out State extends {} = {}, in out E extends Err = never> extends RouteRegister<State, E> {
9
10
  _state: State;
10
11
  _err: E;
11
- group: HandlerGroup;
12
- apply: (fn: MiddlewareFunc<State, Args>) => this;
13
- check: <const T extends MiddlewareFunc<State, Args>>(fn: T) => Router<Args, State, E | InferErr<ReturnType<T>>>;
14
- set: <Prop extends string, const T extends MiddlewareFunc<State, Args>>(prop: Prop, fn: T) => Router<Args, State & Record<Prop, ReturnType<T>>, E>;
15
- parse: <Prop extends string, const T extends MiddlewareFunc<State, Args>>(prop: Prop, fn: T) => Router<Args, State & Record<Prop, InferResult<ReturnType<T>>>, E | InferErr<ReturnType<T>>>;
16
- route: <Prefix extends string, const App extends Router<Args, any, any>>(prefix: Prefix, app: App) => this;
17
- err: (fn: ErrorFunc<E, State, Args>, ...data: HandlerData[]) => this;
12
+ _: HandlerGroup;
13
+ apply: (fn: MiddlewareFunc<State>) => this;
14
+ check: <const T extends MiddlewareFunc<State>>(fn: T) => Router<State, E | InferErr<AwaitedReturn<T>>>;
15
+ set: <Prop extends string, const T extends MiddlewareFunc<State>>(prop: Prop, fn: T) => Router<State & Record<Prop, AwaitedReturn<T>>, E>;
16
+ parse: <Prop extends string, const T extends MiddlewareFunc<State>>(prop: Prop, fn: T) => Router<State & Record<Prop, InferResult<AwaitedReturn<T>>>, E | InferErr<AwaitedReturn<T>>>;
17
+ route: <Prefix extends string, const App extends Router<any, any>>(prefix: Prefix, app: App) => this;
18
+ err: (fn: ErrorFunc<E, State>, ...data: HandlerData[]) => this;
18
19
  }
19
- export type AnyRouter = Router<any, any, any> | Router<any, any>;
20
+ export type AnyRouter = Router<any, any>;
20
21
  declare const _default: () => Router<[]>;
21
22
  export default _default;
package/router/index.js CHANGED
@@ -1 +1 @@
1
- export default ()=>{let obj=Object.create(routerProto);obj.group=[[],[],null,[]];return obj};import{proto}from"./utils";import{ALL,METHODS}from"./method";let createMethodRegister=(method)=>function(path,handler,...data){this.group[1].push([method,path,handler,proto(...data)]);return this};let routerProto=proto({group:null,apply(f){this.group[0].push([0,f]);return this},check(f){this.group[0].push([1,f]);return this},set(prop,f){this.group[0].push([2,f,prop]);return this},parse(prop,f){this.group[0].push([3,f,prop]);return this},route(prefix,app){this.group[3].push([prefix,app.group]);return this},any:createMethodRegister(ALL),err(f,...data){this.group[2]=[f,proto(...data)];return this}},Object.fromEntries(METHODS.map((method)=>[method,createMethodRegister(method.toUpperCase())])));
1
+ export default ()=>{let obj=Object.create(routerProto);obj._=[[],[],null,[]];return obj};import{proto}from"./utils";import{ALL,METHODS}from"./method";let createMethodRegister=(method)=>function(path,handler,...data){this._[1].push([method,path,handler,proto(...data)]);return this};let routerProto=proto({_:null,apply(f){this._[0].push([0,f]);return this},check(f){this._[0].push([2,f]);return this},set(prop,f){this._[0].push([1,f,prop]);return this},parse(prop,f){this._[0].push([3,f,prop]);return this},route(prefix,app){this._[3].push([prefix,app._]);return this},any:createMethodRegister(ALL),err(f,...data){this._[2]=[f,proto(...data)];return this}},Object.fromEntries(METHODS.map((method)=>[method,createMethodRegister(method.toUpperCase())])));
package/router/utils.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends ((x: infer I) => void) ? I : never;
2
+ export type AwaitedReturn<U extends (...a: any[]) => any> = Awaited<ReturnType<U>>;
2
3
  export declare const proto: <T extends any[]>(...f: T) => UnionToIntersection<T[number]>;
@@ -1,3 +0,0 @@
1
- import type { AnyRouter } from '..';
2
- declare const _default: (router: AnyRouter, args: string[]) => (req: Request) => any;
3
- export default _default;
@@ -1 +0,0 @@
1
- export default (router,args)=>{let baseRouter={};let dependencies=[];compileGroup(router.group,[baseRouter,dependencies,createArgSet(["mc",...args]),"let mh=[],mc=mwci(mwr,mh);",compileHandler,compileErrorHandler,transformRoute],"","",false,false,"return mwbr;");return Function("mie","mwci",...dependencies.map((_,i)=>"mp"+(i+1)),'let [mwh,mwj]=["text/html","application/json"].map(c=>["content-type",c]),[mwnf,mwbr]=[404,400].map(s=>new Response(null,{status:s}));return (mwr)=>{'+compile(baseRouter,o2,"mwr.method",'let mwru=mwr.url,ps=mwru.indexOf("/",12)+1,pe=mwru.indexOf("?",ps),p=pe===-1?mwru.slice(ps):mwru.substring(ps,pe);',1)+"return mwnf}")(isErr,createContext,...dependencies)};import{compileGroup,createArgSet,isFuncAsync,selectArgs}from"@mapl/framework";import{o2}from"@mapl/router/tree/compiler";import compile from"@mapl/router/method/compiler";import{transformRoute}from"@mapl/router/transform";import{isErr}from"safe-throw";import{createContext}from"../context";let paramArgs=createArgSet(new Array(16).fill(0).map((_1,i)=>"q"+i));let compileReturn=(state,dat,wrapAsync,contextCreated,result)=>{let typ=dat.type;return typ==null?"return new Response("+result+(contextCreated?",mc);":");"):typ==="raw"?"return "+result+";":(wrapAsync?"return (async()=>{":"")+(contextCreated?"":state[3])+"mh.push("+(typ==="json"?"mwj":"mwh")+");return new Response("+(typ==="json"?"JSON.stringify("+result+")":result)+",mc);"+(wrapAsync?"});":"")};let compileHandler=(fn,dat,path,state,scopeAsync,contextCreated)=>{let wrapAsync=false;let call="mp"+state[1].push(fn)+"(";if(isFuncAsync(fn)){call="await "+call;wrapAsync=!scopeAsync}let paramCount=path[0].length;if(paramCount>0)call+=paramArgs[paramCount];if(fn.length>paramCount){if(paramCount>0)call+=",";call+=selectArgs(state[2],fn.length-paramCount);if(!contextCreated){contextCreated=true;call=state[3]+call}}return compileReturn(state,dat,wrapAsync,contextCreated,call+")")};let compileErrorHandler=(fn,dat,state,scopeAsync,contextCreated)=>{let wrapAsync=false;let call="mp"+state[1].push(fn)+"(me";if(isFuncAsync(fn)){call="await "+call;wrapAsync=!scopeAsync}if(fn.length>1){call+=","+selectArgs(state[2],fn.length-1);if(!contextCreated){contextCreated=true;call=state[3]+call}}return compileReturn(state,dat,wrapAsync,contextCreated,call+")")};