@apify/utilities 2.4.1 → 2.6.0
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.d.ts +1 -0
- package/index.js +84 -13
- package/index.js.map +1 -1
- package/index.mjs +81 -13
- package/index.mjs.map +1 -1
- package/package.json +2 -2
- package/url_params_utils.d.ts +15 -0
- package/webhook_payload_template.d.ts +8 -1
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -47,8 +47,10 @@ __export(src_exports, {
|
|
|
47
47
|
configureLogger: () => configureLogger,
|
|
48
48
|
cryptoRandomObjectId: () => cryptoRandomObjectId,
|
|
49
49
|
dateToString: () => dateToString,
|
|
50
|
+
decodeInput: () => decodeInput,
|
|
50
51
|
delayPromise: () => delayPromise,
|
|
51
52
|
deterministicUniqueId: () => deterministicUniqueId,
|
|
53
|
+
encodeInput: () => encodeInput,
|
|
52
54
|
escapeForBson: () => escapeForBson,
|
|
53
55
|
escapePropertyName: () => escapePropertyName,
|
|
54
56
|
escapeRegExp: () => escapeRegExp,
|
|
@@ -74,6 +76,7 @@ __export(src_exports, {
|
|
|
74
76
|
readStreamToString: () => readStreamToString,
|
|
75
77
|
removeFromArray: () => removeFromArray,
|
|
76
78
|
retryWithExpBackoff: () => retryWithExpBackoff,
|
|
79
|
+
separateImports: () => separateImports,
|
|
77
80
|
sequentializePromises: () => sequentializePromises,
|
|
78
81
|
splitFullName: () => splitFullName,
|
|
79
82
|
timeoutPromise: () => timeoutPromise,
|
|
@@ -1644,12 +1647,16 @@ var WebhookPayloadTemplate = class {
|
|
|
1644
1647
|
* Parse also validates the template structure, so it can be used
|
|
1645
1648
|
* to check validity of the template JSON and usage of allowedVariables.
|
|
1646
1649
|
*/
|
|
1647
|
-
static parse(payloadTemplate, allowedVariables = null, context = {}) {
|
|
1650
|
+
static parse(payloadTemplate, allowedVariables = null, context = {}, options = {}) {
|
|
1648
1651
|
const type = typeof payloadTemplate;
|
|
1649
1652
|
if (type !== "string")
|
|
1650
1653
|
throw new Error(`Cannot parse a ${type} payload template.`);
|
|
1651
1654
|
const template = new WebhookPayloadTemplate(payloadTemplate, allowedVariables, context);
|
|
1652
|
-
|
|
1655
|
+
const data = template._parse();
|
|
1656
|
+
if (options.interpolateStrings) {
|
|
1657
|
+
return template._interpolate(data);
|
|
1658
|
+
}
|
|
1659
|
+
return data;
|
|
1653
1660
|
}
|
|
1654
1661
|
/**
|
|
1655
1662
|
* Stringify an object into a webhook payload template.
|
|
@@ -1691,19 +1698,56 @@ var WebhookPayloadTemplate = class {
|
|
|
1691
1698
|
return new JsonVariable(variableName);
|
|
1692
1699
|
}
|
|
1693
1700
|
_parse() {
|
|
1701
|
+
let currentIndex = 0;
|
|
1694
1702
|
while (true) {
|
|
1695
1703
|
try {
|
|
1696
1704
|
return JSON.parse(this.payload);
|
|
1697
1705
|
} catch (err) {
|
|
1698
|
-
const position = this._findPositionOfNextVariable();
|
|
1699
|
-
if (position) {
|
|
1700
|
-
this._replaceVariable(position);
|
|
1701
|
-
} else {
|
|
1706
|
+
const position = this._findPositionOfNextVariable(currentIndex);
|
|
1707
|
+
if (!position) {
|
|
1702
1708
|
throw new InvalidJsonError(err);
|
|
1703
1709
|
}
|
|
1710
|
+
if (!position.isInsideString) {
|
|
1711
|
+
this._replaceVariable(position);
|
|
1712
|
+
}
|
|
1713
|
+
currentIndex = position.openBraceIndex + 1;
|
|
1704
1714
|
}
|
|
1705
1715
|
}
|
|
1706
1716
|
}
|
|
1717
|
+
_interpolate(value) {
|
|
1718
|
+
if (typeof value === "string") {
|
|
1719
|
+
return this._interpolateString(value);
|
|
1720
|
+
}
|
|
1721
|
+
if (Array.isArray(value)) {
|
|
1722
|
+
return this._interpolateArray(value);
|
|
1723
|
+
}
|
|
1724
|
+
if (typeof value === "object" && value !== null) {
|
|
1725
|
+
return this._interpolateObject(value);
|
|
1726
|
+
}
|
|
1727
|
+
return value;
|
|
1728
|
+
}
|
|
1729
|
+
_interpolateString(value) {
|
|
1730
|
+
if (value.match(/^\{\{([a-zA-Z0-9.]+)\}\}$/)) {
|
|
1731
|
+
const variableName = value.substring(2, value.length - 2);
|
|
1732
|
+
this._validateVariableName(variableName);
|
|
1733
|
+
return this._getVariableValue(variableName);
|
|
1734
|
+
}
|
|
1735
|
+
return value.replace(/\{\{([a-zA-Z0-9.]+)\}\}/g, (match, variableName) => {
|
|
1736
|
+
this._validateVariableName(variableName);
|
|
1737
|
+
const variableValue = this._getVariableValue(variableName);
|
|
1738
|
+
return `${variableValue}`;
|
|
1739
|
+
});
|
|
1740
|
+
}
|
|
1741
|
+
_interpolateObject(value) {
|
|
1742
|
+
const result = {};
|
|
1743
|
+
Object.entries(value).forEach(([key, v]) => {
|
|
1744
|
+
result[key] = this._interpolate(v);
|
|
1745
|
+
});
|
|
1746
|
+
return result;
|
|
1747
|
+
}
|
|
1748
|
+
_interpolateArray(value) {
|
|
1749
|
+
return value.map(this._interpolate.bind(this));
|
|
1750
|
+
}
|
|
1707
1751
|
_findPositionOfNextVariable(startIndex = 0) {
|
|
1708
1752
|
const openBraceIndex = this.payload.indexOf("{{", startIndex);
|
|
1709
1753
|
const closeBraceIndex = this.payload.indexOf("}}", openBraceIndex) + 1;
|
|
@@ -1711,9 +1755,7 @@ var WebhookPayloadTemplate = class {
|
|
|
1711
1755
|
if (!someVariableMaybeExists)
|
|
1712
1756
|
return null;
|
|
1713
1757
|
const isInsideString = this._isVariableInsideString(openBraceIndex);
|
|
1714
|
-
|
|
1715
|
-
return { openBraceIndex, closeBraceIndex };
|
|
1716
|
-
return this._findPositionOfNextVariable(openBraceIndex + 1);
|
|
1758
|
+
return { isInsideString, openBraceIndex, closeBraceIndex };
|
|
1717
1759
|
}
|
|
1718
1760
|
_isVariableInsideString(openBraceIndex) {
|
|
1719
1761
|
const unescapedQuoteCount = this._countUnescapedDoubleQuotesUpToIndex(openBraceIndex);
|
|
@@ -1736,7 +1778,7 @@ var WebhookPayloadTemplate = class {
|
|
|
1736
1778
|
this._validateVariableName(variableName);
|
|
1737
1779
|
const replacement = this._getVariableReplacement(variableName);
|
|
1738
1780
|
this.replacedVariables.push({ variableName, replacement });
|
|
1739
|
-
this.payload = this.payload.
|
|
1781
|
+
this.payload = this.payload.substring(0, openBraceIndex) + replacement + this.payload.substring(closeBraceIndex + 1);
|
|
1740
1782
|
}
|
|
1741
1783
|
_validateVariableName(variableName) {
|
|
1742
1784
|
if (this.allowedVariables === null)
|
|
@@ -1746,15 +1788,19 @@ var WebhookPayloadTemplate = class {
|
|
|
1746
1788
|
if (!isVariableValid)
|
|
1747
1789
|
throw new InvalidVariableError(variableName);
|
|
1748
1790
|
}
|
|
1749
|
-
|
|
1791
|
+
_getVariableValue(variableName) {
|
|
1750
1792
|
const [variable, ...properties] = variableName.split(".");
|
|
1751
1793
|
const context = this.context[variable];
|
|
1752
|
-
const
|
|
1794
|
+
const value = properties.reduce((ctx, prop) => {
|
|
1753
1795
|
if (!ctx || typeof ctx !== "object")
|
|
1754
1796
|
return null;
|
|
1755
1797
|
return ctx[prop];
|
|
1756
1798
|
}, context);
|
|
1757
|
-
return
|
|
1799
|
+
return value;
|
|
1800
|
+
}
|
|
1801
|
+
_getVariableReplacement(variableName) {
|
|
1802
|
+
const value = this._getVariableValue(variableName);
|
|
1803
|
+
return value ? JSON.stringify(value) : null;
|
|
1758
1804
|
}
|
|
1759
1805
|
};
|
|
1760
1806
|
__name(WebhookPayloadTemplate, "WebhookPayloadTemplate");
|
|
@@ -1801,6 +1847,28 @@ function privateDecrypt({
|
|
|
1801
1847
|
return Buffer.concat([decipher.update(encryptedDataBuffer), decipher.final()]).toString("utf-8");
|
|
1802
1848
|
}
|
|
1803
1849
|
__name(privateDecrypt, "privateDecrypt");
|
|
1850
|
+
|
|
1851
|
+
// src/url_params_utils.ts
|
|
1852
|
+
function encodeInput(input) {
|
|
1853
|
+
const data = JSON.stringify(input);
|
|
1854
|
+
const buffer = Buffer.from(data, "utf8");
|
|
1855
|
+
return buffer.toString("base64url");
|
|
1856
|
+
}
|
|
1857
|
+
__name(encodeInput, "encodeInput");
|
|
1858
|
+
function decodeInput(urlHash) {
|
|
1859
|
+
const buffer = Buffer.from(urlHash, "base64url");
|
|
1860
|
+
const decoded = buffer.toString("utf8");
|
|
1861
|
+
return JSON.parse(decoded);
|
|
1862
|
+
}
|
|
1863
|
+
__name(decodeInput, "decodeInput");
|
|
1864
|
+
function separateImports(code) {
|
|
1865
|
+
const lines = code.split("\n");
|
|
1866
|
+
return {
|
|
1867
|
+
code: lines.filter((line) => !line.trim().startsWith("import")).join("\n"),
|
|
1868
|
+
imports: lines.filter((line) => line.trim().startsWith("import")).join("\n")
|
|
1869
|
+
};
|
|
1870
|
+
}
|
|
1871
|
+
__name(separateImports, "separateImports");
|
|
1804
1872
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1805
1873
|
0 && (module.exports = {
|
|
1806
1874
|
CHECK_TYPES,
|
|
@@ -1819,8 +1887,10 @@ __name(privateDecrypt, "privateDecrypt");
|
|
|
1819
1887
|
configureLogger,
|
|
1820
1888
|
cryptoRandomObjectId,
|
|
1821
1889
|
dateToString,
|
|
1890
|
+
decodeInput,
|
|
1822
1891
|
delayPromise,
|
|
1823
1892
|
deterministicUniqueId,
|
|
1893
|
+
encodeInput,
|
|
1824
1894
|
escapeForBson,
|
|
1825
1895
|
escapePropertyName,
|
|
1826
1896
|
escapeRegExp,
|
|
@@ -1846,6 +1916,7 @@ __name(privateDecrypt, "privateDecrypt");
|
|
|
1846
1916
|
readStreamToString,
|
|
1847
1917
|
removeFromArray,
|
|
1848
1918
|
retryWithExpBackoff,
|
|
1919
|
+
separateImports,
|
|
1849
1920
|
sequentializePromises,
|
|
1850
1921
|
splitFullName,
|
|
1851
1922
|
timeoutPromise,
|