Skip to content

Commit

Permalink
Merge pull request #54 from runk/use-fetch
Browse files Browse the repository at this point in the history
Use fetch instead of node:https to download files
  • Loading branch information
sgarner authored Mar 22, 2024
2 parents 2f7056e + 241853e commit 2153c0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"homepage": "https://github.com/runk/node-geolite2#readme",
"dependencies": {
"node-fetch": "^2.7.0",
"tar": "^5.0.5"
},
"devDependencies": {
Expand Down
42 changes: 13 additions & 29 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs');
const https = require('https');
const zlib = require('zlib');
const tar = require('tar');
const path = require('path');
const fetch = require('node-fetch');
const { getAccountId, getLicense, getSelectedDbs } = require('../utils');

let licenseKey;
Expand Down Expand Up @@ -60,37 +60,21 @@ const downloadPath = path.join(__dirname, '..', 'dbs');
if (!fs.existsSync(downloadPath)) fs.mkdirSync(downloadPath);

const request = async (url, options) => {
const response = await new Promise((resolve, reject) => {
https
.request(
url,
{
auth: accountId ? `${accountId}:${licenseKey}` : null,
...options,
},
(response) => {
resolve(response);
const response = await fetch(url, {
headers: accountId
? {
Authorization: `Basic ${Buffer.from(
`${accountId}:${licenseKey}`
).toString('base64')}`,
}
)
.on('error', (err) => reject(err))
.end();
: undefined,
redirect: 'follow',
...options,
});

if (
response.statusCode >= 300 &&
response.statusCode < 400 &&
response.headers.location
) {
// Handle redirect
return request(response.headers.location, {
...options,
auth: null,
});
}

if (response.statusCode !== 200) {
if (!response.ok) {
throw new Error(
`Request failed to ${url} - ${response.statusCode} ${response.statusMessage}`
`Failed to fetch ${url}: ${response.status} ${response.statusText}`
);
}

Expand Down Expand Up @@ -123,7 +107,7 @@ const main = async () => {
const response = await request(link(editionId));
const entryPromises = [];
await new Promise((resolve, reject) =>
response
response.body
.pipe(zlib.createGunzip())
.pipe(tar.t())
.on('entry', (entry) => {
Expand Down

0 comments on commit 2153c0c

Please sign in to comment.