From c6acdedc607fa84d009464d1c75106ad4089abd2 Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 27 Jun 2022 06:49:12 +0200 Subject: [PATCH 1/6] don't remove event listener if not added first - when openWhenHidden is true, the event listener is not added. So don't remove it in dispose(). This helps the webworker use case where document does not exist, but can be worked around by setting openWhenHidden to true. --- src/fetch.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/fetch.ts b/src/fetch.ts index 162ea45..8b00b2c 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -86,7 +86,9 @@ export function fetchEventSource(input: RequestInfo, { let retryInterval = DefaultRetryInterval; let retryTimer = 0; function dispose() { - document.removeEventListener('visibilitychange', onVisibilityChange); + if (!openWhenHidden) { + document.removeEventListener('visibilitychange', onVisibilityChange); + } window.clearTimeout(retryTimer); curRequestController.abort(); } From 2532676e69631bcd14a45ecfbe8ccb1854f9e5f8 Mon Sep 17 00:00:00 2001 From: Stef Date: Thu, 30 Jun 2022 21:16:50 +0200 Subject: [PATCH 2/6] use self rather than window - self will work in both browser window and webworker context --- src/fetch.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index 8b00b2c..d7aac9d 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -89,7 +89,7 @@ export function fetchEventSource(input: RequestInfo, { if (!openWhenHidden) { document.removeEventListener('visibilitychange', onVisibilityChange); } - window.clearTimeout(retryTimer); + self.clearTimeout(retryTimer); curRequestController.abort(); } @@ -133,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(); From 75e96deb41f98f5e336489041289154d5a51a15d Mon Sep 17 00:00:00 2001 From: Stef Date: Thu, 30 Jun 2022 21:18:21 +0200 Subject: [PATCH 3/6] add unit tests for fetch --- helpers/abortcontroller.js | 21 +++++++++++++++++++++ jasmine.json | 3 +++ src/fetch.spec.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 helpers/abortcontroller.js create mode 100644 src/fetch.spec.ts 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(); + }); + }); +}); From 628cfbda3d418adc85a9c8aac2cba3b8dd889f9a Mon Sep 17 00:00:00 2001 From: Stef Date: Tue, 5 Jul 2022 06:37:55 +0200 Subject: [PATCH 4/6] activate build for branch --- .github/workflows/node.js.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 912935b..6ae1565 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -5,9 +5,9 @@ name: Node.js CI on: push: - branches: [ main ] + branches: [ main, web_worker_compat ] pull_request: - branches: [ main ] + branches: [ main, web_worker_compat ] jobs: build: @@ -16,14 +16,14 @@ jobs: strategy: matrix: - node-version: [12.x, 14.x] + node-version: [10.x, 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 From ea3008e38c95d6e1a7ef3a5353741861352c6658 Mon Sep 17 00:00:00 2001 From: Stef Date: Tue, 5 Jul 2022 06:44:58 +0200 Subject: [PATCH 5/6] =?UTF-8?q?ex=C3=A9cution=20sur=20self-hosted?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/node.js.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 6ae1565..b69db28 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -12,7 +12,7 @@ on: jobs: build: - runs-on: ubuntu-latest + runs-on: self-hosted strategy: matrix: From a7fad08f386057a4c11aab707b9f1f67fa06f66a Mon Sep 17 00:00:00 2001 From: Stef Date: Tue, 5 Jul 2022 06:53:47 +0200 Subject: [PATCH 6/6] no v10 --- .github/workflows/node.js.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index b69db28..6985ea3 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -16,7 +16,7 @@ jobs: strategy: matrix: - node-version: [10.x, 12.x, 14.x, 15.x] + node-version: [12.x, 14.x, 15.x] steps: - uses: actions/checkout@v3