WebViewWorker.js
2.78 KB
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
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import serializeError from 'serialize-error';
import { subtle } from './compat';
import { parse, stringify } from './serializeBinary';
export class WebViewWorker {
constructor(sendToMain) {
this.sendToMain = sendToMain;
sendToMain('We are ready!');
}
onMainMessage(message) {
return __awaiter(this, void 0, void 0, function* () {
let id;
let method;
let args;
try {
({ id, method, args } = yield parse(message));
}
catch (e) {
yield this.send({
reason: `Couldn't parse data: ${e}`,
});
return;
}
let value;
try {
if (method === 'getRandomValues') {
value = crypto.getRandomValues(args[0]);
}
else {
const methodName = method.split('.')[1];
console.log(methodName, args);
value = yield subtle()[methodName].apply(subtle(), args);
// if we import a crypto key, we want to save how we imported it
// so we can send that back and re-create the key later
if (methodName === 'importKey') {
value._import = {
format: args[0],
keyData: args[1],
};
}
}
}
catch (e) {
yield this.send({ id, reason: serializeError(e) });
return;
}
yield this.send({ id, value });
});
}
send(data) {
return __awaiter(this, void 0, void 0, function* () {
let message;
try {
message = yield stringify(data);
}
catch (e) {
const newData = {
id: data.id,
reason: `stringify error ${e}`,
};
this.sendToMain(JSON.stringify(newData));
return;
}
this.sendToMain(message);
});
}
}
//# sourceMappingURL=WebViewWorker.js.map