Skip to content

Commit 522a673

Browse files
committed
implement #5
1 parent d0df715 commit 522a673

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

index.js

+36
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class UdgerParser {
1616
*/
1717
constructor(file) {
1818
this.db = new Database(file, {readonly: true, fileMustExist: true});
19+
this.file = file;
1920
this.ip = null;
2021
this.ua = null;
2122

@@ -29,6 +30,32 @@ class UdgerParser {
2930
this.retIp = {};
3031
}
3132

33+
/**
34+
* Connect (reconnect) sqlite database
35+
* @return {Boolean} true if db has been opened, false if already connected
36+
*/
37+
connect() {
38+
if (!this.db) {
39+
this.db = new Database(this.file, {readonly: true, fileMustExist: true});
40+
return true;
41+
}
42+
return false;
43+
}
44+
45+
/**
46+
* Disconnect sqlite database, avoid read/write conflict
47+
* see https://github.com/udger/udger-updater-nodejs/issues/5
48+
* @return {Boolean} true if db has been closed, false if no db opened
49+
*/
50+
disconnect() {
51+
if (this.db) {
52+
this.db.close();
53+
this.db = null;
54+
return true;
55+
}
56+
return false;
57+
}
58+
3259
/**
3360
* Initialize User-Agent or IP(v4/v6), or both
3461
* @param {Object} data - An object
@@ -149,6 +176,13 @@ class UdgerParser {
149176
}
150177
}
151178

179+
/**
180+
* Clean the cache
181+
*/
182+
cacheClean() {
183+
this.cache = {};
184+
}
185+
152186
/**
153187
* Parse the User-Agent string
154188
* @param {String} ua - An User-Agent string
@@ -813,6 +847,8 @@ class UdgerParser {
813847
*/
814848
parse(opts) {
815849

850+
if (!this.db) return {};
851+
816852
if (
817853
this.isCacheEnable() &&
818854
this.cacheKeyExist(this.keyCache)

test/dbConnect.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const tap = require('tap');
2+
const config = require('./lib/config');
3+
4+
tap.test(
5+
'Connect: disconnect() should return true',
6+
(t) => {
7+
let disconnected = config.udgerParser.disconnect();
8+
t.same(disconnected, true);
9+
t.end();
10+
}
11+
);
12+
13+
tap.test(
14+
'Connect: disconnect() should return false (already disconnected)',
15+
(t) => {
16+
let disconnected = config.udgerParser.disconnect();
17+
t.same(disconnected, false);
18+
t.end();
19+
}
20+
);
21+
22+
tap.test(
23+
'Connect: parse() should return {} because disconnected',
24+
(t) => {
25+
let ret = config.udgerParser.parse();
26+
t.same(ret, {});
27+
t.end();
28+
}
29+
);
30+
31+
tap.test(
32+
'Connect: connect() should return true',
33+
(t) => {
34+
let reconnected = config.udgerParser.connect();
35+
t.same(reconnected, true);
36+
t.end();
37+
}
38+
);
39+
40+
tap.test(
41+
'Connect: connect() should return false (already connected)',
42+
(t) => {
43+
let reconnected = config.udgerParser.connect();
44+
t.same(reconnected, false);
45+
t.end();
46+
}
47+
);
48+
49+
tap.test(
50+
'Connect: parse() should return an object',
51+
(t) => {
52+
let ret = config.udgerParser.parse();
53+
t.same(ret, {
54+
from_cache:false,
55+
ip_address:{},
56+
user_agent:{}
57+
});
58+
t.end();
59+
}
60+
);

0 commit comments

Comments
 (0)