@apify/utilities 2.20.3 → 2.23.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/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,31 @@ function createHmacSignature(secretKey, message) {
1960
1962
  return encodeBase62(BigInt(`0x${signature}`));
1961
1963
  }
1962
1964
  __name(createHmacSignature, "createHmacSignature");
1965
+ var webcrypto = globalThis.crypto?.subtle;
1966
+ async function ensureCryptoSubtleExists() {
1967
+ webcrypto ?? (webcrypto = (await import("crypto")).webcrypto.subtle);
1968
+ }
1969
+ __name(ensureCryptoSubtleExists, "ensureCryptoSubtleExists");
1970
+ async function createHmacSignatureAsync(secretKey, message) {
1971
+ await ensureCryptoSubtleExists();
1972
+ const encoder = new TextEncoder();
1973
+ const key = await webcrypto.importKey(
1974
+ "raw",
1975
+ encoder.encode(secretKey),
1976
+ { name: "HMAC", hash: "SHA-256" },
1977
+ false,
1978
+ ["sign"]
1979
+ );
1980
+ const signatureBuffer = await webcrypto.sign(
1981
+ "HMAC",
1982
+ key,
1983
+ encoder.encode(message)
1984
+ );
1985
+ const signatureArray = new Uint8Array(signatureBuffer);
1986
+ const signatureHex = Array.from(signatureArray).map((b) => b.toString(16).padStart(2, "0")).join("").substring(0, 30);
1987
+ return encodeBase62(BigInt(`0x${signatureHex}`));
1988
+ }
1989
+ __name(createHmacSignatureAsync, "createHmacSignatureAsync");
1963
1990
 
1964
1991
  // src/storages.ts
1965
1992
  function createStorageContentSignature({
@@ -1973,6 +2000,26 @@ function createStorageContentSignature({
1973
2000
  return Buffer.from(`${version}.${expiresAt}.${hmac}`).toString("base64url");
1974
2001
  }
1975
2002
  __name(createStorageContentSignature, "createStorageContentSignature");
2003
+ function typedArrayToBase64Url(typedArray) {
2004
+ let binary = "";
2005
+ for (let i = 0; i < typedArray.length; i++) {
2006
+ binary += String.fromCharCode(typedArray[i]);
2007
+ }
2008
+ const base64 = btoa(binary);
2009
+ return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
2010
+ }
2011
+ __name(typedArrayToBase64Url, "typedArrayToBase64Url");
2012
+ async function createStorageContentSignatureAsync({
2013
+ resourceId,
2014
+ urlSigningSecretKey,
2015
+ expiresInMillis,
2016
+ version = 0
2017
+ }) {
2018
+ const expiresAt = expiresInMillis ? (/* @__PURE__ */ new Date()).getTime() + expiresInMillis : 0;
2019
+ const hmac = await createHmacSignatureAsync(urlSigningSecretKey, `${version}.${expiresAt}.${resourceId}`);
2020
+ return typedArrayToBase64Url(new TextEncoder().encode(`${version}.${expiresAt}.${hmac}`));
2021
+ }
2022
+ __name(createStorageContentSignatureAsync, "createStorageContentSignatureAsync");
1976
2023
  // Annotate the CommonJS export names for ESM import in node:
1977
2024
  0 && (module.exports = {
1978
2025
  CHECK_TYPES,
@@ -1992,8 +2039,10 @@ __name(createStorageContentSignature, "createStorageContentSignature");
1992
2039
  concatStreamToBuffer,
1993
2040
  configureLogger,
1994
2041
  createHmacSignature,
2042
+ createHmacSignatureAsync,
1995
2043
  createInjectableRegExp,
1996
2044
  createStorageContentSignature,
2045
+ createStorageContentSignatureAsync,
1997
2046
  cryptoRandomObjectId,
1998
2047
  dateToString,
1999
2048
  delayPromise,