mta-js 1.0.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/index.ts ADDED
@@ -0,0 +1,585 @@
1
+ import { defaultEndpoints, subwayRouteColors } from "./src/defaults";
2
+ import {
3
+ hydrateRemoteDatabaseUrl,
4
+ importRemoteStaticSeed,
5
+ isRemoteDatabaseUrl,
6
+ pushRemoteDatabaseSchema,
7
+ resolveSqliteDatabaseUrl,
8
+ } from "./src/database-url";
9
+ import { MissingBusTimeKeyError, StaticDataMissingError, UnknownRouteError, UnknownStopError } from "./src/errors";
10
+ import { decodeFeedMessage, type GtfsRealtimeFeed, type TranslatedString } from "./src/gtfs-realtime";
11
+ import { fetchArrayBuffer, fetchJson, urlWithParams } from "./src/http";
12
+ import { directionFromStopId, GTFSCache, parseGtfsZip } from "./src/static-gtfs";
13
+ import type {
14
+ Alert,
15
+ AlertQuery,
16
+ Arrival,
17
+ BusArrivalQuery,
18
+ BusVehicleQuery,
19
+ DatabaseStatus,
20
+ Direction,
21
+ GtfsImportSummary,
22
+ MTAEndpoints,
23
+ MTAOptions,
24
+ Route,
25
+ Stop,
26
+ StopsNearQuery,
27
+ StaticGtfsSeed,
28
+ StaticGtfsImportLimits,
29
+ StaticGtfsImportStrategy,
30
+ TransitMode,
31
+ Vehicle,
32
+ } from "./src/types";
33
+
34
+ export class MTA {
35
+ static: GTFSCache;
36
+ readonly database: DatabaseClient;
37
+ readonly subway: SubwayClient;
38
+ readonly bus: BusClient;
39
+ readonly alerts: AlertsClient;
40
+ readonly stops: StopsClient;
41
+
42
+ readonly fetch: typeof fetch;
43
+ readonly now: () => Date;
44
+ readonly busTimeKey?: string;
45
+ readonly endpoints: MTAEndpoints;
46
+ readonly options: MTAOptions;
47
+ private readonly readyPromise: Promise<void>;
48
+ private readonly realtimeCache = new Map<string, { expiresAt: number; feed: GtfsRealtimeFeed }>();
49
+ private readonly realtimeCacheTtlMs: number;
50
+
51
+ constructor(options: MTAOptions = {}) {
52
+ this.options = options;
53
+ this.fetch = options.fetch ?? fetch;
54
+ this.now = options.now ?? (() => new Date());
55
+ this.busTimeKey = options.busTimeKey;
56
+ this.realtimeCacheTtlMs = options.realtimeCacheTtlMs ?? 15_000;
57
+ this.endpoints = {
58
+ ...defaultEndpoints,
59
+ ...options.endpoints,
60
+ subwayFeeds: {
61
+ ...defaultEndpoints.subwayFeeds,
62
+ ...options.endpoints?.subwayFeeds,
63
+ },
64
+ };
65
+ this.static = new GTFSCache(resolveSqliteDatabaseUrl(options.databaseUrl));
66
+ this.readyPromise = this.initializeDatabase(options);
67
+
68
+ this.database = new DatabaseClient(this);
69
+ this.subway = new SubwayClient(this);
70
+ this.bus = new BusClient(this);
71
+ this.alerts = new AlertsClient(this);
72
+ this.stops = new StopsClient(this);
73
+ }
74
+
75
+ async ready() {
76
+ await this.readyPromise;
77
+ return this;
78
+ }
79
+
80
+ close() {
81
+ this.static.close();
82
+ }
83
+
84
+ private async initializeDatabase(options: MTAOptions) {
85
+ if (options.databaseUrl && isRemoteDatabaseUrl(options.databaseUrl)) {
86
+ await this.rehydrateRemoteDatabase();
87
+ }
88
+
89
+ if (options.staticData) this.static.importSeed(options.staticData);
90
+ }
91
+
92
+ async rehydrateRemoteDatabase() {
93
+ const localDatabaseUrl = await retryOnWalConflict(() =>
94
+ hydrateRemoteDatabaseUrl({
95
+ databaseUrl: this.options.databaseUrl,
96
+ databaseAuthToken: this.options.databaseAuthToken,
97
+ databaseLocalPath: this.options.databaseLocalPath,
98
+ fetch: this.fetch,
99
+ refresh: true,
100
+ }),
101
+ );
102
+ this.static.close();
103
+ this.static = new GTFSCache(localDatabaseUrl, { createSchema: false });
104
+ }
105
+
106
+ async realtimeFeed(url: string) {
107
+ const now = this.now().getTime();
108
+ const cached = this.realtimeCache.get(url);
109
+ if (cached && cached.expiresAt > now) return cached.feed;
110
+
111
+ const feed = decodeFeedMessage(await fetchArrayBuffer(this.fetch, url));
112
+ if (this.realtimeCacheTtlMs > 0) {
113
+ this.realtimeCache.set(url, { feed, expiresAt: now + this.realtimeCacheTtlMs });
114
+ }
115
+ return feed;
116
+ }
117
+ }
118
+
119
+ class DatabaseClient {
120
+ constructor(private readonly mta: MTA) {}
121
+
122
+ async push() {
123
+ await this.mta.ready();
124
+ this.mta.static.pushSchema();
125
+ const result = await pushRemoteDatabaseSchema({
126
+ databaseUrl: this.mta.options.databaseUrl,
127
+ databaseAuthToken: this.mta.options.databaseAuthToken,
128
+ });
129
+ return result;
130
+ }
131
+
132
+ async hasStaticData(mode: TransitMode) {
133
+ await this.mta.ready();
134
+ return this.mta.static.hasStaticData(mode);
135
+ }
136
+
137
+ async status(): Promise<DatabaseStatus> {
138
+ await this.mta.ready();
139
+ return this.mta.static.status();
140
+ }
141
+
142
+ async importStaticData(input: {
143
+ mode: TransitMode;
144
+ seed?: StaticGtfsSeed;
145
+ sourceUrl?: string;
146
+ strategy?: StaticGtfsImportStrategy;
147
+ limits?: StaticGtfsImportLimits;
148
+ rehydrate?: boolean;
149
+ }): Promise<GtfsImportSummary | undefined> {
150
+ await this.mta.ready();
151
+ const parsedSeed =
152
+ input.seed ??
153
+ (input.sourceUrl
154
+ ? parseGtfsZip(await readSourceArrayBuffer(this.mta.fetch, input.sourceUrl))
155
+ : undefined);
156
+ const seed = parsedSeed
157
+ ? applyImportStrategy(applyImportLimits(parsedSeed, input.limits), input.strategy ?? "core")
158
+ : undefined;
159
+ if (!seed) {
160
+ throw new Error("importStaticData requires either seed or sourceUrl.");
161
+ }
162
+ const result = await importRemoteStaticSeed({
163
+ databaseUrl: this.mta.options.databaseUrl,
164
+ databaseAuthToken: this.mta.options.databaseAuthToken,
165
+ mode: input.mode,
166
+ seed,
167
+ sourceUrl: input.sourceUrl,
168
+ });
169
+
170
+ if (result.remote && input.rehydrate !== false) {
171
+ await this.mta.rehydrateRemoteDatabase();
172
+ } else {
173
+ this.mta.static.importSeed(seed, input.mode);
174
+ if (input.sourceUrl) {
175
+ this.mta.static.db
176
+ .query("update gtfs_imports set source_url = ?1 where mode = ?2")
177
+ .run(input.sourceUrl, input.mode);
178
+ }
179
+ }
180
+
181
+ return this.mta.static.importSummary(input.mode);
182
+ }
183
+
184
+ async ensureStaticData(input: {
185
+ mode: TransitMode;
186
+ seed?: StaticGtfsSeed;
187
+ sourceUrl?: string;
188
+ strategy?: StaticGtfsImportStrategy;
189
+ limits?: StaticGtfsImportLimits;
190
+ }): Promise<GtfsImportSummary | undefined> {
191
+ await this.mta.ready();
192
+ if (this.mta.static.hasStaticData(input.mode)) {
193
+ return this.mta.static.importSummary(input.mode);
194
+ }
195
+ return this.importStaticData(input);
196
+ }
197
+ }
198
+
199
+ class SubwayClient {
200
+ constructor(private readonly mta: MTA) {}
201
+
202
+ async arrivals(query: {
203
+ stopId: string;
204
+ route?: string;
205
+ direction?: Direction | "uptown" | "downtown";
206
+ limit?: number;
207
+ includeRaw?: boolean;
208
+ }): Promise<Arrival[]> {
209
+ await this.mta.ready();
210
+ const routeIds = query.route ? [normalizeRouteId(query.route)] : Object.keys(this.mta.endpoints.subwayFeeds);
211
+ const feeds = [...new Set(routeIds.map((route) => this.feedForRoute(route)))];
212
+ const stopIds = this.mta.static.getStopIdsForQuery(query.stopId);
213
+ if (this.mta.static.hasStaticData("subway") && !this.mta.static.getStopOrParent(query.stopId)) {
214
+ throw new UnknownStopError(query.stopId);
215
+ }
216
+ const arrivals: Arrival[] = [];
217
+
218
+ for (const feedUrl of feeds) {
219
+ const feed = await this.mta.realtimeFeed(feedUrl);
220
+ arrivals.push(...this.arrivalsFromFeed(feed, stopIds, query));
221
+ }
222
+
223
+ return arrivals
224
+ .sort((a, b) => Date.parse(a.arrivalTime) - Date.parse(b.arrivalTime))
225
+ .slice(0, query.limit ?? 20);
226
+ }
227
+
228
+ private feedForRoute(route: string) {
229
+ const feed = this.mta.endpoints.subwayFeeds[route];
230
+ if (!feed) throw new UnknownRouteError(route);
231
+ return feed;
232
+ }
233
+
234
+ private arrivalsFromFeed(
235
+ feed: GtfsRealtimeFeed,
236
+ stopIds: Set<string>,
237
+ query: { stopId: string; route?: string; direction?: Direction | "uptown" | "downtown"; includeRaw?: boolean },
238
+ ) {
239
+ const arrivals: Arrival[] = [];
240
+ const wantedDirection = normalizeDirection(query.direction);
241
+ const now = this.mta.now().getTime();
242
+
243
+ for (const entity of feed.entity) {
244
+ const tripUpdate = entity.tripUpdate;
245
+ if (!tripUpdate) continue;
246
+
247
+ const trip = tripUpdate.trip;
248
+ const routeId = normalizeRouteId(trip?.routeId ?? query.route ?? "");
249
+ if (query.route && routeId !== normalizeRouteId(query.route)) continue;
250
+
251
+ const staticTrip = trip?.tripId ? this.mta.static.getTrip(trip.tripId) : undefined;
252
+ const route = routeWithFallback(this.mta.static.getRoute(routeId), routeId);
253
+
254
+ for (const update of tripUpdate.stopTimeUpdate ?? []) {
255
+ const stopId = update.stopId;
256
+ if (!stopId || !stopIds.has(stopId)) continue;
257
+ if (update.scheduleRelationship === "SKIPPED" || update.scheduleRelationship === "NO_DATA") continue;
258
+
259
+ const direction = directionFromStopId(stopId);
260
+ if (wantedDirection && direction !== wantedDirection) continue;
261
+
262
+ const event = update.arrival ?? update.departure;
263
+ if (!event?.time) continue;
264
+
265
+ const stop = this.mta.static.getStopOrParent(stopId) ?? fallbackStop(query.stopId);
266
+ arrivals.push({
267
+ mode: "subway",
268
+ route,
269
+ stop,
270
+ direction,
271
+ headsign: staticTrip?.headsign ?? undefined,
272
+ arrivalTime: new Date(event.time * 1000).toISOString(),
273
+ departureTime: update.departure?.time ? new Date(update.departure.time * 1000).toISOString() : undefined,
274
+ minutes: Math.max(0, Math.round((event.time * 1000 - now) / 60_000)),
275
+ tripId: trip?.tripId,
276
+ realtime: true,
277
+ source: "mta-gtfs-rt",
278
+ raw: query.includeRaw ? entity : undefined,
279
+ });
280
+ }
281
+ }
282
+
283
+ return arrivals;
284
+ }
285
+ }
286
+
287
+ class BusClient {
288
+ constructor(private readonly mta: MTA) {}
289
+
290
+ async arrivals(query: BusArrivalQuery): Promise<Arrival[]> {
291
+ await this.mta.ready();
292
+ const key = this.requireKey();
293
+ const body = await fetchJson(
294
+ this.mta.fetch,
295
+ urlWithParams(this.mta.endpoints.busStopMonitoring, {
296
+ key,
297
+ version: "2",
298
+ OperatorRef: "MTA",
299
+ MonitoringRef: query.stopId,
300
+ LineRef: query.route ? busLineRef(query.route) : undefined,
301
+ }),
302
+ );
303
+ const journeys = monitoredStopVisits(body);
304
+ const now = this.mta.now().getTime();
305
+
306
+ return journeys
307
+ .map((journey): Arrival | undefined => {
308
+ const mvj = journey.MonitoredVehicleJourney;
309
+ if (!mvj) return undefined;
310
+ const routeId = routeFromLineRef(mvj.LineRef ?? query.route ?? "");
311
+ const call = mvj.MonitoredCall ?? {};
312
+ const expected = call.ExpectedArrivalTime ?? call.AimedArrivalTime;
313
+ if (!expected) return undefined;
314
+ const stop = this.mta.static.getStop(String(call.StopPointRef ?? query.stopId)) ?? fallbackStop(query.stopId);
315
+ return {
316
+ mode: "bus",
317
+ route: routeWithFallback(this.mta.static.getRoute(routeId), routeId),
318
+ stop,
319
+ direction: "unknown",
320
+ headsign: stringOrUndefined(mvj.DestinationName),
321
+ arrivalTime: new Date(expected).toISOString(),
322
+ minutes: Math.max(0, Math.round((Date.parse(expected) - now) / 60_000)),
323
+ tripId: stringOrUndefined(mvj.FramedVehicleJourneyRef?.DatedVehicleJourneyRef),
324
+ realtime: true,
325
+ source: "mta-bustime",
326
+ raw: query.includeRaw ? journey : undefined,
327
+ };
328
+ })
329
+ .filter((arrival): arrival is Arrival => Boolean(arrival))
330
+ .sort((a, b) => Date.parse(a.arrivalTime) - Date.parse(b.arrivalTime))
331
+ .slice(0, query.limit ?? 20);
332
+ }
333
+
334
+ async vehicles(query: BusVehicleQuery = {}): Promise<Vehicle[]> {
335
+ await this.mta.ready();
336
+ const key = this.requireKey();
337
+ const body = await fetchJson(
338
+ this.mta.fetch,
339
+ urlWithParams(this.mta.endpoints.busVehicleMonitoring, {
340
+ key,
341
+ version: "2",
342
+ OperatorRef: "MTA",
343
+ LineRef: query.route ? busLineRef(query.route) : undefined,
344
+ VehicleRef: query.vehicleId,
345
+ }),
346
+ );
347
+
348
+ return monitoredVehicleJourneys(body)
349
+ .map((mvj): Vehicle => {
350
+ const routeId = routeFromLineRef(mvj.LineRef ?? query.route ?? "");
351
+ const location = mvj.VehicleLocation ?? {};
352
+ const stopId = stringOrUndefined(mvj.MonitoredCall?.StopPointRef);
353
+ return {
354
+ mode: "bus",
355
+ route: routeWithFallback(this.mta.static.getRoute(routeId), routeId),
356
+ vehicleId: stringOrUndefined(mvj.VehicleRef),
357
+ tripId: stringOrUndefined(mvj.FramedVehicleJourneyRef?.DatedVehicleJourneyRef),
358
+ stop: stopId ? this.mta.static.getStop(stopId) ?? fallbackStop(stopId) : undefined,
359
+ lat: numberOrUndefined(location.Latitude),
360
+ lon: numberOrUndefined(location.Longitude),
361
+ bearing: numberOrUndefined(mvj.Bearing),
362
+ destinationName: stringOrUndefined(mvj.DestinationName),
363
+ recordedAt: mvj.RecordedAtTime ? new Date(mvj.RecordedAtTime).toISOString() : undefined,
364
+ source: "mta-bustime",
365
+ raw: query.includeRaw ? mvj : undefined,
366
+ };
367
+ })
368
+ .slice(0, query.limit ?? 50);
369
+ }
370
+
371
+ private requireKey() {
372
+ if (!this.mta.busTimeKey) throw new MissingBusTimeKeyError();
373
+ return this.mta.busTimeKey;
374
+ }
375
+ }
376
+
377
+ class AlertsClient {
378
+ constructor(private readonly mta: MTA) {}
379
+
380
+ async current(query: AlertQuery = {}): Promise<Alert[]> {
381
+ await this.mta.ready();
382
+ const feed = await this.mta.realtimeFeed(this.mta.endpoints.alerts);
383
+ const alerts: Alert[] = [];
384
+
385
+ for (const entity of feed.entity) {
386
+ if (!entity.alert) continue;
387
+ const informed = entity.alert.informedEntity ?? [];
388
+ const routeIds = [...new Set(informed.map((item) => item.routeId).filter((id): id is string => Boolean(id)))];
389
+ const stopIds = [...new Set(informed.map((item) => item.stopId).filter((id): id is string => Boolean(id)))];
390
+ const routes = routeIds.map((id) => routeWithFallback(this.mta.static.getRoute(id), id));
391
+ const stops = stopIds.map((id) => this.mta.static.getStopOrParent(id) ?? fallbackStop(id));
392
+
393
+ if (query.route && !routeIds.some((id) => normalizeRouteId(id) === normalizeRouteId(query.route!))) continue;
394
+ if (query.stopId && !stopIds.includes(query.stopId)) continue;
395
+ if (query.mode && !alertMatchesMode(query.mode, routes, stops, informed)) continue;
396
+
397
+ alerts.push({
398
+ id: entity.id,
399
+ mode: inferAlertMode(routes, stops, informed),
400
+ routes,
401
+ stops,
402
+ header: translatedText(entity.alert.headerText),
403
+ description: translatedText(entity.alert.descriptionText),
404
+ url: translatedText(entity.alert.url),
405
+ effect: entity.alert.effect,
406
+ activePeriods: (entity.alert.activePeriod ?? []).map((period) => ({
407
+ start: period.start ? new Date(period.start * 1000).toISOString() : undefined,
408
+ end: period.end ? new Date(period.end * 1000).toISOString() : undefined,
409
+ })),
410
+ source: "mta-gtfs-rt",
411
+ raw: query.includeRaw ? entity : undefined,
412
+ });
413
+ }
414
+
415
+ return alerts;
416
+ }
417
+ }
418
+
419
+ class StopsClient {
420
+ constructor(private readonly mta: MTA) {}
421
+
422
+ near(query: StopsNearQuery): Promise<Stop[]> {
423
+ return this.mta.ready().then(() => {
424
+ for (const mode of query.modes ?? []) {
425
+ if (!this.mta.static.hasStaticData(mode)) throw new StaticDataMissingError(mode);
426
+ }
427
+ return this.mta.static.stopsNear(query);
428
+ });
429
+ }
430
+ }
431
+
432
+ function normalizeRouteId(route: string) {
433
+ return route.toUpperCase().trim();
434
+ }
435
+
436
+ function normalizeDirection(direction: Direction | "uptown" | "downtown" | undefined): Direction | undefined {
437
+ if (!direction) return undefined;
438
+ if (direction === "uptown") return "north";
439
+ if (direction === "downtown") return "south";
440
+ return direction;
441
+ }
442
+
443
+ function routeWithFallback(route: Route | undefined, routeId: string): Route {
444
+ return (
445
+ route ?? {
446
+ id: routeId,
447
+ shortName: routeId,
448
+ color: subwayRouteColors[routeId] ? `#${subwayRouteColors[routeId]}` : undefined,
449
+ }
450
+ );
451
+ }
452
+
453
+ function fallbackStop(stopId: string): Stop {
454
+ return { id: stopId, name: stopId };
455
+ }
456
+
457
+ function busLineRef(route: string) {
458
+ const normalized = normalizeBusRouteId(route);
459
+ return normalized.includes("_") ? normalized : `MTA NYCT_${normalized}`;
460
+ }
461
+
462
+ function routeFromLineRef(lineRef: string) {
463
+ return String(lineRef).split("_").at(-1)?.toUpperCase() ?? String(lineRef).toUpperCase();
464
+ }
465
+
466
+ function normalizeBusRouteId(route: string) {
467
+ const normalized = route.toUpperCase().trim();
468
+ const aliases: Record<string, string> = {
469
+ M14A: "M14A-SBS",
470
+ M14D: "M14D-SBS",
471
+ M15: "M15-SBS",
472
+ M23: "M23-SBS",
473
+ M34: "M34-SBS",
474
+ M34A: "M34A-SBS",
475
+ M60: "M60-SBS",
476
+ M79: "M79-SBS",
477
+ M86: "M86-SBS",
478
+ };
479
+ return aliases[normalized] ?? normalized;
480
+ }
481
+
482
+ async function readSourceArrayBuffer(fetchImpl: typeof fetch, sourceUrl: string) {
483
+ const path = localSourcePath(sourceUrl);
484
+ if (path) return Bun.file(path).arrayBuffer();
485
+ return fetchArrayBuffer(fetchImpl, sourceUrl);
486
+ }
487
+
488
+ function localSourcePath(sourceUrl: string) {
489
+ if (sourceUrl.startsWith("file:")) return decodeURIComponent(new URL(sourceUrl).pathname);
490
+ if (sourceUrl.startsWith("/") || sourceUrl.startsWith("./") || sourceUrl.startsWith("../")) return sourceUrl;
491
+ return undefined;
492
+ }
493
+
494
+ function stringOrUndefined(value: unknown) {
495
+ return typeof value === "string" && value.length > 0 ? value : undefined;
496
+ }
497
+
498
+ function numberOrUndefined(value: unknown) {
499
+ if (value === undefined || value === null || value === "") return undefined;
500
+ const number = Number(value);
501
+ return Number.isFinite(number) ? number : undefined;
502
+ }
503
+
504
+ function translatedText(value: TranslatedString | undefined) {
505
+ return value?.translation?.find((translation) => !translation.language || translation.language === "en")?.text ??
506
+ value?.translation?.[0]?.text;
507
+ }
508
+
509
+ function monitoredStopVisits(body: unknown): any[] {
510
+ return (
511
+ (body as any)?.Siri?.ServiceDelivery?.StopMonitoringDelivery?.[0]?.MonitoredStopVisit ??
512
+ (body as any)?.Siri?.ServiceDelivery?.StopMonitoringDelivery?.MonitoredStopVisit ??
513
+ []
514
+ );
515
+ }
516
+
517
+ function monitoredVehicleJourneys(body: unknown): any[] {
518
+ const visits =
519
+ (body as any)?.Siri?.ServiceDelivery?.VehicleMonitoringDelivery?.[0]?.VehicleActivity ??
520
+ (body as any)?.Siri?.ServiceDelivery?.VehicleMonitoringDelivery?.VehicleActivity ??
521
+ [];
522
+ return visits.map((visit: any) => visit.MonitoredVehicleJourney).filter(Boolean);
523
+ }
524
+
525
+ function inferAlertMode(
526
+ routes: Route[],
527
+ stops: Stop[],
528
+ informed: { routeType?: number }[],
529
+ ): TransitMode | undefined {
530
+ if (stops.some((stop) => stop.mode)) return stops.find((stop) => stop.mode)?.mode;
531
+ if (informed.some((item) => item.routeType === 3)) return "bus";
532
+ if (informed.some((item) => item.routeType === 1)) return "subway";
533
+ if (routes.some((route) => route.type === 3)) return "bus";
534
+ if (routes.some((route) => route.type === 1 || route.id.length <= 2)) return "subway";
535
+ return undefined;
536
+ }
537
+
538
+ function alertMatchesMode(
539
+ mode: TransitMode,
540
+ routes: Route[],
541
+ stops: Stop[],
542
+ informed: { routeType?: number }[],
543
+ ) {
544
+ return inferAlertMode(routes, stops, informed) === mode;
545
+ }
546
+
547
+ async function retryOnWalConflict<T>(operation: () => Promise<T>) {
548
+ let lastError: unknown;
549
+ for (const delay of [0, 150, 400, 900]) {
550
+ if (delay) await Bun.sleep(delay);
551
+ try {
552
+ return await operation();
553
+ } catch (error) {
554
+ lastError = error;
555
+ const message = error instanceof Error ? error.message : String(error);
556
+ if (!message.includes("WalConflict")) throw error;
557
+ }
558
+ }
559
+ throw lastError;
560
+ }
561
+
562
+ function applyImportLimits(seed: StaticGtfsSeed, limits: StaticGtfsImportLimits | undefined): StaticGtfsSeed {
563
+ if (!limits) return seed;
564
+ return {
565
+ stops: limits.stops === undefined ? seed.stops : seed.stops?.slice(0, limits.stops),
566
+ routes: limits.routes === undefined ? seed.routes : seed.routes?.slice(0, limits.routes),
567
+ trips: limits.trips === undefined ? seed.trips : seed.trips?.slice(0, limits.trips),
568
+ stopTimes: limits.stopTimes === undefined ? seed.stopTimes : seed.stopTimes?.slice(0, limits.stopTimes),
569
+ };
570
+ }
571
+
572
+ function applyImportStrategy(seed: StaticGtfsSeed, strategy: StaticGtfsImportStrategy): StaticGtfsSeed {
573
+ if (strategy === "schedule") return seed;
574
+ return {
575
+ stops: seed.stops,
576
+ routes: seed.routes,
577
+ trips: [],
578
+ stopTimes: [],
579
+ };
580
+ }
581
+
582
+ export { decodeFeedMessage, encodeFeedMessage } from "./src/gtfs-realtime";
583
+ export { GTFSCache } from "./src/static-gtfs";
584
+ export * from "./src/errors";
585
+ export type * from "./src/types";
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "mta-js",
3
+ "version": "1.0.0",
4
+ "description": "A TypeScript client for MTA realtime and static GTFS data.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/HumanInterfaceDesign/mta-js.git"
9
+ },
10
+ "files": [
11
+ "index.ts",
12
+ "src",
13
+ "examples",
14
+ "README.md"
15
+ ],
16
+ "module": "index.ts",
17
+ "type": "module",
18
+ "exports": {
19
+ ".": "./index.ts"
20
+ },
21
+ "types": "./index.ts",
22
+ "bin": {
23
+ "mta-js": "src/cli.ts"
24
+ },
25
+ "scripts": {
26
+ "test": "bun test",
27
+ "typecheck": "bunx tsc --noEmit",
28
+ "db:push": "bun src/cli.ts db push"
29
+ },
30
+ "devDependencies": {
31
+ "@types/bun": "latest"
32
+ },
33
+ "peerDependencies": {
34
+ "typescript": "^5"
35
+ },
36
+ "dependencies": {
37
+ "@libsql/client": "^0.17.3",
38
+ "csv-parse": "^6.2.1",
39
+ "fflate": "^0.8.2",
40
+ "protobufjs": "^8.0.3"
41
+ }
42
+ }