-
Notifications
You must be signed in to change notification settings - Fork 0
/
ne0fs.js
128 lines (124 loc) · 5.31 KB
/
ne0fs.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
fetch('uploader.wasm').then(response => response.arrayBuffer()).then(function (bin) {
document.getElementById('upload').onclick = function () {
const files = document.getElementById('file').files
if (files.length == 0) {
alert('no file selected')
return
}
document.getElementById('status').innerText = 'start uploading ...'
const go = new Go()
WebAssembly.instantiate(bin, go.importObject).then((result) => {
const reader = new FileReader();
const file = files[0]
reader.readAsArrayBuffer(file)
reader.onloadend = function (e) {
var data = new Uint8Array(e.target.result)
global.fs.read = function (fd, buffer, offset, length, position, callback) {
if (fd != 0) {
const err = new Error("not implemented");
err.code = "ENOSYS";
callback(err);
return
}
if (length > data.length) {
length = data.length
}
buffer.set(data.slice(0, length), offset)
data = data.slice(length)
callback(null, length)
}
let stdout = ""
let stderr = ""
const decoder = new TextDecoder("utf-8");
global.fs.writeSync = function (fd, buf) {
switch (fd) {
case 1:
stdout += decoder.decode(buf)
document.getElementById('link').value = stdout
return buf.length
case 2:
stderr += decoder.decode(buf);
const nl = stderr.lastIndexOf("\n");
if (nl != -1) {
document.getElementById('status').innerText = stderr.substr(0, nl)
stderr = stderr.substr(nl + 1);
}
return buf.length
}
}
go.exit = (code) => {
if (code !== 0) {
alert("exit code:", code);
return
}
document.getElementById('status').innerText = 'uploaded, use the displayed link to download'
};
go.argv = ["NE0FS", "-i", "-f", file.name]
go.run(result.instance);
}
});
}
document.getElementById('upload').disabled = false
});
fetch('downloader.wasm').then(response => response.arrayBuffer()).then(function (bin) {
document.getElementById('download').onclick = function () {
const addr = document.getElementById('link').value
if (addr.substr(0, 8) != "ne0fs://") {
alert('not a ne0fs link')
return
}
document.getElementById('status').innerText = 'start downloading ...'
const url = new URL('https://' + addr.substr(8))
const go = new Go()
WebAssembly.instantiate(bin, go.importObject).then((result) => {
let stdout = new Uint8Array(0)
let stderr = ""
const decoder = new TextDecoder("utf-8");
global.fs.writeSync = function (fd, buf) {
switch (fd) {
case 1:
let last = stdout
stdout = new Uint8Array(last.length + buf.length)
stdout.set(last)
stdout.set(buf, last.length)
return buf.length
case 2:
stderr += decoder.decode(buf);
const nl = stderr.lastIndexOf("\n");
if (nl != -1) {
console.log(stderr.substr(0, nl))
document.getElementById('status').innerText = stderr.substr(0, nl)
stderr = stderr.substr(nl + 1);
}
return buf.length
}
}
go.argv = ["NE0FS", "-l", addr, "-o"]
go.exit = (code) => {
if (code !== 0) {
alert("exit code:", code);
return
}
let a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
let blob = new Blob([stdout], { type: 'octet/stream' })
let expr = window.URL.createObjectURL(blob)
a.href = expr
a.download = url.pathname.split('/').pop();
a.click();
window.URL.revokeObjectURL(url);
document.getElementById('status').innerText = 'downloaded'
};
go.run(result.instance);
});
}
document.getElementById('download').disabled = false
});
document.getElementById('copy').onclick = function () {
document.getElementById('link').focus()
document.getElementById('link').select()
document.getElementById('link').setSelectionRange(0, 0x1000)
document.execCommand("copy");
}
document.getElementById('copy').disabled = false