@mapl/web 0.3.11 → 0.3.12
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 +0 -1
- package/build/rolldown.d.ts +3 -4
- package/build/rolldown.js +7 -12
- package/bun/compiler/aot.d.ts +2 -0
- package/bun/compiler/aot.js +1 -0
- package/bun/compiler/jit.d.ts +9 -0
- package/bun/compiler/jit.js +1 -0
- package/bun/compiler/router.js +1 -0
- package/bun/index.d.ts +11 -0
- package/bun/index.js +1 -0
- package/compiler/aot.d.ts +4 -2
- package/compiler/jit.d.ts +5 -4
- package/core/context.d.ts +0 -1
- package/core/handler.d.ts +2 -1
- package/core/index.d.ts +18 -8
- package/core/index.js +1 -1
- package/core/middleware.d.ts +27 -14
- package/index.d.ts +9 -1
- package/index.js +1 -1
- package/package.json +10 -9
- package/compiler/bun/aot.d.ts +0 -2
- package/compiler/bun/aot.js +0 -1
- package/compiler/bun/jit.d.ts +0 -8
- package/compiler/bun/jit.js +0 -1
- package/compiler/bun/router.js +0 -1
- /package/{compiler/bun → bun/compiler}/router.d.ts +0 -0
package/README.md
CHANGED
package/build/rolldown.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export interface MaplOptions {
|
|
|
20
20
|
*/
|
|
21
21
|
asynchronous?: boolean;
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* Compiler module to use
|
|
24
24
|
*/
|
|
25
25
|
target?: "bun";
|
|
26
26
|
/**
|
|
@@ -30,7 +30,6 @@ export interface MaplOptions {
|
|
|
30
30
|
}
|
|
31
31
|
export declare const hydrateImportsPlugin: RolldownPluginOption;
|
|
32
32
|
export declare const SERVER_ENTRY = "index.js";
|
|
33
|
-
declare const
|
|
34
|
-
export
|
|
35
|
-
export declare const watcherReady: (watcher: RolldownWatcher) => Promise<void>;
|
|
33
|
+
export declare const hydrate: (options: MaplOptions) => Promise<void>;
|
|
34
|
+
export declare const onceBundled: (watcher: RolldownWatcher) => Promise<void>;
|
|
36
35
|
export declare const dev: (options: MaplOptions) => RolldownWatcher | void;
|
package/build/rolldown.js
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
|
-
import{mkdirSync,writeFileSync}from"node:fs";import{build,watch}from"rolldown";import{
|
|
1
|
+
import{mkdirSync,writeFileSync}from"node:fs";import{build,watch}from"rolldown";import{evaluateToString}from"runtime-compiler/jit";import{resolve}from"node:path";import{EXTERNALS}from"./utils.js";export let hydrateImportsPlugin={name:`mapl-web-hydrate-config-replacer`,resolveId(source){return this.resolve(source===`runtime-compiler/config`?`runtime-compiler/hydrate-config`:source)}};export let SERVER_ENTRY=`index.js`;let mkdirSafe=dir=>{try{mkdirSync(dir,{recursive:true})}catch{}};let getTargetCompilerMod=targetOption=>targetOption==null?`@mapl/web/compiler/`:`@mapl/web/`+targetOption+`/compiler/`;export let hydrate=async options=>{let buildOptions=options.build;let hydrateOptions=options.hydrate;let asyncOption=options.asynchronous;let inputFile=resolve(options.main);let compilerPrefix=getTargetCompilerMod(options.target);let tmpFile;if(buildOptions==null){tmpFile=inputFile;mkdirSafe(options.outputDir)}else{tmpFile=resolve(options.outputDir,`tmp.js`);let outputOptions=buildOptions?.output;let currentExternals=buildOptions?.external;await build({...buildOptions,input:inputFile,output:{...outputOptions,file:tmpFile},external:currentExternals==null?EXTERNALS:typeof currentExternals===`function`?(id,...args)=>EXTERNALS.includes(id)||currentExternals(id,...args):EXTERNALS.concat(currentExternals)})}let appMod=await import(tmpFile);let{compileToExportedDependency}=await import(compilerPrefix+`jit`);let HANDLER=compileToExportedDependency(appMod.default);let outputFile=resolve(options.outputDir,SERVER_ENTRY);writeFileSync(outputFile,`
|
|
2
2
|
import app from ${JSON.stringify(tmpFile)};
|
|
3
|
-
import hydrateRouter from '
|
|
3
|
+
import hydrateRouter from '${compilerPrefix}aot';
|
|
4
4
|
hydrateRouter(app);
|
|
5
5
|
|
|
6
6
|
import { hydrate } from 'runtime-compiler/hydrate';
|
|
7
7
|
import { getDependency } from 'runtime-compiler';
|
|
8
8
|
|
|
9
9
|
${asyncOption?`await(async`:`(`}${evaluateToString()})(...hydrate());
|
|
10
|
-
export default {
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
`);let currentPlugins=hydrateOptions?.plugins;await build({...hydrateOptions,input:outputFile,output:{...hydrateOptions?.output,file:outputFile},plugins:!currentPlugins?hydrateImportsPlugin:Array.isArray(currentPlugins)?[hydrateImportsPlugin].concat(currentPlugins):[hydrateImportsPlugin,currentPlugins]})};let jitContent=(inputFile,options)=>{let asyncOption=options.asynchronous===true;let targetOption=options.target;return`
|
|
10
|
+
export default getDependency(${HANDLER});
|
|
11
|
+
`);let currentPlugins=hydrateOptions?.plugins;await build({...hydrateOptions,input:outputFile,output:{...hydrateOptions?.output,file:outputFile},plugins:!currentPlugins?hydrateImportsPlugin:Array.isArray(currentPlugins)?[hydrateImportsPlugin].concat(currentPlugins):[hydrateImportsPlugin,currentPlugins]})};let jitContent=(inputFile,options)=>{let asyncOption=options.asynchronous===true;return`
|
|
14
12
|
import app from ${JSON.stringify(inputFile)};
|
|
15
|
-
import { ${asyncOption?`compileToHandler`:`compileToHandlerSync`} } from '
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
${targetOption===`bun`?`routes`:`fetch`}: ${asyncOption?`await compileToHandler`:`compileToHandlerSync`}(app)
|
|
19
|
-
};
|
|
20
|
-
`};export let watcherReady=watcher=>new Promise((res,rej)=>{let listener=e=>{if(e.code===`ERROR`)rej(e.error);else if(e.code===`BUNDLE_END`)res();watcher.off(`event`,listener)};watcher.on(`event`,listener)});export let dev=options=>{let outputFile=resolve(options.outputDir,SERVER_ENTRY);mkdirSafe(options.outputDir);let content=jitContent(resolve(options.main),options);if(options.build!=null){let buildOptions=options.build;let outputOptions=buildOptions?.output;let tmpFile=resolve(options.outputDir,`tmp.js`);mkdirSafe(options.outputDir);writeFileSync(tmpFile,content);return watch({...buildOptions,input:tmpFile,output:{...outputOptions,file:outputFile}})}writeFileSync(outputFile,content)};
|
|
13
|
+
import { ${asyncOption?`compileToHandler`:`compileToHandlerSync`} } from '${getTargetCompilerMod(options.target)}jit';
|
|
14
|
+
export default ${asyncOption?`await compileToHandler`:`compileToHandlerSync`}(app);
|
|
15
|
+
`};export let onceBundled=watcher=>new Promise((res,rej)=>watcher.on(`event`,function f(e){if(e.code===`ERROR`)rej(e.error);else if(e.code===`BUNDLE_END`)res();watcher.off(`event`,f)}));export let dev=options=>{let outputFile=resolve(options.outputDir,SERVER_ENTRY);mkdirSafe(options.outputDir);let content=jitContent(resolve(options.main),options);if(options.build!=null){let buildOptions=options.build;let outputOptions=buildOptions?.output;let tmpFile=resolve(options.outputDir,`tmp.js`);mkdirSafe(options.outputDir);writeFileSync(tmpFile,content);return watch({...buildOptions,input:tmpFile,output:{...outputOptions,file:outputFile}})}writeFileSync(outputFile,content)};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import aot from"../../compiler/aot.js";export default aot;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { RouterTag } from "../../core/index.js";
|
|
2
|
+
import { type CompiledDependency } from "runtime-compiler";
|
|
3
|
+
import type { FetchFn } from "../../core/utils.js";
|
|
4
|
+
import type { BunContext } from "../index.js";
|
|
5
|
+
export type BunRoutes = Record<string, Record<string, FetchFn> | FetchFn>;
|
|
6
|
+
export declare const compileToString: (router: RouterTag<BunContext>) => string;
|
|
7
|
+
export declare const compileToExportedDependency: (router: RouterTag<BunContext>) => CompiledDependency<BunRoutes>;
|
|
8
|
+
export declare const compileToHandler: (router: RouterTag<BunContext>) => Promise<BunRoutes>;
|
|
9
|
+
export declare const compileToHandlerSync: (router: RouterTag<BunContext>) => BunRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{compileGroup,AsyncFunction,contextInit,setContextInit,hooks}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"../../compiler/jit.js";import{insertRoute,resetRouter,routerToString}from"./router.js";let compileToState=router=>{resetRouter();hooks.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]){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]?`})()`:``))};hooks.compileErrorHandler=compileErrorHandler;setContextInit(`let h=[],c={status:200,req:r,headers:h,server:s};`);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 @@
|
|
|
1
|
+
import{injectDependency}from"runtime-compiler";import{RES404}from"../../compiler/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/bun/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { BunRequest, Server } from "bun";
|
|
2
|
+
import { type ChildRouters, type InferHandlers, type InferRouter } from "../core/index.js";
|
|
3
|
+
import type { MiddlewareTypes } from "../core/middleware.js";
|
|
4
|
+
export interface BunContext {
|
|
5
|
+
readonly req: BunRequest;
|
|
6
|
+
readonly server: Server;
|
|
7
|
+
}
|
|
8
|
+
export declare const router: <
|
|
9
|
+
const T extends MiddlewareTypes<BunContext, any, any>[],
|
|
10
|
+
const S extends ChildRouters<BunContext> = {}
|
|
11
|
+
>(middlewares: T, handlers: InferHandlers<BunContext, T>, children?: S) => InferRouter<BunContext, T, S>;
|
package/bun/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{routerImpl}from"../core/index.js";export let router=routerImpl;
|
package/compiler/aot.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { RouterTag } from "../core/index.js";
|
|
2
2
|
import "../core/context.js";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import type { GenericContext } from "../index.js";
|
|
4
|
+
import type { BunContext } from "../bun/index.js";
|
|
5
|
+
export declare const hydrateRouter: (router: RouterTag<GenericContext> | RouterTag<BunContext>) => void;
|
|
6
|
+
declare const _default: (router: RouterTag<GenericContext> | RouterTag<BunContext>) => void;
|
|
5
7
|
export default _default;
|
package/compiler/jit.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { RouterTag } from "../core/index.js";
|
|
2
2
|
import { type CompiledDependency } from "runtime-compiler";
|
|
3
3
|
import type { FetchFn } from "../core/utils.js";
|
|
4
|
+
import type { GenericContext } from "../index.js";
|
|
4
5
|
export declare const RES404: string;
|
|
5
6
|
export declare const RES400: string;
|
|
6
7
|
export declare const paramArgs: string[];
|
|
7
|
-
export declare const compileToString: (router: RouterTag) => string;
|
|
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;
|
|
8
|
+
export declare const compileToString: (router: RouterTag<GenericContext>) => string;
|
|
9
|
+
export declare const compileToExportedDependency: (router: RouterTag<GenericContext>) => CompiledDependency<FetchFn>;
|
|
10
|
+
export declare const compileToHandler: (router: RouterTag<GenericContext>) => Promise<FetchFn>;
|
|
11
|
+
export declare const compileToHandlerSync: (router: RouterTag<GenericContext>) => FetchFn;
|
package/core/context.d.ts
CHANGED
package/core/handler.d.ts
CHANGED
|
@@ -57,9 +57,10 @@ export declare const text: HandlerResponse<BodyInit>;
|
|
|
57
57
|
* @param f
|
|
58
58
|
*/
|
|
59
59
|
export declare const error: <
|
|
60
|
+
C,
|
|
60
61
|
const E extends Err,
|
|
61
62
|
const D extends HandlerData | undefined = undefined
|
|
62
|
-
>(r: RouterTag<E>, f: NoInfer<(err: E, c: Context) => InferReturn<D>>, dat?: D) => RouterTag<never>;
|
|
63
|
+
>(r: RouterTag<C, E>, f: NoInfer<(err: E, c: Context & C) => InferReturn<D>>, dat?: D) => RouterTag<C, never>;
|
|
63
64
|
/**
|
|
64
65
|
* Handle requests to a path with a specific method
|
|
65
66
|
* @param method
|
package/core/index.d.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import type { HandlerTag } from "./handler.js";
|
|
2
|
-
import type { AnyMiddlewareTypes } from "./middleware.js";
|
|
2
|
+
import type { AnyMiddlewareTypes, InferMiddlewareState, InferMiddlewareErr } from "./middleware.js";
|
|
3
3
|
import type { UnionToIntersection } from "./utils.js";
|
|
4
4
|
declare const _: unique symbol;
|
|
5
|
-
export interface RouterTag<
|
|
6
|
-
|
|
5
|
+
export interface RouterTag<
|
|
6
|
+
C,
|
|
7
|
+
E = any
|
|
8
|
+
> {
|
|
9
|
+
[_]: [E, C];
|
|
7
10
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
export type ChildRouters<C> = Record<string, RouterTag<C, any>>;
|
|
12
|
+
export type InferHandlers<
|
|
13
|
+
C,
|
|
14
|
+
T extends AnyMiddlewareTypes[]
|
|
15
|
+
> = HandlerTag<T extends [] ? C : C & UnionToIntersection<InferMiddlewareState<T[number]>>>[];
|
|
16
|
+
export type InferRouterErr<T extends RouterTag<any>> = T[typeof _][0];
|
|
17
|
+
export type InferRouter<
|
|
18
|
+
C,
|
|
19
|
+
T extends AnyMiddlewareTypes[],
|
|
20
|
+
S extends ChildRouters<C>
|
|
21
|
+
> = RouterTag<C, InferRouterErr<S[keyof S]> | InferMiddlewareErr<T[number]>>;
|
|
22
|
+
export {};
|
package/core/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export let routerImpl=(middlewares,handlers,children)=>[middlewares,handlers,,children];
|
package/core/middleware.d.ts
CHANGED
|
@@ -2,24 +2,37 @@ import type { ScopeState } from "@mapl/framework";
|
|
|
2
2
|
import type { InferErr, InferResult } from "@safe-std/error";
|
|
3
3
|
import type { AwaitedReturn } from "./utils.js";
|
|
4
4
|
import type { Context } from "./context.js";
|
|
5
|
-
|
|
6
|
-
export type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
declare const _: unique symbol;
|
|
6
|
+
export type MiddlewareHandler<C> = (c: Context & C) => any;
|
|
7
|
+
export interface MiddlewareTypes<
|
|
8
|
+
Context,
|
|
9
|
+
Err,
|
|
10
|
+
State
|
|
11
|
+
> {
|
|
12
|
+
[_]: [Context, Err, State];
|
|
13
|
+
}
|
|
14
|
+
export type AnyMiddlewareTypes = MiddlewareTypes<any, any, any>;
|
|
15
|
+
export type InferMiddlewareState<T extends AnyMiddlewareTypes> = T[typeof _][1];
|
|
16
|
+
export type InferMiddlewareErr<T extends AnyMiddlewareTypes> = T[typeof _][1];
|
|
11
17
|
export declare const macro: <
|
|
18
|
+
C,
|
|
12
19
|
E = never,
|
|
13
20
|
S = {}
|
|
14
|
-
>(f: (scope: ScopeState) => string) => MiddlewareTypes<E, S>;
|
|
15
|
-
export declare const noOpMacro: MiddlewareTypes<never, {}>;
|
|
16
|
-
export declare const tap: (f: MiddlewareHandler) => MiddlewareTypes<never, {}>;
|
|
21
|
+
>(f: (scope: ScopeState) => string) => MiddlewareTypes<C, E, S>;
|
|
22
|
+
export declare const noOpMacro: MiddlewareTypes<any, never, {}>;
|
|
23
|
+
export declare const tap: <C>(f: MiddlewareHandler<C>) => MiddlewareTypes<C, never, {}>;
|
|
17
24
|
export declare const attach: <
|
|
25
|
+
C,
|
|
18
26
|
Prop extends string,
|
|
19
|
-
const T extends MiddlewareHandler
|
|
20
|
-
>(prop: Prop, f: T) => MiddlewareTypes<never, Record<Prop, AwaitedReturn<T>>>;
|
|
21
|
-
export declare const validate: <
|
|
27
|
+
const T extends MiddlewareHandler<C>
|
|
28
|
+
>(prop: Prop, f: T) => MiddlewareTypes<C, never, Record<Prop, AwaitedReturn<T>>>;
|
|
29
|
+
export declare const validate: <
|
|
30
|
+
C,
|
|
31
|
+
const T extends MiddlewareHandler<C>
|
|
32
|
+
>(f: T) => MiddlewareTypes<C, InferErr<AwaitedReturn<T>>, {}>;
|
|
22
33
|
export declare const parse: <
|
|
34
|
+
C,
|
|
23
35
|
Prop extends string,
|
|
24
|
-
const T extends MiddlewareHandler
|
|
25
|
-
>(prop: Prop, f: T) => MiddlewareTypes<InferErr<AwaitedReturn<T>>, Record<Prop, InferResult<AwaitedReturn<T>>>>;
|
|
36
|
+
const T extends MiddlewareHandler<C>
|
|
37
|
+
>(prop: Prop, f: T) => MiddlewareTypes<C, InferErr<AwaitedReturn<T>>, Record<Prop, InferResult<AwaitedReturn<T>>>>;
|
|
38
|
+
export {};
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import { type ChildRouters, type InferHandlers, type InferRouter } from "./core/index.js";
|
|
2
|
+
import type { MiddlewareTypes } from "./core/middleware.js";
|
|
3
|
+
export interface GenericContext {
|
|
4
|
+
readonly req: Request;
|
|
5
|
+
}
|
|
6
|
+
export declare const router: <
|
|
7
|
+
const T extends MiddlewareTypes<GenericContext, any, any>[],
|
|
8
|
+
const S extends ChildRouters<GenericContext> = {}
|
|
9
|
+
>(middlewares: T, handlers: InferHandlers<GenericContext, T>, children?: S) => InferRouter<GenericContext, T, S>;
|
|
2
10
|
export * as layer from "./core/middleware.js";
|
|
3
11
|
export * as handle from "./core/handler.js";
|
|
4
12
|
export * as cors from "./utils/cors.js";
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import{routerImpl}from"./core/index.js";export let router=routerImpl;export*as layer from"./core/middleware.js";export*as handle from"./core/handler.js";export*as cors from"./utils/cors.js";export{default as staticHeaders}from"./utils/static-headers.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapl/web",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.12",
|
|
4
4
|
"description": "A compiled web framework for all runtimes",
|
|
5
5
|
"keywords": ["fast", "lightweight", "cross-runtime", "framework", "web", "backend"],
|
|
6
6
|
"repository": {
|
|
@@ -19,21 +19,22 @@
|
|
|
19
19
|
"runtime-compiler": "^1.2.6"
|
|
20
20
|
},
|
|
21
21
|
"exports": {
|
|
22
|
-
"./constants": "./constants.js",
|
|
23
22
|
"./core/handler": "./core/handler.js",
|
|
24
23
|
"./core": "./core/index.js",
|
|
25
24
|
"./core/utils": "./core/utils.js",
|
|
26
25
|
".": "./index.js",
|
|
27
|
-
"./
|
|
26
|
+
"./constants": "./constants.js",
|
|
28
27
|
"./core/middleware": "./core/middleware.js",
|
|
29
|
-
"./
|
|
28
|
+
"./core/context": "./core/context.js",
|
|
29
|
+
"./utils/cors": "./utils/cors.js",
|
|
30
30
|
"./utils/static-headers": "./utils/static-headers.js",
|
|
31
|
-
"./
|
|
31
|
+
"./bun": "./bun/index.js",
|
|
32
|
+
"./bun/compiler/jit": "./bun/compiler/jit.js",
|
|
33
|
+
"./bun/compiler/aot": "./bun/compiler/aot.js",
|
|
34
|
+
"./bun/compiler/router": "./bun/compiler/router.js",
|
|
35
|
+
"./build/utils": "./build/utils.js",
|
|
32
36
|
"./build/rolldown": "./build/rolldown.js",
|
|
33
|
-
"./compiler/bun/jit": "./compiler/bun/jit.js",
|
|
34
|
-
"./compiler/bun/router": "./compiler/bun/router.js",
|
|
35
|
-
"./utils/cors": "./utils/cors.js",
|
|
36
37
|
"./compiler/aot": "./compiler/aot.js",
|
|
37
|
-
"./compiler/
|
|
38
|
+
"./compiler/jit": "./compiler/jit.js"
|
|
38
39
|
}
|
|
39
40
|
}
|
package/compiler/bun/aot.d.ts
DELETED
package/compiler/bun/aot.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import aot from"../aot.js";export default aot;
|
package/compiler/bun/jit.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
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;
|
package/compiler/bun/jit.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{compileGroup,AsyncFunction,contextInit,setContextInit,hooks}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();hooks.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]){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]?`})()`:``))};hooks.compileErrorHandler=compileErrorHandler;setContextInit(`let h=[],c={status:200,req:r,headers:h,server:s};`);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)};
|
package/compiler/bun/router.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
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+`}`};
|
|
File without changes
|