configurapi-runner-ws 1.4.1 → 1.5.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.
package/README.md CHANGED
@@ -9,4 +9,6 @@
9
9
  - Check `x-stream-id: $id` header for the stream ID. A chunk belonging to the same stream will have the same stream ID.
10
10
  - connectionId is available at `event.request.headers.connectionId`
11
11
  - `on_connect` and `on_disconnect` will be called when a new connection is made or closed.
12
- - Use `Request.headers['push-function']` to send an out-of-band message. The function is async and accepts an object to send back to the caller.
12
+ - Use `Request.headers['push-function']` to send an out-of-band message. The function is async and accepts an object to send back to the caller.
13
+ - Use `on_connect` to perform authorization.
14
+ - `Sec-WebSocket-Protocol` is automatically returned for `on_connect` events.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "configurapi-runner-ws",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "Websocket runner for configurapi.",
5
5
  "bin": {
6
6
  "configurapi-runner-ws": "src/app.mjs"
package/src/index.js CHANGED
@@ -55,7 +55,22 @@ module.exports = class HttpRunner extends events.EventEmitter
55
55
  this.emit("trace", "Server is listening..."+this.port);
56
56
  }
57
57
 
58
- const wss = new WebSocketServer({ server: httpServer, path: "/ws" })
58
+ const wss = new WebSocketServer({ server: httpServer, path: "/ws", handleProtocols: (protocols, req) => {
59
+ if(protocols === undefined)
60
+ {
61
+ return;
62
+ }
63
+ if(!Array.isArray(protocols))
64
+ {
65
+ let array = Array.from(protocols);
66
+ return array.length > 0 ? array[0] : undefined;
67
+ }
68
+ else
69
+ {
70
+ //array
71
+ return array.length > 0 ? array[0] : undefined;
72
+ }
73
+ } })
59
74
 
60
75
  wss.on("connection", async (ws, req) =>
61
76
  {
@@ -112,7 +127,21 @@ module.exports = class HttpRunner extends events.EventEmitter
112
127
  if(customEventName)
113
128
  {
114
129
  event.name = customEventName;
115
- await this.service.process(event); // No response will be sent back to the caller for 'on_disconnect'
130
+ await this.service.process(event);
131
+
132
+ if(event.name === 'on_connect')
133
+ {
134
+ let protocol = event.request?.headers?.['sec-websocket-protocol'] || '';
135
+
136
+ if(event.response.headers === undefined)
137
+ {
138
+ event.response.headers = {};
139
+ }
140
+
141
+ event.response.headers['Sec-WebSocket-Protocol'] = protocol;
142
+ }
143
+
144
+ await WsAdapter.write(ws, event.response);
116
145
  }
117
146
  else
118
147
  {
package/src/wsAdapter.js CHANGED
@@ -152,6 +152,8 @@ module.exports = {
152
152
  request.headers['connection-id'] = ws.connectionId;
153
153
  request.headers['push-function'] = async (data)=>await this.write(ws, data);
154
154
 
155
+ request.headers['sec-websocket-protocol'] = ws.protocol
156
+
155
157
  return request;
156
158
  }
157
159
  };