Skip to content

Commit c2dfade

Browse files
committed
- wrap lderror in sharedb error format so we can't decode it back to a lderror object in client side.
- bump version
1 parent 79311a9 commit c2dfade

File tree

10 files changed

+71
-22
lines changed

10 files changed

+71
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Logs
22

3+
## v0.0.10
4+
5+
- wrap lderror in sharedb error format so we can't decode it back to a lderror object in client side.
6+
7+
38
## v0.0.9
49

510
- remove `id` from `access` function in readSnapshots for multi-snapsthos scenario

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,13 @@ If `access` is function provided, it will be called in following hooks:
130130
Here is an example to prevent new document creation:
131131

132132
access = ({snapshots}) ->
133-
if snapshots and !(snapshots.0.id) => return Promise.reject(new Error("") <<< {name: 'lderror', id: 1012});
133+
if snapshots and !(snapshots.0.id) =>
134+
return lderror.reject 1012
134135
return Promise.resolve!
135136

137+
Please note that ShareDB logs rejected errors (by recognizing its `stack` attribute) and wrap errors in `{code, message}` format. Consider rejecting a plain lderror object as above example, `@plotdb/ews` will wrap/parse your lderror objects for you so you can receive a correct lderror object in frontend.
138+
139+
136140

137141
## License
138142

dist/sdb-client.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
return (!this._connection
5757
? this.connect()
5858
: Promise.resolve()).then(function(){
59-
return new Promise(function(res, rej){
59+
var p;
60+
p = new Promise(function(res, rej){
6061
var doc;
6162
doc = this$._connection.get(collection != null ? collection : 'doc', id);
6263
return doc.fetch(function(e){
@@ -82,6 +83,12 @@
8283
}
8384
});
8485
});
86+
return p['catch'](function(e){
87+
if (e.code === 'wrapped-lderror') {
88+
e = import$(new Error(), JSON.parse(e.message));
89+
}
90+
return Promise.reject(e);
91+
});
8592
});
8693
},
8794
connect: function(){

dist/sdb-client.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sdb-server.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
(function(){
2-
var sharedb, sharedbPostgres, sharedbPgMdb, ws, http, websocketJsonStream, ews, sdbServer;
2+
var sharedb, sharedbPostgres, sharedbPgMdb, ws, http, websocketJsonStream, ews, lderrorWrapper, sdbServer;
33
sharedb = require('sharedb');
44
sharedbPostgres = require('@plotdb/sharedb-postgres');
55
sharedbPgMdb = require('sharedb-pg-mdb');
66
ws = require('ws');
77
http = require('http');
88
websocketJsonStream = require('websocket-json-stream');
99
ews = require("./index");
10+
lderrorWrapper = function(e){
11+
if (!e) {
12+
e = {
13+
name: 'lderror',
14+
id: 1012
15+
};
16+
}
17+
if (e.name !== 'lderror') {
18+
return e;
19+
} else {
20+
return {
21+
code: "wrapped-lderror",
22+
message: JSON.stringify({
23+
id: e.id,
24+
name: e.name,
25+
message: e.message
26+
})
27+
};
28+
}
29+
};
1030
sdbServer = function(opt){
1131
var app, io, session, access, milestoneDb, wss, metadata, server, mdb, backend, connect, ret;
1232
app = opt.app, io = opt.io, session = opt.session, access = opt.access, milestoneDb = opt.milestoneDb, wss = opt.wss, metadata = opt.metadata;
@@ -93,8 +113,7 @@
93113
}, agent.custom)).then(function(){
94114
return cb();
95115
})['catch'](function(e){
96-
var ref$;
97-
return cb(e || (ref$ = new Error(), ref$.name = 'lderror', ref$.id = 1012, ref$));
116+
return cb(lderrorWrapper(e));
98117
});
99118
});
100119
backend.use('submit', function(arg$, cb){
@@ -110,8 +129,7 @@
110129
}, agent.custom)).then(function(){
111130
return cb();
112131
})['catch'](function(e){
113-
var ref$;
114-
return cb(e || (ref$ = new Error(), ref$.name = 'lderror', ref$.id = 1012, ref$));
132+
return cb(lderrorWrapper(e));
115133
});
116134
});
117135
}

