From 8ae88a3955b14395f1e116989ef752ae101198c0 Mon Sep 17 00:00:00 2001 From: Nickolay Alexandrov Date: Thu, 23 Jul 2015 12:00:19 +0300 Subject: [PATCH] added timeout support --- lib/wmic.js | 11 +++++++++++ package.json | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/wmic.js b/lib/wmic.js index 5007ef3..24c848a 100644 --- a/lib/wmic.js +++ b/lib/wmic.js @@ -17,6 +17,7 @@ var wmic = function (options) { this.username = getUsername(options.username); this.password = options.password; this.namespace = options.namespace || '\\\\root\\cimv2'; + this.timeout = options.timeout || 90000; this.delimiter = '^@^'; this.wmic = options.wmic || 'wmic'; this.isWindows = process.platform === 'win32'; @@ -120,6 +121,7 @@ wmic.prototype.query = function (wql, namespace, callback) { */ wmic.prototype._exec = function (callback) { var self = this; + var wmic = spawn(this.wmic, this._getArgs(), { cwd: path.join(__dirname, '..'), stdio: ['ignore', 'pipe', 'pipe'] @@ -137,6 +139,11 @@ wmic.prototype._exec = function (callback) { stderr += data; }); + var to = setTimeout(function () { + stderr = 'Child process killed by timeout (' + self.timeout / 1000 + ' seconds)'; + wmic.kill(); + }, self.timeout); + wmic.on('close', function (code) { if (code !== 0) { callback(new Error('Exit code: ' + code + '. ' + stderr)); @@ -144,6 +151,10 @@ wmic.prototype._exec = function (callback) { callback(null, parse(stdout, self.parserOptions)); } }); + + wmic.on('exit', function () { + clearTimeout(to); + }); }; /** diff --git a/package.json b/package.json index 971e86b..3989922 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wmi-client", - "version": "0.1.4", + "version": "0.1.5", "description": "Wrapper around the WMI client. Linux and Windows WMI clients are supported.", "main": "index.js", "scripts": {