api.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { util } from "./util";
  2. import { PeerErrorType, PeerEventType } from "./enums";
  3. export class ApiError {
  4. type: PeerErrorType;
  5. message: string = "";
  6. }
  7. export class API {
  8. constructor(private readonly _options: any) {}
  9. private _buildUrl(method: string): string {
  10. const protocol = this._options.secure ? "https://" : "http://";
  11. let url =
  12. protocol +
  13. this._options.host +
  14. ":" +
  15. this._options.port +
  16. this._options.path +
  17. this._options.key +
  18. "/" +
  19. method;
  20. const queryString = "?ts=" + new Date().getTime() + "" + Math.random();
  21. url += queryString;
  22. return url;
  23. }
  24. /** Get a unique ID from the server via XHR and initialize with it. */
  25. retrieveId(cb = (error: ApiError, id?: string) => {}): void {
  26. const http = new XMLHttpRequest();
  27. const url = this._buildUrl("id");
  28. // If there's no ID we need to wait for one before trying to init socket.
  29. http.open("get", url, true);
  30. const self = this;
  31. http.onerror = function(e) {
  32. util.error("Error retrieving ID", e);
  33. let pathError = "";
  34. if (
  35. self._options.path === "/" &&
  36. self._options.host !== util.CLOUD_HOST
  37. ) {
  38. pathError =
  39. " If you passed in a `path` to your self-hosted PeerServer, " +
  40. "you'll also need to pass in that same path when creating a new " +
  41. "Peer.";
  42. }
  43. cb({
  44. type: PeerErrorType.ServerError,
  45. message: "Could not get an ID from the server." + pathError
  46. });
  47. };
  48. http.onreadystatechange = function() {
  49. if (http.readyState !== 4 || http.status === 0) {
  50. return;
  51. }
  52. if (http.status !== 200) {
  53. http.onerror(new ProgressEvent(`status === ${http.status}`));
  54. return;
  55. }
  56. cb(null, http.responseText);
  57. };
  58. http.send(null);
  59. }
  60. listAllPeers(cb = (error: ApiError, peers?: any[]) => {}): void {
  61. const http = new XMLHttpRequest();
  62. let url = this._buildUrl("peers");
  63. // If there's no ID we need to wait for one before trying to init socket.
  64. http.open("get", url, true);
  65. const self = this;
  66. http.onerror = function(e) {
  67. util.error("Error retrieving list of peers", e);
  68. cb({
  69. type: PeerErrorType.ServerError,
  70. message: "Could not get peers from the server."
  71. });
  72. };
  73. http.onreadystatechange = function() {
  74. if (http.readyState !== 4) {
  75. return;
  76. }
  77. if (http.status === 401) {
  78. let helpfulError = "";
  79. if (self._options.host !== util.CLOUD_HOST) {
  80. helpfulError =
  81. "It looks like you're using the cloud server. You can email " +
  82. "team@peerjs.com to enable peer listing for your API key.";
  83. } else {
  84. helpfulError =
  85. "You need to enable `allow_discovery` on your self-hosted " +
  86. "PeerServer to use this feature.";
  87. }
  88. cb({
  89. type: PeerErrorType.ServerError,
  90. message:
  91. "It doesn't look like you have permission to list peers IDs. " +
  92. helpfulError
  93. });
  94. } else if (http.status !== 200) {
  95. cb(null, []);
  96. } else {
  97. cb(JSON.parse(http.responseText));
  98. }
  99. };
  100. http.send(null);
  101. }
  102. }