forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
89 lines (78 loc) · 2.38 KB
/
worker.js
File metadata and controls
89 lines (78 loc) · 2.38 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This worker manually replicates what Sentry.registerWebWorkerWasm() does.
// the reason for manual replication is that it allows us to test the message-passing protocol
// between worker and main thread independent of SDK implementation details
// in production code you would do: registerWebWorkerWasm({ self });
const origInstantiateStreaming = WebAssembly.instantiateStreaming;
WebAssembly.instantiateStreaming = function instantiateStreaming(response, importObject) {
return Promise.resolve(response).then(res => {
return origInstantiateStreaming(res, importObject).then(rv => {
if (res.url) {
registerModuleAndForward(rv.module, res.url);
}
return rv;
});
});
};
function registerModuleAndForward(module, url) {
const buildId = getBuildId(module);
if (buildId) {
const image = {
type: 'wasm',
code_id: buildId,
code_file: url,
debug_file: null,
debug_id: `${`${buildId}00000000000000000000000000000000`.slice(0, 32)}0`,
};
self.postMessage({
_sentryMessage: true,
_sentryWasmImages: [image],
});
}
}
// Extract build ID from WASM module
function getBuildId(module) {
const sections = WebAssembly.Module.customSections(module, 'build_id');
if (sections.length > 0) {
const buildId = Array.from(new Uint8Array(sections[0]))
.map(b => b.toString(16).padStart(2, '0'))
.join('');
return buildId;
}
return null;
}
// Handle messages from the main thread
self.addEventListener('message', async event => {
function crash() {
throw new Error('WASM error from worker');
}
if (event.data.type === 'load-wasm-and-crash') {
const wasmUrl = event.data.wasmUrl;
try {
const { instance } = await WebAssembly.instantiateStreaming(fetch(wasmUrl), {
env: {
external_func: crash,
},
});
instance.exports.internal_func();
} catch (err) {
self.postMessage({
_sentryMessage: true,
_sentryWorkerError: {
reason: err,
filename: self.location.href,
},
});
}
}
});
self.addEventListener('unhandledrejection', event => {
self.postMessage({
_sentryMessage: true,
_sentryWorkerError: {
reason: event.reason,
filename: self.location.href,
},
});
});
// Let the main thread know that worker is ready
self.postMessage({ _sentryMessage: false, type: 'WORKER_READY' });