@20baht/live 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -103,6 +103,18 @@ await db.transaction(async (tx) => {
103
103
 
104
104
  Without Postgres: `memorySource()` and `live.emit(topics)` (one process).
105
105
 
106
+ ### Which source
107
+
108
+ `changed(tx, topics)` is a `pg_notify` inside the transaction: only a `pgSource` that LISTENs hears
109
+ it. With `memorySource()`, raise topics with `live.emit(topics)` instead.
110
+
111
+ | Source | Topics come from | Heard | Writers it sees |
112
+ | --- | --- | --- | --- |
113
+ | `memorySource()` | `live.emit(topics)` in your code | when `emit` runs; a rollback after it does not take it back | this process's code only |
114
+ | `pgSource(listener)` | `liveTriggers` on tables, `changed(tx, topics)`, or `live.emit` (a NOTIFY of its own) | on commit, never on rollback | any writer, any process (`psql`, a job, another server) |
115
+
116
+ `pgliteListener` gives PGlite the same LISTEN, so tests and a single-file app can use `pgSource` too.
117
+
106
118
  ## Browser
107
119
 
108
120
  ```ts
@@ -125,6 +137,9 @@ await live.mutate(
125
137
  - It reconnects with backoff, sending the version it holds; an answer that did not change costs an
126
138
  `unchanged` frame. Without a stream it tries again when the page is shown again or the network is
127
139
  back, never on a timer.
140
+ - A page put away (`pagehide`: navigating off, or into the back/forward cache) closes its stream, so
141
+ a cached page holds none of the host's six HTTP/1.1 connections; back from the cache (`pageshow`
142
+ with `persisted`), it reconnects.
128
143
  - A mutation carries an `Idempotency-Key` (`@20baht/elysia-idempotency`). Its guess stays until a
129
144
  frame follows the write's answer (or `settleMs`, 5 s); a refused write takes it off at once and
130
145
  throws `LiveMutationError` with the status and body.
package/dist/browser.d.ts CHANGED
@@ -26,6 +26,11 @@ export interface LiveEnvironment {
26
26
  clearTimeout: (handle: unknown) => void;
27
27
  /** Hear `online` and the page being shown again; returns how to stop. */
28
28
  onWake: (wake: () => void) => () => void;
29
+ /**
30
+ * Hear the page being put away (`pagehide`: a navigation, or the back/forward cache, where a
31
+ * stream left open would hold one of the host's few HTTP/1.1 connections); returns how to stop.
32
+ */
33
+ onHide?: ((hide: () => void) => () => void) | undefined;
29
34
  /** A fresh id for a mutation's `Idempotency-Key`. */
30
35
  uuid: () => string;
31
36
  }
package/dist/browser.js CHANGED
@@ -24,13 +24,27 @@ const browserEnvironment = () => ({
24
24
  if (document.visibilityState === 'visible')
25
25
  wake();
26
26
  };
27
+ const shown = (event) => {
28
+ if (event.persisted)
29
+ wake();
30
+ };
27
31
  window.addEventListener('online', wake);
32
+ window.addEventListener('pageshow', shown);
28
33
  document.addEventListener('visibilitychange', visible);
29
34
  return () => {
30
35
  window.removeEventListener('online', wake);
36
+ window.removeEventListener('pageshow', shown);
31
37
  document.removeEventListener('visibilitychange', visible);
32
38
  };
33
39
  },
40
+ onHide: (hide) => {
41
+ if (typeof window === 'undefined')
42
+ return () => undefined;
43
+ window.addEventListener('pagehide', hide);
44
+ return () => {
45
+ window.removeEventListener('pagehide', hide);
46
+ };
47
+ },
34
48
  uuid: () => crypto.randomUUID(),
35
49
  });
36
50
  const MIN_BACKOFF = 1000;
@@ -203,6 +217,13 @@ export function createLiveClient(options) {
203
217
  backoff = MIN_BACKOFF;
204
218
  schedule(0);
205
219
  });
220
+ const stopHiding = env.onHide?.(() => {
221
+ env.clearTimeout(pending);
222
+ env.clearTimeout(retry);
223
+ retry = undefined;
224
+ connection?.abort();
225
+ connection = undefined;
226
+ }) ?? (() => undefined);
206
227
  const handleOf = new WeakMap();
207
228
  const subscribe = (request, listener) => {
208
229
  const key = requestKey(request);
@@ -312,6 +333,7 @@ export function createLiveClient(options) {
312
333
  close: () => {
313
334
  closed = true;
314
335
  stopWaking();
336
+ stopHiding();
315
337
  env.clearTimeout(pending);
316
338
  env.clearTimeout(retry);
317
339
  connection?.abort();
package/dist/protocol.js CHANGED
@@ -46,7 +46,9 @@ function isPath(path) {
46
46
  path.startsWith('/') &&
47
47
  !path.startsWith('//') &&
48
48
  path.length <= 2048 &&
49
- !/[?#\s]/.test(path));
49
+ !/[?#\s\\]/.test(path) &&
50
+ !/%2e|%2f|%5c/i.test(path) &&
51
+ !path.split('/').some((segment) => segment === '.' || segment === '..'));
50
52
  }
51
53
  function isStringRecord(value) {
52
54
  return (typeof value === 'object' &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@20baht/live",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=20.19.0"