k99 0.6.0-beta.2 → 0.6.0-beta.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/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
@@ -636,14 +636,21 @@ function storeService(destroy, exec, options) {
636
636
  * @returns {PromiseLike<boolean | import('./main/types').Handler | void> | boolean | import('./main/types').Handler | void}
637
637
  */
638
638
 
639
+ /**
640
+ * @callback Onionskin
641
+ * @param {import('./main/types').Context} ctx
642
+ * @param {() => Promise<import('./main/types').HandlerResult>} next
643
+ * @returns {PromiseLike<import('./main/types').HandlerResult> | import('./main/types').HandlerResult}
644
+ */
645
+
639
646
  /**
640
647
  * @typedef {[import('./main/types').Handler | Router, Record<string, any>, string[]]} FindItem
641
648
  */
642
649
  /**
643
650
  * @callback Finder
644
651
  * @this {Router}
645
- * @property {import('./main/types').Method} method
646
- * @property {string[]} path
652
+ * @param {import('./main/types').Method} method
653
+ * @param {string[]} path
647
654
  * @param {import('./main/types').Context} ctx
648
655
  * @returns {AsyncIterable<FindItem> | Iterable<FindItem>}
649
656
  */
@@ -665,6 +672,7 @@ async function execGuard(guards, ctx, setParams, params) {
665
672
  params: { value: { ...params } },
666
673
  }));
667
674
  if (ret === false) { return false; }
675
+ // @ts-ignore
668
676
  if (typeof ret === 'function') { return ret; }
669
677
  }
670
678
  return true;
@@ -692,10 +700,11 @@ async function find(route, path, ctx, setParams, params) {
692
700
  for await (const [r, result, p] of route.find(ctx.method, path, ctx)) {
693
701
  if (ctx.destroyed) { return null; }
694
702
  const res = await find(r, p, ctx, setParams, { ...params, ...result });
695
- if (res) { return res; }
703
+ if (res) { return route.__onionskin(res); }
696
704
  }
697
705
  return null;
698
706
  }
707
+
699
708
  /**
700
709
  *
701
710
  * @param {string} t
@@ -720,7 +729,7 @@ class Router {
720
729
  * @param {import('./main/types').Context} ctx
721
730
  * @returns {AsyncIterable<FindItem> | Iterable<FindItem>}
722
731
  */
723
- find(method, path, ctx) { return [] }
732
+ find(method, path, ctx) { return []; }
724
733
  /**
725
734
  *
726
735
  * @param {Router[]} routers
@@ -750,6 +759,20 @@ class Router {
750
759
  }
751
760
  /** @readonly @type {Set<Guard>} */
752
761
  guards = new Set();
762
+ /**
763
+ *
764
+ * @param {import('./main/types').Handler} h
765
+ * @returns {import('./main/types').Handler}
766
+ */
767
+ __onionskin = (h) => h;
768
+ /** @param {Onionskin} os */
769
+ onionskin(os) {
770
+ let run = this.__onionskin;
771
+ this.__onionskin = h => {
772
+ const h2 = run(h);
773
+ return async (ctx) => os(ctx, async () => h2(ctx));
774
+ };
775
+ }
753
776
  }
754
777
 
755
778
  /**
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
@@ -121,8 +121,9 @@ interface FindHandler {
121
121
  }
122
122
 
123
123
  type Guard = (ctx: Context) => PromiseLike<boolean | Handler | void> | boolean | Handler | void;
124
+ type Onionskin = (ctx: Context, next: () => Promise<HandlerResult>) => PromiseLike<HandlerResult> | HandlerResult;
124
125
  type FindItem = [Handler | Router, Record<string, any>, string[]];
125
- type Finder = (this: Router) => any;
126
+ type Finder = (this: Router, method: Method, path: string[], ctx: Context) => AsyncIterable<FindItem> | Iterable<FindItem>;
126
127
  /**
127
128
  * @abstract
128
129
  */
@@ -150,6 +151,14 @@ declare class Router {
150
151
  find(method: Method, path: string[], ctx: Context): AsyncIterable<FindItem> | Iterable<FindItem>;
151
152
  /** @readonly @type {Set<Guard>} */
152
153
  readonly guards: Set<Guard>;
154
+ /**
155
+ *
156
+ * @param {import('./main/types').Handler} h
157
+ * @returns {import('./main/types').Handler}
158
+ */
159
+ __onionskin: (h: Handler) => Handler;
160
+ /** @param {Onionskin} os */
161
+ onionskin(os: Onionskin): void;
153
162
  }
154
163
 
155
164
  declare class ApiRouter extends Router {
@@ -566,4 +575,4 @@ declare function storeService<T>(destroy?: ((state: T | undefined, ctx: Context,
566
575
  */
567
576
  declare function createFetch(run: (request: Request) => Promise<Response | null>, notFound?: ((request: Request) => Response | Promise<Response>) | null | undefined): (input: RequestInfo, init?: RequestInit) => Promise<Response>;
568
577
 
569
- export { ApiRouter, type Binder, type Context, type Cookie, type CookieOption, type FindHandler, type FindItem, type Finder, type Guard, type Handler, type HandlerResult, type Match, type Method, type Options, type Route, type RouteBinder, Router, type RouterRoute, type Runner, Service, type StateService, type StoreService, createFetch, main, make, merge, service, stateService, storeService };
578
+ export { ApiRouter, type Binder, type Context, type Cookie, type CookieOption, type FindHandler, type FindItem, type Finder, type Guard, type Handler, type HandlerResult, type Match, type Method, type Onionskin, type Options, type Route, type RouteBinder, Router, type RouterRoute, type Runner, Service, type StateService, type StoreService, createFetch, main, make, merge, service, stateService, storeService };
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
@@ -640,14 +640,21 @@
640
640
  * @returns {PromiseLike<boolean | import('./main/types').Handler | void> | boolean | import('./main/types').Handler | void}
641
641
  */
642
642
 
643
+ /**
644
+ * @callback Onionskin
645
+ * @param {import('./main/types').Context} ctx
646
+ * @param {() => Promise<import('./main/types').HandlerResult>} next
647
+ * @returns {PromiseLike<import('./main/types').HandlerResult> | import('./main/types').HandlerResult}
648
+ */
649
+
643
650
  /**
644
651
  * @typedef {[import('./main/types').Handler | Router, Record<string, any>, string[]]} FindItem
645
652
  */
646
653
  /**
647
654
  * @callback Finder
648
655
  * @this {Router}
649
- * @property {import('./main/types').Method} method
650
- * @property {string[]} path
656
+ * @param {import('./main/types').Method} method
657
+ * @param {string[]} path
651
658
  * @param {import('./main/types').Context} ctx
652
659
  * @returns {AsyncIterable<FindItem> | Iterable<FindItem>}
653
660
  */
@@ -669,6 +676,7 @@
669
676
  params: { value: { ...params } },
670
677
  }));
671
678
  if (ret === false) { return false; }
679
+ // @ts-ignore
672
680
  if (typeof ret === 'function') { return ret; }
673
681
  }
674
682
  return true;
@@ -696,10 +704,11 @@
696
704
  for await (const [r, result, p] of route.find(ctx.method, path, ctx)) {
697
705
  if (ctx.destroyed) { return null; }
698
706
  const res = await find(r, p, ctx, setParams, { ...params, ...result });
699
- if (res) { return res; }
707
+ if (res) { return route.__onionskin(res); }
700
708
  }
701
709
  return null;
702
710
  }
711
+
703
712
  /**
704
713
  *
705
714
  * @param {string} t
@@ -724,7 +733,7 @@
724
733
  * @param {import('./main/types').Context} ctx
725
734
  * @returns {AsyncIterable<FindItem> | Iterable<FindItem>}
726
735
  */
727
- find(method, path, ctx) { return [] }
736
+ find(method, path, ctx) { return []; }
728
737
  /**
729
738
  *
730
739
  * @param {Router[]} routers
@@ -754,6 +763,20 @@
754
763
  }
755
764
  /** @readonly @type {Set<Guard>} */
756
765
  guards = new Set();
