gtfs-realtime-bindings 0.0.6 → 1.1.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 ADDED
@@ -0,0 +1,8 @@
1
+ FROM node:18.12.1-alpine
2
+
3
+ WORKDIR /lib
4
+ COPY nodejs/package.json /lib/package.json
5
+ COPY nodejs/package-lock.json /lib/package-lock.json
6
+ COPY gtfs-realtime.proto /gtfs-realtime.proto
7
+ RUN npm ci
8
+ RUN npm run buildProto
package/README.md CHANGED
@@ -1,57 +1,78 @@
1
- # JavaScript GTFS-realtime Language Bindings
2
-
3
- [![npm version](https://badge.fury.io/js/gtfs-realtime-bindings.svg)](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 [ProtBuf.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
- ```javascript
33
- var GtfsRealtimeBindings = require('gtfs-realtime-bindings');
34
- var request = require('request');
35
-
36
- var requestSettings = {
37
- method: 'GET',
38
- url: 'URL OF YOUR GTFS-REALTIME SOURCE GOES HERE',
39
- encoding: null
40
- };
41
- request(requestSettings, function (error, response, body) {
42
- if (!error && response.statusCode == 200) {
43
- var feed = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(body);
44
- feed.entity.forEach(function(entity) {
45
- if (entity.trip_update) {
46
- console.log(entity.trip_update);
47
- }
48
- });
49
- }
50
- });
51
- ```
52
-
53
- For more details on the naming conventions for the Javascript classes generated
54
- from the
55
- [gtfs-realtime.proto](https://github.com/google/transit/blob/master/gtfs-realtime/proto/gtfs-realtime.proto),
56
- check out the [ProtoBuf.js project](https://github.com/dcodeIO/ProtoBuf.js/wiki)
57
- which we use to handle our Protocol Buffer serialization.
1
+ # JavaScript GTFS-realtime Language Bindings
2
+
3
+ [![npm version](https://badge.fury.io/js/gtfs-realtime-bindings.svg)](http://badge.fury.io/js/gtfs-realtime-bindings)
4
+
5
+ Provides JavaScript classes and their associated types 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,53 @@
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 [Node.js](https://www.npmjs.com/get-npm) (v10.15.2 LTS has been used)
8
-
9
- #### Every time `gtfs-realtime.proto` changes
10
-
11
- 1. Run `npm install` from the `nodejs` folder to install the protobuf package
12
-
13
- 1. Regenerate the language binding source from gtfs-realtime.proto by running the following from the `nodejs` folder:
14
-
15
- ```
16
- npm run buildProto
17
- ```
18
-
19
- 1. Add the license header back to the generated source file.
20
-
21
- 1. Test the generated code:
22
-
23
- ```
24
- npm run test
25
- ```
26
-
27
- 1. Update the version number in `package.json`.
28
-
29
- ## Publishing a new release
30
-
31
- #### Every release
32
-
33
- Log in with your account on NPM that has permissions for https://www.npmjs.com/package/gtfs-realtime-bindings:
34
-
35
- ```
36
- npm login
37
- ```
38
-
39
- Publish the package to NPM:
40
-
41
- ```
42
- npm publish
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
+ docker run -it --rm gtfs-nodejs cat /lib/gtfs-realtime.d.ts | tr -d '\r' > nodejs/gtfs-realtime.d.ts
27
+ ```
28
+
29
+ 1. Add the license header back to the generated source file.
30
+
31
+ 1. Test the generated code:
32
+
33
+ ```
34
+ npm run test
35
+ ```
36
+
37
+ 1. Update the version number in `package.json`.
38
+
39
+ ## Publishing a new release
40
+
41
+ #### Every release
42
+
43
+ Log in with your account on NPM that has permissions for https://www.npmjs.com/package/gtfs-realtime-bindings:
44
+
45
+ ```
46
+ npm login
47
+ ```
48
+
49
+ Publish the package to NPM:
50
+
51
+ ```
52
+ npm publish
53
+ ```