@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.
Files changed (3) hide show
  1. package/index.cjs +175 -1
  2. package/index.mjs +165 -1
  3. package/package.json +3 -4
package/index.cjs CHANGED
@@ -1 +1,175 @@
1
- "use strict";var e=Object.defineProperty,r=Object.defineProperties,t=Object.getOwnPropertyDescriptors,a=Object.getOwnPropertySymbols,n=Object.prototype.hasOwnProperty,o=Object.prototype.propertyIsEnumerable,s=(r,t,a)=>t in r?e(r,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):r[t]=a,i=(e,r)=>{for(var t in r||(r={}))n.call(r,t)&&s(e,t,r[t]);if(a)for(var t of a(r))o.call(r,t)&&s(e,t,r[t]);return e},c=(e,a)=>r(e,t(a));Object.defineProperty(exports,"__esModule",{value:!0}),exports[Symbol.toStringTag]="Module";var f=require("url"),d=require("@graffy/common"),l=require("debug"),u=require("ws"),p=require("@graffy/testing");function g(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var y=g(f),w=g(l),m=g(u);const h=w.default("graffy:server:http");const v=w.default("graffy:server:ws");exports.httpServer=function(e){if(!e)throw new Error("server.store_undef");return async(r,t)=>{const a=y.default.parse(r.url,!0),n=a.query.q&&d.decodeUrl(a.query.q),o=a.query.opts&&d.deserialize(decodeURIComponent(a.query.opts));if("GET"===r.method)try{if("text/event-stream"!==r.headers.accept)throw Error("httpServer.get_unsupported");{t.setHeader("content-type","text/event-stream");const a=setInterval((()=>{r.aborted||t.finished?clearInterval(a):t.write(": \n\n")}),29e3);try{const a=e.call("watch",n,c(i({},o),{raw:!0}));for await(const e of a){if(r.aborted||t.finished)break;t.write(`data: ${d.serialize(e)}\n\n`)}}catch(s){h(s),t.write(`event: graffyerror\ndata: ${s.message}\n\n`)}t.end()}}catch(s){h(s),t.writeHead(400),t.end(`${s.message}`)}else if("POST"===r.method)try{const a=r.query.op;if("write"!==a&&"read"!==a)throw Error("httpServer.unsupported_op: "+a);const n=[];for await(const e of r)n.push(e);const s=d.deserialize(Buffer.concat(n).toString()),i=await e.call(a,s,o);t.writeHead(200),t.end(d.serialize(i))}catch(s){t.writeHead(400),t.end(`${s.message}`)}else t.writeHead(501),t.end("Not implemented")}},exports.wsServer=function(e){if(!e)throw new Error("server.store_undef");const r=new m.default.Server({noServer:!0});return r.on("connection",(function(r){r.graffyStreams={},r.on("message",(async function(t){try{const[n,o,s,f]=d.deserialize(t);if(":pong"===n)return void(r.pingPending=!1);switch(o){case"read":case"write":try{const t=await e.call(o,s,f);r.send(d.serialize([n,null,t]))}catch(a){v(o+"error:"+a.message+" "+p.format(s)),r.send(d.serialize([n,a.message]))}break;case"watch":try{const t=e.call("watch",s,c(i({},f),{raw:!0}));r.graffyStreams[n]=t;for await(const e of t)r.send(d.serialize([n,null,e]))}catch(a){v(o+"error:"+a.message+" "+p.format(s)),r.send(d.serialize([n,a.message]))}break;case"unwatch":if(!r.graffyStreams[n])break;r.graffyStreams[n].return(),delete r.graffyStreams[n]}}catch(n){r.close()}})),r.on("close",(()=>{for(const e in r.graffyStreams)r.graffyStreams[e].return(),delete r.graffyStreams[e]}))})),setInterval((function(){r.clients.forEach((function(e){if(e.pingPending)return e.terminate();e.pingPending=!0,e.send(d.serialize([":ping",Date.now()]))}))}),3e4),async(e,t,a)=>{r.handleUpgrade(e,t,a,(function(t){r.emit("connection",t,e)}))}};
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 e=Object.defineProperty,r=Object.defineProperties,t=Object.getOwnPropertyDescriptors,n=Object.getOwnPropertySymbols,a=Object.prototype.hasOwnProperty,o=Object.prototype.propertyIsEnumerable,s=(r,t,n)=>t in r?e(r,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):r[t]=n,c=(e,r)=>{for(var t in r||(r={}))a.call(r,t)&&s(e,t,r[t]);if(n)for(var t of n(r))o.call(r,t)&&s(e,t,r[t]);return e},f=(e,n)=>r(e,t(n));import i from"url";import{decodeUrl as d,deserialize as p,serialize as m}from"@graffy/common";import g from"debug";import l from"ws";import{format as u}from"@graffy/testing";const w=g("graffy:server:http");function y(e){if(!e)throw new Error("server.store_undef");return async(r,t)=>{const n=i.parse(r.url,!0),a=n.query.q&&d(n.query.q),o=n.query.opts&&p(decodeURIComponent(n.query.opts));if("GET"===r.method)try{if("text/event-stream"!==r.headers.accept)throw Error("httpServer.get_unsupported");{t.setHeader("content-type","text/event-stream");const n=setInterval((()=>{r.aborted||t.finished?clearInterval(n):t.write(": \n\n")}),29e3);try{const n=e.call("watch",a,f(c({},o),{raw:!0}));for await(const e of n){if(r.aborted||t.finished)break;t.write(`data: ${m(e)}\n\n`)}}catch(s){w(s),t.write(`event: graffyerror\ndata: ${s.message}\n\n`)}t.end()}}catch(s){w(s),t.writeHead(400),t.end(`${s.message}`)}else if("POST"===r.method)try{const n=r.query.op;if("write"!==n&&"read"!==n)throw Error("httpServer.unsupported_op: "+n);const a=[];for await(const e of r)a.push(e);const s=p(Buffer.concat(a).toString()),c=await e.call(n,s,o);t.writeHead(200),t.end(m(c))}catch(s){t.writeHead(400),t.end(`${s.message}`)}else t.writeHead(501),t.end("Not implemented")}}const h=g("graffy:server:ws");function v(e){if(!e)throw new Error("server.store_undef");const r=new l.Server({noServer:!0});return r.on("connection",(function(r){r.graffyStreams={},r.on("message",(async function(t){try{const[a,o,s,i]=p(t);if(":pong"===a)return void(r.pingPending=!1);switch(o){case"read":case"write":try{const t=await e.call(o,s,i);r.send(m([a,null,t]))}catch(n){h(o+"error:"+n.message+" "+u(s)),r.send(m([a,n.message]))}break;case"watch":try{const t=e.call("watch",s,f(c({},i),{raw:!0}));r.graffyStreams[a]=t;for await(const e of t)r.send(m([a,null,e]))}catch(n){h(o+"error:"+n.message+" "+u(s)),r.send(m([a,n.message]))}break;case"unwatch":if(!r.graffyStreams[a])break;r.graffyStreams[a].return(),delete r.graffyStreams[a]}}catch(a){r.close()}})),r.on("close",(()=>{for(const e in r.graffyStreams)r.graffyStreams[e].return(),delete r.graffyStreams[e]}))})),setInterval((function(){r.clients.forEach((function(e){if(e.pingPending)return e.terminate();e.pingPending=!0,e.send(m([":ping",Date.now()]))}))}),3e4),async(e,t,n)=>{r.handleUpgrade(e,t,n,(function(t){r.emit("connection",t,e)}))}}export{y as httpServer,v as wsServer};
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.8-alpha.5",
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/testing": "0.15.8-alpha.5"
20
+ "@graffy/common": "0.15.10-alpha.2",
21
+ "debug": "^4.3.2"
23
22
  }
24
23
  }