-
Notifications
You must be signed in to change notification settings - Fork 0
/
dat-archive.js
222 lines (203 loc) · 7.73 KB
/
dat-archive.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const GATEWAY_URL = "http://localhost:3000/"
const DATARCHIVE_URL = "http://localhost:3001/"
const BASE_32_KEY_LENGTH = 52
class DatArchiveProxy {
constructor(url) {
this.url = url
console.log(url)
}
async readFile(path, options) {
// @TODO Support for options.
console.log('I want to readFile: ' + path);
const url = this.url
// const resource = url.replace('dat://','')
const data = {url:url,filename: path}
// const data = {url:url}
const appUrl = DATARCHIVE_URL + 'readFile'
// const appUrl = DATARCHIVE_URL
// const appUrl = GATEWAY_URL + 'readFile'
let response = await fetch(appUrl,{
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin", // include, same-origin, *omit
headers: {
"Content-Type": "application/json; charset=utf-8",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
// let response = await fetch(appUrl)
let result = await response.text();
// var blob = new Blob([buf], {type: 'image/png'})
// return JSON.stringify(result);
return result;
}
async create(document) {
// let document = { title, description, type, author }
console.log('I want to create: ' + JSON.stringify(document));
const url = DATARCHIVE_URL + 'create'
let response = await fetch(url,{
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin", // include, same-origin, *omit
headers: {
"Content-Type": "application/json; charset=utf-8",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(document), // body data type must match "Content-Type" header
})
let result = await response.json();
// return JSON.stringify(result);
const archive = new DatArchive(null)
const mergedArchive = Object.assign(archive, result);
return mergedArchive;
}
async getInfo(opts) {
const url = this.url
console.log('I want to getInfo: ' + url);
// const resource = url.replace('dat://','')
const data = {url:url, opts:opts}
const appUrl = DATARCHIVE_URL + 'getInfo'
let response = await fetch(appUrl,{
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin", // include, same-origin, *omit
headers: {
"Content-Type": "application/json; charset=utf-8",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
let result = await response.json();
// return JSON.stringify(result);
return result;
}
async stat(filename) {
const url = this.url
console.log('I want to stat: ' + url + " for filename: " + filename);
// const resource = url.replace('dat://','')
// var path = url+ "/" + filename
const data = {url:url, filename:filename}
const appUrl = DATARCHIVE_URL + 'stat'
let response = await fetch(appUrl,{
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin", // include, same-origin, *omit
headers: {
"Content-Type": "application/json; charset=utf-8",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
let result = await response.json();
// return JSON.stringify(result);
return result;
}
async mkdir(filename) {
console.log('I want to mkdir: ' + filename);
const url = this.url
// const resource = url.replace('dat://','')
const data = {url:url,filename: filename}
const appUrl = DATARCHIVE_URL + 'mkdir'
let response = await fetch(appUrl,{
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin", // include, same-origin, *omit
headers: {
"Content-Type": "application/json; charset=utf-8",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
let result;
try {
result = await response;
if (response.status === 400) {
return response.statusText;
} else {
return result;
}
} catch (e) {
alert("Error: " + e)
console.log("Error: " + e)
}
}
async watch(path, optionalCallback) {
// @TODO: Support for watching a specific path.
const url = this.url
console.log('I want to watch: ' + url);
const EE = document.createElement('div')
const socket = new WebSocket(`${DATARCHIVE_URL.replace('http:','ws:')}watch/${this.url.replace('dat://','')}`);
socket.addEventListener('message', (event) => {
let message = { type: '', path: '' }
try {
message = JSON.parse(event.data)
} catch (e) { }
if (message.type === 'invalidated') EE.dispatchEvent(new CustomEvent('invalidated'))
if (message.type === 'changed') EE.dispatchEvent(new CustomEvent('changed'))
if (optionalCallback) optionalCallback({path: message.path})
})
return EE
}
async writeFile(filename, text) {
console.log('I want to writeFile: ' + text);
const url = this.url
// const resource = url.replace('dat://','')
const data = {url:url,filename: filename,text: text}
const appUrl = DATARCHIVE_URL + 'writeFile'
let response = await fetch(appUrl,{
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin", // include, same-origin, *omit
headers: {
"Content-Type": "application/json; charset=utf-8",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
let result;
try {
result = await response;
if (response.status === 400) {
return response.statusText;
} else {
return result;
}
} catch (e) {
alert("Error: " + e)
console.log("Error: " + e)
}
}
static readFile(path, opts) {
return new DatArchive().readFile(path, opts)
}
static create(document) {
return new DatArchive().create(document)
}
static getInfo(opts) {
return new DatArchive().getInfo(opts)
}
static mkdir(filename) {
return new DatArchive().mkdir(filename)
}
}
// DatArchive.prototype.readFile = new DatArchive().readFile
// DatArchive.prototype.create = new DatArchive().create
if (!window.DatArchive) {
doPolyfill()
}
function doPolyfill () {
window.DatArchive = DatArchiveProxy
}