-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add HTTP long-polling implementation based on fetch()
Usage: ```js import { Socket, transports, Fetch } from "engine.io-client"; transports.polling = Fetch; const socket = new Socket("https://example.com"); ``` Note: tree-shaking unused transports is not currently supported and will be added later. Related: - socketio/socket.io#4980 - #716
- Loading branch information
1 parent
218c344
commit b11763b
Showing
13 changed files
with
499 additions
and
333 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { Polling } from "./polling.js"; | ||
import { XHR } from "./polling-xhr.js"; | ||
import { WS } from "./websocket.js"; | ||
import { WT } from "./webtransport.js"; | ||
|
||
export const transports = { | ||
websocket: WS, | ||
webtransport: WT, | ||
polling: Polling, | ||
polling: XHR, | ||
}; |
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,72 @@ | ||
import { Polling } from "./polling.js"; | ||
import { CookieJar, createCookieJar } from "./xmlhttprequest.js"; | ||
|
||
/** | ||
* HTTP long-polling based on `fetch()` | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/fetch | ||
*/ | ||
export class Fetch extends Polling { | ||
private readonly cookieJar?: CookieJar; | ||
|
||
constructor(opts) { | ||
super(opts); | ||
|
||
if (this.opts.withCredentials) { | ||
this.cookieJar = createCookieJar(); | ||
} | ||
} | ||
|
||
override doPoll() { | ||
this._fetch() | ||
.then((res) => { | ||
if (!res.ok) { | ||
return this.onError("fetch read error", res.status, res); | ||
} | ||
|
||
res.text().then((data) => this.onData(data)); | ||
}) | ||
.catch((err) => { | ||
this.onError("fetch read error", err); | ||
}); | ||
} | ||
|
||
override doWrite(data: string, callback: () => void) { | ||
this._fetch(data) | ||
.then((res) => { | ||
if (!res.ok) { | ||
return this.onError("fetch write error", res.status, res); | ||
} | ||
|
||
callback(); | ||
}) | ||
.catch((err) => { | ||
this.onError("fetch write error", err); | ||
}); | ||
} | ||
|
||
private _fetch(data?: string) { | ||
const isPost = data !== undefined; | ||
const headers = new Headers(this.opts.extraHeaders); | ||
|
||
if (isPost) { | ||
headers.set("content-type", "text/plain;charset=UTF-8"); | ||
} | ||
|
||
this.cookieJar?.appendCookies(headers); | ||
|
||
return fetch(this.uri(), { | ||
method: isPost ? "POST" : "GET", | ||
body: isPost ? data : null, | ||
headers, | ||
credentials: this.opts.withCredentials ? "include" : "omit", | ||
}).then((res) => { | ||
if (this.cookieJar) { | ||
// @ts-ignore getSetCookie() was added in Node.js v19.7.0 | ||
this.cookieJar.parseCookies(res.headers.getSetCookie()); | ||
} | ||
|
||
return res; | ||
}); | ||
} | ||
} |
Oops, something went wrong.