Why does the following code produce 2 different values for the array buffers? How would the text representation of a blob need to be encoded/processed to allow conversion to and from a blob via it's text representation?
const input = new Uint16Array([512, 591, 26, 123, 158, 0, 189, 123, 5]);
const originalBlob: Blob = new Blob([input]);
const blobFromText = new Blob([await originalBlob.text()]);
console.log(await originalBlob.arrayBuffer());
console.log(await blobFromText.arrayBuffer());
Output
Original:
Int16Array(9) [512,591,26,123,158,0,189,123,5]
Copied:
Int16Array(11) [512,591,26,123,-16401,189,0,-16401,189,123,5]
NodeJS Source indicates a ucs2 encoding being used which should be close to utf-16.
https://github.com/nodejs/node/blob/7919ced0c97e9a5b17e6042e0b57bc911d23583d/lib/internal/blob.js#L216-L218 https://github.com/nodejs/node/blob/7919ced0c97e9a5b17e6042e0b57bc911d23583d/lib/internal/encoding.js#L426
I am asking this question in conjunction with Decode XMLHTTPResponseText into dataUrl without base encoding on server side
Edit: Throwing in a text encoder to convert the text to utf-8 does not alleviate the problem.
const originalBlob: Blob = new Blob([new Uint16Array([512, 591, 26, 123, 158, 0, 189, 123, 5])]);
const originalBlobAsText = await originalBlob.text();
const te = new TextEncoder();
const blobFromText = new Blob([te.encode(originalBlobAsText)]);