clipboard-worker
Base64 codec for clipboard payloads, run off the main thread.
lib/clipboard-worker-bridge.js posts {id, action, payload, mimeType}
and receives {id, success, result, mimeType, byteLength} or
{id, success: false, error}. ENCODE_BINARY_TO_B64 takes an
ArrayBuffer, ENCODE_TEXT_TO_B64 a string, both answering with the base64
text; DECODE_FROM_B64 takes base64 and answers with a string for
text/plain, else with a transferred ArrayBuffer. byteLength is the
decoded size, which the bridge reports and compares against limits.
A multipart download arrives as DECODE_BEGIN, one DECODE_CHUNK per
message and DECODE_END, which alone answers; DECODE_ABORT drops an
unfinished one. Only that keeps the payload off the main thread entirely:
joining the chunks there first builds the whole base64 string, and then
copies it again into the message that carries it here.
Every reply carrying bytes also carries hash, the change-detection digest
of those bytes, computed here because this is where they are already being
walked; the main thread would otherwise run the same per-byte loop over a
payload of any size. HASH_BYTES covers the one caller holding bytes that
did not come through a codec, and REENCODE_PNG normalizes an image to PNG
on an OffscreenCanvas rather than on the page's own.
Variables
CHUNK_SIZE
const CHUNK_SIZE: 32768 = 0x8000;Defined in: clipboard-worker.js:34
Bytes per String.fromCharCode.apply call, keeping its argument list bounded.
HASH_SEED
const HASH_SEED: 5381 = 5381;Defined in: clipboard-worker.js:37
Seed of the djb2 digest the clipboard compares payloads by.
streams
const streams: Map<any, any>;Defined in: clipboard-worker.js:143
Multipart downloads in progress, by request id.
Functions
hashBytes()
function hashBytes(h, bytes): number;Defined in: clipboard-worker.js:45
djb2 over bytes, continuing from h so a digest can span several chunks.
Parameters
| Parameter | Type | Description |
|---|---|---|
h | number | - |
bytes | Uint8Array<ArrayBufferLike> | - |
Returns
number
stringToBase64()
function stringToBase64(text): string;Defined in: clipboard-worker.js:54
Parameters
| Parameter | Type | Description |
|---|---|---|
text | string | - |
Returns
string
Base64 of the UTF-8 encoding of text.
base64ToString()
function base64ToString(base64): object;Defined in: clipboard-worker.js:68
Parameters
| Parameter | Type | Description |
|---|---|---|
base64 | string | - |
Returns
object
The decoded UTF-8 text and its byte size.
text
text: string;byteLength
byteLength: number;base64ToBytes()
function base64ToBytes(base64): Uint8Array<ArrayBufferLike>;Defined in: clipboard-worker.js:82
Parameters
| Parameter | Type | Description |
|---|---|---|
base64 | string | - |
Returns
Uint8Array<ArrayBufferLike>
bytesToBase64()
function bytesToBase64(bytes): string;Defined in: clipboard-worker.js:96
Parameters
| Parameter | Type | Description |
|---|---|---|
bytes | Uint8Array<ArrayBufferLike> | - |
Returns
string
feedStream()
function feedStream(stream, text): void;Defined in: clipboard-worker.js:112
Decodes as much of text as forms whole base64 quartets, appending the
bytes to stream and keeping the rest for the next chunk. A sender is free
to split its base64 anywhere, so a chunk boundary need not fall on one.
Parameters
| Parameter | Type | Description |
|---|---|---|
stream | { parts: Uint8Array<ArrayBufferLike>[]; remainder: string; bytes: number; } | - |
stream.parts | Uint8Array<ArrayBufferLike>[] | - |
stream.remainder | string | - |
stream.bytes | number | - |
text | string | - |
Returns
void
drainStream()
function drainStream(stream): Uint8Array<any>;Defined in: clipboard-worker.js:124
The stream's bytes as one buffer, remainder included.
Parameters
| Parameter | Type |
|---|---|
stream | any |
Returns
Uint8Array<any>
reencodeToPng()
function reencodeToPng(id, blob): Promise<void>;Defined in: clipboard-worker.js:152
Normalizes an image to PNG on an OffscreenCanvas, which is the same decode and encode the page would otherwise run on its own canvas and block on for as long as the image is large.
Parameters
| Parameter | Type | Description |
|---|---|---|
id | number | Request id to answer. |
blob | Blob | The image. |
Returns
Promise<void>