@chainstream-io/centrifuge 1.0.4 → 1.0.5

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.
@@ -75,16 +75,9 @@ export declare class Centrifuge extends Centrifuge_base {
75
75
  setHeaders(headers: {
76
76
  [key: string]: string;
77
77
  }): void;
78
- /** setHttpHeaders allows setting HTTP headers for WebSocket handshake.
79
- * Only works in Node.js environment with 'ws' library.
80
- * Browser WebSocket API does not support custom headers. */
81
- setHttpHeaders(headers: {
82
- [key: string]: string;
83
- }): void;
84
- /** setWebsocket allows setting WebSocket implementation at runtime.
85
- * Useful for lazy-loading 'ws' library in Node.js environment.
86
- * Must be called before connect(). */
87
- setWebsocket(websocket: any): void;
78
+ /** setWsProtocolToken allows setting token to pass via Sec-WebSocket-Protocol header.
79
+ * Works in both browser and Node.js environments. */
80
+ setWsProtocolToken(token: string): void;
88
81
  /** send asynchronous data to a server (without any response from a server
89
82
  * expected, see rpc method if you need response). */
90
83
  send(data: any): Promise<void>;
package/build/index.js CHANGED
@@ -1370,32 +1370,20 @@ class WebsocketTransport {
1370
1370
  return this.options.websocket !== undefined && this.options.websocket !== null;
1371
1371
  }
1372
1372
  initialize(protocol, callbacks) {
1373
- let subProtocol = '';
1373
+ // Build protocols array for Sec-WebSocket-Protocol header
1374
+ const protocols = [];
1374
1375
  if (protocol === 'protobuf') {
1375
- subProtocol = 'centrifuge-protobuf';
1376
+ protocols.push('centrifuge-protobuf');
1376
1377
  }
1377
- // Check if we have HTTP headers to pass (only works in Node.js with 'ws' library)
1378
- // Browser WebSocket API does not support custom headers
1379
- const httpHeaders = this.options.httpHeaders;
1380
- const hasHttpHeaders = httpHeaders && Object.keys(httpHeaders).length > 0;
1381
- if (subProtocol !== '') {
1382
- if (hasHttpHeaders) {
1383
- // Node.js 'ws' library supports third argument with headers
1384
- this._transport = new this.options.websocket(this.endpoint, subProtocol, { headers: httpHeaders });
1385
- }
1386
- else {
1387
- this._transport = new this.options.websocket(this.endpoint, subProtocol);
1388
- }
1389
- }
1390
- else {
1391
- if (hasHttpHeaders) {
1392
- // Node.js 'ws' library supports third argument with headers
1393
- this._transport = new this.options.websocket(this.endpoint, undefined, { headers: httpHeaders });
1394
- }
1395
- else {
1396
- this._transport = new this.options.websocket(this.endpoint);
1397
- }
1378
+ // Add token via Sec-WebSocket-Protocol header for authentication
1379
+ // Format: "Bearer {token}" - server extracts token from this sub-protocol
1380
+ const wsProtocolToken = this.options.wsProtocolToken;
1381
+ if (wsProtocolToken) {
1382
+ protocols.push(`Bearer ${wsProtocolToken}`);
1398
1383
  }
1384
+ // Determine the protocols argument
1385
+ const protocolsArg = protocols.length > 0 ? protocols : undefined;
1386
+ this._transport = new this.options.websocket(this.endpoint, protocolsArg);
1399
1387
  if (protocol === 'protobuf') {
1400
1388
  this._transport.binaryType = 'arraybuffer';
1401
1389
  }
@@ -2056,7 +2044,6 @@ class JsonCodec {
2056
2044
 
2057
2045
  const defaults = {
2058
2046
  headers: {},
2059
- httpHeaders: {},
2060
2047
  token: '',
2061
2048
  getToken: null,
2062
2049
  data: null,
@@ -2076,6 +2063,7 @@ const defaults = {
2076
2063
  timeout: 5000,
2077
2064
  maxServerPingDelay: 10000,
2078
2065
  networkEventTarget: null,
2066
+ wsProtocolToken: '',
2079
2067
  };
2080
2068
  class UnauthorizedError extends Error {
2081
2069
  constructor(message) {
@@ -2221,17 +2209,10 @@ class Centrifuge extends EventEmitter$1 {
2221
2209
  setHeaders(headers) {
2222
2210
  this._config.headers = headers;
2223
2211
  }
2224
- /** setHttpHeaders allows setting HTTP headers for WebSocket handshake.
2225
- * Only works in Node.js environment with 'ws' library.
2226
- * Browser WebSocket API does not support custom headers. */
2227
- setHttpHeaders(headers) {
2228
- this._config.httpHeaders = headers;
2229
- }
2230
- /** setWebsocket allows setting WebSocket implementation at runtime.
2231
- * Useful for lazy-loading 'ws' library in Node.js environment.
2232
- * Must be called before connect(). */
2233
- setWebsocket(websocket) {
2234
- this._config.websocket = websocket;
2212
+ /** setWsProtocolToken allows setting token to pass via Sec-WebSocket-Protocol header.
2213
+ * Works in both browser and Node.js environments. */
2214
+ setWsProtocolToken(token) {
2215
+ this._config.wsProtocolToken = token;
2235
2216
  }
2236
2217
  /** send asynchronous data to a server (without any response from a server
2237
2218
  * expected, see rpc method if you need response). */
@@ -2607,7 +2588,7 @@ class Centrifuge extends EventEmitter$1 {
2607
2588
  this._debug('client will use websocket');
2608
2589
  this._transport = new WebsocketTransport(this._endpoint, {
2609
2590
  websocket: websocket,
2610
- httpHeaders: this._config.httpHeaders
2591
+ wsProtocolToken: this._config.wsProtocolToken
2611
2592
  });
2612
2593
  if (!this._transport.supported()) {
2613
2594
  throw new Error('WebSocket constructor not found, make sure it is available globally or passed as a dependency in Centrifuge options');
@@ -2631,7 +2612,7 @@ class Centrifuge extends EventEmitter$1 {
2631
2612
  this._debug('trying websocket transport');
2632
2613
  this._transport = new WebsocketTransport(transportEndpoint, {
2633
2614
  websocket: websocket,
2634
- httpHeaders: this._config.httpHeaders
2615
+ wsProtocolToken: this._config.wsProtocolToken
2635
2616
  });
2636
2617
  if (!this._transport.supported()) {
2637
2618
  this._debug('websocket transport not available');
package/build/index.mjs CHANGED
@@ -1368,32 +1368,20 @@ class WebsocketTransport {
1368
1368
  return this.options.websocket !== undefined && this.options.websocket !== null;
1369
1369
  }
1370
1370
  initialize(protocol, callbacks) {
1371
- let subProtocol = '';
1371
+ // Build protocols array for Sec-WebSocket-Protocol header
1372
+ const protocols = [];
1372
1373
  if (protocol === 'protobuf') {
1373
- subProtocol = 'centrifuge-protobuf';
1374
+ protocols.push('centrifuge-protobuf');
1374
1375
  }
1375
- // Check if we have HTTP headers to pass (only works in Node.js with 'ws' library)
1376
- // Browser WebSocket API does not support custom headers
1377
- const httpHeaders = this.options.httpHeaders;
1378
- const hasHttpHeaders = httpHeaders && Object.keys(httpHeaders).length > 0;
1379
- if (subProtocol !== '') {
1380
- if (hasHttpHeaders) {
1381
- // Node.js 'ws' library supports third argument with headers
1382
- this._transport = new this.options.websocket(this.endpoint, subProtocol, { headers: httpHeaders });
1383
- }
1384
- else {
1385
- this._transport = new this.options.websocket(this.endpoint, subProtocol);
1386
- }
1387
- }
1388
- else {
1389
- if (hasHttpHeaders) {
1390
- // Node.js 'ws' library supports third argument with headers
1391
- this._transport = new this.options.websocket(this.endpoint, undefined, { headers: httpHeaders });
1392
- }
1393
- else {
1394
- this._transport = new this.options.websocket(this.endpoint);
1395
- }
1376
+ // Add token via Sec-WebSocket-Protocol header for authentication
1377
+ // Format: "Bearer {token}" - server extracts token from this sub-protocol
1378
+ const wsProtocolToken = this.options.wsProtocolToken;
1379
+ if (wsProtocolToken) {
1380
+ protocols.push(`Bearer ${wsProtocolToken}`);
1396
1381
  }
1382
+ // Determine the protocols argument
1383
+ const protocolsArg = protocols.length > 0 ? protocols : undefined;
1384
+ this._transport = new this.options.websocket(this.endpoint, protocolsArg);
1397
1385
  if (protocol === 'protobuf') {
1398
1386
  this._transport.binaryType = 'arraybuffer';
1399
1387
  }
@@ -2054,7 +2042,6 @@ class JsonCodec {
2054
2042
 
2055
2043
  const defaults = {
2056
2044
  headers: {},
2057
- httpHeaders: {},
2058
2045
  token: '',
2059
2046
  getToken: null,
2060
2047
  data: null,
@@ -2074,6 +2061,7 @@ const defaults = {
2074
2061
  timeout: 5000,
2075
2062
  maxServerPingDelay: 10000,
2076
2063
  networkEventTarget: null,
2064
+ wsProtocolToken: '',
2077
2065
  };
2078
2066
  class UnauthorizedError extends Error {
2079
2067
  constructor(message) {
@@ -2219,17 +2207,10 @@ class Centrifuge extends EventEmitter$1 {
2219
2207
  setHeaders(headers) {
2220
2208
  this._config.headers = headers;
2221
2209
  }
2222
- /** setHttpHeaders allows setting HTTP headers for WebSocket handshake.
2223
- * Only works in Node.js environment with 'ws' library.
2224
- * Browser WebSocket API does not support custom headers. */
2225
- setHttpHeaders(headers) {
2226
- this._config.httpHeaders = headers;
2227
- }
2228
- /** setWebsocket allows setting WebSocket implementation at runtime.
2229
- * Useful for lazy-loading 'ws' library in Node.js environment.
2230
- * Must be called before connect(). */
2231
- setWebsocket(websocket) {
2232
- this._config.websocket = websocket;
2210
+ /** setWsProtocolToken allows setting token to pass via Sec-WebSocket-Protocol header.
2211
+ * Works in both browser and Node.js environments. */
2212
+ setWsProtocolToken(token) {
2213
+ this._config.wsProtocolToken = token;
2233
2214
  }
2234
2215
  /** send asynchronous data to a server (without any response from a server
2235
2216
  * expected, see rpc method if you need response). */
@@ -2605,7 +2586,7 @@ class Centrifuge extends EventEmitter$1 {
2605
2586
  this._debug('client will use websocket');
2606
2587
  this._transport = new WebsocketTransport(this._endpoint, {
2607
2588
  websocket: websocket,
2608
- httpHeaders: this._config.httpHeaders
2589
+ wsProtocolToken: this._config.wsProtocolToken
2609
2590
  });
2610
2591
  if (!this._transport.supported()) {
2611
2592
  throw new Error('WebSocket constructor not found, make sure it is available globally or passed as a dependency in Centrifuge options');
@@ -2629,7 +2610,7 @@ class Centrifuge extends EventEmitter$1 {
2629
2610
  this._debug('trying websocket transport');
2630
2611
  this._transport = new WebsocketTransport(transportEndpoint, {
2631
2612
  websocket: websocket,
2632
- httpHeaders: this._config.httpHeaders
2613
+ wsProtocolToken: this._config.wsProtocolToken
2633
2614
  });
2634
2615
  if (!this._transport.supported()) {
2635
2616
  this._debug('websocket transport not available');
@@ -75,16 +75,9 @@ export declare class Centrifuge extends Centrifuge_base {
75
75
  setHeaders(headers: {
76
76
  [key: string]: string;
77
77
  }): void;
78
- /** setHttpHeaders allows setting HTTP headers for WebSocket handshake.
79
- * Only works in Node.js environment with 'ws' library.
80
- * Browser WebSocket API does not support custom headers. */
81
- setHttpHeaders(headers: {
82
- [key: string]: string;
83
- }): void;
84
- /** setWebsocket allows setting WebSocket implementation at runtime.
85
- * Useful for lazy-loading 'ws' library in Node.js environment.
86
- * Must be called before connect(). */
87
- setWebsocket(websocket: any): void;
78
+ /** setWsProtocolToken allows setting token to pass via Sec-WebSocket-Protocol header.
79
+ * Works in both browser and Node.js environments. */
80
+ setWsProtocolToken(token: string): void;
88
81
  /** send asynchronous data to a server (without any response from a server
89
82
  * expected, see rpc method if you need response). */
90
83
  send(data: any): Promise<void>;
@@ -1372,32 +1372,20 @@ class WebsocketTransport {
1372
1372
  return this.options.websocket !== undefined && this.options.websocket !== null;
1373
1373
  }
1374
1374
  initialize(protocol, callbacks) {
1375
- let subProtocol = '';
1375
+ // Build protocols array for Sec-WebSocket-Protocol header
1376
+ const protocols = [];
1376
1377
  if (protocol === 'protobuf') {
1377
- subProtocol = 'centrifuge-protobuf';
1378
- }
1379
- // Check if we have HTTP headers to pass (only works in Node.js with 'ws' library)
1380
- // Browser WebSocket API does not support custom headers
1381
- const httpHeaders = this.options.httpHeaders;
1382
- const hasHttpHeaders = httpHeaders && Object.keys(httpHeaders).length > 0;
1383
- if (subProtocol !== '') {
1384
- if (hasHttpHeaders) {
1385
- // Node.js 'ws' library supports third argument with headers
1386
- this._transport = new this.options.websocket(this.endpoint, subProtocol, { headers: httpHeaders });
1387
- }
1388
- else {
1389
- this._transport = new this.options.websocket(this.endpoint, subProtocol);
1390
- }
1378
+ protocols.push('centrifuge-protobuf');
1391
1379
  }
1392
- else {
1393
- if (hasHttpHeaders) {
1394
- // Node.js 'ws' library supports third argument with headers
1395
- this._transport = new this.options.websocket(this.endpoint, undefined, { headers: httpHeaders });
1396
- }
1397
- else {
1398
- this._transport = new this.options.websocket(this.endpoint);
1399
- }
1380
+ // Add token via Sec-WebSocket-Protocol header for authentication
1381
+ // Format: "Bearer {token}" - server extracts token from this sub-protocol
1382
+ const wsProtocolToken = this.options.wsProtocolToken;
1383
+ if (wsProtocolToken) {
1384
+ protocols.push(`Bearer ${wsProtocolToken}`);
1400
1385
  }
1386
+ // Determine the protocols argument
1387
+ const protocolsArg = protocols.length > 0 ? protocols : undefined;
1388
+ this._transport = new this.options.websocket(this.endpoint, protocolsArg);
1401
1389
  if (protocol === 'protobuf') {
1402
1390
  this._transport.binaryType = 'arraybuffer';
1403
1391
  }
@@ -2058,7 +2046,6 @@ class JsonCodec {
2058
2046
 
2059
2047
  const defaults = {
2060
2048
  headers: {},
2061
- httpHeaders: {},
2062
2049
  token: '',
2063
2050
  getToken: null,
2064
2051
  data: null,
@@ -2078,6 +2065,7 @@ const defaults = {
2078
2065
  timeout: 5000,
2079
2066
  maxServerPingDelay: 10000,
2080
2067
  networkEventTarget: null,
2068
+ wsProtocolToken: '',
2081
2069
  };
2082
2070
  class UnauthorizedError extends Error {
2083
2071
  constructor(message) {
@@ -2223,17 +2211,10 @@ class Centrifuge extends EventEmitter$1 {
2223
2211
  setHeaders(headers) {
2224
2212
  this._config.headers = headers;
2225
2213
  }
2226
- /** setHttpHeaders allows setting HTTP headers for WebSocket handshake.
2227
- * Only works in Node.js environment with 'ws' library.
2228
- * Browser WebSocket API does not support custom headers. */
2229
- setHttpHeaders(headers) {
2230
- this._config.httpHeaders = headers;
2231
- }
2232
- /** setWebsocket allows setting WebSocket implementation at runtime.
2233
- * Useful for lazy-loading 'ws' library in Node.js environment.
2234
- * Must be called before connect(). */
2235
- setWebsocket(websocket) {
2236
- this._config.websocket = websocket;
2214
+ /** setWsProtocolToken allows setting token to pass via Sec-WebSocket-Protocol header.
2215
+ * Works in both browser and Node.js environments. */
2216
+ setWsProtocolToken(token) {
2217
+ this._config.wsProtocolToken = token;
2237
2218
  }
2238
2219
  /** send asynchronous data to a server (without any response from a server
2239
2220
  * expected, see rpc method if you need response). */
@@ -2609,7 +2590,7 @@ class Centrifuge extends EventEmitter$1 {
2609
2590
  this._debug('client will use websocket');
2610
2591
  this._transport = new WebsocketTransport(this._endpoint, {
2611
2592
  websocket: websocket,
2612
- httpHeaders: this._config.httpHeaders
2593
+ wsProtocolToken: this._config.wsProtocolToken
2613
2594
  });
2614
2595
  if (!this._transport.supported()) {
2615
2596
  throw new Error('WebSocket constructor not found, make sure it is available globally or passed as a dependency in Centrifuge options');
@@ -2633,7 +2614,7 @@ class Centrifuge extends EventEmitter$1 {
2633
2614
  this._debug('trying websocket transport');
2634
2615
  this._transport = new WebsocketTransport(transportEndpoint, {
2635
2616
  websocket: websocket,
2636
- httpHeaders: this._config.httpHeaders
2617
+ wsProtocolToken: this._config.wsProtocolToken
2637
2618
  });
2638
2619
  if (!this._transport.supported()) {
2639
2620
  this._debug('websocket transport not available');
@@ -1370,32 +1370,20 @@ class WebsocketTransport {
1370
1370
  return this.options.websocket !== undefined && this.options.websocket !== null;
1371
1371
  }
1372
1372
  initialize(protocol, callbacks) {
1373
- let subProtocol = '';
1373
+ // Build protocols array for Sec-WebSocket-Protocol header
1374
+ const protocols = [];
1374
1375
  if (protocol === 'protobuf') {
1375
- subProtocol = 'centrifuge-protobuf';
1376
- }
1377
- // Check if we have HTTP headers to pass (only works in Node.js with 'ws' library)
1378
- // Browser WebSocket API does not support custom headers
1379
- const httpHeaders = this.options.httpHeaders;
1380
- const hasHttpHeaders = httpHeaders && Object.keys(httpHeaders).length > 0;
1381
- if (subProtocol !== '') {
1382
- if (hasHttpHeaders) {
1383
- // Node.js 'ws' library supports third argument with headers
1384
- this._transport = new this.options.websocket(this.endpoint, subProtocol, { headers: httpHeaders });
1385
- }
1386
- else {
1387
- this._transport = new this.options.websocket(this.endpoint, subProtocol);
1388
- }
1376
+ protocols.push('centrifuge-protobuf');
1389
1377
  }
1390
- else {
1391
- if (hasHttpHeaders) {
1392
- // Node.js 'ws' library supports third argument with headers
1393
- this._transport = new this.options.websocket(this.endpoint, undefined, { headers: httpHeaders });
1394
- }
1395
- else {
1396
- this._transport = new this.options.websocket(this.endpoint);
1397
- }
1378
+ // Add token via Sec-WebSocket-Protocol header for authentication
1379
+ // Format: "Bearer {token}" - server extracts token from this sub-protocol
1380
+ const wsProtocolToken = this.options.wsProtocolToken;
1381
+ if (wsProtocolToken) {
1382
+ protocols.push(`Bearer ${wsProtocolToken}`);
1398
1383
  }
1384
+ // Determine the protocols argument
1385
+ const protocolsArg = protocols.length > 0 ? protocols : undefined;
1386
+ this._transport = new this.options.websocket(this.endpoint, protocolsArg);
1399
1387
  if (protocol === 'protobuf') {
1400
1388
  this._transport.binaryType = 'arraybuffer';
1401
1389
  }
@@ -2056,7 +2044,6 @@ class JsonCodec {
2056
2044
 
2057
2045
  const defaults = {
2058
2046
  headers: {},
2059
- httpHeaders: {},
2060
2047
  token: '',
2061
2048
  getToken: null,
2062
2049
  data: null,
@@ -2076,6 +2063,7 @@ const defaults = {
2076
2063
  timeout: 5000,
2077
2064
  maxServerPingDelay: 10000,
2078
2065
  networkEventTarget: null,
2066
+ wsProtocolToken: '',
2079
2067
  };
2080
2068
  class UnauthorizedError extends Error {
2081
2069
  constructor(message) {
@@ -2221,17 +2209,10 @@ class Centrifuge extends EventEmitter$1 {
2221
2209
  setHeaders(headers) {
2222
2210
  this._config.headers = headers;
2223
2211
  }
2224
- /** setHttpHeaders allows setting HTTP headers for WebSocket handshake.
2225
- * Only works in Node.js environment with 'ws' library.
2226
- * Browser WebSocket API does not support custom headers. */
2227
- setHttpHeaders(headers) {
2228
- this._config.httpHeaders = headers;
2229
- }
2230
- /** setWebsocket allows setting WebSocket implementation at runtime.
2231
- * Useful for lazy-loading 'ws' library in Node.js environment.
2232
- * Must be called before connect(). */
2233
- setWebsocket(websocket) {
2234
- this._config.websocket = websocket;
2212
+ /** setWsProtocolToken allows setting token to pass via Sec-WebSocket-Protocol header.
2213
+ * Works in both browser and Node.js environments. */
2214
+ setWsProtocolToken(token) {
2215
+ this._config.wsProtocolToken = token;
2235
2216
  }
2236
2217
  /** send asynchronous data to a server (without any response from a server
2237
2218
  * expected, see rpc method if you need response). */
@@ -2607,7 +2588,7 @@ class Centrifuge extends EventEmitter$1 {
2607
2588
  this._debug('client will use websocket');
2608
2589
  this._transport = new WebsocketTransport(this._endpoint, {
2609
2590
  websocket: websocket,
2610
- httpHeaders: this._config.httpHeaders
2591
+ wsProtocolToken: this._config.wsProtocolToken
2611
2592
  });
2612
2593
  if (!this._transport.supported()) {
2613
2594
  throw new Error('WebSocket constructor not found, make sure it is available globally or passed as a dependency in Centrifuge options');
@@ -2631,7 +2612,7 @@ class Centrifuge extends EventEmitter$1 {
2631
2612
  this._debug('trying websocket transport');
2632
2613
  this._transport = new WebsocketTransport(transportEndpoint, {
2633
2614
  websocket: websocket,
2634
- httpHeaders: this._config.httpHeaders
2615
+ wsProtocolToken: this._config.wsProtocolToken
2635
2616
  });
2636
2617
  if (!this._transport.supported()) {
2637
2618
  this._debug('websocket transport not available');
@@ -79,9 +79,7 @@ export interface Options {
79
79
  headers: {
80
80
  [key: string]: string;
81
81
  };
82
- httpHeaders: {
83
- [key: string]: string;
84
- };
82
+ wsProtocolToken: string;
85
83
  /** allows enabling debug mode */
86
84
  debug: boolean;
87
85
  /** allows setting initial connection token (JWT) */
package/build/types.d.ts CHANGED
@@ -79,9 +79,7 @@ export interface Options {
79
79
  headers: {
80
80
  [key: string]: string;
81
81
  };
82
- httpHeaders: {
83
- [key: string]: string;
84
- };
82
+ wsProtocolToken: string;
85
83
  /** allows enabling debug mode */
86
84
  debug: boolean;
87
85
  /** allows setting initial connection token (JWT) */