index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.default = exports.connect = exports.io = exports.Socket = exports.Manager = exports.protocol = void 0;
  7. const url_js_1 = require("./url.js");
  8. const manager_js_1 = require("./manager.js");
  9. Object.defineProperty(exports, "Manager", { enumerable: true, get: function () { return manager_js_1.Manager; } });
  10. const socket_js_1 = require("./socket.js");
  11. Object.defineProperty(exports, "Socket", { enumerable: true, get: function () { return socket_js_1.Socket; } });
  12. const debug_1 = __importDefault(require("debug")); // debug()
  13. const debug = debug_1.default("socket.io-client"); // debug()
  14. /**
  15. * Managers cache.
  16. */
  17. const cache = {};
  18. function lookup(uri, opts) {
  19. if (typeof uri === "object") {
  20. opts = uri;
  21. uri = undefined;
  22. }
  23. opts = opts || {};
  24. const parsed = url_js_1.url(uri, opts.path || "/socket.io");
  25. const source = parsed.source;
  26. const id = parsed.id;
  27. const path = parsed.path;
  28. const sameNamespace = cache[id] && path in cache[id]["nsps"];
  29. const newConnection = opts.forceNew ||
  30. opts["force new connection"] ||
  31. false === opts.multiplex ||
  32. sameNamespace;
  33. let io;
  34. if (newConnection) {
  35. debug("ignoring socket cache for %s", source);
  36. io = new manager_js_1.Manager(source, opts);
  37. }
  38. else {
  39. if (!cache[id]) {
  40. debug("new io instance for %s", source);
  41. cache[id] = new manager_js_1.Manager(source, opts);
  42. }
  43. io = cache[id];
  44. }
  45. if (parsed.query && !opts.query) {
  46. opts.query = parsed.queryKey;
  47. }
  48. return io.socket(parsed.path, opts);
  49. }
  50. exports.io = lookup;
  51. exports.connect = lookup;
  52. exports.default = lookup;
  53. // so that "lookup" can be used both as a function (e.g. `io(...)`) and as a
  54. // namespace (e.g. `io.connect(...)`), for backward compatibility
  55. Object.assign(lookup, {
  56. Manager: manager_js_1.Manager,
  57. Socket: socket_js_1.Socket,
  58. io: lookup,
  59. connect: lookup,
  60. });
  61. /**
  62. * Protocol version.
  63. *
  64. * @public
  65. */
  66. var socket_io_parser_1 = require("socket.io-parser");
  67. Object.defineProperty(exports, "protocol", { enumerable: true, get: function () { return socket_io_parser_1.protocol; } });
  68. module.exports = lookup;