Skip to content

Commit

Permalink
Merge pull request #53 from impressi-web/master
Browse files Browse the repository at this point in the history
use of es6, concat-stream, built in stream pipeline. fixed some vulnerabilities
  • Loading branch information
bulkan authored Jan 14, 2021
2 parents 4ecaa05 + 39a8a18 commit 0788122
Show file tree
Hide file tree
Showing 9 changed files with 1,379 additions and 1,323 deletions.
34 changes: 17 additions & 17 deletions exercises/each/solution/solution.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var http = require('http')
, async = require('async');
"use strict";
const { get } = require("http");
const async = require("async");

async.each(process.argv.slice(2), function(item, done){
http.get(item, function(res){
res.on('data', function(chunk){
});

res.on('end', function(){
done(null);
});
}).on('error', function(err){
done(err);
});
},
function(err){
if(err) console.error(err);
});
async.each(
process.argv.slice(2),
function (url, done) {
get(url, (res) => {
res
.on("data", (chunk) => {})
.on("end", () => done(null))
.on("error", done);
}).on("error", done);
},
function (err) {
if (err) console.error(err);
}
);
43 changes: 24 additions & 19 deletions exercises/map/solution/solution.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
var http = require('http')
, async = require('async');
"use strict";
const { get } = require("http");
const async = require("async");
const concat = require("concat-stream");
const { pipeline } = require("stream");

async.map(process.argv.slice(2), function(url, done){
var body = '';
http.get(url, function(res){
res.on('data', function(chunk){
body += chunk.toString();
});

res.on('end', function(){
return done(null, body);
});
});
},
function done(err, results){
if (err) return console.log(err);
// results is an array of the response bodies in the same order
console.log(results);
});
async.map(
process.argv.slice(2),
function (url, done) {
get(url, (res) => {
res.setEncoding("utf-8");
pipeline(
res,
concat((data) => done(null, data)),
(err) => {
if (err) return done(err);
}
);
}).on("error", done);
},
(err, results) => {
if (err) console.error(err);
console.log(results);
}
);
46 changes: 27 additions & 19 deletions exercises/reduce/solution/solution.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
var http = require('http')
, async = require('async');
"use strict";
const { get } = require("http");
const async = require("async");
const { pipeline } = require("stream");
const concat = require("concat-stream");

async.reduce(['one', 'two', 'three'], 0, function(memo, item, done){
var body = '';

http.get(process.argv[2] + "?number=" + item, function(res){
res.on('data', function(chunk){
body += chunk.toString();
});

res.on('end', function(){
done(null, memo + Number(body));
});
}).on('error', done);

}, function done(err, result){
if (err) return console.log(err);
console.log(result);
});
async.reduce(
["one", "two", "three"],
0,
function (memo, item, done) {
get(process.argv[2] + "?number=" + item, (res) => {
res.setEncoding("utf-8");
pipeline(
res,
concat((data) => {
done(null, Number(data) + memo);
}),
(err) => {
if (err) return done(err);
}
);
}).on("error", done);
},
function (err, result) {
if (err) return console.error(err);
console.log(result);
}
);
53 changes: 28 additions & 25 deletions exercises/series_object/solution/solution.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
var http = require('http')
, async = require('async');
const { get } = require("http");
const { pipeline } = require("stream");
const concat = require("concat-stream");
const async = require("async");

