Skip to content

Commit af4e87d

Browse files
author
Chris Wilson
committed
updating to import module
1 parent 4644da5 commit af4e87d

File tree

4 files changed

+51
-46
lines changed

4 files changed

+51
-46
lines changed

index.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
const fs = require('graceful-fs');
2-
const path = require('path');
3-
const querystring = require("querystring");
4-
const fetch = require('node-fetch');
5-
const request = require('postman-request');
6-
const urlparse = require('url');
7-
const mkdirp = require('mkdirp');
8-
const log = require('npmlog');
9-
10-
const RateLimiter = require('limiter').RateLimiter;
1+
import fs from 'graceful-fs';
2+
import path from 'path';
3+
import querystring from "querystring";
4+
import fetch from 'node-fetch';
5+
import request from 'postman-request';
6+
import urlparse from 'url';
7+
import mkdirp from 'mkdirp';
8+
import log from 'npmlog';
9+
10+
// import rl from 'limiter';
11+
// const RateLimiter = rl.RateLimiter;
1112

1213
/* OPTIONS */
1314
/*
@@ -26,7 +27,7 @@ post: POST request
2627
let limiter, opts, queueDownload, download;
2728

2829
// create a filepath out of url, same as wget
29-
const url_to_path = module.exports.url_to_path = function(url, opts) {
30+
const url_to_path = function(url, opts) {
3031
let parsedUrl = urlparse.parse(url);
3132
let p = path.join(parsedUrl.hostname, parsedUrl.path);
3233
// exorcise any trailing "/"
@@ -51,7 +52,7 @@ const url_to_path = module.exports.url_to_path = function(url, opts) {
5152
// downcache("http://example.com", function(err, resp, body) {} )
5253

5354
// we want to be flexible with the order of arguments since there are only three feasible types
54-
module.exports = function() {
55+
const Downcache = function() {
5556
// this doesn't currently fire.
5657
let callback = function(err, resp, body) {
5758
log.info("This is the default downcache callback since you didn't provide one.");
@@ -100,7 +101,7 @@ module.exports = function() {
100101

101102
log.level = opts.log;
102103

103-
limiter = new RateLimiter(1, opts.limit);
104+
// limiter = new RateLimiter(1, opts.limit);
104105

105106
if (!opts.useRequest) {
106107
download = downloadWithFetch;
@@ -109,13 +110,14 @@ module.exports = function() {
109110
}
110111

111112
queueDownload = function(opts, callback) {
112-
limiter.removeTokens(1, function(err, remainingRequests) {
113-
if (err) {
114-
log.info("rate limited " + opts.url);
115-
return callback("rate limited");
116-
}
117-
download(opts, callback);
118-
});
113+
// limiter.removeTokens(1, function(err, remainingRequests) {
114+
// if (err) {
115+
// log.info("rate limited " + opts.url);
116+
// return callback("rate limited");
117+
// }
118+
// download(opts, callback);
119+
// });
120+
download(opts, callback);
119121
}
120122

121123
// copy `url` and `encoding` to the headers object
@@ -162,7 +164,7 @@ let fireCallback = function(opts, err, resp, body, callback) {
162164
}
163165

164166
// check if the file is in cache
165-
let retrieve = module.exports.retrieve = function(opts, callback) {
167+
let retrieve = function(opts, callback) {
166168
if (opts.force) {
167169
queueDownload(opts, callback);
168170
return;
@@ -301,13 +303,15 @@ function savePage(opts, resp, body) {
301303
} // savePage
302304

303305
// update the global settings that get used in absense of a specification in the individual call
304-
module.exports.set = function(property, value) {
306+
function SetProperty(property, value) {
305307
if (typeof property == "string" && typeof value == "string") {
306308
opts[property] = value;
307309
} else if (typeof property == "object") {
308310
opts = Object.assign(property, opts);
309311
}
310312
if (property == "limit" || property.limit) {
311-
limiter = new RateLimiter(1, opts.limit);
313+
// limiter = new RateLimiter(1, opts.limit);
312314
}
313315
}
316+
317+
export { Downcache, SetProperty };

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"author": "Chris Wilson <christopher.e.wilson@gmail.com>",
55
"description": "Download and cache webpages and write them to disk for fast future retrieval",
66
"main": "index.js",
7+
"type": "module",
78
"repository": {
89
"type": "git",
910
"url": "https://github.com/wilson428/downcache"
@@ -15,13 +16,13 @@
1516
"test": "test/test.js"
1617
},
1718
"dependencies": {
18-
"fetch-cookie": "^0.11.0",
19-
"graceful-fs": "^4.2.4",
20-
"limiter": "^1.1.5",
19+
"fetch-cookie": "^1.0.0",
20+
"graceful-fs": "^4.2.8",
21+
"limiter": "^2.1.0",
2122
"mkdirp": "^1.0.4",
22-
"node-fetch": "^2.6.1",
23-
"npmlog": "^4.1.2",
24-
"postman-request": "^2.88.1-postman.29",
23+
"node-fetch": "^3.1.0",
24+
"npmlog": "^6.0.0",
25+
"postman-request": "^2.88.1-postman.30",
2526
"request": "^2.88.2"
2627
},
2728
"devDependencies": {

test/test.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
#!/usr/bin/env node
22

3-
var downcache = require("../index"),
4-
assert = require("assert"),
5-
rimraf = require("rimraf");
3+
import { Downcache, SetProperty } from "../index.js";
4+
import { strict as assert } from 'assert';
5+
import rimraf from "rimraf";
66

7-
downcache.set({
7+
SetProperty({
88
log: "verbose",
99
limit: 2000
1010
});
1111

12-
assert.equal(downcache.url_to_path("http://example.com", {}), "example.com/index.html");
12+
//assert.equal(Downcache.url_to_path("http://example.com", {}), "example.com/index.html");
1313

1414
// remove old cache from previous tests
1515
rimraf("./cache", function(err) {
1616
// see what happens when we get a status code other than 200
17-
downcache("http://api.meetup.com/2/members?group_id=741891&key=dsafsadfsadfasf", function(err, resp, html) {
17+
Downcache("http://api.meetup.com/2/members?group_id=741891&key=dsafsadfsadfasf", function(err, resp, html) {
1818
console.log(err);
1919
});
2020

21-
downcache("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Jimmy%20Rollins&rvprop=content&format=json", function(err, resp, html) {
21+
Downcache("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Jimmy%20Rollins&rvprop=content&format=json", function(err, resp, html) {
2222
//console.log(resp);
2323
//console.log(Object.keys(resp.response));
2424
console.log(resp.type, resp.sub_type);
2525
});
2626

27-
downcache("http://www.imdb.com", function(err, resp, html) {
28-
downcache("http://www.imdb.com/title/tt0068646/", function(err, resp, html) {
27+
Downcache("http://www.imdb.com", function(err, resp, html) {
28+
Downcache("http://www.imdb.com/title/tt0068646/", function(err, resp, html) {
2929
console.log("Downloaded", html.length, "characters from", resp.url);
30-
downcache("http://www.imdb.com/title/tt0068646/fullcredits", function(err, resp, html) {
30+
Downcache("http://www.imdb.com/title/tt0068646/fullcredits", function(err, resp, html) {
3131
console.log("Downloaded", html.length, "characters from", resp.url);
3232
});
3333
});
3434
});
3535

36-
downcache("http://time.com/67280/mean-girls-mythology/", {
36+
Downcache("http://time.com/67280/mean-girls-mythology/", {
3737
path: "great-articles/meangirls.html",
3838
log: "verbose" // will override just for this call
3939
}, function(err, resp, html) {
@@ -42,7 +42,7 @@ rimraf("./cache", function(err) {
4242
}
4343

4444
// try again, see it load from cache
45-
downcache("http://time.com/67280/mean-girls-mythology/", {
45+
Downcache("http://time.com/67280/mean-girls-mythology/", {
4646
path: "great-articles/meangirls.html",
4747
log: "verbose"
4848
}, function(err, resp, html) {
@@ -51,7 +51,7 @@ rimraf("./cache", function(err) {
5151
});
5252

5353
// testing POST
54-
downcache("https://statements.mevaker.gov.il/Handler/GuarantyDonationPublisherHandler.ashx", {
54+
Downcache("https://statements.mevaker.gov.il/Handler/GuarantyDonationPublisherHandler.ashx", {
5555
post: {
5656
action: "gcbp",
5757
p: 3
@@ -61,7 +61,7 @@ rimraf("./cache", function(err) {
6161
});
6262
/*
6363
// still working on this
64-
downcache("http://mlb.mlb.com/images/players/mugshot/ph_424324.jpg", function(err, resp, body) {
64+
Downcache("http://mlb.mlb.com/images/players/mugshot/ph_424324.jpg", function(err, resp, body) {
6565
console.log(resp.type, resp.sub_type);
6666
});
6767
*/

test/test2.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const downcache = require('../index');
1+
import { Downcache } from '../index.js';
22

3-
downcache("https://electproject.github.io/Early-Vote-2020G/index.html", {
3+
Downcache("https://electproject.github.io/Early-Vote-2020G/index.html", {
44
dir: "my_cache",
55
log: "verbose"
66
}, function(err, resp, body) {
@@ -9,7 +9,7 @@ downcache("https://electproject.github.io/Early-Vote-2020G/index.html", {
99
});
1010

1111

12-
downcache("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Jimmy%20Rollins&rvprop=content&format=json", { dir: "json", json: true }, function(err, resp, json) {
12+
Downcache("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Jimmy%20Rollins&rvprop=content&format=json", { dir: "json", json: true }, function(err, resp, json) {
1313
console.log(json);
1414
console.log(typeof json);
1515
});

0 commit comments

Comments
 (0)