javonet-nodejs-sdk 2.5.17 → 2.5.19

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 +89 -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 +113 -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,3 +1,4 @@
1
+ //@ts-check
1
2
  import { ConfigRuntimeFactory } from './ConfigRuntimeFactory.js'
2
3
  import { RuntimeFactory } from './RuntimeFactory.js'
3
4
  import { InMemoryConnectionData } from '../utils/connectionData/InMemoryConnectionData.js'
@@ -7,10 +8,14 @@ import { CommandDeserializer } from '../core/protocol/CommandDeserializer.js'
7
8
  import { RuntimeLogger } from '../utils/RuntimeLogger.js'
8
9
  import { TcpConnectionData } from '../utils/nodejs/connectionData/TcpConnectionData.js'
9
10
  import { WsConnectionData } from '../utils/connectionData/WsConnectionData.js'
11
+ import { ActivationHelper } from './tools/ActivationHelper.js'
12
+ import { UtilsConst } from '../utils/UtilsConst.js'
13
+
14
+ /** @typedef {import('../types.d.ts').ConfigSource} ConfigSource */
10
15
 
11
16
  const requireDynamic = getRequire(import.meta.url)
12
17
 
13
- /** @type {import('../core/transmitter/Transmitter') | null} */
18
+ /** @type {typeof import('../core/transmitter/Transmitter.js').Transmitter | null} */
14
19
  let _Transmitter = null
15
20
 
