27 lines
1.1 KiB
JavaScript
27 lines
1.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.encode = exports.getChunkHeader = void 0;
|
|
const constants_1 = require("./constants");
|
|
const getChunkHeader = (_data) => {
|
|
const header = Buffer.alloc(1);
|
|
header.writeUInt8(constants_1.MESSAGE_MAGIC_HEADER_BYTE);
|
|
return header;
|
|
};
|
|
exports.getChunkHeader = getChunkHeader;
|
|
const encode = (data, options) => {
|
|
const { messageType } = options;
|
|
if (typeof messageType === 'string') {
|
|
throw new Error(`Unsupported message type ${messageType}`);
|
|
}
|
|
const fullSize = constants_1.HEADER_SIZE + data.length;
|
|
const encodedBuffer = Buffer.alloc(fullSize);
|
|
encodedBuffer.writeUInt8(constants_1.MESSAGE_MAGIC_HEADER_BYTE, 0);
|
|
encodedBuffer.writeUInt8(constants_1.MESSAGE_HEADER_BYTE, 1);
|
|
encodedBuffer.writeUInt8(constants_1.MESSAGE_HEADER_BYTE, 2);
|
|
encodedBuffer.writeUInt16BE(messageType, 3);
|
|
encodedBuffer.writeUInt32BE(data.length, 5);
|
|
data.copy(encodedBuffer, constants_1.HEADER_SIZE);
|
|
return encodedBuffer;
|
|
};
|
|
exports.encode = encode;
|
|
//# sourceMappingURL=encode.js.map
|