@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.
- package/README.md +84 -30
- 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
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
22
|
+
Optional (proxy via global-agent for generic HTTP(S) requests—WebSocket selection is still handled automatically as described):
|
|
20
23
|
```js
|
|
21
|
-
//
|
|
24
|
+
// enableProxy.js
|
|
22
25
|
import 'global-agent/bootstrap';
|
|
23
26
|
process.env.GLOBAL_AGENT_HTTP_PROXY = 'http://proxy:3128';
|
|
24
27
|
```
|
|
25
|
-
Run
|
|
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('
|
|
42
|
+
conn.on('mirror:updated', e => console.log('updated', e.mirrorName, e.item.id));
|
|
36
43
|
|
|
37
|
-
async function
|
|
44
|
+
async function main() {
|
|
38
45
|
const users = await conn.getMirror('users', /* GraphQL subscription */ `
|
|
39
|
-
subscription Users($version: Long
|
|
40
|
-
users(version: $version
|
|
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
|
|
59
|
+
for (const user of users.values()) {
|
|
60
|
+
console.log(user);
|
|
61
|
+
}
|
|
47
62
|
}
|
|
48
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
|
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
|