httpExecutor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createHttpError = createHttpError;
  6. exports.parseJson = parseJson;
  7. exports.configureRequestOptionsFromUrl = configureRequestOptionsFromUrl;
  8. exports.safeGetHeader = safeGetHeader;
  9. exports.configureRequestOptions = configureRequestOptions;
  10. exports.safeStringifyJson = safeStringifyJson;
  11. exports.DigestTransform = exports.HttpExecutor = exports.HttpError = void 0;
  12. function _crypto() {
  13. const data = require("crypto");
  14. _crypto = function () {
  15. return data;
  16. };
  17. return data;
  18. }
  19. var _debug2 = _interopRequireDefault(require("debug"));
  20. function _fsExtraP() {
  21. const data = require("fs-extra-p");
  22. _fsExtraP = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _stream() {
  28. const data = require("stream");
  29. _stream = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _url() {
  35. const data = require("url");
  36. _url = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _CancellationToken() {
  42. const data = require("./CancellationToken");
  43. _CancellationToken = function () {
  44. return data;
  45. };
  46. return data;
  47. }
  48. function _index() {
  49. const data = require("./index");
  50. _index = function () {
  51. return data;
  52. };
  53. return data;
  54. }
  55. function _ProgressCallbackTransform() {
  56. const data = require("./ProgressCallbackTransform");
  57. _ProgressCallbackTransform = function () {
  58. return data;
  59. };
  60. return data;
  61. }
  62. function _zlib() {
  63. const data = require("zlib");
  64. _zlib = function () {
  65. return data;
  66. };
  67. return data;
  68. }
  69. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  70. const debug = (0, _debug2.default)("electron-builder");
  71. function createHttpError(response, description = null) {
  72. return new HttpError(response.statusCode || -1, `${response.statusCode} ${response.statusMessage}` + (description == null ? "" : "\n" + JSON.stringify(description, null, " ")) + "\nHeaders: " + safeStringifyJson(response.headers), description);
  73. }
  74. const HTTP_STATUS_CODES = new Map([[429, "Too many requests"], [400, "Bad request"], [403, "Forbidden"], [404, "Not found"], [405, "Method not allowed"], [406, "Not acceptable"], [408, "Request timeout"], [413, "Request entity too large"], [500, "Internal server error"], [502, "Bad gateway"], [503, "Service unavailable"], [504, "Gateway timeout"], [505, "HTTP version not supported"]]);
  75. class HttpError extends Error {
  76. constructor(statusCode, message = `HTTP error: ${HTTP_STATUS_CODES.get(statusCode) || statusCode}`, description = null) {
  77. super(message);
  78. this.statusCode = statusCode;
  79. this.description = description;
  80. this.name = "HttpError";
  81. }
  82. }
  83. exports.HttpError = HttpError;
  84. function parseJson(result) {
  85. return result.then(it => it == null || it.length === 0 ? null : JSON.parse(it));
  86. }
  87. class HttpExecutor {
  88. constructor() {
  89. this.maxRedirects = 10;
  90. }
  91. request(options, cancellationToken = new (_CancellationToken().CancellationToken)(), data) {
  92. configureRequestOptions(options);
  93. const encodedData = data == null ? undefined : Buffer.from(JSON.stringify(data));
  94. if (encodedData != null) {
  95. options.method = "post";
  96. options.headers["Content-Type"] = "application/json";
  97. options.headers["Content-Length"] = encodedData.length;
  98. }
  99. return this.doApiRequest(options, cancellationToken, it => {
  100. it.end(encodedData);
  101. });
  102. }
  103. doApiRequest(options, cancellationToken, requestProcessor, redirectCount = 0) {
  104. if (debug.enabled) {
  105. debug(`Request: ${safeStringifyJson(options)}`);
  106. }
  107. return cancellationToken.createPromise((resolve, reject, onCancel) => {
  108. const request = this.doRequest(options, response => {
  109. try {
  110. this.handleResponse(response, options, cancellationToken, resolve, reject, redirectCount, requestProcessor);
  111. } catch (e) {
  112. reject(e);
  113. }
  114. });
  115. this.addErrorAndTimeoutHandlers(request, reject);
  116. this.addRedirectHandlers(request, options, reject, redirectCount, options => {
  117. this.doApiRequest(options, cancellationToken, requestProcessor, redirectCount).then(resolve).catch(reject);
  118. });
  119. requestProcessor(request, reject);
  120. onCancel(() => request.abort());
  121. });
  122. } // noinspection JSUnusedLocalSymbols
  123. addRedirectHandlers(request, options, reject, redirectCount, handler) {// not required for NodeJS
  124. }
  125. addErrorAndTimeoutHandlers(request, reject) {
  126. this.addTimeOutHandler(request, reject);
  127. request.on("error", reject);
  128. request.on("aborted", () => {
  129. reject(new Error("Request has been aborted by the server"));
  130. });
  131. }
  132. handleResponse(response, options, cancellationToken, resolve, reject, redirectCount, requestProcessor) {
  133. if (debug.enabled) {
  134. debug(`Response: ${response.statusCode} ${response.statusMessage}, request options: ${safeStringifyJson(options)}`);
  135. } // we handle any other >= 400 error on request end (read detailed message in the response body)
  136. if (response.statusCode === 404) {
  137. // error is clear, we don't need to read detailed error description
  138. reject(createHttpError(response, `method: ${options.method} url: ${options.protocol || "https:"}//${options.hostname}${options.path}
  139. Please double check that your authentication token is correct. Due to security reasons actual status maybe not reported, but 404.
  140. `));
  141. return;
  142. } else if (response.statusCode === 204) {
  143. // on DELETE request
  144. resolve();
  145. return;
  146. }
  147. const redirectUrl = safeGetHeader(response, "location");
  148. if (redirectUrl != null) {
  149. if (redirectCount > 10) {
  150. reject(new Error("Too many redirects (> 10)"));
  151. return;
  152. }
  153. this.doApiRequest(HttpExecutor.prepareRedirectUrlOptions(redirectUrl, options), cancellationToken, requestProcessor, redirectCount).then(resolve).catch(reject);
  154. return;
  155. }
  156. let stream = response;
  157. if (options.gzip) {
  158. const gUnzip = (0, _zlib().createGunzip)();
  159. gUnzip.on("error", reject);
  160. response.pipe(gUnzip);
  161. stream = gUnzip;
  162. }
  163. stream.setEncoding("utf8");
  164. let data = "";
  165. stream.on("data", chunk => data += chunk);
  166. stream.on("end", () => {
  167. try {
  168. if (response.statusCode != null && response.statusCode >= 400) {
  169. const contentType = safeGetHeader(response, "content-type");
  170. const isJson = contentType != null && (Array.isArray(contentType) ? contentType.find(it => it.includes("json")) != null : contentType.includes("json"));
  171. reject(createHttpError(response, isJson ? JSON.parse(data) : data));
  172. } else {
  173. resolve(data.length === 0 ? null : data);
  174. }
  175. } catch (e) {
  176. reject(e);
  177. }
  178. });
  179. }
  180. doDownload(requestOptions, destination, redirectCount, options, callback, onCancel) {
  181. const request = this.doRequest(requestOptions, response => {
  182. if (response.statusCode >= 400) {
  183. callback(new Error(`Cannot download "${requestOptions.protocol || "https:"}//${requestOptions.hostname}${requestOptions.path}", status ${response.statusCode}: ${response.statusMessage}`));
  184. return;
  185. }
  186. const redirectUrl = safeGetHeader(response, "location");
  187. if (redirectUrl != null) {
  188. if (redirectCount < this.maxRedirects) {
  189. this.doDownload(HttpExecutor.prepareRedirectUrlOptions(redirectUrl, requestOptions), destination, redirectCount++, options, callback, onCancel);
  190. } else {
  191. callback(new Error(`Too many redirects (> ${this.maxRedirects})`));
  192. }
  193. return;
  194. }
  195. configurePipes(options, response, destination, callback, options.cancellationToken);
  196. });
  197. this.addErrorAndTimeoutHandlers(request, callback);
  198. this.addRedirectHandlers(request, requestOptions, callback, redirectCount, requestOptions => {
  199. this.doDownload(requestOptions, destination, redirectCount++, options, callback, onCancel);
  200. });
  201. onCancel(() => request.abort());
  202. request.end();
  203. }
  204. addTimeOutHandler(request, callback) {
  205. request.on("socket", socket => {
  206. socket.setTimeout(60 * 1000, () => {
  207. callback(new Error("Request timed out"));
  208. request.abort();
  209. });
  210. });
  211. }
  212. static prepareRedirectUrlOptions(redirectUrl, options) {
  213. const newOptions = configureRequestOptionsFromUrl(redirectUrl, Object.assign({}, options));
  214. const headers = newOptions.headers;
  215. if (headers != null && headers.authorization != null && headers.authorization.startsWith("token")) {
  216. const parsedNewUrl = new (_url().URL)(redirectUrl);
  217. if (parsedNewUrl.hostname.endsWith(".amazonaws.com")) {
  218. delete headers.authorization;
  219. }
  220. }
  221. return newOptions;
  222. }
  223. }
  224. exports.HttpExecutor = HttpExecutor;
  225. function configureRequestOptionsFromUrl(url, options) {
  226. const parsedUrl = (0, _url().parse)(url);
  227. options.protocol = parsedUrl.protocol;
  228. options.hostname = parsedUrl.hostname;
  229. if (parsedUrl.port == null) {
  230. if (options.port != null) {
  231. delete options.port;
  232. }
  233. } else {
  234. options.port = parsedUrl.port;
  235. }
  236. options.path = parsedUrl.path;
  237. return configureRequestOptions(options);
  238. }
  239. class DigestTransform extends _stream().Transform {
  240. constructor(expected, algorithm = "sha512", encoding = "base64") {
  241. super();
  242. this.expected = expected;
  243. this.algorithm = algorithm;
  244. this.encoding = encoding;
  245. this._actual = null;
  246. this.isValidateOnEnd = true;
  247. this.digester = (0, _crypto().createHash)(algorithm);
  248. } // noinspection JSUnusedGlobalSymbols
  249. get actual() {
  250. return this._actual;
  251. } // noinspection JSUnusedGlobalSymbols
  252. _transform(chunk, encoding, callback) {
  253. this.digester.update(chunk);
  254. callback(null, chunk);
  255. } // noinspection JSUnusedGlobalSymbols
  256. _flush(callback) {
  257. this._actual = this.digester.digest(this.encoding);
  258. if (this.isValidateOnEnd) {
  259. try {
  260. this.validate();
  261. } catch (e) {
  262. callback(e);
  263. return;
  264. }
  265. }
  266. callback(null);
  267. }
  268. validate() {
  269. if (this._actual == null) {
  270. throw (0, _index().newError)("Not finished yet", "ERR_STREAM_NOT_FINISHED");
  271. }
  272. if (this._actual !== this.expected) {
  273. throw (0, _index().newError)(`${this.algorithm} checksum mismatch, expected ${this.expected}, got ${this._actual}`, "ERR_CHECKSUM_MISMATCH");
  274. }
  275. return null;
  276. }
  277. }
  278. exports.DigestTransform = DigestTransform;
  279. function checkSha2(sha2Header, sha2, callback) {
  280. if (sha2Header != null && sha2 != null) {
  281. // todo why bintray doesn't send this header always
  282. if (sha2Header == null) {
  283. callback(new Error("checksum is required, but server response doesn't contain X-Checksum-Sha2 header"));
  284. return false;
  285. } else if (sha2Header !== sha2) {
  286. callback(new Error(`checksum mismatch: expected ${sha2} but got ${sha2Header} (X-Checksum-Sha2 header)`));
  287. return false;
  288. }
  289. }
  290. return true;
  291. }
  292. function safeGetHeader(response, headerKey) {
  293. const value = response.headers[headerKey];
  294. if (value == null) {
  295. return null;
  296. } else if (Array.isArray(value)) {
  297. // electron API
  298. return value.length === 0 ? null : value[value.length - 1];
  299. } else {
  300. return value;
  301. }
  302. }
  303. function configurePipes(options, response, destination, callback, cancellationToken) {
  304. if (!checkSha2(safeGetHeader(response, "X-Checksum-Sha2"), options.sha2, callback)) {
  305. return;
  306. }
  307. const streams = [];
  308. if (options.onProgress != null) {
  309. const contentLength = safeGetHeader(response, "content-length");
  310. if (contentLength != null) {
  311. streams.push(new (_ProgressCallbackTransform().ProgressCallbackTransform)(parseInt(contentLength, 10), options.cancellationToken, options.onProgress));
  312. }
  313. }
  314. const sha512 = options.sha512;
  315. if (sha512 != null) {
  316. streams.push(new DigestTransform(sha512, "sha512", sha512.length === 128 && !sha512.includes("+") && !sha512.includes("Z") && !sha512.includes("=") ? "hex" : "base64"));
  317. } else if (options.sha2 != null) {
  318. streams.push(new DigestTransform(options.sha2, "sha256", "hex"));
  319. }
  320. const fileOut = (0, _fsExtraP().createWriteStream)(destination);
  321. streams.push(fileOut);
  322. let lastStream = response;
  323. for (const stream of streams) {
  324. stream.on("error", error => {
  325. if (!cancellationToken.cancelled) {
  326. callback(error);
  327. }
  328. });
  329. lastStream = lastStream.pipe(stream);
  330. }
  331. fileOut.on("finish", () => {
  332. fileOut.close(callback);
  333. });
  334. }
  335. function configureRequestOptions(options, token, method) {
  336. if (method != null) {
  337. options.method = method;
  338. }
  339. let headers = options.headers;
  340. if (headers == null) {
  341. headers = {};
  342. options.headers = headers;
  343. }
  344. if (token != null) {
  345. headers.authorization = token.startsWith("Basic") ? token : `token ${token}`;
  346. }
  347. if (headers["User-Agent"] == null) {
  348. headers["User-Agent"] = "electron-builder";
  349. }
  350. if (method == null || method === "GET" || headers["Cache-Control"] == null) {
  351. headers["Cache-Control"] = "no-cache";
  352. } // do not specify for node (in any case we use https module)
  353. if (options.protocol == null && process.versions.electron != null) {
  354. options.protocol = "https:";
  355. }
  356. return options;
  357. }
  358. function safeStringifyJson(data, skippedNames) {
  359. return JSON.stringify(data, (name, value) => {
  360. if (name.endsWith("authorization") || name.endsWith("Password") || name.endsWith("PASSWORD") || name.endsWith("Token") || name.includes("password") || name.includes("token") || skippedNames != null && skippedNames.has(name)) {
  361. return "<stripped sensitive data>";
  362. }
  363. return value;
  364. }, 2);
  365. }
  366. //# sourceMappingURL=httpExecutor.js.map