getWDSMetadata.js 979 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * @typedef {Object} WDSMetaObj
  3. * @property {boolean} enforceWs
  4. * @property {number} version
  5. */
  6. /**
  7. * Derives WDS metadata from a compatible socket client.
  8. * @param {Function} SocketClient A WDS socket client (SockJS/WebSocket).
  9. * @returns {WDSMetaObj} The parsed WDS metadata object.
  10. */
  11. function getWDSMetadata(SocketClient) {
  12. let enforceWs = false;
  13. if (
  14. typeof SocketClient.name !== 'undefined' &&
  15. SocketClient.name !== null &&
  16. SocketClient.name.toLowerCase().includes('websocket')
  17. ) {
  18. enforceWs = true;
  19. }
  20. let version;
  21. // WDS versions <=3.5.0
  22. if (!('onMessage' in SocketClient.prototype)) {
  23. version = 3;
  24. } else {
  25. // WDS versions >=3.5.0 <4
  26. if (
  27. 'getClientPath' in SocketClient ||
  28. Object.getPrototypeOf(SocketClient).name === 'BaseClient'
  29. ) {
  30. version = 3;
  31. } else {
  32. version = 4;
  33. }
  34. }
  35. return {
  36. enforceWs: enforceWs,
  37. version: version,
  38. };
  39. }
  40. module.exports = getWDSMetadata;