UNPKG

7.92 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __generator = (this && this.__generator) || function (thisArg, body) {
12 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14 function verb(n) { return function (v) { return step([n, v]); }; }
15 function step(op) {
16 if (f) throw new TypeError("Generator is already executing.");
17 while (_) try {
18 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19 if (y = 0, t) op = [op[0] & 2, t.value];
20 switch (op[0]) {
21 case 0: case 1: t = op; break;
22 case 4: _.label++; return { value: op[1], done: false };
23 case 5: _.label++; y = op[1]; op = [0]; continue;
24 case 7: op = _.ops.pop(); _.trys.pop(); continue;
25 default:
26 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30 if (t[2]) _.ops.pop();
31 _.trys.pop(); continue;
32 }
33 op = body.call(thisArg, _);
34 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36 }
37};
38var __importDefault = (this && this.__importDefault) || function (mod) {
39 return (mod && mod.__esModule) ? mod : { "default": mod };
40};
41Object.defineProperty(exports, "__esModule", { value: true });
42exports.spawn = exports.plusx = exports.hash = exports.downloadUrl = void 0;
43var node_fetch_1 = __importDefault(require("node-fetch"));
44var crypto_1 = __importDefault(require("crypto"));
45var fs_extra_1 = __importDefault(require("fs-extra"));
46var https_proxy_agent_1 = __importDefault(require("https-proxy-agent"));
47var path_1 = __importDefault(require("path"));
48var child_process_1 = require("child_process");
49var stream_1 = __importDefault(require("stream"));
50var log_1 = require("./log");
51function downloadUrl(url, file) {
52 var _a, _b, _c;
53 return __awaiter(this, void 0, void 0, function () {
54 var proxy, res, tempFile, ws, totalSize, currentSize;
55 return __generator(this, function (_d) {
56 switch (_d.label) {
57 case 0:
58 log_1.log.enableProgress(path_1.default.basename(file));
59 log_1.log.showProgress(0);
60 proxy = (_c = (_b = (_a = process.env.HTTPS_PROXY) !== null && _a !== void 0 ? _a : process.env.https_proxy) !== null && _b !== void 0 ? _b : process.env.HTTP_PROXY) !== null && _c !== void 0 ? _c : process.env.http_proxy;
61 return [4 /*yield*/, node_fetch_1.default(url, proxy ? { agent: https_proxy_agent_1.default(proxy) } : undefined)];
62 case 1:
63 res = _d.sent();
64 if (!res.ok) {
65 log_1.log.disableProgress();
66 throw log_1.wasReported(res.status + ": " + res.statusText);
67 }
68 tempFile = file + ".downloading";
69 fs_extra_1.default.mkdirpSync(path_1.default.dirname(tempFile));
70 ws = fs_extra_1.default.createWriteStream(tempFile);
71 totalSize = Number(res.headers.get('content-length'));
72 currentSize = 0;
73 res.body.on('data', function (chunk) {
74 if (totalSize != null && totalSize !== 0) {
75 currentSize += chunk.length;
76 log_1.log.showProgress((currentSize / totalSize) * 100);
77 }
78 });
79 res.body.pipe(ws);
80 return [2 /*return*/, new Promise(function (resolve, reject) {
81 stream_1.default.finished(ws, function (err) {
82 if (err) {
83 log_1.log.disableProgress();
84 fs_extra_1.default.rmSync(tempFile);
85 reject(log_1.wasReported(err.name + ": " + err.message));
86 }
87 else {
88 log_1.log.showProgress(100);
89 log_1.log.disableProgress();
90 fs_extra_1.default.moveSync(tempFile, file);
91 resolve();
92 }
93 });
94 })];
95 }
96 });
97 });
98}
99exports.downloadUrl = downloadUrl;
100function hash(filePath) {
101 return __awaiter(this, void 0, void 0, function () {
102 return __generator(this, function (_a) {
103 return [2 /*return*/, new Promise(function (resolve, reject) {
104 var resultHash = crypto_1.default.createHash('sha256');
105 var input = fs_extra_1.default.createReadStream(filePath);
106 input.on('error', function (e) {
107 reject(e);
108 });
109 input.on('readable', function () {
110 var data = input.read();
111 if (data) {
112 resultHash.update(data);
113 }
114 else {
115 resolve(resultHash.digest('hex'));
116 }
117 });
118 })];
119 });
120 });
121}
122exports.hash = hash;
123function plusx(file) {
124 return __awaiter(this, void 0, void 0, function () {
125 var s, newMode, base8;
126 return __generator(this, function (_a) {
127 switch (_a.label) {
128 case 0: return [4 /*yield*/, fs_extra_1.default.stat(file)];
129 case 1:
130 s = _a.sent();
131 newMode = s.mode | 64 | 8 | 1;
132 if (s.mode === newMode)
133 return [2 /*return*/];
134 base8 = newMode.toString(8).slice(-3);
135 return [4 /*yield*/, fs_extra_1.default.chmod(file, base8)];
136 case 2:
137 _a.sent();
138 return [2 /*return*/];
139 }
140 });
141 });
142}
143exports.plusx = plusx;
144function spawn(command, args, options) {
145 return __awaiter(this, void 0, void 0, function () {
146 var error;
147 return __generator(this, function (_a) {
148 error = child_process_1.spawnSync(command, args, options).error;
149 if (error) {
150 throw error;
151 }
152 return [2 /*return*/];
153 });
154 });
155}
156exports.spawn = spawn;
157//# sourceMappingURL=utils.js.map
\No newline at end of file