javonet-nodejs-sdk 2.5.17 → 2.5.18

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.
Files changed (193) hide show
  1. package/dist/core/handler/AbstractHandler.cjs +23 -6
  2. package/dist/core/handler/ArrayGetItemHandler.cjs +4 -1
  3. package/dist/core/handler/ArrayGetRankHandler.cjs +3 -0
  4. package/dist/core/handler/ArrayGetSizeHandler.cjs +3 -0
  5. package/dist/core/handler/ArrayHandler.cjs +3 -0
  6. package/dist/core/handler/ArraySetItemHandler.cjs +4 -1
  7. package/dist/core/handler/CastingHandler.cjs +3 -1
  8. package/dist/core/handler/ConvertTypeHandler.cjs +4 -4
  9. package/dist/core/handler/CreateClassInstanceHandler.cjs +5 -2
  10. package/dist/core/handler/DestructReferenceHandler.cjs +3 -0
  11. package/dist/core/handler/EnableNamespaceHandler.cjs +3 -0
  12. package/dist/core/handler/EnableTypeHandler.cjs +3 -0
  13. package/dist/core/handler/GetGlobalStaticFieldHandler.cjs +3 -0
  14. package/dist/core/handler/GetInstanceFieldHandler.cjs +3 -0
  15. package/dist/core/handler/GetInstanceMethodAsDelegateHandler.cjs +15 -4
  16. package/dist/core/handler/GetStaticFieldHandler.cjs +4 -0
  17. package/dist/core/handler/GetStaticMethodAsDelegateHandler.cjs +8 -6
  18. package/dist/core/handler/GetTypeHandler.cjs +10 -2
  19. package/dist/core/handler/Handler.cjs +8 -11
  20. package/dist/core/handler/InvokeGlobalFunctionHandler.cjs +3 -0
  21. package/dist/core/handler/InvokeInstanceMethodHandler.cjs +3 -0
  22. package/dist/core/handler/InvokeStaticMethodHandler.cjs +3 -0
  23. package/dist/core/handler/LoadLibraryHandler.cjs +4 -0
  24. package/dist/core/handler/SetGlobalStaticFieldHandler.cjs +3 -0
  25. package/dist/core/handler/SetInstanceFieldHandler.cjs +3 -0
  26. package/dist/core/handler/SetStaticFieldHandler.cjs +3 -0
  27. package/dist/core/handler/ValueHandler.cjs +4 -0
  28. package/dist/core/interpreter/Interpreter.cjs +28 -12
  29. package/dist/core/namespaceCache/NamespaceCache.cjs +12 -0
  30. package/dist/core/protocol/CommandDeserializer.cjs +10 -2
  31. package/dist/core/protocol/CommandSerializer.cjs +24 -4
  32. package/dist/core/protocol/TypeDeserializer.cjs +35 -0
  33. package/dist/core/protocol/TypeSerializer.cjs +51 -7
  34. package/dist/core/receiver/Receiver.cjs +1 -1
  35. package/dist/core/receiver/ReceiverNative.cjs +6 -0
  36. package/dist/core/transmitter/Transmitter.cjs +18 -2
  37. package/dist/core/transmitter/TransmitterWebsocket.cjs +8 -5
  38. package/dist/core/transmitter/TransmitterWebsocketBrowser.cjs +1 -1
  39. package/dist/core/transmitter/TransmitterWrapper.cjs +25 -8
  40. package/dist/core/typeCache/TypeCache.cjs +8 -0
  41. package/dist/core/webSocketClient/WebSocketClient.cjs +22 -12
  42. package/dist/core/webSocketClient/WebSocketClientBrowser.cjs +2 -2
  43. package/dist/sdk/ConfigRuntimeFactory.cjs +60 -55
  44. package/dist/sdk/InvocationContext.cjs +81 -23
  45. package/dist/sdk/Javonet.cjs +14 -33
  46. package/dist/sdk/RuntimeContext.cjs +85 -30
  47. package/dist/sdk/RuntimeFactory.cjs +3 -0
  48. package/dist/sdk/tools/ActivationHelper.cjs +66 -0
  49. package/dist/sdk/tools/JsonResolver.cjs +214 -0
  50. package/dist/types/core/handler/AbstractHandler.d.ts +23 -4
  51. package/dist/types/core/handler/ArrayGetItemHandler.d.ts +8 -1
  52. package/dist/types/core/handler/ArrayGetRankHandler.d.ts +8 -1
  53. package/dist/types/core/handler/ArrayGetSizeHandler.d.ts +8 -1
  54. package/dist/types/core/handler/ArrayHandler.d.ts +8 -1
  55. package/dist/types/core/handler/ArraySetItemHandler.d.ts +8 -1
  56. package/dist/types/core/handler/CastingHandler.d.ts +4 -0
  57. package/dist/types/core/handler/ConvertTypeHandler.d.ts +8 -4
  58. package/dist/types/core/handler/CreateClassInstanceHandler.d.ts +8 -1
  59. package/dist/types/core/handler/DestructReferenceHandler.d.ts +8 -1
  60. package/dist/types/core/handler/EnableNamespaceHandler.d.ts +8 -1
  61. package/dist/types/core/handler/EnableTypeHandler.d.ts +8 -1
  62. package/dist/types/core/handler/GetGlobalStaticFieldHandler.d.ts +8 -1
  63. package/dist/types/core/handler/GetInstanceFieldHandler.d.ts +8 -1
  64. package/dist/types/core/handler/GetStaticFieldHandler.d.ts +9 -1
  65. package/dist/types/core/handler/GetStaticMethodAsDelegateHandler.d.ts +4 -3
  66. package/dist/types/core/handler/GetTypeHandler.d.ts +17 -8
  67. package/dist/types/core/handler/Handler.d.ts +8 -8
  68. package/dist/types/core/handler/InvokeDelegateHandler.d.ts +4 -0
  69. package/dist/types/core/handler/InvokeGlobalFunctionHandler.d.ts +8 -1
  70. package/dist/types/core/handler/InvokeInstanceMethodHandler.d.ts +8 -1
  71. package/dist/types/core/handler/InvokeStaticMethodHandler.d.ts +8 -1
  72. package/dist/types/core/handler/LoadLibraryHandler.d.ts +8 -3
  73. package/dist/types/core/handler/SetGlobalStaticFieldHandler.d.ts +4 -0
  74. package/dist/types/core/handler/SetInstanceFieldHandler.d.ts +8 -1
  75. package/dist/types/core/handler/SetStaticFieldHandler.d.ts +8 -1
  76. package/dist/types/core/handler/ValueHandler.d.ts +9 -1
  77. package/dist/types/core/interpreter/Interpreter.d.ts +11 -10
  78. package/dist/types/core/namespaceCache/NamespaceCache.d.ts +17 -5
  79. package/dist/types/core/protocol/CommandDeserializer.d.ts +10 -3
  80. package/dist/types/core/protocol/CommandSerializer.d.ts +19 -2
  81. package/dist/types/core/protocol/TypeDeserializer.d.ts +46 -11
  82. package/dist/types/core/protocol/TypeSerializer.d.ts +53 -13
  83. package/dist/types/core/receiver/Receiver.d.ts +2 -2
  84. package/dist/types/core/receiver/ReceiverNative.d.ts +8 -2
  85. package/dist/types/core/transmitter/Transmitter.d.ts +20 -4
  86. package/dist/types/core/transmitter/TransmitterWebsocket.d.ts +8 -4
  87. package/dist/types/core/transmitter/TransmitterWebsocketBrowser.d.ts +4 -7
  88. package/dist/types/core/transmitter/TransmitterWrapper.d.ts +12 -3
  89. package/dist/types/core/typeCache/TypeCache.d.ts +15 -5
  90. package/dist/types/core/webSocketClient/WebSocketClient.d.ts +17 -21
  91. package/dist/types/sdk/ConfigRuntimeFactory.d.ts +9 -6
  92. package/dist/types/sdk/InvocationContext.d.ts +38 -6
  93. package/dist/types/sdk/Javonet.d.ts +12 -7
  94. package/dist/types/sdk/RuntimeContext.d.ts +23 -7
  95. package/dist/types/sdk/RuntimeFactory.d.ts +9 -2
  96. package/dist/types/sdk/tools/ActivationHelper.d.ts +20 -0
  97. package/dist/types/sdk/tools/JsonResolver.d.ts +87 -0
  98. package/dist/types/utils/Command.d.ts +18 -14
  99. package/dist/types/utils/CustomError.d.ts +5 -1
  100. package/dist/types/utils/Runtime.d.ts +1 -2
  101. package/dist/types/utils/RuntimeNameHandler.d.ts +6 -2
  102. package/dist/types/utils/Type.d.ts +1 -0
  103. package/dist/types/utils/TypesHandler.d.ts +16 -0
  104. package/dist/types/utils/UtilsConst.d.ts +32 -0
  105. package/dist/types/utils/connectionData/IConnectionData.d.ts +16 -3
  106. package/dist/types/utils/connectionData/InMemoryConnectionData.d.ts +4 -3
  107. package/dist/types/utils/connectionData/WsConnectionData.d.ts +7 -10
  108. package/dist/types/utils/nodejs/connectionData/TcpConnectionData.d.ts +17 -10
  109. package/dist/utils/Command.cjs +9 -9
  110. package/dist/utils/CustomError.cjs +4 -0
  111. package/dist/utils/RuntimeNameHandler.cjs +1 -1
  112. package/dist/utils/Type.cjs +2 -1
  113. package/dist/utils/TypesHandler.cjs +55 -0
  114. package/dist/utils/UtilsConst.cjs +101 -0
  115. package/dist/utils/connectionData/IConnectionData.cjs +9 -0
  116. package/dist/utils/connectionData/InMemoryConnectionData.cjs +10 -0
  117. package/dist/utils/connectionData/WsConnectionData.cjs +4 -0
  118. package/dist/utils/nodejs/connectionData/TcpConnectionData.cjs +33 -3
  119. package/lib/core/delegatesCache/DelegatesCache.js +1 -0
  120. package/lib/core/handler/AbstractHandler.js +27 -9
  121. package/lib/core/handler/ArrayGetItemHandler.js +9 -1
  122. package/lib/core/handler/ArrayGetRankHandler.js +9 -0
  123. package/lib/core/handler/ArrayGetSizeHandler.js +8 -0
  124. package/lib/core/handler/ArrayHandler.js +8 -0
  125. package/lib/core/handler/ArraySetItemHandler.js +11 -1
  126. package/lib/core/handler/CastingHandler.js +8 -1
  127. package/lib/core/handler/ConvertTypeHandler.js +9 -4
  128. package/lib/core/handler/CreateClassInstanceHandler.js +10 -4
  129. package/lib/core/handler/DestructReferenceHandler.js +8 -0
  130. package/lib/core/handler/EnableNamespaceHandler.js +8 -0
  131. package/lib/core/handler/EnableTypeHandler.js +8 -0
  132. package/lib/core/handler/GetGlobalStaticFieldHandler.js +9 -0
  133. package/lib/core/handler/GetInstanceFieldHandler.js +8 -0
  134. package/lib/core/handler/GetInstanceMethodAsDelegateHandler.js +9 -4
  135. package/lib/core/handler/GetStaticFieldHandler.js +9 -0
  136. package/lib/core/handler/GetStaticMethodAsDelegateHandler.js +8 -7
  137. package/lib/core/handler/GetTypeHandler.js +15 -5
  138. package/lib/core/handler/Handler.js +13 -18
  139. package/lib/core/handler/InvokeDelegateHandler.js +5 -0
  140. package/lib/core/handler/InvokeGlobalFunctionHandler.js +11 -1
  141. package/lib/core/handler/InvokeInstanceMethodHandler.js +8 -0
  142. package/lib/core/handler/InvokeStaticMethodHandler.js +8 -0
  143. package/lib/core/handler/LoadLibraryHandler.js +11 -0
  144. package/lib/core/handler/SetGlobalStaticFieldHandler.js +10 -0
  145. package/lib/core/handler/SetInstanceFieldHandler.js +7 -0
  146. package/lib/core/handler/SetStaticFieldHandler.js +8 -0
  147. package/lib/core/handler/ValueHandler.js +9 -0
  148. package/lib/core/interpreter/Interpreter.js +42 -24
  149. package/lib/core/namespaceCache/NamespaceCache.js +14 -0
  150. package/lib/core/protocol/CommandDeserializer.js +11 -2
  151. package/lib/core/protocol/CommandSerializer.js +40 -9
  152. package/lib/core/protocol/TypeDeserializer.js +36 -0
  153. package/lib/core/protocol/TypeSerializer.js +62 -7
  154. package/lib/core/receiver/Receiver.js +3 -2
  155. package/lib/core/receiver/ReceiverNative.js +8 -0
  156. package/lib/core/transmitter/Transmitter.js +18 -1
  157. package/lib/core/transmitter/TransmitterWebsocket.js +12 -5
  158. package/lib/core/transmitter/TransmitterWebsocketBrowser.js +2 -4
  159. package/lib/core/transmitter/TransmitterWrapper.js +27 -6
  160. package/lib/core/typeCache/TypeCache.js +9 -0
  161. package/lib/core/webSocketClient/WebSocketClient.js +33 -30
  162. package/lib/core/webSocketClient/WebSocketClientBrowser.js +5 -6
  163. package/lib/sdk/ConfigRuntimeFactory.js +70 -68
  164. package/lib/sdk/InvocationContext.js +94 -26
  165. package/lib/sdk/Javonet.js +18 -36
  166. package/lib/sdk/RuntimeContext.js +109 -29
  167. package/lib/sdk/RuntimeFactory.js +7 -0
  168. package/lib/sdk/tools/ActivationHelper.js +51 -0
  169. package/lib/sdk/tools/JsonResolver.js +226 -0
  170. package/lib/types.d.ts +57 -0
  171. package/lib/utils/Command.js +14 -9
  172. package/lib/utils/CustomError.js +5 -0
  173. package/lib/utils/Runtime.js +1 -1
  174. package/lib/utils/RuntimeLogger.js +3 -0
  175. package/lib/utils/RuntimeNameHandler.js +6 -1
  176. package/lib/utils/Type.js +2 -1
  177. package/lib/utils/TypesHandler.js +35 -0
  178. package/lib/utils/UtilsConst.js +88 -0
  179. package/lib/utils/connectionData/IConnectionData.js +14 -0
  180. package/lib/utils/connectionData/InMemoryConnectionData.js +15 -0
  181. package/lib/utils/connectionData/WsConnectionData.js +9 -0
  182. package/lib/utils/nodejs/connectionData/TcpConnectionData.js +35 -5
  183. package/package.json +4 -3
  184. package/dist/sdk/tools/browser/ConfigSourceResolver.cjs +0 -153
  185. package/dist/sdk/tools/nodejs/JsonFileResolver.cjs +0 -120
  186. package/dist/types/sdk/tools/browser/ConfigSourceResolver.d.ts +0 -46
  187. package/dist/types/sdk/tools/nodejs/JsonFileResolver.d.ts +0 -13
  188. package/dist/types/utils/nodejs/TypesConverter.d.ts +0 -48
  189. package/dist/utils/nodejs/TypesConverter.cjs +0 -102
  190. package/lib/declarations.d.ts +0 -35
  191. package/lib/sdk/tools/browser/ConfigSourceResolver.js +0 -146
  192. package/lib/sdk/tools/nodejs/JsonFileResolver.js +0 -106
  193. package/lib/utils/nodejs/TypesConverter.js +0 -91
