-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpure.js
333 lines (257 loc) · 6.83 KB
/
pure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*jshint esnext: true */
// Pure Object Class
class PureMsg {
constructor() {
this.Action = "";
this.DataType = "";
this.LogList = [];
this.RequestMap = new Map();
this.ResponseMap = new Map();
this.TransactionMap = new Map();
}
// Serialize to JSON
// The only problem is the serialization of the Maps
serialize() {
let tmp = {};
Object.assign(tmp, this);
const maps = ['RequestMap', 'ResponseMap', 'TransactionMap'];
for (let map of maps) {
tmp[map] = Object.create(null);
for (let [k, v] of this[map]) {
tmp[map][k] = v;
}
}
return JSON.stringify(tmp);
}
}
// Pure Websocket Link
// Used to wrap request inside a promise object
class PureConn {
openWebsocket() {
this.connecting = true;
this.sock = new WebSocket(this.url);
this.sock.onclose = () => {
this.clientId = "";
this.errorCb({
type: "ConnectionLost",
message: "Connection to the server lost"
});
this.connecting = false;
};
this.sock.onerror = (error) => {
this.errorCb({
type: "WebsocketError",
message: "Error in communicating with the server"
});
};
// Wait for response message to set clientId and create real receiver
this.sock.onmessage = (e) => {
const msg = JSON.parse(e.data);
this.clientId = msg.ClientId;
this.connecting = false;
this.sock.onmessage = (event) => {
let msg = JSON.parse(event.data);
// We initiated the request so there is a promise waiting
if (parseInt(msg.TransactionMap.client, 10) === this.clientId) {
var resolve = this.promisesResolve.get(parseInt(msg.TransactionMap.id, 10));
var reject = this.promisesReject.get(parseInt(msg.TransactionMap.id, 10));
for (let log of msg.LogList || []) {
if (log.Level === 0) {
reject(msg);
}
}
resolve(msg);
}
const id = msg.ResponseMap.id;
// Check if there is a callback
if (this.changeCallback.has(id)) {
this.changeCallback.get(id)(msg);
}
};
for (let r of this.requestQueue) {
// If new connection the client ID changed
r.TransactionMap.set("client", "" + this.clientId);
this.sock.send(r.serialize());
this.requestQueue.delete(r);
}
};
}
constructor(url, username=false, password=false, errorCb=null) {
this.url = url;
this.promisesResolve = new Map();
this.promisesReject = new Map();
this.changeCallback = new Map();
this.idNumber = 0;
this.clientId = "";
this.connecting = false;
this.username = username;
this.password = password;
this.errorCb = errorCb || function() {};
this.requestQueue = new Set();
this.openWebsocket();
}
get id () {
this.idNumber += 1;
return this.idNumber;
}
request(req) {
var reqId = this.id;
const promise = new Promise((resolve, reject) => {
this.promisesResolve.set(reqId, resolve);
this.promisesReject.set(reqId, reject);
});
const tMap = new Map();
tMap.set("id", "" + reqId);
tMap.set("client", "" + this.clientId);
if (this.username) {
tMap.set("username", this.username);
tMap.set("password", this.password);
}
req.TransactionMap = tMap;
if (this.clientId === "") {
this.requestQueue.add(req);
if (this.connecting === false) {
this.openWebsocket();
}
return promise;
}
// Create the handler
this.sock.send(req.serialize());
return promise;
}
}
function processResponse(r) {
// Replace the tag list by a set
let result = r.ResponseMap.result;
let output = {};
for (var re in result) {
// Copy the object
let tmp = Object.assign({}, result[re]);
tmp.Tags = {};
for (var tag of result[re].Tags || []) {
tmp.Tags[tag] = true;
}
output[re] = tmp;
}
return output;
}
class Gomark {
constructor(url, username=false, password=false) {
this.connection = new PureConn(url, username, password, this.error);
}
error(obj) {
if (obj.type == "RetrieveError" && obj.code !== 403) {
return;
}
chrome.notifications.create({
type: "basic",
title: "Gomarks Error",
message: obj.message
});
}
get(url) {
let msg = new PureMsg();
msg.Action = "retrieve";
msg.DataType = "bookmark";
const reqMap = new Map();
reqMap.set("url", url);
msg.RequestMap = reqMap;
return this.connection.request(msg).then((r) => {
return processResponse(r);
}, (r) => {
for (let log of r.LogList || []) {
if (log.Level === 0) {
this.error({
type: "RetrieveError",
message: log.Message,
code: log.Code
});
}
}
throw(r);
});
}
add(url, tags) {
let msg = new PureMsg();
msg.Action = "create";
msg.DataType = "bookmark";
let bookmark = {
url,
tags
};
bookmark.tags = tags;
const reqMap = new Map();
reqMap.set("data", bookmark);
msg.RequestMap = reqMap;
return this.connection.request(msg).then( (result) => {
return processResponse(result);
}, (r) => {
for (let log of r.LogList || []) {
if (log.Level === 0) {
this.error({
type: "CreateError",
message: log.Message,
code: log.Code
});
}
}
throw(r);
});
}
edit(url, tags, add_tags, del_tags) {
let msg = new PureMsg();
msg.Action = "update";
msg.DataType = "bookmark";
let bookmark = {
url,
tags
};
const reqMap = new Map();
reqMap.set("url", url);
reqMap.set("data", bookmark);
reqMap.set("add_tags", add_tags);
reqMap.set("del_tags", del_tags);
msg.RequestMap = reqMap;
return this.connection.request(msg).then( (result) => {
return processResponse(result);
}, (r) => {
for (let log of r.LogList || []) {
if (log.Level === 0) {
this.error({
type: "UpdateError",
message: log.Message,
code: log.Code
});
}
}
throw(r);
});
}
del(url) {
let msg = new PureMsg();
msg.Action = "delete";
msg.DataType = "bookmark";
const reqMap = new Map();
reqMap.set("url", url);
msg.RequestMap = reqMap;
return this.connection.request(msg).then((r) => {
for (let log of r.LogList || []) {
if (log.Level == 2) {
return (log.Message);
}
}
return "Success";
}, (r) => {
for (let log of r.LogList || []) {
if (log.Level === 0) {
this.error({
type: "DeleteError",
message: log.Message,
code: log.Code
});
}
}
throw(r);
});
}
}