Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9158e9b
Showing
7 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
HZ | ||
node_modules | ||
pgts.yaml | ||
ua.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
HZ | ||
node_modules | ||
pgts.yaml* | ||
ua.json* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2011 Vladimir Dronnikov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
all: ua.json | ||
|
||
ua.json: | ||
wget https://github.com/3rd-Eden/useragent/raw/v2.0/tests/fixtures/pgts.yaml | ||
node index.js | ||
|
||
.PHONY: all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ua.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"author": "Vladimir Dronnikov <dronnikov@gmail.com>", | ||
"name": "ua", | ||
"description": "Dirt cheap User-Agent: parser", | ||
"keywords": ["useragent", "browser", "version", "user", "agent"], | ||
"version": "0.0.2", | ||
"main": "./ua.js", | ||
"repository": { | ||
"type": "git", | ||
"url" : "git://github.com/dvv/ua.git" | ||
}, | ||
"licenses": [{ | ||
"type": "MIT", | ||
"url" : "https://github.com/dvv/ua/raw/master/LICENSE" | ||
}], | ||
"dependencies": { | ||
"yamlparser": "latest" | ||
}, | ||
"devDependencies": {}, | ||
"scripts": { | ||
"install": "make", | ||
"postinstall": "make" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
|
||
/*! | ||
* | ||
* Copyright(c) 2011 Vladimir Dronnikov <dronnikov@gmail.com> | ||
* MIT Licensed | ||
* | ||
*/ | ||
|
||
/** | ||
* | ||
* Useragent database hash, keyed by User-Agent: | ||
* | ||
*/ | ||
|
||
// not-available entry | ||
var NA = { | ||
family: "Other" | ||
}; | ||
|
||
var uatable = { | ||
// fallback keys | ||
'': NA, | ||
'undefined': NA, | ||
'null': NA | ||
}; | ||
|
||
/** | ||
* | ||
* Load useragent database into memory | ||
* | ||
*/ | ||
|
||
function prepare(src) { | ||
var Fs = require('fs'); | ||
var freemem = require('os').freemem; | ||
var m0 = freemem(); | ||
// load prepared JSON | ||
try { | ||
uatable = JSON.parse(Fs.readFileSync('ua.json', 'utf8')); | ||
// no prepared JSON. convert from yaml source | ||
} catch(err) { | ||
var raw = require('yamlparser').eval(Fs.readFileSync(src, 'utf8')).test_cases; | ||
// convert array to hash, to achieve O(1) search time | ||
for (var i = raw.length; --i >= 0 ;) { | ||
var v = raw[i]; | ||
// workaround: yamlparser inserts empty hashes for empty key values | ||
if (Object(v.v1) === v.v1) delete v.v1; | ||
if (Object(v.v2) === v.v2) delete v.v2; | ||
if (Object(v.v3) === v.v3) delete v.v3; | ||
// hash by user agent | ||
var k = v.user_agent_string; | ||
delete v.user_agent_string; | ||
uatable[k] = v; | ||
} | ||
// dump for future reuse | ||
Fs.writeFileSync('ua.json', JSON.stringify(uatable), 'utf8'); | ||
console.error('User agent lookup table prepared.'); | ||
} | ||
console.error('User agent lookup table loaded.', Math.round((m0-freemem())/1024/1024*100)/100, 'Mb taken'); | ||
return uatable; | ||
} | ||
|
||
/** | ||
* | ||
* Lookup given `str` in useragent database, return found value | ||
* | ||
*/ | ||
|
||
function lookup(str, cb) { | ||
var ua = uatable[str]; | ||
if (!ua) { | ||
// need fetch from outside | ||
//console.error('NOT FOUND IN DB'); | ||
ua = NA; | ||
} | ||
if (cb) cb(null, ua); // allow continuation | ||
return ua; | ||
} | ||
|
||
/** | ||
* | ||
* Initialize the module | ||
* | ||
*/ | ||
|
||
prepare('pgts.yaml'); | ||
module.exports = lookup; |