diffusion
Version:
Diffusion JavaScript client
88 lines (87 loc) • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Subtransports = void 0;
/**
* @module Transport
*/
var logger_1 = require("./../util/logger");
// eslint-disable-next-line @typescript-eslint/naming-convention
var NodeWebSocket = require('ws');
var log = logger_1.create('Transports');
/**
* Optionally environment specific websockets.
*/
function websocketSubTransportFactoryProvider() {
// tslint:disable-next-line:strict-type-predicates
if (typeof WebSocket === 'function') {
log.debug('Using native websocket');
return {
factory: WebSocket,
enabled: true,
allowsOptions: false
};
}
else if (typeof NodeWebSocket === 'function') {
// browser version does not include 'ws' lib; use native impl
log.debug('Using Node WS library');
return {
factory: NodeWebSocket,
enabled: true,
allowsOptions: true
};
}
else {
log.debug('Websocket transport not available');
return {
enabled: false,
allowsOptions: false
};
}
}
/**
* Optionally provide environment specific XHR.
*/
function xhrSubTransportFactoryProvider() {
// tslint:disable-next-line:strict-type-predicates
if (typeof XMLHttpRequest === 'function') {
log.debug('Using native XHR');
return {
factory: XMLHttpRequest,
enabled: true,
allowsOptions: false
};
}
else {
log.debug('XHR transport not available');
return {
enabled: false,
allowsOptions: false
};
}
}
/**
* Support the detection and creation of WebSocket and XmlHttpRequest objects
* in an environment independent way. Each subtransport object provides an
* enabled field that indicates if the subtransport works and if it works the
* environment specific constructor for the object.
*
* The subtransports will be looked up by transport name.
*/ // eslint-disable-next-line @typescript-eslint/naming-convention
exports.Subtransports = Object.freeze({
/**
* WebSocket subtransport. Supports detection of availability and construction when available.
*/
WS: websocketSubTransportFactoryProvider(),
/**
* HTTP polling subtransport. Supports detection of availability and construction when available.
*/
XHR: xhrSubTransportFactoryProvider(),
/**
* WebSocket subtransport. Supports detection of availability and construction when available.
*/
WEBSOCKET: websocketSubTransportFactoryProvider(),
/**
* HTTP polling subtransport. Supports detection of availability and construction when available.
*/
HTTP_POLLING: xhrSubTransportFactoryProvider()
});