braidfs 0.0.74 → 0.0.76

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 -31
  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 = {
@@ -474,11 +475,11 @@ async function proxy_url(url) {
474
475
  }
475
476
 
476
477
  async function send_out(stuff) {
477
- console.log(`send_out ${url} ${JSON.stringify(stuff, null, 4).slice(0, 1000)}`)
478
-
479
478
  if (!start_something()) return
480
479
  if (is_external_link) {
481
480
  try {
481
+ console.log(`send_out ${url} ${JSON.stringify(stuff, null, 4).slice(0, 1000)}`)
482
+
482
483
  let a = new AbortController()
483
484
  aborts.add(a)
484
485
  await braid_fetch(url, {
@@ -494,7 +495,7 @@ async function proxy_url(url) {
494
495
  })
495
496
  aborts.delete(a)
496
497
  } catch (e) {
497
- if (e?.name !== "AbortError") crash(e)
498
+ if (e?.name !== "AbortError") console.log(e)
498
499
  }
499
500
  }
500
501
  finish_something()
@@ -515,9 +516,25 @@ async function proxy_url(url) {
515
516
  () => 1, () => 0)) {
516
517
  // meta file exists
517
518
  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)
519
+ void ({ version: file_last_version, digest: file_last_digest } = Array.isArray(meta) ? { version: meta } : meta)
520
+
521
+ try {
522
+ self.file_last_text = (await braid_text.get(url, { version: file_last_version })).body
523
+ } catch (e) {
524
+ // the version from the meta file doesn't exist..
525
+ if (fullpath === braidfs_config_file) {
526
+ // in the case of the config file,
527
+ // we want to load the current file contents,
528
+ // which we can acheive by setting file_last_version
529
+ // to the latest
530
+ console.log(`WARNING: there was an issue with the config file, and it is reverting to the contents at: ${braidfs_config_file}`)
531
+ var x = await braid_text.get(url, {})
532
+ file_last_version = x.version
533
+ self.file_last_text = x.body
534
+ file_last_digest = sha256(self.file_last_text)
535
+ } else throw new Error(`sync error: version not found: ${file_last_version}`)
536
+ }
519
537
 
520
- self.file_last_text = (await braid_text.get(url, { version: file_last_version })).body
521
538
  file_needs_writing = !v_eq(file_last_version, (await braid_text.get(url, {})).version)
522
539
 
523
540
  // sanity check
@@ -694,8 +711,8 @@ async function proxy_url(url) {
694
711
 
695
712
  self.signal_file_needs_writing()
696
713
  finish_something()
697
- }, e => (e?.name !== "AbortError") && crash(e))
698
- }).catch(e => (e?.name !== "AbortError") && crash(e))
714
+ }, e => (e?.name !== "AbortError") && console.log(e))
715
+ }).catch(e => (e?.name !== "AbortError") && console.log(e))
699
716
  }
700
717
 
701
718
  // send them stuff we have but they don't
@@ -703,7 +720,7 @@ async function proxy_url(url) {
703
720
  async function send_new_stuff() {
704
721
  if (!start_something()) return
705
722
  try {
706
- let a = new AbortController()
723
+ var a = new AbortController()
707
724
  aborts.add(a)
708
725
  var r = await braid_fetch(url, {
709
726
  signal: a.signal,
@@ -721,20 +738,74 @@ async function proxy_url(url) {
721
738
  return
722
739
  }
723
740
 
724
- var chain = Promise.resolve()
741
+ if (r.headers.get('version') == null)
742
+ return console.log(`GOT NO VERSION FROM: ${url}`)
743
+ var parents = JSON.parse(`[${r.headers.get('version')}]`)
744
+
745
+ var bytes = (await braid_text.get_resource(url)).doc.toBytes()
746
+ var [_, versions, __] = braid_text.dt_parse([...bytes])
747
+ var agents = {}
748
+ for (var v of versions) agents[v[0]] = v[1]
749
+
750
+ function we_have_it(version) {
751
+ var m = version.match(/^(.*)-(\d+)$/s)
752
+ var agent = m[1]
753
+ var seq = 1 * m[2]
754
+ return (agents[agent] ?? -1) >= seq
755
+ }
756
+
757
+ if (parents.length && !parents.some(we_have_it)) {
758
+ var min = 0
759
+ var max = versions.length
760
+ var last_good_parents = []
761
+ while (min < max) {
762
+ var i = Math.ceil((min + max)/2)
763
+ parents = i ? [versions[i - 1].join('-')] : []
764
+
765
+ console.log(`min=${min}, max=${max}, i=${i}, parents=${parents}`)
766
+
767
+ var a = new AbortController()
768
+ aborts.add(a)
769
+ var st = Date.now()
770
+ var r = await braid_fetch(url, {
771
+ signal: a.signal,
772
+ method: "HEAD",
773
+ parents,
774
+ headers: {
775
+ Accept: 'text/plain',
776
+ ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname]),
777
+ },
778
+ retry: true
779
+ })
780
+ console.log(`fetched in ${Date.now() - st}`)
781
+ aborts.delete(a)
782
+
783
+ if (r.ok) {
784
+ min = i
785
+ last_good_parents = parents
786
+ } else {
787
+ max = i - 1
788
+ }
789
+ }
790
+ parents = last_good_parents
791
+
792
+ console.log(`found good parents: ${parents}: ${url}`)
793
+ }
794
+
795
+ var in_parallel = create_parallel_promises(10)
725
796
  braid_text.get(url, braid_text_get_options = {
726
- parents: r.headers.get('version') && JSON.parse(`[${r.headers.get('version')}]`),
797
+ parents,
727
798
  merge_type: 'dt',
728
799
  peer,
729
800
  subscribe: async (u) => {
730
801
  if (u.version.length) {
731
802
  self.signal_file_needs_writing()
732
- chain = chain.then(() => send_out({...u, peer}))
803
+ in_parallel(() => send_out({...u, peer}))
733
804
  }
734
805
  },
735
806
  })
736
807
  } catch (e) {
737
- if (e?.name !== "AbortError") crash(e)
808
+ if (e?.name !== "AbortError") console.log(e)
738
809
  }
739
810
  finish_something()
740
811
  }
@@ -892,11 +963,6 @@ async function set_read_only(fullpath, read_only) {
892
963
  }
893
964
  }
894
965
 
895
- function crash(e) {
896
- console.error('CRASHING: ' + e + ' ' + e.stack)
897
- process.exit(1)
898
- }
899
-
900
966
  async function get_file_lock(fullpath) {
901
967
  if (!get_file_lock.locks) get_file_lock.locks = {}
902
968
  if (!get_file_lock.locks[fullpath]) get_file_lock.locks[fullpath] = Promise.resolve()
@@ -924,3 +990,14 @@ async function file_exists(fullpath) {
924
990
  function sha256(x) {
925
991
  return require('crypto').createHash('sha256').update(x).digest('base64')
926
992
  }
993
+
994
+ function create_parallel_promises(N) {
995
+ var q = []
996
+ var n = 0
997
+ return async f => {
998
+ q.push(f)
999
+ n++
1000
+ while (q.length && n <= N) await q.shift()()
1001
+ n--
1002
+ }
1003
+ }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.74",
3
+ "version": "0.0.76",
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": {