-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from impressi-web/master
use of es6, concat-stream, built in stream pipeline. fixed some vulnerabilities
- Loading branch information
Showing
9 changed files
with
1,379 additions
and
1,323 deletions.
There are no files selected for viewing
This file contains 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
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); | ||
} | ||
); |
This file contains 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
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); | ||
} | ||
); |
This file contains 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
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); | ||
} | ||
); |
This file contains 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
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); | ||
} | ||
); | ||
}); | ||
} |
This file contains 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
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(); | ||
} |
This file contains 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
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); | ||
}); | ||
); |
Oops, something went wrong.