node-persistent-software
Advanced tools
+31
-24
@@ -19,5 +19,5 @@ | ||
| this.process = null; | ||
| this.currentChildProcess = null; | ||
| this.stopped = false; | ||
| this.ended = false; | ||
@@ -39,3 +39,3 @@ this.infinite(); | ||
| this.maxCountRun = max; | ||
| this.countRun = 0; | ||
| this.successCountRun = 0; | ||
| return this; | ||
@@ -52,38 +52,45 @@ } | ||
| if (!this.stopped) { | ||
| if (!this.ended) { | ||
| ++this.countRun; | ||
| if (!this.args) { | ||
| this.process = spawn(this.software); | ||
| this.currentChildProcess = spawn(this.software); | ||
| } | ||
| else if (!this.options) { | ||
| this.process = spawn(this.software, this.args); | ||
| this.currentChildProcess = spawn(this.software, this.args); | ||
| } | ||
| else { | ||
| this.process = spawn(this.software, this.args, this.options); | ||
| this.currentChildProcess = spawn(this.software, this.args, this.options); | ||
| } | ||
| this.process.on("error", (err) => { | ||
| this.currentChildProcess.on("error", (err) => { | ||
| this.eventEmitter.emit("error", (err.message) ? err.message : err); | ||
| }); | ||
| if (!this.process || !this.process.pid) { | ||
| this.stop(); | ||
| if (!this.currentChildProcess || !this.currentChildProcess.pid) { | ||
| this.end(); | ||
| } | ||
| else { | ||
| this.eventEmitter.emit("started"); | ||
| ++this.successCountRun; | ||
| this.process.on("exit", () => { | ||
| if (1 < this.successCountRun) { | ||
| this.eventEmitter.emit("restart"); | ||
| } | ||
| else { | ||
| this.eventEmitter.emit("firststart"); | ||
| } | ||
| this.eventEmitter.emit("ended"); | ||
| this.eventEmitter.emit("start", this.currentChildProcess); | ||
| if (!this.stopped) { | ||
| this.currentChildProcess.on("exit", () => { | ||
| if (0 >= this.maxCountRun || (0 < this.maxCountRun && this.countRun < this.maxCountRun)) { | ||
| this.eventEmitter.emit("stop"); | ||
| if (!this.ended) { | ||
| if (0 >= this.maxCountRun || (0 < this.maxCountRun && this.successCountRun < this.maxCountRun)) { | ||
| this.start(); | ||
| } | ||
| else { | ||
| this.stop(); | ||
| this.end(); | ||
| } | ||
@@ -108,11 +115,11 @@ | ||
| stop() { | ||
| end() { | ||
| this.stopped = true; | ||
| this.ended = true; | ||
| if (this.process && this.process.pid) { | ||
| if (this.currentChildProcess && this.currentChildProcess.pid) { | ||
| try { | ||
| process.kill(this.process.pid); | ||
| this.eventEmitter.emit("ended"); | ||
| process.kill(this.currentChildProcess.pid); | ||
| this.eventEmitter.emit("stop"); | ||
| } | ||
@@ -125,3 +132,3 @@ catch (e) { | ||
| this.eventEmitter.emit("stopped"); | ||
| this.eventEmitter.emit("end"); | ||
@@ -128,0 +135,0 @@ return this; |
+5
-5
| { | ||
| "name": "node-persistent-software", | ||
| "version": "0.1.5", | ||
| "version": "1.0.0", | ||
| "description": "Spawn a software and keep it running", | ||
@@ -26,7 +26,7 @@ "main": "lib/main.js", | ||
| "devDependencies": { | ||
| "gulp": "^3.9.0", | ||
| "gulp-eslint": "^2.0.0", | ||
| "gulp": "^3.9.1", | ||
| "gulp-eslint": "^2.1.0", | ||
| "gulp-exclude-gitignore": "^1.0.0", | ||
| "gulp-mocha": "^2.0.0", | ||
| "gulp-plumber": "^1.0.0" | ||
| "gulp-mocha": "^2.2.0", | ||
| "gulp-plumber": "^1.1.0" | ||
| }, | ||
@@ -33,0 +33,0 @@ "homepage": "https://github.com/Psychopoulet/node-persistent-software#readme", |
+67
-31
@@ -13,21 +13,36 @@ # node-persistent-software | ||
| ### Attributes | ||
| * ``` string software ``` path to the software | ||
| * ``` string args ``` arguments passed to the software | ||
| * ``` object process ``` current process | ||
| * ``` boolean stopped ``` if stopped, keep it stopped | ||
| * ``` array arguments ``` arguments passed to the software | ||
| * ``` object options ``` options passed to the software | ||
| * ``` object currentChildProcess ``` current child_process | ||
| * ``` boolean ended ``` if ended, don't restart it | ||
| * ``` int maxCountRun ``` max start iteration | ||
| * ``` int countRun ``` current start iteration | ||
| * ``` int successCountRun ``` current success start iteration | ||
| * ``` asynchronous-eventemitter eventEmitter ``` async events manager | ||
| * ``` constructor(string software [, array arguments [ , object options ] ] ) ``` => see [spawn](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) documentation | ||
| ### Constructor | ||
| * ``` max(int maxIteration) : return this ``` change max iterations and reset current | ||
| * ``` infinite() : return this ``` no max iteration and reset current | ||
| * ``` start() : return this ``` run the software for the first time | ||
| * ``` stop() : return this ``` stop the software and don't restart it | ||
| * ``` on("error", (err) => {}) : return this ``` fire if an error occurs (use try/catch to avoid loop) | ||
| * ``` on("started", () => {}) : return this ``` fire if the software is started (or restarted) | ||
| * ``` on("ended", () => {}) : return this ``` fire if the software is killed | ||
| * ``` on("stopped", () => {}) : return this ``` fire if the software is stopped | ||
| * ``` constructor(string software [, array arguments [ , object options ] ] ) ``` => see [spawn documentation](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | ||
| ### Methods | ||
| * ``` max(int maxIteration) : this ``` change max iterations and reset current | ||
| * ``` infinite() : this ``` no max iteration and reset current | ||
| * ``` start() : this ``` run the software for the first time | ||
| * ``` end() : this ``` stop the software and don't restart it | ||
| * ``` on(string eventName, function callback) : this ``` | ||
| ### Events | ||
| * ``` on("error", (string err) => {}) : this ``` fire if an error occurs (use try/catch to avoid loop) | ||
| * ``` on("firststart", () => {}) : this ``` fire if the software starts for the first time | ||
| * ``` on("restart", () => {}) : this ``` fire if the software restarts | ||
| * ``` on("start", (object child_process) => {}) : this ``` fire if the software starts (firststart && restart) => see [spawn documentation](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | ||
| * ``` on("stop", () => {}) : this ``` fire if the software is killed | ||
| * ``` on("end", () => {}) : this ``` fire if the software is killed and cannot be restarted | ||
| ## Examples | ||
@@ -40,25 +55,46 @@ | ||
| "C:\\Program Files\\Mozilla Firefox\\firefox.exe", | ||
| ["www.npmjs.com"] | ||
| ).on("error", function(msg) { | ||
| [ "https://www.npmjs.com/package/node-persistent-software" ] | ||
| ).on("error", (msg) => { | ||
| console.log(msg); | ||
| }).on("started", function() { | ||
| console.log("software started"); | ||
| }).on("ended", function() { | ||
| console.log("software ended"); | ||
| }).on("stopped", function() { | ||
| console.log("software stopped"); | ||
| }).infinite().start(); | ||
| }) | ||
| .infinite() | ||
| .on("firststart", () => { | ||
| console.log("Firefox is started for the first time !"); | ||
| }).on("restart", () => { | ||
| console.log("Firefox is started again..."); | ||
| }).on("start", (child_process) => { | ||
| console.log("anyway, Firefox is started."); | ||
| }) | ||
| .on("stop", () => { | ||
| console.log("Firefox is stopped, trying to restart..."); | ||
| }).on("end", () => { | ||
| console.log("/!\ Firefox is stopped and cannot be restarted /!\"); | ||
| }).start(); | ||
| new PersistantSoftware( | ||
| "C:\\Program Files\\Mozilla Firefox\\firefox.exe", | ||
| ["www.github.com"] | ||
| ).on("error", function(msg) { | ||
| [ "https://github.com/Psychopoulet/node-persistent-software" ] | ||
| ).on("error", (msg) { | ||
| console.log(msg); | ||
| }).on("started", function() { | ||
| console.log("software started"); | ||
| }).on("ended", function() { | ||
| console.log("software ended"); | ||
| }).on("stopped", function() { | ||
| console.log("software stopped"); | ||
| }).max(5).start(); | ||
| }) | ||
| .max(5) | ||
| .on("firststart", () => { | ||
| console.log("Firefox is started for the first time !"); | ||
| }).on("restart", () => { | ||
| console.log("Firefox is started again..."); | ||
| }).on("start", () => { | ||
| console.log("anyway, Firefox is started."); | ||
| }) | ||
| .on("stop", () => { | ||
| console.log("Firefox is stopped, trying to restart..."); | ||
| }).on("end", () => { | ||
| console.log("/!\ Firefox is stopped and cannot be restarted /!\"); | ||
| }).start(); | ||
| ``` | ||
@@ -65,0 +101,0 @@ |
+97
-44
@@ -11,80 +11,133 @@ | ||
| describe("run", function() { | ||
| describe("run", () => { | ||
| it("should check wrong path running", function(done) { | ||
| it("should check wrong path running", () => { | ||
| let ps = new PersistantSoftware("wsdvwsdvwsdvwsdvsdv").on("error", function () {}); | ||
| return new Promise((resolve, reject) => { | ||
| ps.on("started", function () { | ||
| done("software found"); | ||
| }).on("stopped", function () { | ||
| let ps = new PersistantSoftware("wsdvwsdvwsdvwsdvsdv").on("error", () => { }); // there IS an error. this is the point... | ||
| assert.strictEqual(1, ps.countRun, "wrong count"); | ||
| assert.strictEqual(1, ps.maxCountRun, "wrong max"); | ||
| ps.on("firststart", () => { | ||
| reject("software found"); | ||
| }).on("end", () => { | ||
| done(); | ||
| try { | ||
| }).max(1).start(); | ||
| assert.strictEqual(0, ps.successCountRun, "wrong count"); | ||
| assert.strictEqual(1, ps.maxCountRun, "wrong max"); | ||
| }); | ||
| resolve(); | ||
| it("should check no args running", function(done) { | ||
| } | ||
| catch(e) { | ||
| reject((e.message) ? e.message : e); | ||
| } | ||
| let ps = new PersistantSoftware("node").on("error", function(msg) { | ||
| (1, console).log("error", msg); | ||
| }).max(1).start(); | ||
| }); | ||
| ps.on("started", function() { | ||
| }).timeout(1 * 1000); | ||
| setTimeout(function() { | ||
| ps.stop(); | ||
| }, 1500); | ||
| it("should check no args running", () => { | ||
| }).on("stopped", function() { | ||
| return new Promise((resolve, reject) => { | ||
| assert.strictEqual(1, ps.countRun, "wrong count"); | ||
| assert.strictEqual(1, ps.maxCountRun, "wrong max"); | ||
| let ps = new PersistantSoftware("node").on("error", (err) => { | ||
| (0, console).log(err); | ||
| }); | ||
| done(); | ||
| ps.on("firststart", () => { | ||
| }).max(1).start(); | ||
| setTimeout(() => { | ||
| ps.end(); | ||
| }, 200); | ||
| }).timeout(5000); | ||
| }).on("restart", () => { | ||
| reject("restarted"); | ||
| }).on("end", () => { | ||
| it("should check normal running with max", function(done) { | ||
| try { | ||
| let ps = new PersistantSoftware( "node", [ "-v" ] ).on("error", function(msg) { | ||
| (1, console).log("error", msg); | ||
| assert.strictEqual(1, ps.successCountRun, "wrong count"); | ||
| assert.strictEqual(1, ps.maxCountRun, "wrong max"); | ||
| resolve(); | ||
| } | ||
| catch(e) { | ||
| reject((e.message) ? e.message : e); | ||
| } | ||
| }).max(1).start(); | ||
| }); | ||
| ps.on("stopped", function() { | ||
| }).timeout(1 * 1000); | ||
| assert.strictEqual(3, ps.countRun, "wrong count"); | ||
| assert.strictEqual(3, ps.maxCountRun, "wrong max"); | ||
| it("should check normal running with max", () => { | ||
| done(); | ||
| return new Promise((resolve, reject) => { | ||
| }).max(3).start(); | ||
| let version = "", ps = new PersistantSoftware( "node", [ "-v" ] ).on("error", (err) => { | ||
| (1, console).log(err); | ||
| }); | ||
| }).timeout(5000); | ||
| ps.on("start", (process) => { | ||
| it("should check normal running with infinite and stop", function(done) { | ||
| process.stdout.on("data", (data) => { | ||
| version = data.toString("utf8").trim(); | ||
| }); | ||
| let ps = new PersistantSoftware( "node", [ "-v" ] ).on("error", function(msg) { | ||
| (1, console).log("error", msg); | ||
| }).on("end", () => { | ||
| try { | ||
| assert.strictEqual(process.version, version, "wrong version"); | ||
| assert.strictEqual(2, ps.successCountRun, "wrong count"); | ||
| assert.strictEqual(2, ps.maxCountRun, "wrong max"); | ||
| resolve(); | ||
| } | ||
| catch(e) { | ||
| reject((e.message) ? e.message : e); | ||
| } | ||
| }).max(2).start(); | ||
| }); | ||
| ps.on("started", function() { | ||
| ps.stop(); | ||
| }).on("stopped", function() { | ||
| }).timeout(1 * 1000); | ||
| assert.strictEqual(1, ps.countRun, "wrong count"); | ||
| assert.strictEqual(0, ps.maxCountRun, "wrong max"); | ||
| it("should check normal running with infinite and end", () => { | ||
| done(); | ||
| return new Promise((resolve, reject) => { | ||
| }).infinite().start(); | ||
| let ps = new PersistantSoftware( "node", [ "-v" ] ).on("error", (err) => { | ||
| (1, console).log(err); | ||
| }); | ||
| }).timeout(5000); | ||
| ps.on("restart", () => { | ||
| ps.end(); | ||
| }).on("end", () => { | ||
| try { | ||
| assert.strictEqual(2, ps.successCountRun, "wrong count"); | ||
| assert.strictEqual(0, ps.maxCountRun, "wrong max"); | ||
| resolve(); | ||
| } | ||
| catch(e) { | ||
| reject((e.message) ? e.message : e); | ||
| } | ||
| }).infinite().start(); | ||
| }); | ||
| }).timeout(1 * 1000); | ||
| }); |
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11602
23.72%219
23.03%0
-100%109
49.32%