From fee0262246d20f9f3ac6535972e5a4e4fb324aae Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 22 Jun 2026 13:37:44 -0400 Subject: [PATCH 1/2] child_process: frame advanced IPC messages natively The `advanced` IPC read path framed messages in JavaScript (parseChannelMessages in lib/internal/child_process/serialization.js): every read scanned the 4-byte big-endian length prefix, accumulated partial frames in an array, and called Buffer.concat to reassemble messages that spanned reads, then crossed into the ipc_serdes binding once per message to deserialize. Move framing into a native per-channel IPCChannelFramer (a BaseObject on the ipc_serdes binding). It owns the cross-read accumulation buffer in C++ and returns an array of complete, deserialized messages per read, so JavaScript no longer reframes the byte stream. Messages that lie entirely within a read are deserialized in place and let host objects borrow the read buffer (zero copy); only frames that span reads are copied, matching the previous Buffer.concat behavior. The wire format (4-byte BE length prefix + V8 payload with custom host-object tags) is preserved byte-for-byte, and handle passing is untouched: the per-read pendingHandle handoff and NODE_HANDLE delivery stay in setupChannel. The `json` read path is deliberately left in JavaScript. Its StringDecoder + split('\n') pipeline avoids copies (V8 rope concatenation and O(1) substrings); reframing it in C++ regressed the json read path by up to ~40% on large messages in benchmarks, so only `advanced` is framed natively. A cctest (test/cctest/test_node_ipc_serdes.cc) exercises the framer directly: single-message round-trips, several messages delivered in one read, and a frame split across two reads (including a split length header). Round-trip throughput (benchmark/child_process/child-process-ipc-roundtrip, advanced; interleaved A/B vs. the pre-change baseline, 8 reps x dur=5s): payload before after change 1 KiB 559,040 626,255 +12.0% (t=5.3) 4 KiB 311,284 330,153 +6.1% (t=2.3) 16 KiB 104,077 125,181 +20.3% 64 KiB 31,921 35,739 +12.0% json round-trip and read throughput are unchanged (within noise). Signed-off-by: Yagiz Nizipli --- lib/internal/child_process/serialization.js | 70 ++----- src/node_ipc_serdes.cc | 208 ++++++++++++++++++-- test/cctest/test_node_ipc_serdes.cc | 144 ++++++++++++++ typings/internalBinding/ipc_serdes.d.ts | 10 + 4 files changed, 364 insertions(+), 68 deletions(-) diff --git a/lib/internal/child_process/serialization.js b/lib/internal/child_process/serialization.js index 989f748734a2..a33ac900d819 100644 --- a/lib/internal/child_process/serialization.js +++ b/lib/internal/child_process/serialization.js @@ -1,78 +1,42 @@ 'use strict'; const { - ArrayPrototypePush, JSONParse, JSONStringify, StringPrototypeSplit, Symbol, - TypedArrayPrototypeSubarray, } = primordials; const { Buffer } = require('buffer'); const { StringDecoder } = require('string_decoder'); -const { serialize, deserialize } = internalBinding('ipc_serdes'); +const { serialize, IPCChannelFramer } = internalBinding('ipc_serdes'); const { streamBaseState, kLastWriteWasAsync } = internalBinding('stream_wrap'); -const kMessageBuffer = Symbol('kMessageBuffer'); -const kMessageBufferSize = Symbol('kMessageBufferSize'); +const kFramer = Symbol('kFramer'); const kJSONBuffer = Symbol('kJSONBuffer'); const kStringDecoder = Symbol('kStringDecoder'); // Messages are parsed in either of the following formats: -// - Newline-delimited JSON, or // - V8-serialized buffers, prefixed with their length as a big endian uint32 -// (aka 'advanced') +// (aka 'advanced'), or +// - newline-delimited JSON. +// +// The 'advanced' read path is framed natively: IPCChannelFramer accumulates +// partial reads in C++ and hands back an array of complete, deserialized +// messages, so JavaScript never reframes the byte stream. The 'json' read path +// stays in JavaScript: its StringDecoder + split('\n') pipeline already avoids +// copies via V8 rope concatenation and O(1) substrings, and is measurably +// faster than reframing the bytes in C++. const advanced = { initMessageChannel(channel) { - channel[kMessageBuffer] = []; - channel[kMessageBufferSize] = 0; + channel[kFramer] = new IPCChannelFramer(); channel.buffering = false; }, - *parseChannelMessages(channel, readData) { - if (readData.length === 0) return; - - if (channel[kMessageBufferSize] && channel[kMessageBuffer][0].length < 4) { - // Message length split into two buffers, so let's concatenate it. - channel[kMessageBuffer][0] = Buffer.concat([channel[kMessageBuffer][0], readData]); - } else { - ArrayPrototypePush(channel[kMessageBuffer], readData); - } - channel[kMessageBufferSize] += readData.length; - - // Index 0 should always be present because we just pushed data into it. - let messageBufferHead = channel[kMessageBuffer][0]; - while (messageBufferHead.length >= 4) { - // We call `readUInt32BE` manually here, because this is faster than first converting - // it to a buffer and using `readUInt32BE` on that. - const fullMessageSize = (( - messageBufferHead[0] << 24 | - messageBufferHead[1] << 16 | - messageBufferHead[2] << 8 | - messageBufferHead[3] - ) >>> 0) + 4; - - if (channel[kMessageBufferSize] < fullMessageSize) break; - - const concatenatedBuffer = channel[kMessageBuffer].length === 1 ? - channel[kMessageBuffer][0] : - Buffer.concat( - channel[kMessageBuffer], - channel[kMessageBufferSize], - ); - - const serializedMessage = - TypedArrayPrototypeSubarray(concatenatedBuffer, 4, fullMessageSize); - - messageBufferHead = TypedArrayPrototypeSubarray(concatenatedBuffer, fullMessageSize); - channel[kMessageBufferSize] = messageBufferHead.length; - channel[kMessageBuffer] = - channel[kMessageBufferSize] !== 0 ? [messageBufferHead] : []; - - yield deserialize(serializedMessage); - } - - channel.buffering = channel[kMessageBufferSize] > 0; + parseChannelMessages(channel, readData) { + if (readData.length === 0) return []; + const messages = channel[kFramer].read(readData); + channel.buffering = channel[kFramer].buffering(); + return messages; }, writeChannelMessage(channel, req, message, handle) { diff --git a/src/node_ipc_serdes.cc b/src/node_ipc_serdes.cc index 05d5ae7ccffe..8963a7b19a4f 100644 --- a/src/node_ipc_serdes.cc +++ b/src/node_ipc_serdes.cc @@ -1,4 +1,6 @@ +#include "base_object-inl.h" #include "env-inl.h" +#include "memory_tracker-inl.h" #include "node_buffer.h" #include "node_errors.h" #include "node_external_reference.h" @@ -7,6 +9,7 @@ #include #include +#include // Native implementation of the `advanced` child_process IPC serialization // codec previously implemented in lib/internal/child_process/serialization.js @@ -19,6 +22,7 @@ namespace node { +using v8::Array; using v8::ArrayBuffer; using v8::ArrayBufferView; using v8::BackingStore; @@ -31,12 +35,14 @@ using v8::Float16Array; using v8::Float32Array; using v8::Float64Array; using v8::FunctionCallbackInfo; +using v8::FunctionTemplate; using v8::Int16Array; using v8::Int32Array; using v8::Int8Array; using v8::Isolate; using v8::Just; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::MaybeLocal; using v8::Name; @@ -280,14 +286,17 @@ class IPCDeserializerDelegate : public ValueDeserializer::Delegate { if (!deserializer_->ReadRawBytes(byte_length, &data)) return {}; const size_t bytes_per_element = BytesPerElement(type_index); - const size_t offset_in_ab = static_cast(data) - - static_cast(ab_->Data()); // Reuse the backing ArrayBuffer when the data is suitably aligned, // otherwise copy into a fresh aligned buffer. Mirrors _readHostObject() - // in lib/v8.js. - if (offset_in_ab % bytes_per_element == 0) { - return MakeView(env_, type_index, ab_, offset_in_ab, byte_length); + // in lib/v8.js. A frame reassembled across reads has no standalone + // ArrayBuffer to borrow from (`ab_` is empty), so always copy. + if (!ab_.IsEmpty()) { + const size_t offset_in_ab = static_cast(data) - + static_cast(ab_->Data()); + if (offset_in_ab % bytes_per_element == 0) { + return MakeView(env_, type_index, ab_, offset_in_ab, byte_length); + } } std::shared_ptr store = ArrayBuffer::NewBackingStore(isolate, byte_length); @@ -339,11 +348,37 @@ static void Serialize(const FunctionCallbackInfo& args) { } } -static void Deserialize(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); +// Deserializes a single `advanced` payload: the V8 wire header followed by the +// value, without the 4-byte big-endian length prefix. When `ab` is non-empty +// it is the ArrayBuffer backing `data`, which the delegate may borrow for +// zero-copy ArrayBufferView host objects; when it is empty (a frame reassembled +// across reads) host objects are copied into fresh buffers. +static MaybeLocal DeserializeAdvancedPayload(Environment* env, + Local ab, + const uint8_t* data, + size_t length) { Isolate* isolate = env->isolate(); Local context = env->context(); + IPCDeserializerDelegate delegate(env, ab); + ValueDeserializer deserializer(isolate, data, length, &delegate); + delegate.set_deserializer(&deserializer); + + bool read_header; + if (!deserializer.ReadHeader(context).To(&read_header)) return {}; + return deserializer.ReadValue(context); +} + +static inline uint32_t ReadUInt32BE(const char* p) { + const uint8_t* b = reinterpret_cast(p); + return (static_cast(b[0]) << 24) | + (static_cast(b[1]) << 16) | + (static_cast(b[2]) << 8) | static_cast(b[3]); +} + +static void Deserialize(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + CHECK(args[0]->IsArrayBufferView()); Local view = args[0].As(); Local ab = view->Buffer(); @@ -351,30 +386,173 @@ static void Deserialize(const FunctionCallbackInfo& args) { const size_t byte_length = view->ByteLength(); const uint8_t* data = static_cast(ab->Data()) + byte_offset; - IPCDeserializerDelegate delegate(env, ab); - ValueDeserializer deserializer(isolate, data, byte_length, &delegate); - delegate.set_deserializer(&deserializer); - - bool read_header; - if (!deserializer.ReadHeader(context).To(&read_header)) return; - Local value; - if (deserializer.ReadValue(context).ToLocal(&value)) { + if (DeserializeAdvancedPayload(env, ab, data, byte_length).ToLocal(&value)) { args.GetReturnValue().Set(value); } } +// Per-channel native framer for the `advanced` codec. It turns the raw IPC +// byte stream into complete, deserialized messages, owning the cross-read +// accumulation buffer that used to live in serialization.js (kMessageBuffer), +// so JavaScript receives whole messages and never reframes partial reads or +// concatenates partial frames itself. +// +// The `json` codec is intentionally not framed here: its StringDecoder + +// split('\n') pipeline in serialization.js already avoids copies (V8 rope +// concatenation and O(1) substrings) and is faster than reassembling the bytes +// in C++. +class IPCChannelFramer : public BaseObject { + public: + IPCChannelFramer(Environment* env, Local wrap) + : BaseObject(env, wrap) { + MakeWeak(); + } + + static void New(const FunctionCallbackInfo& args); + static void Read(const FunctionCallbackInfo& args); + static void Buffering(const FunctionCallbackInfo& args); + + void MemoryInfo(MemoryTracker* tracker) const override { + tracker->TrackField("buffered", buffered_); + } + SET_MEMORY_INFO_NAME(IPCChannelFramer) + SET_SELF_SIZE(IPCChannelFramer) + + private: + // Append complete deserialized messages found in `data` to `out`, buffering + // any trailing partial frame for the next read. Return false (with a pending + // exception) if deserialization fails. + bool ReadAdvanced(Local ab, + const uint8_t* data, + size_t length, + LocalVector* out); + + std::vector buffered_; +}; + +void IPCChannelFramer::New(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + if (!args.IsConstructCall()) { + return THROW_ERR_CONSTRUCT_CALL_REQUIRED( + env, + "Class constructor IPCChannelFramer cannot be invoked without 'new'"); + } + new IPCChannelFramer(env, args.This()); +} + +bool IPCChannelFramer::ReadAdvanced(Local ab, + const uint8_t* data, + size_t length, + LocalVector* out) { + Environment* env = this->env(); + size_t pos = 0; + + // Phase 1: finish a frame whose bytes started in a previous read. It is + // reassembled in `buffered_`, so it has no borrowable ArrayBuffer and its + // host objects are copied. + if (!buffered_.empty()) { + while (buffered_.size() < 4 && pos < length) { + buffered_.push_back(static_cast(data[pos++])); + } + if (buffered_.size() < 4) return true; // still buffering the length header + + const size_t frame_total = + static_cast(ReadUInt32BE(buffered_.data())) + 4; + while (buffered_.size() < frame_total && pos < length) { + buffered_.push_back(static_cast(data[pos++])); + } + if (buffered_.size() < frame_total) return true; // still buffering payload + + const uint8_t* payload = + reinterpret_cast(buffered_.data()) + 4; + Local message; + if (!DeserializeAdvancedPayload( + env, Local(), payload, frame_total - 4) + .ToLocal(&message)) { + return false; + } + out->push_back(message); + buffered_.clear(); + } + + // Phase 2: messages contained entirely within this chunk are deserialized in + // place, letting host objects borrow the chunk's ArrayBuffer (zero copy). + while (length - pos >= 4) { + const size_t frame_total = static_cast(ReadUInt32BE( + reinterpret_cast(data + pos))) + + 4; + if (length - pos < frame_total) break; // incomplete trailing frame + + Local message; + if (!DeserializeAdvancedPayload(env, ab, data + pos + 4, frame_total - 4) + .ToLocal(&message)) { + return false; + } + out->push_back(message); + pos += frame_total; + } + + if (pos < length) { + buffered_.assign(reinterpret_cast(data + pos), + reinterpret_cast(data + length)); + } + return true; +} + +void IPCChannelFramer::Read(const FunctionCallbackInfo& args) { + IPCChannelFramer* framer; + ASSIGN_OR_RETURN_UNWRAP(&framer, args.This()); + Isolate* isolate = framer->env()->isolate(); + + CHECK(args[0]->IsUint8Array()); + Local chunk = args[0].As(); + Local ab = chunk->Buffer(); + const size_t offset = chunk->ByteOffset(); + const size_t length = chunk->ByteLength(); + const uint8_t* data = static_cast(ab->Data()) + offset; + + LocalVector messages(isolate); + if (!framer->ReadAdvanced(ab, data, length, &messages)) { + return; // a pending exception propagates to JavaScript + } + + args.GetReturnValue().Set( + Array::New(isolate, messages.data(), messages.size())); +} + +void IPCChannelFramer::Buffering(const FunctionCallbackInfo& args) { + IPCChannelFramer* framer; + ASSIGN_OR_RETURN_UNWRAP(&framer, args.This()); + args.GetReturnValue().Set(!framer->buffered_.empty()); +} + static void Initialize(Local target, Local unused, Local context, void* priv) { + Environment* env = Environment::GetCurrent(context); + Isolate* isolate = env->isolate(); + SetMethod(context, target, "serialize", Serialize); SetMethod(context, target, "deserialize", Deserialize); + + Local framer = + NewFunctionTemplate(isolate, IPCChannelFramer::New); + framer->InstanceTemplate()->SetInternalFieldCount( + IPCChannelFramer::kInternalFieldCount); + SetProtoMethod(isolate, framer, "read", IPCChannelFramer::Read); + SetProtoMethod(isolate, framer, "buffering", IPCChannelFramer::Buffering); + framer->ReadOnlyPrototype(); + SetConstructorFunction(context, target, "IPCChannelFramer", framer); } static void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(Serialize); registry->Register(Deserialize); + registry->Register(IPCChannelFramer::New); + registry->Register(IPCChannelFramer::Read); + registry->Register(IPCChannelFramer::Buffering); } } // namespace ipc_serdes diff --git a/test/cctest/test_node_ipc_serdes.cc b/test/cctest/test_node_ipc_serdes.cc index fa86e316e7a5..3899e42241c4 100644 --- a/test/cctest/test_node_ipc_serdes.cc +++ b/test/cctest/test_node_ipc_serdes.cc @@ -3,7 +3,9 @@ #include "node_buffer.h" #include "node_test_fixture.h" +#include #include +#include using node::Environment; @@ -207,4 +209,146 @@ TEST_F(IPCSerdesTest, BufferRoundTripsAsBuffer) { EXPECT_EQ(memcmp(node::Buffer::Data(out), "Hello!", 6), 0); } +// --- IPCChannelFramer (native read-path framing) --- + +// Serializes `input` into a complete `advanced` frame (4-byte length prefix +// included), as written on the wire by writeChannelMessage(). +v8::Local SerializeFrame(v8::Local context, + v8::Local serialize, + v8::Local input) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Local buffer_ctor = + context->Global() + ->Get(context, v8::String::NewFromUtf8Literal(isolate, "Buffer")) + .ToLocalChecked(); + v8::Local args[] = {input, buffer_ctor}; + return serialize->Call(context, v8::Undefined(isolate), 2, args) + .ToLocalChecked(); +} + +v8::Local NewFramer(v8::Local context, + v8::Local binding) { + v8::Local ctor = + GetFunction(context, binding, "IPCChannelFramer"); + return ctor->NewInstance(context, 0, nullptr).ToLocalChecked(); +} + +v8::Local MakeChunk(v8::Local context, + const void* data, + size_t length) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Local ab = v8::ArrayBuffer::New(isolate, length); + if (length > 0) memcpy(ab->Data(), data, length); + return v8::Uint8Array::New(ab, 0, length); +} + +v8::Local FramerRead(v8::Local context, + v8::Local framer, + v8::Local chunk) { + v8::Local read = GetFunction(context, framer, "read"); + return read->Call(context, framer, 1, &chunk) + .ToLocalChecked() + .As(); +} + +bool FramerBuffering(v8::Local context, + v8::Local framer) { + v8::Local buffering = GetFunction(context, framer, "buffering"); + return buffering->Call(context, framer, 0, nullptr) + .ToLocalChecked() + ->BooleanValue(v8::Isolate::GetCurrent()); +} + +TEST_F(IPCSerdesTest, FramerRoundTripsSingleMessage) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + v8::Local input = v8::Number::New(isolate_, 42); + v8::Local frame = SerializeFrame(context, serialize, input); + + v8::Local framer = NewFramer(context, binding); + v8::Local messages = FramerRead(context, framer, frame); + + ASSERT_EQ(messages->Length(), 1u); + EXPECT_TRUE(messages->Get(context, 0).ToLocalChecked()->StrictEquals(input)); + EXPECT_FALSE(FramerBuffering(context, framer)); +} + +TEST_F(IPCSerdesTest, FramerSplitsMultipleMessagesInOneRead) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + v8::Local frame1 = + SerializeFrame(context, serialize, v8::Number::New(isolate_, 1)); + v8::Local frame2 = SerializeFrame( + context, serialize, v8::String::NewFromUtf8Literal(isolate_, "two")); + + node::ArrayBufferViewContents b1(frame1); + node::ArrayBufferViewContents b2(frame2); + std::vector combined; + combined.insert(combined.end(), b1.data(), b1.data() + b1.length()); + combined.insert(combined.end(), b2.data(), b2.data() + b2.length()); + + v8::Local framer = NewFramer(context, binding); + v8::Local messages = FramerRead( + context, framer, MakeChunk(context, combined.data(), combined.size())); + + ASSERT_EQ(messages->Length(), 2u); + EXPECT_EQ(messages->Get(context, 0) + .ToLocalChecked() + ->Int32Value(context) + .FromJust(), + 1); + v8::String::Utf8Value second(isolate_, + messages->Get(context, 1).ToLocalChecked()); + EXPECT_STREQ(*second, "two"); + EXPECT_FALSE(FramerBuffering(context, framer)); +} + +TEST_F(IPCSerdesTest, FramerBuffersPartialAdvancedFrame) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + v8::Local frame = SerializeFrame( + context, serialize, v8::String::NewFromUtf8Literal(isolate_, "spanning")); + node::ArrayBufferViewContents bytes(frame); + const size_t total = bytes.length(); + // Split inside the 4-byte length header to exercise that path too. + const size_t split = 2; + ASSERT_GT(total, split); + + v8::Local framer = NewFramer(context, binding); + + v8::Local first = + FramerRead(context, framer, MakeChunk(context, bytes.data(), split)); + EXPECT_EQ(first->Length(), 0u); + EXPECT_TRUE(FramerBuffering(context, framer)); + + v8::Local second = FramerRead( + context, framer, MakeChunk(context, bytes.data() + split, total - split)); + ASSERT_EQ(second->Length(), 1u); + v8::String::Utf8Value value(isolate_, + second->Get(context, 0).ToLocalChecked()); + EXPECT_STREQ(*value, "spanning"); + EXPECT_FALSE(FramerBuffering(context, framer)); +} + } // namespace diff --git a/typings/internalBinding/ipc_serdes.d.ts b/typings/internalBinding/ipc_serdes.d.ts index dc00f068e427..99ef866ce16f 100644 --- a/typings/internalBinding/ipc_serdes.d.ts +++ b/typings/internalBinding/ipc_serdes.d.ts @@ -1,8 +1,18 @@ declare namespace InternalIPCSerdesBinding { type Buffer = Uint8Array; + + // Per-channel native framer for the `advanced` codec. Accumulates partial + // reads in C++ and returns the complete, deserialized messages found in each + // chunk. See src/node_ipc_serdes.cc. + class IPCChannelFramer { + constructor(); + read(chunk: Uint8Array): unknown[]; + buffering(): boolean; + } } export interface IPCSerdesBinding { serialize(message: unknown): InternalIPCSerdesBinding.Buffer; deserialize(buffer: ArrayBufferView): unknown; + IPCChannelFramer: typeof InternalIPCSerdesBinding.IPCChannelFramer; } From 67a2a603923d44147320eb560cb9cf0b8a0e043f Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Sun, 15 Nov 2026 16:20:19 -0500 Subject: [PATCH 2/2] child_process: harden native IPC framer Consume each frame before deserializing it so that an invalid frame neither wedges nor desynchronizes the channel, and deliver the messages decoded from the same read before throwing its error. Accumulate large frames that span reads into a BackingStore that host objects borrow, and small ones into a bounded reused buffer. Copy in bulk and avoid 32-bit overflow in the length check. Signed-off-by: Daniel Lemire --- lib/internal/child_process/serialization.js | 24 +- src/node_ipc_serdes.cc | 264 +++++++++++++----- test/cctest/test_node_ipc_serdes.cc | 139 ++++++++- ...-advanced-serialization-spanning-frames.js | 49 ++++ typings/internalBinding/ipc_serdes.d.ts | 7 +- 5 files changed, 403 insertions(+), 80 deletions(-) create mode 100644 test/parallel/test-child-process-advanced-serialization-spanning-frames.js diff --git a/lib/internal/child_process/serialization.js b/lib/internal/child_process/serialization.js index a33ac900d819..9aeb994b954e 100644 --- a/lib/internal/child_process/serialization.js +++ b/lib/internal/child_process/serialization.js @@ -8,7 +8,12 @@ const { } = primordials; const { Buffer } = require('buffer'); const { StringDecoder } = require('string_decoder'); -const { serialize, IPCChannelFramer } = internalBinding('ipc_serdes'); +const { + serialize, + IPCChannelFramer, + kFramerBuffering, + kFramerError, +} = internalBinding('ipc_serdes'); const { streamBaseState, kLastWriteWasAsync } = internalBinding('stream_wrap'); const kFramer = Symbol('kFramer'); @@ -26,6 +31,15 @@ const kStringDecoder = Symbol('kStringDecoder'); // stays in JavaScript: its StringDecoder + split('\n') pipeline already avoids // copies via V8 rope concatenation and O(1) substrings, and is measurably // faster than reframing the bytes in C++. + +// A frame that failed to deserialize has already been consumed by the framer. +// Deliver the messages decoded from the same read, then throw its error. +function* yieldThenThrow(messages, error) { + for (let i = 0; i < messages.length; i++) + yield messages[i]; + throw error; +} + const advanced = { initMessageChannel(channel) { channel[kFramer] = new IPCChannelFramer(); @@ -34,8 +48,12 @@ const advanced = { parseChannelMessages(channel, readData) { if (readData.length === 0) return []; - const messages = channel[kFramer].read(readData); - channel.buffering = channel[kFramer].buffering(); + const framer = channel[kFramer]; + const messages = framer.read(readData); + const status = framer.status(); + channel.buffering = (status & kFramerBuffering) !== 0; + if ((status & kFramerError) !== 0) + return yieldThenThrow(messages, framer.takeError()); return messages; }, diff --git a/src/node_ipc_serdes.cc b/src/node_ipc_serdes.cc index 8963a7b19a4f..8556c0c7772f 100644 --- a/src/node_ipc_serdes.cc +++ b/src/node_ipc_serdes.cc @@ -7,7 +7,9 @@ #include "node_internals.h" #include "util-inl.h" +#include #include +#include #include #include @@ -49,6 +51,7 @@ using v8::Name; using v8::Nothing; using v8::Object; using v8::String; +using v8::TryCatch; using v8::Uint16Array; using v8::Uint32Array; using v8::Uint8Array; @@ -289,8 +292,9 @@ class IPCDeserializerDelegate : public ValueDeserializer::Delegate { // Reuse the backing ArrayBuffer when the data is suitably aligned, // otherwise copy into a fresh aligned buffer. Mirrors _readHostObject() - // in lib/v8.js. A frame reassembled across reads has no standalone - // ArrayBuffer to borrow from (`ab_` is empty), so always copy. + // in lib/v8.js. A small frame reassembled across reads lives in a buffer + // the framer reuses, with no ArrayBuffer to borrow (`ab_` is empty), so + // its views are always copied. if (!ab_.IsEmpty()) { const size_t offset_in_ab = static_cast(data) - static_cast(ab_->Data()); @@ -351,8 +355,8 @@ static void Serialize(const FunctionCallbackInfo& args) { // Deserializes a single `advanced` payload: the V8 wire header followed by the // value, without the 4-byte big-endian length prefix. When `ab` is non-empty // it is the ArrayBuffer backing `data`, which the delegate may borrow for -// zero-copy ArrayBufferView host objects; when it is empty (a frame reassembled -// across reads) host objects are copied into fresh buffers. +// zero-copy ArrayBufferView host objects; when it is empty, host objects are +// copied into fresh buffers. static MaybeLocal DeserializeAdvancedPayload(Environment* env, Local ab, const uint8_t* data, @@ -369,8 +373,7 @@ static MaybeLocal DeserializeAdvancedPayload(Environment* env, return deserializer.ReadValue(context); } -static inline uint32_t ReadUInt32BE(const char* p) { - const uint8_t* b = reinterpret_cast(p); +static inline uint32_t ReadUInt32BE(const uint8_t* b) { return (static_cast(b[0]) << 24) | (static_cast(b[1]) << 16) | (static_cast(b[2]) << 8) | static_cast(b[3]); @@ -392,16 +395,30 @@ static void Deserialize(const FunctionCallbackInfo& args) { } } +// Frames that span reads and whose payload is at most this many bytes are +// accumulated into a reused buffer rather than a fresh BackingStore. +static constexpr size_t kMaxSmallPayload = 64 * 1024; + +// Bits of the value returned by IPCChannelFramer.prototype.status(). +static constexpr int kFramerBuffering = 1; // a partial frame is buffered +static constexpr int kFramerError = 2; // takeError() has an error to throw + // Per-channel native framer for the `advanced` codec. It turns the raw IPC -// byte stream into complete, deserialized messages, owning the cross-read -// accumulation buffer that used to live in serialization.js (kMessageBuffer), -// so JavaScript receives whole messages and never reframes partial reads or -// concatenates partial frames itself. +// byte stream into complete, deserialized messages, so JavaScript receives +// whole messages and never reframes partial reads itself. +// +// Frames contained entirely within one read are deserialized in place, and +// their ArrayBufferView host objects borrow the read's ArrayBuffer. A small +// frame that spans reads is accumulated into a buffer the framer reuses, whose +// size is bounded by kMaxSmallPayload. A larger one is accumulated into a +// BackingStore sized from its length prefix; once complete, that store becomes +// the frame's ArrayBuffer, so its host objects borrow it instead of copying, +// and the memory is released by the GC with them. // -// The `json` codec is intentionally not framed here: its StringDecoder + -// split('\n') pipeline in serialization.js already avoids copies (V8 rope -// concatenation and O(1) substrings) and is faster than reassembling the bytes -// in C++. +// Each frame is consumed before it is deserialized, so a frame that fails to +// deserialize never desynchronizes the stream. Its error is held until the +// rest of the read has been processed, and JavaScript throws it after +// delivering the messages decoded from the same read. class IPCChannelFramer : public BaseObject { public: IPCChannelFramer(Environment* env, Local wrap) @@ -411,24 +428,54 @@ class IPCChannelFramer : public BaseObject { static void New(const FunctionCallbackInfo& args); static void Read(const FunctionCallbackInfo& args); - static void Buffering(const FunctionCallbackInfo& args); + static void Status(const FunctionCallbackInfo& args); + static void TakeError(const FunctionCallbackInfo& args); void MemoryInfo(MemoryTracker* tracker) const override { - tracker->TrackField("buffered", buffered_); + tracker->TrackFieldWithSize("payload", + payload_ ? payload_->ByteLength() : 0); + tracker->TrackFieldWithSize("small_payload", small_payload_.capacity()); + tracker->TrackField("pending_error", pending_error_); } SET_MEMORY_INFO_NAME(IPCChannelFramer) SET_SELF_SIZE(IPCChannelFramer) private: - // Append complete deserialized messages found in `data` to `out`, buffering - // any trailing partial frame for the next read. Return false (with a pending - // exception) if deserialization fails. + // Append the messages decoded from `data` to `out`, buffering any trailing + // partial frame for the next read. Return false, with a pending exception, + // only if execution is terminating; other errors are held in + // `pending_error_`. bool ReadAdvanced(Local ab, const uint8_t* data, size_t length, LocalVector* out); - - std::vector buffered_; + // Deserialize one payload and append it to `out`. + bool Deliver(Local ab, + const uint8_t* data, + size_t length, + LocalVector* out, + TryCatch* try_catch); + // Start accumulating a payload of `size` bytes that spans reads. + bool StartPayload(size_t size, TryCatch* try_catch); + // Keep the first caught error for takeError(). Return false if execution is + // terminating, in which case the exception is rethrown. + bool RecordError(TryCatch* try_catch); + + // Bytes of a length prefix split across reads. + uint8_t header_[4]; + size_t header_size_ = 0; + // Payload of a frame that spans reads, written to `payload_data_`: either + // `small_payload_` or, for a payload larger than kMaxSmallPayload, + // `payload_`. If allocating `payload_` failed, `discarding_` is set and the + // payload bytes are counted and dropped. + bool in_payload_ = false; + bool discarding_ = false; + uint8_t* payload_data_ = nullptr; + std::vector small_payload_; + std::unique_ptr payload_; + size_t payload_size_ = 0; + size_t payload_filled_ = 0; + v8::Global pending_error_; }; void IPCChannelFramer::New(const FunctionCallbackInfo& args) { @@ -441,63 +488,118 @@ void IPCChannelFramer::New(const FunctionCallbackInfo& args) { new IPCChannelFramer(env, args.This()); } +bool IPCChannelFramer::RecordError(TryCatch* try_catch) { + if (try_catch->HasTerminated() || !try_catch->CanContinue()) { + try_catch->ReThrow(); + return false; + } + if (pending_error_.IsEmpty()) { + Isolate* isolate = env()->isolate(); + Local exception = try_catch->Exception(); + if (exception.IsEmpty()) { + exception = ERR_INVALID_STATE(isolate, "Invalid IPC message"); + } + pending_error_.Reset(isolate, exception); + } + try_catch->Reset(); + return true; +} + +bool IPCChannelFramer::Deliver(Local ab, + const uint8_t* data, + size_t length, + LocalVector* out, + TryCatch* try_catch) { + Local message; + if (DeserializeAdvancedPayload(env(), ab, data, length).ToLocal(&message)) { + out->push_back(message); + return true; + } + return RecordError(try_catch); +} + +bool IPCChannelFramer::StartPayload(size_t size, TryCatch* try_catch) { + in_payload_ = true; + payload_size_ = size; + payload_filled_ = 0; + discarding_ = false; + if (size <= kMaxSmallPayload) { + small_payload_.resize(size); + payload_data_ = small_payload_.data(); + return true; + } + payload_ = ArrayBuffer::NewBackingStore( + env()->isolate(), + size, + v8::BackingStoreInitializationMode::kUninitialized, + v8::BackingStoreOnFailureMode::kReturnNull); + if (payload_) { + payload_data_ = static_cast(payload_->Data()); + return true; + } + discarding_ = true; + payload_data_ = nullptr; + THROW_ERR_MEMORY_ALLOCATION_FAILED(env()); + return RecordError(try_catch); +} + bool IPCChannelFramer::ReadAdvanced(Local ab, const uint8_t* data, size_t length, LocalVector* out) { - Environment* env = this->env(); + Isolate* isolate = env()->isolate(); + TryCatch try_catch(isolate); size_t pos = 0; - // Phase 1: finish a frame whose bytes started in a previous read. It is - // reassembled in `buffered_`, so it has no borrowable ArrayBuffer and its - // host objects are copied. - if (!buffered_.empty()) { - while (buffered_.size() < 4 && pos < length) { - buffered_.push_back(static_cast(data[pos++])); - } - if (buffered_.size() < 4) return true; // still buffering the length header - - const size_t frame_total = - static_cast(ReadUInt32BE(buffered_.data())) + 4; - while (buffered_.size() < frame_total && pos < length) { - buffered_.push_back(static_cast(data[pos++])); - } - if (buffered_.size() < frame_total) return true; // still buffering payload - - const uint8_t* payload = - reinterpret_cast(buffered_.data()) + 4; - Local message; - if (!DeserializeAdvancedPayload( - env, Local(), payload, frame_total - 4) - .ToLocal(&message)) { - return false; + for (;;) { + if (in_payload_) { + // Continue a payload that started in an earlier read. + const size_t n = std::min(payload_size_ - payload_filled_, length - pos); + if (!discarding_ && n > 0) { + memcpy(payload_data_ + payload_filled_, data + pos, n); + } + payload_filled_ += n; + pos += n; + if (payload_filled_ < payload_size_) return true; + + in_payload_ = false; + payload_data_ = nullptr; + if (discarding_) continue; + Local frame; + const uint8_t* frame_data = small_payload_.data(); + if (payload_) { + frame = ArrayBuffer::New(isolate, std::move(payload_)); + frame_data = static_cast(frame->Data()); + } + if (!Deliver(frame, frame_data, payload_size_, out, &try_catch)) { + return false; + } + continue; } - out->push_back(message); - buffered_.clear(); - } - // Phase 2: messages contained entirely within this chunk are deserialized in - // place, letting host objects borrow the chunk's ArrayBuffer (zero copy). - while (length - pos >= 4) { - const size_t frame_total = static_cast(ReadUInt32BE( - reinterpret_cast(data + pos))) + - 4; - if (length - pos < frame_total) break; // incomplete trailing frame - - Local message; - if (!DeserializeAdvancedPayload(env, ab, data + pos + 4, frame_total - 4) - .ToLocal(&message)) { - return false; + const size_t available = length - pos; + if (available == 0) return true; + + if (header_size_ == 0 && available >= sizeof(header_)) { + const size_t size = ReadUInt32BE(data + pos); + if (available - sizeof(header_) >= size) { + // The whole frame is in this read: deserialize it in place. + pos += sizeof(header_); + if (!Deliver(ab, data + pos, size, out, &try_catch)) return false; + pos += size; + continue; + } } - out->push_back(message); - pos += frame_total; - } - if (pos < length) { - buffered_.assign(reinterpret_cast(data + pos), - reinterpret_cast(data + length)); + // Accumulate the length prefix, which may itself span reads. + const size_t n = std::min(sizeof(header_) - header_size_, available); + memcpy(header_ + header_size_, data + pos, n); + header_size_ += n; + pos += n; + if (header_size_ < sizeof(header_)) return true; + header_size_ = 0; + if (!StartPayload(ReadUInt32BE(header_), &try_catch)) return false; } - return true; } void IPCChannelFramer::Read(const FunctionCallbackInfo& args) { @@ -514,17 +616,31 @@ void IPCChannelFramer::Read(const FunctionCallbackInfo& args) { LocalVector messages(isolate); if (!framer->ReadAdvanced(ab, data, length, &messages)) { - return; // a pending exception propagates to JavaScript + return; // execution is terminating } args.GetReturnValue().Set( Array::New(isolate, messages.data(), messages.size())); } -void IPCChannelFramer::Buffering(const FunctionCallbackInfo& args) { +void IPCChannelFramer::Status(const FunctionCallbackInfo& args) { IPCChannelFramer* framer; ASSIGN_OR_RETURN_UNWRAP(&framer, args.This()); - args.GetReturnValue().Set(!framer->buffered_.empty()); + int status = 0; + if (framer->in_payload_ || framer->header_size_ > 0) { + status |= kFramerBuffering; + } + if (!framer->pending_error_.IsEmpty()) status |= kFramerError; + args.GetReturnValue().Set(status); +} + +void IPCChannelFramer::TakeError(const FunctionCallbackInfo& args) { + IPCChannelFramer* framer; + ASSIGN_OR_RETURN_UNWRAP(&framer, args.This()); + if (framer->pending_error_.IsEmpty()) return; + args.GetReturnValue().Set( + framer->pending_error_.Get(framer->env()->isolate())); + framer->pending_error_.Reset(); } static void Initialize(Local target, @@ -542,9 +658,12 @@ static void Initialize(Local target, framer->InstanceTemplate()->SetInternalFieldCount( IPCChannelFramer::kInternalFieldCount); SetProtoMethod(isolate, framer, "read", IPCChannelFramer::Read); - SetProtoMethod(isolate, framer, "buffering", IPCChannelFramer::Buffering); + SetProtoMethod(isolate, framer, "status", IPCChannelFramer::Status); + SetProtoMethod(isolate, framer, "takeError", IPCChannelFramer::TakeError); framer->ReadOnlyPrototype(); SetConstructorFunction(context, target, "IPCChannelFramer", framer); + NODE_DEFINE_CONSTANT(target, kFramerBuffering); + NODE_DEFINE_CONSTANT(target, kFramerError); } static void RegisterExternalReferences(ExternalReferenceRegistry* registry) { @@ -552,7 +671,8 @@ static void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(Deserialize); registry->Register(IPCChannelFramer::New); registry->Register(IPCChannelFramer::Read); - registry->Register(IPCChannelFramer::Buffering); + registry->Register(IPCChannelFramer::Status); + registry->Register(IPCChannelFramer::TakeError); } } // namespace ipc_serdes diff --git a/test/cctest/test_node_ipc_serdes.cc b/test/cctest/test_node_ipc_serdes.cc index 3899e42241c4..ebcc6923fdaa 100644 --- a/test/cctest/test_node_ipc_serdes.cc +++ b/test/cctest/test_node_ipc_serdes.cc @@ -4,6 +4,7 @@ #include "node_test_fixture.h" #include +#include #include #include @@ -251,12 +252,26 @@ v8::Local FramerRead(v8::Local context, .As(); } +// Bits of IPCChannelFramer.prototype.status(), matching node_ipc_serdes.cc. +constexpr int kFramerBuffering = 1; +constexpr int kFramerError = 2; + +int FramerStatus(v8::Local context, v8::Local framer) { + v8::Local status = GetFunction(context, framer, "status"); + return status->Call(context, framer, 0, nullptr) + .ToLocalChecked() + ->Int32Value(context) + .FromJust(); +} + bool FramerBuffering(v8::Local context, v8::Local framer) { - v8::Local buffering = GetFunction(context, framer, "buffering"); - return buffering->Call(context, framer, 0, nullptr) - .ToLocalChecked() - ->BooleanValue(v8::Isolate::GetCurrent()); + return (FramerStatus(context, framer) & kFramerBuffering) != 0; +} + +std::vector FrameBytes(v8::Local frame) { + node::ArrayBufferViewContents bytes(frame); + return std::vector(bytes.data(), bytes.data() + bytes.length()); } TEST_F(IPCSerdesTest, FramerRoundTripsSingleMessage) { @@ -351,4 +366,120 @@ TEST_F(IPCSerdesTest, FramerBuffersPartialAdvancedFrame) { EXPECT_FALSE(FramerBuffering(context, framer)); } +TEST_F(IPCSerdesTest, FramerReassemblesBufferAcrossManyReads) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + // A Buffer host object in a frame that spans several reads, followed by a + // small message completed by the last read. The two sizes exercise the + // reused buffer for small payloads and the BackingStore for large ones. + for (const size_t payload_size : {1000u, 200000u}) { + std::string payload(payload_size, '\0'); + for (size_t i = 0; i < payload_size; i++) { + payload[i] = static_cast(i * 7); + } + v8::Local input = + node::Buffer::Copy(isolate_, payload.data(), payload.size()) + .ToLocalChecked(); + std::vector bytes = + FrameBytes(SerializeFrame(context, serialize, input)); + std::vector tail = FrameBytes( + SerializeFrame(context, serialize, v8::Number::New(isolate_, 7))); + bytes.insert(bytes.end(), tail.begin(), tail.end()); + + v8::Local framer = NewFramer(context, binding); + // Split inside the length prefix, then twice inside the payload. + const size_t splits[] = {0, 3, 200, payload_size / 2 + 100, bytes.size()}; + v8::LocalVector received(isolate_); + for (size_t i = 0; i + 1 < std::size(splits); i++) { + v8::Local messages = FramerRead( + context, + framer, + MakeChunk( + context, bytes.data() + splits[i], splits[i + 1] - splits[i])); + for (uint32_t j = 0; j < messages->Length(); j++) { + received.push_back(messages->Get(context, j).ToLocalChecked()); + } + if (i + 2 < std::size(splits)) { + EXPECT_TRUE(received.empty()); + EXPECT_TRUE(FramerBuffering(context, framer)); + } + } + + ASSERT_EQ(received.size(), 2u); + ASSERT_TRUE(node::Buffer::HasInstance(received[0])); + ASSERT_EQ(node::Buffer::Length(received[0]), payload_size); + EXPECT_EQ( + memcmp(node::Buffer::Data(received[0]), payload.data(), payload_size), + 0); + EXPECT_EQ(received[1]->Int32Value(context).FromJust(), 7); + EXPECT_EQ(FramerStatus(context, framer), 0); + } +} + +TEST_F(IPCSerdesTest, FramerRecoversFromInvalidFrame) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + std::vector good1 = FrameBytes( + SerializeFrame(context, serialize, v8::Number::New(isolate_, 1))); + std::vector good2 = FrameBytes( + SerializeFrame(context, serialize, v8::Number::New(isolate_, 2))); + std::vector good3 = FrameBytes( + SerializeFrame(context, serialize, v8::Number::New(isolate_, 3))); + // A well-framed message whose payload is not a valid V8 serialization. + const std::vector bad = {0, 0, 0, 3, 0xde, 0xad, 0xbe}; + + // One read holding: good1, bad, good2, and the first half of good3. + std::vector bytes = good1; + bytes.insert(bytes.end(), bad.begin(), bad.end()); + bytes.insert(bytes.end(), good2.begin(), good2.end()); + const size_t half = good3.size() / 2; + bytes.insert(bytes.end(), good3.begin(), good3.begin() + half); + + v8::Local framer = NewFramer(context, binding); + v8::Local first = FramerRead( + context, framer, MakeChunk(context, bytes.data(), bytes.size())); + // Messages on both sides of the invalid frame are delivered, and its error + // is held for takeError(). + ASSERT_EQ(first->Length(), 2u); + EXPECT_EQ( + first->Get(context, 0).ToLocalChecked()->Int32Value(context).FromJust(), + 1); + EXPECT_EQ( + first->Get(context, 1).ToLocalChecked()->Int32Value(context).FromJust(), + 2); + EXPECT_EQ(FramerStatus(context, framer), kFramerBuffering | kFramerError); + + v8::Local take_error = + GetFunction(context, framer, "takeError"); + v8::Local error = + take_error->Call(context, framer, 0, nullptr).ToLocalChecked(); + EXPECT_TRUE(error->IsNativeError()); + EXPECT_EQ(FramerStatus(context, framer), kFramerBuffering); + + // The stream is still in sync: the rest of good3 completes it. + v8::Local second = + FramerRead(context, + framer, + MakeChunk(context, good3.data() + half, good3.size() - half)); + ASSERT_EQ(second->Length(), 1u); + EXPECT_EQ( + second->Get(context, 0).ToLocalChecked()->Int32Value(context).FromJust(), + 3); + EXPECT_EQ(FramerStatus(context, framer), 0); +} + } // namespace diff --git a/test/parallel/test-child-process-advanced-serialization-spanning-frames.js b/test/parallel/test-child-process-advanced-serialization-spanning-frames.js new file mode 100644 index 000000000000..b80c7e12f8b5 --- /dev/null +++ b/test/parallel/test-child-process-advanced-serialization-spanning-frames.js @@ -0,0 +1,49 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const child_process = require('child_process'); + +// Messages of varied sizes, many larger than one IPC read, carrying typed +// arrays of several element sizes. Frames that span reads are reassembled +// natively; check that their contents arrive intact and in order. +const sizes = [1, 7, 4096, 65535, 65536, 65537, 200003, 3, 1 << 20, 13]; + +function makeMessage(i, size) { + const bytes = Buffer.alloc(size); + for (let j = 0; j < size; j++) bytes[j] = (i * 31 + j) & 0xff; + const doubles = new Float64Array(size >>> 3 || 1); + for (let j = 0; j < doubles.length; j++) doubles[j] = i + j / 8; + const shorts = new Uint16Array(size >>> 1 || 1); + for (let j = 0; j < shorts.length; j++) shorts[j] = (i + j) & 0xffff; + return { i, bytes, doubles, shorts }; +} + +if (process.argv[2] === 'child') { + let i = 0; + (function send() { + if (i < sizes.length) { + process.send(makeMessage(i, sizes[i]), send); + i++; + } else { + process.disconnect(); + } + })(); +} else { + const child = child_process.spawn(process.execPath, [__filename, 'child'], { + stdio: ['inherit', 'inherit', 'inherit', 'ipc'], + serialization: 'advanced', + }); + let next = 0; + child.on('message', common.mustCall((message) => { + const expected = makeMessage(next, sizes[next]); + assert.strictEqual(message.i, next); + assert.ok(Buffer.isBuffer(message.bytes)); + assert.deepStrictEqual(message.bytes, expected.bytes); + assert.deepStrictEqual(message.doubles, expected.doubles); + assert.deepStrictEqual(message.shorts, expected.shorts); + next++; + }, sizes.length)); + child.on('exit', common.mustCall(() => { + assert.strictEqual(next, sizes.length); + })); +} diff --git a/typings/internalBinding/ipc_serdes.d.ts b/typings/internalBinding/ipc_serdes.d.ts index 99ef866ce16f..711c15ed4f53 100644 --- a/typings/internalBinding/ipc_serdes.d.ts +++ b/typings/internalBinding/ipc_serdes.d.ts @@ -7,7 +7,10 @@ declare namespace InternalIPCSerdesBinding { class IPCChannelFramer { constructor(); read(chunk: Uint8Array): unknown[]; - buffering(): boolean; + // Bitmask of kFramerBuffering and kFramerError. + status(): number; + // Returns and clears the error of a frame that failed to deserialize. + takeError(): unknown; } } @@ -15,4 +18,6 @@ export interface IPCSerdesBinding { serialize(message: unknown): InternalIPCSerdesBinding.Buffer; deserialize(buffer: ArrayBufferView): unknown; IPCChannelFramer: typeof InternalIPCSerdesBinding.IPCChannelFramer; + kFramerBuffering: number; + kFramerError: number; }