summaryrefslogtreecommitdiff
path: root/viewer/main.ts
blob: 5aa1df688c8dbaf6743207aab304afbf23330d73 (plain)
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
/// <reference lib="dom" />

const canvas = document.createElement("canvas")
canvas.style.position = "absolute"
canvas.style.top = "0px"
canvas.style.left = "0px"
canvas.style.width = "100vw"
canvas.style.height = "100vh"
document.body.append(canvas)
const ctx = canvas.getContext("2d")!

function draw() {
    ctx.fillStyle = "black"
    ctx.fillRect(0, 0, canvas.width, canvas.height)
}

canvas.addEventListener("resize", () => draw())
draw()

class Resource<T> {
    constructor(public hash: Uint8Array) { }
    toString() {
        return Array.from(this.hash)
            .map(e => e.toString(16).padStart(2, "0"))
            .join("")
    }
    async download_raw() {
        const res = await fetch(`http://127.0.0.1:28556/${this.toString()}`)
        if (!res.ok) throw new Error("aaaa");
        return await res.bytes()
    }
}

function read_res_table(buffer: Uint8Array): { [key: string]: Uint8Array[] } {
    const view = new DataView(buffer.buffer)
    let p = 0
    const out: { [key: string]: Uint8Array[] } = {}
    while (p < view.byteLength) {
        const key_len = view.getInt16(p, true)
        p += 2
        const value_len = view.getInt16(p, true)
        p += 2
        const key = String.fromCharCode(...buffer.slice(p, p + key_len))
        p += key_len
        const value = buffer.slice(p, p + value_len)
        p += value_len
        out[key] ??= []
        out[key].push(value)
    }
    return out
}

async function init() {
    const resp = await fetch("http://127.0.0.1:28556/entry")
    if (!resp.ok) throw new Error("aaaa");
    const entry_res = new Resource(await resp.bytes())

    const entry = await entry_res.download_raw()
    console.log(read_res_table(entry))
}
init()