From b2ca735e157a7c99fe3a06df0d733c97067acc3c Mon Sep 17 00:00:00 2001 From: Alexandre Capt Date: Tue, 14 May 2019 15:35:12 +0200 Subject: [PATCH] feat(vcl): Support Custom VCL --- src/fastly/include-util.js | 19 +++++++++++++------ src/fastly/vcl.js | 15 ++++++++++++--- src/publish.js | 4 ++-- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/fastly/include-util.js b/src/fastly/include-util.js index 0c5fc171..7149142e 100644 --- a/src/fastly/include-util.js +++ b/src/fastly/include-util.js @@ -13,11 +13,18 @@ const fs = require('fs-extra'); const path = require('path'); -module.exports = function include(srcfile) { - return fs.readFileSync(srcfile).toString().replace(/(synthetic \{"include:.*"\};)/g, (m) => { +function synthetize(content, filePath) { + return content.replace(/(synthetic \{"include:.*"\};)/g, (m) => { const ref = m.replace(/^synthetic \{"include:/, '').replace(/"};$/, ''); - const name = path.resolve(path.dirname(srcfile), ref); - const content = fs.readFileSync(name).toString(); - return `synthetic {"${content}"};`; + const name = path.resolve(filePath, ref); + const c = fs.readFileSync(name).toString(); + return `synthetic {"${c}"};`; }); -}; +} + +function include(srcfile) { + return synthetize(fs.readFileSync(srcfile).toString(), path.dirname(srcfile)); +} + +module.exports.include = include; +module.exports.synthetize = synthetize; diff --git a/src/fastly/vcl.js b/src/fastly/vcl.js index 374da3af..95a59ee9 100644 --- a/src/fastly/vcl.js +++ b/src/fastly/vcl.js @@ -10,7 +10,10 @@ * governing permissions and limitations under the License. */ const path = require('path'); -const include = require('./include-util'); +const { + include, + synthetize, +} = require('./include-util'); const { resolve, parameters, @@ -26,9 +29,15 @@ async function init(fastly, version) { return fastly.setMainVCL(version, 'helix.vcl'); } -async function extensions(fastly, version) { +async function extensions(fastly, version, vclOverride = {}) { const vclfile = path.resolve(__dirname, '../../layouts/fastly/extensions.vcl'); - const content = include(vclfile); + let content; + if (vclOverride.extensions) { + // found a extensions.vcl override + content = synthetize(vclOverride.extensions, path.dirname(vclfile)); + } else { + content = include(vclfile); + } return writevcl(fastly, version, content, 'extensions.vcl'); } diff --git a/src/publish.js b/src/publish.js index 99b4371b..adb9481c 100644 --- a/src/publish.js +++ b/src/publish.js @@ -17,7 +17,7 @@ const vcl = require('./fastly/vcl'); const dictionaries = require('./fastly/dictionaries'); const redirects = require('./fastly/redirects'); -async function publish(configuration, service, token, version) { +async function publish(configuration, service, token, version, vclOverrides = {}) { if (!(!!token && !!service)) { return { body: { @@ -36,7 +36,7 @@ async function publish(configuration, service, token, version) { backends.init(fastly, version), backends.updatestrains(fastly, version, config.strains), vcl.init(fastly, version), - vcl.extensions(fastly, version), + vcl.extensions(fastly, version, vclOverrides), vcl.updatestrains(fastly, version, config.strains), redirects.updatestrains(fastly, version, config.strains), dictionaries.init(