@@ -1,7 +1,11 @@
1
+ //@ts-check
1
2
  import { getRequire, isNodejsRuntime } from '../../utils/Runtime.js'
2
3
 
3
4
  const requireDynamic = getRequire(import.meta.url)
4
5
 
6
+ /** @typedef {import('ws').WebSocket} wsClient */
7
+ /** @typedef {typeof import('ws').WebSocket} WebSocketClass */
8
+
5
9
  /**
6
10
  * Enum for WebSocket states.
7
11
  * @readonly
@@ -13,6 +17,7 @@ const WebSocketStateEnum = {
13
17
  ERROR: 'error',
14
18
  }
15
19
 
20
+ /** @type {WebSocketClass | null} */
16
21
  let WebSocket = null
17
22
 
18
23
  /**
@@ -21,34 +26,23 @@ let WebSocket = null
21
26
  class WebSocketClient {
22
27
  /**
23
28
  * @param {string} url
24
- * @param {object} options
29
+ * @param {{ isDisconnectedAfterMessage: boolean }} options
25
30
  */
26
31
  constructor(url, options) {
27
- /**
28
- * @type {string}
29
- */
32
+ /** @type {string} */
30
33
  this.url = url
31
-
32
- /**
33
- * @type {WebSocket | null}
34
- */
34
+ /** @type {wsClient | null} */
35
35
  this.instance = null
36
-
37
- /**
38
- * @type {boolean} isConnected indicates whether the WebSocket is connected.
39
- */
36
+ /** @type {boolean} */
40
37
  this.isConnected = false
41
-
42
- /**
43
- * @type {boolean}
44
- */
38
+ /** @type {boolean} */
45
39
  this.isDisconnectedAfterMessage = options?.isDisconnectedAfterMessage ?? true
46
40
  }
