k99 0.3.0-alpha.0 → 0.5.0-beta.0
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 +8 -0
- package/README.zh.md +6 -2
- package/browser/index.d.ts +26 -0
- package/{dist/k99Browser.browser.js → browser/index.js} +15 -10
- package/browser/index.min.js +6 -0
- package/{dist/k99Browser.esm.min.js → browser/index.min.mjs} +3 -3
- package/{dist/k99Browser.esm.js → browser/index.mjs} +13 -9
- package/browser/package.json +6 -7
- package/cli/index.d.ts +122 -0
- package/cli/index.mjs +202 -0
- package/cli/package.json +4 -0
- package/cli.mjs +12 -0
- package/{types.d.ts → index.d.ts} +63 -171
- package/{dist/k99.browser.js → index.js} +737 -786
- package/index.min.js +6 -0
- package/index.min.mjs +6 -0
- package/{dist/k99.esm.js → index.mjs} +735 -781
- package/node/{types.d.ts → index.d.ts} +2 -3
- package/node/index.mjs +143 -371
- package/node/package.json +3 -4
- package/package.json +18 -74
- package/services/index.d.ts +16 -0
- package/services/index.js +139 -0
- package/services/index.min.js +6 -0
- package/services/index.min.mjs +6 -0
- package/services/index.mjs +128 -0
- package/services/package.json +6 -7
- package/starter.mjs +13 -0
- package/browser/types.d.ts +0 -20
- package/cli/command/create.js +0 -79
- package/cli/command/index.js +0 -20
- package/cli/command/init.js +0 -85
- package/cli/command/start.js +0 -48
- package/cli/index.js +0 -14
- package/cli/opt/bind.js +0 -16
- package/cli/opt/index.js +0 -31
- package/cli/opt/listen.js +0 -16
- package/cli/opt/path.js +0 -16
- package/cli/opt/port.js +0 -24
- package/cli/opt/show-router.js +0 -16
- package/dist/k99.browser.min.js +0 -6
- package/dist/k99.esm.min.js +0 -6
- package/dist/k99.js +0 -2939
- package/dist/k99.mjs +0 -2916
- package/dist/k99Browser.browser.min.js +0 -6
- package/dist/k99Browser.js +0 -484
- package/dist/k99Browser.mjs +0 -475
- package/dist/k99Services.browser.js +0 -145
- package/dist/k99Services.browser.min.js +0 -6
- package/dist/k99Services.esm.js +0 -136
- package/dist/k99Services.esm.min.js +0 -6
- package/dist/k99Services.js +0 -141
- package/dist/k99Services.mjs +0 -136
- package/node/index.js +0 -1386
- package/services/types.d.ts +0 -22
- package/starter.js +0 -19
package/README.md
ADDED
package/README.zh.md
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
K99:
|
|
1
|
+
K99: 前后端均可用的 web 服务器
|
|
2
2
|
========
|
|
3
3
|
|
|
4
|
-
K99
|
|
4
|
+
K99 是可同时用于前端和后端的服务器中间件,是服务端的抽象层,k99 自身不依赖任何 node API 或浏览器 API。
|
|
5
|
+
|
|
6
|
+
通过 k99/browser 可以模拟出一个与原生 fetch API 完全相同的接口
|
|
7
|
+
|
|
8
|
+
通过 k99/node 可以将 k99 用于 node 端的 http/https/http2 等模块,也可以用于 express 等第三方模块。
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* k99 v0.5.0-beta.0
|
|
3
|
+
* (c) 2019-2022 猛火 Fierflame
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
import { Assets, Log, Settings, K99Request, K99Response, App } from 'k99';
|
|
7
|
+
|
|
8
|
+
declare function createFsAssetsApi(name: string): Assets.Api;
|
|
9
|
+
|
|
10
|
+
declare function createCacheLogApi(name: string): Log.Api;
|
|
11
|
+
|
|
12
|
+
declare function createCacheSettingsApi(name: string): Settings.Api;
|
|
13
|
+
|
|
14
|
+
declare function createRead(request: Request): {
|
|
15
|
+
(size?: number | undefined, encoding?: false | undefined): Promise<ArrayBuffer | null>;
|
|
16
|
+
(size: number, encoding: true): Promise<string | null>;
|
|
17
|
+
(size?: number | undefined, encoding?: boolean | undefined): Promise<string | ArrayBuffer | null>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
declare function createRequest(request: Request, searchParser?: (search: string) => Record<string, string | string[]>): K99Request;
|
|
21
|
+
|
|
22
|
+
declare function createResponse(target: K99Response): Response;
|
|
23
|
+
|
|
24
|
+
declare function createFetch(app: App, notFound?: null | ((request: Request) => Response | Promise<Response>), searchParser?: (search: string) => Record<string, string | string[]>): (input: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
25
|
+
|
|
26
|
+
export { createFsAssetsApi as createCacheAssetsApi, createCacheLogApi, createCacheSettingsApi, createFetch, createRead, createRequest, createResponse };
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* k99 v0.
|
|
3
|
-
* (c) 2019-
|
|
2
|
+
* k99 v0.5.0-beta.0
|
|
3
|
+
* (c) 2019-2022 猛火 Fierflame
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
6
|
(function (global, factory) {
|
|
7
7
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
8
8
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
9
9
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.k99Browser = {}));
|
|
10
|
-
}(this, (function (exports) { 'use strict';
|
|
10
|
+
})(this, (function (exports) { 'use strict';
|
|
11
11
|
|
|
12
12
|
function createFsAssetsApi(name) {
|
|
13
13
|
const cachePromise = caches.open(name);
|
|
@@ -355,7 +355,7 @@
|
|
|
355
355
|
if (index in query) {
|
|
356
356
|
query[index] = [query[index], value].flat();
|
|
357
357
|
} else {
|
|
358
|
-
query[index] =
|
|
358
|
+
query[index] = value;
|
|
359
359
|
}
|
|
360
360
|
}
|
|
361
361
|
|
|
@@ -372,7 +372,7 @@
|
|
|
372
372
|
return headers;
|
|
373
373
|
}
|
|
374
374
|
|
|
375
|
-
function createRequest(request) {
|
|
375
|
+
function createRequest(request, searchParser = parseQuery) {
|
|
376
376
|
const url = new URL(request.url);
|
|
377
377
|
return {
|
|
378
378
|
method: (request.method || 'GET').toUpperCase(),
|
|
@@ -380,7 +380,7 @@
|
|
|
380
380
|
headers: getHeaders(request.headers),
|
|
381
381
|
pathname: url.pathname || '/',
|
|
382
382
|
search: url.search || '',
|
|
383
|
-
query:
|
|
383
|
+
query: searchParser(url.search.substring(1)),
|
|
384
384
|
read: createRead(request)
|
|
385
385
|
};
|
|
386
386
|
}
|
|
@@ -442,6 +442,10 @@
|
|
|
442
442
|
} else {
|
|
443
443
|
controller.enqueue(toUint8Array(value));
|
|
444
444
|
}
|
|
445
|
+
},
|
|
446
|
+
|
|
447
|
+
async cancel(reason) {
|
|
448
|
+
iterator.return(reason);
|
|
445
449
|
}
|
|
446
450
|
|
|
447
451
|
}), {
|
|
@@ -450,11 +454,11 @@
|
|
|
450
454
|
});
|
|
451
455
|
}
|
|
452
456
|
|
|
453
|
-
function createFetch(app, notFound) {
|
|
457
|
+
function createFetch(app, notFound, searchParser) {
|
|
454
458
|
if (notFound) {
|
|
455
459
|
return async function fetch(input, init) {
|
|
456
460
|
const request = new Request(input, init);
|
|
457
|
-
const r = await app.request(createRequest(request));
|
|
461
|
+
const r = await app.request(createRequest(request, searchParser));
|
|
458
462
|
|
|
459
463
|
if (r) {
|
|
460
464
|
return createResponse(r);
|
|
@@ -466,7 +470,7 @@
|
|
|
466
470
|
|
|
467
471
|
return async function fetch(input, init) {
|
|
468
472
|
const request = new Request(input, init);
|
|
469
|
-
const r = await app.request(createRequest(request));
|
|
473
|
+
const r = await app.request(createRequest(request, searchParser));
|
|
470
474
|
|
|
471
475
|
if (r) {
|
|
472
476
|
return createResponse(r);
|
|
@@ -482,9 +486,10 @@
|
|
|
482
486
|
exports.createCacheLogApi = createCacheLogApi;
|
|
483
487
|
exports.createCacheSettingsApi = createCacheSettingsApi;
|
|
484
488
|
exports.createFetch = createFetch;
|
|
489
|
+
exports.createRead = createRead;
|
|
485
490
|
exports.createRequest = createRequest;
|
|
486
491
|
exports.createResponse = createResponse;
|
|
487
492
|
|
|
488
493
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
489
494
|
|
|
490
|
-
}))
|
|
495
|
+
}));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* k99 v0.5.0-beta.0
|
|
3
|
+
* (c) 2019-2022 猛火 Fierflame
|
|
4
|
+
* @license MIT
|
|
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).k99Browser={})}(this,(function(e){"use strict";function t(e){const t=new Uint8Array(e);let n=[],r=0;for(;r<t.length;){let e=0|t[r++],a=0;for(e>=240?a=3:e>=224?a=2:e>=192&&(a=1),a&&(e&=(1<<6-a)-1);a--;)e=e<<6|63&t[r++];n.push(e)}return n.map((e=>String.fromCharCode(e))).join("")}function n(e){let n,r=!1,a=0,o=null,s=!1;const u=[];let c=0;const i=[];function f(){if(!o)return;const e=c?function(){if(!a||c<=a){const e=new Uint8Array(c);let t=0;for(const n of u)for(let r=0;r<n.byteLength;r++,t++)e[t]=n[r];return c=0,u.length=0,e}let e=u.shift();if(!e)return null;const t=new Uint8Array(a);let n=0,r=0,o=e.length;for(;n<a;){if(r>=length){if(e=u.shift(),!e)break;r=0,o=e.length}t[n]=e[r],r++,n++}return e&&r<o&&u.unshift(new Uint8Array(e.buffer.slice(r))),c-=a,t}():null;o(e&&r?t(e):e),[a,r,o]=i.shift()||[0,!1,null]}return function(t=0,l=!1){return new Promise((h=>{n||(n=e.getReader()),t=Math.max(t,0),o?i.push([t,l,h]):(r=l,a=t,o=h,async function(){for(;o;){if(s){f();continue}if(c&&(!a||c>=a)){f();continue}const{value:t,done:r}=await n.read();r?s=!0:(c+=(e=t).length||0,u.push(e),c&&(!a||c>=a)&&f())}var e}())}))}}function r(e){const{body:r}=e;return r?n(r):function(e){let n;return function(r=0,a=!1){return n||(n=e.arrayBuffer().then((e=>[e,e,0]))),n=n.then((e=>{if(!e)return null;let[,t,n]=e;if(n>=t.byteLength)return null;if(!r||r+n>=t.byteLength)return[t.slice(n),t,t.byteLength];let a=n+r;return[t.slice(n,a),t,a]})),n.then((e=>{if(!e)return null;const[n]=e;return a?t(n):n}))}}(e)}function a(e){const t=e.indexOf("=");return t<0?[decodeURIComponent(e),""]:[decodeURIComponent(e.substring(0,t)),decodeURIComponent(e.substring(t+1))]}function o(e){const t={};for(const n of e.split("&").filter(Boolean)){const[e,r]=a(n);t[e]=e in t?[t[e],r].flat():r}return t}function s(e){const t={};for(const[n,r]of e.entries())t[n.toLowerCase()]=r;return t}function u(e,t=o){const n=new URL(e.url);return{method:(e.method||"GET").toUpperCase(),url:`${n.pathname}${n.search}`||"/",headers:s(e.headers),pathname:n.pathname||"/",search:n.search||"",query:t(n.search.substring(1)),read:r(e)}}function c(e){return"string"==typeof e?function(e){let t=[];for(let n of e.split("").map((e=>e.charCodeAt(0))))n<128?t.push(n):n<2048?(t.push(192|31&n>>6),t.push(128|63&n)):(t.push(224|15&n>>12),t.push(128|63&n>>6),t.push(128|63&n));return new Uint8Array(t)}(e):ArrayBuffer.isView(e)?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):new Uint8Array(e)}function i(e){const t=new Headers;for(const[n,r]of Object.entries(e.headers))for(const e of[r].flat())t.append(n.toLowerCase(),String(e));return t}function f(e){const t=e[Symbol.asyncIterator]();return new Response(new ReadableStream({async pull(e){const{value:n,done:r}=await t.next();r?e.close():e.enqueue(c(n))},async cancel(e){t.return(e)}}),{status:e.statusCode,headers:i(e)})}e.createCacheAssetsApi=function(e){const t=caches.open(e);return{async read(e,n){e=`/${e}`;const r=await t,a=await r.match(e);return a?n?a.text():a.arrayBuffer():null},async write(e,n){e=`/${e}`;const r=await t;return await r.put(e,new Response(n)),!0},async delete(e){e=`/${e}`;return(await t).delete(e)},stat:()=>null}},e.createCacheLogApi=function(e){const t=caches.open(e);return{async read(e){e=`/${e}`;const n=await t,r=await n.match(e);return r?r.text():""},async write(e,n){e=`/${e}`;const r=await t,a=await r.match(e),o=a?`${await a.text()}${n}`:n;return await r.put(e,new Response(o)),!0},async clear(e){e=`/${e}`;const n=await t;await n.delete(e)}}},e.createCacheSettingsApi=function(e){const t=caches.open(e);return{async read(e){e=`/${e}`;const n=await t,r=await n.match(e);if(r)return r.json()},async write(e,n){e=`/${e}`;const r=await t;return void 0===n?r.delete(e):(await r.put(e,new Response(JSON.stringify(n))),!0)}}},e.createFetch=function(e,t,n){return t?async function(r,a){const o=new Request(r,a),s=await e.request(u(o,n));return s?f(s):t(o)}:async function(t,r){const a=new Request(t,r),o=await e.request(u(a,n));return o?f(o):new Response(null,{status:404})}},e.createRead=r,e.createRequest=u,e.createResponse=f,Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* k99 v0.
|
|
3
|
-
* (c) 2019-
|
|
2
|
+
* k99 v0.5.0-beta.0
|
|
3
|
+
* (c) 2019-2022 猛火 Fierflame
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
|
-
function t(t){const n=caches.open(t);return{async read(t,e){t=`/${t}`;const r=await n,a=await r.match(t);return a?e?a.text():a.arrayBuffer():null},async write(t,e){t=`/${t}`;const r=await n;return await r.put(t,new Response(e)),!0},async delete(t){t=`/${t}`;return(await n).delete(t)},stat:()=>null}}function n(t){const n=caches.open(t);return{async read(t){t=`/${t}`;const e=await n,r=await e.match(t);return r?r.text():""},async write(t,e){t=`/${t}`;const r=await n,a=await r.match(t),s=a?`${await a.text()}${e}`:e;return await r.put(t,new Response(s)),!0},async clear(t){t=`/${t}`;const e=await n;await e.delete(t)}}}function e(t){const n=caches.open(t);return{async read(t){t=`/${t}`;const e=await n,r=await e.match(t);if(r)return r.json()},async write(t,e){t=`/${t}`;const r=await n;return void 0===e?r.delete(t):(await r.put(t,new Response(JSON.stringify(e))),!0)}}}function r(t){const n=new Uint8Array(t);let e=[],r=0;for(;r<n.length;){let t=0|n[r++],a=0;for(t>=240?a=3:t>=224?a=2:t>=192&&(a=1),a&&(t&=(1<<6-a)-1);a--;)t=t<<6|63&n[r++];e.push(t)}return e.map((t=>String.fromCharCode(t))).join("")}function a(t){let n,e=!1,a=0,s=null,o=!1;const u=[];let c=0;const i=[];function f(){if(!s)return;const t=c?function(){if(!a||c<=a){const t=new Uint8Array(c);let n=0;for(const e of u)for(let r=0;r<e.byteLength;r++,n++)t[n]=e[r];return c=0,u.length=0,t}let t=u.shift();if(!t)return null;const n=new Uint8Array(a);let e=0,r=0,s=t.length;for(;e<a;){if(r>=length){if(t=u.shift(),!t)break;r=0,s=t.length}n[e]=t[r],r++,e++}return t&&r<s&&u.unshift(new Uint8Array(t.buffer.slice(r))),c-=a,n}():null;s(t&&e?r(t):t),[a,e,s]=i.shift()||[0,!1,null]}return function(r=0,l=!1){return new Promise((h=>{n||(n=t.getReader()),r=Math.max(r,0),s?i.push([r,l,h]):(e=l,a=r,s=h,async function(){for(;s;){if(o){f();continue}if(c&&(!a||c>=a)){f();continue}const{value:e,done:r}=await n.read();r?o=!0:(c+=(t=e).length||0,u.push(t),c&&(!a||c>=a)&&f())}var t}())}))}}function s(t){const{body:n}=t;return n?a(n):function(t){let n;return function(e=0,a=!1){return n||(n=t.arrayBuffer().then((t=>[t,t,0]))),n=n.then((t=>{if(!t)return null;let[,n,r]=t;if(r>=n.byteLength)return null;if(!e||e+r>=n.byteLength)return[n.slice(r),n,n.byteLength];let a=r+e;return[n.slice(r,a),n,a]})),n.then((t=>{if(!t)return null;const[n]=t;return a?r(n):n}))}}(t)}function o(t){const n=t.indexOf("=");return n<0?[decodeURIComponent(t),""]:[decodeURIComponent(t.substring(0,n)),decodeURIComponent(t.substring(n+1))]}function u(t){const n={};for(const e of t.split("&").filter(Boolean)){const[t,r]=o(e);n[t]=t in n?[n[t],r].flat():
|
|
6
|
+
function t(t){const n=caches.open(t);return{async read(t,e){t=`/${t}`;const r=await n,a=await r.match(t);return a?e?a.text():a.arrayBuffer():null},async write(t,e){t=`/${t}`;const r=await n;return await r.put(t,new Response(e)),!0},async delete(t){t=`/${t}`;return(await n).delete(t)},stat:()=>null}}function n(t){const n=caches.open(t);return{async read(t){t=`/${t}`;const e=await n,r=await e.match(t);return r?r.text():""},async write(t,e){t=`/${t}`;const r=await n,a=await r.match(t),s=a?`${await a.text()}${e}`:e;return await r.put(t,new Response(s)),!0},async clear(t){t=`/${t}`;const e=await n;await e.delete(t)}}}function e(t){const n=caches.open(t);return{async read(t){t=`/${t}`;const e=await n,r=await e.match(t);if(r)return r.json()},async write(t,e){t=`/${t}`;const r=await n;return void 0===e?r.delete(t):(await r.put(t,new Response(JSON.stringify(e))),!0)}}}function r(t){const n=new Uint8Array(t);let e=[],r=0;for(;r<n.length;){let t=0|n[r++],a=0;for(t>=240?a=3:t>=224?a=2:t>=192&&(a=1),a&&(t&=(1<<6-a)-1);a--;)t=t<<6|63&n[r++];e.push(t)}return e.map((t=>String.fromCharCode(t))).join("")}function a(t){let n,e=!1,a=0,s=null,o=!1;const u=[];let c=0;const i=[];function f(){if(!s)return;const t=c?function(){if(!a||c<=a){const t=new Uint8Array(c);let n=0;for(const e of u)for(let r=0;r<e.byteLength;r++,n++)t[n]=e[r];return c=0,u.length=0,t}let t=u.shift();if(!t)return null;const n=new Uint8Array(a);let e=0,r=0,s=t.length;for(;e<a;){if(r>=length){if(t=u.shift(),!t)break;r=0,s=t.length}n[e]=t[r],r++,e++}return t&&r<s&&u.unshift(new Uint8Array(t.buffer.slice(r))),c-=a,n}():null;s(t&&e?r(t):t),[a,e,s]=i.shift()||[0,!1,null]}return function(r=0,l=!1){return new Promise((h=>{n||(n=t.getReader()),r=Math.max(r,0),s?i.push([r,l,h]):(e=l,a=r,s=h,async function(){for(;s;){if(o){f();continue}if(c&&(!a||c>=a)){f();continue}const{value:e,done:r}=await n.read();r?o=!0:(c+=(t=e).length||0,u.push(t),c&&(!a||c>=a)&&f())}var t}())}))}}function s(t){const{body:n}=t;return n?a(n):function(t){let n;return function(e=0,a=!1){return n||(n=t.arrayBuffer().then((t=>[t,t,0]))),n=n.then((t=>{if(!t)return null;let[,n,r]=t;if(r>=n.byteLength)return null;if(!e||e+r>=n.byteLength)return[n.slice(r),n,n.byteLength];let a=r+e;return[n.slice(r,a),n,a]})),n.then((t=>{if(!t)return null;const[n]=t;return a?r(n):n}))}}(t)}function o(t){const n=t.indexOf("=");return n<0?[decodeURIComponent(t),""]:[decodeURIComponent(t.substring(0,n)),decodeURIComponent(t.substring(n+1))]}function u(t){const n={};for(const e of t.split("&").filter(Boolean)){const[t,r]=o(e);n[t]=t in n?[n[t],r].flat():r}return n}function c(t){const n={};for(const[e,r]of t.entries())n[e.toLowerCase()]=r;return n}function i(t,n=u){const e=new URL(t.url);return{method:(t.method||"GET").toUpperCase(),url:`${e.pathname}${e.search}`||"/",headers:c(t.headers),pathname:e.pathname||"/",search:e.search||"",query:n(e.search.substring(1)),read:s(t)}}function f(t){return"string"==typeof t?function(t){let n=[];for(let e of t.split("").map((t=>t.charCodeAt(0))))e<128?n.push(e):e<2048?(n.push(192|31&e>>6),n.push(128|63&e)):(n.push(224|15&e>>12),n.push(128|63&e>>6),n.push(128|63&e));return new Uint8Array(n)}(t):ArrayBuffer.isView(t)?new Uint8Array(t.buffer,t.byteOffset,t.byteLength):new Uint8Array(t)}function l(t){const n=new Headers;for(const[e,r]of Object.entries(t.headers))for(const t of[r].flat())n.append(e.toLowerCase(),String(t));return n}function h(t){const n=t[Symbol.asyncIterator]();return new Response(new ReadableStream({async pull(t){const{value:e,done:r}=await n.next();r?t.close():t.enqueue(f(e))},async cancel(t){n.return(t)}}),{status:t.statusCode,headers:l(t)})}function w(t,n,e){return n?async function(r,a){const s=new Request(r,a),o=await t.request(i(s,e));return o?h(o):n(s)}:async function(n,r){const a=new Request(n,r),s=await t.request(i(a,e));return s?h(s):new Response(null,{status:404})}}export{t as createCacheAssetsApi,n as createCacheLogApi,e as createCacheSettingsApi,w as createFetch,s as createRead,i as createRequest,h as createResponse};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* k99 v0.
|
|
3
|
-
* (c) 2019-
|
|
2
|
+
* k99 v0.5.0-beta.0
|
|
3
|
+
* (c) 2019-2022 猛火 Fierflame
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
6
|
function createFsAssetsApi(name) {
|
|
@@ -349,7 +349,7 @@ function parseQuery(s) {
|
|
|
349
349
|
if (index in query) {
|
|
350
350
|
query[index] = [query[index], value].flat();
|
|
351
351
|
} else {
|
|
352
|
-
query[index] =
|
|
352
|
+
query[index] = value;
|
|
353
353
|
}
|
|
354
354
|
}
|
|
355
355
|
|
|
@@ -366,7 +366,7 @@ function getHeaders(h) {
|
|
|
366
366
|
return headers;
|
|
367
367
|
}
|
|
368
368
|
|
|
369
|
-
function createRequest(request) {
|
|
369
|
+
function createRequest(request, searchParser = parseQuery) {
|
|
370
370
|
const url = new URL(request.url);
|
|
371
371
|
return {
|
|
372
372
|
method: (request.method || 'GET').toUpperCase(),
|
|
@@ -374,7 +374,7 @@ function createRequest(request) {
|
|
|
374
374
|
headers: getHeaders(request.headers),
|
|
375
375
|
pathname: url.pathname || '/',
|
|
376
376
|
search: url.search || '',
|
|
377
|
-
query:
|
|
377
|
+
query: searchParser(url.search.substring(1)),
|
|
378
378
|
read: createRead(request)
|
|
379
379
|
};
|
|
380
380
|
}
|
|
@@ -436,6 +436,10 @@ function createResponse(target) {
|
|
|
436
436
|
} else {
|
|
437
437
|
controller.enqueue(toUint8Array(value));
|
|
438
438
|
}
|
|
439
|
+
},
|
|
440
|
+
|
|
441
|
+
async cancel(reason) {
|
|
442
|
+
iterator.return(reason);
|
|
439
443
|
}
|
|
440
444
|
|
|
441
445
|
}), {
|
|
@@ -444,11 +448,11 @@ function createResponse(target) {
|
|
|
444
448
|
});
|
|
445
449
|
}
|
|
446
450
|
|
|
447
|
-
function createFetch(app, notFound) {
|
|
451
|
+
function createFetch(app, notFound, searchParser) {
|
|
448
452
|
if (notFound) {
|
|
449
453
|
return async function fetch(input, init) {
|
|
450
454
|
const request = new Request(input, init);
|
|
451
|
-
const r = await app.request(createRequest(request));
|
|
455
|
+
const r = await app.request(createRequest(request, searchParser));
|
|
452
456
|
|
|
453
457
|
if (r) {
|
|
454
458
|
return createResponse(r);
|
|
@@ -460,7 +464,7 @@ function createFetch(app, notFound) {
|
|
|
460
464
|
|
|
461
465
|
return async function fetch(input, init) {
|
|
462
466
|
const request = new Request(input, init);
|
|
463
|
-
const r = await app.request(createRequest(request));
|
|
467
|
+
const r = await app.request(createRequest(request, searchParser));
|
|
464
468
|
|
|
465
469
|
if (r) {
|
|
466
470
|
return createResponse(r);
|
|
@@ -472,4 +476,4 @@ function createFetch(app, notFound) {
|
|
|
472
476
|
};
|
|
473
477
|
}
|
|
474
478
|
|
|
475
|
-
export { createFsAssetsApi as createCacheAssetsApi, createCacheLogApi, createCacheSettingsApi, createFetch, createRequest, createResponse };
|
|
479
|
+
export { createFsAssetsApi as createCacheAssetsApi, createCacheLogApi, createCacheSettingsApi, createFetch, createRead, createRequest, createResponse };
|
package/browser/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"main": "
|
|
3
|
-
"
|
|
4
|
-
"browser": "
|
|
5
|
-
"unpkg": "
|
|
6
|
-
"jsdelivr": "
|
|
7
|
-
|
|
8
|
-
}
|
|
2
|
+
"main": "./index.mjs",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"browser": "./index.min.js",
|
|
5
|
+
"unpkg": "./index.min.js",
|
|
6
|
+
"jsdelivr": "./index.min.js"
|
|
7
|
+
}
|
package/cli/index.d.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* k99 v0.5.0-beta.0
|
|
3
|
+
* (c) 2019-2022 猛火 Fierflame
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
import { OptType } from 'entry-cli';
|
|
7
|
+
|
|
8
|
+
declare function value$4(...opts: string[]): string;
|
|
9
|
+
declare const name$4 = "socket";
|
|
10
|
+
declare const explain$6 = "\u76D1\u542C\u7684\u672C\u5730\u5957\u63A5\u5B57";
|
|
11
|
+
|
|
12
|
+
declare namespace listen {
|
|
13
|
+
export {
|
|
14
|
+
value$4 as value,
|
|
15
|
+
name$4 as name,
|
|
16
|
+
explain$6 as explain,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare function value$3(...opts: string[]): string;
|
|
21
|
+
declare const name$3 = "path";
|
|
22
|
+
declare const explain$5 = "\u73AF\u5883\u8DEF\u5F84";
|
|
23
|
+
|
|
24
|
+
declare namespace path {
|
|
25
|
+
export {
|
|
26
|
+
value$3 as value,
|
|
27
|
+
name$3 as name,
|
|
28
|
+
explain$5 as explain,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare function value$2(...opts: string[]): boolean;
|
|
33
|
+
declare const name$2 = "";
|
|
34
|
+
declare const explain$4 = "\u662F\u5426\u663E\u793A\u8DEF\u7531";
|
|
35
|
+
|
|
36
|
+
declare namespace showRouter {
|
|
37
|
+
export {
|
|
38
|
+
value$2 as value,
|
|
39
|
+
name$2 as name,
|
|
40
|
+
explain$4 as explain,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare function value$1(...opts: string[]): number | undefined;
|
|
45
|
+
declare const name$1 = "port";
|
|
46
|
+
declare const explain$3 = "\u76D1\u542C\u7684\u7F51\u7EDC\u7AEF\u53E3\u53F7";
|
|
47
|
+
|
|
48
|
+
declare namespace port {
|
|
49
|
+
export {
|
|
50
|
+
value$1 as value,
|
|
51
|
+
name$1 as name,
|
|
52
|
+
explain$3 as explain,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
declare function value(...opts: string[]): string;
|
|
57
|
+
declare const name = "ip";
|
|
58
|
+
declare const explain$2 = "\u7ED1\u5B9A\u7684 IP";
|
|
59
|
+
|
|
60
|
+
declare const bind_value: typeof value;
|
|
61
|
+
declare const bind_name: typeof name;
|
|
62
|
+
declare namespace bind {
|
|
63
|
+
export {
|
|
64
|
+
bind_value as value,
|
|
65
|
+
bind_name as name,
|
|
66
|
+
explain$2 as explain,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
declare type List = keyof typeof opt;
|
|
71
|
+
declare const opt: {
|
|
72
|
+
bind: typeof bind;
|
|
73
|
+
listen: typeof listen;
|
|
74
|
+
showRouter: typeof showRouter;
|
|
75
|
+
path: typeof path;
|
|
76
|
+
port: typeof port;
|
|
77
|
+
};
|
|
78
|
+
declare type opt = OptType<typeof opt>;
|
|
79
|
+
|
|
80
|
+
declare function exec$1({ path, listen, port, bind, }: opt, ...args: string[]): Promise<void>;
|
|
81
|
+
declare const opts$1: List[];
|
|
82
|
+
declare const argv$1 = "";
|
|
83
|
+
declare const explain$1 = "\u542F\u52A8\u670D\u52A1\u5668";
|
|
84
|
+
declare function help$1(...argv: string[]): Promise<void>;
|
|
85
|
+
|
|
86
|
+
declare namespace start {
|
|
87
|
+
export {
|
|
88
|
+
exec$1 as exec,
|
|
89
|
+
opts$1 as opts,
|
|
90
|
+
argv$1 as argv,
|
|
91
|
+
explain$1 as explain,
|
|
92
|
+
help$1 as help,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
declare function exec({ path }: opt, ...args: string[]): Promise<void>;
|
|
97
|
+
declare const opts: List[];
|
|
98
|
+
declare const argv = "";
|
|
99
|
+
declare const explain = "\u521D\u59CB\u5316\u9879\u76EE";
|
|
100
|
+
declare function help(...argv: string[]): Promise<void>;
|
|
101
|
+
|
|
102
|
+
declare const init_exec: typeof exec;
|
|
103
|
+
declare const init_opts: typeof opts;
|
|
104
|
+
declare const init_argv: typeof argv;
|
|
105
|
+
declare const init_explain: typeof explain;
|
|
106
|
+
declare const init_help: typeof help;
|
|
107
|
+
declare namespace init {
|
|
108
|
+
export {
|
|
109
|
+
init_exec as exec,
|
|
110
|
+
init_opts as opts,
|
|
111
|
+
init_argv as argv,
|
|
112
|
+
init_explain as explain,
|
|
113
|
+
init_help as help,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
declare const _default: {
|
|
118
|
+
start: typeof start;
|
|
119
|
+
init: typeof init;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export { _default as command, opt as options, start };
|
package/cli/index.mjs
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* k99 v0.5.0-beta.0
|
|
3
|
+
* (c) 2019-2022 猛火 Fierflame
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
import * as http from 'http';
|
|
7
|
+
import { start as start$1, createHttpCallback } from 'k99/node';
|
|
8
|
+
import * as fsFn from 'fs';
|
|
9
|
+
|
|
10
|
+
async function exec$1({
|
|
11
|
+
path,
|
|
12
|
+
listen,
|
|
13
|
+
port = 8080,
|
|
14
|
+
bind
|
|
15
|
+
}, ...args) {
|
|
16
|
+
if (path) {
|
|
17
|
+
process.chdir(path);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const router = await start$1();
|
|
21
|
+
console.log('K99', '@', router.path);
|
|
22
|
+
const server = http.createServer(createHttpCallback(router)); // (global as any).router = router;
|
|
23
|
+
|
|
24
|
+
if (listen && typeof listen === 'string') {
|
|
25
|
+
server.listen(listen);
|
|
26
|
+
console.log('K99', '>', listen);
|
|
27
|
+
} else {
|
|
28
|
+
server.listen(port, bind);
|
|
29
|
+
console.log('K99', '>', `${bind || '0.0.0.0'}:${port}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const opts$1 = ['path', 'listen', 'port', 'bind'];
|
|
33
|
+
const argv$1 = '';
|
|
34
|
+
const explain$6 = '启动服务器';
|
|
35
|
+
async function help$1(...argv) {}
|
|
36
|
+
|
|
37
|
+
var start = /*#__PURE__*/Object.freeze({
|
|
38
|
+
__proto__: null,
|
|
39
|
+
exec: exec$1,
|
|
40
|
+
opts: opts$1,
|
|
41
|
+
argv: argv$1,
|
|
42
|
+
explain: explain$6,
|
|
43
|
+
help: help$1
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const configFileNames = ['k99.config.js', '.k99.config.js', 'k99.config.json', '.k99.config.json'];
|
|
47
|
+
const entryFile = `
|
|
48
|
+
const baseConfig = require('./.k99.config.json');
|
|
49
|
+
Object.assign(exports, baseConfig);
|
|
50
|
+
`;
|
|
51
|
+
async function exec({
|
|
52
|
+
path
|
|
53
|
+
}, ...args) {
|
|
54
|
+
if (path) {
|
|
55
|
+
await new Promise(r => fsFn.mkdir(path, {
|
|
56
|
+
recursive: true
|
|
57
|
+
}, () => r()));
|
|
58
|
+
process.chdir(path);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
console.log('K99', '@', process.cwd());
|
|
62
|
+
|
|
63
|
+
for (const n of configFileNames) {
|
|
64
|
+
if (await new Promise(r => fsFn.stat(n, e => r(!e)))) {
|
|
65
|
+
console.log(`The config (${n}) is Existing.`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
await new Promise((resolve, reject) => fsFn.writeFile('.k99.config.json', JSON.stringify({}, null, '\t'), e => e ? reject(e) : resolve()));
|
|
71
|
+
await new Promise((resolve, reject) => fsFn.writeFile('k99.config.js', entryFile, e => e ? reject(e) : resolve()));
|
|
72
|
+
const cfg = JSON.stringify(await new Promise(resolve => fsFn.readFile('package.json', 'utf-8', (_, d) => resolve(d)))) || {
|
|
73
|
+
private: true
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (!cfg.main) {
|
|
77
|
+
cfg.main = 'node_modules/k99/starter';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (!cfg.scripts) {
|
|
81
|
+
cfg.scripts = {};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!cfg.scripts.start) {
|
|
85
|
+
cfg.scripts.start = 'k99 --port=3000 start';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!cfg.scripts.test) {
|
|
89
|
+
cfg.scripts.test = 'node --inspect node_modules/k99/cli --port=3000 --show-router start';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (!cfg.dependencies) {
|
|
93
|
+
cfg.dependencies = {};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!cfg.dependencies.k99) {
|
|
97
|
+
cfg.dependencies.k99 = '^0.5.0-beta.0';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
await new Promise((resolve, reject) => fsFn.writeFile('package.json', JSON.stringify(cfg, null, 2), e => e ? reject(e) : resolve()));
|
|
101
|
+
}
|
|
102
|
+
const opts = ['path'];
|
|
103
|
+
const argv = '';
|
|
104
|
+
const explain$5 = '初始化项目';
|
|
105
|
+
async function help(...argv) {}
|
|
106
|
+
|
|
107
|
+
var init = /*#__PURE__*/Object.freeze({
|
|
108
|
+
__proto__: null,
|
|
109
|
+
exec: exec,
|
|
110
|
+
opts: opts,
|
|
111
|
+
argv: argv,
|
|
112
|
+
explain: explain$5,
|
|
113
|
+
help: help
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
var index = {
|
|
117
|
+
start,
|
|
118
|
+
init
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
function value$4(...opts) {
|
|
122
|
+
return opts[0];
|
|
123
|
+
}
|
|
124
|
+
const name$4 = 'socket';
|
|
125
|
+
const explain$4 = '监听的本地套接字';
|
|
126
|
+
|
|
127
|
+
var listen = /*#__PURE__*/Object.freeze({
|
|
128
|
+
__proto__: null,
|
|
129
|
+
value: value$4,
|
|
130
|
+
name: name$4,
|
|
131
|
+
explain: explain$4
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
function value$3(...opts) {
|
|
135
|
+
return opts[0] || '';
|
|
136
|
+
}
|
|
137
|
+
const name$3 = 'path';
|
|
138
|
+
const explain$3 = '环境路径';
|
|
139
|
+
|
|
140
|
+
var path = /*#__PURE__*/Object.freeze({
|
|
141
|
+
__proto__: null,
|
|
142
|
+
value: value$3,
|
|
143
|
+
name: name$3,
|
|
144
|
+
explain: explain$3
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
function value$2(...opts) {
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
const name$2 = '';
|
|
151
|
+
const explain$2 = '是否显示路由';
|
|
152
|
+
|
|
153
|
+
var showRouter = /*#__PURE__*/Object.freeze({
|
|
154
|
+
__proto__: null,
|
|
155
|
+
value: value$2,
|
|
156
|
+
name: name$2,
|
|
157
|
+
explain: explain$2
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
function value$1(...opts) {
|
|
161
|
+
for (let opt of opts) {
|
|
162
|
+
if (/^\d+$/.test(opt)) {
|
|
163
|
+
const port = Number(opt);
|
|
164
|
+
|
|
165
|
+
if (port > 0 && port < 65535) {
|
|
166
|
+
return port;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const name$1 = 'port';
|
|
172
|
+
const explain$1 = '监听的网络端口号';
|
|
173
|
+
|
|
174
|
+
var port = /*#__PURE__*/Object.freeze({
|
|
175
|
+
__proto__: null,
|
|
176
|
+
value: value$1,
|
|
177
|
+
name: name$1,
|
|
178
|
+
explain: explain$1
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
function value(...opts) {
|
|
182
|
+
return opts[0];
|
|
183
|
+
}
|
|
184
|
+
const name = 'ip';
|
|
185
|
+
const explain = '绑定的 IP';
|
|
186
|
+
|
|
187
|
+
var bind = /*#__PURE__*/Object.freeze({
|
|
188
|
+
__proto__: null,
|
|
189
|
+
value: value,
|
|
190
|
+
name: name,
|
|
191
|
+
explain: explain
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const opt = {
|
|
195
|
+
bind,
|
|
196
|
+
listen,
|
|
197
|
+
showRouter,
|
|
198
|
+
path,
|
|
199
|
+
port
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export { index as command, opt as options, start };
|
package/cli/package.json
ADDED
package/cli.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/*!
|
|
3
|
+
* k99 v0.5.0-beta.0
|
|
4
|
+
* (c) 2019-2022 猛火 Fierflame
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
import { cli } from 'entry-cli';
|
|
8
|
+
import { command, options } from 'k99/cli';
|
|
9
|
+
|
|
10
|
+
cli(command, options, {
|
|
11
|
+
execCmd: 'k99'
|
|
12
|
+
})(process.argv.slice(2));
|