Skip to content

Commit 4f72204

Browse files
committed
Better handle data in transformer
1 parent c9299d4 commit 4f72204

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

libraries/Camera/extras/WebSerialCamera/transformers.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ class StartStopSequenceTransformer {
5959

6060
// Concatenate incoming chunk with existing buffer
6161
this.buffer = new Uint8Array([...this.buffer, ...chunk]);
62-
6362
let startIndex = 0;
6463

65-
while (startIndex < this.buffer.length) {
64+
// Only process data if at least one start and stop sequence is present in the buffer
65+
const minimumRequiredBytes = Math.min(this.startSequence.length, this.stopSequence.length);
66+
67+
while (this.buffer.length >= minimumRequiredBytes) {
6668
if (this.waitingForStart) {
6769
// Look for the start sequence
6870
startIndex = this.indexOfSequence(this.buffer, this.startSequence, startIndex);
@@ -73,7 +75,7 @@ class StartStopSequenceTransformer {
7375
return;
7476
}
7577

76-
// Remove bytes before the start sequence
78+
// Remove bytes before the start sequence including the start sequence
7779
this.buffer = this.buffer.slice(startIndex + this.startSequence.length);
7880
startIndex = 0; // Reset startIndex after removing bytes
7981
this.waitingForStart = false;
@@ -89,13 +91,14 @@ class StartStopSequenceTransformer {
8991

9092
// Extract bytes between start and stop sequences
9193
const bytesToProcess = this.buffer.slice(startIndex, stopIndex);
94+
// Remove processed bytes from the buffer including the stop sequence.
9295
this.buffer = this.buffer.slice(stopIndex + this.stopSequence.length);
9396

9497
// Check if the number of bytes matches the expected amount
9598
if (this.expectedBytes !== null && bytesToProcess.length !== this.expectedBytes) {
96-
// Drop all bytes in the buffer to avoid broken data
97-
throw new Error(`🚫 Expected ${this.expectedBytes} bytes, but got ${bytesToProcess.length} bytes instead.`);
98-
this.buffer = new Uint8Array(0);
99+
// Skip processing the bytes, but keep the remaining data in the buffer
100+
console.error(`🚫 Expected ${this.expectedBytes} bytes, but got ${bytesToProcess.length} bytes instead. Dropping data.`);
101+
this.waitingForStart = true;
99102
return;
100103
}
101104

@@ -106,17 +109,16 @@ class StartStopSequenceTransformer {
106109
}
107110

108111
/**
109-
* Flushes the buffer and processes any remaining bytes when the stream is closed.
112+
* Flushes the buffer and discards any remaining bytes when the stream is closed.
110113
*
111114
* @param {WritableStreamDefaultController} controller - The controller for the writable stream.
112115
*/
113116
flush(controller) {
114-
// Only enqueue the remaining bytes if they meet the expectedBytes criteria
115-
if (this.buffer.length === this.expectedBytes || this.expectedBytes === null) {
116-
controller?.enqueue(this.buffer);
117-
}
117+
// Discard the remaining data in the buffer
118+
this.buffer = new Uint8Array(0);
118119
}
119120

121+
120122
/**
121123
* Finds the index of the given sequence in the buffer.
122124
*

0 commit comments

Comments
 (0)