index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict'
  2. var Server = require('./Server'),
  3. Connection = require('./Connection'),
  4. net = require('net'),
  5. tls = require('tls'),
  6. url = require('url')
  7. /**
  8. * Create a WebSocket server
  9. * @param {Object} [options] will be passed to net.createServer() or tls.createServer(), with the additional property 'secure' (a boolean)
  10. * @param {Function} callback will be added as 'connection' listener
  11. * @returns {Server}
  12. */
  13. exports.createServer = function (options, callback) {
  14. if (typeof options === 'function' || !arguments.length) {
  15. return new Server(false, options)
  16. }
  17. return new Server(Boolean(options.secure), options, callback)
  18. }
  19. /**
  20. * Create a WebSocket client
  21. * @param {string} URL with the format 'ws://localhost:8000/chat' (the port can be ommited)
  22. * @param {Object} [options] will be passed to net.connect() or tls.connect()
  23. * @param {Function} callback will be added as 'connect' listener
  24. * @returns {Connection}
  25. */
  26. exports.connect = function (URL, options, callback) {
  27. var socket
  28. if (typeof options === 'function') {
  29. callback = options
  30. options = undefined
  31. }
  32. options = options || {}
  33. var connectionOptions = parseWSURL(URL)
  34. options.port = connectionOptions.port
  35. options.host = connectionOptions.host
  36. connectionOptions.extraHeaders = options.extraHeaders
  37. connectionOptions.protocols = options.protocols
  38. if (connectionOptions.secure) {
  39. socket = tls.connect(options)
  40. } else {
  41. socket = net.connect(options)
  42. }
  43. return new Connection(socket, connectionOptions, callback)
  44. }
  45. /**
  46. * Set the minimum size of a pack of binary data to send in a single frame
  47. * @param {number} bytes
  48. */
  49. exports.setBinaryFragmentation = function (bytes) {
  50. Connection.binaryFragmentation = bytes
  51. }
  52. /**
  53. * Set the maximum size the internal Buffer can grow, to avoid memory attacks
  54. * @param {number} bytes
  55. */
  56. exports.setMaxBufferLength = function (bytes) {
  57. Connection.maxBufferLength = bytes
  58. }
  59. /**
  60. * Parse the WebSocket URL
  61. * @param {string} URL
  62. * @returns {Object}
  63. * @private
  64. */
  65. function parseWSURL(URL) {
  66. var parts, secure
  67. parts = url.parse(URL)
  68. parts.protocol = parts.protocol || 'ws:'
  69. if (parts.protocol === 'ws:') {
  70. secure = false
  71. } else if (parts.protocol === 'wss:') {
  72. secure = true
  73. } else {
  74. throw new Error('Invalid protocol ' + parts.protocol + '. It must be ws or wss')
  75. }
  76. parts.port = parts.port || (secure ? 443 : 80)
  77. parts.path = parts.path || '/'
  78. return {
  79. path: parts.path,
  80. port: parts.port,
  81. secure: secure,
  82. host: parts.hostname
  83. }
  84. }