UNPKG

nx

Version:

The core Nx plugin contains the core functionality of Nx like the project graph, nx commands and task orchestration.

43 lines (42 loc) 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MESSAGE_END_SEQ = void 0; exports.consumeMessagesFromSocket = consumeMessagesFromSocket; exports.isJsonMessage = isJsonMessage; const VERY_END_CODE = 4; exports.MESSAGE_END_SEQ = 'NX_MSG_END' + String.fromCharCode(VERY_END_CODE); function consumeMessagesFromSocket(callback) { let message = ''; return (data) => { const chunk = data.toString(); message += chunk; // Check if accumulated message ends with MESSAGE_END_SEQ (not just the chunk) // This handles TCP packet fragmentation where MESSAGE_END_SEQ may be split across packets if (chunk.codePointAt(chunk.length - 1) === VERY_END_CODE && message.endsWith(exports.MESSAGE_END_SEQ)) { // Remove the trailing MESSAGE_END_SEQ const fullMessage = message.substring(0, message.length - exports.MESSAGE_END_SEQ.length); // Server may send multiple messages in one chunk, so splitting by MESSAGE_END_SEQ const messages = fullMessage.split(exports.MESSAGE_END_SEQ); for (const splitMessage of messages) { if (splitMessage) { callback(splitMessage); } } message = ''; } // If message doesn't end with MESSAGE_END_SEQ, keep accumulating chunks }; } function isJsonMessage(message) { return ( // json objects ['[', '{'].some((prefix) => message.startsWith(prefix)) || // booleans message === 'true' || message === 'false' || // strings (message.startsWith('"') && message.endsWith('"')) || // numbers /^[0-9]+(\.?[0-9]+)?$/.test(message)); }