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.6KB

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