@wervt/client 0.0.2 → 0.0.4

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.
@@ -17,6 +17,7 @@ export interface FeedbackOptions {
17
17
  context?: Record<string, unknown>;
18
18
  lang?: string;
19
19
  position?: string;
20
+ side?: string;
20
21
  label?: string;
21
22
  color?: string;
22
23
  }
package/dist/feedback.js CHANGED
@@ -13,6 +13,7 @@ function loadFeedback(options) {
13
13
  for (const name of [
14
14
  "lang",
15
15
  "position",
16
+ "side",
16
17
  "label",
17
18
  "color"
18
19
  ]) if (options[name]) script.dataset[name] = options[name];
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { FeedbackOptions, feedback, loadFeedback } from "./feedback.js";
2
2
  import { track, trackPageView } from "./track.js";
3
- import { fetchUser, loginUrl, logoutUrl } from "./user.js";
3
+ import { accountUrl, fetchUser, loginUrl, logoutUrl } from "./user.js";
4
4
  import { ToolInfo, callTool, hasWebMcp, registerTools } from "./webmcp.js";
5
5
  import { WervtRole, WervtUser } from "@wervt/core";
6
- export { type FeedbackOptions, type ToolInfo, type WervtRole, type WervtUser, callTool, feedback, fetchUser, hasWebMcp, loadFeedback, loginUrl, logoutUrl, registerTools, track, trackPageView };
6
+ export { type FeedbackOptions, type ToolInfo, type WervtRole, type WervtUser, accountUrl, callTool, feedback, fetchUser, hasWebMcp, loadFeedback, loginUrl, logoutUrl, registerTools, track, trackPageView };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { feedback, loadFeedback } from "./feedback.js";
2
- import { fetchUser, loginUrl, logoutUrl } from "./user.js";
2
+ import { accountUrl, fetchUser, loginUrl, logoutUrl } from "./user.js";
3
3
  import { track, trackPageView } from "./track.js";
4
4
  import { callTool, hasWebMcp, registerTools } from "./webmcp.js";
5
- export { callTool, feedback, fetchUser, hasWebMcp, loadFeedback, loginUrl, logoutUrl, registerTools, track, trackPageView };
5
+ export { accountUrl, callTool, feedback, fetchUser, hasWebMcp, loadFeedback, loginUrl, logoutUrl, registerTools, track, trackPageView };
package/dist/shape.js CHANGED
@@ -3,30 +3,52 @@ import { shapeParser } from "@wervt/core";
3
3
  // src/shape.ts
4
4
  /** The rows of the shape at `url` (absolute) as of now, without subscribing. */
5
5
  async function loadSnapshot(url, fetchClient) {
6
- const rows = /* @__PURE__ */ new Map();
7
- let fail = () => {};
8
- const stream = new ShapeStream({
9
- url,
10
- subscribe: false,
11
- fetchClient,
12
- parser: shapeParser,
13
- onError: (e) => fail(e)
14
- });
15
6
  try {
16
- await new Promise((resolve, reject) => {
17
- fail = reject;
18
- stream.subscribe((messages) => {
19
- applyMessages(rows, messages);
20
- if (stream.isUpToDate) resolve();
21
- }, (e) => reject(e));
22
- });
7
+ return await readCurrentRows(url, fetchClient) ?? await replayLog(url, fetchClient);
23
8
  } catch (e) {
24
- if (e?.status === 401) return {
9
+ if (isUnauthorized(e)) return {
25
10
  rows: [],
26
11
  offset: "-1",
27
12
  unauthorized: true
28
13
  };
29
14
  throw e;
15
+ }
16
+ }
17
+ const isUnauthorized = (e) => e?.status === 401;
18
+ /**
19
+ * Asks Postgres for the current rows, so a page render costs the same however many changes the
20
+ * shape has seen. Replaying the log grew without bound: a table of 35 rows updated every 30 s took
21
+ * 105 MB and 5 s after ten days.
22
+ *
23
+ * Undefined when that isn't possible (a runtime without the snapshot route, or the shape replaced
24
+ * meanwhile); the log still has the answer.
25
+ */
26
+ async function readCurrentRows(url, fetchClient) {
27
+ const stream = openStream(url, fetchClient, "now");
28
+ try {
29
+ await untilUpToDate(stream);
30
+ const handle = stream.shapeHandle;
31
+ const { data, responseHandle } = await stream.fetchSnapshot({ where: "true" });
32
+ if (responseHandle !== handle) return void 0;
33
+ const rows = /* @__PURE__ */ new Map();
34
+ applyMessages(rows, data);
35
+ return {
36
+ rows: [...rows],
37
+ offset: stream.lastOffset,
38
+ handle
39
+ };
40
+ } catch (e) {
41
+ if (isUnauthorized(e)) throw e;
42
+ return;
43
+ } finally {
44
+ stream.unsubscribeAll();
45
+ }
46
+ }
47
+ async function replayLog(url, fetchClient) {
48
+ const rows = /* @__PURE__ */ new Map();
49
+ const stream = openStream(url, fetchClient);
50
+ try {
51
+ await untilUpToDate(stream, (messages) => applyMessages(rows, messages));
30
52
  } finally {
31
53
  stream.unsubscribeAll();
32
54
  }
@@ -36,6 +58,27 @@ async function loadSnapshot(url, fetchClient) {
36
58
  handle: stream.shapeHandle
37
59
  };
38
60
  }
61
+ function openStream(url, fetchClient, offset) {
62
+ let fail = () => {};
63
+ const stream = new ShapeStream({
64
+ url,
65
+ offset,
66
+ subscribe: false,
67
+ fetchClient,
68
+ parser: shapeParser,
69
+ onError: (e) => fail(e)
70
+ });
71
+ return Object.assign(stream, { onFailure: (handler) => fail = handler });
72
+ }
73
+ function untilUpToDate(stream, onMessages) {
74
+ return new Promise((resolve, reject) => {
75
+ stream.onFailure(reject);
76
+ stream.subscribe((messages) => {
77
+ onMessages?.(messages);
78
+ if (stream.isUpToDate) resolve();
79
+ }, (e) => reject(e));
80
+ });
81
+ }
39
82
  /** Applies Electric change messages to `rows`. Returns whether anything changed. */
40
83
  function applyMessages(rows, messages) {
41
84
  let changed = false;
package/dist/user.d.ts CHANGED
@@ -3,6 +3,11 @@ import { WervtUser } from "@wervt/core";
3
3
  export declare const loginUrl: (redirect?: string) => string;
4
4
  /** Sign out of wervt (all apps), then come back to `redirect`. */
5
5
  export declare const logoutUrl: (redirect?: string) => string;
6
+ /**
7
+ * The wervt account page (name, picture, email) in this app's look, with a button back to
8
+ * `redirect`. Open it with a full page load, like the login.
9
+ */
10
+ export declare const accountUrl: (redirect?: string) => string;
6
11
  /**
7
12
  * The signed-in user from the app's `/_wervt/user` route, `null` when nobody is signed in or the
8
13
  * route can't be reached. For pages the server didn't render with the user in them.
package/dist/user.js CHANGED
@@ -4,6 +4,11 @@ const loginUrl = (redirect = "/") => `/_wervt/login?redirect=${encodeURIComponen
4
4
  /** Sign out of wervt (all apps), then come back to `redirect`. */
5
5
  const logoutUrl = (redirect = "/") => `/_wervt/logout?redirect=${encodeURIComponent(redirect)}`;
6
6
  /**
7
+ * The wervt account page (name, picture, email) in this app's look, with a button back to
8
+ * `redirect`. Open it with a full page load, like the login.
9
+ */
10
+ const accountUrl = (redirect = "/") => `/_wervt/account?redirect=${encodeURIComponent(redirect)}`;
11
+ /**
7
12
  * The signed-in user from the app's `/_wervt/user` route, `null` when nobody is signed in or the
8
13
  * route can't be reached. For pages the server didn't render with the user in them.
9
14
  */
@@ -19,4 +24,4 @@ async function fetchUser(options = {}) {
19
24
  function trimBase(base = "") {
20
25
  return base.replace(/\/$/, "");
21
26
  }
22
- export { fetchUser, loginUrl, logoutUrl, trimBase };
27
+ export { accountUrl, fetchUser, loginUrl, logoutUrl, trimBase };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wervt/client",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Browser SDK for apps on wervt, for any framework: the signed-in user, live data from shapes, analytics events, the feedback widget and WebMCP tools.",
5
5
  "keywords": [
6
6
  "electric",
@@ -31,7 +31,7 @@
31
31
  }
32
32
  },
33
33
  "dependencies": {
34
- "@wervt/core": "^0.0.2"
34
+ "@wervt/core": "^0.0.3"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@electric-sql/client": "^1.5.28"