@apify/utilities 2.3.1 → 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.js CHANGED
@@ -19,6 +19,10 @@ var __copyProps = (to, from, except, desc) => {
19
19
  return to;
20
20
  };
21
21
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
22
26
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
27
  mod
24
28
  ));
@@ -157,6 +161,7 @@ function betterSetInterval(func, delay) {
157
161
  }, "callback");
158
162
  funcWrapper();
159
163
  return {
164
+ // eslint-disable-next-line no-underscore-dangle
160
165
  _betterClearInterval() {
161
166
  isRunning = false;
162
167
  clearTimeout(timeoutId);
@@ -195,6 +200,7 @@ function weightedAverage(val1, weight1, val2, weight2) {
195
200
  }
196
201
  __name(weightedAverage, "weightedAverage");
197
202
  var FORBIDDEN_USERNAMES_REGEXPS = [
203
+ // Meteor app routes
198
204
  "page-not-found",
199
205
  "docs",
200
206
  "terms-of-use",
@@ -232,6 +238,7 @@ var FORBIDDEN_USERNAMES_REGEXPS = [
232
238
  "community",
233
239
  "crawlers",
234
240
  "ext",
241
+ // Various strings
235
242
  "admin",
236
243
  "administration",
237
244
  "crawler",
@@ -376,6 +383,7 @@ var FORBIDDEN_USERNAMES_REGEXPS = [
376
383
  "freelancer",
377
384
  "partner",
378
385
  "preview",
386
+ // Special files
379
387
  "index",
380
388
  "index\\.html",
381
389
  "(favicon\\.[a-z]+)",
@@ -385,11 +393,17 @@ var FORBIDDEN_USERNAMES_REGEXPS = [
385
393
  "(sitemap\\.[a-z]+)",
386
394
  "(apple-touch-icon.*)",
387
395
  "security-whitepaper\\.pdf",
396
+ // All hidden files
388
397
  "(\\..*)",
398
+ // File starting with xxx-
389
399
  "(xxx-.*)",
400
+ // Strings not starting with letter or number
390
401
  "([^0-9a-z].*)",
402
+ // Strings not ending with letter or number
391
403
  "(.*[^0-9a-z])",
404
+ // Strings where there's more than one underscore, comma or dash in row
392
405
  "(.*[_.\\-]{2}.*)",
406
+ // Reserved usernames from https://github.com/shouldbee/reserved-usernames/blob/master/reserved-usernames.json
393
407
  "0",
394
408
  "about",
395
409
  "access",
@@ -1140,7 +1154,9 @@ function parseUrl(str) {
1140
1154
  },
1141
1155
  parser: {
1142
1156
  strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
1157
+ // eslint-disable-line max-len,no-useless-escape
1143
1158
  loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
1159
+ // eslint-disable-line max-len,no-useless-escape
1144
1160
  }
1145
1161
  };
1146
1162
  const m = o.parser[o.strictMode ? "strict" : "loose"].exec(str);
@@ -1167,15 +1183,25 @@ function normalizeUrl(url, keepFragment) {
1167
1183
  if (typeof url !== "string" || !url.length) {
1168
1184
  return null;
1169
1185
  }
1170
- const urlObj = parseUrl(url.trim());
1171
- if (!urlObj.protocol || !urlObj.host) {
1186
+ let urlObj;
1187
+ try {
1188
+ urlObj = new URL(url.trim());
1189
+ } catch {
1172
1190
  return null;
1173
1191
  }
1174
- const path = urlObj.path.replace(/\/$/, "");
1175
- const params = urlObj.query ? urlObj.query.split("&").filter((param) => {
1176
- return !/^utm_/.test(param);
1177
- }).sort() : [];
1178
- return `${urlObj.protocol.trim().toLowerCase()}://${urlObj.host.trim().toLowerCase()}${path.trim()}${params.length ? `?${params.join("&").trim()}` : ""}${keepFragment && urlObj.fragment ? `#${urlObj.fragment.trim()}` : ""}`;
1192
+ const { searchParams } = urlObj;
1193
+ for (const key of [...searchParams.keys()]) {
1194
+ if (key.startsWith("utm_")) {
1195
+ searchParams.delete(key);
1196
+ }
1197
+ }
1198
+ searchParams.sort();
1199
+ const protocol = urlObj.protocol.toLowerCase();
1200
+ const host = urlObj.host.toLowerCase();
1201
+ const path = urlObj.pathname.replace(/\/$/, "");
1202
+ const search = searchParams.toString() ? `?${searchParams}` : "";
1203
+ const hash = keepFragment ? urlObj.hash : "";
1204
+ return `${protocol}//${host}${path}${search}${hash}`;
1179
1205
  }
1180
1206
  __name(normalizeUrl, "normalizeUrl");
1181
1207
  function markedSetNofollowLinks(href, title, text) {
@@ -1465,7 +1491,7 @@ var HealthChecker = class {
1465
1491
  });
1466
1492
  await collection.insertOne({
1467
1493
  _id: id,
1468
- createdAt: new Date()
1494
+ createdAt: /* @__PURE__ */ new Date()
1469
1495
  });
1470
1496
  const retrieved = await collection.findOne({ _id: id });
1471
1497
  if (!retrieved)
@@ -1524,6 +1550,8 @@ var ParseJsonlStream = class extends import_stream.Transform {
1524
1550
  }
1525
1551
  callback(null, chunk);
1526
1552
  }
1553
+ // This function is called right after stream.end() is called by the writer.
1554
+ // It just tries to process the pending chunk and returns an error if that fails.
1527
1555
  _flush(callback) {
1528
1556
  if (this.pendingChunk) {
1529
1557
  try {
@@ -1589,6 +1617,13 @@ var WebhookPayloadTemplate = class {
1589
1617
  this.payload = this.template;
1590
1618
  this.replacedVariables = [];
1591
1619
  }
1620
+ /**
1621
+ * Parse existing webhook payload template string into an object, replacing
1622
+ * template variables using the provided context.
1623
+ *
1624
+ * Parse also validates the template structure, so it can be used
1625
+ * to check validity of the template JSON and usage of allowedVariables.
1626
+ */
1592
1627
  static parse(payloadTemplate, allowedVariables = null, context = {}) {
1593
1628
  const type = typeof payloadTemplate;
1594
1629
  if (type !== "string")
@@ -1596,12 +1631,42 @@ var WebhookPayloadTemplate = class {
1596
1631
  const template = new WebhookPayloadTemplate(payloadTemplate, allowedVariables, context);
1597
1632
  return template._parse();
1598
1633
  }
1634
+ /**
1635
+ * Stringify an object into a webhook payload template.
1636
+ * Values created using `getTemplateVariable('foo.bar')`
1637
+ * will be stringified to `{{foo.bar}}` template variable.
1638
+ */
1599
1639
  static stringify(objectTemplate, replacer, indent = 2) {
1600
1640
  const type = typeof objectTemplate;
1601
1641
  if (!objectTemplate || type !== "object")
1602
1642
  throw new Error(`Cannot stringify a ${type} payload template.`);
1603
1643
  return jsonStringifyExtended(objectTemplate, replacer, indent);
1604
1644
  }
1645
+ /**
1646
+ * Produces an instance of a template variable that can be used
1647
+ * in objects and will be stringified into `{{variableName}}` syntax.
1648
+ *
1649
+ * **Example:**
1650
+ * ```js
1651
+ * const resourceVariable = WebhookPayloadTemplate.getVariable('resource');
1652
+ * const objectTemplate = {
1653
+ * foo: 'foo',
1654
+ * bar: ['bar'],
1655
+ * res: resourceVariable,
1656
+ * }
1657
+ *
1658
+ * const payloadTemplate = WebhookPayloadTemplate.stringify(objectTemplate);
1659
+ * ```
1660
+ *
1661
+ * **Produces:**
1662
+ * ```json
1663
+ * {
1664
+ * "foo": "foo",
1665
+ * "bar": ["bar"],
1666
+ * "res": {{resource}},
1667
+ * }
1668
+ * ```
1669
+ */
1605
1670
  static getVariable(variableName) {
1606
1671
  return new JsonVariable(variableName);
1607
1672
  }
@@ -1771,18 +1836,18 @@ __name(privateDecrypt, "privateDecrypt");
1771
1836
  weightedAverage
1772
1837
  });
1773
1838
  /*!
1774
- * This module contains various client-side utility and helper functions.
1839
+ * This module contains various server utility and helper functions.
1840
+ * Note that it automatically exports functions from utilities.client.js
1775
1841
  *
1776
1842
  * Author: Jan Curn (jan@apify.com)
1777
- * Copyright(c) 2016 Apify. All rights reserved.
1843
+ * Copyright(c) 2015 Apify. All rights reserved.
1778
1844
  *
1779
1845
  */
1780
1846
  /*!
1781
- * This module contains various server utility and helper functions.
1782
- * Note that it automatically exports functions from utilities.client.js
1847
+ * This module contains various client-side utility and helper functions.
1783
1848
  *
1784
1849
  * Author: Jan Curn (jan@apify.com)
1785
- * Copyright(c) 2015 Apify. All rights reserved.
1850
+ * Copyright(c) 2016 Apify. All rights reserved.
1786
1851
  *
1787
1852
  */
1788
1853
  //# sourceMappingURL=index.js.map