@apify/utilities 2.23.0 → 2.23.2

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
@@ -1962,22 +1962,37 @@ function createHmacSignature(secretKey, message) {
1962
1962
  return encodeBase62(BigInt(`0x${signature}`));
1963
1963
  }
1964
1964
  __name(createHmacSignature, "createHmacSignature");
1965
- var webcrypto = globalThis.crypto?.subtle;
1966
- async function ensureCryptoSubtleExists() {
1967
- webcrypto ?? (webcrypto = (await import("crypto")).webcrypto.subtle);
1965
+ var subtleCrypto = globalThis.crypto?.subtle;
1966
+ async function ensureSubtleCryptoExists() {
1967
+ if (!subtleCrypto) {
1968
+ try {
1969
+ subtleCrypto = require("crypto")?.webcrypto?.subtle;
1970
+ if (subtleCrypto) return;
1971
+ } catch {
1972
+ }
1973
+ try {
1974
+ subtleCrypto = (await import("crypto"))?.webcrypto?.subtle;
1975
+ } catch {
1976
+ }
1977
+ if (!subtleCrypto) {
1978
+ throw new Error(`SubtleCrypto is not available in this environment.
1979
+ Please ensure you're running in an environment that supports Web Crypto API,
1980
+ or submit an issue to https://github.com/apify/apify-shared-js so we can help you further.`);
1981
+ }
1982
+ }
1968
1983
  }
1969
- __name(ensureCryptoSubtleExists, "ensureCryptoSubtleExists");
1984
+ __name(ensureSubtleCryptoExists, "ensureSubtleCryptoExists");
1970
1985
  async function createHmacSignatureAsync(secretKey, message) {
1971
- await ensureCryptoSubtleExists();
1986
+ await ensureSubtleCryptoExists();
1972
1987
  const encoder = new TextEncoder();
1973
- const key = await webcrypto.importKey(
1988
+ const key = await subtleCrypto.importKey(
1974
1989
  "raw",
1975
1990
  encoder.encode(secretKey),
1976
1991
  { name: "HMAC", hash: "SHA-256" },
1977
1992
  false,
1978
1993
  ["sign"]
1979
1994
  );
1980
- const signatureBuffer = await webcrypto.sign(
1995
+ const signatureBuffer = await subtleCrypto.sign(
1981
1996
  "HMAC",
1982
1997
  key,
1983
1998
  encoder.encode(message)