@graffy/server 0.17.6 → 0.17.8-alpha.1
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 +17 -1
- package/index.mjs +18 -2
- package/package.json +2 -2
- package/types/wsServer.d.ts +12 -1
package/index.cjs
CHANGED
|
@@ -16,6 +16,15 @@ function server$1(store, { auth } = {}) {
|
|
|
16
16
|
const qParam = parsed.query.q && String(parsed.query.q);
|
|
17
17
|
const query = qParam && common.unpack(JSON.parse(decodeURIComponent(qParam)));
|
|
18
18
|
if (req.headers.accept === "text/event-stream") {
|
|
19
|
+
if (auth && !await auth("watch", common.decodeQuery(query), options)) {
|
|
20
|
+
const body = "unauthorized";
|
|
21
|
+
res.writeHead(401, {
|
|
22
|
+
"Content-Type": "text/plain",
|
|
23
|
+
"Content-Length": Buffer.byteLength(body)
|
|
24
|
+
});
|
|
25
|
+
res.end(body);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
19
28
|
res.setHeader("content-type", "text/event-stream");
|
|
20
29
|
const keepAlive = setInterval(() => {
|
|
21
30
|
if (req.aborted || res.finished) {
|
|
@@ -107,7 +116,7 @@ data: ${e.message}
|
|
|
107
116
|
}
|
|
108
117
|
const log = debug("graffy:server:ws");
|
|
109
118
|
const PING_INTERVAL = 3e4;
|
|
110
|
-
function server(store) {
|
|
119
|
+
function server(store, { auth } = {}) {
|
|
111
120
|
if (!store) throw new Error("server.store_undef");
|
|
112
121
|
const wss = new ws.WebSocketServer({ noServer: true });
|
|
113
122
|
wss.on("connection", function connection(ws2) {
|
|
@@ -120,6 +129,13 @@ function server(store) {
|
|
|
120
129
|
ws2.pingPending = false;
|
|
121
130
|
return;
|
|
122
131
|
}
|
|
132
|
+
if (auth && op !== "unwatch") {
|
|
133
|
+
const decoded = op === "write" ? common.decodeGraph(payload) : common.decodeQuery(payload);
|
|
134
|
+
if (!await auth(op, decoded, options)) {
|
|
135
|
+
ws2.send(JSON.stringify([id, "unauthorized"]));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
123
139
|
switch (op) {
|
|
124
140
|
case "read":
|
|
125
141
|
case "write":
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import url from "node:url";
|
|
2
|
-
import { unpack, pack, decodeGraph
|
|
2
|
+
import { unpack, decodeQuery, pack, decodeGraph } from "@graffy/common";
|
|
3
3
|
import debug from "debug";
|
|
4
4
|
import { WebSocketServer } from "ws";
|
|
5
5
|
const log$1 = debug("graffy:server:http");
|
|
@@ -14,6 +14,15 @@ function server$1(store, { auth } = {}) {
|
|
|
14
14
|
const qParam = parsed.query.q && String(parsed.query.q);
|
|
15
15
|
const query = qParam && unpack(JSON.parse(decodeURIComponent(qParam)));
|
|
16
16
|
if (req.headers.accept === "text/event-stream") {
|
|
17
|
+
if (auth && !await auth("watch", decodeQuery(query), options)) {
|
|
18
|
+
const body = "unauthorized";
|
|
19
|
+
res.writeHead(401, {
|
|
20
|
+
"Content-Type": "text/plain",
|
|
21
|
+
"Content-Length": Buffer.byteLength(body)
|
|
22
|
+
});
|
|
23
|
+
res.end(body);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
17
26
|
res.setHeader("content-type", "text/event-stream");
|
|
18
27
|
const keepAlive = setInterval(() => {
|
|
19
28
|
if (req.aborted || res.finished) {
|
|
@@ -105,7 +114,7 @@ data: ${e.message}
|
|
|
105
114
|
}
|
|
106
115
|
const log = debug("graffy:server:ws");
|
|
107
116
|
const PING_INTERVAL = 3e4;
|
|
108
|
-
function server(store) {
|
|
117
|
+
function server(store, { auth } = {}) {
|
|
109
118
|
if (!store) throw new Error("server.store_undef");
|
|
110
119
|
const wss = new WebSocketServer({ noServer: true });
|
|
111
120
|
wss.on("connection", function connection(ws) {
|
|
@@ -118,6 +127,13 @@ function server(store) {
|
|
|
118
127
|
ws.pingPending = false;
|
|
119
128
|
return;
|
|
120
129
|
}
|
|
130
|
+
if (auth && op !== "unwatch") {
|
|
131
|
+
const decoded = op === "write" ? decodeGraph(payload) : decodeQuery(payload);
|
|
132
|
+
if (!await auth(op, decoded, options)) {
|
|
133
|
+
ws.send(JSON.stringify([id, "unauthorized"]));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
121
137
|
switch (op) {
|
|
122
138
|
case "read":
|
|
123
139
|
case "write":
|
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.17.
|
|
5
|
+
"version": "0.17.8-alpha.1",
|
|
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.17.
|
|
19
|
+
"@graffy/common": "0.17.8-alpha.1",
|
|
20
20
|
"debug": "^4.4.1",
|
|
21
21
|
"ws": "^8.18.2"
|
|
22
22
|
}
|
package/types/wsServer.d.ts
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
-
|
|
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: GraffyStore, { auth }?: {
|
|
10
|
+
auth?: (operation: string, payload: any, options: any) => Promise<boolean>;
|
|
11
|
+
} | undefined): (request: any, socket: any, head: any) => Promise<void>;
|
|
12
|
+
export type GraffyStore = import("@graffy/core").default;
|