braidfs 0.0.100 → 0.0.102

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 +69 -13
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -693,26 +693,26 @@ async function sync_url(url) {
693
693
  file_loop_pump_lock--
694
694
  }
695
695
 
696
- self.update_fork_point = (event, parents) => {
697
- self.fork_point = extend_frontier(self.fork_point, event, parents)
696
+ self.update_fork_point = (version, parents) => {
697
+ self.fork_point = extend_frontier(self.fork_point, version, parents)
698
698
  self.signal_file_needs_writing(true)
699
699
  }
700
700
 
701
- function extend_frontier(frontier, event, parents) {
701
+ function extend_frontier(frontier, version, parents) {
702
702
  // special case:
703
703
  // if current frontier has all parents,
704
704
  // then we can just remove those
705
- // and add event
705
+ // and add version
706
706
  var frontier_set = new Set(frontier)
707
707
  if (parents.length &&
708
708
  parents.every(p => frontier_set.has(p))) {
709
709
  parents.forEach(p => frontier_set.delete(p))
710
- frontier_set.add(event)
710
+ for (var event of version) frontier_set.add(event)
711
711
  frontier = [...frontier_set.values()]
712
712
  } else {
713
713
  // full-proof approach..
714
714
  var looking_for = frontier_set
715
- looking_for.add(event)
715
+ for (var event of version) looking_for.add(event)
716
716
 
717
717
  frontier = []
718
718
  var shadow = new Set()
@@ -782,13 +782,14 @@ async function sync_url(url) {
782
782
  var a = new AbortController()
783
783
  aborts.add(a)
784
784
  return await braid_fetch(url, {
785
+ ...params,
785
786
  signal: a.signal,
786
787
  headers: {
788
+ ...params.headers,
787
789
  "Merge-Type": "dt",
788
790
  "Content-Type": 'text/plain',
789
791
  ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname])
790
792
  },
791
- ...params
792
793
  })
793
794
  } catch (e) {
794
795
  if (freed || closed) return
@@ -808,7 +809,7 @@ async function sync_url(url) {
808
809
 
809
810
  // the server has acknowledged this version,
810
811
  // so add it to the fork point
811
- if (r.ok) self.update_fork_point(stuff.version[0], stuff.parents)
812
+ if (r.ok) self.update_fork_point(stuff.version, stuff.parents)
812
813
 
813
814
  // if we're not authorized,
814
815
  if (r.status == 401 || r.status == 403) {
@@ -828,7 +829,12 @@ async function sync_url(url) {
828
829
 
829
830
  // see if remote has the fork point
830
831
  if (self.fork_point) {
831
- var r = await my_fetch({ method: "HEAD", version: self.fork_point })
832
+ var r = await my_fetch({
833
+ method: "HEAD",
834
+ version: self.fork_point,
835
+ retry: { retryRes: r =>
836
+ r.status !== 309 && r.status !== 500 }
837
+ })
832
838
  if (freed || closed) return
833
839
  if (r.ok) return console.log(`[find_fork_point] "${url.split('/').pop()}" has our latest fork point, hooray!`)
834
840
  }
@@ -848,7 +854,12 @@ async function sync_url(url) {
848
854
  console.log(`min=${min}, max=${max}, i=${i}, version=${version}`)
849
855
 
850
856
  var st = Date.now()
851
- var r = await my_fetch({ method: "HEAD", version })
857
+ var r = await my_fetch({
858
+ method: "HEAD",
859
+ version,
860
+ retry: { retryRes: r =>
861
+ r.status !== 309 && r.status !== 500 }
862
+ })
852
863
  if (freed || closed) return
853
864
  console.log(`fetched in ${Date.now() - st}`)
854
865
 
@@ -867,7 +878,52 @@ async function sync_url(url) {
867
878
  await send_new_stuff()
868
879
  if (freed || closed) return
869
880
 
870
- let a = new AbortController()
881
+ // attempt to download the initial stuff in one go,
882
+ // using transfer-encoding dt
883
+ //
884
+ // first check for support..
885
+ //
886
+ var res = await my_fetch({
887
+ method: 'HEAD',
888
+ headers: { 'accept-transfer-encoding': 'dt' },
889
+ })
890
+ if (freed || closed) return
891
+
892
+ if (res.ok && res.headers.get('x-transfer-encoding') === 'dt') {
893
+ var res = await my_fetch({
894
+ headers: { 'accept-transfer-encoding': 'dt' },
895
+ parents: self.fork_point,
896
+ })
897
+ if (freed || closed) return
898
+ console.log(`got external updates about ${url}`)
899
+
900
+ // manually apply the dt bytes..
901
+ // ..code bits taken from braid-text put..
902
+ var bytes = new Uint8Array(await res.arrayBuffer())
903
+ if (freed || closed) return
904
+
905
+ var start_i = 1 + resource.doc.getLocalVersion().reduce((a, b) => Math.max(a, b), -1)
906
+ resource.doc.mergeBytes(bytes)
907
+
908
+ // update resource.actor_seqs
909
+ var end_i = resource.doc.getLocalVersion().reduce((a, b) => Math.max(a, b), -1)
910
+ for (var i = start_i; i <= end_i; i++) {
911
+ var v = resource.doc.localToRemoteVersion([i])[0]
912
+ if (!resource.actor_seqs[v[0]]) resource.actor_seqs[v[0]] = new braid_text.RangeSet()
913
+ resource.actor_seqs[v[0]].add_range(v[1], v[1])
914
+ }
915
+
916
+ resource.val = resource.doc.get()
917
+ resource.need_defrag = true
918
+ await resource.db_delta(bytes)
919
+ if (freed || closed) return
920
+
921
+ // ..do the things we do when getting subscribe updates..
922
+ self.update_fork_point(JSON.parse(`[${res.headers.get('current-version')}]`), self.fork_point)
923
+ self.signal_file_needs_writing()
924
+ }
925
+
926
+ var a = new AbortController()
871
927
  aborts.add(a)
872
928
  var res = await braid_fetch(url, {
873
929
  signal: a.signal,
@@ -914,7 +970,7 @@ async function sync_url(url) {
914
970
  // the server is giving us this version,
915
971
  // so it must have it,
916
972
  // so let's add it to our fork point
917
- self.update_fork_point(update.version[0], update.parents)
973
+ self.update_fork_point(update.version, update.parents)
918
974
 
919
975
  self.signal_file_needs_writing()
920
976
  }, retry)
@@ -936,7 +992,7 @@ async function sync_url(url) {
936
992
  if (!q.length) {
937
993
  var frontier = self.fork_point
938
994
  for (var u of in_flight.values())
939
- frontier = extend_frontier(frontier, u.version[0], u.parents)
995
+ frontier = extend_frontier(frontier, u.version, u.parents)
940
996
 
941
997
  var options = {
942
998
  parents: frontier,
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.100",
3
+ "version": "0.0.102",
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.76",
10
- "braid-text": "^0.2.30",
10
+ "braid-text": "^0.2.40",
11
11
  "chokidar": "^3.6.0"
12
12
  },
13
13
  "bin": {