diff --git a/cli.js b/cli.js index 652c301..a34e0ea 100755 --- a/cli.js +++ b/cli.js @@ -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) { diff --git a/package.json b/package.json index c6af555..7d2023e 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..1eeec54 --- /dev/null +++ b/test.sh @@ -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