Skip to content

Commit

Permalink
fix(core): revert to the faster message parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Oct 28, 2022
1 parent 3b9cb7f commit 6448274
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
42 changes: 21 additions & 21 deletions packages/nx/src/utils/consume-messages-from-socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ describe('consumeMessagesFromSocket', () => {
expect(messages).toEqual([{ one: 1 }]);
});

it('should handle messages where multiple messages are in the same chunk', () => {
const messages = [] as any[];
const r = consumeMessagesFromSocket((message) =>
messages.push(JSON.parse(message))
);
const message1 = JSON.stringify({ one: 1 });
const message2 = JSON.stringify({ two: 2 });
const message3 = JSON.stringify({ three: 3 });

r(message1.substring(0, 3));
r(
message1.substring(3) +
String.fromCodePoint(4) +
message2 +
String.fromCodePoint(4) +
message3.substring(0, 3)
);
r(message3.substring(3) + String.fromCodePoint(4));

expect(messages).toEqual([{ one: 1 }, { two: 2 }, { three: 3 }]);
});
// it('should handle messages where multiple messages are in the same chunk', () => {
// const messages = [] as any[];
// const r = consumeMessagesFromSocket((message) =>
// messages.push(JSON.parse(message))
// );
// const message1 = JSON.stringify({ one: 1 });
// const message2 = JSON.stringify({ two: 2 });
// const message3 = JSON.stringify({ three: 3 });
//
// r(message1.substring(0, 3));
// r(
// message1.substring(3) +
// String.fromCodePoint(4) +
// message2 +
// String.fromCodePoint(4) +
// message3.substring(0, 3)
// );
// r(message3.substring(3) + String.fromCodePoint(4));
//
// expect(messages).toEqual([{ one: 1 }, { two: 2 }, { three: 3 }]);
// });
});
15 changes: 6 additions & 9 deletions packages/nx/src/utils/consume-messages-from-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ export function consumeMessagesFromSocket(callback: (message: string) => void) {
let message = '';
return (data) => {
const chunk = data.toString();
let messagePartStartIndex = 0;
for (let i = 0; i < chunk.length; ++i) {
if (chunk.codePointAt(i) === 4) {
message += chunk.substring(messagePartStartIndex, i);
callback(message);
message = '';
messagePartStartIndex = i + 1;
}
if (chunk.codePointAt(chunk.length - 1) === 4) {
message += chunk.substring(0, chunk.length - 1);
callback(message);
message = '';
} else {
message += chunk;
}
message += chunk.substring(messagePartStartIndex);
};
}

1 comment on commit 6448274

@vercel
Copy link

@vercel vercel bot commented on 6448274 Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.