gtfs 4.13.4 → 4.14.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/dist/index.js CHANGED
@@ -3480,31 +3480,34 @@ function formatOrderByClause(orderBy2) {
3480
3480
 
3481
3481
  // src/lib/import.ts
3482
3482
  var downloadFiles = async (task) => {
3483
- task.log(`Downloading GTFS from ${task.agency_url}`);
3483
+ task.log(`Downloading GTFS from ${task.url}`);
3484
3484
  task.path = `${task.downloadDir}/gtfs.zip`;
3485
- const response = await fetch(task.agency_url, {
3485
+ const response = await fetch(task.url, {
3486
3486
  method: "GET",
3487
3487
  headers: task.headers || {},
3488
3488
  signal: task.downloadTimeout ? AbortSignal.timeout(task.downloadTimeout) : void 0
3489
3489
  });
3490
3490
  if (response.status !== 200) {
3491
- throw new Error(`Unable to download GTFS from ${task.agency_url}`);
3491
+ throw new Error(`Unable to download GTFS from ${task.url}`);
3492
3492
  }
3493
3493
  const buffer = await response.arrayBuffer();
3494
3494
  await writeFile(task.path, Buffer.from(buffer));
3495
3495
  task.log("Download successful");
3496
3496
  };
3497
- var downloadGtfsRealtimeData = async (url, task) => {
3498
- const response = await fetch(url, {
3497
+ var downloadGtfsRealtimeData = async (urlAndHeaders, task) => {
3498
+ task.log(`Downloading GTFS-Realtime from ${urlAndHeaders.url}`);
3499
+ const response = await fetch(urlAndHeaders.url, {
3499
3500
  method: "GET",
3500
3501
  headers: {
3501
- ...task.realtime_headers || {},
3502
+ ...urlAndHeaders.headers ?? {},
3502
3503
  "Accept-Encoding": "gzip"
3503
3504
  },
3504
3505
  signal: task.downloadTimeout ? AbortSignal.timeout(task.downloadTimeout) : void 0
3505
3506
  });
3506
3507
  if (response.status !== 200) {
3507
- task.logWarning(`Unable to download GTFS-Realtime from ${url}`);
3508
+ task.logWarning(
3509
+ `Unable to download GTFS-Realtime from ${urlAndHeaders.url}`
3510
+ );
3508
3511
  return null;
3509
3512
  }
3510
3513
  const buffer = await response.arrayBuffer();
@@ -3585,48 +3588,70 @@ var prepareRealtimeValue = (entity, column, task) => {
3585
3588
  );
3586
3589
  };
3587
3590
  var updateRealtimeData = async (task) => {
3588
- if (!task.realtime_urls) {
3591
+ if (task.realtimeAlerts === void 0 && task.realtimeTripUpdates === void 0 && task.realtimeVehiclePositions === void 0) {
3589
3592
  return;
3590
3593
  }
3591
3594
  const db = openDb({
3592
3595
  sqlitePath: task.sqlitePath
3593
3596
  });
3594
- task.log(
3595
- `Starting GTFS-Realtime import from ${task.realtime_urls.length} urls`
3596
- );
3597
- for (const realtimeUrl of task.realtime_urls) {
3598
- task.log(`Downloading GTFS-Realtime from ${realtimeUrl}`);
3599
- const gtfsRealtimeData = await downloadGtfsRealtimeData(realtimeUrl, task);
3600
- if (!gtfsRealtimeData?.entity) {
3601
- continue;
3602
- }
3603
- task.log(`Download successful`);
3604
- let totalLineCount = 0;
3605
- for (const entity of gtfsRealtimeData.entity) {
3606
- let model;
3607
- if (entity.vehicle) {
3608
- model = vehiclePositions;
3609
- }
3610
- if (entity.tripUpdate) {
3611
- model = tripUpdates;
3612
- }
3613
- if (entity.alert) {
3614
- model = serviceAlerts;
3615
- }
3616
- if (!model) {
3617
- break;
3618
- }
3619
- const fieldValues = model.schema.map(
3620
- (column) => prepareRealtimeValue(entity, column, task)
3621
- );
3622
- try {
3623
- db.prepare(
3624
- `REPLACE INTO ${model.filenameBase} (${model.schema.map((column) => column.name).join(", ")}) VALUES (${fieldValues.join(", ")})`
3625
- ).run();
3626
- } catch (error) {
3627
- task.logWarning("Import error: " + error.message);
3597
+ if (task.realtimeAlerts?.url) {
3598
+ const gtfsRealtimeData = await downloadGtfsRealtimeData(
3599
+ task.realtimeAlerts,
3600
+ task
3601
+ );
3602
+ if (gtfsRealtimeData?.entity) {
3603
+ task.log(`Download successful`);
3604
+ let totalLineCount = 0;
3605
+ for (const entity of gtfsRealtimeData.entity) {
3606
+ const fieldValues = serviceAlerts.schema.map(
3607
+ (column) => prepareRealtimeValue(entity, column, task)
3608
+ );
3609
+ try {
3610
+ db.prepare(
3611
+ `REPLACE INTO ${serviceAlerts.filenameBase} (${serviceAlerts.schema.map((column) => column.name).join(", ")}) VALUES (${fieldValues.join(", ")})`
3612
+ ).run();
3613
+ } catch (error) {
3614
+ task.logWarning("Import error: " + error.message);
3615
+ }
3616
+ const alertTargetArray = [];
3617
+ for (const informedEntity of entity.alert.informedEntity) {
3618
+ informedEntity.parent = entity;
3619
+ const subValues = serviceAlertTargets.schema.map(
3620
+ (column) => prepareRealtimeValue(informedEntity, column, task)
3621
+ );
3622
+ alertTargetArray.push(`(${subValues.join(", ")})`);
3623
+ totalLineCount++;
3624
+ }
3625
+ try {
3626
+ db.prepare(
3627
+ `REPLACE INTO ${serviceAlertTargets.filenameBase} (${serviceAlertTargets.schema.map((column) => column.name).join(", ")}) VALUES ${alertTargetArray.join(", ")}`
3628
+ ).run();
3629
+ } catch (error) {
3630
+ task.logWarning("Import error: " + error.message);
3631
+ }
3632
+ task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
3628
3633
  }
3629
- if (entity.tripUpdate) {
3634
+ }
3635
+ }
3636
+ if (task.realtimeTripUpdates?.url) {
3637
+ const gtfsRealtimeData = await downloadGtfsRealtimeData(
3638
+ task.realtimeTripUpdates,
3639
+ task
3640
+ );
3641
+ if (gtfsRealtimeData?.entity) {
3642
+ task.log(`Download successful`);
3643
+ let totalLineCount = 0;
3644
+ for (const entity of gtfsRealtimeData.entity) {
3645
+ const fieldValues = tripUpdates.schema.map(
3646
+ (column) => prepareRealtimeValue(entity, column, task)
3647
+ );
3648
+ try {
3649
+ db.prepare(
3650
+ `REPLACE INTO ${tripUpdates.filenameBase} (${tripUpdates.schema.map((column) => column.name).join(", ")}) VALUES (${fieldValues.join(", ")})`
3651
+ ).run();
3652
+ } catch (error) {
3653
+ task.logWarning("Import error: " + error.message);
3654
+ }
3630
3655
  const stopTimeUpdateArray = [];
3631
3656
  for (const stopTimeUpdate of entity.tripUpdate.stopTimeUpdate) {
3632
3657
  stopTimeUpdate.parent = entity;
@@ -3643,26 +3668,31 @@ var updateRealtimeData = async (task) => {
3643
3668
  } catch (error) {
3644
3669
  task.logWarning("Import error: " + error.message);
3645
3670
  }
3671
+ task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
3646
3672
  }
3647
- if (entity.alert) {
3648
- const alertTargetArray = [];
3649
- for (const informedEntity of entity.alert.informedEntity) {
3650
- informedEntity.parent = entity;
3651
- const subValues = serviceAlertTargets.schema.map(
3652
- (column) => prepareRealtimeValue(informedEntity, column, task)
3653
- );
3654
- alertTargetArray.push(`(${subValues.join(", ")})`);
3655
- totalLineCount++;
3656
- }
3673
+ }
3674
+ }
3675
+ if (task.realtimeVehiclePositions?.url) {
3676
+ const gtfsRealtimeData = await downloadGtfsRealtimeData(
3677
+ task.realtimeVehiclePositions,
3678
+ task
3679
+ );
3680
+ if (gtfsRealtimeData?.entity) {
3681
+ task.log(`Download successful`);
3682
+ let totalLineCount = 0;
3683
+ for (const entity of gtfsRealtimeData.entity) {
3684
+ const fieldValues = tripUpdates.schema.map(
3685
+ (column) => prepareRealtimeValue(entity, column, task)
3686
+ );
3657
3687
  try {
3658
3688
  db.prepare(
3659
- `REPLACE INTO ${serviceAlertTargets.filenameBase} (${serviceAlertTargets.schema.map((column) => column.name).join(", ")}) VALUES ${alertTargetArray.join(", ")}`
3689
+ `REPLACE INTO ${tripUpdates.filenameBase} (${tripUpdates.schema.map((column) => column.name).join(", ")}) VALUES (${fieldValues.join(", ")})`
3660
3690
  ).run();
3661
3691
  } catch (error) {
3662
3692
  task.logWarning("Import error: " + error.message);
3663
3693
  }
3694
+ task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
3664
3695
  }
3665
- task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
3666
3696
  }
3667
3697
  }
3668
3698
  task.log(`GTFS-Realtime data import complete`);
@@ -3974,10 +4004,11 @@ async function importGtfs(initialConfig) {
3974
4004
  const tempPath = temporaryDirectory();
3975
4005
  const task = {
3976
4006
  exclude: agency2.exclude,
3977
- agency_url: agency2.url,
4007
+ url: agency2.url,
3978
4008
  headers: agency2.headers,
3979
- realtime_headers: agency2.realtimeHeaders,
3980
- realtime_urls: agency2.realtimeUrls,
4009
+ realtimeAlerts: agency2.realtimeAlerts,
4010
+ realtimeTripUpdates: agency2.realtimeTripUpdates,
4011
+ realtimeVehiclePositions: agency2.realtimeVehiclePositions,
3981
4012
  downloadDir: tempPath,
3982
4013
  downloadTimeout: config.downloadTimeout,
3983
4014
  gtfsRealtimeExpirationSeconds: config.gtfsRealtimeExpirationSeconds,
@@ -3992,7 +4023,7 @@ async function importGtfs(initialConfig) {
3992
4023
  logError: logError2
3993
4024
  };
3994
4025
  try {
3995
- if (task.agency_url) {
4026
+ if (task.url) {
3996
4027
  await downloadFiles(task);
3997
4028
  }
3998
4029
  await readFiles(task);
@@ -4040,12 +4071,10 @@ async function updateGtfsRealtime(initialConfig) {
4040
4071
  deleteExpiredRealtimeData(config);
4041
4072
  await Promise.all(
4042
4073
  config.agencies.map(async (agency2) => {
4043
- if (agency2.realtimeUrls === void 0) {
4044
- return;
4045
- }
4046
4074
  const task = {
4047
- realtime_headers: agency2.realtimeHeaders,
4048
- realtime_urls: agency2.realtimeUrls,
4075
+ realtimeAlerts: agency2.realtimeAlerts,
4076
+ realtimeTripUpdates: agency2.realtimeTripUpdates,
4077
+ realtimeVehiclePositions: agency2.realtimeVehiclePositions,
4049
4078
  downloadTimeout: config.downloadTimeout,
4050
4079
  gtfsRealtimeExpirationSeconds: config.gtfsRealtimeExpirationSeconds,
4051
4080
  sqlitePath: config.sqlitePath,
@@ -4068,7 +4097,7 @@ async function updateGtfsRealtime(initialConfig) {
4068
4097
  } catch (error) {
4069
4098
  if (error?.code === "SQLITE_CANTOPEN") {
4070
4099
  logError2(
4071
- `Unae to open sqlite database "${config.sqlitePath}" defined as \`sqlitePath\` config.json. Ensure the parent directory exists or remove \`sqlitePath\` from config.json.`
4100
+ `Unable to open sqlite database "${config.sqlitePath}" defined as \`sqlitePath\` config.json. Ensure the parent directory exists or remove \`sqlitePath\` from config.json.`
4072
4101
  );
4073
4102
  }
4074
4103
  throw error;