"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = exports.MeshLinkProtocolVersion = void 0;
exports.encodeMeshLinkFrame = encodeMeshLinkFrame;
exports.decodeMeshLinkFrame = decodeMeshLinkFrame;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const types_1 = require("../../srpc/types");
exports.MeshLinkProtocolVersion = 2;
const HeaderLengthBytes = 4;
const MaxHeaderBytes = 64 * 1024;
function encodeMeshLinkFrame(header, body = new Uint8Array(), maxFrameBytes) {
    const encodedHeader = Buffer.from(JSON.stringify({ version: exports.MeshLinkProtocolVersion, ...header }));
    if (encodedHeader.length > MaxHeaderBytes)
        throw new types_1.SrpcMeshProtocolError('sRPC mesh frame header is too large');
    const frameLength = HeaderLengthBytes + encodedHeader.length + body.byteLength;
    if (!Number.isSafeInteger(frameLength) || (maxFrameBytes !== undefined && frameLength > maxFrameBytes)) {
        throw new types_1.SrpcMeshProtocolError('sRPC mesh frame exceeds the configured limit');
    }
    const result = Buffer.allocUnsafe(frameLength);
    result.writeUInt32BE(encodedHeader.length, 0);
    encodedHeader.copy(result, HeaderLengthBytes);
    Buffer.from(body).copy(result, HeaderLengthBytes + encodedHeader.length);
    return result;
}
function decodeMeshLinkFrame(data, maxFrameBytes) {
    const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
    if (buffer.length > maxFrameBytes)
        throw new types_1.SrpcMeshProtocolError('sRPC mesh frame exceeds the configured limit');
    if (buffer.length < HeaderLengthBytes)
        throw new types_1.SrpcMeshProtocolError('Invalid sRPC mesh frame');
    const headerLength = buffer.readUInt32BE(0);
    if (headerLength < 2 || headerLength > MaxHeaderBytes || HeaderLengthBytes + headerLength > buffer.length) {
        throw new types_1.SrpcMeshProtocolError('Invalid sRPC mesh frame header length');
    }
    let header;
    try {
        header = JSON.parse(buffer.subarray(HeaderLengthBytes, HeaderLengthBytes + headerLength).toString('utf8'));
    }
    catch {
        throw new types_1.SrpcMeshProtocolError('Invalid sRPC mesh frame header');
    }
    if (header.version !== exports.MeshLinkProtocolVersion || typeof header.type !== 'string') {
        throw new types_1.SrpcMeshProtocolError('Unsupported sRPC mesh protocol version');
    }
    return {
        header,
        body: buffer.subarray(HeaderLengthBytes + headerLength)
    };
}
exports.__tsfTypeAliases = _a.decodeCompactMetadataV1("[1,{\"MeshLinkFrame\":{\"kind\":18,\"typeName\":\"MeshLinkFrame\",\"types\":[{\"kind\":20,\"name\":\"header\",\"type\":{\"kind\":18,\"typeName\":\"MeshLinkFrameHeader\",\"types\":[{\"kind\":20,\"name\":\"version\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"type\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"invoke\"},{\"kind\":10,\"literal\":\"reserveStreamIds\"},{\"kind\":10,\"literal\":\"confirmStreamIds\"},{\"kind\":10,\"literal\":\"releaseStreamIds\"},{\"kind\":10,\"literal\":\"streamWrite\"},{\"kind\":10,\"literal\":\"streamFinish\"},{\"kind\":10,\"literal\":\"streamDestroy\"},{\"kind\":10,\"literal\":\"streamActivate\"},{\"kind\":10,\"literal\":\"streamAttach\"},{\"kind\":10,\"literal\":\"revokeCapability\"},{\"kind\":10,\"literal\":\"clientInvoke\"},{\"kind\":10,\"literal\":\"fenceClient\"},{\"kind\":10,\"literal\":\"clientUpdateMetadata\"},{\"kind\":10,\"literal\":\"disconnect\"},{\"kind\":10,\"literal\":\"updateMetadata\"},{\"kind\":10,\"literal\":\"accepted\"},{\"kind\":10,\"literal\":\"result\"},{\"kind\":10,\"literal\":\"ping\"},{\"kind\":10,\"literal\":\"pong\"},{\"kind\":10,\"literal\":\"goAway\"}],\"typeName\":\"MeshLinkFrameType\"},\"optional\":false},{\"kind\":20,\"name\":\"meshKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"replyTo\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"clientId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"connectionId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"prefix\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"timeoutMs\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"deadlineAt\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"streamId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"count\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"ids\",\"type\":{\"kind\":12,\"types\":[{\"kind\":14,\"type\":{\"kind\":7}},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"capability\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"reservationId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"ok\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"error\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"errorName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"userError\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"reason\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"closeCapability\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true}]},\"optional\":false},{\"kind\":20,\"name\":\"body\",\"type\":{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":0}},\"optional\":false}]},\"MeshLinkFrameHeader\":{\"kind\":18,\"typeName\":\"MeshLinkFrameHeader\",\"types\":[{\"kind\":20,\"name\":\"version\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"type\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"invoke\"},{\"kind\":10,\"literal\":\"reserveStreamIds\"},{\"kind\":10,\"literal\":\"confirmStreamIds\"},{\"kind\":10,\"literal\":\"releaseStreamIds\"},{\"kind\":10,\"literal\":\"streamWrite\"},{\"kind\":10,\"literal\":\"streamFinish\"},{\"kind\":10,\"literal\":\"streamDestroy\"},{\"kind\":10,\"literal\":\"streamActivate\"},{\"kind\":10,\"literal\":\"streamAttach\"},{\"kind\":10,\"literal\":\"revokeCapability\"},{\"kind\":10,\"literal\":\"clientInvoke\"},{\"kind\":10,\"literal\":\"fenceClient\"},{\"kind\":10,\"literal\":\"clientUpdateMetadata\"},{\"kind\":10,\"literal\":\"disconnect\"},{\"kind\":10,\"literal\":\"updateMetadata\"},{\"kind\":10,\"literal\":\"accepted\"},{\"kind\":10,\"literal\":\"result\"},{\"kind\":10,\"literal\":\"ping\"},{\"kind\":10,\"literal\":\"pong\"},{\"kind\":10,\"literal\":\"goAway\"}],\"typeName\":\"MeshLinkFrameType\"},\"optional\":false},{\"kind\":20,\"name\":\"meshKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"replyTo\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"clientId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"connectionId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"prefix\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"timeoutMs\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"deadlineAt\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"streamId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"count\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"ids\",\"type\":{\"kind\":12,\"types\":[{\"kind\":14,\"type\":{\"kind\":7}},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"capability\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"reservationId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"ok\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"error\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"errorName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"userError\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"reason\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"closeCapability\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true}]},\"MeshLinkFrameType\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"invoke\"},{\"kind\":10,\"literal\":\"reserveStreamIds\"},{\"kind\":10,\"literal\":\"confirmStreamIds\"},{\"kind\":10,\"literal\":\"releaseStreamIds\"},{\"kind\":10,\"literal\":\"streamWrite\"},{\"kind\":10,\"literal\":\"streamFinish\"},{\"kind\":10,\"literal\":\"streamDestroy\"},{\"kind\":10,\"literal\":\"streamActivate\"},{\"kind\":10,\"literal\":\"streamAttach\"},{\"kind\":10,\"literal\":\"revokeCapability\"},{\"kind\":10,\"literal\":\"clientInvoke\"},{\"kind\":10,\"literal\":\"fenceClient\"},{\"kind\":10,\"literal\":\"clientUpdateMetadata\"},{\"kind\":10,\"literal\":\"disconnect\"},{\"kind\":10,\"literal\":\"updateMetadata\"},{\"kind\":10,\"literal\":\"accepted\"},{\"kind\":10,\"literal\":\"result\"},{\"kind\":10,\"literal\":\"ping\"},{\"kind\":10,\"literal\":\"pong\"},{\"kind\":10,\"literal\":\"goAway\"}],\"typeName\":\"MeshLinkFrameType\"}}]", [() => (typeof Buffer !== "undefined" ? Buffer : (typeof exports !== "undefined" ? exports.Buffer : undefined))]);
//# sourceMappingURL=protocol.js.map