Skip to content

Commit

Permalink
Merge pull request #681 from Pchelolo/syntax_to_es6
Browse files Browse the repository at this point in the history
Syntax to es6
  • Loading branch information
gwicke committed Oct 3, 2016
2 parents 3f433b1 + e232620 commit 086b05a
Show file tree
Hide file tree
Showing 26 changed files with 2,810 additions and 2,990 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends: 'eslint-config-node-services'
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "6"

Expand Down
53 changes: 24 additions & 29 deletions lib/access_check_filter.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
'use strict';

var mwUtil = require('./mwUtil');
var P = require('bluebird');
var URI = require('hyperswitch').URI;
const mwUtil = require('./mwUtil');
const P = require('bluebird');
const URI = require('hyperswitch').URI;

module.exports = function(hyper, req, next, options, specInfo) {
var rp = req.params;
var titleParamName = options.title ? options.title : 'title';
var checkURIParts = [rp.domain, 'sys', 'page_revisions', 'restrictions', rp.title];
module.exports = (hyper, req, next, options, specInfo) => {
const rp = req.params;
const titleParamName = options.title || 'title';
const checkURIParts = [rp.domain, 'sys', 'page_revisions', 'restrictions', rp.title];
if (rp.revision) {
checkURIParts.push(rp.revision + '');
checkURIParts.push(`${rp.revision}`);
}

return P.join(
next(hyper, req),
hyper.get({ uri: new URI(checkURIParts) })
.catch({ status: 404 }, function() {
return null;
})
.catch({ status: 404 }, () => null)
)
.spread(function(content, restriction) {
.spread((content, restriction) => {
if (restriction || content.headers.location) {
if (restriction && restriction.body) {
var revInfo = mwUtil.parseETag(content.headers.etag);
const revInfo = mwUtil.parseETag(content.headers.etag);
mwUtil.applyAccessChecks(restriction.body, revInfo.rev);
}

// Use redirect target from restrictions table or content response.
var redirectTarget = restriction && restriction.body
const redirectTarget = restriction && restriction.body
&& restriction.body.redirect || content.headers.location;
if (redirectTarget
&& req.query.redirect !== false
&& !mwUtil.isNoCacheRequest(req)) {
var newParams = Object.assign({}, rp);
const newParams = Object.assign({}, rp);
newParams[titleParamName] = redirectTarget;
var location = mwUtil.createRelativeTitleRedirect(specInfo.path,
let location = mwUtil.createRelativeTitleRedirect(specInfo.path,
req, {
newReqParams: newParams,
titleParamName: titleParamName,
titleParamName,
dropPathAfterTitle: true,
});

if (mwUtil.isSelfRedirect(req, location)) {
location = mwUtil.addQueryString(location, { redirect: false });
}

var contentPromise;
let contentPromise;
if (options.attach_body_to_redirect) {
contentPromise = P.resolve(content);
} else {
Expand All @@ -55,19 +53,16 @@ module.exports = function(hyper, req, next, options, specInfo) {
}
});
}
return contentPromise.then(function(content) {
return {
status: 302,
headers: Object.assign(content.headers, {
location: location,
'cache-control': options.redirect_cache_control || 'no-cache'
}),
body: content.body
};
});
return contentPromise.then((theContent) => ({
status: 302,
headers: Object.assign(theContent.headers, {
location,
'cache-control': options.redirect_cache_control || 'no-cache'
}),
body: theContent.body
}));
}
}

return content;
});
};
29 changes: 13 additions & 16 deletions lib/ensure_content_type.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

var cType = require('content-type');
var mwUtil = require('./mwUtil');
const cType = require('content-type');
const mwUtil = require('./mwUtil');

// Utility function to split path & version suffix from a profile parameter.
function splitProfile(profile) {
var match = /^(.*)\/([0-9\.]+)$/.exec(profile);
const match = /^(.*)\/([0-9\.]+)$/.exec(profile);
return {
path: match[1],
version: match[2],
Expand All @@ -23,7 +23,7 @@ function splitProfile(profile) {

function checkContentType(hyper, req, next, expectedContentType, responsePromise) {
return responsePromise
.then(function(res) {
.then((res) => {
// Do not check or re-render if the response was only rendered within
// the last two minutes.
if (res.headers && res.headers.etag
Expand All @@ -45,16 +45,16 @@ function checkContentType(hyper, req, next, expectedContentType, responsePromise

if (res.headers && res.headers['content-type'] !== expectedContentType) {
// Parse the expected & response content type, and compare profiles.
var expectedProfile = cType.parse(expectedContentType).parameters.profile;
var actualProfile = cType.parse(res.headers['content-type']).parameters.profile;
const expectedProfile = cType.parse(expectedContentType).parameters.profile;
const actualProfile = cType.parse(res.headers['content-type']).parameters.profile;

if (actualProfile && actualProfile !== expectedProfile) {
if (!expectedProfile) {
return updateSpecWarning();
}
// Check if actual content type is newer than the spec
var actualProfileParts = splitProfile(actualProfile);
var expectedProfileParts = splitProfile(expectedProfile);
const actualProfileParts = splitProfile(actualProfile);
const expectedProfileParts = splitProfile(expectedProfile);
if (actualProfileParts.path === expectedProfileParts.path
&& actualProfileParts.version > expectedProfileParts.version) {
return updateSpecWarning();
Expand All @@ -64,8 +64,7 @@ function checkContentType(hyper, req, next, expectedContentType, responsePromise
// Re-try request with no-cache header
if (!mwUtil.isNoCacheRequest(req)) {
req.headers['cache-control'] = 'no-cache';
return checkContentType(hyper, req, next,
expectedContentType, next(hyper, req));
return checkContentType(hyper, req, next, expectedContentType, next(hyper, req));
} else {
// Log issue
hyper.log('warn/content-type/upgrade_failed', {
Expand All @@ -77,18 +76,16 @@ function checkContentType(hyper, req, next, expectedContentType, responsePromise
res.headers['cache-control'] = 'max-age=10, s-maxage=10';
}
}

// Default: Just return.
return res;
});
}


module.exports = function(hyper, req, next, options, specInfo) {
var rp = req.params;
var produces = specInfo.spec.produces;
var expectedContentType = Array.isArray(produces) && produces[0];
var responsePromise = next(hyper, req);
module.exports = (hyper, req, next, options, specInfo) => {
const produces = specInfo.spec.produces;
const expectedContentType = Array.isArray(produces) && produces[0];
const responsePromise = next(hyper, req);
if (expectedContentType) {
// Found a content type. Ensure that we return the latest profile.
return checkContentType(hyper, req, next, expectedContentType, responsePromise);
Expand Down
25 changes: 11 additions & 14 deletions lib/mediawiki_auth_filter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"use strict";

var HyperSwitch = require('hyperswitch');
var HTTPError = HyperSwitch.HTTPError;
var URI = HyperSwitch.URI;
var P = require('bluebird');
const HyperSwitch = require('hyperswitch');
const HTTPError = HyperSwitch.HTTPError;
const URI = HyperSwitch.URI;

function copyForwardedHeaders(req, rootReq, headersLins) {
if (rootReq.headers) {
req.headers = req.headers || {};
headersLins.forEach(function(header) {
headersLins.forEach((header) => {
if (!req.headers[header] && rootReq.headers[header]) {
req.headers[header] = rootReq.headers[header];
}
Expand All @@ -26,17 +25,17 @@ function checkPermissions(hyper, req, permissions) {
uiprop: 'rights'
}
})
.then(function(userInfo) {
.then((userInfo) => {
userInfo = userInfo.body;
if (userInfo && userInfo.rights && Array.isArray(userInfo.rights)) {
permissions.forEach(function(perm) {
permissions.forEach((perm) => {
if (userInfo.rights.indexOf(perm) < 0) {
throw new HTTPError({
status: 401,
body: {
type: 'unauthorized',
title: 'Not authorized to access the resource',
description: 'Need permission ' + perm
description: `Need permission ${perm}`
}
});
}
Expand All @@ -53,7 +52,7 @@ function checkPermissions(hyper, req, permissions) {
});
}

module.exports = function(hyper, req, next, options) {
module.exports = (hyper, req, next, options) => {
if (hyper._isSysRequest(req)) {
return next(hyper, req);
}
Expand All @@ -66,13 +65,11 @@ module.exports = function(hyper, req, next, options) {
}

return checkPermissions(hyper, req, options.permissions)
.then(function() {
return next(hyper, req);
})
.then(function(res) {
.then(() => next(hyper, req))
.then((res) => {
if (res.headers) {
res.headers['cache-control'] = 'no-cache';
}
return res;
});
};
};
Loading

0 comments on commit 086b05a

Please sign in to comment.