add deno mod

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

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) {