This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Worker.js
175 lines (169 loc) · 3.9 KB
/
Worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {Context} from "./lib/Context.js";
import {VP8PayloadHeader} from "./lib/VP8PayloadHeader.js";
import {Utils} from "./lib/Utils.js";
class TaskQueue
{
constructor()
{
this.tasks = [];
this.running = false;
}
enqueue(promise,callback,error)
{
//enqueue task
this.tasks.push({promise,callback,error});
//Try run
this.run();
}
async run()
{
//If already running
if (this.running)
//Nothing
return;
//Running
this.running = true;
//Run all pending tasks
while(this.tasks.length)
{
try {
//Wait for first promise to finish
const result = await this.tasks[0].promise;
//Run callback
this.tasks[0].callback(result);
} catch(e) {
//Run error callback
this.tasks[0].error(e);
}
//Remove task from queue
this.tasks.shift();
}
//Ended
this.running = false;
}
}
let context;
onmessage = async (event) => {
//Get data
const {transId,cmd,args} = event.data;
try {
let result = true;
//Depending on the cmd
switch(event.data.cmd)
{
case "init":
{
//Get info
const {senderId, config} = args;
//Crate context
context = new Context(senderId, config);
break;
}
case "encrypt":
{
//The recrypt queue
const tasks = new TaskQueue();
//Get event data
const{id, kind, readableStream, writableStream} = args;
//Create transform stream foo encrypting
const transformStream = new TransformStream({
transform: async (chunk, controller)=>{
//Nothing in clear
let skip = 0;
//Check if it is video and we are skipping vp8 payload header
if (kind=="video" && context.isSkippingVp8PayloadHeader())
{
//Get VP8 header
const vp8 = VP8PayloadHeader.parse(chunk.data);
//Skip it
skip = vp8.byteLength;
}
//Enqueue task
tasks.enqueue (
context.encrypt(kind, id, chunk.data, skip),
(encrypted) => {
//Set back encrypted payload
chunk.data = encrypted.buffer;
//write back
controller.enqueue(chunk);
},
(error)=>{
//TODO: handle errors
}
);
}
});
//Encrypt
readableStream
.pipeThrough(transformStream)
.pipeTo(writableStream);
break;
}
case "decrypt":
{
//The recrypt queue
const tasks = new TaskQueue();
//Last reveiced senderId
let senderId = -1;
//Get event data
const{id, kind, readableStream, writableStream} = args;
//Create transform stream for encrypting
const transformStream = new TransformStream({
transform: async (chunk, controller)=>{
//Nothing in clear
let skip = 0;
//Check if it is video and we are skipping vp8 payload header
if (kind=="video" && context.isSkippingVp8PayloadHeader())
{
//Get VP8 header
const vp8 = VP8PayloadHeader.parse(chunk.data);
//Skip it
skip = vp8.byteLength;
}
//Enqueue task
tasks.enqueue (
context.decrypt(kind, id, chunk.data, skip),
(decrypted) => {
//Set back decrypted payload
chunk.data = decrypted.buffer;
//write back
controller.enqueue(chunk);
//If it is a sender
if (decrypted.senderId!=senderId)
{
//Store it
senderId = decrypted.senderId;
//Launch event
postMessage ({event: {
name : "authenticated",
data : {
id : id,
senderId : senderId
}
}});
}
},
(error)=>{
//TODO: handle errors
}
);
}
});
//Decrypt
readableStream
.pipeThrough(transformStream)
.pipeTo(writableStream);
break;
}
default:
//Excute "cmd" method on context
result = await context[cmd](...args || []);
}
//Send result back
postMessage ({transId,result});
} catch (error) {
console.error(error);
//Send error back
postMessage({transId,error});
}
};