javonet-nodejs-sdk 2.6.4 → 2.6.6
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 +15 -4
- package/dist/core/handler/LoadLibraryHandler.cjs +10 -6
- package/dist/core/interpreter/Interpreter.cjs +42 -16
- package/dist/core/protocol/CommandDeserializer.cjs +4 -4
- package/dist/core/protocol/CommandSerializer.cjs +7 -2
- package/dist/core/protocol/TypeDeserializer.cjs +11 -11
- package/dist/core/receiver/Receiver.cjs +7 -8
- package/dist/core/receiver/ReceiverNative.cjs +4 -2
- package/dist/core/transmitter/Transmitter.cjs +2 -2
- package/dist/core/transmitter/TransmitterWebsocket.cjs +2 -2
- package/dist/core/transmitter/TransmitterWebsocketBrowser.cjs +2 -2
- package/dist/core/transmitter/TransmitterWrapper.cjs +2 -1
- package/dist/core/webSocketClient/WebSocketClient.cjs +4 -4
- package/dist/core/webSocketClient/WebSocketClientBrowser.cjs +7 -7
- package/dist/sdk/InvocationContext.cjs +47 -38
- package/dist/sdk/RuntimeFactory.cjs +15 -17
- package/dist/sdk/tools/ComplexTypeResolver.cjs +28 -3
- package/dist/types/core/handler/Handler.d.ts +3 -3
- package/dist/types/core/interpreter/Interpreter.d.ts +3 -3
- package/dist/types/core/protocol/CommandDeserializer.d.ts +3 -3
- package/dist/types/core/protocol/CommandSerializer.d.ts +3 -3
- package/dist/types/core/protocol/TypeDeserializer.d.ts +22 -22
- package/dist/types/core/receiver/Receiver.d.ts +6 -5
- package/dist/types/core/receiver/ReceiverNative.d.ts +6 -4
- package/dist/types/core/transmitter/Transmitter.d.ts +3 -3
- package/dist/types/core/transmitter/TransmitterWebsocket.d.ts +3 -3
- package/dist/types/core/transmitter/TransmitterWebsocketBrowser.d.ts +3 -3
- package/dist/types/core/transmitter/TransmitterWrapper.d.ts +3 -2
- package/dist/types/core/webSocketClient/WebSocketClient.d.ts +7 -7
- package/dist/types/core/webSocketClient/WebSocketClientBrowser.d.ts +9 -9
- package/dist/types/sdk/InvocationContext.d.ts +7 -11
- package/dist/types/sdk/RuntimeFactory.d.ts +2 -2
- package/dist/types/sdk/tools/ComplexTypeResolver.d.ts +2 -2
- package/lib/core/handler/Handler.js +18 -4
- package/lib/core/handler/LoadLibraryHandler.js +15 -7
- package/lib/core/interpreter/Interpreter.js +47 -20
- package/lib/core/protocol/CommandDeserializer.js +4 -4
- package/lib/core/protocol/CommandSerializer.js +8 -2
- package/lib/core/protocol/TypeDeserializer.js +11 -11
- package/lib/core/receiver/Receiver.js +7 -8
- package/lib/core/receiver/ReceiverNative.js +4 -2
- package/lib/core/transmitter/Transmitter.js +2 -2
- package/lib/core/transmitter/TransmitterWebsocket.js +2 -2
- package/lib/core/transmitter/TransmitterWebsocketBrowser.js +2 -2
- package/lib/core/transmitter/TransmitterWrapper.js +2 -1
- package/lib/core/webSocketClient/WebSocketClient.js +5 -5
- package/lib/core/webSocketClient/WebSocketClientBrowser.js +8 -8
- package/lib/sdk/InvocationContext.js +48 -41
- package/lib/sdk/RuntimeContext.js +0 -7
- package/lib/sdk/RuntimeFactory.js +23 -28
- package/lib/sdk/tools/ComplexTypeResolver.js +22 -3
- package/package.json +4 -2
|
@@ -14,10 +14,10 @@ import { Handler } from '../handler/Handler.js'
|
|
|
14
14
|
* @typedef {import('../../utils/Command.js').Command} Command
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
/** @type {typeof import('../receiver/Receiver.js').Receiver
|
|
18
|
-
let _Receiver
|
|
19
|
-
/** @type {typeof import('../transmitter/Transmitter.js').Transmitter
|
|
20
|
-
let _Transmitter
|
|
17
|
+
/** @type {typeof import('../receiver/Receiver.js').Receiver} */
|
|
18
|
+
let _Receiver
|
|
19
|
+
/** @type {typeof import('../transmitter/Transmitter.js').Transmitter} */
|
|
20
|
+
let _Transmitter
|
|
21
21
|
/** @type {typeof import('../transmitter/TransmitterWebsocket.js').TransmitterWebsocket | typeof import('../transmitter/TransmitterWebsocketBrowser.js').TransmitterWebsocketBrowser | null} */
|
|
22
22
|
let _TransmitterWebsocket = null
|
|
23
23
|
|
|
@@ -48,11 +48,14 @@ export class Interpreter {
|
|
|
48
48
|
async executeAsync(command, connectionData) {
|
|
49
49
|
try {
|
|
50
50
|
let messageByteArray = new CommandSerializer().serialize(command, connectionData)
|
|
51
|
-
/** @type {
|
|
51
|
+
/** @type {Uint8Array | undefined} */
|
|
52
52
|
let responseByteArray = undefined
|
|
53
53
|
|
|
54
54
|
if (connectionData.connectionType === ConnectionType.WEB_SOCKET) {
|
|
55
|
-
const _response = await _TransmitterWebsocket?.sendCommand(
|
|
55
|
+
const _response = await _TransmitterWebsocket?.sendCommand(
|
|
56
|
+
await messageByteArray,
|
|
57
|
+
connectionData
|
|
58
|
+
)
|
|
56
59
|
if (_response) {
|
|
57
60
|
const command = new CommandDeserializer(_response).deserialize()
|
|
58
61
|
return command
|
|
@@ -69,13 +72,13 @@ export class Interpreter {
|
|
|
69
72
|
const { Receiver } = requireDynamic('../receiver/Receiver.js')
|
|
70
73
|
_Receiver = Receiver
|
|
71
74
|
}
|
|
72
|
-
responseByteArray = await _Receiver?.sendCommand(messageByteArray)
|
|
75
|
+
responseByteArray = await _Receiver?.sendCommand(await messageByteArray)
|
|
73
76
|
} else {
|
|
74
77
|
if (!_Transmitter) {
|
|
75
78
|
const { Transmitter } = requireDynamic('../transmitter/Transmitter.js')
|
|
76
79
|
_Transmitter = Transmitter
|
|
77
80
|
}
|
|
78
|
-
responseByteArray = await _Transmitter?.sendCommand(messageByteArray)
|
|
81
|
+
responseByteArray = await _Transmitter?.sendCommand(await messageByteArray)
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
84
|
|
|
@@ -98,37 +101,61 @@ export class Interpreter {
|
|
|
98
101
|
execute(command, connectionData) {
|
|
99
102
|
try {
|
|
100
103
|
let messageByteArray = new CommandSerializer().serialize(command, connectionData)
|
|
101
|
-
/** @type {
|
|
104
|
+
/** @type {Promise<Uint8Array> | Uint8Array | undefined} */
|
|
102
105
|
let responseByteArray = undefined
|
|
103
106
|
|
|
104
107
|
if (connectionData.connectionType === ConnectionType.WEB_SOCKET) {
|
|
105
108
|
throw new Error('Not supported')
|
|
106
109
|
}
|
|
107
|
-
|
|
110
|
+
else {
|
|
108
111
|
if (!isNodejsRuntime()) {
|
|
109
112
|
throw new Error('InMemory is only allowed in Nodejs runtime')
|
|
110
113
|
}
|
|
111
114
|
|
|
112
|
-
if (command.runtimeName === RuntimeName.Nodejs) {
|
|
115
|
+
if (command.runtimeName === RuntimeName.Nodejs && connectionData.connectionType === ConnectionType.IN_MEMORY) {
|
|
113
116
|
if (!_Receiver) {
|
|
114
117
|
const { Receiver } = requireDynamic('../receiver/Receiver.js')
|
|
115
118
|
_Receiver = Receiver
|
|
116
119
|
}
|
|
117
|
-
|
|
120
|
+
if (!_Receiver) {
|
|
121
|
+
throw new Error('Receiver is undefined')
|
|
122
|
+
}
|
|
123
|
+
if (messageByteArray instanceof Uint8Array) {
|
|
124
|
+
responseByteArray = _Receiver.sendCommand(messageByteArray)
|
|
125
|
+
} else {
|
|
126
|
+
responseByteArray = messageByteArray.then((resolvedMessage) => {
|
|
127
|
+
return _Receiver.sendCommand(resolvedMessage)
|
|
128
|
+
})
|
|
129
|
+
}
|
|
118
130
|
} else {
|
|
119
131
|
if (!_Transmitter) {
|
|
120
132
|
const { Transmitter } = requireDynamic('../transmitter/Transmitter.js')
|
|
121
133
|
_Transmitter = Transmitter
|
|
122
134
|
}
|
|
123
|
-
|
|
135
|
+
if (!_Transmitter) {
|
|
136
|
+
throw new Error('Transmitter is undefined')
|
|
137
|
+
}
|
|
138
|
+
if (messageByteArray instanceof Uint8Array) {
|
|
139
|
+
responseByteArray = _Transmitter.sendCommand(messageByteArray)
|
|
140
|
+
} else {
|
|
141
|
+
responseByteArray = messageByteArray.then((resolvedMessage) => {
|
|
142
|
+
return _Transmitter.sendCommand(resolvedMessage)
|
|
143
|
+
})
|
|
144
|
+
}
|
|
124
145
|
}
|
|
125
|
-
}
|
|
126
146
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
147
|
+
if (!responseByteArray) {
|
|
148
|
+
throw new Error('No response received from Transmitter')
|
|
149
|
+
}
|
|
130
150
|
|
|
131
|
-
|
|
151
|
+
if (responseByteArray instanceof Promise) {
|
|
152
|
+
return responseByteArray.then((resolvedResponse) => {
|
|
153
|
+
return new CommandDeserializer(resolvedResponse).deserialize()
|
|
154
|
+
})
|
|
155
|
+
} else {
|
|
156
|
+
return new CommandDeserializer(responseByteArray).deserialize()
|
|
157
|
+
}
|
|
158
|
+
}
|
|
132
159
|
} catch (error) {
|
|
133
160
|
throw error
|
|
134
161
|
}
|
|
@@ -136,8 +163,8 @@ export class Interpreter {
|
|
|
136
163
|
|
|
137
164
|
/**
|
|
138
165
|
*
|
|
139
|
-
* @param {
|
|
140
|
-
* @returns {Command}
|
|
166
|
+
* @param {Uint8Array} messageByteArray
|
|
167
|
+
* @returns {Promise<Command> | Command}
|
|
141
168
|
*/
|
|
142
169
|
process(messageByteArray) {
|
|
143
170
|
try {
|
|
@@ -5,11 +5,11 @@ import { TypeDeserializer } from './TypeDeserializer.js'
|
|
|
5
5
|
|
|
6
6
|
export class CommandDeserializer {
|
|
7
7
|
/**
|
|
8
|
-
* @param {
|
|
8
|
+
* @param {Uint8Array} byteArray
|
|
9
9
|
*/
|
|
10
|
-
constructor(
|
|
11
|
-
this.buffer =
|
|
12
|
-
this.command = new Command(buffer[0], buffer[10], [])
|
|
10
|
+
constructor(byteArray) {
|
|
11
|
+
this.buffer = byteArray
|
|
12
|
+
this.command = new Command(this.buffer[0], this.buffer[10], [])
|
|
13
13
|
this.position = 11
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -11,15 +11,21 @@ import { TypesHandler } from '../../utils/TypesHandler.js'
|
|
|
11
11
|
class CommandSerializer {
|
|
12
12
|
/**
|
|
13
13
|
* Serializes the root command with connection data and optional runtime version.
|
|
14
|
-
* @param {Command} rootCommand
|
|
14
|
+
* @param {Promise<Command> | Command} rootCommand
|
|
15
15
|
* @param {IConnectionData} connectionData
|
|
16
16
|
* @param {number} runtimeVersion
|
|
17
|
-
* @returns {
|
|
17
|
+
* @returns {Promise<Uint8Array> | Uint8Array}
|
|
18
18
|
*/
|
|
19
19
|
serialize(rootCommand, connectionData, runtimeVersion = 0) {
|
|
20
20
|
const buffers = []
|
|
21
21
|
|
|
22
22
|
// Write runtime name and version
|
|
23
|
+
if (rootCommand instanceof Promise) {
|
|
24
|
+
return rootCommand.then(resolvedCommand => {
|
|
25
|
+
return this.serialize(resolvedCommand, connectionData, runtimeVersion)
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
buffers.push(Uint8Array.of(rootCommand.runtimeName, runtimeVersion))
|
|
24
30
|
|
|
25
31
|
// Write connection data or default zeros
|
|
@@ -5,7 +5,7 @@ import { StringEncodingMode } from '../../utils/StringEncodingMode.js'
|
|
|
5
5
|
|
|
6
6
|
export class TypeDeserializer {
|
|
7
7
|
/**
|
|
8
|
-
* @param {
|
|
8
|
+
* @param {Uint8Array} encodedCommand
|
|
9
9
|
* @returns {Command}
|
|
10
10
|
*/
|
|
11
11
|
static deserializeCommand(encodedCommand) {
|
|
@@ -14,7 +14,7 @@ export class TypeDeserializer {
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* @param {number} stringEncodingMode
|
|
17
|
-
* @param {
|
|
17
|
+
* @param {Uint8Array} encodedString
|
|
18
18
|
*/
|
|
19
19
|
static deserializeString(stringEncodingMode, encodedString) {
|
|
20
20
|
switch (stringEncodingMode) {
|
|
@@ -43,7 +43,7 @@ export class TypeDeserializer {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
|
-
* @param {
|
|
46
|
+
* @param {Uint8Array} encodedInt
|
|
47
47
|
*/
|
|
48
48
|
static deserializeInt(encodedInt) {
|
|
49
49
|
return (
|
|
@@ -55,56 +55,56 @@ export class TypeDeserializer {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
* @param {
|
|
58
|
+
* @param {Uint8Array} encodedBool
|
|
59
59
|
*/
|
|
60
60
|
static deserializeBool(encodedBool) {
|
|
61
61
|
return encodedBool[0] === 1
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
* @param {
|
|
65
|
+
* @param {Uint8Array} encodedFloat
|
|
66
66
|
*/
|
|
67
67
|
static deserializeFloat(encodedFloat) {
|
|
68
68
|
return Buffer.from(encodedFloat).readFloatLE()
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
|
-
* @param {
|
|
72
|
+
* @param {Uint8Array} encodedByte
|
|
73
73
|
*/
|
|
74
74
|
static deserializeByte(encodedByte) {
|
|
75
75
|
return Buffer.from(encodedByte).readUint8()
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
|
-
* @param {
|
|
79
|
+
* @param {Uint8Array} encodedChar
|
|
80
80
|
*/
|
|
81
81
|
static deserializeChar(encodedChar) {
|
|
82
82
|
return Buffer.from(encodedChar).readUint8()
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
|
-
* @param {
|
|
86
|
+
* @param {Uint8Array} encodedLongLong
|
|
87
87
|
*/
|
|
88
88
|
static deserializeLongLong(encodedLongLong) {
|
|
89
89
|
return Buffer.from(encodedLongLong).readBigInt64LE()
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
|
-
* @param {
|
|
93
|
+
* @param {Uint8Array} encodedDouble
|
|
94
94
|
*/
|
|
95
95
|
static deserializeDouble(encodedDouble) {
|
|
96
96
|
return Buffer.from(encodedDouble).readDoubleLE()
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
|
-
* @param {
|
|
100
|
+
* @param {Uint8Array} encodedULLong
|
|
101
101
|
*/
|
|
102
102
|
static deserializeULLong(encodedULLong) {
|
|
103
103
|
return Buffer.from(encodedULLong).readBigUInt64LE()
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
|
-
* @param {
|
|
107
|
+
* @param {Uint8Array} encodedUInt
|
|
108
108
|
*/
|
|
109
109
|
static deserializeUInt(encodedUInt) {
|
|
110
110
|
return Buffer.from(encodedUInt).readUIntLE(0, 4)
|
|
@@ -18,14 +18,13 @@ export class Receiver {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* @param {
|
|
21
|
+
* @param {Uint8Array} messageByteArray
|
|
22
|
+
* @returns {Promise<Uint8Array> | Uint8Array}
|
|
22
23
|
*/
|
|
23
24
|
static sendCommand(messageByteArray) {
|
|
24
25
|
try {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.connectionData
|
|
28
|
-
)
|
|
26
|
+
let command = new Interpreter().process(messageByteArray)
|
|
27
|
+
return new CommandSerializer().serialize(command, this.connectionData)
|
|
29
28
|
} catch (error) {
|
|
30
29
|
const exceptionCommand = ExceptionSerializer.serializeException(
|
|
31
30
|
error,
|
|
@@ -36,11 +35,11 @@ export class Receiver {
|
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
/**
|
|
39
|
-
* @param {
|
|
40
|
-
* @returns {
|
|
38
|
+
* @param {Uint8Array} messageByteArray
|
|
39
|
+
* @returns {Promise<Uint8Array> | Uint8Array}
|
|
41
40
|
*/
|
|
42
41
|
static heartBeat(messageByteArray) {
|
|
43
|
-
let response = new
|
|
42
|
+
let response = new Uint8Array(2)
|
|
44
43
|
response[0] = messageByteArray[11]
|
|
45
44
|
response[1] = messageByteArray[12] - 2
|
|
46
45
|
return response
|
|
@@ -3,14 +3,16 @@ import { Receiver } from './Receiver.js'
|
|
|
3
3
|
|
|
4
4
|
export class ReceiverNative {
|
|
5
5
|
/**
|
|
6
|
-
* @param {
|
|
6
|
+
* @param {Uint8Array} messageByteArray
|
|
7
|
+
* @returns {Promise<Uint8Array> | Uint8Array}
|
|
7
8
|
*/
|
|
8
9
|
static sendCommand(messageByteArray) {
|
|
9
10
|
return Receiver.sendCommand(messageByteArray)
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
|
-
* @param {
|
|
14
|
+
* @param {Uint8Array} messageByteArray
|
|
15
|
+
* @returns {Uint8Array | Promise<Uint8Array>}
|
|
14
16
|
*/
|
|
15
17
|
static heartBeat(messageByteArray) {
|
|
16
18
|
return Receiver.heartBeat(messageByteArray)
|
|
@@ -3,8 +3,8 @@ import { TransmitterWrapper } from './TransmitterWrapper.js'
|
|
|
3
3
|
|
|
4
4
|
export class Transmitter {
|
|
5
5
|
/**
|
|
6
|
-
* @param {
|
|
7
|
-
* @returns {
|
|
6
|
+
* @param {Uint8Array} messageArray
|
|
7
|
+
* @returns {Uint8Array}
|
|
8
8
|
*/
|
|
9
9
|
static sendCommand(messageArray) {
|
|
10
10
|
return TransmitterWrapper.sendCommand(messageArray)
|
|
@@ -22,9 +22,9 @@ export class TransmitterWebsocket {
|
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* @async
|
|
25
|
-
* @param {
|
|
25
|
+
* @param {Uint8Array} messageByteArray
|
|
26
26
|
* @param {IConnectionData} connectionData
|
|
27
|
-
* @returns {Promise<
|
|
27
|
+
* @returns {Promise<Uint8Array>} responseByteArray
|
|
28
28
|
*/
|
|
29
29
|
static sendCommand(messageByteArray, connectionData) {
|
|
30
30
|
const { hostname } = connectionData
|
|
@@ -17,9 +17,9 @@ export class TransmitterWebsocketBrowser {
|
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* @async
|
|
20
|
-
* @param {
|
|
20
|
+
* @param {Uint8Array} messageByteArray
|
|
21
21
|
* @param {IConnectionData} connectionData
|
|
22
|
-
* @returns {Promise<
|
|
22
|
+
* @returns {Promise<Uint8Array>} responseByteArray
|
|
23
23
|
*/
|
|
24
24
|
static async sendCommand(messageByteArray, connectionData) {
|
|
25
25
|
const { hostname } = connectionData
|
|
@@ -36,7 +36,7 @@ let WebSocket = null
|
|
|
36
36
|
/** @type {Record<string, wsClient>} */
|
|
37
37
|
export const clients = {}
|
|
38
38
|
|
|
39
|
-
/** @type {Record<string, Array<{resolve: Function, reject: Function, messageArray:
|
|
39
|
+
/** @type {Record<string, Array<{resolve: Function, reject: Function, messageArray: Uint8Array}>>} */
|
|
40
40
|
export const messageQueue = {}
|
|
41
41
|
|
|
42
42
|
/** @type {Record<string, boolean>} */
|
|
@@ -69,8 +69,8 @@ class WebSocketClient {
|
|
|
69
69
|
/**
|
|
70
70
|
* Sends messageArray through websocket connection with guaranteed order preservation
|
|
71
71
|
* @async
|
|
72
|
-
* @param {
|
|
73
|
-
* @returns {Promise<
|
|
72
|
+
* @param {Uint8Array} messageArray
|
|
73
|
+
* @returns {Promise<Uint8Array>}
|
|
74
74
|
*/
|
|
75
75
|
send(messageArray) {
|
|
76
76
|
return new Promise((resolve, reject) => {
|
|
@@ -117,8 +117,8 @@ class WebSocketClient {
|
|
|
117
117
|
* Sends a single message through websocket connection
|
|
118
118
|
* @private
|
|
119
119
|
* @async
|
|
120
|
-
* @param {
|
|
121
|
-
* @returns {Promise<
|
|
120
|
+
* @param {Uint8Array} messageArray
|
|
121
|
+
* @returns {Promise<Uint8Array>}
|
|
122
122
|
*/
|
|
123
123
|
_send(messageArray) {
|
|
124
124
|
return new Promise((resolve, reject) => {
|
|
@@ -25,7 +25,7 @@ if (isBrowserRuntime()) {
|
|
|
25
25
|
/** @type {Record<string, WebSocket>} */
|
|
26
26
|
export const clients = {}
|
|
27
27
|
|
|
28
|
-
/** @type {Record<string, Array<{resolve: Function, reject: Function, messageArray:
|
|
28
|
+
/** @type {Record<string, Array<{resolve: Function, reject: Function, messageArray: Uint8Array}>>} */
|
|
29
29
|
export const messageQueue = {}
|
|
30
30
|
|
|
31
31
|
/** @type {Record<string, boolean>} */
|
|
@@ -59,8 +59,8 @@ class WebSocketClientBrowser {
|
|
|
59
59
|
/**
|
|
60
60
|
* Sends messageArray through websocket connection with guaranteed order preservation
|
|
61
61
|
* @async
|
|
62
|
-
* @param {
|
|
63
|
-
* @returns {Promise<
|
|
62
|
+
* @param {Uint8Array} messageArray
|
|
63
|
+
* @returns {Promise<Uint8Array>}
|
|
64
64
|
*/
|
|
65
65
|
send(messageArray) {
|
|
66
66
|
return new Promise((resolve, reject) => {
|
|
@@ -106,8 +106,8 @@ class WebSocketClientBrowser {
|
|
|
106
106
|
* Sends a single message through websocket connection
|
|
107
107
|
* @private
|
|
108
108
|
* @async
|
|
109
|
-
* @param {
|
|
110
|
-
* @returns {Promise<
|
|
109
|
+
* @param {Uint8Array} messageArray
|
|
110
|
+
* @returns {Promise<Uint8Array>}
|
|
111
111
|
*/
|
|
112
112
|
_sendSingle(messageArray) {
|
|
113
113
|
return new Promise((resolve, reject) => {
|
|
@@ -174,8 +174,8 @@ class WebSocketClientBrowser {
|
|
|
174
174
|
* Sends the data to the WebSocket server and listens for a response.
|
|
175
175
|
* @private
|
|
176
176
|
* @param {WebSocket} client
|
|
177
|
-
* @param {
|
|
178
|
-
* @param {(value:
|
|
177
|
+
* @param {Uint8Array} data
|
|
178
|
+
* @param {(value: Uint8Array) => void} resolve
|
|
179
179
|
* @param {(reason?: any) => void} reject
|
|
180
180
|
*/
|
|
181
181
|
_sendMessage(client, data, resolve, reject) {
|
|
@@ -188,7 +188,7 @@ class WebSocketClientBrowser {
|
|
|
188
188
|
return reject(new Error('Invalid message received'))
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
const byteArray = new
|
|
191
|
+
const byteArray = new Uint8Array(message?.data)
|
|
192
192
|
resolve(byteArray)
|
|
193
193
|
|
|
194
194
|
if (this.isDisconnectedAfterMessage) {
|
|
@@ -27,12 +27,10 @@ class InvocationContext {
|
|
|
27
27
|
#connectionData
|
|
28
28
|
/** @type {Command | null} */
|
|
29
29
|
#currentCommand = null
|
|
30
|
-
/** @type {Command | null} */
|
|
30
|
+
/** @type {Command | Promise<Command> | null} */
|
|
31
31
|
#responseCommand = null
|
|
32
32
|
/** @type {boolean} */
|
|
33
33
|
#isExecuted = false
|
|
34
|
-
/** @type {any | null} */
|
|
35
|
-
#resultValue = null
|
|
36
34
|
/** @type {Interpreter | null} */
|
|
37
35
|
#interpreter = null
|
|
38
36
|
|
|
@@ -50,7 +48,6 @@ class InvocationContext {
|
|
|
50
48
|
this.#responseCommand = null
|
|
51
49
|
this.#isExecuted = isExecuted
|
|
52
50
|
this.#interpreter = null
|
|
53
|
-
this.#resultValue = null
|
|
54
51
|
}
|
|
55
52
|
|
|
56
53
|
/**
|
|
@@ -59,7 +56,6 @@ class InvocationContext {
|
|
|
59
56
|
*/
|
|
60
57
|
#createInstanceContext(localCommand) {
|
|
61
58
|
if (this.#connectionData.connectionType === ConnectionType.WEB_SOCKET) {
|
|
62
|
-
// @ts-expect-error
|
|
63
59
|
return new InvocationWsContext(
|
|
64
60
|
this.#runtimeName,
|
|
65
61
|
this.#connectionData,
|
|
@@ -96,7 +92,17 @@ class InvocationContext {
|
|
|
96
92
|
throw new Error('Object is not iterable')
|
|
97
93
|
}
|
|
98
94
|
let position = -1
|
|
99
|
-
|
|
95
|
+
/** @type {number} */
|
|
96
|
+
let arraySize = 0
|
|
97
|
+
/** @type {InvocationContext | Promise<InvocationContext>} */
|
|
98
|
+
const sizeCtx = /** @type {any} */ (this.getSize().execute())
|
|
99
|
+
if (sizeCtx instanceof Promise) {
|
|
100
|
+
sizeCtx.then((ctx) => {
|
|
101
|
+
arraySize = Number(ctx.getValue())
|
|
102
|
+
})
|
|
103
|
+
} else {
|
|
104
|
+
arraySize = Number(sizeCtx.getValue())
|
|
105
|
+
}
|
|
100
106
|
|
|
101
107
|
return {
|
|
102
108
|
next: () => ({
|
|
@@ -113,7 +119,7 @@ class InvocationContext {
|
|
|
113
119
|
* Commands are becoming nested through each invocation of methods on Invocation Context.
|
|
114
120
|
* Each invocation triggers the creation of new Invocation Context instance wrapping the current command with new parent command valid for invoked method.
|
|
115
121
|
* Developer can decide on any moment of the materialization for the context taking full control of the chunks of the expression being transferred and processed on target runtime.
|
|
116
|
-
* @returns {InvocationContext} the InvocationContext after executing the command.
|
|
122
|
+
* @returns {Promise<InvocationContext> | InvocationContext} the InvocationContext after executing the command.
|
|
117
123
|
* @see [Javonet Guides](https://www.javonet.com/guides/v2/javascript/foundations/execute-method)
|
|
118
124
|
* @method
|
|
119
125
|
*/
|
|
@@ -125,22 +131,29 @@ class InvocationContext {
|
|
|
125
131
|
if (!this.#interpreter) {
|
|
126
132
|
this.#interpreter = new Interpreter()
|
|
127
133
|
}
|
|
128
|
-
|
|
134
|
+
|
|
129
135
|
this.#responseCommand = this.#interpreter.execute(this.#currentCommand, this.#connectionData)
|
|
130
136
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
137
|
+
const handleResponse = (/** @type {Command } */ resolvedResponse) => {
|
|
138
|
+
if (!resolvedResponse) {
|
|
139
|
+
throw new Error('responseCommand is undefined in Invocation Context execute method')
|
|
140
|
+
}
|
|
141
|
+
if (resolvedResponse.commandType === CommandType.Exception) {
|
|
142
|
+
throw ExceptionThrower.throwException(resolvedResponse)
|
|
143
|
+
}
|
|
144
|
+
if (resolvedResponse.commandType === CommandType.CreateClassInstance) {
|
|
145
|
+
this.#currentCommand = resolvedResponse
|
|
146
|
+
this.#isExecuted = true
|
|
147
|
+
return this
|
|
148
|
+
}
|
|
149
|
+
return new InvocationContext(this.#runtimeName, this.#connectionData, resolvedResponse, true)
|
|
141
150
|
}
|
|
142
151
|
|
|
143
|
-
|
|
152
|
+
if (this.#responseCommand instanceof Promise) {
|
|
153
|
+
return this.#responseCommand.then(handleResponse)
|
|
154
|
+
} else {
|
|
155
|
+
return handleResponse(this.#responseCommand)
|
|
156
|
+
}
|
|
144
157
|
}
|
|
145
158
|
|
|
146
159
|
/**
|
|
@@ -397,7 +410,7 @@ class InvocationContext {
|
|
|
397
410
|
|
|
398
411
|
/**
|
|
399
412
|
* Retrieves the type of the object from the target runtime.
|
|
400
|
-
* @returns {string} The type of the object.
|
|
413
|
+
* @returns {Promise<string> | string} The type of the object.
|
|
401
414
|
* @see [Javonet Guides](https://www.javonet.com/guides/v2/javascript/type-handling/getting-object-type)
|
|
402
415
|
* @method
|
|
403
416
|
*/
|
|
@@ -408,7 +421,9 @@ class InvocationContext {
|
|
|
408
421
|
this.#connectionData,
|
|
409
422
|
this.#buildCommand(localCommand)
|
|
410
423
|
)
|
|
411
|
-
|
|
424
|
+
/** @type {InvocationContext} */
|
|
425
|
+
const execCtx = /** @type {any} */ (invocationContext.execute())
|
|
426
|
+
const result = execCtx.getValue()
|
|
412
427
|
return /** @type {string} */ (result)
|
|
413
428
|
}
|
|
414
429
|
|
|
@@ -424,39 +439,36 @@ class InvocationContext {
|
|
|
424
439
|
|
|
425
440
|
/**
|
|
426
441
|
* Retrieves an array from the target runtime.
|
|
427
|
-
* @returns {
|
|
428
|
-
* @see [Javonet Guides](https://www.javonet.com/guides/v2/javascript/arrays-and-collections/retrieve-array)
|
|
442
|
+
* @returns {Promise<any[]>}
|
|
429
443
|
* @method
|
|
430
444
|
*/
|
|
431
445
|
retrieveArray() {
|
|
432
|
-
|
|
433
|
-
|
|
446
|
+
const localCommand = new Command(this.#runtimeName, CommandType.RetrieveArray, [])
|
|
447
|
+
const localInvCtx = new InvocationContext(
|
|
434
448
|
this.#runtimeName,
|
|
435
449
|
this.#connectionData,
|
|
436
450
|
this.#buildCommand(localCommand)
|
|
437
451
|
)
|
|
438
|
-
localInvCtx.execute()
|
|
439
452
|
|
|
440
|
-
|
|
441
|
-
|
|
453
|
+
localInvCtx.execute() // Promise<InvocationContext> | InvocationContext
|
|
454
|
+
|
|
455
|
+
const extract = (/** @type {Command} */ respCommand) => {
|
|
456
|
+
return respCommand.payload
|
|
442
457
|
}
|
|
443
|
-
|
|
458
|
+
|
|
459
|
+
// Normalize to Promise
|
|
460
|
+
return localInvCtx.#responseCommand instanceof Promise ? localInvCtx.#responseCommand.then(extract) : localInvCtx.#responseCommand?.payload
|
|
444
461
|
}
|
|
445
462
|
|
|
446
463
|
/**
|
|
447
464
|
* Returns the primitive value from the target runtime. This could be any primitive type in JavaScript,
|
|
448
465
|
* such as int, boolean, byte, char, long, double, float, etc.
|
|
449
|
-
* @returns {
|
|
466
|
+
* @returns {unknown} The value of the current command.
|
|
450
467
|
* @see [Javonet Guides](https://www.javonet.com/guides/v2/javascript/foundations/execute-method)
|
|
451
468
|
* @method
|
|
452
469
|
*/
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* @returns {unknown}
|
|
456
|
-
*/
|
|
457
470
|
getValue() {
|
|
458
|
-
|
|
459
|
-
return this.#resultValue
|
|
471
|
+
return this.#currentCommand?.payload[0]
|
|
460
472
|
}
|
|
461
473
|
|
|
462
474
|
/**
|
|
@@ -522,8 +534,6 @@ class InvocationWsContext extends InvocationContext {
|
|
|
522
534
|
#interpreter
|
|
523
535
|
/** @type {boolean} */
|
|
524
536
|
#isExecuted
|
|
525
|
-
/** @type {any | null} */
|
|
526
|
-
#resultValue
|
|
527
537
|
|
|
528
538
|
/**
|
|
529
539
|
* @param {RuntimeNameType} runtimeName
|
|
@@ -540,7 +550,6 @@ class InvocationWsContext extends InvocationContext {
|
|
|
540
550
|
this.#responseCommand = null
|
|
541
551
|
this.#isExecuted = isExecuted
|
|
542
552
|
this.#interpreter = null
|
|
543
|
-
this.#resultValue = null
|
|
544
553
|
}
|
|
545
554
|
|
|
546
555
|
/**
|
|
@@ -555,7 +564,6 @@ class InvocationWsContext extends InvocationContext {
|
|
|
555
564
|
* @async
|
|
556
565
|
* @method
|
|
557
566
|
*/
|
|
558
|
-
// @ts-expect-error
|
|
559
567
|
async execute() {
|
|
560
568
|
if (this.#currentCommand === null) {
|
|
561
569
|
throw new Error('currentCommand is undefined in Invocation Context execute method')
|
|
@@ -591,7 +599,6 @@ class InvocationWsContext extends InvocationContext {
|
|
|
591
599
|
* @async
|
|
592
600
|
* @method
|
|
593
601
|
*/
|
|
594
|
-
// @ts-expect-error
|
|
595
602
|
async getResultType() {
|
|
596
603
|
const localCommand = new Command(this.#runtimeName, CommandType.GetResultType, [])
|
|
597
604
|
const invocationContext = new InvocationWsContext(
|