@mapl/web 0.3.11 → 0.3.13

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
@@ -37,5 +37,6 @@ Try it out using:
37
37
  ```sh
38
38
  npm install -g degit
39
39
  degit github:mapljs/web/examples/generic
40
+ # or with Bun
41
+ degit github:mapljs/web/examples/bun
40
42
  ```
41
- And change `@mapl/web` version in `package.json`.
@@ -20,7 +20,7 @@ export interface MaplOptions {
20
20
  */
21
21
  asynchronous?: boolean;
22
22
  /**
23
- * Build target
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 _default: (options: MaplOptions) => Promise<void>;
34
- export default _default;
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{compileToExportedDependency as generic}from"../compiler/jit.js";import{compileToExportedDependency as bun}from"../compiler/bun/jit.js";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{}};export default async options=>{let buildOptions=options.build;let hydrateOptions=options.hydrate;let targetOption=options.target;let asyncOption=options.asynchronous;let inputFile=resolve(options.main);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 HANDLER=(targetOption===`bun`?bun:generic)(appMod.default);let outputFile=resolve(options.outputDir,SERVER_ENTRY);writeFileSync(outputFile,`
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 '@mapl/web/compiler/${targetOption===`bun`?`bun/`:``}aot';
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
- ${targetOption===`bun`?`routes`:`fetch`}: getDependency(${HANDLER})
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 '@mapl/web/compiler/${targetOption===`bun`?`bun/`:``}jit';
16
-
17
- export default {
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,2 @@
1
+ import aot from "../../compiler/aot.js";
2
+ export default aot;
@@ -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 { BunContext } from "../index.js";
4
+ import type { RouterTypes } from "bun";
5
+ export type BunRoutes<T extends string = string> = Record<T, RouterTypes.RouteValue<T>>;
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
- export declare const hydrateRouter: (router: RouterTag) => void;
4
- declare const _default: (router: RouterTag) => void;
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
@@ -1,6 +1,5 @@
1
1
  export type Header = [string, string] | readonly [string, string];
2
2
  export interface Context {
3
- req: Request;
4
3
  headers: Header[];
5
4
  status?: number;
6
5
  statusText?: string;
package/core/handler.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Err } from "@safe-std/error";
1
+ import type { AnyErr } from "@safe-std/error";
2
2
  import type { Context } from "./context.js";
3
3
  import type { RouterTag } from "./index.js";
4
4
  import type { RequestMethod } from "./utils.js";
@@ -57,9 +57,10 @@ export declare const text: HandlerResponse<BodyInit>;
57
57
  * @param f
58
58
  */
59
59
  export declare const error: <
60
- const E extends Err,
60
+ C,
61
+ const E extends AnyErr,
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<E = any> {
6
- [_]: E;
5
+ export interface RouterTag<
6
+ C,
7
+ E = any
8
+ > {
9
+ [_]: [E, C];
7
10
  }
8
- declare const _default: <
9
- const T extends AnyMiddlewareTypes[],
10
- const S extends Record<string, RouterTag> = {}
11
- >(middlewares: T, handlers: HandlerTag<T extends [] ? {} : UnionToIntersection<T[number][1]>>[], children?: S) => RouterTag<S[keyof S][typeof _] | T[number][0]>;
12
- export default _default;
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 default (middlewares,handlers,children)=>[middlewares,handlers,,children];
1
+ export let routerImpl=(middlewares,handlers,children)=>[middlewares,handlers,,children];
@@ -1,25 +1,38 @@
1
1
  import type { ScopeState } from "@mapl/framework";
2
- import type { InferErr, InferResult } from "@safe-std/error";
3
2
  import type { AwaitedReturn } from "./utils.js";
4
3
  import type { Context } from "./context.js";
5
- export type MiddlewareHandler = (c: Context) => any;
6
- export type MiddlewareTypes<
7
- E,
8
- S
9
- > = [err: E, state: S];
10
- export type AnyMiddlewareTypes = MiddlewareTypes<any, any>;
4
+ import type { InferErr, InferResult } from "@safe-std/error";
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 _][2];
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: <const T extends MiddlewareHandler>(f: T) => MiddlewareTypes<InferErr<AwaitedReturn<T>>, {}>;
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,5 +1,15 @@
1
- export { default as router } from "./core/index.js";
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";
13
+ export * as stream from "./utils/stream.js";
14
+ export * as basicAuth from "./utils/basic-auth.js";
5
15
  export { default as staticHeaders } from "./utils/static-headers.js";
package/index.js CHANGED
@@ -1 +1 @@
1
- export{default as router}from"./core/index.js";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";
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*as stream from"./utils/stream.js";export*as basicAuth from"./utils/basic-auth.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.11",
3
+ "version": "0.3.13",
4
4
  "description": "A compiled web framework for all runtimes",
5
5
  "keywords": ["fast", "lightweight", "cross-runtime", "framework", "web", "backend"],
6
6
  "repository": {
@@ -15,25 +15,28 @@
15
15
  "@mapl/framework": "^0.5.3",
16
16
  "@mapl/router": "^0.6.2",
17
17
  "@rollup/plugin-terser": "^0.4.4",
18
- "@safe-std/error": "^1.0.1",
18
+ "@safe-std/error": "^2.0.3",
19
19
  "runtime-compiler": "^1.2.6"
20
20
  },
21
21
  "exports": {
22
22
  "./constants": "./constants.js",
23
- "./core/handler": "./core/handler.js",
24
- "./core": "./core/index.js",
25
- "./core/utils": "./core/utils.js",
26
23
  ".": "./index.js",
27
24
  "./core/context": "./core/context.js",
28
25
  "./core/middleware": "./core/middleware.js",
29
- "./build/utils": "./build/utils.js",
30
- "./utils/static-headers": "./utils/static-headers.js",
26
+ "./bun": "./bun/index.js",
27
+ "./core": "./core/index.js",
28
+ "./utils/stream": "./utils/stream.js",
29
+ "./bun/compiler/jit": "./bun/compiler/jit.js",
30
+ "./core/handler": "./core/handler.js",
31
+ "./bun/compiler/aot": "./bun/compiler/aot.js",
32
+ "./bun/compiler/router": "./bun/compiler/router.js",
33
+ "./core/utils": "./core/utils.js",
34
+ "./compiler/aot": "./compiler/aot.js",
31
35
  "./compiler/jit": "./compiler/jit.js",
36
+ "./build/utils": "./build/utils.js",
37
+ "./utils/basic-auth": "./utils/basic-auth.js",
32
38
  "./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
- "./compiler/aot": "./compiler/aot.js",
37
- "./compiler/bun/aot": "./compiler/bun/aot.js"
39
+ "./utils/static-headers": "./utils/static-headers.js",
40
+ "./utils/cors": "./utils/cors.js"
38
41
  }
39
42
  }
