@graffy/server 0.16.0-alpha.5 → 0.16.0-alpha.6

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 (3) hide show
  1. package/index.cjs +21 -13
  2. package/index.mjs +22 -14
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -13,9 +13,11 @@ function server$1(store, { auth } = {}) {
13
13
  throw new Error("server.store_undef");
14
14
  return async (req, res) => {
15
15
  const parsed = url__default.default.parse(req.url, true);
16
- const options = parsed.query.opts && !Array.isArray(parsed.query.opts) && JSON.parse(decodeURIComponent(parsed.query.opts)) || void 0;
16
+ const optParam = parsed.query.opts && String(parsed.query.opts);
17
+ const options = optParam && JSON.parse(decodeURIComponent(optParam));
17
18
  if (req.method === "GET") {
18
- const query = parsed.query.q && common.deserialize(decodeURIComponent(parsed.query.q));
19
+ const qParam = parsed.query.q && String(parsed.query.q);
20
+ const query = qParam && common.unpack(JSON.parse(decodeURIComponent(qParam)));
19
21
  try {
20
22
  if (req.headers["accept"] === "text/event-stream") {
21
23
  res.setHeader("content-type", "text/event-stream");
@@ -34,7 +36,7 @@ function server$1(store, { auth } = {}) {
34
36
  for await (const value of stream) {
35
37
  if (req.aborted || res.finished)
36
38
  break;
37
- res.write(`data: ${common.serialize(value)}
39
+ res.write(`data: ${JSON.stringify(common.pack(value))}
38
40
 
39
41
  `);
40
42
  }
@@ -64,15 +66,19 @@ data: ${e.message}
64
66
  const chunks = [];
65
67
  for await (const chunk of req)
66
68
  chunks.push(chunk);
67
- const payload = common.deserialize(Buffer.concat(chunks).toString());
68
- if (auth && !await auth(op, payload, options)) {
69
+ const payload = common.unpack(JSON.parse(Buffer.concat(chunks).toString()));
70
+ if (auth && !await auth(
71
+ op,
72
+ (op === "write" ? common.encodeGraph : common.encodeQuery)(payload),
73
+ options
74
+ )) {
69
75
  res.writeHead(401);
70
76
  res.end("unauthorized");
71
77
  return;
72
78
  }
73
79
  const value = await store.call(op, payload, options);
74
80
  res.writeHead(200);
75
- res.end(common.serialize(value));
81
+ res.end(JSON.stringify(common.pack(value)));
76
82
  } catch (e) {
77
83
  log$1(e.message);
78
84
  log$1(e.stack);
@@ -95,7 +101,8 @@ function server(store) {
95
101
  ws2.graffyStreams = {};
96
102
  ws2.on("message", async function message(msg) {
97
103
  try {
98
- const [id, op, payload, options] = common.deserialize(msg);
104
+ const [id, op, packedPayload, options] = JSON.parse(msg);
105
+ const payload = common.unpack(packedPayload);
99
106
  if (id === ":pong") {
100
107
  ws2.pingPending = false;
101
108
  return;
@@ -105,10 +112,10 @@ function server(store) {
105
112
  case "write":
106
113
  try {
107
114
  const result = await store.call(op, payload, options);
108
- ws2.send([id, null, common.serialize(result)].join(":"));
115
+ ws2.send(JSON.stringify([id, null, common.pack(result)]));
109
116
  } catch (e) {
110
117
  log(op + "error:" + e.message + " " + payload);
111
- ws2.send([id, e.message].join(":"));
118
+ ws2.send(JSON.stringify([id, e.message]));
112
119
  }
113
120
  break;
114
121
  case "watch":
@@ -119,11 +126,11 @@ function server(store) {
119
126
  });
120
127
  ws2.graffyStreams[id] = stream;
121
128
  for await (const value of stream) {
122
- ws2.send([id, null, common.serialize(value)].join(":"));
129
+ ws2.send(JSON.stringify([id, null, common.pack(value)]));
123
130
  }
124
131
  } catch (e) {
125
132
  log(op + "error:" + e.message + " " + payload);
126
- ws2.send(common.serialize([id, e.message]));
133
+ ws2.send(JSON.stringify([id, e.message]));
127
134
  }
128
135
  break;
129
136
  case "unwatch":
@@ -133,7 +140,8 @@ function server(store) {
133
140
  delete ws2.graffyStreams[id];
134
141
  break;
135
142
  }
136
- } catch (_) {
143
+ } catch (e) {
144
+ log("Closing socket due to error: " + e.message);
137
145
  ws2.close();
138
146
  }
139
147
  });
@@ -149,7 +157,7 @@ function server(store) {
149
157
  if (ws2.pingPending)
150
158
  return ws2.terminate();
151
159
  ws2.pingPending = true;
152
- ws2.send(common.serialize([":ping", Date.now()]));
160
+ ws2.send(JSON.stringify([":ping", Date.now()]));
153
161
  });
154
162
  }, PING_INTERVAL);
155
163
  return async (request, socket, head) => {
package/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import url from "url";
2
- import { deserialize, serialize } from "@graffy/common";
2
+ import { unpack, pack, encodeGraph, encodeQuery } from "@graffy/common";
3
3
  import debug from "debug";
4
4
  import { WebSocketServer } from "ws";
5
5
  const log$1 = debug("graffy:server:http");
@@ -8,9 +8,11 @@ function server$1(store, { auth } = {}) {
8
8
  throw new Error("server.store_undef");
9
9
  return async (req, res) => {
10
10
  const parsed = url.parse(req.url, true);
11
- const options = parsed.query.opts && !Array.isArray(parsed.query.opts) && JSON.parse(decodeURIComponent(parsed.query.opts)) || void 0;
11
+ const optParam = parsed.query.opts && String(parsed.query.opts);
12
+ const options = optParam && JSON.parse(decodeURIComponent(optParam));
12
13
  if (req.method === "GET") {
13
- const query = parsed.query.q && deserialize(decodeURIComponent(parsed.query.q));
14
+ const qParam = parsed.query.q && String(parsed.query.q);
15
+ const query = qParam && unpack(JSON.parse(decodeURIComponent(qParam)));
14
16
  try {
15
17
  if (req.headers["accept"] === "text/event-stream") {
16
18
  res.setHeader("content-type", "text/event-stream");
@@ -29,7 +31,7 @@ function server$1(store, { auth } = {}) {
29
31
  for await (const value of stream) {
30
32
  if (req.aborted || res.finished)
31
33
  break;
32
- res.write(`data: ${serialize(value)}
34
+ res.write(`data: ${JSON.stringify(pack(value))}
33
35
 
34
36
  `);
35
37
  }
@@ -59,15 +61,19 @@ data: ${e.message}
59
61
  const chunks = [];
60
62
  for await (const chunk of req)
61
63
  chunks.push(chunk);
62
- const payload = deserialize(Buffer.concat(chunks).toString());
63
- if (auth && !await auth(op, payload, options)) {
64
+ const payload = unpack(JSON.parse(Buffer.concat(chunks).toString()));
65
+ if (auth && !await auth(
66
+ op,
67
+ (op === "write" ? encodeGraph : encodeQuery)(payload),
68
+ options
69
+ )) {
64
70
  res.writeHead(401);
65
71
  res.end("unauthorized");
66
72
  return;
67
73
  }
68
74
  const value = await store.call(op, payload, options);
69
75
  res.writeHead(200);
70
- res.end(serialize(value));
76
+ res.end(JSON.stringify(pack(value)));
71
77
  } catch (e) {
72
78
  log$1(e.message);
73
79
  log$1(e.stack);
@@ -90,7 +96,8 @@ function server(store) {
90
96
  ws.graffyStreams = {};
91
97
  ws.on("message", async function message(msg) {
92
98
  try {
93
- const [id, op, payload, options] = deserialize(msg);
99
+ const [id, op, packedPayload, options] = JSON.parse(msg);
100
+ const payload = unpack(packedPayload);
94
101
  if (id === ":pong") {
95
102
  ws.pingPending = false;
96
103
  return;
@@ -100,10 +107,10 @@ function server(store) {
100
107
  case "write":
101
108
  try {
102
109
  const result = await store.call(op, payload, options);
103
- ws.send([id, null, serialize(result)].join(":"));
110
+ ws.send(JSON.stringify([id, null, pack(result)]));
104
111
  } catch (e) {
105
112
  log(op + "error:" + e.message + " " + payload);
106
- ws.send([id, e.message].join(":"));
113
+ ws.send(JSON.stringify([id, e.message]));
107
114
  }
108
115
  break;
109
116
  case "watch":
@@ -114,11 +121,11 @@ function server(store) {
114
121
  });
115
122
  ws.graffyStreams[id] = stream;
116
123
  for await (const value of stream) {
117
- ws.send([id, null, serialize(value)].join(":"));
124
+ ws.send(JSON.stringify([id, null, pack(value)]));
118
125
  }
119
126
  } catch (e) {
120
127
  log(op + "error:" + e.message + " " + payload);
121
- ws.send(serialize([id, e.message]));
128
+ ws.send(JSON.stringify([id, e.message]));
122
129
  }
123
130
  break;
124
131
  case "unwatch":
@@ -128,7 +135,8 @@ function server(store) {
128
135
  delete ws.graffyStreams[id];
129
136
  break;
130
137
  }
131
- } catch (_) {
138
+ } catch (e) {
139
+ log("Closing socket due to error: " + e.message);
132
140
  ws.close();
133
141
  }
134
142
  });
@@ -144,7 +152,7 @@ function server(store) {
144
152
  if (ws.pingPending)
145
153
  return ws.terminate();
146
154
  ws.pingPending = true;
147
- ws.send(serialize([":ping", Date.now()]));
155
+ ws.send(JSON.stringify([":ping", Date.now()]));
148
156
  });
149
157
  }, PING_INTERVAL);
150
158
  return async (request, socket, head) => {
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.16.0-alpha.5",
5
+ "version": "0.16.0-alpha.6",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
- "@graffy/common": "0.16.0-alpha.5",
19
+ "@graffy/common": "0.16.0-alpha.6",
20
20
  "debug": "^4.3.3",
21
21
  "ws": "^8.4.2"
22
22
  }