mirror of
https://github.com/186526/handlers.js
synced 2024-10-13 00:29:43 +00:00
Add Web Worker Platform Adapter && Fix module path bug
This commit is contained in:
17
README.md
Normal file
17
README.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Handlers.js
|
||||||
|
|
||||||
|
> Handlers.js is a unified and lightweight web application framework for multiple platforms.
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import handlerJS from "./";
|
||||||
|
|
||||||
|
const App = new handlerJS();
|
||||||
|
|
||||||
|
App.binding(
|
||||||
|
"/",
|
||||||
|
App.create("ANY", async () => "Hello World!")
|
||||||
|
);
|
||||||
|
|
||||||
|
App.useMappingAdapter();
|
||||||
|
App.listen(8080);
|
||||||
|
```
|
@ -1,6 +1,6 @@
|
|||||||
// From https://gist.github.com/186526/82b7372619fb4b029568040ee4a4aa44, Open source with MIT license
|
// From https://gist.github.com/186526/82b7372619fb4b029568040ee4a4aa44, Open source with MIT license
|
||||||
|
|
||||||
import { response, request } from "..";
|
import { response, request } from "../index";
|
||||||
import { defaultHeaders } from "../src/interface/response";
|
import { defaultHeaders } from "../src/interface/response";
|
||||||
|
|
||||||
function uuid(): string {
|
function uuid(): string {
|
||||||
@ -19,7 +19,7 @@ function uuid(): string {
|
|||||||
|
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
const codeAlternative = {
|
const codeAlternative:{[index: number]:string} = {
|
||||||
100: "Continue",
|
100: "Continue",
|
||||||
101: "Switching Protocols",
|
101: "Switching Protocols",
|
||||||
102: "Processing",
|
102: "Processing",
|
||||||
@ -85,7 +85,7 @@ const codeAlternative = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const template: string =
|
const template: string =
|
||||||
'<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /><meta name="renderer" content="webkit" /><meta name="force-rendering" content="webkit" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /><link rel="preconnect" href="https://fonts.gstatic.com"><link href="https://fonts.googleapis.com/css2?family=Google+Sans&family=Fira+Mono&family=Ubuntu&display=swap" rel="stylesheet"><title>${statusCode} | ${codeAlternative}</title><link href="https://fonts.googleapis.com/icon?family=Material+Icons+Outlined" rel="stylesheet"><style type="text/css">:root{ --background-color: #fff; --font-color: #000; --font-color-lighter: rgb(87, 89, 88); --font-size-main: 3.545rem; --font-size-description: 1.245rem; --box-color: #F2F2F2; --working-color: #137333; --working-color-background: #e6f4ea; --error-color-background: #fce8e6; --error-color: #c5221f; --working-with-error-color: #b05a00; --working-with-error-color-background: #fef7e0; --icon-size: 48px;} body{ margin: 2rem 2rem; font-family: Google Sans, Ubuntu, Roboto, Noto Sans SC, sans-serif; color: var(--font-color); background-color: var(--background-color);} nav{ margin-left: 1rem;} nav description{ font-family: Ubuntu, Roboto, Noto Sans SC, sans-serif; font-size: var(--font-size-description); line-height: var(--fonr-size-description); color: var(--font-color-lighter);} nav main{ font-size: var(--font-size-main); line-height: var(--font-size-main); font-family: Fira Mono, Ubuntu, monospace;} code{ font-family: Fira Mono, monospace;} status{ margin-top: 2.5rem; display: flex; flex-direction: row; flex-wrap: wrap; justify-content: center; align-items: center;} status block.status-block{ background-color: var(--box-color); padding: 2rem; margin: 1rem 1rem; min-height: 3rem; border-radius: 9px; flex-grow: 1;} status block.status-block.working-block{ background-color: var(--working-color-background);} status block.status-block.error-block{ background-color: var(--error-color-background);} status block.status-block.working-with-error-block{ background-color: var(--working-with-error-color-background);} .status-block main{ font-size: calc(var(--font-size-description) + 0.1rem);} .status-working{ color: var(--working-color);} .status-error{ color: var(--error-color);} .status-working-with-error{ color: var(--working-with-error-color);} icon{ font-size: var(--icon-size) !important;} a{ text-decoration: none; color: #1967d2;} reason{ display: flex; flex-direction: row; flex-wrap: wrap; justify-content: space-between; align-items: center;} reason>*{ display: block; margin: 1rem; flex-grow: 1; max-width: 40%;} reason main{ font-size: calc(var(--font-size-description) + 0.2rem); font-weight: 550;} footer{ margin: 1rem; color: var(--font-color-lighter); font-size: calc(var(--font-size-description) - 0.4rem);} footer>request-status{ font-size: calc(var(--font-size-description) - 0.6rem);} footer>*{ display: block;} @media screen and (max-width:480px){ body{ margin: 6rem 2rem;} :root{ --font-size-main: 3.0rem; --font-size-description: 1.045rem;} reason>*{ max-width: 100%;} footer{ font-size: calc(var(--font-size-description) - 0.2rem);} footer>request-status{ font-size: calc(var(--font-size-description) - 0.4rem);}} @media screen and (min-width: 768px){ body{ margin: 8% 10%;} nav *{ display: inline-block; margin-left: 1%;}} @media (prefers-color-scheme: dark){ :root{ --font-color: rgba(255, 255, 255, 0.86); --font-color-lighter: rgba(255, 255, 255, 0.4); --background-color: rgb(0, 0, 0); --box-color: rgb(40 40 40 / 73%); --working-color-background: #07220f; --error-color-background: #270501; --working-with-error-color-background: #392605;}} </style></head><body><nav><main>${statusCode}</main><description>${codeAlternative}</description></nav><status><block class="status-block ${clientStatusShort}-block" id="client-status-block"><icon class="material-icons-outlined status-${clientStatusShort}">web</icon><main>Your Client</main><status-text class="status-${clientStatusShort}">${clientStatus}</status-text></block><block class="status-block working-block" id="edge-status-block"><icon class="material-icons-outlined status-working">alt_route</icon><main>Handlers.JS</main><status-text class="status-working">Working </block><block class="status-block ${ServerStatusLowerCase}-block" id="website-status-block"><icon class="material-icons-outlined status-${ServerStatusLowerCase}">widgets</icon><main>Route</main><status-text class="status-${ServerStatusLowerCase}">${ServerStatus}</status-text></block></status><reason><explain><main>${codeAlternative}</main><p>${explain} </p></explain><howto><main>What can I do?</main><p>${howto} </p></howto></reason><footer><provider>Running with <a href="https://git.186526.xyz/186526/handlers.js">Handlers.JS</a>.</provider><br><request-status>Your IP is <code>${ip}</code><br>Request ID is <code>${id}</code></request-status></footer></body></html>';
|
'<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /><meta name="renderer" content="webkit" /><meta name="force-rendering" content="webkit" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /><link rel="preconnect" href="https://fonts.gstatic.com"><link href="https://fonts.googleapis.com/css2?family=Google+Sans&family=Fira+Mono&family=Ubuntu&display=swap" rel="stylesheet"><title>${statusCode} | ${codeAlternative}</title><link href="https://fonts.googleapis.com/icon?family=Material+Icons+Outlined" rel="stylesheet"><style type="text/css">:root{ --background-color: #fff; --font-color: #000; --font-color-lighter: rgb(87, 89, 88); --font-size-main: 3.545rem; --font-size-description: 1.245rem; --box-color: #F2F2F2; --working-color: #137333; --working-color-background: #e6f4ea; --error-color-background: #fce8e6; --error-color: #c5221f; --working-with-error-color: #b05a00; --working-with-error-color-background: #fef7e0; --icon-size: 48px;} body{ margin: 2rem 2rem; font-family: Google Sans, Ubuntu, Roboto, Noto Sans SC, sans-serif; color: var(--font-color); background-color: var(--background-color);} nav{ margin-left: 1rem;} nav description{ font-family: Ubuntu, Roboto, Noto Sans SC, sans-serif; font-size: var(--font-size-description); line-height: var(--fonr-size-description); color: var(--font-color-lighter);} nav main{ font-size: var(--font-size-main); line-height: var(--font-size-main); font-family: Fira Mono, Ubuntu, monospace;} code{ font-family: Fira Mono, monospace;} status{ margin-top: 2.5rem; display: flex; flex-direction: row; flex-wrap: wrap; justify-content: center; align-items: center;} status block.status-block{ background-color: var(--box-color); padding: 2rem; margin: 1rem 1rem; min-height: 3rem; border-radius: 9px; flex-grow: 1;} status block.status-block.working-block{ background-color: var(--working-color-background);} status block.status-block.error-block{ background-color: var(--error-color-background);} status block.status-block.working-with-error-block{ background-color: var(--working-with-error-color-background);} .status-block main{ font-size: calc(var(--font-size-description) + 0.1rem);} .status-working{ color: var(--working-color);} .status-error{ color: var(--error-color);} .status-working-with-error{ color: var(--working-with-error-color);} icon{ font-size: var(--icon-size) !important;} a{ text-decoration: none; color: #1967d2;} reason{ display: flex; flex-direction: row; flex-wrap: wrap; justify-content: space-between; align-items: center;} reason>*{ display: block; margin: 1rem; flex-grow: 1; max-width: 40%;} reason main{ font-size: calc(var(--font-size-description) + 0.2rem); font-weight: 550;} footer{ margin: 1rem; color: var(--font-color-lighter); font-size: calc(var(--font-size-description) - 0.4rem);} footer>request-status{ font-size: calc(var(--font-size-description) - 0.6rem);} footer>*{ display: block;} @media screen and (max-width:480px){ body{ margin: 6rem 2rem;} :root{ --font-size-main: 3.0rem; --font-size-description: 1.045rem;} reason>*{ max-width: 100%;} footer{ font-size: calc(var(--font-size-description) - 0.2rem);} footer>request-status{ font-size: calc(var(--font-size-description) - 0.4rem);}} @media screen and (min-width: 768px){ body{ margin: 8% 10%;} nav *{ display: inline-block; margin-left: 1%;}} @media (prefers-color-scheme: dark){ :root{ --font-color: rgba(255, 255, 255, 0.86); --font-color-lighter: rgba(255, 255, 255, 0.4); --background-color: rgb(0, 0, 0); --box-color: rgb(40 40 40 / 73%); --working-color-background: #07220f; --error-color-background: #270501; --working-with-error-color-background: #392605;}} </style></head><body><nav><main>${statusCode}</main><description>${codeAlternative}</description></nav><status><block class="status-block ${clientStatusShort}-block" id="client-status-block"><icon class="material-icons-outlined status-${clientStatusShort}">web</icon><main>Your Client</main><status-text class="status-${clientStatusShort}">${clientStatus}</status-text></block><block class="status-block working-block" id="edge-status-block"><icon class="material-icons-outlined status-working">alt_route</icon><main>Handlers.js</main><status-text class="status-working">Working </block><block class="status-block ${ServerStatusLowerCase}-block" id="website-status-block"><icon class="material-icons-outlined status-${ServerStatusLowerCase}">widgets</icon><main>Route</main><status-text class="status-${ServerStatusLowerCase}">${ServerStatus}</status-text></block></status><reason><explain><main>${codeAlternative}</main><p>${explain} </p></explain><howto><main>What can I do?</main><p>${howto} </p></howto></reason><footer><provider>Running with <a href="https://git.186526.xyz/186526/Handlers.js">Handlers.js</a>.</provider><br><request-status>Your IP is <code>${ip}</code><br>Request ID is <code>${id}</code></request-status></footer></body></html>';
|
||||||
|
|
||||||
export default (
|
export default (
|
||||||
errorCode: number,
|
errorCode: number,
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
import * as handlerJS from "..";
|
import {
|
||||||
|
rootRouter,
|
||||||
|
method,
|
||||||
|
handler,
|
||||||
|
route,
|
||||||
|
response,
|
||||||
|
ChainInterrupted,
|
||||||
|
} from "../index";
|
||||||
import errorHandler from "./errorHandler";
|
import errorHandler from "./errorHandler";
|
||||||
|
|
||||||
interface requestType {
|
const App = new rootRouter<any, any>();
|
||||||
hood: boolean;
|
|
||||||
id: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const App = new handlerJS.rootRouter<requestType, any>();
|
|
||||||
|
|
||||||
App.binding(
|
App.binding(
|
||||||
"/(.*)",
|
"/(.*)",
|
||||||
new handlerJS.handler("ANY", [
|
new handler("ANY", [
|
||||||
async (request, response) => {
|
async (request, response) => {
|
||||||
console.log(request);
|
console.log(request);
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -26,29 +28,29 @@ App.binding(
|
|||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
console.log("Hello World!");
|
console.log("Hello World!");
|
||||||
resolve("Hello World!");
|
resolve("Hello World!");
|
||||||
throw handlerJS.ChainInterrupted;
|
throw ChainInterrupted;
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
App.route("/v1/(.*)")
|
App.route("/v1/(.*)")
|
||||||
.add(
|
.add(
|
||||||
new handlerJS.route(
|
new route(
|
||||||
["/echo", "/echo/(.*)"],
|
["/echo", "/echo/(.*)"],
|
||||||
[
|
[
|
||||||
new handlerJS.handler(handlerJS.method["GET"], [
|
new handler(method["GET"], [
|
||||||
async (request, response) => {
|
async (requestMessage, responseMessage) => {
|
||||||
response = response ?? new handlerJS.response("");
|
responseMessage = responseMessage ?? new response("");
|
||||||
response?.headers.set("Hello", "World");
|
responseMessage?.headers.set("Hello", "World");
|
||||||
response.body = request.url.pathname;
|
responseMessage.body = requestMessage.url.pathname;
|
||||||
return response;
|
return responseMessage;
|
||||||
},
|
},
|
||||||
]),
|
]),
|
||||||
new handlerJS.handler(handlerJS.method["POST"], [
|
new handler(method["POST"], [
|
||||||
async (request, response) => {
|
async (requestMessage, responseMessage) => {
|
||||||
response = response ?? new handlerJS.response("");
|
responseMessage = responseMessage ?? new response("");
|
||||||
response.body = request.body;
|
responseMessage.body = requestMessage.body;
|
||||||
return response;
|
return responseMessage;
|
||||||
},
|
},
|
||||||
]),
|
]),
|
||||||
]
|
]
|
||||||
@ -56,11 +58,13 @@ App.route("/v1/(.*)")
|
|||||||
)
|
)
|
||||||
.binding(
|
.binding(
|
||||||
"/error",
|
"/error",
|
||||||
App.create(handlerJS.method["ANY"], async () => {
|
App.create(method["ANY"], async () => {
|
||||||
throw new Error("Nothing will happen here.");
|
throw new Error("Nothing will happen here.");
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.useErrorResponder(errorHandler);
|
.useErrorResponder(errorHandler);
|
||||||
|
|
||||||
App.useAdapater(handlerJS.platformAdapater.NodePlatformAdapter);
|
App.useMappingAdapter();
|
||||||
App.adapater.listen(8080);
|
App.listen(8080);
|
||||||
|
|
||||||
|
export default App;
|
2
index.ts
2
index.ts
@ -6,7 +6,7 @@ export { router } from "./src/router";
|
|||||||
export { methodENUM as method } from "./src/interface/method";
|
export { methodENUM as method } from "./src/interface/method";
|
||||||
export { response } from "./src/interface/response";
|
export { response } from "./src/interface/response";
|
||||||
export { request } from "./src/interface/request";
|
export { request } from "./src/interface/request";
|
||||||
export { ChainInterrupted } from "./src/interface";
|
export { ChainInterrupted } from "./src/interface/index";
|
||||||
export * as platformAdapater from "./src/platform/export";
|
export * as platformAdapater from "./src/platform/export";
|
||||||
|
|
||||||
export { rootRouter };
|
export { rootRouter };
|
||||||
|
30
package.json
30
package.json
@ -1,8 +1,6 @@
|
|||||||
{
|
{
|
||||||
"dependencies": {
|
|
||||||
"path-to-regexp": "^6.2.1"
|
|
||||||
},
|
|
||||||
"name": "handlers.js",
|
"name": "handlers.js",
|
||||||
|
"description": "Handlers.js is a unified and lightweight web application framework for multiple platforms.",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"main": "index.ts",
|
"main": "index.ts",
|
||||||
"author": "186526 <i@186526.xyz>",
|
"author": "186526 <i@186526.xyz>",
|
||||||
@ -17,9 +15,27 @@
|
|||||||
"cross-platform",
|
"cross-platform",
|
||||||
"unified"
|
"unified"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"dependencies": {
|
||||||
"@types/node": "^18.0.0",
|
"path-to-regexp": "^6.2.1"
|
||||||
"typescript": "^4.7.4"
|
|
||||||
},
|
},
|
||||||
"type": "module"
|
"devDependencies": {
|
||||||
|
"@cloudflare/workers-types": "^3.13.0",
|
||||||
|
"@types/node": "^18.0.0",
|
||||||
|
"@webpack-cli/generators": "^2.5.0",
|
||||||
|
"prettier": "^2.7.1",
|
||||||
|
"ts-loader": "^9.3.1",
|
||||||
|
"typescript": "^4.7.4",
|
||||||
|
"webpack": "^5.73.0",
|
||||||
|
"webpack-cli": "^4.10.0"
|
||||||
|
},
|
||||||
|
"sideEffects": false,
|
||||||
|
"scripts": {
|
||||||
|
"build": "yarn build:node && yarn build:webworker",
|
||||||
|
"build:node": "TARGET=node webpack",
|
||||||
|
"build:webworker": "TARGET=webworker webpack",
|
||||||
|
"watch": "webpack --watch"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.0.0"
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { method, request, responder, response } from "./interface";
|
import { method, request, responder, response } from "./interface/index";
|
||||||
|
|
||||||
export class handler<RequestCustomType, ResponseCustomType> {
|
export class handler<RequestCustomType, ResponseCustomType> {
|
||||||
public responders: responder<RequestCustomType, ResponseCustomType>[];
|
public responders: responder<RequestCustomType, ResponseCustomType>[];
|
||||||
|
1
src/index.ts
Normal file
1
src/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
console.log("Hello World!");
|
@ -9,7 +9,7 @@ export class defaultHeaders extends headers {
|
|||||||
this.set("Content-Type", "text/plain; charset=utf-8");
|
this.set("Content-Type", "text/plain; charset=utf-8");
|
||||||
this.set(
|
this.set(
|
||||||
"Server",
|
"Server",
|
||||||
`Handlers.JS/${packageJSON.version} ${platform}/${version}`
|
`Handlers.js/${packageJSON.version} ${platform}/${version}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,17 @@ export const platform = (() => {
|
|||||||
if (typeof process != "undefined") {
|
if (typeof process != "undefined") {
|
||||||
return "Node.js";
|
return "Node.js";
|
||||||
}
|
}
|
||||||
return "UNKNOWN";
|
if (typeof self != "undefined") {
|
||||||
|
return "Web Worker";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Unknown";
|
||||||
})();
|
})();
|
||||||
export const version = (() => {
|
export const version = (() => {
|
||||||
switch (platform) {
|
switch (platform) {
|
||||||
case "Node.js":
|
case "Node.js":
|
||||||
return process.version;
|
return process.version;
|
||||||
default:
|
default:
|
||||||
return "UNKNOWN";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
@ -1 +1,9 @@
|
|||||||
export { NodePlatformAdapter } from "./node";
|
import { NodePlatformAdapter } from "./node";
|
||||||
|
import { SWPlatformAdapter } from "./serviceworker";
|
||||||
|
|
||||||
|
export const platformAdapaterMapping = {
|
||||||
|
"Node.js": NodePlatformAdapter,
|
||||||
|
"Web Worker": SWPlatformAdapter,
|
||||||
|
};
|
||||||
|
|
||||||
|
export { NodePlatformAdapter, SWPlatformAdapter };
|
||||||
|
@ -5,7 +5,7 @@ export interface platformAdapater<T = any, K = any> {
|
|||||||
router: router<T, K>;
|
router: router<T, K>;
|
||||||
listen(port: number): void;
|
listen(port: number): void;
|
||||||
handleRequest(nativeRequest: any): Promise<request<T>>;
|
handleRequest(nativeRequest: any): Promise<request<T>>;
|
||||||
handleResponse(response: response<K>, nativeResponse?: any): any;
|
handleResponse(response: response<K> | Promise<response<K>>, nativeResponse?: any): any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface platformAdapaterConstructor<T = any, K = any> {
|
export interface platformAdapaterConstructor<T = any, K = any> {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { platformAdapater } from ".";
|
import { platformAdapater } from "./index";
|
||||||
import { request, response } from "../interface";
|
import { request, response } from "../interface";
|
||||||
import { router } from "../../";
|
import { router } from "../router";
|
||||||
import { headers } from "../interface/headers";
|
import { headers } from "../interface/headers";
|
||||||
|
|
||||||
import http from "http";
|
import http from "http";
|
||||||
@ -10,7 +10,6 @@ export class NodePlatformAdapter<T = any, K = any> implements platformAdapater {
|
|||||||
|
|
||||||
constructor(router: router<T, K>) {
|
constructor(router: router<T, K>) {
|
||||||
this.router = router;
|
this.router = router;
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async listen(port: number): Promise<void> {
|
async listen(port: number): Promise<void> {
|
||||||
|
50
src/platform/serviceworker.ts
Normal file
50
src/platform/serviceworker.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import { platformAdapater } from "./index";
|
||||||
|
import { request } from "../interface/request";
|
||||||
|
import { response } from "../interface/response";
|
||||||
|
import { router } from "../router";
|
||||||
|
import { headers } from "../interface/headers";
|
||||||
|
|
||||||
|
export class SWPlatformAdapter<T = any, K = any> implements platformAdapater {
|
||||||
|
public router: router<T, K>;
|
||||||
|
|
||||||
|
constructor(router: router<T, K>) {
|
||||||
|
this.router = router;
|
||||||
|
}
|
||||||
|
|
||||||
|
async listen(_port?: number): Promise<void> {
|
||||||
|
self.addEventListener("fetch", (event: FetchEvent) => {
|
||||||
|
event.respondWith(this.handler(event));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleRequest(nativeRequest: Request): Promise<request<T>> {
|
||||||
|
const requestHeaders = new headers(
|
||||||
|
Object.fromEntries(nativeRequest.headers.entries())
|
||||||
|
);
|
||||||
|
const requestMessage: request<T> = new request(
|
||||||
|
nativeRequest.method,
|
||||||
|
new URL(nativeRequest.url),
|
||||||
|
requestHeaders,
|
||||||
|
await nativeRequest.text(),
|
||||||
|
{},
|
||||||
|
requestHeaders.get("CF-Connecting-IP") || ""
|
||||||
|
);
|
||||||
|
return requestMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleResponse(response: response<K>): Promise<Response> {
|
||||||
|
const nativResponse = new Response(response.body, {
|
||||||
|
status: response.status,
|
||||||
|
headers: response.headers.headers,
|
||||||
|
});
|
||||||
|
return nativResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
async handler(event: FetchEvent): Promise<Response> {
|
||||||
|
return await this.handleResponse(
|
||||||
|
await this.handleRequest(event.request).then((request) =>
|
||||||
|
this.router.respond(request)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -6,15 +6,18 @@ import {
|
|||||||
ChainInterrupted,
|
ChainInterrupted,
|
||||||
AllMismatchInterrupted,
|
AllMismatchInterrupted,
|
||||||
responder,
|
responder,
|
||||||
|
method,
|
||||||
} from "./interface/index";
|
} from "./interface/index";
|
||||||
import { defaultHeaders } from "./interface/response";
|
import { defaultHeaders } from "./interface/response";
|
||||||
import route from "./route";
|
import route from "./route";
|
||||||
import { method, methodENUM } from "./interface/method";
|
import { methodENUM } from "./interface/method";
|
||||||
import {
|
import {
|
||||||
createPlatformAdapater,
|
createPlatformAdapater,
|
||||||
platformAdapaterConstructor,
|
platformAdapaterConstructor,
|
||||||
platformAdapater,
|
platformAdapater,
|
||||||
} from "./platform";
|
} from "./platform/index";
|
||||||
|
import { platformAdapaterMapping } from "./platform/export";
|
||||||
|
import { platform } from "./lib";
|
||||||
|
|
||||||
export class router<K = any, V = any> {
|
export class router<K = any, V = any> {
|
||||||
public routes: route[];
|
public routes: route[];
|
||||||
@ -208,4 +211,15 @@ export class rootRouter<K = any, V = any> extends router<K, V> {
|
|||||||
this.adapater = createPlatformAdapater(adapater, this);
|
this.adapater = createPlatformAdapater(adapater, this);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
useMappingAdapter(
|
||||||
|
mapping: { [platform: string]: platformAdapaterConstructor } = platformAdapaterMapping
|
||||||
|
): this {
|
||||||
|
if(mapping[platform] == undefined) throw new Error("Platform not found in mapping");
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
"module": "esnext",
|
"module": "commonjs",
|
||||||
"lib": [
|
"lib": [
|
||||||
"ESNext",
|
"ESNext",
|
||||||
"WebWorker",
|
],
|
||||||
"DOM"
|
"types": [
|
||||||
|
"@cloudflare/workers-types",
|
||||||
|
"@types/node",
|
||||||
],
|
],
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
@ -16,8 +18,8 @@
|
|||||||
"strictNullChecks": true,
|
"strictNullChecks": true,
|
||||||
"strictFunctionTypes": true,
|
"strictFunctionTypes": true,
|
||||||
"noImplicitThis": true,
|
"noImplicitThis": true,
|
||||||
"noUnusedLocals": true,
|
"noUnusedLocals": false,
|
||||||
"noUnusedParameters": true,
|
"noUnusedParameters": false,
|
||||||
"noImplicitReturns": true,
|
"noImplicitReturns": true,
|
||||||
"noFallthroughCasesInSwitch": true,
|
"noFallthroughCasesInSwitch": true,
|
||||||
"allowSyntheticDefaultImports": true,
|
"allowSyntheticDefaultImports": true,
|
||||||
@ -25,13 +27,19 @@
|
|||||||
"emitDecoratorMetadata": true,
|
"emitDecoratorMetadata": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"baseUrl": "."
|
"baseUrl": ".",
|
||||||
|
"plugins": [
|
||||||
|
{
|
||||||
|
"transform": "@zerollup/ts-transform-paths",
|
||||||
|
}
|
||||||
|
],
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules"
|
"node_modules"
|
||||||
],
|
],
|
||||||
"include": [
|
"include": [
|
||||||
"index.ts",
|
"index.ts",
|
||||||
"src/**/*.ts"
|
"src/**/*.ts",
|
||||||
]
|
"demo/**/*.ts",
|
||||||
|
],
|
||||||
}
|
}
|
57
webpack.config.js
Normal file
57
webpack.config.js
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// Generated using webpack-cli https://github.com/webpack/webpack-cli
|
||||||
|
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
const isProduction = process.env.NODE_ENV == "production";
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
output: {
|
||||||
|
path: path.resolve(__dirname, "dist"),
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
// Add your plugins here
|
||||||
|
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
|
||||||
|
],
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
test: /\.(ts|tsx)$/i,
|
||||||
|
loader: "ts-loader",
|
||||||
|
exclude: ["/node_modules/"],
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add your rules for custom modules here
|
||||||
|
// Learn more about loaders from https://webpack.js.org/loaders/
|
||||||
|
],
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
extensions: [".tsx", ".ts", ".jsx", ".js", "..."],
|
||||||
|
fallback: {
|
||||||
|
"http": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = () => {
|
||||||
|
if (isProduction) {
|
||||||
|
config.mode = "production";
|
||||||
|
config.entry = "./index.ts";
|
||||||
|
} else {
|
||||||
|
config.mode = "development";
|
||||||
|
config.entry = "./demo/index.ts";
|
||||||
|
}
|
||||||
|
switch(process.env.TARGET) {
|
||||||
|
case "node":
|
||||||
|
config.target = "node14";
|
||||||
|
config.output.filename = "main.node.js";
|
||||||
|
break;
|
||||||
|
case "webworker":
|
||||||
|
config.target = "webworker";
|
||||||
|
config.output.filename = "main.webworker.js";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
config.target = "es6";
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
};
|
Reference in New Issue
Block a user