braidfs 0.0.86 → 0.0.88

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 +16 -19
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -530,7 +530,7 @@ async function sync_url(url) {
530
530
  }
531
531
 
532
532
  if (!start_something()) return
533
- await within_file_lock(fullpath, async () => {
533
+ await within_fiber(fullpath, async () => {
534
534
  var fullpath = await get_fullpath()
535
535
  if (await require('fs').promises.access(meta_path).then(
536
536
  () => 1, () => 0)) {
@@ -584,7 +584,7 @@ async function sync_url(url) {
584
584
 
585
585
  if (!start_something()) return
586
586
 
587
- await within_file_lock(fullpath, async () => {
587
+ await within_fiber(fullpath, async () => {
588
588
  var fullpath = await get_fullpath()
589
589
 
590
590
  while (file_needs_reading || file_needs_writing) {
@@ -888,7 +888,7 @@ async function ensure_path(path) {
888
888
  var parts = path.split('/').slice(1)
889
889
  for (var i = 1; i <= parts.length; i++) {
890
890
  var partial = '/' + parts.slice(0, i).join('/')
891
- await within_file_lock(partial, async () => {
891
+ await within_fiber(normalize_url(partial), async () => {
892
892
  try {
893
893
  let stat = await require("fs").promises.stat(partial)
894
894
  if (stat.isDirectory()) return // good
@@ -907,7 +907,7 @@ async function ensure_path(path) {
907
907
  ////////////////////////////////
908
908
 
909
909
  function normalize_url(url) {
910
- return url.replace(/(\/index|\/)+$/, '')
910
+ return url.replace(/(\/index)*\/?$/, '')
911
911
  }
912
912
 
913
913
  async function is_dir(p) {
@@ -1025,21 +1025,18 @@ async function set_read_only(fullpath, read_only) {
1025
1025
  }
1026
1026
  }
1027
1027
 
1028
- async function get_file_lock(fullpath) {
1029
- if (!get_file_lock.locks) get_file_lock.locks = {}
1030
- if (!get_file_lock.locks[fullpath]) get_file_lock.locks[fullpath] = Promise.resolve()
1031
- return new Promise(done =>
1032
- get_file_lock.locks[fullpath] = get_file_lock.locks[fullpath].then(() =>
1033
- new Promise(done2 => done(done2))))
1034
- }
1035
-
1036
- async function within_file_lock(fullpath, func) {
1037
- var lock = await get_file_lock(fullpath)
1038
- try {
1039
- return await func()
1040
- } finally {
1041
- lock()
1042
- }
1028
+ function within_fiber(id, func) {
1029
+ if (!within_fiber.chains) within_fiber.chains = {}
1030
+ var prev = within_fiber.chains[id] || Promise.resolve()
1031
+ var curr = prev.then(async () => {
1032
+ try {
1033
+ return await func()
1034
+ } finally {
1035
+ if (within_fiber.chains[id] === curr)
1036
+ delete within_fiber.chains[id]
1037
+ }
1038
+ })
1039
+ return within_fiber.chains[id] = curr
1043
1040
  }
1044
1041
 
1045
1042
  async function file_exists(fullpath) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.86",
3
+ "version": "0.0.88",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",