@graffy/client 0.15.25 → 0.16.0-alpha.10

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 CHANGED
@@ -13,7 +13,7 @@ const debug__default = /* @__PURE__ */ _interopDefaultLegacy(debug);
13
13
  function getOptionsParam(options) {
14
14
  if (!options)
15
15
  return "";
16
- return encodeURIComponent(common.serialize(options));
16
+ return encodeURIComponent(JSON.stringify(options));
17
17
  }
18
18
  const aggregateQueries = {};
19
19
  class AggregateQuery {
@@ -37,7 +37,7 @@ class AggregateQuery {
37
37
  const response = await fetch(this.url, {
38
38
  method: "POST",
39
39
  headers: { "Content-Type": "application/json" },
40
- body: common.serialize(this.combinedQuery)
40
+ body: JSON.stringify(common.pack(this.combinedQuery))
41
41
  });
42
42
  if (response.status !== 200) {
43
43
  const message = await response.text();
@@ -47,9 +47,14 @@ class AggregateQuery {
47
47
  }
48
48
  return;
49
49
  }
50
- const data = await response.json();
51
- for (const reader of this.readers) {
52
- reader.resolve(data);
50
+ try {
51
+ const data = common.unpack(JSON.parse(await response.text()));
52
+ for (const reader of this.readers)
53
+ reader.resolve(data);
54
+ } catch (e) {
55
+ console.error(e);
56
+ for (const reader of this.readers)
57
+ reader.reject(e);
53
58
  }
54
59
  }
55
60
  }
@@ -88,14 +93,16 @@ const httpClient = (baseUrl, {
88
93
  if (!EventSource)
89
94
  throw Error("client.sse.unavailable");
90
95
  const optionsParam = getOptionsParam(await getOptions("watch", options));
91
- const url = `${baseUrl}?q=${common.encodeUrl(query)}&opts=${optionsParam}`;
96
+ const url = `${baseUrl}?q=${encodeURIComponent(
97
+ JSON.stringify(common.pack(query))
98
+ )}&opts=${optionsParam}`;
92
99
  const source = new EventSource(url);
93
100
  yield* stream.makeStream((push, end) => {
94
101
  source.onmessage = ({ data }) => {
95
- push(common.deserialize(data));
102
+ push(common.unpack(JSON.parse(data)));
96
103
  };
97
104
  source.onerror = (e) => {
98
- end(Error("client.sse.transport: " + e.message));
105
+ end(Error("client.sse.transport: " + e));
99
106
  };
100
107
  source.addEventListener("graffyerror", (e) => {
101
108
  end(Error("server." + e.data));
@@ -113,10 +120,10 @@ const httpClient = (baseUrl, {
113
120
  return fetch(url, {
114
121
  method: "POST",
115
122
  headers: { "Content-Type": "application/json" },
116
- body: common.serialize(change)
117
- }).then((res) => {
123
+ body: JSON.stringify(common.pack(change))
124
+ }).then(async (res) => {
118
125
  if (res.status === 200)
119
- return res.json();
126
+ return common.unpack(JSON.parse(await res.text()));
120
127
  return res.text().then((message) => {
121
128
  throw Error("server." + message);
122
129
  });
@@ -168,7 +175,7 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
168
175
  socket.onopen = opened;
169
176
  }
170
177
  function received(event) {
171
- const [id, ...data] = common.deserialize(event.data);
178
+ const [id, ...data] = JSON.parse(event.data);
172
179
  setAlive();
173
180
  if (id === ":ping") {
174
181
  send([":pong"]);
@@ -232,12 +239,13 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
232
239
  }
233
240
  if (Date.now() - lastAlive < PING_TIMEOUT)
234
241
  return true;
242
+ log("Ping timeout, closing", lastAlive);
235
243
  socket.close();
236
244
  return false;
237
245
  }
238
246
  function send(req) {
239
247
  if (isAlive()) {
240
- socket.send(common.serialize(req));
248
+ socket.send(JSON.stringify(req));
241
249
  } else {
242
250
  buffer.push(req);
243
251
  }
@@ -270,10 +278,10 @@ const wsClient = (url, {
270
278
  function once(op, payload, options) {
271
279
  return new Promise((resolve, reject) => {
272
280
  const id = socket.start(
273
- [op, payload, getOptions(op, options) || {}],
281
+ [op, common.pack(payload), getOptions(op, options) || {}],
274
282
  (error, result) => {
275
283
  socket.stop(id);
276
- error ? reject(Error("server." + error)) : resolve(result);
284
+ error ? reject(Error("server." + error)) : resolve(common.unpack(result));
277
285
  }
278
286
  );
279
287
  });
@@ -292,14 +300,14 @@ const wsClient = (url, {
292
300
  const op = "watch";
293
301
  return stream.makeStream((push, end) => {
294
302
  const id = socket.start(
295
- [op, query, getOptions(op, options) || {}],
303
+ [op, common.pack(query), getOptions(op, options) || {}],
296
304
  (error, result) => {
297
305
  if (error) {
298
306
  socket.stop(id);
299
307
  end(Error("server." + error));
300
308
  return;
301
309
  }
302
- push(result);
310
+ push(common.unpack(result));
303
311
  }
304
312
  );
305
313
  return () => {
package/index.mjs CHANGED
@@ -4,13 +4,13 @@ var __publicField = (obj, key, value) => {
4
4
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
  return value;
6
6
  };
7
- import { encodeUrl, deserialize, serialize, add, makeId, makeWatcher } from "@graffy/common";
7
+ import { pack, unpack, add, makeId, makeWatcher } from "@graffy/common";
8
8
  import { makeStream } from "@graffy/stream";
9
9
  import debug from "debug";
10
10
  function getOptionsParam(options) {
11
11
  if (!options)
12
12
  return "";
13
- return encodeURIComponent(serialize(options));
13
+ return encodeURIComponent(JSON.stringify(options));
14
14
  }
15
15
  const aggregateQueries = {};
16
16
  class AggregateQuery {
@@ -34,7 +34,7 @@ class AggregateQuery {
34
34
  const response = await fetch(this.url, {
35
35
  method: "POST",
36
36
  headers: { "Content-Type": "application/json" },
37
- body: serialize(this.combinedQuery)
37
+ body: JSON.stringify(pack(this.combinedQuery))
38
38
  });
39
39
  if (response.status !== 200) {
40
40
  const message = await response.text();
@@ -44,9 +44,14 @@ class AggregateQuery {
44
44
  }
45
45
  return;
46
46
  }
47
- const data = await response.json();
48
- for (const reader of this.readers) {
49
- reader.resolve(data);
47
+ try {
48
+ const data = unpack(JSON.parse(await response.text()));
49
+ for (const reader of this.readers)
50
+ reader.resolve(data);
51
+ } catch (e) {
52
+ console.error(e);
53
+ for (const reader of this.readers)
54
+ reader.reject(e);
50
55
  }
51
56
  }
52
57
  }
@@ -85,14 +90,16 @@ const httpClient = (baseUrl, {
85
90
  if (!EventSource)
86
91
  throw Error("client.sse.unavailable");
87
92
  const optionsParam = getOptionsParam(await getOptions("watch", options));
88
- const url = `${baseUrl}?q=${encodeUrl(query)}&opts=${optionsParam}`;
93
+ const url = `${baseUrl}?q=${encodeURIComponent(
94
+ JSON.stringify(pack(query))
95
+ )}&opts=${optionsParam}`;
89
96
  const source = new EventSource(url);
90
97
  yield* makeStream((push, end) => {
91
98
  source.onmessage = ({ data }) => {
92
- push(deserialize(data));
99
+ push(unpack(JSON.parse(data)));
93
100
  };
94
101
  source.onerror = (e) => {
95
- end(Error("client.sse.transport: " + e.message));
102
+ end(Error("client.sse.transport: " + e));
96
103
  };
97
104
  source.addEventListener("graffyerror", (e) => {
98
105
  end(Error("server." + e.data));
@@ -110,10 +117,10 @@ const httpClient = (baseUrl, {
110
117
  return fetch(url, {
111
118
  method: "POST",
112
119
  headers: { "Content-Type": "application/json" },
113
- body: serialize(change)
114
- }).then((res) => {
120
+ body: JSON.stringify(pack(change))
121
+ }).then(async (res) => {
115
122
  if (res.status === 200)
116
- return res.json();
123
+ return unpack(JSON.parse(await res.text()));
117
124
  return res.text().then((message) => {
118
125
  throw Error("server." + message);
119
126
  });
@@ -165,7 +172,7 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
165
172
  socket.onopen = opened;
166
173
  }
167
174
  function received(event) {
168
- const [id, ...data] = deserialize(event.data);
175
+ const [id, ...data] = JSON.parse(event.data);
169
176
  setAlive();
170
177
  if (id === ":ping") {
171
178
  send([":pong"]);
@@ -229,12 +236,13 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
229
236
  }
230
237
  if (Date.now() - lastAlive < PING_TIMEOUT)
231
238
  return true;
239
+ log("Ping timeout, closing", lastAlive);
232
240
  socket.close();
233
241
  return false;
234
242
  }
235
243
  function send(req) {
236
244
  if (isAlive()) {
237
- socket.send(serialize(req));
245
+ socket.send(JSON.stringify(req));
238
246
  } else {
239
247
  buffer.push(req);
240
248
  }
@@ -267,10 +275,10 @@ const wsClient = (url, {
267
275
  function once(op, payload, options) {
268
276
  return new Promise((resolve, reject) => {
269
277
  const id = socket.start(
270
- [op, payload, getOptions(op, options) || {}],
278
+ [op, pack(payload), getOptions(op, options) || {}],
271
279
  (error, result) => {
272
280
  socket.stop(id);
273
- error ? reject(Error("server." + error)) : resolve(result);
281
+ error ? reject(Error("server." + error)) : resolve(unpack(result));
274
282
  }
275
283
  );
276
284
  });
@@ -289,14 +297,14 @@ const wsClient = (url, {
289
297
  const op = "watch";
290
298
  return makeStream((push, end) => {
291
299
  const id = socket.start(
292
- [op, query, getOptions(op, options) || {}],
300
+ [op, pack(query), getOptions(op, options) || {}],
293
301
  (error, result) => {
294
302
  if (error) {
295
303
  socket.stop(id);
296
304
  end(Error("server." + error));
297
305
  return;
298
306
  }
299
- push(result);
307
+ push(unpack(result));
300
308
  }
301
309
  );
302
310
  return () => {
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.15.25",
5
+ "version": "0.16.0-alpha.10",
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/stream": "0.15.25",
20
- "@graffy/common": "0.15.25",
19
+ "@graffy/common": "0.16.0-alpha.10",
20
+ "@graffy/stream": "0.16.0-alpha.10",
21
21
  "debug": "^4.3.3"
22
22
  }
23
23
  }
@@ -3,14 +3,14 @@ export default httpClient;
3
3
  *
4
4
  * @param {string} baseUrl
5
5
  * @param {{
6
- * getOptions?: () => Promise<void>,
6
+ * getOptions?: (op: string, options: any) => Promise<void>,
7
7
  * watch?: 'sse' | 'none' | 'hang',
8
8
  * connInfoPath?: string,
9
9
  * } | undefined} options
10
10
  * @returns {(store: any) => void}
11
11
  */
12
12
  declare function httpClient(baseUrl: string, { getOptions, watch, connInfoPath, }?: {
13
- getOptions?: () => Promise<void>;
13
+ getOptions?: (op: string, options: any) => Promise<void>;
14
14
  watch?: 'sse' | 'none' | 'hang';
15
15
  connInfoPath?: string;
16
- } | undefined): (store: any) => void;
16
+ }): (store: any) => void;