@graffy/server 0.15.17 → 0.15.19-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/Readme.md CHANGED
@@ -4,4 +4,4 @@ Node.js library for building an API for a Graffy store.
4
4
 
5
5
  This creates an HTTP request handler for serving an API that is compatible with Graffy Client. The request handler can be used with the Node.js built-in http, https and http2 `createServer()` and with Express.js.
6
6
 
7
- See [Graffy documentation](https://aravindet.github.io/graffy/#/GraffyServer) for more.
7
+ See [Graffy documentation](https://graffy.org#/GraffyServer) for more.
package/index.cjs CHANGED
@@ -30,14 +30,14 @@ function _interopDefaultLegacy(e) {
30
30
  var url__default = /* @__PURE__ */ _interopDefaultLegacy(url);
31
31
  var debug__default = /* @__PURE__ */ _interopDefaultLegacy(debug);
32
32
  const log$1 = debug__default["default"]("graffy:server:http");
33
- function server$1(store) {
33
+ function server$1(store, { auth } = {}) {
34
34
  if (!store)
35
35
  throw new Error("server.store_undef");
36
36
  return async (req, res) => {
37
37
  const parsed = url__default["default"].parse(req.url, true);
38
- const query = parsed.query.q && common.decodeUrl(parsed.query.q);
39
38
  const options = parsed.query.opts && !Array.isArray(parsed.query.opts) && common.deserialize(decodeURIComponent(parsed.query.opts));
40
39
  if (req.method === "GET") {
40
+ const query = parsed.query.q && common.decodeUrl(parsed.query.q);
41
41
  try {
42
42
  if (req.headers["accept"] === "text/event-stream") {
43
43
  res.setHeader("content-type", "text/event-stream");
@@ -78,7 +78,7 @@ data: ${e.message}
78
78
  }
79
79
  } else if (req.method === "POST") {
80
80
  try {
81
- const op = req.query.op;
81
+ const op = parsed.query.op;
82
82
  if (op !== "write" && op !== "read") {
83
83
  throw Error("httpServer.unsupported_op: " + op);
84
84
  }
@@ -86,6 +86,11 @@ data: ${e.message}
86
86
  for await (const chunk of req)
87
87
  chunks.push(chunk);
88
88
  const payload = common.deserialize(Buffer.concat(chunks).toString());
89
+ if (auth && !await auth(op, payload, options)) {
90
+ res.writeHead(401);
91
+ res.end("unauthorized");
92
+ return;
93
+ }
89
94
  const value = await store.call(op, payload, options);
90
95
  res.writeHead(200);
91
96
  res.end(common.serialize(value));
package/index.mjs CHANGED
@@ -18,18 +18,18 @@ var __spreadValues = (a, b) => {
18
18
  };
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
  import url from "url";
21
- import { decodeUrl, deserialize, serialize } from "@graffy/common";
21
+ import { deserialize, decodeUrl, serialize } from "@graffy/common";
22
22
  import debug from "debug";
23
23
  import { WebSocketServer } from "ws";
24
24
  const log$1 = debug("graffy:server:http");
25
- function server$1(store) {
25
+ function server$1(store, { auth } = {}) {
26
26
  if (!store)
27
27
  throw new Error("server.store_undef");
28
28
  return async (req, res) => {
29
29
  const parsed = url.parse(req.url, true);
30
- const query = parsed.query.q && decodeUrl(parsed.query.q);
31
30
  const options = parsed.query.opts && !Array.isArray(parsed.query.opts) && deserialize(decodeURIComponent(parsed.query.opts));
32
31
  if (req.method === "GET") {
32
+ const query = parsed.query.q && decodeUrl(parsed.query.q);
33
33
  try {
34
34
  if (req.headers["accept"] === "text/event-stream") {
35
35
  res.setHeader("content-type", "text/event-stream");
@@ -70,7 +70,7 @@ data: ${e.message}
70
70
  }
71
71
  } else if (req.method === "POST") {
72
72
  try {
73
- const op = req.query.op;
73
+ const op = parsed.query.op;
74
74
  if (op !== "write" && op !== "read") {
75
75
  throw Error("httpServer.unsupported_op: " + op);
76
76
  }
@@ -78,6 +78,11 @@ data: ${e.message}
78
78
  for await (const chunk of req)
79
79
  chunks.push(chunk);
80
80
  const payload = deserialize(Buffer.concat(chunks).toString());
81
+ if (auth && !await auth(op, payload, options)) {
82
+ res.writeHead(401);
83
+ res.end("unauthorized");
84
+ return;
85
+ }
81
86
  const value = await store.call(op, payload, options);
82
87
  res.writeHead(200);
83
88
  res.end(serialize(value));
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.17",
5
+ "version": "0.15.19-alpha.2",
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
- "ws": "^8.4.2",
20
- "@graffy/common": "0.15.17",
21
- "debug": "^4.3.3"
19
+ "@graffy/common": "0.15.19-alpha.2",
20
+ "debug": "^4.3.3",
21
+ "ws": "^8.4.2"
22
22
  }
23
23
  }
@@ -1 +1,12 @@
1
- export default function server(store: any): (req: any, res: any) => Promise<void>;
1
+ /**
2
+ * @typedef {import('@graffy/core').default} GraffyStore
3
+ * @param {GraffyStore} store
4
+ * @param {{
5
+ * auth?: (operation: string, payload: any, options: any) => Promise<boolean>
6
+ * } | undefined} options
7
+ * @returns
8
+ */
9
+ export default function server(store: any, { auth }?: {
10
+ auth?: (operation: string, payload: any, options: any) => Promise<boolean>;
11
+ }): (req: any, res: any) => Promise<void>;
12
+ export type GraffyStore = any;