@apocaliss92/nodedreame 1.9.0 → 1.10.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/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.9.0";
84
+ var LIBRARY_VERSION = "1.10.0";
85
85
 
86
86
  // src/transport/errors.ts
87
87
  var DreameError = class extends Error {
@@ -2633,6 +2633,157 @@ function decodeCleanedAreaPixels(pixels, width, height) {
2633
2633
  return { cleaned, dirty };
2634
2634
  }
2635
2635
 
2636
+ // src/models/vacuum/map/merge.ts
2637
+ var OutOfOrderFrameError = class extends Error {
2638
+ expectedFrameId;
2639
+ actualFrameId;
2640
+ constructor(expectedFrameId, actualFrameId) {
2641
+ super(`map: out-of-order P-frame (expected frame_id=${expectedFrameId}, got ${actualFrameId})`);
2642
+ this.name = "OutOfOrderFrameError";
2643
+ this.expectedFrameId = expectedFrameId;
2644
+ this.actualFrameId = actualFrameId;
2645
+ }
2646
+ };
2647
+ function mergePFrame(prevInflated, pFrameInflated) {
2648
+ const prevHeader = parseMapHeader(prevInflated);
2649
+ const pHeader = parseMapHeader(pFrameInflated);
2650
+ if (pHeader.frameType !== "P") {
2651
+ throw new MapDecodeError(`mergePFrame: expected P-frame, got frame_type=${pHeader.frameType}`);
2652
+ }
2653
+ if (pHeader.mapId !== prevHeader.mapId) {
2654
+ throw new MapDecodeError(
2655
+ `mergePFrame: map_id mismatch (prev=${prevHeader.mapId}, P=${pHeader.mapId}) \u2014 request a fresh I-frame`
2656
+ );
2657
+ }
2658
+ if (pHeader.frameId !== prevHeader.frameId + 1) {
2659
+ throw new OutOfOrderFrameError(prevHeader.frameId + 1, pHeader.frameId);
2660
+ }
2661
+ if (prevHeader.gridSize !== pHeader.gridSize && pHeader.width > 0 && pHeader.height > 0) {
2662
+ throw new MapDecodeError(
2663
+ `mergePFrame: grid_size changed mid-stream (${prevHeader.gridSize} \u2192 ${pHeader.gridSize})`
2664
+ );
2665
+ }
2666
+ const prevTail = parseMapJsonTail(sliceTailText(prevInflated, prevHeader));
2667
+ const pTail = parseMapJsonTail(sliceTailText(pFrameInflated, pHeader));
2668
+ const prevLeft = prevTail.origin?.[0] ?? prevHeader.left;
2669
+ const prevTop = prevTail.origin?.[1] ?? prevHeader.top;
2670
+ const grid = prevHeader.gridSize;
2671
+ const prevRight = prevLeft + prevHeader.width * grid;
2672
+ const prevBottom = prevTop + prevHeader.height * grid;
2673
+ const hasPixelDelta = pHeader.width > 0 && pHeader.height > 0;
2674
+ let unionLeft = prevLeft;
2675
+ let unionTop = prevTop;
2676
+ let unionWidth = prevHeader.width;
2677
+ let unionHeight = prevHeader.height;
2678
+ let pLeft = 0;
2679
+ let pTop = 0;
2680
+ if (hasPixelDelta) {
2681
+ pLeft = pTail.origin?.[0] ?? pHeader.left;
2682
+ pTop = pTail.origin?.[1] ?? pHeader.top;
2683
+ const pRight = pLeft + pHeader.width * grid;
2684
+ const pBottom = pTop + pHeader.height * grid;
2685
+ if ((pLeft - prevLeft) % grid !== 0 || (pTop - prevTop) % grid !== 0) {
2686
+ throw new MapDecodeError(
2687
+ `mergePFrame: P-frame origin not aligned to prev grid (offset=${pLeft - prevLeft},${pTop - prevTop} vs grid=${grid})`
2688
+ );
2689
+ }
2690
+ unionLeft = Math.min(prevLeft, pLeft);
2691
+ unionTop = Math.min(prevTop, pTop);
2692
+ const unionRight = Math.max(prevRight, pRight);
2693
+ const unionBottom = Math.max(prevBottom, pBottom);
2694
+ unionWidth = (unionRight - unionLeft) / grid;
2695
+ unionHeight = (unionBottom - unionTop) / grid;
2696
+ }
2697
+ const newPixels = Buffer.alloc(unionWidth * unionHeight);
2698
+ const prevPixelEnd = HEADER_SIZE + prevHeader.width * prevHeader.height;
2699
+ if (prevInflated.length < prevPixelEnd) {
2700
+ throw new MapDecodeError(
2701
+ `mergePFrame: prev buffer truncated (need ${prevPixelEnd} bytes for header+pixels, got ${prevInflated.length})`
2702
+ );
2703
+ }
2704
+ const prevPixels = prevInflated.subarray(HEADER_SIZE, prevPixelEnd);
2705
+ const prevDxPx = (prevLeft - unionLeft) / grid;
2706
+ const prevDyPx = (prevTop - unionTop) / grid;
2707
+ for (let y = 0; y < prevHeader.height; y++) {
2708
+ const srcOff = y * prevHeader.width;
2709
+ const dstOff = (prevDyPx + y) * unionWidth + prevDxPx;
2710
+ prevPixels.copy(newPixels, dstOff, srcOff, srcOff + prevHeader.width);
2711
+ }
2712
+ if (hasPixelDelta) {
2713
+ const pPixelEnd = HEADER_SIZE + pHeader.width * pHeader.height;
2714
+ if (pFrameInflated.length < pPixelEnd) {
2715
+ throw new MapDecodeError(
2716
+ `mergePFrame: P buffer truncated (need ${pPixelEnd} bytes for header+pixels, got ${pFrameInflated.length})`
2717
+ );
2718
+ }
2719
+ const pPixels = pFrameInflated.subarray(HEADER_SIZE, pPixelEnd);
2720
+ const pDxPx = (pLeft - unionLeft) / grid;
2721
+ const pDyPx = (pTop - unionTop) / grid;
2722
+ for (let y = 0; y < pHeader.height; y++) {
2723
+ const dstRow = (pDyPx + y) * unionWidth + pDxPx;
2724
+ const srcRow = y * pHeader.width;
2725
+ for (let x = 0; x < pHeader.width; x++) {
2726
+ newPixels[dstRow + x] = newPixels[dstRow + x] + pPixels[srcRow + x] & 255;
2727
+ }
2728
+ }
2729
+ }
2730
+ const newHeader = Buffer.alloc(HEADER_SIZE);
2731
+ newHeader.writeInt16LE(prevHeader.mapId, 0);
2732
+ newHeader.writeInt16LE(pHeader.frameId, 2);
2733
+ newHeader[4] = FRAME_TYPE.I;
2734
+ newHeader.writeInt16LE(pHeader.robotX, 5);
2735
+ newHeader.writeInt16LE(pHeader.robotY, 7);
2736
+ newHeader.writeInt16LE(pHeader.robotA, 9);
2737
+ newHeader.writeInt16LE(pHeader.chargerX, 11);
2738
+ newHeader.writeInt16LE(pHeader.chargerY, 13);
2739
+ newHeader.writeInt16LE(pHeader.chargerA, 15);
2740
+ newHeader.writeInt16LE(grid, 17);
2741
+ newHeader.writeInt16LE(unionWidth, 19);
2742
+ newHeader.writeInt16LE(unionHeight, 21);
2743
+ newHeader.writeInt16LE(unionLeft, 23);
2744
+ newHeader.writeInt16LE(unionTop, 25);
2745
+ const mergedTail = mergeTails(prevTail, pTail, unionLeft, unionTop);
2746
+ const tailBytes = Buffer.from(JSON.stringify(mergedTail), "utf8");
2747
+ return Buffer.concat([newHeader, newPixels, tailBytes]);
2748
+ }
2749
+ function mergePFrameEnvelope(prev, pframe, prevOpts, pframeOpts) {
2750
+ const prevBuf = typeof prev === "string" ? unwrapEnvelope(prev, prevOpts) : prev;
2751
+ const pBuf = typeof pframe === "string" ? unwrapEnvelope(pframe, pframeOpts) : pframe;
2752
+ return mergePFrame(prevBuf, pBuf);
2753
+ }
2754
+ function mergeTails(prev, p, unionLeft, unionTop) {
2755
+ const merged = { ...p };
2756
+ merged.origin = [unionLeft, unionTop];
2757
+ const prevTr = typeof prev.tr === "string" ? prev.tr : "";
2758
+ const pTr = typeof p.tr === "string" ? p.tr : "";
2759
+ if (prevTr || pTr) {
2760
+ merged.tr = prevTr + pTr;
2761
+ } else {
2762
+ delete merged.tr;
2763
+ }
2764
+ if (!("seg_inf" in p) && "seg_inf" in prev) {
2765
+ merged.seg_inf = prev.seg_inf;
2766
+ }
2767
+ if (!("sa" in p) && "sa" in prev) {
2768
+ merged.sa = prev.sa;
2769
+ }
2770
+ for (const key2 of PERSISTENT_TAIL_KEYS) {
2771
+ if (!(key2 in p) && key2 in prev) {
2772
+ merged[key2] = prev[key2];
2773
+ }
2774
+ }
2775
+ return merged;
2776
+ }
2777
+ var PERSISTENT_TAIL_KEYS = [
2778
+ "vw",
2779
+ "vws",
2780
+ "sneak_areas",
2781
+ "sneak_areas_end",
2782
+ "walls_info",
2783
+ "rism",
2784
+ "decmap"
2785
+ ];
2786
+
2636
2787
  // src/models/vacuum/map/decode.ts
2637
2788
  function decodeVacuumMap(input, opts = {}) {
2638
2789
  const inflated = typeof input === "string" ? unwrapEnvelope(input, opts) : looksLikeBase64Zlib(input) ? unwrapEnvelope(input.toString("latin1"), opts) : input;
@@ -2677,6 +2828,10 @@ function decodeVacuumMap(input, opts = {}) {
2677
2828
  cleanedArea
2678
2829
  };
2679
2830
  }
2831
+ function applyVacuumPFrame(prev, pframe, opts = {}) {
2832
+ const merged = typeof prev === "string" || typeof pframe === "string" ? mergePFrameEnvelope(prev, pframe, opts.prev, opts.pframe) : mergePFrame(prev, pframe);
2833
+ return { buffer: merged, data: decodeVacuumMap(merged) };
2834
+ }
2680
2835
  function mergeDimensions(header, tail) {
2681
2836
  const left = tail.origin?.[0] ?? header.left;
2682
2837
  const top = tail.origin?.[1] ?? header.top;
@@ -3614,6 +3769,14 @@ var VacuumDevice = class _VacuumDevice extends BaseDevice {
3614
3769
  */
3615
3770
  async getMap(input) {
3616
3771
  this.#requireCap(this.#caps.canMap, "getMap", "map decoding");
3772
+ const blob = await this.#fetchMapBlob(input);
3773
+ const map = decodeVacuumMap(blob, this.#decodeOpts(input));
3774
+ this.#lastMap = map;
3775
+ this.emit("map", map);
3776
+ return map;
3777
+ }
3778
+ /** Fetch the raw OSS blob (still the base64+zlib envelope) for a map frame. */
3779
+ async #fetchMapBlob(input) {
3617
3780
  const session = this.currentSession();
3618
3781
  const region = this.region;
3619
3782
  const fetcher = input.fetcher ?? new OssFetcher();
@@ -3627,14 +3790,77 @@ var VacuumDevice = class _VacuumDevice extends BaseDevice {
3627
3790
  ...input.timeoutMs !== void 0 ? { timeoutMs: input.timeoutMs } : {},
3628
3791
  ...input.signal !== void 0 ? { signal: input.signal } : {}
3629
3792
  };
3630
- const blob = await fetcher.fetchBlob(fetchInput);
3631
- const map = decodeVacuumMap(blob, {
3793
+ return fetcher.fetchBlob(fetchInput);
3794
+ }
3795
+ /** The AES key/iv decode options, threaded from a {@link VacuumGetMapInput}. */
3796
+ #decodeOpts(input) {
3797
+ return {
3632
3798
  ...input.key !== void 0 ? { key: input.key } : {},
3633
3799
  ...input.iv !== void 0 ? { iv: input.iv } : {}
3634
- });
3635
- this.#lastMap = map;
3636
- this.emit("map", map);
3637
- return map;
3800
+ };
3801
+ }
3802
+ /**
3803
+ * The merge base for live P-frame streaming — always an INFLATED frame buffer.
3804
+ * An I-frame (re)seeds it; each P-frame merge replaces it with the merged
3805
+ * inflated buffer (so further P-frames stack). `null` until the first I-frame
3806
+ * (or after an out-of-order / map-id reset).
3807
+ */
3808
+ #mapStreamBase = null;
3809
+ /** Drop the P-frame merge base so the next I-frame re-seeds the stream. */
3810
+ resetMapStream() {
3811
+ this.#mapStreamBase = null;
3812
+ }
3813
+ /** Inflate an OSS blob to a raw frame buffer (live envelope → zlib; verbatim if already inflated). */
3814
+ #inflateFrame(blob, opts) {
3815
+ return looksLikeBase64Zlib(blob) ? unwrapEnvelope(blob.toString("latin1"), opts) : blob;
3816
+ }
3817
+ /**
3818
+ * Fetch the latest advertised map frame and fold it into a continuously
3819
+ * updating map. An I-frame (re)seeds the merge base and renders standalone; a
3820
+ * P-frame is merged onto the base via {@link applyVacuumPFrame} so the live
3821
+ * grid stays COMPLETE (a P-frame decoded standalone is only byte-deltas). On an
3822
+ * out-of-order P-frame or a map-id change the base is dropped and `null`
3823
+ * returned — the next I-frame re-seeds. Returns `null` when no frame is
3824
+ * advertised yet, or a P-frame arrives before any I-frame.
3825
+ *
3826
+ * Caches {@link lastMap} and emits `'map'` exactly like {@link getMap}, so a
3827
+ * map-watching consumer (e.g. the camstack map child) gets a fresh complete
3828
+ * frame on every push during a live clean.
3829
+ */
3830
+ async fetchLatestMapStreaming(opts = {}) {
3831
+ this.#requireCap(this.#caps.canMap, "fetchLatestMapStreaming", "map decoding");
3832
+ const filename = this.mapFilename;
3833
+ if (filename === null) return null;
3834
+ const input = { filename, ...opts };
3835
+ const decodeOpts = this.#decodeOpts(input);
3836
+ const blob = await this.#fetchMapBlob(input);
3837
+ const inflated = this.#inflateFrame(blob, decodeOpts);
3838
+ const frame = decodeVacuumMap(inflated, decodeOpts);
3839
+ if (frame.frameType === "I") {
3840
+ this.#mapStreamBase = inflated;
3841
+ this.#lastMap = frame;
3842
+ this.emit("map", frame);
3843
+ return frame;
3844
+ }
3845
+ if (frame.frameType === "P") {
3846
+ if (this.#mapStreamBase === null) return null;
3847
+ try {
3848
+ const { buffer, data } = applyVacuumPFrame(this.#mapStreamBase, inflated);
3849
+ this.#mapStreamBase = buffer;
3850
+ this.#lastMap = data;
3851
+ this.emit("map", data);
3852
+ return data;
3853
+ } catch (err) {
3854
+ if (err instanceof OutOfOrderFrameError || err instanceof MapDecodeError) {
3855
+ this.#mapStreamBase = null;
3856
+ return null;
3857
+ }
3858
+ throw err;
3859
+ }
3860
+ }
3861
+ this.#lastMap = frame;
3862
+ this.emit("map", frame);
3863
+ return frame;
3638
3864
  }
3639
3865
  /** Props worth seeding on start() / polling — exported for the facade. */
3640
3866
  static DEFAULT_PROPS = [