Skip to content

Commit 6e801d0

Browse files
committed
Update dat-archive.js polyfill
1 parent 779ebb1 commit 6e801d0

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

dat-archive.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
const GATEWAY_URL = "http://localhost:3000/"
22
const DATARCHIVE_URL = "http://localhost:3001/"
33
const BASE_32_KEY_LENGTH = 52
4-
class DatArchive {
4+
class DatArchiveProxy {
55

66
constructor(url) {
77
this.url = url
88
console.log(url)
99
}
1010

11-
async readFile(path, filename) {
11+
async readFile(path, options) {
12+
// @TODO Support for options.
1213
console.log('I want to readFile: ' + path);
1314
const url = this.url
1415
// const resource = url.replace('dat://','')
15-
const data = {url:url,filename: filename}
16+
const data = {url:url,filename: path}
1617
// const data = {url:url}
1718
const appUrl = DATARCHIVE_URL + 'readFile'
1819
// const appUrl = DATARCHIVE_URL
@@ -30,7 +31,7 @@ class DatArchive {
3031
body: JSON.stringify(data), // body data type must match "Content-Type" header
3132
})
3233
// let response = await fetch(appUrl)
33-
let result = await response.json();
34+
let result = await response.text();
3435
// var blob = new Blob([buf], {type: 'image/png'})
3536
// return JSON.stringify(result);
3637
return result;
@@ -139,29 +140,25 @@ class DatArchive {
139140
}
140141
}
141142

142-
async watch(pathSpec) {
143+
async watch(path, optionalCallback) {
144+
// @TODO: Support for watching a specific path.
143145
const url = this.url
144146
console.log('I want to watch: ' + url);
145-
const data = {url:url, pathSpec:pathSpec}
146-
const appUrl = DATARCHIVE_URL + 'watch'
147-
let response = await fetch(appUrl,{
148-
method: "POST",
149-
mode: "cors",
150-
cache: "no-cache",
151-
credentials: "same-origin", // include, same-origin, *omit
152-
headers: {
153-
"Content-Type": "application/json; charset=utf-8",
154-
},
155-
redirect: "follow", // manual, *follow, error
156-
referrer: "no-referrer", // no-referrer, *client
157-
body: JSON.stringify(data), // body data type must match "Content-Type" header
147+
const EE = document.createElement('div')
148+
const socket = new WebSocket(`${DATARCHIVE_URL.replace('http:','ws:')}watch/${this.url.replace('dat://','')}`);
149+
socket.addEventListener('message', (event) => {
150+
let message = { type: '', path: '' }
151+
try {
152+
message = JSON.parse(event.data)
153+
} catch (e) { }
154+
if (message.type === 'invalidated') EE.dispatchEvent(new CustomEvent('invalidated'))
155+
if (message.type === 'changed') EE.dispatchEvent(new CustomEvent('changed'))
156+
if (optionalCallback) optionalCallback({path: message.path})
158157
})
159-
let result = await response.json();
160-
// return JSON.stringify(result);
161-
return result;
158+
return EE
162159
}
163160

164-
async writeFile(text, filename) {
161+
async writeFile(filename, text) {
165162
console.log('I want to writeFile: ' + text);
166163
const url = this.url
167164
// const resource = url.replace('dat://','')
@@ -220,6 +217,6 @@ if (!window.DatArchive) {
220217
}
221218

222219
function doPolyfill () {
223-
window.DatArchive = DatArchive
220+
window.DatArchive = DatArchiveProxy
224221
}
225222

0 commit comments

Comments
 (0)