-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
54 lines (35 loc) · 1.26 KB
/
handler.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
'use strict';
const request = require('request');
const protobuf = require('protobufjs');
module.exports.gtfs = (event, context, callback) => {
protobuf.load('gtfs-realtime.proto')
.then(function(root) {
let message = root.lookupType("transit_realtime.FeedMessage");
return new Promise((resolve, reject) => {
var requestSettings = {
method: 'GET',
url: 'https://cdn.mbta.com/realtime/VehiclePositions.pb',
encoding: null
};
request(requestSettings, function (err, response, body) {
let gtfs_realtime = message.decode(body)
if (err) {
console.log(`Got error`, err);
callback(err);
reject(err);
return;
}
const output = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true,
"content-type": 'application/json'
},
body: JSON.stringify(gtfs_realtime.entity)
};
callback(null, output);
});
});
});
}