diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 32398fe20..9c44792f9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,3 +30,7 @@ jobs: - name: Run tests run: npm test + + - name: Run tests with fetch() + run: npm run test:node-fetch + if: ${{ matrix.node-version == '20' }} diff --git a/lib/transports/polling-fetch.ts b/lib/transports/polling-fetch.ts index 882b3192d..b9d1c2f0e 100644 --- a/lib/transports/polling-fetch.ts +++ b/lib/transports/polling-fetch.ts @@ -17,36 +17,35 @@ export class Fetch extends Polling { } } - override async doPoll() { - try { - const res = await this._fetch(); + override doPoll() { + this._fetch() + .then((res) => { + if (!res.ok) { + return this.onError("fetch read error", res.status, res); + } - if (!res.ok) { - return this.onError("fetch read error", res.status, res); - } - - const data = await res.text(); - this.onData(data); - } catch (e) { - this.onError("fetch read error", e); - } + res.text().then((data) => this.onData(data)); + }) + .catch((err) => { + this.onError("fetch read error", err); + }); } - override async doWrite(data: string, callback: () => void) { - try { - const res = await this._fetch(data); + override doWrite(data: string, callback: () => void) { + this._fetch(data) + .then((res) => { + if (!res.ok) { + return this.onError("fetch write error", res.status, res); + } - if (!res.ok) { - return this.onError("fetch write error", res.status, res); - } - - callback(); - } catch (e) { - this.onError("fetch write error", e); - } + callback(); + }) + .catch((err) => { + this.onError("fetch write error", err); + }); } - private async _fetch(data?: string) { + private _fetch(data?: string) { const isPost = data !== undefined; const headers = new Headers(this.opts.extraHeaders); @@ -56,18 +55,18 @@ export class Fetch extends Polling { this.cookieJar?.appendCookies(headers); - const res = await fetch(this.uri(), { + return fetch(this.uri(), { method: isPost ? "POST" : "GET", body: isPost ? data : null, headers, credentials: this.opts.withCredentials ? "include" : "omit", - }); - - if (this.cookieJar) { - // @ts-ignore getSetCookie() was added in Node.js v19.7.0 - this.cookieJar.parseCookies(res.headers.getSetCookie()); - } + }).then((res) => { + if (this.cookieJar) { + // @ts-ignore getSetCookie() was added in Node.js v19.7.0 + this.cookieJar.parseCookies(res.headers.getSetCookie()); + } - return res; + return res; + }); } } diff --git a/lib/transports/xmlhttprequest.ts b/lib/transports/xmlhttprequest.ts index 145d183a2..6d52c45a3 100644 --- a/lib/transports/xmlhttprequest.ts +++ b/lib/transports/xmlhttprequest.ts @@ -95,7 +95,7 @@ export class CookieJar { if (cookies.length) { xhr.setDisableHeaderCheck(true); - xhr.set("cookie", cookies.join("; ")); + xhr.setRequestHeader("cookie", cookies.join("; ")); } } diff --git a/package.json b/package.json index 6d76fa1cf..fd02316f3 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,7 @@ }, "scripts": { "compile": "rimraf ./build && tsc && tsc -p tsconfig.esm.json && ./postcompile.sh", - "test": "npm run format:check && npm run compile && if test \"$BROWSERS\" = \"1\" ; then npm run test:browser; else npm run test:node && npm run test:node-fetch; fi", + "test": "npm run format:check && npm run compile && if test \"$BROWSERS\" = \"1\" ; then npm run test:browser; else npm run test:node; fi", "test:node": "mocha --bail --require test/support/hooks.js test/index.js test/webtransport.mjs", "test:node-fetch": "USE_FETCH=1 npm run test:node", "test:browser": "zuul test/index.js", diff --git a/test/connection.js b/test/connection.js index 49dd1257c..231393127 100644 --- a/test/connection.js +++ b/test/connection.js @@ -199,7 +199,9 @@ describe("connection", function () { if (env.browser && typeof addEventListener === "function") { it("should close the socket when receiving a beforeunload event", (done) => { - const socket = new Socket(); + const socket = new Socket({ + closeOnBeforeunload: true, + }); const createEvent = (name) => { if (typeof Event === "function") {