javonet-nodejs-sdk 2.5.20 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -107,8 +107,8 @@ class Handler {
107
107
  }
108
108
  let response = handlers[command.commandType].handleCommand(command);
109
109
  return this.parseCommand(response, command.runtimeName);
110
- } catch (error) {
111
- return import_ExceptionSerializer.ExceptionSerializer.serializeException(error, command);
110
+ } catch (e) {
111
+ return import_ExceptionSerializer.ExceptionSerializer.serializeException(e, command);
112
112
  }
113
113
  }
114
114
  /**
@@ -23,6 +23,9 @@ __export(ResolveReferenceHandler_exports, {
23
23
  module.exports = __toCommonJS(ResolveReferenceHandler_exports);
24
24
  var import_ReferencesCache = require("../referenceCache/ReferencesCache.cjs");
25
25
  var import_AbstractHandler = require("./AbstractHandler.cjs");
26
+ var import_CommandType = require("../../utils/CommandType.cjs");
27
+ var import_RuntimeName = require("../../utils/RuntimeName.cjs");
28
+ var import_Command = require("../../utils/Command.cjs");
26
29
  class ResolveReferenceHandler extends import_AbstractHandler.AbstractHandler {
27
30
  constructor() {
28
31
  super();
@@ -32,7 +35,11 @@ class ResolveReferenceHandler extends import_AbstractHandler.AbstractHandler {
32
35
  * @returns {any}
33
36
  */
34
37
  process(command) {
35
- return import_ReferencesCache.ReferencesCache.getInstance().resolveReference(command.payload[0]);
38
+ if (command.runtimeName === import_RuntimeName.RuntimeName.Nodejs) {
39
+ return import_ReferencesCache.ReferencesCache.getInstance().resolveReference(command.payload[0]);
40
+ } else {
41
+ return new import_Command.Command(command.runtimeName, import_CommandType.CommandType.Reference, command.payload[0]);
42
+ }
36
43
  }
37
44
  }
38
45
  // Annotate the CommonJS export names for ESM import in node:
@@ -39,7 +39,14 @@ if (!_TransmitterWebsocket) {
39
39
  const requireDynamic = (0, import_Runtime.getRequire)(import_meta.url);
40
40
  class Interpreter {
41
41
  /** @type {Handler | null} */
42
- handler = null;
42
+ _handler = null;
43
+ /** @type {Handler} */
44
+ get handler() {
45
+ if (!this._handler) {
46
+ this._handler = new import_Handler.Handler(this);
47
+ }
48
+ return this._handler;
49
+ }
43
50
  /**
44
51
  *
45
52
  * @param {Command} command
@@ -48,45 +55,32 @@ class Interpreter {
48
55
  */
49
56
  async executeAsync(command, connectionData) {
50
57
  try {
51
- if (!this.handler) {
52
- this.handler = new import_Handler.Handler(this);
53
- }
54
58
  let messageByteArray = new import_CommandSerializer.CommandSerializer().serialize(command, connectionData);
55
59
  let responseByteArray = void 0;
56
- if (command.runtimeName === import_RuntimeName.RuntimeName.Nodejs && connectionData.connectionType === import_ConnectionType.ConnectionType.IN_MEMORY) {
57
- if ((0, import_Runtime.isNodejsRuntime)()) {
60
+ if (connectionData.connectionType === import_ConnectionType.ConnectionType.WEB_SOCKET) {
61
+ const _response = await _TransmitterWebsocket?.sendCommand(messageByteArray, connectionData);
62
+ if (_response) {
63
+ const command2 = new import_CommandDeserializer.CommandDeserializer(_response).deserialize();
64
+ return command2;
65
+ } else {
66
+ throw new Error("Response not received from TransmitterWebsocket");
67
+ }
68
+ } else {
69
+ if (!(0, import_Runtime.isNodejsRuntime)()) {
70
+ throw new Error("InMemory is only allowed in Nodejs runtime");
71
+ }
72
+ if (command.runtimeName === import_RuntimeName.RuntimeName.Nodejs) {
58
73
  if (!_Receiver) {
59
74
  const { Receiver } = require("../receiver/Receiver.cjs");
60
75
  _Receiver = Receiver;
61
76
  }
62
- responseByteArray = _Receiver?.sendCommand(messageByteArray);
77
+ responseByteArray = await _Receiver?.sendCommand(messageByteArray);
63
78
  } else {
64
- throw new Error("Node.js runtime not found");
65
- }
66
- } else if (connectionData.connectionType === import_ConnectionType.ConnectionType.WEB_SOCKET) {
67
- try {
68
- const _response = await _TransmitterWebsocket?.sendCommand(
69
- messageByteArray,
70
- connectionData
71
- );
72
- if (_response) {
73
- const command2 = new import_CommandDeserializer.CommandDeserializer(_response).deserialize();
74
- return command2;
75
- } else {
76
- throw new Error("Response not received from TransmitterWebsocket");
77
- }
78
- } catch (error) {
79
- throw error;
80
- }
81
- } else {
82
- if ((0, import_Runtime.isNodejsRuntime)()) {
83
79
  if (!_Transmitter) {
84
80
  const { Transmitter } = require("../transmitter/Transmitter.cjs");
85
81
  _Transmitter = Transmitter;
86
82
  }
87
83
  responseByteArray = await _Transmitter?.sendCommand(messageByteArray);
88
- } else {
89
- throw new Error("Allowed only to run in nodejs runtime");
90
84
  }
91
85
  }
92
86
  if (!responseByteArray) {
@@ -105,43 +99,27 @@ class Interpreter {
105
99
  */
106
100
  execute(command, connectionData) {
107
101
  try {
108
- if (!this.handler) {
109
- this.handler = new import_Handler.Handler(this);
110
- }
111
102
  let messageByteArray = new import_CommandSerializer.CommandSerializer().serialize(command, connectionData);
112
103
  let responseByteArray = void 0;
113
- if (command.runtimeName === import_RuntimeName.RuntimeName.Nodejs && connectionData.connectionType === import_ConnectionType.ConnectionType.IN_MEMORY) {
114
- if ((0, import_Runtime.isNodejsRuntime)()) {
104
+ if (connectionData.connectionType === import_ConnectionType.ConnectionType.WEB_SOCKET) {
105
+ throw new Error("Not supported");
106
+ }
107
+ if (connectionData.connectionType === import_ConnectionType.ConnectionType.IN_MEMORY) {
108
+ if (!(0, import_Runtime.isNodejsRuntime)()) {
109
+ throw new Error("InMemory is only allowed in Nodejs runtime");
110
+ }
111
+ if (command.runtimeName === import_RuntimeName.RuntimeName.Nodejs) {
115
112
  if (!_Receiver) {
116
113
  const { Receiver } = require("../receiver/Receiver.cjs");
117
114
  _Receiver = Receiver;
118
115
  }
119
116
  responseByteArray = _Receiver?.sendCommand(messageByteArray);
120
- }
121
- } else if (connectionData.connectionType === import_ConnectionType.ConnectionType.WEB_SOCKET) {
122
- const promise = _TransmitterWebsocket?.sendCommand(messageByteArray, connectionData);
123
- if (!promise) {
124
- throw new Error("TransmitterWebsocket not found");
125
- }
126
- return promise.then((_response) => {
127
- if (_response) {
128
- const command2 = new import_CommandDeserializer.CommandDeserializer(_response).deserialize();
129
- return command2;
130
- } else {
131
- throw new Error("Response not received from TransmitterWebsocket");
132
- }
133
- }).catch((error) => {
134
- throw error;
135
- });
136
- } else {
137
- if ((0, import_Runtime.isNodejsRuntime)()) {
117
+ } else {
138
118
  if (!_Transmitter) {
139
119
  const { Transmitter } = require("../transmitter/Transmitter.cjs");
140
120
  _Transmitter = Transmitter;
141
121
  }
142
122
  responseByteArray = _Transmitter?.sendCommand(messageByteArray);
143
- } else {
144
- throw new Error("Allowed only to run in nodejs runtime");
145
123
  }
146
124
  }
147
125
  if (!responseByteArray) {
@@ -159,9 +137,6 @@ class Interpreter {
159
137
  */
160
138
  process(messageByteArray) {
161
139
  try {
162
- if (!this.handler) {
163
- this.handler = new import_Handler.Handler(this);
164
- }
165
140
  const receivedCommand = new import_CommandDeserializer.CommandDeserializer(messageByteArray).deserialize();
166
141
  return this.handler?.handleCommand(receivedCommand);
167
142
  } catch (error) {
@@ -25,26 +25,34 @@ var import_Interpreter = require("../interpreter/Interpreter.cjs");
25
25
  var import_CommandSerializer = require("../protocol/CommandSerializer.cjs");
26
26
  var import_Runtime = require("../../utils/Runtime.cjs");
27
27
  var import_InMemoryConnectionData = require("../../utils/connectionData/InMemoryConnectionData.cjs");
28
- const import_meta = {};
29
- let _RuntimeLogger = null;
30
- const requireDynamic = (0, import_Runtime.getRequire)(import_meta.url);
28
+ var import_ExceptionSerializer = require("../../utils/exception/ExceptionSerializer.cjs");
29
+ var import_Command = require("../../utils/Command.cjs");
30
+ var import_CommandType = require("../../utils/CommandType.cjs");
31
+ var import_RuntimeName = require("../../utils/RuntimeName.cjs");
32
+ var import_RuntimeLogger = require("../../utils/RuntimeLogger.cjs");
31
33
  class Receiver {
32
34
  static connectionData = new import_InMemoryConnectionData.InMemoryConnectionData();
33
35
  Receiver() {
34
- if (!_RuntimeLogger) {
35
- const { RuntimeLogger } = require("../../utils/RuntimeLogger.cjs");
36
- _RuntimeLogger = RuntimeLogger;
37
- }
38
- _RuntimeLogger?.printRuntimeInfo();
36
+ }
37
+ static getRuntimeInfo() {
38
+ return import_RuntimeLogger.RuntimeLogger.getRuntimeInfo();
39
39
  }
40
40
  /**
41
41
  * @param {Int8Array} messageByteArray
42
42
  */
43
43
  static sendCommand(messageByteArray) {
44
- return new import_CommandSerializer.CommandSerializer().serialize(
45
- new import_Interpreter.Interpreter().process(messageByteArray),
46
- this.connectionData
47
- );
44
+ try {
45
+ return new import_CommandSerializer.CommandSerializer().serialize(
46
+ new import_Interpreter.Interpreter().process(messageByteArray),
47
+ this.connectionData
48
+ );
49
+ } catch (error) {
50
+ const exceptionCommand = import_ExceptionSerializer.ExceptionSerializer.serializeException(
51
+ error,
52
+ new import_Command.Command(import_RuntimeName.RuntimeName.Nodejs, import_CommandType.CommandType.Exception, [])
53
+ );
54
+ return new import_CommandSerializer.CommandSerializer().serialize(exceptionCommand, this.connectionData);
55
+ }
48
56
  }
49
57
  /**
50
58
  * @param {Int8Array} messageByteArray
@@ -123,7 +123,9 @@ class InvocationContext {
123
123
  if (this.#currentCommand === null) {
124
124
  throw new Error("currentCommand is undefined in Invocation Context execute method");
125
125
  }
126
- this.#interpreter = new import_Interpreter.Interpreter();
126
+ if (!this.#interpreter) {
127
+ this.#interpreter = new import_Interpreter.Interpreter();
128
+ }
127
129
  this.#responseCommand = this.#interpreter.execute(this.#currentCommand, this.#connectionData);
128
130
  if (!this.#responseCommand) {
129
131
  throw new Error("responseCommand is undefined in Invocation Context execute method");
@@ -495,7 +497,9 @@ class InvocationWsContext extends InvocationContext {
495
497
  if (this.#currentCommand === null) {
496
498
  throw new Error("currentCommand is undefined in Invocation Context execute method");
497
499
  }
498
- this.#interpreter = new import_Interpreter.Interpreter();
500
+ if (!this.#interpreter) {
501
+ this.#interpreter = new import_Interpreter.Interpreter();
502
+ }
499
503
  this.#responseCommand = await this.#interpreter.executeAsync(
500
504
  this.#currentCommand,
501
505
  this.#connectionData
@@ -1,7 +1,3 @@
1
- export type Command = import("../../utils/Command.js").Command;
2
- /**
3
- * @typedef {import('../../utils/Command.js').Command} Command
4
- */
5
1
  export class ResolveReferenceHandler extends AbstractHandler {
6
2
  /**
7
3
  * @param {Command} command
@@ -10,3 +6,4 @@ export class ResolveReferenceHandler extends AbstractHandler {
10
6
  process(command: Command): any;
11
7
  }
12
8
  import { AbstractHandler } from './AbstractHandler.js';
9
+ import { Command } from '../../utils/Command.js';
@@ -1,6 +1,8 @@
1
1
  export class Interpreter {
2
2
  /** @type {Handler | null} */
3
- handler: Handler | null;
3
+ _handler: Handler | null;
4
+ /** @type {Handler} */
5
+ get handler(): Handler;
4
6
  /**
5
7
  *
6
8
  * @param {Command} command
@@ -1,5 +1,6 @@
1
1
  export class Receiver {
2
2
  static connectionData: InMemoryConnectionData;
3
+ static getRuntimeInfo(): string;
3
4
  /**
4
5
  * @param {Int8Array} messageByteArray
5
6
  */
@@ -41,4 +41,7 @@ export namespace CommandType {
41
41
  let PassDelegate: 39;
42
42
  let InvokeDelegate: 40;
43
43
  let ConvertType: 41;
44
+ let AddEventListener: 42;
45
+ let PluginWrapper: 43;
46
+ let GetAsyncOperationResult: 44;
44
47
  }
@@ -65,7 +65,10 @@ const CommandType = (
65
65
  GetInstanceMethodAsDelegate: 38,
66
66
  PassDelegate: 39,
67
67
  InvokeDelegate: 40,
68
- ConvertType: 41
68
+ ConvertType: 41,
69
+ AddEventListener: 42,
70
+ PluginWrapper: 43,
71
+ GetAsyncOperationResult: 44
69
72
  }
70
73
  );
71
74
  // Annotate the CommonJS export names for ESM import in node:
@@ -28,19 +28,31 @@ var import_ExceptionType = require("../ExceptionType.cjs");
28
28
  class ExceptionSerializer {
29
29
  static serializeException(exception, command) {
30
30
  let exceptionCommand = new import_Command.Command(import_RuntimeName.RuntimeName.Nodejs, import_CommandType.CommandType.Exception, []);
31
- exceptionCommand = exceptionCommand.addArgToPayload(this.getExceptionCode(exception));
32
- exceptionCommand = exceptionCommand.addArgToPayload(command.toString());
33
- exceptionCommand = exceptionCommand.addArgToPayload(exception.name);
34
- exceptionCommand = exceptionCommand.addArgToPayload(exception.message);
35
31
  let stackClasses = [];
36
32
  let stackMethods = [];
37
33
  let stackLines = [];
38
34
  let stackFiles = [];
39
- this.serializeStackTrace(exception, stackClasses, stackMethods, stackLines, stackFiles);
40
- exceptionCommand = exceptionCommand.addArgToPayload(stackClasses.join("|"));
41
- exceptionCommand = exceptionCommand.addArgToPayload(stackMethods.join("|"));
42
- exceptionCommand = exceptionCommand.addArgToPayload(stackLines.join("|"));
43
- exceptionCommand = exceptionCommand.addArgToPayload(stackFiles.join("|"));
35
+ try {
36
+ this.serializeStackTrace(exception, stackClasses, stackMethods, stackLines, stackFiles);
37
+ exceptionCommand = exceptionCommand.addArgToPayload(this.getExceptionCode(exception));
38
+ exceptionCommand = exceptionCommand.addArgToPayload(command ? command.toString() : "Command is null");
39
+ exceptionCommand = exceptionCommand.addArgToPayload(exception.name);
40
+ exceptionCommand = exceptionCommand.addArgToPayload(exception.message);
41
+ exceptionCommand = exceptionCommand.addArgToPayload(stackClasses.join("|"));
42
+ exceptionCommand = exceptionCommand.addArgToPayload(stackMethods.join("|"));
43
+ exceptionCommand = exceptionCommand.addArgToPayload(stackLines.join("|"));
44
+ exceptionCommand = exceptionCommand.addArgToPayload(stackFiles.join("|"));
45
+ } catch (e) {
46
+ exceptionCommand = new import_Command.Command(import_RuntimeName.RuntimeName.Nodejs, import_CommandType.CommandType.Exception, []);
47
+ exceptionCommand = exceptionCommand.addArgToPayload(this.getExceptionCode(e));
48
+ exceptionCommand = exceptionCommand.addArgToPayload(command ? command.toString() : "Command is null");
49
+ exceptionCommand = exceptionCommand.addArgToPayload("Node.js Exception Serialization Error");
50
+ exceptionCommand = exceptionCommand.addArgToPayload(e.message);
51
+ exceptionCommand = exceptionCommand.addArgToPayload("ExceptionSerializer");
52
+ exceptionCommand = exceptionCommand.addArgToPayload("serializeException");
53
+ exceptionCommand = exceptionCommand.addArgToPayload("unknown");
54
+ exceptionCommand = exceptionCommand.addArgToPayload("ExceptionSerializer.js");
55
+ }
44
56
  return exceptionCommand;
45
57
  }
46
58
  static getExceptionCode(exception) {
@@ -56,22 +68,35 @@ class ExceptionSerializer {
56
68
  }
57
69
  }
58
70
  static serializeStackTrace(exception, stackClasses, stackMethods, stackLines, stackFiles) {
71
+ if (!exception || typeof exception.stack !== "string") {
72
+ return;
73
+ }
59
74
  const stackTrace = exception.stack.split("\n").slice(1);
60
- for (let i = 0; i < stackTrace.length; i++) {
61
- const parts = stackTrace[i].trim().match(/at\s(.*)\s\((.*):(\d+):(\d+)\)/);
75
+ for (const line of stackTrace) {
76
+ const trimmedLine = line.trim();
77
+ if (trimmedLine.includes("Javonet.Node.js")) {
78
+ continue;
79
+ }
80
+ let parts = trimmedLine.match(/at\s+(.*?)\s+\((.*?):(\d+):\d+\)/);
62
81
  if (parts) {
63
- stackClasses.push(parts[1]);
64
- stackMethods.push("unknown");
65
- stackLines.push(parts[3]);
66
- stackFiles.push(parts[2]);
67
- } else {
68
- const parts2 = stackTrace[i].trim().match(/at\s(.*):(\d+):(\d+)/);
69
- if (parts2) {
82
+ const classAndMethod = parts[1].split(".");
83
+ if (classAndMethod.length > 1) {
84
+ stackClasses.push(classAndMethod[0]);
85
+ stackMethods.push(classAndMethod.slice(1).join("."));
86
+ } else {
70
87
  stackClasses.push("unknown");
71
- stackMethods.push("unknown");
72
- stackLines.push(parts2[2]);
73
- stackFiles.push(parts2[1]);
88
+ stackMethods.push(parts[1]);
74
89
  }
90
+ stackFiles.push(parts[2]);
91
+ stackLines.push(parts[3]);
92
+ continue;
93
+ }
94
+ parts = trimmedLine.match(/at\s+(.*?):(\d+):\d+/);
95
+ if (parts) {
96
+ stackClasses.push("unknown");
97
+ stackMethods.push("unknown");
98
+ stackFiles.push(parts[1]);
99
+ stackLines.push(parts[2]);
75
100
  }
76
101
  }
77
102
  }
@@ -100,8 +100,8 @@ class Handler {
100
100
  }
101
101
  let response = handlers[command.commandType].handleCommand(command)
102
102
  return this.parseCommand(response, command.runtimeName)
103
- } catch (error) {
104
- return ExceptionSerializer.serializeException(error, command)
103
+ } catch (e) {
104
+ return ExceptionSerializer.serializeException(e, command)
105
105
  }
106
106
  }
107
107
 
@@ -1,10 +1,10 @@
1
1
  // @ts-check
2
2
  import { ReferencesCache } from '../referenceCache/ReferencesCache.js'
3
3
  import { AbstractHandler } from './AbstractHandler.js'
4
+ import { CommandType } from '../../utils/CommandType.js'
5
+ import { RuntimeName } from '../../utils/RuntimeName.js'
6
+ import { Command } from '../../utils/Command.js'
4
7
 
5
- /**
6
- * @typedef {import('../../utils/Command.js').Command} Command
7
- */
8
8
 
9
9
  class ResolveReferenceHandler extends AbstractHandler {
10
10
  constructor() {
@@ -16,7 +16,12 @@ class ResolveReferenceHandler extends AbstractHandler {
16
16
  * @returns {any}
17
17
  */
18
18
  process(command) {
19
- return ReferencesCache.getInstance().resolveReference(command.payload[0])
19
+ if (command.runtimeName === RuntimeName.Nodejs) {
20
+ return ReferencesCache.getInstance().resolveReference(command.payload[0])
21
+ }
22
+ else {
23
+ return new Command(command.runtimeName, CommandType.Reference, command.payload[0])
24
+ }
20
25
  }
21
26
  }
22
27
 
@@ -29,7 +29,16 @@ const requireDynamic = getRequire(import.meta.url)
29
29
 
30
30
  export class Interpreter {
31
31
  /** @type {Handler | null} */
32
- handler = null
32
+ _handler = null
33
+
34
+ /** @type {Handler} */
35
+ get handler() {
36
+ if (!this._handler) {
37
+ this._handler = new Handler(this)
38
+ }
39
+ return this._handler
40
+ }
41
+
33
42
  /**
34
43
  *
35
44
  * @param {Command} command
@@ -38,57 +47,42 @@ export class Interpreter {
38
47
  */
39
48
  async executeAsync(command, connectionData) {
40
49
  try {
41
- if (!this.handler) {
42
- this.handler = new Handler(this)
43
- }
44
50
  let messageByteArray = new CommandSerializer().serialize(command, connectionData)
45
51
  /** @type {Int8Array | undefined} */
46
52
  let responseByteArray = undefined
47
53
 
48
- if (
49
- command.runtimeName === RuntimeName.Nodejs &&
50
- connectionData.connectionType === ConnectionType.IN_MEMORY
51
- ) {
52
- if (isNodejsRuntime()) {
53
- // lazy receiver loading
54
+ if (connectionData.connectionType === ConnectionType.WEB_SOCKET) {
55
+ const _response = await _TransmitterWebsocket?.sendCommand(messageByteArray, connectionData)
56
+ if (_response) {
57
+ const command = new CommandDeserializer(_response).deserialize()
58
+ return command
59
+ } else {
60
+ throw new Error('Response not received from TransmitterWebsocket')
61
+ }
62
+ } else {
63
+ if (!isNodejsRuntime()) {
64
+ throw new Error('InMemory is only allowed in Nodejs runtime')
65
+ }
66
+
67
+ if (command.runtimeName === RuntimeName.Nodejs) {
54
68
  if (!_Receiver) {
55
69
  const { Receiver } = requireDynamic('../receiver/Receiver.js')
56
70
  _Receiver = Receiver
57
71
  }
58
- responseByteArray = _Receiver?.sendCommand(messageByteArray)
72
+ responseByteArray = await _Receiver?.sendCommand(messageByteArray)
59
73
  } else {
60
- throw new Error('Node.js runtime not found')
61
- }
62
- } else if (connectionData.connectionType === ConnectionType.WEB_SOCKET) {
63
- try {
64
- const _response = await _TransmitterWebsocket?.sendCommand(
65
- messageByteArray,
66
- connectionData
67
- )
68
- if (_response) {
69
- const command = new CommandDeserializer(_response).deserialize()
70
- return command
71
- } else {
72
- throw new Error('Response not received from TransmitterWebsocket')
73
- }
74
- } catch (error) {
75
- throw error
76
- }
77
- } else {
78
- if (isNodejsRuntime()) {
79
- // lazy transmitter loading
80
74
  if (!_Transmitter) {
81
75
  const { Transmitter } = requireDynamic('../transmitter/Transmitter.js')
82
76
  _Transmitter = Transmitter
83
77
  }
84
78
  responseByteArray = await _Transmitter?.sendCommand(messageByteArray)
85
- } else {
86
- throw new Error('Allowed only to run in nodejs runtime')
87
79
  }
88
80
  }
81
+
89
82
  if (!responseByteArray) {
90
83
  throw new Error('No response received from Transmitter')
91
84
  }
85
+
92
86
  return new CommandDeserializer(responseByteArray).deserialize()
93
87
  } catch (error) {
94
88
  throw error
@@ -103,60 +97,37 @@ export class Interpreter {
103
97
  */
104
98
  execute(command, connectionData) {
105
99
  try {
106
- if (!this.handler) {
107
- this.handler = new Handler(this)
108
- }
109
100
  let messageByteArray = new CommandSerializer().serialize(command, connectionData)
110
101
  /** @type {Int8Array | undefined} */
111
102
  let responseByteArray = undefined
112
103
 
113
- if (
114
- command.runtimeName === RuntimeName.Nodejs &&
115
- connectionData.connectionType === ConnectionType.IN_MEMORY
116
- ) {
117
- if (isNodejsRuntime()) {
118
- // lazy receiver loading
104
+ if (connectionData.connectionType === ConnectionType.WEB_SOCKET) {
105
+ throw new Error('Not supported')
106
+ }
107
+ if (connectionData.connectionType === ConnectionType.IN_MEMORY) {
108
+ if (!isNodejsRuntime()) {
109
+ throw new Error('InMemory is only allowed in Nodejs runtime')
110
+ }
111
+
112
+ if (command.runtimeName === RuntimeName.Nodejs) {
119
113
  if (!_Receiver) {
120
114
  const { Receiver } = requireDynamic('../receiver/Receiver.js')
121
115
  _Receiver = Receiver
122
116
  }
123
117
  responseByteArray = _Receiver?.sendCommand(messageByteArray)
124
- }
125
- } else if (connectionData.connectionType === ConnectionType.WEB_SOCKET) {
126
- // lazy transmitter websocket loading
127
- const promise = _TransmitterWebsocket?.sendCommand(messageByteArray, connectionData)
128
-
129
- if (!promise) {
130
- throw new Error('TransmitterWebsocket not found')
131
- }
132
-
133
- return promise
134
- .then((_response) => {
135
- if (_response) {
136
- const command = new CommandDeserializer(_response).deserialize()
137
- return command
138
- } else {
139
- throw new Error('Response not received from TransmitterWebsocket')
140
- }
141
- })
142
- .catch((error) => {
143
- throw error
144
- })
145
- } else {
146
- if (isNodejsRuntime()) {
118
+ } else {
147
119
  if (!_Transmitter) {
148
120
  const { Transmitter } = requireDynamic('../transmitter/Transmitter.js')
149
121
  _Transmitter = Transmitter
150
122
  }
151
-
152
123
  responseByteArray = _Transmitter?.sendCommand(messageByteArray)
153
- } else {
154
- throw new Error('Allowed only to run in nodejs runtime')
155
124
  }
156
125
  }
126
+
157
127
  if (!responseByteArray) {
158
128
  throw new Error('No response received from Transmitter')
159
129
  }
130
+
160
131
  return new CommandDeserializer(responseByteArray).deserialize()
161
132
  } catch (error) {
162
133
  throw error
@@ -170,9 +141,6 @@ export class Interpreter {
170
141
  */
171
142
  process(messageByteArray) {
172
143
  try {
173
- if (!this.handler) {
174
- this.handler = new Handler(this)
175
- }
176
144
  const receivedCommand = new CommandDeserializer(messageByteArray).deserialize()
177
145
  return this.handler?.handleCommand(receivedCommand)
178
146
  } catch (error) {
@@ -3,30 +3,38 @@ import { Interpreter } from '../interpreter/Interpreter.js'
3
3
  import { CommandSerializer } from '../protocol/CommandSerializer.js'
4
4
  import { getRequire } from '../../utils/Runtime.js'
5
5
  import { InMemoryConnectionData } from '../../utils/connectionData/InMemoryConnectionData.js'
6
-
7
- /** @type {typeof import('../../utils/RuntimeLogger.js').RuntimeLogger | null} */
8
- let _RuntimeLogger = null
9
-
10
- const requireDynamic = getRequire(import.meta.url)
6
+ import { ExceptionSerializer } from '../../utils/exception/ExceptionSerializer.js'
7
+ import { Command } from '../../utils/Command.js'
8
+ import { CommandType } from '../../utils/CommandType.js'
9
+ import { RuntimeName } from '../../utils/RuntimeName.js'
10
+ import { RuntimeLogger } from '../../utils/RuntimeLogger.js'
11
11
 
12
12
  export class Receiver {
13
13
  static connectionData = new InMemoryConnectionData()
14
14
  Receiver() {
15
- if (!_RuntimeLogger) {
16
- const { RuntimeLogger } = requireDynamic('../../utils/RuntimeLogger.js')
17
- _RuntimeLogger = RuntimeLogger
18
- }
19
- _RuntimeLogger?.printRuntimeInfo()
15
+
16
+ }
17
+
18
+ static getRuntimeInfo() {
19
+ return RuntimeLogger.getRuntimeInfo()
20
20
  }
21
21
 
22
22
  /**
23
23
  * @param {Int8Array} messageByteArray
24
24
  */
25
25
  static sendCommand(messageByteArray) {
26
- return new CommandSerializer().serialize(
27
- new Interpreter().process(messageByteArray),
28
- this.connectionData
29
- )
26
+ try {
27
+ return new CommandSerializer().serialize(
28
+ new Interpreter().process(messageByteArray),
29
+ this.connectionData
30
+ )
31
+ } catch (error) {
32
+ const exceptionCommand = ExceptionSerializer.serializeException(
33
+ error,
34
+ new Command(RuntimeName.Nodejs, CommandType.Exception, [])
35
+ )
36
+ return new CommandSerializer().serialize(exceptionCommand, this.connectionData)
37
+ }
30
38
  }
31
39
 
32
40
  /**
@@ -122,7 +122,9 @@ class InvocationContext {
122
122
  throw new Error('currentCommand is undefined in Invocation Context execute method')
123
123
  }
124
124
 
125
- this.#interpreter = new Interpreter()
125
+ if (!this.#interpreter) {
126
+ this.#interpreter = new Interpreter()
127
+ }
126
128
  //@ts-expect-error
127
129
  this.#responseCommand = this.#interpreter.execute(this.#currentCommand, this.#connectionData)
128
130
 
@@ -137,6 +139,7 @@ class InvocationContext {
137
139
  this.#isExecuted = true
138
140
  return this
139
141
  }
142
+
140
143
  return new InvocationContext(this.#runtimeName, this.#connectionData, this.#responseCommand, true)
141
144
  }
142
145
 
@@ -531,7 +534,9 @@ class InvocationWsContext extends InvocationContext {
531
534
  throw new Error('currentCommand is undefined in Invocation Context execute method')
532
535
  }
533
536
 
534
- this.#interpreter = new Interpreter()
537
+ if (!this.#interpreter) {
538
+ this.#interpreter = new Interpreter()
539
+ }
535
540
  this.#responseCommand = await this.#interpreter.executeAsync(
536
541
  this.#currentCommand,
537
542
  this.#connectionData
@@ -548,6 +553,7 @@ class InvocationWsContext extends InvocationContext {
548
553
  this.#isExecuted = true
549
554
  return this
550
555
  }
556
+
551
557
  return new InvocationWsContext(this.#runtimeName, this.#connectionData, this.#responseCommand, true)
552
558
  }
553
559
  }
@@ -42,4 +42,7 @@ export const CommandType = /** @type {const} */ ({
42
42
  PassDelegate: 39,
43
43
  InvokeDelegate: 40,
44
44
  ConvertType: 41,
45
+ AddEventListener: 42,
46
+ PluginWrapper: 43,
47
+ GetAsyncOperationResult: 44,
45
48
  })
@@ -6,21 +6,34 @@ import { ExceptionType } from '../ExceptionType.js'
6
6
  class ExceptionSerializer {
7
7
  static serializeException(exception, command) {
8
8
  let exceptionCommand = new Command(RuntimeName.Nodejs, CommandType.Exception, [])
9
- exceptionCommand = exceptionCommand.addArgToPayload(this.getExceptionCode(exception))
10
- exceptionCommand = exceptionCommand.addArgToPayload(command.toString())
11
- exceptionCommand = exceptionCommand.addArgToPayload(exception.name)
12
- exceptionCommand = exceptionCommand.addArgToPayload(exception.message)
13
9
 
14
10
  let stackClasses = []
15
11
  let stackMethods = []
16
12
  let stackLines = []
17
13
  let stackFiles = []
18
14
 
19
- this.serializeStackTrace(exception, stackClasses, stackMethods, stackLines, stackFiles)
20
- exceptionCommand = exceptionCommand.addArgToPayload(stackClasses.join('|'))
21
- exceptionCommand = exceptionCommand.addArgToPayload(stackMethods.join('|'))
22
- exceptionCommand = exceptionCommand.addArgToPayload(stackLines.join('|'))
23
- exceptionCommand = exceptionCommand.addArgToPayload(stackFiles.join('|'))
15
+ try {
16
+ this.serializeStackTrace(exception, stackClasses, stackMethods, stackLines, stackFiles)
17
+
18
+ exceptionCommand = exceptionCommand.addArgToPayload(this.getExceptionCode(exception))
19
+ exceptionCommand = exceptionCommand.addArgToPayload(command ? command.toString() : 'Command is null')
20
+ exceptionCommand = exceptionCommand.addArgToPayload(exception.name)
21
+ exceptionCommand = exceptionCommand.addArgToPayload(exception.message)
22
+ exceptionCommand = exceptionCommand.addArgToPayload(stackClasses.join('|'))
23
+ exceptionCommand = exceptionCommand.addArgToPayload(stackMethods.join('|'))
24
+ exceptionCommand = exceptionCommand.addArgToPayload(stackLines.join('|'))
25
+ exceptionCommand = exceptionCommand.addArgToPayload(stackFiles.join('|'))
26
+ } catch (e) {
27
+ exceptionCommand = new Command(RuntimeName.Nodejs, CommandType.Exception, [])
28
+ exceptionCommand = exceptionCommand.addArgToPayload(this.getExceptionCode(e))
29
+ exceptionCommand = exceptionCommand.addArgToPayload(command ? command.toString() : 'Command is null')
30
+ exceptionCommand = exceptionCommand.addArgToPayload('Node.js Exception Serialization Error')
31
+ exceptionCommand = exceptionCommand.addArgToPayload(e.message)
32
+ exceptionCommand = exceptionCommand.addArgToPayload('ExceptionSerializer')
33
+ exceptionCommand = exceptionCommand.addArgToPayload('serializeException')
34
+ exceptionCommand = exceptionCommand.addArgToPayload('unknown')
35
+ exceptionCommand = exceptionCommand.addArgToPayload('ExceptionSerializer.js')
36
+ }
24
37
 
25
38
  return exceptionCommand
26
39
  }
@@ -39,23 +52,40 @@ class ExceptionSerializer {
39
52
  }
40
53
 
41
54
  static serializeStackTrace(exception, stackClasses, stackMethods, stackLines, stackFiles) {
55
+ if (!exception || typeof exception.stack !== 'string') {
56
+ return
57
+ }
42
58
  const stackTrace = exception.stack.split('\n').slice(1)
43
59
 
44
- for (let i = 0; i < stackTrace.length; i++) {
45
- const parts = stackTrace[i].trim().match(/at\s(.*)\s\((.*):(\d+):(\d+)\)/)
60
+ for (const line of stackTrace) {
61
+ const trimmedLine = line.trim()
62
+ if (trimmedLine.includes('Javonet.Node.js')) {
63
+ continue
64
+ }
65
+
66
+ // Pattern for: at Class.method (file:line:column) or at functionName (file:line:column)
67
+ let parts = trimmedLine.match(/at\s+(.*?)\s+\((.*?):(\d+):\d+\)/)
46
68
  if (parts) {
47
- stackClasses.push(parts[1])
48
- stackMethods.push('unknown')
49
- stackLines.push(parts[3])
50
- stackFiles.push(parts[2])
51
- } else {
52
- const parts = stackTrace[i].trim().match(/at\s(.*):(\d+):(\d+)/)
53
- if (parts) {
69
+ const classAndMethod = parts[1].split('.')
70
+ if (classAndMethod.length > 1) {
71
+ stackClasses.push(classAndMethod[0])
72
+ stackMethods.push(classAndMethod.slice(1).join('.'))
73
+ } else {
54
74
  stackClasses.push('unknown')
55
- stackMethods.push('unknown')
56
- stackLines.push(parts[2])
57
- stackFiles.push(parts[1])
75
+ stackMethods.push(parts[1])
58
76
  }
77
+ stackFiles.push(parts[2])
78
+ stackLines.push(parts[3])
79
+ continue
80
+ }
81
+
82
+ // Pattern for: at file:line:column (often for anonymous functions or top-level scripts)
83
+ parts = trimmedLine.match(/at\s+(.*?):(\d+):\d+/)
84
+ if (parts) {
85
+ stackClasses.push('unknown')
86
+ stackMethods.push('unknown')
87
+ stackFiles.push(parts[1])
88
+ stackLines.push(parts[2])
59
89
  }
60
90
  }
61
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "javonet-nodejs-sdk",
3
- "version": "2.5.20",
3
+ "version": "2.6.0",
4
4
  "description": "Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. It works on Linux/Windows and MacOS for applications created in JVM, CLR/Netcore, Perl, Python, Ruby, NodeJS, C++ or GoLang and gives you unparalleled freedom and flexibility with native performance in building your mixed-technologies products. Let it be accessing best AI or cryptography libraries, devices SDKs, legacy client modules, internal custom packages or anything from public repositories available on NPM, Nuget, PyPI, Maven/Gradle, RubyGems or GitHub. Get free from programming languages barriers today! For more information check out our guides at https://www.javonet.com/guides/v2/",
5
5
  "keywords": [],
6
6
  "author": "SdNCenter Sp. z o. o.",
@@ -70,6 +70,7 @@
70
70
  "eslint-config-prettier": "^9.1.0",
71
71
  "eslint-plugin-jest": "^28.9.0",
72
72
  "eslint-plugin-prettier": "^5.2.1",
73
+ "fs-extra": "^11.3.0",
73
74
  "husky": "^8.0.0",
74
75
  "ignore": "^6.0.2",
75
76
  "jest": "^29.7.0",
@@ -87,7 +88,6 @@
87
88
  "peerDependencies": {
88
89
  "crypto": "^1.0.1",
89
90
  "dns": "^0.2.2",
90
- "fs-extra": "^11.2.0",
91
91
  "ws": "^8.0.0"
92
92
  },
93
93
  "peerDependenciesMeta": {