add deno mod
This commit is contained in:
parent
4c11a44b48
commit
17e515ad32
20
package.json
20
package.json
@ -1,11 +1,8 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://json.schemastore.org/package",
|
"$schema": "https://json.schemastore.org/package",
|
||||||
"name": "@kevisual/router",
|
"name": "@kevisual/router",
|
||||||
"version": "0.0.10",
|
"version": "0.0.11",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
|
||||||
"module": "dist/index.js",
|
|
||||||
"types": "dist/index.d.ts",
|
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npm run clean && rollup -c",
|
"build": "npm run clean && rollup -c",
|
||||||
@ -14,7 +11,8 @@
|
|||||||
"clean": "rm -rf dist"
|
"clean": "rm -rf dist"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist",
|
||||||
|
"src"
|
||||||
],
|
],
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "abearxiong",
|
"author": "abearxiong",
|
||||||
@ -24,20 +22,20 @@
|
|||||||
"@rollup/plugin-node-resolve": "^16.0.1",
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
||||||
"@rollup/plugin-typescript": "^12.1.2",
|
"@rollup/plugin-typescript": "^12.1.2",
|
||||||
"@types/lodash-es": "^4.17.12",
|
"@types/lodash-es": "^4.17.12",
|
||||||
"@types/node": "^22.14.0",
|
"@types/node": "^22.14.1",
|
||||||
"@types/ws": "^8.18.1",
|
"@types/ws": "^8.18.1",
|
||||||
"@types/xml2js": "^0.4.14",
|
"@types/xml2js": "^0.4.14",
|
||||||
"cookie": "^1.0.2",
|
"cookie": "^1.0.2",
|
||||||
"lodash-es": "^4.17.21",
|
"lodash-es": "^4.17.21",
|
||||||
"nanoid": "^5.1.5",
|
"nanoid": "^5.1.5",
|
||||||
"rollup": "^4.39.0",
|
"rollup": "^4.40.0",
|
||||||
"rollup-plugin-dts": "^6.2.1",
|
"rollup-plugin-dts": "^6.2.1",
|
||||||
"ts-loader": "^9.5.2",
|
"ts-loader": "^9.5.2",
|
||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"tslib": "^2.8.1",
|
"tslib": "^2.8.1",
|
||||||
"typescript": "^5.8.2",
|
"typescript": "^5.8.3",
|
||||||
"xml2js": "^0.6.2",
|
"xml2js": "^0.6.2",
|
||||||
"zod": "^3.24.2"
|
"zod": "^3.24.3"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -71,6 +69,10 @@
|
|||||||
"./simple-lib": {
|
"./simple-lib": {
|
||||||
"import": "./dist/router-simple-lib.js",
|
"import": "./dist/router-simple-lib.js",
|
||||||
"require": "./dist/router-simple-lib.js"
|
"require": "./dist/router-simple-lib.js"
|
||||||
|
},
|
||||||
|
"./mod.ts": {
|
||||||
|
"import": "./src/mod.ts",
|
||||||
|
"require": "./src/mod.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import { QueryRouter, Route, RouteContext, RouteOpts } from './route.ts';
|
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 { WsServer } from './server/ws-server.ts';
|
||||||
import { CustomError } from './result/error.ts';
|
import { CustomError } from './result/error.ts';
|
||||||
|
|
||||||
|
13
src/mod.ts
Normal file
13
src/mod.ts
Normal 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';
|
26
src/route.ts
26
src/route.ts
@ -18,16 +18,31 @@ export type RouteContext<T = { code?: number }, S = any> = {
|
|||||||
// 传递状态
|
// 传递状态
|
||||||
state?: S;
|
state?: S;
|
||||||
// transfer data
|
// transfer data
|
||||||
|
/**
|
||||||
|
* 当前路径
|
||||||
|
*/
|
||||||
currentPath?: string;
|
currentPath?: string;
|
||||||
|
/**
|
||||||
|
* 当前key
|
||||||
|
*/
|
||||||
currentKey?: string;
|
currentKey?: string;
|
||||||
|
/**
|
||||||
|
* 当前route
|
||||||
|
*/
|
||||||
currentRoute?: Route;
|
currentRoute?: Route;
|
||||||
progress?: [[string, string]][];
|
/**
|
||||||
|
* 进度
|
||||||
|
*/
|
||||||
|
progress?: [string, string][];
|
||||||
// onlyForNextRoute will be clear after next route
|
// onlyForNextRoute will be clear after next route
|
||||||
nextQuery?: { [key: string]: any };
|
nextQuery?: { [key: string]: any };
|
||||||
// end
|
// end
|
||||||
end?: boolean;
|
end?: boolean;
|
||||||
// 处理router manager
|
// 处理router manager
|
||||||
// TODO:
|
// TODO:
|
||||||
|
/**
|
||||||
|
* 请求 route的返回结果,包函ctx
|
||||||
|
*/
|
||||||
queryRouter?: QueryRouter;
|
queryRouter?: QueryRouter;
|
||||||
error?: any;
|
error?: any;
|
||||||
/** 请求 route的返回结果,包函ctx */
|
/** 请求 route的返回结果,包函ctx */
|
||||||
@ -334,6 +349,12 @@ export class QueryRouter {
|
|||||||
ctx.currentKey = key;
|
ctx.currentKey = key;
|
||||||
ctx.currentRoute = route;
|
ctx.currentRoute = route;
|
||||||
ctx.index = (ctx.index || 0) + 1;
|
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) {
|
if (ctx.index > maxNextRoute) {
|
||||||
ctx.code = 500;
|
ctx.code = 500;
|
||||||
ctx.message = 'Too many nextRoute';
|
ctx.message = 'Too many nextRoute';
|
||||||
@ -509,7 +530,7 @@ export class QueryRouter {
|
|||||||
const { path, key = '', payload = {}, ...query } = message;
|
const { path, key = '', payload = {}, ...query } = message;
|
||||||
ctx = ctx || {};
|
ctx = ctx || {};
|
||||||
ctx.query = { ...ctx.query, ...query, ...payload };
|
ctx.query = { ...ctx.query, ...query, ...payload };
|
||||||
ctx.state = {};
|
ctx.state = { ...ctx?.state };
|
||||||
ctx.throw = this.throw;
|
ctx.throw = this.throw;
|
||||||
// put queryRouter to ctx
|
// put queryRouter to ctx
|
||||||
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
||||||
@ -517,6 +538,7 @@ export class QueryRouter {
|
|||||||
ctx.call = this.call.bind(this);
|
ctx.call = this.call.bind(this);
|
||||||
ctx.queryRoute = this.queryRoute.bind(this);
|
ctx.queryRoute = this.queryRoute.bind(this);
|
||||||
ctx.index = 0;
|
ctx.index = 0;
|
||||||
|
ctx.progress = ctx.progress || [];
|
||||||
const res = await this.runRoute(path, key, ctx);
|
const res = await this.runRoute(path, key, ctx);
|
||||||
const serialize = ctx.needSerialize ?? true; // 是否需要序列化
|
const serialize = ctx.needSerialize ?? true; // 是否需要序列化
|
||||||
if (serialize) {
|
if (serialize) {
|
||||||
|
0
src/server/deno-ws-server.ts
Normal file
0
src/server/deno-ws-server.ts
Normal 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 { parseBody } from './parse-body.ts';
|
||||||
import url from 'url';
|
import url from 'node:url';
|
||||||
import { createHandleCtx } from './server.ts';
|
import { createHandleCtx } from './server.ts';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import * as http from 'http';
|
import type { IncomingMessage } from 'node:http';
|
||||||
import url from 'url';
|
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) => {
|
return new Promise<T>((resolve, reject) => {
|
||||||
const arr: any[] = [];
|
const arr: any[] = [];
|
||||||
req.on('data', (chunk) => {
|
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);
|
const parsedUrl = url.parse(req.url, true);
|
||||||
return parsedUrl.query;
|
return parsedUrl.query;
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import http, { IncomingMessage, ServerResponse } from 'http';
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
||||||
import https from 'https';
|
import http from 'node:http';
|
||||||
import http2 from 'http2';
|
import https from 'node:https';
|
||||||
|
import http2 from 'node:http2';
|
||||||
import { handleServer } from './handle-server.ts';
|
import { handleServer } from './handle-server.ts';
|
||||||
import * as cookie from 'cookie';
|
import * as cookie from 'cookie';
|
||||||
export type Listener = (...args: any[]) => void;
|
export type Listener = (...args: any[]) => void;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { WebSocketServer, WebSocket } from 'ws';
|
import { WebSocketServer } from 'ws';
|
||||||
|
import type { WebSocket } from 'ws';
|
||||||
import { Server } from './server.ts';
|
import { Server } from './server.ts';
|
||||||
import { parseIfJson } from '../utils/parse.ts';
|
import { parseIfJson } from '../utils/parse.ts';
|
||||||
|
|
||||||
@ -23,7 +24,10 @@ export class WsServerBase {
|
|||||||
listeners: { type: string; listener: ListenerFn }[] = [];
|
listeners: { type: string; listener: ListenerFn }[] = [];
|
||||||
listening: boolean = false;
|
listening: boolean = false;
|
||||||
constructor(opts: WsServerBaseOpts) {
|
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 || '';
|
this.path = opts.path || '';
|
||||||
}
|
}
|
||||||
setPath(path: string) {
|
setPath(path: string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user