@couch-kit/display 0.0.0 → 0.1.1
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/CHANGELOG.md +24 -0
- package/dist/index.js +90 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/index.ts +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1 +1,25 @@
|
|
|
1
1
|
# @couch-kit/display
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#134](https://github.com/faluciano/react-native-couch-kit/pull/134) [`2b29581`](https://github.com/faluciano/react-native-couch-kit/commit/2b29581a8c4eddee4d4131030c375fcf1f0f4f98) Thanks [@faluciano](https://github.com/faluciano)! - **Bundle & tree-shaking**
|
|
8
|
+
|
|
9
|
+
- Fix an empty published `dist/index.js`. With `sideEffects: false`, a named
|
|
10
|
+
re-export barrel (`export { RelayDisplayHost } from …`) let bun's bundler
|
|
11
|
+
tree-shake the sole class out of the built entry, so `0.1.0` shipped a bundle
|
|
12
|
+
with no implementation. Use an `export *` barrel and add a post-build guard
|
|
13
|
+
that fails if `RelayDisplayHost` is missing from the output.
|
|
14
|
+
|
|
15
|
+
## 0.1.0
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- [#132](https://github.com/faluciano/react-native-couch-kit/pull/132) [`514d108`](https://github.com/faluciano/react-native-couch-kit/commit/514d108a57717b1bbd12d96e27c2c8f8bbb49470) Thanks [@faluciano](https://github.com/faluciano)! - **New features**
|
|
20
|
+
|
|
21
|
+
- New `@couch-kit/display` package. `RelayDisplayHost` owns the authoritative
|
|
22
|
+
`GameHostRuntime` in a browser tab and bridges it to a game-agnostic relay,
|
|
23
|
+
enabling cross-network play where phones on different networks join by room
|
|
24
|
+
code. Framework-agnostic (`subscribe` / `getState` / `dispatch` / `stop`);
|
|
25
|
+
promoted from the in-repo reference example so games no longer vendor it.
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,93 @@
|
|
|
1
|
+
// src/relay-display-host.ts
|
|
2
|
+
import {
|
|
3
|
+
GameHostRuntime,
|
|
4
|
+
frameByteLength,
|
|
5
|
+
DEFAULT_MAX_MESSAGE_BYTES
|
|
6
|
+
} from "@couch-kit/runtime";
|
|
7
|
+
import {
|
|
8
|
+
RelayMessageTypes
|
|
9
|
+
} from "@couch-kit/client";
|
|
10
|
+
|
|
11
|
+
class RelayDisplayHost {
|
|
12
|
+
runtime;
|
|
13
|
+
ws;
|
|
14
|
+
roomId;
|
|
15
|
+
peers = new Set;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
const { url, roomId, ...runtimeConfig } = options;
|
|
18
|
+
this.roomId = roomId;
|
|
19
|
+
this.runtime = new GameHostRuntime(runtimeConfig);
|
|
20
|
+
this.ws = new WebSocket(url);
|
|
21
|
+
this.ws.onopen = () => {
|
|
22
|
+
this.ws.send(JSON.stringify({
|
|
23
|
+
type: RelayMessageTypes.CREATE_ROOM,
|
|
24
|
+
roomId: this.roomId
|
|
25
|
+
}));
|
|
26
|
+
};
|
|
27
|
+
this.ws.onmessage = (event) => {
|
|
28
|
+
let msg;
|
|
29
|
+
try {
|
|
30
|
+
msg = JSON.parse(event.data);
|
|
31
|
+
} catch {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.handleRelayMessage(msg);
|
|
35
|
+
};
|
|
36
|
+
this.ws.onerror = (event) => this.runtime.handleError(event instanceof Error ? event : new Error("Relay socket error"));
|
|
37
|
+
const transport = {
|
|
38
|
+
send: (connectionId, message) => this.sendEnvelope(message, connectionId),
|
|
39
|
+
broadcast: (message) => this.sendEnvelope(message)
|
|
40
|
+
};
|
|
41
|
+
this.runtime.setTransport(transport);
|
|
42
|
+
}
|
|
43
|
+
getState = () => this.runtime.getState();
|
|
44
|
+
subscribe = (listener) => this.runtime.subscribe(listener);
|
|
45
|
+
dispatch = (action) => this.runtime.dispatch(action);
|
|
46
|
+
stop() {
|
|
47
|
+
this.runtime.setTransport(null);
|
|
48
|
+
this.runtime.stop();
|
|
49
|
+
this.ws.close();
|
|
50
|
+
}
|
|
51
|
+
sendEnvelope(message, to) {
|
|
52
|
+
const envelope = {
|
|
53
|
+
type: RelayMessageTypes.DATA,
|
|
54
|
+
roomId: this.roomId,
|
|
55
|
+
data: JSON.stringify(message)
|
|
56
|
+
};
|
|
57
|
+
if (to !== undefined)
|
|
58
|
+
envelope.to = to;
|
|
59
|
+
this.ws.send(JSON.stringify(envelope));
|
|
60
|
+
}
|
|
61
|
+
handleRelayMessage(msg) {
|
|
62
|
+
switch (msg.type) {
|
|
63
|
+
case RelayMessageTypes.PEER_JOINED:
|
|
64
|
+
this.peers.add(msg.peerId);
|
|
65
|
+
this.runtime.handleConnection(msg.peerId);
|
|
66
|
+
break;
|
|
67
|
+
case RelayMessageTypes.PEER_LEFT:
|
|
68
|
+
this.peers.delete(msg.peerId);
|
|
69
|
+
this.runtime.handleDisconnect(msg.peerId);
|
|
70
|
+
break;
|
|
71
|
+
case RelayMessageTypes.DATA: {
|
|
72
|
+
if (!msg.from)
|
|
73
|
+
break;
|
|
74
|
+
if (frameByteLength(msg.data) > DEFAULT_MAX_MESSAGE_BYTES)
|
|
75
|
+
break;
|
|
76
|
+
let parsed;
|
|
77
|
+
try {
|
|
78
|
+
parsed = JSON.parse(msg.data);
|
|
79
|
+
} catch {
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
this.runtime.handleMessage(msg.from, parsed).catch((err) => this.runtime.handleError(err instanceof Error ? err : new Error(String(err))));
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
case RelayMessageTypes.ERROR:
|
|
86
|
+
this.runtime.handleError(new Error(msg.message));
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
1
91
|
export {
|
|
2
92
|
RelayDisplayHost
|
|
3
93
|
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from "./relay-display-host";
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,sBAAsB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@couch-kit/display",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"provenance": true
|
|
@@ -43,7 +43,8 @@
|
|
|
43
43
|
],
|
|
44
44
|
"sideEffects": false,
|
|
45
45
|
"scripts": {
|
|
46
|
-
"build": "bun build ./src/index.ts --outdir ./dist --target browser --external @couch-kit/core --external @couch-kit/runtime --external @couch-kit/client && tsc -p tsconfig.build.json",
|
|
46
|
+
"build": "bun build ./src/index.ts --outdir ./dist --target browser --external @couch-kit/core --external @couch-kit/runtime --external @couch-kit/client && tsc -p tsconfig.build.json && bun run verify-build",
|
|
47
|
+
"verify-build": "node -e \"if(!require('fs').readFileSync('./dist/index.js','utf8').includes('class RelayDisplayHost'))throw new Error('dist/index.js is missing RelayDisplayHost — empty bundle');\"",
|
|
47
48
|
"prepublishOnly": "bun run build",
|
|
48
49
|
"test": "bun test",
|
|
49
50
|
"lint": "eslint src/",
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// A single `export *` barrel: a named re-export (`export { X } from`) combined
|
|
2
|
+
// with `sideEffects: false` lets bun's bundler tree-shake the sole class out of
|
|
3
|
+
// the built entry, publishing an empty `dist/index.js`. `export *` keeps it.
|
|
4
|
+
export * from "./relay-display-host";
|