Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 9158e9b810b67df73c1d6ad91329030e44f7f323 0 parents
@dvv authored
4 .gitignore
@@ -0,0 +1,4 @@
+HZ
+node_modules
+pgts.yaml
+ua.json
4 .npmignore
@@ -0,0 +1,4 @@
+HZ
+node_modules
+pgts.yaml*
+ua.json*
19 LICENSE
@@ -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.
7 Makefile
@@ -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
1  index.js
24 package.json
@@ -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"
+ }
+}
88 ua.js
@@ -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;
Please sign in to comment.
Something went wrong with that request. Please try again.