dist/sdb-server.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@plotdb/ews",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "elastic websocket",
55
"main": "dist/index.min.js",
66
"browser": "dist/index.min.js",

src/sdb-client.ls

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ ews.sdb-client.prototype = Object.create(Object.prototype) <<< do
2222

2323
get: ({id, watch, create, collection}) ->
2424
<~ (if !@_connection => @connect! else Promise.resolve!).then _
25-
(res, rej) <~ new Promise _
26-
doc = @_connection.get (if collection? => collection else \doc), id
27-
(e) <~ doc.fetch _
28-
if e => return rej e
29-
doc.subscribe (ops, source) -> res doc
30-
doc.on \error, (err) ~> @fire \error, {doc, err}
31-
if watch? => doc.on \op, (ops, source) -> watch ops, source
32-
if !doc.type => doc.create ((if create => create! else null) or {})
25+
p = new Promise (res, rej) ~>
26+
doc = @_connection.get (if collection? => collection else \doc), id
27+
(e) <~ doc.fetch _
28+
if e => return rej e
29+
doc.subscribe (ops, source) -> res doc
30+
doc.on \error, (err) ~> @fire \error, {doc, err}
31+
if watch? => doc.on \op, (ops, source) -> watch ops, source
32+
if !doc.type => doc.create ((if create => create! else null) or {})
33+
p.catch (e) ~>
34+
if e.code == \wrapped-lderror => e = new Error! <<< JSON.parse(e.message)
35+
Promise.reject e
3336

3437
connect: ->
3538
if @_connection => return Promise.resolve!

src/sdb-server.ls

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
require! <[sharedb @plotdb/sharedb-postgres sharedb-pg-mdb ws http websocket-json-stream]>
22
ews = require "./index"
33

4+
# according to `lib/agent.js`, sharedb send Error to frontend as objects in `{code, message}` format:
5+
# - https://github.com/share/sharedb/blob/master/lib/agent.js#L288 (`getReplyErrorObject` function)
6+
# where allowed error code (as string) can be found here:
7+
# - https://github.com/share/sharedb/blob/master/lib/error.js#L15
8+
# however, this blocks our custom error object.
9+
# thus, we use a custom error code `wrapped-lderror` and stringify lderror in `message` field,
10+
# so we can decode it in frontend.
11+
lderror-wrapper = (e) ->
12+
if !e => e = {name: \lderror, id: 1012}
13+
if e.name != \lderror => e
14+
else {code: "wrapped-lderror", message: JSON.stringify(e{id,name,message})}
15+
416
sdb-server = (opt) ->
517
{app, io, session, access, milestone-db, wss, metadata} = opt
618

@@ -80,12 +92,12 @@ sdb-server = (opt) ->
8092
id = if snapshots.length > 1 => null else snapshots.0.id
8193
access({id, collection, snapshots, type: \readSnapshots} <<< agent.custom)
8294
.then -> cb!
83-
.catch (e) -> cb(e or (new Error! <<< {name: \lderror, id: 1012}))
95+
.catch (e) -> cb lderror-wrapper e
8496
backend.use \submit, ({collection, agent, id}, cb) ->
8597
if !agent.stream.ws => return cb!
8698
access({id, collection, type: \submit} <<< agent.custom)
8799
.then -> cb!
88-
.catch (e) -> cb(e or (new Error! <<< {name: \lderror, id: 1012}))
100+
.catch (e) -> cb lderror-wrapper e
89101

90102
ret = { server, sdb: backend, connect, wss }
91103

0 commit comments

Comments
 (0)