@mmstack/mesh 21.15.1 → 21.15.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 +5 -1
- package/fesm2022/mmstack-mesh.mjs +7 -3
- package/fesm2022/mmstack-mesh.mjs.map +1 -1
- package/package.json +2 -2
- package/types/mmstack-mesh.d.ts +18 -3
package/README.md
CHANGED
|
@@ -499,7 +499,11 @@ post-migration by construction, and journal replay stays correct forever.
|
|
|
499
499
|
- `webSocketTransport(url)` for a relay over WebSocket.
|
|
500
500
|
- `directTransport(relay, ctx)` connects straight to an in-process `createRelay`, with no
|
|
501
501
|
network. It is the backbone of the tests, and useful for a single-process demo or an
|
|
502
|
-
SSR-side room.
|
|
502
|
+
SSR-side room. Messages cross as JSON both ways, so a direct peer holds exactly what a socket
|
|
503
|
+
peer would. `directTransport(relay, ctx, { byReference: true })` hands the relay's messages
|
|
504
|
+
over by reference instead: a snapshot welcome then costs the peer no copy of the room, at the
|
|
505
|
+
price that the peer must never mutate what it receives, since it holds the relay's own
|
|
506
|
+
objects. The peer's own messages still cross as JSON.
|
|
503
507
|
|
|
504
508
|
A transport is a small interface (`send`, `onMessage`, `onClose`, `close`), so wiring a custom
|
|
505
509
|
one is a few lines. `onClose` reports the close `code` and `reason` (a 1009 means the relay
|
|
@@ -1634,9 +1634,13 @@ const asJson = (msg) => JSON.parse(JSON.stringify(msg));
|
|
|
1634
1634
|
*
|
|
1635
1635
|
* Messages cross as JSON in both directions, as they would over a socket, so a direct peer holds
|
|
1636
1636
|
* exactly what a socket peer would. Handed over by reference, an `undefined` an emitter wrote
|
|
1637
|
-
* would reach relay memory where a socket peer's would have been dropped.
|
|
1637
|
+
* would reach relay memory where a socket peer's would have been dropped. The relay's own
|
|
1638
|
+
* messages may skip the hop ({@link DirectTransportOptions.byReference}): they never hold an
|
|
1639
|
+
* `undefined` (every value in a room arrived as JSON), so the two peers read the same state and
|
|
1640
|
+
* differ only in who owns the objects.
|
|
1638
1641
|
*/
|
|
1639
|
-
function directTransport(relay, ctx) {
|
|
1642
|
+
function directTransport(relay, ctx, options = {}) {
|
|
1643
|
+
const inbound = options.byReference ? (msg) => msg : asJson;
|
|
1640
1644
|
return () => {
|
|
1641
1645
|
const messageCbs = new Set();
|
|
1642
1646
|
const closeCbs = new Set();
|
|
@@ -1645,7 +1649,7 @@ function directTransport(relay, ctx) {
|
|
|
1645
1649
|
send: (msg) => {
|
|
1646
1650
|
if (!open)
|
|
1647
1651
|
return;
|
|
1648
|
-
const wired =
|
|
1652
|
+
const wired = inbound(msg);
|
|
1649
1653
|
for (const cb of [...messageCbs])
|
|
1650
1654
|
cb(wired);
|
|
1651
1655
|
},
|