Skip to content

Instantly share code, notes, and snippets.

@saml
Last active February 10, 2017 21:44
Show Gist options
  • Save saml/da66515b577fa7413bd9211eb3f6b793 to your computer and use it in GitHub Desktop.
Save saml/da66515b577fa7413bd9211eb3f6b793 to your computer and use it in GitHub Desktop.
'use strict';
const http = require('http');
const fs = require('fs');
const stream = require('stream');
const req = http.request({hostname:'localhost',port:5000,method:'PUT',path:'/'});
class Null extends stream.Writable {
_write(chunk, encoding, callback) {
callback();
}
}
class ReqStream extends stream.Writable {
constructor(req) {
super();
this.req = req;
req.on('response', (res) => {
res.pipe(new Null());
if (res.statusCode !== 200) {
this.emit('error', new Error(res.statusText));
this.end();
}
});
}
_write(chunk, encoding, callback) {
this.req.write(chunk, encoding, callback);
}
end(chunk, encoding, callback) {
super.end(chunk, encoding, callback);
this.req.end(chunk, encoding, callback);
}
}
const target = new ReqStream(req);
//const input = fs.createReadStream('./streams.js');
const input = fs.createReadStream('/home/saml/Downloads/ReactOS-0.4.3.iso');
input.pipe(target);
new Promise((resolve, reject) => {
target.on('finish', resolve);
target.on('error', reject);
})
.then(() => console.log('bye'))
.catch((err) => console.log('err', err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment