Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child process exec options, fixes maxBuffer size problem #8

Merged
merged 5 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ Shows the current version number.

Write reporting result to the path

#### `-b, --buffersize <size>`

Increase [maxBuffer](https://nodejs.org/docs/latest-v10.x/api/child_process.html#child_process_child_process_exec_command_options_callback) size to prevent #3, `!!! OUTPUT ERROR` or `Unexpected end of JSON input` errors. This is because [child_process stdout being truncated](https://github.com/nodejs/node/issues/19218) when validator check a lot of files.

##### CLI `-b, --buffersize`

```bash
# increase buffer size (1024 * 500)
node-w3c-validator -i static/**/*.html -b 500
```

##### Node.js API `exec.bufferSize`

```js
// increase buffer size (1024 * 500)
nodeW3CValidator(validatePath, {
format: 'html',
exec: {
bufferSize: 1024 * 500
}
}, function (err, output) {
// ...
});
```

---

## Node.js API
Expand Down
11 changes: 9 additions & 2 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ program
.option('--no-stream', 'Forces all documents to be be parsed in buffered mode instead of streaming mode (causes some parse errors to be treated as non-fatal document errors instead of as fatal document errors)')
.option('-v, --verbose', 'Specifies "verbose" output (currently this just means that the names of files being checked are written to stdout)')
.option('-o, --output [path]', 'Write reporting result to the path')
.option('-b, --buffersize <size>', '1024 * <size> Increase maxBuffer size for child_process.exec, if result output truncated')
.parse(process.argv);

/**
Expand All @@ -47,7 +48,8 @@ const cliProps = [
'skipNonHtml',
'html',
'stream',
'verbose'
'verbose',
'buffersize'
];

/**
Expand All @@ -59,7 +61,8 @@ const cliProps = [
function detectUserOptions () {
let outputPath = program.output;
let userOptions = {
output: false
output: false,
exec: {}
};

cliProps.forEach(prop => {
Expand All @@ -70,6 +73,10 @@ function detectUserOptions () {
}
if (value !== undefined) {
userOptions[prop] = value;

if (prop === 'buffersize') {
userOptions.exec.maxBuffer = 1024 * value;
}
}
});
if (typeof outputPath === 'string' && outputPath.length) {
Expand Down
16 changes: 14 additions & 2 deletions lib/render-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,23 @@ function renderHtml (jsonString) {
let msg = JSON.parse(jsonString).messages;
messages = msg;
} catch (e) {
console.log(chalk.red('!!! OUTPUT ERROR'));
console.log('');
console.log(e.message);

if (e.message === 'Unexpected token E in JSON at position 1') {
console.log(chalk.red('Possible cause of error - incorrect path or files were not found'));
}
return e.message;

if (e.message === 'Unexpected end of JSON input') {
console.log(chalk.red('Possible cause of error'));
console.log('NodeJS maxBuffer to small for your task because child_process stdout being truncated.');
console.log('Try increase maxBuffer size with:');
console.log(` ${chalk.gray('-')} CLI --buffersize <size> (1024 * <size>)`);
console.log(` ${chalk.gray('-')} NodeJs API options.exec.maxBuffer 1024 * <size>`);
console.log(`${chalk.yellow('Readme:')} https://github.com/dutchenkoOleg/node-w3c-validator#-b---buffersize-size`);
console.log('');
}
process.exit(1);
}

if (!messages.length) {
Expand Down
6 changes: 5 additions & 1 deletion lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ function getArgvFromObject (options) {
argv.push(`--filterpattern ${options.filterpattern}`);
}

if (options.buffersize) {
argv.push(`--buffersize ${options.buffersize}`);
}

booleanArgs.forEach(key => {
if (options[key]) {
argv.push(`--${camel2dash(key)}`);
Expand All @@ -152,7 +156,7 @@ function nodeW3CValidator (validationPath, userOptions, done) {
let execPath = [vnuCmd].concat(getArgvFromObject(options), fileset.sync(validationPath));

// console.log(execPath.join(' '));
exec(execPath.join(' '), function (err, stdout, stderr) {
exec(execPath.join(' '), options.exec, function (err, stdout, stderr) {
let output = stdout;

if (err === null) {
Expand Down