@apocaliss92/nodedreame 1.8.0 → 1.8.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/dist/index.cjs CHANGED
@@ -81,7 +81,7 @@ module.exports = __toCommonJS(index_exports);
81
81
 
82
82
  // src/support/version.ts
83
83
  var LIBRARY_NAME = "nodedreame";
84
- var LIBRARY_VERSION = "1.8.0";
84
+ var LIBRARY_VERSION = "1.8.1";
85
85
 
86
86
  // src/transport/errors.ts
87
87
  var DreameError = class extends Error {
@@ -1963,11 +1963,43 @@ function unwrapEnvelope(value, opts = {}) {
1963
1963
  }
1964
1964
  bytes = aesCbcDecrypt(bytes, key2, opts.iv);
1965
1965
  }
1966
+ let inflated;
1966
1967
  try {
1967
- return zlib.inflateSync(bytes);
1968
+ inflated = zlib.inflateSync(bytes);
1968
1969
  } catch (err) {
1969
1970
  throw new MapDecodeError("envelope: zlib inflate failed", { cause: err });
1970
1971
  }
1972
+ let guard = 0;
1973
+ while (guard < MAX_EXTRA_WRAP_LAYERS && looksLikeBase64Zlib(inflated)) {
1974
+ const text = inflated.toString("latin1").replace(/-/g, "+").replace(/_/g, "/");
1975
+ try {
1976
+ inflated = zlib.inflateSync(Buffer.from(text, "base64"));
1977
+ } catch (err) {
1978
+ throw new MapDecodeError("envelope: nested zlib inflate failed", { cause: err });
1979
+ }
1980
+ guard += 1;
1981
+ }
1982
+ return inflated;
1983
+ }
1984
+ var MAX_EXTRA_WRAP_LAYERS = 4;
1985
+ function looksLikeBase64Zlib(buf) {
1986
+ if (buf.length < 8) return false;
1987
+ for (const b of buf) {
1988
+ const isB64 = b >= 65 && b <= 90 || // A-Z
1989
+ b >= 97 && b <= 122 || // a-z
1990
+ b >= 48 && b <= 57 || // 0-9
1991
+ b === 43 || b === 47 || // + /
1992
+ b === 45 || b === 95 || // - _ (url-safe)
1993
+ b === 61;
1994
+ if (!isB64) return false;
1995
+ }
1996
+ try {
1997
+ const prefix = buf.subarray(0, 16).toString("latin1").replace(/-/g, "+").replace(/_/g, "/");
1998
+ const decoded = Buffer.from(prefix, "base64");
1999
+ return decoded.length >= 2 && decoded[0] === 120;
2000
+ } catch {
2001
+ return false;
2002
+ }
1971
2003
  }
1972
2004
  function aesCbcDecrypt(cipher, rawKey, iv) {
1973
2005
  const keyBytes = Buffer.from(
@@ -2603,7 +2635,7 @@ function decodeCleanedAreaPixels(pixels, width, height) {
2603
2635
 
2604
2636
  // src/models/vacuum/map/decode.ts
2605
2637
  function decodeVacuumMap(input, opts = {}) {
2606
- const inflated = typeof input === "string" ? unwrapEnvelope(input, opts) : input;
2638
+ const inflated = typeof input === "string" ? unwrapEnvelope(input, opts) : looksLikeBase64Zlib(input) ? unwrapEnvelope(input.toString("latin1"), opts) : input;
2607
2639
  const { header, tail } = parseFrame(inflated);
2608
2640
  const dimensions = mergeDimensions(header, tail);
2609
2641
  const robot = pose(header.robotX, header.robotY, header.robotA, tail.nr === true);