lib/clipboard-worker-bridge
Off-main-thread base64 for clipboard payloads and the clipboard send path, shared by both transports.
A per-byte String.fromCharCode + btoa build of a multi-MB clipboard
blocks the main thread for seconds, freezing the video presentation and
input dispatch that share it, so encode and decode run in
clipboard-worker.js. Both transports emit the identical wire protocol to
the same server handler: cw / cb as a single message, or the multipart
cws+cwd+cwe / cbs+cbd+cbe sequence for large payloads. The
server decodes each data chunk independently, so each raw chunk is
base64-encoded on its own, never the whole payload encoded and then sliced
as a string.
Classes
ClipboardWorkerBridge
Defined in: lib/clipboard-worker-bridge.js:24
Request/response bridge to the clipboard worker.
The worker is created lazily on the first request and every call resolves
with { result, mimeType, byteLength } from the worker's reply.
Constructors
Constructor
new ClipboardWorkerBridge(): ClipboardWorkerBridge;Defined in: lib/clipboard-worker-bridge.js:25
Returns
Properties
worker
worker: any;Defined in: lib/clipboard-worker-bridge.js:26
callbacks
callbacks: Map<any, any>;Defined in: lib/clipboard-worker-bridge.js:27
msgId
msgId: number;Defined in: lib/clipboard-worker-bridge.js:28
Methods
init()
init(): void;Defined in: lib/clipboard-worker-bridge.js:32
Creates the worker when it does not exist yet.
Returns
void
terminate()
terminate(): void;Defined in: lib/clipboard-worker-bridge.js:52
Stops the worker and rejects every pending request with an AbortError.
Returns
void
encodeText()
encodeText(text): Promise<{
result: string;
mimeType: string;
byteLength: number;
}>;Defined in: lib/clipboard-worker-bridge.js:70
Parameters
| Parameter | Type | Description |
|---|---|---|
text | string | UTF-8 text to encode. |
Returns
Promise<{
result: string;
mimeType: string;
byteLength: number;
}>
encodeBinary()
encodeBinary(arrayBuffer): Promise<{
result: string;
mimeType: string;
byteLength: number;
}>;Defined in: lib/clipboard-worker-bridge.js:86
Encodes a buffer with a zero-copy transfer: the buffer is neutered, so callers pass one they own exclusively (a fresh or sliced copy, never a shared view).
Parameters
| Parameter | Type | Description |
|---|---|---|
arrayBuffer | ArrayBuffer | Bytes to encode; unusable afterwards. |
Returns
Promise<{
result: string;
mimeType: string;
byteLength: number;
}>
decode()
decode(base64String, mimeType): Promise<{
result: any;
mimeType: string;
byteLength: number;
}>;Defined in: lib/clipboard-worker-bridge.js:103
Parameters
| Parameter | Type | Description |
|---|---|---|
base64String | string | Payload to decode. |
mimeType | string | Type reported back with the decoded bytes. |
Returns
Promise<{
result: any;
mimeType: string;
byteLength: number;
}>
Functions
encodeClipboardChunk()
function encodeClipboardChunk(worker, bytes): Promise<string>;Defined in: lib/clipboard-worker-bridge.js:123
Base64-encodes one clipboard byte run off the main thread.
A fresh slice gives the worker a buffer it can neuter through zero-copy
transfer; on worker failure it degrades to a chunked main-thread encode,
still far cheaper than a per-byte String.fromCharCode build.
Parameters
| Parameter | Type | Description |
|---|---|---|
worker | ClipboardWorkerBridge | The bridge to encode through. |
bytes | Uint8Array<ArrayBufferLike> | The run to encode; left untouched. |
Returns
Promise<string>
The base64 text.
sendClipboardChunked()
function sendClipboardChunked(
bytes,
mimeType,
io
): Promise<void>;Defined in: lib/clipboard-worker-bridge.js:157
Sends a clipboard payload, as one message when it fits a chunk and as a multipart sequence otherwise.
Each chunk is encoded off the main thread with a yield between chunks, so
a multi-MB clipboard never blocks video presentation or input dispatch.
The transports differ only in the injected send and waitDrain.
Parameters
| Parameter | Type | Description |
|---|---|---|
bytes | Uint8Array<ArrayBufferLike> | The payload. |
mimeType | string | text/plain selects the text messages, anything else the binary ones. |
io | { worker: ClipboardWorkerBridge; send: (message) => void; waitDrain?: () => Promise<boolean | void>; chunkRawBytes: number; nextTid: () => string | number; } | Transport hooks. |
io.worker | ClipboardWorkerBridge | The bridge to encode through. |
io.send | (message) => void | Sends one wire message. |
io.waitDrain? | () => Promise<boolean | void> | Awaited before every chunk for backpressure; resolving false aborts the transfer (the channel closed). |
io.chunkRawBytes | number | Raw bytes per chunk. |
io.nextTid | () => string | number | Allocates the multipart transfer id. |
Returns
Promise<void>