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

Stream JSON output (--json) #4630

Closed
wants to merge 2 commits into from

Conversation

simonflk
Copy link

@simonflk simonflk commented Apr 3, 2017

This fixes #4629

What kind of change does this PR introduce?

bugfix

Did you add tests for your changes?

No. I will have a look and see if I can find where to add appropriate tests.

If relevant, link to documentation update:

Summary

This fixes an issue in #4629 where I reported that I was unable to write out profile JSON.

Does this PR introduce a breaking change?

I don't believe so, no.

Other information

This fixes webpack#4629

JSON.stringify can fail with `RangeError: Invalid string length`
when it runs out of memory. See nodejs/node-v0.x-archivei#14170.
With this fix, I was able to write out the profile JSON without
errors.
@jsf-clabot
Copy link

jsf-clabot commented Apr 3, 2017

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@sokra sokra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think using JSONStream will solve this issue. JSONStream uses JSON.stringify(item) internally. This does work in your example because it uses JSON.stringify(obj) instead of JSON.stringify(obj, null, 2). This generates a much smaller string.

Maybe just change to JSON.stringify(obj) which increases the limit. Or find a json stringifier which really stringifies in chunks.

@webpack-bot
Copy link
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

@timse
Copy link
Member

timse commented Apr 8, 2017

json-streamify looks like it does actually stream, but it comes at the cost of "lexing" and traversing a potentially huge json. not sure if thats good.

@sokra
Copy link
Member

sokra commented Apr 10, 2017

We can try JSON.stringify and if it throws use the safe json-stringify impl.

@avocadee
Copy link

avocadee commented Apr 14, 2017

I applied the change to webpack 1.13.0, node version v4.4.7, npm version 2.15.8
var stringify = require("JSONStream").stringify("", "", "");
stringify.pipe(process.stdout);
stringify.write(stats.toJson(outputOptions));
stringify.end();

But I got this error :(

stream.js:74
throw er; // Unhandled stream error in pipe.
^

RangeError: Invalid string length
at join (native)
at Object.stringify (native)
at Stream. (C:\XXXXXX\webpack\node_modules\JSONStream\index.js:196:23)
at Stream.stream.write (C:\XXXXXX\webpack\node_modules\JSONStream\node_modules\through\index.js:26:11)
at compilerCallback (C:\XXXXXX\webpack\node_modules\webpack\bin\webpack.js:175:14)
at Compiler. (C:\XXXXXX\webpack\node_modules\webpack\lib\Compiler.js:194:14)
at Compiler.emitRecords (C:\XXXXXX\webpack\node_modules\webpack\lib\Compiler.js:282:37)
at Compiler. (C:\XXXXXX\webpack\node_modules\webpack\lib\Compiler.js:187:11)
at C:\XXXXXX\webpack\patches\webpack\lib\Compiler.js:73:20
at Compiler.applyPluginsAsync (C:\XXXXXX\webpack\node_modules\webpack\node_modules\tapable\lib\Tapable.js:60:69)
at Compiler.afterEmit (C:\XXXXXX\webpack\patches\webpack\lib\Compiler.js:70:14)
at Compiler. (C:\XXXXXX\webpack\patches\webpack\lib\Compiler.js:65:27)
at C:\XXXXXX\webpack\node_modules\webpack\node_modules\async\lib\async.js:52:16
at replenish (C:\XXXXXX\webpack\node_modules\webpack\node_modules\async\lib\async.js:306:28)
at C:\XXXXXX\webpack\node_modules\webpack\node_modules\async\lib\async.js:326:29
at C:\XXXXXX\webpack\node_modules\webpack\node_modules\async\lib\async.js:44:16
at C:\XXXXXX\webpack\node_modules\webpack\node_modules\async\lib\async.js:1209:30
at C:\XXXXXX\webpack\node_modules\less\node_modules\graceful-fs\graceful-fs.js:43:10
at C:\XXXXXX\webpack\node_modules\webpack\node_modules\enhanced-resolve\node_modules\graceful-fs\graceful-fs.js:43:10
at FSReqWrap.oncomplete (fs.js:82:15)

@sokra
Copy link
Member

sokra commented Jul 24, 2017

@sokra sokra closed this Jul 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

webpack --profile --json "RangeError: Invalid string length" when outputting _huge_ JSON file
6 participants