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",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
@ -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
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;
|
||||
// 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) {
|
||||
|
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 url from 'url';
|
||||
import url from 'node:url';
|
||||
import { createHandleCtx } from './server.ts';
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user