22
33var util = require ( 'util' ) ;
44
5- module . exports = function ( opts ) {
5+ module . exports = function ( options ) {
6+ var opts = util . _extend ( {
7+ indent : ' '
8+ } , options ) ;
9+
610 var code = [ ] ;
711
8- var options = {
12+ var reqOpts = {
913 method : this . source . method ,
1014 hostname : this . source . uriObj . hostname ,
1115 port : this . source . uriObj . port ,
@@ -19,27 +23,42 @@ module.exports = function (opts) {
1923 return encodeURIComponent ( cookie . name ) + '=' + encodeURIComponent ( cookie . value ) ;
2024 } ) ;
2125
22- options . headers . Cookie = cookies . join ( '; ' ) ;
26+ reqOpts . headers . Cookie = cookies . join ( '; ' ) ;
2327 }
2428
25- code . push ( 'var options = ' + JSON . stringify ( options , null , 2 ) ) ;
29+ code . push ( 'var http = require("http");' ) ;
30+
31+ code . push ( null ) ;
32+
33+ code . push ( util . format ( 'var options = %s;' , JSON . stringify ( reqOpts , null , 2 ) ) ) ;
34+
35+ code . push ( null ) ;
36+
37+ code . push ( 'var req = http.request(options, function (res) {' ) ;
38+
39+ code . push ( opts . indent + 'var chunks = [];' ) ;
40+
41+ code . push ( null ) ;
42+
43+ code . push ( opts . indent + 'res.on("data", function (chunk) {' ) ;
44+ code . push ( opts . indent + opts . indent + 'chunks.push(chunk);' ) ;
45+ code . push ( opts . indent + '});' ) ;
46+
47+ code . push ( null ) ;
2648
27- code . push ( [
28- 'var req = http.request(options, function (res) {' ,
29- 'console.log("STATUS: " + res.statusCode);' ,
30- 'console.log("HEADERS: " + JSON.stringify(res.headers));' ,
31- 'res.setEncoding("utf8");' ,
32- 'res.on("data", function (chunk) {' ,
33- ' console.log("BODY: " + chunk);' ,
34- '});'
35- ] . join ( '\n ' ) + '\n});' ) ;
49+ code . push ( opts . indent + 'res.on("end", function () {' ) ;
50+ code . push ( opts . indent + opts . indent + 'var body = Buffer.concat(chunks);' ) ;
51+ code . push ( opts . indent + '});' ) ;
52+ code . push ( '});' ) ;
3653
3754 if ( this . source . postData ) {
38- code . push ( util . format ( 'this.source.write(%s)' , JSON . stringify ( this . source . postData . text ) ) ) ;
55+ code . push ( null ) ;
56+ code . push ( util . format ( 'req.write(%s)' , JSON . stringify ( this . source . postData . text ) ) ) ;
3957 }
4058
41- code . push ( 'this.source .end();' ) ;
59+ code . push ( 'req .end();' ) ;
4260
61+ code . push ( null ) ;
4362 return code . join ( '\n' ) ;
4463} ;
4564
0 commit comments