Skip to content

Commit 35d23b3

Browse files
Merge pull request #8 from yunusga/child_process_exec_options
Child process exec options, fixes maxBuffer size problem
2 parents 79bc6ee + 3f4bf26 commit 35d23b3

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

Diff for: README.md

+25
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ Shows the current version number.
121121

122122
Write reporting result to the path
123123

124+
#### `-b, --buffersize <size>`
125+
126+
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.
127+
128+
##### CLI `-b, --buffersize`
129+
130+
```bash
131+
# increase buffer size (1024 * 500)
132+
node-w3c-validator -i static/**/*.html -b 500
133+
```
134+
135+
##### Node.js API `exec.bufferSize`
136+
137+
```js
138+
// increase buffer size (1024 * 500)
139+
nodeW3CValidator(validatePath, {
140+
format: 'html',
141+
exec: {
142+
bufferSize: 1024 * 500
143+
}
144+
}, function (err, output) {
145+
// ...
146+
});
147+
```
148+
124149
---
125150

126151
## Node.js API

Diff for: bin/cmd.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ program
3131
.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)')
3232
.option('-v, --verbose', 'Specifies "verbose" output (currently this just means that the names of files being checked are written to stdout)')
3333
.option('-o, --output [path]', 'Write reporting result to the path')
34+
.option('-b, --buffersize <size>', '1024 * <size> Increase maxBuffer size for child_process.exec, if result output truncated')
3435
.parse(process.argv);
3536

3637
/**
@@ -47,7 +48,8 @@ const cliProps = [
4748
'skipNonHtml',
4849
'html',
4950
'stream',
50-
'verbose'
51+
'verbose',
52+
'buffersize'
5153
];
5254

5355
/**
@@ -59,7 +61,8 @@ const cliProps = [
5961
function detectUserOptions () {
6062
let outputPath = program.output;
6163
let userOptions = {
62-
output: false
64+
output: false,
65+
exec: {}
6366
};
6467

6568
cliProps.forEach(prop => {
@@ -70,6 +73,10 @@ function detectUserOptions () {
7073
}
7174
if (value !== undefined) {
7275
userOptions[prop] = value;
76+
77+
if (prop === 'buffersize') {
78+
userOptions.exec.maxBuffer = 1024 * value;
79+
}
7380
}
7481
});
7582
if (typeof outputPath === 'string' && outputPath.length) {

Diff for: lib/render-html.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,23 @@ function renderHtml (jsonString) {
6060
let msg = JSON.parse(jsonString).messages;
6161
messages = msg;
6262
} catch (e) {
63-
console.log(chalk.red('!!! OUTPUT ERROR'));
63+
console.log('');
64+
console.log(e.message);
65+
6466
if (e.message === 'Unexpected token E in JSON at position 1') {
6567
console.log(chalk.red('Possible cause of error - incorrect path or files were not found'));
6668
}
67-
return e.message;
69+
70+
if (e.message === 'Unexpected end of JSON input') {
71+
console.log(chalk.red('Possible cause of error'));
72+
console.log('NodeJS maxBuffer to small for your task because child_process stdout being truncated.');
73+
console.log('Try increase maxBuffer size with:');
74+
console.log(` ${chalk.gray('-')} CLI --buffersize <size> (1024 * <size>)`);
75+
console.log(` ${chalk.gray('-')} NodeJs API options.exec.maxBuffer 1024 * <size>`);
76+
console.log(`${chalk.yellow('Readme:')} https://github.com/dutchenkoOleg/node-w3c-validator#-b---buffersize-size`);
77+
console.log('');
78+
}
79+
process.exit(1);
6880
}
6981

7082
if (!messages.length) {

Diff for: lib/validator.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ function getArgvFromObject (options) {
127127
argv.push(`--filterpattern ${options.filterpattern}`);
128128
}
129129

130+
if (options.buffersize) {
131+
argv.push(`--buffersize ${options.buffersize}`);
132+
}
133+
130134
booleanArgs.forEach(key => {
131135
if (options[key]) {
132136
argv.push(`--${camel2dash(key)}`);
@@ -152,7 +156,7 @@ function nodeW3CValidator (validationPath, userOptions, done) {
152156
let execPath = [vnuCmd].concat(getArgvFromObject(options), fileset.sync(validationPath));
153157

154158
// console.log(execPath.join(' '));
155-
exec(execPath.join(' '), function (err, stdout, stderr) {
159+
exec(execPath.join(' '), options.exec, function (err, stdout, stderr) {
156160
let output = stdout;
157161

158162
if (err === null) {

0 commit comments

Comments
 (0)