configurapi-runner-ws 1.0.2 → 1.2.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 +8 -2
- package/package.json +2 -2
- package/src/index.js +15 -4
- package/src/wsAdapter.js +2 -1
package/README.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# configurapi-runner-ws
|
|
2
2
|
|
|
3
3
|
- Use `Event` for an incoming data.
|
|
4
|
-
- User `Response` for a response data.
|
|
5
|
-
-
|
|
4
|
+
- User `Response` for a regular response data.
|
|
5
|
+
- Use `StreamResponse` from `configurapi-handler-ws` for a stream response.
|
|
6
|
+
- With a stream response, data will be delivered from the server in chunks.
|
|
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
|
+
- connectionId is available at `event.request.headers.connectionId`
|
|
11
|
+
- `on_connect` and `on_disconnect` will be called when a new connection is made or closed.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "configurapi-runner-ws",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Websocket runner for configurapi.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"configurapi-runner-ws": "src/app.mjs"
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"homepage": "https://gitlab.com/mappies/configurapi-runner-ws#readme",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"commander": "^14.0.1",
|
|
31
|
-
"configurapi": "^
|
|
31
|
+
"configurapi": "^2.2.1",
|
|
32
32
|
"configurapi-handler-ws": "^1.0.0",
|
|
33
33
|
"dotenv": "^17.2.3",
|
|
34
34
|
"one-liner": "^1.3.0",
|
package/src/index.js
CHANGED
|
@@ -64,6 +64,8 @@ module.exports = class HttpRunner extends events.EventEmitter
|
|
|
64
64
|
|
|
65
65
|
this.emit("trace", `[connect] ${connectionId}`);
|
|
66
66
|
|
|
67
|
+
this._requestListener(ws, undefined, 'on_connect');
|
|
68
|
+
|
|
67
69
|
ws.on("message", async (data) =>
|
|
68
70
|
{
|
|
69
71
|
const incomingMessage = typeof data === "string" ? data : data.toString("utf8");
|
|
@@ -73,6 +75,8 @@ module.exports = class HttpRunner extends events.EventEmitter
|
|
|
73
75
|
|
|
74
76
|
ws.on("close", (code, reason) =>
|
|
75
77
|
{
|
|
78
|
+
this._requestListener(ws, undefined, 'on_disconnect');
|
|
79
|
+
|
|
76
80
|
const reasonMessage = reason && reason.length ? reason.toString("utf8") : "";
|
|
77
81
|
|
|
78
82
|
this.emit("trace", `[disconnect] ${connectionId} (${code}${reasonMessage ? ` - ${reasonMessage}` : ""})`);
|
|
@@ -99,15 +103,22 @@ module.exports = class HttpRunner extends events.EventEmitter
|
|
|
99
103
|
if ('sPort' in options) this.sPort = options.sPort;
|
|
100
104
|
}
|
|
101
105
|
|
|
102
|
-
async _requestListener(ws, incomingMessage)
|
|
106
|
+
async _requestListener(ws, incomingMessage, customEventName)
|
|
103
107
|
{
|
|
104
108
|
try
|
|
105
109
|
{
|
|
106
110
|
let event = new Configurapi.Event(await WsAdapter.toRequest(ws, incomingMessage));
|
|
107
111
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
112
|
+
if(customEventName)
|
|
113
|
+
{
|
|
114
|
+
event.name = customEventName;
|
|
115
|
+
await this.service.process(event); // No response will be sent back to the caller for 'on_disconnect'
|
|
116
|
+
}
|
|
117
|
+
else
|
|
118
|
+
{
|
|
119
|
+
await this.service.process(event);
|
|
120
|
+
await WsAdapter.write(ws, event.response); // Send response back to the caller.
|
|
121
|
+
}
|
|
111
122
|
}
|
|
112
123
|
catch (error)
|
|
113
124
|
{
|
package/src/wsAdapter.js
CHANGED
|
@@ -138,7 +138,7 @@ module.exports = {
|
|
|
138
138
|
|
|
139
139
|
toRequest: async function(ws, incomingMessage)
|
|
140
140
|
{
|
|
141
|
-
let data = JSON.parse(incomingMessage);
|
|
141
|
+
let data = incomingMessage ? JSON.parse(incomingMessage) : {};
|
|
142
142
|
|
|
143
143
|
let request = new Configurapi.Request();
|
|
144
144
|
|
|
@@ -146,6 +146,7 @@ module.exports = {
|
|
|
146
146
|
request.headers = data.headers || {};
|
|
147
147
|
request.name = data.name || undefined;
|
|
148
148
|
request.query = data.query || {};
|
|
149
|
+
request.params = data.params || {};
|
|
149
150
|
request.payload = data.payload || undefined;
|
|
150
151
|
|
|
151
152
|
request.headers['connectionId'] = ws.connectionId;
|