@chainstream-io/centrifuge 1.0.4 → 1.0.6

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,22 @@ 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
- }
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
- }
1398
- }
1376
+ protocols.push('centrifuge-protobuf');
1377
+ }
1378
+ // Add token via Sec-WebSocket-Protocol header for authentication
1379
+ // Note: Sec-WebSocket-Protocol values cannot contain spaces
1380
+ // So we use format "Bearer.{token}" or just the token directly
1381
+ const wsProtocolToken = this.options.wsProtocolToken;
1382
+ if (wsProtocolToken) {
1383
+ // Use token directly without "Bearer " prefix since spaces are not allowed
1384
+ protocols.push(wsProtocolToken);
1385
+ }
1386
+ // Determine the protocols argument
1387
+ const protocolsArg = protocols.length > 0 ? protocols : undefined;
1388
+ this._transport = new this.options.websocket(this.endpoint, protocolsArg);
1399
1389
  if (protocol === 'protobuf') {
1400
1390
  this._transport.binaryType = 'arraybuffer';
1401
1391
  }
@@ -2056,7 +2046,6 @@ class JsonCodec {
2056
2046
 
2057
2047
  const defaults = {
2058
2048
  headers: {},
2059
- httpHeaders: {},
2060
2049
  token: '',
2061
2050
  getToken: null,
2062
2051
  data: null,
@@ -2076,6 +2065,7 @@ const defaults = {
2076
2065
  timeout: 5000,
2077
2066
  maxServerPingDelay: 10000,
2078
2067
  networkEventTarget: null,
2068
+ wsProtocolToken: '',
2079
2069
  };
2080
2070
  class UnauthorizedError extends Error {
2081
2071
  constructor(message) {
@@ -2221,17 +2211,10 @@ class Centrifuge extends EventEmitter$1 {
2221
2211
  setHeaders(headers) {
2222
2212
  this._config.headers = headers;
2223
2213
  }
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;
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;
2235
2218
  }
2236
2219
  /** send asynchronous data to a server (without any response from a server
2237
2220
  * expected, see rpc method if you need response). */
@@ -2607,7 +2590,7 @@ class Centrifuge extends EventEmitter$1 {
2607
2590
  this._debug('client will use websocket');
2608
2591
  this._transport = new WebsocketTransport(this._endpoint, {
2609
2592
  websocket: websocket,
2610
- httpHeaders: this._config.httpHeaders
2593
+ wsProtocolToken: this._config.wsProtocolToken
2611
2594
  });
2612
2595
  if (!this._transport.supported()) {
2613
2596
  throw new Error('WebSocket constructor not found, make sure it is available globally or passed as a dependency in Centrifuge options');
@@ -2631,7 +2614,7 @@ class Centrifuge extends EventEmitter$1 {
2631
2614
  this._debug('trying websocket transport');
2632
2615
  this._transport = new WebsocketTransport(transportEndpoint, {
2633
2616
  websocket: websocket,
2634
- httpHeaders: this._config.httpHeaders
2617
+ wsProtocolToken: this._config.wsProtocolToken
2635
2618
  });
2636
2619
  if (!this._transport.supported()) {
2637
2620
  this._debug('websocket transport not available');
package/build/index.mjs CHANGED
@@ -1368,32 +1368,22 @@ 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
- }
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
- }
1396
- }
1374
+ protocols.push('centrifuge-protobuf');
1375
+ }
1376
+ // Add token via Sec-WebSocket-Protocol header for authentication
1377
+ // Note: Sec-WebSocket-Protocol values cannot contain spaces
1378
+ // So we use format "Bearer.{token}" or just the token directly
1379
+ const wsProtocolToken = this.options.wsProtocolToken;
1380
+ if (wsProtocolToken) {
1381
+ // Use token directly without "Bearer " prefix since spaces are not allowed
1382
+ protocols.push(wsProtocolToken);
1383
+ }
1384
+ // Determine the protocols argument
1385
+ const protocolsArg = protocols.length > 0 ? protocols : undefined;
1386
+ this._transport = new this.options.websocket(this.endpoint, protocolsArg);
1397
1387
  if (protocol === 'protobuf') {
1398
1388
  this._transport.binaryType = 'arraybuffer';
1399
1389
  }
@@ -2054,7 +2044,6 @@ class JsonCodec {
2054
2044
 
2055
2045
  const defaults = {
2056
2046
  headers: {},
2057
- httpHeaders: {},
2058
2047
  token: '',
2059
2048
  getToken: null,
2060
2049
  data: null,
@@ -2074,6 +2063,7 @@ const defaults = {
2074
2063
  timeout: 5000,
2075
2064
  maxServerPingDelay: 10000,
2076
2065
  networkEventTarget: null,
2066
+ wsProtocolToken: '',
2077
2067
  };
2078
2068
  class UnauthorizedError extends Error {
2079
2069
  constructor(message) {
@@ -2219,17 +2209,10 @@ class Centrifuge extends EventEmitter$1 {
2219
2209
  setHeaders(headers) {
2220
2210
  this._config.headers = headers;
2221
2211
  }
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;
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;
2233
2216
  }
2234
2217
  /** send asynchronous data to a server (without any response from a server
2235
2218
  * expected, see rpc method if you need response). */
@@ -2605,7 +2588,7 @@ class Centrifuge extends EventEmitter$1 {
2605
2588
  this._debug('client will use websocket');
2606
2589
  this._transport = new WebsocketTransport(this._endpoint, {
2607
2590
  websocket: websocket,
2608
- httpHeaders: this._config.httpHeaders
2591
+ wsProtocolToken: this._config.wsProtocolToken
2609
2592
  });
2610
2593
  if (!this._transport.supported()) {
2611
2594
  throw new Error('WebSocket constructor not found, make sure it is available globally or passed as a dependency in Centrifuge options');
@@ -2629,7 +2612,7 @@ class Centrifuge extends EventEmitter$1 {
2629
2612
  this._debug('trying websocket transport');
2630
2613
  this._transport = new WebsocketTransport(transportEndpoint, {
2631
2614
  websocket: websocket,
2632
- httpHeaders: this._config.httpHeaders
2615
+ wsProtocolToken: this._config.wsProtocolToken
2633
2616
  });
2634
2617
  if (!this._transport.supported()) {
2635
2618
  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,22 @@ 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
- }
1391
- }
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
- }
1400
- }
1378
+ protocols.push('centrifuge-protobuf');
1379
+ }
1380
+ // Add token via Sec-WebSocket-Protocol header for authentication
1381
+ // Note: Sec-WebSocket-Protocol values cannot contain spaces
1382
+ // So we use format "Bearer.{token}" or just the token directly
1383
+ const wsProtocolToken = this.options.wsProtocolToken;
1384
+ if (wsProtocolToken) {
1385
+ // Use token directly without "Bearer " prefix since spaces are not allowed
1386
+ protocols.push(wsProtocolToken);
1387
+ }
1388
+ // Determine the protocols argument
1389
+ const protocolsArg = protocols.length > 0 ? protocols : undefined;
1390
+ this._transport = new this.options.websocket(this.endpoint, protocolsArg);
1401
1391
  if (protocol === 'protobuf') {
1402
1392
  this._transport.binaryType = 'arraybuffer';
1403
1393
  }
@@ -2058,7 +2048,6 @@ class JsonCodec {
2058
2048
 
2059
2049
  const defaults = {
2060
2050
  headers: {},
2061
- httpHeaders: {},
2062
2051
  token: '',
2063
2052
  getToken: null,
2064
2053
  data: null,
@@ -2078,6 +2067,7 @@ const defaults = {
2078
2067
  timeout: 5000,
2079
2068
  maxServerPingDelay: 10000,
2080
2069
  networkEventTarget: null,
2070
+ wsProtocolToken: '',
2081
2071
  };
2082
2072
  class UnauthorizedError extends Error {
2083
2073
  constructor(message) {
@@ -2223,17 +2213,10 @@ class Centrifuge extends EventEmitter$1 {
2223
2213
  setHeaders(headers) {
2224
2214
  this._config.headers = headers;
2225
2215
  }
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;
2216
+ /** setWsProtocolToken allows setting token to pass via Sec-WebSocket-Protocol header.
2217
+ * Works in both browser and Node.js environments. */
2218
+ setWsProtocolToken(token) {
2219
+ this._config.wsProtocolToken = token;
2237
2220
  }
2238
2221
  /** send asynchronous data to a server (without any response from a server
2239
2222
  * expected, see rpc method if you need response). */
@@ -2609,7 +2592,7 @@ class Centrifuge extends EventEmitter$1 {
2609
2592
  this._debug('client will use websocket');
2610
2593
  this._transport = new WebsocketTransport(this._endpoint, {
2611
2594
  websocket: websocket,
2612
- httpHeaders: this._config.httpHeaders
2595
+ wsProtocolToken: this._config.wsProtocolToken
2613
2596
  });
2614
2597
  if (!this._transport.supported()) {
2615
2598
  throw new Error('WebSocket constructor not found, make sure it is available globally or passed as a dependency in Centrifuge options');
@@ -2633,7 +2616,7 @@ class Centrifuge extends EventEmitter$1 {
2633
2616
  this._debug('trying websocket transport');
2634
2617
  this._transport = new WebsocketTransport(transportEndpoint, {
2635
2618
  websocket: websocket,
2636
- httpHeaders: this._config.httpHeaders
2619
+ wsProtocolToken: this._config.wsProtocolToken
2637
2620
  });
2638
2621
  if (!this._transport.supported()) {
2639
2622
  this._debug('websocket transport not available');
@@ -1370,32 +1370,22 @@ 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
- }
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
- }
1398
- }
1376
+ protocols.push('centrifuge-protobuf');
1377
+ }
1378
+ // Add token via Sec-WebSocket-Protocol header for authentication
1379
+ // Note: Sec-WebSocket-Protocol values cannot contain spaces
1380
+ // So we use format "Bearer.{token}" or just the token directly
1381
+ const wsProtocolToken = this.options.wsProtocolToken;
1382
+ if (wsProtocolToken) {
1383
+ // Use token directly without "Bearer " prefix since spaces are not allowed
1384
+ protocols.push(wsProtocolToken);
1385
+ }
1386
+ // Determine the protocols argument
1387
+ const protocolsArg = protocols.length > 0 ? protocols : undefined;
1388
+ this._transport = new this.options.websocket(this.endpoint, protocolsArg);
1399
1389
  if (protocol === 'protobuf') {
1400
1390
  this._transport.binaryType = 'arraybuffer';
1401
1391
  }
@@ -2056,7 +2046,6 @@ class JsonCodec {
2056
2046
 
2057
2047
  const defaults = {
2058
2048
  headers: {},
2059
- httpHeaders: {},
2060
2049
  token: '',
2061
2050
  getToken: null,
2062
2051
  data: null,
@@ -2076,6 +2065,7 @@ const defaults = {
2076
2065
  timeout: 5000,
2077
2066
  maxServerPingDelay: 10000,
2078
2067
  networkEventTarget: null,
2068
+ wsProtocolToken: '',
2079
2069
  };
2080
2070
  class UnauthorizedError extends Error {
2081
2071
  constructor(message) {
@@ -2221,17 +2211,10 @@ class Centrifuge extends EventEmitter$1 {
2221
2211
  setHeaders(headers) {
2222
2212
  this._config.headers = headers;
2223
2213
  }
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;
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;
2235
2218
  }
2236
2219
  /** send asynchronous data to a server (without any response from a server
2237
2220
  * expected, see rpc method if you need response). */
@@ -2607,7 +2590,7 @@ class Centrifuge extends EventEmitter$1 {
2607
2590
  this._debug('client will use websocket');
2608
2591
  this._transport = new WebsocketTransport(this._endpoint, {
2609
2592
  websocket: websocket,
2610
- httpHeaders: this._config.httpHeaders
2593
+ wsProtocolToken: this._config.wsProtocolToken
2611
2594
  });
2612
2595
  if (!this._transport.supported()) {
2613
2596
  throw new Error('WebSocket constructor not found, make sure it is available globally or passed as a dependency in Centrifuge options');
@@ -2631,7 +2614,7 @@ class Centrifuge extends EventEmitter$1 {
2631
2614
  this._debug('trying websocket transport');
2632
2615
  this._transport = new WebsocketTransport(transportEndpoint, {
2633
2616
  websocket: websocket,
2634
- httpHeaders: this._config.httpHeaders
2617
+ wsProtocolToken: this._config.wsProtocolToken
2635
2618
  });
2636
2619
  if (!this._transport.supported()) {
2637
2620
  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) */