@uxf/scripts 1.5.5 → 1.6.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/.gitlab-ci.yml CHANGED
@@ -2,7 +2,7 @@ stages:
2
2
  - release
3
3
 
4
4
  release:
5
- image: node:16.9.1-buster-slim
5
+ image: node:18-buster-slim
6
6
  stage: release
7
7
  tags:
8
8
  - pluto-docker
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/scripts",
3
- "version": "1.5.5",
3
+ "version": "1.6.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
package/src/Sitemap.js CHANGED
@@ -27,6 +27,28 @@ const axios = create({
27
27
  withCredentials: true,
28
28
  maxRedirects: 0,
29
29
  timeout: 20000,
30
+ headers: {
31
+ 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
32
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
33
+ 'Accept-Encoding': 'gzip, deflate, br',
34
+ 'Accept-Language': 'en-US,en;q=0.9',
35
+ 'Cache-Control': 'no-cache',
36
+ 'Connection': 'keep-alive',
37
+ 'DNT': '1',
38
+ 'Pragma': 'no-cache',
39
+ 'Sec-Ch-Ua': '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
40
+ 'Sec-Ch-Ua-Arch': '"x86"',
41
+ 'Sec-Ch-Ua-Mobile': '?0',
42
+ 'Sec-Ch-Ua-Platform': '"Windows"',
43
+ 'Sec-Fetch-Dest': 'document',
44
+ 'Sec-Fetch-Mode': 'navigate',
45
+ 'Sec-Fetch-Site': 'cross-site',
46
+ 'Sec-Fetch-User': '?1',
47
+ 'Sec-Fetch-User-Agent': '?1',
48
+ 'TE': 'Trailers',
49
+ 'Upgrade-Insecure-Requests': '1',
50
+ 'X-Requested-With': 'XMLHttpRequest',
51
+ },
30
52
  });
31
53
 