@@ -0,0 +1,4 @@
1
+ export declare const extract: (req: Request) => {
2
+ name: string
3
+ pwd: string
4
+ } | undefined;
@@ -0,0 +1 @@
1
+ export let extract=req=>{let header=req.headers.get(`authorization`);if(header!==null&&header.startsWith(`Basic `))try{let sliced=atob(header.slice(6));let sep=sliced.indexOf(`:`);if(sep>-1)return{name:sliced.slice(0,sep),pwd:sliced.slice(sep+1)}}catch{}};
package/utils/cors.d.ts CHANGED
@@ -13,5 +13,5 @@ export declare const allowHeaders: (v: string[] | string) => PreflightHeader;
13
13
  export declare const maxAge: (v: number) => PreflightHeader;
14
14
  export declare const allowCredentials: Header;
15
15
  export declare const exposeHeaders: (v: string[] | string) => Header;
16
- export declare const init: (origins: HeaderValue, preflightHeaders?: PreflightHeader[], headers?: Header[]) => MiddlewareTypes<never, {}>;
16
+ export declare const init: (origins: HeaderValue, preflightHeaders?: PreflightHeader[], headers?: Header[]) => MiddlewareTypes<any, never, {}>;
17
17
  export {};
@@ -1,5 +1,5 @@
1
1
  import type { Header } from "../core/context.js";
2
2
  import { type MiddlewareTypes } from "../core/middleware.js";
3
3
  export declare const injectList: (list: any[]) => string;
4
- declare const _default: (headers: Headers | Header[] | Record<string, any>) => MiddlewareTypes<never, {}>;
4
+ declare const _default: (headers: Headers | Header[] | Record<string, any>) => MiddlewareTypes<any, never, {}>;
5
5
  export default _default;
@@ -0,0 +1,5 @@
1
+ export interface StreamSource<R> extends UnderlyingSource<R> {
2
+ controller: ReadableStreamDefaultController<R>;
3
+ stream: ReadableStream<R>;
4
+ }
5
+ export declare const source: <R>(queueingStrategy?: QueuingStrategy<R>) => StreamSource<R>;
@@ -0,0 +1 @@
1
+ function start(controller){this.controller=controller}export let source=queueingStrategy=>{let source={controller:null,stream:null,start};source.stream=new ReadableStream(source,queueingStrategy);return source};
@@ -1,2 +0,0 @@
1
- import aot from "../aot.js";
2
- export default aot;
@@ -1 +0,0 @@
1
- import aot from"../aot.js";export default aot;
@@ -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;
@@ -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)};
@@ -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