Skip to content

Commit 0ef778d

Browse files
authored
Merge pull request #8 from flamebase/develop
Develop
2 parents d0f504e + baf552b commit 0ef778d

File tree

3 files changed

+174
-23
lines changed

3 files changed

+174
-23
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
[ ![flamebase/flamebase-database-node](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js&type=6&v=1.1.0&x2=0)](https://www.npmjs.com/package/flamebase-database-node)
1+
[ ![flamebase/flamebase-database-node](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js&type=6&v=1.2.0&x2=0)](https://www.npmjs.com/package/flamebase-database-node)
22
# :fire: flamebase-database-node
33

44
Real time JSON database (server node).
55

6+
### What is this?
7+
Flamebase is an open source project that tries to emulate Firebase Database features as much as possible. I like Firebase but it's expensive for what it currently offers.
8+
If you are doing an altruist project with Firebase, pray not to became successful, because the monthly amount will increase considerably.
9+
10+
In this repo you can find the proper package for work with json db as flamebase-database-server-cluster does.
11+
For now it still developing, so please be patient with errors.
12+
613
### Usage
714

815
- Import library:
@@ -68,6 +75,7 @@ FD.ref.messages[messageId] = message;
6875
FD.syncToDatabase();
6976
```
7077
At this point we have a JSON reference synchronized with our JSON database.
78+
7179
Define some configuration properties to keep devices up to date when JSON reference changes.
7280
```javascript
7381
var config = {};

index.js

Lines changed: 163 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

2-
var JsonDB = require('node-json-db');
3-
var FCM = require('fcm-push');
4-
var diff = require('rus-diff').diff;
5-
var log4js = require('log4js');
6-
var SN = require('sync-node');
2+
var JsonDB = require('node-json-db');
3+
var FCM = require('fcm-push');
4+
var diff = require('rus-diff').diff;
5+
var log4js = require('log4js');
6+
var SN = require('sync-node');
7+
var sha1 = require('sha1');
8+
79

810
const TAG = "Flamebase Database";
911
var logger = log4js.getLogger(TAG);
@@ -12,6 +14,7 @@ JSON.stringifyAligned = require('json-align');
1214

1315
var ACTION_SIMPLE_UPDATE = "simple_update";
1416
var ACTION_SLICE_UPDATE = "slice_update";
17+
var ACTION_NO_UPDATE = "no_update";
1518

1619
function FlamebaseDatabase(database, path) {
1720

@@ -49,8 +52,14 @@ function FlamebaseDatabase(database, path) {
4952
*/
5053
this.syncFromDatabase = function() {
5154
try {
52-
console.log("####################### data path: " + path);
55+
if (this.debugVal) {
56+
logger.debug("####################### data path: " + path);
57+
}
5358
object.ref = object.db.getData(path);
59+
this.lastStringReference = JSON.stringify(object.ref);
60+
if (this.debugVal) {
61+
logger.debug("####################### ref: " + JSON.stringify(object.ref));
62+
}
5463
object.syncNotifications();
5564
} catch(e) {
5665
console.log("####################### not found, generating {} ");
@@ -138,8 +147,8 @@ function FlamebaseDatabase(database, path) {
138147
}
139148
}
140149

141-
var data_android = this.getPartsFor(this.OS.ANDROID);
142-
var data_ios = this.getPartsFor(this.OS.IOS);
150+
var data_android = this.getPartsFor(this.OS.ANDROID, JSON.parse(this.lastStringReference), this.ref);
151+
var data_ios = this.getPartsFor(this.OS.IOS, JSON.parse(this.lastStringReference), this.ref);
143152

144153
if (object.debugVal) {
145154
logger.debug("android_tokens_size: " + android_tokens.length);
@@ -179,6 +188,8 @@ function FlamebaseDatabase(database, path) {
179188
sen.notification = notification;
180189
this.sendPushMessage(sen);
181190
}
191+
} else if (this.debugVal) {
192+
logger.debug("no differences located");
182193
}
183194
}
184195

@@ -211,6 +222,119 @@ function FlamebaseDatabase(database, path) {
211222
s.notification = notification;
212223
this.sendPushMessage(s);
213224
}
225+
} else if (this.debugVal) {
226+
logger.debug("no differences located");
227+
}
228+
}
229+
};
230+
231+
this.sendDifferencesForClient = function(before, device) {
232+
233+
var ios_tokens = [];
234+
var android_tokens = [];
235+
236+
var id = this.pushConfig.referenceId();
237+
var notification = this.pushConfig.notification();
238+
239+
if (device.os.indexOf(this.OS.IOS) !== -1) {
240+
ios_tokens.push(device.token);
241+
} else {
242+
android_tokens.push(device.token);
243+
}
244+
245+
var data_android = this.getPartsFor(this.OS.ANDROID, JSON.parse(before), this.ref);
246+
var data_ios = this.getPartsFor(this.OS.IOS, JSON.parse(before), this.ref);
247+
248+
if (object.debugVal) {
249+
logger.debug("android_tokens_size: " + android_tokens.length);
250+
logger.debug("ios_tokens_size: " + ios_tokens.length);
251+
logger.debug("data_android_size: " + data_android.parts.length);
252+
logger.debug("data_ios_size: " + data_ios.parts.length);
253+
}
254+
255+
this.lastStringReference = JSON.stringify(this.ref);
256+
257+
if (android_tokens.length > 0) {
258+
if (data_android.parts.length === 1) {
259+
var data = {};
260+
data.id = id;
261+
data.tag = this.pushConfig.tag();
262+
data.reference = data_android.parts[0];
263+
data.action = ACTION_SIMPLE_UPDATE;
264+
data.size = data_android.parts.length;
265+
data.index = 0;
266+
var send = {};
267+
send.data = data;
268+
send.tokens = android_tokens;
269+
send.notification = notification;
270+
this.sendPushMessage(send);
271+
} else if (data_android.parts.length > 1) {
272+
for (var i = 0; i < data_android.parts.length; i++) {
273+
var dat = {};
274+
dat.id = id;
275+
dat.tag = this.pushConfig.tag();
276+
dat.reference = data_android.parts[i];
277+
dat.action = ACTION_SLICE_UPDATE;
278+
dat.index = i;
279+
dat.size = data_android.parts.length;
280+
var sen = {};
281+
sen.data = dat;
282+
sen.tokens = android_tokens;
283+
sen.notification = notification;
284+
this.sendPushMessage(sen);
285+
}
286+
} else {
287+
var data = {};
288+
data.id = id;
289+
data.tag = this.pushConfig.tag();
290+
data.action = ACTION_NO_UPDATE;
291+
var send = {};
292+
send.data = data;
293+
send.tokens = android_tokens;
294+
send.notification = notification;
295+
this.sendPushMessage(send);
296+
}
297+
}
298+
299+
if (ios_tokens.length > 0) {
300+
if (data_ios.parts.length === 1) {
301+
var da = {};
302+
da.id = id;
303+
da.tag = this.pushConfig.tag();
304+
da.reference = data_ios.parts[0];
305+
da.action = ACTION_SIMPLE_UPDATE;
306+
da.size = data_ios.parts.length;
307+
da.index = 0;
308+
var se = {};
309+
se.data = da;
310+
se.tokens = ios_tokens;
311+
se.notification = notification;
312+
this.sendPushMessage(se);
313+
} else if (data_ios.parts.length > 1) {
314+
for (var i = 0; i < data_ios.parts.length; i++) {
315+
var d = {};
316+
d.id = id;
317+
d.tag = this.pushConfig.tag();
318+
d.reference = data_ios.parts[i];
319+
d.action = ACTION_SLICE_UPDATE;
320+
d.index = i;
321+
d.size = data_ios.parts.length;
322+
var s = {};
323+
s.data = d;
324+
s.tokens = ios_tokens;
325+
s.notification = notification;
326+
this.sendPushMessage(s);
327+
}
328+
} else {
329+
var data = {};
330+
data.id = id;
331+
data.tag = this.pushConfig.tag();
332+
data.action = ACTION_NO_UPDATE;
333+
var send = {};
334+
send.data = data;
335+
send.tokens = ios_tokens;
336+
send.notification = notification;
337+
this.sendPushMessage(send);
214338
}
215339
}
216340
};
@@ -245,25 +369,43 @@ function FlamebaseDatabase(database, path) {
245369
});
246370
};
247371

