Skip to content

Commit

Permalink
Bump version of node-fetch, drop dependency on promisify-require
Browse files Browse the repository at this point in the history
Newer version of Node.js expose "fs.promises" to access versions of async
functions that return a Promise instead of using a callback.

Note this means that the package now needs Node.js v12 or above.
  • Loading branch information
tidoust committed Feb 28, 2020
1 parent f93106d commit f5937ce
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
15 changes: 7 additions & 8 deletions fetch-filecache.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const baseFetch = require('node-fetch');
const Response = require('node-fetch').Response;
const rimraf = require('rimraf');
const path = require('path');
const promisifyRequire = require('promisify-require');
const fs = promisifyRequire('fs');
const fs = require('fs');

let globalConfig = {
cacheFolder: '.cache',
Expand Down Expand Up @@ -233,7 +232,7 @@ async function fetch(url, options) {

async function checkCacheFolder() {
try {
let stat = await fs.stat(config.cacheFolder);
let stat = await fs.promises.stat(config.cacheFolder);
if (!stat.isDirectory()) {
throw new Error('Looking for a cache folder but found a cache file instead');
}
Expand All @@ -244,7 +243,7 @@ async function fetch(url, options) {
throw err;
}
try {
await fs.mkdir(config.cacheFolder);
await fs.promises.mkdir(config.cacheFolder);
}
catch (mkerr) {
// Someone may have created the folder in the meantime
Expand Down Expand Up @@ -294,7 +293,7 @@ async function fetch(url, options) {

async function readHeadersFromCache() {
try {
let data = await fs.readFile(cacheHeadersFilename);
let data = await fs.promises.readFile(cacheHeadersFilename);
let headers = JSON.parse(data, 'utf8');
return headers;
}
Expand All @@ -305,7 +304,7 @@ async function fetch(url, options) {
}

async function readFromCache() {
let data = await fs.readFile(cacheHeadersFilename, 'utf8');
let data = await fs.promises.readFile(cacheHeadersFilename, 'utf8');
let headers = JSON.parse(data);
let status = headers.status || 200;
if (headers.status) {
Expand All @@ -331,7 +330,7 @@ async function fetch(url, options) {
}
});
try {
await fs.writeFile(cacheHeadersFilename, JSON.stringify(prevHeaders, null, 2), 'utf8');
await fs.promises.writeFile(cacheHeadersFilename, JSON.stringify(prevHeaders, null, 2), 'utf8');
}
catch (err) {
}
Expand All @@ -344,7 +343,7 @@ async function fetch(url, options) {
writable.on('close', _ => {
let headers = { status: response.status, received: (new Date()).toUTCString() };
response.headers.forEach((value, header) => headers[header] = value);
fs.writeFile(cacheHeadersFilename, JSON.stringify(headers, null, 2), 'utf8')
fs.promises.writeFile(cacheHeadersFilename, JSON.stringify(headers, null, 2), 'utf8')
.then(resolve).catch(reject);
});
writable.on('error', reject);
Expand Down
11 changes: 3 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
},
"license": "MIT",
"engines": {
"node": ">=8.0.0"
"node": ">=12.0.0"
},
"dependencies": {
"filenamify-url": "^1.0.0",
"node-fetch": "^2.1.2",
"promisify-require": "^1.0.2",
"node-fetch": "^2.6.0",
"rimraf": "^2.6.2"
}
}

0 comments on commit f5937ce

Please sign in to comment.