Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use environment from appConfig instead of deriving it ourselves #249

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};