UNPKG

edgecast-purge

Version:

a service to purge media from edgecast's cache

98 lines (82 loc) 2.56 kB
'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var https = require('https'); var path = require('path'); var winston = require('winston'); // winston/logging configuration var tsFormat = function tsFormat() { return new Date().toLocaleTimeString(); }; var logger = new winston.Logger({ transports: [ // colorize the output to the console new winston.transports.Console({ timestamp: tsFormat, colorize: true })] }); var EdgeCastPurge = function () { /** * constructor * @param {string} token - the api token * @param {string} customerId - the customer id */ function EdgeCastPurge(token, customerId) { _classCallCheck(this, EdgeCastPurge); this.token = token; this.customerId = customerId; this.endpoint = path.join('/v2', 'mcc', 'customers', customerId, 'edge', 'purge'); this.headers = { Authorization: 'tok: ' + token, Accept: 'application/json', 'Content-Type': 'application/json' }; } /** * purge * @param {array} purge list - resources to purge */ EdgeCastPurge.prototype.purge = function purge(toPurge) { var _this = this; var purgeList = typeof toPurge === 'string' ? [toPurge] : toPurge; return Promise.all(purgeList.map(function (resourceURL) { return _this.purgeRsource(resourceURL); })); }; EdgeCastPurge.prototype.purgeRsource = function purgeRsource(resourceURL) { var _this2 = this; return new Promise(function (resolve, reject) { var postData = { MediaPath: resourceURL, MediaType: '3' }; var chunk = []; var req = https.request({ method: 'PUT', hostname: 'api.edgecast.com', port: 443, path: _this2.endpoint, headers: _this2.headers }, // prettier-ignore function (res) { res.on('data', function (d) { chunk.push(d); }); res.on('end', function () { var endData = chunk.join(); if (res.statusCode >= 200 && res.statusCode < 300) { logger.info('purged: ' + resourceURL); return resolve(endData); } logger.error('error purging: ' + resourceURL + '; err=' + (endData.Message || endData.toString())); return reject(endData); }); }); req.write(JSON.stringify(postData)); req.end(); }); }; return EdgeCastPurge; }(); module.exports = EdgeCastPurge;