From 932889424a1691c110e2793d44091d832f132a7c Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Thu, 31 Dec 2015 08:26:36 +0100 Subject: [PATCH] detect win64 by user agent fix /download universal link (win64 artifact must be returned) --- lib/index.js | 8 ++------ lib/platforms.js | 10 ++++++++++ test/platforms.js | 9 +++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index cc1dc526..75d69bb4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -85,13 +85,9 @@ module.exports = function nuts(opts) { if (!filename) { // Detect platform from useragent if (!platform) { - if (req.useragent.isMac) platform = platforms.OSX; - if (req.useragent.isWindows) platform = platforms.WINDOWS; - if (req.useragent.isLinux) platform = platforms.LINUX; - if (req.useragent.isLinux64) platform = platforms.LINUX_64; + platform = platforms.detectPlatformByUserAgent(req.useragent) + if (!platform) return next(new Error('No platform specified and impossible to detect one')); } - - if (!platform) return next(new Error('No platform specified and impossible to detect one')); } else { platform = null; } diff --git a/lib/platforms.js b/lib/platforms.js index 764aece1..f8a1764f 100644 --- a/lib/platforms.js +++ b/lib/platforms.js @@ -52,6 +52,15 @@ function detectPlatform(platform) { return _.compact([prefix, suffix]).join('_'); } +function detectPlatformByUserAgent(useragent) { + if (useragent.isMac) return platforms.OSX; + else if (useragent.source.indexOf("WOW64") !== -1 || useragent.source.indexOf("Win64") !== -1) return platforms.WINDOWS_64; + else if (useragent.isWindows) return platforms.WINDOWS; + else if (useragent.isLinux) return platforms.LINUX; + else if (useragent.isLinux64) return platforms.LINUX_64; + else return null +} + // Satisfies a platform function satisfiesPlatform(platform, list) { if (_.contains(list, platform)) return true; @@ -108,6 +117,7 @@ function resolveForVersion(version, platformID, opts) { module.exports = platforms; module.exports.detect = detectPlatform; +module.exports.detectPlatformByUserAgent = detectPlatformByUserAgent; module.exports.satisfies = satisfiesPlatform; module.exports.toType = platformToType; module.exports.resolve = resolveForVersion; diff --git a/test/platforms.js b/test/platforms.js index f82d39b5..b870484e 100644 --- a/test/platforms.js +++ b/test/platforms.js @@ -1,5 +1,6 @@ var should = require('should'); var platforms = require('../lib/platforms'); +var useragent = require('express-useragent'); describe('Platforms', function() { @@ -16,6 +17,14 @@ describe('Platforms', function() { platforms.detect('RELEASES').should.be.exactly(platforms.WINDOWS_32); }); + it('should detect windows_64', function() { + platforms.detect('MyApp-x64.exe').should.be.exactly(platforms.WINDOWS_64); + var chrome = useragent.parse('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36') + platforms.detectPlatformByUserAgent(chrome).should.be.exactly(platforms.WINDOWS_64); + var edge = useragent.parse('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586') + platforms.detectPlatformByUserAgent(edge).should.be.exactly(platforms.WINDOWS_64); + }); + it('should detect linux', function() { platforms.detect('atom-amd64.deb').should.be.exactly(platforms.LINUX_64); });