@chitchat/sdk-web 0.1.0-dev.0 → 0.1.0-dev.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/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # ChitChat Web SDK
2
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.
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. Plain `ws://` is rejected except for an explicit `allowInsecureWs: true` localhost-only development configuration.
4
4
 
5
5
  Raw TCP is not available in browsers and is never attempted by this module.
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@chitchat/sdk-web",
3
- "version": "0.1.0-dev.0",
3
+ "version": "0.1.0-dev.1",
4
4
  "main": "src/index.js",
5
+ "exports": "./src/index.js",
5
6
  "files": ["src", "README.md", "LICENSE"],
6
7
  "engines": { "node": ">=18" },
7
- "dependencies": { "@chitchat/sdk-core": "0.1.0-dev.0" },
8
+ "dependencies": { "@chitchat/sdk-core": "0.1.0-dev.1" },
8
9
  "license": "UNLICENSED",
9
10
  "publishConfig": { "access": "public", "tag": "development" }
10
11
  }
package/src/index.js CHANGED
@@ -3,15 +3,18 @@
3
3
  let ReliableRealtimeClient;
4
4
  try { ({ ReliableRealtimeClient } = require('@chitchat/sdk-core')); } catch (_) { ({ ReliableRealtimeClient } = require('../../chitchat-core/src')); }
5
5
 
6
- const toWssEndpoint = (endpoint) => {
7
- const url = new URL(endpoint);
6
+ const isLocalHttpUrl = (url) => ['localhost', '127.0.0.1', '::1'].includes(url.hostname);
7
+ const toWssEndpoint = (endpoint, { allowInsecureWs = false } = {}) => {
8
+ let url;
9
+ try { url = new URL(endpoint); } catch (_) { throw new Error('A valid HTTPS or WSS endpoint is required'); }
10
+ if (url.username || url.password) throw new Error('Realtime endpoint must not include credentials');
8
11
  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');
12
+ if (url.protocol === 'http:' && allowInsecureWs === true && isLocalHttpUrl(url)) url.protocol = 'ws:';
13
+ if (url.protocol !== 'wss:' && !(url.protocol === 'ws:' && allowInsecureWs === true && isLocalHttpUrl(url))) throw new Error('Web realtime requires WSS (WS is allowed only for explicit localhost development)');
11
14
  return url.toString();
12
15
  };
13
16
 
14
- const createWssTransportFactory = ({ WebSocketImpl = globalThis.WebSocket, encode, decode }) => {
17
+ const createWssTransportFactory = ({ WebSocketImpl = globalThis.WebSocket, encode, decode, allowInsecureWs = false }) => {
15
18
  if (!WebSocketImpl || typeof encode !== 'function' || typeof decode !== 'function') throw new Error('WebSocket implementation and protobuf encode/decode functions are required');
16
19
  return async ({ endpoint, token, protocol }) => {
17
20
  let socket; let onMessage = () => {}; let onClose = () => {};
@@ -19,7 +22,7 @@ const createWssTransportFactory = ({ WebSocketImpl = globalThis.WebSocket, encod
19
22
  onMessage: (listener) => { onMessage = listener; },
20
23
  onClose: (listener) => { onClose = listener; },
21
24
  open: () => new Promise((resolve, reject) => {
22
- socket = new WebSocketImpl(toWssEndpoint(endpoint), protocol);
25
+ socket = new WebSocketImpl(toWssEndpoint(endpoint, { allowInsecureWs }), protocol);
23
26
  socket.binaryType = 'arraybuffer';
24
27
  socket.onopen = () => { socket.send(encode({ type: 'auth', token })); resolve(); };
25
28
  socket.onerror = () => reject(new Error('WSS connection failed'));
@@ -32,6 +35,6 @@ const createWssTransportFactory = ({ WebSocketImpl = globalThis.WebSocket, encod
32
35
  };
33
36
  };
34
37
 
35
- const createChitChatWebClient = ({ endpoint, tokenProvider, protobuf, ...options }) => new ReliableRealtimeClient({ endpoint, tokenProvider, runtime: 'web', webTransportFactory: createWssTransportFactory(protobuf), ...options });
38
+ const createChitChatWebClient = ({ endpoint, tokenProvider, protobuf, allowInsecureWs = false, ...options }) => new ReliableRealtimeClient({ endpoint, tokenProvider, runtime: 'web', webTransportFactory: createWssTransportFactory({ ...protobuf, allowInsecureWs }), ...options });
36
39
 
37
40
  module.exports = { createChitChatWebClient, createWssTransportFactory, toWssEndpoint };