Skip to content

Commit

Permalink
release: 0.1.2-1
Browse files Browse the repository at this point in the history
  • Loading branch information
186526 committed Aug 1, 2024
1 parent 89c5ed5 commit 7051a95
Show file tree
Hide file tree
Showing 12 changed files with 783 additions and 425 deletions.
6 changes: 0 additions & 6 deletions jest.config.js

This file was deleted.

22 changes: 22 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand All @@ -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}\" "
},
Expand Down
19 changes: 13 additions & 6 deletions src/platform/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ 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,
};

export class DenoPlatformAdapter<T = any, K = any>
extends SWPlatformAdapter<T, K>
implements platformAdapater<T, K>
{
public server: Deno.Listener;

async listen(port: number): Promise<void> {
const Server: Deno.Listener = Deno.listen({ port });
this.server = Deno.listen({ port });

for await (const connection of Server) {
for await (const connection of this.server) {
const httpConnection = Deno.serveHttp(connection);

for await (const requestEvent of httpConnection) {
Expand All @@ -43,6 +45,10 @@ export class DenoPlatformAdapter<T = any, K = any>
}
}

close() {
this.server.close();
}

async handleRequest(
nativeRequest: Request,
connection: Deno.Conn = DefaultConn,
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down
15 changes: 10 additions & 5 deletions src/platform/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ 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);
const response = await this.router.respond(request);
this.handleResponse(response, res);
},
);
server.listen(port);
return;
this.server.listen(port);
}

close() {
this.server.close();
}

async handleRequest(
Expand Down
11 changes: 8 additions & 3 deletions src/platform/serviceworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down
9 changes: 7 additions & 2 deletions src/platform/txiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
5 changes: 5 additions & 0 deletions test/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ describe('Test server', () => {
expect(secondData).toEqual('hit');
});
});

afterAll((done) => {
_.close();
done();
});
21 changes: 6 additions & 15 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [
"ESNext"
],
"target": "ESNext",
"module": "ESNext",
"lib": ["ESNext"],
"types": [
"@cloudflare/workers-types",
"@types/node",
Expand Down Expand Up @@ -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
}
}
Loading

0 comments on commit 7051a95

Please sign in to comment.