-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tonjs): add code of utils and bin of tonjs
- Loading branch information
1 parent
28788a7
commit efd929d
Showing
6 changed files
with
492 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>", | ||
|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.