766
+ /**
767
+ *
768
+ * @param {import('./main/types').Handler} h
769
+ * @returns {import('./main/types').Handler}
770
+ */
771
+ __onionskin = (h) => h;
772
+ /** @param {Onionskin} os */
773
+ onionskin(os) {
774
+ let run = this.__onionskin;
775
+ this.__onionskin = h => {
776
+ const h2 = run(h);
777
+ return async (ctx) => os(ctx, async () => h2(ctx));
778
+ };
779
+ }
757
780
  }
758
781
 
759
782
  /**
package/index.min.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
6
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).k99={})}(this,(function(e){"use strict";function t(e){return(new TextEncoder).encode(e)}function n(e){if(e instanceof ArrayBuffer)return!0;try{if(e instanceof SharedArrayBuffer)return!0}catch{}return!1}function r(e){return Symbol.asyncIterator in e||Symbol.iterator in e}async function o(e,i){if("string"==typeof i)return e.write(t(i));if("object"==typeof i){if(ArrayBuffer.isView(i))return e.write(new Uint8Array(i.buffer,i.byteOffset,i.byteLength));if(n(i))return e.write(new Uint8Array(i));if(r(i))for await(const t of i)t&&await o(e,t)}}function i(e,t){return"bigint"==typeof t?String(t):t}function s(e,s,u){const c=function(e,s){if(e instanceof ReadableStream)return[e,0,""];if(e instanceof URLSearchParams)return[e,0,""];if(e instanceof Blob)return[e,e.size,e.type];if(e instanceof FormData)return[e,0,""];if(ArrayBuffer.isView(e)||n(e))return[e,e.byteLength,""];if("string"==typeof e){const n=t(e);return[n,n.byteLength,""]}if("object"!=typeof e)return null;if(Array.isArray(e)||!r(e)){const n=t(JSON.stringify(e,i));return[n,n.byteLength,"application/json"]}const{writable:u,readable:c}=new TransformStream,f=u.getWriter();return s?.catch((e=>{u.abort(e||new DOMException("The user aborted a request.")).catch((()=>{}))})),(async()=>{for await(const t of e)t&&await o(f,t);await u.close()})().catch((()=>{})),[c,0,""]}(e,u);if(!c)return null;const[f,a,l]=c;return l&&!s.get("Content-Type")&&s.set("Content-Type",l),a>0&&!s.get("Content-Length")&&s.set("Content-Length",String(a)),f}function u(e,t){e.delete("set-cookie");for(const{name:n,value:r,expire:o,domain:i,path:s,secure:u,httpOnly:c}of t)n&&e.append("set-cookie",[`${encodeURI(n)}=${encodeURI(r||"")}`,o&&`Expires=${o}`,i&&`Domain=${encodeURI(i)}`,s&&`Path=${encodeURI(s)}`,u&&"Secure",c&&"HttpOnly"].filter(Boolean).join("; "))}const c=new Set(["GET","OPTIONS"]);function f(e,t,n){n?e.set(t,n):e.delete(t)}function a(e,t,{runner:n,error:r,method:o,environment:i}={}){return function e(a,l){const p=function(e,t){let n="";return"string"==typeof t?n=t:"function"==typeof t&&(n=t(e)),n&&"string"==typeof n||(n=e.method||"GET"),n.toUpperCase()}(a,o),h=new URL(a.url),{signal:y,headers:d}=a,g=function(e){return new Promise(((t,n)=>{if(e.aborted)return n(e.reason);e.addEventListener("abort",(()=>n(e.reason)),{once:!0})}))}(y),m=new Map,w=function(e){let t={};for(const n of e.replace(/\s/g,"").split(";")){const e=n.split("=");t[decodeURIComponent(e.shift())]=decodeURIComponent(e.join("="))}return t}(d.get("cookie")||""),b=[],O=new Headers,T=l?.root;let E=200,S=!1,j=null,R=()=>{},U=e=>{};const P=new Promise(((e,t)=>{R=e,U=t}));P.catch((()=>{}));let v={};const A={environment:i,parent:l,get error(){return j},get root(){return T||this},signal:y,url:h,fetch(t,{method:n="get",signal:r,body:o,headers:i}={}){const u=new URL(t,h),f=new Headers(i||{});if(!o||c.has(n.toUpperCase()))return e(new Request(u,{method:n,headers:f,signal:r}),A);const a=s(o,f);return e(new Request(u,{method:n,headers:f,signal:r,body:a}),A)},done(e,t){if(S)return null;const n=P.then(e,t);return n.catch(r),n},service(e,...t){if(e.rootOnly&&T)return T.service(e,...t);let n=m.get(e);if(!n){if(n=e(A),"function"!=typeof n)return;m.set(e,n)}return n(...t)},method:p,get params(){return v},requestHeaders:d,requestType:d.get("content-type")||"",referer:d.get("referer")||"",userAgent:d.get("user-agent")||"",accept:(d.get("accept")||"").split(/,\s*/).filter(Boolean),acceptLanguage:(d.get("accept-language")||"").split(/,\s*/).filter(Boolean),cookies:w,request:a,get destroyed(){return S},get status(){return E},set status(e){E=e},responseHeaders:O,get location(){return O.get("location")||""},set location(e){f(O,"location",e)},get responseType(){return O.get("content-type")||""},set responseType(e){f(O,"content-type",e)},getCookie:e=>function*(e,t){const n=e;for(const e of n)t&&e.name!==t||(yield{...e})}(b,e),setCookie(e,t,{expire:n,domain:r,path:o,secure:i,httpOnly:s}={}){b.push({name:e,value:t,expire:n,domain:r,path:o,secure:i,httpOnly:s}),u(O,b)},clearCookie(e,t){!function(e,t,n,r){let o="Fri, 31 Dec 1999 16:00:00 GMT";if("string"==typeof n){if(!n)return;const{domain:t,path:i,secure:s,httpOnly:u}=!0!==r&&r||{};e.push({name:n,value:"delete",expire:o,domain:t,path:i,secure:s,httpOnly:u})}else{const{domain:i,path:s,secure:u,httpOnly:c}=n||{};if(e.length=0,r)for(let n in t)e.push({name:n,value:"delete",expire:o,domain:i,path:s,secure:u,httpOnly:c})}}(b,w,e,t),u(O,b)}};function x(){return Promise.race([g,Promise.resolve().then((()=>t(A,(e=>{v=e}))))]).then((e=>e?Promise.race([g,e(A)]).then((e=>{if(e instanceof Response)return e;const t=new Headers(A.responseHeaders),{status:n}=A;if(!e)return new Response(null,{status:n,headers:t});const r=s(e,t,g);return new Response(r,{status:n,headers:t})})):null)).then((e=>(S=!0,R(),e)),(e=>(S=!0,j=e||!0,U(j),Promise.reject(e))))}return n?n(A,x):x()}(e)}async function l(e,t,n,r,o){if(!(e instanceof h))return r(o),e;if(e.disabled)return null;const i=await async function(e,t,n,r){if(!e.size)return!0;n(r);for(const n of e){if(t.destroyed)return!1;const e=await n(Object.create(t,{params:{value:{...r}}}));if(!1===e)return!1;if("function"==typeof e)return e}return!0}(e.guards,n,r,o);if(!i)return null;if("function"==typeof i)return i;if(n.destroyed)return null;for await(const[i,s,u]of e.find(n.method,t,n)){if(n.destroyed)return null;const e=await l(i,u,n,r,{...o,...s});if(e)return e}return null}function p(e){try{return decodeURIComponent(e)}catch{return e}}class h{disabled=!1;find(e,t,n){return[]}static make(e){return async(t,n)=>{const r=e.flat(),o=t.url.pathname.split("/").filter(Boolean).map(p);for(const e of r){const r=await l(e,o,t,n,{});if(r)return r}return null}}static create(e){return Object.create(h.prototype,{find:{configurable:!0,value:e,writable:!0}})}guards=new Set}const y=/^:([a-zA-Z][a-zA-Z0-9]*)(?:\((.+)\))?([ius]+)?([?+*]?)$/;function d(e){const t=y.exec(e);if(!t)return e;const[,n,r=".*",o,i]=t;if(!r)return{name:n,pattern:new RegExp("^.*$",o),optional:"?"===i||"*"===i,many:"+"===i||"*"===i};let s=0,u=0;const c=["^(?:"];for(;s<r.length;){const t=r[s++];if(c.push(t),"\\"!==t)if(")"!==t)if("["!==t){if("("===t&&(u++,"?"===r[s]&&(s+=2,":"!==r[s-1])))return e}else for(;s<r.length;){const e=r[s++];if(c.push(e),"]"===e)break;"\\"===e&&c.push(r[s++])}else{if(0===u)return e;u--}else c.push(r[s++])}return u?e:(c.push(")$"),{name:n,pattern:new RegExp(c.join(""),o),optional:"?"===i||"*"===i,many:"+"===i||"*"===i})}function g(e,t){const n=[];for(const t of e.split("/"))t&&!/^\.+$/.test(t)&&n.push(d(t));if(n.length)return e=>function(e,t,n){const r={};for(let n=0;n<e.length;n++){const o=e[n],i=t[n];if(o!==i){if("string"==typeof o)return;if(!i)return o.optional?[r,[]]:void 0;if(!o.pattern.test(i))return;r[o.name]=i}}if(!n)return[r,t.slice(e.length)];if(t.length<=e.length)return[r,[]];const o=e[e.length-1];if("string"!=typeof o&&(o.many||!(t.length>e.length))){for(let n=e.length;n<t.length;n++)if(!o.pattern.test(t[n]))return;return r[o.name]=t.slice(e.length-1),[r,[]]}}(n,e,t)}function m(e,t,n,r){const o={match:g(n||"",!0),methods:new Set(t),handler:r};e.push(o);let i=!1;return()=>{if(i)return;i=!0;const t=e.indexOf(o);t<0||e.splice(t,1)}}const w=e=>"function"==typeof e;function b(e,t,n){if(!n.length)return n=>m(e,t,"",n);const[r,o]=n;if(r&&"object"==typeof r){const o=String.raw(r,...n.slice(1));return n=>m(e,t,o,n)}const i="string"==typeof r?r:"",s=[r,o].find(w);return s?m(e,t,i,s):n=>m(e,t,i,n)}const O=new Set(["GET","POST","PUT","DELETE","HEAD","OPTIONS"]);function T(e){return O.has(e)}function E(e,t,n){const r=n instanceof h?n:"function"==typeof n?h.create(n):new S;return e.push({match:g(t,!1),router:r}),r}class S extends h{#e=[];route(...e){const[t]=e;if(t&&"object"==typeof t&&!(t instanceof h)){const n=String.raw(t,...e.slice(1));return e=>E(this.#e,n,e)}const n="string"==typeof t?t:"",r="string"==typeof t?e[1]:t;return E(this.#e,n,r)}*find(e,t,n){for(const n of Array.from(this.#e)){if(!n.router&&!n.methods.has(e))continue;const{match:r}=n;if(!r){!n.router&&t.length||(yield[n.router||n.handler,{},t]);continue}if(!t.length)continue;const o=r(t);o&&(yield[n.router||n.handler,...o])}}verb(e,t,n){const r=function(e){return e?"string"==typeof e?[e.toUpperCase()].filter(T):Array.from(e).map((e=>"string"==typeof e&&e.toUpperCase())).filter(T):["GET","POST","PUT","DELETE"]}(e);return r.length?b(this.#e,r,[t,n]):()=>{}}match(...e){return b(this.#e,["GET","POST","PUT","DELETE"],e)}get(...e){return b(this.#e,["GET"],e)}post(...e){return b(this.#e,["POST"],e)}put(...e){return b(this.#e,["PUT"],e)}delete(...e){return b(this.#e,["DELETE"],e)}head(...e){return b(this.#e,["HEAD"],e)}options(...e){return b(this.#e,["OPTIONS"],e)}}e.ApiRouter=S,e.Router=h,e.createFetch=function(e,t){return async function(n,r){const o=new Request(n,r),{signal:i}=o;i.throwIfAborted();const s=await e(o);return s||("function"==typeof t?t(o):new Response(null,{status:404}))}},e.main=a,e.make=function(e,t){return n=>a(n,e,t)},e.merge=function(...e){return t=>async function(e,t){for(const n of t){const t=await n(e);if("boolean"==typeof t)return t;if(t)return t}}(t,e.flat())},e.service=function(e,t,n){const r=function(n){return"function"==typeof t&&n.done((()=>t(n)),(e=>t(n,e))),(...t)=>e(n,...t)},{rootOnly:o}="object"==typeof t&&t||"object"==typeof n&&n||{};return Object.assign(r,{rootOnly:Boolean(o)}),r},e.stateService=function(e,t,n,r){const o=function(r){const o=e(r)||{};return"function"==typeof t&&r.done((()=>t(o,r)),(e=>t(o,r,e))),"function"!=typeof n?()=>o:()=>(n(o,r),o)},{rootOnly:i}="object"==typeof t&&t||"object"==typeof n&&n||"object"==typeof r&&r||{};return Object.assign(o,{rootOnly:Boolean(i)}),o},e.storeService=function(e,t,n){const r=function(n){let r;return"function"==typeof e&&n.done((()=>e(r,n)),(t=>e(r,n,t))),"function"!=typeof t?(...e)=>(e.length&&([r]=e),r):(...e)=>(e.length&&([r]=e,t(r,n)),r)},{rootOnly:o}="object"==typeof e&&e||"object"==typeof t&&t||"object"==typeof n&&n||{};return Object.assign(r,{rootOnly:Boolean(o)}),r}}));
6
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).k99={})}(this,(function(e){"use strict";function t(e){return(new TextEncoder).encode(e)}function n(e){if(e instanceof ArrayBuffer)return!0;try{if(e instanceof SharedArrayBuffer)return!0}catch{}return!1}function r(e){return Symbol.asyncIterator in e||Symbol.iterator in e}async function o(e,i){if("string"==typeof i)return e.write(t(i));if("object"==typeof i){if(ArrayBuffer.isView(i))return e.write(new Uint8Array(i.buffer,i.byteOffset,i.byteLength));if(n(i))return e.write(new Uint8Array(i));if(r(i))for await(const t of i)t&&await o(e,t)}}function i(e,t){return"bigint"==typeof t?String(t):t}function s(e,s,u){const c=function(e,s){if(e instanceof ReadableStream)return[e,0,""];if(e instanceof URLSearchParams)return[e,0,""];if(e instanceof Blob)return[e,e.size,e.type];if(e instanceof FormData)return[e,0,""];if(ArrayBuffer.isView(e)||n(e))return[e,e.byteLength,""];if("string"==typeof e){const n=t(e);return[n,n.byteLength,""]}if("object"!=typeof e)return null;if(Array.isArray(e)||!r(e)){const n=t(JSON.stringify(e,i));return[n,n.byteLength,"application/json"]}const{writable:u,readable:c}=new TransformStream,f=u.getWriter();return s?.catch((e=>{u.abort(e||new DOMException("The user aborted a request.")).catch((()=>{}))})),(async()=>{for await(const t of e)t&&await o(f,t);await u.close()})().catch((()=>{})),[c,0,""]}(e,u);if(!c)return null;const[f,a,l]=c;return l&&!s.get("Content-Type")&&s.set("Content-Type",l),a>0&&!s.get("Content-Length")&&s.set("Content-Length",String(a)),f}function u(e,t){e.delete("set-cookie");for(const{name:n,value:r,expire:o,domain:i,path:s,secure:u,httpOnly:c}of t)n&&e.append("set-cookie",[`${encodeURI(n)}=${encodeURI(r||"")}`,o&&`Expires=${o}`,i&&`Domain=${encodeURI(i)}`,s&&`Path=${encodeURI(s)}`,u&&"Secure",c&&"HttpOnly"].filter(Boolean).join("; "))}const c=new Set(["GET","OPTIONS"]);function f(e,t,n){n?e.set(t,n):e.delete(t)}function a(e,t,{runner:n,error:r,method:o,environment:i}={}){return function e(a,l){const p=function(e,t){let n="";return"string"==typeof t?n=t:"function"==typeof t&&(n=t(e)),n&&"string"==typeof n||(n=e.method||"GET"),n.toUpperCase()}(a,o),h=new URL(a.url),{signal:y,headers:d}=a,g=function(e){return new Promise(((t,n)=>{if(e.aborted)return n(e.reason);e.addEventListener("abort",(()=>n(e.reason)),{once:!0})}))}(y),m=new Map,w=function(e){let t={};for(const n of e.replace(/\s/g,"").split(";")){const e=n.split("=");t[decodeURIComponent(e.shift())]=decodeURIComponent(e.join("="))}return t}(d.get("cookie")||""),b=[],O=new Headers,T=l?.root;let E=200,S=!1,j=null,R=()=>{},U=e=>{};const P=new Promise(((e,t)=>{R=e,U=t}));P.catch((()=>{}));let v={};const A={environment:i,parent:l,get error(){return j},get root(){return T||this},signal:y,url:h,fetch(t,{method:n="get",signal:r,body:o,headers:i}={}){const u=new URL(t,h),f=new Headers(i||{});if(!o||c.has(n.toUpperCase()))return e(new Request(u,{method:n,headers:f,signal:r}),A);const a=s(o,f);return e(new Request(u,{method:n,headers:f,signal:r,body:a}),A)},done(e,t){if(S)return null;const n=P.then(e,t);return n.catch(r),n},service(e,...t){if(e.rootOnly&&T)return T.service(e,...t);let n=m.get(e);if(!n){if(n=e(A),"function"!=typeof n)return;m.set(e,n)}return n(...t)},method:p,get params(){return v},requestHeaders:d,requestType:d.get("content-type")||"",referer:d.get("referer")||"",userAgent:d.get("user-agent")||"",accept:(d.get("accept")||"").split(/,\s*/).filter(Boolean),acceptLanguage:(d.get("accept-language")||"").split(/,\s*/).filter(Boolean),cookies:w,request:a,get destroyed(){return S},get status(){return E},set status(e){E=e},responseHeaders:O,get location(){return O.get("location")||""},set location(e){f(O,"location",e)},get responseType(){return O.get("content-type")||""},set responseType(e){f(O,"content-type",e)},getCookie:e=>function*(e,t){const n=e;for(const e of n)t&&e.name!==t||(yield{...e})}(b,e),setCookie(e,t,{expire:n,domain:r,path:o,secure:i,httpOnly:s}={}){b.push({name:e,value:t,expire:n,domain:r,path:o,secure:i,httpOnly:s}),u(O,b)},clearCookie(e,t){!function(e,t,n,r){let o="Fri, 31 Dec 1999 16:00:00 GMT";if("string"==typeof n){if(!n)return;const{domain:t,path:i,secure:s,httpOnly:u}=!0!==r&&r||{};e.push({name:n,value:"delete",expire:o,domain:t,path:i,secure:s,httpOnly:u})}else{const{domain:i,path:s,secure:u,httpOnly:c}=n||{};if(e.length=0,r)for(let n in t)e.push({name:n,value:"delete",expire:o,domain:i,path:s,secure:u,httpOnly:c})}}(b,w,e,t),u(O,b)}};function k(){return Promise.race([g,Promise.resolve().then((()=>t(A,(e=>{v=e}))))]).then((e=>e?Promise.race([g,e(A)]).then((e=>{if(e instanceof Response)return e;const t=new Headers(A.responseHeaders),{status:n}=A;if(!e)return new Response(null,{status:n,headers:t});const r=s(e,t,g);return new Response(r,{status:n,headers:t})})):null)).then((e=>(S=!0,R(),e)),(e=>(S=!0,j=e||!0,U(j),Promise.reject(e))))}return n?n(A,k):k()}(e)}async function l(e,t,n,r,o){if(!(e instanceof h))return r(o),e;if(e.disabled)return null;const i=await async function(e,t,n,r){if(!e.size)return!0;n(r);for(const n of e){if(t.destroyed)return!1;const e=await n(Object.create(t,{params:{value:{...r}}}));if(!1===e)return!1;if("function"==typeof e)return e}return!0}(e.guards,n,r,o);if(!i)return null;if("function"==typeof i)return i;if(n.destroyed)return null;for await(const[i,s,u]of e.find(n.method,t,n)){if(n.destroyed)return null;const t=await l(i,u,n,r,{...o,...s});if(t)return e.__onionskin(t)}return null}function p(e){try{return decodeURIComponent(e)}catch{return e}}class h{disabled=!1;find(e,t,n){return[]}static make(e){return async(t,n)=>{const r=e.flat(),o=t.url.pathname.split("/").filter(Boolean).map(p);for(const e of r){const r=await l(e,o,t,n,{});if(r)return r}return null}}static create(e){return Object.create(h.prototype,{find:{configurable:!0,value:e,writable:!0}})}guards=new Set;__onionskin=e=>e;onionskin(e){let t=this.__onionskin;this.__onionskin=n=>{const r=t(n);return async t=>e(t,(async()=>r(t)))}}}const y=/^:([a-zA-Z][a-zA-Z0-9]*)(?:\((.+)\))?([ius]+)?([?+*]?)$/;function d(e){const t=y.exec(e);if(!t)return e;const[,n,r=".*",o,i]=t;if(!r)return{name:n,pattern:new RegExp("^.*$",o),optional:"?"===i||"*"===i,many:"+"===i||"*"===i};let s=0,u=0;const c=["^(?:"];for(;s<r.length;){const t=r[s++];if(c.push(t),"\\"!==t)if(")"!==t)if("["!==t){if("("===t&&(u++,"?"===r[s]&&(s+=2,":"!==r[s-1])))return e}else for(;s<r.length;){const e=r[s++];if(c.push(e),"]"===e)break;"\\"===e&&c.push(r[s++])}else{if(0===u)return e;u--}else c.push(r[s++])}return u?e:(c.push(")$"),{name:n,pattern:new RegExp(c.join(""),o),optional:"?"===i||"*"===i,many:"+"===i||"*"===i})}function g(e,t){const n=[];for(const t of e.split("/"))t&&!/^\.+$/.test(t)&&n.push(d(t));if(n.length)return e=>function(e,t,n){const r={};for(let n=0;n<e.length;n++){const o=e[n],i=t[n];if(o!==i){if("string"==typeof o)return;if(!i)return o.optional?[r,[]]:void 0;if(!o.pattern.test(i))return;r[o.name]=i}}if(!n)return[r,t.slice(e.length)];if(t.length<=e.length)return[r,[]];const o=e[e.length-1];if("string"!=typeof o&&(o.many||!(t.length>e.length))){for(let n=e.length;n<t.length;n++)if(!o.pattern.test(t[n]))return;return r[o.name]=t.slice(e.length-1),[r,[]]}}(n,e,t)}function m(e,t,n,r){const o={match:g(n||"",!0),methods:new Set(t),handler:r};e.push(o);let i=!1;return()=>{if(i)return;i=!0;const t=e.indexOf(o);t<0||e.splice(t,1)}}const w=e=>"function"==typeof e;function b(e,t,n){if(!n.length)return n=>m(e,t,"",n);const[r,o]=n;if(r&&"object"==typeof r){const o=String.raw(r,...n.slice(1));return n=>m(e,t,o,n)}const i="string"==typeof r?r:"",s=[r,o].find(w);return s?m(e,t,i,s):n=>m(e,t,i,n)}const O=new Set(["GET","POST","PUT","DELETE","HEAD","OPTIONS"]);function T(e){return O.has(e)}function E(e,t,n){const r=n instanceof h?n:"function"==typeof n?h.create(n):new S;return e.push({match:g(t,!1),router:r}),r}class S extends h{#e=[];route(...e){const[t]=e;if(t&&"object"==typeof t&&!(t instanceof h)){const n=String.raw(t,...e.slice(1));return e=>E(this.#e,n,e)}const n="string"==typeof t?t:"",r="string"==typeof t?e[1]:t;return E(this.#e,n,r)}*find(e,t,n){for(const n of Array.from(this.#e)){if(!n.router&&!n.methods.has(e))continue;const{match:r}=n;if(!r){!n.router&&t.length||(yield[n.router||n.handler,{},t]);continue}if(!t.length)continue;const o=r(t);o&&(yield[n.router||n.handler,...o])}}verb(e,t,n){const r=function(e){return e?"string"==typeof e?[e.toUpperCase()].filter(T):Array.from(e).map((e=>"string"==typeof e&&e.toUpperCase())).filter(T):["GET","POST","PUT","DELETE"]}(e);return r.length?b(this.#e,r,[t,n]):()=>{}}match(...e){return b(this.#e,["GET","POST","PUT","DELETE"],e)}get(...e){return b(this.#e,["GET"],e)}post(...e){return b(this.#e,["POST"],e)}put(...e){return b(this.#e,["PUT"],e)}delete(...e){return b(this.#e,["DELETE"],e)}head(...e){return b(this.#e,["HEAD"],e)}options(...e){return b(this.#e,["OPTIONS"],e)}}e.ApiRouter=S,e.Router=h,e.createFetch=function(e,t){return async function(n,r){const o=new Request(n,r),{signal:i}=o;i.throwIfAborted();const s=await e(o);return s||("function"==typeof t?t(o):new Response(null,{status:404}))}},e.main=a,e.make=function(e,t){return n=>a(n,e,t)},e.merge=function(...e){return t=>async function(e,t){for(const n of t){const t=await n(e);if("boolean"==typeof t)return t;if(t)return t}}(t,e.flat())},e.service=function(e,t,n){const r=function(n){return"function"==typeof t&&n.done((()=>t(n)),(e=>t(n,e))),(...t)=>e(n,...t)},{rootOnly:o}="object"==typeof t&&t||"object"==typeof n&&n||{};return Object.assign(r,{rootOnly:Boolean(o)}),r},e.stateService=function(e,t,n,r){const o=function(r){const o=e(r)||{};return"function"==typeof t&&r.done((()=>t(o,r)),(e=>t(o,r,e))),"function"!=typeof n?()=>o:()=>(n(o,r),o)},{rootOnly:i}="object"==typeof t&&t||"object"==typeof n&&n||"object"==typeof r&&r||{};return Object.assign(o,{rootOnly:Boolean(i)}),o},e.storeService=function(e,t,n){const r=function(n){let r;return"function"==typeof e&&n.done((()=>e(r,n)),(t=>e(r,n,t))),"function"!=typeof t?(...e)=>(e.length&&([r]=e),r):(...e)=>(e.length&&([r]=e,t(r,n)),r)},{rootOnly:o}="object"==typeof e&&e||"object"==typeof t&&t||"object"==typeof n&&n||{};return Object.assign(r,{rootOnly:Boolean(o)}),r}}));
package/index.min.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
6
- function e(e){return(new TextEncoder).encode(e)}function t(e){if(e instanceof ArrayBuffer)return!0;try{if(e instanceof SharedArrayBuffer)return!0}catch{}return!1}function n(e){return Symbol.asyncIterator in e||Symbol.iterator in e}async function r(o,s){if("string"==typeof s)return o.write(e(s));if("object"==typeof s){if(ArrayBuffer.isView(s))return o.write(new Uint8Array(s.buffer,s.byteOffset,s.byteLength));if(t(s))return o.write(new Uint8Array(s));if(n(s))for await(const e of s)e&&await r(o,e)}}function o(e,t){return"bigint"==typeof t?String(t):t}function s(s,i,u){const c=function(s,i){if(s instanceof ReadableStream)return[s,0,""];if(s instanceof URLSearchParams)return[s,0,""];if(s instanceof Blob)return[s,s.size,s.type];if(s instanceof FormData)return[s,0,""];if(ArrayBuffer.isView(s)||t(s))return[s,s.byteLength,""];if("string"==typeof s){const t=e(s);return[t,t.byteLength,""]}if("object"!=typeof s)return null;if(Array.isArray(s)||!n(s)){const t=e(JSON.stringify(s,o));return[t,t.byteLength,"application/json"]}const{writable:u,readable:c}=new TransformStream,f=u.getWriter();return i?.catch((e=>{u.abort(e||new DOMException("The user aborted a request.")).catch((()=>{}))})),(async()=>{for await(const e of s)e&&await r(f,e);await u.close()})().catch((()=>{})),[c,0,""]}(s,u);if(!c)return null;const[f,a,l]=c;return l&&!i.get("Content-Type")&&i.set("Content-Type",l),a>0&&!i.get("Content-Length")&&i.set("Content-Length",String(a)),f}function i(e,t){e.delete("set-cookie");for(const{name:n,value:r,expire:o,domain:s,path:i,secure:u,httpOnly:c}of t)n&&e.append("set-cookie",[`${encodeURI(n)}=${encodeURI(r||"")}`,o&&`Expires=${o}`,s&&`Domain=${encodeURI(s)}`,i&&`Path=${encodeURI(i)}`,u&&"Secure",c&&"HttpOnly"].filter(Boolean).join("; "))}const u=new Set(["GET","OPTIONS"]);function c(e,t,n){n?e.set(t,n):e.delete(t)}function f(e,t,{runner:n,error:r,method:o,environment:f}={}){return function e(a,l){const p=function(e,t){let n="";return"string"==typeof t?n=t:"function"==typeof t&&(n=t(e)),n&&"string"==typeof n||(n=e.method||"GET"),n.toUpperCase()}(a,o),h=new URL(a.url),{signal:y,headers:d}=a,g=function(e){return new Promise(((t,n)=>{if(e.aborted)return n(e.reason);e.addEventListener("abort",(()=>n(e.reason)),{once:!0})}))}(y),m=new Map,w=function(e){let t={};for(const n of e.replace(/\s/g,"").split(";")){const e=n.split("=");t[decodeURIComponent(e.shift())]=decodeURIComponent(e.join("="))}return t}(d.get("cookie")||""),b=[],O=new Headers,T=l?.root;let E=200,S=!1,j=null,R=()=>{},U=e=>{};const P=new Promise(((e,t)=>{R=e,U=t}));P.catch((()=>{}));let A={};const L={environment:f,parent:l,get error(){return j},get root(){return T||this},signal:y,url:h,fetch(t,{method:n="get",signal:r,body:o,headers:i}={}){const c=new URL(t,h),f=new Headers(i||{});if(!o||u.has(n.toUpperCase()))return e(new Request(c,{method:n,headers:f,signal:r}),L);const a=s(o,f);return e(new Request(c,{method:n,headers:f,signal:r,body:a}),L)},done(e,t){if(S)return null;const n=P.then(e,t);return n.catch(r),n},service(e,...t){if(e.rootOnly&&T)return T.service(e,...t);let n=m.get(e);if(!n){if(n=e(L),"function"!=typeof n)return;m.set(e,n)}return n(...t)},method:p,get params(){return A},requestHeaders:d,requestType:d.get("content-type")||"",referer:d.get("referer")||"",userAgent:d.get("user-agent")||"",accept:(d.get("accept")||"").split(/,\s*/).filter(Boolean),acceptLanguage:(d.get("accept-language")||"").split(/,\s*/).filter(Boolean),cookies:w,request:a,get destroyed(){return S},get status(){return E},set status(e){E=e},responseHeaders:O,get location(){return O.get("location")||""},set location(e){c(O,"location",e)},get responseType(){return O.get("content-type")||""},set responseType(e){c(O,"content-type",e)},getCookie:e=>function*(e,t){const n=e;for(const e of n)t&&e.name!==t||(yield{...e})}(b,e),setCookie(e,t,{expire:n,domain:r,path:o,secure:s,httpOnly:u}={}){b.push({name:e,value:t,expire:n,domain:r,path:o,secure:s,httpOnly:u}),i(O,b)},clearCookie(e,t){!function(e,t,n,r){let o="Fri, 31 Dec 1999 16:00:00 GMT";if("string"==typeof n){if(!n)return;const{domain:t,path:s,secure:i,httpOnly:u}=!0!==r&&r||{};e.push({name:n,value:"delete",expire:o,domain:t,path:s,secure:i,httpOnly:u})}else{const{domain:s,path:i,secure:u,httpOnly:c}=n||{};if(e.length=0,r)for(let n in t)e.push({name:n,value:"delete",expire:o,domain:s,path:i,secure:u,httpOnly:c})}}(b,w,e,t),i(O,b)}};function v(){return Promise.race([g,Promise.resolve().then((()=>t(L,(e=>{A=e}))))]).then((e=>e?Promise.race([g,e(L)]).then((e=>{if(e instanceof Response)return e;const t=new Headers(L.responseHeaders),{status:n}=L;if(!e)return new Response(null,{status:n,headers:t});const r=s(e,t,g);return new Response(r,{status:n,headers:t})})):null)).then((e=>(S=!0,R(),e)),(e=>(S=!0,j=e||!0,U(j),Promise.reject(e))))}return n?n(L,v):v()}(e)}function a(e,t){return n=>f(n,e,t)}function l(...e){return t=>async function(e,t){for(const n of t){const t=await n(e);if("boolean"==typeof t)return t;if(t)return t}}(t,e.flat())}function p(e,t,n){const r=function(n){return"function"==typeof t&&n.done((()=>t(n)),(e=>t(n,e))),(...t)=>e(n,...t)},{rootOnly:o}="object"==typeof t&&t||"object"==typeof n&&n||{};return Object.assign(r,{rootOnly:Boolean(o)}),r}function h(e,t,n,r){const o=function(r){const o=e(r)||{};return"function"==typeof t&&r.done((()=>t(o,r)),(e=>t(o,r,e))),"function"!=typeof n?()=>o:()=>(n(o,r),o)},{rootOnly:s}="object"==typeof t&&t||"object"==typeof n&&n||"object"==typeof r&&r||{};return Object.assign(o,{rootOnly:Boolean(s)}),o}function y(e,t,n){const r=function(n){let r;return"function"==typeof e&&n.done((()=>e(r,n)),(t=>e(r,n,t))),"function"!=typeof t?(...e)=>(e.length&&([r]=e),r):(...e)=>(e.length&&([r]=e,t(r,n)),r)},{rootOnly:o}="object"==typeof e&&e||"object"==typeof t&&t||"object"==typeof n&&n||{};return Object.assign(r,{rootOnly:Boolean(o)}),r}async function d(e,t,n,r,o){if(!(e instanceof m))return r(o),e;if(e.disabled)return null;const s=await async function(e,t,n,r){if(!e.size)return!0;n(r);for(const n of e){if(t.destroyed)return!1;const e=await n(Object.create(t,{params:{value:{...r}}}));if(!1===e)return!1;if("function"==typeof e)return e}return!0}(e.guards,n,r,o);if(!s)return null;if("function"==typeof s)return s;if(n.destroyed)return null;for await(const[s,i,u]of e.find(n.method,t,n)){if(n.destroyed)return null;const e=await d(s,u,n,r,{...o,...i});if(e)return e}return null}function g(e){try{return decodeURIComponent(e)}catch{return e}}class m{disabled=!1;find(e,t,n){return[]}static make(e){return async(t,n)=>{const r=e.flat(),o=t.url.pathname.split("/").filter(Boolean).map(g);for(const e of r){const r=await d(e,o,t,n,{});if(r)return r}return null}}static create(e){return Object.create(m.prototype,{find:{configurable:!0,value:e,writable:!0}})}guards=new Set}const w=/^:([a-zA-Z][a-zA-Z0-9]*)(?:\((.+)\))?([ius]+)?([?+*]?)$/;function b(e){const t=w.exec(e);if(!t)return e;const[,n,r=".*",o,s]=t;if(!r)return{name:n,pattern:new RegExp("^.*$",o),optional:"?"===s||"*"===s,many:"+"===s||"*"===s};let i=0,u=0;const c=["^(?:"];for(;i<r.length;){const t=r[i++];if(c.push(t),"\\"!==t)if(")"!==t)if("["!==t){if("("===t&&(u++,"?"===r[i]&&(i+=2,":"!==r[i-1])))return e}else for(;i<r.length;){const e=r[i++];if(c.push(e),"]"===e)break;"\\"===e&&c.push(r[i++])}else{if(0===u)return e;u--}else c.push(r[i++])}return u?e:(c.push(")$"),{name:n,pattern:new RegExp(c.join(""),o),optional:"?"===s||"*"===s,many:"+"===s||"*"===s})}function O(e,t){const n=[];for(const t of e.split("/"))t&&!/^\.+$/.test(t)&&n.push(b(t));if(n.length)return e=>function(e,t,n){const r={};for(let n=0;n<e.length;n++){const o=e[n],s=t[n];if(o!==s){if("string"==typeof o)return;if(!s)return o.optional?[r,[]]:void 0;if(!o.pattern.test(s))return;r[o.name]=s}}if(!n)return[r,t.slice(e.length)];if(t.length<=e.length)return[r,[]];const o=e[e.length-1];if("string"!=typeof o&&(o.many||!(t.length>e.length))){for(let n=e.length;n<t.length;n++)if(!o.pattern.test(t[n]))return;return r[o.name]=t.slice(e.length-1),[r,[]]}}(n,e,t)}function T(e,t,n,r){const o={match:O(n||"",!0),methods:new Set(t),handler:r};e.push(o);let s=!1;return()=>{if(s)return;s=!0;const t=e.indexOf(o);t<0||e.splice(t,1)}}const E=e=>"function"==typeof e;function S(e,t,n){if(!n.length)return n=>T(e,t,"",n);const[r,o]=n;if(r&&"object"==typeof r){const o=String.raw(r,...n.slice(1));return n=>T(e,t,o,n)}const s="string"==typeof r?r:"",i=[r,o].find(E);return i?T(e,t,s,i):n=>T(e,t,s,n)}const j=new Set(["GET","POST","PUT","DELETE","HEAD","OPTIONS"]);function R(e){return j.has(e)}function U(e,t,n){const r=n instanceof m?n:"function"==typeof n?m.create(n):new P;return e.push({match:O(t,!1),router:r}),r}class P extends m{#e=[];route(...e){const[t]=e;if(t&&"object"==typeof t&&!(t instanceof m)){const n=String.raw(t,...e.slice(1));return e=>U(this.#e,n,e)}const n="string"==typeof t?t:"",r="string"==typeof t?e[1]:t;return U(this.#e,n,r)}*find(e,t,n){for(const n of Array.from(this.#e)){if(!n.router&&!n.methods.has(e))continue;const{match:r}=n;if(!r){!n.router&&t.length||(yield[n.router||n.handler,{},t]);continue}if(!t.length)continue;const o=r(t);o&&(yield[n.router||n.handler,...o])}}verb(e,t,n){const r=function(e){return e?"string"==typeof e?[e.toUpperCase()].filter(R):Array.from(e).map((e=>"string"==typeof e&&e.toUpperCase())).filter(R):["GET","POST","PUT","DELETE"]}(e);return r.length?S(this.#e,r,[t,n]):()=>{}}match(...e){return S(this.#e,["GET","POST","PUT","DELETE"],e)}get(...e){return S(this.#e,["GET"],e)}post(...e){return S(this.#e,["POST"],e)}put(...e){return S(this.#e,["PUT"],e)}delete(...e){return S(this.#e,["DELETE"],e)}head(...e){return S(this.#e,["HEAD"],e)}options(...e){return S(this.#e,["OPTIONS"],e)}}function A(e,t){return async function(n,r){const o=new Request(n,r),{signal:s}=o;s.throwIfAborted();const i=await e(o);return i||("function"==typeof t?t(o):new Response(null,{status:404}))}}export{P as ApiRouter,m as Router,A as createFetch,f as main,a as make,l as merge,p as service,h as stateService,y as storeService};
6
+ function e(e){return(new TextEncoder).encode(e)}function t(e){if(e instanceof ArrayBuffer)return!0;try{if(e instanceof SharedArrayBuffer)return!0}catch{}return!1}function n(e){return Symbol.asyncIterator in e||Symbol.iterator in e}async function r(o,s){if("string"==typeof s)return o.write(e(s));if("object"==typeof s){if(ArrayBuffer.isView(s))return o.write(new Uint8Array(s.buffer,s.byteOffset,s.byteLength));if(t(s))return o.write(new Uint8Array(s));if(n(s))for await(const e of s)e&&await r(o,e)}}function o(e,t){return"bigint"==typeof t?String(t):t}function s(s,i,u){const c=function(s,i){if(s instanceof ReadableStream)return[s,0,""];if(s instanceof URLSearchParams)return[s,0,""];if(s instanceof Blob)return[s,s.size,s.type];if(s instanceof FormData)return[s,0,""];if(ArrayBuffer.isView(s)||t(s))return[s,s.byteLength,""];if("string"==typeof s){const t=e(s);return[t,t.byteLength,""]}if("object"!=typeof s)return null;if(Array.isArray(s)||!n(s)){const t=e(JSON.stringify(s,o));return[t,t.byteLength,"application/json"]}const{writable:u,readable:c}=new TransformStream,a=u.getWriter();return i?.catch((e=>{u.abort(e||new DOMException("The user aborted a request.")).catch((()=>{}))})),(async()=>{for await(const e of s)e&&await r(a,e);await u.close()})().catch((()=>{})),[c,0,""]}(s,u);if(!c)return null;const[a,f,l]=c;return l&&!i.get("Content-Type")&&i.set("Content-Type",l),f>0&&!i.get("Content-Length")&&i.set("Content-Length",String(f)),a}function i(e,t){e.delete("set-cookie");for(const{name:n,value:r,expire:o,domain:s,path:i,secure:u,httpOnly:c}of t)n&&e.append("set-cookie",[`${encodeURI(n)}=${encodeURI(r||"")}`,o&&`Expires=${o}`,s&&`Domain=${encodeURI(s)}`,i&&`Path=${encodeURI(i)}`,u&&"Secure",c&&"HttpOnly"].filter(Boolean).join("; "))}const u=new Set(["GET","OPTIONS"]);function c(e,t,n){n?e.set(t,n):e.delete(t)}function a(e,t,{runner:n,error:r,method:o,environment:a}={}){return function e(f,l){const p=function(e,t){let n="";return"string"==typeof t?n=t:"function"==typeof t&&(n=t(e)),n&&"string"==typeof n||(n=e.method||"GET"),n.toUpperCase()}(f,o),h=new URL(f.url),{signal:y,headers:d}=f,g=function(e){return new Promise(((t,n)=>{if(e.aborted)return n(e.reason);e.addEventListener("abort",(()=>n(e.reason)),{once:!0})}))}(y),m=new Map,w=function(e){let t={};for(const n of e.replace(/\s/g,"").split(";")){const e=n.split("=");t[decodeURIComponent(e.shift())]=decodeURIComponent(e.join("="))}return t}(d.get("cookie")||""),b=[],O=new Headers,T=l?.root;let E=200,S=!1,j=null,R=()=>{},U=e=>{};const P=new Promise(((e,t)=>{R=e,U=t}));P.catch((()=>{}));let A={};const L={environment:a,parent:l,get error(){return j},get root(){return T||this},signal:y,url:h,fetch(t,{method:n="get",signal:r,body:o,headers:i}={}){const c=new URL(t,h),a=new Headers(i||{});if(!o||u.has(n.toUpperCase()))return e(new Request(c,{method:n,headers:a,signal:r}),L);const f=s(o,a);return e(new Request(c,{method:n,headers:a,signal:r,body:f}),L)},done(e,t){if(S)return null;const n=P.then(e,t);return n.catch(r),n},service(e,...t){if(e.rootOnly&&T)return T.service(e,...t);let n=m.get(e);if(!n){if(n=e(L),"function"!=typeof n)return;m.set(e,n)}return n(...t)},method:p,get params(){return A},requestHeaders:d,requestType:d.get("content-type")||"",referer:d.get("referer")||"",userAgent:d.get("user-agent")||"",accept:(d.get("accept")||"").split(/,\s*/).filter(Boolean),acceptLanguage:(d.get("accept-language")||"").split(/,\s*/).filter(Boolean),cookies:w,request:f,get destroyed(){return S},get status(){return E},set status(e){E=e},responseHeaders:O,get location(){return O.get("location")||""},set location(e){c(O,"location",e)},get responseType(){return O.get("content-type")||""},set responseType(e){c(O,"content-type",e)},getCookie:e=>function*(e,t){const n=e;for(const e of n)t&&e.name!==t||(yield{...e})}(b,e),setCookie(e,t,{expire:n,domain:r,path:o,secure:s,httpOnly:u}={}){b.push({name:e,value:t,expire:n,domain:r,path:o,secure:s,httpOnly:u}),i(O,b)},clearCookie(e,t){!function(e,t,n,r){let o="Fri, 31 Dec 1999 16:00:00 GMT";if("string"==typeof n){if(!n)return;const{domain:t,path:s,secure:i,httpOnly:u}=!0!==r&&r||{};e.push({name:n,value:"delete",expire:o,domain:t,path:s,secure:i,httpOnly:u})}else{const{domain:s,path:i,secure:u,httpOnly:c}=n||{};if(e.length=0,r)for(let n in t)e.push({name:n,value:"delete",expire:o,domain:s,path:i,secure:u,httpOnly:c})}}(b,w,e,t),i(O,b)}};function k(){return Promise.race([g,Promise.resolve().then((()=>t(L,(e=>{A=e}))))]).then((e=>e?Promise.race([g,e(L)]).then((e=>{if(e instanceof Response)return e;const t=new Headers(L.responseHeaders),{status:n}=L;if(!e)return new Response(null,{status:n,headers:t});const r=s(e,t,g);return new Response(r,{status:n,headers:t})})):null)).then((e=>(S=!0,R(),e)),(e=>(S=!0,j=e||!0,U(j),Promise.reject(e))))}return n?n(L,k):k()}(e)}function f(e,t){return n=>a(n,e,t)}function l(...e){return t=>async function(e,t){for(const n of t){const t=await n(e);if("boolean"==typeof t)return t;if(t)return t}}(t,e.flat())}function p(e,t,n){const r=function(n){return"function"==typeof t&&n.done((()=>t(n)),(e=>t(n,e))),(...t)=>e(n,...t)},{rootOnly:o}="object"==typeof t&&t||"object"==typeof n&&n||{};return Object.assign(r,{rootOnly:Boolean(o)}),r}function h(e,t,n,r){const o=function(r){const o=e(r)||{};return"function"==typeof t&&r.done((()=>t(o,r)),(e=>t(o,r,e))),"function"!=typeof n?()=>o:()=>(n(o,r),o)},{rootOnly:s}="object"==typeof t&&t||"object"==typeof n&&n||"object"==typeof r&&r||{};return Object.assign(o,{rootOnly:Boolean(s)}),o}function y(e,t,n){const r=function(n){let r;return"function"==typeof e&&n.done((()=>e(r,n)),(t=>e(r,n,t))),"function"!=typeof t?(...e)=>(e.length&&([r]=e),r):(...e)=>(e.length&&([r]=e,t(r,n)),r)},{rootOnly:o}="object"==typeof e&&e||"object"==typeof t&&t||"object"==typeof n&&n||{};return Object.assign(r,{rootOnly:Boolean(o)}),r}async function d(e,t,n,r,o){if(!(e instanceof m))return r(o),e;if(e.disabled)return null;const s=await async function(e,t,n,r){if(!e.size)return!0;n(r);for(const n of e){if(t.destroyed)return!1;const e=await n(Object.create(t,{params:{value:{...r}}}));if(!1===e)return!1;if("function"==typeof e)return e}return!0}(e.guards,n,r,o);if(!s)return null;if("function"==typeof s)return s;if(n.destroyed)return null;for await(const[s,i,u]of e.find(n.method,t,n)){if(n.destroyed)return null;const t=await d(s,u,n,r,{...o,...i});if(t)return e.__onionskin(t)}return null}function g(e){try{return decodeURIComponent(e)}catch{return e}}class m{disabled=!1;find(e,t,n){return[]}static make(e){return async(t,n)=>{const r=e.flat(),o=t.url.pathname.split("/").filter(Boolean).map(g);for(const e of r){const r=await d(e,o,t,n,{});if(r)return r}return null}}static create(e){return Object.create(m.prototype,{find:{configurable:!0,value:e,writable:!0}})}guards=new Set;__onionskin=e=>e;onionskin(e){let t=this.__onionskin;this.__onionskin=n=>{const r=t(n);return async t=>e(t,(async()=>r(t)))}}}const w=/^:([a-zA-Z][a-zA-Z0-9]*)(?:\((.+)\))?([ius]+)?([?+*]?)$/;function b(e){const t=w.exec(e);if(!t)return e;const[,n,r=".*",o,s]=t;if(!r)return{name:n,pattern:new RegExp("^.*$",o),optional:"?"===s||"*"===s,many:"+"===s||"*"===s};let i=0,u=0;const c=["^(?:"];for(;i<r.length;){const t=r[i++];if(c.push(t),"\\"!==t)if(")"!==t)if("["!==t){if("("===t&&(u++,"?"===r[i]&&(i+=2,":"!==r[i-1])))return e}else for(;i<r.length;){const e=r[i++];if(c.push(e),"]"===e)break;"\\"===e&&c.push(r[i++])}else{if(0===u)return e;u--}else c.push(r[i++])}return u?e:(c.push(")$"),{name:n,pattern:new RegExp(c.join(""),o),optional:"?"===s||"*"===s,many:"+"===s||"*"===s})}function O(e,t){const n=[];for(const t of e.split("/"))t&&!/^\.+$/.test(t)&&n.push(b(t));if(n.length)return e=>function(e,t,n){const r={};for(let n=0;n<e.length;n++){const o=e[n],s=t[n];if(o!==s){if("string"==typeof o)return;if(!s)return o.optional?[r,[]]:void 0;if(!o.pattern.test(s))return;r[o.name]=s}}if(!n)return[r,t.slice(e.length)];if(t.length<=e.length)return[r,[]];const o=e[e.length-1];if("string"!=typeof o&&(o.many||!(t.length>e.length))){for(let n=e.length;n<t.length;n++)if(!o.pattern.test(t[n]))return;return r[o.name]=t.slice(e.length-1),[r,[]]}}(n,e,t)}function T(e,t,n,r){const o={match:O(n||"",!0),methods:new Set(t),handler:r};e.push(o);let s=!1;return()=>{if(s)return;s=!0;const t=e.indexOf(o);t<0||e.splice(t,1)}}const E=e=>"function"==typeof e;function S(e,t,n){if(!n.length)return n=>T(e,t,"",n);const[r,o]=n;if(r&&"object"==typeof r){const o=String.raw(r,...n.slice(1));return n=>T(e,t,o,n)}const s="string"==typeof r?r:"",i=[r,o].find(E);return i?T(e,t,s,i):n=>T(e,t,s,n)}const j=new Set(["GET","POST","PUT","DELETE","HEAD","OPTIONS"]);function R(e){return j.has(e)}function U(e,t,n){const r=n instanceof m?n:"function"==typeof n?m.create(n):new P;return e.push({match:O(t,!1),router:r}),r}class P extends m{#e=[];route(...e){const[t]=e;if(t&&"object"==typeof t&&!(t instanceof m)){const n=String.raw(t,...e.slice(1));return e=>U(this.#e,n,e)}const n="string"==typeof t?t:"",r="string"==typeof t?e[1]:t;return U(this.#e,n,r)}*find(e,t,n){for(const n of Array.from(this.#e)){if(!n.router&&!n.methods.has(e))continue;const{match:r}=n;if(!r){!n.router&&t.length||(yield[n.router||n.handler,{},t]);continue}if(!t.length)continue;const o=r(t);o&&(yield[n.router||n.handler,...o])}}verb(e,t,n){const r=function(e){return e?"string"==typeof e?[e.toUpperCase()].filter(R):Array.from(e).map((e=>"string"==typeof e&&e.toUpperCase())).filter(R):["GET","POST","PUT","DELETE"]}(e);return r.length?S(this.#e,r,[t,n]):()=>{}}match(...e){return S(this.#e,["GET","POST","PUT","DELETE"],e)}get(...e){return S(this.#e,["GET"],e)}post(...e){return S(this.#e,["POST"],e)}put(...e){return S(this.#e,["PUT"],e)}delete(...e){return S(this.#e,["DELETE"],e)}head(...e){return S(this.#e,["HEAD"],e)}options(...e){return S(this.#e,["OPTIONS"],e)}}function A(e,t){return async function(n,r){const o=new Request(n,r),{signal:s}=o;s.throwIfAborted();const i=await e(o);return i||("function"==typeof t?t(o):new Response(null,{status:404}))}}export{P as ApiRouter,m as Router,A as createFetch,a as main,f as make,l as merge,p as service,h as stateService,y as storeService};
package/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
@@ -634,14 +634,21 @@ function storeService(destroy, exec, options) {
634
634
  * @returns {PromiseLike<boolean | import('./main/types').Handler | void> | boolean | import('./main/types').Handler | void}
635
635
  */
636
636
 
637
+ /**
638
+ * @callback Onionskin
639
+ * @param {import('./main/types').Context} ctx
640
+ * @param {() => Promise<import('./main/types').HandlerResult>} next
641
+ * @returns {PromiseLike<import('./main/types').HandlerResult> | import('./main/types').HandlerResult}
642
+ */
643
+
637
644
  /**
638
645
  * @typedef {[import('./main/types').Handler | Router, Record<string, any>, string[]]} FindItem
639
646
  */
640
647
  /**
641
648
  * @callback Finder
642
649
  * @this {Router}
643
- * @property {import('./main/types').Method} method
644
- * @property {string[]} path
650
+ * @param {import('./main/types').Method} method
651
+ * @param {string[]} path
645
652
  * @param {import('./main/types').Context} ctx
646
653
  * @returns {AsyncIterable<FindItem> | Iterable<FindItem>}
647
654
  */
@@ -663,6 +670,7 @@ async function execGuard(guards, ctx, setParams, params) {
663
670
  params: { value: { ...params } },
664
671
  }));
665
672
  if (ret === false) { return false; }
673
+ // @ts-ignore
666
674
  if (typeof ret === 'function') { return ret; }
667
675
  }
668
676
  return true;
@@ -690,10 +698,11 @@ async function find(route, path, ctx, setParams, params) {
690
698
  for await (const [r, result, p] of route.find(ctx.method, path, ctx)) {
691
699
  if (ctx.destroyed) { return null; }
692
700
  const res = await find(r, p, ctx, setParams, { ...params, ...result });
693
- if (res) { return res; }
701
+ if (res) { return route.__onionskin(res); }
694
702
  }
695
703
  return null;
696
704
  }
705
+
697
706
  /**
698
707
  *
699
708
  * @param {string} t
@@ -718,7 +727,7 @@ class Router {
718
727
  * @param {import('./main/types').Context} ctx
719
728
  * @returns {AsyncIterable<FindItem> | Iterable<FindItem>}
720
729
  */
721
- find(method, path, ctx) { return [] }
730
+ find(method, path, ctx) { return []; }
722
731
  /**
723
732
  *
724
733
  * @param {Router[]} routers
@@ -748,6 +757,20 @@ class Router {
748
757
  }
749
758
  /** @readonly @type {Set<Guard>} */
750
759
  guards = new Set();
760
+ /**
761
+ *
762
+ * @param {import('./main/types').Handler} h
763
+ * @returns {import('./main/types').Handler}
764
+ */
765
+ __onionskin = (h) => h;
766
+ /** @param {Onionskin} os */
767
+ onionskin(os) {
768
+ let run = this.__onionskin;
769
+ this.__onionskin = h => {
770
+ const h2 = run(h);
771
+ return async (ctx) => os(ctx, async () => h2(ctx));
772
+ };
773
+ }
751
774
  }
752
775
 
753
776
  /**
package/node/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
package/node/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /*!
3
- * k99 v0.6.0-beta.2
3
+ * k99 v0.6.0-beta.4
4
4
  * (c) 2019-2024 猛火Fierflame
5
5
  * @license MIT
6
6
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "k99",
3
3
  "description": "前后端均可用的 web 服务器",
4
- "version": "0.6.0-beta.2",
4
+ "version": "0.6.0-beta.4",
5
5
  "dependencies": {},
6
6
  "keywords": [
7
7
  "k99",
@@ -13,13 +13,13 @@
13
13
  "jsdelivr": "./index.min.js",
14
14
  "author": "猛火Fierflame",
15
15
  "license": "MIT",
16
- "homepage": "https://gitee.com/fierflame/k99#git-readme",
16
+ "homepage": "https://gitee.com/neeloong/k99#git-readme",
17
17
  "repository": {
18
18
  "type": "git",
19
- "url": "https://gitee.com/fierflame/k99.git"
19
+ "url": "https://gitee.com/neeloong/k99.git"
20
20
  },
21
21
  "bugs": {
22
- "url": "https://gitee.com/fierflame/k99/issues"
22
+ "url": "https://gitee.com/neeloong/k99/issues"
23
23
  },
24
24
  "exports": {
25
25
  ".": {
package/services.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
package/services.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
package/services.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
package/services.min.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
package/services.min.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */
package/services.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * k99 v0.6.0-beta.2
2
+ * k99 v0.6.0-beta.4
3
3
  * (c) 2019-2024 猛火Fierflame
4
4
  * @license MIT
5
5
  */