From fd0238ac88de0b79659978b884f8d86ba1bbc3d0 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Fri, 16 Dec 2016 13:08:09 +1100 Subject: [PATCH 01/14] Add a platform variant for installing a musl binary --- lib/extensions.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/extensions.js b/lib/extensions.js index 854efbd5e..bda3c1f95 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -182,8 +182,14 @@ function getBinaryName() { } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryName) { binaryName = pkg.nodeSassConfig.binaryName; } else { + var platform = process.platform; + var variant = getPlatformVariant(); + if (variant !== "") { + platform += "_" + variant; + } + binaryName = [ - process.platform, '-', + platform, '-', process.arch, '-', process.versions.modules ].join(''); @@ -359,6 +365,23 @@ function getVersionInfo(binding) { ].join(eol); } +/** + * Gets the platform variant, either an empty string for non-Linux systems or the + * libc implementation (glibc|musl) on Linux. + * + * @api public + */ + +function getPlatformVariant() { + if (process.platform != "linux") { + return ""; + } + if (fs.readFileSync(process.execPath).indexOf('libc.musl-x86_64.so.1')) { + return "musl"; + } + return "glibc"; +} + module.exports.hasBinary = hasBinary; module.exports.getBinaryUrl = getBinaryUrl; module.exports.getBinaryName = getBinaryName; From 85193ea09c43465e2bb5af74e3a99c8e8693cded Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Fri, 16 Dec 2016 13:25:40 +1100 Subject: [PATCH 02/14] Don't use glibc variant, default to linux --- lib/extensions.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/extensions.js b/lib/extensions.js index bda3c1f95..12fe1bdc4 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -188,6 +188,11 @@ function getBinaryName() { platform += "_" + variant; } + // for backwards compat, glibc variant is the default + if (platform === "linux_glibc") { + platform = "linux"; + } + binaryName = [ platform, '-', process.arch, '-', From 5773e8dc311cb3a323eaca4e78a5946ec968424a Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Fri, 16 Dec 2016 13:26:02 +1100 Subject: [PATCH 03/14] Add missing index check for indexOf --- lib/extensions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/extensions.js b/lib/extensions.js index 12fe1bdc4..83b105f9e 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -381,7 +381,7 @@ function getPlatformVariant() { if (process.platform != "linux") { return ""; } - if (fs.readFileSync(process.execPath).indexOf('libc.musl-x86_64.so.1')) { + if (fs.readFileSync(process.execPath).indexOf('libc.musl-x86_64.so.1') !== -1) { return "musl"; } return "glibc"; From a3e3e6d29fb28e9414e2656658647ae62d088590 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Fri, 16 Dec 2016 13:41:35 +1100 Subject: [PATCH 04/14] Fix linting errors --- lib/extensions.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index 83b105f9e..d51e3a003 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -184,13 +184,13 @@ function getBinaryName() { } else { var platform = process.platform; var variant = getPlatformVariant(); - if (variant !== "") { - platform += "_" + variant; + if (variant != '') { + platform += '_' + variant; } // for backwards compat, glibc variant is the default - if (platform === "linux_glibc") { - platform = "linux"; + if (platform === 'linux_glibc') { + platform = 'linux'; } binaryName = [ @@ -378,13 +378,13 @@ function getVersionInfo(binding) { */ function getPlatformVariant() { - if (process.platform != "linux") { - return ""; + if (process.platform != 'linux') { + return ''; } - if (fs.readFileSync(process.execPath).indexOf('libc.musl-x86_64.so.1') !== -1) { - return "musl"; + if (fs.readFileSync(process.execPath).indexOf('libc.musl-x86_64.so.1') != -1) { + return 'musl'; } - return "glibc"; + return 'glibc'; } module.exports.hasBinary = hasBinary; From 3cf42321f1d95297f5ff841a3620ad518bc37a67 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Fri, 16 Dec 2016 14:17:10 +1100 Subject: [PATCH 05/14] Drop glibc variant --- lib/extensions.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index d51e3a003..428d39e3d 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -188,11 +188,6 @@ function getBinaryName() { platform += '_' + variant; } - // for backwards compat, glibc variant is the default - if (platform === 'linux_glibc') { - platform = 'linux'; - } - binaryName = [ platform, '-', process.arch, '-', @@ -371,8 +366,7 @@ function getVersionInfo(binding) { } /** - * Gets the platform variant, either an empty string for non-Linux systems or the - * libc implementation (glibc|musl) on Linux. + * Gets the platform variant, currently either an empty string or 'musl' for Linux/musl platforms. * * @api public */ @@ -384,7 +378,7 @@ function getPlatformVariant() { if (fs.readFileSync(process.execPath).indexOf('libc.musl-x86_64.so.1') != -1) { return 'musl'; } - return 'glibc'; + return ''; } module.exports.hasBinary = hasBinary; From 83e30955cc20eda6e51eb374e9367751e6b2d295 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Fri, 16 Dec 2016 14:18:47 +1100 Subject: [PATCH 06/14] More linting fixes: == -> === --- lib/extensions.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index 428d39e3d..b90275b12 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -184,7 +184,7 @@ function getBinaryName() { } else { var platform = process.platform; var variant = getPlatformVariant(); - if (variant != '') { + if (variant !== '') { platform += '_' + variant; } @@ -372,10 +372,10 @@ function getVersionInfo(binding) { */ function getPlatformVariant() { - if (process.platform != 'linux') { + if (process.platform !== 'linux') { return ''; } - if (fs.readFileSync(process.execPath).indexOf('libc.musl-x86_64.so.1') != -1) { + if (fs.readFileSync(process.execPath).indexOf('libc.musl-x86_64.so.1') !== -1) { return 'musl'; } return ''; From 78f4601d51c066a41d67801ee4be46720cae8bd8 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Sat, 17 Dec 2016 14:23:50 +1100 Subject: [PATCH 07/14] Handle readFileSync exceptions and 0.x Buffer quirks --- lib/extensions.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index b90275b12..116e9b2e3 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -375,10 +375,14 @@ function getPlatformVariant() { if (process.platform !== 'linux') { return ''; } - if (fs.readFileSync(process.execPath).indexOf('libc.musl-x86_64.so.1') !== -1) { - return 'musl'; - } - return ''; + var contents = ''; + try { + contents = fs.readFileSync('foo.bar').toString(); + if (contents.indexOf('libc.musl-x86_64.so.1') !== -1) { + return 'musl'; + } + } catch (err) { } + return '' } module.exports.hasBinary = hasBinary; From b0a63a0366874a21b8459ddfc12aa965b94cd69b Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Sat, 17 Dec 2016 14:47:40 +1100 Subject: [PATCH 08/14] Only use .toString() when needed for speed --- lib/extensions.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/extensions.js b/lib/extensions.js index 116e9b2e3..b808bbd5e 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -377,7 +377,10 @@ function getPlatformVariant() { } var contents = ''; try { - contents = fs.readFileSync('foo.bar').toString(); + contents = fs.readFileSync('foo.bar'); + if (!contents.indexOf) { + contents = contents.toString(); + } if (contents.indexOf('libc.musl-x86_64.so.1') !== -1) { return 'musl'; } From 7e32a9466aa4d0d9720155e8426b8a13241f8d52 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Sat, 17 Dec 2016 14:49:13 +1100 Subject: [PATCH 09/14] Fix linting errors --- lib/extensions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index b808bbd5e..3c0c6f87a 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -384,8 +384,8 @@ function getPlatformVariant() { if (contents.indexOf('libc.musl-x86_64.so.1') !== -1) { return 'musl'; } - } catch (err) { } - return '' + } catch (err) { } // eslint-disable-line no-empty + return ''; } module.exports.hasBinary = hasBinary; From 4647e02f0cef35ebdfe347727a7571d745603221 Mon Sep 17 00:00:00 2001 From: Guy Maliar Date: Sat, 17 Dec 2016 14:17:13 +0200 Subject: [PATCH 10/14] fix getPlatformVariant, getHumanPlatform and getBinaryPath --- lib/extensions.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index 3c0c6f87a..97743039d 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -23,6 +23,7 @@ function getHumanPlatform(platform) { case 'darwin': return 'OS X'; case 'freebsd': return 'FreeBSD'; case 'linux': return 'Linux'; + case 'linux_musl': return 'Linux/musl'; case 'win32': return 'Windows'; default: return false; } @@ -262,7 +263,7 @@ function getBinaryPath() { } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) { binaryPath = pkg.nodeSassConfig.binaryPath; } else { - binaryPath = path.join(defaultBinaryPath, getBinaryName().replace(/_/, '/')); + binaryPath = path.join(defaultBinaryPath, getBinaryName().replace(/_(?=binding\.node)/, '/', '/')); } return binaryPath; @@ -377,7 +378,7 @@ function getPlatformVariant() { } var contents = ''; try { - contents = fs.readFileSync('foo.bar'); + contents = fs.readFileSync(process.execPath); if (!contents.indexOf) { contents = contents.toString(); } From df1803d71c4635a4f07a68452bb9f5123f8b9a04 Mon Sep 17 00:00:00 2001 From: Guy Maliar Date: Sat, 17 Dec 2016 15:08:57 +0200 Subject: [PATCH 11/14] fix extra / argument --- lib/extensions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/extensions.js b/lib/extensions.js index 97743039d..3c8a6a47a 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -263,7 +263,7 @@ function getBinaryPath() { } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) { binaryPath = pkg.nodeSassConfig.binaryPath; } else { - binaryPath = path.join(defaultBinaryPath, getBinaryName().replace(/_(?=binding\.node)/, '/', '/')); + binaryPath = path.join(defaultBinaryPath, getBinaryName().replace(/_(?=binding\.node)/, '/')); } return binaryPath; From 73199fa1ad32cbe731e1b38441f8cf0344334042 Mon Sep 17 00:00:00 2001 From: xzyfer Date: Sun, 18 Dec 2016 14:53:14 +1100 Subject: [PATCH 12/14] Cleanup --- lib/extensions.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index 3c8a6a47a..5e5f40054 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -172,7 +172,9 @@ function getArgument(name, args) { */ function getBinaryName() { - var binaryName; + var binaryName, + variant, + platform = process.platform; if (getArgument('--sass-binary-name')) { binaryName = getArgument('--sass-binary-name'); @@ -183,9 +185,8 @@ function getBinaryName() { } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryName) { binaryName = pkg.nodeSassConfig.binaryName; } else { - var platform = process.platform; - var variant = getPlatformVariant(); - if (variant !== '') { + variant = getPlatformVariant(); + if (variant) { platform += '_' + variant; } @@ -373,10 +374,12 @@ function getVersionInfo(binding) { */ function getPlatformVariant() { + var contents = ''; + if (process.platform !== 'linux') { return ''; } - var contents = ''; + try { contents = fs.readFileSync(process.execPath); if (!contents.indexOf) { @@ -386,6 +389,7 @@ function getPlatformVariant() { return 'musl'; } } catch (err) { } // eslint-disable-line no-empty + return ''; } From 209b1a6ab3a1565086ed88c7419bd977ab18ccab Mon Sep 17 00:00:00 2001 From: xzyfer Date: Sun, 18 Dec 2016 15:05:38 +1100 Subject: [PATCH 13/14] Always toString --- lib/extensions.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index 5e5f40054..9ca86f663 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -381,10 +381,7 @@ function getPlatformVariant() { } try { - contents = fs.readFileSync(process.execPath); - if (!contents.indexOf) { - contents = contents.toString(); - } + contents = fs.readFileSync(process.execPath).toString(); if (contents.indexOf('libc.musl-x86_64.so.1') !== -1) { return 'musl'; } From d38985af04f0ecb6f128d715ed6bbab456e46a60 Mon Sep 17 00:00:00 2001 From: xzyfer Date: Sun, 18 Dec 2016 16:57:10 +1100 Subject: [PATCH 14/14] Delay tostring --- lib/extensions.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/extensions.js b/lib/extensions.js index 9ca86f663..7d5fd9e88 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -381,7 +381,14 @@ function getPlatformVariant() { } try { - contents = fs.readFileSync(process.execPath).toString(); + contents = fs.readFileSync(process.execPath); + + // Buffer.indexOf was added in v1.5.0 so cast to string for old node + // Delay contents.toStrings because it's expensive + if (!contents.indexOf) { + contents = contents.toString(); + } + if (contents.indexOf('libc.musl-x86_64.so.1') !== -1) { return 'musl'; }