@apify/utilities 2.20.3 → 2.23.1

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/cjs/index.cjs CHANGED
@@ -50,8 +50,10 @@ __export(index_exports, {
50
50
  concatStreamToBuffer: () => concatStreamToBuffer,
51
51
  configureLogger: () => configureLogger,
52
52
  createHmacSignature: () => createHmacSignature,
53
+ createHmacSignatureAsync: () => createHmacSignatureAsync,
53
54
  createInjectableRegExp: () => createInjectableRegExp,
54
55
  createStorageContentSignature: () => createStorageContentSignature,
56
+ createStorageContentSignatureAsync: () => createStorageContentSignatureAsync,
55
57
  cryptoRandomObjectId: () => cryptoRandomObjectId,
56
58
  dateToString: () => dateToString,
57
59
  delayPromise: () => delayPromise,
@@ -1960,6 +1962,42 @@ function createHmacSignature(secretKey, message) {
1960
1962
  return encodeBase62(BigInt(`0x${signature}`));
1961
1963
  }
1962
1964
  __name(createHmacSignature, "createHmacSignature");
1965
+ var subtleCrypto = globalThis.crypto?.subtle;
1966
+ async function ensureSubtleCryptoExists() {
1967
+ if (!subtleCrypto) {
1968
+ if (globalThis.require) {
1969
+ subtleCrypto = globalThis.require("node:crypto")?.webcrypto?.subtle;
1970
+ } else {
1971
+ subtleCrypto = (await import("crypto"))?.webcrypto?.subtle;
1972
+ }
1973
+ if (!subtleCrypto) {
1974
+ throw new Error(`SubtleCrypto is not available in this environment.
1975
+ Please ensure you're running in an environment that supports Web Crypto API,
1976
+ or submit an issue to https://github.com/apify/apify-shared-js so we can help you further.`);
1977
+ }
1978
+ }
1979
+ }
1980
+ __name(ensureSubtleCryptoExists, "ensureSubtleCryptoExists");
1981
+ async function createHmacSignatureAsync(secretKey, message) {
1982
+ await ensureSubtleCryptoExists();
1983
+ const encoder = new TextEncoder();
1984
+ const key = await subtleCrypto.importKey(
1985
+ "raw",
1986
+ encoder.encode(secretKey),
1987
+ { name: "HMAC", hash: "SHA-256" },
1988
+ false,
1989
+ ["sign"]
1990
+ );
1991
+ const signatureBuffer = await subtleCrypto.sign(
1992
+ "HMAC",
1993
+ key,
1994
+ encoder.encode(message)
1995
+ );
1996
+ const signatureArray = new Uint8Array(signatureBuffer);
1997
+ const signatureHex = Array.from(signatureArray).map((b) => b.toString(16).padStart(2, "0")).join("").substring(0, 30);
1998
+ return encodeBase62(BigInt(`0x${signatureHex}`));
1999
+ }
2000
+ __name(createHmacSignatureAsync, "createHmacSignatureAsync");
1963
2001
 
1964
2002
  // src/storages.ts
1965
2003
  function createStorageContentSignature({
@@ -1973,6 +2011,26 @@ function createStorageContentSignature({
1973
2011
  return Buffer.from(`${version}.${expiresAt}.${hmac}`).toString("base64url");
1974
2012
  }
1975
2013
  __name(createStorageContentSignature, "createStorageContentSignature");
2014
+ function typedArrayToBase64Url(typedArray) {
2015
+ let binary = "";
2016
+ for (let i = 0; i < typedArray.length; i++) {
2017
+ binary += String.fromCharCode(typedArray[i]);
2018
+ }
2019
+ const base64 = btoa(binary);
2020
+ return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
2021
+ }
2022
+ __name(typedArrayToBase64Url, "typedArrayToBase64Url");
2023
+ async function createStorageContentSignatureAsync({
2024
+ resourceId,
2025
+ urlSigningSecretKey,
2026
+ expiresInMillis,
2027
+ version = 0
2028
+ }) {
2029
+ const expiresAt = expiresInMillis ? (/* @__PURE__ */ new Date()).getTime() + expiresInMillis : 0;
2030
+ const hmac = await createHmacSignatureAsync(urlSigningSecretKey, `${version}.${expiresAt}.${resourceId}`);
2031
+ return typedArrayToBase64Url(new TextEncoder().encode(`${version}.${expiresAt}.${hmac}`));
2032
+ }
2033
+ __name(createStorageContentSignatureAsync, "createStorageContentSignatureAsync");
1976
2034
  // Annotate the CommonJS export names for ESM import in node:
1977
2035
  0 && (module.exports = {
1978
2036
  CHECK_TYPES,
@@ -1992,8 +2050,10 @@ __name(createStorageContentSignature, "createStorageContentSignature");
1992
2050
  concatStreamToBuffer,
1993
2051
  configureLogger,
1994
2052
  createHmacSignature,
2053
+ createHmacSignatureAsync,
1995
2054
  createInjectableRegExp,
1996
2055
  createStorageContentSignature,
2056
+ createStorageContentSignatureAsync,
1997
2057
  cryptoRandomObjectId,
1998
2058
  dateToString,
1999
2059
  delayPromise,