@apify/utilities 2.7.10 → 2.8.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/code_hash_manager.d.ts +34 -0
- package/index.d.ts +1 -0
- package/index.js +63 -34
- package/index.js.map +1 -1
- package/index.mjs +61 -32
- package/index.mjs.map +1 -1
- package/package.json +2 -2
- package/url_params_utils.d.ts +0 -8
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare enum CodeHashMetaKey {
|
|
2
|
+
VERSION = "v",
|
|
3
|
+
USER = "u"
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Allows hashing of an Actor input together with some metadata into a shareable link for the "Run on Apify" button.
|
|
7
|
+
* Uses a common secret for checking the signatures.
|
|
8
|
+
*
|
|
9
|
+
* The hash consists of 3 parts separated by a dot, as in `ABC.DEF.GHI`, each being a base64url encoded string:
|
|
10
|
+
* - `meta` object with the `version` and `user` properties.
|
|
11
|
+
* - `data` data object (the one that gets encoded)
|
|
12
|
+
* - `signature` used for verification of the URL hash, computed from the `meta` and `data` objects
|
|
13
|
+
*/
|
|
14
|
+
export declare class CodeHashManager {
|
|
15
|
+
private readonly secret;
|
|
16
|
+
static readonly SECTION_SEPARATOR = ".";
|
|
17
|
+
static readonly VERSION = 1;
|
|
18
|
+
constructor(secret: string);
|
|
19
|
+
/**
|
|
20
|
+
* Encodes object (e.g. input for actor) to a string hash and uses the `secret` to sign the hash.
|
|
21
|
+
*/
|
|
22
|
+
encode<T extends object>(data: T, user: string): string;
|
|
23
|
+
decode(urlHash: string): {
|
|
24
|
+
data: any;
|
|
25
|
+
meta: {
|
|
26
|
+
user: any;
|
|
27
|
+
version: any;
|
|
28
|
+
validSignature: boolean;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
private toBase64;
|
|
32
|
+
private fromBase64;
|
|
33
|
+
private generateSignature;
|
|
34
|
+
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -32,6 +32,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
32
32
|
var src_exports = {};
|
|
33
33
|
__export(src_exports, {
|
|
34
34
|
CHECK_TYPES: () => CHECK_TYPES,
|
|
35
|
+
CodeHashManager: () => CodeHashManager,
|
|
36
|
+
CodeHashMetaKey: () => CodeHashMetaKey,
|
|
35
37
|
HealthChecker: () => HealthChecker,
|
|
36
38
|
InvalidJsonError: () => InvalidJsonError,
|
|
37
39
|
InvalidVariableError: () => InvalidVariableError,
|
|
@@ -47,10 +49,8 @@ __export(src_exports, {
|
|
|
47
49
|
configureLogger: () => configureLogger,
|
|
48
50
|
cryptoRandomObjectId: () => cryptoRandomObjectId,
|
|
49
51
|
dateToString: () => dateToString,
|
|
50
|
-
decodeInput: () => decodeInput,
|
|
51
52
|
delayPromise: () => delayPromise,
|
|
52
53
|
deterministicUniqueId: () => deterministicUniqueId,
|
|
53
|
-
encodeInput: () => encodeInput,
|
|
54
54
|
escapeForBson: () => escapeForBson,
|
|
55
55
|
escapePropertyName: () => escapePropertyName,
|
|
56
56
|
escapeRegExp: () => escapeRegExp,
|
|
@@ -1857,36 +1857,6 @@ function privateDecrypt({
|
|
|
1857
1857
|
__name(privateDecrypt, "privateDecrypt");
|
|
1858
1858
|
|
|
1859
1859
|
// src/url_params_utils.ts
|
|
1860
|
-
function base64urlToBase64(input) {
|
|
1861
|
-
input = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
1862
|
-
const pad = input.length % 4;
|
|
1863
|
-
if (pad) {
|
|
1864
|
-
if (pad === 1) {
|
|
1865
|
-
throw new Error("InvalidLengthError: Input base64url string is the wrong length to determine padding");
|
|
1866
|
-
}
|
|
1867
|
-
input += new Array(5 - pad).join("=");
|
|
1868
|
-
}
|
|
1869
|
-
return input;
|
|
1870
|
-
}
|
|
1871
|
-
__name(base64urlToBase64, "base64urlToBase64");
|
|
1872
|
-
function base64ToBase64Url(input) {
|
|
1873
|
-
return input.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/m, "");
|
|
1874
|
-
}
|
|
1875
|
-
__name(base64ToBase64Url, "base64ToBase64Url");
|
|
1876
|
-
function encodeInput(input) {
|
|
1877
|
-
const data = JSON.stringify(input);
|
|
1878
|
-
const buffer = Buffer.from(data, "utf8");
|
|
1879
|
-
const base64 = buffer.toString("base64");
|
|
1880
|
-
return base64ToBase64Url(base64);
|
|
1881
|
-
}
|
|
1882
|
-
__name(encodeInput, "encodeInput");
|
|
1883
|
-
function decodeInput(urlHash) {
|
|
1884
|
-
const base64 = base64urlToBase64(urlHash);
|
|
1885
|
-
const buffer = Buffer.from(base64, "base64");
|
|
1886
|
-
const decoded = buffer.toString("utf8");
|
|
1887
|
-
return JSON.parse(decoded);
|
|
1888
|
-
}
|
|
1889
|
-
__name(decodeInput, "decodeInput");
|
|
1890
1860
|
function separateImports(code) {
|
|
1891
1861
|
const lines = code.split("\n");
|
|
1892
1862
|
return {
|
|
@@ -1895,9 +1865,70 @@ function separateImports(code) {
|
|
|
1895
1865
|
};
|
|
1896
1866
|
}
|
|
1897
1867
|
__name(separateImports, "separateImports");
|
|
1868
|
+
|
|
1869
|
+
// src/code_hash_manager.ts
|
|
1870
|
+
var import_node_crypto = require("crypto");
|
|
1871
|
+
var CodeHashMetaKey = /* @__PURE__ */ ((CodeHashMetaKey2) => {
|
|
1872
|
+
CodeHashMetaKey2["VERSION"] = "v";
|
|
1873
|
+
CodeHashMetaKey2["USER"] = "u";
|
|
1874
|
+
return CodeHashMetaKey2;
|
|
1875
|
+
})(CodeHashMetaKey || {});
|
|
1876
|
+
var _CodeHashManager = class _CodeHashManager {
|
|
1877
|
+
constructor(secret) {
|
|
1878
|
+
this.secret = secret;
|
|
1879
|
+
}
|
|
1880
|
+
/**
|
|
1881
|
+
* Encodes object (e.g. input for actor) to a string hash and uses the `secret` to sign the hash.
|
|
1882
|
+
*/
|
|
1883
|
+
encode(data, user) {
|
|
1884
|
+
const meta = {
|
|
1885
|
+
["u" /* USER */]: user,
|
|
1886
|
+
["v" /* VERSION */]: _CodeHashManager.VERSION
|
|
1887
|
+
};
|
|
1888
|
+
const metaBase64 = this.toBase64(JSON.stringify(meta));
|
|
1889
|
+
const inputBase64 = this.toBase64(JSON.stringify(data));
|
|
1890
|
+
const dataToSign = [metaBase64, inputBase64].join(_CodeHashManager.SECTION_SEPARATOR);
|
|
1891
|
+
const signature = this.generateSignature(dataToSign);
|
|
1892
|
+
const signatureBase64 = this.toBase64(signature);
|
|
1893
|
+
const parts = [metaBase64, inputBase64, signatureBase64];
|
|
1894
|
+
return parts.join(_CodeHashManager.SECTION_SEPARATOR);
|
|
1895
|
+
}
|
|
1896
|
+
decode(urlHash) {
|
|
1897
|
+
const parts = urlHash.split(_CodeHashManager.SECTION_SEPARATOR);
|
|
1898
|
+
const dataToSign = parts.slice(0, 2).join(_CodeHashManager.SECTION_SEPARATOR);
|
|
1899
|
+
const meta = JSON.parse(this.fromBase64(parts[0]).toString());
|
|
1900
|
+
const data = JSON.parse(this.fromBase64(parts[1]).toString());
|
|
1901
|
+
const signature = this.fromBase64(parts[2]);
|
|
1902
|
+
const expectedSignature = this.generateSignature(dataToSign);
|
|
1903
|
+
const validSignature = (0, import_node_crypto.timingSafeEqual)(signature, expectedSignature);
|
|
1904
|
+
return {
|
|
1905
|
+
data,
|
|
1906
|
+
meta: {
|
|
1907
|
+
user: meta["u" /* USER */],
|
|
1908
|
+
version: meta["v" /* VERSION */],
|
|
1909
|
+
validSignature
|
|
1910
|
+
}
|
|
1911
|
+
};
|
|
1912
|
+
}
|
|
1913
|
+
toBase64(data) {
|
|
1914
|
+
return Buffer.from(data).toString("base64url");
|
|
1915
|
+
}
|
|
1916
|
+
fromBase64(encoded) {
|
|
1917
|
+
return Buffer.from(encoded, "base64url");
|
|
1918
|
+
}
|
|
1919
|
+
generateSignature(data) {
|
|
1920
|
+
return (0, import_node_crypto.createHmac)("sha256", this.secret).update(data).digest();
|
|
1921
|
+
}
|
|
1922
|
+
};
|
|
1923
|
+
__name(_CodeHashManager, "CodeHashManager");
|
|
1924
|
+
_CodeHashManager.SECTION_SEPARATOR = ".";
|
|
1925
|
+
_CodeHashManager.VERSION = 1;
|
|
1926
|
+
var CodeHashManager = _CodeHashManager;
|
|
1898
1927
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1899
1928
|
0 && (module.exports = {
|
|
1900
1929
|
CHECK_TYPES,
|
|
1930
|
+
CodeHashManager,
|
|
1931
|
+
CodeHashMetaKey,
|
|
1901
1932
|
HealthChecker,
|
|
1902
1933
|
InvalidJsonError,
|
|
1903
1934
|
InvalidVariableError,
|
|
@@ -1913,10 +1944,8 @@ __name(separateImports, "separateImports");
|
|
|
1913
1944
|
configureLogger,
|
|
1914
1945
|
cryptoRandomObjectId,
|
|
1915
1946
|
dateToString,
|
|
1916
|
-
decodeInput,
|
|
1917
1947
|
delayPromise,
|
|
1918
1948
|
deterministicUniqueId,
|
|
1919
|
-
encodeInput,
|
|
1920
1949
|
escapeForBson,
|
|
1921
1950
|
escapePropertyName,
|
|
1922
1951
|
escapeRegExp,
|