Skip to content

Commit 5e7d387

Browse files
VadimKovalenkoSNFthesocialdev
authored andcommitted
Upgrade push notifications to nodejs 12
1) Bump eslint-config-wikimedia and fix linting errors Bug: T290573 Change-Id: I11d5ccb56c2ae4da0b2accae398362ebae435c61
1 parent bae81e2 commit 5e7d387

40 files changed

+4185
-3444
lines changed

.pipeline/blubber.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
version: v4
2-
base: docker-registry.wikimedia.org/buster-nodejs10-slim
2+
base: docker-registry.wikimedia.org/nodejs12-slim
33
lives:
44
in: /srv/service
55
runs:
66
environment: { APP_BASE_PATH: /srv/service }
77

88
variants:
99
all-dependencies:
10-
base: docker-registry.wikimedia.org/buster-nodejs10-devel
10+
base: docker-registry.wikimedia.org/nodejs12-devel
1111
copies: [local]
12-
apt: { packages: [git, build-essential, python-pkgconfig] }
12+
apt: { packages: [git, build-essential, python, pkg-config] }
1313
node: { requirements: [package.json, package-lock.json] }
1414
runs: { environment: { LINK: g++ } }
1515
build:
@@ -33,7 +33,7 @@ variants:
3333
destination: /srv/service/dist
3434
node: { env: production }
3535
production:
36-
base: docker-registry.wikimedia.org/buster-nodejs10-slim
36+
base: docker-registry.wikimedia.org/nodejs12-slim
3737
node: { requirements: [] }
3838
includes: [prep]
3939
apt: { packages: [ca-certificates] }

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: '3.7'
33
services:
44
### push-notifications ##################################
55
push-notifications:
6-
image: node:10.20.1-buster
6+
image: node:12-slim
77
# On Linux, these lines ensure file ownership is set to your host user/group
88
user: "${MW_DOCKER_UID}:${MW_DOCKER_GID}"
99
volumes:

package-lock.json

Lines changed: 2121 additions & 1383 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"@typescript-eslint/parser": "^4.1.1",
8989
"ajv": "^6.12.5",
9090
"concurrently": "^5.3.0",
91-
"eslint-config-wikimedia": "^0.17.0",
91+
"eslint-config-wikimedia": "^0.20.0",
9292
"eslint-plugin-jsdoc": "^30.5.1",
9393
"eslint-plugin-json": "^2.1.2",
9494
"extend": "^3.0.2",

src/app.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import * as loaders from './loaders';
1212
* @return {BBPromise} the promise resolving to the app object
1313
*/
1414
function initApp(options) {
15-
// the main application object
16-
const app: express.Application = express();
15+
// the main application object
16+
const app: express.Application = express();
1717

18-
// get the options and make them available in the app
19-
app.logger = options.logger; // the logging device
20-
app.metrics = options.metrics; // the metrics
21-
app.conf = options.config; // this app's config options
22-
app.info = packageInfo; // this app's package info
18+
// get the options and make them available in the app
19+
app.logger = options.logger; // the logging device
20+
app.metrics = options.metrics; // the metrics
21+
app.conf = options.config; // this app's config options
22+
app.info = packageInfo; // this app's package info
2323

24-
return loaders.init(app);
24+
return loaders.init(app);
2525
}
2626

2727
/**
@@ -31,30 +31,30 @@ function initApp(options) {
3131
* @return {BBPromise} a promise creating the web server
3232
*/
3333
function createServer(app) {
34-
// return a promise which creates an HTTP server,
35-
// attaches the app to it, and starts accepting
36-
// incoming client requests
37-
let server;
38-
return new BBPromise((resolve) => {
39-
server = http.createServer(app).listen(
40-
app.conf.port,
41-
app.conf.interface,
42-
resolve
43-
);
44-
server = addShutdown(server);
45-
}).then(() => {
46-
app.logger.log('info',
47-
`Worker ${process.pid} listening on ${app.conf.interface || '*'}:${app.conf.port}`);
34+
// return a promise which creates an HTTP server,
35+
// attaches the app to it, and starts accepting
36+
// incoming client requests
37+
let server;
38+
return new BBPromise((resolve) => {
39+
server = http.createServer(app).listen(
40+
app.conf.port,
41+
app.conf.interface,
42+
resolve
43+
);
44+
server = addShutdown(server);
45+
}).then(() => {
46+
app.logger.log('info',
47+
`Worker ${process.pid} listening on ${app.conf.interface || '*'}:${app.conf.port}`);
4848

49-
// Don't delay incomplete packets for 40ms (Linux default) on
50-
// pipelined HTTP sockets. We write in large chunks or buffers, so
51-
// lack of coalescing should not be an issue here.
52-
server.on('connection', (socket) => {
53-
socket.setNoDelay(true);
54-
});
49+
// Don't delay incomplete packets for 40ms (Linux default) on
50+
// pipelined HTTP sockets. We write in large chunks or buffers, so
51+
// lack of coalescing should not be an issue here.
52+
server.on('connection', (socket) => {
53+
socket.setNoDelay(true);
54+
});
5555

56-
return server;
57-
});
56+
return server;
57+
});
5858
}
5959

