diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 912935b..6985ea3 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -5,25 +5,25 @@ name: Node.js CI on: push: - branches: [ main ] + branches: [ main, web_worker_compat ] pull_request: - branches: [ main ] + branches: [ main, web_worker_compat ] jobs: build: - runs-on: ubuntu-latest + runs-on: self-hosted strategy: matrix: - node-version: [12.x, 14.x] + node-version: [12.x, 14.x, 15.x] steps: - - uses: actions/checkout@v2 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} - - run: npm ci - - run: npm run build --if-present - - run: npm test + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + - run: npm ci + - run: npm run build --if-present + - run: npm test diff --git a/helpers/abortcontroller.js b/helpers/abortcontroller.js new file mode 100644 index 0000000..844bc16 --- /dev/null +++ b/helpers/abortcontroller.js @@ -0,0 +1,21 @@ +'use strict'; + +class AbortController { + constructor() { + Object.defineProperty(this, 'signal', { value: {aborted: false}, writable: true, configurable: true }); + } +} + +Object.defineProperty(global, 'AbortController', { + writable: true, + enumerable: false, + configurable: true, + value: AbortController, +}); + +Object.defineProperty(global, 'self', { + writable: true, + enumerable: false, + configurable: true, + value: global, +}); \ No newline at end of file diff --git a/jasmine.json b/jasmine.json index 2f79233..bfdc22d 100644 --- a/jasmine.json +++ b/jasmine.json @@ -2,5 +2,8 @@ "spec_dir": "lib", "spec_files": [ "**/*.spec.js" + ], + "helpers": [ + "../helpers/**/*.js" ] } diff --git a/src/fetch.spec.ts b/src/fetch.spec.ts new file mode 100644 index 0000000..b099c5d --- /dev/null +++ b/src/fetch.spec.ts @@ -0,0 +1,29 @@ +import * as fetch from "./fetch"; + +describe("fetch", () => { + describe("fetchEventSource", () => { + it("cannot create event source since there is no document", () => { + fetch.fetchEventSource("http://localhost:3000", {}).catch((error) => { + expect(error).toEqual(new ReferenceError("document is not defined")); + }); + }); + + it("cannot create event source since there is no window", () => { + fetch + .fetchEventSource("http://localhost:3000", { openWhenHidden: true }) + .catch((error) => { + expect(error).toEqual(new ReferenceError("window is not defined")); + }); + }); + + it("can create event source", () => { + const fetchStub = jasmine.createSpy(); + + const promise = fetch.fetchEventSource("http://localhost:3000", { + openWhenHidden: true, + fetch: fetchStub, + }); + expect(promise).toBeDefined(); + }); + }); +}); diff --git a/src/fetch.ts b/src/fetch.ts index 162ea45..d7aac9d 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -86,8 +86,10 @@ export function fetchEventSource(input: RequestInfo, { let retryInterval = DefaultRetryInterval; let retryTimer = 0; function dispose() { - document.removeEventListener('visibilitychange', onVisibilityChange); - window.clearTimeout(retryTimer); + if (!openWhenHidden) { + document.removeEventListener('visibilitychange', onVisibilityChange); + } + self.clearTimeout(retryTimer); curRequestController.abort(); } @@ -131,8 +133,8 @@ export function fetchEventSource(input: RequestInfo, { try { // check if we need to retry: const interval: any = onerror?.(err) ?? retryInterval; - window.clearTimeout(retryTimer); - retryTimer = window.setTimeout(create, interval); + self.clearTimeout(retryTimer); + retryTimer = self.setTimeout(create, interval); } catch (innerErr) { // we should not retry anymore: dispose();