puppeteer-chromium-resolver
Advanced tools
Comparing version 3.0.1 to 3.1.0
169
index.js
const path = require('path'); | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
const util = require('util'); | ||
const URL = require('url'); | ||
const puppeteer = require('puppeteer-core'); | ||
const PingMonitor = require('ping-monitor'); | ||
const Gauge = require('gauge'); | ||
@@ -47,50 +45,2 @@ const gauge = new Gauge(); | ||
const tryRequest = (url, method, timeout) => { | ||
return new Promise((resolve) => { | ||
output("Try requesting " + url); | ||
var options = URL.parse(url); | ||
options.method = method; | ||
options.timeout = timeout; | ||
var req; | ||
if (options.protocol === 'https:') { | ||
req = require('https').request(options); | ||
} else { | ||
req = require('http').request(options); | ||
} | ||
var timeoutId = setTimeout(() => { | ||
req.abort(); | ||
resolve(); | ||
}, timeout + 1000); | ||
req.setTimeout(timeout); | ||
req.on('response', async (res) => { | ||
clearTimeout(timeoutId); | ||
req.abort(); | ||
//output(res.statusCode); | ||
if (res.statusCode === 200) { | ||
resolve(true); | ||
return; | ||
} | ||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { | ||
var value = await tryRequest(res.headers.location, method, timeout); | ||
resolve(value); | ||
return; | ||
} | ||
resolve(); | ||
}); | ||
req.on('error', () => { | ||
clearTimeout(timeoutId); | ||
req.abort(); | ||
resolve(); | ||
}); | ||
req.on('timeout', () => { | ||
clearTimeout(timeoutId); | ||
req.abort(); | ||
resolve(); | ||
}); | ||
req.end(); | ||
}); | ||
}; | ||
//========================================================================================= | ||
const toMegabytes = (bytes) => { | ||
@@ -109,30 +59,12 @@ const mb = bytes / 1024 / 1024; | ||
//========================================================================================= | ||
const archiveName = (platform, revision) => { | ||
if (platform === 'linux') { | ||
return 'chrome-linux'; | ||
} | ||
if (platform === 'mac') { | ||
return 'chrome-mac'; | ||
} | ||
if (platform === 'win32' || platform === 'win64') { | ||
// Windows archive name changed at r591479. | ||
return parseInt(revision, 10) > 591479 ? 'chrome-win' : 'chrome-win32'; | ||
} | ||
return null; | ||
const delay = async (ms) => { | ||
return new Promise((resolve) => { | ||
if (ms) { | ||
setTimeout(resolve, ms); | ||
} else { | ||
setImmediate(resolve); | ||
} | ||
}); | ||
}; | ||
const getDownloadUrl = (browserFetcher, host, revision) => { | ||
const downloadURLs = { | ||
linux: '%s/chromium-browser-snapshots/Linux_x64/%d/%s.zip', | ||
mac: '%s/chromium-browser-snapshots/Mac/%d/%s.zip', | ||
win32: '%s/chromium-browser-snapshots/Win/%d/%s.zip', | ||
win64: '%s/chromium-browser-snapshots/Win_x64/%d/%s.zip', | ||
}; | ||
var platform = browserFetcher.platform(); | ||
return util.format(downloadURLs[platform], host, revision, archiveName(platform, revision)); | ||
}; | ||
//========================================================================================= | ||
@@ -179,10 +111,2 @@ | ||
}); | ||
//try url if is valid in 5000ms | ||
var downloadUrl = getDownloadUrl(browserFetcher, option.host, option.revision); | ||
var res = await tryRequest(downloadUrl, 'GET', 5000).catch((e) => { | ||
output(e); | ||
}); | ||
if (!res) { | ||
return false; | ||
} | ||
//download start now | ||
@@ -211,2 +135,68 @@ return await downloadNow(option, browserFetcher); | ||
const pingHost = function (host, timeout = 5000) { | ||
return new Promise((resolve) => { | ||
const myMonitor = new PingMonitor({ | ||
website: host | ||
}); | ||
let time_start = Date.now(); | ||
let timeout_id = setTimeout(() => { | ||
myMonitor.stop(); | ||
resolve({ | ||
host: host, | ||
time: timeout, | ||
isUp: 0 | ||
}); | ||
}, timeout); | ||
myMonitor.on('up', function (res, state) { | ||
clearTimeout(timeout_id); | ||
myMonitor.stop(); | ||
resolve({ | ||
host: host, | ||
time: res.time, | ||
isUp: 1 | ||
}); | ||
}); | ||
myMonitor.on('down', function (res) { | ||
clearTimeout(timeout_id); | ||
myMonitor.stop(); | ||
resolve({ | ||
host: host, | ||
time: res.time, | ||
isUp: 0 | ||
}); | ||
}); | ||
myMonitor.on('error', function (error) { | ||
clearTimeout(timeout_id); | ||
myMonitor.stop(); | ||
resolve({ | ||
host: host, | ||
time: Date.now() - time_start, | ||
isUp: 0 | ||
}); | ||
}); | ||
}); | ||
}; | ||
const sortHosts = async (hosts) => { | ||
if (hosts.length < 2) { | ||
return hosts; | ||
} | ||
const list = []; | ||
for (const host of hosts) { | ||
const info = await pingHost(host); | ||
list.push(info); | ||
} | ||
//console.log(list); | ||
list.sort((a, b) => { | ||
if (a.isUp === b.isUp) { | ||
return a.time - b.time; | ||
} | ||
return b.isUp - a.isUp; | ||
}); | ||
//console.log(list); | ||
hosts = list.map(item => item.host); | ||
return hosts; | ||
}; | ||
const downloadHandler = async (option) => { | ||
@@ -219,4 +209,7 @@ // //Not found, try to download to user folder | ||
} | ||
hosts = await sortHosts(hosts); | ||
option.hosts = hosts; | ||
option.retryTimes = 0; | ||
let res = await downloadStart(option); | ||
@@ -355,4 +348,6 @@ if (!res) { | ||
}).catch((error) => { | ||
output(error, true); | ||
//output(error, true); | ||
console.log(error); | ||
}); | ||
await delay(100); | ||
if (browser) { | ||
@@ -362,2 +357,3 @@ option.launchable = true; | ||
browser.close(); | ||
await delay(100); | ||
} | ||
@@ -416,2 +412,3 @@ }; | ||
option.userFolder = initUserFolder(option); | ||
//output("User folder: " + option.userFolder); | ||
@@ -418,0 +415,0 @@ let localChromium = detectionLocalChromium(option); |
{ | ||
"name": "puppeteer-chromium-resolver", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Tool to resolve puppeteer and chromium faster, detect local installed chromium, download chromium with custom mirror host, cache chromium revision out of node_modules, test chromium headless being launchable.", | ||
@@ -8,6 +8,7 @@ "main": "index.js", | ||
"gauge": "^2.7.4", | ||
"puppeteer-core": "^2.0.0" | ||
"ping-monitor": "^0.4.4", | ||
"puppeteer-core": "^2.1.1" | ||
}, | ||
"puppeteer": { | ||
"chromium_revision": "706915" | ||
"chromium_revision": "722234" | ||
}, | ||
@@ -21,2 +22,2 @@ "repository": { | ||
} | ||
} | ||
} |
@@ -64,2 +64,6 @@ | ||
+ v3.1.0 | ||
- updated puppeteer-core version to 2.1.1 | ||
- auto detect host response time and download from quicker one | ||
+ v3.0.1 | ||
@@ -66,0 +70,0 @@ + v2.0.2 |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
82
0
15923
3
370
+ Addedping-monitor@^0.4.4
+ Addedping-monitor@0.4.4(transitive)
Updatedpuppeteer-core@^2.1.1