braidfs 0.0.75 → 0.0.77

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 +108 -33
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -17,20 +17,21 @@ var config = null,
17
17
 
18
18
  if (require('fs').existsSync(proxy_base)) {
19
19
  try {
20
- config = JSON.parse(require('fs').readFileSync(braidfs_config_file, 'utf8'))
21
-
22
- // for 0.0.55 users upgrading to 0.0.56,
23
- // which changes the config "domains" key to "cookies",
24
- // and condenses its structure a bit
25
- if (config.domains) {
26
- config.cookies = Object.fromEntries(Object.entries(config.domains).map(([k, v]) => {
27
- if (v.auth_headers?.Cookie) return [k, v.auth_headers.Cookie]
28
- }).filter(x => x))
29
- delete config.domains
30
- require('fs').writeFileSync(braidfs_config_file, JSON.stringify(config, null, 4))
31
- }
32
- } catch (e) {
33
- return console.log(`Cannot parse the configuration file at: ${braidfs_config_file}`)
20
+ config = require('fs').readFileSync(braidfs_config_file, 'utf8')
21
+ } catch (e) { return console.log(`could not find config file: ${braidfs_config_file}`) }
22
+ try {
23
+ config = JSON.parse(config)
24
+ } catch (e) { return console.log(`could not parse config file: ${braidfs_config_file}`) }
25
+
26
+ // for 0.0.55 users upgrading to 0.0.56,
27
+ // which changes the config "domains" key to "cookies",
28
+ // and condenses its structure a bit
29
+ if (config.domains) {
30
+ config.cookies = Object.fromEntries(Object.entries(config.domains).map(([k, v]) => {
31
+ if (v.auth_headers?.Cookie) return [k, v.auth_headers.Cookie]
32
+ }).filter(x => x))
33
+ delete config.domains
34
+ require('fs').writeFileSync(braidfs_config_file, JSON.stringify(config, null, 4))
34
35
  }
35
36
  } else {
36
37
  config = {
@@ -262,6 +263,10 @@ async function trash_file(fullpath, path) {
262
263
  await require('fs').promises.rename(fullpath, dest)
263
264
 
264
265
  // and log an error
266
+ await log_error(`error: unsynced file ${fullpath}; moved to ${dest}`)
267
+ }
268
+
269
+ async function log_error(text) {
265
270
  var x = await braid_text.get('.braidfs/errors', {}),
266
271
  len = [...x.body].length
267
272
  await braid_text.put('.braidfs/errors', {
@@ -269,7 +274,7 @@ async function trash_file(fullpath, path) {
269
274
  patches: [{
270
275
  unit: 'text',
271
276
  range: `[${len}:${len}]`,
272
- content: `error: unsynced file ${fullpath}; moved to ${dest}\n`
277
+ content: `${text}\n`
273
278
  }]
274
279
  })
275
280
  }
@@ -474,11 +479,11 @@ async function proxy_url(url) {
474
479
  }
475
480
 
476
481
  async function send_out(stuff) {
477
- console.log(`send_out ${url} ${JSON.stringify(stuff, null, 4).slice(0, 1000)}`)
478
-
479
482
  if (!start_something()) return
480
483
  if (is_external_link) {
481
484
  try {
485
+ console.log(`send_out ${url} ${JSON.stringify(stuff, null, 4).slice(0, 1000)}`)
486
+
482
487
  let a = new AbortController()
483
488
  aborts.add(a)
484
489
  await braid_fetch(url, {
@@ -494,7 +499,7 @@ async function proxy_url(url) {
494
499
  })
495
500
  aborts.delete(a)
496
501
  } catch (e) {
497
- if (e?.name !== "AbortError") crash(e)
502
+ if (e?.name !== "AbortError") console.log(e)
498
503
  }
499
504
  }
500
505
  finish_something()
@@ -515,9 +520,25 @@ async function proxy_url(url) {
515
520
  () => 1, () => 0)) {
516
521
  // meta file exists
517
522
  let meta = JSON.parse(await require('fs').promises.readFile(meta_path, { encoding: 'utf8' }))
518
- let _ = ({ version: file_last_version, digest: file_last_digest } = Array.isArray(meta) ? { version: meta } : meta)
523
+ void ({ version: file_last_version, digest: file_last_digest } = Array.isArray(meta) ? { version: meta } : meta)
524
+
525
+ try {
526
+ self.file_last_text = (await braid_text.get(url, { version: file_last_version })).body
527
+ } catch (e) {
528
+ // the version from the meta file doesn't exist..
529
+ if (fullpath === braidfs_config_file) {
530
+ // in the case of the config file,
531
+ // we want to load the current file contents,
532
+ // which we can acheive by setting file_last_version
533
+ // to the latest
534
+ console.log(`WARNING: there was an issue with the config file, and it is reverting to the contents at: ${braidfs_config_file}`)
535
+ var x = await braid_text.get(url, {})
536
+ file_last_version = x.version
537
+ self.file_last_text = x.body
538
+ file_last_digest = sha256(self.file_last_text)
539
+ } else throw new Error(`sync error: version not found: ${file_last_version}`)
540
+ }
519
541
 
520
- self.file_last_text = (await braid_text.get(url, { version: file_last_version })).body
521
542
  file_needs_writing = !v_eq(file_last_version, (await braid_text.get(url, {})).version)
522
543
 
523
544
  // sanity check
@@ -659,9 +680,12 @@ async function proxy_url(url) {
659
680
  subscribe: true,
660
681
  retry: {
661
682
  onRes: (res) => {
662
- if (res.status !== 209) return console.log(
663
- `FAILED TO CONNECT TO: ${url}\n` +
664
- `GOT STATUS CODE: ${res.status}, expected 209.`)
683
+ if (res.status !== 209) {
684
+ log_error(`Can't sync ${url} -- got bad response ${res.status} from server (expected 209)`)
685
+ return console.log(
686
+ `FAILED TO CONNECT TO: ${url}\n` +
687
+ `GOT STATUS CODE: ${res.status}, expected 209.`)
688
+ }
665
689
 
666
690
  console.log(`connected to ${url}`)
667
691
  console.log(` editable = ${res.headers.get('editable')}`)
@@ -694,8 +718,8 @@ async function proxy_url(url) {
694
718
 
695
719
  self.signal_file_needs_writing()
696
720
  finish_something()
697
- }, e => (e?.name !== "AbortError") && crash(e))
698
- }).catch(e => (e?.name !== "AbortError") && crash(e))
721
+ }, e => (e?.name !== "AbortError") && console.log(e))
722
+ }).catch(e => (e?.name !== "AbortError") && console.log(e))
699
723
  }
