Skip to content

Commit

Permalink
Makes modular variant of the library available to Web Workers
Browse files Browse the repository at this point in the history
Removes dedicated Web Worker build from webpack. Now automatically
detects whether we are running in a Web Worker context where needed.

Resolves #1514
  • Loading branch information
VeskeR committed Dec 12, 2023
1 parent 717733d commit ae0ea99
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions src/common/lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion src/common/types/IPlatformConfig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ export interface IPlatformConfig {
byteLength: number,
callback: (err: Error | null, result: ArrayBuffer | null) => void
) => void;
isWebworker?: boolean;
}
5 changes: 0 additions & 5 deletions src/platform/web/config-webworker.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/platform/web/index-webworker.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/platform/web/lib/http/request/fetchrequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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';
}

Expand Down
1 change: 0 additions & 1 deletion test/support/browser_file_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 0 additions & 51 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
};

0 comments on commit ae0ea99

Please sign in to comment.