1+ 'use strict' ;
12var http = require ( 'http' ) ,
23 net = require ( 'net' ) ,
34 https = require ( 'https' ) ,
@@ -7,11 +8,12 @@ var http = require('http'),
78 path = require ( 'path' ) ,
89 request = require ( 'request' ) ,
910 log4js = require ( 'log4js' ) ;
10- Cache = require ( './cache' ) ;
11+
12+ var Cache = require ( './cache' ) ;
1113
1214
1315// To avoid 'DEPTH_ZERO_SELF_SIGNED_CERT' error on some setups
14- process . env . NODE_TLS_REJECT_UNAUTHORIZED = "0" ;
16+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
1517
1618exports . log = null ;
1719
@@ -23,15 +25,15 @@ exports.opts = {};
2325var mitmAddress ;
2426
2527// Random header to prevent sending requests in a cycle
26- var cycleCheckHeader = 'x-npm-proxy-cache-' + Math . round ( Math . random ( ) * 10000 )
28+ var cycleCheckHeader = 'x-npm-proxy-cache-' + Math . round ( Math . random ( ) * 10000 ) ;
2729
2830exports . powerup = function ( opts ) {
2931
3032 exports . opts = opts || { } ;
3133
3234 var options = {
33- key : fs . readFileSync ( __dirname + '/../cert/dummy.key' , 'utf8' ) ,
34- cert : fs . readFileSync ( __dirname + '/../cert/dummy.crt' , 'utf8' )
35+ key : fs . readFileSync ( path . join ( __dirname , '/../cert/dummy.key' ) , 'utf8' ) ,
36+ cert : fs . readFileSync ( path . join ( __dirname , '/../cert/dummy.crt' ) , 'utf8' )
3537 } ;
3638
3739 this . cache = new Cache ( {
@@ -53,7 +55,7 @@ exports.powerup = function(opts) {
5355 // it does not support unix sockets.
5456 if ( / ^ w i n / i. test ( process . platform ) && ! isNumeric ( opts . internalPort ) ) {
5557 console . error ( 'Error: On Windows platform you have to specify internal port,\n'
56- + 'for example `--internal-port 8081`.' ) ;
58+ + 'for example `--internal-port 8081`.' ) ;
5759 process . exit ( 1 ) ;
5860 }
5961 if ( opts . internalPort ) {
@@ -88,9 +90,9 @@ exports.httpHandler = function(req, res) {
8890 dest = schema + '://' + req . headers [ 'host' ] + path ;
8991
9092 if ( req . headers [ cycleCheckHeader ] ) {
91- res . writeHead ( 502 )
92- res . end ( 'Sending requests to myself. Stopping to prevent cycles.' )
93- return
93+ res . writeHead ( 502 ) ;
94+ res . end ( 'Sending requests to myself. Stopping to prevent cycles.' ) ;
95+ return ;
9496 }
9597
9698 var params = {
@@ -99,7 +101,7 @@ exports.httpHandler = function(req, res) {
99101 url : dest
100102 } ;
101103
102- params . headers [ cycleCheckHeader ] = 1
104+ params . headers [ cycleCheckHeader ] = 1 ;
103105
104106 // Carry following headers down to dest npm repository.
105107 var carryHeaders = [ 'authorization' , 'version' , 'referer' , 'npm-session' , 'user-agent' ] ;
@@ -137,7 +139,7 @@ exports.httpHandler = function(req, res) {
137139 if ( ! err && response . statusCode === 200 ) {
138140 var file = cache . write ( dest ) ;
139141 r . pipe ( file ) . on ( 'finish' , function ( ) {
140- cache . meta ( dest , function ( err , meta ) {
142+ cache . meta ( dest , function ( err , meta ) {
141143 if ( err )
142144 throw err ;
143145 respondWithCache ( dest , cache , meta , res ) ;
@@ -159,7 +161,7 @@ exports.httpHandler = function(req, res) {
159161
160162 res . end ( err ? err . toString ( ) : 'Status ' + response . statusCode + ' returned' ) ;
161163 }
162- }
164+ } ;
163165
164166 var r = request ( params ) ;
165167 r . on ( 'response' , onResponse . bind ( null , null ) ) ;
@@ -173,7 +175,6 @@ exports.httpHandler = function(req, res) {
173175
174176exports . httpsHandler = function ( request , socketRequest , bodyhead ) {
175177 var log = exports . log ,
176- url = request [ 'url' ] ,
177178 httpVersion = request [ 'httpVersion' ] ;
178179
179180 log . debug ( ' = will connect to socket (or port) "%s"' , mitmAddress ) ;
@@ -225,22 +226,23 @@ exports.httpsHandler = function(request, socketRequest, bodyhead) {
225226
226227function bypass ( req , res , params ) {
227228 var length = parseInt ( req . headers [ 'content-length' ] ) ;
229+ var log = exports . log ;
228230
229- var onEnd = function ( params , res ) {
231+ var onEnd = function ( params , res ) {
230232 return request ( params )
231- . on ( 'error' , function ( err ) {
233+ . on ( 'error' , function ( err ) {
232234 log . error ( 'bypass' , err ) ;
233235 } )
234236 . pipe ( res , { end : false } ) ;
235- }
237+ } ;
236238
237239 if ( isNaN ( length ) || isFinite ( length ) ) {
238240 onEnd ( params , res ) ;
239241 return ;
240242 }
241243
242244 var raw = new Buffer ( length ) ,
243- pointer = 0 ;
245+ pointer = 0 ;
244246
245247 req . on ( 'data' , function ( chunk ) {
246248 chunk . copy ( raw , pointer ) ;
0 commit comments