Skip to content

Commit

Permalink
use environment from appConfig instead of deriving it ourselves (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
jelhan authored Oct 28, 2021
1 parent 66dc7ea commit a53418f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 56 deletions.
21 changes: 4 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
buildPolicyString,
calculateConfig,
debug,
getEnvironmentFromRuntimeConfig,
readConfig,
} = require('./lib/utils');

Expand Down Expand Up @@ -206,29 +205,17 @@ 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) {
debug('Skip because not enabled in configuration');
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`);
Expand Down
39 changes: 0 additions & 39 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

'use strict';

const assert = require('assert');
const debug = require('./utils/debug');
const fs = require('fs');
const path = require('path');
Expand Down Expand Up @@ -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 = /<meta name=".*\/config\/environment" content="(.*)" \/>/;
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,
};

0 comments on commit a53418f

Please sign in to comment.