mirror of
https://github.com/186526/handlers.js
synced 2024-10-13 00:29:43 +00:00
Added NodePlatformAdapter and fixed bug in platform independent functions and added Demo.
This commit is contained in:
1
src/platform/export.ts
Normal file
1
src/platform/export.ts
Normal file
@ -0,0 +1 @@
|
||||
export { NodePlatformAdapter } from "./node";
|
||||
@ -1,7 +1,20 @@
|
||||
import { request, response } from "../interface";
|
||||
import { router } from "../../index";
|
||||
|
||||
export interface PlatformAdapater<T = any, K = any> {
|
||||
export interface platformAdapater<T = any, K = any> {
|
||||
router: router<T, K>;
|
||||
listen(port: number): void;
|
||||
handleRequest(request: any): request<T>;
|
||||
handleResponse(response: response<K>, NativeResponse?: any): any;
|
||||
handleRequest(nativeRequest: any): Promise<request<T>>;
|
||||
handleResponse(response: response<K>, nativeResponse?: any): any;
|
||||
}
|
||||
|
||||
export interface platformAdapaterConstructor<T = any, K = any> {
|
||||
new (router: router<T, K>): platformAdapater<T, K>;
|
||||
}
|
||||
|
||||
export function createPlatformAdapater(
|
||||
adapater: platformAdapaterConstructor,
|
||||
router: router
|
||||
): platformAdapater {
|
||||
return new adapater(router);
|
||||
}
|
||||
|
||||
@ -1,27 +1,77 @@
|
||||
import { PlatformAdapater } from ".";
|
||||
import { platformAdapater } from ".";
|
||||
import { request, response } from "../interface";
|
||||
import router from "../router";
|
||||
import { router } from "../../";
|
||||
import { headers } from "../interface/headers";
|
||||
|
||||
import http from "http";
|
||||
|
||||
export class NodePlatformAdapter<T = any, K = any> implements PlatformAdapater {
|
||||
constructor(Router: )
|
||||
listen(port: number): void {
|
||||
export class NodePlatformAdapter<T = any, K = any> implements platformAdapater {
|
||||
public router: router<T, K>;
|
||||
|
||||
constructor(router: router<T, K>) {
|
||||
this.router = router;
|
||||
return this;
|
||||
}
|
||||
|
||||
async listen(port: number): Promise<void> {
|
||||
const server = http.createServer();
|
||||
server.on(
|
||||
"request",
|
||||
(req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
const request = this.handleRequest(req);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
handleRequest(request: http.IncomingMessage): request<T> {
|
||||
throw new Error("Method not implemented.");
|
||||
async handleRequest(
|
||||
nativeRequest: http.IncomingMessage
|
||||
): Promise<request<T>> {
|
||||
if (
|
||||
typeof nativeRequest.method != "string" ||
|
||||
typeof nativeRequest.url != "string" ||
|
||||
typeof nativeRequest.headers != "object"
|
||||
) {
|
||||
throw new Error("Invalid request");
|
||||
}
|
||||
|
||||
let body: string = "";
|
||||
const ip: string = nativeRequest.socket.remoteAddress?.replace("::ffff:","") ?? "0.0.0.0";
|
||||
const requestHeaders = new headers(<any>nativeRequest.headers);
|
||||
|
||||
if (!["GET", "HEAD", "DELETE", "OPTIONS"].includes(nativeRequest.method)) {
|
||||
nativeRequest.on("data", (data: string) => {
|
||||
body += data;
|
||||
});
|
||||
|
||||
await new Promise((resolve) =>
|
||||
nativeRequest.on("end", () => {
|
||||
resolve(true);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return new request<T>(
|
||||
nativeRequest.method,
|
||||
new URL(
|
||||
nativeRequest.url,
|
||||
`http://${requestHeaders.get("host") ?? "localhost"}`
|
||||
),
|
||||
requestHeaders,
|
||||
body,
|
||||
{},
|
||||
ip
|
||||
);
|
||||
}
|
||||
|
||||
handleResponse(response: response<K>, NativeResponse: http.ServerResponse) {
|
||||
throw new Error("Method not implemented.");
|
||||
handleResponse(response: response<K>, nativeResponse: http.ServerResponse) {
|
||||
nativeResponse.statusCode = response.status;
|
||||
response.headers.forEach((key, value) => {
|
||||
nativeResponse.setHeader(key, value);
|
||||
});
|
||||
nativeResponse.end(response.body);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user