-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
396 additions
and
24 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : ""; | ||
} | ||
} |
Oops, something went wrong.