scratch-www/bin/lib/fastly-extended.js

211 lines
8 KiB
JavaScript
Raw Permalink Normal View History

2024-01-17 15:40:27 -05:00
const Fastly = require('fastly');
2016-04-16 13:46:03 -04:00
/*
* Fastly library extended to allow configuration for a particular service
* and some helper methods.
*
* @param {string} API key
* @param {string} Service id
*/
module.exports = function (apiKey, serviceId) {
2024-01-17 15:40:27 -05:00
const fastly = Fastly(apiKey);
fastly.serviceId = serviceId;
2016-04-16 13:46:03 -04:00
/*
* Helper method for constructing Fastly API urls
2016-04-19 10:47:33 -04:00
*
2016-04-16 13:46:03 -04:00
* @param {string} Service id
* @param {number} Version
*
* @return {string}
*/
fastly.getFastlyAPIPrefix = function (servId, version) {
2024-01-17 15:40:27 -05:00
return `/service/${encodeURIComponent(servId)}/version/${version}`;
};
2016-04-16 13:46:03 -04:00
/*
* getLatestActiveVersion: Get the most recent version for the configured service
2016-04-19 10:47:33 -04:00
*
2016-04-16 13:46:03 -04:00
* @param {callback} Callback with signature *err, latestVersion)
*/
fastly.getLatestActiveVersion = function (cb) {
if (!this.serviceId) {
2016-04-19 10:47:33 -04:00
return cb('Failed to get latest version. No serviceId configured');
}
2024-01-17 15:40:27 -05:00
const url = `/service/${encodeURIComponent(this.serviceId)}/version`;
this.request('GET', url, (err, versions) => {
if (err) {
2024-01-17 15:40:27 -05:00
return cb(`Failed to fetch versions: ${err}`);
}
2024-01-17 15:40:27 -05:00
const latestVersion = versions.reduce((latestActiveSoFar, cur) => {
2019-09-26 10:49:12 -04:00
// if one of [latestActiveSoFar, cur] is active and the other isn't,
// return whichever is active. If both are not active, return
// latestActiveSoFar.
if (!cur || !cur.active) return latestActiveSoFar;
if (!latestActiveSoFar || !latestActiveSoFar.active) return cur;
// when both are active, prefer whichever has a higher version number.
return (cur.number > latestActiveSoFar.number) ? cur : latestActiveSoFar;
}, null);
return cb(null, latestVersion);
2016-04-19 10:47:33 -04:00
});
};
2016-04-16 13:46:03 -04:00
/*
* setCondition: Upsert a Fastly condition entry
* Attempts to PUT and POSTs if the PUT request is a 404
2016-04-19 10:47:33 -04:00
*
2016-04-16 13:46:03 -04:00
* @param {number} Version number
* @param {object} Condition object sent to the API
* @param {callback} Callback for fastly.request
*/
fastly.setCondition = function (version, condition, cb) {
if (!this.serviceId) {
2016-04-19 10:47:33 -04:00
return cb('Failed to set condition. No serviceId configured');
}
2024-01-17 15:40:27 -05:00
const name = condition.name;
const putUrl = `${this.getFastlyAPIPrefix(this.serviceId, version)}/condition/${encodeURIComponent(name)}`;
const postUrl = `${this.getFastlyAPIPrefix(this.serviceId, version)}/condition`;
return this.request('PUT', putUrl, condition, (err, response) => {
if (err && err.statusCode === 404) {
2024-01-17 15:40:27 -05:00
this.request('POST', postUrl, condition, (e, resp) => {
if (e) {
2024-01-17 15:40:27 -05:00
return cb(`Failed while inserting condition "${condition.statement}": ${e}`);
}
return cb(null, resp);
});
return;
}
if (err) {
2024-01-17 15:40:27 -05:00
return cb(`Failed to update condition "${condition.statement}": ${err}`);
}
return cb(null, response);
2024-01-17 15:40:27 -05:00
});
};
2016-04-16 13:46:03 -04:00
/*
* setFastlyHeader: Upsert a Fastly header entry
* Attempts to PUT and POSTs if the PUT request is a 404
2016-04-19 10:47:33 -04:00
*
2016-04-16 13:46:03 -04:00
* @param {number} Version number
* @param {object} Header object sent to the API
* @param {callback} Callback for fastly.request
*/
fastly.setFastlyHeader = function (version, header, cb) {
if (!this.serviceId) {
2016-04-19 10:47:33 -04:00
cb('Failed to set header. No serviceId configured');
}
2024-01-17 15:40:27 -05:00
const name = header.name;
const putUrl = `${this.getFastlyAPIPrefix(this.serviceId, version)}/header/${encodeURIComponent(name)}`;
const postUrl = `${this.getFastlyAPIPrefix(this.serviceId, version)}/header`;
return this.request('PUT', putUrl, header, (err, response) => {
if (err && err.statusCode === 404) {
2024-01-17 15:40:27 -05:00
this.request('POST', postUrl, header, (e, resp) => {
if (e) {
2024-01-17 15:40:27 -05:00
return cb(`Failed to insert header: ${e}`);
}
return cb(null, resp);
});
return;
}
if (err) {
2024-01-17 15:40:27 -05:00
return cb(`Failed to update header: ${err}`);
}
return cb(null, response);
2024-01-17 15:40:27 -05:00
});
};
2016-04-19 13:04:45 -04:00
2016-04-19 18:42:03 -04:00
/*
* setResponseObject: Upsert a Fastly response object
* Attempts to PUT and POSTs if the PUT request is a 404
*
* @param {number} Version number
* @param {object} Response object sent to the API
* @param {callback} Callback for fastly.request
*/
fastly.setResponseObject = function (version, responseObj, cb) {
if (!this.serviceId) {
cb('Failed to set response object. No serviceId configured');
}
2024-01-17 15:40:27 -05:00
const name = responseObj.name;
const putUrl =
`${this.getFastlyAPIPrefix(this.serviceId, version)}/response_object/${encodeURIComponent(name)}`;
const postUrl = `${this.getFastlyAPIPrefix(this.serviceId, version)}/response_object`;
return this.request('PUT', putUrl, responseObj, (err, response) => {
2016-04-19 18:42:03 -04:00
if (err && err.statusCode === 404) {
2024-01-17 15:40:27 -05:00
this.request('POST', postUrl, responseObj, (e, resp) => {
if (e) {
2024-01-17 15:40:27 -05:00
return cb(`Failed to insert response object: ${e}`);
2016-04-19 18:42:03 -04:00
}
return cb(null, resp);
2016-04-19 18:42:03 -04:00
});
return;
}
if (err) {
2024-01-17 15:40:27 -05:00
return cb(`Failed to update response object: ${err}`);
2016-04-19 18:42:03 -04:00
}
return cb(null, response);
2024-01-17 15:40:27 -05:00
});
2016-04-19 18:42:03 -04:00
};
2016-04-19 13:04:45 -04:00
/*
* cloneVersion: Clone a version to create a new version
*
* @param {number} Version to clone
* @param {callback} Callback for fastly.request
*/
fastly.cloneVersion = function (version, cb) {
if (!this.serviceId) return cb('Failed to clone version. No serviceId configured.');
2024-01-17 15:40:27 -05:00
const url = `${this.getFastlyAPIPrefix(this.serviceId, version)}/clone`;
2016-04-19 13:04:45 -04:00
this.request('PUT', url, cb);
};
/*
* activateVersion: Activate a version
*
* @param {number} Version number
* @param {callback} Callback for fastly.request
*/
fastly.activateVersion = function (version, cb) {
if (!this.serviceId) return cb('Failed to activate version. No serviceId configured.');
2024-01-17 15:40:27 -05:00
const url = `${this.getFastlyAPIPrefix(this.serviceId, version)}/activate`;
2016-04-19 13:04:45 -04:00
this.request('PUT', url, cb);
};
/*
* Upsert a custom vcl file. Attempts a PUT, and falls back
* to POST if not there already.
*
* @param {number} version current version number for fastly service
* @param {string} name name of the custom vcl file to be upserted
* @param {string} vcl stringified custom vcl to be uploaded
* @param {Function} cb function that takes in two args: err, response
*/
fastly.setCustomVCL = function (version, name, vcl, cb) {
if (!this.serviceId) {
return cb('Failed to set response object. No serviceId configured');
}
2024-01-17 15:40:27 -05:00
const url = `${this.getFastlyAPIPrefix(this.serviceId, version)}/vcl/${name}`;
const postUrl = `${this.getFastlyAPIPrefix(this.serviceId, version)}/vcl`;
const content = {content: vcl};
return this.request('PUT', url, content, (err, response) => {
if (err && err.statusCode === 404) {
content.name = name;
2024-01-17 15:40:27 -05:00
this.request('POST', postUrl, content, (e, resp) => {
if (e) {
2024-01-17 15:40:27 -05:00
return cb(`Failed while adding custom vcl "${name}": ${e}`);
}
return cb(null, resp);
});
return;
}
if (err) {
2024-01-17 15:40:27 -05:00
return cb(`Failed to update custom vcl "${name}": ${err}`);
}
return cb(null, response);
2024-01-17 15:40:27 -05:00
});
};
return fastly;
};