Offset is not applied in Buffer.from
console.log "Deducting #{msgSentBuffer.length} from #{msgReceivingBuffer.length}" #logs Deducting 5 from 13
msgAnswer = Buffer.from(msgReceivingBuffer, msgSentBuffer.length) #so 5 should be the offset
console.log "Length is now: #{msgAnswer.length}" #logs Length is now:13, but should be 13 - 5 = 8
console.log chalk.bgMagenta "Answer", toIntArray msgAnswer #Is a copy of msgReceivingBuffer there is nothing deducted
Or is The optional byteOffset and length arguments specify a memory range within the arrayBuffer that will be shared by the Buffer. this to be understood as:
/cc @nodejs/buffer
Documentation is a bit confusing. Passing Buffer falls under the API of Buffer.from(array). Which copies the whole contents of the Buffer into a new Buffer.
What you're likely expecting is Buffer.from(arrayBuffer[, byteOffset[, length]]), but to do this you'll need to change your code to the following:
msgAnswer = Buffer.from(msgReceivingBuffer.buffer,
msgReceivingBuffer.byteOffset + msgSentBuffer.length);
Can you try that out and let me know if it works for you?
Side note, reason the API is setup like this is because if a dev simply wants to slice the buffer then they can run msgAnswer.slice(msgSentBuffer.length); Buffer.from() accepts ArrayBuffer simply as a way to work with typed arrays.
Thanks, your solution nearly worked. Without the third length param, the new buffer was very long and full of random numbers. Finally this worked:
console.log chalk.bgMagenta "Original", toIntArray msgReceivingBuffer #Original 80,0,1,242,189,1,3,80,0,136,65,5,222
console.log chalk.bgRed "Offset", msgReceivingBuffer.byteOffset, "Offset2 ", msgSentBuffer.length, "Length ", msgReceivingBuffer.length #Offset 4016 Offset2 5 Length 13
msgAnswer = Buffer.from msgReceivingBuffer.buffer, msgReceivingBuffer.byteOffset + msgSentBuffer.length, 8
console.log chalk.bgMagenta "Sent", toIntArray msgSentBuffer #Sent 80,0,1,242,189
console.log chalk.bgMagenta "Answer", toIntArray msgAnswer #Answer 1,3,80,0,136,65,5,222
msgAnswer = msgReceivingBuffer.slice(5)works also in looks more intuitive and shorter.
1)
Just to understand, why is msgReceivingBuffer.byteOffset 4016, so all my Buffer objects are save within the same space?
Instances of the Buffer class are similar to arrays of integers but correspond to fixed-sized, raw memory allocations outside the V8 heap.
So every single Buffer object is just allocated space in that global Buffer object?
In certain cases we optimize new Buffer allocations by using a preallocated larger Buffer and take a slice from that. It's just a performance optimization for certain cases.