700
724
 
701
725
  // send them stuff we have but they don't
@@ -703,7 +727,7 @@ async function proxy_url(url) {
703
727
  async function send_new_stuff() {
704
728
  if (!start_something()) return
705
729
  try {
706
- let a = new AbortController()
730
+ var a = new AbortController()
707
731
  aborts.add(a)
708
732
  var r = await braid_fetch(url, {
709
733
  signal: a.signal,
@@ -721,9 +745,65 @@ async function proxy_url(url) {
721
745
  return
722
746
  }
723
747
 
748
+ if (r.headers.get('version') == null) {
749
+ log_error(`Can't sync ${url} -- got no version from server`)
750
+ return console.log(`GOT NO VERSION FROM: ${url}`)
751
+ }
752
+ var parents = JSON.parse(`[${r.headers.get('version')}]`)
753
+
754
+ var bytes = (await braid_text.get_resource(url)).doc.toBytes()
755
+ var [_, versions, __] = braid_text.dt_parse([...bytes])
756
+ var agents = {}
757
+ for (var v of versions) agents[v[0]] = v[1]
758
+
759
+ function we_have_it(version) {
760
+ var m = version.match(/^(.*)-(\d+)$/s)
761
+ var agent = m[1]
762
+ var seq = 1 * m[2]
763
+ return (agents[agent] ?? -1) >= seq
764
+ }
765
+
766
+ if (parents.length && !parents.some(we_have_it)) {
767
+ var min = 0
768
+ var max = versions.length
769
+ var last_good_parents = []
770
+ while (min < max) {
771
+ var i = Math.ceil((min + max)/2)
772
+ parents = i ? [versions[i - 1].join('-')] : []
773
+
774
+ console.log(`min=${min}, max=${max}, i=${i}, parents=${parents}`)
775
+
776
+ var a = new AbortController()
777
+ aborts.add(a)
778
+ var st = Date.now()
779
+ var r = await braid_fetch(url, {
780
+ signal: a.signal,
781
+ method: "HEAD",
782
+ parents,
783
+ headers: {
784
+ Accept: 'text/plain',
785
+ ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname]),
786
+ },
787
+ retry: true
788
+ })
789
+ console.log(`fetched in ${Date.now() - st}`)
790
+ aborts.delete(a)
791
+
792
+ if (r.ok) {
793
+ min = i
794
+ last_good_parents = parents
795
+ } else {
796
+ max = i - 1
797
+ }
798
+ }
799
+ parents = last_good_parents
800
+
801
+ console.log(`found good parents: ${parents}: ${url}`)
802
+ }
803
+
724
804
  var in_parallel = create_parallel_promises(10)
725
805
  braid_text.get(url, braid_text_get_options = {
726
- parents: r.headers.get('version') && JSON.parse(`[${r.headers.get('version')}]`),
806
+ parents,
727
807
  merge_type: 'dt',
728
808
  peer,
729
809
  subscribe: async (u) => {
@@ -734,7 +814,7 @@ async function proxy_url(url) {
734
814
  },
735
815
  })
736
816
  } catch (e) {
737
- if (e?.name !== "AbortError") crash(e)
817
+ if (e?.name !== "AbortError") console.log(e)
738
818
  }
739
819
  finish_something()
740
820
  }
@@ -892,11 +972,6 @@ async function set_read_only(fullpath, read_only) {
892
972
  }
893
973
  }
894
974
 
895
- function crash(e) {
896
- console.error('CRASHING: ' + e + ' ' + e.stack)
897
- process.exit(1)
898
- }
899
-
900
975
  async function get_file_lock(fullpath) {
901
976
  if (!get_file_lock.locks) get_file_lock.locks = {}
902
977
  if (!get_file_lock.locks[fullpath]) get_file_lock.locks[fullpath] = Promise.resolve()
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.75",
3
+ "version": "0.0.77",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",
7
7
  "homepage": "https://braid.org",
8
8
  "dependencies": {
9
9
  "braid-http": "^1.3.73",
10
- "braid-text": "^0.2.11",
10
+ "braid-text": "^0.2.14",
11
11
  "chokidar": "^3.6.0"
12
12
  },
13
13
  "bin": {