Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
feat(vcl): Support Custom VCL
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed May 14, 2019
1 parent 275494f commit b2ca735
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
19 changes: 13 additions & 6 deletions src/fastly/include-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
15 changes: 12 additions & 3 deletions src/fastly/vcl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
}

Expand Down
4 changes: 2 additions & 2 deletions src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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(
Expand Down

0 comments on commit b2ca735

Please sign in to comment.