-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix maxBuffer
bug with TextDecoder()
#105
Conversation
} | ||
|
||
return finalize(contents, length, textDecoder); | ||
appendFinalChunk({state, convertChunk, getSize, addChunk, getFinalChunk, maxBuffer}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The final chunk (�
) must use the same logic as the previous chunks, including the maxBuffer
logic.
To do this required two changes:
- Adding a
getFinalChunk()
method that returns eitherundefined
(no final chunk) or the final chunk, with the right type - Extracting the
appendChunk()
logic so that it can be used aftergetFinalChunk()
. To do this, the inside of the mainfor
loop had to be extracted to its own functionappendChunk()
. To do this, stateful arguments had to be put into astate
object. Changing this required refactoring some additional functions.
@@ -112,6 +112,11 @@ test('get stream with truncated UTF-8 sequences', async t => { | |||
t.is(result, `${multiByteString.slice(0, -1)}${INVALID_UTF8_MARKER}`); | |||
}); | |||
|
|||
test('handles truncated UTF-8 sequences over maxBuffer', async t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test succeeds with the current PR, and fails without it.
source/array-buffer.js
Outdated
|
||
export async function getStreamAsArrayBuffer(stream, options) { | ||
return getStreamContents(stream, arrayBufferMethods, options); | ||
} | ||
|
||
const initArrayBuffer = () => new Uint8Array(0); | ||
const initArrayBuffer = () => ({contents: new Uint8Array(0)}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes in this file and the following ones are related to refactoring stateful variables into a state
object. See comment below.
@@ -77,5 +77,6 @@ const arrayBufferMethods = { | |||
}, | |||
getSize: getLengthProp, | |||
addChunk: addArrayBufferChunk, | |||
getFinalChunk: noop, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getFinalChunk()
is only used by getString()
. It is a noop
otherwise.
Feel free to do another release when you are ready. You have publish access on npm. |
Sounds good! Will do after #106. |
Done in |
If the stream contains a UTF-8 string with a partial multibyte sequence at the end, a final character
\ufffd
(�
) is appended. This is the standard behavior byTextDecoder
and other tools.At the moment, this final byte is not checked by the
maxBuffer
logic. I.e. the returned string could be one byte overmaxBuffer
when appending�
.This PR fixes this bug. This required some refactoring.