javonet-nodejs-sdk 2.5.19 → 2.6.0
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/dist/core/handler/Handler.cjs +2 -2
- package/dist/core/handler/ResolveReferenceHandler.cjs +8 -1
- package/dist/core/interpreter/Interpreter.cjs +31 -56
- package/dist/core/receiver/Receiver.cjs +20 -12
- package/dist/core/transmitter/TransmitterWebsocket.cjs +1 -1
- package/dist/core/transmitter/TransmitterWebsocketBrowser.cjs +1 -2
- package/dist/core/webSocketClient/WebSocketClient.cjs +62 -74
- package/dist/core/webSocketClient/WebSocketClientBrowser.cjs +51 -56
- package/dist/sdk/InvocationContext.cjs +6 -2
- package/dist/types/core/handler/ResolveReferenceHandler.d.ts +1 -4
- package/dist/types/core/interpreter/Interpreter.d.ts +3 -1
- package/dist/types/core/receiver/Receiver.d.ts +1 -0
- package/dist/types/core/webSocketClient/WebSocketClient.d.ts +5 -10
- package/dist/types/core/webSocketClient/WebSocketClientBrowser.d.ts +16 -26
- package/dist/types/utils/CommandType.d.ts +3 -0
- package/dist/utils/CommandType.cjs +4 -1
- package/dist/utils/exception/ExceptionSerializer.cjs +46 -21
- package/lib/core/handler/Handler.js +2 -2
- package/lib/core/handler/ResolveReferenceHandler.js +9 -4
- package/lib/core/interpreter/Interpreter.js +39 -71
- package/lib/core/receiver/Receiver.js +22 -14
- package/lib/core/transmitter/TransmitterWebsocket.js +1 -1
- package/lib/core/transmitter/TransmitterWebsocketBrowser.js +1 -2
- package/lib/core/webSocketClient/WebSocketClient.js +65 -88
- package/lib/core/webSocketClient/WebSocketClientBrowser.js +56 -73
- package/lib/sdk/InvocationContext.js +8 -2
- package/lib/utils/CommandType.js +3 -0
- package/lib/utils/exception/ExceptionSerializer.js +51 -21
- package/package.json +14 -6
|
@@ -107,8 +107,8 @@ class Handler {
|
|
|
107
107
|
}
|
|
108
108
|
let response = handlers[command.commandType].handleCommand(command);
|
|
109
109
|
return this.parseCommand(response, command.runtimeName);
|
|
110
|
-
} catch (
|
|
111
|
-
return import_ExceptionSerializer.ExceptionSerializer.serializeException(
|
|
110
|
+
} catch (e) {
|
|
111
|
+
return import_ExceptionSerializer.ExceptionSerializer.serializeException(e, command);
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
/**
|
|
@@ -23,6 +23,9 @@ __export(ResolveReferenceHandler_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(ResolveReferenceHandler_exports);
|
|
24
24
|
var import_ReferencesCache = require("../referenceCache/ReferencesCache.cjs");
|
|
25
25
|
var import_AbstractHandler = require("./AbstractHandler.cjs");
|
|
26
|
+
var import_CommandType = require("../../utils/CommandType.cjs");
|
|
27
|
+
var import_RuntimeName = require("../../utils/RuntimeName.cjs");
|
|
28
|
+
var import_Command = require("../../utils/Command.cjs");
|
|
26
29
|
class ResolveReferenceHandler extends import_AbstractHandler.AbstractHandler {
|
|
27
30
|
constructor() {
|
|
28
31
|
super();
|
|
@@ -32,7 +35,11 @@ class ResolveReferenceHandler extends import_AbstractHandler.AbstractHandler {
|
|
|
32
35
|
* @returns {any}
|
|
33
36
|
*/
|
|
34
37
|
process(command) {
|
|
35
|
-
|
|
38
|
+
if (command.runtimeName === import_RuntimeName.RuntimeName.Nodejs) {
|
|
39
|
+
return import_ReferencesCache.ReferencesCache.getInstance().resolveReference(command.payload[0]);
|
|
40
|
+
} else {
|
|
41
|
+
return new import_Command.Command(command.runtimeName, import_CommandType.CommandType.Reference, command.payload[0]);
|
|
42
|
+
}
|
|
36
43
|
}
|
|
37
44
|
}
|
|
38
45
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -39,7 +39,14 @@ if (!_TransmitterWebsocket) {
|
|
|
39
39
|
const requireDynamic = (0, import_Runtime.getRequire)(import_meta.url);
|
|
40
40
|
class Interpreter {
|
|
41
41
|
/** @type {Handler | null} */
|
|
42
|
-
|
|
42
|
+
_handler = null;
|
|
43
|
+
/** @type {Handler} */
|
|
44
|
+
get handler() {
|
|
45
|
+
if (!this._handler) {
|
|
46
|
+
this._handler = new import_Handler.Handler(this);
|
|
47
|
+
}
|
|
48
|
+
return this._handler;
|
|
49
|
+
}
|
|
43
50
|
/**
|
|
44
51
|
*
|
|
45
52
|
* @param {Command} command
|
|
@@ -48,45 +55,32 @@ class Interpreter {
|
|
|
48
55
|
*/
|
|
49
56
|
async executeAsync(command, connectionData) {
|
|
50
57
|
try {
|
|
51
|
-
if (!this.handler) {
|
|
52
|
-
this.handler = new import_Handler.Handler(this);
|
|
53
|
-
}
|
|
54
58
|
let messageByteArray = new import_CommandSerializer.CommandSerializer().serialize(command, connectionData);
|
|
55
59
|
let responseByteArray = void 0;
|
|
56
|
-
if (
|
|
57
|
-
|
|
60
|
+
if (connectionData.connectionType === import_ConnectionType.ConnectionType.WEB_SOCKET) {
|
|
61
|
+
const _response = await _TransmitterWebsocket?.sendCommand(messageByteArray, connectionData);
|
|
62
|
+
if (_response) {
|
|
63
|
+
const command2 = new import_CommandDeserializer.CommandDeserializer(_response).deserialize();
|
|
64
|
+
return command2;
|
|
65
|
+
} else {
|
|
66
|
+
throw new Error("Response not received from TransmitterWebsocket");
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
if (!(0, import_Runtime.isNodejsRuntime)()) {
|
|
70
|
+
throw new Error("InMemory is only allowed in Nodejs runtime");
|
|
71
|
+
}
|
|
72
|
+
if (command.runtimeName === import_RuntimeName.RuntimeName.Nodejs) {
|
|
58
73
|
if (!_Receiver) {
|
|
59
74
|
const { Receiver } = require("../receiver/Receiver.cjs");
|
|
60
75
|
_Receiver = Receiver;
|
|
61
76
|
}
|
|
62
|
-
responseByteArray = _Receiver?.sendCommand(messageByteArray);
|
|
77
|
+
responseByteArray = await _Receiver?.sendCommand(messageByteArray);
|
|
63
78
|
} else {
|
|
64
|
-
throw new Error("Node.js runtime not found");
|
|
65
|
-
}
|
|
66
|
-
} else if (connectionData.connectionType === import_ConnectionType.ConnectionType.WEB_SOCKET) {
|
|
67
|
-
try {
|
|
68
|
-
const _response = await _TransmitterWebsocket?.sendCommand(
|
|
69
|
-
messageByteArray,
|
|
70
|
-
connectionData
|
|
71
|
-
);
|
|
72
|
-
if (_response) {
|
|
73
|
-
const command2 = new import_CommandDeserializer.CommandDeserializer(_response).deserialize();
|
|
74
|
-
return command2;
|
|
75
|
-
} else {
|
|
76
|
-
throw new Error("Response not received from TransmitterWebsocket");
|
|
77
|
-
}
|
|
78
|
-
} catch (error) {
|
|
79
|
-
throw error;
|
|
80
|
-
}
|
|
81
|
-
} else {
|
|
82
|
-
if ((0, import_Runtime.isNodejsRuntime)()) {
|
|
83
79
|
if (!_Transmitter) {
|
|
84
80
|
const { Transmitter } = require("../transmitter/Transmitter.cjs");
|
|
85
81
|
_Transmitter = Transmitter;
|
|
86
82
|
}
|
|
87
83
|
responseByteArray = await _Transmitter?.sendCommand(messageByteArray);
|
|
88
|
-
} else {
|
|
89
|
-
throw new Error("Allowed only to run in nodejs runtime");
|
|
90
84
|
}
|
|
91
85
|
}
|
|
92
86
|
if (!responseByteArray) {
|
|
@@ -105,43 +99,27 @@ class Interpreter {
|
|
|
105
99
|
*/
|
|
106
100
|
execute(command, connectionData) {
|
|
107
101
|
try {
|
|
108
|
-
if (!this.handler) {
|
|
109
|
-
this.handler = new import_Handler.Handler(this);
|
|
110
|
-
}
|
|
111
102
|
let messageByteArray = new import_CommandSerializer.CommandSerializer().serialize(command, connectionData);
|
|
112
103
|
let responseByteArray = void 0;
|
|
113
|
-
if (
|
|
114
|
-
|
|
104
|
+
if (connectionData.connectionType === import_ConnectionType.ConnectionType.WEB_SOCKET) {
|
|
105
|
+
throw new Error("Not supported");
|
|
106
|
+
}
|
|
107
|
+
if (connectionData.connectionType === import_ConnectionType.ConnectionType.IN_MEMORY) {
|
|
108
|
+
if (!(0, import_Runtime.isNodejsRuntime)()) {
|
|
109
|
+
throw new Error("InMemory is only allowed in Nodejs runtime");
|
|
110
|
+
}
|
|
111
|
+
if (command.runtimeName === import_RuntimeName.RuntimeName.Nodejs) {
|
|
115
112
|
if (!_Receiver) {
|
|
116
113
|
const { Receiver } = require("../receiver/Receiver.cjs");
|
|
117
114
|
_Receiver = Receiver;
|
|
118
115
|
}
|
|
119
116
|
responseByteArray = _Receiver?.sendCommand(messageByteArray);
|
|
120
|
-
}
|
|
121
|
-
} else if (connectionData.connectionType === import_ConnectionType.ConnectionType.WEB_SOCKET) {
|
|
122
|
-
const promise = _TransmitterWebsocket?.sendCommand(messageByteArray, connectionData);
|
|
123
|
-
if (!promise) {
|
|
124
|
-
throw new Error("TransmitterWebsocket not found");
|
|
125
|
-
}
|
|
126
|
-
return promise.then((_response) => {
|
|
127
|
-
if (_response) {
|
|
128
|
-
const command2 = new import_CommandDeserializer.CommandDeserializer(_response).deserialize();
|
|
129
|
-
return command2;
|
|
130
|
-
} else {
|
|
131
|
-
throw new Error("Response not received from TransmitterWebsocket");
|
|
132
|
-
}
|
|
133
|
-
}).catch((error) => {
|
|
134
|
-
throw error;
|
|
135
|
-
});
|
|
136
|
-
} else {
|
|
137
|
-
if ((0, import_Runtime.isNodejsRuntime)()) {
|
|
117
|
+
} else {
|
|
138
118
|
if (!_Transmitter) {
|
|
139
119
|
const { Transmitter } = require("../transmitter/Transmitter.cjs");
|
|
140
120
|
_Transmitter = Transmitter;
|
|
141
121
|
}
|
|
142
122
|
responseByteArray = _Transmitter?.sendCommand(messageByteArray);
|
|
143
|
-
} else {
|
|
144
|
-
throw new Error("Allowed only to run in nodejs runtime");
|
|
145
123
|
}
|
|
146
124
|
}
|
|
147
125
|
if (!responseByteArray) {
|
|
@@ -159,9 +137,6 @@ class Interpreter {
|
|
|
159
137
|
*/
|
|
160
138
|
process(messageByteArray) {
|
|
161
139
|
try {
|
|
162
|
-
if (!this.handler) {
|
|
163
|
-
this.handler = new import_Handler.Handler(this);
|
|
164
|
-
}
|
|
165
140
|
const receivedCommand = new import_CommandDeserializer.CommandDeserializer(messageByteArray).deserialize();
|
|
166
141
|
return this.handler?.handleCommand(receivedCommand);
|
|
167
142
|
} catch (error) {
|
|
@@ -25,26 +25,34 @@ var import_Interpreter = require("../interpreter/Interpreter.cjs");
|
|
|
25
25
|
var import_CommandSerializer = require("../protocol/CommandSerializer.cjs");
|
|
26
26
|
var import_Runtime = require("../../utils/Runtime.cjs");
|
|
27
27
|
var import_InMemoryConnectionData = require("../../utils/connectionData/InMemoryConnectionData.cjs");
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
var import_ExceptionSerializer = require("../../utils/exception/ExceptionSerializer.cjs");
|
|
29
|
+
var import_Command = require("../../utils/Command.cjs");
|
|
30
|
+
var import_CommandType = require("../../utils/CommandType.cjs");
|
|
31
|
+
var import_RuntimeName = require("../../utils/RuntimeName.cjs");
|
|
32
|
+
var import_RuntimeLogger = require("../../utils/RuntimeLogger.cjs");
|
|
31
33
|
class Receiver {
|
|
32
34
|
static connectionData = new import_InMemoryConnectionData.InMemoryConnectionData();
|
|
33
35
|
Receiver() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
_RuntimeLogger?.printRuntimeInfo();
|
|
36
|
+
}
|
|
37
|
+
static getRuntimeInfo() {
|
|
38
|
+
return import_RuntimeLogger.RuntimeLogger.getRuntimeInfo();
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* @param {Int8Array} messageByteArray
|
|
42
42
|
*/
|
|
43
43
|
static sendCommand(messageByteArray) {
|
|
44
|
-
|
|
45
|
-
new
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
try {
|
|
45
|
+
return new import_CommandSerializer.CommandSerializer().serialize(
|
|
46
|
+
new import_Interpreter.Interpreter().process(messageByteArray),
|
|
47
|
+
this.connectionData
|
|
48
|
+
);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
const exceptionCommand = import_ExceptionSerializer.ExceptionSerializer.serializeException(
|
|
51
|
+
error,
|
|
52
|
+
new import_Command.Command(import_RuntimeName.RuntimeName.Nodejs, import_CommandType.CommandType.Exception, [])
|
|
53
|
+
);
|
|
54
|
+
return new import_CommandSerializer.CommandSerializer().serialize(exceptionCommand, this.connectionData);
|
|
55
|
+
}
|
|
48
56
|
}
|
|
49
57
|
/**
|
|
50
58
|
* @param {Int8Array} messageByteArray
|
|
@@ -47,7 +47,7 @@ class TransmitterWebsocket {
|
|
|
47
47
|
static sendCommand(messageByteArray, connectionData) {
|
|
48
48
|
const { hostname } = connectionData;
|
|
49
49
|
const options = {
|
|
50
|
-
isDisconnectedAfterMessage:
|
|
50
|
+
isDisconnectedAfterMessage: false
|
|
51
51
|
};
|
|
52
52
|
return new import_WebSocketClient.WebSocketClient(hostname, options).send(messageByteArray);
|
|
53
53
|
}
|
|
@@ -41,8 +41,7 @@ class TransmitterWebsocketBrowser {
|
|
|
41
41
|
*/
|
|
42
42
|
static async sendCommand(messageByteArray, connectionData) {
|
|
43
43
|
const { hostname } = connectionData;
|
|
44
|
-
|
|
45
|
-
return response;
|
|
44
|
+
return new import_WebSocketClientBrowser.WebSocketClientBrowser(hostname).send(messageByteArray);
|
|
46
45
|
}
|
|
47
46
|
/**
|
|
48
47
|
* @async
|
|
@@ -18,18 +18,27 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var WebSocketClient_exports = {};
|
|
20
20
|
__export(WebSocketClient_exports, {
|
|
21
|
-
WebSocketClient: () => WebSocketClient
|
|
21
|
+
WebSocketClient: () => WebSocketClient,
|
|
22
|
+
clients: () => clients
|
|
22
23
|
});
|
|
23
24
|
module.exports = __toCommonJS(WebSocketClient_exports);
|
|
24
25
|
var import_Runtime = require("../../utils/Runtime.cjs");
|
|
25
26
|
const import_meta = {};
|
|
26
27
|
const requireDynamic = (0, import_Runtime.getRequire)(import_meta.url);
|
|
27
28
|
const WebSocketStateEnum = {
|
|
29
|
+
CONNECTING: 0,
|
|
30
|
+
OPEN: 1,
|
|
31
|
+
CLOSING: 2,
|
|
32
|
+
CLOSED: 3
|
|
33
|
+
};
|
|
34
|
+
const WebSocketStateEvent = {
|
|
28
35
|
OPEN: "open",
|
|
29
36
|
CLOSE: "close",
|
|
30
|
-
ERROR: "error"
|
|
37
|
+
ERROR: "error",
|
|
38
|
+
MESSAGE: "message"
|
|
31
39
|
};
|
|
32
40
|
let WebSocket = null;
|
|
41
|
+
const clients = {};
|
|
33
42
|
class WebSocketClient {
|
|
34
43
|
/**
|
|
35
44
|
* @param {string} url
|
|
@@ -37,45 +46,14 @@ class WebSocketClient {
|
|
|
37
46
|
*/
|
|
38
47
|
constructor(url, options) {
|
|
39
48
|
this.url = url;
|
|
40
|
-
this.
|
|
41
|
-
this.isConnected = false;
|
|
42
|
-
this.isDisconnectedAfterMessage = options?.isDisconnectedAfterMessage ?? true;
|
|
49
|
+
this.isDisconnectedAfterMessage = options?.isDisconnectedAfterMessage ?? false;
|
|
43
50
|
}
|
|
44
|
-
/**
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (!WebSocket) {
|
|
51
|
-
if ((0, import_Runtime.isNodejsRuntime)()) {
|
|
52
|
-
try {
|
|
53
|
-
WebSocket = requireDynamic("ws");
|
|
54
|
-
} catch (error) {
|
|
55
|
-
if (typeof error === "object" && error && "code" in error && error.code === "MODULE_NOT_FOUND") {
|
|
56
|
-
throw new Error("ws module not found. Please install it using npm install ws");
|
|
57
|
-
}
|
|
58
|
-
throw error;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return new Promise((resolve, reject) => {
|
|
63
|
-
if (!WebSocket) {
|
|
64
|
-
reject(new Error("ws client is null"));
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
const client = new WebSocket(this.url);
|
|
68
|
-
client.on(WebSocketStateEnum.OPEN, () => {
|
|
69
|
-
this.instance = client;
|
|
70
|
-
resolve(client);
|
|
71
|
-
});
|
|
72
|
-
client.on(WebSocketStateEnum.ERROR, (error) => {
|
|
73
|
-
reject(error);
|
|
74
|
-
});
|
|
75
|
-
client.on(WebSocketStateEnum.CLOSE, () => {
|
|
76
|
-
reject(new Error("Connection closed before receiving message"));
|
|
77
|
-
});
|
|
78
|
-
});
|
|
51
|
+
/** @type {wsClient | null} */
|
|
52
|
+
get instance() {
|
|
53
|
+
return clients[this.url] || null;
|
|
54
|
+
}
|
|
55
|
+
get isConnected() {
|
|
56
|
+
return this.instance ? this.instance?.readyState === WebSocketStateEnum.OPEN : false;
|
|
79
57
|
}
|
|
80
58
|
/**
|
|
81
59
|
* Sends messageArray through websocket connection
|
|
@@ -85,21 +63,36 @@ class WebSocketClient {
|
|
|
85
63
|
*/
|
|
86
64
|
send(messageArray) {
|
|
87
65
|
return new Promise((resolve, reject) => {
|
|
88
|
-
;
|
|
89
|
-
|
|
66
|
+
const client = this.instance;
|
|
67
|
+
if (client) {
|
|
90
68
|
client.send(
|
|
91
69
|
/** @type {any} */
|
|
92
70
|
messageArray
|
|
93
71
|
);
|
|
94
|
-
|
|
72
|
+
const messageHandler = (message) => {
|
|
95
73
|
resolve(message);
|
|
96
74
|
if (this.isDisconnectedAfterMessage) {
|
|
97
75
|
this.disconnect();
|
|
98
76
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
77
|
+
client.removeListener(WebSocketStateEvent.MESSAGE, messageHandler);
|
|
78
|
+
};
|
|
79
|
+
client.on(WebSocketStateEvent.MESSAGE, messageHandler);
|
|
80
|
+
} else {
|
|
81
|
+
this._connect().then((ws) => {
|
|
82
|
+
ws.send(
|
|
83
|
+
/** @type {any} */
|
|
84
|
+
messageArray
|
|
85
|
+
);
|
|
86
|
+
const messageHandler = (message) => {
|
|
87
|
+
resolve(message);
|
|
88
|
+
if (this.isDisconnectedAfterMessage) {
|
|
89
|
+
this.disconnect();
|
|
90
|
+
}
|
|
91
|
+
ws.removeListener(WebSocketStateEvent.MESSAGE, messageHandler);
|
|
92
|
+
};
|
|
93
|
+
ws.on(WebSocketStateEvent.MESSAGE, messageHandler);
|
|
94
|
+
}).catch(reject);
|
|
95
|
+
}
|
|
103
96
|
});
|
|
104
97
|
}
|
|
105
98
|
/**
|
|
@@ -108,9 +101,8 @@ class WebSocketClient {
|
|
|
108
101
|
disconnect() {
|
|
109
102
|
if (this.instance) {
|
|
110
103
|
this.instance.close();
|
|
111
|
-
this.
|
|
104
|
+
delete clients[this.url];
|
|
112
105
|
}
|
|
113
|
-
this.isConnected = false;
|
|
114
106
|
}
|
|
115
107
|
/**
|
|
116
108
|
* Connects to the WebSocket server.
|
|
@@ -119,36 +111,31 @@ class WebSocketClient {
|
|
|
119
111
|
* @returns {Promise<wsClient>} - A promise that resolves when the connection is established.
|
|
120
112
|
*/
|
|
121
113
|
_connect() {
|
|
122
|
-
if (!WebSocket) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
)
|
|
131
|
-
throw new Error("ws module not found. Please install it using npm install ws");
|
|
132
|
-
}
|
|
133
|
-
throw error;
|
|
114
|
+
if (!WebSocket && (0, import_Runtime.isNodejsRuntime)()) {
|
|
115
|
+
try {
|
|
116
|
+
WebSocket = requireDynamic("ws");
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if (
|
|
119
|
+
/** @type {{ code?: string }} */
|
|
120
|
+
error.code === "MODULE_NOT_FOUND"
|
|
121
|
+
) {
|
|
122
|
+
throw new Error("ws module not found. Please install it using npm install ws");
|
|
134
123
|
}
|
|
124
|
+
throw error;
|
|
135
125
|
}
|
|
136
126
|
}
|
|
137
127
|
return new Promise((resolve, reject) => {
|
|
138
128
|
if (!WebSocket) {
|
|
139
|
-
reject(new Error("ws client is null"));
|
|
140
|
-
return;
|
|
129
|
+
return reject(new Error("ws client is null"));
|
|
141
130
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
this.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
client.on(
|
|
149
|
-
|
|
150
|
-
});
|
|
151
|
-
client.on(WebSocketStateEnum.CLOSE, () => {
|
|
131
|
+
let client = clients[this.url];
|
|
132
|
+
if (!client) {
|
|
133
|
+
client = new WebSocket(this.url);
|
|
134
|
+
clients[this.url] = client;
|
|
135
|
+
}
|
|
136
|
+
client.on(WebSocketStateEvent.OPEN, () => resolve(client));
|
|
137
|
+
client.on(WebSocketStateEvent.ERROR, (error) => reject(error));
|
|
138
|
+
client.on(WebSocketStateEvent.CLOSE, () => {
|
|
152
139
|
reject(new Error("Connection closed before receiving message"));
|
|
153
140
|
});
|
|
154
141
|
});
|
|
@@ -156,5 +143,6 @@ class WebSocketClient {
|
|
|
156
143
|
}
|
|
157
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
158
145
|
0 && (module.exports = {
|
|
159
|
-
WebSocketClient
|
|
146
|
+
WebSocketClient,
|
|
147
|
+
clients
|
|
160
148
|
});
|
|
@@ -18,7 +18,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var WebSocketClientBrowser_exports = {};
|
|
20
20
|
__export(WebSocketClientBrowser_exports, {
|
|
21
|
-
WebSocketClientBrowser: () => WebSocketClientBrowser
|
|
21
|
+
WebSocketClientBrowser: () => WebSocketClientBrowser,
|
|
22
|
+
clients: () => clients
|
|
22
23
|
});
|
|
23
24
|
module.exports = __toCommonJS(WebSocketClientBrowser_exports);
|
|
24
25
|
var import_Runtime = require("../../utils/Runtime.cjs");
|
|
@@ -35,6 +36,7 @@ let WsClient = null;
|
|
|
35
36
|
if ((0, import_Runtime.isBrowserRuntime)()) {
|
|
36
37
|
WsClient = WebSocket;
|
|
37
38
|
}
|
|
39
|
+
const clients = {};
|
|
38
40
|
class WebSocketClientBrowser {
|
|
39
41
|
/**
|
|
40
42
|
* @param {string} url
|
|
@@ -42,10 +44,15 @@ class WebSocketClientBrowser {
|
|
|
42
44
|
*/
|
|
43
45
|
constructor(url, options) {
|
|
44
46
|
this.url = url;
|
|
45
|
-
this.instance = null;
|
|
46
|
-
this.isConnected = false;
|
|
47
47
|
this.isDisconnectedAfterMessage = options?.isDisconnectedAfterMessage ?? false;
|
|
48
48
|
}
|
|
49
|
+
/** @type {WebSocket | null} */
|
|
50
|
+
get instance() {
|
|
51
|
+
return clients[this.url] || null;
|
|
52
|
+
}
|
|
53
|
+
get isConnected() {
|
|
54
|
+
return this.instance ? this.instance.readyState === WebSocket.OPEN : false;
|
|
55
|
+
}
|
|
49
56
|
/**
|
|
50
57
|
* Sends messageArray through websocket connection
|
|
51
58
|
* @async
|
|
@@ -54,94 +61,81 @@ class WebSocketClientBrowser {
|
|
|
54
61
|
*/
|
|
55
62
|
send(messageArray) {
|
|
56
63
|
return new Promise((resolve, reject) => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
} catch (error) {
|
|
66
|
-
reject(error);
|
|
64
|
+
const client = this.instance;
|
|
65
|
+
if (client && this.isConnected) {
|
|
66
|
+
this._sendMessage(client, messageArray, resolve, reject);
|
|
67
|
+
} else {
|
|
68
|
+
this._connect().then((ws) => {
|
|
69
|
+
this._sendMessage(ws, messageArray, resolve, reject);
|
|
70
|
+
}).catch(reject);
|
|
67
71
|
}
|
|
68
72
|
});
|
|
69
73
|
}
|
|
70
74
|
/**
|
|
71
75
|
* Disconnects the WebSocket by terminating the connection.
|
|
72
|
-
* @returns {void}
|
|
73
|
-
*
|
|
74
76
|
*/
|
|
75
77
|
disconnect() {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
+
const client = this.instance;
|
|
79
|
+
if (client) {
|
|
80
|
+
client.close();
|
|
81
|
+
delete clients[this.url];
|
|
78
82
|
}
|
|
79
|
-
this.isConnected = false;
|
|
80
83
|
}
|
|
81
84
|
/**
|
|
82
85
|
* Connects to the WebSocket server.
|
|
83
86
|
* @private
|
|
84
87
|
* @async
|
|
85
|
-
* @returns {Promise<
|
|
88
|
+
* @returns {Promise<WebSocket>}
|
|
86
89
|
*/
|
|
87
90
|
_connect() {
|
|
88
91
|
return new Promise((resolve, reject) => {
|
|
89
|
-
if (WsClient
|
|
92
|
+
if (!WsClient) {
|
|
90
93
|
return reject(new Error("missing WebSocket client"));
|
|
91
94
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this.isConnected = false;
|
|
95
|
+
let client = clients[this.url];
|
|
96
|
+
if (!client) {
|
|
97
|
+
client = new WsClient(this.url);
|
|
98
|
+
client.binaryType = "arraybuffer";
|
|
99
|
+
clients[this.url] = client;
|
|
100
|
+
}
|
|
101
|
+
client.addEventListener(WebSocketState.OPEN, () => resolve(client));
|
|
102
|
+
client.addEventListener(WebSocketState.ERROR, reject);
|
|
103
|
+
client.addEventListener(WebSocketState.CLOSE, () => {
|
|
104
|
+
reject(new Error("Connection closed before receiving message"));
|
|
103
105
|
});
|
|
104
106
|
});
|
|
105
107
|
}
|
|
106
108
|
/**
|
|
107
109
|
* Sends the data to the WebSocket server and listens for a response.
|
|
108
110
|
* @private
|
|
109
|
-
* @
|
|
110
|
-
* @param {Int8Array} data
|
|
111
|
-
* @param {
|
|
112
|
-
* @param {
|
|
113
|
-
* @returns {void}
|
|
111
|
+
* @param {WebSocket} client
|
|
112
|
+
* @param {Int8Array} data
|
|
113
|
+
* @param {(value: Int8Array) => void} resolve
|
|
114
|
+
* @param {(reason?: any) => void} reject
|
|
114
115
|
*/
|
|
115
|
-
_sendMessage(data, resolve, reject) {
|
|
116
|
+
_sendMessage(client, data, resolve, reject) {
|
|
116
117
|
try {
|
|
117
|
-
if (this.instance === null || !this.instance) {
|
|
118
|
-
throw new Error("error websocket not connected");
|
|
119
|
-
}
|
|
120
118
|
const arrayBuffer = data.buffer;
|
|
121
|
-
|
|
122
|
-
const handleMessage = (
|
|
123
|
-
if (
|
|
124
|
-
|
|
119
|
+
client.send(arrayBuffer);
|
|
120
|
+
const handleMessage = (message) => {
|
|
121
|
+
if (!message?.data) {
|
|
122
|
+
return reject(new Error("Invalid message received"));
|
|
125
123
|
}
|
|
126
|
-
const byteArray = new Int8Array(
|
|
124
|
+
const byteArray = new Int8Array(message?.data);
|
|
127
125
|
resolve(byteArray);
|
|
128
126
|
if (this.isDisconnectedAfterMessage) {
|
|
129
127
|
this.disconnect();
|
|
130
128
|
}
|
|
131
|
-
|
|
132
|
-
|
|
129
|
+
client.removeEventListener(WebSocketState.MESSAGE, handleMessage);
|
|
130
|
+
client.removeEventListener(WebSocketState.ERROR, handleError);
|
|
133
131
|
};
|
|
134
132
|
const handleError = (error) => {
|
|
135
133
|
reject(error);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
this.instance.removeEventListener(WebSocketState.ERROR, handleError);
|
|
139
|
-
}
|
|
134
|
+
client.removeEventListener(WebSocketState.MESSAGE, handleMessage);
|
|
135
|
+
client.removeEventListener(WebSocketState.ERROR, handleError);
|
|
140
136
|
};
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
this.instance.addEventListener(WebSocketState.ERROR, handleError);
|
|
144
|
-
}
|
|
137
|
+
client.addEventListener(WebSocketState.MESSAGE, handleMessage);
|
|
138
|
+
client.addEventListener(WebSocketState.ERROR, handleError);
|
|
145
139
|
} catch (err) {
|
|
146
140
|
reject(err);
|
|
147
141
|
}
|
|
@@ -149,5 +143,6 @@ class WebSocketClientBrowser {
|
|
|
149
143
|
}
|
|
150
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
151
145
|
0 && (module.exports = {
|
|
152
|
-
WebSocketClientBrowser
|
|
146
|
+
WebSocketClientBrowser,
|
|
147
|
+
clients
|
|
153
148
|
});
|
|
@@ -123,7 +123,9 @@ class InvocationContext {
|
|
|
123
123
|
if (this.#currentCommand === null) {
|
|
124
124
|
throw new Error("currentCommand is undefined in Invocation Context execute method");
|
|
125
125
|
}
|
|
126
|
-
this.#interpreter
|
|
126
|
+
if (!this.#interpreter) {
|
|
127
|
+
this.#interpreter = new import_Interpreter.Interpreter();
|
|
128
|
+
}
|
|
127
129
|
this.#responseCommand = this.#interpreter.execute(this.#currentCommand, this.#connectionData);
|
|
128
130
|
if (!this.#responseCommand) {
|
|
129
131
|
throw new Error("responseCommand is undefined in Invocation Context execute method");
|
|
@@ -495,7 +497,9 @@ class InvocationWsContext extends InvocationContext {
|
|
|
495
497
|
if (this.#currentCommand === null) {
|
|
496
498
|
throw new Error("currentCommand is undefined in Invocation Context execute method");
|
|
497
499
|
}
|
|
498
|
-
this.#interpreter
|
|
500
|
+
if (!this.#interpreter) {
|
|
501
|
+
this.#interpreter = new import_Interpreter.Interpreter();
|
|
502
|
+
}
|
|
499
503
|
this.#responseCommand = await this.#interpreter.executeAsync(
|
|
500
504
|
this.#currentCommand,
|
|
501
505
|
this.#connectionData
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
export type Command = import("../../utils/Command.js").Command;
|
|
2
|
-
/**
|
|
3
|
-
* @typedef {import('../../utils/Command.js').Command} Command
|
|
4
|
-
*/
|
|
5
1
|
export class ResolveReferenceHandler extends AbstractHandler {
|
|
6
2
|
/**
|
|
7
3
|
* @param {Command} command
|
|
@@ -10,3 +6,4 @@ export class ResolveReferenceHandler extends AbstractHandler {
|
|
|
10
6
|
process(command: Command): any;
|
|
11
7
|
}
|
|
12
8
|
import { AbstractHandler } from './AbstractHandler.js';
|
|
9
|
+
import { Command } from '../../utils/Command.js';
|