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

Implemented osVersion #136

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module provides details such as the following:
"browser":"Chrome",
"version":"17.0.963.79",
"os":"Windows 7",
"osVersion": "6.1"
"platform":"Microsoft Windows",
"source":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79..."
}
Expand Down
152 changes: 152 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Type definitions for express-useragent 1.0
// Project: https://www.npmjs.org/package/express-useragent
// Definitions by: Isman Usoh <https://github.com/isman-usoh>
// Ciarán Ingle <https://github.com/inglec-arista>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

import { Request, Response, NextFunction } from 'express';

declare global {
namespace Express {
interface Request {
useragent?: Details;
}
}
}

export interface Details {
isMobile: boolean;
isTablet: boolean;
isiPad: boolean;
isiPod: boolean;
isiPhone: boolean;
isAndroid: boolean;
isBlackberry: boolean;
isOpera: boolean;
isIE: boolean;
isEdge: boolean;
isIECompatibilityMode: boolean;
isSafari: boolean;
isFirefox: boolean;
isWebkit: boolean;
isChrome: boolean;
isKonqueror: boolean;
isOmniWeb: boolean;
isSeaMonkey: boolean;
isFlock: boolean;
isAmaya: boolean;
isEpiphany: boolean;
isDesktop: boolean;
isWindows: boolean;
isWindowsPhone: boolean;
isLinux: boolean;
isLinux64: boolean;
isMac: boolean;
isChromeOS: boolean;
isBada: boolean;
isSamsung: boolean;
isRaspberry: boolean;
isBot: boolean;
isCurl: boolean;
isAndroidTablet: boolean;
isWinJs: boolean;
isKindleFire: boolean;
isSilk: boolean;
isCaptive: boolean;
isSmartTV: boolean;
silkAccelerated: boolean;
browser: string;
version: string;
os: string;
osVersion: string;
platform: string;
geoIp: { [key: string]: any };
source: string;
}

export const version: string;

export const DefaultAgent: Details;

export const Agent: Details;

export function getBrowser(string: string): string;

export function getBrowserVersion(string: string): string;

export function getOS(string: string): string;

export function getPlatform(string: string): string;

export function testCompatibilityMode(): void;

export function testSilk(): 'Silk' | false;

export function testKindleFire(): string | false;

export function testCaptiveNetwork(): 'CaptiveNetwork' | false;

export function reset(): Details;

export function testMobile(): void;

export function testTablet(): void;

export function testNginxGeoIP(): void;

export function testBot(): void;

export function testSmartTV(): void;

export function testAndroidTablet(): void;

export function testWebkit(): void;

export function parse(source: string): Details;

export class UserAgent {
version: string;

DefaultAgent: Details;

Agent: Details;

getBrowser(string: string): string;

getBrowserVersion(string: string): string;

getOS(string: string): string;

getOSVersion(string: string) : string;

getPlatform(string: string): string;

testCompatibilityMode(): void;

testSilk(): 'Silk' | false;

testKindleFire(): string | false;

testCaptiveNetwork(): 'CaptiveNetwork' | false;

reset(): Details;

testMobile(): void;

testTablet(): void;

testNginxGeoIP(): void;

testBot(): void;

testSmartTV(): void;

testAndroidTablet(): void;

testWebkit(): void;

parse(source: string): Details;
}

export function express(): (req: Request, res: Response, next?: NextFunction) => void;
52 changes: 51 additions & 1 deletion lib/express-useragent.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,28 @@
Wii: /wii/i,
PS3: /playstation 3/i,
PSP: /playstation portable/i,
PS4: /playstation 4/i,
iPad: /\(iPad.*os (\d+)[._](\d+)/i,
iPhone: /\(iPhone.*os (\d+)[._](\d+)/i,
iOS: /ios/i,
Bada: /Bada\/(\d+)\.(\d+)/i,
Curl: /curl\/(\d+)\.(\d+)\.(\d+)/i,
Electron: /Electron\/(\d+)\.(\d+)\.(\d+)/i,
};

this._OSVersions = {
Windows: /(?:windows nt) ([\d\.\-\_]+)/i,
WindowsPhone: /(?:windows phone) ([\d\.\-\_]+)/i,
Mac: /(?:os x) ([\d\.\-\_]+)/i,
iPhone: /(?:iphone.* os) ([\d\.\-\_]+)/i,
iPad: /(?:ipad.* os) ([\d\.\-\_]+)/i,
Android: /(?:android) ([\d\.\-\_]+)/i,
Ubuntu: /(?:ubuntu)\/([\d\.\-\_]+)/i,
ChromeOS: /(?:cros(.*)?) ([\d\.\-\_]+)/i,
PS3: /(?:playstation 3[;]?) ([\d\.\-\_]+)/i,
PS4: /(?:playstation 4) ([\d\.\-\_]+)/i,
};

this._Platform = {
Windows: /windows nt/i,
WindowsPhone: /windows phone/i,
Expand Down Expand Up @@ -592,6 +607,40 @@
}
};

this.getOSVersion = function (string) {

var osVersion = null;

if (this.Agent.isWindowsPhone)
osVersion = this._OSVersions.WindowsPhone.exec(string);
else if (this.Agent.isWindows)
osVersion = this._OSVersions.Windows.exec(string);
else if (this.Agent.isiPhone)
osVersion = this._OSVersions.iPhone.exec(string);
else if (this.Agent.isiPad)
osVersion = this._OSVersions.iPad.exec(string);
else if (this.Agent.isMac)
osVersion = this._OSVersions.Mac.exec(string);
else if (this.Agent.isChromeOS)
osVersion = this._OSVersions.ChromeOS.exec(string);
else if (this.Agent.isAndroid || this.isAndroidNative || this.isAndroidTablet)
osVersion = this._OSVersions.Android.exec(string);
else if (this.Agent.isLinux || this.Agent.isLinux64) {
osVersion = this._OSVersions.Ubuntu.exec(string);

// Android is basically Linux, we can try to find the version for Android devices.
if (!osVersion)
osVersion = this._OSVersions.Android.exec(string);
}

if (osVersion && osVersion.length > 1)
osVersion = osVersion[osVersion.length - 1];
else
return '';

return osVersion.replace(/(_)/g, '.');
};

this.getPlatform = function (string) {
switch (true) {
case this._Platform.Windows.test(string):
Expand Down Expand Up @@ -807,7 +856,7 @@
var ua = this;
var isBot = IS_BOT_REGEXP.exec(ua.Agent.source.toLowerCase());
if (isBot) {
ua.Agent.isBot = isBot[1];
ua.Agent.isBot = !!isBot[1];
} else if (!ua.Agent.isAuthoritative) {
// Test unauthoritative parse for `bot` in UA to flag for bot
ua.Agent.isBot = /bot/i.test(ua.Agent.source);
Expand Down Expand Up @@ -851,6 +900,7 @@
ua.Agent.browser = ua.getBrowser(ua.Agent.source);
ua.Agent.version = ua.getBrowserVersion(ua.Agent.source);
ua.Agent.electronVersion = ua.getElectronVersion(ua.Agent.source);
ua.Agent.osVersion = ua.getOSVersion(ua.Agent.source);
ua.testBot();
ua.testSmartTV();
ua.testMobile();
Expand Down
Loading