Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
zackliu committed Sep 19, 2023
1 parent d47eb37 commit 1ae6ba5
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 24 deletions.
4 changes: 2 additions & 2 deletions dist/engine.io.esm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/engine.io.esm.min.js.map

Large diffs are not rendered by default.

235 changes: 232 additions & 3 deletions dist/engine.io.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/engine.io.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/engine.io.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/engine.io.min.js.map

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions lib/negotiate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import debugModule from "debug"; // debug()
import { SocketOptions } from "./socket.js";
import { encode } from "./contrib/parseqs.js";

const debug = debugModule("engine.io-client:negotiate"); // debug()

class NegotiateError extends Error {
public readonly type = "NegotiateError";

constructor(
reason: string,
readonly context: any
) {
super(reason);
}
}

export interface NegotiateResult {
url: string;
token: string;
}

export class Negotiate {
public query: Record<string, string>;
protected opts: SocketOptions;
protected setTimeoutFn: typeof setTimeout;

/**
* Transport abstract constructor.
*
* @param {Object} opts - options
* @protected
*/
constructor(opts) {
this.opts = opts;
this.query = opts.query;
}

/**
* Start the negotiation
*/
public async start(): Promise<NegotiateResult> {
const uri = this.uri();
let negotiateResult = await fetch(uri);

if (negotiateResult.status !== 200) {
throw new NegotiateError("Unexpected status code " + negotiateResult.status, negotiateResult);
}

const result = await negotiateResult.json();
if (!result.url) {
throw new NegotiateError("Missing property `url` in negotiate response.", result);
}

return result;
}

/**
* Generates uri for negotiate.
*
* @private
*/
private uri() {
const schema = this.opts.secure ? "https" : "http";
const query: { b64?: number; sid?: string } = this.query || {};

return this.createUri(schema, query);
}

private createUri(schema: string, query: Record<string, unknown> = {}) {
return (
schema +
"://" +
this._hostname() +
this._port() +
this.opts.negotiatePath +
this._query(query)
);
}

private _hostname() {
const hostname = this.opts.hostname;
return hostname.indexOf(":") === -1 ? hostname : "[" + hostname + "]";
}

private _port() {
if (
this.opts.port &&
((this.opts.secure && Number(this.opts.port !== 443)) ||
(!this.opts.secure && Number(this.opts.port) !== 80))
) {
return ":" + this.opts.port;
} else {
return "";
}
}

private _query(query: Record<string, unknown>) {
const encodedQuery = encode(query);
return encodedQuery.length ? "?" + encodedQuery : "";
}
}
Loading

0 comments on commit 1ae6ba5

Please sign in to comment.