@chitchat/sdk-web 0.1.0-dev.0

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 (3) hide show
  1. package/README.md +5 -0
  2. package/package.json +10 -0
  3. package/src/index.js +37 -0
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # ChitChat Web SDK
2
+
3
+ `createChitChatWebClient` always uses WSS. It authenticates through the first binary Protobuf frame, never by placing a session token in a URL. Supply the generated Protobuf codec and a function that fetches a short-lived project-user session token.
4
+
5
+ Raw TCP is not available in browsers and is never attempted by this module.
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@chitchat/sdk-web",
3
+ "version": "0.1.0-dev.0",
4
+ "main": "src/index.js",
5
+ "files": ["src", "README.md", "LICENSE"],
6
+ "engines": { "node": ">=18" },
7
+ "dependencies": { "@chitchat/sdk-core": "0.1.0-dev.0" },
8
+ "license": "UNLICENSED",
9
+ "publishConfig": { "access": "public", "tag": "development" }
10
+ }
package/src/index.js ADDED
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ let ReliableRealtimeClient;
4
+ try { ({ ReliableRealtimeClient } = require('@chitchat/sdk-core')); } catch (_) { ({ ReliableRealtimeClient } = require('../../chitchat-core/src')); }
5
+
6
+ const toWssEndpoint = (endpoint) => {
7
+ const url = new URL(endpoint);
8
+ if (url.protocol === 'https:') url.protocol = 'wss:';
9
+ if (url.protocol === 'http:') url.protocol = 'ws:';
10
+ if (!['wss:', 'ws:'].includes(url.protocol)) throw new Error('A HTTP(S) or WS(S) endpoint is required');
11
+ return url.toString();
12
+ };
13
+
14
+ const createWssTransportFactory = ({ WebSocketImpl = globalThis.WebSocket, encode, decode }) => {
15
+ if (!WebSocketImpl || typeof encode !== 'function' || typeof decode !== 'function') throw new Error('WebSocket implementation and protobuf encode/decode functions are required');
16
+ return async ({ endpoint, token, protocol }) => {
17
+ let socket; let onMessage = () => {}; let onClose = () => {};
18
+ return {
19
+ onMessage: (listener) => { onMessage = listener; },
20
+ onClose: (listener) => { onClose = listener; },
21
+ open: () => new Promise((resolve, reject) => {
22
+ socket = new WebSocketImpl(toWssEndpoint(endpoint), protocol);
23
+ socket.binaryType = 'arraybuffer';
24
+ socket.onopen = () => { socket.send(encode({ type: 'auth', token })); resolve(); };
25
+ socket.onerror = () => reject(new Error('WSS connection failed'));
26
+ socket.onclose = (event) => onClose({ code: event.code, reason: event.reason });
27
+ socket.onmessage = (event) => onMessage(decode(event.data));
28
+ }),
29
+ send: (frame) => socket.send(encode(frame)),
30
+ close: () => new Promise((resolve) => { if (!socket || socket.readyState === 3) return resolve(); socket.onclose = () => resolve(); socket.close(); })
31
+ };
32
+ };
33
+ };
34
+
35
+ const createChitChatWebClient = ({ endpoint, tokenProvider, protobuf, ...options }) => new ReliableRealtimeClient({ endpoint, tokenProvider, runtime: 'web', webTransportFactory: createWssTransportFactory(protobuf), ...options });
36
+
37
+ module.exports = { createChitChatWebClient, createWssTransportFactory, toWssEndpoint };