Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Adding support for Phantom 2.5.0 beta #689

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

var requestProgress = require('request-progress')
var progress = require('progress')
var extractZip = require('extract-zip')
var decompress = require('decompress')
var cp = require('child_process')
var fs = require('fs-extra')
var helper = require('./lib/phantomjs')
Expand All @@ -24,6 +24,7 @@ var originalPath = process.env.PATH

var checkPhantomjsVersion = util.checkPhantomjsVersion
var getTargetPlatform = util.getTargetPlatform
var getTargetPlatformRelease = util.getTargetPlatformRelease
var getTargetArch = util.getTargetArch
var getDownloadSpec = util.getDownloadSpec
var findValidPhantomJsBinary = util.findValidPhantomJsBinary
Expand Down Expand Up @@ -153,7 +154,10 @@ function getRequestOptions() {

var proxyUrl = process.env.npm_config_https_proxy ||
process.env.npm_config_http_proxy ||
process.env.npm_config_proxy
process.env.npm_config_proxy ||
process.env.http_proxy;


if (proxyUrl) {

// Print using proxy
Expand Down Expand Up @@ -266,35 +270,22 @@ function extractDownload(filePath) {
// extract to a unique directory in case multiple processes are
// installing and extracting at once
var extractedPath = filePath + '-extract-' + Date.now()
var options = {cwd: extractedPath}

fs.mkdirsSync(extractedPath, '0777')
// Make double sure we have 0777 permissions; some operating systems
// default umask does not allow write by default.
fs.chmodSync(extractedPath, '0777')

if (filePath.substr(-4) === '.zip') {
console.log('Extracting zip contents')
extractZip(path.resolve(filePath), {dir: extractedPath}, function(err) {
if (err) {
console.error('Error extracting zip')
deferred.reject(err)
} else {
deferred.resolve(extractedPath)
}
})
console.log('Extracting archive')
decompress(path.resolve(filePath), extractedPath)
.then(function(files) {
deferred.resolve(extractedPath)
},
function(err) {
console.error('Error extracting zip')
deferred.reject(err)
});

} else {
console.log('Extracting tar contents (via spawned process)')
cp.execFile('tar', ['jxf', path.resolve(filePath)], options, function (err) {
if (err) {
console.error('Error extracting archive')
deferred.reject(err)
} else {
deferred.resolve(extractedPath)
}
})
}
return deferred.promise
}

Expand Down Expand Up @@ -405,7 +396,7 @@ function downloadPhantomjs() {
var downloadSpec = getDownloadSpec()
if (!downloadSpec) {
console.error(
'Unexpected platform or architecture: ' + getTargetPlatform() + '/' + getTargetArch() + '\n' +
'Unexpected platform or architecture: ' + getTargetPlatform() + '/' + getTargetArch() + '/' + getTargetPlatformRelease() + '\n' +
'It seems there is no binary available for your platform/architecture\n' +
'Try to install PhantomJS globally')
exit(1)
Expand Down
2 changes: 1 addition & 1 deletion lib/phantomjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ try {
* The version of phantomjs installed by this package.
* @type {number}
*/
exports.version = '2.1.1'
exports.version = '2.5.0'


/**
Expand Down
90 changes: 69 additions & 21 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ var hasha = require('hasha')
var helper = require('./phantomjs')
var kew = require('kew')
var path = require('path')
var os = require('os')

var DEFAULT_CDN = 'https://github.com/Medium/phantomjs/releases/download/v2.1.1'
var DEFAULT_CDN = 'https://bitbucket.org/ariya/phantomjs/downloads'
var libPath = __dirname

/**
Expand Down Expand Up @@ -71,14 +72,16 @@ function writeLocationFile(location) {
}

var platform = getTargetPlatform()
var release = getTargetPlatformRelease()
var arch = getTargetArch()

var contents = 'module.exports.location = "' + location + '"\n'

if (/^[a-zA-Z0-9]*$/.test(platform) && /^[a-zA-Z0-9]*$/.test(arch)) {
contents +=
'module.exports.platform = "' + getTargetPlatform() + '"\n' +
'module.exports.arch = "' + getTargetArch() + '"\n'
'module.exports.platform = "' + platform + '"\n' +
'module.exports.platformRelease = "' + release + '"\n' +
'module.exports.arch = "' + arch + '"\n'
}

fs.writeFileSync(path.join(libPath, 'location.js'), contents)
Expand All @@ -88,30 +91,53 @@ function writeLocationFile(location) {
* @return {?{url: string, checksum: string}} Get the download URL and expected
* SHA-256 checksum for phantomjs. May return null if no download url exists.
*/
function getDownloadSpec() {
function getDownloadSpec()
{
var cdnUrl = process.env.npm_config_phantomjs_cdnurl ||
process.env.PHANTOMJS_CDNURL ||
DEFAULT_CDN

var downloadUrl = cdnUrl + '/phantomjs-' + helper.version + '-'
var checksum = ''

var platform = getTargetPlatform()
var arch = getTargetArch()
if (platform === 'linux' && arch === 'x64') {
downloadUrl += 'linux-x86_64.tar.bz2'
checksum = '86dd9a4bf4aee45f1a84c9f61cf1947c1d6dce9b9e8d2a907105da7852460d2f'
} else if (platform === 'linux' && arch == 'ia32') {
downloadUrl += 'linux-i686.tar.bz2'
checksum = '80e03cfeb22cc4dfe4e73b68ab81c9fdd7c78968cfd5358e6af33960464f15e3'
} else if (platform === 'darwin') {
downloadUrl += 'macosx.zip'
checksum = '538cf488219ab27e309eafc629e2bcee9976990fe90b1ec334f541779150f8c1'
} else if (platform === 'win32') {
downloadUrl += 'windows.zip'
checksum = 'd9fb05623d6b26d3654d008eab3adafd1f6350433dfd16138c46161f42c7dcc8'
} else {
var platform = getTargetPlatform();

if (platform === 'linux')
{
var release = getTargetPlatformRelease();

if (release)
{
var releaseMajor = release.split(".")[0];

if (releaseMajor == 14) {
downloadUrl += 'beta-linux-ubuntu-trusty-x86_64.tar.gz'
checksum = '00E75A5A359A777DB95E4EDEDD4C5E8755D5852CB016CBCCFCA6F5E2A08C4A09'
}
else if (releaseMajor == 16) {
downloadUrl += 'beta-linux-ubuntu-xenial-x86_64.tar'
checksum = 'B478BB44E7A77468683A615BDA082716D76E99DC17EBBC005D31D06B6715429E'
}
else {
return null;
}
}
else {
return null;
}
}
else if (platform === 'darwin') {
downloadUrl += 'beta-macos.zip'
checksum = '8183EAAAC1BF73EDBE2414870FFA4B6262B42AB97D3F252CEE073746CA272ACC'
}
else if (platform === 'win32') {
downloadUrl += 'beta-windows.zip'
checksum = '3E0D684E7564862CBF43E38B9E29DD087052DE19D76C525D6F08C37D211A4381'
}
else {
return null
}

return {url: downloadUrl, checksum: checksum}
}

Expand Down Expand Up @@ -140,20 +166,42 @@ function verifyChecksum(fileName, checksum) {
* @return {string}
*/
function getTargetPlatform() {
return process.env.PHANTOMJS_PLATFORM || process.platform
return process.env.PHANTOMJS_PLATFORM || os.platform();
}

/**
* @return {string}
*/
function getTargetPlatformRelease() {

var platform = getTargetPlatform();

if (platform === 'linux')
{
let child = cp.spawnSync( 'lsb_release', ['-a'] );
let lsb_release = child.stdout.toString();

var match = lsb_release.match(/(?:Release:\s+)([0-9\.]+)/);
if (match) {
return match[1];
}
}

return process.env.PHANTOMJS_PLATFORM_RELEASE || os.release();
}

/**
* @return {string}
*/
function getTargetArch() {
return process.env.PHANTOMJS_ARCH || process.arch
return process.env.PHANTOMJS_ARCH || os.arch();
}

module.exports = {
checkPhantomjsVersion: checkPhantomjsVersion,
getDownloadSpec: getDownloadSpec,
getTargetPlatform: getTargetPlatform,
getTargetPlatformRelease: getTargetPlatformRelease,
getTargetArch: getTargetArch,
findValidPhantomJsBinary: findValidPhantomJsBinary,
verifyChecksum: verifyChecksum,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phantomjs-prebuilt",
"version": "2.1.14",
"version": "2.5.0",
"keywords": [
"phantomjs",
"headless",
Expand Down Expand Up @@ -39,8 +39,8 @@
"test": "nodeunit --reporter=minimal test/tests.js && eslint ."
},
"dependencies": {
"decompress": "^4.1.0",
"es6-promise": "~4.0.3",
"extract-zip": "~1.5.0",
"fs-extra": "~1.0.0",
"hasha": "~2.2.0",
"kew": "~0.7.0",
Expand Down