Add Web Worker Platform Adapter && Fix module path bug

This commit is contained in:
186526 2022-06-29 20:00:52 +00:00 committed by GitHub
parent bdbb5deda9
commit b3552cb6a2
17 changed files with 4765 additions and 56 deletions

17
README.md Normal file
View 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);
```

View File

@ -1,6 +1,6 @@
// 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";
function uuid(): string {
@ -19,7 +19,7 @@ function uuid(): string {
return uuid;
}
const codeAlternative = {
const codeAlternative:{[index: number]:string} = {
100: "Continue",
101: "Switching Protocols",
102: "Processing",
@ -85,7 +85,7 @@ const codeAlternative = {
};
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 (
errorCode: number,

View File

@ -1,16 +1,18 @@
import * as handlerJS from "..";
import {
rootRouter,
method,
handler,
route,
response,
ChainInterrupted,
} from "../index";
import errorHandler from "./errorHandler";
interface requestType {
hood: boolean;
id: number;
}
const App = new handlerJS.rootRouter<requestType, any>();
const App = new rootRouter<any, any>();
App.binding(
"/(.*)",
new handlerJS.handler("ANY", [
new handler("ANY", [
async (request, response) => {
console.log(request);
return undefined;
@ -26,29 +28,29 @@ App.binding(
new Promise((resolve) => {
console.log("Hello World!");
resolve("Hello World!");
throw handlerJS.ChainInterrupted;
throw ChainInterrupted;
})
)
);
App.route("/v1/(.*)")
.add(
new handlerJS.route(
new route(
["/echo", "/echo/(.*)"],
[
new handlerJS.handler(handlerJS.method["GET"], [
async (request, response) => {
response = response ?? new handlerJS.response("");
response?.headers.set("Hello", "World");
response.body = request.url.pathname;
return response;
new handler(method["GET"], [
async (requestMessage, responseMessage) => {
responseMessage = responseMessage ?? new response("");
responseMessage?.headers.set("Hello", "World");
responseMessage.body = requestMessage.url.pathname;
return responseMessage;
},
]),
new handlerJS.handler(handlerJS.method["POST"], [
async (request, response) => {
response = response ?? new handlerJS.response("");
response.body = request.body;
return response;
new handler(method["POST"], [
async (requestMessage, responseMessage) => {
responseMessage = responseMessage ?? new response("");
responseMessage.body = requestMessage.body;
return responseMessage;
},
]),
]
@ -56,11 +58,13 @@ App.route("/v1/(.*)")
)
.binding(
"/error",
App.create(handlerJS.method["ANY"], async () => {
App.create(method["ANY"], async () => {
throw new Error("Nothing will happen here.");
})
)
.useErrorResponder(errorHandler);
App.useAdapater(handlerJS.platformAdapater.NodePlatformAdapter);
App.adapater.listen(8080);
App.useMappingAdapter();
App.listen(8080);
export default App;

View File

@ -6,7 +6,7 @@ export { router } from "./src/router";
export { methodENUM as method } from "./src/interface/method";
export { response } from "./src/interface/response";
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 { rootRouter };

View File

@ -1,8 +1,6 @@
{
"dependencies": {
"path-to-regexp": "^6.2.1"
},
"name": "handlers.js",
"description": "Handlers.js is a unified and lightweight web application framework for multiple platforms.",
"version": "0.0.1",
"main": "index.ts",
"author": "186526 <i@186526.xyz>",
@ -17,9 +15,27 @@
"cross-platform",
"unified"
],
"devDependencies": {
"@types/node": "^18.0.0",
"typescript": "^4.7.4"
"dependencies": {
"path-to-regexp": "^6.2.1"
},
"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"
}
}

View File

@ -1,4 +1,4 @@
import { method, request, responder, response } from "./interface";
import { method, request, responder, response } from "./interface/index";
export class handler<RequestCustomType, ResponseCustomType> {
public responders: responder<RequestCustomType, ResponseCustomType>[];

1
src/index.ts Normal file
View File

@ -0,0 +1 @@
console.log("Hello World!");

View File

@ -9,7 +9,7 @@ export class defaultHeaders extends headers {
this.set("Content-Type", "text/plain; charset=utf-8");
this.set(
"Server",
`Handlers.JS/${packageJSON.version} ${platform}/${version}`
`Handlers.js/${packageJSON.version} ${platform}/${version}`
);
}
}

View File

@ -4,13 +4,17 @@ export const platform = (() => {
if (typeof process != "undefined") {
return "Node.js";
}
return "UNKNOWN";
if (typeof self != "undefined") {
return "Web Worker";
}
return "Unknown";
})();
export const version = (() => {
switch (platform) {
case "Node.js":
return process.version;
default:
return "UNKNOWN";
return "Unknown";
}
})();

View File

@ -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 };

View File

@ -5,7 +5,7 @@ export interface platformAdapater<T = any, K = any> {
router: router<T, K>;
listen(port: number): void;
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> {

View File

@ -1,6 +1,6 @@
import { platformAdapater } from ".";
import { platformAdapater } from "./index";
import { request, response } from "../interface";
import { router } from "../../";
import { router } from "../router";
import { headers } from "../interface/headers";
import http from "http";
@ -10,7 +10,6 @@ export class NodePlatformAdapter<T = any, K = any> implements platformAdapater {
constructor(router: router<T, K>) {
this.router = router;
return this;
}
async listen(port: number): Promise<void> {

View 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)
)
)
}
}

View File

@ -6,15 +6,18 @@ import {
ChainInterrupted,
AllMismatchInterrupted,
responder,
method,
} from "./interface/index";
import { defaultHeaders } from "./interface/response";
import route from "./route";
import { method, methodENUM } from "./interface/method";
import { methodENUM } from "./interface/method";
import {
createPlatformAdapater,
platformAdapaterConstructor,
platformAdapater,
} from "./platform";
} from "./platform/index";
import { platformAdapaterMapping } from "./platform/export";
import { platform } from "./lib";
export class router<K = any, V = any> {
public routes: route[];
@ -208,4 +211,15 @@ export class rootRouter<K = any, V = any> extends router<K, V> {
this.adapater = createPlatformAdapater(adapater, 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);
}
}

View File

@ -1,11 +1,13 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"module": "commonjs",
"lib": [
"ESNext",
"WebWorker",
"DOM"
],
"types": [
"@cloudflare/workers-types",
"@types/node",
],
"skipLibCheck": true,
"sourceMap": true,
@ -16,8 +18,8 @@
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
@ -25,13 +27,19 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": "."
"baseUrl": ".",
"plugins": [
{
"transform": "@zerollup/ts-transform-paths",
}
],
},
"exclude": [
"node_modules"
],
"include": [
"index.ts",
"src/**/*.ts"
]
"src/**/*.ts",
"demo/**/*.ts",
],
}

57
webpack.config.js Normal file
View 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;
};

4533
yarn.lock

File diff suppressed because it is too large Load Diff