Gathering detailed insights and metrics for @ghini/kit
Gathering detailed insights and metrics for @ghini/kit
Gathering detailed insights and metrics for @ghini/kit
Gathering detailed insights and metrics for @ghini/kit
@ghini/kit-dev
js的实用工具函数库,开发完整版,有较详细的demo和注释. 这是个汇总大模块,使用方便,代价是初始化开销略微大,但对现代性能来说微乎其微. 要优化起来也很简单,直接把使用部分单独拎出去就行,仅在最求极致的速度(✅)和蚊子腿内存(❌)时才有可能这么做
@ghini/sv
Everything you need to build a Svelte library, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte).
npm install @ghini/kit
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
7
2
1npm i @ghini/kit
1import kit from "@ghini/kit/dev";
快速开始,非常实用,用完就离不开的cs(console):
测试文件路径: ./test/t-cs.js
1import kit from "@ghini/kit/dev"; 2kit.cs(); 3// kit.cs(0); 4// kit.cs(1); 5// kit.cs(2); 6// kit.cs(3); 7// kit.cs({ 8// dev:{info:3}, 9// }); 10// kit.cs({ 11// dev:{info:6}, 12// err:{info:3}, 13// log:{trace:2}, 14// }); 15console.log("Let's start!"); 16console.info("info"); 17console.debug("debug"); 18console.warn("warn"); 19console.error("error"); 20console.dev("dev");
1node --watch ./test/t-console.js
进阶用法: 尝试依序将下方注释解开
测试文件路径:./test/api/main.js
1import kit from "@ghini/kit"; 2kit.cs(); 3const server = kit.h2s(); 4kit.h2s({ allowHTTP1: false }); 5kit.hss(); 6kit.hs(); 7kit.h2s(8080); 8server.addr("/", (gold) => gold.json(gold)); 9server.addr("/post", "post", (gold) => gold.raw("post only")); 10server.addr(/regexp/, (gold) => gold.raw("任意包含regexp的路径 Any path that contains regexp")); 11console.log(server.routes);
./test/t-req.js
1import kit from "@ghini/kit/dev"; 2kit.cs({ 3 dev: { line: 3 }, 4}); 5// 创建http https h2服务器,并设置/test路由 6const sarr = await Promise.all([kit.hs(3300), kit.hss(3301), kit.h2s(3302)]); 7function fn(g) { 8 const { query, data, cookie } = g; 9 g.setcookie([`t=${performance.now()};max-age=3600`, "a=1;max-age=3600"]); 10 g.json({ query, data, cookie }); 11} 12sarr.forEach((s) => { 13 s.addr("/timetout", () => {}); 14 s.addr("/test", fn); 15}); 16 17// req使用示例 18console.log("超时(错误处理)示例==========================================="); 19let [res, res1, res2] = await Promise.all([ 20 kit 21 .req("http://localhost:3300/timetout") 22 .then((res) => console.log(res) || res), 23 kit 24 .req("https://localhost:3301/timetout", { cert: false, timeout: 4000 }) 25 .then((res) => console.log(res) || res), 26 kit 27 .req("https://localhost:3302/timetout", { cert: false, timeout: 1000 }) 28 .then((res) => console.log(res) || res), 29]); 30console.log(res.reqbd); 31console.log(res1.reqbd); 32console.log(res2.reqbd); 33await kit.sleep(5000); 34console.log( 35 "method,query,body,options,headers示例===========================================" 36); 37const json = { foo: "hello" }; 38[res, res1, res2] = await Promise.all([ 39 res 40 .req("/test?a=1&a=2&b=宝贝 post", { json }) 41 .then((res) => console.log(res) || res), 42 res1 43 .req("/test?a=1&a=2&b=宝贝 post", { cert: false, timeout: 4000, json }) 44 .then((res) => console.log(res) || res), 45 res2 46 .req("/test?a=1&a=2&b=宝贝 post", { cert: false, timeout: 1000, json }) 47 .then((res) => console.log(res) || res), 48]); 49await kit.sleep(3000); 50console.log("快速完全重复请求示例==========================================="); 51res.req().then((res) => console.log(res) || res); 52res1.req().then((res) => console.log(res) || res); 53res2.req().then((res) => console.log(res) || res);
测试文件路径:./test/t-redis.js
测试文件路径:./test/t-basic.js
./test/t-codec.js
1import kit from "@ghini/kit/dev"; 2kit.cs(); 3// http1.1服务器 4kit.hs(3001); 5// http2tls服务器,兼容1.1 6const server = kit.h2s(3002); 7// 默认路由为空,但有全局404错误处理 8console.log(server.routes); 9// 这些是预定义的路由,可以尝试一一去访问,体会其效果 10server.routes=kit.default_routes(); 11// function default_routes() { 12// return [ 13// ["/", "*", "*", hd_hello.bind("Ghini"), undefined, {}], 14// [/^\/gold/, "*", "*", hd_hello.bind("RegExp"), undefined, {}], 15// ["/gold", "*", "*", hd_default, undefined, {}], 16// ["/data", "POST", "*", hd_data, undefined, {}], 17// ["/error", "*", "*", hd_error, undefined, {}], 18// ["/stream", "*", "*", hd_stream, undefined, {}], 19// ["/countdata", "*", "*", (g) => g.end(g.body), hd_countdata, {}], 20// ]; 21// } 22console.log(server.routes);
1server.addr(/^\/gold/, (gold) => gold.raw("cover")); 2server.addr("/data", "post", (gold) => gold.json(gold)); 3server.addr(/any/, (gold) => { 4 gold.respond({ 5 "content-type": "text/plain;charset=UTF-8", 6 "set-cookie": 7 "name=ghini;max-age=3600;path=/;httponly;secure;samesite=strict", 8 }); 9 gold.write("你可以使用任何带有any的url访问这个路由\n"); 10 gold.end("但不包括?后面的参数,其属于param"); 11}); 12// 第一个函数是handle_onend,第二个函数(很少用)是handle_ondata(需要传body,多一点效果明显) 13server.addr( 14 "/handle_ondata", 15 (g) => g.end(g.body), 16 (g, chunk, chunks) => { 17 // 响应头一定是在body前返回的,如果直接write会默认给响应头 18 // gold.respond方法做过优化,不会重复给响应头 19 g.respond({ 20 "content-type": "text/plain;charset=UTF-8", 21 }); 22 console.log(chunk, chunks.length); 23 // 这里我们尝试改掉原来的chunks.push(chunk); 会影响gold.body的生成 24 // chunks.push(chunk); 25 chunks.push(Buffer.from(chunks.length+',')); 26 g.write(`data: ${chunks.length}\n`); 27 } 28); 29console.log(server.routes);
1server._404 = undefined; //虽然能去掉,但后台会报错 2server._404 = () => {}; //正确不报错的去404提示 3server._404 = (g) => { 4 g.err("404 not found , 自定义的404响应", 404); 5};
1// 用postman之类工具,对/data进行一些测试,比如[/post](https://localhost:3002/data?a=123&中文=你好),对body的json x-www-form-urlencoded form-data的主流类型都进行了支持,轻松一键获取数据. 2// 我们使用最复杂的情况,form-data指定key携带多文件,和空key多文件 3server.addr("/data",'post', (gold) => { 4 gold.respond({ "content-type": "text/plain;charset=UTF-8" }); 5 gold.write(JSON.stringify(gold.param)+'\n'); 6 gold.end(JSON.stringify(gold.data,0,2)); 7});
1{"a":"123","中文":"你好"} 2{ 3 "vk_swiftshader_icd.json": { 4 "filename": "vk_swiftshader_icd.json", 5 "content": "{\"file_format_version\": \"1.0.0\", \"ICD\": {\"library_path\": \".\\\\vk_swiftshader.dll\", \"api_version\": \"1.0.5\"}}", 6 "contentType": "application/json" 7 }, 8 "Squirrel-UpdateSelf.log": { 9 "filename": "Squirrel-UpdateSelf.log", 10 "content": "[15/12/24 09:23:05] info: Program: Starting Squirrel Updater: --updateSelf=C:\\Users\\pznfo\\AppData\\Local\\SquirrelTemp\\Update.exe\r\n[15/12/24 09:23:05] info: Program: About to wait for parent PID 46400\r\n[15/12/24 09:23:07] info: Program: Finished Squirrel Updater", 11 "contentType": "text/plain" 12 }, 13 "usekey": [ 14 { 15 "filename": "profile.json", 16 "content": "{\n \"name\": \"Postman\",\n \"company\": \"Postman\",\n \"installationOptions\": {}\n}", 17 "contentType": "application/json" 18 }, 19 { 20 "filename": "icon.png", 21 "content": "�PNG\r\n\u001a\n+00:00���:\u0000\u0000\u0000%tEXtdate:modify\u0000IEND�B`�", 22 "contentType": "image/png" 23 } 24 ], 25 "ok": "good" 26}
No vulnerabilities found.
No security vulnerabilities found.