1
1
const GATEWAY_URL = "http://localhost:3000/"
2
2
const DATARCHIVE_URL = "http://localhost:3001/"
3
3
const BASE_32_KEY_LENGTH = 52
4
- class DatArchive {
4
+ class DatArchiveProxy {
5
5
6
6
constructor ( url ) {
7
7
this . url = url
8
8
console . log ( url )
9
9
}
10
10
11
- async readFile ( path , filename ) {
11
+ async readFile ( path , options ) {
12
+ // @TODO Support for options.
12
13
console . log ( 'I want to readFile: ' + path ) ;
13
14
const url = this . url
14
15
// const resource = url.replace('dat://','')
15
- const data = { url :url , filename : filename }
16
+ const data = { url :url , filename : path }
16
17
// const data = {url:url}
17
18
const appUrl = DATARCHIVE_URL + 'readFile'
18
19
// const appUrl = DATARCHIVE_URL
@@ -30,7 +31,7 @@ class DatArchive {
30
31
body : JSON . stringify ( data ) , // body data type must match "Content-Type" header
31
32
} )
32
33
// let response = await fetch(appUrl)
33
- let result = await response . json ( ) ;
34
+ let result = await response . text ( ) ;
34
35
// var blob = new Blob([buf], {type: 'image/png'})
35
36
// return JSON.stringify(result);
36
37
return result ;
@@ -139,29 +140,25 @@ class DatArchive {
139
140
}
140
141
}
141
142
142
- async watch ( pathSpec ) {
143
+ async watch ( path , optionalCallback ) {
144
+ // @TODO : Support for watching a specific path.
143
145
const url = this . url
144
146
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 } )
158
157
} )
159
- let result = await response . json ( ) ;
160
- // return JSON.stringify(result);
161
- return result ;
158
+ return EE
162
159
}
163
160
164
- async writeFile ( text , filename ) {
161
+ async writeFile ( filename , text ) {
165
162
console . log ( 'I want to writeFile: ' + text ) ;
166
163
const url = this . url
167
164
// const resource = url.replace('dat://','')
@@ -220,6 +217,6 @@ if (!window.DatArchive) {
220
217
}
221
218
222
219
function doPolyfill ( ) {
223
- window . DatArchive = DatArchive
220
+ window . DatArchive = DatArchiveProxy
224
221
}
225
222
0 commit comments