-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathPaneWithWasmPack.tsx
52 lines (44 loc) · 1.52 KB
/
PaneWithWasmPack.tsx
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
import React from 'react';
import Header from './Header';
import SimplePane, { SimplePaneProps } from './SimplePane';
import { FunctionalIFrameComponent } from './Container';
interface PaneWithWasmPackProps extends SimplePaneProps {
success?: boolean;
wasm_js?: string;
wasm_bg?: string;
}
function base64ToByteArray(src: string) {
const decode = atob(src);
const byteNumbers = new Array(decode.length);
for (let i = 0; i < decode.length; i++) {
byteNumbers[i] = decode.charCodeAt(i);
}
return new Uint8Array(byteNumbers);
}
function createObjectURL(src: ArrayBuffer | string, mime: string) {
return URL.createObjectURL(new Blob([src], { type: mime }));
}
function createEntryJS(wasm_js: string, wasm_bg: string, success: boolean) {
if (!success) {
return '';
}
const wasmJS = atob(wasm_js);
const bgWasm = base64ToByteArray(wasm_bg);
const wasmJSBlob = createObjectURL(wasmJS, 'application/javascript');
const bgWasmBlob = createObjectURL(bgWasm, 'application/wasm');
const entryJS = `
import init from '${wasmJSBlob}';
await init('${bgWasmBlob}');
`;
return createObjectURL(entryJS, 'application/javascript');
}
const PaneWithWasmPack: React.SFC<PaneWithWasmPackProps> = ({ wasm_js, wasm_bg, success, ...rest }) => (
<SimplePane {...rest}>
<div className="output-result">
<Header label="Result" />
<FunctionalIFrameComponent url={createEntryJS(wasm_js, wasm_bg, success)}>
</FunctionalIFrameComponent>
</div>
</SimplePane>
);
export default PaneWithWasmPack;