32
54
  module.exports = {
@@ -8,7 +8,7 @@ const GoogleChat = require("../GoogleChat");
8
8
 
9
9
  /**
10
10
  *
11
- * @typedef {{parentUrl: (string | undefined), isImg: boolean, time: number, ttl: number, url: string, status: number, message: (string | undefined)}} UrlCheckResponse
11
+ * @typedef {{parentUrl: (string | undefined), isImg: boolean, time: number, ttl: number, url: string, status: number, message: (string | undefined), shouldIgnoreError: (boolean | undefined)}} UrlCheckResponse
12
12
  */
13
13
 
14
14
  const MAX_TTL = 3;
@@ -34,6 +34,24 @@ function isImageUrl(url) {
34
34
  return new RegExp("[a-z].*(\\.jpg|\\.png|\\.webp|\\.avif|\\.gif)$", "gim").test(url);
35
35
  }
36
36
 
37
+ /**
38
+ *
39
+ * @param url {string}
40
+ * @param status {number}
41
+ * @param e {Error}
42
+ * @returns {boolean}
43
+ */
44
+ function shouldIgnoreError(url, status, e) {
45
+ if (status === 999 && url.startsWith("https://www.linkedin.com")) {
46
+ return true;
47
+ }
48
+ if ((status === -1 || status === 302) && url.startsWith("https://www.facebook.com/sharer/")) {
49
+ return true;
50
+ }
51
+
52
+ return false;
53
+ }
54
+
37
55
  /**
38
56
  *
39
57
  * @param errors {UrlCheckResponse[]}
@@ -109,14 +127,15 @@ function createCorrectLinks(incorrectLinks, webUrl) {
109
127
  */
110
128
  async function fetchUrl(url, parentUrl = undefined, ttl = 1) {
111
129
  try {
112
- Sitemap.axios.defaults.maxRedirects = parentUrl ? 1 : 0;
130
+ Sitemap.axios.defaults.maxRedirects = parentUrl ? 3 : 0;
131
+ const encodedUrl = parentUrl ? encodeURI(url) : url;
113
132
 
114
133
  const t0 = performance.now();
115
- const { status } = await Sitemap.axios.get(url);
134
+ const { status } = await Sitemap.axios.get(encodedUrl);
116
135
  const t1 = performance.now();
117
136
 
118
137
  if (status !== 200 && ttl < MAX_TTL) {
119
- return await fetchUrl(url, parentUrl, ttl + 1);
138
+ return await fetchUrl(encodedUrl, parentUrl, ttl + 1);
120
139
  }
121
140
 
122
141
  return {
@@ -128,14 +147,28 @@ async function fetchUrl(url, parentUrl = undefined, ttl = 1) {
128
147
  time: Math.ceil(t1 - t0),
129
148
  };
130
149
  } catch (e) {
150
+ const status = Number.parseInt((e && e.response && e.response.status) || -1, 10);
151
+
152
+ if (shouldIgnoreError(url, status, e)) {
153
+ return {
154
+ url,
155
+ parentUrl,
156
+ isImg: isImageUrl(url),
157
+ ttl,
158
+ status,
159
+ shouldIgnoreError: true,
160
+ time: 0,
161
+ };
162
+ }
131
163
  return {
132
164
  url,
133
165
  parentUrl,
134
166
  isImg: isImageUrl(url),
135
167
  ttl,
136
- status: Number.parseInt((e && e.response && e.response.status) || -1, 10),
168
+ status,
137
169
  time: 0,
138
170
  message: e.message,
171
+ shouldIgnoreError: false,
139
172
  };
140
173
  }
141
174
  }
@@ -251,9 +284,10 @@ function printUrlResult(result) {
251
284
  /**
252
285
  *
253
286
  * @param errorText {string}
287
+ * @param title {string}
254
288
  */
255
- function logErrors(errorText) {
256
- stdout.write("\nErrors:\n");
289
+ function logErrors(errorText, title) {
290
+ stdout.write(title);
257
291
  stdout.write(`${errorText}\n\n`);
258
292
  }
259
293
 
@@ -275,8 +309,8 @@ function convertTime(millis) {
275
309
  */
276
310
  function logStatistics(okResults, time) {
277
311
  const avgTime = Math.round(okResults.reduce((prev, curr) => prev + curr.time, 0) / TESTED_URLS.length);
278
- const maxTime = okResults.reduce((prev, curr) => (curr.time > prev.time ? curr : prev));
279
- const minTime = okResults.reduce((prev, curr) => (curr.time < prev.time ? curr : prev));
312
+ const maxTime = okResults.reduce((prev, curr) => (curr.time > prev.time ? curr : prev), []);
313
+ const minTime = okResults.reduce((prev, curr) => (curr.time < prev.time ? curr : prev), []);
280
314
 
281
315
  stdout.write("\nSummary:\n");
282
316
  stdout.write(createTabSpace() + `Time ${convertTime(time)}\n`);
@@ -350,15 +384,24 @@ module.exports = async function run(sitemapUrl, webUrl, slackChannel, skip, test
350
384
  await testSitemapUrls((await Sitemap.getSitemap(sitemapUrl)), webUrl, sitemapUrl, skip, testNested);
351
385
  const finishTime = performance.now();
352
386
 
353
- const errors = TESTED_URLS.filter(r => r.status !== 200);
387
+ const errors = TESTED_URLS.filter(r => r.status !== 200 && r.shouldIgnoreError === false);
388
+ const ignoredErrors = TESTED_URLS.filter(r => r.status !== 200 && r.shouldIgnoreError === true);
354
389
  const ok = TESTED_URLS.filter(r => r.status === 200);
355
390
 
356
391
  if (errors.length > 0) {
357
392
  const errorText = createErrorResult(errors);
358
- logErrors(errorText);
393
+ logErrors(errorText, "\nErrors:\n");
394
+ const ignoredErrorText = createErrorResult(ignoredErrors);
395
+ logErrors(ignoredErrorText, "\nIngored errors:\n");
359
396
  await sendSlackMessage(errorText, slackChannel);
360
- await sendGoogleChatMessage(errors);
397
+ await sendGoogleChatMessage(errorText);
361
398
  }
399
+
400
+ if (ignoredErrors.length > 0) {
401
+ const ignoredErrorText = createErrorResult(ignoredErrors);
402
+ logErrors(ignoredErrorText, "\nIngored errors:\n");
403
+ }
404
+
362
405
  logStatistics(ok, Math.ceil(finishTime - startTime));
363
406
 
364
407
  process.exit(errors.length > 0 ? 1 : 0);