From ae0ea991eb4c54655be72c991430c88ec2a61672 Mon Sep 17 00:00:00 2001 From: Andrew Bulat Date: Tue, 12 Dec 2023 22:10:42 +0000 Subject: [PATCH] Makes modular variant of the library available to Web Workers Removes dedicated Web Worker build from webpack. Now automatically detects whether we are running in a Web Worker context where needed. Resolves #1514 --- .github/workflows/check.yml | 2 +- README.md | 6 +-- package.json | 1 - src/common/lib/util/utils.ts | 10 ++++ src/common/types/IPlatformConfig.d.ts | 1 - src/platform/web/config-webworker.ts | 5 -- src/platform/web/index-webworker.ts | 4 -- .../web/lib/http/request/fetchrequest.ts | 4 +- test/support/browser_file_list.js | 1 - webpack.config.js | 51 ------------------- 10 files changed, 14 insertions(+), 71 deletions(-) delete mode 100644 src/platform/web/config-webworker.ts delete mode 100644 src/platform/web/index-webworker.ts diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index f131a149da..1c20b7623e 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -21,5 +21,5 @@ jobs: - run: npm ci - run: npm run lint - run: npm run format:check - - run: npx tsc --noEmit ably.d.ts modules.d.ts build/ably-webworker.min.d.ts + - run: npx tsc --noEmit ably.d.ts modules.d.ts - run: npm audit --production diff --git a/README.md b/README.md index 309bae8705..8052723151 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,7 @@ This SDK supports the following platforms: **TypeScript:** see [below](#typescript) -**WebWorkers**: We build a separate bundle which supports running in a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) context. You can import it like this: - -```js -import Ably from 'ably/build/ably-webworker.min'; -``` +**WebWorkers:** Browser bundle supports running in a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) context. You can also use a [modular variant](#modular-tree-shakable-variant) of the library in Web Workers. We regression-test the library against a selection of those (which will change over time, but usually consists of the versions that are supported upstream, plus old versions of IE). diff --git a/package.json b/package.json index 3446ecb1f8..d97404440b 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,6 @@ "async": "ably-forks/async#requirejs", "aws-sdk": "^2.1413.0", "chai": "^4.2.0", - "copy-webpack-plugin": "^11.0.0", "cors": "^2.8.5", "esbuild": "^0.18.10", "esbuild-plugin-umd-wrapper": "^1.0.7", diff --git a/src/common/lib/util/utils.ts b/src/common/lib/util/utils.ts index 9388ad2db2..90af710dcb 100644 --- a/src/common/lib/util/utils.ts +++ b/src/common/lib/util/utils.ts @@ -571,3 +571,13 @@ export function arrEquals(a: any[], b: any[]) { export function throwMissingModuleError(moduleName: keyof ModulesMap): never { throw new ErrorInfo(`${moduleName} module not provided`, 40019, 400); } + +// from: https://stackoverflow.com/a/18002694 +export function isWebWorkerContext(): boolean { + // run this in global scope of window or worker. since window.self = window, we're ok + if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) { + return true; + } else { + return false; + } +} diff --git a/src/common/types/IPlatformConfig.d.ts b/src/common/types/IPlatformConfig.d.ts index 9d678f23bb..50263c7016 100644 --- a/src/common/types/IPlatformConfig.d.ts +++ b/src/common/types/IPlatformConfig.d.ts @@ -27,5 +27,4 @@ export interface IPlatformConfig { byteLength: number, callback: (err: Error | null, result: ArrayBuffer | null) => void ) => void; - isWebworker?: boolean; } diff --git a/src/platform/web/config-webworker.ts b/src/platform/web/config-webworker.ts deleted file mode 100644 index 45a1aa43ba..0000000000 --- a/src/platform/web/config-webworker.ts +++ /dev/null @@ -1,5 +0,0 @@ -import Config from './config'; - -Config.isWebworker = true; - -export default Config; diff --git a/src/platform/web/index-webworker.ts b/src/platform/web/index-webworker.ts deleted file mode 100644 index de3be9b971..0000000000 --- a/src/platform/web/index-webworker.ts +++ /dev/null @@ -1,4 +0,0 @@ -import './config-webworker'; -import Ably from './index'; - -export default Ably; diff --git a/src/platform/web/lib/http/request/fetchrequest.ts b/src/platform/web/lib/http/request/fetchrequest.ts index 68a9febf0f..33214e048f 100644 --- a/src/platform/web/lib/http/request/fetchrequest.ts +++ b/src/platform/web/lib/http/request/fetchrequest.ts @@ -5,7 +5,7 @@ import { RequestCallback, RequestCallbackHeaders, RequestParams } from 'common/t import Platform from 'common/platform'; import Defaults from 'common/lib/util/defaults'; import * as Utils from 'common/lib/util/utils'; -import { getGlobalObject } from 'common/lib/util/utils'; +import { getGlobalObject, isWebWorkerContext } from 'common/lib/util/utils'; function isAblyError(responseBody: unknown, headers: Headers): responseBody is { error?: ErrorInfo } { return !!headers.get('x-ably-errorcode'); @@ -55,7 +55,7 @@ export default function fetchRequest( body: body as any, }; - if (!Platform.Config.isWebworker) { + if (!isWebWorkerContext()) { requestInit.credentials = fetchHeaders.has('authorization') ? 'include' : 'same-origin'; } diff --git a/test/support/browser_file_list.js b/test/support/browser_file_list.js index 7350578e0a..748ab836ad 100644 --- a/test/support/browser_file_list.js +++ b/test/support/browser_file_list.js @@ -3,7 +3,6 @@ window.__testFiles__.files = { 'build/ably-nativescript.js': true, 'build/ably-node.js': true, 'build/ably-reactnative.js': true, - 'build/ably-webworker.min.js': true, 'build/ably.js': true, 'build/ably.min.js': true, 'browser/lib/util/base64.js': true, diff --git a/webpack.config.js b/webpack.config.js index c569d64272..e6c9c04536 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -2,7 +2,6 @@ const path = require('path'); const { BannerPlugin } = require('webpack'); const banner = require('./src/fragments/license'); -const CopyPlugin = require('copy-webpack-plugin'); // This is needed for baseUrl to resolve correctly from tsconfig const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); @@ -64,26 +63,6 @@ const nodeConfig = { }, }; -const browserConfig = { - ...baseConfig, - output: { - ...baseConfig.output, - filename: 'ably.js', - }, - entry: { - index: platformPath('web'), - }, - resolve: { - ...baseConfig.resolve, - fallback: { - crypto: false, - }, - }, - optimization: { - minimize: false, - }, -}; - const nativeScriptConfig = { ...baseConfig, output: { @@ -137,38 +116,8 @@ const reactNativeConfig = { }, }; -const webworkerConfig = { - target: ['webworker', 'es5'], - ...browserConfig, - entry: { - index: platformPath('web', 'index-webworker.ts'), - }, - output: { - ...baseConfig.output, - filename: 'ably-webworker.min.js', - globalObject: 'this', - }, - optimization: { - minimize: true, - }, - performance: { - hints: 'warning', - }, - plugins: [ - new CopyPlugin({ - patterns: [ - { - from: path.resolve(__dirname, 'src', 'fragments', 'ably.d.ts'), - to: path.resolve(__dirname, 'build', 'ably-webworker.min.d.ts'), - }, - ], - }), - ], -}; - module.exports = { node: nodeConfig, - webworker: webworkerConfig, nativeScript: nativeScriptConfig, reactNative: reactNativeConfig, };