1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- /* global TransformStream */
- /* eslint-disable no-bitwise */
-
- // Worker for E2EE/Insertable streams.
-
- import { Context } from './Context';
- import { polyFillEncodedFrameMetadata } from './utils';
-
- const contexts = new Map(); // Map participant id => context
-
- onmessage = async event => {
- const { operation } = event.data;
-
- if (operation === 'encode') {
- const { readableStream, writableStream, participantId } = event.data;
-
- if (!contexts.has(participantId)) {
- contexts.set(participantId, new Context(participantId));
- }
- const context = contexts.get(participantId);
- const transformStream = new TransformStream({
- transform: context.encodeFunction.bind(context)
- });
-
- readableStream
- .pipeThrough(new TransformStream({
- transform: polyFillEncodedFrameMetadata // M83 polyfill.
- }))
- .pipeThrough(transformStream)
- .pipeTo(writableStream);
- } else if (operation === 'decode') {
- const { readableStream, writableStream, participantId } = event.data;
-
- if (!contexts.has(participantId)) {
- contexts.set(participantId, new Context(participantId));
- }
- const context = contexts.get(participantId);
- const transformStream = new TransformStream({
- transform: context.decodeFunction.bind(context)
- });
-
- readableStream
- .pipeThrough(new TransformStream({
- transform: polyFillEncodedFrameMetadata // M83 polyfill.
- }))
- .pipeThrough(transformStream)
- .pipeTo(writableStream);
- } else if (operation === 'setKey') {
- const { participantId, key, keyIndex } = event.data;
-
- if (!contexts.has(participantId)) {
- contexts.set(participantId, new Context(participantId));
- }
- const context = contexts.get(participantId);
-
- if (key) {
- context.setKey(key, keyIndex);
- } else {
- context.setKey(false, keyIndex);
- }
- } else if (operation === 'cleanup') {
- const { participantId } = event.data;
-
- contexts.delete(participantId);
- } else {
- console.error('e2ee worker', operation);
- }
- };
|