forked from jychuah/particle-firebase-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
360 lines (320 loc) · 10.3 KB
/
index.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
var throng = require('throng');
var express = require('express');
var bodyParser = require('body-parser');
var firebase = require('firebase');
var Particle = require('particle-api-js');
var fs = require('fs');
if (!process.env.DATABASE) {
console.log("No DATABASE environment variable specified!");
process.exit();
}
if (!process.env.SERVICEACCOUNTFILE) {
console.log("No SERVICEACCOUNTFILE environment variable specified!");
}
try {
stats = fs.lstatSync(process.env.SERVICEACCOUNTFILE);
} catch(error) {
console.log("Couldn't open " + process.env.SERVICEACCOUNTFILE);
process.exit();
}
var WORKERS = process.env.WEB_CONCURRENCY || 1;
function start() {
var app = express();
var router = express.Router();
app.use(bodyParser.json());
var particle = new Particle();
var firebaseApps = { };
var particleStreams = { };
console.log("Environment: ", process.env);
// Verify that request has device_id and particle_token query fields
function verifyRequest(req, res, next) {
var properties = ['device_id', 'particle_token'];
for (var index in properties) {
if (!req.query.hasOwnProperty(properties[index])) {
res.status(400).send("Missing property: " + properties[index]);
res.end();
return;
}
}
next();
}
function stripDotJson(path) {
if (path.indexOf(".json") == path.length - 5) {
return path.substring(0, path.length - 5);
}
return path;
}
// Verify that device_id and particle_token are valid by trying
// to grab an event stream from the Particle.io cloud
// Also, if the device ever goes offline, dispose of the event stream
function verifyParticle(req, res, next) {
var device_id = req.query.device_id;
var devicesPr = particle.getEventStream({ deviceId : device_id, auth: req.query.particle_token });
devicesPr.then(
function(stream) {
if (particleStreams[device_id]) {
particleStreams[device_id].end();
}
particleStreams[device_id] = stream;
stream.on('event', function(data) {
// if device went offline, end streams, delete firebase app, delete references
if (data.data === 'offline') {
try {
particleStreams[device_id].end();
delete particleStreams[device_id];
firebaseApps[device_id].delete().then(function() {
delete firebaseApps[device_id];
console.log("Released resources for device: ", device_id);
});
} catch (error) {
console.error(error);
}
}
});
console.log("Established event stream for: ", device_id);
next();
},
function (error) {
if (particleStreams[device_id]) {
particleStreams[device_id].end();
}
delete particleStreams[device_id];
res.status(error.statusCode);
res.send(error.body.error);
res.end();
}
);
return;
}
function cleanFirebaseApp(req, res, next) {
var device_id = req.query.device_id;
if (firebaseApps[device_id]) {
firebaseApps[device_id].delete().then(function() {
delete firebaseApps[device_id];
next();
});
} else {
next();
}
}
function initFirebaseApp(req, res, next) {
var device_id = req.query.device_id;
if (!firebaseApps.hasOwnProperty(device_id)) {
firebaseApps[device_id] = firebase.initializeApp({
databaseURL: process.env.DATABASE,
serviceAccount: process.env.SERVICEACCOUNTFILE,
databaseAuthVariableOverride: {
uid : device_id
}
}, device_id);
}
next();
}
function checkFirebaseAccess(req, res, next) {
var device_id = req.query.device_id;
var fb = firebaseApps[device_id];
var path = stripDotJson(req.path);
var ref = fb.database().ref(path);
var promise = ref.once('value',
function(snapshot) {
res.snapshot = snapshot;
next();
},
function(error) {
res.status(403).send("Firebase permission denied: " + error.message);
res.end();
}
);
}
function dispatchEvent(req, snapshot) {
var payload = { };
payload[snapshot.key] = snapshot.val();
console.log("Publishing " + req.query.event_type + " for " + req.query.device_id);
particle.publishEvent({ name : req.query.event_type, data : JSON.stringify(payload), auth : req.query.particle_token, isPrivate : true });
}
function registerFirebaseEvent(req, res, next) {
var device_id = req.query.device_id;
var fb = firebaseApps[device_id];
var ref = fb.database().ref(stripDotJson(req.path));
console.log("Registering " + req.query.event_type + " for " + req.query.device_id);
console.log("query", req.query);
ref.on(req.query.event_type, function(snapshot) {
dispatchEvent(req, snapshot);
}, function(error) {
console.log(req.query.event_type + " event canceled for " + req.query.device_id, error);
particle.publishEvent({ name : "cancel", data : error, auth : req.query.particle_token, isPrivate : true });
});
res.status(200).send("OK");
}
function checkEventType(req, res, next) {
var types = ['value', 'child_added', 'child_changed', 'child_removed'];
if (types.indexOf(req.query.event_type) > -1) {
console.log('Event stream request for ' + req.query.device_id);
next();
} else {
console.log('REST request for ' + req.query.device_id);
next('route');
}
}
function put(req, res, next) {
res.ref.set(req.body, function(error) {
if (error) {
res.status(403).send("PUT Permission denied");
} else {
res.status(200).send("OK");
}
});
}
function patch(req, res, next) {
res.ref.update(req.body, function(error) {
if (error) {
res.status(403).send("PATCH Permission denied");
} else {
res.ref.once('value',
function(snapshot) {
res.status(200).send(snapshot.val());
},
function(error) {
res.status(204).send("PATCH successful, but could not retrieve values.");
}
);
}
});
}
function del(req, res, next) {
res.ref.remove().then(
function() {
res.status(200).send("OK");
}
).catch(
function(error) {
console.log(error);
res.status(403).send("DELETE Permission denied");
}
)
}
function post(req, res, next) {
try {
var newRef = res.ref.push(req.body);
newRef.then(
function() {
res.status(200).send(newRef.key);
}
).catch(
function(error) {
res.status(403).send("POST Permission denied");
}
)
} catch(error) {
res.status(400).send("POST data contains invalid key or value");
res.end();
}
}
function unsupported(req, res, next) {
res.status(405).send("Method not allowed");
}
function startQuery(req, res, next) {
res.ref = firebaseApps[req.query.device_id].database().ref(stripDotJson(req.path));
next();
}
function stripQuotes(word) {
if (word.charAt(0) == '"' && word.charAt(word.length - 1) == '"') {
return word.substring(1, word.length - 1);
} else {
return word;
}
}
function orderBy(req, res, next) {
if (req.query.orderBy) {
var order = stripQuotes(req.query.orderBy);
try {
switch (order) {
case "$key" : res.ref = res.ref.orderByKey(); break;
case "$value" : res.ref = res.ref.orderByValue(); break;
case "$priority" : res.ref = res.ref.orderByPriority(); break;
default : res.ref = res.ref.orderByChild(order); break;
}
} catch (error) {
res.status(400).send("Invalid orderBy value");
}
}
next();
}
function doGet(req, res, next) {
if (!res.ref) {
res.status(500).send("Something went really wrong.");
res.end();
} else {
res.ref.once('value',
function(snapshot) {
res.key = snapshot.key;
res.val = snapshot.val();
next();
},
function(error) {
res.status(403).send("GET Permission denied");
res.end();
}
);
}
}
function shallow(req, res, next) {
if (req.query.shallow == true || req.query.shallow == "true") {
for (var key in res.val) {
if (res.val[key] instanceof Object) {
res.val[key] = true;
}
}
}
next();
}
function returnGet(req, res, next) {
res.setHeader('Content-Type', 'application/json');
res.status(200);
res.send(res.val);
res.end();
}
function otherQueries(req, res, next) {
for (var key in req.query) {
var param = req.query[key];
if (!isNaN(parseFloat(param))) {
param = parseFloat(param);
} else {
param = stripQuotes(param);
}
try {
switch(key) {
case "startAt" : res.ref = res.ref.startAt(param); break;
case "endAt" : res.ref = res.ref.endAt(param); break;
case "equalTo" : res.ref = res.ref.equalTo(param); break;
case "limitToFirst" : res.ref = res.ref.limitToFirst(param); break;
case "limitToLast" : res.ref = res.ref.limitToLast(param); break;
}
} catch (error) {
console.log("otherQueries error", error);
res.status(400).send("Invalid parameter for query " + key);
res.end();
}
}
next();
}
app.use(verifyRequest);
app.use(verifyParticle);
// Trap for event stream requests and handle those first. If it's not an event stream request, then
// checkEventType will call a next('route') to bypass the rest of the middleware in this route
app.get('*', checkEventType, cleanFirebaseApp, initFirebaseApp, checkFirebaseAccess, registerFirebaseEvent);
app.get('*', initFirebaseApp, startQuery, orderBy, otherQueries, doGet, shallow, returnGet);
app.put('*', initFirebaseApp, startQuery, put);
app.patch('*', initFirebaseApp, startQuery, patch);
app.delete('*', initFirebaseApp, startQuery,del);
app.post('*', initFirebaseApp, startQuery, post);
var server = app.listen(process.env.PORT || 9000, function() {
console.log('Listening on port %d', server.address().port);
});
}
throng({
workers: WORKERS,
grace: 4000,
lifetime: Infinity,
start: start
});