@nanolink/mirrors 1.0.1 → 1.0.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.
Files changed (2) hide show
  1. package/README.md +84 -30
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,30 +1,37 @@
1
1
  # @nanolink/mirrors
2
2
 
3
- GraphQL subscription client + mirror synchronization helper utilities.
3
+ GraphQL subscription client + in‑memory mirror synchronization utilities. Optimized for incremental change streams that send START / UPDATED / DELETED / DONE / VERSION_ERROR frames.
4
4
 
5
5
  ## Features
6
- - Lightweight `SubscriptionClient` around `graphql-ws` with manual reconnect & lifecycle events.
7
- - `MirrorSync` keeps a local in‑memory mirror using START / UPDATED / DELETED / DONE / VERSION_ERROR messages.
8
- - Deferred `loaded` promise resolves after first full sync DONE.
9
- - Automatic resubscribe after reconnect (from last version) without duplicating items.
10
- - Read‑only delegated map interface for safe consumer access.
11
- - `Connection` helper to manage multiple named mirrors and re‑emit namespaced events (`mirror:start`, `mirror:updated`, ...).
12
- - Optional global proxy support via `global-agent` (enable before creating the client).
6
+ * Lightweight `SubscriptionClient` around `graphql-ws` with explicit connect, controlled reconnect, and small event surface.
7
+ * Dual version support in `MirrorSync` (`version` numeric + `opVersion` string) for hybrid sequence + causality ordering (either dimension can drive resync logic).
8
+ * Stale delete & update guards: ignores events older in either version dimension to prevent resurrecting removed or outdated entities.
9
+ * Efficient updates: UPDATED replaces item wholesale only when newer; no deep merge overhead.
10
+ * Full sync cycle handling via START/DONE gates; `loaded` promise resolves after first DONE and re-arms on VERSION_ERROR.
11
+ * Automatic resubscribe after reconnect using last known versions (no duplicate inserts).
12
+ * Read‑only delegated map interface for consumers (prevents accidental mutation of internal state).
13
+ * `Connection` helper manages multiple mirrors, re‑emitting namespaced events (`mirror:start`, `mirror:updated`, ...).
14
+ * Proxy-aware WebSocket resolution: if proxy env vars are present (ALL_PROXY / HTTPS_PROXY / HTTP_PROXY / GLOBAL_AGENT_HTTP_PROXY) the client prefers the Node `ws` implementation; otherwise uses existing global WebSocket (browser / Node >=18) or falls back to `ws`.
15
+ * Minimal dependencies; event system via `eventemitter3`.
13
16
 
14
17
  ## Install
15
- ```
18
+ ```bash
16
19
  npm install @nanolink/mirrors
17
20
  ```
18
21
 
19
- If you need a proxy:
22
+ Optional (proxy via global-agent for generic HTTP(S) requests—WebSocket selection is still handled automatically as described):
20
23
  ```js
21
- // enableGlobalProxy.js
24
+ // enableProxy.js
22
25
  import 'global-agent/bootstrap';
23
26
  process.env.GLOBAL_AGENT_HTTP_PROXY = 'http://proxy:3128';
24
27
  ```
25
- Run node with: `node -r global-agent/bootstrap yourApp.js`
28
+ Run with:
29
+ ```bash
30
+ node -r global-agent/bootstrap app.js
31
+ ```
26
32
 
27
33
  ## Usage
34
+ ### Quick (multi‑mirror connection)
28
35
  ```ts
29
36
  import { Connection } from '@nanolink/mirrors';
30
37
 
@@ -32,42 +39,89 @@ const conn = new Connection('https://api.example.com', 'TOKEN');
32
39
  conn.connect();
33
40
 
34
41
  conn.on('connected', () => console.log('socket up'));
35
- conn.on('mirror:updated', e => console.log('item updated', e.mirrorName, e.item.id));
42
+ conn.on('mirror:updated', e => console.log('updated', e.mirrorName, e.item.id));
36
43
 
