From 3ddb4c4c756a4f4a73ee52b9a4f337907af1d32d Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Thu, 22 Aug 2019 13:20:41 +0200 Subject: [PATCH] fix(core): update --- package-lock.json | 18 ++++++------- package.json | 6 ++--- test/test.ts | 4 +++ ts/smartproxy.classes.proxymaster.ts | 38 ++++++++++++++++++++++++---- ts/smartproxy.classes.smartproxy.ts | 1 + ts/smartproxy.plugins.ts | 7 ++--- 6 files changed, 54 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index 570401c..1278681 100644 --- a/package-lock.json +++ b/package-lock.json @@ -266,9 +266,9 @@ "integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ==" }, "@pushrocks/smartrequest": { - "version": "1.1.20", - "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrequest/-/smartrequest-1.1.20.tgz", - "integrity": "sha512-3qOrxZT9+Fhr4GD/dFH+xDHYuSUTStbJl6/jNyh6W0d1L64zH2wfuG8MwlHc3CUHFXPaGauwK+4LQ91JEUx8TQ==", + "version": "1.1.23", + "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrequest/-/smartrequest-1.1.23.tgz", + "integrity": "sha512-Hws3YfzIE0b/E3aTkSugLskKWBq7e8HDXEN+RlRyTFONxW/XONKJFTw4mp3jk+puWpYGDoOTcP+Ua4jd19z9pA==", "requires": { "@pushrocks/smartpromise": "^3.0.2", "@types/form-data": "^2.2.1", @@ -330,9 +330,9 @@ } }, "@pushrocks/smartsystem": { - "version": "2.0.6", - "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsystem/-/smartsystem-2.0.6.tgz", - "integrity": "sha512-8gDFJ2XWcTXdas10qEwqU9spNfA6WNYr8f2z8ry5EW2GP/Ja7G7Xjif9R9VTeRyJiE9eNTWWwFCOxXomAl5qbQ==", + "version": "2.0.7", + "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsystem/-/smartsystem-2.0.7.tgz", + "integrity": "sha512-YwRmwfFZGJCsy4xSW6HmUfWXiHgbBdsbuwVjYJ/fb2pQn4aYp2W2FL0S6gA4pofryQVkaUcxEjFEKTpfNjM1dA==", "requires": { "@pushrocks/lik": "^3.0.10", "@pushrocks/smartenv": "^4.0.7", @@ -365,9 +365,9 @@ } }, "@tsclass/tsclass": { - "version": "2.0.4", - "resolved": "https://verdaccio.lossless.one/@tsclass%2ftsclass/-/tsclass-2.0.4.tgz", - "integrity": "sha512-wNn0V2gqx9CD/RNgeG2zbJEM2gvyN79J87m0DtXTlezlqf2elaK9b5wY0iooemyHpu8SsWhNYaedNylW4Kk9hQ==" + "version": "2.0.5", + "resolved": "https://verdaccio.lossless.one/@tsclass%2ftsclass/-/tsclass-2.0.5.tgz", + "integrity": "sha512-C+XTgdnpwtZYufz50qh04NyuIMFQSQI2606B3mzKUuVEhPXeoo4u8TN/NHzoHQHKL2JxH/RO23i3jXtV8V6mzg==" }, "@types/chai": { "version": "4.2.0", diff --git a/package.json b/package.json index f44ed64..11182bf 100644 --- a/package.json +++ b/package.json @@ -23,10 +23,10 @@ "dependencies": { "@pushrocks/smartnetwork": "^1.1.6", "@pushrocks/smartpromise": "^3.0.2", - "@pushrocks/smartrequest": "^1.1.20", + "@pushrocks/smartrequest": "^1.1.23", "@pushrocks/smartspawn": "^2.0.8", - "@pushrocks/smartsystem": "^2.0.6", - "@tsclass/tsclass": "^2.0.4", + "@pushrocks/smartsystem": "^2.0.7", + "@tsclass/tsclass": "^2.0.5", "@types/ws": "^6.0.2", "ws": "^7.1.2" }, diff --git a/test/test.ts b/test/test.ts index 5a2c74e..1023ecf 100644 --- a/test/test.ts +++ b/test/test.ts @@ -11,6 +11,10 @@ tap.test('should start the testproxy', async () => { await testProxy.start(); }); +tap.test('should wait for 5 seconds', async (tools) => { + await tools.delayFor(5000); +}); + tap.test('should close the testproxy', async () => { await testProxy.stop(); }); diff --git a/ts/smartproxy.classes.proxymaster.ts b/ts/smartproxy.classes.proxymaster.ts index c587818..5152b78 100644 --- a/ts/smartproxy.classes.proxymaster.ts +++ b/ts/smartproxy.classes.proxymaster.ts @@ -1,22 +1,50 @@ -import * as plugins from './smartproxy.plugins'; import { expose } from '@pushrocks/smartspawn'; +import * as plugins from './smartproxy.plugins'; +import * as cluster from 'cluster'; class ProxyMaster { - public hostCandidates: plugins.tsclass + public hostCandidates: plugins.tsclass.network.IReverseProxyConfig[] = []; public clusterChilds: any[] = []; + public smartsystem = new plugins.smartsystem.Smartsystem(); + public async start() { + if (cluster.isMaster) { + console.log('ProxyMaster registered as cluster master'); + console.log(`should spawn ${this.smartsystem.cpus.length} workers`); + for (const cpu of this.smartsystem.cpus) { + cluster.fork(); + } + } else { + const proxyworkerMod = await import('./smartproxy.classes.proxyworker'); + const proxyWorkerInstance = new proxyworkerMod.ProxyWorker(); + console.log('Proxymaster registered a ProxyWorker!'); + const data = new Uint8Array(Buffer.from('Hello Node.js')); + fs.writeFile('message.txt', data, (err) => { + if (err) throw err; + console.log('The file has been saved!'); + }); + } + } } const defaultProxyMaster = new ProxyMaster(); +if (!cluster.isMaster) { + defaultProxyMaster.start(); +} +console.log('hi'); +// the following is interesting for the master process only const proxyMasterCalls = { terminateMaster: async () => { process.kill(0); }, - + start: async () => { + await defaultProxyMaster.start(); + }, + updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => { + + } }; export type TProxyMasterCalls = typeof proxyMasterCalls; expose (proxyMasterCalls); - -console.log('Proxymaster started!'); diff --git a/ts/smartproxy.classes.smartproxy.ts b/ts/smartproxy.classes.smartproxy.ts index 3168ed4..f5e4bce 100644 --- a/ts/smartproxy.classes.smartproxy.ts +++ b/ts/smartproxy.classes.smartproxy.ts @@ -15,6 +15,7 @@ export class SmartProxy { public async start () { this.proxyMasterFunctions = await plugins.smartspawn.spawn(new plugins.smartspawn.Worker('./smartproxy.classes.proxymaster')); console.log('successfully spawned proxymaster'); + await this.proxyMasterFunctions.start(); } public async stop () { diff --git a/ts/smartproxy.plugins.ts b/ts/smartproxy.plugins.ts index eb65f34..d575fcb 100644 --- a/ts/smartproxy.plugins.ts +++ b/ts/smartproxy.plugins.ts @@ -1,6 +1,5 @@ // node native scope - -import cluster from 'cluster'; +import * as cluster from 'cluster'; import http from 'http'; import https from 'https'; @@ -17,11 +16,13 @@ export { import * as smartpromise from '@pushrocks/smartpromise'; import * as smartrequest from '@pushrocks/smartrequest'; import * as smartspawn from '@pushrocks/smartspawn'; +import * as smartsystem from '@pushrocks/smartsystem'; export { smartrequest, smartpromise, - smartspawn + smartspawn, + smartsystem, }; // third party scope