forked from dtolstyi/node-chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
143 lines (125 loc) · 4.52 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
const path = require('path');
const urlParser = require('url');
const got = require('got');
const tunnel = require('tunnel');
const config = require('./config');
const CDN_URL = 'https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/';
module.exports = {
/**
* Returns chromium output folder name for current OS
*
* @returns {string}
*/
getOsChromiumFolderName() {
const platform = process.platform;
let archivePlatformPrefix = platform;
if (platform === 'darwin') {
archivePlatformPrefix = 'mac';
} else if (platform === 'win32') {
archivePlatformPrefix = 'win';
}
return `chrome-${archivePlatformPrefix}`;
},
/**
* Returns path to Chromium executable binary where it's being downloaded
*
* @returns {string}
*/
getOsChromiumBinPath() {
let binPath = path.join(config.BIN_OUT_PATH, this.getOsChromiumFolderName());
const platform = process.platform;
if (platform === 'linux') {
binPath = path.join(binPath, 'chrome');
} else if (platform === 'win32') {
binPath = path.join(binPath, 'chrome.exe');
} else if (platform === 'darwin') {
binPath = path.join(binPath, 'Chromium.app/Contents/MacOS/Chromium');
} else {
throw new Error('Unsupported platform');
}
return binPath;
},
/**
* Returns full URL where Chromium can be found for current OS
*
* @param revision - Chromium revision
*
* @returns {string}
*/
getDownloadUrl(revision) {
return `${this.getOsCdnUrl()}%2F${revision}%2F${this.getOsChromiumFolderName()}.zip?alt=media`;
},
/**
* Returns CDN Url according to current OS
*
* @returns {string}
*/
getOsCdnUrl() {
let url = CDN_URL;
const platform = process.platform;
if (platform === 'linux') {
url += 'Linux';
if (process.arch === 'x64') {
url += '_x64';
}
} else if (platform === 'win32') {
url += 'Win';
if (process.arch === 'x64') {
url += '_x64';
}
} else if (platform === 'darwin') {
url += 'Mac';
} else {
throw new Error('Unsupported platform');
}
return url;
},
/**
* Retrieves latest available Chromium revision number string for current OS
*
* @returns {Promise<String>}
*/
async getLatestRevisionNumber() {
const url = this.getOsCdnUrl() + '%2FLAST_CHANGE?alt=media';
return (await got(url, this.getRequestOptions(url))).body;
},
/**
* Computes necessary configuration options for use with *got*. For the time being this only considers proxy settings.
* @param url the target URL
* @returns {Object}
*/
getRequestOptions(url) {
const requestOptions = {};
const proxy = url.startsWith('https://') ? (process.env.npm_config_https_proxy || process.env.HTTPS_PROXY) :
(process.env.npm_config_proxy || process.env.npm_config_http_proxy || process.env.HTTP_PROXY);
if (proxy) {
const proxyUrl = urlParser.parse(proxy);
const noProxy = (process.env.npm_config_no_proxy || process.env.NO_PROXY || '').split(',');
if (noProxy.find(exc => proxyUrl.hostname.endsWith(exc)) !== undefined) {
console.info('Using http(s) proxy server: ' + proxy);
const tunnelOptions = {
proxy: {
host: proxyUrl.hostname,
port: proxyUrl.port
}
};
if (proxyUrl.username && proxyUrl.password) {
tunnelOptions.proxy.proxyAuth = `${proxyUrl.username}:${proxyUrl.password}`;
}
if (url.startsWith('https://')) {
if (proxy.startsWith('https://')) {
requestOptions.agent = tunnel.httpsOverHttps(tunnelOptions);
} else {
requestOptions.agent = tunnel.httpsOverHttp(tunnelOptions);
}
} else if (proxy.startsWith('https://')) {
requestOptions.agent = tunnel.httpOverHttps(tunnelOptions);
} else {
requestOptions.agent = tunnel.httpOverHttp(tunnelOptions);
}
}
}
return requestOptions;
}
};