braidfs 0.0.44 → 0.0.45

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.
Files changed (2) hide show
  1. package/index.js +25 -13
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -233,25 +233,26 @@ async function watch_files() {
233
233
 
234
234
  console.log('watch files..')
235
235
  watch_files.watcher = require('chokidar').watch(proxy_base).
236
- on('change', x => chokidar_handler(x, false)).
237
- on('add', x => chokidar_handler(x, true))
236
+ on('add', x => chokidar_handler(x, 'add')).
237
+ on('change', x => chokidar_handler(x, 'change')).
238
+ on('unlink', x => chokidar_handler(x, 'unlink'))
238
239
 
239
- async function chokidar_handler(fullpath, added) {
240
+ async function chokidar_handler(fullpath, event) {
240
241
  // Make sure the path is within proxy_base..
241
242
  if (!fullpath.startsWith(proxy_base))
242
243
  return on_watcher_miss(`path ${fullpath} outside ${proxy_base}`)
243
244
 
244
245
  // Make sure the path is to a file, and not a directory
245
- if ((await require('fs').promises.stat(fullpath)).isDirectory())
246
+ if (event != 'unlink' && (await require('fs').promises.stat(fullpath)).isDirectory())
246
247
  return on_watcher_miss(`expected file, got: ${fullpath}`)
247
248
 
248
249
  var path = require('path').relative(proxy_base, fullpath)
249
250
  if (skip_file(path)) return
250
- console.log(`file event: ${path}, added: ${added}`)
251
+ console.log(`file event: ${path}, event: ${event}`)
251
252
 
252
253
  var proxy = await proxy_url.cache[normalize_url(path)]
253
254
 
254
- if (proxy && !added) proxy.signal_file_needs_reading()
255
+ if (proxy && event != 'add') proxy.signal_file_needs_reading()
255
256
  if (!proxy) await trash_file(fullpath, path)
256
257
  }
257
258
  }
@@ -443,13 +444,6 @@ async function proxy_url(url) {
443
444
  // file exists, but not meta file
444
445
  file_last_version = []
445
446
  self.file_last_text = ''
446
- } else {
447
- // file doesn't exist, nor does meta file
448
- file_needs_writing = true
449
- file_last_version = []
450
- self.file_last_text = ''
451
-
452
- await require('fs').promises.writeFile(fullpath, self.file_last_text)
453
447
  }
454
448
  }
455
449
 
@@ -459,6 +453,17 @@ async function proxy_url(url) {
459
453
 
460
454
  file_needs_reading = false
461
455
 
456
+ // check if file is missing, and create it if so..
457
+ if (!(await file_exists(fullpath))) {
458
+ console.log(`file not found, creating: ${fullpath}`)
459
+
460
+ file_needs_writing = true
461
+ file_last_version = []
462
+ self.file_last_text = ''
463
+
464
+ await require('fs').promises.writeFile(fullpath, self.file_last_text)
465
+ }
466
+
462
467
  if (self.file_read_only === null) try { self.file_read_only = await is_read_only(fullpath) } catch (e) { }
463
468
 
464
469
  let text = await require('fs').promises.readFile(
@@ -851,3 +856,10 @@ async function within_file_lock(fullpath, func) {
851
856
  lock()
852
857
  }
853
858
  }
859
+
860
+ async function file_exists(fullpath) {
861
+ try {
862
+ let x = await require('fs').promises.stat(fullpath)
863
+ return x.isFile()
864
+ } catch (e) { }
865
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.44",
3
+ "version": "0.0.45",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",