ocpp-ws-io 1.0.0-alpha → 1.0.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.

Potentially problematic release.


This version of ocpp-ws-io might be problematic. Click here for more details.

package/dist/index.js CHANGED
@@ -38056,21 +38056,25 @@ var OCPPClient = class extends import_node_events.EventEmitter {
38056
38056
  }
38057
38057
  });
38058
38058
  }
38059
- // ─── Handle ──────────────────────────────────────────────────
38060
- handle(methodOrHandler, handler) {
38061
- if (typeof methodOrHandler === "function") {
38062
- this._wildcardHandler = methodOrHandler;
38063
- } else if (typeof methodOrHandler === "string" && handler) {
38064
- this._handlers.set(methodOrHandler, handler);
38059
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
38060
+ handle(...args) {
38061
+ if (args.length === 1 && typeof args[0] === "function") {
38062
+ this._wildcardHandler = args[0];
38063
+ } else if (args.length === 2 && typeof args[0] === "string" && typeof args[1] === "function") {
38064
+ this._handlers.set(args[0], args[1]);
38065
+ } else if (args.length === 3 && typeof args[0] === "string" && typeof args[1] === "string" && typeof args[2] === "function") {
38066
+ this._handlers.set(`${args[0]}:${args[1]}`, args[2]);
38065
38067
  } else {
38066
38068
  throw new Error(
38067
- "Invalid arguments: provide (method, handler) or (wildcardHandler)"
38069
+ "Invalid arguments: provide (version, method, handler), (method, handler), or (wildcardHandler)"
38068
38070
  );
38069
38071
  }
38070
38072
  }
38071
- removeHandler(method) {
38072
- if (method) {
38073
- this._handlers.delete(method);
38073
+ removeHandler(versionOrMethod, method) {
38074
+ if (versionOrMethod && method) {
38075
+ this._handlers.delete(`${versionOrMethod}:${method}`);
38076
+ } else if (versionOrMethod) {
38077
+ this._handlers.delete(versionOrMethod);
38074
38078
  } else {
38075
38079
  this._wildcardHandler = null;
38076
38080
  }
@@ -38079,14 +38083,23 @@ var OCPPClient = class extends import_node_events.EventEmitter {
38079
38083
  this._handlers.clear();
38080
38084
  this._wildcardHandler = null;
38081
38085
  }
38082
- // ─── Call ────────────────────────────────────────────────────
38083
- async call(method, params = {}, options = {}) {
38086
+ async call(...args) {
38087
+ let method;
38088
+ let params;
38089
+ let options;
38090
+ if (args.length >= 3 && typeof args[0] === "string" && typeof args[1] === "string") {
38091
+ method = args[1];
38092
+ params = args[2] ?? {};
38093
+ options = args[3] ?? {};
38094
+ } else {
38095
+ method = args[0];
38096
+ params = args[1] ?? {};
38097
+ options = args[2] ?? {};
38098
+ }
38084
38099
  if (this._state !== OPEN) {
38085
38100
  throw new Error(`Cannot call: client is in state ${this._state}`);
38086
38101
  }
38087
- return this._callQueue.push(
38088
- () => this._sendCall(method, params, options)
38089
- );
38102
+ return this._callQueue.push(() => this._sendCall(method, params, options));
38090
38103
  }
38091
38104
  async _sendCall(method, params, options) {
38092
38105
  const msgId = (0, import_node_crypto.randomUUID)();
@@ -38216,7 +38229,7 @@ var OCPPClient = class extends import_node_events.EventEmitter {
38216
38229
  `Already processing call with ID: ${msgId}`
38217
38230
  );
38218
38231
  }
38219
- let handler = this._handlers.get(method);
38232
+ let handler = this._protocol ? this._handlers.get(`${this._protocol}:${method}`) ?? this._handlers.get(method) : this._handlers.get(method);
38220
38233
  let isWildcard = false;
38221
38234
  if (!handler) {
38222
38235
  if (this._wildcardHandler) {
@@ -38236,6 +38249,7 @@ var OCPPClient = class extends import_node_events.EventEmitter {
38236
38249
  const context = {
38237
38250
  messageId: msgId,
38238
38251
  method,
38252
+ protocol: this._protocol,
38239
38253
  params,
38240
38254
  signal: ac.signal
38241
38255
  };
@@ -38293,6 +38307,18 @@ var OCPPClient = class extends import_node_events.EventEmitter {
38293
38307
  _onBadMessage(rawMessage, error) {
38294
38308
  this._badMessageCount++;
38295
38309
  this.emit("badMessage", { message: rawMessage, error });
38310
+ const match = rawMessage.match(/^\s*\[\s*2\s*,\s*"([^"]+)"/);
38311
+ if (match?.[1] && this._ws) {
38312
+ const errorResponse = [
38313
+ MessageType.CALLERROR,
38314
+ match[1],
38315
+ "FormatViolation",
38316
+ error.message || "Invalid message format",
38317
+ {}
38318
+ ];
38319
+ this._ws.send(JSON.stringify(errorResponse));
38320
+ this.emit("callError", errorResponse);
38321
+ }
38296
38322
  if (this._badMessageCount >= this._options.maxBadMessages) {
38297
38323
  this.close({ code: 1002, reason: "Too many bad messages" }).catch(
38298
38324
  () => {
@@ -38482,7 +38508,7 @@ var OCPPServerClient = class extends OCPPClient {
38482
38508
  this._state = ConnectionState.OPEN;
38483
38509
  this._identity = this._options.identity;
38484
38510
  this._ws = context.ws;
38485
- this._protocol = context.ws.protocol;
38511
+ this._protocol = context.protocol ?? context.ws.protocol;
38486
38512
  this._attachWebsocket(context.ws);
38487
38513
  }
38488
38514
  /**
@@ -38731,7 +38757,11 @@ var OCPPServer = class extends import_node_events2.EventEmitter {
38731
38757
  }
38732
38758
  const serverProtocols = this._options.protocols ?? [];
38733
38759
  let selectedProtocol;
38734
- if (serverProtocols.length > 0 && protocols.size > 0) {
38760
+ if (serverProtocols.length > 0) {
38761
+ if (protocols.size === 0) {
38762
+ abortHandshake(socket, 400, "Missing subprotocol");
38763
+ return;
38764
+ }
38735
38765
  selectedProtocol = serverProtocols.find((p) => protocols.has(p));
38736
38766
  if (!selectedProtocol) {
38737
38767
  abortHandshake(socket, 400, "No matching subprotocol");
@@ -38806,7 +38836,8 @@ var OCPPServer = class extends import_node_events2.EventEmitter {
38806
38836
  const client = new OCPPServerClient(clientOptions, {
38807
38837
  ws,
38808
38838
  handshake,
38809
- session: {}
38839
+ session: {},
38840
+ protocol: selectedProtocol
38810
38841
  });
38811
38842
  this._clients.add(client);
38812
38843
  client.on("close", () => {