braidfs 0.0.93 → 0.0.95

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 +99 -66
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -489,55 +489,6 @@ async function sync_url(url) {
489
489
  file_loop_pump()
490
490
  }
491
491
 
492
- async function my_fetch(params) {
493
- if (freed) return
494
- try {
495
- var a = new AbortController()
496
- aborts.add(a)
497
- return await braid_fetch(url, {
498
- signal: a.signal,
499
- headers: {
500
- "Merge-Type": "dt",
501
- "Content-Type": 'text/plain',
502
- ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname])
503
- },
504
- retry: { retryRes: r => r.status !== 401 && r.status !== 403 },
505
- ...params
506
- })
507
- } catch (e) {
508
- if (freed) return
509
- if (e?.name !== "AbortError") console.log(e)
510
- } finally {
511
- if (freed) return
512
- aborts.delete(a)
513
- }
514
- }
515
-
516
- async function send_out(stuff) {
517
- if (!is_external_link) return
518
- if (freed) return
519
-
520
- console.log(`send_out ${url} ${JSON.stringify(stuff, null, 4).slice(0, 1000)}`)
521
-
522
- var r = await my_fetch({ method: "PUT", ...stuff })
523
- if (freed) return
524
-
525
- // the server has acknowledged this version,
526
- // so add it to the fork point
527
- if (r.ok) self.update_fork_point(stuff.version[0], stuff.parents)
528
-
529
- // if we're not authorized,
530
- if (r.status == 401 || r.status == 403) {
531
- // and it's one of our versions (a local edit),
532
- if (self.peer === braid_text.decode_version(stuff.version[0])[0]) {
533
- // then revert it
534
- console.log(`access denied: reverting local edits`)
535
- unsync_url(url)
536
- sync_url(url)
537
- }
538
- }
539
- }
540
-
541
492
  await within_fiber(fullpath, async () => {
542
493
  if (freed) return
543
494
  var fullpath = await get_fullpath()
@@ -717,7 +668,7 @@ async function sync_url(url) {
717
668
  await write_meta_file()
718
669
  if (freed) return
719
670
 
720
- if (await wait_on(is_read_only(fullpath) !== self.file_read_only)) {
671
+ if (await wait_on(is_read_only(fullpath)) !== self.file_read_only) {
721
672
  if (freed) return
722
673
  self.file_ignore_until = Date.now() + 1000
723
674
  await wait_on(set_read_only(fullpath, self.file_read_only))
@@ -776,6 +727,55 @@ async function sync_url(url) {
776
727
  return frontier.sort()
777
728
  }
778
729
 
730
+ async function my_fetch(params) {
731
+ if (freed) return
732
+ try {
733
+ var a = new AbortController()
734
+ aborts.add(a)
735
+ return await braid_fetch(url, {
736
+ signal: a.signal,
737
+ headers: {
738
+ "Merge-Type": "dt",
739
+ "Content-Type": 'text/plain',
740
+ ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname])
741
+ },
742
+ retry: { retryRes: r => r.status !== 401 && r.status !== 403 },
743
+ ...params
744
+ })
745
+ } catch (e) {
746
+ if (freed) return
747
+ if (e?.name !== "AbortError") console.log(e)
748
+ } finally {
749
+ if (freed) return
750
+ aborts.delete(a)
751
+ }
752
+ }
753
+
754
+ async function send_out(stuff) {
755
+ if (!is_external_link) return
756
+ if (freed) return
757
+
758
+ console.log(`send_out ${url} ${JSON.stringify(stuff, null, 4).slice(0, 1000)}`)
759
+
760
+ var r = await my_fetch({ method: "PUT", ...stuff })
761
+ if (freed) return
762
+
763
+ // the server has acknowledged this version,
764
+ // so add it to the fork point
765
+ if (r.ok) self.update_fork_point(stuff.version[0], stuff.parents)
766
+
767
+ // if we're not authorized,
768
+ if (r.status == 401 || r.status == 403) {
769
+ // and it's one of our versions (a local edit),
770
+ if (self.peer === braid_text.decode_version(stuff.version[0])[0]) {
771
+ // then revert it
772
+ console.log(`access denied: reverting local edits`)
773
+ unsync_url(url)
774
+ sync_url(url)
775
+ }
776
+ }
777
+ }
778
+
779
779
  async function find_fork_point() {
780
780
  if (freed) return
781
781
  console.log(`[find_fork_point] url: ${url}`)
@@ -889,7 +889,6 @@ async function sync_url(url) {
889
889
  if (update.version.length !== 1) throw 'unexpected'
890
890
 
891
891
  await wait_on(braid_text.put(url, { ...update, peer: self.peer, merge_type: 'dt' }))
892
- console.log(`PUT DONE!`)
893
892
  if (freed) return
894
893
 
895
894
  // the server is giving us this version,
@@ -904,8 +903,52 @@ async function sync_url(url) {
904
903
 
905
904
  // send it stuff we have but it doesn't't
906
905
  async function send_new_stuff(fork_point) {
907
- if (freed) return
908
- var in_parallel = create_parallel_promises(10)
906
+ if (freed) return
907
+ var q = []
908
+ var in_flight = new Map()
909
+ var max_in_flight = 10
910
+ var send_pump_lock = 0
911
+
912
+ async function send_pump() {
913
+ send_pump_lock++
914
+ if (send_pump_lock > 1) return
915
+ try {
916
+ if (freed) return
917
+ if (in_flight.size >= max_in_flight) return
918
+ if (!q.length) {
919
+ var frontier = self.fork_point
920
+ for (var u of in_flight.values())
921
+ frontier = extend_frontier(frontier, u.version[0], u.parents)
922
+
923
+ var options = {
924
+ parents: frontier,
925
+ merge_type: 'dt',
926
+ peer: self.peer,
927
+ subscribe: u => u.version.length && q.push(u)
928
+ }
929
+ await braid_text.get(url, options)
930
+ await braid_text.forget(url, options)
931
+ }
932
+ while (q.length && in_flight.size < max_in_flight) {
933
+ let u = q.shift()
934
+ in_flight.set(u.version[0], u);
935
+ (async () => {
936
+ await initial_connect_promise
937
+ if (freed) return
938
+ await send_out({...u, peer: self.peer})
939
+ if (freed) return
940
+ in_flight.delete(u.version[0])
941
+ setTimeout(send_pump, 0)
942
+ })()
943
+ }
944
+ } finally {
945
+ var retry = send_pump_lock > 1
946
+ send_pump_lock = 0
947
+ if (retry) setTimeout(send_pump, 0)
948
+ }
949
+ }
950
+
951
+ var initial_stuff = true
909
952
  await wait_on(braid_text.get(url, braid_text_get_options = {
910
953
  parents: fork_point,
911
954
  merge_type: 'dt',
@@ -914,11 +957,12 @@ async function sync_url(url) {
914
957
  if (freed) return
915
958
  if (u.version.length) {
916
959
  self.signal_file_needs_writing()
917
- await initial_connect_promise
918
- in_parallel(() => send_out({...u, peer: self.peer}))
960
+ if (initial_stuff || in_flight.size < max_in_flight) q.push(u)
961
+ send_pump()
919
962
  }
920
963
  },
921
964
  }))
965
+ initial_stuff = false
922
966
  }
923
967
 
924
968
  // for config and errors file, listen for web changes
@@ -1098,14 +1142,3 @@ async function file_exists(fullpath) {
1098
1142
  function sha256(x) {
1099
1143
  return require('crypto').createHash('sha256').update(x).digest('base64')
1100
1144
  }
1101
-
1102
- function create_parallel_promises(N) {
1103
- var q = []
1104
- var n = 0
1105
- return async f => {
1106
- q.push(f)
1107
- n++
1108
- while (q.length && n <= N) await q.shift()()
1109
- n--
1110
- }
1111
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.93",
3
+ "version": "0.0.95",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",