@johnhenry/browsermesh-discovery 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/discovery.mjs +27 -0
package/package.json
CHANGED
package/src/discovery.mjs
CHANGED
|
@@ -904,10 +904,29 @@ const WORKER_SCRIPT = `
|
|
|
904
904
|
};
|
|
905
905
|
`;
|
|
906
906
|
|
|
907
|
+
/**
|
|
908
|
+
* Whether this environment provides SharedWorker.
|
|
909
|
+
*
|
|
910
|
+
* Notably false in **Android WebView**, which does not implement SharedWorker
|
|
911
|
+
* at any API level -- so SharedWorkerRelayStrategy is unavailable there, and
|
|
912
|
+
* on any platform embedding a WebView rather than a full browser. Safari has
|
|
913
|
+
* had it since 16.
|
|
914
|
+
*
|
|
915
|
+
* @returns {boolean}
|
|
916
|
+
*/
|
|
917
|
+
export function supportsSharedWorker() {
|
|
918
|
+
return typeof SharedWorker === 'function'
|
|
919
|
+
}
|
|
920
|
+
|
|
907
921
|
/**
|
|
908
922
|
* Discovers peers within same-origin tabs using a SharedWorker as relay.
|
|
909
923
|
* The SharedWorker maintains a peer registry and forwards messages
|
|
910
924
|
* between connected ports.
|
|
925
|
+
*
|
|
926
|
+
* **Not available in Android WebView**, which ships no SharedWorker at all.
|
|
927
|
+
* Check `supportsSharedWorker()` before starting this strategy, or supply
|
|
928
|
+
* `createWorkerFn` to provide your own. `start()` throws a named error rather
|
|
929
|
+
* than a bare ReferenceError when the API is missing.
|
|
911
930
|
*/
|
|
912
931
|
export class SharedWorkerRelayStrategy extends DiscoveryStrategy {
|
|
913
932
|
/** @type {Function|null} */
|
|
@@ -939,6 +958,14 @@ export class SharedWorkerRelayStrategy extends DiscoveryStrategy {
|
|
|
939
958
|
async start() {
|
|
940
959
|
if (this.active) return
|
|
941
960
|
|
|
961
|
+
if (!this.#createWorkerFn && !supportsSharedWorker()) {
|
|
962
|
+
throw new Error(
|
|
963
|
+
'SharedWorker is not available in this environment (Android WebView does not ' +
|
|
964
|
+
'implement it). Check supportsSharedWorker() before starting this strategy, ' +
|
|
965
|
+
'or pass createWorkerFn to supply your own.',
|
|
966
|
+
)
|
|
967
|
+
}
|
|
968
|
+
|
|
942
969
|
const factory = this.#createWorkerFn ?? (() => {
|
|
943
970
|
const blob = new Blob([WORKER_SCRIPT], { type: 'application/javascript' })
|
|
944
971
|
const url = URL.createObjectURL(blob)
|