grunt-lib-phantomjs icon indicating copy to clipboard operation
grunt-lib-phantomjs copied to clipboard

add stringify helper

Open tkellen opened this issue 11 years ago • 6 comments

via @jsoverson on IRC

It works, which is good. EventEmitter2 has what looks like some issues unregistering listeners, and communication involving cyclic structures breaks when jsonifying data. I had to implement a custom stringify method that might be beneficial to incorporate into the lib.

tkellen avatar Nov 07 '12 16:11 tkellen

@jsoverson Could you expand on this a little bit? Ben and I are both unclear about what is needed here. Thanks!

tkellen avatar Nov 07 '12 16:11 tkellen

In both contrib-jasmine and contrib-qunit there has to be a sendMessage function that JSON stringifies its message/data and then issues an alert in order to communicate with phantomJS.

Standard JSON stringification doesn't deal well with cyclic structures so, when the input may be unknown (like when reporting expected vs actual in unit tests) the bridge may break if it gets a complex structure with circular references.

sendMessage in qunit : https://github.com/gruntjs/grunt-contrib-qunit/blob/master/phantomjs/bridge.js#L19 sendMessage in jasmine : https://github.com/gruntjs/grunt-contrib-jasmine/blob/master/tasks/jasmine/reporters/PhantomReporter.js#L9

The stringify method I had to write to protect against it: https://github.com/gruntjs/grunt-contrib-jasmine/blob/master/tasks/jasmine/reporters/PhantomReporter.js#L97

The sendMessage method should probably exist in grunt-lib-phantomjs along with whatever stringify protection is necessary.

I ended up being concerned about performance so I only used the stringify method where I thought it would be most necessary, but I don't think performance cost ended up being significant and it could be rolled into sendMessage itself.

jsoverson avatar Nov 12 '12 21:11 jsoverson

It's been a year with this issue, what are you guys thinking regarding this stringify helper?

vladikoff avatar Nov 06 '13 02:11 vladikoff

@sindresorhus had an object stringification method used in yeoman. It should probably still be added, but his might have gone under more revisions.

jsoverson avatar Nov 06 '13 06:11 jsoverson

@jsoverson stringify-object is for printing out formatted JS objects, not for messaging. You probably want https://npmjs.org/package/json-stringify-safe

sindresorhus avatar Nov 06 '13 07:11 sindresorhus

It's been ~3 years with this issue. Are you going to implement the helper or using the module that sindersorhus points to or something else ?

Arkni avatar Feb 13 '16 16:02 Arkni