configurapi-runner-ws 1.4.0 → 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 +6 -4
- package/package.json +1 -1
- package/src/index.js +31 -2
- package/src/wsAdapter.js +6 -4
package/README.md
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
- User `Response` for a regular response data.
|
|
5
5
|
- Use `StreamResponse` from `configurapi-handler-ws` for a stream response.
|
|
6
6
|
- With a stream response, data will be delivered from the server in chunks.
|
|
7
|
-
- A check will have `
|
|
8
|
-
- The last chunk will have `
|
|
9
|
-
- Check `
|
|
7
|
+
- A check will have `x-stream: chunk` header.
|
|
8
|
+
- The last chunk will have `x-stream: done` header.
|
|
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
|
|
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
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);
|
|
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
|
@@ -48,7 +48,7 @@ module.exports = {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
const payload = { chunk: bucket };
|
|
51
|
-
const headers = { '
|
|
51
|
+
const headers = { 'x-stream': 'chunk', 'x-stream-id': streamId, 'Content-Type': 'application/json' };
|
|
52
52
|
const response = new Response(bucket, 200, headers);
|
|
53
53
|
|
|
54
54
|
ws.send(JSON.stringify(response), (err) =>
|
|
@@ -94,7 +94,7 @@ module.exports = {
|
|
|
94
94
|
|
|
95
95
|
if (ws.readyState === WebSocket.OPEN)
|
|
96
96
|
{
|
|
97
|
-
const headers = { '
|
|
97
|
+
const headers = { 'x-stream-id': streamId, 'x-stream': 'done' };
|
|
98
98
|
const response = new Response('', 204, headers);
|
|
99
99
|
|
|
100
100
|
ws.send(JSON.stringify(response), (err) =>
|
|
@@ -149,8 +149,10 @@ module.exports = {
|
|
|
149
149
|
request.params = data.params || {};
|
|
150
150
|
request.payload = data.payload || undefined;
|
|
151
151
|
|
|
152
|
-
request.headers['
|
|
153
|
-
request.headers['
|
|
152
|
+
request.headers['connection-id'] = ws.connectionId;
|
|
153
|
+
request.headers['push-function'] = async (data)=>await this.write(ws, data);
|
|
154
|
+
|
|
155
|
+
request.headers['sec-websocket-protocol'] = ws.protocol
|
|
154
156
|
|
|
155
157
|
return request;
|
|
156
158
|
}
|