37
- async function start() {
44
+ async function main() {
38
45
  const users = await conn.getMirror('users', /* GraphQL subscription */ `
39
- subscription Users($version: Long!) {
40
- users(version: $version) { type total deleteId deleteVersion data { id version name } }
46
+ subscription Users($version: Long, $opVersion: String) {
47
+ users(version: $version, opVersion: $opVersion) {
48
+ type
49
+ total
50
+ deleteId
51
+ deleteVersion
52
+ deleteOpVersion
53
+ data { id version opVersion name }
54
+ }
41
55
  }
42
56
  `, {});
43
57
 
44
- // Waits until first DONE; then you can read:
45
58
  console.log('Initial size', users.size);
46
- for (const [id, user] of users) console.log(user);
59
+ for (const user of users.values()) {
60
+ console.log(user);
61
+ }
47
62
  }
48
- start();
63
+ main();
64
+ ```
65
+
66
+ ### Direct low‑level client
67
+ ```ts
68
+ import { SubscriptionClient } from '@nanolink/mirrors';
69
+
70
+ const sc = new SubscriptionClient({ url: 'https://api.example.com', maxReconnectAttempts: 10 });
71
+ sc.connect();
72
+ sc.on('connected', () => {
73
+ const dispose = sc.subscribe({
74
+ query: 'subscription Ping { ping }'
75
+ }, {
76
+ next: (msg) => console.log(msg),
77
+ error: (e) => console.error('err', e),
78
+ complete: () => console.log('done')
79
+ });
80
+ });
49
81
  ```
50
82
 
51
83
  ## Events
52
84
  `SubscriptionClient` emits:
53
- - connecting, connected, reconnected, disconnected, retry, error
85
+ * connecting
86
+ * connected (first successful connect)
87
+ * reconnected (subsequent successful connect after a disconnect)
88
+ * disconnected ({ code, reason, wasClean })
89
+ * retry ({ attempt }) before a reconnect attempt delay
90
+ * error (network/protocol)
54
91
 
55
92
  `Connection` re‑emits mirror events as `mirror:<event>` with payload `{ mirrorName, ... }`:
56
- - start, updated, deleted, done, versionError, resubscribe, error, removed, cleared
93
+ * start
94
+ * updated (only when a newer item actually replaced stored data)
95
+ * deleted
96
+ * done (end of full sync batch)
97
+ * versionError (triggered resync)
98
+ * resubscribe (automatic after reconnect)
99
+ * error
100
+ * removed (mirror explicitly removed)
101
+ * cleared (mirror internal state cleared)
57
102
 
58
103
  ## API Surface
59
- - `SubscriptionClient` - low level client (call `connect()` then `subscribe()`)
60
- - `MirrorSync` - single mirror controller (normally use via `Connection.getMirror()`)
61
- - `Connection` - multi‑mirror manager extending `SubscriptionClient`
62
- - `ReadonlyMapView` - interface returned by `getMirror()` / `MirrorSync.load()`
104
+ * `SubscriptionClient` low level websocket subscription wrapper.
105
+ * `MirrorSync` single mirror controller (dual version tracking).
106
+ * `Connection` manages multiple mirrors + namespaced events.
107
+ * `ReadonlyMapView` immutable view returned by `getMirror()` / `MirrorSync.load()`.
63
108
 
64
109
  ## Notes
65
- - Always call `connect()` explicitly; there is no implicit lazy connect.
66
- - A VERSION_ERROR triggers an automatic full resync (resetting the loaded promise).
67
- - The first (and only the first) field in the GraphQL payload is used as the sync message wrapper.
110
+ * Always call `connect()` explicitly; no implicit lazy connect.
111
+ * VERSION_ERROR triggers automatic full resync (re-arms `loaded`).
112
+ * First top-level field in GraphQL subscription payload is treated as the sync envelope.
113
+ * Provide `webSocketImpl` manually if bundling for environments without a global WebSocket and you do NOT want `ws` as fallback.
114
+ * When proxy env vars are set in Node, `SubscriptionClient` prefers `ws` (allowing external agent configuration); browsers ignore these env vars.
68
115
 
69
- ## Build
70
- TypeScript compiles to `dist/`. Published package exposes `dist/index.js` and types.
116
+ ## Build & Publish
117
+ TypeScript sources compile to `dist/`.
118
+
119
+ Scripts:
120
+ ```bash
121
+ npm run build # compile
122
+ npm run publish:dry # preview publish contents
123
+ npm run release # build + publish (public)
124
+ ```
71
125
 
72
126
  ## License
73
127
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanolink/mirrors",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "GraphQL subscription client + mirror synchronization utilities.",
5
5
  "license": "MIT",
6
6
  "author": "Nanolink ApS, Mogens Bak Nielsen",