File: /home/frenchy/www/french-american.org/current/node_modules/snyk/dist/lib/protect/fetch-patch.js
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const needle = require("needle");
const fs = require("fs");
const analytics = require("../analytics");
const debugModule = require("debug");
const proxyFromEnv = require("proxy-from-env");
const ProxyAgent = require("proxy-agent");
const debug = debugModule('snyk:fetch-patch');
const getProxyForUrl = proxyFromEnv.getProxyForUrl;
function getPatchFile(patchUrl, patchFilename) {
return __awaiter(this, void 0, void 0, function* () {
let options;
const proxyUri = getProxyForUrl(patchUrl);
if (proxyUri) {
debug('Using proxy: ', proxyUri);
options = { agent: new ProxyAgent(proxyUri) };
}
let res;
let patchData;
try {
res = yield needle('get', patchUrl, options || {});
if (!res || res.statusCode !== 200) {
throw res;
}
patchData = res.body;
debug(`Successfully downloaded patch from ${patchUrl}, patch size ${patchData.length} bytes`);
}
catch (error) {
debug(`Failed to download patch from ${patchUrl}`, error);
analytics.add('patch-fetch-fail', {
message: error && (error.message || error.body),
code: error && error.statusCode,
});
throw error;
}
try {
fs.writeFileSync(patchFilename, patchData);
debug(`Successfully wrote patch to ${patchFilename}`);
}
catch (error) {
debug(`Failed to write patch to ${patchFilename}`, error);
analytics.add('patch-fetch-fail', {
message: error && error.message,
patchFilename,
patchUrl,
});
throw error;
}
return patchFilename;
});
}
module.exports = getPatchFile;
//# sourceMappingURL=fetch-patch.js.map