@apify/utilities 2.3.2 → 2.3.4

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,8 @@ var FORBIDDEN_USERNAMES_REGEXPS = [
376
383
  "freelancer",
377
384
  "partner",
378
385
  "preview",
386
+ "templates",
387
+ // Special files
379
388
  "index",
380
389
  "index\\.html",
381
390
  "(favicon\\.[a-z]+)",
@@ -385,11 +394,17 @@ var FORBIDDEN_USERNAMES_REGEXPS = [
385
394
  "(sitemap\\.[a-z]+)",
386
395
  "(apple-touch-icon.*)",
387
396
  "security-whitepaper\\.pdf",
397
+ // All hidden files
388
398
  "(\\..*)",
399
+ // File starting with xxx-
389
400
  "(xxx-.*)",
401
+ // Strings not starting with letter or number
390
402
  "([^0-9a-z].*)",
403
+ // Strings not ending with letter or number
391
404
  "(.*[^0-9a-z])",
405
+ // Strings where there's more than one underscore, comma or dash in row
392
406
  "(.*[_.\\-]{2}.*)",
407
+ // Reserved usernames from https://github.com/shouldbee/reserved-usernames/blob/master/reserved-usernames.json
393
408
  "0",
394
409
  "about",
395
410
  "access",
@@ -1140,7 +1155,9 @@ function parseUrl(str) {
1140
1155
  },
1141
1156
  parser: {
1142
1157
  strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
1158
+ // eslint-disable-line max-len,no-useless-escape
1143
1159
  loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
1160
+ // eslint-disable-line max-len,no-useless-escape
1144
1161
  }
1145
1162
  };
1146
1163
  const m = o.parser[o.strictMode ? "strict" : "loose"].exec(str);
@@ -1167,15 +1184,25 @@ function normalizeUrl(url, keepFragment) {
1167
1184
  if (typeof url !== "string" || !url.length) {
1168
1185
  return null;
1169
1186
  }
1170
- const urlObj = parseUrl(url.trim());
1171
- if (!urlObj.protocol || !urlObj.host) {
1187
+ let urlObj;
1188
+ try {
1189
+ urlObj = new URL(url.trim());
1190
+ } catch {
1172
1191
  return null;
1173
1192
  }
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()}` : ""}`;
1193
+ const { searchParams } = urlObj;
1194
+ for (const key of [...searchParams.keys()]) {
1195
+ if (key.startsWith("utm_")) {
1196
+ searchParams.delete(key);
1197
+ }
1198
+ }
1199
+ searchParams.sort();
1200
+ const protocol = urlObj.protocol.toLowerCase();
1201
+ const host = urlObj.host.toLowerCase();
1202
+ const path = urlObj.pathname.replace(/\/$/, "");
1203
+ const search = searchParams.toString() ? `?${searchParams}` : "";
1204
+ const hash = keepFragment ? urlObj.hash : "";
1205
+ return `${protocol}//${host}${path}${search}${hash}`;
1179
1206
  }
1180
1207
  __name(normalizeUrl, "normalizeUrl");
1181
1208
  function markedSetNofollowLinks(href, title, text) {
@@ -1465,7 +1492,7 @@ var HealthChecker = class {
1465
1492
  });
1466
1493
  await collection.insertOne({
1467
1494
  _id: id,
1468
- createdAt: new Date()
1495
+ createdAt: /* @__PURE__ */ new Date()
1469
1496
  });
1470
1497
  const retrieved = await collection.findOne({ _id: id });
1471
1498
  if (!retrieved)
@@ -1524,6 +1551,8 @@ var ParseJsonlStream = class extends import_stream.Transform {
1524
1551
  }
1525
1552
  callback(null, chunk);
1526
1553
  }
1554
+ // This function is called right after stream.end() is called by the writer.
1555
+ // It just tries to process the pending chunk and returns an error if that fails.
1527
1556
  _flush(callback) {
1528
1557
  if (this.pendingChunk) {
1529
1558
  try {
@@ -1589,6 +1618,13 @@ var WebhookPayloadTemplate = class {
1589
1618
  this.payload = this.template;
1590
1619
  this.replacedVariables = [];
1591
1620
  }
1621
+ /**
1622
+ * Parse existing webhook payload template string into an object, replacing
1623
+ * template variables using the provided context.
1624
+ *
1625
+ * Parse also validates the template structure, so it can be used
1626
+ * to check validity of the template JSON and usage of allowedVariables.
1627
+ */
1592
1628
  static parse(payloadTemplate, allowedVariables = null, context = {}) {
1593
1629
  const type = typeof payloadTemplate;
1594
1630
  if (type !== "string")
@@ -1596,12 +1632,42 @@ var WebhookPayloadTemplate = class {
1596
1632
  const template = new WebhookPayloadTemplate(payloadTemplate, allowedVariables, context);
1597
1633
  return template._parse();
1598
1634
  }
1635
+ /**
1636
+ * Stringify an object into a webhook payload template.
1637
+ * Values created using `getTemplateVariable('foo.bar')`
1638
+ * will be stringified to `{{foo.bar}}` template variable.
1639
+ */
1599
1640
  static stringify(objectTemplate, replacer, indent = 2) {
1600
1641
  const type = typeof objectTemplate;
1601
1642
  if (!objectTemplate || type !== "object")
1602
1643
  throw new Error(`Cannot stringify a ${type} payload template.`);
1603
1644
  return jsonStringifyExtended(objectTemplate, replacer, indent);
1604
1645
  }
1646
+ /**
1647
+ * Produces an instance of a template variable that can be used
1648
+ * in objects and will be stringified into `{{variableName}}` syntax.
1649
+ *
1650
+ * **Example:**
1651
+ * ```js
1652
+ * const resourceVariable = WebhookPayloadTemplate.getVariable('resource');
1653
+ * const objectTemplate = {
1654
+ * foo: 'foo',
1655
+ * bar: ['bar'],
1656
+ * res: resourceVariable,
1657
+ * }
1658
+ *
1659
+ * const payloadTemplate = WebhookPayloadTemplate.stringify(objectTemplate);
1660
+ * ```
1661
+ *
1662
+ * **Produces:**
1663
+ * ```json
1664
+ * {
1665
+ * "foo": "foo",
1666
+ * "bar": ["bar"],
1667
+ * "res": {{resource}},
1668
+ * }
1669
+ * ```
1670
+ */
1605
1671
  static getVariable(variableName) {
1606
1672
  return new JsonVariable(variableName);
1607
1673
  }
@@ -1771,18 +1837,18 @@ __name(privateDecrypt, "privateDecrypt");
1771
1837
  weightedAverage
1772
1838
  });
1773
1839
  /*!
1774
- * This module contains various client-side utility and helper functions.
1840
+ * This module contains various server utility and helper functions.
1841
+ * Note that it automatically exports functions from utilities.client.js
1775
1842
  *
1776
1843
  * Author: Jan Curn (jan@apify.com)
1777
- * Copyright(c) 2016 Apify. All rights reserved.
1844
+ * Copyright(c) 2015 Apify. All rights reserved.
1778
1845
  *
1779
1846
  */
1780
1847
  /*!
1781
- * This module contains various server utility and helper functions.
1782
- * Note that it automatically exports functions from utilities.client.js
1848
+ * This module contains various client-side utility and helper functions.
1783
1849
  *
1784
1850
  * Author: Jan Curn (jan@apify.com)
1785
- * Copyright(c) 2015 Apify. All rights reserved.
1851
+ * Copyright(c) 2016 Apify. All rights reserved.
1786
1852
  *
1787
1853
  */
1788
1854
  //# sourceMappingURL=index.js.map