From 4d03482f8a89805b84dd45c6c49bfc404c53dc08 Mon Sep 17 00:00:00 2001 From: "TZU-YEN, CHANG" Date: Wed, 4 Mar 2020 01:06:36 +0800 Subject: [PATCH] feat: add the createApp and listen app for server manual setup --- packages/tonjs/example/manual.ts | 27 +++++++++++++++++++ packages/tonjs/package.json | 2 +- packages/tonjs/src/bin.ts | 45 ++++++++++++++++---------------- packages/tonjs/src/index.ts | 36 +++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 24 deletions(-) create mode 100644 packages/tonjs/example/manual.ts diff --git a/packages/tonjs/example/manual.ts b/packages/tonjs/example/manual.ts new file mode 100644 index 0000000..450c440 --- /dev/null +++ b/packages/tonjs/example/manual.ts @@ -0,0 +1,27 @@ +import { + send, + redirect, + createApp, + listen, + registerGracefulShutdown, + route +} from '../src/index' + +const host = '0.0.0.0' +const port = 3000 + +async function main() { + const app = createApp() + route(app, 'GET', '/', (req, res) => send(res, 200, 'TonJS')) + route(app, 'GET', '/redirect', (req, res) => + redirect(res, 302, 'https://tonjs.com') + ) + const token = await listen(app, host, port) + registerGracefulShutdown(token) + // eslint-disable-next-line + console.info( + `\nyou raise me up, to listen on http://${host}:${port}\n` + ) +} + +main() diff --git a/packages/tonjs/package.json b/packages/tonjs/package.json index 3bc1a17..a5c6647 100644 --- a/packages/tonjs/package.json +++ b/packages/tonjs/package.json @@ -4,12 +4,12 @@ "description": "The Node.js Web Framework For A Ton Of Request", "bin": "dist/bind.js", "main": "dist/index.js", + "type": "dist/index.d.ts", "repository": "git@github.com:AllJointTW/TonJS.git", "author": "TZU-YEN, CHANG ", "license": "GPL-3.0", "scripts": { "dev": "nodemon --watch '{src,example}/**/*.ts' --ignore '{src,example}/**/*.{spec,test}.ts' --exec 'ts-node' src/bin.ts", - "dev:js": "nodemon --watch '{dist,example}/**/*.js' --ignore '{dist,example}/**/*.{spec,test}.js' dist/bin.js", "format": ":", "lint": ":", "build": "tsc" diff --git a/packages/tonjs/src/bin.ts b/packages/tonjs/src/bin.ts index 7c2b4a2..0bb58d6 100644 --- a/packages/tonjs/src/bin.ts +++ b/packages/tonjs/src/bin.ts @@ -1,6 +1,7 @@ import path from 'path' -import uWS from 'uWebSockets.js' import { + createApp, + listen, route, TonHandler, TonListenSocket, @@ -35,32 +36,35 @@ const { argv } = yargs ssl: { type: 'boolean', desc: 'Need SSL?', - implies: ['ssl-key', 'ssl-cert'] + implies: ['key', 'cert'] }, - 'ssl-key': { + key: { type: 'string', desc: 'Specify the path of key of SSL' }, - 'ssl-cert': { + cert: { type: 'string', desc: 'Specify the path of cert of SSL' }, passphrase: { type: 'string', desc: 'Specify the passphrase of SSL cert' + }, + dhParams: { + type: 'string', + desc: 'Specify the path of params.dh of SSL' + }, + preferLowMemoryUsage: { + type: 'boolean', + desc: + 'Translate SSL to buffer, not only low memory, but also low performance' } }) async function main() { try { const [entry = 'index.js'] = argv._ - const app = argv.ssl - ? uWS.SSLApp({ - cert_file_name: argv['ssl-cert'], // eslint-disable-line - key_file_name: argv['ssl-key'], // eslint-disable-line - passphrase: argv.passphrase - }) - : uWS.App() + const app = createApp(argv) const endpoints: TonHandler | TonRoutes = ( await import(path.resolve(process.cwd(), entry)) ).default @@ -83,19 +87,14 @@ async function main() { route(app, 'ANY', '*', endpoints as TonHandler) } - app.listen(argv.host, argv.port, (token: TonListenSocket) => { - if (!token) { - // eslint-disable-next-line - console.info(`\nfailed to listen on ${argv.host}:${argv.port}`) - return - } - // eslint-disable-next-line - console.info( - `\nyou raise me up, to listen on http://${argv.host}:${argv.port}` - ) - registerGracefulShutdown(token) - }) + const token = await listen(app, argv.host, argv.port) + registerGracefulShutdown(token) + // eslint-disable-next-line + console.info( + `\nyou raise me up, to listen on http://${argv.host}:${argv.port}\n` + ) } catch (err) { + console.info(`\nfailed to listen on ${argv.host}:${argv.port}\n`) // eslint-disable-line console.error(err) // eslint-disable-line } } diff --git a/packages/tonjs/src/index.ts b/packages/tonjs/src/index.ts index e82a382..bc8f6b9 100644 --- a/packages/tonjs/src/index.ts +++ b/packages/tonjs/src/index.ts @@ -5,6 +5,14 @@ import bytes from 'bytes' import { readable } from 'is-stream' export type TonApp = uWS.TemplatedApp +export type TonAppOptions = { + ssl?: boolean + key?: string + cert?: string + passphrase?: string + dhParams?: string + preferLowMemoryUsage?: boolean +} export type TonRequest = uWS.HttpRequest export type TonResponse = uWS.HttpResponse & { aborted: boolean } export type TonStream = stream.Readable & { size?: number } @@ -294,3 +302,31 @@ export function registerGracefulShutdown(socket: TonListenSocket) { process.on('SIGTERM', wrapper) process.on('exit', wrapper) } + +export function createApp(options: TonAppOptions = {}): TonApp { + if (options.ssl) { + return uWS.SSLApp({ + key_file_name: options.key, // eslint-disable-line + cert_file_name: options.cert, // eslint-disable-line + passphrase: options.passphrase, + dh_params_file_name: options.dhParams, // eslint-disable-line + ssl_prefer_low_memory_usage: options.preferLowMemoryUsage // eslint-disable-line + }) + } + return uWS.App() +} + +export function listen( + app: TonApp, + host: string, + port: number +): Promise { + return new Promise((resolve, reject) => { + app.listen(host, port, (token: TonListenSocket) => { + if (!token) { + return reject() + } + return resolve(token) + }) + }) +}