gtfs-realtime-bindings 0.0.6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Dockerfile +8 -0
- package/README.md +78 -57
- package/UPDATING.md +52 -43
- package/gtfs-realtime.js +6062 -4732
- package/package.json +38 -37
- package/test/test.js +37 -35
package/Dockerfile
ADDED
package/README.md
CHANGED
|
@@ -1,57 +1,78 @@
|
|
|
1
|
-
# JavaScript GTFS-realtime Language Bindings
|
|
2
|
-
|
|
3
|
-
[](http://badge.fury.io/js/gtfs-realtime-bindings)
|
|
4
|
-
|
|
5
|
-
Provides JavaScript classes generated from the
|
|
6
|
-
[GTFS-realtime](https://github.com/google/transit/tree/master/gtfs-realtime) Protocol
|
|
7
|
-
Buffer specification. These classes will allow you to parse a binary Protocol
|
|
8
|
-
Buffer GTFS-realtime data feed into JavaScript objects.
|
|
9
|
-
|
|
10
|
-
These bindings are designed to be used in the [Node.js](http://nodejs.org/)
|
|
11
|
-
environment, but with some effort, they can probably be used in other
|
|
12
|
-
JavaScript environments as well.
|
|
13
|
-
|
|
14
|
-
We use the [
|
|
15
|
-
JavaScript Protocol Buffer support.
|
|
16
|
-
|
|
17
|
-
## Add the Dependency
|
|
18
|
-
|
|
19
|
-
To use the `gtfs-realtime-bindings` classes in your own project, you need to
|
|
20
|
-
first install our [Node.js npm package](https://www.npmjs.com/package/gtfs-realtime-bindings):
|
|
21
|
-
|
|
22
|
-
```
|
|
23
|
-
npm install gtfs-realtime-bindings
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
## Example Code
|
|
27
|
-
|
|
28
|
-
The following Node.js code snippet demonstrates downloading a GTFS-realtime
|
|
29
|
-
data feed from a particular URL, parsing it as a FeedMessage (the root type of
|
|
30
|
-
the GTFS-realtime schema), and iterating over the results.
|
|
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
|
-
|
|
1
|
+
# JavaScript GTFS-realtime Language Bindings
|
|
2
|
+
|
|
3
|
+
[](http://badge.fury.io/js/gtfs-realtime-bindings)
|
|
4
|
+
|
|
5
|
+
Provides JavaScript classes generated from the
|
|
6
|
+
[GTFS-realtime](https://github.com/google/transit/tree/master/gtfs-realtime) Protocol
|
|
7
|
+
Buffer specification. These classes will allow you to parse a binary Protocol
|
|
8
|
+
Buffer GTFS-realtime data feed into JavaScript objects.
|
|
9
|
+
|
|
10
|
+
These bindings are designed to be used in the [Node.js](http://nodejs.org/)
|
|
11
|
+
environment, but with some effort, they can probably be used in other
|
|
12
|
+
JavaScript environments as well.
|
|
13
|
+
|
|
14
|
+
We use the [ProtoBuf.js](https://github.com/dcodeIO/ProtoBuf.js) library for
|
|
15
|
+
JavaScript Protocol Buffer support.
|
|
16
|
+
|
|
17
|
+
## Add the Dependency
|
|
18
|
+
|
|
19
|
+
To use the `gtfs-realtime-bindings` classes in your own project, you need to
|
|
20
|
+
first install our [Node.js npm package](https://www.npmjs.com/package/gtfs-realtime-bindings):
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
npm install gtfs-realtime-bindings
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Example Code
|
|
27
|
+
|
|
28
|
+
The following Node.js code snippet demonstrates downloading a GTFS-realtime
|
|
29
|
+
data feed from a particular URL, parsing it as a FeedMessage (the root type of
|
|
30
|
+
the GTFS-realtime schema), and iterating over the results.
|
|
31
|
+
|
|
32
|
+
In order to make this example work, you must first install `node-fetch` with NPM.
|
|
33
|
+
|
|
34
|
+
_Note: this exemple is using ES modules (`import`/`export` syntax) and is not compatible
|
|
35
|
+
with CommonJS (`require` syntax). You can use CommonJS by converting `import` to `require`
|
|
36
|
+
and installing `node-fetch@2`. Learn more about ES modules [here](https://nodejs.org/api/esm.html)._
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
import GtfsRealtimeBindings from "gtfs-realtime-bindings";
|
|
40
|
+
import fetch from "node-fetch";
|
|
41
|
+
|
|
42
|
+
(async () => {
|
|
43
|
+
try {
|
|
44
|
+
const response = await fetch("<GTFS-realtime source URL>", {
|
|
45
|
+
headers: {
|
|
46
|
+
"x-api-key": "<redacted>",
|
|
47
|
+
// replace with your GTFS-realtime source's auth token
|
|
48
|
+
// e.g. x-api-key is the header value used for NY's MTA GTFS APIs
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
if (!res.ok) {
|
|
52
|
+
const error = new Error(`${res.url}: ${res.status} ${res.statusText}`);
|
|
53
|
+
error.response = res;
|
|
54
|
+
throw error;
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
const buffer = await response.arrayBuffer();
|
|
58
|
+
const feed = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(
|
|
59
|
+
new Uint8Array(buffer)
|
|
60
|
+
);
|
|
61
|
+
feed.entity.forEach((entity) => {
|
|
62
|
+
if (entity.tripUpdate) {
|
|
63
|
+
console.log(entity.tripUpdate);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
console.log(error);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
})();
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
For more details on the naming conventions for the JavaScript classes generated
|
|
75
|
+
from the
|
|
76
|
+
[gtfs-realtime.proto](https://github.com/google/transit/blob/master/gtfs-realtime/proto/gtfs-realtime.proto),
|
|
77
|
+
check out the [ProtoBuf.js project](https://github.com/dcodeIO/ProtoBuf.js/wiki)
|
|
78
|
+
which we use to handle our Protocol Buffer serialization.
|
package/UPDATING.md
CHANGED
|
@@ -1,43 +1,52 @@
|
|
|
1
|
-
# How-To Update Bindings When gtfs-realtime.proto Changes
|
|
2
|
-
|
|
3
|
-
## Regenerate the language binding source from gtfs-realtime.proto.
|
|
4
|
-
|
|
5
|
-
#### One-Time Setup
|
|
6
|
-
|
|
7
|
-
1. Download and install [
|
|
8
|
-
|
|
9
|
-
#### Every time
|
|
10
|
-
|
|
11
|
-
1.
|
|
12
|
-
|
|
13
|
-
1.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
1.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
1
|
+
# How-To Update Bindings When gtfs-realtime.proto Changes
|
|
2
|
+
|
|
3
|
+
## Regenerate the language binding source from gtfs-realtime.proto.
|
|
4
|
+
|
|
5
|
+
#### One-Time Setup
|
|
6
|
+
|
|
7
|
+
1. Download and install [Docker](https://docs.docker.com/get-docker/)
|
|
8
|
+
|
|
9
|
+
#### Every time node package dependencies change
|
|
10
|
+
|
|
11
|
+
1. Download and install [Node.js](https://www.npmjs.com/get-npm) (check the `package.json` for `engines` version).
|
|
12
|
+
|
|
13
|
+
1. Edit the dependency versions in using `npm install` and/or `npm remove`.
|
|
14
|
+
|
|
15
|
+
1. Re-generate the code by following instructions below.
|
|
16
|
+
|
|
17
|
+
#### Re-generating the code
|
|
18
|
+
|
|
19
|
+
1. Run the following from the project root folder:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
docker build -t gtfs-nodejs -f nodejs/Dockerfile .
|
|
23
|
+
# -it to make sure docker run can be killed with ctrl-c
|
|
24
|
+
# -t uses TTY, which causes linux to include carriage returns, which are stripped using tr
|
|
25
|
+
docker run -it --rm gtfs-nodejs cat /lib/gtfs-realtime.js | tr -d '\r' > nodejs/gtfs-realtime.js
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
1. Add the license header back to the generated source file.
|
|
29
|
+
|
|
30
|
+
1. Test the generated code:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
npm run test
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
1. Update the version number in `package.json`.
|
|
37
|
+
|
|
38
|
+
## Publishing a new release
|
|
39
|
+
|
|
40
|
+
#### Every release
|
|
41
|
+
|
|
42
|
+
Log in with your account on NPM that has permissions for https://www.npmjs.com/package/gtfs-realtime-bindings:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
npm login
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Publish the package to NPM:
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
npm publish
|
|
52
|
+
```
|