From 57931dfbe9da6121bf0e493cc4350f6986ed5344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Quixada=CC=81?= Date: Sun, 17 Oct 2021 18:06:19 -0400 Subject: [PATCH 01/11] feat: added service worker support --- rollup.config.js | 5 ++--- test/fetch-api/browser/index.html | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 759d274..dfd00e5 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -25,7 +25,7 @@ export default [ name: 'irrelevant', strict: false, banner: outdent(` - // Save the global object in a variable + // Save global object in a variable var __global__ = (typeof globalThis !== 'undefined' && globalThis) || (typeof self !== 'undefined' && self) || @@ -53,8 +53,7 @@ export default [ delete __globalThis__.fetch.polyfill; // Choose between native implementation (__global__) or custom implementation (__globalThis__) - // var ctx = __global__.fetch ? __global__ : __globalThis__; - var ctx = __globalThis__ // this line disable service worker support temporarily + var ctx = __global__.fetch ? __global__ : __globalThis__; exports = ctx.fetch // To enable: import fetch from 'cross-fetch' exports.default = ctx.fetch // For TypeScript consumers without esModuleInterop. diff --git a/test/fetch-api/browser/index.html b/test/fetch-api/browser/index.html index 3c7ec73..c4e45f2 100644 --- a/test/fetch-api/browser/index.html +++ b/test/fetch-api/browser/index.html @@ -19,7 +19,7 @@ - + + + + diff --git a/test/fetch-api/service-worker/run.sh b/test/fetch-api/service-worker/run.sh new file mode 100755 index 0000000..f029c17 --- /dev/null +++ b/test/fetch-api/service-worker/run.sh @@ -0,0 +1,5 @@ +#!/bin/sh +browser="./node_modules/.bin/mocha-headless-chrome" + +npx webpack --config $(dirname "$0")/webpack.config.js && +./bin/server --exec "$browser -f $(dirname $0)/index.html" --closeOnExec diff --git a/test/fetch-api/service-worker/sw-reporter.js b/test/fetch-api/service-worker/sw-reporter.js new file mode 100644 index 0000000..dc75038 --- /dev/null +++ b/test/fetch-api/service-worker/sw-reporter.js @@ -0,0 +1,135 @@ +/** + * A SW Reporter created to be compatible with mocha-headless-chrome's repoter + * See: https://github.com/direct-adv-interfaces/mocha-headless-chrome/blob/273d9b8bc7445ea1196b10ad0eaf0a8bce6cbd5f/lib/runner.js#L68 + */ + +// eslint-disable-next-line no-unused-vars +const addReporterEventListeners = (runResults) => { + const all = [] + const passes = [] + const failures = [] + const pending = [] + + const error = (err) => { + if (!err) return {} + + const res = {} + Object.getOwnPropertyNames(err).forEach((key) => (res[key] = err[key])) + return res + } + + const clean = (test) => ({ + title: test.title, + fullTitle: test.fullTitle(), + duration: test.duration, + err: error(test.err) + }) + + const getResult = (stats) => ({ + result: { + stats: { + tests: all.length, + passes: passes.length, + pending: pending.length, + failures: failures.length, + start: stats.start.toISOString(), + end: stats.end.toISOString(), + duration: stats.duration + }, + tests: all.map(clean), + pending: pending.map(clean), + failures: failures.map(clean), + passes: passes.map(clean) + } + }) + + runResults + .on('pass', (test) => { + passes.push(test) + all.push(test) + }) + .on('fail', (test) => { + failures.push(test) + all.push(test) + }) + .on('pending', (test) => { + pending.push(test) + all.push(test) + }) + .on('end', () => { + const result = getResult(runResults.stats) + const channel = new BroadcastChannel('sw-result') + channel.postMessage(JSON.stringify(result)) + }) +} + +const { Spec } = Mocha.reporters +const { + EVENT_RUN_END, + EVENT_TEST_FAIL, + EVENT_TEST_PASS, + EVENT_TEST_PENDING +} = Mocha.Runner.constants + +function SWReporter (runner, options) { + Spec.call(this, runner, options) + + const all = [] + const passes = [] + const failures = [] + const pending = [] + + const error = (err) => { + if (!err) return {} + + const res = {} + Object.getOwnPropertyNames(err).forEach((key) => (res[key] = err[key])) + return res + } + + const clean = (test) => ({ + title: test.title, + fullTitle: test.fullTitle(), + duration: test.duration, + err: error(test.err) + }) + + const getResult = (stats) => ({ + result: { + stats: { + tests: all.length, + passes: passes.length, + pending: pending.length, + failures: failures.length, + start: stats.start.toISOString(), + end: stats.end.toISOString(), + duration: stats.duration + }, + tests: all.map(clean), + pending: pending.map(clean), + failures: failures.map(clean), + passes: passes.map(clean) + } + }) + + runner + .on(EVENT_TEST_PASS, (test) => { + passes.push(test) + all.push(test) + }) + .on(EVENT_TEST_FAIL, (test) => { + failures.push(test) + all.push(test) + }) + .on(EVENT_TEST_PENDING, (test) => { + pending.push(test) + all.push(test) + }) + .once(EVENT_RUN_END, () => { + const result = getResult(runner.stats) + const channel = new BroadcastChannel('sw-result') + channel.postMessage(JSON.stringify(result)) + }) +} + +Mocha.utils.inherits(SWReporter, Spec) diff --git a/test/fetch-api/service-worker/sw.js b/test/fetch-api/service-worker/sw.js new file mode 100644 index 0000000..b1b3266 --- /dev/null +++ b/test/fetch-api/service-worker/sw.js @@ -0,0 +1,33 @@ +/* eslint-disable no-undef */ +import fetch, { Request, Response, Headers } from '../../..' + +const logChannel = new BroadcastChannel('sw-logger') + +// Redirect all logs to top window +console.log = (...args) => logChannel.postMessage(args) + +importScripts('../../../node_modules/mocha/mocha.js') +importScripts('../../../node_modules/chai/chai.js') +importScripts('./sw-reporter.js') + +importScripts('../api.spec.js') +importScripts('../../module-system/module.spec.js') + +globalThis.expect = chai.expect +globalThis.fetch = fetch +globalThis.Request = Request +globalThis.Response = Response +globalThis.Headers = Headers + +mocha.setup({ + ui: 'bdd', + reporter: null // SWReporter, +}) + +describe('Browser:Fetch:ServiceWorker', () => { + addFetchSuite() + addNativeSuite({ fetch }) +}) + +addReporterEventListeners(mocha.run()) +// mocha.run() diff --git a/test/fetch-api/service-worker/webpack.config.js b/test/fetch-api/service-worker/webpack.config.js new file mode 100644 index 0000000..58cc8d4 --- /dev/null +++ b/test/fetch-api/service-worker/webpack.config.js @@ -0,0 +1,12 @@ +const path = require('path') + +module.exports = { + target: 'webworker', + mode: 'none', + entry: path.join(__dirname, 'sw.js'), + output: { + path: __dirname, + filename: 'sw.bundle.js' + }, + stats: 'none' +} From e5e1bf47d0d8869713a8bc001c17892aaf3477a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Quixada=CC=81?= Date: Sun, 30 Apr 2023 14:29:44 -0400 Subject: [PATCH 04/11] chore: renamed sw-reporter.js to sw.reporter.js --- test/fetch-api/service-worker/index.html | 1 + test/fetch-api/service-worker/sw.js | 2 +- .../fetch-api/service-worker/{sw-reporter.js => sw.reporter.js} | 0 3 files changed, 2 insertions(+), 1 deletion(-) rename test/fetch-api/service-worker/{sw-reporter.js => sw.reporter.js} (100%) diff --git a/test/fetch-api/service-worker/index.html b/test/fetch-api/service-worker/index.html index 3148226..cbb50e1 100644 --- a/test/fetch-api/service-worker/index.html +++ b/test/fetch-api/service-worker/index.html @@ -6,6 +6,7 @@ + Please check console! From 60a48efeaeef3960f86fd10e0e638297aec7b2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Quixada=CC=81?= Date: Sun, 30 Apr 2023 15:02:05 -0400 Subject: [PATCH 06/11] refactor: consolidated make browser and make service-worker into make debug --- Makefile | 18 ++++++++++-------- package.json | 1 - 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 5ee525e..dea105c 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,24 @@ +define debug_list +* Browser: http://127.0.0.1:8000/test/fetch-api/browser/ +* Service Worker: http://127.0.0.1:8000/test/fetch-api/service-worker/ +endef +export debug_list + .PHONY: all all: test lint typecheck -.PHONY: browser -browser: - @./bin/server --exec "npx open-cli http://127.0.0.1:8000/test/fetch-api/browser/" - .PHONY: clean clean: @rm -Rf node_modules dist -.PHONY: service-worker -service-worker: - @./bin/server --exec "npx open-cli http://127.0.0.1:8000/test/fetch-api/service-worker/" - .PHONY: commit commit: npx cz +.PHONY: debug +debug: + @./bin/server --exec "echo $$debug_list" + .PHONY: release release: npx standard-version diff --git a/package.json b/package.json index 7f6645e..74487d0 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,6 @@ "mocha-headless-chrome": "4.0.0", "nock": "13.3.0", "nyc": "15.1.0", - "open-cli": "7.2.0", "rimraf": "5.0.0", "rollup": "3.20.7", "rollup-plugin-copy": "3.4.0", From db4df4d6279bfc9f9890e12feed6fda96daaf0b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Quixada=CC=81?= Date: Sat, 6 May 2023 16:53:03 -0400 Subject: [PATCH 07/11] chore: created cloud worker to fix XmlHttpRequest issue --- examples/cloud-worker/package.json | 2 +- examples/cloud-worker/wrangler.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/cloud-worker/package.json b/examples/cloud-worker/package.json index 49790ad..fb70ac6 100644 --- a/examples/cloud-worker/package.json +++ b/examples/cloud-worker/package.json @@ -1,5 +1,5 @@ { - "name": "cloud-worker", + "name": "cloud-worker-fix", "version": "0.0.0", "devDependencies": { "wrangler": "2.19.0" diff --git a/examples/cloud-worker/wrangler.toml b/examples/cloud-worker/wrangler.toml index 557fd42..b895f3c 100644 --- a/examples/cloud-worker/wrangler.toml +++ b/examples/cloud-worker/wrangler.toml @@ -1,3 +1,3 @@ -name = "cloud-worker" +name = "cloud-worker-fix" main = "src/index.js" compatibility_date = "2023-05-06" From b5b9d257f4c313363098490ed8ce0c2a1b2c649f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Quixada=CC=81?= Date: Sun, 7 May 2023 08:09:06 -0400 Subject: [PATCH 08/11] chore: added debug home page --- Makefile | 13 ++----- test/fetch-api/index.html | 43 ++++++++++++++++++++++++ test/fetch-api/service-worker/index.html | 6 +++- 3 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 test/fetch-api/index.html diff --git a/Makefile b/Makefile index dea105c..71d0a9d 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,3 @@ -define debug_list -* Browser: http://127.0.0.1:8000/test/fetch-api/browser/ -* Service Worker: http://127.0.0.1:8000/test/fetch-api/service-worker/ -endef -export debug_list - .PHONY: all all: test lint typecheck @@ -15,10 +9,6 @@ clean: commit: npx cz -.PHONY: debug -debug: - @./bin/server --exec "echo $$debug_list" - .PHONY: release release: npx standard-version @@ -27,6 +17,9 @@ release: release-alpha: npx standard-version --prerelease alpha +.PHONY: server +server: + @./bin/server --exec "echo Fetch api test suites: http://127.0.0.1:8000/test/fetch-api/" ## # Builds diff --git a/test/fetch-api/index.html b/test/fetch-api/index.html new file mode 100644 index 0000000..32b4b1e --- /dev/null +++ b/test/fetch-api/index.html @@ -0,0 +1,43 @@ + + + + + + + + + + +
+

Fetch API Test Suites

+ +
+

Implementations

+ + +
+
+ + + diff --git a/test/fetch-api/service-worker/index.html b/test/fetch-api/service-worker/index.html index af74381..43dc53a 100644 --- a/test/fetch-api/service-worker/index.html +++ b/test/fetch-api/service-worker/index.html @@ -3,10 +3,14 @@ + - Please check console! +
+

Please check console!

+
+