@graffy/server 0.15.25-alpha.3 → 0.15.25-alpha.5

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