Skip to content

Commit

Permalink
Replace unzipper dependency for adm-zip
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrojas87 committed Dec 18, 2023
1 parent 19b4bbd commit 6b9a697
Show file tree
Hide file tree
Showing 6 changed files with 720 additions and 930 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [16.x, 18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- name: Checkout
Expand Down
44 changes: 25 additions & 19 deletions lib/GtfsIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GtfsIndex {
throw new Error('Please provide a valid url or a path to a GTFS feed');
} else {
if (this.path.endsWith('.zip')) {
await this.unzip(fs.createReadStream(this.path));
Utils.unzip(this.path, this.auxPath);
} else {
this.auxPath = this.path;
}
Expand All @@ -47,23 +47,29 @@ class GtfsIndex {
}
}

async download(url, headers) {
try {
const res = await request(url, { method: 'GET', headers });

if (res.statusCode === 200) {
await this.unzip(await res.body);
} else {
throw new Error(`Error on HTTP request: ${url}, Message: ${await res.body.text()}`);
download(url, headers) {
return new Promise(async (resolve, reject) => {
try {
const res = await request(url, {
method: 'GET',
headers,
maxRedirections: 10
});

if (res.statusCode === 200) {
res.body.pipe(createWriteStream("/tmp/gtfs.zip"))
.on("finish", () => {
Utils.unzip("/tmp/gtfs.zip", this.auxPath);
resolve();
});
} else {
reject(new Error(`Error on HTTP request: ${url}, Message: ${await res.body.text()}`));
}
} catch (err) {
await this.cleanUp();
reject(err);
}
} catch (err) {
await this.cleanUp();
throw err;
}
}

unzip(stream) {
return Utils.unzipStream(stream, this.auxPath);
});
}

async createIndexes(store, uTrips, deduce) {
Expand Down Expand Up @@ -143,7 +149,7 @@ class GtfsIndex {
let rp = this.createIndex(this.auxPath + '/routes.txt', routes_index, 'route_id');
cp = this.createIndex(this.auxPath + '/calendar.txt', calendar_index, 'service_id');

if (deduce) {
if (deduce) {
cdp = this.processCalendarDates(this.auxPath + '/calendar_dates.txt', calendarDates);
}

Expand Down Expand Up @@ -303,7 +309,7 @@ class GtfsIndex {
async cleanUp() {
// We don't want to delete sources that are probably been reused
if (this.auxPath !== this.path) {
await del([this.auxPath], { force: true });
await del([this.auxPath, "/tmp/gtfs.zip"], { force: true });
}
}

Expand Down
22 changes: 6 additions & 16 deletions lib/Utils.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
const fs = require('fs');
const unzip = require('unzipper');
const { format } = require('date-fns');
const AdmZip = require('adm-zip');

function unzipStream(stream, outPath) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath);
}
stream.pipe(unzip.Extract({ path: outPath }))
.on('error', async err => {
reject(err);
})
.on('close', () => {
resolve();
});
});
}
function unzip(zipped, path) {
const adm = new AdmZip(zipped);
adm.extractAllTo(path, true);
}

function resolveURI(template, raw, resolve, stopType) {
let varNames = template.varNames;
Expand Down Expand Up @@ -90,7 +80,7 @@ function resolveScheduleRelationship(value) {
}

module.exports = {
unzipStream,
unzip,
resolveURI,
resolveScheduleRelationship
}
Loading

0 comments on commit 6b9a697

Please sign in to comment.