needle icon indicating copy to clipboard operation
needle copied to clipboard

Piping XML not working on 2.2.2

Open synzen opened this issue 6 years ago • 3 comments

It looks like trying to pipe XML pages does nothing on 2.2.2, and the finish event is never emitted:

// test.js
const needle = require('needle')
const writable = require('fs').createWriteStream('file.txt');

needle.get('https://www.w3schools.com/xml/note.xml')
.pipe(writable)
.on('finish', () => console.log('Done!'))

image

On 2.1.2, file.txt is empty but on 2.2.2 the file is populated as expected. Tested on node v8.11.3 and v10.9.0

synzen avatar Aug 16 '18 23:08 synzen

Yikes, I'll take a look asap

On Thu, Aug 16, 2018 at 8:12 PM synzen [email protected] wrote:

It looks like trying to pipe XML pages does nothing on 2.2.2, and the finish event is never emitted:

// test.jsconst needle = require('needle')const writable = require('fs').createWriteStream('file.txt'); needle.get('https://www.w3schools.com/xml/note.xml') .pipe(writable) .on('finish', () => console.log('Done!'))

[image: image] https://user-images.githubusercontent.com/18688793/44239539-dd48e900-a187-11e8-9099-2c8fbb93564f.png

On 2.1.2, file.txt is empty but on 2.2.2 the file is populated as expected

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tomas/needle/issues/254, or mute the thread https://github.com/notifications/unsubscribe-auth/AAApTs6GsUo1CJK02o8SIMi6DK6g-hnWks5uRfxpgaJpZM4WAuBx .

-- Tomás Pollak forkhq.com

tomas avatar Aug 16 '18 23:08 tomas

Ok so what's happening under the hood is that you're piping an object stream into a non-object one, because Needle is parsing the XML output as part of its processing logic.

So you can either disable parsing, in which case the file's contents are passed directly to the output stream, or pipe to a transform stream in the middle that converts the parsed XML object into a Buffer/string that can be written cleanly to the output.

Obviously, method one is much simpler:

// test.js
const needle = require('needle')
const writable = require('fs').createWriteStream('file.txt');

needle.get('https://www.w3schools.com/xml/note.xml', { parse: false })
.pipe(writable)
.on('finish', () => console.log('Done!'))

Needle should fall back to a regular buffer (the original data) if piping the object fails. But for some reason it's not working. On it now.

tomas avatar Aug 17 '18 22:08 tomas

Ah I see, thanks for the explanation. I tried setting parse to false and, apparently contrary to what you've tested, now it works. I was just confused because I thought something wasn't right since its behavior changed after 2.1.2. I'll leave this open for your to close since I'm not sure, according to you, if there was something not working. Thanks again!

synzen avatar Aug 18 '18 00:08 synzen