UNPKG

4.55 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2022 Red Hat, Inc. and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17/* eslint-disable @typescript-eslint/no-explicit-any */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.registerMsgPackExtensions = exports.MsgPackMessageDecoder = exports.MsgPackMessageEncoder = exports.defaultMsgPack = exports.EncodingError = exports.ResponseError = void 0;
20const msgpackr_1 = require("msgpackr");
21const msg_pack_extension_manager_1 = require("./msg-pack-extension-manager");
22/**
23 * A special error that can be returned in case a request
24 * has failed. Provides additional information i.e. an error code
25 * and additional error data.
26 */
27class ResponseError extends Error {
28 constructor(code, message, data) {
29 super(message);
30 this.code = code;
31 this.data = data;
32 }
33}
34exports.ResponseError = ResponseError;
35/**
36 * Custom error thrown by the {@link RpcMessageEncoder} if an error occurred during the encoding and the
37 * object could not be written to the given {@link WriteBuffer}
38 */
39class EncodingError extends Error {
40 constructor(msg, cause) {
41 super(msg);
42 this.cause = cause;
43 }
44}
45exports.EncodingError = EncodingError;
46exports.defaultMsgPack = new msgpackr_1.Packr({ moreTypes: true, encodeUndefinedAsNil: false, bundleStrings: false });
47class MsgPackMessageEncoder {
48 constructor(msgPack = exports.defaultMsgPack) {
49 this.msgPack = msgPack;
50 }
51 cancel(buf, requestId) {
52 this.encode(buf, { type: 5 /* Cancel */, id: requestId });
53 }
54 notification(buf, requestId, method, args) {
55 this.encode(buf, { type: 2 /* Notification */, id: requestId, method, args });
56 }
57 request(buf, requestId, method, args) {
58 this.encode(buf, { type: 1 /* Request */, id: requestId, method, args });
59 }
60 replyOK(buf, requestId, res) {
61 this.encode(buf, { type: 3 /* Reply */, id: requestId, res });
62 }
63 replyErr(buf, requestId, err) {
64 this.encode(buf, { type: 4 /* ReplyErr */, id: requestId, err });
65 }
66 encode(buf, value) {
67 try {
68 buf.writeBytes(this.msgPack.encode(value));
69 }
70 catch (err) {
71 if (err instanceof Error) {
72 throw new EncodingError(`Error during encoding: '${err.message}'`, err);
73 }
74 throw err;
75 }
76 }
77}
78exports.MsgPackMessageEncoder = MsgPackMessageEncoder;
79class MsgPackMessageDecoder {
80 constructor(msgPack = exports.defaultMsgPack) {
81 this.msgPack = msgPack;
82 }
83 decode(buf) {
84 const bytes = buf.readBytes();
85 return this.msgPack.decode(bytes);
86 }
87 parse(buffer) {
88 return this.decode(buffer);
89 }
90}
91exports.MsgPackMessageDecoder = MsgPackMessageDecoder;
92function registerMsgPackExtensions() {
93 // Register custom msgPack extension for Errors.
94 msg_pack_extension_manager_1.MsgPackExtensionManager.getInstance().registerExtensions({
95 class: Error,
96 tag: 1,
97 // eslint-disable-next-line @typescript-eslint/no-explicit-any
98 serialize: (error) => {
99 var _a;
100 const { code, data, message, name } = error;
101 const stack = (_a = error.stacktrace) !== null && _a !== void 0 ? _a : error.stack;
102 const isResponseError = error instanceof ResponseError;
103 return { code, data, message, name, stack, isResponseError };
104 },
105 deserialize: data => {
106 const error = data.isResponseError ? new ResponseError(data.code, data.message, data.data) : new Error(data.message);
107 error.name = data.name;
108 error.stack = data.stack;
109 return error;
110 }
111 });
112}
113exports.registerMsgPackExtensions = registerMsgPackExtensions;
114//# sourceMappingURL=rpc-message-encoder.js.map
\No newline at end of file