Skip to content

Commit a1bc3d9

Browse files
authored
Merge pull request #23 from apb2006/dev
Dev
2 parents 1a87de5 + bbabb11 commit a1bc3d9

8 files changed

Lines changed: 103 additions & 53 deletions

File tree

LICENSE

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
Copyright (c) 2011-2012 Andy Bunce
1+
Copyright (c) 2011-2017 Andy Bunce
2+
All rights reserved.
23

3-
Permission is hereby granted, free of charge, to any person obtaining
4-
a copy of this software and associated documentation files (the
5-
"Software"), to deal in the Software without restriction, including
6-
without limitation the rights to use, copy, modify, merge, publish,
7-
distribute, sublicense, and/or sell copies of the Software, and to
8-
permit persons to whom the Software is furnished to do so, subject to
9-
the following conditions:
4+
The BSD 3-Clause License
105

11-
The above copyright notice and this permission notice shall be
12-
included in all copies or substantial portions of the Software.
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions
8+
are met:
139

14-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
1. Redistributions of source code must retain the above copyright
11+
notice, this list of conditions and the following disclaimer.
12+
13+
2. Redistributions in binary form must reproduce the above copyright
14+
notice, this list of conditions and the following disclaimer in the
15+
documentation and/or other materials provided with the distribution.
16+
17+
3. Neither the name of the copyright holders nor the names of its
18+
contributors may be used to endorse or promote products derived
19+
from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ To install with npm:
2121
$ mkdir myproject
2222
cd myproject
2323
$ npm install basex
24-
basex@0.8.0 ./node_modules/basex
24+
basex@0.9.0 ./node_modules/basex
2525
```
2626

2727
Once BaseX is installed and the BaseX server is running, test it.

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v0.9.0 - 2017-03-23
2+
- session object now emits socketError events - see #9 #21
3+
- min node version set to 4.0
4+
- dependancies updated
5+
16
## v0.8.0 - 2017-03-14
27
- Remove obsolete event watch interface.
38
- Fix #21 query.execute with error.

docs/commands.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ The default values are:
3131
username (default="admin")
3232
password (default= "admin")
3333

34+
Since version 0.8.1 the `session` object emits `socketError` events on socket errors.
35+
This allows an application to react to events such as the server doing down.(See `examples/issue22.js`)
36+
3437
# Session commands
3538

3639
##execute
@@ -85,23 +88,7 @@ Replaces a document with the specified input stream.
8588
````
8689
Stores raw data at the specified path.
8790

88-
##watch
89-
````
90-
session.watch(name,notification,callback)
91-
````
92-
Request notifications for event with `name`. The function `notification` is called
93-
each time an event with the name is received. The signature is `notification(name,data)`.
94-
````
95-
function watchCallback(name,msg){
96-
console.log("watch update-----> ",msg)
97-
};
98-
session1.watch("testevent",watchCallback, log.print);
99-
````
100-
##unwatch
101-
````
102-
session.unwatch(name,callback)
103-
````
104-
Unwatches the specified event.
91+
10592

10693
##info
10794
session.info(callback)

examples/issue21.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
const basex = require("../index");
55
var session = new basex.Session()
6-
basex.debug_mode = false;
6+
basex.debug_mode = true;
77
function performQuery(q_str) {
88
return new Promise(rs => {
99
var q = session.query(q_str)

examples/issue22.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* socket errors #22
3+
*/
4+
const basex = require("../index");
5+
6+
basex.debug_mode = false;
7+
var session;
8+
9+
10+
function socketError(e){
11+
console.log("socket trouble detected..",e.code);
12+
if (e.code == 'ECONNREFUSED') {
13+
console.error('connection refused. Check BaseX server is running.');
14+
} else if(e.code == 'ECONNRESET' || e.code == 'EPIPE'){
15+
console.log("Restarting client in 10 seconds");
16+
setTimeout(work, 10000); // 10 seconds pass..
17+
} else {
18+
console.error("SOCKET ERROR: ", e.code,":", e);
19+
}
20+
};
21+
function work(){
22+
console.log("New session");
23+
session = new basex.Session();
24+
session.on("socketError",socketError);
25+
// poll server time every second
26+
setInterval(serverdateTime,1000);
27+
};
28+
29+
30+
function serverdateTime() {
31+
var query = 'fn:current-dateTime()';
32+
var q = session.query(query)
33+
q.execute((e, r) => { console.log("Result: ", r.result, "; Error:", e);});
34+
};
35+
36+
37+
38+
work();
39+
40+
41+
42+

index.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ var Session = function(host, port, username, password) {
6161
this.buffer = "";
6262
this.q_pending = new Queue(); // holds commands to send
6363
this.q_sent = new Queue(); // holds commands sent
64-
// event stuff
65-
this.event = null;
64+
6665
// initial parser for auth
6766
/**
6867
* Description
@@ -154,15 +153,14 @@ var Session = function(host, port, username, password) {
154153
//console.dir(self.current_command);
155154
}
156155
};
157-
158-
this.stream.on("error", socketError);
159-
156+
157+
this.stream.on("error", socketError.bind(this));
158+
// this.stream.on('error', this.emit.bind(this, 'socketError'));
160159
this.stream.on("close", function() {
161160
if (exports.debug_mode) {
162161
console.log(self.tag + ": stream closed");
163162
}
164-
if (self.event) self.event.close()
165-
163+
166164
if (self.closefn) {
167165
self.closefn();
168166
self.closefn = null;
@@ -413,18 +411,25 @@ var Session = function(host, port, username, password) {
413411
};
414412

415413
/**
416-
* Description
414+
* react to socket errors.
415+
* @TODO research error meanings
417416
* @method socketError
418417
* @param {} e
419418
* @return
420419
*/
421420
function socketError(e) {
422421
if (e.code == 'ECONNREFUSED') {
423-
console.log('ECONNREFUSED: connection refused. Check BaseX server is running.');
422+
console.error('ECONNREFUSED: connection refused. Check BaseX server is running.');
423+
}else if (e.code == 'ECONNRESET') {
424+
console.error('ECONNRESET: reset.',this.tag);
425+
}else if ( e.code == 'EPIPE') {
426+
console.error('EPIPE');
424427
} else {
425-
console.log("SOCKET ERROR: ", e);
426-
}
428+
console.error("SOCKET ERROR: ", e);
429+
};
430+
this.emit("socketError",e)
427431
}
432+
428433
/**
429434
* hash str using md5
430435
* @method md5

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "basex",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"description": "A BaseX (XML database) client library",
55
"keywords":
66
[
77
"xml",
88
"xquery",
99
"xslt",
10-
"search",
11-
"database"
10+
"database",
11+
"basex"
1212
],
1313

1414
"author": "Andy Bunce ",
@@ -26,7 +26,7 @@
2626

2727
"dependencies":
2828
{
29-
"combined-stream": "0.0.4"
29+
"combined-stream": "1.0.3"
3030
},
3131

3232
"devDependencies":
@@ -38,8 +38,8 @@
3838

3939
"engines":
4040
{
41-
"node": ">=0.6"
41+
"node": ">=4.0"
4242
},
4343

44-
"license": "BSD"
44+
"license": "BSD-3-Clause"
4545
}

0 commit comments

Comments
 (0)