-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-test.mjs
67 lines (63 loc) · 1.96 KB
/
web-test.mjs
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
import forest from './forest/index.mjs'
import ioWeb from './io/web.mjs'
import index from './index.mjs'
/** @typedef {import('./forest/index.mjs').ForestNodeId} ForestNodeId */
const { get } = forest
const { web } = ioWeb
const { fetchRead } = index
const webIo = web(d => ({
log: text => {
// @ts-ignore
d.getElementById('log').innerText += `${text}\n`
},
error: text => {
// @ts-ignore
d.getElementById('log').innerText += `${text}\n`
}
}))
const d = webIo.document
// @ts-ignore
d.getElementById('download').addEventListener('click', () => {
reset()
// @ts-ignore
const hash = d.getElementById('input-hash').value
// @ts-ignore
const host = d.getElementById('input-host').value
let buffer = new Uint8Array()
const fRead = fetchRead(host)(webIo)
/** @type {(forestNodeId: ForestNodeId) => Promise<Uint8Array>} */
const read = forestNodeId => {
webIo.console.log(`read from ${forestNodeId}`)
return fRead(forestNodeId)
}
/** @type {(b: Uint8Array) => Promise<void>} */
const write = async (b) => {
webIo.console.log(`write ${b.length}`)
buffer = new Uint8Array([...buffer, ...b])
}
get({ read, write })(hash).then(exitCode => {
if (exitCode !== null) {
webIo.console.error(`error exit code = ${exitCode}`)
return
}
if (buffer[0] === 0xff && buffer[1] === 0xd8) {
const image = new Blob([buffer], { type: 'image/jpeg' });
const imageUrl = URL.createObjectURL(image);
// @ts-ignore
d.getElementById('output-image').style.display = 'block'
// @ts-ignore
d.getElementById('output-image').src = imageUrl;
return
}
// @ts-ignore
d.getElementById('output-text').style.display = 'block'
// @ts-ignore
d.getElementById('output-text').innerText = new TextDecoder().decode(buffer)
})
});
const reset = () => {
// @ts-ignore
d.getElementById('output-image').style.display = 'none'
// @ts-ignore
d.getElementById('output-text').style.display = 'none'
}