|
@@ -7,44 +7,54 @@ import { Context } from './Context';
|
7
|
7
|
|
8
|
8
|
const contexts = new Map(); // Map participant id => context
|
9
|
9
|
|
10
|
|
-onmessage = async event => {
|
11
|
|
- const { operation } = event.data;
|
|
10
|
+/**
|
|
11
|
+ * Retrieves the participant {@code Context}, creating it if necessary.
|
|
12
|
+ *
|
|
13
|
+ * @param {string} participantId - The participant whose context we need.
|
|
14
|
+ * @returns {Object} The context.
|
|
15
|
+ */
|
|
16
|
+function getParticipantContext(participantId) {
|
|
17
|
+ if (!contexts.has(participantId)) {
|
|
18
|
+ contexts.set(participantId, new Context(participantId));
|
|
19
|
+ }
|
12
|
20
|
|
13
|
|
- if (operation === 'encode') {
|
14
|
|
- const { readableStream, writableStream, participantId } = event.data;
|
|
21
|
+ return contexts.get(participantId);
|
|
22
|
+}
|
15
|
23
|
|
16
|
|
- if (!contexts.has(participantId)) {
|
17
|
|
- contexts.set(participantId, new Context(participantId));
|
18
|
|
- }
|
19
|
|
- const context = contexts.get(participantId);
|
|
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;
|
20
|
35
|
const transformStream = new TransformStream({
|
21
|
|
- transform: context.encodeFunction.bind(context)
|
|
36
|
+ transform: transformFn.bind(context)
|
22
|
37
|
});
|
23
|
38
|
|
24
|
39
|
readableStream
|
25
|
40
|
.pipeThrough(transformStream)
|
26
|
41
|
.pipeTo(writableStream);
|
27
|
|
- } else if (operation === 'decode') {
|
28
|
|
- const { readableStream, writableStream, participantId } = event.data;
|
|
42
|
+ } else {
|
|
43
|
+ console.error(`Invalid operation: ${operation}`);
|
|
44
|
+ }
|
|
45
|
+}
|
29
|
46
|
|
30
|
|
- if (!contexts.has(participantId)) {
|
31
|
|
- contexts.set(participantId, new Context(participantId));
|
32
|
|
- }
|
33
|
|
- const context = contexts.get(participantId);
|
34
|
|
- const transformStream = new TransformStream({
|
35
|
|
- transform: context.decodeFunction.bind(context)
|
36
|
|
- });
|
|
47
|
+onmessage = async event => {
|
|
48
|
+ const { operation } = event.data;
|
37
|
49
|
|
38
|
|
- readableStream
|
39
|
|
- .pipeThrough(transformStream)
|
40
|
|
- .pipeTo(writableStream);
|
|
50
|
+ if (operation === 'encode' || operation === 'decode') {
|
|
51
|
+ const { readableStream, writableStream, participantId } = event.data;
|
|
52
|
+ const context = getParticipantContext(participantId);
|
|
53
|
+
|
|
54
|
+ handleTransform(context, operation, readableStream, writableStream);
|
41
|
55
|
} else if (operation === 'setKey') {
|
42
|
56
|
const { participantId, key, keyIndex } = event.data;
|
43
|
|
-
|
44
|
|
- if (!contexts.has(participantId)) {
|
45
|
|
- contexts.set(participantId, new Context(participantId));
|
46
|
|
- }
|
47
|
|
- const context = contexts.get(participantId);
|
|
57
|
+ const context = getParticipantContext(participantId);
|
48
|
58
|
|
49
|
59
|
if (key) {
|
50
|
60
|
context.setKey(key, keyIndex);
|
|
@@ -59,3 +69,14 @@ onmessage = async event => {
|
59
|
69
|
console.error('e2ee worker', operation);
|
60
|
70
|
}
|
61
|
71
|
};
|
|
72
|
+
|
|
73
|
+// Operations using RTCRtpScriptTransform.
|
|
74
|
+if (self.RTCTransformEvent) {
|
|
75
|
+ self.onrtctransform = event => {
|
|
76
|
+ const transformer = event.transformer;
|
|
77
|
+ const { operation, participantId } = transformer.options;
|
|
78
|
+ const context = getParticipantContext(participantId);
|
|
79
|
+
|
|
80
|
+ handleTransform(context, operation, transformer.readable, transformer.writable);
|
|
81
|
+ };
|
|
82
|
+}
|