html-snapshots 1.4.0 → 1.5.1
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/lib/common/index.js
CHANGED
|
@@ -27,7 +27,8 @@ function ensure (options, must) {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
30
|
+
* Simple test for url
|
|
31
|
+
*
|
|
31
32
|
* If you can think of a more approriate test for this use case,
|
|
32
33
|
* please let me know in the issues...
|
|
33
34
|
*
|
|
@@ -41,6 +42,55 @@ function isUrl (obj) {
|
|
|
41
42
|
return false;
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Simple test for a function
|
|
47
|
+
*
|
|
48
|
+
* @param {Any} value - Some object to test.
|
|
49
|
+
* @returns {Boolean} True if is a function, false otherwise.
|
|
50
|
+
*/
|
|
51
|
+
function isFunction (value) {
|
|
52
|
+
return typeof value === "function";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Simple test for an object
|
|
57
|
+
*
|
|
58
|
+
* @param {Any} value - Some object to test.
|
|
59
|
+
* @returns {Boolean} True if is an object, false otherwise.
|
|
60
|
+
*/
|
|
61
|
+
function isObject (value) {
|
|
62
|
+
const type = typeof value;
|
|
63
|
+
return value != null && type === "object";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Wrap a function so it runs one time only.
|
|
68
|
+
*
|
|
69
|
+
* @param {Function} fn - The function to run once only.
|
|
70
|
+
* @param {...any} args - The arguments.
|
|
71
|
+
* @returns {Function} To run the given function once only.
|
|
72
|
+
*/
|
|
73
|
+
function once (fn, ...args) {
|
|
74
|
+
function onceWrapper () {
|
|
75
|
+
if (!this.ran) {
|
|
76
|
+
this.ran = true;
|
|
77
|
+
return fn(...args);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
onceWrapper.ran = false;
|
|
81
|
+
return onceWrapper.bind(onceWrapper);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get the first element of an array.
|
|
86
|
+
*
|
|
87
|
+
* @param {Array} array
|
|
88
|
+
* @returns {Any} The first value of an array or undefined.
|
|
89
|
+
*/
|
|
90
|
+
function head (array) {
|
|
91
|
+
return (array && array.length) ? array[0] : undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
44
94
|
/**
|
|
45
95
|
* Prepend a message to an Error message.
|
|
46
96
|
*
|
|
@@ -103,8 +153,12 @@ function checkResponse (res, mediaTypes) {
|
|
|
103
153
|
}
|
|
104
154
|
|
|
105
155
|
module.exports = {
|
|
156
|
+
checkResponse,
|
|
106
157
|
ensure,
|
|
158
|
+
head,
|
|
107
159
|
isUrl,
|
|
108
|
-
|
|
109
|
-
|
|
160
|
+
isFunction,
|
|
161
|
+
isObject,
|
|
162
|
+
once,
|
|
163
|
+
prependMsgToErr
|
|
110
164
|
};
|
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
*/
|
|
7
7
|
const fs = require("fs");
|
|
8
8
|
const pathLib = require("path");
|
|
9
|
-
const mkdirp = require("mkdirp");
|
|
10
9
|
const common = require("../index");
|
|
11
10
|
const base = require("../../input-generators/_base");
|
|
12
11
|
|
|
@@ -19,7 +18,9 @@ const base = require("../../input-generators/_base");
|
|
|
19
18
|
function prepareWrite (outputPath, callback) {
|
|
20
19
|
const path = pathLib.parse(outputPath);
|
|
21
20
|
const dir = pathLib.join(path.root, path.dir);
|
|
22
|
-
|
|
21
|
+
fs.mkdir(dir, { recursive: true }, err => {
|
|
22
|
+
callback(err);
|
|
23
|
+
});
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
/**
|
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
* Copyright (c) 2013 - 2022 Alex Grant, LocalNerve, contributors
|
|
7
7
|
* Licensed under the MIT license.
|
|
8
8
|
*/
|
|
9
|
-
const _ = require("lodash");
|
|
10
9
|
const smc = require("./sitemap-collection");
|
|
11
10
|
|
|
12
11
|
/**
|
|
@@ -26,9 +25,7 @@ function sitemapIndexUrls (smLib, options, parseResult) {
|
|
|
26
25
|
};
|
|
27
26
|
|
|
28
27
|
// Check if we should process each sitemap in the index.
|
|
29
|
-
|
|
30
|
-
// if the sitemap index is malformed, just blow up
|
|
31
|
-
parseResult.sitemapindex.sitemap,
|
|
28
|
+
parseResult.sitemapindex?.sitemap?.forEach(
|
|
32
29
|
sitemapNode => {
|
|
33
30
|
// optionally ignore current sitemaps by sitemap policy
|
|
34
31
|
const shouldProcess = !options.sitemapPolicy ||
|
|
@@ -13,7 +13,6 @@ const util = require("util");
|
|
|
13
13
|
const path = require("path");
|
|
14
14
|
const urlm = require("url");
|
|
15
15
|
const zlib = require("zlib");
|
|
16
|
-
const _ = require("lodash");
|
|
17
16
|
const xml2js = require("xml2js");
|
|
18
17
|
const smi = require("./sitemap-index");
|
|
19
18
|
const common = require("../index");
|
|
@@ -47,8 +46,9 @@ function stillCurrent (urlNode, options) {
|
|
|
47
46
|
let lesser, greater, oPath;
|
|
48
47
|
const statOpts = {throwIfNoEntry: false};
|
|
49
48
|
const now = Date.now();
|
|
50
|
-
const lMod =
|
|
51
|
-
const cFreq =
|
|
49
|
+
const lMod = common.head(urlNode.lastmod);
|
|
50
|
+
const cFreq = common.head(urlNode.changefreq) ?
|
|
51
|
+
common.head(urlNode.changefreq).toLowerCase() : null;
|
|
52
52
|
|
|
53
53
|
// only lastmod specified
|
|
54
54
|
if (lMod && !cFreq) {
|
|
@@ -101,12 +101,11 @@ function parse (options, document, callback) {
|
|
|
101
101
|
.catch(callback);
|
|
102
102
|
} else {
|
|
103
103
|
// Process the url input, but break if base.input returns false.
|
|
104
|
-
// In other words,
|
|
104
|
+
// In other words, result.urlset.url.find is looking for a non-falsy err.
|
|
105
105
|
// For now, this can only happen if no outputDir is defined,
|
|
106
106
|
// which is a fatal bad option problem and will happen immediately.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
result.urlset.url,
|
|
107
|
+
// if sitemap malformed (result.urlset.url is not array), just blow up...
|
|
108
|
+
result.urlset.url.find(
|
|
110
109
|
urlNode => {
|
|
111
110
|
// optionally ignore current urls by sitemap policy
|
|
112
111
|
let url;
|
|
@@ -116,12 +115,15 @@ function parse (options, document, callback) {
|
|
|
116
115
|
if (process) {
|
|
117
116
|
// if sitemap is malformed, just blow up
|
|
118
117
|
url = urlm.parse(urlNode.loc[0]);
|
|
119
|
-
if (!base.input(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
118
|
+
if (!base.input(
|
|
119
|
+
{ ...options,
|
|
120
|
+
...{
|
|
121
|
+
protocol: url.protocol,
|
|
122
|
+
auth: url.auth,
|
|
123
|
+
hostname: url.hostname,
|
|
124
|
+
port: url.port
|
|
125
|
+
}
|
|
126
|
+
},
|
|
125
127
|
urlNode.loc[0])
|
|
126
128
|
) {
|
|
127
129
|
source = urlNode.loc[0];
|
package/lib/html-snapshots.js
CHANGED
|
@@ -15,7 +15,6 @@ const path = require("path");
|
|
|
15
15
|
const EventEmitter = require("events").EventEmitter;
|
|
16
16
|
const rimraf = require("rimraf").sync;
|
|
17
17
|
const asyncLib = require("async");
|
|
18
|
-
const _ = require("lodash");
|
|
19
18
|
|
|
20
19
|
const common = require("./common");
|
|
21
20
|
const inputFactory = require("./input-generators");
|
|
@@ -108,7 +107,7 @@ function phantomjsWorker (input, options, notifier, qcb) {
|
|
|
108
107
|
if (!notifier.known(input.outputFile)) {
|
|
109
108
|
|
|
110
109
|
// map snapshotScript object script to a real path
|
|
111
|
-
if (
|
|
110
|
+
if (common.isObject(options.snapshotScript)) {
|
|
112
111
|
snapshotScript = `${path.join(__dirname, phantomDir, options.snapshotScript.script)}.js`;
|
|
113
112
|
customModule = options.snapshotScript.module;
|
|
114
113
|
}
|
|
@@ -176,7 +175,7 @@ function puppeteerWorker (input, options, notifier, qcb) {
|
|
|
176
175
|
if (!notifier.known(input.outputFile)) {
|
|
177
176
|
|
|
178
177
|
// map snapshotScript object script
|
|
179
|
-
if (
|
|
178
|
+
if (common.isObject(options.snapshotScript)) {
|
|
180
179
|
snapshotScript = options.puppeteer;
|
|
181
180
|
if (options.snapshotScript.script === "removeScripts") {
|
|
182
181
|
filter = "./removeScripts";
|
|
@@ -280,7 +279,7 @@ module.exports = {
|
|
|
280
279
|
const completion = new Promise((resolve, reject) => {
|
|
281
280
|
function completionResolver (err, completed) {
|
|
282
281
|
try {
|
|
283
|
-
|
|
282
|
+
common.isFunction(listener) && listener(err, completed);
|
|
284
283
|
} catch (e) {
|
|
285
284
|
console.error("User supplied listener exception", e);
|
|
286
285
|
}
|
|
@@ -297,7 +296,7 @@ module.exports = {
|
|
|
297
296
|
|
|
298
297
|
// create a worker queue with a parallel process limit.
|
|
299
298
|
const q = asyncLib.queue(
|
|
300
|
-
(task, callback) => { task(
|
|
299
|
+
(task, callback) => { task(common.once(callback)); },
|
|
301
300
|
options.processLimit
|
|
302
301
|
);
|
|
303
302
|
|
|
@@ -313,7 +312,7 @@ module.exports = {
|
|
|
313
312
|
|
|
314
313
|
// generate input for the snapshots.
|
|
315
314
|
return inputGenerator.run(options, input => {
|
|
316
|
-
q.push(
|
|
315
|
+
q.push(worker.bind(worker, input, options, notifier));
|
|
317
316
|
}).then(() => completion)
|
|
318
317
|
}
|
|
319
318
|
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
const EventEmitter = require("events").EventEmitter;
|
|
11
11
|
const path = require("path");
|
|
12
12
|
const urlm = require("url");
|
|
13
|
-
const _ = require("lodash");
|
|
14
13
|
const common = require("../common");
|
|
15
14
|
|
|
16
15
|
// Defaults for this module
|
|
@@ -80,10 +79,11 @@ function normalize (obj) {
|
|
|
80
79
|
*/
|
|
81
80
|
function supplyMissingDefault (options, name) {
|
|
82
81
|
if (options[name]() === void 0) {
|
|
83
|
-
options[name]
|
|
84
|
-
|
|
82
|
+
const originalFn = options[name].bind(options[name]);
|
|
83
|
+
options[name] = key => {
|
|
84
|
+
const res = originalFn(key);
|
|
85
85
|
return res === void 0 ? defaults[name] : res;
|
|
86
|
-
}
|
|
86
|
+
};
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
|
|
@@ -184,7 +184,7 @@ function getOutputPath (options, page, parse) {
|
|
|
184
184
|
* @returns {String|Boolean} The full path to the output file or false on failure.
|
|
185
185
|
*/
|
|
186
186
|
function mapOutputFile (options, page, parse) {
|
|
187
|
-
if (!
|
|
187
|
+
if (!common.isFunction(options.outputPath)) {
|
|
188
188
|
options.outputPath = normalize(options.outputPath);
|
|
189
189
|
}
|
|
190
190
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "html-snapshots",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Alex Grant",
|
|
6
6
|
"email": "alex@localnerve.com",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"test": "mocha test/mocha/**/*.js --exit --recursive --reporter spec",
|
|
23
23
|
"test:async": "mocha test/mocha/async/test.js --exit --reporter spec",
|
|
24
24
|
"test:common": "mocha test/mocha/common/*.js --exit --reporter spec",
|
|
25
|
+
"test:common:debug": "mocha --inspect-brk test/mocha/common/*.js --exit --reporter spec",
|
|
25
26
|
"test:browsers": "mocha test/mocha/browsers/test.js --exit --reporter spec",
|
|
26
27
|
"test:browsers:cover": "c8 -- npm run test:browsers",
|
|
27
28
|
"test:input-generators": "mocha test/mocha/input-generators/*.js --exit --reporter spec",
|
|
@@ -56,15 +57,13 @@
|
|
|
56
57
|
"async-lock": "1.4.0",
|
|
57
58
|
"combine-errors": "3.0.3",
|
|
58
59
|
"got": "12.5.3",
|
|
59
|
-
"lodash": "4.17.21",
|
|
60
|
-
"mkdirp": "1.0.4",
|
|
61
60
|
"phantomjs-prebuilt": "2.1.16",
|
|
62
|
-
"puppeteer": "^19.
|
|
61
|
+
"puppeteer": "^19.3.0",
|
|
63
62
|
"rimraf": "3.0.2",
|
|
64
63
|
"xml2js": "0.4.23"
|
|
65
64
|
},
|
|
66
65
|
"devDependencies": {
|
|
67
|
-
"eslint": "8.
|
|
66
|
+
"eslint": "8.29.0",
|
|
68
67
|
"express": "4.18.2",
|
|
69
68
|
"mocha": "10.1.0",
|
|
70
69
|
"c8": "7.12.0",
|