@graffy/client 0.16.0-alpha.1 → 0.16.0-alpha.11

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
  }
@@ -89,15 +94,15 @@ const httpClient = (baseUrl, {
89
94
  throw Error("client.sse.unavailable");
90
95
  const optionsParam = getOptionsParam(await getOptions("watch", options));
91
96
  const url = `${baseUrl}?q=${encodeURIComponent(
92
- common.serialize(query)
97
+ JSON.stringify(common.pack(query))
93
98
  )}&opts=${optionsParam}`;
94
99
  const source = new EventSource(url);
95
100
  yield* stream.makeStream((push, end) => {
96
101
  source.onmessage = ({ data }) => {
97
- push(common.deserialize(data));
102
+ push(common.unpack(JSON.parse(data)));
98
103
  };
99
104
  source.onerror = (e) => {
100
- end(Error("client.sse.transport: " + e.message));
105
+ end(Error("client.sse.transport: " + e));
101
106
  };
102
107
  source.addEventListener("graffyerror", (e) => {
103
108
  end(Error("server." + e.data));
@@ -115,10 +120,10 @@ const httpClient = (baseUrl, {
115
120
  return fetch(url, {
116
121
  method: "POST",
117
122
  headers: { "Content-Type": "application/json" },
118
- body: common.serialize(change)
119
- }).then((res) => {
123
+ body: JSON.stringify(common.pack(change))
124
+ }).then(async (res) => {
120
125
  if (res.status === 200)
121
- return res.json();
126
+ return common.unpack(JSON.parse(await res.text()));
122
127
  return res.text().then((message) => {
123
128
  throw Error("server." + message);
124
129
  });
@@ -170,7 +175,7 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
170
175
  socket.onopen = opened;
171
176
  }
172
177
  function received(event) {
173
- const [id, ...data] = common.deserialize(event.data);
178
+ const [id, ...data] = JSON.parse(event.data);
174
179
  setAlive();
175
180
  if (id === ":ping") {
176
181
  send([":pong"]);
@@ -234,12 +239,13 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
234
239
  }
235
240
  if (Date.now() - lastAlive < PING_TIMEOUT)
236
241
  return true;
242
+ log("Ping timeout, closing", lastAlive);
237
243
  socket.close();
238
244
  return false;
239
245
  }
240
246
  function send(req) {
241
247
  if (isAlive()) {
242
- socket.send(common.serialize(req));
248
+ socket.send(JSON.stringify(req));
243
249
  } else {
244
250
  buffer.push(req);
245
251
  }
@@ -272,10 +278,10 @@ const wsClient = (url, {
272
278
  function once(op, payload, options) {
273
279
  return new Promise((resolve, reject) => {
274
280
  const id = socket.start(
275
- [op, payload, getOptions(op, options) || {}],
281
+ [op, common.pack(payload), getOptions(op, options) || {}],
276
282
  (error, result) => {
277
283
  socket.stop(id);
278
- error ? reject(Error("server." + error)) : resolve(result);
284
+ error ? reject(Error("server." + error)) : resolve(common.unpack(result));
279
285
  }
280
286
  );
281
287
  });
@@ -294,14 +300,14 @@ const wsClient = (url, {
294
300
  const op = "watch";
295
301
  return stream.makeStream((push, end) => {
296
302
  const id = socket.start(
297
- [op, query, getOptions(op, options) || {}],
303
+ [op, common.pack(query), getOptions(op, options) || {}],
298
304
  (error, result) => {
299
305
  if (error) {
300
306
  socket.stop(id);
301
307
  end(Error("server." + error));
302
308
  return;
303
309
  }
304
- push(result);
310
+ push(common.unpack(result));
305
311
  }
306
312
  );
307
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 { serialize, deserialize, 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
  }
@@ -86,15 +91,15 @@ const httpClient = (baseUrl, {
86
91
  throw Error("client.sse.unavailable");
87
92
  const optionsParam = getOptionsParam(await getOptions("watch", options));
88
93
  const url = `${baseUrl}?q=${encodeURIComponent(
89
- serialize(query)
94
+ JSON.stringify(pack(query))
90
95
  )}&opts=${optionsParam}`;
91
96
  const source = new EventSource(url);
92
97
  yield* makeStream((push, end) => {
93
98
  source.onmessage = ({ data }) => {
94
- push(deserialize(data));
99
+ push(unpack(JSON.parse(data)));
95
100
  };
96
101
  source.onerror = (e) => {
97
- end(Error("client.sse.transport: " + e.message));
102
+ end(Error("client.sse.transport: " + e));
98
103
  };
99
104
  source.addEventListener("graffyerror", (e) => {
100
105
  end(Error("server." + e.data));
@@ -112,10 +117,10 @@ const httpClient = (baseUrl, {
112
117
  return fetch(url, {
113
118
  method: "POST",
114
119
  headers: { "Content-Type": "application/json" },
115
- body: serialize(change)
116
- }).then((res) => {
120
+ body: JSON.stringify(pack(change))
121
+ }).then(async (res) => {
117
122
  if (res.status === 200)
118
- return res.json();
123
+ return unpack(JSON.parse(await res.text()));
119
124
  return res.text().then((message) => {
120
125
  throw Error("server." + message);
121
126
  });
@@ -167,7 +172,7 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
167
172
  socket.onopen = opened;
168
173
  }
169
174
  function received(event) {
170
- const [id, ...data] = deserialize(event.data);
175
+ const [id, ...data] = JSON.parse(event.data);
171
176
  setAlive();
172
177
  if (id === ":ping") {
173
178
  send([":pong"]);
@@ -231,12 +236,13 @@ function Socket(url, { onUnhandled = void 0, onStatusChange = void 0 } = {}) {
231
236
  }
232
237
  if (Date.now() - lastAlive < PING_TIMEOUT)
233
238
  return true;
239
+ log("Ping timeout, closing", lastAlive);
234
240
  socket.close();
235
241
  return false;
236
242
  }
237
243
  function send(req) {
238
244
  if (isAlive()) {
239
- socket.send(serialize(req));
245
+ socket.send(JSON.stringify(req));
240
246
  } else {
241
247
  buffer.push(req);
242
248
  }
@@ -269,10 +275,10 @@ const wsClient = (url, {
269
275
  function once(op, payload, options) {
270
276
  return new Promise((resolve, reject) => {
271
277
  const id = socket.start(
272
- [op, payload, getOptions(op, options) || {}],
278
+ [op, pack(payload), getOptions(op, options) || {}],
273
279
  (error, result) => {
274
280
  socket.stop(id);
275
- error ? reject(Error("server." + error)) : resolve(result);
281
+ error ? reject(Error("server." + error)) : resolve(unpack(result));
276
282
  }
277
283
  );
278
284
  });
@@ -291,14 +297,14 @@ const wsClient = (url, {
291
297
  const op = "watch";
292
298
  return makeStream((push, end) => {
293
299
  const id = socket.start(
294
- [op, query, getOptions(op, options) || {}],
300
+ [op, pack(query), getOptions(op, options) || {}],
295
301
  (error, result) => {
296
302
  if (error) {
297
303
  socket.stop(id);
298
304
  end(Error("server." + error));
299
305
  return;
300
306
  }
301
- push(result);
307
+ push(unpack(result));
302
308
  }
303
309
  );
304
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.16.0-alpha.1",
5
+ "version": "0.16.0-alpha.11",
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.0-alpha.1",
20
- "@graffy/stream": "0.16.0-alpha.1",
19
+ "@graffy/stream": "0.16.0-alpha.11",
20
+ "@graffy/common": "0.16.0-alpha.11",
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;