Last active
September 13, 2017 04:06
-
-
Save bengl/24d58254d2d60144485d8763f20e566c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Readable } = require('stream'); | |
const oldOn = Readable.prototype.on; | |
Readable.prototype.on = function on(name, fn) { | |
oldOn.apply(this, arguments); | |
if (name !== 'allData' || this._readableState.objectMode) { | |
return; | |
} | |
const bufs = []; | |
oldOn.call(this, 'data', d => bufs.push(d)); | |
oldOn.call(this, 'end', () => { | |
this.emit('allData', | |
Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : bufs.join('')); | |
}); | |
return this; | |
}; | |
Readable.prototype.addListener = Readable.prototype.on; | |
require('events').prototype.whence = function (name) { | |
return new Promise(res => this.once(name, res)); | |
}; | |
// -------------------------- | |
const https = require('https'); | |
async function get (url) { | |
const res = await https.get(url).whence('response'); | |
return res.whence('allData'); | |
} | |
(async function main () { | |
console.log(await get('https://nodejs.org')); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment