You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Worker.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* global TransformStream */
  2. /* eslint-disable no-bitwise */
  3. // Worker for E2EE/Insertable streams.
  4. //
  5. import { Context } from './Context';
  6. import { polyFillEncodedFrameMetadata } from './utils';
  7. const contexts = new Map(); // Map participant id => context
  8. onmessage = async event => {
  9. const { operation } = event.data;
  10. if (operation === 'encode') {
  11. const { readableStream, writableStream, participantId } = event.data;
  12. if (!contexts.has(participantId)) {
  13. contexts.set(participantId, new Context(participantId));
  14. }
  15. const context = contexts.get(participantId);
  16. const transformStream = new TransformStream({
  17. transform: context.encodeFunction.bind(context)
  18. });
  19. readableStream
  20. .pipeThrough(new TransformStream({
  21. transform: polyFillEncodedFrameMetadata // M83 polyfill.
  22. }))
  23. .pipeThrough(transformStream)
  24. .pipeTo(writableStream);
  25. } else if (operation === 'decode') {
  26. const { readableStream, writableStream, participantId } = event.data;
  27. if (!contexts.has(participantId)) {
  28. contexts.set(participantId, new Context(participantId));
  29. }
  30. const context = contexts.get(participantId);
  31. const transformStream = new TransformStream({
  32. transform: context.decodeFunction.bind(context)
  33. });
  34. readableStream
  35. .pipeThrough(new TransformStream({
  36. transform: polyFillEncodedFrameMetadata // M83 polyfill.
  37. }))
  38. .pipeThrough(transformStream)
  39. .pipeTo(writableStream);
  40. } else if (operation === 'setKey') {
  41. const { participantId, key, keyIndex } = event.data;
  42. if (!contexts.has(participantId)) {
  43. contexts.set(participantId, new Context(participantId));
  44. }
  45. const context = contexts.get(participantId);
  46. if (key) {
  47. context.setKey(key, keyIndex);
  48. } else {
  49. context.setKey(false, keyIndex);
  50. }
  51. } else if (operation === 'cleanup') {
  52. const { participantId } = event.data;
  53. contexts.delete(participantId);
  54. } else {
  55. console.error('e2ee worker', operation);
  56. }
  57. };