add deno mod

This commit is contained in:
xion 2025-04-17 23:54:21 +08:00
parent 4c11a44b48
commit 17e515ad32
9 changed files with 65 additions and 23 deletions

View File

@ -1,11 +1,8 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router",
"version": "0.0.10",
"version": "0.0.11",
"description": "",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"scripts": {
"build": "npm run clean && rollup -c",
@ -14,7 +11,8 @@
"clean": "rm -rf dist"
},
"files": [
"dist"
"dist",
"src"
],
"keywords": [],
"author": "abearxiong",
@ -24,20 +22,20 @@
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-typescript": "^12.1.2",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.14.0",
"@types/node": "^22.14.1",
"@types/ws": "^8.18.1",
"@types/xml2js": "^0.4.14",
"cookie": "^1.0.2",
"lodash-es": "^4.17.21",
"nanoid": "^5.1.5",
"rollup": "^4.39.0",
"rollup": "^4.40.0",
"rollup-plugin-dts": "^6.2.1",
"ts-loader": "^9.5.2",
"ts-node": "^10.9.2",
"tslib": "^2.8.1",
"typescript": "^5.8.2",
"typescript": "^5.8.3",
"xml2js": "^0.6.2",
"zod": "^3.24.2"
"zod": "^3.24.3"
},
"repository": {
"type": "git",
@ -71,6 +69,10 @@
"./simple-lib": {
"import": "./dist/router-simple-lib.js",
"require": "./dist/router-simple-lib.js"
},
"./mod.ts": {
"import": "./src/mod.ts",
"require": "./src/mod.ts"
}
}
}

View File

@ -1,5 +1,5 @@
import { QueryRouter, Route, RouteContext, RouteOpts } from './route.ts';
import { Server, Cors, ServerOpts, HandleCtx } from './server/server.ts';
import { Server, ServerOpts, HandleCtx } from './server/server.ts';
import { WsServer } from './server/ws-server.ts';
import { CustomError } from './result/error.ts';

13
src/mod.ts Normal file
View File

@ -0,0 +1,13 @@
import { Route, QueryRouter, QueryRouterServer } from './route.ts';
export { App } from './app.ts';
export { Route, QueryRouter, QueryRouterServer };
export { Rule, Schema, createSchema } from './validator/index.ts';
export type { RouteContext, RouteOpts } from './route.ts';
export type { Run } from './route.ts';
export { CustomError } from './result/error.ts';

View File

@ -18,16 +18,31 @@ export type RouteContext<T = { code?: number }, S = any> = {
// 传递状态
state?: S;
// transfer data
/**
*
*/
currentPath?: string;
/**
* key
*/
currentKey?: string;
/**
* route
*/
currentRoute?: Route;
progress?: [[string, string]][];
/**
*
*/
progress?: [string, string][];
// onlyForNextRoute will be clear after next route
nextQuery?: { [key: string]: any };
// end
end?: boolean;
// 处理router manager
// TODO:
/**
* route的返回结果ctx
*/
queryRouter?: QueryRouter;
error?: any;
/** 请求 route的返回结果包函ctx */
@ -334,6 +349,12 @@ export class QueryRouter {
ctx.currentKey = key;
ctx.currentRoute = route;
ctx.index = (ctx.index || 0) + 1;
const progress = [path, key] as [string, string];
if (ctx.progress) {
ctx.progress.push(progress);
} else {
ctx.progress = [progress];
}
if (ctx.index > maxNextRoute) {
ctx.code = 500;
ctx.message = 'Too many nextRoute';
@ -509,7 +530,7 @@ export class QueryRouter {
const { path, key = '', payload = {}, ...query } = message;
ctx = ctx || {};
ctx.query = { ...ctx.query, ...query, ...payload };
ctx.state = {};
ctx.state = { ...ctx?.state };
ctx.throw = this.throw;
// put queryRouter to ctx
// TODO: 是否需要queryRouter函数内部处理router路由执行这应该是避免去内部去包含的功能过
@ -517,6 +538,7 @@ export class QueryRouter {
ctx.call = this.call.bind(this);
ctx.queryRoute = this.queryRoute.bind(this);
ctx.index = 0;
ctx.progress = ctx.progress || [];
const res = await this.runRoute(path, key, ctx);
const serialize = ctx.needSerialize ?? true; // 是否需要序列化
if (serialize) {

View File

View File

@ -1,6 +1,6 @@
import http, { IncomingMessage, Server, ServerResponse } from 'http';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { parseBody } from './parse-body.ts';
import url from 'url';
import url from 'node:url';
import { createHandleCtx } from './server.ts';
/**

View File

@ -1,7 +1,7 @@
import * as http from 'http';
import url from 'url';
import type { IncomingMessage } from 'node:http';
import url from 'node:url';
export const parseBody = async <T = Record<string, any>>(req: http.IncomingMessage) => {
export const parseBody = async <T = Record<string, any>>(req: IncomingMessage) => {
return new Promise<T>((resolve, reject) => {
const arr: any[] = [];
req.on('data', (chunk) => {
@ -18,7 +18,7 @@ export const parseBody = async <T = Record<string, any>>(req: http.IncomingMessa
});
};
export const parseSearch = (req: http.IncomingMessage) => {
export const parseSearch = (req: IncomingMessage) => {
const parsedUrl = url.parse(req.url, true);
return parsedUrl.query;
};

View File

@ -1,6 +1,7 @@
import http, { IncomingMessage, ServerResponse } from 'http';
import https from 'https';
import http2 from 'http2';
import type { IncomingMessage, ServerResponse } from 'node:http';
import http from 'node:http';
import https from 'node:https';
import http2 from 'node:http2';
import { handleServer } from './handle-server.ts';
import * as cookie from 'cookie';
export type Listener = (...args: any[]) => void;

View File

@ -1,4 +1,5 @@
import { WebSocketServer, WebSocket } from 'ws';
import { WebSocketServer } from 'ws';
import type { WebSocket } from 'ws';
import { Server } from './server.ts';
import { parseIfJson } from '../utils/parse.ts';
@ -23,7 +24,10 @@ export class WsServerBase {
listeners: { type: string; listener: ListenerFn }[] = [];
listening: boolean = false;
constructor(opts: WsServerBaseOpts) {
this.wss = opts.wss || new WebSocketServer();
this.wss = opts.wss;
if (!this.wss) {
throw new Error('wss is required');
}
this.path = opts.path || '';
}
setPath(path: string) {