@nyc-transit-kit/mta 0.1.0 → 0.1.1
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/package.json +3 -3
- package/src/gtfs-realtime.ts +37 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nyc-transit-kit/mta",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"exports": {
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"test": "bun test ./test"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@nyc-transit-kit/contracts": "0.1.
|
|
27
|
-
"@nyc-transit-kit/soda3": "0.1.
|
|
26
|
+
"@nyc-transit-kit/contracts": "0.1.1",
|
|
27
|
+
"@nyc-transit-kit/soda3": "0.1.1",
|
|
28
28
|
"effect": "4.0.0-beta.83",
|
|
29
29
|
"gtfs-realtime-bindings": "2.0.0"
|
|
30
30
|
},
|
package/src/gtfs-realtime.ts
CHANGED
|
@@ -22,7 +22,7 @@ import { MtaDecodeError, MtaHttpError, MtaInvalidInputError } from "./errors"
|
|
|
22
22
|
export type GtfsRealtimeDecoderImplementation = (
|
|
23
23
|
bytes: Uint8Array,
|
|
24
24
|
feed: GtfsFeedKind
|
|
25
|
-
) => Effect.Effect<
|
|
25
|
+
) => Effect.Effect<MtaGtfsRealtimeDecodedSummary, MtaDecodeError>
|
|
26
26
|
export type { MtaGtfsRealtimeCaptureRequestInput, MtaGtfsRealtimeProbeRequestInput }
|
|
27
27
|
|
|
28
28
|
const FeedMessage = GtfsRealtimeBindings.transit_realtime.FeedMessage
|
|
@@ -133,10 +133,18 @@ export class GtfsRealtimeDecoder extends Context.Service<
|
|
|
133
133
|
// Test/custom fallback for callers that want to bypass protobuf decoding.
|
|
134
134
|
static readonly Passthrough = Layer.succeed(GtfsRealtimeDecoder)({
|
|
135
135
|
decode: (bytes, feed) =>
|
|
136
|
-
Effect.succeed(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
Effect.succeed(
|
|
137
|
+
MtaGtfsRealtimeDecodedSummary.make({
|
|
138
|
+
feed,
|
|
139
|
+
entityCount: 0,
|
|
140
|
+
tripUpdateCount: 0,
|
|
141
|
+
vehiclePositionCount: 0,
|
|
142
|
+
alertCount: 0,
|
|
143
|
+
raw: {
|
|
144
|
+
byteLength: bytes.byteLength
|
|
145
|
+
}
|
|
146
|
+
})
|
|
147
|
+
)
|
|
140
148
|
})
|
|
141
149
|
}
|
|
142
150
|
|
|
@@ -158,6 +166,7 @@ const decodeRealtimeProbeRequest = decodeRealtimeInput(MtaGtfsRealtimeProbeReque
|
|
|
158
166
|
const decodeRealtimeCaptureRequest = decodeRealtimeInput(MtaGtfsRealtimeCaptureRequest)
|
|
159
167
|
|
|
160
168
|
const httpStatusText = (status: number) => `HTTP ${status}`
|
|
169
|
+
const isOkStatus = (status: number) => status >= 200 && status < 300
|
|
161
170
|
|
|
162
171
|
const mapMtaHttpClientError = (operation: string, error: HttpClientError.HttpClientError) => {
|
|
163
172
|
const response = error.response
|
|
@@ -177,6 +186,13 @@ const mapMtaHttpClientError = (operation: string, error: HttpClientError.HttpCli
|
|
|
177
186
|
})
|
|
178
187
|
}
|
|
179
188
|
|
|
189
|
+
const failGtfsRealtimeStatus = (status: number) =>
|
|
190
|
+
MtaHttpError.make({
|
|
191
|
+
operation: "gtfs-realtime",
|
|
192
|
+
status,
|
|
193
|
+
statusText: httpStatusText(status)
|
|
194
|
+
})
|
|
195
|
+
|
|
180
196
|
const fetchGtfsRealtimeBytes = Effect.fn("Mta.fetchGtfsRealtimeBytes")(function* (request: {
|
|
181
197
|
readonly feed: GtfsFeedKind
|
|
182
198
|
readonly url: string
|
|
@@ -185,14 +201,6 @@ const fetchGtfsRealtimeBytes = Effect.fn("Mta.fetchGtfsRealtimeBytes")(function*
|
|
|
185
201
|
Effect.mapError((error) => mapMtaHttpClientError("gtfs-realtime", error))
|
|
186
202
|
)
|
|
187
203
|
|
|
188
|
-
if (response.status < 200 || response.status >= 300) {
|
|
189
|
-
return yield* MtaHttpError.make({
|
|
190
|
-
operation: "gtfs-realtime",
|
|
191
|
-
status: response.status,
|
|
192
|
-
statusText: httpStatusText(response.status)
|
|
193
|
-
})
|
|
194
|
-
}
|
|
195
|
-
|
|
196
204
|
const buffer = yield* response.arrayBuffer.pipe(
|
|
197
205
|
Effect.mapError(() =>
|
|
198
206
|
MtaHttpError.make({
|
|
@@ -229,11 +237,21 @@ export const probeGtfsRealtime = Effect.fn("Mta.probeGtfsRealtime")(function* (
|
|
|
229
237
|
const request = yield* decodeRealtimeProbeRequest(input)
|
|
230
238
|
const decoder = yield* GtfsRealtimeDecoder
|
|
231
239
|
const { response, bytes } = yield* fetchGtfsRealtimeBytes(request)
|
|
240
|
+
|
|
241
|
+
if (!isOkStatus(response.status)) {
|
|
242
|
+
return MtaGtfsRealtimeProbeResult.make({
|
|
243
|
+
feed: request.feed,
|
|
244
|
+
ok: false,
|
|
245
|
+
status: response.status,
|
|
246
|
+
byteLength: bytes.byteLength
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
|
|
232
250
|
const decoded = yield* decoder.decode(bytes, request.feed)
|
|
233
251
|
|
|
234
252
|
return MtaGtfsRealtimeProbeResult.make({
|
|
235
253
|
feed: request.feed,
|
|
236
|
-
ok:
|
|
254
|
+
ok: true,
|
|
237
255
|
status: response.status,
|
|
238
256
|
byteLength: bytes.byteLength,
|
|
239
257
|
decoded
|
|
@@ -245,6 +263,11 @@ export const captureGtfsRealtime = Effect.fn("Mta.captureGtfsRealtime")(function
|
|
|
245
263
|
) {
|
|
246
264
|
const request = yield* decodeRealtimeCaptureRequest(input)
|
|
247
265
|
const { response, bytes } = yield* fetchGtfsRealtimeBytes(request)
|
|
266
|
+
|
|
267
|
+
if (!isOkStatus(response.status)) {
|
|
268
|
+
return yield* failGtfsRealtimeStatus(response.status)
|
|
269
|
+
}
|
|
270
|
+
|
|
248
271
|
const sha256 = yield* sha256Hex(bytes, request.feed)
|
|
249
272
|
|
|
250
273
|
return MtaGtfsRealtimeCaptureResult.make({
|