@tmlmobilidade/gtfs-rt 20260216.1651.10

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.
@@ -0,0 +1,7 @@
1
+ import { type GtfsRtFeedMessage } from '@tmlmobilidade/types';
2
+ /**
3
+ * Decodes a GTFS-RT feed from an ArrayBuffer or Buffer.
4
+ * @param buffer The GTFS-RT feed as an ArrayBuffer or Buffer.
5
+ * @returns The decoded GTFS-RT feed as a JavaScript object.
6
+ */
7
+ export declare function decodeGtfsRtFeed(buffer: ArrayBuffer | Buffer): Promise<GtfsRtFeedMessage>;
package/dist/decode.js ADDED
@@ -0,0 +1,26 @@
1
+ /* * */
2
+ import { initProto } from './init-proto.js';
3
+ /**
4
+ * Decodes a GTFS-RT feed from an ArrayBuffer or Buffer.
5
+ * @param buffer The GTFS-RT feed as an ArrayBuffer or Buffer.
6
+ * @returns The decoded GTFS-RT feed as a JavaScript object.
7
+ */
8
+ export async function decodeGtfsRtFeed(buffer) {
9
+ //
10
+ //
11
+ // Initialize the protobufjs root
12
+ // with the GTFS-RT .proto file
13
+ const protoRoot = await initProto();
14
+ //
15
+ // Check if the buffer is an ArrayBuffer
16
+ // and convert it to a Node.js Buffer if necessary
17
+ if (buffer instanceof ArrayBuffer) {
18
+ buffer = Buffer.from(buffer);
19
+ }
20
+ //
21
+ // Decode the GTFS-RT feed using the protobufjs root
22
+ const feedMessageType = protoRoot.lookupType('transit_realtime.FeedMessage');
23
+ const message = feedMessageType.decode(buffer);
24
+ return feedMessageType.toObject(message);
25
+ //
26
+ }
@@ -0,0 +1,7 @@
1
+ import { type GtfsRtFeedMessage } from '@tmlmobilidade/types';
2
+ /**
3
+ * Encodes a GTFS-RT feed into an ArrayBuffer.
4
+ * @param feedMessage The GTFS-RT feed as a JavaScript object.
5
+ * @returns The encoded GTFS-RT feed as an ArrayBuffer.
6
+ */
7
+ export declare function encodeGtfsRtFeed(feedMessage: GtfsRtFeedMessage): Promise<Uint8Array<ArrayBufferLike>>;
package/dist/encode.js ADDED
@@ -0,0 +1,20 @@
1
+ /* * */
2
+ import { initProto } from './init-proto.js';
3
+ /**
4
+ * Encodes a GTFS-RT feed into an ArrayBuffer.
5
+ * @param feedMessage The GTFS-RT feed as a JavaScript object.
6
+ * @returns The encoded GTFS-RT feed as an ArrayBuffer.
7
+ */
8
+ export async function encodeGtfsRtFeed(feedMessage) {
9
+ //
10
+ //
11
+ // Initialize the protobufjs root
12
+ // with the GTFS-RT .proto file
13
+ const protoRoot = await initProto();
14
+ //
15
+ // Encode the GTFS-RT feed using the protobufjs root
16
+ const feedMessageType = protoRoot.lookupType('transit_realtime.FeedMessage');
17
+ const message = feedMessageType.fromObject(feedMessage);
18
+ return feedMessageType.encode(message).finish();
19
+ //
20
+ }
@@ -0,0 +1,2 @@
1
+ export * from './decode.js';
2
+ export * from './encode.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './decode.js';
2
+ export * from './encode.js';
@@ -0,0 +1,7 @@
1
+ import protobufjs from 'protobufjs';
2
+ /**
3
+ * Initializes the protobufjs Root by loading
4
+ * the GTFS Realtime .proto definition file.
5
+ * @returns The initialized protobufjs Root.
6
+ */
7
+ export declare function initProto(): Promise<protobufjs.Root>;
@@ -0,0 +1,45 @@
1
+ /* * */
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import protobufjs from 'protobufjs';
5
+ /* * */
6
+ const PROTO_SPEC_FILE_URL = 'https://gtfs.org/documentation/realtime/gtfs-realtime.proto';
7
+ const PROTO_SPEC_FILE_PATH = '/tmp/go-gtfs-rt/gtfs-realtime.proto';
8
+ /* * */
9
+ let PROTO_ROOT_INSTANCE = null;
10
+ /**
11
+ * Initializes the protobufjs Root by loading
12
+ * the GTFS Realtime .proto definition file.
13
+ * @returns The initialized protobufjs Root.
14
+ */
15
+ export async function initProto() {
16
+ //
17
+ //
18
+ // If the protobufjs Root has already been initialized,
19
+ // return the existing instance to avoid redundant work.
20
+ if (PROTO_ROOT_INSTANCE)
21
+ return PROTO_ROOT_INSTANCE;
22
+ //
23
+ // Check if the .proto file already exists locally.
24
+ // If not, download a fresh copy from the GTFS Realtime specification.
25
+ if (!fs.existsSync(PROTO_SPEC_FILE_PATH)) {
26
+ // Download the .proto definition file
27
+ const response = await fetch(PROTO_SPEC_FILE_URL);
28
+ const protoText = await response.text();
29
+ // Get the directory of the path where the .proto file will be saved
30
+ const dirPath = path.dirname(PROTO_SPEC_FILE_PATH);
31
+ // Create the directory if it doesn't exist
32
+ if (!fs.existsSync(dirPath))
33
+ fs.mkdirSync(dirPath, { recursive: true });
34
+ // Save the .proto file locally so it can be loaded by protobufjs
35
+ fs.writeFileSync(PROTO_SPEC_FILE_PATH, protoText);
36
+ }
37
+ //
38
+ // Load the .proto file using protobufjs,
39
+ // save the initialized Root instance
40
+ // in a global variable and return it.
41
+ const protoRoot = new protobufjs.Root();
42
+ PROTO_ROOT_INSTANCE = protoRoot.loadSync(PROTO_SPEC_FILE_PATH, { keepCase: true });
43
+ return PROTO_ROOT_INSTANCE;
44
+ //
45
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@tmlmobilidade/gtfs-rt",
3
+ "version": "20260216.1651.10",
4
+ "author": {
5
+ "email": "iso@tmlmobilidade.pt",
6
+ "name": "TML-ISO"
7
+ },
8
+ "license": "AGPL-3.0-or-later",
9
+ "homepage": "https://github.com/tmlmobilidade/go#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/tmlmobilidade/go/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/tmlmobilidade/go.git"
16
+ },
17
+ "keywords": [
18
+ "public transit",
19
+ "tml",
20
+ "transportes metropolitanos de lisboa",
21
+ "go"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "type": "module",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "scripts": {
33
+ "build": "tsc && resolve-tspaths",
34
+ "lint": "eslint ./src/ && tsc --noEmit",
35
+ "lint:fix": "eslint ./src/ --fix",
36
+ "watch": "tsc-watch --onSuccess 'resolve-tspaths'"
37
+ },
38
+ "dependencies": {
39
+ "protobufjs": "8.0.0"
40
+ },
41
+ "devDependencies": {
42
+ "@tmlmobilidade/tsconfig": "*",
43
+ "@tmlmobilidade/types": "*",
44
+ "@types/node": "25.2.2",
45
+ "resolve-tspaths": "0.8.23",
46
+ "tsc-watch": "7.2.0",
47
+ "typescript": "5.9.3"
48
+ }
49
+ }