6060
/**
@@ -67,7 +67,7 @@ function createServer(app) {
6767
* @return {BBPromise} HTTP server
6868
*/
6969
module.exports = (options) => {
70-
return initApp(options).then(createServer);
70+
return initApp(options).then(createServer);
7171
};
7272

7373
export {};

src/lib/api-util.js

Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const HTTPError = sUtil.HTTPError;
77
requestLib.debug = true;
88

99
const TokenType = {
10-
CSRF: 'csrf',
11-
LOGIN: 'login'
10+
CSRF: 'csrf',
11+
LOGIN: 'login'
1212
};
1313

1414
/**
@@ -18,12 +18,12 @@ const TokenType = {
1818
* @return {!string} return host header if it exists
1919
*/
2020
function extractCookieDomain(request) {
21-
const hostname = request.headers?.host;
22-
if (!hostname) {
23-
return request.uri;
24-
}
25-
// The cookie domain needs to be an URL formatted with a protocol
26-
return `http://${hostname}`;
21+
const hostname = request.headers?.host;
22+
if (!hostname) {
23+
return request.uri;
24+
}
25+
// The cookie domain needs to be an URL formatted with a protocol
26+
return `http://${hostname}`;
2727
}
2828

2929
/**
@@ -35,49 +35,49 @@ function extractCookieDomain(request) {
3535
* @return {!Promise} a promise resolving as the response object from the MW API
3636
*/
3737
function mwApiPost(app, query, headers = {}) {
38-
const request = app.mwapi_tpl.expand({
39-
request: {
40-
headers: {
41-
'user-agent': app.conf.user_agent,
42-
'X-Forwarded-Proto': 'https'
43-
},
44-
query
45-
}
46-
});
47-
Object.assign(request.headers, headers, app.cookies?.headers || {});
48-
// Use custom cookie jar if it exists
49-
request.jar = app.cookies?.jar || true;
50-
return preq(request).then((response) => {
51-
if (app.cookies && response.headers['set-cookie']) {
52-
const cookieDomain = extractCookieDomain(request);
53-
if (cookieDomain) {
54-
const cookie = response.headers['set-cookie'].map((c) => {
55-
return app.cookies.jar.setCookie(c, cookieDomain);
56-
});
57-
// the cookies need to be passed as request headers too
58-
app.cookies.headers = { cookie };
59-
}
60-
}
61-
// Server error
62-
if (response.status < 200 || response.status > 399) {
63-
throw new HTTPError({
64-
status: response.status,
65-
type: 'api_error',
66-
title: 'MW API error',
67-
detail: response.body
68-
});
69-
}
70-
// MW API Error
71-
if (response.body.error) {
72-
throw new HTTPError({
73-
status: response.status,
74-
type: 'api_error',
75-
title: response.body.error.code,
76-
detail: response.body.error.info
77-
});
78-
}
79-
return response;
80-
});
38+
const request = app.mwapi_tpl.expand({
39+
request: {
40+
headers: {
41+
'user-agent': app.conf.user_agent,
42+
'X-Forwarded-Proto': 'https'
43+
},
44+
query
45+
}
46+
});
47+
Object.assign(request.headers, headers, app.cookies?.headers || {});
48+
// Use custom cookie jar if it exists
49+
request.jar = app.cookies?.jar || true;
50+
return preq(request).then((response) => {
51+
if (app.cookies && response.headers['set-cookie']) {
52+
const cookieDomain = extractCookieDomain(request);
53+
if (cookieDomain) {
54+
const cookie = response.headers['set-cookie'].map((c) => {
55+
return app.cookies.jar.setCookie(c, cookieDomain);
56+
});
57+
// the cookies need to be passed as request headers too
58+
app.cookies.headers = { cookie };
59+
}
60+
}
61+
// Server error
62+
if (response.status < 200 || response.status > 399) {
63+
throw new HTTPError({
64+
status: response.status,
65+
type: 'api_error',
66+
title: 'MW API error',
67+
detail: response.body
68+
});
69+
}
70+
// MW API Error
71+
if (response.body.error) {
72+
throw new HTTPError({
73+
status: response.status,
74+
type: 'api_error',
75+
title: response.body.error.code,
76+
detail: response.body.error.info
77+
});
78+
}
79+
return response;
80+
});
8181
}
8282

