Skip to content

Commit

Permalink
handle FeedMessage without FeetEntitys πŸ›πŸ’₯βœ…
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Mar 15, 2022
1 parent f715824 commit fb5bc0d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
10 changes: 8 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ const bindings = argv['gtfs-rt-bindings']
const {FeedMessage} = bindings.transit_realtime || bindings
const onFeedMessage = (buf) => {
const data = FeedMessage.toObject(FeedMessage.decode(buf))
if (!data || !data.header || !Array.isArray(data.entity)) {
throw new Error('invalid feed')
if (!data) throw new Error('invalid feed')
if (!data.header) throw new Error('invalid feed: missing header')

// Protocol buffers don't encode empty arrays, so .entity is missing with 0 FeedEntitys.
if (!('entity' in data)) {
data.entity = []
} else if (!Array.isArray(data.entity)) {
throw new Error('invalid feed: missing entity[]')
}

if (printAsJSON) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"mri": "^1.1.1"
},
"scripts": {
"test": "cat example-feed.pbf | ./cli.js --json | wc -l",
"test": "./test.sh",
"prepublishOnly": "npm test"
}
}
17 changes: 17 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -e
set -o pipefail

cd "$(dirname $0)"

set -x

cat example-feed.pbf | ./cli.js --json | wc -l

# FeedMessage with valid FeedHeader but 0 FeedEntitys
empty="$(echo '0a 0d 0a 03 32 2e 30 10 00 18 9d ce c4 91 06' | xxd -r -p | ./cli.js)"
if [ "$empty" != '' ]; then
1>&2 echo "FeedMessage with valid FeedHeader but 0 FeedEntitys: unexpected output \`$health_status\`"
exit 1
fi

0 comments on commit fb5bc0d

Please sign in to comment.