From 109585d20c70a2c246a83b970b8e71487a75f62b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Mon, 15 Jul 2019 15:09:04 +0200 Subject: [PATCH] Merge pull request #1947 from overleaf/ta-cleanup-guard-1 Remove __guard__ Function Used to Access Settings GitOrigin-RevId: 15e3749990a9fc68f8d344390b1bf0d09d839106 --- services/web/app.js | 16 +-- .../Authentication/AuthenticationManager.js | 94 ++++---------- .../app/src/Features/Blog/BlogController.js | 12 +- .../BrandVariations/BrandVariationsHandler.js | 14 +- .../src/Features/Compile/CompileManager.js | 9 +- .../app/src/Features/Email/EmailBuilder.js | 18 +-- .../web/app/src/Features/Email/EmailSender.js | 122 +++++------------- .../web/app/src/Features/Helpers/UrlHelper.js | 13 +- .../Features/Institutions/InstitutionsAPI.js | 14 +- .../Notifications/NotificationsBuilder.js | 17 +-- .../src/Features/Project/ProjectController.js | 11 +- .../Project/ProjectCreationHandler.js | 14 +- .../Features/References/ReferencesHandler.js | 14 +- .../Features/Subscription/RecurlyWrapper.js | 6 +- .../SubscriptionViewModelBuilder.js | 5 +- .../Subscription/V1SubscriptionManager.js | 2 +- .../src/Features/User/UserPagesController.js | 16 +-- .../web/app/src/Features/User/UserUpdater.js | 14 +- services/web/app/src/Features/V1/V1Api.js | 27 +--- .../app/src/infrastructure/ExpressLocals.js | 25 +--- .../web/app/src/infrastructure/Features.js | 17 +-- services/web/config/settings.defaults.coffee | 2 +- services/web/test/smoke/src/SmokeTests.js | 18 +-- .../AuthorizationManagerTests.js | 3 +- .../test/unit/src/Blog/BlogControllerTests.js | 3 +- .../unit/src/Compile/CompileManagerTests.js | 3 +- .../src/Institutions/InstitutionsAPITests.js | 5 +- .../src/Project/ProjectControllerTests.js | 3 +- .../V1SusbcriptionManagerTests.js | 3 +- 29 files changed, 119 insertions(+), 401 deletions(-) diff --git a/services/web/app.js b/services/web/app.js index 2b92330cd9..819ad05c12 100644 --- a/services/web/app.js +++ b/services/web/app.js @@ -6,7 +6,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -43,14 +42,7 @@ if (Settings.catchErrors) { logger.error({ err: error }, 'uncaughtException') ) } - -const port = - Settings.port || - __guard__( - Settings.internal != null ? Settings.internal.web : undefined, - x => x.port - ) || - 3000 +const port = Settings.port || Settings.internal.web.port || 3000 const host = Settings.internal.web.host || 'localhost' if (!module.parent) { // Called directly @@ -69,9 +61,3 @@ if (!module.parent) { } module.exports = Server.server - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/app/src/Features/Authentication/AuthenticationManager.js b/services/web/app/src/Features/Authentication/AuthenticationManager.js index fafc76ade1..5f75a8e67b 100644 --- a/services/web/app/src/Features/Authentication/AuthenticationManager.js +++ b/services/web/app/src/Features/Authentication/AuthenticationManager.js @@ -9,7 +9,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS202: Simplify dynamic range loops * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md @@ -25,13 +24,8 @@ const Errors = require('../Errors/Errors') const UserGetter = require('../User/UserGetter') const V1Handler = require('../V1/V1Handler') -const BCRYPT_ROUNDS = - __guard__( - Settings != null ? Settings.security : undefined, - x => x.bcryptRounds - ) || 12 -const BCRYPT_MINOR_VERSION = - (Settings != null ? Settings.security.bcryptMinorVersion : undefined) || 'a' +const BCRYPT_ROUNDS = Settings.security.bcryptRounds || 12 +const BCRYPT_MINOR_VERSION = Settings.security.bcryptMinorVersion || 'a' const _checkWriteResult = function(result, callback) { // for MongoDB @@ -107,24 +101,17 @@ module.exports = AuthenticationManager = { return { message: 'password not set' } } - const allowAnyChars = - (Settings.passwordStrengthOptions != null - ? Settings.passwordStrengthOptions.allowAnyChars - : undefined) === true - const min = - __guard__( - Settings.passwordStrengthOptions != null - ? Settings.passwordStrengthOptions.length - : undefined, - x1 => x1.min - ) || 6 - let max = - __guard__( - Settings.passwordStrengthOptions != null - ? Settings.passwordStrengthOptions.length - : undefined, - x2 => x2.max - ) || 72 + let allowAnyChars, min, max + if (Settings.passwordStrengthOptions) { + allowAnyChars = Settings.passwordStrengthOptions.allowAnyChars === true + if (Settings.passwordStrengthOptions.length) { + min = Settings.passwordStrengthOptions.length.min + max = Settings.passwordStrengthOptions.length.max + } + } + allowAnyChars = !!allowAnyChars + min = min || 6 + max = max || 72 // we don't support passwords > 72 characters in length, because bcrypt truncates them if (max > 72) { @@ -196,12 +183,7 @@ module.exports = AuthenticationManager = { if (callback == null) { callback = function(error) {} } - if ( - __guard__( - Settings != null ? Settings.security : undefined, - x1 => x1.disableBcryptRoundsUpgrades - ) - ) { + if (Settings.security.disableBcryptRoundsUpgrades) { return callback() } // check current number of rounds and rehash if necessary @@ -274,34 +256,20 @@ module.exports = AuthenticationManager = { }, _passwordCharactersAreValid(password) { - const digits = - __guard__( - Settings.passwordStrengthOptions != null - ? Settings.passwordStrengthOptions.chars - : undefined, - x1 => x1.digits - ) || '1234567890' - const letters = - __guard__( - Settings.passwordStrengthOptions != null - ? Settings.passwordStrengthOptions.chars - : undefined, - x2 => x2.letters - ) || 'abcdefghijklmnopqrstuvwxyz' - const letters_up = - __guard__( - Settings.passwordStrengthOptions != null - ? Settings.passwordStrengthOptions.chars - : undefined, - x3 => x3.letters_up - ) || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' - const symbols = - __guard__( - Settings.passwordStrengthOptions != null - ? Settings.passwordStrengthOptions.chars - : undefined, - x4 => x4.symbols - ) || '@#$%^&*()-_=+[]{};:<>/?!£€.,' + let digits, letters, letters_up, symbols + if ( + Settings.passwordStrengthOptions && + Settings.passwordStrengthOptions.chars + ) { + digits = Settings.passwordStrengthOptions.chars.digits + letters = Settings.passwordStrengthOptions.chars.letters + letters_up = Settings.passwordStrengthOptions.chars.letters_up + symbols = Settings.passwordStrengthOptions.chars.symbols + } + digits = digits || '1234567890' + letters = letters || 'abcdefghijklmnopqrstuvwxyz' + letters_up = letters_up || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + symbols = symbols || '@#$%^&*()-_=+[]{};:<>/?!£€.,' for ( let charIndex = 0, end = password.length - 1, asc = end >= 0; @@ -320,9 +288,3 @@ module.exports = AuthenticationManager = { return true } } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/app/src/Features/Blog/BlogController.js b/services/web/app/src/Features/Blog/BlogController.js index 465c9b3179..587ea6329b 100644 --- a/services/web/app/src/Features/Blog/BlogController.js +++ b/services/web/app/src/Features/Blog/BlogController.js @@ -56,22 +56,14 @@ module.exports = BlogController = { data = data.trim() try { data = JSON.parse(data) - if ( - __guard__( - settings.cdn != null ? settings.cdn.web : undefined, - x => x.host - ) != null - ) { + if (settings.cdn && settings.cdn.web && settings.cdn.web.host) { if (data != null) { data.content = __guard__( data != null ? data.content : undefined, x1 => x1.replace( /src="(\/[^"]+)"/g, - `src='${__guard__( - settings.cdn != null ? settings.cdn.web : undefined, - x2 => x2.host - )}$1'` + `src='${settings.cdn.web.host}$1'` ) ) } diff --git a/services/web/app/src/Features/BrandVariations/BrandVariationsHandler.js b/services/web/app/src/Features/BrandVariations/BrandVariationsHandler.js index 715fe2bc5e..f36f0e4e5a 100644 --- a/services/web/app/src/Features/BrandVariations/BrandVariationsHandler.js +++ b/services/web/app/src/Features/BrandVariations/BrandVariationsHandler.js @@ -9,7 +9,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -84,15 +83,4 @@ var _setV1AsHostIfRelativeURL = urlString => // As it only applies if the second argument is not absolute, we can use it to transform relative URLs into // absolute ones using v1 as the host. If the URL is absolute (e.g. a filepicker one), then the base // argument is just ignored - url.resolve( - __guard__( - __guard__(settings != null ? settings.apis : undefined, x1 => x1.v1), - x => x.url - ), - urlString - ) -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} + url.resolve(settings.apis.v1.url, urlString) diff --git a/services/web/app/src/Features/Compile/CompileManager.js b/services/web/app/src/Features/Compile/CompileManager.js index ecc21e55f4..f48b7ab4c0 100644 --- a/services/web/app/src/Features/Compile/CompileManager.js +++ b/services/web/app/src/Features/Compile/CompileManager.js @@ -245,14 +245,7 @@ module.exports = CompileManager = { endpointName: 'auto_compile', timeInterval: 20, subjectName: compileGroup, - throttle: - __guard__( - __guard__( - Settings != null ? Settings.rateLimit : undefined, - x1 => x1.autoCompile - ), - x => x[compileGroup] - ) || 25 + throttle: Settings.rateLimit.autoCompile[compileGroup] || 25 } return rateLimiter.addCount(opts, function(err, canCompile) { if (err != null) { diff --git a/services/web/app/src/Features/Email/EmailBuilder.js b/services/web/app/src/Features/Email/EmailBuilder.js index 67fa99d895..033b338796 100644 --- a/services/web/app/src/Features/Email/EmailBuilder.js +++ b/services/web/app/src/Features/Email/EmailBuilder.js @@ -7,7 +7,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -518,15 +517,11 @@ module.exports = { opts.siteUrl = settings.siteUrl opts.body = template.compiledTemplate(opts) if ( - __guard__( - settings.email != null ? settings.email.templates : undefined, - x => x.customFooter - ) != null + settings.email && + settings.email.template && + settings.email.template.customFooter ) { - opts.body += __guard__( - settings.email != null ? settings.email.templates : undefined, - x1 => x1.customFooter - ) + opts.body += settings.email.template.customFooter } return { subject: template.subject(opts), @@ -538,11 +533,6 @@ module.exports = { } } -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} function __guardMethod__(obj, methodName, transform) { if ( typeof obj !== 'undefined' && diff --git a/services/web/app/src/Features/Email/EmailSender.js b/services/web/app/src/Features/Email/EmailSender.js index 5ce8c97025..0cb8699fc0 100644 --- a/services/web/app/src/Features/Email/EmailSender.js +++ b/services/web/app/src/Features/Email/EmailSender.js @@ -10,7 +10,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -41,86 +40,41 @@ let client = { return callback() } } -if ( - __guard__( - __guard__( - Settings != null ? Settings.email : undefined, - x1 => x1.parameters - ), - x => x.AWSAccessKeyID - ) != null || - __guard__(Settings != null ? Settings.email : undefined, x2 => x2.driver) === - 'ses' -) { - logger.log('using aws ses for email') - nm_client = nodemailer.createTransport( - sesTransport(Settings.email.parameters) - ) -} else if ( - __guard__( - __guard__( - Settings != null ? Settings.email : undefined, - x4 => x4.parameters - ), - x3 => x3.sendgridApiKey - ) != null -) { - logger.log('using sendgrid for email') - nm_client = nodemailer.createTransport( - sgTransport({ - auth: { - api_key: __guard__( - __guard__( - Settings != null ? Settings.email : undefined, - x6 => x6.parameters - ), - x5 => x5.sendgridApiKey - ) - } - }) - ) -} else if ( - __guard__( - __guard__( - Settings != null ? Settings.email : undefined, - x8 => x8.parameters - ), - x7 => x7.MandrillApiKey - ) != null -) { - logger.log('using mandril for email') - nm_client = nodemailer.createTransport( - mandrillTransport({ - auth: { - apiKey: __guard__( - __guard__( - Settings != null ? Settings.email : undefined, - x10 => x10.parameters - ), - x9 => x9.MandrillApiKey - ) - } - }) - ) -} else if ( - __guard__( - Settings != null ? Settings.email : undefined, - x11 => x11.parameters - ) != null -) { - logger.log('using smtp for email') - const smtp = _.pick( - __guard__( - Settings != null ? Settings.email : undefined, - x12 => x12.parameters - ), - 'host', - 'port', - 'secure', - 'auth', - 'ignoreTLS' - ) - nm_client = nodemailer.createTransport(smtp) +if (Settings.email && Settings.email.parameters) { + let emailParameters = Settings.email.parameters + if (emailParameters.AWSAccessKeyID || Settings.email.driver === 'ses') { + logger.log('using aws ses for email') + nm_client = nodemailer.createTransport(sesTransport(emailParameters)) + } else if (emailParameters.sendgridApiKey) { + logger.log('using sendgrid for email') + nm_client = nodemailer.createTransport( + sgTransport({ + auth: { + api_key: emailParameters.sendgridApiKey + } + }) + ) + } else if (emailParameters.MandrillApiKey) { + logger.log('using mandril for email') + nm_client = nodemailer.createTransport( + mandrillTransport({ + auth: { + apiKey: emailParameters.MandrillApiKey + } + }) + ) + } else { + logger.log('using smtp for email') + const smtp = _.pick( + emailParameters, + 'host', + 'port', + 'secure', + 'auth', + 'ignoreTLS' + ) + nm_client = nodemailer.createTransport(smtp) + } } else { logger.warn( 'Email transport and/or parameters not defined. No emails will be sent.' @@ -200,9 +154,3 @@ module.exports = { }) } } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/app/src/Features/Helpers/UrlHelper.js b/services/web/app/src/Features/Helpers/UrlHelper.js index c8d450a605..384f72ccae 100644 --- a/services/web/app/src/Features/Helpers/UrlHelper.js +++ b/services/web/app/src/Features/Helpers/UrlHelper.js @@ -6,7 +6,6 @@ // Fix any style issues and re-enable lint. /* * decaffeinate suggestions: - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -16,12 +15,7 @@ const Settings = require('settings-sharelatex') module.exports = UrlHelper = { wrapUrlWithProxy(url) { // TODO: Consider what to do for Community and Enterprise edition? - if ( - __guard__( - Settings.apis != null ? Settings.apis.linkedUrlProxy : undefined, - x => x.url - ) == null - ) { + if (!Settings.apis.linkedUrlProxy.url) { throw new Error('no linked url proxy configured') } return `${Settings.apis.linkedUrlProxy.url}?url=${encodeURIComponent(url)}` @@ -34,8 +28,3 @@ module.exports = UrlHelper = { return url } } -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/app/src/Features/Institutions/InstitutionsAPI.js b/services/web/app/src/Features/Institutions/InstitutionsAPI.js index 995f2db54c..c16c966547 100644 --- a/services/web/app/src/Features/Institutions/InstitutionsAPI.js +++ b/services/web/app/src/Features/Institutions/InstitutionsAPI.js @@ -8,7 +8,6 @@ * decaffeinate suggestions: * DS101: Remove unnecessary use of Array.from * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -150,12 +149,7 @@ var makeAffiliationRequest = function(requestOptions, callback) { if (callback == null) { callback = function(error) {} } - if ( - !__guard__( - __guard__(settings != null ? settings.apis : undefined, x1 => x1.v1), - x => x.url - ) - ) { + if (!settings.apis.v1.url) { return callback(null) } // service is not configured if (!requestOptions.extraSuccessStatusCodes) { @@ -214,9 +208,3 @@ var makeAffiliationRequest = function(requestOptions, callback) { logger ) ) - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/app/src/Features/Notifications/NotificationsBuilder.js b/services/web/app/src/Features/Notifications/NotificationsBuilder.js index 5dce932301..48be7cb551 100644 --- a/services/web/app/src/Features/Notifications/NotificationsBuilder.js +++ b/services/web/app/src/Features/Notifications/NotificationsBuilder.js @@ -7,7 +7,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -119,15 +118,7 @@ module.exports = { if (callback == null) { callback = function() {} } - if ( - !__guard__( - __guard__( - settings != null ? settings.apis : undefined, - x1 => x1.v1 - ), - x => x.url - ) - ) { + if (!settings.apis.v1.url) { return null } // service is not configured return request( @@ -179,9 +170,3 @@ module.exports = { } } } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/app/src/Features/Project/ProjectController.js b/services/web/app/src/Features/Project/ProjectController.js index 7ed756f4c1..9df2b1c58b 100644 --- a/services/web/app/src/Features/Project/ProjectController.js +++ b/services/web/app/src/Features/Project/ProjectController.js @@ -427,14 +427,9 @@ module.exports = ProjectController = { } if ( - __guard__( - Settings != null ? Settings.algolia : undefined, - x => x.app_id - ) != null && - __guard__( - Settings != null ? Settings.algolia : undefined, - x1 => x1.read_only_api_key - ) != null + Settings.algolia && + Settings.algolia.app_id && + Settings.algolia.read_only_api_key ) { viewModel.showUserDetailsArea = true viewModel.algolia_api_key = Settings.algolia.read_only_api_key diff --git a/services/web/app/src/Features/Project/ProjectCreationHandler.js b/services/web/app/src/Features/Project/ProjectCreationHandler.js index 6074844ee7..4b8c270649 100644 --- a/services/web/app/src/Features/Project/ProjectCreationHandler.js +++ b/services/web/app/src/Features/Project/ProjectCreationHandler.js @@ -9,7 +9,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -104,12 +103,7 @@ const ProjectCreationHandler = { Object.assign(project, attributes) - if ( - __guard__( - Settings.apis != null ? Settings.apis.project_history : undefined, - x => x.displayHistoryForNewProjects - ) - ) { + if (Settings.apis.project_history.displayHistoryForNewProjects) { project.overleaf.history.display = true } if (Settings.currentImageName != null) { @@ -338,12 +332,6 @@ metrics.timeAsyncMethod( logger ) -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} - const promises = { createBlankProject: promisify(ProjectCreationHandler.createBlankProject) } diff --git a/services/web/app/src/Features/References/ReferencesHandler.js b/services/web/app/src/Features/References/ReferencesHandler.js index 323248e9f7..974a15be1e 100644 --- a/services/web/app/src/Features/References/ReferencesHandler.js +++ b/services/web/app/src/Features/References/ReferencesHandler.js @@ -25,12 +25,7 @@ const Async = require('async') const oneMinInMs = 60 * 1000 const fiveMinsInMs = oneMinInMs * 5 -if ( - __guard__( - settings.apis != null ? settings.apis.references : undefined, - x => x.url - ) == null -) { +if (!settings.apis.references.url) { logger.log('references search not enabled') } @@ -149,12 +144,7 @@ module.exports = ReferencesHandler = { }, _doIndexOperation(projectId, project, docIds, fileIds, callback) { - if ( - __guard__( - settings.apis != null ? settings.apis.references : undefined, - x1 => x1.url - ) == null - ) { + if (!settings.apis.references.url) { return callback() } return ReferencesHandler._isFullIndex(project, function(err, isFullIndex) { diff --git a/services/web/app/src/Features/Subscription/RecurlyWrapper.js b/services/web/app/src/Features/Subscription/RecurlyWrapper.js index a0cc20bdb3..ccf80fcba5 100644 --- a/services/web/app/src/Features/Subscription/RecurlyWrapper.js +++ b/services/web/app/src/Features/Subscription/RecurlyWrapper.js @@ -26,11 +26,7 @@ const logger = require('logger-sharelatex') const Async = require('async') module.exports = RecurlyWrapper = { - apiUrl: - __guard__( - Settings.apis != null ? Settings.apis.recurly : undefined, - x => x.url - ) || 'https://api.recurly.com/v2', + apiUrl: Settings.apis.recurly.url || 'https://api.recurly.com/v2', _paypal: { checkAccountExists(cache, next) { diff --git a/services/web/app/src/Features/Subscription/SubscriptionViewModelBuilder.js b/services/web/app/src/Features/Subscription/SubscriptionViewModelBuilder.js index c34812d479..f59c0b55c0 100644 --- a/services/web/app/src/Features/Subscription/SubscriptionViewModelBuilder.js +++ b/services/web/app/src/Features/Subscription/SubscriptionViewModelBuilder.js @@ -33,10 +33,7 @@ const buildBillingDetails = function(recurlySubscription) { recurlySubscription != null ? recurlySubscription.account : undefined, x => x.hosted_login_token ) - const recurlySubdomain = __guard__( - __guard__(Settings != null ? Settings.apis : undefined, x2 => x2.recurly), - x1 => x1.subdomain - ) + const recurlySubdomain = Settings.apis.recurly.subdomain if (hostedLoginToken != null && recurlySubdomain != null) { return [ 'https://', diff --git a/services/web/app/src/Features/Subscription/V1SubscriptionManager.js b/services/web/app/src/Features/Subscription/V1SubscriptionManager.js index 3602e2c93b..801f327b05 100644 --- a/services/web/app/src/Features/Subscription/V1SubscriptionManager.js +++ b/services/web/app/src/Features/Subscription/V1SubscriptionManager.js @@ -154,7 +154,7 @@ module.exports = V1SubscriptionManager = { if (callback == null) { callback = function(err, body, v1Id) {} } - if (!__guard__(settings != null ? settings.apis : undefined, x => x.v1)) { + if (!settings.apis.v1.url) { return callback(null, null) } diff --git a/services/web/app/src/Features/User/UserPagesController.js b/services/web/app/src/Features/User/UserPagesController.js index 932029a922..cd5443a7d7 100644 --- a/services/web/app/src/Features/User/UserPagesController.js +++ b/services/web/app/src/Features/User/UserPagesController.js @@ -126,15 +126,13 @@ module.exports = UserPagesController = { delete req.session.ssoError } logger.log({ user: user_id }, 'loading settings page') - const shouldAllowEditingDetails = - !__guard__( - Settings != null ? Settings.ldap : undefined, - x => x.updateUserDetailsOnLogin - ) && - !__guard__( - Settings != null ? Settings.saml : undefined, - x1 => x1.updateUserDetailsOnLogin - ) + let shouldAllowEditingDetails = true + if (Settings.ldap && Settings.ldap.updateUserDetailsOnLogin) { + shouldAllowEditingDetails = false + } + if (Settings.saml && Settings.saml.updateUserDetailsOnLogin) { + shouldAllowEditingDetails = false + } const oauthProviders = Settings.oauthProviders || {} return UserGetter.getUser(user_id, function(err, user) { diff --git a/services/web/app/src/Features/User/UserUpdater.js b/services/web/app/src/Features/User/UserUpdater.js index 3a2f466780..dffd8e05b1 100644 --- a/services/web/app/src/Features/User/UserUpdater.js +++ b/services/web/app/src/Features/User/UserUpdater.js @@ -10,7 +10,6 @@ * decaffeinate suggestions: * DS101: Remove unnecessary use of Array.from * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -193,12 +192,7 @@ module.exports = UserUpdater = { }, updateEmailAddressInV1(userId, newEmail, callback) { - if ( - __guard__( - Settings.apis != null ? Settings.apis.v1 : undefined, - x => x.url - ) == null - ) { + if (!Settings.apis.v1.url) { return callback() } return UserGetter.getUser(userId, { 'overleaf.id': 1, emails: 1 }, function( @@ -331,9 +325,3 @@ module.exports = UserUpdater = { ].map(method => metrics.timeAsyncMethod(UserUpdater, method, 'mongo.UserUpdater', logger) ) - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/app/src/Features/V1/V1Api.js b/services/web/app/src/Features/V1/V1Api.js index 22c12e4320..a0e93ee2ae 100644 --- a/services/web/app/src/Features/V1/V1Api.js +++ b/services/web/app/src/Features/V1/V1Api.js @@ -7,7 +7,6 @@ * decaffeinate suggestions: * DS101: Remove unnecessary use of Array.from * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -18,19 +17,10 @@ const Errors = require('../Errors/Errors') // TODO: check what happens when these settings aren't defined const DEFAULT_V1_PARAMS = { - baseUrl: __guard__( - __guard__(settings != null ? settings.apis : undefined, x1 => x1.v1), - x => x.url - ), + baseUrl: settings.apis.v1.url, auth: { - user: __guard__( - __guard__(settings != null ? settings.apis : undefined, x3 => x3.v1), - x2 => x2.user - ), - pass: __guard__( - __guard__(settings != null ? settings.apis : undefined, x5 => x5.v1), - x4 => x4.pass - ) + user: settings.apis.v1.user, + pass: settings.apis.v1.pass }, json: true, timeout: 30 * 1000 @@ -39,10 +29,7 @@ const DEFAULT_V1_PARAMS = { const v1Request = request.defaults(DEFAULT_V1_PARAMS) const DEFAULT_V1_OAUTH_PARAMS = { - baseUrl: __guard__( - __guard__(settings != null ? settings.apis : undefined, x7 => x7.v1), - x6 => x6.url - ), + baseUrl: settings.apis.v1.url, json: true, timeout: 30 * 1000 } @@ -98,9 +85,3 @@ module.exports = V1Api = { } } } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/app/src/infrastructure/ExpressLocals.js b/services/web/app/src/infrastructure/ExpressLocals.js index 9ab0a49283..1f7d4ff8b4 100644 --- a/services/web/app/src/infrastructure/ExpressLocals.js +++ b/services/web/app/src/infrastructure/ExpressLocals.js @@ -94,14 +94,10 @@ if (!Settings.useMinifiedJs) { } } -const cdnAvailable = - __guard__(Settings.cdn != null ? Settings.cdn.web : undefined, x => x.host) != - null +const cdnAvailable = Settings.cdn && Settings.cdn.web && !!Settings.cdn.web.host + const darkCdnAvailable = - __guard__( - Settings.cdn != null ? Settings.cdn.web : undefined, - x1 => x1.darkHost - ) != null + Settings.cdn && Settings.cdn.web && !!Settings.cdn.web.darkHost module.exports = function(app, webRouter, privateApiRouter, publicApiRouter) { webRouter.use(function(req, res, next) { @@ -167,15 +163,9 @@ module.exports = function(app, webRouter, privateApiRouter, publicApiRouter) { const isLive = !isDark && !isSmoke if (cdnAvailable && isLive && !cdnBlocked) { - staticFilesBase = __guard__( - Settings.cdn != null ? Settings.cdn.web : undefined, - x6 => x6.host - ) + staticFilesBase = Settings.cdn.web.host } else if (darkCdnAvailable && isDark) { - staticFilesBase = __guard__( - Settings.cdn != null ? Settings.cdn.web : undefined, - x7 => x7.darkHost - ) + staticFilesBase = Settings.cdn.web.darkHost } else { staticFilesBase = '' } @@ -428,10 +418,7 @@ module.exports = function(app, webRouter, privateApiRouter, publicApiRouter) { delete req.session.justLoggedIn } } - res.locals.gaToken = __guard__( - Settings.analytics != null ? Settings.analytics.ga : undefined, - x2 => x2.token - ) + res.locals.gaToken = Settings.analytics && Settings.analytics.ga.token res.locals.tenderUrl = Settings.tenderUrl res.locals.sentrySrc = Settings.sentry != null ? Settings.sentry.src : undefined diff --git a/services/web/app/src/infrastructure/Features.js b/services/web/app/src/infrastructure/Features.js index 6a0a2f6934..d0bbd9d29f 100644 --- a/services/web/app/src/infrastructure/Features.js +++ b/services/web/app/src/infrastructure/Features.js @@ -6,7 +6,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -44,15 +43,7 @@ module.exports = Features = { case 'view-templates': return Settings.overleaf == null case 'affiliations': - return ( - __guard__( - __guard__( - Settings != null ? Settings.apis : undefined, - x1 => x1.v1 - ), - x => x.url - ) != null - ) + return !!Settings.apis.v1.url case 'redirect-sl': return Settings.redirectToV2 != null default: @@ -60,9 +51,3 @@ module.exports = Features = { } } } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/config/settings.defaults.coffee b/services/web/config/settings.defaults.coffee index 98d8c5b133..f14c80786f 100644 --- a/services/web/config/settings.defaults.coffee +++ b/services/web/config/settings.defaults.coffee @@ -499,7 +499,7 @@ module.exports = settings = # url: "/templates/all" #}] - rateLimits: + rateLimit: autoCompile: everyone: process.env['RATE_LIMIT_AUTO_COMPILE_EVERYONE'] or 100 standard: process.env['RATE_LIMIT_AUTO_COMPILE_STANDARD'] or 25 diff --git a/services/web/test/smoke/src/SmokeTests.js b/services/web/test/smoke/src/SmokeTests.js index 5ce6bca2ff..ff6abe6b4a 100644 --- a/services/web/test/smoke/src/SmokeTests.js +++ b/services/web/test/smoke/src/SmokeTests.js @@ -8,7 +8,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -21,15 +20,8 @@ if (Object.prototype.should == null) { } const { expect } = chai const Settings = require('settings-sharelatex') -const ownPort = - __guard__( - Settings.internal != null ? Settings.internal.web : undefined, - x => x.port - ) || - Settings.port || - 3000 -const port = - (Settings.web != null ? Settings.web.web_router_port : undefined) || ownPort // send requests to web router if this is the api process +let ownPort = Settings.internal.web.port || Settings.port || 3000 +const port = Settings.web.web_router_port || ownPort // send requests to web router if this is the api process const cookeFilePath = `/tmp/smoke-test-cookie-${ownPort}-to-${port}.txt` const buildUrl = path => ` -b ${cookeFilePath} --resolve 'smoke${ @@ -227,9 +219,3 @@ curl -H "X-Forwarded-Proto: https" -v ${buildUrl('project')}\ }) }) }) - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/test/unit/src/Authorization/AuthorizationManagerTests.js b/services/web/test/unit/src/Authorization/AuthorizationManagerTests.js index d1f7d412d6..04b8479cbb 100644 --- a/services/web/test/unit/src/Authorization/AuthorizationManagerTests.js +++ b/services/web/test/unit/src/Authorization/AuthorizationManagerTests.js @@ -35,7 +35,8 @@ describe('AuthorizationManager', function() { '../Errors/Errors': Errors, '../TokenAccess/TokenAccessHandler': (this.TokenAccessHandler = { isValidToken: sinon.stub().callsArgWith(2, null, false, false) - }) + }), + 'settings-sharelatex': { passwordStrengthOptions: {} } } }) this.user_id = 'user-id-1' diff --git a/services/web/test/unit/src/Blog/BlogControllerTests.js b/services/web/test/unit/src/Blog/BlogControllerTests.js index 55280b6e18..ae7f4cfc77 100644 --- a/services/web/test/unit/src/Blog/BlogControllerTests.js +++ b/services/web/test/unit/src/Blog/BlogControllerTests.js @@ -28,7 +28,8 @@ describe('BlogController', function() { blog: { url: 'http://blog.sharelatex.env' } - } + }, + cdn: { web: { host: null } } } this.request = { get: sinon.stub() } this.ErrorController = {} diff --git a/services/web/test/unit/src/Compile/CompileManagerTests.js b/services/web/test/unit/src/Compile/CompileManagerTests.js index 9af9f4ff54..15ae2703ff 100644 --- a/services/web/test/unit/src/Compile/CompileManagerTests.js +++ b/services/web/test/unit/src/Compile/CompileManagerTests.js @@ -32,7 +32,8 @@ describe('CompileManager', function() { }, requires: { 'settings-sharelatex': (this.settings = { - redis: { web: { host: 'localhost', port: 42 } } + redis: { web: { host: 'localhost', port: 42 } }, + rateLimit: { autoCompile: {} } }), '../../infrastructure/RedisWrapper': { client: () => { diff --git a/services/web/test/unit/src/Institutions/InstitutionsAPITests.js b/services/web/test/unit/src/Institutions/InstitutionsAPITests.js index c402850bb8..268ba4410a 100644 --- a/services/web/test/unit/src/Institutions/InstitutionsAPITests.js +++ b/services/web/test/unit/src/Institutions/InstitutionsAPITests.js @@ -79,7 +79,8 @@ describe('InstitutionsAPI', function() { }) it('handle empty response', function(done) { - this.settings.apis = null + this.settings.apis.v1.url = '' + return this.InstitutionsAPI.getInstitutionAffiliations( this.institutionId, (err, body) => { @@ -162,7 +163,7 @@ describe('InstitutionsAPI', function() { }) it('handle empty response', function(done) { - this.settings.apis = null + this.settings.apis.v1.url = '' return this.InstitutionsAPI.getUserAffiliations( this.stubbedUser._id, (err, body) => { diff --git a/services/web/test/unit/src/Project/ProjectControllerTests.js b/services/web/test/unit/src/Project/ProjectControllerTests.js index b91d9bb019..da747755c5 100644 --- a/services/web/test/unit/src/Project/ProjectControllerTests.js +++ b/services/web/test/unit/src/Project/ProjectControllerTests.js @@ -37,7 +37,8 @@ describe('ProjectController', function() { url: 'chat.com' } }, - siteUrl: 'mysite.com' + siteUrl: 'mysite.com', + algolia: {} } this.brandVariationDetails = { id: '12', diff --git a/services/web/test/unit/src/Subscription/V1SusbcriptionManagerTests.js b/services/web/test/unit/src/Subscription/V1SusbcriptionManagerTests.js index 0888a0d6cf..d13dedbdad 100644 --- a/services/web/test/unit/src/Subscription/V1SusbcriptionManagerTests.js +++ b/services/web/test/unit/src/Subscription/V1SusbcriptionManagerTests.js @@ -39,7 +39,8 @@ describe('V1SubscriptionManager', function() { 'settings-sharelatex': (this.Settings = { apis: { v1: { - host: (this.host = 'http://overleaf.example.com') + host: (this.host = 'http://overleaf.example.com'), + url: 'v1.url' } }, v1GrandfatheredFeaturesUidCutoff: 10,