braidfs 0.0.133 → 0.0.134
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +41 -42
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -141,6 +141,10 @@ You can run it with:
|
|
|
141
141
|
async function main() {
|
|
142
142
|
process.on("unhandledRejection", (x) => console.log(`unhandledRejection: ${x.stack}`))
|
|
143
143
|
process.on("uncaughtException", (x) => console.log(`uncaughtException: ${x.stack}`))
|
|
144
|
+
|
|
145
|
+
await braid_text.db_folder_init()
|
|
146
|
+
await braid_blob.init()
|
|
147
|
+
|
|
144
148
|
require('http').createServer(async (req, res) => {
|
|
145
149
|
try {
|
|
146
150
|
// console.log(`${req.method} ${req.url}`)
|
|
@@ -216,45 +220,44 @@ async function main() {
|
|
|
216
220
|
console.log(`daemon started on port ${config.port}`)
|
|
217
221
|
console.log('!! only accessible from localhost !!')
|
|
218
222
|
|
|
219
|
-
sync_url('.braidfs/config')
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
+
sync_url('.braidfs/config')
|
|
224
|
+
braid_text.get('.braidfs/config', {
|
|
225
|
+
subscribe: async update => {
|
|
226
|
+
let prev = config
|
|
223
227
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}
|
|
228
|
+
let x = await braid_text.get('.braidfs/config')
|
|
229
|
+
try {
|
|
230
|
+
config = JSON.parse(x)
|
|
231
|
+
|
|
232
|
+
// did anything get deleted?
|
|
233
|
+
var old_syncs = Object.entries(prev.sync).filter(x => x[1]).map(x => normalize_url(x[0]).replace(/^https?:\/\//, ''))
|
|
234
|
+
var new_syncs = new Set(Object.entries(config.sync).filter(x => x[1]).map(x => normalize_url(x[0]).replace(/^https?:\/\//, '')))
|
|
235
|
+
for (let url of old_syncs.filter(x => !new_syncs.has(x)))
|
|
236
|
+
unsync_url(url)
|
|
237
|
+
|
|
238
|
+
// sync all the new stuff
|
|
239
|
+
for (let x of Object.entries(config.sync)) if (x[1]) sync_url(x[0])
|
|
240
|
+
|
|
241
|
+
// if any auth stuff has changed,
|
|
242
|
+
// have the appropriate connections reconnect
|
|
243
|
+
let changed = new Set()
|
|
244
|
+
// any old domains no longer exist?
|
|
245
|
+
for (let domain of Object.keys(prev.cookies ?? {}))
|
|
246
|
+
if (!config.cookies?.[domain]) changed.add(domain)
|
|
247
|
+
// any new domains not like the old?
|
|
248
|
+
for (let [domain, v] of Object.entries(config.cookies ?? {}))
|
|
249
|
+
if (!prev.cookies?.[domain]
|
|
250
|
+
|| JSON.stringify(prev.cookies[domain]) !== JSON.stringify(v))
|
|
251
|
+
changed.add(domain)
|
|
252
|
+
// ok, have every domain which has changed reconnect
|
|
253
|
+
for (let [path, x] of Object.entries(sync_url.cache))
|
|
254
|
+
if (changed.has(path.split(/\//)[0].split(/:/)[0]))
|
|
255
|
+
(await x).reconnect?.()
|
|
256
|
+
} catch (e) {
|
|
257
|
+
if (x !== '') console.log(`warning: config file is currently invalid.`)
|
|
258
|
+
return
|
|
256
259
|
}
|
|
257
|
-
}
|
|
260
|
+
}
|
|
258
261
|
})
|
|
259
262
|
sync_url('.braidfs/errors')
|
|
260
263
|
|
|
@@ -413,15 +416,12 @@ function unsync_url(url) {
|
|
|
413
416
|
delete unsync_url.cache[url]
|
|
414
417
|
}
|
|
415
418
|
|
|
416
|
-
|
|
419
|
+
function sync_url(url) {
|
|
417
420
|
// normalize url by removing any trailing /index/index/
|
|
418
421
|
var normalized_url = normalize_url(url),
|
|
419
422
|
wasnt_normal = normalized_url != url
|
|
420
423
|
url = normalized_url
|
|
421
424
|
|
|
422
|
-
await braid_text.db_folder_init()
|
|
423
|
-
await braid_blob.init()
|
|
424
|
-
|
|
425
425
|
var is_external_link = url.match(/^https?:\/\//),
|
|
426
426
|
path = is_external_link ? url.replace(/^https?:\/\//, '') : url,
|
|
427
427
|
fullpath = `${sync_base}/${path}`,
|
|
@@ -480,7 +480,6 @@ async function sync_url(url) {
|
|
|
480
480
|
} else throw new Error(`unknown merge-type: ${self.merge_type}`)
|
|
481
481
|
})()
|
|
482
482
|
}
|
|
483
|
-
return
|
|
484
483
|
|
|
485
484
|
async function detect_merge_type() {
|
|
486
485
|
// special case for .braidfs/config and .braidfs/error
|