UNPKG

3.13 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getNetSession = getNetSession;
7exports.ElectronHttpExecutor = exports.NET_SESSION_NAME = void 0;
8
9function _builderUtilRuntime() {
10 const data = require("builder-util-runtime");
11
12 _builderUtilRuntime = function () {
13 return data;
14 };
15
16 return data;
17}
18
19function _electron() {
20 const data = require("electron");
21
22 _electron = function () {
23 return data;
24 };
25
26 return data;
27}
28
29const NET_SESSION_NAME = "electron-updater";
30exports.NET_SESSION_NAME = NET_SESSION_NAME;
31
32function getNetSession() {
33 return _electron().session.fromPartition(NET_SESSION_NAME, {
34 cache: false
35 });
36}
37
38class ElectronHttpExecutor extends _builderUtilRuntime().HttpExecutor {
39 constructor(proxyLoginCallback) {
40 super();
41 this.proxyLoginCallback = proxyLoginCallback;
42 this.cachedSession = null;
43 }
44
45 async download(url, destination, options) {
46 return await options.cancellationToken.createPromise((resolve, reject, onCancel) => {
47 const requestOptions = {
48 headers: options.headers || undefined,
49 redirect: "manual"
50 };
51 (0, _builderUtilRuntime().configureRequestUrl)(url, requestOptions);
52 (0, _builderUtilRuntime().configureRequestOptions)(requestOptions);
53 this.doDownload(requestOptions, {
54 destination,
55 options,
56 onCancel,
57 callback: error => {
58 if (error == null) {
59 resolve(destination);
60 } else {
61 reject(error);
62 }
63 },
64 responseHandler: null
65 }, 0);
66 });
67 }
68
69 createRequest(options, callback) {
70 // fix (node 7+) for making electron updater work when using AWS private buckets, check if headers contain Host property
71 if (options.headers && options.headers.Host) {
72 // set host value from headers.Host
73 options.host = options.headers.Host; // remove header property 'Host', if not removed causes net::ERR_INVALID_ARGUMENT exception
74
75 delete options.headers.Host;
76 } // differential downloader can call this method very often, so, better to cache session
77
78
79 if (this.cachedSession == null) {
80 this.cachedSession = getNetSession();
81 }
82
83 const request = _electron().net.request({ ...options,
84 session: this.cachedSession
85 });
86
87 request.on("response", callback);
88
89 if (this.proxyLoginCallback != null) {
90 request.on("login", this.proxyLoginCallback);
91 }
92
93 return request;
94 }
95
96 addRedirectHandlers(request, options, reject, redirectCount, handler) {
97 request.on("redirect", (statusCode, method, redirectUrl) => {
98 // no way to modify request options, abort old and make a new one
99 // https://github.com/electron/electron/issues/11505
100 request.abort();
101
102 if (redirectCount > this.maxRedirects) {
103 reject(this.createMaxRedirectError());
104 } else {
105 handler(_builderUtilRuntime().HttpExecutor.prepareRedirectUrlOptions(redirectUrl, options));
106 }
107 });
108 }
109
110} exports.ElectronHttpExecutor = ElectronHttpExecutor;
111// __ts-babel@6.0.4
112//# sourceMappingURL=electronHttpExecutor.js.map
\No newline at end of file