From a53418f27a0d182e7010b79839e42904c9708521 Mon Sep 17 00:00:00 2001 From: Jeldrik Hanschke Date: Fri, 29 Oct 2021 01:08:52 +0200 Subject: [PATCH] use environment from appConfig instead of deriving it ourselves (#249) --- index.js | 21 ++++----------------- lib/utils.js | 39 --------------------------------------- 2 files changed, 4 insertions(+), 56 deletions(-) diff --git a/index.js b/index.js index ed072e1..337202e 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,6 @@ const { buildPolicyString, calculateConfig, debug, - getEnvironmentFromRuntimeConfig, readConfig, } = require('./lib/utils'); @@ -206,15 +205,8 @@ module.exports = { return; } - const isTestIndexHtml = - type.startsWith('test-') || - getEnvironmentFromRuntimeConfig(existingContent) === 'test'; - const environment = isTestIndexHtml ? 'test' : appConfig.environment; - debug( - `### Process contentFor hook for ${type} of ${ - isTestIndexHtml ? 'index.html' : 'tests/index.html' - }` - ); + const { environment } = appConfig; + debug(`### Process contentFor hook ${type} for environment ${environment}`); const config = this._getConfigFor(environment); if (!config.enabled) { @@ -222,13 +214,8 @@ module.exports = { return; } - // inject CSP meta tag in - if ( - // 1. `head` slot of `index.html` and - (type === 'head' && !isTestIndexHtml) || - // 2. `test-head` slot of `tests/index.html` - type === 'test-head' - ) { + // inject CSP meta tag in head + if (type === 'head') { // skip if not configured to deliver via meta tag if (!config.delivery.includes('meta')) { debug(`Skip because not configured to deliver CSP via meta tag`); diff --git a/lib/utils.js b/lib/utils.js index 57494a2..c36c31d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -2,7 +2,6 @@ 'use strict'; -const assert = require('assert'); const debug = require('./utils/debug'); const fs = require('fs'); const path = require('path'); @@ -176,48 +175,10 @@ function appendSourceList(policyObject, directiveName, sourceList) { policyObject[directiveName].push(sourceList); } -/** - * Determines based on `existingContent` passed to `contentFor` hook, if the hook - * is run for `index.html` or `tests/index.html`. - * - * When running this addon only meta tag for runtime configuration is injected in - * existingContent for sure. The runtime configuration has different value for - * `environment` property between index.html and tests/index.html. - * - * @param {string[]} isIndexHtmlForTesting - * @return {boolean} - */ -function getEnvironmentFromRuntimeConfig(existingContent) { - let encodedRunTimeConfig; - let configRegExp = //; - for (let content of existingContent) { - let matches = content.match(configRegExp); - - if (matches && matches.length >= 1) { - encodedRunTimeConfig = matches[1]; - } - } - assert( - encodedRunTimeConfig, - 'Run time configuration is required in order to determine if contentFor hook is run for ' + - 'but seems to be missing in existing content.' - ); - - let runTimeConfig; - try { - runTimeConfig = JSON.parse(decodeURIComponent(encodedRunTimeConfig)); - } catch (error) { - throw new Error(`Could not decode runtime configuration cause of ${error}`); - } - - return runTimeConfig.environment; -} - module.exports = { appendSourceList, buildPolicyString, calculateConfig, debug, - getEnvironmentFromRuntimeConfig, readConfig, };