Repository navigation
Expand file tree
/
Copy pathbrowserfs.ts
More file actions
65 lines (61 loc) · 2.15 KB
/
Copy pathbrowserfs.ts
File metadata and controls
65 lines (61 loc) · 2.15 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
// @ts-nocheck
export class BrowserFS {
ws: Websocket
wrappedfile: File
readcallback: Function
activity = false
activityTimer: ReturnType<typeof setTimeout>
constructor(pathname: string, pubfile: File, authtoken: string, activitycallback: Function) {
this.mountready = new Promise((resolve, reject) => {
this.resolvemount = resolve
})
this.wrappedfile = pubfile
this.readcallback = activitycallback
this.ws = new WebSocket('wss://' + window.location.host + '/browserfs/' + pathname)
this.ws.binaryType = 'arraybuffer'
this.ws.onmessage = this.process_reply
this.ws.onopen = (e) => {
this.ws.send(authtoken)
let ab = new ArrayBuffer(8)
let dv: Dataview = new DataView(ab)
dv.setBigUint64(0, BigInt(pubfile.size))
this.ws.send(ab)
this.resolvemount()
}
}
close = async () => {
await this.ws.close()
}
process_reply = (req) => {
var dvw = new DataView(req.data)
let opcode = dvw.getUint32()
if (opcode == 1) {
try {
if (!this.activity) {
this.activity = true
if (this.readcallback) {
this.readcallback(false)
}
}
let txid = Number(dvw.getUint32(4))
let offset = Number(dvw.getBigUint64(8))
let sz = Number(dvw.getBigUint64(16))
let chnk = this.wrappedfile.slice(offset, offset + sz)
let rsp = new ArrayBuffer(8)
dvw = new DataView(rsp)
dvw.setUint32(0, 2)
dvw.setUint32(4, txid)
const rspblob = new Blob([rsp, chnk])
this.ws.send(rspblob)
} finally {
clearTimeout(this.activityTimer)
this.activityTimer = setTimeout(() => {
this.activity = false
if (this.readcallback) {
this.readcallback(true)
}
}, 250)
}
}
}
}