Skip to content

Commit

Permalink
child_process: use primordials
Browse files Browse the repository at this point in the history
Suggested by @himself65 and @anonrig
  • Loading branch information
CaramelFur committed May 2, 2022
1 parent 04ad443 commit 7e80934
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/internal/child_process/serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const {
JSONParse,
JSONStringify,
StringPrototypeSplit,
ArrayPrototypePush,
ReflectApply,
Symbol,
TypedArrayPrototypeSubarray,
} = primordials;
Expand All @@ -13,6 +15,7 @@ const v8 = require('v8');
const { isArrayBufferView } = require('internal/util/types');
const assert = require('internal/assert');
const { streamBaseState, kLastWriteWasAsync } = internalBinding('stream_wrap');
const { readUInt32BE } = require('internal/buffer');

const kMessageBuffer = Symbol('kMessageBuffer');
const kMessageBufferSize = Symbol('kMessageBufferSize');
Expand Down Expand Up @@ -60,15 +63,15 @@ const advanced = {
*parseChannelMessages(channel, readData) {
if (readData.length === 0) return;

channel[kMessageBuffer].push(readData);
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 = Buffer.prototype.readUInt32BE.call(messageBufferHead, 0) + 4;
const fullMessageSize = ReflectApply(readUInt32BE, messageBufferHead, [0]) + 4;

if (channel[kMessageBufferSize] < fullMessageSize) break;

Expand Down

0 comments on commit 7e80934

Please sign in to comment.