Skip to content

Commit

Permalink
feat(tonjs): add code of utils and bin of tonjs
Browse files Browse the repository at this point in the history
  • Loading branch information
trylovetom committed Mar 1, 2020
1 parent 28788a7 commit efd929d
Show file tree
Hide file tree
Showing 6 changed files with 492 additions and 45 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
"packages/*"
],
"scripts": {
"dev": "nodemon --watch 'packages/**/*.ts' --ignore 'packages/**/*.{spec,test}.ts' --exec 'ts-node' packages/tonjs/src/index.ts",
"dev": "nodemon --watch 'packages/**/*.ts' --ignore 'packages/**/*.{spec,test}.ts' --exec 'ts-node' packages/tonjs/src/bin.ts example/bin.ts",
"format": "prettier **/*.{js,json,ts,md,yaml} !**/dist/** !./dist/** --write && yarn lint --fix",
"lint": "eslint **/*.ts"
},
"devDependencies": {
"@alljoint-next/eslint-config-typescript": "^0.1.3",
"@alljoint-next/eslint-config-typescript": "^0.2.0",
"@alljoint-next/ts-config": "^0.1.2",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@types/node": "^13.7.4",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"husky": "^4.2.3",
"lerna": "3.20.2",
"nodemon": "^2.0.2",
Expand Down
13 changes: 13 additions & 0 deletions packages/tonjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable */
const { send, redirect } = require("./dist");

module.exports = {
"GET /": (req, res) => send(res, 200, "TonJS!"),
"GET /redirect-to-alljoint.tw": function redirect302(req, res) {
redirect(res, 302, "https://alljoint.tw");
}
};

// module.exports = function redirectToAlljoint(req, res) {
// redirect(res, 301, "https://alljoint.tw");
// };
12 changes: 10 additions & 2 deletions packages/tonjs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@alljoint-next/tonjs",
"version": "0.0.0",
"description": "The Node.js Web Framework Can Do A Ton Of Request",
"description": "The Node.js Web Framework For A Ton Of Request",
"bin": "dist/bind.js",
"main": "dist/index.js",
"repository": "[email protected]:AllJointTW/TonJS.git",
"author": "TZU-YEN, CHANG <[email protected]>",
Expand All @@ -12,6 +13,13 @@
"build": "tsc"
},
"dependencies": {
"uWebSockets.js": "uNetworking/uWebSockets.js#v17.1.0"
"bytes": "^3.1.0",
"is-stream": "^2.0.0",
"uWebSockets.js": "uNetworking/uWebSockets.js#v17.1.0",
"yargs": "^15.2.0"
},
"devDependencies": {
"@types/bytes": "^3.1.0",
"@types/yargs": "^15.0.4"
}
}
69 changes: 69 additions & 0 deletions packages/tonjs/src/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import path from "path";
import uWS from "uWebSockets.js";
import { route, TonHandler, TonMethods } from "./index";

import yargs = require("yargs");

const { argv } = yargs
.scriptName("ton")
.usage("Usage: $0 <entry> <options>")
.help()
.version()
.alias("version", "v")
.epilogue("for more information, find our docs at https://tonjs.com")
.example("$0 index.js", "listen on 0.0.0.0:3000 and index.js as the entry.")
.locale("en")
.options({
host: {
type: "string",
alias: "h",
desc: "Specify the host name",
default: "0.0.0.0"
},
port: {
type: "number",
alias: "p",
desc: "Specify the port number",
default: 3000
}
});

async function main() {
const [entry = "index.js"] = argv._;
const app = uWS.App();
const endpoints: TonHandler | { [pattern: string]: TonHandler } = (
await import(path.resolve(process.cwd(), entry))
).default;
const httpPattern = /(^GET|POST|OPTIONS|DEL|PATCH|PUT|HEAD|CONNECT|TRACE|ANY|WS|PUBLISH)\s+(\S+)/;

console.info("\nroutes:"); // eslint-disable-line
if (typeof endpoints === "object" && endpoints !== null) {
Object.keys(endpoints).forEach(key => {
const results = key.match(httpPattern);
if (!results) {
throw new Error(`routes: can't parse ${key}`);
}
const [, methods, pattern] = results;
let handlerName = endpoints[key].name || "anonymous";
if (handlerName === key) {
handlerName = "anonymous";
}
console.info(` ${key} => ${handlerName}()`); // eslint-disable-line
route(app, methods as TonMethods, pattern, endpoints[key] as TonHandler);
});
} else {
const handlerName = endpoints.name || "anonymous";
console.info(` * => ${handlerName}()`); // eslint-disable-line
route(app, "ANY", "*", endpoints as TonHandler);
}

app.listen(argv.host, argv.port, token => {
if (!token) {
console.info(`\nfailed to listen on ${argv.host}:${argv.port}`); // eslint-disable-line
return;
}
console.info(`\nyou raise me up, to listen on ${argv.host}:${argv.port}`); // eslint-disable-line
});
}

main();
Loading

0 comments on commit efd929d

Please sign in to comment.