47
41
 
48
42
  /**
49
43
  * Connects to the WebSocket server.
50
44
  * @async
51
- * @returns {Promise<WebSocket>} - A promise that resolves when the connection is established.
45
+ * @returns {Promise<wsClient>} - A promise that resolves when the connection is established.
52
46
  */
53
47
  async connect() {
54
48
  if (!WebSocket) {
@@ -56,7 +50,12 @@ class WebSocketClient {
56
50
  try {
57
51
  WebSocket = requireDynamic('ws')
58
52
  } catch (error) {
59
- if (error.code === 'MODULE_NOT_FOUND') {
53
+ if (
54
+ typeof error === 'object' &&
55
+ error &&
56
+ 'code' in error &&
57
+ error.code === 'MODULE_NOT_FOUND'
58
+ ) {
60
59
  throw new Error('ws module not found. Please install it using npm install ws')
61
60
  }
62
61
  throw error
@@ -65,6 +64,10 @@ class WebSocketClient {
65
64
  }
66
65
 
67
66
  return new Promise((resolve, reject) => {
67
+ if (!WebSocket) {
68
+ reject(new Error('ws client is null'))
69
+ return
70
+ }
68
71
  const client = new WebSocket(this.url)
69
72
 
70
73
  client.on(WebSocketStateEnum.OPEN, () => {
@@ -73,7 +76,7 @@ class WebSocketClient {
73
76
  })
74
77
 
75
78
  // eslint-disable-next-line no-unused-vars
76
- client.on(WebSocketStateEnum.ERROR, (error) => {
79
+ client.on(WebSocketStateEnum.ERROR, (/** @type {unknown} */ error) => {
77
80
  reject(error)
78
81
  })
79
82
 
@@ -86,20 +89,16 @@ class WebSocketClient {
86
89
  /**
87
90
  * Sends messageArray through websocket connection
88
91
  * @async
89
- * @param {Buffer|ArrayBuffer|Buffer[]} messageArray
90
- * @returns {Promise<Buffer|ArrayBuffer|Buffer[]>}
92
+ * @param {Int8Array} messageArray
93
+ * @returns {Promise<Int8Array>}
91
94
  */
92
95
  send(messageArray) {
93
96
  return new Promise((resolve, reject) => {
94
97
  ;(this.instance ? Promise.resolve(this.instance) : this._connect())
95
98
  .then((client) => {
96
- client.send(messageArray, (err) => {
97
- if (err) {
98
- reject(err)
99
- }
100
- })
99
+ client.send(/** @type {any} */ (messageArray))
101
100
 
102
- client.on('message', (message) => {
101
+ client.on('message', (/** @type {any} */ message) => {
103
102
  resolve(message)
104
103
 
105
104
  if (this.isDisconnectedAfterMessage) {
@@ -128,7 +127,7 @@ class WebSocketClient {
128
127
  * Connects to the WebSocket server.
129
128
  * @private
130
129
  * @async
131
- * @returns {Promise<WebSocket>} - A promise that resolves when the connection is established.
130
+ * @returns {Promise<wsClient>} - A promise that resolves when the connection is established.
132
131
  */
133
132
  _connect() {
134
133
  if (!WebSocket) {
@@ -136,7 +135,7 @@ class WebSocketClient {
136
135
  try {
137
136
  WebSocket = requireDynamic('ws')
138
137
  } catch (error) {
139
- if (error.code === 'MODULE_NOT_FOUND') {
138
+ if (/** @type {{ code?: string }} */ (error).code === 'MODULE_NOT_FOUND') {
140
139
  throw new Error('ws module not found. Please install it using npm install ws')
141
140
  }
142
141
  throw error
@@ -145,6 +144,10 @@ class WebSocketClient {
145
144
  }
146
145
 
147
146
  return new Promise((resolve, reject) => {
147
+ if (!WebSocket) {
148
+ reject(new Error('ws client is null'))
149
+ return
150
+ }
148
151
  const client = new WebSocket(this.url)
149
152
 
150
153
  client.on(WebSocketStateEnum.OPEN, () => {
@@ -154,7 +157,7 @@ class WebSocketClient {
154
157
  })
155
158
 
156
159
  // eslint-disable-next-line no-unused-vars
157
- client.on(WebSocketStateEnum.ERROR, (error) => {
160
+ client.on(WebSocketStateEnum.ERROR, (/** @type {unknown} */ error) => {
158
161
  reject(error)
159
162
  })
160
163
 
@@ -1,6 +1,6 @@
1
+ // @ts-check
1
2
  import { isBrowserRuntime } from "../../utils/Runtime.js"
2
3
 
3
- // @ts-nocheck
4
4
  const WebSocketState = /** @type {const} */ ({
5
5
  OPEN: 'open',
6
6
  CLOSE: 'close',
@@ -9,13 +9,12 @@ const WebSocketState = /** @type {const} */ ({
9
9
  })
10
10
 
11
11
  /**
12
- * @type {WebSocket | null}
12
+ * @type {typeof WebSocket | null}
13
13
  */
14
14
  let WsClient = null
15
15
 
16
16
  if (isBrowserRuntime()) {
17
- /** @type {WebSocket} */
18
- // @ts-ignore
17
+ /** @type {typeof WebSocket} */
19
18
  WsClient = WebSocket
20
19
  }
21
20
 
@@ -96,8 +95,8 @@ class WebSocketClientBrowser {
96
95
  */
97
96
  _connect() {
98
97
  return new Promise((resolve, reject) => {
99
- if (!WsClient || WsClient === null) {
100
- reject(new Error('missing WebSocket client'))
98
+ if (WsClient === null) {
99
+ return reject(new Error('missing WebSocket client'))
101
100
  }
102
101
  this.instance = new WsClient(this.url)
103
102
  this.instance.binaryType = 'arraybuffer'
@@ -1,35 +1,31 @@
1
+ // @ts-check
1
2
  import { WsConnectionData } from '../utils/connectionData/WsConnectionData.js'
2
3
  import { TcpConnectionData } from '../utils/nodejs/connectionData/TcpConnectionData.js'
3
4
  import { RuntimeName } from '../utils/RuntimeName.js'
4
5
  import { RuntimeNameHandler } from '../utils/RuntimeNameHandler.js'
5
6
  import { RuntimeContext } from './RuntimeContext.js'
6
- import { ConfigSourceResolver } from './tools/browser/ConfigSourceResolver.js'
7
- import { isNodejsRuntime, isBrowserRuntime, getRequire } from '../utils/Runtime.js'
8
- import { JsonFileResolver } from './tools/nodejs/JsonFileResolver.js'
7
+ import { isNodejsRuntime, isBrowserRuntime, getRequire, getDirname } from '../utils/Runtime.js'
9
8
  import { InMemoryConnectionData } from '../utils/connectionData/InMemoryConnectionData.js'
9
+ import { JsonResolver } from './tools/JsonResolver.js'
10
+ import { UtilsConst } from '../utils/UtilsConst.js'
10
11
 
11
12
  const requireDynamic = getRequire(import.meta.url)
13
+
14
+ /**
15
+ * @typedef {import('../types.d.ts').ConfigSource} ConfigSource
16
+ * @typedef {import('../types.d.ts').RuntimeName} RuntimeNameType
17
+ */
12
18
  /**
13
19
  * The ConfigRuntimeFactory class provides methods for creating runtime contexts.
14
20
  * Each method corresponds to a specific runtime (CLR, JVM, .NET Core, Perl, Ruby, Node.js, Python) and returns a RuntimeContext instance for that runtime.
15
21
  * @see [Javonet Guides](https://www.javonet.com/guides/v2/javascript/foundations/runtime-context)
16
22
  */
17
23
  export class ConfigRuntimeFactory {
18
- /** @type {import('../core/transmitter/Transmitter.js') | null} */
19
- transmitter = null
20
-
21
24
  /**
22
- * @param {object | string} configSource
23
- * @param {import('../core/transmitter/Transmitter.js') | null} transmitter
25
+ * @param {ConfigSource | string} configSource
24
26
  */
25
- constructor(configSource, transmitter = null) {
27
+ constructor(configSource) {
26
28
  this.configSource = configSource
27
- this.transmitter = transmitter
28
-
29
- if (isNodejsRuntime()) {
30
- // @ts-ignore
31
- this.transmitter?.setConfigSource(configSource)
32
- }
33
29
  }
34
30
 
35
31
  /**
@@ -113,93 +109,99 @@ export class ConfigRuntimeFactory {
113
109
  }
114
110
 
115
111
  /**
116
- * @param {number} runtime
112
+ * @param {RuntimeNameType} runtime
117
113
  * @param {string} configName
118
- * @returns
114
+ * @returns {RuntimeContext}
119
115
  */
120
116
  #getRuntimeContext(runtime, configName = 'default') {
117
+ const jsonResolver = new JsonResolver(this.configSource)
118
+
119
+ UtilsConst.setLicenseKey(jsonResolver.getLicenseKey())
120
+ if (jsonResolver.getWorkingDirectory()) {
121
+ UtilsConst.setJavonetWorkingDirectory(jsonResolver.getWorkingDirectory())
122
+ }
123
+
121
124
  if (isBrowserRuntime()) {
122
- const jfr = new ConfigSourceResolver(this.configSource)
123
- const connType = jfr.getChannelType(RuntimeNameHandler.getName(runtime), configName)
124
- let connData = null
125
+ const channelType = jsonResolver.getChannelType(RuntimeNameHandler.getName(runtime), configName)
126
+ let connectionData = null
125
127
 
126
- if (connType === 'webSocket') {
127
- connData = new WsConnectionData(
128
- jfr.getChannelHost(RuntimeNameHandler.getName(runtime), configName)
128
+ if (channelType === 'webSocket') {
129
+ connectionData = new WsConnectionData(
130
+ jsonResolver.getChannelHost(RuntimeNameHandler.getName(runtime), configName)
129
131
  )
130
132
  } else {
131
- throw new Error('Unsupported connection type: ' + connType)
133
+ throw new Error('Unsupported connection type: ' + channelType)
132
134
  }
133
135
 
134
- const rtmCtx = RuntimeContext.getInstance(runtime, connData)
135
- this.#loadModules(runtime, configName, jfr, rtmCtx)
136
+ const rtmCtx = RuntimeContext.getInstance(runtime, connectionData)
137
+ this.#loadModules(runtime, configName, jsonResolver, rtmCtx)
136
138
  return rtmCtx
137
139
  }
138
140
 
139
141
  if (isNodejsRuntime()) {
140
- let jfr = new JsonFileResolver(this.configSource)
141
-
142
142
  try {
143
- const licenseKey = jfr.getLicenseKey()
144
- this.transmitter?.activate(licenseKey)
143
+ const channelType = jsonResolver.getChannelType(
144
+ RuntimeNameHandler.getName(runtime),
145
+ configName
146
+ )
147
+ const licenseKey = jsonResolver.getLicenseKey()
148
+
149
+ let connectionData = null
150
+ if (channelType === 'inMemory') {
151
+ connectionData = new InMemoryConnectionData()
152
+ } else if (channelType === 'tcp') {
153
+ connectionData = new TcpConnectionData(
154
+ jsonResolver.getChannelHost(RuntimeNameHandler.getName(runtime), configName),
155
+ /** @type {number} */ (jsonResolver.getChannelPort(RuntimeNameHandler.getName(runtime), configName))
156
+ )
157
+ } else if (channelType === 'webSocket') {
158
+ connectionData = new WsConnectionData(
159
+ jsonResolver.getChannelHost(RuntimeNameHandler.getName(runtime), configName)
160
+ )
161
+ } else {
162
+ throw new Error('Unsupported connection type: ' + channelType)
163
+ }
164
+
165
+ const rtmCtx = RuntimeContext.getInstance(runtime, connectionData)
166
+ this.#loadModules(runtime, configName, jsonResolver, rtmCtx)
167
+ return rtmCtx
145
168
  } catch (error) {
146
169
  throw error
147
170
  }
148
-
149
- let rtmCtx = null
150
- let connData = null
151
- let connType = jfr.getChannelType(RuntimeNameHandler.getName(runtime), configName)
152
-
153
- if (connType === 'inMemory') {
154
- connData = new InMemoryConnectionData()
155
- } else if (connType === 'tcp') {
156
- connData = new TcpConnectionData(
157
- jfr.getChannelHost(RuntimeNameHandler.getName(runtime), configName),
158
- jfr.getChannelPort(RuntimeNameHandler.getName(runtime), configName)
159
- )
160
- } else if (connType === 'webSocket') {
161
- connData = new WsConnectionData(
162
- jfr.getChannelHost(RuntimeNameHandler.getName(runtime), configName)
163
- )
164
- } else {
165
- throw new Error('Unsupported connection type: ' + connType)
166
- }
167
-
168
- rtmCtx = RuntimeContext.getInstance(runtime, connData)
169
- this.#loadModules(runtime, configName, jfr, rtmCtx)
170
- return rtmCtx
171
171
  }
172
+
173
+ throw new Error('Unsupported runtime environment')
172
174
  }
173
175
 
174
- // @ts-ignore
175
- #loadModules(runtime, configName, jfr, rtmCtx) {
176
+ /**
177
+ * @param {number} runtime
178
+ * @param {string} configName
179
+ * @param {JsonResolver} jsonResolver
180
+ * @param {RuntimeContext} rtmCtx
181
+ */
182
+ #loadModules(runtime, configName, jsonResolver, rtmCtx) {
176
183
  try {
177
- const modules = jfr
184
+ const modules = jsonResolver
178
185
  .getModules(RuntimeNameHandler.getName(runtime), configName)
179
186
  .split(',')
180
- // @ts-ignore
181
187
  .filter((module) => module.trim() !== '')
182
188
 
183
189
  if (isNodejsRuntime()) {
184
- if (typeof this.configSource !== 'string') {
185
- throw new Error('configSource is not a string')
186
- }
187
-
188
- const path = requireDynamic('path')
189
- const configDirectoryAbsolutePath = path?.dirname(this.configSource)
190
- // @ts-ignore
190
+ const path = /** @type {import('path').PlatformPath} */ (
191
+ /** @type {unknown} */ (requireDynamic('path'))
192
+ )
193
+ // TODO: make sure that relative path works
194
+ const configDirectoryAbsolutePath = process.cwd()
191
195
  modules.forEach((module) => {
192
- if (path?.isAbsolute(module)) {
196
+ if (path.isAbsolute(module)) {
193
197
  rtmCtx.loadLibrary(module)
194
198
  } else {
195
- rtmCtx.loadLibrary(path?.join(configDirectoryAbsolutePath, module))
199
+ rtmCtx.loadLibrary(path.join(configDirectoryAbsolutePath, module))
196
200
  }
197
201
  })
198
202
  }
199
- // eslint-disable-next-line no-unused-vars
200
203
  } catch (error) {
201
204
  throw error
202
- // most likely browser will never support this
203
205
  }
204
206
  }
205
207
  }
@@ -1,3 +1,4 @@
1
+ //@ts-check
1
2
  import { delegatesCacheInstance } from '../core/delegatesCache/DelegatesCache.js'
2
3
  import { Interpreter } from '../core/interpreter/Interpreter.js'
3
4
  import { Command } from '../utils/Command.js'
@@ -5,6 +6,12 @@ import { CommandType } from '../utils/CommandType.js'
5
6
  import { ConnectionType } from '../utils/ConnectionType.js'
6
7
  import { ExceptionThrower } from '../utils/exception/ExceptionThrower.js'
7
8
  import { RuntimeName } from '../utils/RuntimeName.js'
9
+ import { TypesHandler } from '../utils/TypesHandler.js'
10
+
11
+ /**
12
+ * @typedef {import('../types.d.ts').RuntimeName} RuntimeNameType
13
+ * @typedef {import('../utils/connectionData/IConnectionData.js').IConnectionData} IConnectionData
14
+ */
8
15
 
9
16
  /**
10
17
  * InvocationContext is a class that represents a context for invoking commands.
@@ -14,15 +21,28 @@ import { RuntimeName } from '../utils/RuntimeName.js'
14
21
  * @class
15
22
  */
16
23
  class InvocationContext {
24
+ /** @type {RuntimeNameType} */
17
25
  #runtimeName
26
+ /** @type {IConnectionData} */
18
27
  #connectionData
19
- #currentCommand
20
- #responseCommand
21
- #interpreter
22
- // eslint-disable-next-line no-unused-private-class-members
23
- #isExecuted
24
- #resultValue
28
+ /** @type {Command | null} */
29
+ #currentCommand = null
30
+ /** @type {Command | null} */
31
+ #responseCommand = null
32
+ /** @type {boolean} */
33
+ #isExecuted = false
34
+ /** @type {any | null} */
35
+ #resultValue = null
36
+ /** @type {Interpreter | null} */
37
+ #interpreter = null
25
38
 
39
+ /**
40
+ *
41
+ * @param {RuntimeNameType} runtimeName
42
+ * @param {IConnectionData} connectionData
43
+ * @param {Command} command
44
+ * @param {boolean} isExecuted
45
+ */
26
46
  constructor(runtimeName, connectionData, command, isExecuted = false) {
27
47
  this.#runtimeName = runtimeName
28
48
  this.#connectionData = connectionData
@@ -33,8 +53,13 @@ class InvocationContext {
33
53
  this.#resultValue = null
34
54
  }
35
55
 
56
+ /**
57
+ * @param {Command} localCommand
58
+ * @returns {InvocationContext}
59
+ */
36
60
  #createInstanceContext(localCommand) {
37
61
  if (this.#connectionData.connectionType === ConnectionType.WEB_SOCKET) {
62
+ // @ts-expect-error
38
63
  return new InvocationWsContext(
39
64
  this.#runtimeName,
40
65
  this.#connectionData,
@@ -48,6 +73,9 @@ class InvocationContext {
48
73
  )
49
74
  }
50
75
 
76
+ /**
77
+ * @returns {Command|null}
78
+ */
51
79
  get_current_command() {
52
80
  return this.#currentCommand
53
81
  }
@@ -63,12 +91,12 @@ class InvocationContext {
63
91
  // }
64
92
  //}
65
93
 
66
- [Symbol.iterator] = function () {
67
- if (this.#currentCommand.commandType !== CommandType.Reference) {
94
+ [Symbol.iterator] = () => {
95
+ if (this.#currentCommand?.commandType !== CommandType.Reference) {
68
96
  throw new Error('Object is not iterable')
69
97
  }
70
98
  let position = -1
71
- let arraySize = this.getSize().execute().getValue()
99
+ const arraySize = Number(this.getSize().execute().getValue())
72
100
 
73
101
  return {
74
102
  next: () => ({
@@ -90,21 +118,25 @@ class InvocationContext {
90
118
  * @method
91
119
  */
92
120
  execute() {
121
+ if (this.#currentCommand === null) {
122
+ throw new Error('currentCommand is undefined in Invocation Context execute method')
123
+ }
124
+
93
125
  this.#interpreter = new Interpreter()
126
+ //@ts-expect-error
94
127
  this.#responseCommand = this.#interpreter.execute(this.#currentCommand, this.#connectionData)
95
- if (this.#responseCommand === undefined) {
128
+
129
+ if (!this.#responseCommand) {
96
130
  throw new Error('responseCommand is undefined in Invocation Context execute method')
97
131
  }
98
- if (this.#responseCommand.commandType === CommandType.Exception) {
132
+ if (this.#responseCommand?.commandType === CommandType.Exception) {
99
133
  throw ExceptionThrower.throwException(this.#responseCommand)
100
134
  }
101
-
102
- if (this.#responseCommand.commandType === CommandType.CreateClassInstance) {
135
+ if (this.#responseCommand?.commandType === CommandType.CreateClassInstance) {
103
136
  this.#currentCommand = this.#responseCommand
104
137
  this.#isExecuted = true
105
138
  return this
106
139
  }
107
-
108
140
  return new InvocationContext(this.#runtimeName, this.#connectionData, this.#responseCommand, true)
109
141
  }
110
142
 
@@ -347,7 +379,7 @@ class InvocationContext {
347
379
  * Creates a null object of a specific type on the target runtime.
348
380
  * @param {string} methodName - The name of the method to invoke.
349
381
  * @param {...any} args - Method arguments.
350
- * @returns {InvocationContext} An InvocationContext instance with the command to create a null object.
382
+ * @returns {InvocationContext|InvocationWsContext} An InvocationContext instance with the command to create a null object.
351
383
  * TODO: connect documentation page url
352
384
  * @see [Javonet Guides](https://www.javonet.com/guides/)
353
385
  * @method
@@ -374,6 +406,10 @@ class InvocationContext {
374
406
  this.#buildCommand(localCommand)
375
407
  )
376
408
  localInvCtx.execute()
409
+
410
+ if (localInvCtx.#responseCommand === null) {
411
+ throw new Error('responseCommand is undefined in Invocation Context execute method')
412
+ }
377
413
  return localInvCtx.#responseCommand.payload
378
414
  }
379
415
 
@@ -385,19 +421,30 @@ class InvocationContext {
385
421
  * @method
386
422
  */
387
423
 
424
+ /**
425
+ * @returns {unknown}
426
+ */
388
427
  getValue() {
389
- this.#resultValue = this.#currentCommand.payload[0]
428
+ this.#resultValue = this.#currentCommand?.payload[0]
390
429
  return this.#resultValue
391
430
  }
392
431
 
393
- #buildCommand = function (command) {
432
+ /**
433
+ * @param {Command} command
434
+ * @returns {Command}
435
+ */
436
+ #buildCommand(command) {
394
437
  for (let i = 0; i < command.payload.length; i++) {
395
438
  command.payload[i] = this.#encapsulatePayloadItem(command.payload[i])
396
439
  }
397
440
  return command.prependArgToPayload(this.#currentCommand)
398
441
  }
399
442
 
400
- #encapsulatePayloadItem = function (payloadItem) {
443
+ /**
444
+ * @param {unknown} payloadItem
445
+ * @returns {Command|null}
446
+ */
447
+ #encapsulatePayloadItem(payloadItem) {
401
448
  // eslint-disable-next-line valid-typeof
402
449
  if (payloadItem instanceof Command) {
403
450
  for (let i = 0; i < payloadItem.payload.length; i++) {
@@ -409,7 +456,7 @@ class InvocationContext {
409
456
  return payloadItem.get_current_command()
410
457
  } else if (payloadItem instanceof Array) {
411
458
  const copiedArray = payloadItem.map((item) => this.#encapsulatePayloadItem(item))
412
- return new Command(this.runtimeName, CommandType.Array, copiedArray)
459
+ return new Command(this.#runtimeName, CommandType.Array, copiedArray)
413
460
  } else if (typeof payloadItem === 'function') {
414
461
  let newArray = new Array(payloadItem.length + 1)
415
462
  for (let i = 0; i < newArray.length; i++) {
@@ -419,23 +466,41 @@ class InvocationContext {
419
466
  ...newArray
420
467
  )
421
468
  return new Command(this.#runtimeName, CommandType.PassDelegate, args)
469
+ } else if (TypesHandler.isPrimitiveOrNullOrUndefined(payloadItem)) {
470
+ return new Command(this.#runtimeName, CommandType.Value, [payloadItem])
422
471
  } else {
423
- return new Command(this.runtimeName, CommandType.Value, [payloadItem])
472
+ throw Error(
473
+ 'Unsupported payload item type: ' +
474
+ (payloadItem?.constructor?.name || typeof payloadItem) +
475
+ ' for payload item: ' +
476
+ payloadItem
477
+ )
424
478
  }
425
479
  }
426
480
  }
427
481
 
428
482
  class InvocationWsContext extends InvocationContext {
483
+ /** @type {RuntimeNameType} */
429
484
  #runtimeName
485
+ /** @type {IConnectionData} */
430
486
  #connectionData
487
+ /** @type {Command | null} */
431
488
  #currentCommand
489
+ /** @type {Command | null} */
432
490
  #responseCommand
491
+ /** @type {Interpreter | null} */
433
492
  #interpreter
434
- // eslint-disable-next-line no-unused-private-class-members
493
+ /** @type {boolean} */
435
494
  #isExecuted
436
- // eslint-disable-next-line no-unused-private-class-members
495
+ /** @type {any | null} */
437
496
  #resultValue
438
497
 
498
+ /**
499
+ * @param {RuntimeNameType} runtimeName
500
+ * @param {IConnectionData} connectionData
501
+ * @param {Command} command
502
+ * @param {boolean} isExecuted
503
+ */
439
504
  constructor(runtimeName, connectionData, command, isExecuted = false) {
440
505
  super(runtimeName, connectionData, command, isExecuted)
441
506
 
@@ -455,11 +520,17 @@ class InvocationWsContext extends InvocationContext {
455
520
  * Commands are becoming nested through each invocation of methods on Invocation Context.
456
521
  * Each invocation triggers the creation of new Invocation Context instance wrapping the current command with new parent command valid for invoked method.
457
522
  * 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.
458
- * @returns {InvocationContext} the InvocationContext after executing the command.
523
+ * @returns {Promise<InvocationWsContext>} the InvocationContext after executing the command.
459
524
  * @see [Javonet Guides](https://www.javonet.com/guides/v2/javascript/foundations/execute-method)
525
+ * @async
460
526
  * @method
461
527
  */
528
+ // @ts-expect-error
462
529
  async execute() {
530
+ if (this.#currentCommand === null) {
531
+ throw new Error('currentCommand is undefined in Invocation Context execute method')
532
+ }
533
+
463
534
  this.#interpreter = new Interpreter()
464
535
  this.#responseCommand = await this.#interpreter.executeAsync(
465
536
  this.#currentCommand,
@@ -469,17 +540,14 @@ class InvocationWsContext extends InvocationContext {
469
540
  if (this.#responseCommand === undefined) {
470
541
  throw new Error('responseCommand is undefined in Invocation Context execute method')
471
542
  }
472
-
473
543
  if (this.#responseCommand.commandType === CommandType.Exception) {
474
544
  throw ExceptionThrower.throwException(this.#responseCommand)
475
545
  }
476
-
477
546
  if (this.#responseCommand.commandType === CommandType.CreateClassInstance) {
478
547
  this.#currentCommand = this.#responseCommand
479
548
  this.#isExecuted = true
480
549
  return this
481
550
  }
482
-
483
551
  return new InvocationWsContext(this.#runtimeName, this.#connectionData, this.#responseCommand, true)
484
552
  }
485
553
  }