UNPKG

3.35 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.fetchToTemp = exports.fetch = void 0;
7function os() {
8 const data = _interopRequireWildcard(require("os"));
9 os = function () {
10 return data;
11 };
12 return data;
13}
14function path() {
15 const data = _interopRequireWildcard(require("path"));
16 path = function () {
17 return data;
18 };
19 return data;
20}
21function fs() {
22 const data = _interopRequireWildcard(require("fs"));
23 fs = function () {
24 return data;
25 };
26 return data;
27}
28function _nodeFetch() {
29 const data = _interopRequireDefault(require("node-fetch"));
30 _nodeFetch = function () {
31 return data;
32 };
33 return data;
34}
35var _errors = require("./errors");
36var _logger = _interopRequireDefault(require("./logger"));
37function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
38function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
39function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
40async function unwrapFetchResult(response) {
41 const data = await response.text();
42 try {
43 return JSON.parse(data);
44 } catch (e) {
45 return data;
46 }
47}
48
49/**
50 * Downloads the given `url` to the OS's temp folder and
51 * returns the path to it.
52 */
53const fetchToTemp = url => {
54 try {
55 return new Promise((resolve, reject) => {
56 const fileName = path().basename(url);
57 const tmpDir = path().join(os().tmpdir(), fileName);
58 (0, _nodeFetch().default)(url).then(result => {
59 if (result.status >= 400) {
60 return reject(`Fetch request failed with status ${result.status}`);
61 }
62 const dest = fs().createWriteStream(tmpDir);
63 result.body.pipe(dest);
64 result.body.on('end', () => {
65 resolve(tmpDir);
66 });
67 result.body.on('error', reject);
68 });
69 });
70 } catch (e) {
71 _logger.default.error(e);
72 throw e;
73 }
74};
75exports.fetchToTemp = fetchToTemp;
76const fetch = async (url, options) => {
77 const result = await (0, _nodeFetch().default)(url, options);
78 const data = await unwrapFetchResult(result);
79 if (result.status >= 400) {
80 throw new _errors.CLIError(`Fetch request failed with status ${result.status}: ${data}.`);
81 }
82 return {
83 status: result.status,
84 headers: result.headers,
85 data
86 };
87};
88exports.fetch = fetch;
89
90//# sourceMappingURL=fetch.ts.map
\No newline at end of file