braidfs 0.0.79 → 0.0.81

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 +76 -60
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -149,10 +149,9 @@ async function main() {
149
149
  var patches = diff(parent_text, text)
150
150
 
151
151
  if (patches.length) {
152
- var peer = Math.random().toString(36).slice(2)
153
- var char_count = patches_to_code_points(patches, parent_text)
154
- var version = [peer + "-" + char_count]
155
- await braid_text.put(proxy.url, { version, parents, patches, peer, merge_type: 'dt' })
152
+ proxy.local_edit_counter += patches_to_code_points(patches, parent_text)
153
+ var version = [proxy.peer + "-" + (proxy.local_edit_counter - 1)]
154
+ await braid_text.put(proxy.url, { version, parents, patches, merge_type: 'dt' })
156
155
 
157
156
  // may be able to do this more efficiently.. we want to make sure we're capturing a file write that is after our version was written.. there may be a way we can avoid calling file_needs_writing here
158
157
  var stat = await new Promise(done => {
@@ -181,7 +180,6 @@ async function main() {
181
180
  if (!config.allow_remote_access) console.log('!! only accessible from localhost !!')
182
181
 
183
182
  proxy_url('.braidfs/config').then(() => {
184
- let peer = Math.random().toString(36).slice(2)
185
183
  braid_text.get('.braidfs/config', {
186
184
  subscribe: async update => {
187
185
  let prev = config
@@ -218,8 +216,7 @@ async function main() {
218
216
  if (x !== '') console.log(`warning: config file is currently invalid.`)
219
217
  return
220
218
  }
221
- },
222
- peer
219
+ }
223
220
  })
224
221
  })
225
222
  proxy_url('.braidfs/errors')
@@ -427,9 +424,9 @@ async function proxy_url(url) {
427
424
  return p
428
425
  }
429
426
 
430
- var peer = Math.random().toString(36).slice(2),
431
- char_counter = -1,
432
- file_last_version = null,
427
+ self.peer = Math.random().toString(36).slice(2)
428
+ self.local_edit_counter = 0
429
+ var file_last_version = null,
433
430
  file_last_digest = null
434
431
  self.file_last_text = null
435
432
  self.file_last_stat = null
@@ -489,7 +486,7 @@ async function proxy_url(url) {
489
486
  signal: a.signal,
490
487
  headers: {
491
488
  "Merge-Type": "dt",
492
- "Content-Type": '*/*',
489
+ "Content-Type": 'text/plain',
493
490
  ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname])
494
491
  },
495
492
  method: "PUT",
@@ -504,6 +501,53 @@ async function proxy_url(url) {
504
501
  finish_something()
505
502
  }
506
503
 
504
+ if (!start_something()) return
505
+ await within_file_lock(fullpath, async () => {
506
+ var fullpath = await get_fullpath()
507
+ if (await require('fs').promises.access(meta_path).then(
508
+ () => 1, () => 0)) {
509
+ // meta file exists
510
+ let meta = JSON.parse(await require('fs').promises.readFile(meta_path, { encoding: 'utf8' }))
511
+ // destructure stuff from the meta file
512
+ !({
513
+ version: file_last_version,
514
+ digest: file_last_digest,
515
+ peer: self.peer,
516
+ local_edit_counter: self.local_edit_counter
517
+ } = Array.isArray(meta) ? { version: meta } : meta)
518
+
519
+ if (!self.peer) self.peer = Math.random().toString(36).slice(2)
520
+ if (!self.local_edit_counter) self.local_edit_counter = 0
521
+
522
+ try {
523
+ self.file_last_text = (await braid_text.get(url, { version: file_last_version })).body
524
+ } catch (e) {
525
+ // the version from the meta file doesn't exist..
526
+ if (fullpath === braidfs_config_file) {
527
+ // in the case of the config file,
528
+ // we want to load the current file contents,
529
+ // which we can acheive by setting file_last_version
530
+ // to the latest
531
+ console.log(`WARNING: there was an issue with the config file, and it is reverting to the contents at: ${braidfs_config_file}`)
532
+ var x = await braid_text.get(url, {})
533
+ file_last_version = x.version
534
+ self.file_last_text = x.body
535
+ file_last_digest = sha256(self.file_last_text)
536
+ } else throw new Error(`sync error: version not found: ${file_last_version}`)
537
+ }
538
+
539
+ file_needs_writing = !v_eq(file_last_version, (await braid_text.get(url, {})).version)
540
+
541
+ // sanity check
542
+ if (file_last_digest && sha256(self.file_last_text) != file_last_digest) throw new Error('file_last_text does not match file_last_digest')
543
+ } else if (await require('fs').promises.access(fullpath).then(() => 1, () => 0)) {
544
+ // file exists, but not meta file
545
+ file_last_version = []
546
+ self.file_last_text = ''
547
+ }
548
+ })
549
+ finish_something()
550
+
507
551
  file_loop_pump()
508
552
  async function file_loop_pump() {
509
553
  if (file_loop_pump_lock) return
@@ -514,41 +558,6 @@ async function proxy_url(url) {
514
558
  await within_file_lock(fullpath, async () => {
515
559
  var fullpath = await get_fullpath()
516
560
 
517
- if (file_last_version === null) {
518
- if (await require('fs').promises.access(meta_path).then(
519
- () => 1, () => 0)) {
520
- // meta file exists
521
- let meta = JSON.parse(await require('fs').promises.readFile(meta_path, { encoding: 'utf8' }))
522
- void ({ version: file_last_version, digest: file_last_digest } = Array.isArray(meta) ? { version: meta } : meta)
523
-
524
- try {
525
- self.file_last_text = (await braid_text.get(url, { version: file_last_version })).body
526
- } catch (e) {
527
- // the version from the meta file doesn't exist..
528
- if (fullpath === braidfs_config_file) {
529
- // in the case of the config file,
530
- // we want to load the current file contents,
531
- // which we can acheive by setting file_last_version
532
- // to the latest
533
- console.log(`WARNING: there was an issue with the config file, and it is reverting to the contents at: ${braidfs_config_file}`)
534
- var x = await braid_text.get(url, {})
535
- file_last_version = x.version
536
- self.file_last_text = x.body
537
- file_last_digest = sha256(self.file_last_text)
538
- } else throw new Error(`sync error: version not found: ${file_last_version}`)
539
- }
540
-
541
- file_needs_writing = !v_eq(file_last_version, (await braid_text.get(url, {})).version)
542
-
543
- // sanity check
544
- if (file_last_digest && sha256(self.file_last_text) != file_last_digest) throw new Error('file_last_text does not match file_last_digest')
545
- } else if (await require('fs').promises.access(fullpath).then(() => 1, () => 0)) {
546
- // file exists, but not meta file
547
- file_last_version = []
548
- self.file_last_text = ''
549
- }
550
- }
551
-
552
561
  while (file_needs_reading || file_needs_writing) {
553
562
  if (file_needs_reading) {
554
563
  console.log(`reading file: ${fullpath}`)
@@ -578,21 +587,26 @@ async function proxy_url(url) {
578
587
  console.log(`found changes in: ${fullpath}`)
579
588
 
580
589
  // convert from js-indicies to code-points
581
- char_counter += patches_to_code_points(patches, self.file_last_text)
590
+ self.local_edit_counter += patches_to_code_points(patches, self.file_last_text)
582
591
 
583
592
  self.file_last_text = text
584
593
 
585
- var version = [peer + "-" + char_counter]
594
+ var version = [self.peer + "-" + (self.local_edit_counter - 1)]
586
595
  var parents = file_last_version
587
596
  file_last_version = version
588
597
 
589
598
  add_to_version_cache(text, version)
590
599
 
591
- send_out({ version, parents, patches, peer })
600
+ send_out({ version, parents, patches, peer: self.peer })
592
601
 
593
- await braid_text.put(url, { version, parents, patches, peer, merge_type: 'dt' })
602
+ await braid_text.put(url, { version, parents, patches, peer: self.peer, merge_type: 'dt' })
594
603
 
595
- await require('fs').promises.writeFile(meta_path, JSON.stringify({ version: file_last_version, digest: sha256(self.file_last_text) }))
604
+ await require('fs').promises.writeFile(meta_path, JSON.stringify({
605
+ version: file_last_version,
606
+ digest: sha256(self.file_last_text),
607
+ peer: self.peer,
608
+ local_edit_counter: self.local_edit_counter
609
+ }))
596
610
  } else {
597
611
  add_to_version_cache(text, file_last_version)
598
612
 
@@ -634,7 +648,9 @@ async function proxy_url(url) {
634
648
 
635
649
  await require('fs').promises.writeFile(meta_path, JSON.stringify({
636
650
  version: file_last_version,
637
- digest: sha256(self.file_last_text)
651
+ digest: sha256(self.file_last_text),
652
+ peer: self.peer,
653
+ local_edit_counter: self.local_edit_counter
638
654
  }))
639
655
  }
640
656
 
@@ -673,7 +689,7 @@ async function proxy_url(url) {
673
689
  signal: a.signal,
674
690
  headers: {
675
691
  "Merge-Type": "dt",
676
- Accept: '*/*',
692
+ Accept: 'text/plain',
677
693
  ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname]),
678
694
  },
679
695
  subscribe: true,
@@ -698,7 +714,7 @@ async function proxy_url(url) {
698
714
  let cur = await braid_text.get(url, {})
699
715
  if (cur.version.length) return cur.version
700
716
  },
701
- peer
717
+ peer: self.peer
702
718
  }).then(x => {
703
719
  if (x.status === 209) x.subscribe(async update => {
704
720
  console.log(`got external update about ${url}`)
@@ -712,7 +728,7 @@ async function proxy_url(url) {
712
728
 
713
729
  if (!start_something()) return
714
730
 
715
- await braid_text.put(url, { ...update, peer, merge_type: 'dt' })
731
+ await braid_text.put(url, { ...update, peer: self.peer, merge_type: 'dt' })
716
732
 
717
733
 
718
734
  self.signal_file_needs_writing()
@@ -732,7 +748,7 @@ async function proxy_url(url) {
732
748
  signal: a.signal,
733
749
  method: "HEAD",
734
750
  headers: {
735
- Accept: '*/*',
751
+ Accept: 'text/plain',
736
752
  ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname]),
737
753
  },
738
754
  retry: true
@@ -780,7 +796,7 @@ async function proxy_url(url) {
780
796
  method: "HEAD",
781
797
  parents,
782
798
  headers: {
783
- Accept: '*/*',
799
+ Accept: 'text/plain',
784
800
  ...(x => x && {Cookie: x})(config.cookies?.[new URL(url).hostname]),
785
801
  },
786
802
  retry: true
@@ -804,11 +820,11 @@ async function proxy_url(url) {
804
820
  braid_text.get(url, braid_text_get_options = {
805
821
  parents,
806
822
  merge_type: 'dt',
807
- peer,
823
+ peer: self.peer,
808
824
  subscribe: async (u) => {
809
825
  if (u.version.length) {
810
826
  self.signal_file_needs_writing()
811
- in_parallel(() => send_out({...u, peer}))
827
+ in_parallel(() => send_out({...u, peer: self.peer}))
812
828
  }
813
829
  },
814
830
  })
@@ -821,7 +837,7 @@ async function proxy_url(url) {
821
837
  // for config and errors file, listen for web changes
822
838
  if (!is_external_link) braid_text.get(url, braid_text_get_options = {
823
839
  merge_type: 'dt',
824
- peer,
840
+ peer: self.peer,
825
841
  subscribe: self.signal_file_needs_writing,
826
842
  })
827
843
 
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.79",
3
+ "version": "0.0.81",
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.14",
10
+ "braid-text": "^0.2.22",
11
11
  "chokidar": "^3.6.0"
12
12
  },
13
13
  "bin": {