async.series({
requestOne: function(done){
fetchURL(process.argv[2], done);
async.series(
{
requestOne: function (done) {
fetchURL(process.argv[2], done);
},
requestTwo: function (done) {
fetchURL(process.argv[3], done);
},
},
requestTwo: function(done){
fetchURL(process.argv[3], done);
function (err, results) {
if (err) return console.error(err);
console.log(results);
}
},
function done(err, result){
if (err) return console.error(err);
console.log(result);
});
);


function fetchURL(url, done) {
var body = '';
http.get(url, function(res){
res.on('data', function(chunk){
body += chunk.toString();
});

res.on('end', function(chunk){
done(null, body);
});
}).on('error', function(e){
done(e);
function fetchURL(url, callback) {
get(url, (res) => {
res.setEncoding("utf-8");
pipeline(
res,
concat((data) => {
callback(null, data);
}),
(err) => {
if (err) return callback(err);
}
);
});
}
117 changes: 60 additions & 57 deletions exercises/times/solution/solution.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,68 @@
var http = require('http')
, qs = require('querystring')
, async = require('async')
, hostname = process.argv[2]
, port = process.argv[3]
, url = 'http://' + hostname + ':' + port;
"use strict";
const { get, request } = require("http");
const async = require("async");
const { pipeline } = require("stream");
const concat = require("concat-stream");

async.series({
post: function(done){
async.times(5, function(n, next){
_addUser(++n, function(err){
next(err);
});
}, function next(err){
if (err) return done(err);
done(null, 'saved');
});
async.series(
{
create: function (done) {
async.times(
5,
function (n, next) {
_addUser(++n, next);
},
function (err) {
if (err) return done(err);
done(null);
}
);
},
users: function (done) {
const opts = {
hostname: process.argv[2],
port: process.argv[3],
pathname: "/users",
};
get(opts, (res) => {
res.setEncoding("utf-8");
pipeline(
res,
concat((data) => {
done(null, data);
}),
(err) => {
if (err) return done(err);
}
);
}).on("error", done);
},
},

get: function(done){
http.get(url + '/users', function(res){
var body = "";
res.on('data', function(chunk){
body += chunk.toString();
});

res.on('end', function(){
done(null, body);
});
}).on('error', done);
function (err, results) {
if (err) return console.error(err);
console.log(results.users);
}
);

}, function done(err, result){
if (err) return console.log(err);
console.log(result.get);
});


function _addUser(user_id, next){
var postdata = JSON.stringify({'user_id': user_id}),
opts = {
hostname: hostname,
port: port,
path: '/users/create',
method: 'POST',
function _addUser(user_id, next) {
const postData = JSON.stringify({ user_id });
const opts = {
hostname: process.argv[2],
port: process.argv[3],
pathname: "/users/create",
method: "POST",
headers: {
'Content-Length': postdata.length
}
"Content-Length": postData.length,
},
};

var req = http.request(opts, function(res){
res.on('data', function(chunk){})

res.on('end', function(){
next();
});
});

req.on('error', function(err){
next(err);
});

req.write(postdata);
const req = request(opts, (res) => {
res
.on("data", (chunk) => {})
.on("end", () => {
next(null, "created");
})
.on("error", next);
}).on("error", next);
req.write(postData);
req.end();
}
62 changes: 34 additions & 28 deletions exercises/waterfall/solution/solution.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
var fs = require('fs')
, http = require('http')
, async = require('async');
"use strict";
const { get } = require("http");
const async = require("async");
const concat = require("concat-stream");
const fs = require("fs");
const { pipeline } = require("stream");

async.waterfall([
function(done){
fs.readFile(process.argv[2], function(err, data){
if (err) return done(err);
done(null, data)
});
},

function(data, done){
var body = '';
http.get(data.toString().trimRight(), function(res){
res.on('data', function(chunk){
body += chunk.toString();
});

res.on('end', function(chunk){
done(null, body);
});
}).on('error', function(e){
done(e);
});
async.waterfall(
[
function (done) {
pipeline(
fs.createReadStream(process.argv[2], "utf-8"),
concat((data) => done(null, data)),
(err) => {
if (err) return done(err);
}
);
},
function (url, done) {
get(url, (res) => {
res.setEncoding("utf-8");
pipeline(
res,
concat((data) => done(null, data)),
(err) => {
if (err) return done(err);
}
);
}).on("error", done);
},
],
function (err, result) {
if (err) return console.error(err);
console.log(result);
}
], function done(err, result){
if (err) return console.error(err);
console.log(result);
});
);
Loading

0 comments on commit 0788122

Please sign in to comment.