@@ -59,10 +59,12 @@ class StartStopSequenceTransformer {
59
59
60
60
// Concatenate incoming chunk with existing buffer
61
61
this . buffer = new Uint8Array ( [ ...this . buffer , ...chunk ] ) ;
62
-
63
62
let startIndex = 0 ;
64
63
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 ) {
66
68
if ( this . waitingForStart ) {
67
69
// Look for the start sequence
68
70
startIndex = this . indexOfSequence ( this . buffer , this . startSequence , startIndex ) ;
@@ -73,7 +75,7 @@ class StartStopSequenceTransformer {
73
75
return ;
74
76
}
75
77
76
- // Remove bytes before the start sequence
78
+ // Remove bytes before the start sequence including the start sequence
77
79
this . buffer = this . buffer . slice ( startIndex + this . startSequence . length ) ;
78
80
startIndex = 0 ; // Reset startIndex after removing bytes
79
81
this . waitingForStart = false ;
@@ -89,13 +91,14 @@ class StartStopSequenceTransformer {
89
91
90
92
// Extract bytes between start and stop sequences
91
93
const bytesToProcess = this . buffer . slice ( startIndex , stopIndex ) ;
94
+ // Remove processed bytes from the buffer including the stop sequence.
92
95
this . buffer = this . buffer . slice ( stopIndex + this . stopSequence . length ) ;
93
96
94
97
// Check if the number of bytes matches the expected amount
95
98
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 ;
99
102
return ;
100
103
}
101
104
@@ -106,17 +109,16 @@ class StartStopSequenceTransformer {
106
109
}
107
110
108
111
/**
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.
110
113
*
111
114
* @param {WritableStreamDefaultController } controller - The controller for the writable stream.
112
115
*/
113
116
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 ) ;
118
119
}
119
120
121
+
120
122
/**
121
123
* Finds the index of the given sequence in the buffer.
122
124
*
0 commit comments