@mercuryworkshop/bare-mux
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -62,2 +62,3 @@ import { BareHeaders } from './baretypes'; | ||
setManualTransport(functionBody: string): Promise<void>; | ||
getInnerPort(): MessagePort | Promise<MessagePort>; | ||
} | ||
@@ -69,5 +70,5 @@ export declare class BareClient { | ||
*/ | ||
constructor(workerPath?: string); | ||
constructor(worker?: string | MessagePort); | ||
createWebSocket(remote: string | URL, protocols?: string | string[] | undefined, webSocketImpl?: WebSocketImpl, requestHeaders?: BareHeaders, arrayBufferImpl?: typeof ArrayBuffer): WebSocket; | ||
fetch(url: string | URL, init?: RequestInit): Promise<BareResponseFetch>; | ||
} |
@@ -30,4 +30,3 @@ import { BareHeaders, TransferrableResponse } from "./baretypes"; | ||
export type BroadcastMessage = { | ||
type: "getPath" | "path" | "refreshPort"; | ||
path?: string; | ||
type: "refreshPort"; | ||
}; | ||
@@ -38,5 +37,5 @@ export declare class WorkerConnection { | ||
workerPath: string; | ||
constructor(workerPath?: string); | ||
constructor(worker?: string | MessagePort); | ||
createChannel(workerPath?: string, inInit?: boolean): void; | ||
sendMessage(message: WorkerMessage, transferable?: Transferable[]): Promise<WorkerResponse>; | ||
} |
@@ -15,2 +15,5 @@ (function (global, factory) { | ||
const Response = globalThis.Response; | ||
const SharedWorker = globalThis.SharedWorker; | ||
const localStorage = globalThis.localStorage; | ||
const serviceWorker = globalThis.navigator.serviceWorker; | ||
const WebSocketFields = { | ||
@@ -47,21 +50,13 @@ prototype: { | ||
} | ||
function createPort(path, channel, registerHandlers) { | ||
function createPort(path, registerHandlers) { | ||
const worker = new SharedWorker(path, "bare-mux-worker"); | ||
if (registerHandlers) { | ||
// uv removes navigator.serviceWorker so this errors | ||
if (navigator.serviceWorker) { | ||
navigator.serviceWorker.addEventListener("message", event => { | ||
if (event.data.type === "getPort" && event.data.port) { | ||
console.debug("bare-mux: recieved request for port from sw"); | ||
const worker = new SharedWorker(path, "bare-mux-worker"); | ||
event.data.port.postMessage(worker.port, [worker.port]); | ||
} | ||
}); | ||
} | ||
channel.onmessage = (event) => { | ||
if (event.data.type === "getPath") { | ||
console.debug("bare-mux: recieved request for worker path from broadcast channel"); | ||
channel.postMessage({ type: "path", path: path }); | ||
// @ts-expect-error we are using snapshot.ts | ||
serviceWorker.addEventListener("message", (event) => { | ||
if (event.data.type === "getPort" && event.data.port) { | ||
console.debug("bare-mux: recieved request for port from sw"); | ||
const newWorker = new SharedWorker(path, "bare-mux-worker"); | ||
event.data.port.postMessage(newWorker.port, [newWorker.port]); | ||
} | ||
}; | ||
}); | ||
} | ||
@@ -71,5 +66,10 @@ return worker.port; | ||
class WorkerConnection { | ||
constructor(workerPath) { | ||
constructor(worker) { | ||
this.channel = new BroadcastChannel("bare-mux"); | ||
this.createChannel(workerPath, true); | ||
if (worker instanceof MessagePort) { | ||
this.port = worker; | ||
} | ||
else { | ||
this.createChannel(worker, true); | ||
} | ||
} | ||
@@ -93,15 +93,14 @@ createChannel(workerPath, inInit) { | ||
throw new Error("Invalid URL. Must be absolute or start at the root."); | ||
this.port = createPort(workerPath, this.channel, inInit); | ||
this.port = createPort(workerPath, inInit); | ||
console.debug("bare-mux: setting localStorage bare-mux-path to", workerPath); | ||
localStorage["bare-mux-path"] = workerPath; | ||
} | ||
else if (SharedWorker) { | ||
// running in a window, was not passed a workerPath | ||
// ask other bare-mux clients for the workerPath | ||
this.port = new Promise(resolve => { | ||
this.channel.onmessage = (event) => { | ||
if (event.data.type === "path") { | ||
resolve(createPort(event.data.path, this.channel, inInit)); | ||
} | ||
}; | ||
this.channel.postMessage({ type: "getPath" }); | ||
}); | ||
// use sessionStorage for the workerPath | ||
const path = localStorage["bare-mux-path"]; | ||
console.debug("bare-mux: got localStorage bare-mux-path:", path); | ||
if (!path) | ||
throw new Error("Unable to get bare-mux workerPath from localStorage."); | ||
this.port = createPort(path, inInit); | ||
} | ||
@@ -186,2 +185,5 @@ else { | ||
} | ||
getInnerPort() { | ||
return this.worker.port; | ||
} | ||
} | ||
@@ -192,4 +194,4 @@ class BareClient { | ||
*/ | ||
constructor(workerPath) { | ||
this.worker = new WorkerConnection(workerPath); | ||
constructor(worker) { | ||
this.worker = new WorkerConnection(worker); | ||
} | ||
@@ -196,0 +198,0 @@ createWebSocket(remote, protocols = [], webSocketImpl, requestHeaders, arrayBufferImpl) { |
@@ -34,2 +34,4 @@ export declare const fetch: typeof globalThis.fetch; | ||
}; | ||
export declare const localStorage: Storage; | ||
export declare const serviceWorker: ServiceWorkerContainer; | ||
export declare const WebSocketFields: { | ||
@@ -36,0 +38,0 @@ prototype: { |
{ | ||
"name": "@mercuryworkshop/bare-mux", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "", | ||
@@ -13,3 +13,3 @@ "type": "module", | ||
".": { | ||
"import": "./dist/module.js", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js", | ||
@@ -16,0 +16,0 @@ "types": "./dist/index.d.ts" |
@@ -58,1 +58,12 @@ # Bare-Mux | ||
``` | ||
## WebWorker support | ||
Due to limitations in browsers, there is no way for bare-mux to get a connection to the bare-mux SharedWorker while inside a WebWorker. Proxies that use bare-mux must manually pass in a MessagePort to the SharedWorker to be able to use BareClient in a WebWorker. | ||
```js | ||
const connection = new Ultraviolet.BareMuxConnection(); | ||
let port = connection.getInnerPort(); | ||
// this could be a promise, but right now it's only a promise when called inside a service worker | ||
if (port instanceof Promise) port = await port; | ||
// ... transfer it to worker ... | ||
this.bareClient = new BareClient(port) | ||
``` |
Sorry, the diff of this file is not supported yet
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
86236
17
1119
69
2
2
70