-
Notifications
You must be signed in to change notification settings - Fork 1
/
firebase-server.js
65 lines (54 loc) · 2.11 KB
/
firebase-server.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
var FirebaseServer = require('firebase-server');
var fs = require("fs");
var util = require('util');
var port = 5000;
var hostname = 'localhost.firebaseio.com';
var SECRET = 'MYSECRET123';
//var generate_token_with_uid = 'malignant-unit-testing';
var server = new FirebaseServer(port, hostname + ':' + port, {
/* You can put your initial data model here, or just leave it empty */
});
var rulesText = fs.readFileSync("src/firebase-rules.json", 'utf8');
/* json-comments, strip-json-comments, json5 all changed double-quotes to single and were a bit overkill
Just remove // quotes myself
*/
var rx = /\s*\/\/[^\n]+/g;
rulesText = rulesText.replace(rx, '');
console.log('Firebase Rules (after comment removal):\n', rulesText);
var rules = JSON.parse(rulesText);
// Allow the admin token to wipe database for unit tests
rules.rules['.write'] = "auth.uid === 'admin'";
//util.inspect(rules, {showHidden: false, depth: null}));
server.setRules(rules);
if (typeof generate_token_with_uid !== 'undefined' && generate_token_with_uid.length > 1) {
var FirebaseTokenGenerator = require("firebase-token-generator");
var tokenGenerator = new FirebaseTokenGenerator(SECRET);
var token = tokenGenerator.createToken({ uid: generate_token_with_uid});
console.log('new custom token for '+generate_token_with_uid, token);
}
server.setAuthSecret(SECRET);
console.info('FirebaseServer started at ' + hostname + ':' + port);
function exitHandler(options, err) {
if (options.cleanup) {
console.log('cleanup...', options);
server.exportData().then(function (data) {
console.log('exported data\n', data);
server.close();
process.exit();
});
}
else {
if (options.error) {
console.log(err.stack);
}
// if (options.ext)
console.log('exiting...', options);
process.exit();
}
}
//do something when app is closing
//process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true, cleanup: true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {error:true}));