Selkies
Developer ReferenceWeb client coreLib

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

ClipboardWorkerBridge

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
ParameterTypeDescription
textstringUTF-8 text to encode.
Returns

Promise<{ result: string; mimeType: string; byteLength: number; }>

encodeBinary()
encodeBinary(arrayBuffer, hash): Promise<{
  result: string;
  mimeType: string;
  byteLength: number;
  hash: number;
}>;

Defined in: lib/clipboard-worker-bridge.js:88

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
ParameterTypeDescription
arrayBufferArrayBufferBytes to encode; unusable afterwards.
hashnumberDigest to continue, so a payload sent as several chunks is digested as the one payload it is.
Returns

Promise<{ result: string; mimeType: string; byteLength: number; hash: number; }>

hashBytes()
hashBytes(arrayBuffer): Promise<{
  byteLength: number;
  hash: number;
}>;

Defined in: lib/clipboard-worker-bridge.js:105

Digests bytes that reached the page without passing through a codec.

Parameters
ParameterTypeDescription
arrayBufferArrayBufferBytes to digest; unusable afterwards.
Returns

Promise<{ byteLength: number; hash: number; }>

reencodePng()
reencodePng(blob): Promise<{
  result: Blob;
  mimeType: string;
  byteLength: number;
}>;

Defined in: lib/clipboard-worker-bridge.js:119

Re-encodes an image as PNG off the main thread.

Parameters
ParameterTypeDescription
blobBlobThe image.
Returns

Promise<{ result: Blob; mimeType: string; byteLength: number; }>

decode()
decode(base64String, mimeType): Promise<{
  result: any;
  mimeType: string;
  byteLength: number;
}>;

Defined in: lib/clipboard-worker-bridge.js:133

Parameters
ParameterTypeDescription
base64StringstringPayload to decode.
mimeTypestringType reported back with the decoded bytes.
Returns

Promise<{ result: any; mimeType: string; byteLength: number; }>

decodeStream()
decodeStream(mimeType): object;

Defined in: lib/clipboard-worker-bridge.js:151

Opens a multipart download the worker accumulates.

Each chunk is handed over as it arrives, so the page never holds the whole base64 payload, let alone joins it into one string and copies that into a message.

Parameters
ParameterTypeDescription
mimeTypestringType reported back with the decoded bytes.
Returns

object

push
push: (base64) => void;
Parameters
ParameterType
base64string
Returns

void

finish
finish: () => Promise<{
  result: any;
  mimeType: string;
  byteLength: number;
  hash: number;
}>;
Returns

Promise<{ result: any; mimeType: string; byteLength: number; hash: number; }>

abort
abort: () => void;
Returns

void

Variables

CLIPBOARD_HASH_SEED

const CLIPBOARD_HASH_SEED: 5381 = 5381;

Defined in: lib/clipboard-worker-bridge.js:177

Seed of the digest the clipboard compares payloads by; matches the worker's.

Functions

encodeClipboardChunk()

function encodeClipboardChunk(
   worker, 
   bytes, 
   hash
): Promise<{
  b64: string;
  hash: number;
}>;

Defined in: lib/clipboard-worker-bridge.js:192

Base64-encodes one clipboard byte run off the main thread, digesting it on the way through.

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

ParameterTypeDescription
workerClipboardWorkerBridgeThe bridge to encode through.
bytesUint8Array<ArrayBufferLike>The run to encode; left untouched.
hashnumberDigest to continue across chunks.

Returns

Promise<{ b64: string; hash: number; }>

The base64 text and the digest through this run, undefined once any chunk fell back.


sendClipboardChunked()

function sendClipboardChunked(
   bytes, 
   mimeType, 
   io
): Promise<number>;

Defined in: lib/clipboard-worker-bridge.js:230

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, so a multi-MB clipboard never blocks video presentation or input dispatch, and the round trip to the worker is itself the yield between chunks. The transports differ only in the injected send and waitDrain, which is what keeps a transfer from queueing ahead of the audio and input sharing the connection.

Parameters

ParameterTypeDescription
bytesUint8Array<ArrayBufferLike>The payload.
mimeTypestringtext/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.workerClipboardWorkerBridgeThe bridge to encode through.
io.send(message) => voidSends one wire message.
io.waitDrain?() => Promise<boolean | void>Awaited before every chunk for backpressure; resolving false aborts the transfer (the channel closed).
io.chunkRawBytesnumberRaw bytes per chunk.
io.nextTid() => string | numberAllocates the multipart transfer id.

Returns

Promise<number>

The payload's digest, or undefined where the worker was unavailable or the transfer was cut short.

On this page

Edit on GitHub