core-3nweb-client-lib 0.53.5 → 0.53.7

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.
@@ -352,9 +352,9 @@ class SyncedObj {
352
352
  this.scheduleSelfGC();
353
353
  }
354
354
  dropCachedLocalObjVersionsLessOrEqual(version) {
355
- for (const version of this.localVers.keys()) {
356
- if (version <= version) {
357
- this.localVers.delete(version);
355
+ for (const localVersion of this.localVers.keys()) {
356
+ if (localVersion <= version) {
357
+ this.localVers.delete(localVersion);
358
358
  }
359
359
  }
360
360
  }
@@ -170,7 +170,7 @@ class FolderNode extends node_in_fs_1.NodeInFS {
170
170
  this.setUpdatedParams(src.version, attrs, xattrs);
171
171
  }
172
172
  async adoptRemote(opts) {
173
- const objsToRm = await this.doChange(true, async () => {
173
+ await this.doChange(true, async () => {
174
174
  try {
175
175
  const adopted = await this.syncedStorage().adoptRemote(this.objId, opts);
176
176
  if (!adopted) {
@@ -193,19 +193,11 @@ class FolderNode extends node_in_fs_1.NodeInFS {
193
193
  const event = this.makeEntryRenamingEvent(this.version, 'sync', newName, oldName);
194
194
  this.broadcastEvent(event, false, objId);
195
195
  }
196
- return removedNodes;
197
196
  }
198
197
  catch (exc) {
199
198
  throw (0, common_1.setPathInExc)(exc, this.name);
200
199
  }
201
200
  });
202
- if (!objsToRm) {
203
- return;
204
- }
205
- for (const objToRm of objsToRm) {
206
- const nodeToRm = await this.getOrMakeChildNodeForInfo(objToRm);
207
- this.callRemoveObjOn('sync', nodeToRm);
208
- }
209
201
  }
210
202
  makeEntryAdditionEvent(newVersion, src, node, moveLabel) {
211
203
  const event = {
@@ -323,41 +315,40 @@ class FolderNode extends node_in_fs_1.NodeInFS {
323
315
  }
324
316
  const deferred = (0, deferred_1.defer)();
325
317
  const nodeSet = this.storage.nodes.setPromise(info.objId, deferred.promise);
326
- try {
327
- let node;
318
+ (async () => {
328
319
  if (info.isFile) {
329
- node = await file_node_1.FileNode.makeForExisting(this.storage, this.objId, info.name, info.objId, info.key);
320
+ return await file_node_1.FileNode.makeForExisting(this.storage, this.objId, info.name, info.objId, info.key);
330
321
  }
331
322
  else if (info.isFolder) {
332
323
  const src = await this.storage.getObjSrc(info.objId);
333
- node = await FolderNode.readNodeFromObjBytes(this.storage, info.name, info.objId, src, info.key);
324
+ return await FolderNode.readNodeFromObjBytes(this.storage, info.name, info.objId, src, info.key);
334
325
  }
335
326
  else if (info.isLink) {
336
- node = await link_node_1.LinkNode.makeForExisting(this.storage, this.objId, info.name, info.objId, info.key);
327
+ return await link_node_1.LinkNode.makeForExisting(this.storage, this.objId, info.name, info.objId, info.key);
337
328
  }
338
329
  else {
339
330
  throw new Error(`Unknown type of fs node`);
340
331
  }
341
- deferred.resolve(node);
342
- return nodeSet;
343
- }
344
- catch (exc) {
332
+ })().then((node) => deferred.resolve(node), async (exc) => {
345
333
  if (exc.failedCipherVerification) {
346
334
  exc = (0, file_1.makeFileException)('ioError', `${this.name}/${info.name}`, exc);
347
335
  }
348
336
  else if (exc.objNotFound) {
349
337
  if (fromCurrentNodes) {
350
- await this.fixMissingChildAndThrow(exc, info);
338
+ try {
339
+ await this.fixMissingChildAndThrow(exc, info);
340
+ }
341
+ catch (exc) {
342
+ deferred.reject(exc);
343
+ return;
344
+ }
351
345
  }
352
346
  else {
353
347
  try {
354
348
  const node = await this.getNonLocal(info);
355
349
  if (node) {
356
350
  deferred.resolve(node);
357
- return nodeSet;
358
- }
359
- else {
360
- throw exc;
351
+ return;
361
352
  }
362
353
  }
363
354
  catch (exc2) {
@@ -366,8 +357,8 @@ class FolderNode extends node_in_fs_1.NodeInFS {
366
357
  }
367
358
  }
368
359
  deferred.reject((0, error_1.errWithCause)(exc, `Failed to instantiate fs node '${this.name}/${info.name}'`));
369
- return nodeSet;
370
- }
360
+ });
361
+ return nodeSet;
371
362
  }
372
363
  async getNonLocal(info) {
373
364
  var _a;
@@ -644,17 +644,15 @@ class ObjIdToPathMap {
644
644
  Object.seal(this);
645
645
  }
646
646
  async fillFromTree(root) {
647
- const listOfAll = await recursiveIdAndPathList(root, '.', this.maxDepth);
648
- for (const [objId, path, depth] of listOfAll) {
649
- this.map.set(objId, { path, depth });
650
- }
647
+ const rootItem = { nameInParent: '.' };
648
+ this.map.set(root.objId, rootItem);
649
+ await recursivelyAddIntoMap(this.map, root, rootItem, this.maxDepth, 1);
651
650
  }
652
- getPathCorrectingTreeMap({ event, objId, parentObjId, childObjId }) {
653
- let pathAndDepth = this.map.get(objId);
654
- if (pathAndDepth) {
655
- const { path, depth } = pathAndDepth;
656
- this.updateFoundObjMapOnEvent(objId, path, depth, childObjId, event);
657
- return path;
651
+ getPathAndIfNeededCorrectTreeMap({ event, objId, parentObjId, childObjId }) {
652
+ let pathMapItem = this.map.get(objId);
653
+ if (pathMapItem) {
654
+ this.updateFoundObjMapOnEvent(objId, pathMapItem, childObjId, event);
655
+ return pathOfMapItem(pathMapItem);
658
656
  }
659
657
  if (event.type === 'removed') {
660
658
  return;
@@ -666,66 +664,110 @@ class ObjIdToPathMap {
666
664
  if (!parent) {
667
665
  return;
668
666
  }
669
- if ((this.maxDepth !== undefined)
670
- && (this.maxDepth <= parent.depth)) {
667
+ if ((this.maxDepth !== undefined) && (this.maxDepth <= depthOf(parent))) {
671
668
  return;
672
669
  }
673
- const path = `${parent.path}/${event.path}`;
674
- this.map.set(objId, { path, depth: parent.depth + 1 });
675
- return path;
670
+ let item = this.map.get(objId);
671
+ if (item) {
672
+ item.parent = parent;
673
+ }
674
+ else {
675
+ item = { nameInParent: event.path, parent };
676
+ this.map.set(objId, item);
677
+ }
678
+ return pathOfMapItem(item);
676
679
  }
677
- updateFoundObjMapOnEvent(objId, path, depth, childObjId, event) {
680
+ updateFoundObjMapOnEvent(objId, objPathItem, childObjId, event) {
678
681
  if (event.type === 'removed') {
679
- this.map.delete(objId);
682
+ recursivelyRemoveFromMap(this.map, objId, objPathItem);
680
683
  return;
681
684
  }
682
- if ((this.maxDepth !== undefined) && (this.maxDepth <= depth)) {
685
+ if ((this.maxDepth !== undefined) && (this.maxDepth <= depthOf(objPathItem))) {
683
686
  return;
684
687
  }
685
688
  if (event.type === 'entry-renaming') {
686
689
  const childEntry = this.map.get(childObjId);
687
690
  if (childEntry) {
688
- childEntry.path === `${path}/${event.newName}`;
691
+ childEntry.nameInParent === event.newName;
689
692
  }
690
693
  }
691
694
  else if (event.type === 'entry-removal') {
692
695
  const childEntry = this.map.get(childObjId);
693
- if (childEntry && (childEntry.path === `${path}/${event.name}`)) {
694
- this.map.delete(childObjId);
696
+ // Context: FolderNode.moveChildTo() does move with addition first, therefore,
697
+ // if removal is part of move, parent will be different
698
+ if (((childEntry === null || childEntry === void 0 ? void 0 : childEntry.parent) === objPathItem) && (childEntry.nameInParent === event.name)) {
699
+ recursivelyRemoveFromMap(this.map, childObjId, childEntry);
695
700
  }
696
701
  }
697
702
  else if (event.type === 'entry-addition') {
698
- this.map.set(childObjId, { path: `${path}/${event.entry.name}`, depth: depth + 1 });
703
+ // Context: FolderNode.moveChildTo() does move with addition first, therefore,
704
+ // child may exist, and only its parent need change
705
+ const childEntry = this.map.get(childObjId);
706
+ if (childEntry) {
707
+ childEntry.parent = objPathItem;
708
+ childEntry.nameInParent = event.entry.name;
709
+ }
710
+ else {
711
+ const childEntry = { parent: objPathItem, nameInParent: event.entry.name };
712
+ this.map.set(objId, childEntry);
713
+ // XXX if childEntry is a folder with its own children, subtree should be added
714
+ // if (event.entry.isFolder && (!this.maxDepth || (depthOf(childEntry) < this.maxDepth))) {
715
+ // await recursivelyAddIntoMap(this.map, childNode, childEntry, this.maxDepth, childEntry.depth+1);
716
+ // }
717
+ }
699
718
  }
700
719
  }
701
720
  copyWithLongPath(nodeEvent) {
702
- const path = this.getPathCorrectingTreeMap(nodeEvent);
721
+ const path = this.getPathAndIfNeededCorrectTreeMap(nodeEvent);
703
722
  return (path ? shallowCopyWithPath(nodeEvent.event, path) : undefined);
704
723
  }
705
724
  }
706
725
  Object.freeze(ObjIdToPathMap.prototype);
707
726
  Object.freeze(ObjIdToPathMap);
708
- async function recursiveIdAndPathList(folder, path, maxDepth, depth = 0) {
709
- const { lst } = folder.list();
710
- const idAndPaths = [
711
- [folder.objId, path, depth]
712
- ];
713
- if ((maxDepth !== undefined) && (depth === maxDepth)) {
714
- return idAndPaths;
715
- }
727
+ async function recursivelyAddIntoMap(map, parentNode, parent, maxDepth, depth) {
728
+ const { lst } = parentNode.list();
716
729
  for (const item of lst) {
717
- const childPath = `${path}/${item.name}`;
730
+ const childItem = { parent, nameInParent: item.name };
718
731
  if (item.isFile || item.isLink) {
719
- const { objId } = folder.getCurrentNodeInfo(item.name);
720
- idAndPaths.push([objId, childPath, depth + 1]);
732
+ const { objId } = parentNode.getCurrentNodeInfo(item.name);
733
+ map.set(objId, childItem);
721
734
  }
722
735
  else if (item.isFolder) {
723
- const child = await folder.getFolder(item.name);
724
- const childLst = await recursiveIdAndPathList(child, childPath, maxDepth, depth + 1);
725
- idAndPaths.push(...childLst);
736
+ const child = await parentNode.getFolder(item.name);
737
+ map.set(child.objId, childItem);
738
+ if ((maxDepth === undefined) || (depth < maxDepth)) {
739
+ await recursivelyAddIntoMap(map, child, childItem, maxDepth, depth + 1);
740
+ }
726
741
  }
727
742
  }
728
- return idAndPaths;
743
+ }
744
+ function recursivelyRemoveFromMap(map, objId, objItem) {
745
+ map.delete(objId);
746
+ const childrenToRm = [];
747
+ for (const [childId, childItem] of map.entries()) {
748
+ if (childItem.parent === objItem) {
749
+ childrenToRm.push([childId, childItem]);
750
+ }
751
+ }
752
+ for (const [childId, childItem] of childrenToRm) {
753
+ recursivelyRemoveFromMap(map, childId, childItem);
754
+ }
755
+ }
756
+ function pathOfMapItem(item) {
757
+ if (item.parent) {
758
+ const prefix = pathOfMapItem(item.parent);
759
+ return `${prefix}/${item.nameInParent}`;
760
+ }
761
+ else {
762
+ return item.nameInParent;
763
+ }
764
+ }
765
+ function depthOf(item) {
766
+ let depth = 0;
767
+ for (let p = item.parent; p; p = p.parent) {
768
+ depth += 1;
769
+ }
770
+ return depth;
729
771
  }
730
772
  function shallowCopyWithPath(event, path) {
731
773
  const shallowCopy = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-3nweb-client-lib",
3
- "version": "0.53.5",
3
+ "version": "0.53.7",
4
4
  "description": "3NWeb client core library, embeddable into different environments",
5
5
  "main": "build/lib-index.js",
6
6
  "types": "build/lib-index.d.ts",