您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Worker.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  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 (sharedContext) {
  14. return sharedContext;
  15. }
  16. if (!contexts.has(participantId)) {
  17. contexts.set(participantId, new Context());
  18. }
  19. return contexts.get(participantId);
  20. }
  21. /**
  22. * Sets an encode / decode transform.
  23. *
  24. * @param {Object} context - The participant context where the transform will be applied.
  25. * @param {string} operation - Encode / decode.
  26. * @param {Object} readableStream - Readable stream part.
  27. * @param {Object} writableStream - Writable stream part.
  28. */
  29. function handleTransform(context, operation, readableStream, writableStream) {
  30. if (operation === 'encode' || operation === 'decode') {
  31. const transformFn = operation === 'encode' ? context.encodeFunction : context.decodeFunction;
  32. const transformStream = new TransformStream({
  33. transform: transformFn.bind(context)
  34. });
  35. readableStream
  36. .pipeThrough(transformStream)
  37. .pipeTo(writableStream);
  38. } else {
  39. console.error(`Invalid operation: ${operation}`);
  40. }
  41. }
  42. onmessage = event => {
  43. const { operation } = event.data;
  44. if (operation === 'initialize') {
  45. const { sharedKey } = event.data;
  46. if (sharedKey) {
  47. sharedContext = new Context({ sharedKey });
  48. }
  49. } else if (operation === 'encode' || operation === 'decode') {
  50. const { readableStream, writableStream, participantId } = event.data;
  51. const context = getParticipantContext(participantId);
  52. handleTransform(context, operation, readableStream, writableStream);
  53. } else if (operation === 'setKey') {
  54. const { participantId, key, keyIndex } = event.data;
  55. const context = getParticipantContext(participantId);
  56. if (key) {
  57. context.setKey(key, keyIndex);
  58. } else {
  59. context.setKey(false, keyIndex);
  60. }
  61. } else if (operation === 'cleanup') {
  62. const { participantId } = event.data;
  63. contexts.delete(participantId);
  64. } else if (operation === 'cleanupAll') {
  65. contexts.clear();
  66. } else {
  67. console.error('e2ee worker', operation);
  68. }
  69. };
  70. // Operations using RTCRtpScriptTransform.
  71. if (self.RTCTransformEvent) {
  72. self.onrtctransform = event => {
  73. const transformer = event.transformer;
  74. const { operation, participantId } = transformer.options;
  75. const context = getParticipantContext(participantId);
  76. handleTransform(context, operation, transformer.readable, transformer.writable);
  77. };
  78. }