248-
this.getPartsFor = function(os) {
372+
this.getPartsFor = function(os, before, after) {
249373
var notification = this.pushConfig.notification();
250374
var notificationLength = JSON.stringify(notification).length;
251375

252-
var differences = JSON.stringify(diff(JSON.parse(this.lastStringReference), this.ref));
253-
differences = this.string2Hex(differences);
376+
//var differences = JSON.stringify(diff(JSON.parse(this.lastStringReference), this.ref));
377+
var differences = JSON.stringify(diff(before, after));
254378
var partsToSend = [];
255-
var limit = os.indexOf(this.OS.IOS) !== -1 ? this.lengthLimit.IOS - notificationLength : this.lengthLimit.ANDROID - notificationLength;
256-
if (differences.length > limit) {
257-
var index = -1;
258-
var pendingChars = differences.length;
259-
while (pendingChars > 0) {
260-
index++;
261-
var part = differences.slice(index * limit, (pendingChars < limit ? index * limit + pendingChars : (index + 1) * limit));
262-
pendingChars = pendingChars - part.length;
263-
partsToSend.push(part);
379+
380+
if (this.debugVal) {
381+
logger.debug("diff: " + differences);
382+
}
383+
384+
if (differences === "false") {
385+
var currentStringAfter = JSON.stringify(after);
386+
var currentStringBefore = JSON.stringify(before);
387+
if (currentStringBefore.length !== currentStringAfter.length) {
388+
logger.error("something went wrong; sha1 diff: " + currentStringBefore.length + " - " + currentStringAfter.length);
389+
}
390+
if (this.debugVal) {
391+
logger.debug("no differences");
264392
}
265393
} else {
266-
partsToSend.push(differences);
394+
differences = this.string2Hex(differences);
395+
396+
var limit = os.indexOf(this.OS.IOS) !== -1 ? this.lengthLimit.IOS - notificationLength : this.lengthLimit.ANDROID - notificationLength;
397+
if (differences.length > limit) {
398+
var index = -1;
399+
var pendingChars = differences.length;
400+
while (pendingChars > 0) {
401+
index++;
402+
var part = differences.slice(index * limit, (pendingChars < limit ? index * limit + pendingChars : (index + 1) * limit));
403+
pendingChars = pendingChars - part.length;
404+
partsToSend.push(part);
405+
}
406+
} else {
407+
partsToSend.push(differences);
408+
}
267409
}
268410

269411
var result = {};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"log4js": "^1.1.1",
1212
"node-json-db": "^0.7.3",
1313
"rus-diff": "^1.1.0",
14+
"sha1": "^1.1.1",
1415
"sync-node": ">= 1.3.1"
1516
},
1617
"description": "Realtime JSON DB",
@@ -47,5 +48,5 @@
4748
"scripts": {
4849
"test": "echo \"Error: no test specified\" && exit 1"
4950
},
50-
"version": "1.1.0"
51+
"version": "1.2.0"
5152
}

0 commit comments

Comments
 (0)