@graffy/server 0.15.8-alpha.5 → 0.15.10-alpha.2
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 +175 -1
- package/index.mjs +165 -1
- package/package.json +3 -4
package/index.cjs
CHANGED
|
@@ -1 +1,175 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp.call(b, prop))
|
|
12
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols)
|
|
14
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
+
if (__propIsEnum.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
20
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports[Symbol.toStringTag] = "Module";
|
|
23
|
+
var url = require("url");
|
|
24
|
+
var common = require("@graffy/common");
|
|
25
|
+
var debug = require("debug");
|
|
26
|
+
var WebSocket = require("ws");
|
|
27
|
+
function _interopDefaultLegacy(e) {
|
|
28
|
+
return e && typeof e === "object" && "default" in e ? e : { "default": e };
|
|
29
|
+
}
|
|
30
|
+
var url__default = /* @__PURE__ */ _interopDefaultLegacy(url);
|
|
31
|
+
var debug__default = /* @__PURE__ */ _interopDefaultLegacy(debug);
|
|
32
|
+
var WebSocket__default = /* @__PURE__ */ _interopDefaultLegacy(WebSocket);
|
|
33
|
+
const log$1 = debug__default["default"]("graffy:server:http");
|
|
34
|
+
function server$1(store) {
|
|
35
|
+
if (!store)
|
|
36
|
+
throw new Error("server.store_undef");
|
|
37
|
+
return async (req, res) => {
|
|
38
|
+
const parsed = url__default["default"].parse(req.url, true);
|
|
39
|
+
const query = parsed.query.q && common.decodeUrl(parsed.query.q);
|
|
40
|
+
const options = parsed.query.opts && common.deserialize(decodeURIComponent(parsed.query.opts));
|
|
41
|
+
if (req.method === "GET") {
|
|
42
|
+
try {
|
|
43
|
+
if (req.headers["accept"] === "text/event-stream") {
|
|
44
|
+
res.setHeader("content-type", "text/event-stream");
|
|
45
|
+
const keepAlive = setInterval(() => {
|
|
46
|
+
if (req.aborted || res.finished) {
|
|
47
|
+
clearInterval(keepAlive);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
res.write(": \n\n");
|
|
51
|
+
}, 29e3);
|
|
52
|
+
try {
|
|
53
|
+
const stream = store.call("watch", query, __spreadProps(__spreadValues({}, options), {
|
|
54
|
+
raw: true
|
|
55
|
+
}));
|
|
56
|
+
for await (const value of stream) {
|
|
57
|
+
if (req.aborted || res.finished)
|
|
58
|
+
break;
|
|
59
|
+
res.write(`data: ${common.serialize(value)}
|
|
60
|
+
|
|
61
|
+
`);
|
|
62
|
+
}
|
|
63
|
+
} catch (e) {
|
|
64
|
+
log$1(e);
|
|
65
|
+
res.write(`event: graffyerror
|
|
66
|
+
data: ${e.message}
|
|
67
|
+
|
|
68
|
+
`);
|
|
69
|
+
}
|
|
70
|
+
res.end();
|
|
71
|
+
} else {
|
|
72
|
+
throw Error("httpServer.get_unsupported");
|
|
73
|
+
}
|
|
74
|
+
} catch (e) {
|
|
75
|
+
log$1(e);
|
|
76
|
+
res.writeHead(400);
|
|
77
|
+
res.end(`${e.message}`);
|
|
78
|
+
}
|
|
79
|
+
} else if (req.method === "POST") {
|
|
80
|
+
try {
|
|
81
|
+
const op = req.query.op;
|
|
82
|
+
if (op !== "write" && op !== "read") {
|
|
83
|
+
throw Error("httpServer.unsupported_op: " + op);
|
|
84
|
+
}
|
|
85
|
+
const chunks = [];
|
|
86
|
+
for await (const chunk of req)
|
|
87
|
+
chunks.push(chunk);
|
|
88
|
+
const payload = common.deserialize(Buffer.concat(chunks).toString());
|
|
89
|
+
const value = await store.call(op, payload, options);
|
|
90
|
+
res.writeHead(200);
|
|
91
|
+
res.end(common.serialize(value));
|
|
92
|
+
} catch (e) {
|
|
93
|
+
res.writeHead(400);
|
|
94
|
+
res.end(`${e.message}`);
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
res.writeHead(501);
|
|
98
|
+
res.end("Not implemented");
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const log = debug__default["default"]("graffy:server:ws");
|
|
103
|
+
const PING_INTERVAL = 3e4;
|
|
104
|
+
function server(store) {
|
|
105
|
+
if (!store)
|
|
106
|
+
throw new Error("server.store_undef");
|
|
107
|
+
const wss = new WebSocket__default["default"].Server({ noServer: true });
|
|
108
|
+
wss.on("connection", function connection(ws) {
|
|
109
|
+
ws.graffyStreams = {};
|
|
110
|
+
ws.on("message", async function message(msg) {
|
|
111
|
+
try {
|
|
112
|
+
const [id, op, payload, options] = common.deserialize(msg);
|
|
113
|
+
if (id === ":pong") {
|
|
114
|
+
ws.pingPending = false;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
switch (op) {
|
|
118
|
+
case "read":
|
|
119
|
+
case "write":
|
|
120
|
+
try {
|
|
121
|
+
const result = await store.call(op, payload, options);
|
|
122
|
+
ws.send(common.serialize([id, null, result]));
|
|
123
|
+
} catch (e) {
|
|
124
|
+
log(op + "error:" + e.message + " " + payload);
|
|
125
|
+
ws.send(common.serialize([id, e.message]));
|
|
126
|
+
}
|
|
127
|
+
break;
|
|
128
|
+
case "watch":
|
|
129
|
+
try {
|
|
130
|
+
const stream = store.call("watch", payload, __spreadProps(__spreadValues({}, options), {
|
|
131
|
+
raw: true
|
|
132
|
+
}));
|
|
133
|
+
ws.graffyStreams[id] = stream;
|
|
134
|
+
for await (const value of stream) {
|
|
135
|
+
ws.send(common.serialize([id, null, value]));
|
|
136
|
+
}
|
|
137
|
+
} catch (e) {
|
|
138
|
+
log(op + "error:" + e.message + " " + payload);
|
|
139
|
+
ws.send(common.serialize([id, e.message]));
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
case "unwatch":
|
|
143
|
+
if (!ws.graffyStreams[id])
|
|
144
|
+
break;
|
|
145
|
+
ws.graffyStreams[id].return();
|
|
146
|
+
delete ws.graffyStreams[id];
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
} catch (_) {
|
|
150
|
+
ws.close();
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
ws.on("close", () => {
|
|
154
|
+
for (const id in ws.graffyStreams) {
|
|
155
|
+
ws.graffyStreams[id].return();
|
|
156
|
+
delete ws.graffyStreams[id];
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
setInterval(function ping() {
|
|
161
|
+
wss.clients.forEach(function each(ws) {
|
|
162
|
+
if (ws.pingPending)
|
|
163
|
+
return ws.terminate();
|
|
164
|
+
ws.pingPending = true;
|
|
165
|
+
ws.send(common.serialize([":ping", Date.now()]));
|
|
166
|
+
});
|
|
167
|
+
}, PING_INTERVAL);
|
|
168
|
+
return async (request, socket, head) => {
|
|
169
|
+
wss.handleUpgrade(request, socket, head, function done(ws) {
|
|
170
|
+
wss.emit("connection", ws, request);
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
exports.httpServer = server$1;
|
|
175
|
+
exports.wsServer = server;
|
package/index.mjs
CHANGED
|
@@ -1 +1,165 @@
|
|
|
1
|
-
var
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import url from "url";
|
|
21
|
+
import { decodeUrl, deserialize, serialize } from "@graffy/common";
|
|
22
|
+
import debug from "debug";
|
|
23
|
+
import WebSocket from "ws";
|
|
24
|
+
const log$1 = debug("graffy:server:http");
|
|
25
|
+
function server$1(store) {
|
|
26
|
+
if (!store)
|
|
27
|
+
throw new Error("server.store_undef");
|
|
28
|
+
return async (req, res) => {
|
|
29
|
+
const parsed = url.parse(req.url, true);
|
|
30
|
+
const query = parsed.query.q && decodeUrl(parsed.query.q);
|
|
31
|
+
const options = parsed.query.opts && deserialize(decodeURIComponent(parsed.query.opts));
|
|
32
|
+
if (req.method === "GET") {
|
|
33
|
+
try {
|
|
34
|
+
if (req.headers["accept"] === "text/event-stream") {
|
|
35
|
+
res.setHeader("content-type", "text/event-stream");
|
|
36
|
+
const keepAlive = setInterval(() => {
|
|
37
|
+
if (req.aborted || res.finished) {
|
|
38
|
+
clearInterval(keepAlive);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
res.write(": \n\n");
|
|
42
|
+
}, 29e3);
|
|
43
|
+
try {
|
|
44
|
+
const stream = store.call("watch", query, __spreadProps(__spreadValues({}, options), {
|
|
45
|
+
raw: true
|
|
46
|
+
}));
|
|
47
|
+
for await (const value of stream) {
|
|
48
|
+
if (req.aborted || res.finished)
|
|
49
|
+
break;
|
|
50
|
+
res.write(`data: ${serialize(value)}
|
|
51
|
+
|
|
52
|
+
`);
|
|
53
|
+
}
|
|
54
|
+
} catch (e) {
|
|
55
|
+
log$1(e);
|
|
56
|
+
res.write(`event: graffyerror
|
|
57
|
+
data: ${e.message}
|
|
58
|
+
|
|
59
|
+
`);
|
|
60
|
+
}
|
|
61
|
+
res.end();
|
|
62
|
+
} else {
|
|
63
|
+
throw Error("httpServer.get_unsupported");
|
|
64
|
+
}
|
|
65
|
+
} catch (e) {
|
|
66
|
+
log$1(e);
|
|
67
|
+
res.writeHead(400);
|
|
68
|
+
res.end(`${e.message}`);
|
|
69
|
+
}
|
|
70
|
+
} else if (req.method === "POST") {
|
|
71
|
+
try {
|
|
72
|
+
const op = req.query.op;
|
|
73
|
+
if (op !== "write" && op !== "read") {
|
|
74
|
+
throw Error("httpServer.unsupported_op: " + op);
|
|
75
|
+
}
|
|
76
|
+
const chunks = [];
|
|
77
|
+
for await (const chunk of req)
|
|
78
|
+
chunks.push(chunk);
|
|
79
|
+
const payload = deserialize(Buffer.concat(chunks).toString());
|
|
80
|
+
const value = await store.call(op, payload, options);
|
|
81
|
+
res.writeHead(200);
|
|
82
|
+
res.end(serialize(value));
|
|
83
|
+
} catch (e) {
|
|
84
|
+
res.writeHead(400);
|
|
85
|
+
res.end(`${e.message}`);
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
res.writeHead(501);
|
|
89
|
+
res.end("Not implemented");
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const log = debug("graffy:server:ws");
|
|
94
|
+
const PING_INTERVAL = 3e4;
|
|
95
|
+
function server(store) {
|
|
96
|
+
if (!store)
|
|
97
|
+
throw new Error("server.store_undef");
|
|
98
|
+
const wss = new WebSocket.Server({ noServer: true });
|
|
99
|
+
wss.on("connection", function connection(ws) {
|
|
100
|
+
ws.graffyStreams = {};
|
|
101
|
+
ws.on("message", async function message(msg) {
|
|
102
|
+
try {
|
|
103
|
+
const [id, op, payload, options] = deserialize(msg);
|
|
104
|
+
if (id === ":pong") {
|
|
105
|
+
ws.pingPending = false;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
switch (op) {
|
|
109
|
+
case "read":
|
|
110
|
+
case "write":
|
|
111
|
+
try {
|
|
112
|
+
const result = await store.call(op, payload, options);
|
|
113
|
+
ws.send(serialize([id, null, result]));
|
|
114
|
+
} catch (e) {
|
|
115
|
+
log(op + "error:" + e.message + " " + payload);
|
|
116
|
+
ws.send(serialize([id, e.message]));
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
case "watch":
|
|
120
|
+
try {
|
|
121
|
+
const stream = store.call("watch", payload, __spreadProps(__spreadValues({}, options), {
|
|
122
|
+
raw: true
|
|
123
|
+
}));
|
|
124
|
+
ws.graffyStreams[id] = stream;
|
|
125
|
+
for await (const value of stream) {
|
|
126
|
+
ws.send(serialize([id, null, value]));
|
|
127
|
+
}
|
|
128
|
+
} catch (e) {
|
|
129
|
+
log(op + "error:" + e.message + " " + payload);
|
|
130
|
+
ws.send(serialize([id, e.message]));
|
|
131
|
+
}
|
|
132
|
+
break;
|
|
133
|
+
case "unwatch":
|
|
134
|
+
if (!ws.graffyStreams[id])
|
|
135
|
+
break;
|
|
136
|
+
ws.graffyStreams[id].return();
|
|
137
|
+
delete ws.graffyStreams[id];
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
} catch (_) {
|
|
141
|
+
ws.close();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
ws.on("close", () => {
|
|
145
|
+
for (const id in ws.graffyStreams) {
|
|
146
|
+
ws.graffyStreams[id].return();
|
|
147
|
+
delete ws.graffyStreams[id];
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
setInterval(function ping() {
|
|
152
|
+
wss.clients.forEach(function each(ws) {
|
|
153
|
+
if (ws.pingPending)
|
|
154
|
+
return ws.terminate();
|
|
155
|
+
ws.pingPending = true;
|
|
156
|
+
ws.send(serialize([":ping", Date.now()]));
|
|
157
|
+
});
|
|
158
|
+
}, PING_INTERVAL);
|
|
159
|
+
return async (request, socket, head) => {
|
|
160
|
+
wss.handleUpgrade(request, socket, head, function done(ws) {
|
|
161
|
+
wss.emit("connection", ws, request);
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
export { server$1 as httpServer, server as wsServer };
|
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.10-alpha.2",
|
|
6
6
|
"main": "./index.cjs",
|
|
7
7
|
"exports": {
|
|
8
8
|
"import": "./index.mjs",
|
|
@@ -16,9 +16,8 @@
|
|
|
16
16
|
},
|
|
17
17
|
"license": "Apache-2.0",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@graffy/common": "0.15.8-alpha.5",
|
|
20
|
-
"debug": "^4.3.2",
|
|
21
19
|
"ws": "^7.0.0",
|
|
22
|
-
"@graffy/
|
|
20
|
+
"@graffy/common": "0.15.10-alpha.2",
|
|
21
|
+
"debug": "^4.3.2"
|
|
23
22
|
}
|
|
24
23
|
}
|