|
@@ -1,32 +1,27 @@
|
|
|
import { util } from "./util";
|
|
|
import logger from "./logger";
|
|
|
+import { PeerJSOption } from "./optionInterfaces";
|
|
|
+import { version } from "../package.json";
|
|
|
|
|
|
export class API {
|
|
|
- constructor(private readonly _options: any) {}
|
|
|
-
|
|
|
- private _buildUrl(method: string): string {
|
|
|
- const protocol = this._options.secure ? "https://" : "http://";
|
|
|
- let url =
|
|
|
- protocol +
|
|
|
- this._options.host +
|
|
|
- ":" +
|
|
|
- this._options.port +
|
|
|
- this._options.path +
|
|
|
- this._options.key +
|
|
|
- "/" +
|
|
|
- method;
|
|
|
- const queryString = "?ts=" + new Date().getTime() + "" + Math.random();
|
|
|
- url += queryString;
|
|
|
-
|
|
|
- return url;
|
|
|
+ constructor(private readonly _options: PeerJSOption) {}
|
|
|
+
|
|
|
+ private _buildRequest(method: string): Promise<Response> {
|
|
|
+ const protocol = this._options.secure ? "https" : "http";
|
|
|
+ const { host, port, path, key } = this._options;
|
|
|
+ const url = new URL(`${protocol}://${host}:${port}${path}${key}/${method}`);
|
|
|
+ // TODO: Why timestamp, why random?
|
|
|
+ url.searchParams.set("ts", `${Date.now()}${Math.random()}`);
|
|
|
+ url.searchParams.set("version", version);
|
|
|
+ return fetch(url.href, {
|
|
|
+ referrerPolicy: "strict-origin-when-cross-origin",
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/** Get a unique ID from the server via XHR and initialize with it. */
|
|
|
async retrieveId(): Promise<string> {
|
|
|
- const url = this._buildUrl("id");
|
|
|
-
|
|
|
try {
|
|
|
- const response = await fetch(url);
|
|
|
+ const response = await this._buildRequest("id");
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
throw new Error(`Error. Status:${response.status}`);
|
|
@@ -54,10 +49,8 @@ export class API {
|
|
|
|
|
|
/** @deprecated */
|
|
|
async listAllPeers(): Promise<any[]> {
|
|
|
- const url = this._buildUrl("peers");
|
|
|
-
|
|
|
try {
|
|
|
- const response = await fetch(url);
|
|
|
+ const response = await this._buildRequest("peers");
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
if (response.status === 401) {
|