@litert/televoke
Version:
A simple RPC service framework.
177 lines • 7.5 kB
JavaScript
;
/**
* Copyright 2025 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TvEncoderV2 = void 0;
const CS = require("./Constants.v2");
function writePacketHeader(ret, command, type, seq) {
ret.writeUInt8(command, 0);
ret.writeUInt8(type, 1);
const low = seq % 4294967296;
const high = (seq - low) / 4294967296;
ret.writeUInt16LE(high, 2);
ret.writeUInt32LE(low, 4);
}
class ErrorResponseEncoder {
encode(packet) {
const errorMsg = `${packet.ct.name}:${packet.ct.message}`;
const msgLen = Buffer.byteLength(errorMsg);
const buf = Buffer.allocUnsafe(CS.HEADER_SIZE + 2 + msgLen);
writePacketHeader(buf, packet.cmd, packet.typ, packet.seq);
buf.writeUInt16LE(msgLen, CS.HEADER_SIZE);
buf.write(errorMsg, CS.HEADER_SIZE + 2);
return [buf];
}
}
class VoidResponseEncoder {
encode(packet) {
const buf = Buffer.allocUnsafe(CS.HEADER_SIZE);
writePacketHeader(buf, packet.cmd, packet.typ, packet.seq);
return [buf];
}
}
class ApiRequestEncoder {
encode(packet) {
const apiNameLen = Buffer.byteLength(packet.ct.name);
const leadSeg = Buffer.allocUnsafe(CS.HEADER_SIZE + 2 + 4 + apiNameLen);
writePacketHeader(leadSeg, packet.cmd, packet.typ, packet.seq);
leadSeg.writeUInt16LE(apiNameLen, CS.HEADER_SIZE);
leadSeg.write(packet.ct.name, CS.HEADER_SIZE + 2);
if (Array.isArray(packet.ct.body)) {
let bodyLen = 0;
for (const p of packet.ct.body) {
bodyLen += Buffer.byteLength(p);
}
leadSeg.writeUInt32LE(bodyLen, CS.HEADER_SIZE + 2 + apiNameLen);
return [leadSeg, ...packet.ct.body];
}
leadSeg.writeUInt32LE(Buffer.byteLength(packet.ct.body), CS.HEADER_SIZE + 2 + apiNameLen);
return [leadSeg, packet.ct.body];
}
}
class BinaryChunkRequestEncoder {
encode(packet) {
const leadSeg = Buffer.allocUnsafe(CS.HEADER_SIZE + 12);
writePacketHeader(leadSeg, packet.cmd, packet.typ, packet.seq);
leadSeg.writeUInt32LE(packet.ct.streamId, CS.HEADER_SIZE);
leadSeg.writeUInt32LE(packet.ct.index, CS.HEADER_SIZE + 4);
if (Array.isArray(packet.ct.body)) {
let bodyLen = 0;
for (const p of packet.ct.body) {
bodyLen += Buffer.byteLength(p);
}
leadSeg.writeUInt32LE(bodyLen, CS.HEADER_SIZE + 8);
return [leadSeg, ...packet.ct.body];
}
leadSeg.writeUInt32LE(Buffer.byteLength(packet.ct.body), CS.HEADER_SIZE + 8);
return [leadSeg, packet.ct.body];
}
}
class PingRequestEncoder {
encode(packet) {
const leadSeg = Buffer.allocUnsafe(CS.HEADER_SIZE + 2);
writePacketHeader(leadSeg, packet.cmd, packet.typ, packet.seq);
if (Array.isArray(packet.ct)) {
let bodyLen = 0;
for (const p of packet.ct) {
bodyLen += Buffer.byteLength(p);
}
leadSeg.writeUInt16LE(bodyLen, CS.HEADER_SIZE);
return [leadSeg, ...packet.ct];
}
leadSeg.writeUInt16LE(Buffer.byteLength(packet.ct), CS.HEADER_SIZE);
return [leadSeg, packet.ct];
}
}
class CloseRequestEncoder {
encode(packet) {
const leadSeg = Buffer.allocUnsafe(CS.HEADER_SIZE);
writePacketHeader(leadSeg, packet.cmd, packet.typ, packet.seq);
return [leadSeg];
}
}
class PushMessageRequestEncoder {
encode(packet) {
const leadSeg = Buffer.allocUnsafe(CS.HEADER_SIZE + 4);
writePacketHeader(leadSeg, packet.cmd, packet.typ, packet.seq);
if (Array.isArray(packet.ct)) {
let bodyLen = 0;
for (const p of packet.ct) {
bodyLen += Buffer.byteLength(p);
}
leadSeg.writeUInt32LE(bodyLen, CS.HEADER_SIZE);
return [leadSeg, ...packet.ct];
}
leadSeg.writeUInt32LE(Buffer.byteLength(packet.ct), CS.HEADER_SIZE);
return [leadSeg, packet.ct];
}
}
class ApiResponseEncoder {
encode(packet) {
const leadSeg = Buffer.allocUnsafe(CS.HEADER_SIZE + 4);
writePacketHeader(leadSeg, packet.cmd, packet.typ, packet.seq);
if (Array.isArray(packet.ct)) {
let bodyLen = 0;
for (const p of packet.ct) {
bodyLen += Buffer.byteLength(p);
}
leadSeg.writeUInt32LE(bodyLen, CS.HEADER_SIZE);
return [leadSeg, ...packet.ct];
}
leadSeg.writeUInt32LE(Buffer.byteLength(packet.ct), CS.HEADER_SIZE);
return [leadSeg, packet.ct];
}
}
class PingResponseEncoder {
encode(packet) {
const leadSeg = Buffer.allocUnsafe(CS.HEADER_SIZE + 2);
writePacketHeader(leadSeg, packet.cmd, packet.typ, packet.seq);
if (Array.isArray(packet.ct)) {
let bodyLen = 0;
for (const p of packet.ct) {
bodyLen += Buffer.byteLength(p);
}
leadSeg.writeUInt16LE(bodyLen, CS.HEADER_SIZE);
return [leadSeg, ...packet.ct];
}
leadSeg.writeUInt16LE(Buffer.byteLength(packet.ct), CS.HEADER_SIZE);
return [leadSeg, packet.ct];
}
}
const encoders = {
[CS.EPacketType.REQUEST * 0x100 + CS.ECommand.API_CALL]: new ApiRequestEncoder(),
[CS.EPacketType.REQUEST * 0x100 + CS.ECommand.BINARY_CHUNK]: new BinaryChunkRequestEncoder(),
[CS.EPacketType.REQUEST * 0x100 + CS.ECommand.CLOSE]: new CloseRequestEncoder(),
[CS.EPacketType.REQUEST * 0x100 + CS.ECommand.PING]: new PingRequestEncoder(),
[CS.EPacketType.REQUEST * 0x100 + CS.ECommand.PUSH_MESSAGE]: new PushMessageRequestEncoder(),
[CS.EPacketType.SUCCESS_RESPONSE * 0x100 + CS.ECommand.API_CALL]: new ApiResponseEncoder(),
[CS.EPacketType.SUCCESS_RESPONSE * 0x100 + CS.ECommand.BINARY_CHUNK]: new VoidResponseEncoder(),
[CS.EPacketType.SUCCESS_RESPONSE * 0x100 + CS.ECommand.CLOSE]: new VoidResponseEncoder(),
[CS.EPacketType.SUCCESS_RESPONSE * 0x100 + CS.ECommand.PING]: new PingResponseEncoder(),
[CS.EPacketType.SUCCESS_RESPONSE * 0x100 + CS.ECommand.PUSH_MESSAGE]: new VoidResponseEncoder(),
[CS.EPacketType.ERROR_RESPONSE * 0x100 + CS.ECommand.API_CALL]: new ErrorResponseEncoder(),
[CS.EPacketType.ERROR_RESPONSE * 0x100 + CS.ECommand.BINARY_CHUNK]: new ErrorResponseEncoder(),
[CS.EPacketType.ERROR_RESPONSE * 0x100 + CS.ECommand.CLOSE]: new ErrorResponseEncoder(),
[CS.EPacketType.ERROR_RESPONSE * 0x100 + CS.ECommand.PING]: new ErrorResponseEncoder(),
[CS.EPacketType.ERROR_RESPONSE * 0x100 + CS.ECommand.PUSH_MESSAGE]: new ErrorResponseEncoder(),
};
class TvEncoderV2 {
encode(packet) {
return encoders[packet.typ * 0x100 + packet.cmd].encode(packet);
}
}
exports.TvEncoderV2 = TvEncoderV2;
//# sourceMappingURL=EncoderV2.js.map