braidfs 0.0.112 → 0.0.114

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 +33 -22
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -71,10 +71,12 @@ if (argv.length === 1 && argv[0].match(/^(run|serve)$/)) {
71
71
  for (let i = 0; i < argv.length; i += 2) {
72
72
  var sync = argv[i] === 'sync',
73
73
  url = argv[i + 1]
74
- if (!url.match(/^https?:\/\//)) {
75
- if (url.startsWith('/')) url = require('path').relative(sync_base, url)
76
- url = `https://${url}`
74
+
75
+ if (!is_well_formed_absolute_url(url)) {
76
+ console.log(`malformed url: ${url}`)
77
+ return
77
78
  }
79
+
78
80
  console.log(`${sync ? '' : 'un'}subscribing ${sync ? 'to' : 'from'} ${url}`)
79
81
  try {
80
82
  var res = await braid_fetch(`http://localhost:${config.port}/.braidfs/config`, {
@@ -769,15 +771,17 @@ async function sync_url(url) {
769
771
 
770
772
  await prev_disconnect?.()
771
773
  if (freed || closed) return
774
+
775
+ await reconnect_rate_limiter.get_turn(url)
776
+ if (freed || closed) return
772
777
 
773
- async function retry(e) {
778
+ function retry(e) {
774
779
  if (freed || closed) return
775
780
  var p = self.disconnect()
776
781
 
777
782
  console.log(`reconnecting in ${waitTime}s: ${url} after error: ${e}`)
778
783
  last_connect_timer = setTimeout(async () => {
779
784
  await p
780
- await reconnect_rate_limiter.get_turn(url)
781
785
  last_connect_timer = null
782
786
  connect()
783
787
  }, waitTime * 1000)
@@ -803,11 +807,7 @@ async function sync_url(url) {
803
807
  ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname])
804
808
  },
805
809
  })
806
- } catch (e) {
807
- if (freed || closed) return
808
- aborts.delete(a)
809
- throw e
810
- }
810
+ } catch (e) { retry(e) }
811
811
  }
812
812
 
813
813
  async function send_out(stuff) {
@@ -815,8 +815,7 @@ async function sync_url(url) {
815
815
 
816
816
  console.log(`send_out ${url} ${JSON.stringify(stuff, null, 4).slice(0, 1000)}`)
817
817
 
818
- var r = await my_fetch({ method: "PUT", ...stuff,
819
- retry: { retryRes: r => r.status !== 401 && r.status !== 403 }})
818
+ var r = await my_fetch({ method: "PUT", ...stuff })
820
819
  if (freed || closed) return
821
820
 
822
821
  // the server has acknowledged this version,
@@ -824,7 +823,7 @@ async function sync_url(url) {
824
823
  if (r.ok) self.update_fork_point(stuff.version, stuff.parents)
825
824
 
826
825
  // if we're not authorized,
827
- if (r.status == 401 || r.status == 403) {
826
+ else if (r.status == 401 || r.status == 403) {
828
827
  // and it's one of our versions (a local edit),
829
828
  if (self.peer === braid_text.decode_version(stuff.version[0])[0]) {
830
829
  // then revert it
@@ -833,6 +832,9 @@ async function sync_url(url) {
833
832
  sync_url(url)
834
833
  }
835
834
  }
835
+
836
+ // on other errors, restart the connection
837
+ else retry(new Error(`unexpected PUT status: ${r.status}`))
836
838
  }
837
839
 
838
840
  async function find_fork_point() {
@@ -843,11 +845,12 @@ async function sync_url(url) {
843
845
  if (self.fork_point) {
844
846
  var r = await my_fetch({
845
847
  method: "HEAD",
846
- version: self.fork_point,
847
- retry: { retryRes: r =>
848
- r.status !== 309 && r.status !== 500 }
848
+ version: self.fork_point
849
849
  })
850
850
  if (freed || closed) return
851
+
852
+ if (!r.ok && r.status !== 309 && r.status !== 500) return retry(new Error(`unexpected HEAD status: ${r.status}`))
853
+
851
854
  if (r.ok) return console.log(`[find_fork_point] "${url.split('/').pop()}" has our latest fork point, hooray!`)
852
855
  }
853
856
 
@@ -866,15 +869,12 @@ async function sync_url(url) {
866
869
  console.log(`min=${min}, max=${max}, i=${i}, version=${version}`)
867
870
 
868
871
  var st = Date.now()
869
- var r = await my_fetch({
870
- method: "HEAD",
871
- version,
872
- retry: { retryRes: r =>
873
- r.status !== 309 && r.status !== 500 }
874
- })
872
+ var r = await my_fetch({ method: "HEAD", version })
875
873
  if (freed || closed) return
876
874
  console.log(`fetched in ${Date.now() - st}`)
877
875
 
876
+ if (!r.ok && r.status !== 309 && r.status !== 500) return retry(new Error(`unexpected HEAD status: ${r.status}`))
877
+
878
878
  if (r.ok) {
879
879
  min = i
880
880
  self.fork_point = version
@@ -1387,3 +1387,14 @@ async function file_exists(fullpath) {
1387
1387
  function sha256(x) {
1388
1388
  return require('crypto').createHash('sha256').update(x).digest('base64')
1389
1389
  }
1390
+
1391
+ function is_well_formed_absolute_url(urlString) {
1392
+ if (!urlString || typeof urlString !== 'string') return
1393
+ if (urlString.match(/\s/)) return
1394
+ try {
1395
+ var url = new URL(urlString)
1396
+ if (url.hostname.includes('%')) return
1397
+ if (!url.hostname) return
1398
+ return urlString.match(/^https?:\/\//)
1399
+ } catch (error) {}
1400
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.112",
3
+ "version": "0.0.114",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",