JpegEmbedder: Support Buffers that have data in their backing buffer is offset #1273
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
This adds support when the jpeg image data in the backing buffer for a
Buffer
is offset from index 0.Why?
Buffer.buffer isn't necessarily indexed the same as the Buffer.
Ran into this when some transparent optimization caused a single buffer to be used for multiple short hardcoded test image/non-image Buffers.
https://stackoverflow.com/a/50118846 talks about it, the gist of it that Buffer.from can call Buffer.allocUnsafe which may reuse memory from a pool, so image data can be stored in the middle of the buffer.
Someone could deliberately do this too with something like a sprite sheet.
How?
DataView already supports handling this and Uint8Array has the necessary offset/length fields, so added them to the DataView constructor.
Testing?
Wrote a new test, failed before change, passed after, existing tests pass, scope of the change is very small.
Only ran the nodejs and browser integration tests.
New Dependencies?
No
Screenshots
N/A
Suggested Reading?
https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html#buffer_class_method_buffer_allocunsafe_size
Anything Else?
Checklist