Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Worker.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* eslint-disable no-bitwise */
  2. // Worker for E2EE/Insertable streams.
  3. import { Context } from './Context';
  4. const contexts = new Map(); // Map participant id => context
  5. let sharedContext;
  6. let enabled = false;
  7. /**
  8. * Retrieves the participant {@code Context}, creating it if necessary.
  9. *
  10. * @param {string} participantId - The participant whose context we need.
  11. * @returns {Object} The context.
  12. */
  13. function getParticipantContext(participantId) {
  14. if (sharedContext) {
  15. return sharedContext;
  16. }
  17. if (!contexts.has(participantId)) {
  18. const context = new Context();
  19. context.setEnabled(enabled);
  20. contexts.set(participantId, context);
  21. }
  22. return contexts.get(participantId);
  23. }
  24. /**
  25. * Sets an encode / decode transform.
  26. *
  27. * @param {Object} context - The participant context where the transform will be applied.
  28. * @param {string} operation - Encode / decode.
  29. * @param {Object} readableStream - Readable stream part.
  30. * @param {Object} writableStream - Writable stream part.
  31. */
  32. function handleTransform(context, operation, readableStream, writableStream) {
  33. if (operation === 'encode' || operation === 'decode') {
  34. const transformFn = operation === 'encode' ? context.encodeFunction : context.decodeFunction;
  35. const transformStream = new TransformStream({
  36. transform: transformFn.bind(context)
  37. });
  38. readableStream
  39. .pipeThrough(transformStream)
  40. .pipeTo(writableStream);
  41. } else {
  42. console.error(`Invalid operation: ${operation}`);
  43. }
  44. }
  45. onmessage = event => {
  46. const { operation } = event.data;
  47. if (operation === 'initialize') {
  48. const { sharedKey } = event.data;
  49. if (sharedKey) {
  50. sharedContext = new Context({ sharedKey });
  51. }
  52. } else if (operation === 'encode' || operation === 'decode') {
  53. const { readableStream, writableStream, participantId } = event.data;
  54. const context = getParticipantContext(participantId);
  55. handleTransform(context, operation, readableStream, writableStream);
  56. } else if (operation === 'setEnabled') {
  57. enabled = event.data.enabled;
  58. contexts.forEach(context => context.setEnabled(enabled));
  59. } else if (operation === 'setKey') {
  60. const { participantId, key, keyIndex } = event.data;
  61. const context = getParticipantContext(participantId);
  62. if (key) {
  63. context.setKey(key, keyIndex);
  64. } else {
  65. context.setKey(false, keyIndex);
  66. }
  67. } else if (operation === 'cleanup') {
  68. const { participantId } = event.data;
  69. contexts.delete(participantId);
  70. } else if (operation === 'cleanupAll') {
  71. contexts.clear();
  72. } else {
  73. console.error('e2ee worker', operation);
  74. }
  75. };
  76. // Operations using RTCRtpScriptTransform.
  77. if (self.RTCTransformEvent) {
  78. self.onrtctransform = event => {
  79. const transformer = event.transformer;
  80. const { operation, participantId } = transformer.options;
  81. const context = getParticipantContext(participantId);
  82. handleTransform(context, operation, transformer.readable, transformer.writable);
  83. };
  84. }