release: 0.1.2-1

This commit is contained in:
186526 2024-08-02 04:31:08 +08:00
parent 89c5ed561a
commit 7051a95fb9
No known key found for this signature in database
GPG Key ID: 2ED6600900186526
12 changed files with 781 additions and 423 deletions

View File

@ -1,6 +0,0 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node',
forceExit: true
};

22
jest.config.ts Normal file
View File

@ -0,0 +1,22 @@
// jest.config.ts
import type { JestConfigWithTsJest } from 'ts-jest';
const jestConfig: JestConfigWithTsJest = {
// [...]
preset: 'ts-jest/presets/default-esm', // or other ESM presets
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
};
export default jestConfig;

View File

@ -1,7 +1,7 @@
{ {
"name": "handlers.js", "name": "handlers.js",
"description": "Handlers.js is a unified and lightweight web application framework for multiple platforms.", "description": "Handlers.js is a unified and lightweight web application framework for multiple platforms.",
"version": "0.1.2", "version": "0.1.2-1",
"main": "./dist/index.js", "main": "./dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
"exports": { "exports": {
@ -42,9 +42,9 @@
"bun-types": "^0.1.4", "bun-types": "^0.1.4",
"eslint": "9.x", "eslint": "9.x",
"globals": "^15.8.0", "globals": "^15.8.0",
"jest": "^28.1.2", "jest": "^29.7.0",
"prettier": "^2.7.1", "prettier": "^2.7.1",
"ts-jest": "^28.0.5", "ts-jest": "^29.2.4",
"ts-loader": "^9.3.1", "ts-loader": "^9.3.1",
"ts-node": "^10.8.1", "ts-node": "^10.8.1",
"tsc-alias": "^1.8.10", "tsc-alias": "^1.8.10",
@ -67,9 +67,9 @@
"demo": "env NODE_ENV=development yarn build:node && node ./dist/main.node.js", "demo": "env NODE_ENV=development yarn build:node && node ./dist/main.node.js",
"tsc": "tsc && tsc-alias", "tsc": "tsc && tsc-alias",
"lint": "eslint --fix **/*.ts", "lint": "eslint --fix **/*.ts",
"test:node": "jest ./test/node.test.ts", "test:node": "NODE_OPTIONS=--experimental-vm-modules jest ./test/node.test.ts",
"test:deno": "BUILD_TARGET=deno:test webpack && jest ./test/deno.test.ts", "test:deno": "BUILD_TARGET=deno:test webpack && jest ./test/deno.test.ts",
"coverage": "jest --collectCoverage --", "coverage": "ODE_OPTIONS=--experimental-vm-modules jest --collectCoverage --",
"prepublish": "env NODE_ENV=production yarn build && yarn tsc", "prepublish": "env NODE_ENV=production yarn build && yarn tsc",
"format": "prettier --write \"**/*.{ts,json,md}\" " "format": "prettier --write \"**/*.{ts,json,md}\" "
}, },

View File

@ -20,8 +20,8 @@ const DefaultConn: Deno.Conn = {
closeWrite: async () => undefined, closeWrite: async () => undefined,
readable: '', readable: '',
writable: '', writable: '',
read: async (p: Uint8Array) => null, read: async (_p: Uint8Array) => null,
write: async (p: Uint8Array) => 0, write: async (_p: Uint8Array) => 0,
close: () => undefined, close: () => undefined,
}; };
@ -29,10 +29,12 @@ export class DenoPlatformAdapter<T = any, K = any>
extends SWPlatformAdapter<T, K> extends SWPlatformAdapter<T, K>
implements platformAdapater<T, K> implements platformAdapater<T, K>
{ {
async listen(port: number): Promise<void> { public server: Deno.Listener;
const Server: Deno.Listener = Deno.listen({ port });
for await (const connection of Server) { async listen(port: number): Promise<void> {
this.server = Deno.listen({ port });
for await (const connection of this.server) {
const httpConnection = Deno.serveHttp(connection); const httpConnection = Deno.serveHttp(connection);
for await (const requestEvent of httpConnection) { for await (const requestEvent of httpConnection) {
@ -43,6 +45,10 @@ export class DenoPlatformAdapter<T = any, K = any>
} }
} }
close() {
this.server.close();
}
async handleRequest( async handleRequest(
nativeRequest: Request, nativeRequest: Request,
connection: Deno.Conn = DefaultConn, connection: Deno.Conn = DefaultConn,
@ -56,8 +62,9 @@ export class DenoPlatformAdapter<T = any, K = any>
requestHeaders, requestHeaders,
await nativeRequest.text(), await nativeRequest.text(),
{}, {},
`${connection.remoteAddr.hostname}:${connection.remoteAddr.port}` || `${connection.remoteAddr.hostname ?? '0.0.0.0'}:${
'', connection.remoteAddr.port ?? '0'
}`,
); );
return requestMessage; return requestMessage;
} }

View File

@ -4,6 +4,7 @@ import { router } from '../router';
export interface platformAdapater<T = any, K = any> { export interface platformAdapater<T = any, K = any> {
router: router<T, K>; router: router<T, K>;
listen(port: number): void; listen(port: number): void;
close(): void;
handleRequest(nativeRequest: any): Promise<request<T>>; handleRequest(nativeRequest: any): Promise<request<T>>;
handleResponse( handleResponse(
response: response<K> | Promise<response<K>>, response: response<K> | Promise<response<K>>,

View File

@ -8,14 +8,16 @@ import { methodENUM } from 'src/interface/method';
export class NodePlatformAdapter<T = any, K = any> implements platformAdapater { export class NodePlatformAdapter<T = any, K = any> implements platformAdapater {
public router: router<T, K>; public router: router<T, K>;
public server: http.Server;
constructor(router: router<T, K>) { constructor(router: router<T, K>, server?: http.Server) {
this.router = router; this.router = router;
if (server) this.server = server;
else this.server = http.createServer();
} }
async listen(port: number): Promise<void> { async listen(port: number): Promise<void> {
const server = http.createServer(); this.server.on(
server.on(
'request', 'request',
async (req: http.IncomingMessage, res: http.ServerResponse) => { async (req: http.IncomingMessage, res: http.ServerResponse) => {
const request = await this.handleRequest(req); const request = await this.handleRequest(req);
@ -23,8 +25,11 @@ export class NodePlatformAdapter<T = any, K = any> implements platformAdapater {
this.handleResponse(response, res); this.handleResponse(response, res);
}, },
); );
server.listen(port); this.server.listen(port);
return; }
close() {
this.server.close();
} }
async handleRequest( async handleRequest(

View File

@ -8,15 +8,20 @@ import { methodENUM } from 'src/interface/method';
export class SWPlatformAdapter<T = any, K = any> implements platformAdapater { export class SWPlatformAdapter<T = any, K = any> implements platformAdapater {
public router: router<T, K>; public router: router<T, K>;
private eventHandler = (event: FetchEvent) => {
event.respondWith(this.handler(event));
};
constructor(router: router<T, K>) { constructor(router: router<T, K>) {
this.router = router; this.router = router;
} }
async listen(_port?: number): Promise<void> { async listen(_port?: number): Promise<void> {
self.addEventListener('fetch', (event: FetchEvent) => { self.addEventListener('fetch', this.eventHandler);
event.respondWith(this.handler(event)); }
});
close() {
self.removeEventListener('fetch', this.eventHandler);
} }
async handleRequest(nativeRequest: Request): Promise<request<T>> { async handleRequest(nativeRequest: Request): Promise<request<T>> {

View File

@ -8,15 +8,16 @@ export class TxikiPlatformAdapter<T = any, K = any>
implements platformAdapater implements platformAdapater
{ {
public router: router<T, K>; public router: router<T, K>;
public server: tjs.Listener;
constructor(router: router<T, K>) { constructor(router: router<T, K>) {
this.router = router; this.router = router;
} }
async listen(port?: number): Promise<void> { async listen(port?: number): Promise<void> {
const Server = await tjs.listen('tcp', '0.0.0.0', port); this.server = await tjs.listen('tcp', '0.0.0.0', port);
for await (const conn of Server) { for await (const conn of this.server) {
const httpConn = serveHttp(conn); const httpConn = serveHttp(conn);
for await (const conn of httpConn) { for await (const conn of httpConn) {
@ -31,6 +32,10 @@ export class TxikiPlatformAdapter<T = any, K = any>
} }
} }
close() {
this.server.close();
}
async handleRequest(nativeRequest: request<any>): Promise<request<T>> { async handleRequest(nativeRequest: request<any>): Promise<request<T>> {
return nativeRequest; return nativeRequest;
} }

View File

@ -215,10 +215,12 @@ export class rootRouter<K = any, V = any> extends router<K, V> {
return responseMessage; return responseMessage;
}; };
useAdapater(adapater: platformAdapaterConstructor): this { useAdapater(adapater: platformAdapaterConstructor): this {
this.adapater = createPlatformAdapater(adapater, this); this.adapater = createPlatformAdapater(adapater, this);
return this; return this;
} }
useMappingAdapter( useMappingAdapter(
mapping: { mapping: {
[platform: string]: platformAdapaterConstructor; [platform: string]: platformAdapaterConstructor;
@ -231,8 +233,14 @@ export class rootRouter<K = any, V = any> extends router<K, V> {
else this.useAdapater(mapping[platform]); else this.useAdapater(mapping[platform]);
return this; return this;
} }
listen(port: number): void { listen(port: number): void {
if (this.adapater == null) throw new Error('No platform adapter set'); if (this.adapater == null) throw new Error('No platform adapter set');
this.adapater.listen(port); this.adapater.listen(port);
} }
close(): void {
if (this.adapater == null) throw new Error('No platform adapter set');
this.adapater.close();
}
} }

View File

@ -81,3 +81,8 @@ describe('Test server', () => {
expect(secondData).toEqual('hit'); expect(secondData).toEqual('hit');
}); });
}); });
afterAll((done) => {
_.close();
done();
});

View File

@ -1,10 +1,8 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "esnext", "target": "ESNext",
"module": "esnext", "module": "ESNext",
"lib": [ "lib": ["ESNext"],
"ESNext"
],
"types": [ "types": [
"@cloudflare/workers-types", "@cloudflare/workers-types",
"@types/node", "@types/node",
@ -37,18 +35,11 @@
], ],
"declaration": true "declaration": true
}, },
"exclude": [ "exclude": ["node_modules"],
"node_modules" "include": ["index.ts", "src/**/*.ts", "demo/**/*.ts", "types/*.d.ts"],
],
"include": [
"index.ts",
"src/**/*.ts",
"demo/**/*.ts",
"types/*.d.ts"
],
"tsc-alias": { "tsc-alias": {
"resolveFullPaths": true, "resolveFullPaths": true,
"verbose": true "verbose": true
} }
// "esm": true // "esm": true
} }

1075
yarn.lock

File diff suppressed because it is too large Load Diff