Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

runscript

Package Overview
Dependencies
Maintainers
4
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

runscript - npm Package Compare versions

Comparing version 1.3.1 to 1.4.0

6

History.md
1.4.0 / 2019-07-06
==================
**features**
* [[`a0d7ffb`](http://github.com/node-modules/runscript/commit/a0d7ffb815041baa89b46fb5d76b23f759cd56fb)] - feat: run script with timeout (#8) (fengmk2 <<fengmk2@gmail.com>>)
1.3.1 / 2019-06-15

@@ -3,0 +9,0 @@ ==================

58

index.js

@@ -15,6 +15,9 @@ 'use strict';

* @see https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
* @param {Object} [extraOptions] - extra options for running
* - {Number} [extraOptions.timeout] - child process running timeout
* @return {Object} stdio object, will contains stdio.stdout and stdio.stderr buffer.
*/
module.exports = function runScript(script, options) {
module.exports = function runScript(script, options, extraOptions) {
return new Promise((resolve, reject) => {
extraOptions = extraOptions || {};
options = options || {};

@@ -42,8 +45,12 @@ options.env = options.env || Object.create(process.env);

debug('%s %s %s, %j', sh, shFlag, script, options);
debug('%s %s %s, %j, %j', sh, shFlag, script, options, extraOptions);
const proc = spawn(sh, [ shFlag, script ], options);
const stdout = [];
const stderr = [];
let isEnd = false;
let timeoutTimer;
if (proc.stdout) {
proc.stdout.on('data', buf => {
debug('stdout %d bytes', buf.length);
stdout.push(buf);

@@ -57,2 +64,3 @@ });

proc.stderr.on('data', buf => {
debug('stderr %d bytes', buf.length);
stderr.push(buf);

@@ -64,4 +72,18 @@ });

}
proc.on('error', reject);
proc.on('error', err => {
debug('proc emit error: %s', err);
if (isEnd) return;
isEnd = true;
clearTimeout(timeoutTimer);
reject(err);
});
proc.on('close', code => {
debug('proc emit close: %s', code);
if (isEnd) return;
isEnd = true;
clearTimeout(timeoutTimer);
const stdio = {

@@ -79,2 +101,3 @@ stdout: null,

const err = new Error(`Run "${sh} ${shFlag} ${script}" error, exit code ${code}`);
err.name = 'RunScriptError';
err.stdio = stdio;

@@ -85,3 +108,32 @@ return reject(err);

});
proc.on('exit', code => {
debug('proc emit exit: %s', code);
});
if (typeof extraOptions.timeout === 'number' && extraOptions.timeout > 0) {
// start timer
timeoutTimer = setTimeout(() => {
debug('proc run timeout: %dms', extraOptions.timeout);
isEnd = true;
debug('kill child process %s', proc.pid);
proc.kill();
const err = new Error(`Run "${sh} ${shFlag} ${script}" timeout in ${extraOptions.timeout}ms`);
err.name = 'RunScriptTimeoutError';
const stdio = {
stdout: null,
stderr: null,
};
if (stdout.length > 0) {
stdio.stdout = Buffer.concat(stdout);
}
if (stderr.length > 0) {
stdio.stderr = Buffer.concat(stderr);
}
err.stdio = stdio;
return reject(err);
}, extraOptions.timeout);
}
});
};

2

LICENSE.txt
This software is licensed under the MIT License.
Copyright (c) 2016 - 2017 node-modules and other contributors
Copyright (c) 2016 - present node-modules and other contributors

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

{
"name": "runscript",
"version": "1.3.1",
"version": "1.4.0",
"description": "Run script easy!",

@@ -26,4 +26,4 @@ "main": "index.js",

"egg-ci": "^1.8.0",
"eslint": "3",
"eslint-config-egg": "3",
"eslint": "4",
"eslint-config-egg": "6",
"typescript": "^3.5.1"

@@ -30,0 +30,0 @@ },

@@ -46,4 +46,20 @@ runscript

### run with timeout
Run user script for a maximum of 10 seconds.
```js
const runScript = require('runscript');
runScript('node user-script.js', { stdio: 'pipe' }, { timeout: 10000 })
.then(stdio => {
console.log(stdio);
})
.catch(err => {
console.error(err);
});
```
## License
[MIT](LICENSE.txt)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc