@@ -2,11 +2,11 @@ const { URL } = require('url');
22const { request } = require ( 'undici' ) ;
33const util = require ( 'util' ) ;
44const fs = require ( 'fs' ) ;
5- const unzip = require ( 'unzipper' ) ;
65const csv = require ( 'fast-csv' ) ;
76const del = require ( 'del' ) ;
87const childProcess = require ( 'child_process' ) ;
98const { Level } = require ( 'level' ) ;
9+ const Utils = require ( './Utils' ) ;
1010
1111const exec = util . promisify ( childProcess . exec ) ;
1212
@@ -17,60 +17,48 @@ class GtfsIndex {
1717 this . _headers = options . headers || { } ;
1818 }
1919
20- getIndexes ( options ) {
21- return new Promise ( async ( resolve , reject ) => {
22- try {
23- await this . cleanUp ( ) ;
24- // Download or load from disk static GTFS feed
25- if ( this . path . startsWith ( 'http' ) || this . path . startsWith ( 'https' ) ) {
26- const downloadURL = new URL ( this . path ) ;
27- if ( ! downloadURL . protocol ) {
28- reject ( 'Please provide a valid URL or a path to a GTFS feed' ) ;
29- return ;
30- } else {
31- await this . download ( this . path , this . headers ) ;
32- }
20+ async getIndexes ( options ) {
21+ try {
22+ await this . cleanUp ( ) ;
23+ // Download or load from disk static GTFS feed
24+ if ( this . path . startsWith ( 'http' ) || this . path . startsWith ( 'https' ) ) {
25+ const downloadURL = new URL ( this . path ) ;
26+ if ( ! downloadURL . protocol ) {
27+ throw new Error ( 'Please provide a valid URL or a path to a GTFS feed' ) ;
3328 } else {
34- if ( ! fs . existsSync ( this . path ) ) {
35- reject ( 'Please provide a valid url or a path to a GTFS feed' ) ;
36- return ;
37- } else {
29+ await this . download ( this . path , this . headers ) ;
30+ }
31+ } else {
32+ if ( ! fs . existsSync ( this . path ) ) {
33+ throw new Error ( 'Please provide a valid url or a path to a GTFS feed' ) ;
34+ } else {
35+ if ( this . path . endsWith ( '.zip' ) ) {
3836 await this . unzip ( fs . createReadStream ( this . path ) ) ;
37+ } else {
38+ this . auxPath = this . path ;
3939 }
4040 }
41-
42- resolve ( this . createIndexes ( options . store , options . trips , options . deduce ) ) ;
43- } catch ( err ) {
44- await this . cleanUp ( ) ;
45- reject ( err ) ;
4641 }
47- } ) ;
42+
43+ return this . createIndexes ( options . store , options . trips , options . deduce ) ;
44+ } catch ( err ) {
45+ await this . cleanUp ( ) ;
46+ throw err ;
47+ }
4848 }
4949
5050 async download ( url , headers ) {
5151 const res = await request ( url , { method : 'GET' , headers } ) ;
5252
53- if ( res . statusCode === 200 ) {
53+ if ( res . statusCode === 200 ) {
5454 await this . unzip ( await res . body ) ;
5555 } else {
5656 throw new Error ( `Error on HTTP request: ${ url } , Message: ${ await res . body . text ( ) } ` ) ;
5757 }
5858 }
5959
60- unzip ( res ) {
61- return new Promise ( ( resolve , reject ) => {
62- if ( ! fs . existsSync ( this . auxPath ) ) {
63- fs . mkdirSync ( this . auxPath ) ;
64- }
65- res . pipe ( unzip . Extract ( { path : this . auxPath } ) )
66- . on ( 'error' , async err => {
67- await this . cleanUp ( ) ;
68- reject ( err ) ;
69- } )
70- . on ( 'close' , ( ) => {
71- resolve ( ) ;
72- } ) ;
73- } ) ;
60+ unzip ( stream ) {
61+ return Utils . unzipStream ( stream , this . auxPath ) ;
7462 }
7563
7664 async createIndexes ( store , uTrips , deduce ) {
@@ -88,7 +76,7 @@ class GtfsIndex {
8876 let cdp = null ;
8977
9078
91- if ( deduce ) {
79+ if ( deduce ) {
9280 tripsByRoute = new Map ( ) ;
9381 firstStops = new Map ( ) ;
9482 calendar = new Map ( ) ;
@@ -147,7 +135,7 @@ class GtfsIndex {
147135 let sp = this . createIndex ( this . auxPath + '/stops.txt' , stops_index , 'stop_id' ) ;
148136 let rp = this . createIndex ( this . auxPath + '/routes.txt' , routes_index , 'route_id' ) ;
149137
150- if ( deduce ) {
138+ if ( deduce ) {
151139 cp = this . createIndex ( this . auxPath + '/calendar.txt' , calendar , 'service_id' ) ;
152140 cdp = this . processCalendarDates ( this . auxPath + '/calendar_dates.txt' , calendarDates ) ;
153141 }
@@ -317,6 +305,10 @@ class GtfsIndex {
317305 return this . _auxPath ;
318306 }
319307
308+ set auxPath ( path ) {
309+ this . _auxPath = path ;
310+ }
311+
320312 get headers ( ) {
321313 return this . _headers ;
322314 }
0 commit comments