Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect win64 by user agent #28

Open
wants to merge 1 commit 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
8 changes: 2 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions lib/platforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
9 changes: 9 additions & 0 deletions test/platforms.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var should = require('should');
var platforms = require('../lib/platforms');
var useragent = require('express-useragent');

describe('Platforms', function() {

Expand All @@ -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);
});
Expand Down