@x-oasis/async-call-rpc 0.1.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -0
- package/dist/async-call-rpc.cjs.development.js +1275 -0
- package/dist/async-call-rpc.cjs.development.js.map +1 -0
- package/dist/async-call-rpc.cjs.production.min.js +2 -0
- package/dist/async-call-rpc.cjs.production.min.js.map +1 -0
- package/dist/async-call-rpc.esm.js +1559 -0
- package/dist/async-call-rpc.esm.js.map +1 -0
- package/dist/buffer/BufferFactory.d.ts +12 -0
- package/dist/buffer/DataBuffer.d.ts +2 -0
- package/dist/buffer/MessagePackBuffer.d.ts +10 -0
- package/dist/buffer/ReadBaseBuffer.d.ts +5 -0
- package/dist/buffer/ReadBuffer.d.ts +14 -0
- package/dist/buffer/SerializationFormat.d.ts +16 -0
- package/dist/buffer/WriteBaseBuffer.d.ts +5 -0
- package/dist/buffer/WriteBuffer.d.ts +5 -0
- package/dist/buffer/examples.d.ts +10 -0
- package/dist/buffer/index.d.ts +8 -0
- package/dist/common.d.ts +4 -0
- package/dist/endpoint/ProxyRPCClient.d.ts +12 -0
- package/dist/endpoint/RPCClientHost.d.ts +12 -0
- package/dist/endpoint/RPCService.d.ts +20 -0
- package/dist/endpoint/RPCServiceHost.d.ts +10 -0
- package/dist/error.d.ts +21 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +8 -0
- package/dist/middlewares/buffer.d.ts +18 -0
- package/dist/middlewares/handleDisconnectedRequest.d.ts +7 -0
- package/dist/middlewares/handlePortRequest.d.ts +3 -0
- package/dist/middlewares/handleRequest.d.ts +3 -0
- package/dist/middlewares/handleRequestUtils.d.ts +10 -0
- package/dist/middlewares/handleResponse.d.ts +3 -0
- package/dist/middlewares/index.d.ts +7 -0
- package/dist/middlewares/logger.d.ts +4 -0
- package/dist/middlewares/normalize.d.ts +14 -0
- package/dist/middlewares/prepareRequestData.d.ts +28 -0
- package/dist/middlewares/sendRequest.d.ts +6 -0
- package/dist/middlewares/updateSeqInfo.d.ts +6 -0
- package/dist/middlewares/utils.d.ts +3 -0
- package/dist/protocol/AbstractChannelProtocol.d.ts +55 -0
- package/dist/protocol/MessageChannel.d.ts +18 -0
- package/dist/protocol/WebSocketChannel.d.ts +23 -0
- package/dist/protocol/WorkerChannel.d.ts +11 -0
- package/dist/types/buffer.d.ts +4 -0
- package/dist/types/channel.d.ts +27 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/middleware.d.ts +44 -0
- package/dist/types/proxyChannel.d.ts +3 -0
- package/dist/types/proxyService.d.ts +11 -0
- package/dist/types/rpc.d.ts +25 -0
- package/dist/types/rpcProtocol.d.ts +22 -0
- package/dist/utils/constants.d.ts +5 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/jsonrpc.d.ts +36 -0
- package/package.json +36 -0
- package/src/buffer/ARCHITECTURE.md +298 -0
- package/src/buffer/BufferFactory.ts +124 -0
- package/src/buffer/CHANGELOG.md +207 -0
- package/src/buffer/DataBuffer.ts +1 -0
- package/src/buffer/MessagePackBuffer.ts +79 -0
- package/src/buffer/OPTIMIZATION.md +258 -0
- package/src/buffer/README.md +147 -0
- package/src/buffer/ReadBaseBuffer.ts +20 -0
- package/src/buffer/ReadBuffer.ts +58 -0
- package/src/buffer/SerializationFormat.ts +81 -0
- package/src/buffer/WriteBaseBuffer.ts +20 -0
- package/src/buffer/WriteBuffer.ts +15 -0
- package/src/buffer/examples.ts +242 -0
- package/src/buffer/index.ts +15 -0
- package/src/common.ts +20 -0
- package/src/endpoint/ProxyRPCClient.ts +64 -0
- package/src/endpoint/RPCClientHost.ts +45 -0
- package/src/endpoint/RPCService.ts +54 -0
- package/src/endpoint/RPCServiceHost.ts +18 -0
- package/src/error.ts +98 -0
- package/src/index.ts +16 -0
- package/src/middlewares/buffer.ts +33 -0
- package/src/middlewares/handleDisconnectedRequest.ts +30 -0
- package/src/middlewares/handlePortRequest.ts +141 -0
- package/src/middlewares/handleRequest.ts +128 -0
- package/src/middlewares/handleRequestUtils.ts +43 -0
- package/src/middlewares/handleResponse.ts +36 -0
- package/src/middlewares/index.ts +11 -0
- package/src/middlewares/logger.ts +22 -0
- package/src/middlewares/normalize.ts +167 -0
- package/src/middlewares/prepareRequestData.ts +137 -0
- package/src/middlewares/sendRequest.ts +15 -0
- package/src/middlewares/updateSeqInfo.ts +34 -0
- package/src/middlewares/utils.ts +67 -0
- package/src/protocol/AbstractChannelProtocol.ts +343 -0
- package/src/protocol/MessageChannel.ts +80 -0
- package/src/protocol/WebSocketChannel.ts +179 -0
- package/src/protocol/WorkerChannel.ts +36 -0
- package/src/types/buffer.ts +5 -0
- package/src/types/channel.ts +50 -0
- package/src/types/index.ts +9 -0
- package/src/types/messageChannel.ts +133 -0
- package/src/types/middleware.ts +54 -0
- package/src/types/proxyChannel.ts +3 -0
- package/src/types/proxyService.ts +18 -0
- package/src/types/rpc.ts +61 -0
- package/src/types/rpcProtocol.ts +24 -0
- package/src/utils/constants.ts +17 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/jsonrpc.ts +242 -0
|
@@ -0,0 +1,1275 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
+
|
|
7
|
+
var disposable = require('@x-oasis/disposable');
|
|
8
|
+
var emitter = require('@x-oasis/emitter');
|
|
9
|
+
var id = require('@x-oasis/id');
|
|
10
|
+
var deferred = require('@x-oasis/deferred');
|
|
11
|
+
var isAscii = require('@x-oasis/is-ascii');
|
|
12
|
+
var isPromise = _interopDefault(require('@x-oasis/is-promise'));
|
|
13
|
+
|
|
14
|
+
var ProxyRPCClient = /*#__PURE__*/function () {
|
|
15
|
+
function ProxyRPCClient(requestPath, options) {
|
|
16
|
+
var _ref = options || {},
|
|
17
|
+
channel = _ref.channel;
|
|
18
|
+
this.requestPath = requestPath;
|
|
19
|
+
if (channel) {
|
|
20
|
+
this.setChannel(channel);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
var _proto = ProxyRPCClient.prototype;
|
|
24
|
+
_proto.setChannel = function setChannel(channel) {
|
|
25
|
+
this.channel = channel;
|
|
26
|
+
};
|
|
27
|
+
_proto.handleMessage = function handleMessage() {
|
|
28
|
+
var _this$channel;
|
|
29
|
+
(_this$channel = this.channel).onMessage.apply(_this$channel, arguments);
|
|
30
|
+
};
|
|
31
|
+
_proto.createProxy = function createProxy() {
|
|
32
|
+
var _this = this;
|
|
33
|
+
var getTrap = function getTrap(_, methodName) {
|
|
34
|
+
return function () {
|
|
35
|
+
if (!_this.channel) {
|
|
36
|
+
throw new Error("[ProxyRPCClient error] `this.channel` is null, when invoke function " + methodName);
|
|
37
|
+
}
|
|
38
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
39
|
+
args[_key] = arguments[_key];
|
|
40
|
+
}
|
|
41
|
+
return _this.channel.makeRequest({
|
|
42
|
+
requestPath: _this.requestPath,
|
|
43
|
+
methodName: methodName,
|
|
44
|
+
args: args
|
|
45
|
+
}).promise;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
return new Proxy({}, {
|
|
49
|
+
get: getTrap
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
return ProxyRPCClient;
|
|
53
|
+
}();
|
|
54
|
+
|
|
55
|
+
var RPCClientHost = /*#__PURE__*/function () {
|
|
56
|
+
function RPCClientHost() {
|
|
57
|
+
this.hostMap = new Map();
|
|
58
|
+
}
|
|
59
|
+
var _proto = RPCClientHost.prototype;
|
|
60
|
+
_proto.registerClient = function registerClient(requestPath, options) {
|
|
61
|
+
var client = new ProxyRPCClient(requestPath, options);
|
|
62
|
+
this.hostMap.set(requestPath, client);
|
|
63
|
+
return client;
|
|
64
|
+
};
|
|
65
|
+
return RPCClientHost;
|
|
66
|
+
}();
|
|
67
|
+
var RPCClientHost$1 = /*#__PURE__*/new RPCClientHost();
|
|
68
|
+
|
|
69
|
+
var RPCService = /*#__PURE__*/function () {
|
|
70
|
+
function RPCService(servicePath, options) {
|
|
71
|
+
this.handlersMap = new Map();
|
|
72
|
+
var _ref = options || {},
|
|
73
|
+
channel = _ref.channel,
|
|
74
|
+
handlers = _ref.handlers,
|
|
75
|
+
serviceHost = _ref.serviceHost;
|
|
76
|
+
this.servicePath = servicePath;
|
|
77
|
+
this.serviceHost = serviceHost;
|
|
78
|
+
if (channel) {
|
|
79
|
+
this.setChannel(channel);
|
|
80
|
+
}
|
|
81
|
+
this.registerHandlers(handlers);
|
|
82
|
+
}
|
|
83
|
+
var _proto = RPCService.prototype;
|
|
84
|
+
_proto.setChannel = function setChannel(channel) {
|
|
85
|
+
this.channel = channel;
|
|
86
|
+
this.channel.setService(this);
|
|
87
|
+
this.channel.on(this.handleMessage.bind(this));
|
|
88
|
+
};
|
|
89
|
+
_proto.registerHandlers = function registerHandlers(handlers) {
|
|
90
|
+
if (!handlers) return;
|
|
91
|
+
for (var _i = 0, _Object$entries = Object.entries(handlers); _i < _Object$entries.length; _i++) {
|
|
92
|
+
var _Object$entries$_i = _Object$entries[_i],
|
|
93
|
+
methodName = _Object$entries$_i[0],
|
|
94
|
+
handler = _Object$entries$_i[1];
|
|
95
|
+
this.registerHandler(methodName, handler);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
_proto.handleMessage = function handleMessage() {
|
|
99
|
+
var _this$channel;
|
|
100
|
+
(_this$channel = this.channel).onMessage.apply(_this$channel, arguments);
|
|
101
|
+
};
|
|
102
|
+
_proto.registerHandler = function registerHandler(methodName, handler) {
|
|
103
|
+
this.handlersMap.set(methodName, handler);
|
|
104
|
+
};
|
|
105
|
+
_proto.getHandler = function getHandler(methodName) {
|
|
106
|
+
return this.handlersMap.get(methodName);
|
|
107
|
+
};
|
|
108
|
+
return RPCService;
|
|
109
|
+
}();
|
|
110
|
+
|
|
111
|
+
var RPCServiceHost = /*#__PURE__*/function () {
|
|
112
|
+
function RPCServiceHost() {
|
|
113
|
+
this.hostMap = new Map();
|
|
114
|
+
}
|
|
115
|
+
var _proto = RPCServiceHost.prototype;
|
|
116
|
+
_proto.registerService = function registerService(servicePath, serviceHandlers) {
|
|
117
|
+
var service = new RPCService(servicePath, {
|
|
118
|
+
handlers: serviceHandlers
|
|
119
|
+
});
|
|
120
|
+
this.hostMap.set(servicePath, service);
|
|
121
|
+
return service;
|
|
122
|
+
};
|
|
123
|
+
return RPCServiceHost;
|
|
124
|
+
}();
|
|
125
|
+
var RPCServiceHost$1 = /*#__PURE__*/new RPCServiceHost();
|
|
126
|
+
|
|
127
|
+
function _defineProperties(target, props) {
|
|
128
|
+
for (var i = 0; i < props.length; i++) {
|
|
129
|
+
var descriptor = props[i];
|
|
130
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
131
|
+
descriptor.configurable = true;
|
|
132
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
133
|
+
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
137
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
138
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
139
|
+
Object.defineProperty(Constructor, "prototype", {
|
|
140
|
+
writable: false
|
|
141
|
+
});
|
|
142
|
+
return Constructor;
|
|
143
|
+
}
|
|
144
|
+
function _extends() {
|
|
145
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
146
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
147
|
+
var source = arguments[i];
|
|
148
|
+
for (var key in source) {
|
|
149
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
150
|
+
target[key] = source[key];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return target;
|
|
155
|
+
};
|
|
156
|
+
return _extends.apply(this, arguments);
|
|
157
|
+
}
|
|
158
|
+
function _inheritsLoose(subClass, superClass) {
|
|
159
|
+
subClass.prototype = Object.create(superClass.prototype);
|
|
160
|
+
subClass.prototype.constructor = subClass;
|
|
161
|
+
_setPrototypeOf(subClass, superClass);
|
|
162
|
+
}
|
|
163
|
+
function _setPrototypeOf(o, p) {
|
|
164
|
+
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
|
|
165
|
+
o.__proto__ = p;
|
|
166
|
+
return o;
|
|
167
|
+
};
|
|
168
|
+
return _setPrototypeOf(o, p);
|
|
169
|
+
}
|
|
170
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
171
|
+
if (source == null) return {};
|
|
172
|
+
var target = {};
|
|
173
|
+
var sourceKeys = Object.keys(source);
|
|
174
|
+
var key, i;
|
|
175
|
+
for (i = 0; i < sourceKeys.length; i++) {
|
|
176
|
+
key = sourceKeys[i];
|
|
177
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
178
|
+
target[key] = source[key];
|
|
179
|
+
}
|
|
180
|
+
return target;
|
|
181
|
+
}
|
|
182
|
+
function _assertThisInitialized(self) {
|
|
183
|
+
if (self === void 0) {
|
|
184
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
185
|
+
}
|
|
186
|
+
return self;
|
|
187
|
+
}
|
|
188
|
+
function _toPrimitive(input, hint) {
|
|
189
|
+
if (typeof input !== "object" || input === null) return input;
|
|
190
|
+
var prim = input[Symbol.toPrimitive];
|
|
191
|
+
if (prim !== undefined) {
|
|
192
|
+
var res = prim.call(input, hint || "default");
|
|
193
|
+
if (typeof res !== "object") return res;
|
|
194
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
195
|
+
}
|
|
196
|
+
return (hint === "string" ? String : Number)(input);
|
|
197
|
+
}
|
|
198
|
+
function _toPropertyKey(arg) {
|
|
199
|
+
var key = _toPrimitive(arg, "string");
|
|
200
|
+
return typeof key === "symbol" ? key : String(key);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
var RequestType;
|
|
204
|
+
(function (RequestType) {
|
|
205
|
+
RequestType["PromiseRequest"] = "pr";
|
|
206
|
+
RequestType["PromiseAbort"] = "pa";
|
|
207
|
+
RequestType["SignalRequest"] = "sr";
|
|
208
|
+
RequestType["SignalAbort"] = "sa";
|
|
209
|
+
})(RequestType || (RequestType = {}));
|
|
210
|
+
var ResponseType;
|
|
211
|
+
(function (ResponseType) {
|
|
212
|
+
ResponseType["ReturnSuccess"] = "rs";
|
|
213
|
+
ResponseType["ReturnFail"] = "rf";
|
|
214
|
+
ResponseType["PortSuccess"] = "ps";
|
|
215
|
+
ResponseType["PortFail"] = "pf";
|
|
216
|
+
})(ResponseType || (ResponseType = {}));
|
|
217
|
+
|
|
218
|
+
var SendMiddlewareLifecycle;
|
|
219
|
+
(function (SendMiddlewareLifecycle) {
|
|
220
|
+
SendMiddlewareLifecycle[SendMiddlewareLifecycle["Initial"] = 0] = "Initial";
|
|
221
|
+
SendMiddlewareLifecycle[SendMiddlewareLifecycle["Prepare"] = 10] = "Prepare";
|
|
222
|
+
SendMiddlewareLifecycle[SendMiddlewareLifecycle["Transform"] = 20] = "Transform";
|
|
223
|
+
SendMiddlewareLifecycle[SendMiddlewareLifecycle["DataOperation"] = 30] = "DataOperation";
|
|
224
|
+
SendMiddlewareLifecycle[SendMiddlewareLifecycle["Send"] = 40] = "Send";
|
|
225
|
+
SendMiddlewareLifecycle[SendMiddlewareLifecycle["Aborted"] = 100] = "Aborted";
|
|
226
|
+
})(SendMiddlewareLifecycle || (SendMiddlewareLifecycle = {}));
|
|
227
|
+
|
|
228
|
+
var serialize = function serialize(channel) {
|
|
229
|
+
var fn = function fn(value) {
|
|
230
|
+
return _extends({}, value, {
|
|
231
|
+
data: channel.writeBuffer.encode(value.data)
|
|
232
|
+
});
|
|
233
|
+
};
|
|
234
|
+
fn.lifecycle = SendMiddlewareLifecycle.DataOperation;
|
|
235
|
+
return fn;
|
|
236
|
+
};
|
|
237
|
+
var deserialize = function deserialize(channel) {
|
|
238
|
+
return function (value) {
|
|
239
|
+
var data = value.data;
|
|
240
|
+
var decoded = data;
|
|
241
|
+
try {
|
|
242
|
+
decoded = channel.readBuffer.decode(data);
|
|
243
|
+
} catch (err) {
|
|
244
|
+
console.error('[decode error]', data, err);
|
|
245
|
+
}
|
|
246
|
+
return _extends({}, value, {
|
|
247
|
+
data: decoded
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
var normalizeMessageChannelRawMessage = function normalizeMessageChannelRawMessage() {
|
|
253
|
+
return function (event) {
|
|
254
|
+
return {
|
|
255
|
+
event: event,
|
|
256
|
+
data: event.data,
|
|
257
|
+
ports: event.ports || []
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
};
|
|
261
|
+
function isBufferAvailable() {
|
|
262
|
+
return typeof Buffer !== 'undefined' && Buffer.isBuffer !== undefined;
|
|
263
|
+
}
|
|
264
|
+
function isBuffer(value) {
|
|
265
|
+
if (!isBufferAvailable()) {
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
return Buffer.isBuffer(value);
|
|
269
|
+
}
|
|
270
|
+
function arrayBufferToString(buffer) {
|
|
271
|
+
if (isBufferAvailable()) {
|
|
272
|
+
return Buffer.from(buffer).toString('utf8');
|
|
273
|
+
} else {
|
|
274
|
+
try {
|
|
275
|
+
return new TextDecoder('utf-8').decode(buffer);
|
|
276
|
+
} catch (e) {
|
|
277
|
+
var uint8Array = new Uint8Array(buffer);
|
|
278
|
+
return String.fromCharCode.apply(null, Array.from(uint8Array));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function bufferToString(buffer) {
|
|
283
|
+
if (isBufferAvailable() && Buffer.isBuffer(buffer)) {
|
|
284
|
+
return buffer.toString('utf8');
|
|
285
|
+
}
|
|
286
|
+
return String(buffer);
|
|
287
|
+
}
|
|
288
|
+
var normalizeWebSocketRawMessage = function normalizeWebSocketRawMessage() {
|
|
289
|
+
return function (eventOrData) {
|
|
290
|
+
if (eventOrData === undefined || eventOrData === null) {
|
|
291
|
+
console.warn('[normalizeWebSocketRawMessage] Received undefined or null data');
|
|
292
|
+
return {
|
|
293
|
+
event: null,
|
|
294
|
+
data: '',
|
|
295
|
+
ports: []
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
var normalizedData = '';
|
|
299
|
+
var event = null;
|
|
300
|
+
var ports = [];
|
|
301
|
+
if (eventOrData && typeof eventOrData === 'object' && 'data' in eventOrData) {
|
|
302
|
+
event = eventOrData;
|
|
303
|
+
var data = event.data;
|
|
304
|
+
ports = event.ports ? [].concat(event.ports) : [];
|
|
305
|
+
if (data === undefined || data === null) {
|
|
306
|
+
normalizedData = '';
|
|
307
|
+
} else if (typeof data === 'string') {
|
|
308
|
+
normalizedData = data;
|
|
309
|
+
} else if (isBuffer(data)) {
|
|
310
|
+
normalizedData = bufferToString(data);
|
|
311
|
+
} else if (data instanceof ArrayBuffer) {
|
|
312
|
+
normalizedData = arrayBufferToString(data);
|
|
313
|
+
} else if (data instanceof Blob) {
|
|
314
|
+
normalizedData = '';
|
|
315
|
+
} else {
|
|
316
|
+
normalizedData = String(data);
|
|
317
|
+
}
|
|
318
|
+
} else {
|
|
319
|
+
var actualData = eventOrData;
|
|
320
|
+
if ((arguments.length <= 1 ? 0 : arguments.length - 1) > 0 && (typeof eventOrData === 'string' || isBuffer(eventOrData))) {
|
|
321
|
+
actualData = eventOrData;
|
|
322
|
+
}
|
|
323
|
+
if (isBuffer(actualData)) {
|
|
324
|
+
normalizedData = bufferToString(actualData);
|
|
325
|
+
} else if (typeof actualData === 'string') {
|
|
326
|
+
normalizedData = actualData;
|
|
327
|
+
} else if (actualData instanceof ArrayBuffer) {
|
|
328
|
+
normalizedData = arrayBufferToString(actualData);
|
|
329
|
+
} else {
|
|
330
|
+
normalizedData = String(actualData);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (normalizedData === undefined || normalizedData === null) {
|
|
334
|
+
normalizedData = '';
|
|
335
|
+
}
|
|
336
|
+
return {
|
|
337
|
+
event: event,
|
|
338
|
+
data: normalizedData,
|
|
339
|
+
ports: ports
|
|
340
|
+
};
|
|
341
|
+
};
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
var handleResponse = function handleResponse(protocol) {
|
|
345
|
+
return function (message) {
|
|
346
|
+
if (!message) return message;
|
|
347
|
+
var data = message.data;
|
|
348
|
+
var header = data[0];
|
|
349
|
+
var body = data[1];
|
|
350
|
+
var type = header[0];
|
|
351
|
+
if (Object.values(RequestType).includes(type)) {
|
|
352
|
+
return message;
|
|
353
|
+
}
|
|
354
|
+
var seqId = header[1];
|
|
355
|
+
var findDefer = protocol.ongoingRequests.get("" + seqId);
|
|
356
|
+
if (findDefer) {
|
|
357
|
+
protocol.ongoingRequests["delete"]("" + seqId);
|
|
358
|
+
if (type === ResponseType.PortSuccess) {
|
|
359
|
+
findDefer.resolve(message.ports[0]);
|
|
360
|
+
} else if (type === ResponseType.ReturnFail) {
|
|
361
|
+
findDefer.reject(body[0]);
|
|
362
|
+
} else findDefer.resolve(body[0]);
|
|
363
|
+
} else {
|
|
364
|
+
var findListener = protocol.requestEvents.get("" + seqId);
|
|
365
|
+
if (findListener) {
|
|
366
|
+
findListener.apply(void 0, body);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return null;
|
|
370
|
+
};
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
var prepareNormalData = function prepareNormalData(channel) {
|
|
374
|
+
var fn = function fn(props) {
|
|
375
|
+
var requestPath = '';
|
|
376
|
+
var methodName = '';
|
|
377
|
+
var params = [];
|
|
378
|
+
var seqId = channel.seqId;
|
|
379
|
+
var isOptionsRequest = false;
|
|
380
|
+
if (typeof props === 'string') {
|
|
381
|
+
requestPath = props;
|
|
382
|
+
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
383
|
+
args[_key3 - 1] = arguments[_key3];
|
|
384
|
+
}
|
|
385
|
+
methodName = args[0];
|
|
386
|
+
params = args.slice(1);
|
|
387
|
+
} else {
|
|
388
|
+
requestPath = props.requestPath;
|
|
389
|
+
methodName = props.methodName;
|
|
390
|
+
isOptionsRequest = props.isOptionsRequest;
|
|
391
|
+
params = [].concat(props.args);
|
|
392
|
+
}
|
|
393
|
+
var header = [RequestType.PromiseRequest, seqId, requestPath, methodName];
|
|
394
|
+
var body = params;
|
|
395
|
+
return {
|
|
396
|
+
seqId: seqId,
|
|
397
|
+
isOptionsRequest: isOptionsRequest,
|
|
398
|
+
data: [header, body]
|
|
399
|
+
};
|
|
400
|
+
};
|
|
401
|
+
fn.lifecycle = SendMiddlewareLifecycle.Prepare;
|
|
402
|
+
return fn;
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
var isEventMethod = function isEventMethod(name) {
|
|
406
|
+
if (typeof name !== 'string') return false;
|
|
407
|
+
return name[0] === 'o' && name[1] === 'n' && isAscii.isUpperAsciiLetter(name.charCodeAt(2));
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
var updateSeqInfo = function updateSeqInfo(channelProtocol) {
|
|
411
|
+
var fn = function fn(value) {
|
|
412
|
+
var data = value.data,
|
|
413
|
+
seqId = value.seqId;
|
|
414
|
+
var header = data[0];
|
|
415
|
+
var body = data[1];
|
|
416
|
+
var methodName = header[3];
|
|
417
|
+
if (methodName && isEventMethod(methodName)) {
|
|
418
|
+
channelProtocol.requestEvents.set("" + seqId, body[0]);
|
|
419
|
+
data[1] = [];
|
|
420
|
+
} else {
|
|
421
|
+
var returnValue = deferred.createDeferred();
|
|
422
|
+
channelProtocol.ongoingRequests.set("" + seqId, returnValue);
|
|
423
|
+
value.returnValue = returnValue;
|
|
424
|
+
}
|
|
425
|
+
return value;
|
|
426
|
+
};
|
|
427
|
+
fn.lifecycle = SendMiddlewareLifecycle.Transform;
|
|
428
|
+
return fn;
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
var sendRequest = function sendRequest(channelProtocol) {
|
|
432
|
+
var fn = function fn(value) {
|
|
433
|
+
var data = value.data,
|
|
434
|
+
transfer = value.transfer;
|
|
435
|
+
if (transfer) channelProtocol.send(data, transfer);else channelProtocol.send(data);
|
|
436
|
+
return value;
|
|
437
|
+
};
|
|
438
|
+
fn.lifecycle = SendMiddlewareLifecycle.Send;
|
|
439
|
+
return fn;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
var resumeMiddlewares = function resumeMiddlewares(middlewares, entry) {
|
|
443
|
+
var start = false;
|
|
444
|
+
return middlewares.reduce(function (a, b) {
|
|
445
|
+
var _a$middlewareContext;
|
|
446
|
+
if (!b.lifecycle) return a;
|
|
447
|
+
if (a.lifecycle >= b.lifecycle && b.displayName === a.methodName) {
|
|
448
|
+
start = true;
|
|
449
|
+
}
|
|
450
|
+
if (!start) return a;
|
|
451
|
+
var targetLifecycle = a == null ? void 0 : (_a$middlewareContext = a.middlewareContext) == null ? void 0 : _a$middlewareContext.minLifecycle;
|
|
452
|
+
var currentLifecycle = b == null ? void 0 : b.lifecycle;
|
|
453
|
+
if (targetLifecycle && currentLifecycle && currentLifecycle < targetLifecycle) {
|
|
454
|
+
return a;
|
|
455
|
+
}
|
|
456
|
+
return b(a);
|
|
457
|
+
}, entry);
|
|
458
|
+
};
|
|
459
|
+
var runMiddlewares = function runMiddlewares(middlewares, args, _context) {
|
|
460
|
+
var context = _extends({
|
|
461
|
+
isResumed: false,
|
|
462
|
+
startLifecycle: SendMiddlewareLifecycle.Initial,
|
|
463
|
+
minLifecycle: SendMiddlewareLifecycle.Initial
|
|
464
|
+
}, _context || {});
|
|
465
|
+
return middlewares.reduce(function (a, b, index) {
|
|
466
|
+
var _a$middlewareContext2;
|
|
467
|
+
if (!index) return b.apply(void 0, a);
|
|
468
|
+
if (index === 1) {
|
|
469
|
+
a.middlewareContext = context;
|
|
470
|
+
}
|
|
471
|
+
var targetLifecycle = a == null ? void 0 : (_a$middlewareContext2 = a.middlewareContext) == null ? void 0 : _a$middlewareContext2.minLifecycle;
|
|
472
|
+
var currentLifecycle = b == null ? void 0 : b.lifecycle;
|
|
473
|
+
if (targetLifecycle && currentLifecycle && currentLifecycle < targetLifecycle) {
|
|
474
|
+
return a;
|
|
475
|
+
}
|
|
476
|
+
return b(a);
|
|
477
|
+
}, args);
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
var ReadBaseBuffer = function ReadBaseBuffer() {};
|
|
481
|
+
|
|
482
|
+
var DataType;
|
|
483
|
+
(function (DataType) {
|
|
484
|
+
DataType[DataType["Undefined"] = 0] = "Undefined";
|
|
485
|
+
DataType[DataType["String"] = 1] = "String";
|
|
486
|
+
DataType[DataType["Buffer"] = 2] = "Buffer";
|
|
487
|
+
DataType[DataType["VSBuffer"] = 3] = "VSBuffer";
|
|
488
|
+
DataType[DataType["Array"] = 4] = "Array";
|
|
489
|
+
DataType[DataType["Object"] = 5] = "Object";
|
|
490
|
+
DataType[DataType["Int"] = 6] = "Int";
|
|
491
|
+
})(DataType || (DataType = {}));
|
|
492
|
+
var ReadBuffer = /*#__PURE__*/function (_ReadBaseBuffer) {
|
|
493
|
+
_inheritsLoose(ReadBuffer, _ReadBaseBuffer);
|
|
494
|
+
function ReadBuffer() {
|
|
495
|
+
return _ReadBaseBuffer.apply(this, arguments) || this;
|
|
496
|
+
}
|
|
497
|
+
var _proto = ReadBuffer.prototype;
|
|
498
|
+
_proto.decode = function decode(data) {
|
|
499
|
+
if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
|
|
500
|
+
var decoder = new TextDecoder();
|
|
501
|
+
var text = decoder.decode(data);
|
|
502
|
+
return JSON.parse(text);
|
|
503
|
+
}
|
|
504
|
+
return JSON.parse(data);
|
|
505
|
+
};
|
|
506
|
+
_proto.getFormat = function getFormat() {
|
|
507
|
+
return 'json';
|
|
508
|
+
};
|
|
509
|
+
return ReadBuffer;
|
|
510
|
+
}(ReadBaseBuffer);
|
|
511
|
+
|
|
512
|
+
var WriteBaseBuffer = function WriteBaseBuffer() {};
|
|
513
|
+
|
|
514
|
+
var WriteBuffer = /*#__PURE__*/function (_WriteBaseBuffer) {
|
|
515
|
+
_inheritsLoose(WriteBuffer, _WriteBaseBuffer);
|
|
516
|
+
function WriteBuffer() {
|
|
517
|
+
return _WriteBaseBuffer.apply(this, arguments) || this;
|
|
518
|
+
}
|
|
519
|
+
var _proto = WriteBuffer.prototype;
|
|
520
|
+
_proto.encode = function encode(data) {
|
|
521
|
+
return JSON.stringify(data);
|
|
522
|
+
};
|
|
523
|
+
_proto.getFormat = function getFormat() {
|
|
524
|
+
return 'json';
|
|
525
|
+
};
|
|
526
|
+
return WriteBuffer;
|
|
527
|
+
}(WriteBaseBuffer);
|
|
528
|
+
|
|
529
|
+
var _FORMAT_INFO;
|
|
530
|
+
var SerializationFormat;
|
|
531
|
+
(function (SerializationFormat) {
|
|
532
|
+
SerializationFormat["JSON"] = "json";
|
|
533
|
+
SerializationFormat["MESSAGEPACK"] = "msgpack";
|
|
534
|
+
SerializationFormat["CBOR"] = "cbor";
|
|
535
|
+
SerializationFormat["PROTOBUF"] = "protobuf";
|
|
536
|
+
SerializationFormat["CUSTOM"] = "custom";
|
|
537
|
+
})(SerializationFormat || (SerializationFormat = {}));
|
|
538
|
+
var FORMAT_INFO = (_FORMAT_INFO = {}, _FORMAT_INFO[SerializationFormat.JSON] = {
|
|
539
|
+
format: SerializationFormat.JSON,
|
|
540
|
+
name: 'JSON',
|
|
541
|
+
description: 'JavaScript Object Notation - Human-readable text format',
|
|
542
|
+
mimeType: 'application/json',
|
|
543
|
+
isBinary: false,
|
|
544
|
+
isText: true
|
|
545
|
+
}, _FORMAT_INFO[SerializationFormat.MESSAGEPACK] = {
|
|
546
|
+
format: SerializationFormat.MESSAGEPACK,
|
|
547
|
+
name: 'MessagePack',
|
|
548
|
+
description: 'Binary serialization format - High performance, compact size',
|
|
549
|
+
mimeType: 'application/x-msgpack',
|
|
550
|
+
isBinary: true,
|
|
551
|
+
isText: false
|
|
552
|
+
}, _FORMAT_INFO[SerializationFormat.CBOR] = {
|
|
553
|
+
format: SerializationFormat.CBOR,
|
|
554
|
+
name: 'CBOR',
|
|
555
|
+
description: 'Concise Binary Object Representation (RFC 7049)',
|
|
556
|
+
mimeType: 'application/cbor',
|
|
557
|
+
isBinary: true,
|
|
558
|
+
isText: false
|
|
559
|
+
}, _FORMAT_INFO[SerializationFormat.PROTOBUF] = {
|
|
560
|
+
format: SerializationFormat.PROTOBUF,
|
|
561
|
+
name: 'Protocol Buffers',
|
|
562
|
+
description: 'Google Protocol Buffers - Schema-based binary format',
|
|
563
|
+
mimeType: 'application/x-protobuf',
|
|
564
|
+
isBinary: true,
|
|
565
|
+
isText: false
|
|
566
|
+
}, _FORMAT_INFO[SerializationFormat.CUSTOM] = {
|
|
567
|
+
format: SerializationFormat.CUSTOM,
|
|
568
|
+
name: 'Custom',
|
|
569
|
+
description: 'User-defined custom serialization format',
|
|
570
|
+
mimeType: 'application/octet-stream',
|
|
571
|
+
isBinary: true,
|
|
572
|
+
isText: false
|
|
573
|
+
}, _FORMAT_INFO);
|
|
574
|
+
|
|
575
|
+
var BufferFactory = /*#__PURE__*/function () {
|
|
576
|
+
function BufferFactory() {}
|
|
577
|
+
BufferFactory.registerReadBuffer = function registerReadBuffer(format, factory) {
|
|
578
|
+
this.readBufferRegistry.set(format, factory);
|
|
579
|
+
};
|
|
580
|
+
BufferFactory.registerWriteBuffer = function registerWriteBuffer(format, factory) {
|
|
581
|
+
this.writeBufferRegistry.set(format, factory);
|
|
582
|
+
};
|
|
583
|
+
BufferFactory.createReadBuffer = function createReadBuffer(format) {
|
|
584
|
+
if (format === void 0) {
|
|
585
|
+
format = SerializationFormat.JSON;
|
|
586
|
+
}
|
|
587
|
+
var factory = this.readBufferRegistry.get(format);
|
|
588
|
+
if (factory) {
|
|
589
|
+
return factory();
|
|
590
|
+
}
|
|
591
|
+
switch (format) {
|
|
592
|
+
case SerializationFormat.JSON:
|
|
593
|
+
return new ReadBuffer();
|
|
594
|
+
case SerializationFormat.MESSAGEPACK:
|
|
595
|
+
try {
|
|
596
|
+
throw new Error('MessagePack not available. Install @msgpack/msgpack and register it.');
|
|
597
|
+
} catch (e) {
|
|
598
|
+
throw new Error("Unsupported read buffer format: " + format + ". Register a custom implementation.");
|
|
599
|
+
}
|
|
600
|
+
default:
|
|
601
|
+
throw new Error("Unsupported read buffer format: " + format);
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
BufferFactory.createWriteBuffer = function createWriteBuffer(format) {
|
|
605
|
+
if (format === void 0) {
|
|
606
|
+
format = SerializationFormat.JSON;
|
|
607
|
+
}
|
|
608
|
+
var factory = this.writeBufferRegistry.get(format);
|
|
609
|
+
if (factory) {
|
|
610
|
+
return factory();
|
|
611
|
+
}
|
|
612
|
+
switch (format) {
|
|
613
|
+
case SerializationFormat.JSON:
|
|
614
|
+
return new WriteBuffer();
|
|
615
|
+
case SerializationFormat.MESSAGEPACK:
|
|
616
|
+
try {
|
|
617
|
+
throw new Error('MessagePack not available. Install @msgpack/msgpack and register it.');
|
|
618
|
+
} catch (e) {
|
|
619
|
+
throw new Error("Unsupported write buffer format: " + format + ". Register a custom implementation.");
|
|
620
|
+
}
|
|
621
|
+
default:
|
|
622
|
+
throw new Error("Unsupported write buffer format: " + format);
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
BufferFactory.getRegisteredFormats = function getRegisteredFormats() {
|
|
626
|
+
var formats = new Set();
|
|
627
|
+
this.readBufferRegistry.forEach(function (_, format) {
|
|
628
|
+
return formats.add(format);
|
|
629
|
+
});
|
|
630
|
+
this.writeBufferRegistry.forEach(function (_, format) {
|
|
631
|
+
return formats.add(format);
|
|
632
|
+
});
|
|
633
|
+
return Array.from(formats);
|
|
634
|
+
};
|
|
635
|
+
return BufferFactory;
|
|
636
|
+
}();
|
|
637
|
+
BufferFactory.readBufferRegistry = /*#__PURE__*/new Map();
|
|
638
|
+
BufferFactory.writeBufferRegistry = /*#__PURE__*/new Map();
|
|
639
|
+
BufferFactory.registerReadBuffer(SerializationFormat.JSON, function () {
|
|
640
|
+
return new ReadBuffer();
|
|
641
|
+
});
|
|
642
|
+
BufferFactory.registerWriteBuffer(SerializationFormat.JSON, function () {
|
|
643
|
+
return new WriteBuffer();
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
var ERROR = 'Error';
|
|
647
|
+
var isArray = Array.isArray;
|
|
648
|
+
var isObject = function isObject(val) {
|
|
649
|
+
return val !== null && typeof val === 'object' && !isArray(val);
|
|
650
|
+
};
|
|
651
|
+
var isFunction = function isFunction(val) {
|
|
652
|
+
return typeof val === 'function';
|
|
653
|
+
};
|
|
654
|
+
var UNDEFINED = void 0;
|
|
655
|
+
|
|
656
|
+
var jsonrpc = '2.0';
|
|
657
|
+
var makeRequest = function makeRequest(id, method, params, remoteStack) {
|
|
658
|
+
var x = {
|
|
659
|
+
jsonrpc: jsonrpc,
|
|
660
|
+
id: id,
|
|
661
|
+
method: method,
|
|
662
|
+
params: params,
|
|
663
|
+
remoteStack: remoteStack
|
|
664
|
+
};
|
|
665
|
+
deleteUndefined(x, 'id');
|
|
666
|
+
deleteFalsy(x, 'remoteStack');
|
|
667
|
+
return x;
|
|
668
|
+
};
|
|
669
|
+
var makeSuccessResponse = function makeSuccessResponse(id, result) {
|
|
670
|
+
var x = {
|
|
671
|
+
jsonrpc: jsonrpc,
|
|
672
|
+
id: id,
|
|
673
|
+
result: result
|
|
674
|
+
};
|
|
675
|
+
deleteUndefined(x, 'id');
|
|
676
|
+
return x;
|
|
677
|
+
};
|
|
678
|
+
var makeErrorResponse = function makeErrorResponse(id, code, message, data) {
|
|
679
|
+
if (id === UNDEFINED) id = null;
|
|
680
|
+
code = Math.floor(code);
|
|
681
|
+
if (Number.isNaN(code)) code = -1;
|
|
682
|
+
var x = {
|
|
683
|
+
jsonrpc: jsonrpc,
|
|
684
|
+
id: id,
|
|
685
|
+
error: {
|
|
686
|
+
code: code,
|
|
687
|
+
message: message,
|
|
688
|
+
data: data
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
deleteUndefined(x.error, 'data');
|
|
692
|
+
return x;
|
|
693
|
+
};
|
|
694
|
+
var ErrorResponseParseError = function ErrorResponseParseError(e, mapper) {
|
|
695
|
+
var obj = ErrorResponseMapped({}, e, mapper);
|
|
696
|
+
var o = obj.error;
|
|
697
|
+
o.code = -32700;
|
|
698
|
+
o.message = 'Parse error';
|
|
699
|
+
return obj;
|
|
700
|
+
};
|
|
701
|
+
var ErrorResponseInvalidRequest = function ErrorResponseInvalidRequest(id) {
|
|
702
|
+
return makeErrorResponse(id, -32600, 'Invalid Request');
|
|
703
|
+
};
|
|
704
|
+
var ErrorResponseMethodNotFound = function ErrorResponseMethodNotFound(id) {
|
|
705
|
+
return makeErrorResponse(id, -32601, 'Method not found');
|
|
706
|
+
};
|
|
707
|
+
var ErrorResponseInvalidParams = function ErrorResponseInvalidParams(id) {
|
|
708
|
+
return makeErrorResponse(id, -32602, 'Invalid params');
|
|
709
|
+
};
|
|
710
|
+
var ErrorResponseInternalError = function ErrorResponseInternalError(id) {
|
|
711
|
+
return makeErrorResponse(id, -32603, 'Internal error');
|
|
712
|
+
};
|
|
713
|
+
var ErrorResponseMapped = function ErrorResponseMapped(request, e, mapper) {
|
|
714
|
+
var id = request.id;
|
|
715
|
+
var _mapper = mapper(e, request),
|
|
716
|
+
code = _mapper.code,
|
|
717
|
+
message = _mapper.message,
|
|
718
|
+
data = _mapper.data;
|
|
719
|
+
return makeErrorResponse(id, code, message, data);
|
|
720
|
+
};
|
|
721
|
+
var defaultErrorMapper = function defaultErrorMapper(stack, code) {
|
|
722
|
+
if (stack === void 0) {
|
|
723
|
+
stack = '';
|
|
724
|
+
}
|
|
725
|
+
if (code === void 0) {
|
|
726
|
+
code = -1;
|
|
727
|
+
}
|
|
728
|
+
return function (e) {
|
|
729
|
+
var message = toString('', function () {
|
|
730
|
+
return e.message;
|
|
731
|
+
});
|
|
732
|
+
var type = toString(ERROR, function (ctor) {
|
|
733
|
+
if (ctor === void 0) {
|
|
734
|
+
ctor = e.constructor;
|
|
735
|
+
}
|
|
736
|
+
return isFunction(ctor) && ctor.name;
|
|
737
|
+
});
|
|
738
|
+
var E = globalThis.DOMException;
|
|
739
|
+
if (E && e instanceof E) {
|
|
740
|
+
type = "DOMException:" + e.name;
|
|
741
|
+
}
|
|
742
|
+
var eType = typeof e;
|
|
743
|
+
if (eType === 'string' || eType === 'number' || eType === 'boolean' || eType === 'bigint') {
|
|
744
|
+
type = ERROR;
|
|
745
|
+
message = String(e);
|
|
746
|
+
}
|
|
747
|
+
var data = stack ? {
|
|
748
|
+
stack: stack,
|
|
749
|
+
type: type
|
|
750
|
+
} : {
|
|
751
|
+
type: type
|
|
752
|
+
};
|
|
753
|
+
return {
|
|
754
|
+
code: code,
|
|
755
|
+
message: message,
|
|
756
|
+
data: data
|
|
757
|
+
};
|
|
758
|
+
};
|
|
759
|
+
};
|
|
760
|
+
var isJSONRPCObject = function isJSONRPCObject(data) {
|
|
761
|
+
if (!isObject(data)) return false;
|
|
762
|
+
if (!('jsonrpc' in data)) return false;
|
|
763
|
+
var obj = data;
|
|
764
|
+
if (obj.jsonrpc !== jsonrpc) return false;
|
|
765
|
+
if ('params' in obj) {
|
|
766
|
+
var params = obj.params;
|
|
767
|
+
if (!isArray(params) && !isObject(params)) return false;
|
|
768
|
+
}
|
|
769
|
+
return true;
|
|
770
|
+
};
|
|
771
|
+
var toString = function toString(_default, val) {
|
|
772
|
+
try {
|
|
773
|
+
var v = val();
|
|
774
|
+
if (v === UNDEFINED) return _default;
|
|
775
|
+
return String(v);
|
|
776
|
+
} catch (_unused) {
|
|
777
|
+
return _default;
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
var deleteUndefined = function deleteUndefined(x, key) {
|
|
781
|
+
if (x[key] === UNDEFINED) delete x[key];
|
|
782
|
+
};
|
|
783
|
+
var deleteFalsy = function deleteFalsy(x, key) {
|
|
784
|
+
if (!x[key]) delete x[key];
|
|
785
|
+
};
|
|
786
|
+
|
|
787
|
+
(function (JSONRPCErrorCode) {
|
|
788
|
+
JSONRPCErrorCode[JSONRPCErrorCode["ParseError"] = -32700] = "ParseError";
|
|
789
|
+
JSONRPCErrorCode[JSONRPCErrorCode["InvalidRequest"] = -32600] = "InvalidRequest";
|
|
790
|
+
JSONRPCErrorCode[JSONRPCErrorCode["MethodNotFound"] = -32601] = "MethodNotFound";
|
|
791
|
+
JSONRPCErrorCode[JSONRPCErrorCode["InvalidParams"] = -32602] = "InvalidParams";
|
|
792
|
+
JSONRPCErrorCode[JSONRPCErrorCode["InternalError"] = -32603] = "InternalError";
|
|
793
|
+
JSONRPCErrorCode[JSONRPCErrorCode["ServerErrorStart"] = -32000] = "ServerErrorStart";
|
|
794
|
+
JSONRPCErrorCode[JSONRPCErrorCode["ServerErrorEnd"] = -32099] = "ServerErrorEnd";
|
|
795
|
+
})(exports.JSONRPCErrorCode || (exports.JSONRPCErrorCode = {}));
|
|
796
|
+
|
|
797
|
+
var createErrorResponseBody = function createErrorResponseBody(error, request) {
|
|
798
|
+
var mapper = defaultErrorMapper(error instanceof Error ? error.stack : '', exports.JSONRPCErrorCode.InternalError);
|
|
799
|
+
if (request) {
|
|
800
|
+
var errorResponse = ErrorResponseMapped(request, error, mapper);
|
|
801
|
+
return errorResponse.error;
|
|
802
|
+
}
|
|
803
|
+
var _mapper = mapper(error, {}),
|
|
804
|
+
code = _mapper.code,
|
|
805
|
+
message = _mapper.message,
|
|
806
|
+
data = _mapper.data;
|
|
807
|
+
return {
|
|
808
|
+
code: code,
|
|
809
|
+
message: message,
|
|
810
|
+
data: data
|
|
811
|
+
};
|
|
812
|
+
};
|
|
813
|
+
var handleRequest = function handleRequest(protocol) {
|
|
814
|
+
return function (message) {
|
|
815
|
+
var service = protocol.service;
|
|
816
|
+
var data = message.data;
|
|
817
|
+
var header = data[0];
|
|
818
|
+
var body = data[1];
|
|
819
|
+
var type = header[0];
|
|
820
|
+
if (Object.values(ResponseType).includes(type)) {
|
|
821
|
+
return message;
|
|
822
|
+
}
|
|
823
|
+
var seqId = header[1];
|
|
824
|
+
var requestPath = header[2];
|
|
825
|
+
var methodName = header[3];
|
|
826
|
+
var args = body[0];
|
|
827
|
+
var jsonrpcRequest = makeRequest(seqId, methodName, args);
|
|
828
|
+
var handler = service.getHandler(methodName);
|
|
829
|
+
if (!handler) {
|
|
830
|
+
var errorResponse = ErrorResponseMethodNotFound(seqId);
|
|
831
|
+
var responseHeader = [ResponseType.ReturnFail, seqId];
|
|
832
|
+
var responseBody = [errorResponse.error];
|
|
833
|
+
protocol.sendReply(protocol.writeBuffer.encode([responseHeader, responseBody]));
|
|
834
|
+
return message;
|
|
835
|
+
}
|
|
836
|
+
var _result = handler(args);
|
|
837
|
+
var result = Promise.resolve(_result);
|
|
838
|
+
if (isPromise(result)) {
|
|
839
|
+
result.then(function (response) {
|
|
840
|
+
var responseHeader = [ResponseType.ReturnSuccess, seqId];
|
|
841
|
+
var responseBody = [];
|
|
842
|
+
var sendData = null;
|
|
843
|
+
try {
|
|
844
|
+
responseBody = [response];
|
|
845
|
+
sendData = protocol.writeBuffer.encode([responseHeader, responseBody]);
|
|
846
|
+
} catch (err) {
|
|
847
|
+
var errorBody = createErrorResponseBody(err, jsonrpcRequest);
|
|
848
|
+
responseBody = [errorBody];
|
|
849
|
+
sendData = protocol.writeBuffer.encode([responseHeader, responseBody]);
|
|
850
|
+
console.error("[handleRequest sendReply encode error ] " + requestPath + " " + methodName, err);
|
|
851
|
+
}
|
|
852
|
+
protocol.sendReply(sendData);
|
|
853
|
+
}, function (err) {
|
|
854
|
+
var errorBody = createErrorResponseBody(err, jsonrpcRequest);
|
|
855
|
+
var responseHeader = [ResponseType.ReturnFail, seqId];
|
|
856
|
+
var responseBody = [errorBody];
|
|
857
|
+
protocol.sendReply(protocol.writeBuffer.encode([responseHeader, responseBody]));
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
return message;
|
|
861
|
+
};
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
var AbstractChannelProtocol = /*#__PURE__*/function (_Disposable) {
|
|
865
|
+
_inheritsLoose(AbstractChannelProtocol, _Disposable);
|
|
866
|
+
function AbstractChannelProtocol(props) {
|
|
867
|
+
var _this;
|
|
868
|
+
_this = _Disposable.call(this) || this;
|
|
869
|
+
_this._seqId = -1;
|
|
870
|
+
_this._onMessageMiddleware = [normalizeMessageChannelRawMessage, deserialize, handleRequest, handleResponse];
|
|
871
|
+
_this._senderMiddleware = [prepareNormalData, updateSeqInfo, serialize, sendRequest];
|
|
872
|
+
_this._readBuffer = null;
|
|
873
|
+
_this._writeBuffer = null;
|
|
874
|
+
_this._isConnected = true;
|
|
875
|
+
_this.ongoingRequests = new Map();
|
|
876
|
+
_this.pendingSendEntries = new Set();
|
|
877
|
+
_this.requestEvents = new Map();
|
|
878
|
+
_this.onDidConnectedEvent = new emitter.Event({
|
|
879
|
+
name: 'on-did-connected'
|
|
880
|
+
});
|
|
881
|
+
_this.onDidConnected = _this.onDidConnectedEvent.subscribe;
|
|
882
|
+
_this.onDidDisconnectedEvent = new emitter.Event({
|
|
883
|
+
name: 'on-did-disconnect'
|
|
884
|
+
});
|
|
885
|
+
_this.onDidDisconnected = _this.onDidDisconnectedEvent.subscribe;
|
|
886
|
+
var _ref = props || {},
|
|
887
|
+
description = _ref.description,
|
|
888
|
+
masterProcessName = _ref.masterProcessName,
|
|
889
|
+
_ref$connected = _ref.connected,
|
|
890
|
+
connected = _ref$connected === void 0 ? true : _ref$connected,
|
|
891
|
+
_ref$serializationFor = _ref.serializationFormat,
|
|
892
|
+
serializationFormat = _ref$serializationFor === void 0 ? SerializationFormat.JSON : _ref$serializationFor,
|
|
893
|
+
readBuffer = _ref.readBuffer,
|
|
894
|
+
writeBuffer = _ref.writeBuffer;
|
|
895
|
+
_this._description = description;
|
|
896
|
+
_this._isConnected = connected;
|
|
897
|
+
_this._masterProcessName = masterProcessName;
|
|
898
|
+
_this._serializationFormat = serializationFormat;
|
|
899
|
+
if (readBuffer) {
|
|
900
|
+
_this._readBuffer = readBuffer;
|
|
901
|
+
}
|
|
902
|
+
if (writeBuffer) {
|
|
903
|
+
_this._writeBuffer = writeBuffer;
|
|
904
|
+
}
|
|
905
|
+
_this._key = id.generateRandomKey();
|
|
906
|
+
_this.registerDisposable(_this.onDidConnected(_this.didConnected.bind(_assertThisInitialized(_this))));
|
|
907
|
+
_this._onMessageMiddleware = _this.decorateOnMessageMiddleware(_this._onMessageMiddleware);
|
|
908
|
+
_this._senderMiddleware = _this.decorateSendMiddleware(_this._senderMiddleware);
|
|
909
|
+
_this.applyOnMessageMiddleware(_this._onMessageMiddleware);
|
|
910
|
+
_this.applySendMiddleware(_this._senderMiddleware);
|
|
911
|
+
return _this;
|
|
912
|
+
}
|
|
913
|
+
var _proto = AbstractChannelProtocol.prototype;
|
|
914
|
+
_proto.setService = function setService(service) {
|
|
915
|
+
this._service = service;
|
|
916
|
+
};
|
|
917
|
+
_proto.setSerializationFormat = function setSerializationFormat(format) {
|
|
918
|
+
if (this._serializationFormat !== format) {
|
|
919
|
+
this._serializationFormat = format;
|
|
920
|
+
this._readBuffer = null;
|
|
921
|
+
this._writeBuffer = null;
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
_proto.addPendingSendEntry = function addPendingSendEntry(entry) {
|
|
925
|
+
this.pendingSendEntries.add(entry);
|
|
926
|
+
};
|
|
927
|
+
_proto.decorateSendMiddleware = function decorateSendMiddleware(middlewares) {
|
|
928
|
+
return middlewares;
|
|
929
|
+
};
|
|
930
|
+
_proto.decorateOnMessageMiddleware = function decorateOnMessageMiddleware(middlewares) {
|
|
931
|
+
return middlewares;
|
|
932
|
+
};
|
|
933
|
+
_proto.applyOnMessageMiddleware = function applyOnMessageMiddleware(fns) {
|
|
934
|
+
var _this2 = this;
|
|
935
|
+
var copy = [].concat(fns);
|
|
936
|
+
this._onMessageMiddleware = [];
|
|
937
|
+
copy.forEach(function (fn) {
|
|
938
|
+
if (typeof fn === 'function') {
|
|
939
|
+
_this2._onMessageMiddleware.push(fn(_this2));
|
|
940
|
+
}
|
|
941
|
+
});
|
|
942
|
+
};
|
|
943
|
+
_proto.applySendMiddleware = function applySendMiddleware(fns) {
|
|
944
|
+
var _this3 = this;
|
|
945
|
+
var copy = [].concat(fns);
|
|
946
|
+
this._senderMiddleware = [];
|
|
947
|
+
copy.forEach(function (fn) {
|
|
948
|
+
if (typeof fn === 'function') {
|
|
949
|
+
_this3._senderMiddleware.push(fn(_this3));
|
|
950
|
+
}
|
|
951
|
+
});
|
|
952
|
+
};
|
|
953
|
+
_proto.isConnected = function isConnected() {
|
|
954
|
+
return this._isConnected;
|
|
955
|
+
};
|
|
956
|
+
_proto.send = function send() {
|
|
957
|
+
throw new Error('send method is not implemented');
|
|
958
|
+
};
|
|
959
|
+
_proto.on = function on() {
|
|
960
|
+
throw new Error('onMessage method is not implemented');
|
|
961
|
+
};
|
|
962
|
+
_proto.resumePendingEntry = function resumePendingEntry() {
|
|
963
|
+
var _this4 = this;
|
|
964
|
+
this.pendingSendEntries.forEach(function (entry) {
|
|
965
|
+
_this4.pendingSendEntries["delete"](entry);
|
|
966
|
+
resumeMiddlewares(_this4.senderMiddleware, entry);
|
|
967
|
+
});
|
|
968
|
+
};
|
|
969
|
+
_proto.didConnected = function didConnected() {
|
|
970
|
+
this.resumePendingEntry();
|
|
971
|
+
};
|
|
972
|
+
_proto.connect = function connect() {
|
|
973
|
+
this.onDidConnectedEvent.fire();
|
|
974
|
+
};
|
|
975
|
+
_proto.activate = function activate() {
|
|
976
|
+
this._isConnected = true;
|
|
977
|
+
this.onDidConnectedEvent.fire();
|
|
978
|
+
};
|
|
979
|
+
_proto.disconnect = function disconnect() {
|
|
980
|
+
this._isConnected = false;
|
|
981
|
+
this.onDidDisconnectedEvent.fire();
|
|
982
|
+
};
|
|
983
|
+
_proto.makeRequest = function makeRequest() {
|
|
984
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
985
|
+
args[_key] = arguments[_key];
|
|
986
|
+
}
|
|
987
|
+
var _runMiddlewares = runMiddlewares(this.senderMiddleware, args),
|
|
988
|
+
returnValue = _runMiddlewares.returnValue;
|
|
989
|
+
if (returnValue) return returnValue;
|
|
990
|
+
};
|
|
991
|
+
_proto.sendReply = function sendReply() {
|
|
992
|
+
this.send.apply(this, arguments);
|
|
993
|
+
};
|
|
994
|
+
_proto.onMessage = function onMessage() {
|
|
995
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
996
|
+
args[_key2] = arguments[_key2];
|
|
997
|
+
}
|
|
998
|
+
runMiddlewares(this._onMessageMiddleware, args);
|
|
999
|
+
};
|
|
1000
|
+
_createClass(AbstractChannelProtocol, [{
|
|
1001
|
+
key: "service",
|
|
1002
|
+
get: function get() {
|
|
1003
|
+
return this._service;
|
|
1004
|
+
}
|
|
1005
|
+
}, {
|
|
1006
|
+
key: "senderMiddleware",
|
|
1007
|
+
get: function get() {
|
|
1008
|
+
return this._senderMiddleware;
|
|
1009
|
+
}
|
|
1010
|
+
}, {
|
|
1011
|
+
key: "readBuffer",
|
|
1012
|
+
get: function get() {
|
|
1013
|
+
if (this._readBuffer) {
|
|
1014
|
+
return this._readBuffer;
|
|
1015
|
+
}
|
|
1016
|
+
try {
|
|
1017
|
+
this._readBuffer = BufferFactory.createReadBuffer(this._serializationFormat);
|
|
1018
|
+
} catch (error) {
|
|
1019
|
+
console.warn("[AbstractChannelProtocol] Failed to create read buffer with format \"" + this._serializationFormat + "\", falling back to JSON.", error);
|
|
1020
|
+
this._readBuffer = BufferFactory.createReadBuffer(SerializationFormat.JSON);
|
|
1021
|
+
}
|
|
1022
|
+
return this._readBuffer;
|
|
1023
|
+
}
|
|
1024
|
+
}, {
|
|
1025
|
+
key: "writeBuffer",
|
|
1026
|
+
get: function get() {
|
|
1027
|
+
if (this._writeBuffer) {
|
|
1028
|
+
return this._writeBuffer;
|
|
1029
|
+
}
|
|
1030
|
+
try {
|
|
1031
|
+
this._writeBuffer = BufferFactory.createWriteBuffer(this._serializationFormat);
|
|
1032
|
+
} catch (error) {
|
|
1033
|
+
console.warn("[AbstractChannelProtocol] Failed to create write buffer with format \"" + this._serializationFormat + "\", falling back to JSON.", error);
|
|
1034
|
+
this._writeBuffer = BufferFactory.createWriteBuffer(SerializationFormat.JSON);
|
|
1035
|
+
}
|
|
1036
|
+
return this._writeBuffer;
|
|
1037
|
+
}
|
|
1038
|
+
}, {
|
|
1039
|
+
key: "serializationFormat",
|
|
1040
|
+
get: function get() {
|
|
1041
|
+
return this._serializationFormat;
|
|
1042
|
+
}
|
|
1043
|
+
}, {
|
|
1044
|
+
key: "seqId",
|
|
1045
|
+
get: function get() {
|
|
1046
|
+
this._seqId += 1;
|
|
1047
|
+
return this._key + "_" + this._seqId;
|
|
1048
|
+
}
|
|
1049
|
+
}, {
|
|
1050
|
+
key: "description",
|
|
1051
|
+
get: function get() {
|
|
1052
|
+
return this._description;
|
|
1053
|
+
}
|
|
1054
|
+
}, {
|
|
1055
|
+
key: "masterProcessName",
|
|
1056
|
+
get: function get() {
|
|
1057
|
+
return this._masterProcessName;
|
|
1058
|
+
}
|
|
1059
|
+
}]);
|
|
1060
|
+
return AbstractChannelProtocol;
|
|
1061
|
+
}(disposable.Disposable);
|
|
1062
|
+
|
|
1063
|
+
var _excluded = ["port", "sender", "targetOrigin"];
|
|
1064
|
+
var RPCMessageChannel = /*#__PURE__*/function (_AbstractChannelProto) {
|
|
1065
|
+
_inheritsLoose(RPCMessageChannel, _AbstractChannelProto);
|
|
1066
|
+
function RPCMessageChannel(options) {
|
|
1067
|
+
var _this;
|
|
1068
|
+
var port = options.port,
|
|
1069
|
+
_options$sender = options.sender,
|
|
1070
|
+
sender = _options$sender === void 0 ? window : _options$sender,
|
|
1071
|
+
_options$targetOrigin = options.targetOrigin,
|
|
1072
|
+
targetOrigin = _options$targetOrigin === void 0 ? '*' : _options$targetOrigin,
|
|
1073
|
+
protocolOptions = _objectWithoutPropertiesLoose(options, _excluded);
|
|
1074
|
+
_this = _AbstractChannelProto.call(this, protocolOptions) || this;
|
|
1075
|
+
_this.port = port;
|
|
1076
|
+
_this.targetOrigin = targetOrigin;
|
|
1077
|
+
_this.sender = sender;
|
|
1078
|
+
if (_this.port.start) {
|
|
1079
|
+
_this.port.start();
|
|
1080
|
+
}
|
|
1081
|
+
return _this;
|
|
1082
|
+
}
|
|
1083
|
+
var _proto = RPCMessageChannel.prototype;
|
|
1084
|
+
_proto.on = function on(listener) {
|
|
1085
|
+
var _this2 = this;
|
|
1086
|
+
var f = function f(ev) {
|
|
1087
|
+
listener(ev);
|
|
1088
|
+
};
|
|
1089
|
+
this.port.addEventListener('message', f);
|
|
1090
|
+
return function () {
|
|
1091
|
+
return _this2.port.removeEventListener('message', f);
|
|
1092
|
+
};
|
|
1093
|
+
};
|
|
1094
|
+
_proto.send = function send(message, transfer) {
|
|
1095
|
+
if (transfer && transfer.length > 0) {
|
|
1096
|
+
this.port.postMessage(message, transfer);
|
|
1097
|
+
} else {
|
|
1098
|
+
this.port.postMessage(message);
|
|
1099
|
+
}
|
|
1100
|
+
};
|
|
1101
|
+
_proto.decorateSendMiddleware = function decorateSendMiddleware(middlewares) {
|
|
1102
|
+
return middlewares;
|
|
1103
|
+
};
|
|
1104
|
+
_proto.decorateOnMessageMiddleware = function decorateOnMessageMiddleware(middlewares) {
|
|
1105
|
+
return middlewares;
|
|
1106
|
+
};
|
|
1107
|
+
_proto.disconnect = function disconnect() {
|
|
1108
|
+
if (this.port) {
|
|
1109
|
+
this.port.close();
|
|
1110
|
+
}
|
|
1111
|
+
};
|
|
1112
|
+
return RPCMessageChannel;
|
|
1113
|
+
}(AbstractChannelProtocol);
|
|
1114
|
+
|
|
1115
|
+
var _excluded$1 = ["name"];
|
|
1116
|
+
var WorkerChannel = /*#__PURE__*/function (_AbstractChannelProto) {
|
|
1117
|
+
_inheritsLoose(WorkerChannel, _AbstractChannelProto);
|
|
1118
|
+
function WorkerChannel(worker, options) {
|
|
1119
|
+
var _this;
|
|
1120
|
+
var _ref = options || {},
|
|
1121
|
+
name = _ref.name,
|
|
1122
|
+
protocolOptions = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
1123
|
+
_this = _AbstractChannelProto.call(this, protocolOptions) || this;
|
|
1124
|
+
_this.worker = worker;
|
|
1125
|
+
_this.name = name || 'worker';
|
|
1126
|
+
return _this;
|
|
1127
|
+
}
|
|
1128
|
+
var _proto = WorkerChannel.prototype;
|
|
1129
|
+
_proto.on = function on(listener) {
|
|
1130
|
+
var _this2 = this;
|
|
1131
|
+
var f = function f(ev) {
|
|
1132
|
+
listener(ev);
|
|
1133
|
+
};
|
|
1134
|
+
this.worker.addEventListener('message', f);
|
|
1135
|
+
return function () {
|
|
1136
|
+
return _this2.worker.removeEventListener('message', f);
|
|
1137
|
+
};
|
|
1138
|
+
};
|
|
1139
|
+
_proto.send = function send(data) {
|
|
1140
|
+
this.worker.postMessage(data);
|
|
1141
|
+
};
|
|
1142
|
+
return WorkerChannel;
|
|
1143
|
+
}(AbstractChannelProtocol);
|
|
1144
|
+
|
|
1145
|
+
var _excluded$2 = ["name", "maxReconnectAttempts", "reconnectDelay", "connected"];
|
|
1146
|
+
var WebSocketChannel = /*#__PURE__*/function (_AbstractChannelProto) {
|
|
1147
|
+
_inheritsLoose(WebSocketChannel, _AbstractChannelProto);
|
|
1148
|
+
function WebSocketChannel(socket, options) {
|
|
1149
|
+
var _this;
|
|
1150
|
+
var opts = options || {};
|
|
1151
|
+
var name = opts.name,
|
|
1152
|
+
maxReconnectAttempts = opts.maxReconnectAttempts,
|
|
1153
|
+
reconnectDelay = opts.reconnectDelay,
|
|
1154
|
+
connected = opts.connected,
|
|
1155
|
+
protocolOptions = _objectWithoutPropertiesLoose(opts, _excluded$2);
|
|
1156
|
+
_this = _AbstractChannelProto.call(this, _extends({
|
|
1157
|
+
connected: connected != null ? connected : false
|
|
1158
|
+
}, protocolOptions)) || this;
|
|
1159
|
+
_this.socket = socket;
|
|
1160
|
+
_this.name = name || 'websocket';
|
|
1161
|
+
_this.reconnectAttempts = 0;
|
|
1162
|
+
_this.maxReconnectAttempts = maxReconnectAttempts != null ? maxReconnectAttempts : 5;
|
|
1163
|
+
_this.reconnectDelay = reconnectDelay != null ? reconnectDelay : 1000;
|
|
1164
|
+
_this.setupSocketHandlers();
|
|
1165
|
+
return _this;
|
|
1166
|
+
}
|
|
1167
|
+
var _proto = WebSocketChannel.prototype;
|
|
1168
|
+
_proto.setupSocketHandlers = function setupSocketHandlers() {
|
|
1169
|
+
var _this2 = this;
|
|
1170
|
+
this.socket.addEventListener('open', function () {
|
|
1171
|
+
_this2.activate();
|
|
1172
|
+
});
|
|
1173
|
+
this.socket.addEventListener('close', function () {
|
|
1174
|
+
_AbstractChannelProto.prototype.disconnect.call(_this2);
|
|
1175
|
+
});
|
|
1176
|
+
this.socket.addEventListener('error', function (error) {
|
|
1177
|
+
console.error("[WebSocketChannel " + _this2.name + "] Error:", error);
|
|
1178
|
+
});
|
|
1179
|
+
};
|
|
1180
|
+
_proto.on = function on(listener) {
|
|
1181
|
+
var _this3 = this;
|
|
1182
|
+
var f = function f(ev) {
|
|
1183
|
+
if (ev === undefined || ev === null) {
|
|
1184
|
+
console.warn("[WebSocketChannel " + _this3.name + "] Received undefined/null message event");
|
|
1185
|
+
return;
|
|
1186
|
+
}
|
|
1187
|
+
listener(ev);
|
|
1188
|
+
};
|
|
1189
|
+
if (typeof this.socket.addEventListener === 'function') {
|
|
1190
|
+
this.socket.addEventListener('message', f);
|
|
1191
|
+
return function () {
|
|
1192
|
+
if (typeof _this3.socket.removeEventListener === 'function') {
|
|
1193
|
+
_this3.socket.removeEventListener('message', f);
|
|
1194
|
+
}
|
|
1195
|
+
};
|
|
1196
|
+
} else {
|
|
1197
|
+
var wsSocket = this.socket;
|
|
1198
|
+
if (typeof wsSocket.on === 'function') {
|
|
1199
|
+
wsSocket.on('message', f);
|
|
1200
|
+
return function () {
|
|
1201
|
+
if (typeof wsSocket.off === 'function') {
|
|
1202
|
+
wsSocket.off('message', f);
|
|
1203
|
+
} else if (typeof wsSocket.removeListener === 'function') {
|
|
1204
|
+
wsSocket.removeListener('message', f);
|
|
1205
|
+
}
|
|
1206
|
+
};
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
return function () {};
|
|
1210
|
+
};
|
|
1211
|
+
_proto.send = function send(data) {
|
|
1212
|
+
if (this.socket.readyState === WebSocket.OPEN) {
|
|
1213
|
+
if (typeof data === 'string') {
|
|
1214
|
+
this.socket.send(data);
|
|
1215
|
+
} else if (data instanceof ArrayBuffer || data instanceof Blob) {
|
|
1216
|
+
this.socket.send(data);
|
|
1217
|
+
} else {
|
|
1218
|
+
this.socket.send(JSON.stringify(data));
|
|
1219
|
+
}
|
|
1220
|
+
} else {
|
|
1221
|
+
console.warn("[WebSocketChannel " + this.name + "] Cannot send: WebSocket is not open. State: " + this.socket.readyState);
|
|
1222
|
+
}
|
|
1223
|
+
};
|
|
1224
|
+
_proto.decorateSendMiddleware = function decorateSendMiddleware(middlewares) {
|
|
1225
|
+
return middlewares;
|
|
1226
|
+
};
|
|
1227
|
+
_proto.decorateOnMessageMiddleware = function decorateOnMessageMiddleware(middlewares) {
|
|
1228
|
+
if (middlewares.length > 0) {
|
|
1229
|
+
return [normalizeWebSocketRawMessage].concat(middlewares.slice(1));
|
|
1230
|
+
}
|
|
1231
|
+
return middlewares;
|
|
1232
|
+
};
|
|
1233
|
+
_proto.disconnect = function disconnect() {
|
|
1234
|
+
if (this.socket) {
|
|
1235
|
+
this.socket.close();
|
|
1236
|
+
}
|
|
1237
|
+
_AbstractChannelProto.prototype.disconnect.call(this);
|
|
1238
|
+
};
|
|
1239
|
+
_proto.isOpen = function isOpen() {
|
|
1240
|
+
return this.socket.readyState === WebSocket.OPEN;
|
|
1241
|
+
};
|
|
1242
|
+
_createClass(WebSocketChannel, [{
|
|
1243
|
+
key: "readyState",
|
|
1244
|
+
get: function get() {
|
|
1245
|
+
return this.socket.readyState;
|
|
1246
|
+
}
|
|
1247
|
+
}]);
|
|
1248
|
+
return WebSocketChannel;
|
|
1249
|
+
}(AbstractChannelProtocol);
|
|
1250
|
+
|
|
1251
|
+
exports.ERROR = ERROR;
|
|
1252
|
+
exports.ErrorResponseInternalError = ErrorResponseInternalError;
|
|
1253
|
+
exports.ErrorResponseInvalidParams = ErrorResponseInvalidParams;
|
|
1254
|
+
exports.ErrorResponseInvalidRequest = ErrorResponseInvalidRequest;
|
|
1255
|
+
exports.ErrorResponseMapped = ErrorResponseMapped;
|
|
1256
|
+
exports.ErrorResponseMethodNotFound = ErrorResponseMethodNotFound;
|
|
1257
|
+
exports.ErrorResponseParseError = ErrorResponseParseError;
|
|
1258
|
+
exports.MessageChannel = RPCMessageChannel;
|
|
1259
|
+
exports.ProxyRPCClient = ProxyRPCClient;
|
|
1260
|
+
exports.RPCService = RPCService;
|
|
1261
|
+
exports.UNDEFINED = UNDEFINED;
|
|
1262
|
+
exports.WebSocketChannel = WebSocketChannel;
|
|
1263
|
+
exports.WorkerChannel = WorkerChannel;
|
|
1264
|
+
exports.clientHost = RPCClientHost$1;
|
|
1265
|
+
exports.defaultErrorMapper = defaultErrorMapper;
|
|
1266
|
+
exports.isArray = isArray;
|
|
1267
|
+
exports.isFunction = isFunction;
|
|
1268
|
+
exports.isJSONRPCObject = isJSONRPCObject;
|
|
1269
|
+
exports.isObject = isObject;
|
|
1270
|
+
exports.jsonrpc = jsonrpc;
|
|
1271
|
+
exports.makeErrorResponse = makeErrorResponse;
|
|
1272
|
+
exports.makeRequest = makeRequest;
|
|
1273
|
+
exports.makeSuccessResponse = makeSuccessResponse;
|
|
1274
|
+
exports.serviceHost = RPCServiceHost$1;
|
|
1275
|
+
//# sourceMappingURL=async-call-rpc.cjs.development.js.map
|