Skip to content

Commit cae3802

Browse files
committed
linter
1 parent 2464a70 commit cae3802

9 files changed

Lines changed: 894 additions & 150 deletions

File tree

.eslintrc

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# borrowed from https://github.com/nodejs/node/blob/master/.eslintrc
2+
env:
3+
node: true
4+
mocha: true
5+
6+
rules:
7+
# Possible Errors
8+
# http://eslint.org/docs/rules/#possible-errors
9+
comma-dangle: [2, "only-multiline"]
10+
no-control-regex: 2
11+
no-debugger: 2
12+
no-dupe-args: 2
13+
no-dupe-keys: 2
14+
no-duplicate-case: 2
15+
no-empty-character-class: 2
16+
no-ex-assign: 2
17+
no-extra-boolean-cast: 2
18+
no-extra-parens: [2, "functions"]
19+
no-extra-semi: 2
20+
no-func-assign: 2
21+
no-invalid-regexp: 2
22+
no-irregular-whitespace: 2
23+
no-negated-in-lhs: 2
24+
no-obj-calls: 2
25+
no-proto: 2
26+
no-unexpected-multiline: 2
27+
no-unreachable: 2
28+
use-isnan: 2
29+
valid-typeof: 2
30+
31+
# Best Practices
32+
# http://eslint.org/docs/rules/#best-practices
33+
no-fallthrough: 2
34+
no-multi-spaces: 2
35+
no-octal: 2
36+
no-redeclare: 2
37+
no-self-assign: 2
38+
no-unused-labels: 2
39+
40+
# Strict Mode
41+
# http://eslint.org/docs/rules/#strict-mode
42+
strict: [2, "global"]
43+
44+
# Variables
45+
# http://eslint.org/docs/rules/#variables
46+
no-delete-var: 2
47+
no-undef: 2
48+
no-unused-vars: [2, {"args": "none"}]
49+
50+
# Node.js and CommonJS
51+
# http://eslint.org/docs/rules/#nodejs-and-commonjs
52+
no-mixed-requires: 2
53+
no-new-require: 2
54+
no-path-concat: 2
55+
no-restricted-modules: [2, "sys", "_linklist"]
56+
57+
# Stylistic Issues
58+
# http://eslint.org/docs/rules/#stylistic-issues
59+
comma-spacing: 2
60+
eol-last: 2
61+
indent: [2, 2, {SwitchCase: 1}]
62+
key-spacing: [2, {mode: "minimum"}]
63+
keyword-spacing: 2
64+
linebreak-style: [2, "unix"]
65+
max-len: [2, 120, 2]
66+
new-parens: 2
67+
no-mixed-spaces-and-tabs: 2
68+
no-multiple-empty-lines: [2, {max: 2}]
69+
no-trailing-spaces: 2
70+
quotes: [2, "single", "avoid-escape"]
71+
semi: 2
72+
space-before-blocks: [2, "always"]
73+
space-before-function-paren: [2, "never"]
74+
space-in-parens: [2, "never"]
75+
space-infix-ops: 2
76+
space-unary-ops: 2
77+
78+
# ECMAScript 6
79+
# http://eslint.org/docs/rules/#ecmascript-6
80+
arrow-parens: [2, "always"]
81+
arrow-spacing: [2, {"before": true, "after": true}]
82+
constructor-super: 2
83+
no-class-assign: 2
84+
no-confusing-arrow: 2
85+
no-const-assign: 2
86+
no-dupe-class-members: 2
87+
no-new-symbol: 2
88+
no-this-before-super: 2
89+
prefer-const: 2
90+
template-curly-spacing: 2

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
'use strict';
12
module.exports = require('./lib/proxy');

lib/cache.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
'use strict';
12
var path = require('path'),
23
fs = require('fs'),
34
crypto = require('crypto'),
45
mkdirp = require('mkdirp');
56

67

78
function Cache(opts) {
8-
9-
this.opts = opts || {}
9+
this.opts = opts || {};
1010
this.opts.ttl = (opts.ttl || 1800) * 1000;
1111
this.opts.friendlyNames = opts.friendlyNames;
12-
this.opts.path = opts.path || __dirname + '/../cache';
12+
this.opts.path = opts.path || path.join(__dirname, '/../cache');
1313

1414
this.locks = {};
1515

@@ -69,12 +69,12 @@ function Cache(opts) {
6969
// create lock
7070
locks[key] = true;
7171

72-
mkdirp.sync(path.dir, 0755);
72+
mkdirp.sync(path.dir, 511); // 511 is decimal equvivalent of 0777
7373

7474
var file = fs.createWriteStream(path.full);
7575
file.on('finish', function() {
7676
// release lock
77-
delete(locks[key]);
77+
delete (locks[key]);
7878
file.close();
7979
});
8080

@@ -114,14 +114,14 @@ function Cache(opts) {
114114

115115

116116
this.unlink = function(key) {
117-
delete(this.locks[key]);
117+
delete (this.locks[key]);
118118
fs.unlinkSync(this.getPath(key).full);
119119
};
120120

121-
};
121+
}
122122

123123
Cache.NOT_FOUND = 0;
124-
Cache.EXPIRED = 2;
125-
Cache.FRESH = 4;
124+
Cache.EXPIRED = 2;
125+
Cache.FRESH = 4;
126126

127127
module.exports = Cache;

lib/proxy.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
var 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

1618
exports.log = null;
1719

@@ -23,15 +25,15 @@ exports.opts = {};
2325
var 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

2830
exports.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 (/^win/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

174176
exports.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

226227
function 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);

npm-debug.log

Lines changed: 0 additions & 48 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
"description": "HTTP/HTTPS caching proxy for work with `npm` utility / registry",
1414
"main": "index.js",
1515
"devDependencies": {
16+
"eslint": "^3.19.0",
1617
"github-publish-release": "^1.3.3",
1718
"mocha": "^3.2.0",
1819
"rimraf": "^2.6.1",
1920
"uuid": "^3.0.1"
2021
},
2122
"scripts": {
22-
"test": "mocha -R spec --recursive",
23-
"release": "scripts/release"
23+
"lint": "eslint -c .eslintrc --fix .",
24+
"release": "scripts/release",
25+
"test": "mocha -R spec --recursive"
2426
},
2527
"repository": {
2628
"type": "git",

0 commit comments

Comments
 (0)