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",
"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",
"types": "./dist/index.d.ts",
"exports": {
@ -42,9 +42,9 @@
"bun-types": "^0.1.4",
"eslint": "9.x",
"globals": "^15.8.0",
"jest": "^28.1.2",
"jest": "^29.7.0",
"prettier": "^2.7.1",
"ts-jest": "^28.0.5",
"ts-jest": "^29.2.4",
"ts-loader": "^9.3.1",
"ts-node": "^10.8.1",
"tsc-alias": "^1.8.10",
@ -67,9 +67,9 @@
"demo": "env NODE_ENV=development yarn build:node && node ./dist/main.node.js",
"tsc": "tsc && tsc-alias",
"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",
"coverage": "jest --collectCoverage --",
"coverage": "ODE_OPTIONS=--experimental-vm-modules jest --collectCoverage --",
"prepublish": "env NODE_ENV=production yarn build && yarn tsc",
"format": "prettier --write \"**/*.{ts,json,md}\" "
},

View File

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

View File

@ -4,6 +4,7 @@ import { router } from '../router';
export interface platformAdapater<T = any, K = any> {
router: router<T, K>;
listen(port: number): void;
close(): void;
handleRequest(nativeRequest: any): Promise<request<T>>;
handleResponse(
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 {
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;
if (server) this.server = server;
else this.server = http.createServer();
}
async listen(port: number): Promise<void> {
const server = http.createServer();
server.on(
this.server.on(
'request',
async (req: http.IncomingMessage, res: http.ServerResponse) => {
const request = await this.handleRequest(req);
@ -23,8 +25,11 @@ export class NodePlatformAdapter<T = any, K = any> implements platformAdapater {
this.handleResponse(response, res);
},
);
server.listen(port);
return;
this.server.listen(port);
}
close() {
this.server.close();
}
async handleRequest(

View File

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

View File

@ -8,15 +8,16 @@ export class TxikiPlatformAdapter<T = any, K = any>
implements platformAdapater
{
public router: router<T, K>;
public server: tjs.Listener;
constructor(router: router<T, K>) {
this.router = router;
}
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);
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>> {
return nativeRequest;
}

View File

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

View File

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

1075
yarn.lock

File diff suppressed because it is too large Load Diff