@graffy/client 0.16.19 → 0.16.20-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 +22 -46
- package/index.mjs +22 -46
- package/package.json +4 -4
- package/types/httpClient.d.ts +1 -1
package/index.cjs
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
-
var __publicField = (obj, key, value) =>
|
|
5
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
-
return value;
|
|
7
|
-
};
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
5
|
const common = require("@graffy/common");
|
|
9
6
|
const stream = require("@graffy/stream");
|
|
10
7
|
const debug = require("debug");
|
|
11
8
|
function getOptionsParam(options) {
|
|
12
|
-
if (!options)
|
|
13
|
-
return "";
|
|
9
|
+
if (!options) return "";
|
|
14
10
|
return encodeURIComponent(JSON.stringify(options));
|
|
15
11
|
}
|
|
16
12
|
const aggregateQueries = {};
|
|
@@ -23,8 +19,7 @@ class AggregateQuery {
|
|
|
23
19
|
}
|
|
24
20
|
add(query) {
|
|
25
21
|
common.add(this.combinedQuery, query);
|
|
26
|
-
if (this.timer)
|
|
27
|
-
clearTimeout(this.timer);
|
|
22
|
+
if (this.timer) clearTimeout(this.timer);
|
|
28
23
|
this.timer = setTimeout(() => this.doFetch(), 0);
|
|
29
24
|
return new Promise((resolve, reject) => {
|
|
30
25
|
this.readers.push({ query, resolve, reject });
|
|
@@ -47,17 +42,14 @@ class AggregateQuery {
|
|
|
47
42
|
}
|
|
48
43
|
try {
|
|
49
44
|
const data = common.unpack(JSON.parse(await response.text()));
|
|
50
|
-
for (const reader of this.readers)
|
|
51
|
-
reader.resolve(data);
|
|
45
|
+
for (const reader of this.readers) reader.resolve(data);
|
|
52
46
|
} catch (e) {
|
|
53
|
-
for (const reader of this.readers)
|
|
54
|
-
reader.reject(e);
|
|
47
|
+
for (const reader of this.readers) reader.reject(e);
|
|
55
48
|
}
|
|
56
49
|
}
|
|
57
50
|
}
|
|
58
51
|
function makeQuery(url, query) {
|
|
59
|
-
if (!aggregateQueries[url])
|
|
60
|
-
aggregateQueries[url] = new AggregateQuery(url);
|
|
52
|
+
if (!aggregateQueries[url]) aggregateQueries[url] = new AggregateQuery(url);
|
|
61
53
|
return aggregateQueries[url].add(query);
|
|
62
54
|
}
|
|
63
55
|
const httpClient = (baseUrl, {
|
|
@@ -72,23 +64,20 @@ const httpClient = (baseUrl, {
|
|
|
72
64
|
});
|
|
73
65
|
store.onRead(connInfoPath, () => ({ url: baseUrl }));
|
|
74
66
|
store.on("read", async (query, options) => {
|
|
75
|
-
if (!fetch)
|
|
76
|
-
throw Error("client.fetch.unavailable");
|
|
67
|
+
if (!fetch) throw Error("client.fetch.unavailable");
|
|
77
68
|
const optionsParam = getOptionsParam(await getOptions("read", options));
|
|
78
69
|
const url = `${baseUrl}?opts=${optionsParam}&op=read`;
|
|
79
70
|
return makeQuery(url, query);
|
|
80
71
|
});
|
|
81
72
|
store.on("watch", async function* (query, options) {
|
|
82
|
-
if (watch === "none")
|
|
83
|
-
throw Error("client.no_watch");
|
|
73
|
+
if (watch === "none") throw Error("client.no_watch");
|
|
84
74
|
if (watch === "hang") {
|
|
85
75
|
yield* stream.makeStream((push) => {
|
|
86
76
|
push(void 0);
|
|
87
77
|
});
|
|
88
78
|
return;
|
|
89
79
|
}
|
|
90
|
-
if (!EventSource)
|
|
91
|
-
throw Error("client.sse.unavailable");
|
|
80
|
+
if (!EventSource) throw Error("client.sse.unavailable");
|
|
92
81
|
const optionsParam = getOptionsParam(await getOptions("watch", options));
|
|
93
82
|
const url = `${baseUrl}?q=${encodeURIComponent(
|
|
94
83
|
JSON.stringify(common.pack(query))
|
|
@@ -110,8 +99,7 @@ const httpClient = (baseUrl, {
|
|
|
110
99
|
});
|
|
111
100
|
});
|
|
112
101
|
store.on("write", async (change, options) => {
|
|
113
|
-
if (!fetch)
|
|
114
|
-
throw Error("client.fetch.unavailable");
|
|
102
|
+
if (!fetch) throw Error("client.fetch.unavailable");
|
|
115
103
|
const optionsParam = getOptionsParam(await getOptions("write", options));
|
|
116
104
|
const url = `${baseUrl}?opts=${optionsParam}&op=write`;
|
|
117
105
|
return fetch(url, {
|
|
@@ -119,8 +107,7 @@ const httpClient = (baseUrl, {
|
|
|
119
107
|
headers: { "Content-Type": "application/json" },
|
|
120
108
|
body: JSON.stringify(common.pack(change))
|
|
121
109
|
}).then(async (res) => {
|
|
122
|
-
if (res.status === 200)
|
|
123
|
-
return common.unpack(JSON.parse(await res.text()));
|
|
110
|
+
if (res.status === 200) return common.unpack(JSON.parse(await res.text()));
|
|
124
111
|
return res.text().then((message) => {
|
|
125
112
|
throw Error(`server.${message}`);
|
|
126
113
|
});
|
|
@@ -150,14 +137,12 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
|
|
|
150
137
|
const id = common.makeId();
|
|
151
138
|
const request = [id, ...params];
|
|
152
139
|
handlers[id] = { request, callback };
|
|
153
|
-
if (isAlive())
|
|
154
|
-
send(request);
|
|
140
|
+
if (isAlive()) send(request);
|
|
155
141
|
return id;
|
|
156
142
|
}
|
|
157
143
|
function stop(id, params) {
|
|
158
144
|
delete handlers[id];
|
|
159
|
-
if (params)
|
|
160
|
-
send([id, ...params]);
|
|
145
|
+
if (params) send([id, ...params]);
|
|
161
146
|
}
|
|
162
147
|
function connect() {
|
|
163
148
|
log("Trying to connect to", url);
|
|
@@ -184,8 +169,7 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
|
|
|
184
169
|
}
|
|
185
170
|
function closed(_event) {
|
|
186
171
|
log("Closed");
|
|
187
|
-
if (isOpen && onStatusChange)
|
|
188
|
-
onStatusChange(false);
|
|
172
|
+
if (isOpen && onStatusChange) onStatusChange(false);
|
|
189
173
|
const wasOpen = isOpen;
|
|
190
174
|
isOpen = false;
|
|
191
175
|
isConnecting = false;
|
|
@@ -212,30 +196,24 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
|
|
|
212
196
|
isConnecting = false;
|
|
213
197
|
lastAttempt = Date.now();
|
|
214
198
|
setAlive();
|
|
215
|
-
if (onStatusChange)
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
send(handlers[id].request);
|
|
219
|
-
while (buffer.length)
|
|
220
|
-
send(buffer.shift());
|
|
199
|
+
if (onStatusChange) onStatusChange(true);
|
|
200
|
+
for (const id in handlers) send(handlers[id].request);
|
|
201
|
+
while (buffer.length) send(buffer.shift());
|
|
221
202
|
}
|
|
222
203
|
function setAlive() {
|
|
223
204
|
lastAlive = Date.now();
|
|
224
205
|
log("Set alive", lastAlive - lastAttempt);
|
|
225
|
-
if (lastAlive - lastAttempt > RESET_TIMEOUT)
|
|
226
|
-
attempts = 0;
|
|
206
|
+
if (lastAlive - lastAttempt > RESET_TIMEOUT) attempts = 0;
|
|
227
207
|
}
|
|
228
208
|
function isAlive() {
|
|
229
209
|
log("Liveness check", isOpen ? "open" : "closed", Date.now() - lastAlive);
|
|
230
210
|
clearTimeout(aliveTimer);
|
|
231
211
|
aliveTimer = setTimeout(isAlive, INTERVAL);
|
|
232
212
|
if (!isOpen) {
|
|
233
|
-
if (!isConnecting)
|
|
234
|
-
maybeConnect();
|
|
213
|
+
if (!isConnecting) maybeConnect();
|
|
235
214
|
return false;
|
|
236
215
|
}
|
|
237
|
-
if (Date.now() - lastAlive < PING_TIMEOUT)
|
|
238
|
-
return true;
|
|
216
|
+
if (Date.now() - lastAlive < PING_TIMEOUT) return true;
|
|
239
217
|
log("Ping timeout, closing", lastAlive);
|
|
240
218
|
socket.close();
|
|
241
219
|
return false;
|
|
@@ -260,8 +238,7 @@ const wsClient = (url, {
|
|
|
260
238
|
watch = void 0,
|
|
261
239
|
connInfoPath = "connection"
|
|
262
240
|
} = {}) => (store) => {
|
|
263
|
-
if (!WebSocket)
|
|
264
|
-
throw Error("client.websocket.unavailable");
|
|
241
|
+
if (!WebSocket) throw Error("client.websocket.unavailable");
|
|
265
242
|
const socket = Socket(url, { onUnhandled, onStatusChange });
|
|
266
243
|
let status = false;
|
|
267
244
|
const statusWatcher = common.makeWatcher();
|
|
@@ -292,8 +269,7 @@ const wsClient = (url, {
|
|
|
292
269
|
store.on("read", (query, options) => once("read", query, options));
|
|
293
270
|
store.on("write", (change, options) => once("write", change, options));
|
|
294
271
|
store.on("watch", (query, options) => {
|
|
295
|
-
if (watch === "none")
|
|
296
|
-
throw Error("client.no_watch");
|
|
272
|
+
if (watch === "none") throw Error("client.no_watch");
|
|
297
273
|
const op = "watch";
|
|
298
274
|
return stream.makeStream((push, end) => {
|
|
299
275
|
const id = socket.start(
|
package/index.mjs
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) =>
|
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
4
|
import { pack, unpack, add, makeId, makeWatcher } from "@graffy/common";
|
|
8
5
|
import { makeStream } from "@graffy/stream";
|
|
9
6
|
import debug from "debug";
|
|
10
7
|
function getOptionsParam(options) {
|
|
11
|
-
if (!options)
|
|
12
|
-
return "";
|
|
8
|
+
if (!options) return "";
|
|
13
9
|
return encodeURIComponent(JSON.stringify(options));
|
|
14
10
|
}
|
|
15
11
|
const aggregateQueries = {};
|
|
@@ -22,8 +18,7 @@ class AggregateQuery {
|
|
|
22
18
|
}
|
|
23
19
|
add(query) {
|
|
24
20
|
add(this.combinedQuery, query);
|
|
25
|
-
if (this.timer)
|
|
26
|
-
clearTimeout(this.timer);
|
|
21
|
+
if (this.timer) clearTimeout(this.timer);
|
|
27
22
|
this.timer = setTimeout(() => this.doFetch(), 0);
|
|
28
23
|
return new Promise((resolve, reject) => {
|
|
29
24
|
this.readers.push({ query, resolve, reject });
|
|
@@ -46,17 +41,14 @@ class AggregateQuery {
|
|
|
46
41
|
}
|
|
47
42
|
try {
|
|
48
43
|
const data = unpack(JSON.parse(await response.text()));
|
|
49
|
-
for (const reader of this.readers)
|
|
50
|
-
reader.resolve(data);
|
|
44
|
+
for (const reader of this.readers) reader.resolve(data);
|
|
51
45
|
} catch (e) {
|
|
52
|
-
for (const reader of this.readers)
|
|
53
|
-
reader.reject(e);
|
|
46
|
+
for (const reader of this.readers) reader.reject(e);
|
|
54
47
|
}
|
|
55
48
|
}
|
|
56
49
|
}
|
|
57
50
|
function makeQuery(url, query) {
|
|
58
|
-
if (!aggregateQueries[url])
|
|
59
|
-
aggregateQueries[url] = new AggregateQuery(url);
|
|
51
|
+
if (!aggregateQueries[url]) aggregateQueries[url] = new AggregateQuery(url);
|
|
60
52
|
return aggregateQueries[url].add(query);
|
|
61
53
|
}
|
|
62
54
|
const httpClient = (baseUrl, {
|
|
@@ -71,23 +63,20 @@ const httpClient = (baseUrl, {
|
|
|
71
63
|
});
|
|
72
64
|
store.onRead(connInfoPath, () => ({ url: baseUrl }));
|
|
73
65
|
store.on("read", async (query, options) => {
|
|
74
|
-
if (!fetch)
|
|
75
|
-
throw Error("client.fetch.unavailable");
|
|
66
|
+
if (!fetch) throw Error("client.fetch.unavailable");
|
|
76
67
|
const optionsParam = getOptionsParam(await getOptions("read", options));
|
|
77
68
|
const url = `${baseUrl}?opts=${optionsParam}&op=read`;
|
|
78
69
|
return makeQuery(url, query);
|
|
79
70
|
});
|
|
80
71
|
store.on("watch", async function* (query, options) {
|
|
81
|
-
if (watch === "none")
|
|
82
|
-
throw Error("client.no_watch");
|
|
72
|
+
if (watch === "none") throw Error("client.no_watch");
|
|
83
73
|
if (watch === "hang") {
|
|
84
74
|
yield* makeStream((push) => {
|
|
85
75
|
push(void 0);
|
|
86
76
|
});
|
|
87
77
|
return;
|
|
88
78
|
}
|
|
89
|
-
if (!EventSource)
|
|
90
|
-
throw Error("client.sse.unavailable");
|
|
79
|
+
if (!EventSource) throw Error("client.sse.unavailable");
|
|
91
80
|
const optionsParam = getOptionsParam(await getOptions("watch", options));
|
|
92
81
|
const url = `${baseUrl}?q=${encodeURIComponent(
|
|
93
82
|
JSON.stringify(pack(query))
|
|
@@ -109,8 +98,7 @@ const httpClient = (baseUrl, {
|
|
|
109
98
|
});
|
|
110
99
|
});
|
|
111
100
|
store.on("write", async (change, options) => {
|
|
112
|
-
if (!fetch)
|
|
113
|
-
throw Error("client.fetch.unavailable");
|
|
101
|
+
if (!fetch) throw Error("client.fetch.unavailable");
|
|
114
102
|
const optionsParam = getOptionsParam(await getOptions("write", options));
|
|
115
103
|
const url = `${baseUrl}?opts=${optionsParam}&op=write`;
|
|
116
104
|
return fetch(url, {
|
|
@@ -118,8 +106,7 @@ const httpClient = (baseUrl, {
|
|
|
118
106
|
headers: { "Content-Type": "application/json" },
|
|
119
107
|
body: JSON.stringify(pack(change))
|
|
120
108
|
}).then(async (res) => {
|
|
121
|
-
if (res.status === 200)
|
|
122
|
-
return unpack(JSON.parse(await res.text()));
|
|
109
|
+
if (res.status === 200) return unpack(JSON.parse(await res.text()));
|
|
123
110
|
return res.text().then((message) => {
|
|
124
111
|
throw Error(`server.${message}`);
|
|
125
112
|
});
|
|
@@ -149,14 +136,12 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
|
|
|
149
136
|
const id = makeId();
|
|
150
137
|
const request = [id, ...params];
|
|
151
138
|
handlers[id] = { request, callback };
|
|
152
|
-
if (isAlive())
|
|
153
|
-
send(request);
|
|
139
|
+
if (isAlive()) send(request);
|
|
154
140
|
return id;
|
|
155
141
|
}
|
|
156
142
|
function stop(id, params) {
|
|
157
143
|
delete handlers[id];
|
|
158
|
-
if (params)
|
|
159
|
-
send([id, ...params]);
|
|
144
|
+
if (params) send([id, ...params]);
|
|
160
145
|
}
|
|
161
146
|
function connect() {
|
|
162
147
|
log("Trying to connect to", url);
|
|
@@ -183,8 +168,7 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
|
|
|
183
168
|
}
|
|
184
169
|
function closed(_event) {
|
|
185
170
|
log("Closed");
|
|
186
|
-
if (isOpen && onStatusChange)
|
|
187
|
-
onStatusChange(false);
|
|
171
|
+
if (isOpen && onStatusChange) onStatusChange(false);
|
|
188
172
|
const wasOpen = isOpen;
|
|
189
173
|
isOpen = false;
|
|
190
174
|
isConnecting = false;
|
|
@@ -211,30 +195,24 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
|
|
|
211
195
|
isConnecting = false;
|
|
212
196
|
lastAttempt = Date.now();
|
|
213
197
|
setAlive();
|
|
214
|
-
if (onStatusChange)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
send(handlers[id].request);
|
|
218
|
-
while (buffer.length)
|
|
219
|
-
send(buffer.shift());
|
|
198
|
+
if (onStatusChange) onStatusChange(true);
|
|
199
|
+
for (const id in handlers) send(handlers[id].request);
|
|
200
|
+
while (buffer.length) send(buffer.shift());
|
|
220
201
|
}
|
|
221
202
|
function setAlive() {
|
|
222
203
|
lastAlive = Date.now();
|
|
223
204
|
log("Set alive", lastAlive - lastAttempt);
|
|
224
|
-
if (lastAlive - lastAttempt > RESET_TIMEOUT)
|
|
225
|
-
attempts = 0;
|
|
205
|
+
if (lastAlive - lastAttempt > RESET_TIMEOUT) attempts = 0;
|
|
226
206
|
}
|
|
227
207
|
function isAlive() {
|
|
228
208
|
log("Liveness check", isOpen ? "open" : "closed", Date.now() - lastAlive);
|
|
229
209
|
clearTimeout(aliveTimer);
|
|
230
210
|
aliveTimer = setTimeout(isAlive, INTERVAL);
|
|
231
211
|
if (!isOpen) {
|
|
232
|
-
if (!isConnecting)
|
|
233
|
-
maybeConnect();
|
|
212
|
+
if (!isConnecting) maybeConnect();
|
|
234
213
|
return false;
|
|
235
214
|
}
|
|
236
|
-
if (Date.now() - lastAlive < PING_TIMEOUT)
|
|
237
|
-
return true;
|
|
215
|
+
if (Date.now() - lastAlive < PING_TIMEOUT) return true;
|
|
238
216
|
log("Ping timeout, closing", lastAlive);
|
|
239
217
|
socket.close();
|
|
240
218
|
return false;
|
|
@@ -259,8 +237,7 @@ const wsClient = (url, {
|
|
|
259
237
|
watch = void 0,
|
|
260
238
|
connInfoPath = "connection"
|
|
261
239
|
} = {}) => (store) => {
|
|
262
|
-
if (!WebSocket)
|
|
263
|
-
throw Error("client.websocket.unavailable");
|
|
240
|
+
if (!WebSocket) throw Error("client.websocket.unavailable");
|
|
264
241
|
const socket = Socket(url, { onUnhandled, onStatusChange });
|
|
265
242
|
let status = false;
|
|
266
243
|
const statusWatcher = makeWatcher();
|
|
@@ -291,8 +268,7 @@ const wsClient = (url, {
|
|
|
291
268
|
store.on("read", (query, options) => once("read", query, options));
|
|
292
269
|
store.on("write", (change, options) => once("write", change, options));
|
|
293
270
|
store.on("watch", (query, options) => {
|
|
294
|
-
if (watch === "none")
|
|
295
|
-
throw Error("client.no_watch");
|
|
271
|
+
if (watch === "none") throw Error("client.no_watch");
|
|
296
272
|
const op = "watch";
|
|
297
273
|
return makeStream((push, end) => {
|
|
298
274
|
const id = socket.start(
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graffy/client",
|
|
3
3
|
"description": "Graffy client library for the browser, usin the `fetch()` or `WebSocket` APIs. This module is intended to be used with a Node.js server running Graffy Server.",
|
|
4
4
|
"author": "aravind (https://github.com/aravindet)",
|
|
5
|
-
"version": "0.16.
|
|
5
|
+
"version": "0.16.20-alpha.1",
|
|
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
|
-
"@graffy/common": "0.16.
|
|
20
|
-
"@graffy/stream": "0.16.
|
|
21
|
-
"debug": "^4.3.
|
|
19
|
+
"@graffy/common": "0.16.20-alpha.1",
|
|
20
|
+
"@graffy/stream": "0.16.20-alpha.1",
|
|
21
|
+
"debug": "^4.3.7"
|
|
22
22
|
}
|
|
23
23
|
}
|
package/types/httpClient.d.ts
CHANGED
|
@@ -11,6 +11,6 @@ export default httpClient;
|
|
|
11
11
|
*/
|
|
12
12
|
declare function httpClient(baseUrl: string, { getOptions, watch, connInfoPath, }?: {
|
|
13
13
|
getOptions?: (op: string, options: any) => Promise<void>;
|
|
14
|
-
watch?:
|
|
14
|
+
watch?: "sse" | "none" | "hang";
|
|
15
15
|
connInfoPath?: string;
|
|
16
16
|
} | undefined): (store: any) => void;
|