@graffy/server 0.15.15-alpha.2 → 0.15.17
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/index.cjs +26 -27
- package/index.mjs +2 -2
- package/package.json +4 -4
package/index.cjs
CHANGED
|
@@ -23,13 +23,12 @@ exports[Symbol.toStringTag] = "Module";
|
|
|
23
23
|
var url = require("url");
|
|
24
24
|
var common = require("@graffy/common");
|
|
25
25
|
var debug = require("debug");
|
|
26
|
-
var
|
|
26
|
+
var ws = require("ws");
|
|
27
27
|
function _interopDefaultLegacy(e) {
|
|
28
28
|
return e && typeof e === "object" && "default" in e ? e : { "default": e };
|
|
29
29
|
}
|
|
30
30
|
var url__default = /* @__PURE__ */ _interopDefaultLegacy(url);
|
|
31
31
|
var debug__default = /* @__PURE__ */ _interopDefaultLegacy(debug);
|
|
32
|
-
var WebSocket__default = /* @__PURE__ */ _interopDefaultLegacy(WebSocket);
|
|
33
32
|
const log$1 = debug__default["default"]("graffy:server:http");
|
|
34
33
|
function server$1(store) {
|
|
35
34
|
if (!store)
|
|
@@ -107,14 +106,14 @@ const PING_INTERVAL = 3e4;
|
|
|
107
106
|
function server(store) {
|
|
108
107
|
if (!store)
|
|
109
108
|
throw new Error("server.store_undef");
|
|
110
|
-
const wss = new
|
|
111
|
-
wss.on("connection", function connection(
|
|
112
|
-
|
|
113
|
-
|
|
109
|
+
const wss = new ws.WebSocketServer({ noServer: true });
|
|
110
|
+
wss.on("connection", function connection(ws2) {
|
|
111
|
+
ws2.graffyStreams = {};
|
|
112
|
+
ws2.on("message", async function message(msg) {
|
|
114
113
|
try {
|
|
115
114
|
const [id, op, payload, options] = common.deserialize(msg);
|
|
116
115
|
if (id === ":pong") {
|
|
117
|
-
|
|
116
|
+
ws2.pingPending = false;
|
|
118
117
|
return;
|
|
119
118
|
}
|
|
120
119
|
switch (op) {
|
|
@@ -122,10 +121,10 @@ function server(store) {
|
|
|
122
121
|
case "write":
|
|
123
122
|
try {
|
|
124
123
|
const result = await store.call(op, payload, options);
|
|
125
|
-
|
|
124
|
+
ws2.send(common.serialize([id, null, result]));
|
|
126
125
|
} catch (e) {
|
|
127
126
|
log(op + "error:" + e.message + " " + payload);
|
|
128
|
-
|
|
127
|
+
ws2.send(common.serialize([id, e.message]));
|
|
129
128
|
}
|
|
130
129
|
break;
|
|
131
130
|
case "watch":
|
|
@@ -133,44 +132,44 @@ function server(store) {
|
|
|
133
132
|
const stream = store.call("watch", payload, __spreadProps(__spreadValues({}, options), {
|
|
134
133
|
raw: true
|
|
135
134
|
}));
|
|
136
|
-
|
|
135
|
+
ws2.graffyStreams[id] = stream;
|
|
137
136
|
for await (const value of stream) {
|
|
138
|
-
|
|
137
|
+
ws2.send(common.serialize([id, null, value]));
|
|
139
138
|
}
|
|
140
139
|
} catch (e) {
|
|
141
140
|
log(op + "error:" + e.message + " " + payload);
|
|
142
|
-
|
|
141
|
+
ws2.send(common.serialize([id, e.message]));
|
|
143
142
|
}
|
|
144
143
|
break;
|
|
145
144
|
case "unwatch":
|
|
146
|
-
if (!
|
|
145
|
+
if (!ws2.graffyStreams[id])
|
|
147
146
|
break;
|
|
148
|
-
|
|
149
|
-
delete
|
|
147
|
+
ws2.graffyStreams[id].return();
|
|
148
|
+
delete ws2.graffyStreams[id];
|
|
150
149
|
break;
|
|
151
150
|
}
|
|
152
151
|
} catch (_) {
|
|
153
|
-
|
|
152
|
+
ws2.close();
|
|
154
153
|
}
|
|
155
154
|
});
|
|
156
|
-
|
|
157
|
-
for (const id in
|
|
158
|
-
|
|
159
|
-
delete
|
|
155
|
+
ws2.on("close", () => {
|
|
156
|
+
for (const id in ws2.graffyStreams) {
|
|
157
|
+
ws2.graffyStreams[id].return();
|
|
158
|
+
delete ws2.graffyStreams[id];
|
|
160
159
|
}
|
|
161
160
|
});
|
|
162
161
|
});
|
|
163
162
|
setInterval(function ping() {
|
|
164
|
-
wss.clients.forEach(function each(
|
|
165
|
-
if (
|
|
166
|
-
return
|
|
167
|
-
|
|
168
|
-
|
|
163
|
+
wss.clients.forEach(function each(ws2) {
|
|
164
|
+
if (ws2.pingPending)
|
|
165
|
+
return ws2.terminate();
|
|
166
|
+
ws2.pingPending = true;
|
|
167
|
+
ws2.send(common.serialize([":ping", Date.now()]));
|
|
169
168
|
});
|
|
170
169
|
}, PING_INTERVAL);
|
|
171
170
|
return async (request, socket, head) => {
|
|
172
|
-
wss.handleUpgrade(request, socket, head, function done(
|
|
173
|
-
wss.emit("connection",
|
|
171
|
+
wss.handleUpgrade(request, socket, head, function done(ws2) {
|
|
172
|
+
wss.emit("connection", ws2, request);
|
|
174
173
|
});
|
|
175
174
|
};
|
|
176
175
|
}
|
package/index.mjs
CHANGED
|
@@ -20,7 +20,7 @@ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
|
20
20
|
import url from "url";
|
|
21
21
|
import { decodeUrl, deserialize, serialize } from "@graffy/common";
|
|
22
22
|
import debug from "debug";
|
|
23
|
-
import
|
|
23
|
+
import { WebSocketServer } from "ws";
|
|
24
24
|
const log$1 = debug("graffy:server:http");
|
|
25
25
|
function server$1(store) {
|
|
26
26
|
if (!store)
|
|
@@ -98,7 +98,7 @@ const PING_INTERVAL = 3e4;
|
|
|
98
98
|
function server(store) {
|
|
99
99
|
if (!store)
|
|
100
100
|
throw new Error("server.store_undef");
|
|
101
|
-
const wss = new
|
|
101
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
102
102
|
wss.on("connection", function connection(ws) {
|
|
103
103
|
ws.graffyStreams = {};
|
|
104
104
|
ws.on("message", async function message(msg) {
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graffy/server",
|
|
3
3
|
"description": "Node.js library for building an API for a Graffy store.",
|
|
4
4
|
"author": "aravind (https://github.com/aravindet)",
|
|
5
|
-
"version": "0.15.
|
|
5
|
+
"version": "0.15.17",
|
|
6
6
|
"main": "./index.cjs",
|
|
7
7
|
"exports": {
|
|
8
8
|
"import": "./index.mjs",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
},
|
|
17
17
|
"license": "Apache-2.0",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
19
|
+
"ws": "^8.4.2",
|
|
20
|
+
"@graffy/common": "0.15.17",
|
|
21
|
+
"debug": "^4.3.3"
|
|
22
22
|
}
|
|
23
23
|
}
|