From ff8e185ec3e48eac3f76336303b09670131c8c3f Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Wed, 21 Aug 2019 23:41:06 +0200 Subject: [PATCH] fix(core): update --- package-lock.json | 29 ++++++++++++++-- package.json | 6 +++- test/test.ts | 13 ++++++-- ts/smartproxy.classes.smartproxy.ts | 51 ++++++++++++++++++++++++----- ts/smartproxy.plugins.ts | 18 +++++++--- 5 files changed, 99 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 37dab55..de41a82 100644 --- a/package-lock.json +++ b/package-lock.json @@ -318,6 +318,11 @@ "smartchai": "^2.0.1" } }, + "@tsclass/tsclass": { + "version": "2.0.3", + "resolved": "https://verdaccio.lossless.one/@tsclass%2ftsclass/-/tsclass-2.0.3.tgz", + "integrity": "sha512-13bn6eLaNN2gM5+ycZjulacEzpruOVOCcEMn1+2CZ8UAZf6yhghq+hXEUxqmuf1ZVR1CF+k9HsArxFBSh1cnUQ==" + }, "@types/chai": { "version": "4.2.0", "resolved": "https://verdaccio.lossless.one/@types%2fchai/-/chai-4.2.0.tgz", @@ -393,8 +398,7 @@ "@types/node": { "version": "12.7.2", "resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-12.7.2.tgz", - "integrity": "sha512-dyYO+f6ihZEtNPDcWNR1fkoTDf3zAK3lAABDze3mz6POyIercH0lEUawUFXlG8xaQZmm1yEBON/4TsYv/laDYg==", - "dev": true + "integrity": "sha512-dyYO+f6ihZEtNPDcWNR1fkoTDf3zAK3lAABDze3mz6POyIercH0lEUawUFXlG8xaQZmm1yEBON/4TsYv/laDYg==" }, "@types/vinyl": { "version": "2.0.3", @@ -411,6 +415,14 @@ "integrity": "sha512-ZrJDWpvg75LTGX4XwuneY9s6bF3OeZcGTpoGh3zDV9ytzcHMFsRrMIaLBRJZQMBoGyKs6unBQfVdrLZiYfb1zQ==", "dev": true }, + "@types/ws": { + "version": "6.0.2", + "resolved": "https://verdaccio.lossless.one/@types%2fws/-/ws-6.0.2.tgz", + "integrity": "sha512-22XiR1ox9LftTaAtn/c5JCninwc7moaqbkJfaDUb7PkaUitcf5vbTZHdq9dxSMviCm9C3W85rzB8e6yNR70apQ==", + "requires": { + "@types/node": "*" + } + }, "@types/yargs": { "version": "12.0.12", "resolved": "https://verdaccio.lossless.one/@types%2fyargs/-/yargs-12.0.12.tgz", @@ -459,6 +471,11 @@ "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", "dev": true }, + "async-limiter": { + "version": "1.0.1", + "resolved": "https://verdaccio.lossless.one/async-limiter/-/async-limiter-1.0.1.tgz", + "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" + }, "asynckit": { "version": "0.4.0", "resolved": "https://verdaccio.lossless.one/asynckit/-/asynckit-0.4.0.tgz", @@ -1708,6 +1725,14 @@ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true }, + "ws": { + "version": "7.1.2", + "resolved": "https://verdaccio.lossless.one/ws/-/ws-7.1.2.tgz", + "integrity": "sha512-gftXq3XI81cJCgkUiAVixA0raD9IVmXqsylCrjRygw4+UOOGzPoxnQ6r/CnVL9i+mDncJo94tSkyrtuuQVBmrg==", + "requires": { + "async-limiter": "^1.0.0" + } + }, "y18n": { "version": "4.0.0", "resolved": "https://verdaccio.lossless.one/y18n/-/y18n-4.0.0.tgz", diff --git a/package.json b/package.json index 664be4d..2d2ad46 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,11 @@ "tslint-config-prettier": "^1.15.0" }, "dependencies": { - "@pushrocks/smartrequest": "^1.1.20" + "@pushrocks/smartpromise": "^3.0.2", + "@pushrocks/smartrequest": "^1.1.20", + "@tsclass/tsclass": "^2.0.3", + "@types/ws": "^6.0.2", + "ws": "^7.1.2" }, "files": [ "ts/*", diff --git a/test/test.ts b/test/test.ts index 33c8de3..5a2c74e 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,9 +1,18 @@ import { expect, tap } from '@pushrocks/tapbundle'; import * as smartproxy from '../ts/index'; +let testProxy: smartproxy.SmartProxy; + tap.test('first test', async () => { - const testProxy = new smartproxy.SmartProxy(); - // await testProxy.start(); + testProxy = new smartproxy.SmartProxy(); +}); + +tap.test('should start the testproxy', async () => { + await testProxy.start(); +}); + +tap.test('should close the testproxy', async () => { + await testProxy.stop(); }); tap.start(); diff --git a/ts/smartproxy.classes.smartproxy.ts b/ts/smartproxy.classes.smartproxy.ts index aa7ada0..4b83872 100644 --- a/ts/smartproxy.classes.smartproxy.ts +++ b/ts/smartproxy.classes.smartproxy.ts @@ -21,10 +21,14 @@ export class SmartProxy { public async start() { this.httpsServer = plugins.http.createServer(async (req, res) => { req.headers.host = this.router.routeReq(req); - const response = await plugins.smartrequest.request(`https://${req.headers.host}${req.url}`, { - method: req.method, - headers: req.headers - }, true); + const response = await plugins.smartrequest.request( + `https://${req.headers.host}${req.url}`, + { + method: req.method, + headers: req.headers + }, + true + ); res.statusCode = response.statusCode; for (const header of Object.keys(response.headers)) { res.setHeader(header, response.headers[header]); @@ -42,15 +46,44 @@ export class SmartProxy { key: hostCandidate.privateKey }); */ } - this.httpsServer.on('upgrade', (req, socket: Socket) => { - - }) + + // Enable websockets + const wss = new plugins.ws.Server({ server: this.httpsServer }); + wss.on('connection', function connection(ws) { + const wscConnected = plugins.smartpromise.defer(); + const wsc = new plugins.ws(`${ws.url}`); + wsc.on('open', () => { + wscConnected.resolve(); + }); + + ws.on('message', async (message) => { + await wscConnected.promise; + wsc.emit('message', message); + }); + wsc.on('message', (message) => { + ws.emit('message', message); + }); + + // handle closing + ws.on('close', (message) => { + wsc.close(); + }); + wsc.on('close', (message) => { + ws.close(); + }); + }); + this.httpsServer.listen(3000); - - } public async update() { await this.start(); } + + public async stop() { + const done = plugins.smartpromise.defer(); + this.httpsServer.close(() => { + done.resolve(); + }); + } } diff --git a/ts/smartproxy.plugins.ts b/ts/smartproxy.plugins.ts index 31b05cb..91fb564 100644 --- a/ts/smartproxy.plugins.ts +++ b/ts/smartproxy.plugins.ts @@ -1,12 +1,22 @@ // node native scope -import * as http from 'http'; -import * as https from 'https'; +import crypto from 'crypto'; +import http from 'http'; +import https from 'https'; -export { http, https }; +export { crypto, http, https }; // pushrocks scope +import * as smartpromise from '@pushrocks/smartpromise'; import * as smartrequest from '@pushrocks/smartrequest'; export { - smartrequest + smartrequest, + smartpromise +}; + +// third party scope +import ws from 'ws'; + +export { + ws };