UNPKG

10.4 kBJavaScriptView Raw
1"use strict";
2/* --------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License. See License.txt in the project root for license information.
5 * ------------------------------------------------------------------------------------------ */
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.Message = exports.NotificationType9 = exports.NotificationType8 = exports.NotificationType7 = exports.NotificationType6 = exports.NotificationType5 = exports.NotificationType4 = exports.NotificationType3 = exports.NotificationType2 = exports.NotificationType1 = exports.NotificationType0 = exports.NotificationType = exports.RequestType9 = exports.RequestType8 = exports.RequestType7 = exports.RequestType6 = exports.RequestType5 = exports.RequestType4 = exports.RequestType3 = exports.RequestType2 = exports.RequestType1 = exports.RequestType = exports.RequestType0 = exports.AbstractMessageSignature = exports.ParameterStructures = exports.ResponseError = exports.ErrorCodes = void 0;
8const is = require("./is");
9/**
10 * Predefined error codes.
11 */
12var ErrorCodes;
13(function (ErrorCodes) {
14 // Defined by JSON RPC
15 ErrorCodes.ParseError = -32700;
16 ErrorCodes.InvalidRequest = -32600;
17 ErrorCodes.MethodNotFound = -32601;
18 ErrorCodes.InvalidParams = -32602;
19 ErrorCodes.InternalError = -32603;
20 /**
21 * This is the start range of JSON RPC reserved error codes.
22 * It doesn't denote a real error code. No application error codes should
23 * be defined between the start and end range. For backwards
24 * compatibility the `ServerNotInitialized` and the `UnknownErrorCode`
25 * are left in the range.
26 *
27 * @since 3.16.0
28 */
29 ErrorCodes.jsonrpcReservedErrorRangeStart = -32099;
30 /** @deprecated use jsonrpcReservedErrorRangeStart */
31 ErrorCodes.serverErrorStart = -32099;
32 /**
33 * An error occurred when write a message to the transport layer.
34 */
35 ErrorCodes.MessageWriteError = -32099;
36 /**
37 * An error occurred when reading a message from the transport layer.
38 */
39 ErrorCodes.MessageReadError = -32098;
40 /**
41 * The connection got disposed or lost and all pending responses got
42 * rejected.
43 */
44 ErrorCodes.PendingResponseRejected = -32097;
45 /**
46 * The connection is inactive and a use of it failed.
47 */
48 ErrorCodes.ConnectionInactive = -32096;
49 /**
50 * Error code indicating that a server received a notification or
51 * request before the server has received the `initialize` request.
52 */
53 ErrorCodes.ServerNotInitialized = -32002;
54 ErrorCodes.UnknownErrorCode = -32001;
55 /**
56 * This is the end range of JSON RPC reserved error codes.
57 * It doesn't denote a real error code.
58 *
59 * @since 3.16.0
60 */
61 ErrorCodes.jsonrpcReservedErrorRangeEnd = -32000;
62 /** @deprecated use jsonrpcReservedErrorRangeEnd */
63 ErrorCodes.serverErrorEnd = -32000;
64})(ErrorCodes || (exports.ErrorCodes = ErrorCodes = {}));
65/**
66 * An error object return in a response in case a request
67 * has failed.
68 */
69class ResponseError extends Error {
70 constructor(code, message, data) {
71 super(message);
72 this.code = is.number(code) ? code : ErrorCodes.UnknownErrorCode;
73 this.data = data;
74 Object.setPrototypeOf(this, ResponseError.prototype);
75 }
76 toJson() {
77 const result = {
78 code: this.code,
79 message: this.message
80 };
81 if (this.data !== undefined) {
82 result.data = this.data;
83 }
84 return result;
85 }
86}
87exports.ResponseError = ResponseError;
88class ParameterStructures {
89 constructor(kind) {
90 this.kind = kind;
91 }
92 static is(value) {
93 return value === ParameterStructures.auto || value === ParameterStructures.byName || value === ParameterStructures.byPosition;
94 }
95 toString() {
96 return this.kind;
97 }
98}
99exports.ParameterStructures = ParameterStructures;
100/**
101 * The parameter structure is automatically inferred on the number of parameters
102 * and the parameter type in case of a single param.
103 */
104ParameterStructures.auto = new ParameterStructures('auto');
105/**
106 * Forces `byPosition` parameter structure. This is useful if you have a single
107 * parameter which has a literal type.
108 */
109ParameterStructures.byPosition = new ParameterStructures('byPosition');
110/**
111 * Forces `byName` parameter structure. This is only useful when having a single
112 * parameter. The library will report errors if used with a different number of
113 * parameters.
114 */
115ParameterStructures.byName = new ParameterStructures('byName');
116/**
117 * An abstract implementation of a MessageType.
118 */
119class AbstractMessageSignature {
120 constructor(method, numberOfParams) {
121 this.method = method;
122 this.numberOfParams = numberOfParams;
123 }
124 get parameterStructures() {
125 return ParameterStructures.auto;
126 }
127}
128exports.AbstractMessageSignature = AbstractMessageSignature;
129/**
130 * Classes to type request response pairs
131 */
132class RequestType0 extends AbstractMessageSignature {
133 constructor(method) {
134 super(method, 0);
135 }
136}
137exports.RequestType0 = RequestType0;
138class RequestType extends AbstractMessageSignature {
139 constructor(method, _parameterStructures = ParameterStructures.auto) {
140 super(method, 1);
141 this._parameterStructures = _parameterStructures;
142 }
143 get parameterStructures() {
144 return this._parameterStructures;
145 }
146}
147exports.RequestType = RequestType;
148class RequestType1 extends AbstractMessageSignature {
149 constructor(method, _parameterStructures = ParameterStructures.auto) {
150 super(method, 1);
151 this._parameterStructures = _parameterStructures;
152 }
153 get parameterStructures() {
154 return this._parameterStructures;
155 }
156}
157exports.RequestType1 = RequestType1;
158class RequestType2 extends AbstractMessageSignature {
159 constructor(method) {
160 super(method, 2);
161 }
162}
163exports.RequestType2 = RequestType2;
164class RequestType3 extends AbstractMessageSignature {
165 constructor(method) {
166 super(method, 3);
167 }
168}
169exports.RequestType3 = RequestType3;
170class RequestType4 extends AbstractMessageSignature {
171 constructor(method) {
172 super(method, 4);
173 }
174}
175exports.RequestType4 = RequestType4;
176class RequestType5 extends AbstractMessageSignature {
177 constructor(method) {
178 super(method, 5);
179 }
180}
181exports.RequestType5 = RequestType5;
182class RequestType6 extends AbstractMessageSignature {
183 constructor(method) {
184 super(method, 6);
185 }
186}
187exports.RequestType6 = RequestType6;
188class RequestType7 extends AbstractMessageSignature {
189 constructor(method) {
190 super(method, 7);
191 }
192}
193exports.RequestType7 = RequestType7;
194class RequestType8 extends AbstractMessageSignature {
195 constructor(method) {
196 super(method, 8);
197 }
198}
199exports.RequestType8 = RequestType8;
200class RequestType9 extends AbstractMessageSignature {
201 constructor(method) {
202 super(method, 9);
203 }
204}
205exports.RequestType9 = RequestType9;
206class NotificationType extends AbstractMessageSignature {
207 constructor(method, _parameterStructures = ParameterStructures.auto) {
208 super(method, 1);
209 this._parameterStructures = _parameterStructures;
210 }
211 get parameterStructures() {
212 return this._parameterStructures;
213 }
214}
215exports.NotificationType = NotificationType;
216class NotificationType0 extends AbstractMessageSignature {
217 constructor(method) {
218 super(method, 0);
219 }
220}
221exports.NotificationType0 = NotificationType0;
222class NotificationType1 extends AbstractMessageSignature {
223 constructor(method, _parameterStructures = ParameterStructures.auto) {
224 super(method, 1);
225 this._parameterStructures = _parameterStructures;
226 }
227 get parameterStructures() {
228 return this._parameterStructures;
229 }
230}
231exports.NotificationType1 = NotificationType1;
232class NotificationType2 extends AbstractMessageSignature {
233 constructor(method) {
234 super(method, 2);
235 }
236}
237exports.NotificationType2 = NotificationType2;
238class NotificationType3 extends AbstractMessageSignature {
239 constructor(method) {
240 super(method, 3);
241 }
242}
243exports.NotificationType3 = NotificationType3;
244class NotificationType4 extends AbstractMessageSignature {
245 constructor(method) {
246 super(method, 4);
247 }
248}
249exports.NotificationType4 = NotificationType4;
250class NotificationType5 extends AbstractMessageSignature {
251 constructor(method) {
252 super(method, 5);
253 }
254}
255exports.NotificationType5 = NotificationType5;
256class NotificationType6 extends AbstractMessageSignature {
257 constructor(method) {
258 super(method, 6);
259 }
260}
261exports.NotificationType6 = NotificationType6;
262class NotificationType7 extends AbstractMessageSignature {
263 constructor(method) {
264 super(method, 7);
265 }
266}
267exports.NotificationType7 = NotificationType7;
268class NotificationType8 extends AbstractMessageSignature {
269 constructor(method) {
270 super(method, 8);
271 }
272}
273exports.NotificationType8 = NotificationType8;
274class NotificationType9 extends AbstractMessageSignature {
275 constructor(method) {
276 super(method, 9);
277 }
278}
279exports.NotificationType9 = NotificationType9;
280var Message;
281(function (Message) {
282 /**
283 * Tests if the given message is a request message
284 */
285 function isRequest(message) {
286 const candidate = message;
287 return candidate && is.string(candidate.method) && (is.string(candidate.id) || is.number(candidate.id));
288 }
289 Message.isRequest = isRequest;
290 /**
291 * Tests if the given message is a notification message
292 */
293 function isNotification(message) {
294 const candidate = message;
295 return candidate && is.string(candidate.method) && message.id === void 0;
296 }
297 Message.isNotification = isNotification;
298 /**
299 * Tests if the given message is a response message
300 */
301 function isResponse(message) {
302 const candidate = message;
303 return candidate && (candidate.result !== void 0 || !!candidate.error) && (is.string(candidate.id) || is.number(candidate.id) || candidate.id === null);
304 }
305 Message.isResponse = isResponse;
306})(Message || (exports.Message = Message = {}));