Skip to content

Commit 8cec918

Browse files
Petr PchelkoYuri Astrakhan
authored andcommitted
Added missing peerDependency and fixed doc lint warnings
1 parent 4d3102c commit 8cec918

File tree

5 files changed

+117
-123
lines changed

5 files changed

+117
-123
lines changed

app.js

Lines changed: 112 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -20,115 +20,116 @@ var yaml = require('js-yaml');
2020
*/
2121
function initApp(options) {
2222

23-
// the main application object
24-
var app = express();
25-
26-
// get the options and make them available in the app
27-
app.logger = options.logger; // the logging device
28-
app.metrics = options.metrics; // the metrics
29-
app.conf = options.config; // this app's config options
30-
app.info = packageInfo; // this app's package info
31-
32-
// ensure some sane defaults
33-
if(!app.conf.port) { app.conf.port = 8888; }
34-
if(!app.conf.interface) { app.conf.interface = '0.0.0.0'; }
35-
if(app.conf.compression_level === undefined) { app.conf.compression_level = 3; }
36-
if(app.conf.cors === undefined) { app.conf.cors = '*'; }
37-
if(app.conf.csp === undefined) {
38-
app.conf.csp =
39-
"default-src 'self'; object-src 'none'; media-src *; img-src *; style-src *; frame-ancestors 'self'";
40-
}
41-
42-
// set outgoing proxy
43-
if(app.conf.proxy) {
44-
process.env.HTTP_PROXY = app.conf.proxy;
45-
// if there is a list of domains which should
46-
// not be proxied, set it
47-
if(app.conf.no_proxy_list) {
48-
if(Array.isArray(app.conf.no_proxy_list)) {
49-
process.env.NO_PROXY = app.conf.no_proxy_list.join(',');
50-
} else {
51-
process.env.NO_PROXY = app.conf.no_proxy_list;
23+
return BBPromise.try(() => {
24+
// the main application object
25+
var app = express();
26+
27+
// get the options and make them available in the app
28+
app.logger = options.logger; // the logging device
29+
app.metrics = options.metrics; // the metrics
30+
app.conf = options.config; // this app's config options
31+
app.info = packageInfo; // this app's package info
32+
33+
// ensure some sane defaults
34+
if(!app.conf.port) { app.conf.port = 8888; }
35+
if(!app.conf.interface) { app.conf.interface = '0.0.0.0'; }
36+
if(app.conf.compression_level === undefined) { app.conf.compression_level = 3; }
37+
if(app.conf.cors === undefined) { app.conf.cors = '*'; }
38+
if(app.conf.csp === undefined) {
39+
app.conf.csp =
40+
"default-src 'self'; object-src 'none'; media-src *; img-src *; style-src *; frame-ancestors 'self'";
41+
}
42+
43+
// set outgoing proxy
44+
if(app.conf.proxy) {
45+
process.env.HTTP_PROXY = app.conf.proxy;
46+
// if there is a list of domains which should
47+
// not be proxied, set it
48+
if(app.conf.no_proxy_list) {
49+
if(Array.isArray(app.conf.no_proxy_list)) {
50+
process.env.NO_PROXY = app.conf.no_proxy_list.join(',');
51+
} else {
52+
process.env.NO_PROXY = app.conf.no_proxy_list;
53+
}
5254
}
5355
}
54-
}
55-
56-
// set up header whitelisting for logging
57-
if(!app.conf.log_header_whitelist) {
58-
app.conf.log_header_whitelist = [
59-
'cache-control', 'content-type', 'content-length', 'if-match',
60-
'user-agent', 'x-request-id'
61-
];
62-
}
63-
app.conf.log_header_whitelist = new RegExp('^(?:' + app.conf.log_header_whitelist.map(function(item) {
64-
return item.trim();
65-
}).join('|') + ')$', 'i');
66-
67-
// set up the request templates for the APIs
68-
apiUtil.setupApiTemplates(app);
69-
70-
// set up the spec
71-
if(!app.conf.spec) {
72-
app.conf.spec = __dirname + '/spec.yaml';
73-
}
74-
if(app.conf.spec.constructor !== Object) {
75-
try {
76-
app.conf.spec = yaml.safeLoad(fs.readFileSync(app.conf.spec));
77-
} catch(e) {
78-
app.logger.log('warn/spec', 'Could not load the spec: ' + e);
79-
app.conf.spec = {};
56+
57+
// set up header whitelisting for logging
58+
if(!app.conf.log_header_whitelist) {
59+
app.conf.log_header_whitelist = [
60+
'cache-control', 'content-type', 'content-length', 'if-match',
61+
'user-agent', 'x-request-id'
62+
];
63+
}
64+
app.conf.log_header_whitelist = new RegExp('^(?:' + app.conf.log_header_whitelist.map(function(item) {
65+
return item.trim();
66+
}).join('|') + ')$', 'i');
67+
68+
// set up the request templates for the APIs
69+
apiUtil.setupApiTemplates(app);
70+
71+
// set up the spec
72+
if(!app.conf.spec) {
73+
app.conf.spec = __dirname + '/spec.yaml';
8074
}
81-
}
82-
if(!app.conf.spec.swagger) {
83-
app.conf.spec.swagger = '2.0';
84-
}
85-
if(!app.conf.spec.info) {
86-
app.conf.spec.info = {
87-
version: app.info.version,
88-
title: app.info.name,
89-
description: app.info.description
90-
};
91-
}
92-
app.conf.spec.info.version = app.info.version;
93-
if(!app.conf.spec.paths) {
94-
app.conf.spec.paths = {};
95-
}
96-
97-
// set the CORS and CSP headers
98-
app.all('*', function(req, res, next) {
99-
if(app.conf.cors !== false) {
100-
res.header('access-control-allow-origin', app.conf.cors);
101-
res.header('access-control-allow-headers', 'accept, x-requested-with, content-type');
102-
res.header('access-control-expose-headers', 'etag');
75+
if(app.conf.spec.constructor !== Object) {
76+
try {
77+
app.conf.spec = yaml.safeLoad(fs.readFileSync(app.conf.spec));
78+
} catch(e) {
79+
app.logger.log('warn/spec', 'Could not load the spec: ' + e);
80+
app.conf.spec = {};
81+
}
10382
}
104-
if(app.conf.csp !== false) {
105-
res.header('x-xss-protection', '1; mode=block');
106-
res.header('x-content-type-options', 'nosniff');
107-
res.header('x-frame-options', 'SAMEORIGIN');
108-
res.header('content-security-policy', app.conf.csp);
109-
res.header('x-content-security-policy', app.conf.csp);
110-
res.header('x-webkit-csp', app.conf.csp);
83+
if(!app.conf.spec.swagger) {
84+
app.conf.spec.swagger = '2.0';
85+
}
86+
if(!app.conf.spec.info) {
87+
app.conf.spec.info = {
88+
version: app.info.version,
89+
title: app.info.name,
90+
description: app.info.description
91+
};
92+
}
93+
app.conf.spec.info.version = app.info.version;
94+
if(!app.conf.spec.paths) {
95+
app.conf.spec.paths = {};
11196
}
112-
sUtil.initAndLogRequest(req, app);
113-
next();
114-
});
11597

116-
// set up the user agent header string to use for requests
117-
app.conf.user_agent = app.conf.user_agent || app.info.name;
98+
// set the CORS and CSP headers
99+
app.all('*', function(req, res, next) {
100+
if(app.conf.cors !== false) {
101+
res.header('access-control-allow-origin', app.conf.cors);
102+
res.header('access-control-allow-headers', 'accept, x-requested-with, content-type');
103+
res.header('access-control-expose-headers', 'etag');
104+
}
105+
if(app.conf.csp !== false) {
106+
res.header('x-xss-protection', '1; mode=block');
107+
res.header('x-content-type-options', 'nosniff');
108+
res.header('x-frame-options', 'SAMEORIGIN');
109+
res.header('content-security-policy', app.conf.csp);
110+
res.header('x-content-security-policy', app.conf.csp);
111+
res.header('x-webkit-csp', app.conf.csp);
112+
}
113+
sUtil.initAndLogRequest(req, app);
114+
next();
115+
});
118116

119-
// disable the X-Powered-By header
120-
app.set('x-powered-by', false);
121-
// disable the ETag header - users should provide them!
122-
app.set('etag', false);
123-
// enable compression
124-
app.use(compression({level: app.conf.compression_level}));
125-
// use the JSON body parser
126-
app.use(bodyParser.json());
127-
// use the application/x-www-form-urlencoded parser
128-
app.use(bodyParser.urlencoded({extended: true}));
117+
// set up the user agent header string to use for requests
118+
app.conf.user_agent = app.conf.user_agent || app.info.name;
129119

130-
return BBPromise.resolve(app);
120+
// disable the X-Powered-By header
121+
app.set('x-powered-by', false);
122+
// disable the ETag header - users should provide them!
123+
app.set('etag', false);
124+
// enable compression
125+
app.use(compression({level: app.conf.compression_level}));
126+
// use the JSON body parser
127+
app.use(bodyParser.json());
128+
// use the application/x-www-form-urlencoded parser
129+
app.use(bodyParser.urlencoded({extended: true}));
131130

131+
return app;
132+
});
132133
}
133134

134135

@@ -141,7 +142,7 @@ function loadRoutes (app) {
141142

142143
// get the list of files in routes/
143144
return fs.readdirAsync(__dirname + '/routes').map(function(fname) {
144-
return BBPromise.try(function() {
145+
return BBPromise.try(() => {
145146
// ... and then load each route
146147
// but only if it's a js file
147148
if(!/\.js$/.test(fname)) {
@@ -150,7 +151,8 @@ function loadRoutes (app) {
150151
// import the route file
151152
var route = require(__dirname + '/routes/' + fname);
152153
return route(app);
153-
}).then(function(route) {
154+
})
155+
.then(route => {
154156
if(route === undefined) {
155157
return undefined;
156158
}
@@ -173,11 +175,12 @@ function loadRoutes (app) {
173175
// all good, use that route
174176
app.use(route.path, route.router);
175177
});
176-
}).then(function () {
178+
})
179+
.then(() => {
177180
// catch errors
178181
sUtil.setErrorHandler(app);
179182
// route loading is now complete, return the app object
180-
return BBPromise.resolve(app);
183+
return app;
181184
});
182185

183186
}
@@ -194,20 +197,21 @@ function createServer(app) {
194197
// attaches the app to it, and starts accepting
195198
// incoming client requests
196199
var server;
197-
return new BBPromise(function (resolve) {
200+
return new BBPromise(resolve => {
198201
server = http.createServer(app).listen(
199202
app.conf.port,
200203
app.conf.interface,
201204
resolve
202205
);
203-
}).then(function () {
206+
})
207+
.then(() => {
204208
app.logger.log('info',
205209
'Worker ' + process.pid + ' listening on ' + (app.conf.interface || '*') + ':' + app.conf.port);
206210

207211
// Don't delay incomplete packets for 40ms (Linux default) on
208212
// pipelined HTTP sockets. We write in large chunks or buffers, so
209213
// lack of coalescing should not be an issue here.
210-
server.on("connection", function(socket) {
214+
server.on("connection", function (socket) {
211215
socket.setNoDelay(true);
212216
});
213217

@@ -227,7 +231,7 @@ module.exports = function(options) {
227231

228232
return initApp(options)
229233
.then(loadRoutes)
230-
.then(function(app) {
234+
.then(app => {
231235
// serve static files from static/
232236
app.use('/static', express.static(__dirname + '/static'));
233237
return app;

lib/api-util.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const HTTPError = sUtil.HTTPError;
99

1010
/**
1111
* Calls the MW API with the supplied query as its body
12-
*
1312
* @param {Object} app the application object
1413
* @param {string} domain the domain to issue the request to
1514
* @param {Object} query an object with all the query parameters for the MW API
@@ -46,7 +45,6 @@ function mwApiGet(app, domain, query) {
4645

4746
/**
4847
* Calls the REST API with the supplied domain, path and request parameters
49-
*
5048
* @param {Object} app the application object
5149
* @param {string} domain the domain to issue the request for
5250
* @param {string} path the REST API path to contact without the leading slash
@@ -80,7 +78,6 @@ function restApiGet(app, domain, path, restReq) {
8078

8179
/**
8280
* Sets up the request templates for MW and RESTBase API requests
83-
*
8481
* @param {Application} app the application object
8582
*/
8683
function setupApiTemplates(app) {

lib/util.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class HTTPError extends Error {
3838

3939
/**
4040
* Generates an object suitable for logging out of a request object
41-
*
4241
* @param {Request} req the request
4342
* @param {RegExp} whitelistRE the RegExp used to filter headers
4443
* @return {Object} an object containing the key components of the request
@@ -71,7 +70,6 @@ function reqForLog(req, whitelistRE) {
7170

7271
/**
7372
* Serialises an error object in a form suitable for logging
74-
*
7573
* @param {Error} err error to serialise
7674
* @return {Object} the serialised version of the error
7775
*/
@@ -93,8 +91,7 @@ function errForLog(err) {
9391

9492
/**
9593
* Generates a unique request ID
96-
*
97-
* @return {String} the generated request ID
94+
* @return {string} the generated request ID
9895
*/
9996
function generateRequestId() {
10097

@@ -108,7 +105,6 @@ function generateRequestId() {
108105
* promised try blocks so as to allow catching all errors,
109106
* regardless of whether a handler returns/uses promises
110107
* or not.
111-
*
112108
* @param {Object} route the object containing the router and path to bind it to
113109
* @param {Application} app the application object
114110
*/
@@ -149,7 +145,6 @@ function wrapRouteHandlers(route, app) {
149145
/**
150146
* Generates an error handler for the given applications
151147
* and installs it. Usage:
152-
*
153148
* @param {Application} app the application object to add the handler to
154149
*/
155150
function setErrorHandler(app) {
@@ -229,7 +224,6 @@ function setErrorHandler(app) {
229224

230225
/**
231226
* Creates a new router with some default options.
232-
*
233227
* @param {Object} [opts] additional options to pass to express.Router()
234228
* @return {Router} a new router object
235229
*/
@@ -250,7 +244,6 @@ function createRouter(opts) {
250244

251245
/**
252246
* Adds logger to the request and logs it
253-
*
254247
* @param {*} req request object
255248
* @param {Application} app application object
256249
*/

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
"nsp": "^2.6.3",
5252
"mocha-eslint":"^3.0.1",
5353
"eslint": "^3.12.0",
54+
"eslint-config-node-services": "^2.0.2",
5455
"eslint-plugin-json": "^1.2.0",
55-
"eslint-config-node-services": "^2.0.2"
56+
"eslint-plugin-jsdoc": "^3.0.0"
5657
},
5758
"deploy": {
5859
"target": "debian",

routes/v1.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ router.get('/siteinfo/:prop?', (req, res) => {
8585
/**
8686
* A helper function that obtains the Parsoid HTML for a given title and
8787
* loads it into a domino DOM document instance.
88-
*
89-
* @param {String} domain the domain to contact
90-
* @param {String} title the title of the page to get
88+
* @param {string} domain the domain to contact
89+
* @param {string} title the title of the page to get
9190
* @return {Promise} a promise resolving as the HTML element object
9291
*/
9392
function getBody(domain, title) {

0 commit comments

Comments
 (0)