8383
/**
@@ -88,15 +88,15 @@ function mwApiPost(app, query, headers = {}) {
8888
* @return {!Promise}
8989
*/
9090
function mwApiGetToken(app, type = TokenType.CSRF) {
91-
return mwApiPost(app, { action: 'query', format: 'json', meta: 'tokens', type })
92-
.then((rsp) => {
93-
return rsp && rsp.body && rsp.body.query && rsp.body.query.tokens &&
91+
return mwApiPost(app, { action: 'query', format: 'json', meta: 'tokens', type })
92+
.then((rsp) => {
93+
return rsp && rsp.body && rsp.body.query && rsp.body.query.tokens &&
9494
rsp.body.query.tokens[`${type}token`];
95-
})
96-
.catch((err) => {
97-
app.logger.log('error/mwapi', err);
98-
throw err;
99-
});
95+
})
96+
.catch((err) => {
97+
app.logger.log('error/mwapi', err);
98+
throw err;
99+
});
100100
}
101101

102102
/**
@@ -106,31 +106,31 @@ function mwApiGetToken(app, type = TokenType.CSRF) {
106106
* @return {!Promise}
107107
*/
108108
function mwApiLogin(app) {
109-
if (!app.conf.mw_subscription_manager_username || !app.conf.mw_subscription_manager_password) {
110-
throw new Error('mw_subscription_manager_username and mw_subscription_manager_password' +
109+
if (!app.conf.mw_subscription_manager_username || !app.conf.mw_subscription_manager_password) {
110+
throw new Error('mw_subscription_manager_username and mw_subscription_manager_password' +
111111
' must be defined in the app configuration!');
112-
}
113-
// Everytime login is called, recreate cookie object and expose in the app object
114-
// The cookie jar and headers will only be used when executing mwApiLogin and config is enabled
115-
if (app.conf.enable_custom_cookie_jar) {
116-
app.cookies = {
117-
jar: requestLib.jar(),
118-
headers: {}
119-
};
120-
}
121-
return mwApiGetToken(app, TokenType.LOGIN).then((logintoken) => mwApiPost(
122-
app, {
123-
action: 'clientlogin',
124-
format: 'json',
125-
username: app.conf.mw_subscription_manager_username,
126-
password: app.conf.mw_subscription_manager_password,
127-
loginreturnurl: 'https://example.com',
128-
logintoken
129-
}
130-
)).catch((err) => {
131-
app.logger.log('error/login', err);
132-
throw err;
133-
});
112+
}
113+
// Everytime login is called, recreate cookie object and expose in the app object
114+
// The cookie jar and headers will only be used when executing mwApiLogin and config is enabled
115+
if (app.conf.enable_custom_cookie_jar) {
116+
app.cookies = {
117+
jar: requestLib.jar(),
118+
headers: {}
119+
};
120+
}
121+
return mwApiGetToken(app, TokenType.LOGIN).then((logintoken) => mwApiPost(
122+
app, {
123+
action: 'clientlogin',
124+
format: 'json',
125+
username: app.conf.mw_subscription_manager_username,
126+
password: app.conf.mw_subscription_manager_password,
127+
loginreturnurl: 'https://example.com',
128+
logintoken
129+
}
130+
)).catch((err) => {
131+
app.logger.log('error/login', err);
132+
throw err;
133+
});
134134
}
135135

136136
/**
@@ -139,21 +139,21 @@ function mwApiLogin(app) {
139139
* @param {!Application} app the application object
140140
*/
141141
function setupApiTemplates(app) {
142-
if (!app.conf.mwapi_req) {
143-
app.conf.mwapi_req = {
144-
method: 'post',
145-
uri: 'https://meta.wikimedia.org/w/api.php',
146-
headers: '{{request.headers}}',
147-
body: '{{ default(request.query, {}) }}'
148-
};
149-
}
150-
app.mwapi_tpl = new Template(app.conf.mwapi_req);
142+
if (!app.conf.mwapi_req) {
143+
app.conf.mwapi_req = {
144+
method: 'post',
145+
uri: 'https://meta.wikimedia.org/w/api.php',
146+
headers: '{{request.headers}}',
147+
body: '{{ default(request.query, {}) }}'
148+
};
149+
}
150+
app.mwapi_tpl = new Template(app.conf.mwapi_req);
151151
}
152152

153153
module.exports = {
154-
mwApiGetToken,
155-
mwApiLogin,
156-
mwApiPost,
157-
setupApiTemplates,
158-
extractCookieDomain
154+
mwApiGetToken,
155+
mwApiLogin,
156+
mwApiPost,
157+
setupApiTemplates,
158+
extractCookieDomain
159159
};

0 commit comments

Comments
 (0)