16
21
  if (isNodejsRuntime()) {
@@ -64,28 +69,28 @@ class Javonet {
64
69
  /**
65
70
  * Initializes Javonet with a custom configuration file taken from external source.
66
71
  * Currentyl supported: Configuration file in JSON format
67
- * @param config - Path to a configuration file.
72
+ * @param {string|ConfigSource} config - Path to a configuration file.
68
73
  * @returns {ConfigRuntimeFactory} A ConfigRuntimeFactory instance with configuration data.
69
74
  * @see [Javonet Guides](https://www.javonet.com/guides/v2/javascript/foundations/configure-channel)
70
75
  */
71
76
  static withConfig(config) {
72
- return new ConfigRuntimeFactory(config, _Transmitter)
77
+ return new ConfigRuntimeFactory(config)
73
78
  }
74
79
 
75
80
  /**
76
81
  * Activates Javonet with the provided license key.
77
82
  * @param {string} licenseKey - The license key to activate Javonet.
78
- * @returns {number} The activation status code.
83
+ * @returns {void} The activation status code.
79
84
  * @see [Javonet Guides](https://www.javonet.com/guides/v2/javascript/getting-started/activating-javonet)
80
85
  */
81
86
  static activate(licenseKey) {
82
- if (isNodejsRuntime()) {
83
- return _Transmitter?.activate(licenseKey)
84
- } else {
85
- throw new Error('Javonet.activate is allowed only to run in nodejs runtime')
86
- }
87
+ UtilsConst.setLicenseKey(licenseKey)
87
88
  }
88
89
 
90
+ /**
91
+ * Gets the runtime information.
92
+ * @returns {string} The runtime information.
93
+ */
89
94
  static getRuntimeInfo() {
90
95
  if (isNodejsRuntime()) {
91
96
  return RuntimeLogger.getRuntimeInfo()
@@ -96,43 +101,20 @@ class Javonet {
96
101
 
97
102
  /**
98
103
  * Sets the configuration source for the Javonet SDK.
99
- *
100
104
  * @param {string} configSource - The configuration source.
105
+ * @returns {void}
101
106
  */
102
107
  static setConfigSource(configSource) {
103
- if (isNodejsRuntime()) {
104
- return _Transmitter?.setConfigSource(configSource)
105
- } else {
106
- throw new Error('Javonet.setConfigSource is allowed only to run in nodejs runtime')
107
- }
108
+ UtilsConst.setConfigSource(configSource)
108
109
  }
109
110
 
110
111
  /**
111
112
  * Sets the working directory for the Javonet SDK.
112
- *
113
113
  * @param {string} path - The working directory.
114
+ * @returns {void}
114
115
  */
115
116
  static setJavonetWorkingDirectory(path) {
116
- try {
117
- if (isNodejsRuntime()) {
118
- const { mkdirSync } = requireDynamic('fs')
119
-
120
- path = path.replace(/\\/g, '/')
121
- if (!path.endsWith('/')) {
122
- path += '/'
123
- }
124
-
125
- mkdirSync(path, { recursive: true, mode: 0o700 })
126
- _Transmitter?.setJavonetWorkingDirectory(path)
127
- } else {
128
- throw new Error('Javonet.setJavonetWorkingDirectory is allowed only to run in nodejs runtime')
129
- }
130
- } catch (error) {
131
- if (error.code === 'MODULE_NOT_FOUND') {
132
- throw new Error('fs module not found. Please install it using npm install fs')
133
- }
134
- throw error
135
- }
117
+ UtilsConst.setJavonetWorkingDirectory(path)
136
118
  }
137
119
  }
138
120
 
@@ -1,4 +1,4 @@
1
- /* eslint-disable valid-typeof */
1
+ // @ts-check
2
2
  import { Command } from '../utils/Command.js'
3
3
  import { CommandType } from '../utils/CommandType.js'
4
4
  import { InvocationContext, InvocationWsContext } from './InvocationContext.js'
@@ -7,6 +7,20 @@ import { ExceptionThrower } from '../utils/exception/ExceptionThrower.js'
7
7
  import { RuntimeName } from '../utils/RuntimeName.js'
8
8
  import { Interpreter } from '../core/interpreter/Interpreter.js'
9
9
  import { delegatesCacheInstance } from '../core/delegatesCache/DelegatesCache.js'
10
+ import { TypesHandler } from '../utils/TypesHandler.js'
11
+ import { UtilsConst } from '../utils/UtilsConst.js'
12
+ import { getRequire } from '../utils/Runtime.js'
13
+
14
+ /**
15
+ * @typedef {import('../types.d.ts').RuntimeName} RuntimeNameType
16
+ * @typedef {import('../utils/connectionData/IConnectionData.js').IConnectionData} IConnectionData
17
+ * @typedef {typeof import('../core/transmitter/Transmitter.js').Transmitter} Transmitter
18
+ */
19
+
20
+ /** @type {typeof import('../core/transmitter/Transmitter.js').Transmitter | null} */
21
+ let _Transmitter = null
22
+
23
+ const requireDynamic = getRequire(import.meta.url)
10
24
 
11
25
  /**
12
26
  * Represents a single context which allows interaction with a selected technology.
@@ -20,54 +34,97 @@ import { delegatesCacheInstance } from '../core/delegatesCache/DelegatesCache.js
20
34
  * @class
21
35
  */
22
36
  class RuntimeContext {
37
+ /** @type {Map<string, RuntimeContext>} */
23
38
  static memoryRuntimeContexts = new Map()
39
+ /** @type {Map<string, RuntimeContext>} */
24
40
  static networkRuntimeContexts = new Map()
41
+ /** @type {Map<string, RuntimeContext>} */
25
42
  static webSocketRuntimeContexts = new Map()
26
- #currentCommand
27
- #responseCommand
28
- #interpreter
29
43
 
44
+ /** @type {Command | null} */
45
+ #currentCommand = null
46
+ /** @type {Command | null} */
47
+ #responseCommand = null
48
+ /** @type {Interpreter | null} */
49
+ #interpreter = null
50
+
51
+ /**
52
+ * @param {RuntimeNameType} runtimeName
53
+ * @param {IConnectionData} connectionData
54
+ */
30
55
  constructor(runtimeName, connectionData) {
56
+ this._isExecuted = false
31
57
  this.runtimeName = runtimeName
32
58
  this.connectionData = connectionData
33
- this.#currentCommand = null
34
- this.#responseCommand = null
35
59
  this.#interpreter = new Interpreter()
60
+
61
+ if (this.connectionData.connectionType === ConnectionType.WEB_SOCKET) {
62
+ return
63
+ }
64
+ if (
65
+ this.runtimeName === RuntimeName.Nodejs &&
66
+ this.connectionData.connectionType === ConnectionType.IN_MEMORY
67
+ ) {
68
+ return
69
+ }
70
+
71
+ _Transmitter?.setJavonetWorkingDirectory(UtilsConst.getJavonetWorkingDirectory())
72
+ if (UtilsConst.getConfigSource() !== '') {
73
+ _Transmitter?.setConfigSource(UtilsConst.getConfigSource())
74
+ }
75
+ _Transmitter?.activate(UtilsConst.getLicenseKey())
36
76
  }
37
77
 
78
+ /**
79
+ * @param {RuntimeNameType} runtimeName
80
+ * @param {IConnectionData} connectionData
81
+ * @returns {RuntimeContext}
82
+ */
38
83
  static getInstance(runtimeName, connectionData) {
39
84
  switch (connectionData.connectionType) {
40
85
  case ConnectionType.IN_MEMORY:
41
- if (runtimeName in RuntimeContext.memoryRuntimeContexts) {
42
- let runtimeCtx = RuntimeContext.memoryRuntimeContexts[runtimeName]
43
- runtimeCtx.currentCommand = null
86
+ const key = String(runtimeName)
87
+ if (RuntimeContext.memoryRuntimeContexts.has(key)) {
88
+ const runtimeCtx = RuntimeContext.memoryRuntimeContexts.get(key)
89
+ if (!runtimeCtx) throw new Error('Runtime context not found')
90
+ runtimeCtx.#currentCommand = null
44
91
  return runtimeCtx
45
92
  } else {
46
- let runtimeCtx = new RuntimeContext(runtimeName, connectionData)
47
- RuntimeContext.memoryRuntimeContexts[runtimeName] = runtimeCtx
93
+ if (!_Transmitter) {
94
+ const { Transmitter } = requireDynamic('../core/transmitter/Transmitter.js')
95
+ _Transmitter = Transmitter
96
+ }
97
+ const runtimeCtx = new RuntimeContext(runtimeName, connectionData)
98
+ RuntimeContext.memoryRuntimeContexts.set(key, runtimeCtx)
48
99
  return runtimeCtx
49
100
  }
50
101
  case ConnectionType.TCP: {
51
- let key1 = runtimeName + JSON.stringify(connectionData)
52
- if (key1 in RuntimeContext.networkRuntimeContexts) {
53
- let runtimeCtx = RuntimeContext.networkRuntimeContexts[key1]
54
- runtimeCtx.currentCommand = null
102
+ const key1 = String(runtimeName) + JSON.stringify(connectionData)
103
+ if (RuntimeContext.networkRuntimeContexts.has(key1)) {
104
+ const runtimeCtx = RuntimeContext.networkRuntimeContexts.get(key1)
105
+ if (!runtimeCtx) throw new Error('Runtime context not found')
106
+ runtimeCtx.#currentCommand = null
55
107
  return runtimeCtx
56
108
  } else {
57
- let runtimeCtx = new RuntimeContext(runtimeName, connectionData)
58
- RuntimeContext.networkRuntimeContexts[key1] = runtimeCtx
109
+ if (!_Transmitter) {
110
+ const { Transmitter } = requireDynamic('../core/transmitter/Transmitter.js')
111
+ _Transmitter = Transmitter
112
+ }
113
+ const runtimeCtx = new RuntimeContext(runtimeName, connectionData)
114
+ RuntimeContext.networkRuntimeContexts.set(key1, runtimeCtx)
59
115
  return runtimeCtx
60
116
  }
61
117
  }
62
118
  case ConnectionType.WEB_SOCKET: {
63
- let key2 = runtimeName + JSON.stringify(connectionData)
64
- if (key2 in RuntimeContext.webSocketRuntimeContexts) {
65
- let runtimeCtx = RuntimeContext.webSocketRuntimeContexts[key2]
66
- runtimeCtx.currentCommand = null
119
+ const key2 = String(runtimeName) + JSON.stringify(connectionData)
120
+ if (RuntimeContext.webSocketRuntimeContexts.has(key2)) {
121
+ const runtimeCtx = RuntimeContext.webSocketRuntimeContexts.get(key2)
122
+ if (!runtimeCtx) throw new Error('Runtime context not found')
123
+ runtimeCtx.#currentCommand = null
67
124
  return runtimeCtx
68
125
  } else {
69
- let runtimeCtx = new RuntimeContext(runtimeName, connectionData)
70
- RuntimeContext.webSocketRuntimeContexts[key2] = runtimeCtx
126
+ const runtimeCtx = new RuntimeContext(runtimeName, connectionData)
127
+ RuntimeContext.webSocketRuntimeContexts.set(key2, runtimeCtx)
71
128
  return runtimeCtx
72
129
  }
73
130
  }
@@ -86,12 +143,13 @@ class RuntimeContext {
86
143
  * @method
87
144
  */
88
145
  execute() {
89
- this.#responseCommand = this.#interpreter.execute(this.#currentCommand, this.connectionData)
146
+ // @ts-expect-error
147
+ this.#responseCommand = this.#interpreter?.execute(this.#currentCommand, this.connectionData)
90
148
  this.#currentCommand = null
91
149
  if (this.#responseCommand === undefined) {
92
150
  throw new Error('responseCommand is undefined in Runtime Context execute method')
93
151
  }
94
- if (this.#responseCommand.commandType === CommandType.Exception) {
152
+ if (this.#responseCommand?.commandType === CommandType.Exception) {
95
153
  throw ExceptionThrower.throwException(this.#responseCommand)
96
154
  }
97
155
  }
@@ -124,6 +182,7 @@ class RuntimeContext {
124
182
  let localCommand = new Command(this.runtimeName, CommandType.GetType, [typeName, ...args])
125
183
  this.#currentCommand = null
126
184
  if (this.connectionData.connectionType === ConnectionType.WEB_SOCKET) {
185
+ // @ts-expect-error
127
186
  return new InvocationWsContext(
128
187
  this.runtimeName,
129
188
  this.connectionData,
@@ -146,6 +205,7 @@ class RuntimeContext {
146
205
  let localCommand = new Command(this.runtimeName, CommandType.Cast, args)
147
206
  this.#currentCommand = null
148
207
  if (this.connectionData.connectionType === ConnectionType.WEB_SOCKET) {
208
+ // @ts-expect-error
149
209
  return new InvocationWsContext(
150
210
  this.runtimeName,
151
211
  this.connectionData,
@@ -168,6 +228,7 @@ class RuntimeContext {
168
228
  let localCommand = new Command(this.runtimeName, CommandType.GetEnumItem, args)
169
229
  this.#currentCommand = null
170
230
  if (this.connectionData.connectionType === ConnectionType.WEB_SOCKET) {
231
+ // @ts-expect-error
171
232
  return new InvocationWsContext(
172
233
  this.runtimeName,
173
234
  this.connectionData,
@@ -190,6 +251,7 @@ class RuntimeContext {
190
251
  let localCommand = new Command(this.runtimeName, CommandType.AsRef, args)
191
252
  this.#currentCommand = null
192
253
  if (this.connectionData.connectionType === ConnectionType.WEB_SOCKET) {
254
+ // @ts-expect-error
193
255
  return new InvocationWsContext(
194
256
  this.runtimeName,
195
257
  this.connectionData,
@@ -212,6 +274,7 @@ class RuntimeContext {
212
274
  let localCommand = new Command(this.runtimeName, CommandType.AsOut, args)
213
275
  this.#currentCommand = null
214
276
  if (this.connectionData.connectionType === ConnectionType.WEB_SOCKET) {
277
+ // @ts-expect-error
215
278
  return new InvocationWsContext(
216
279
  this.runtimeName,
217
280
  this.connectionData,
@@ -238,6 +301,7 @@ class RuntimeContext {
238
301
  ])
239
302
  this.#currentCommand = null
240
303
  if (this.connectionData.connectionType === ConnectionType.WEB_SOCKET) {
304
+ // @ts-expect-error
241
305
  return new InvocationWsContext(
242
306
  this.runtimeName,
243
307
  this.connectionData,
@@ -247,22 +311,35 @@ class RuntimeContext {
247
311
  return new InvocationContext(this.runtimeName, this.connectionData, this.#buildCommand(localCommand))
248
312
  }
249
313
 
250
- #buildCommand = function (command) {
314
+ /**
315
+ * @param {Command} command
316
+ * @returns {Command}
317
+ */
318
+ #buildCommand(command) {
251
319
  for (let i = 0; i < command.payload.length; i++) {
252
320
  command.payload[i] = this.#encapsulatePayloadItem(command.payload[i])
253
321
  }
254
322
  return command.prependArgToPayload(this.#currentCommand)
255
323
  }
256
324
 
257
- #encapsulatePayloadItem = function (payloadItem) {
325
+ /**
326
+ * @param {unknown} payloadItem
327
+ * @returns {Command}
328
+ */
329
+ #encapsulatePayloadItem(payloadItem) {
258
330
  if (payloadItem instanceof Command) {
259
331
  for (let i = 0; i < payloadItem.payload.length; i++) {
260
332
  payloadItem.payload[i] = this.#encapsulatePayloadItem(payloadItem.payload[i])
261
333
  }
262
334
  return payloadItem
263
335
  } else if (payloadItem instanceof InvocationContext) {
264
- return payloadItem.get_current_command()
336
+ const command = payloadItem?.get_current_command()
337
+ if (!command) {
338
+ throw new Error('Command not found')
339
+ }
340
+ return command
265
341
  } else if (payloadItem instanceof Array) {
342
+ /** @type {Command[]} */
266
343
  const copiedArray = payloadItem.map((item) => this.#encapsulatePayloadItem(item))
267
344
  return new Command(this.runtimeName, CommandType.Array, copiedArray)
268
345
  } else if (typeof payloadItem === 'function') {
@@ -274,8 +351,15 @@ class RuntimeContext {
274
351
  ...newArray
275
352
  )
276
353
  return new Command(this.runtimeName, CommandType.PassDelegate, args)
277
- } else {
354
+ } else if (TypesHandler.isPrimitiveOrNullOrUndefined(payloadItem)) {
278
355
  return new Command(this.runtimeName, CommandType.Value, [payloadItem])
356
+ } else {
357
+ throw Error(
358
+ 'Unsupported payload item type: ' +
359
+ (payloadItem?.constructor?.name || typeof payloadItem) +
360
+ ' for payload item: ' +
361
+ payloadItem
362
+ )
279
363
  }
280
364
  }
281
365
 
@@ -1,12 +1,19 @@
1
+ //@ts-check
1
2
  import { RuntimeName } from '../utils/RuntimeName.js'
2
3
  import { RuntimeContext } from './RuntimeContext.js'
3
4
 
5
+ /**
6
+ * @typedef {import('../utils/connectionData/IConnectionData.js').IConnectionData} IConnectionData
7
+ */
4
8
  /**
5
9
  * The RuntimeFactory class provides methods for creating runtime contexts.
6
10
  * Each method corresponds to a specific runtime (CLR, JVM, .NET Core, Perl, Ruby, Node.js, Python) and returns a RuntimeContext instance for that runtime.
7
11
  * @see [Javonet Guides](https://www.javonet.com/guides/v2/javascript/foundations/runtime-context)
8
12
  */
9
13
  export class RuntimeFactory {
14
+ /**
15
+ * @param {IConnectionData} connectionData
16
+ */
10
17
  constructor(connectionData) {
11
18
  this.connectionData = connectionData
12
19
  }
@@ -0,0 +1,51 @@
1
+ // @ts-check
2
+ import { isBrowserRuntime } from '../../utils/Runtime.js'
3
+
4
+ class ActivationHelper {
5
+ /** @type {string} */
6
+ static temporaryLicenseKey = 'License key not set'
7
+
8
+ /**
9
+ * @param {string} value
10
+ */
11
+ static setTemporaryLicenseKey(value) {
12
+ if (!value || value === 'your-license-key') {
13
+ return
14
+ }
15
+ // TODO: Uncomment this when we have a way to send messages to App Insights
16
+ // if (ActivationHelper.temporaryLicenseKey !== value) {
17
+ // SdkMessageHelper.sendExceptionToAppInsights('SdkMessage', 'Activation');
18
+ // }
19
+ ActivationHelper.temporaryLicenseKey = value
20
+ }
21
+
22
+ /**
23
+ * @returns {string}
24
+ */
25
+ static getTemporaryLicenseKey() {
26
+ return ActivationHelper.temporaryLicenseKey
27
+ }
28
+
29
+ /**
30
+ * @returns {Promise<string>}
31
+ */
32
+ static async getLicenseKey() {
33
+ try {
34
+ return ActivationHelper._getLicenseKeyFromFile()
35
+ } catch {
36
+ return ActivationHelper.temporaryLicenseKey
37
+ }
38
+ }
39
+
40
+ /**
41
+ * @returns {string}
42
+ */
43
+ static _getLicenseKeyFromFile() {
44
+ if (!isBrowserRuntime()) {
45
+ throw new Error('ActivationHelper is not supported in the browser runtime.')
46
+ }
47
+ return ""
48
+ }
49
+ }
50
+
51
+ export { ActivationHelper }
@@ -0,0 +1,226 @@
1
+ //@ts-check
2
+ import { isNodejsRuntime, getRequire } from '../../utils/Runtime.js'
3
+
4
+ /**
5
+ * @typedef {import('../../types.d.ts').RuntimeChannelType} RuntimeChannelType
6
+ * @typedef {import('../../types.d.ts').ConfigSource} ConfigSource
7
+ * @typedef {import('../../types.d.ts').RuntimeChannel} RuntimeChannel
8
+ * @typedef {import('../../types.d.ts').Runtime} Runtime
9
+ * @typedef {import('../../types.d.ts').Runtimes} Runtimes
10
+ * @typedef {import('../../types.d.ts').RuntimeType} RuntimeType
11
+ */
12
+
13
+ /**
14
+ * @param {any} param
15
+ * @returns {boolean}
16
+ */
17
+ function isValidObject(param) {
18
+ return typeof param === 'object' && param !== null && !Array.isArray(param)
19
+ }
20
+
21
+ /**
22
+ * @param {string | ConfigSource} param
23
+ * @returns {boolean}
24
+ */
25
+ function isValidJson(param) {
26
+ try {
27
+ if (typeof param === 'string') {
28
+ const parsed = JSON.parse(param)
29
+ return typeof parsed === 'object' && parsed !== null
30
+ }
31
+ return false
32
+ } catch (e) {
33
+ return false
34
+ }
35
+ }
36
+
37
+ const requireDynamic = getRequire(import.meta.url)
38
+
39
+ /** @type {any | null} */
40
+ let fs = null
41
+
42
+ class JsonResolver {
43
+ /** @type {ConfigSource} */
44
+ jsonObject
45
+ /** @type {string | ConfigSource} */
46
+ configSource
47
+
48
+ /**
49
+ * @param {string | ConfigSource} configSource
50
+ */
51
+ constructor(configSource) {
52
+ this.configSource = JSON.stringify(configSource)
53
+ this.jsonObject = this._getJsonObject(configSource)
54
+ }
55
+
56
+ /**
57
+ * @returns {Partial<Runtimes>}
58
+ */
59
+ get runtimes() {
60
+ return this.jsonObject?.runtimes || {}
61
+ }
62
+
63
+ /**
64
+ * @returns {string}
65
+ */
66
+ get licenseKey() {
67
+ return this.jsonObject?.licenseKey || 'your-license-key'
68
+ }
69
+
70
+ /**
71
+ * @returns {string}
72
+ */
73
+ get workingDirectory() {
74
+ return this.jsonObject?.workingDirectory || ''
75
+ }
76
+
77
+ /**
78
+ * @returns {string}
79
+ */
80
+ getLicenseKey() {
81
+ if (!this.licenseKey) {
82
+ throw new Error('License key not found in configuration source. Check your configuration source.')
83
+ }
84
+ return this.licenseKey
85
+ }
86
+
87
+ /**
88
+ * @returns {string}
89
+ */
90
+ getWorkingDirectory() {
91
+ return this.workingDirectory
92
+ }
93
+
94
+ /**
95
+ * @param {RuntimeType} runtimeName
96
+ * @param {string} configName
97
+ * @returns {RuntimeChannelType}
98
+ */
99
+ getChannelType(runtimeName, configName) {
100
+ const channel = this._getChannel(runtimeName, configName)
101
+ return channel.type
102
+ }
103
+
104
+ /**
105
+ * @param {RuntimeType} runtimeName
106
+ * @param {string} configName
107
+ * @returns {string}
108
+ */
109
+ getChannelHost(runtimeName, configName) {
110
+ const channel = this._getChannel(runtimeName, configName)
111
+ if (channel.host === undefined) {
112
+ throw new Error(`Host not found for channel in runtime ${runtimeName}, config ${configName}`)
113
+ }
114
+ return channel.host
115
+ }
116
+
117
+ /**
118
+ * @param {RuntimeType} runtimeName
119
+ * @param {string} configName
120
+ * @returns {number|string}
121
+ */
122
+ getChannelPort(runtimeName, configName) {
123
+ const channel = this._getChannel(runtimeName, configName)
124
+ if (channel.port === undefined) {
125
+ throw new Error(`Port not found for channel in runtime ${runtimeName}, config ${configName}`)
126
+ }
127
+ return channel.port
128
+ }
129
+
130
+ /**
131
+ * @param {RuntimeType} runtimeName
132
+ * @param {string} configName
133
+ * @returns {string}
134
+ */
135
+ getModules(runtimeName, configName) {
136
+ const runtime = this._getRuntime(runtimeName, configName)
137
+ return runtime.modules || ''
138
+ }
139
+
140
+ /**
141
+ * @private
142
+ * @param {string | ConfigSource} configSource
143
+ * @returns {ConfigSource}
144
+ */
145
+ _getJsonObject(configSource) {
146
+ if (isValidObject(configSource)) {
147
+ return /** @type {ConfigSource} */ (configSource)
148
+ }
149
+ if (typeof configSource === 'string' && isValidJson(configSource)) {
150
+ const parsedJson = JSON.parse(configSource)
151
+ if (isValidObject(parsedJson)) {
152
+ return parsedJson
153
+ }
154
+ }
155
+ if (isNodejsRuntime()) {
156
+ if (!fs) {
157
+ fs = requireDynamic('fs')
158
+ }
159
+ if (fs?.existsSync(configSource)) {
160
+ const jsonText = fs.readFileSync(configSource, 'utf8')
161
+ if (isValidJson(jsonText)) {
162
+ const parsedFromFile = JSON.parse(jsonText)
163
+ if (isValidObject(parsedFromFile)) {
164
+ return parsedFromFile
165
+ }
166
+ }
167
+ }
168
+ }
169
+
170
+ throw new Error(
171
+ `Configuration source is not valid. Check your configuration:${String(this.configSource)}`
172
+ )
173
+ }
174
+
175
+ /**
176
+ * @private
177
+ * @param {RuntimeType} runtimeName
178
+ * @param {string} configName
179
+ * @returns {Runtime}
180
+ */
181
+ _getRuntime(runtimeName, configName) {
182
+ const runtimeList = this.runtimes[runtimeName]
183
+
184
+ if (Array.isArray(runtimeList)) {
185
+ const runtime = runtimeList.find((item) => item.name === configName)
186
+ if (runtime) {
187
+ return runtime
188
+ }
189
+ } else if (typeof runtimeList?.name === 'string' && runtimeList?.name === configName) {
190
+ return runtimeList
191
+ }
192
+
193
+ throw new Error(
194
+ `Runtime config '${configName}' not found in configuration source for runtime '${runtimeName}'. Check your configuration source.`
195
+ )
196
+ }
197
+
198
+ /**
199
+ * @private
200
+ * @param {RuntimeType} runtimeName
201
+ * @param {string} configName
202
+ * @returns {string}
203
+ */
204
+ _getRuntimeName(runtimeName, configName) {
205
+ const runtime = this._getRuntime(runtimeName, configName)
206
+ return runtime.name
207
+ }
208
+
209
+ /**
210
+ * @private
211
+ * @param {RuntimeType} runtimeName
212
+ * @param {string} configName
213
+ * @returns {RuntimeChannel}
214
+ */
215
+ _getChannel(runtimeName, configName) {
216
+ const runtime = this._getRuntime(runtimeName, configName)
217
+ if (!runtime.channel) {
218
+ throw new Error(
219
+ `Channel not found for runtime config '${configName}' and runtime '${runtimeName}'. Check your configuration source.`
220
+ )
221
+ }
222
+ return runtime.channel
223
+ }
224
+ }
225
+
226
+ export { JsonResolver }