@apify/utilities 2.3.2 → 2.3.3

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/index.mjs CHANGED
@@ -77,6 +77,7 @@ function betterSetInterval(func, delay) {
77
77
  }, "callback");
78
78
  funcWrapper();
79
79
  return {
80
+ // eslint-disable-next-line no-underscore-dangle
80
81
  _betterClearInterval() {
81
82
  isRunning = false;
82
83
  clearTimeout(timeoutId);
@@ -115,6 +116,7 @@ function weightedAverage(val1, weight1, val2, weight2) {
115
116
  }
116
117
  __name(weightedAverage, "weightedAverage");
117
118
  var FORBIDDEN_USERNAMES_REGEXPS = [
119
+ // Meteor app routes
118
120
  "page-not-found",
119
121
  "docs",
120
122
  "terms-of-use",
@@ -152,6 +154,7 @@ var FORBIDDEN_USERNAMES_REGEXPS = [
152
154
  "community",
153
155
  "crawlers",
154
156
  "ext",
157
+ // Various strings
155
158
  "admin",
156
159
  "administration",
157
160
  "crawler",
@@ -296,6 +299,7 @@ var FORBIDDEN_USERNAMES_REGEXPS = [
296
299
  "freelancer",
297
300
  "partner",
298
301
  "preview",
302
+ // Special files
299
303
  "index",
300
304
  "index\\.html",
301
305
  "(favicon\\.[a-z]+)",
@@ -305,11 +309,17 @@ var FORBIDDEN_USERNAMES_REGEXPS = [
305
309
  "(sitemap\\.[a-z]+)",
306
310
  "(apple-touch-icon.*)",
307
311
  "security-whitepaper\\.pdf",
312
+ // All hidden files
308
313
  "(\\..*)",
314
+ // File starting with xxx-
309
315
  "(xxx-.*)",
316
+ // Strings not starting with letter or number
310
317
  "([^0-9a-z].*)",
318
+ // Strings not ending with letter or number
311
319
  "(.*[^0-9a-z])",
320
+ // Strings where there's more than one underscore, comma or dash in row
312
321
  "(.*[_.\\-]{2}.*)",
322
+ // Reserved usernames from https://github.com/shouldbee/reserved-usernames/blob/master/reserved-usernames.json
313
323
  "0",
314
324
  "about",
315
325
  "access",
@@ -1060,7 +1070,9 @@ function parseUrl(str) {
1060
1070
  },
1061
1071
  parser: {
1062
1072
  strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
1073
+ // eslint-disable-line max-len,no-useless-escape
1063
1074
  loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
1075
+ // eslint-disable-line max-len,no-useless-escape
1064
1076
  }
1065
1077
  };
1066
1078
  const m = o.parser[o.strictMode ? "strict" : "loose"].exec(str);
@@ -1087,15 +1099,25 @@ function normalizeUrl(url, keepFragment) {
1087
1099
  if (typeof url !== "string" || !url.length) {
1088
1100
  return null;
1089
1101
  }
1090
- const urlObj = parseUrl(url.trim());
1091
- if (!urlObj.protocol || !urlObj.host) {
1102
+ let urlObj;
1103
+ try {
1104
+ urlObj = new URL(url.trim());
1105
+ } catch {
1092
1106
  return null;
1093
1107
  }
1094
- const path = urlObj.path.replace(/\/$/, "");
1095
- const params = urlObj.query ? urlObj.query.split("&").filter((param) => {
1096
- return !/^utm_/.test(param);
1097
- }).sort() : [];
1098
- return `${urlObj.protocol.trim().toLowerCase()}://${urlObj.host.trim().toLowerCase()}${path.trim()}${params.length ? `?${params.join("&").trim()}` : ""}${keepFragment && urlObj.fragment ? `#${urlObj.fragment.trim()}` : ""}`;
1108
+ const { searchParams } = urlObj;
1109
+ for (const key of [...searchParams.keys()]) {
1110
+ if (key.startsWith("utm_")) {
1111
+ searchParams.delete(key);
1112
+ }
1113
+ }
1114
+ searchParams.sort();
1115
+ const protocol = urlObj.protocol.toLowerCase();
1116
+ const host = urlObj.host.toLowerCase();
1117
+ const path = urlObj.pathname.replace(/\/$/, "");
1118
+ const search = searchParams.toString() ? `?${searchParams}` : "";
1119
+ const hash = keepFragment ? urlObj.hash : "";
1120
+ return `${protocol}//${host}${path}${search}${hash}`;
1099
1121
  }
1100
1122
  __name(normalizeUrl, "normalizeUrl");
1101
1123
  function markedSetNofollowLinks(href, title, text) {
@@ -1385,7 +1407,7 @@ var HealthChecker = class {
1385
1407
  });
1386
1408
  await collection.insertOne({
1387
1409
  _id: id,
1388
- createdAt: new Date()
1410
+ createdAt: /* @__PURE__ */ new Date()
1389
1411
  });
1390
1412
  const retrieved = await collection.findOne({ _id: id });
1391
1413
  if (!retrieved)
@@ -1444,6 +1466,8 @@ var ParseJsonlStream = class extends Transform {
1444
1466
  }
1445
1467
  callback(null, chunk);
1446
1468
  }
1469
+ // This function is called right after stream.end() is called by the writer.
1470
+ // It just tries to process the pending chunk and returns an error if that fails.
1447
1471
  _flush(callback) {
1448
1472
  if (this.pendingChunk) {
1449
1473
  try {
@@ -1509,6 +1533,13 @@ var WebhookPayloadTemplate = class {
1509
1533
  this.payload = this.template;
1510
1534
  this.replacedVariables = [];
1511
1535
  }
1536
+ /**
1537
+ * Parse existing webhook payload template string into an object, replacing
1538
+ * template variables using the provided context.
1539
+ *
1540
+ * Parse also validates the template structure, so it can be used
1541
+ * to check validity of the template JSON and usage of allowedVariables.
1542
+ */
1512
1543
  static parse(payloadTemplate, allowedVariables = null, context = {}) {
1513
1544
  const type = typeof payloadTemplate;
1514
1545
  if (type !== "string")
@@ -1516,12 +1547,42 @@ var WebhookPayloadTemplate = class {
1516
1547
  const template = new WebhookPayloadTemplate(payloadTemplate, allowedVariables, context);
1517
1548
  return template._parse();
1518
1549
  }
1550
+ /**
1551
+ * Stringify an object into a webhook payload template.
1552
+ * Values created using `getTemplateVariable('foo.bar')`
1553
+ * will be stringified to `{{foo.bar}}` template variable.
1554
+ */
1519
1555
  static stringify(objectTemplate, replacer, indent = 2) {
1520
1556
  const type = typeof objectTemplate;
1521
1557
  if (!objectTemplate || type !== "object")
1522
1558
  throw new Error(`Cannot stringify a ${type} payload template.`);
1523
1559
  return jsonStringifyExtended(objectTemplate, replacer, indent);
1524
1560
  }
1561
+ /**
1562
+ * Produces an instance of a template variable that can be used
1563
+ * in objects and will be stringified into `{{variableName}}` syntax.
1564
+ *
1565
+ * **Example:**
1566
+ * ```js
1567
+ * const resourceVariable = WebhookPayloadTemplate.getVariable('resource');
1568
+ * const objectTemplate = {
1569
+ * foo: 'foo',
1570
+ * bar: ['bar'],
1571
+ * res: resourceVariable,
1572
+ * }
1573
+ *
1574
+ * const payloadTemplate = WebhookPayloadTemplate.stringify(objectTemplate);
1575
+ * ```
1576
+ *
1577
+ * **Produces:**
1578
+ * ```json
1579
+ * {
1580
+ * "foo": "foo",
1581
+ * "bar": ["bar"],
1582
+ * "res": {{resource}},
1583
+ * }
1584
+ * ```
1585
+ */
1525
1586
  static getVariable(variableName) {
1526
1587
  return new JsonVariable(variableName);
1527
1588
  }
@@ -1690,18 +1751,18 @@ export {
1690
1751
  weightedAverage
1691
1752
  };
1692
1753
  /*!
1693
- * This module contains various client-side utility and helper functions.
1754
+ * This module contains various server utility and helper functions.
1755
+ * Note that it automatically exports functions from utilities.client.js
1694
1756
  *
1695
1757
  * Author: Jan Curn (jan@apify.com)
1696
- * Copyright(c) 2016 Apify. All rights reserved.
1758
+ * Copyright(c) 2015 Apify. All rights reserved.
1697
1759
  *
1698
1760
  */
1699
1761
  /*!
1700
- * This module contains various server utility and helper functions.
1701
- * Note that it automatically exports functions from utilities.client.js
1762
+ * This module contains various client-side utility and helper functions.
1702
1763
  *
1703
1764
  * Author: Jan Curn (jan@apify.com)
1704
- * Copyright(c) 2015 Apify. All rights reserved.
1765
+ * Copyright(c) 2016 Apify. All rights reserved.
1705
1766
  *
1706
1767
  */
1707
1768
  //# sourceMappingURL=index.mjs.map