Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Worker.js 2.2KB

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