server: sketchy check to determine if buffer is pool buffer

This commit is contained in:
Koushik Dutta
2024-08-19 16:04:38 -07:00
parent 86dcb66e6a
commit 0ebbc5ea8f
2 changed files with 10 additions and 5 deletions

View File

@@ -12,9 +12,14 @@ class BufferTransfer implements RpcSerializer {
if (!serializationContext)
return this.bufferSerializer.serialize(value);
// must create a copy. Buffers as allocated by node are not transferable.
const ab = value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength);
value = Buffer.from(ab);
// node may used pooled buffers for Buffer.allocUnsafe, Buffer.from, and other calls.
// these buffers will be smaller than Buffer.poolSize.
// In these instances, do not transfer the buffer, as it may not be transferible.
// create a copy so it can be safely transfered.
if (value.buffer.byteLength <= Buffer.poolSize) {
const ab = value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength);
value = Buffer.from(ab);
}
serializationContext.transferList ||= [];
const transferList: worker_threads.TransferListItem[] = serializationContext.transferList;