@mapl/router 0.0.16 → 0.0.18

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.js CHANGED
@@ -1 +1 @@
1
- export const REQ="__req";export const PATH="__req_p";export const PATH_LEN="__req_pl";export const PARAMS="__req_ps";export const PREV_PARAM_IDX="__req_ppi";export const CURRENT_PARAM_IDX="__req_cpi";
1
+ export var REQ="__req";export var PATH="__req_p";export var PATH_LEN="__req_pl";export var PARAMS="__req_ps";export var PREV_PARAM_IDX="__req_ppi";export var CURRENT_PARAM_IDX="__req_cpi";
package/index.js CHANGED
@@ -1 +1 @@
1
- import{compileNode}from"./tree/compiler.js";import{createNode,insertItem as nodeInsertItem}from"./tree/node.js";export function createRouter(){return[null,null]}export function insertItem(router,path,item){if(path.includes("*"))nodeInsertItem(router[1]??=createNode("/"),path,item);else(router[0]??={})[path]=item}export function compileRouter(router,state){const contentBuilder=state.contentBuilder;const hasStatic=router[0]!==null;if(hasStatic){const staticMap=router[0];let hasMultiple=false;for(const key in staticMap){contentBuilder.push(`${hasMultiple?"else ":""}if(__req_p==="${key.slice(1).replace(/"/g,'\\"')}"){`);state.compileItem(staticMap[key],state,false);contentBuilder.push("}");hasMultiple=true}}if(hasStatic)contentBuilder.push("else{");contentBuilder.push("let __req_pl=__req_p.length;");if(router[1]!==null)compileNode(router[1],state,false,false,-1,"");if(hasStatic)contentBuilder.push("}")}
1
+ import{compileNode}from"./tree/compiler.js";import{createNode,insertItem as nodeInsertItem}from"./tree/node.js";export function createRouter(){return[null,null]}export function insertItem(router,path,item){if(path.includes("*"))nodeInsertItem(router[1]??=createNode("/"),path,item);else(router[0]??={})[path]=item}export function compileRouter(router,contentBuilder){let hasStatic=router[0]!==null;if(hasStatic){let staticMap=router[0];let hasMultiple=false;for(let key in staticMap){contentBuilder.push(`${hasMultiple?"else ":""}if(__req_p==="${key.slice(1).replace(/"/g,"\\\"")}"){${staticMap[key]}}`);contentBuilder.push("}");hasMultiple=true}}if(router[1]!==null){if(hasStatic)contentBuilder.push("else{");contentBuilder.push("let __req_pl=__req_p.length;");compileNode(router[1],contentBuilder,false,false,-1,"");if(hasStatic)contentBuilder.push("}")}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/router",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "main": "./index.js",
5
5
  "devDependencies": {
6
6
  "@stylistic/eslint-plugin": "latest",
@@ -12,7 +12,7 @@
12
12
  "typescript": "latest"
13
13
  },
14
14
  "peerDependencies": {
15
- "@mapl/compiler": "^0.0.6"
15
+ "@mapl/compiler": ">=^0.0.7"
16
16
  },
17
17
  "description": "Fast compiled router for all runtimes",
18
18
  "keywords": [
package/tree/compiler.js CHANGED
@@ -1 +1 @@
1
- export function compileNode(node,state,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix){const builder=state.contentBuilder;const part=node[0];const partLen=part.length;startIndexValue++;if(partLen!==1){builder.push(`if(__req_pl>${startIndexPrefix}${startIndexValue+partLen-2})`);for(let i=1;i<partLen;i++,startIndexValue++)builder.push(`if(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})===${part.charCodeAt(i)})`);builder.push("{")}if(node[1]!==null){builder.push(`if(__req_pl===${startIndexPrefix}${startIndexValue}){`);state.compileItem(node[1],state,hasParam);builder.push("}")}if(node[2]!==null){const children=node[2];const childrenKeys=Object.keys(children);if(childrenKeys.length===1){builder.push(`if(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})===${childrenKeys[0]}){`);compileNode(children[childrenKeys[0]],state,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("}")}else{builder.push(`switch(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})){`);for(let i=0,l=childrenKeys.length;i<l;i++){builder.push(`case ${childrenKeys[i]}:`);compileNode(children[childrenKeys[i]],state,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("break;")}builder.push("}")}}if(node[3]!==null){const params=node[3];const hasStore=params[1]!==null;const hasChild=params[0]!==null;const requireAllocation=hasParam?hasMultipleParams:hasChild||!hasStore;if(requireAllocation)builder.push("{");if(hasParam)builder.push(`${hasMultipleParams?"":"let "}__req_ppi=${startIndexPrefix}${startIndexValue};`);const currentIndex=hasParam?"__req_ppi":startIndexPrefix+startIndexValue;const slashIndex=`__req_p.indexOf('/',${currentIndex})`;if(hasChild||!hasStore)builder.push(`${hasParam?"":"let "}__req_cpi=${slashIndex};`);if(hasStore){const paramsVal=`__req_p.slice(${currentIndex})`;builder.push(`if(${hasChild?"__req_cpi":slashIndex}===-1){${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};`);state.compileItem(params[1],state,true);builder.push("}")}if(hasChild){const paramsVal=`__req_p.substring(${currentIndex},__req_cpi)`;builder.push(`if(${hasStore?"":"__req_cpi!==-1&&"}__req_cpi!==${currentIndex}){${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};`);compileNode(params[0],state,true,hasParam,0,"__req_cpi+");if(!requireAllocation)builder.push("__req_ps.pop();");builder.push("}")}if(requireAllocation)builder.push("}")}if(node[4]!==null){const noStore=node[1]===null;if(noStore)builder.push(`if(__req_pl!==${startIndexPrefix}${startIndexValue}){`);const paramsVal=`__req_p.slice(${startIndexPrefix}${startIndexValue})`;builder.push(`${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};`);state.compileItem(node[4],state,true);if(noStore)builder.push("}")}if(partLen!==1)builder.push("}")}
1
+ export function compileNode(node,builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix){let part=node[0];let partLen=part.length;startIndexValue++;if(partLen!==1){builder.push(`if(__req_pl>${startIndexPrefix}${startIndexValue+partLen-2})`);for(let i=1;i<partLen;i++,startIndexValue++)builder.push(`if(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})===${part.charCodeAt(i)})`);builder.push("{")}if(node[1]!==null)builder.push(`if(__req_pl===${startIndexPrefix}${startIndexValue}){${node[1]}}`);if(node[2]!==null){let children=node[2];let childrenKeys=Object.keys(children);if(childrenKeys.length===1){builder.push(`if(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})===${childrenKeys[0]}){`);compileNode(children[childrenKeys[0]],builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("}")}else{builder.push(`switch(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})){`);for(let i=0,l=childrenKeys.length;i<l;i++){builder.push(`case ${childrenKeys[i]}:`);compileNode(children[childrenKeys[i]],builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("break;")}builder.push("}")}}if(node[3]!==null){let params=node[3];let hasStore=params[1]!==null;let hasChild=params[0]!==null;let requireAllocation=hasParam?hasMultipleParams:hasChild||!hasStore;if(requireAllocation)builder.push("{");if(hasParam)builder.push(`${hasMultipleParams?"":"let "}__req_ppi=${startIndexPrefix}${startIndexValue};`);let currentIndex=hasParam?"__req_ppi":startIndexPrefix+startIndexValue;let slashIndex=`__req_p.indexOf('/',${currentIndex})`;if(hasChild||!hasStore)builder.push(`${hasParam?"":"let "}__req_cpi=${slashIndex};`);if(hasStore){let paramsVal=`__req_p.slice(${currentIndex})`;builder.push(`if(${hasChild?"__req_cpi":slashIndex}===-1){${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};${params[1]}}`)}if(hasChild){let paramsVal=`__req_p.substring(${currentIndex},__req_cpi)`;builder.push(`if(${hasStore?"":"__req_cpi!==-1&&"}__req_cpi!==${currentIndex}){${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};`);compileNode(params[0],builder,true,hasParam,0,"__req_cpi+");if(!requireAllocation)builder.push("__req_ps.pop();");builder.push("}")}if(requireAllocation)builder.push("}")}if(node[4]!==null){let noStore=node[1]===null;if(noStore)builder.push(`if(__req_pl!==${startIndexPrefix}${startIndexValue}){`);let paramsVal=`__req_p.slice(${startIndexPrefix}${startIndexValue})`;builder.push(`${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};${node[4]}`);if(noStore)builder.push("}")}if(partLen!==1)builder.push("}")}
package/tree/node.js CHANGED
@@ -1 +1 @@
1
- export function createNode(part){return[part,null,null,null,null]}export function createParamNode(nextNode){return[nextNode,null]}export function cloneNode(node,part){return[part,node[1],node[2],node[3],node[4]]}export function resetNode(node,part,children){node[0]=part;node[2]=children;node[1]=null;node[3]=null;node[4]=null}export function visitNode(node,path){const parts=path.split("*");for(let i=0,{length}=parts;i<length;++i){if(i!==0){if(node[3]===null){const nextNode=createNode(parts[i]);node[3]=createParamNode(nextNode);node=nextNode}else node=node[3][0]??=createNode(parts[i])}for(let j=0,pathPart=parts[i];;++j){const nodePart=node[0];if(j===pathPart.length){if(j<nodePart.length){const children={};children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));resetNode(node,pathPart,children)}break}if(j===nodePart.length){if(node[2]===null)node[2]={};else{const nextNode=node[2][pathPart.charCodeAt(j)];if(typeof nextNode!=="undefined"){node=nextNode;pathPart=pathPart.slice(j);j=0;continue}}const nextNode=createNode(pathPart.slice(j));node[2][pathPart.charCodeAt(j)]=nextNode;node=nextNode;break}if(pathPart.charCodeAt(j)!==nodePart.charCodeAt(j)){const children={};children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));const nextNode=createNode(pathPart.slice(j));children[pathPart.charCodeAt(j)]=nextNode;resetNode(node,nodePart.substring(0,j),children);node=nextNode;break}}}return node}export function insertItem(node,path,item){if(path.charCodeAt(path.length-1)===42){if(path.charCodeAt(path.length-2)===42)visitNode(node,path.substring(0,path.length-2))[4]=item;else(visitNode(node,path.substring(0,path.length-1))[3]??=createParamNode(null))[1]=item}else visitNode(node,path)[1]=item}
1
+ export function createNode(part){return[part,null,null,null,null]}export function createParamNode(nextNode){return[nextNode,null]}export function cloneNode(node,part){return[part,node[1],node[2],node[3],node[4]]}export function resetNode(node,part,children){node[0]=part;node[2]=children;node[1]=null;node[3]=null;node[4]=null}export function visitNode(node,path){let parts=path.split("*");for(let i=0,{length}=parts;i<length;++i){if(i!==0){if(node[3]===null){let nextNode=createNode(parts[i]);node[3]=createParamNode(nextNode);node=nextNode}else node=node[3][0]??=createNode(parts[i])}for(let j=0,pathPart=parts[i];;++j){let nodePart=node[0];if(j===pathPart.length){if(j<nodePart.length){let children={};children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));resetNode(node,pathPart,children)}break}if(j===nodePart.length){if(node[2]===null)node[2]={};else{let nextNode=node[2][pathPart.charCodeAt(j)];if(typeof nextNode!=="undefined"){node=nextNode;pathPart=pathPart.slice(j);j=0;continue}}let nextNode=createNode(pathPart.slice(j));node[2][pathPart.charCodeAt(j)]=nextNode;node=nextNode;break}if(pathPart.charCodeAt(j)!==nodePart.charCodeAt(j)){let children={};children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));let nextNode=createNode(pathPart.slice(j));children[pathPart.charCodeAt(j)]=nextNode;resetNode(node,nodePart.substring(0,j),children);node=nextNode;break}}}return node}export function insertItem(node,path,item){if(path.charCodeAt(path.length-1)===42){if(path.charCodeAt(path.length-2)===42)visitNode(node,path.substring(0,path.length-2))[4]=item;else(visitNode(node,path.substring(0,path.length-1))[3]??=createParamNode(null))[1]=item}else visitNode(node,path)[1]=item}
package/constants.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export declare const REQ = "__req";
2
- export declare const PATH = "__req_p";
3
- export declare const PATH_LEN = "__req_pl";
4
- export declare const PARAMS = "__req_ps";
5
- export declare const PREV_PARAM_IDX = "__req_ppi";
6
- export declare const CURRENT_PARAM_IDX = "__req_cpi";
package/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { type Node } from './tree/node.js';
2
- import type { RouterCompilerState } from './types.js';
3
- export type Router = [staticMap: Record<string, unknown> | null, root: Node | null];
4
- export declare function createRouter(): Router;
5
- export declare function insertItem(router: Router, path: string, item: any): void;
6
- export declare function compileRouter(router: Router, state: RouterCompilerState): void;
@@ -1,3 +0,0 @@
1
- import type { RouterCompilerState } from '../types.js';
2
- import type { Node } from './node.js';
3
- export declare function compileNode(node: Node, state: RouterCompilerState, hasParam: boolean, hasMultipleParams: boolean, startIndexValue: number, startIndexPrefix: string): void;
package/tree/node.d.ts DELETED
@@ -1,17 +0,0 @@
1
- export type Node = [
2
- part: string,
3
- store: unknown,
4
- children: Record<number, Node> | null,
5
- params: ParamNode | null,
6
- wildcardStore: unknown
7
- ];
8
- export type ParamNode = [
9
- child: Node | null,
10
- store: unknown
11
- ];
12
- export declare function createNode(part: string): Node;
13
- export declare function createParamNode(nextNode: ParamNode[0]): ParamNode;
14
- export declare function cloneNode(node: Node, part: string): Node;
15
- export declare function resetNode(node: Node, part: string, children: Node[2]): void;
16
- export declare function visitNode(node: Node, path: string): Node;
17
- export declare function insertItem(node: Node, path: string, item: unknown): void;
package/types.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { CompilerState } from '@mapl/compiler';
2
- export interface RouterCompilerState extends CompilerState {
3
- compileItem: (item: any, state: RouterCompilerState, hasParam: boolean) => void;
4
- }