core-3nweb-client-lib 0.25.6 → 0.26.0

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 (108) hide show
  1. package/README.md +2 -2
  2. package/build/api-defs/files.d.ts +23 -20
  3. package/build/core/asmail/config/common.js +2 -2
  4. package/build/core/asmail/delivery/index.js +4 -3
  5. package/build/core/asmail/delivery/msg.js +5 -4
  6. package/build/core/asmail/delivery/per-recipient-wip.js +2 -2
  7. package/build/core/asmail/inbox/attachments/fs.js +6 -0
  8. package/build/core/asmail/inbox/cached-msgs.js +2 -2
  9. package/build/core/asmail/inbox/inbox-events.js +2 -1
  10. package/build/core/asmail/inbox/index.js +2 -2
  11. package/build/core/asmail/inbox/msg-downloader.js +2 -2
  12. package/build/core/asmail/inbox/msg-indexing.js +3 -3
  13. package/build/core/asmail/inbox/msg-on-disk.js +2 -2
  14. package/build/core/asmail/keyring/keyring-storage.js +2 -2
  15. package/build/core/asmail/sending-params/own-params.js +2 -2
  16. package/build/core/asmail/sending-params/params-from-others.js +2 -2
  17. package/build/core/id-manager.js +2 -2
  18. package/build/core/sign-in.d.ts +5 -4
  19. package/build/core/sign-in.js +9 -11
  20. package/build/core/sign-up.d.ts +1 -0
  21. package/build/core/sign-up.js +7 -3
  22. package/build/core/storage/common/json-saving.d.ts +21 -0
  23. package/build/core/storage/common/json-saving.js +82 -0
  24. package/build/core/storage/common/obj-info-file.d.ts +43 -0
  25. package/build/core/storage/common/obj-info-file.js +119 -3
  26. package/build/core/storage/index.js +1 -1
  27. package/build/core/storage/local/obj-files-gc.js +8 -6
  28. package/build/core/storage/local/obj-files.d.ts +3 -3
  29. package/build/core/storage/local/obj-files.js +9 -9
  30. package/build/core/storage/local/obj-status.d.ts +9 -25
  31. package/build/core/storage/local/obj-status.js +28 -110
  32. package/build/core/storage/local/storage.d.ts +8 -1
  33. package/build/core/storage/local/storage.js +10 -2
  34. package/build/core/storage/synced/downloader.js +6 -5
  35. package/build/core/storage/synced/obj-files-gc.d.ts +1 -0
  36. package/build/core/storage/synced/obj-files-gc.js +44 -5
  37. package/build/core/storage/synced/obj-files.d.ts +13 -20
  38. package/build/core/storage/synced/obj-files.js +70 -48
  39. package/build/core/storage/synced/obj-status.d.ts +74 -15
  40. package/build/core/storage/synced/obj-status.js +291 -107
  41. package/build/core/storage/synced/remote-events.js +32 -26
  42. package/build/core/storage/synced/storage.d.ts +11 -1
  43. package/build/core/storage/synced/storage.js +28 -3
  44. package/build/core/storage/synced/upsyncer.d.ts +8 -7
  45. package/build/core/storage/synced/upsyncer.js +211 -163
  46. package/build/ipc-via-protobuf/asmail-cap.js +15 -32
  47. package/build/ipc-via-protobuf/connector-clients-side.js +2 -2
  48. package/build/ipc-via-protobuf/file.js +22 -14
  49. package/build/ipc-via-protobuf/fs.js +31 -33
  50. package/build/ipc-via-protobuf/startup-cap.js +5 -5
  51. package/build/lib-client/3nstorage/exceptions.d.ts +9 -8
  52. package/build/lib-client/3nstorage/exceptions.js +18 -9
  53. package/build/lib-client/3nstorage/service.js +10 -6
  54. package/build/lib-client/3nstorage/xsp-fs/common.d.ts +18 -4
  55. package/build/lib-client/3nstorage/xsp-fs/common.js +6 -1
  56. package/build/lib-client/3nstorage/xsp-fs/file-node.js +3 -3
  57. package/build/lib-client/3nstorage/xsp-fs/file.js +4 -1
  58. package/build/lib-client/3nstorage/xsp-fs/folder-node.js +27 -13
  59. package/build/lib-client/3nstorage/xsp-fs/fs.d.ts +11 -6
  60. package/build/lib-client/3nstorage/xsp-fs/fs.js +189 -58
  61. package/build/lib-client/3nstorage/xsp-fs/node-in-fs.d.ts +4 -0
  62. package/build/lib-client/3nstorage/xsp-fs/node-in-fs.js +51 -24
  63. package/build/lib-client/3nstorage/xsp-fs/node-persistence.js +2 -2
  64. package/build/lib-client/3nstorage/xsp-fs/xsp-payload-v1.js +2 -2
  65. package/build/lib-client/3nstorage/xsp-fs/xsp-payload-v2.js +3 -3
  66. package/build/lib-client/cryptor/cryptor-in-worker.js +4 -4
  67. package/build/lib-client/cryptor/cryptor-wasm.js +1 -1
  68. package/build/lib-client/cryptor/cryptor.wasm +0 -0
  69. package/build/lib-client/cryptor/in-proc-wasm.js +2 -2
  70. package/build/lib-client/files.js +2 -0
  71. package/build/lib-client/fs-collection.js +3 -2
  72. package/build/lib-client/logging/log-to-file.js +4 -4
  73. package/build/lib-client/objs-on-disk/file-writing-proc.js +2 -2
  74. package/build/lib-client/objs-on-disk/obj-folders.js +2 -2
  75. package/build/lib-client/request-utils.js +2 -2
  76. package/build/lib-client/server-events.js +4 -3
  77. package/build/lib-client/ws-utils.js +2 -2
  78. package/build/lib-common/async-fs-node.js +4 -3
  79. package/build/lib-common/byte-streaming/wrapping.js +17 -17
  80. package/build/lib-common/exceptions/file.js +6 -1
  81. package/build/lib-common/ipc/generic-ipc.js +2 -2
  82. package/build/lib-common/json-utils.js +2 -1
  83. package/build/lib-common/objs-on-disk/obj-file.js +4 -3
  84. package/build/lib-common/objs-on-disk/utils.js +2 -2
  85. package/build/lib-common/processes/deferred.d.ts +6 -0
  86. package/build/lib-common/processes/deferred.js +30 -0
  87. package/build/lib-common/processes/labelled-exec-pools.d.ts +33 -0
  88. package/build/lib-common/processes/labelled-exec-pools.js +141 -0
  89. package/build/lib-common/processes/pressure.d.ts +7 -0
  90. package/build/lib-common/processes/pressure.js +56 -0
  91. package/build/lib-common/processes/sleep.d.ts +1 -0
  92. package/build/lib-common/processes/sleep.js +26 -0
  93. package/build/lib-common/{processes.d.ts → processes/synced.d.ts} +0 -40
  94. package/build/lib-common/{processes.js → processes/synced.js} +187 -204
  95. package/build/lib-common/processes/timeout.d.ts +1 -0
  96. package/build/lib-common/processes/timeout.js +51 -0
  97. package/build/lib-common/service-api/3nstorage/owner.d.ts +5 -4
  98. package/build/lib-common/service-api/3nstorage/owner.js +3 -2
  99. package/build/lib-common/utils-for-observables.d.ts +15 -1
  100. package/build/lib-common/utils-for-observables.js +68 -17
  101. package/build/protos/asmail.proto.js +404 -78
  102. package/build/protos/file.proto.js +370 -44
  103. package/build/protos/fs.proto.js +404 -78
  104. package/package.json +4 -4
  105. package/protos/file.proto +10 -2
  106. package/protos/fs.proto +2 -2
  107. package/build/core/storage/synced/upsync-status.d.ts +0 -41
  108. package/build/core/storage/synced/upsync-status.js +0 -158
@@ -1 +1,44 @@
1
1
  export declare function readJSONInfoFileIn<T>(objFolder: string, fileName: string): Promise<T | undefined>;
2
+ export interface VersionsInfo {
3
+ /**
4
+ * This field indicates current object version in cache.
5
+ */
6
+ current?: number;
7
+ /**
8
+ * This is a list of archived versions in the cache.
9
+ */
10
+ archived?: number[];
11
+ /**
12
+ * This is a map from base version to diff-ed version(s), that use(s) base.
13
+ */
14
+ baseToDiff: {
15
+ [baseVersion: number]: number[];
16
+ };
17
+ /**
18
+ * This is a map from diff version to base version.
19
+ */
20
+ diffToBase: {
21
+ [diffVersion: number]: number;
22
+ };
23
+ }
24
+ /**
25
+ * This function adds base->diff link to given versions info.
26
+ * @param versions
27
+ * @param diffVer
28
+ * @param baseVer
29
+ */
30
+ export declare function addBaseToDiffLinkInStatus(versions: VersionsInfo, diffVer: number, baseVer: number): void;
31
+ /**
32
+ * This function removes given version from versions info, if it is neither
33
+ * archived, nor is a base for another version. If given version is itself
34
+ * based on another, this function is recursively applied to base version, as
35
+ * well.
36
+ * @param versions
37
+ * @param ver
38
+ */
39
+ export declare function rmNonArchVersionsIn(versions: VersionsInfo, ver: number): void;
40
+ export declare function rmArchVersionsIn(versions: VersionsInfo, ver: number): void;
41
+ export declare function setCurrentVersionIn(versions: VersionsInfo, version: number, baseVer: number | undefined): void;
42
+ export declare function rmCurrentVersionIn(versions: VersionsInfo): number | undefined;
43
+ export declare function nonGarbageVersionsIn(versions: VersionsInfo): Set<number>;
44
+ export declare function addWithBasesTo(nonGarbage: Set<number>, ver: number | undefined, versions: VersionsInfo): void;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- Copyright (C) 2020 3NSoft Inc.
3
+ Copyright (C) 2020, 2022 3NSoft Inc.
4
4
 
5
5
  This program is free software: you can redistribute it and/or modify it under
6
6
  the terms of the GNU General Public License as published by the Free Software
@@ -13,9 +13,10 @@
13
13
  See the GNU General Public License for more details.
14
14
 
15
15
  You should have received a copy of the GNU General Public License along with
16
- this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
17
18
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.readJSONInfoFileIn = void 0;
19
+ exports.addWithBasesTo = exports.nonGarbageVersionsIn = exports.rmCurrentVersionIn = exports.setCurrentVersionIn = exports.rmArchVersionsIn = exports.rmNonArchVersionsIn = exports.addBaseToDiffLinkInStatus = exports.readJSONInfoFileIn = void 0;
19
20
  const fs = require("../../../lib-common/async-fs-node");
20
21
  const path_1 = require("path");
21
22
  const exceptions_1 = require("../../../lib-client/3nstorage/exceptions");
@@ -41,4 +42,119 @@ async function readJSONInfoFileIn(objFolder, fileName) {
41
42
  }
42
43
  }
43
44
  exports.readJSONInfoFileIn = readJSONInfoFileIn;
45
+ /**
46
+ * This function adds base->diff link to given versions info.
47
+ * @param versions
48
+ * @param diffVer
49
+ * @param baseVer
50
+ */
51
+ function addBaseToDiffLinkInStatus(versions, diffVer, baseVer) {
52
+ if (diffVer <= baseVer) {
53
+ throw new Error(`Given diff version ${diffVer} is not greater than base version ${baseVer}`);
54
+ }
55
+ versions.diffToBase[diffVer] = baseVer;
56
+ const diffs = versions.baseToDiff[baseVer];
57
+ if (diffs) {
58
+ if (diffs.indexOf(diffVer) < 0) {
59
+ diffs.push(diffVer);
60
+ }
61
+ }
62
+ else {
63
+ versions.baseToDiff[baseVer] = [diffVer];
64
+ }
65
+ }
66
+ exports.addBaseToDiffLinkInStatus = addBaseToDiffLinkInStatus;
67
+ /**
68
+ * This function removes given version from versions info, if it is neither
69
+ * archived, nor is a base for another version. If given version is itself
70
+ * based on another, this function is recursively applied to base version, as
71
+ * well.
72
+ * @param versions
73
+ * @param ver
74
+ */
75
+ function rmNonArchVersionsIn(versions, ver) {
76
+ if (!versions.archived
77
+ || !versions.archived.includes(ver)) {
78
+ return;
79
+ }
80
+ if (versions.baseToDiff[ver]) {
81
+ return;
82
+ }
83
+ const base = versions.diffToBase[ver];
84
+ if (typeof base !== 'number') {
85
+ return;
86
+ }
87
+ delete versions.diffToBase[ver];
88
+ const diffs = versions.baseToDiff[base];
89
+ if (!diffs) {
90
+ return;
91
+ }
92
+ const diffInd = diffs.indexOf(ver);
93
+ if (diffInd < 0) {
94
+ return;
95
+ }
96
+ diffs.splice(diffInd, 1);
97
+ if (diffs.length === 0) {
98
+ delete versions.baseToDiff[base];
99
+ rmNonArchVersionsIn(versions, base);
100
+ }
101
+ }
102
+ exports.rmNonArchVersionsIn = rmNonArchVersionsIn;
103
+ function rmArchVersionsIn(versions, ver) {
104
+ if (!versions.archived) {
105
+ return;
106
+ }
107
+ const vInd = versions.archived.indexOf(ver);
108
+ if (vInd < 0) {
109
+ return;
110
+ }
111
+ versions.archived.splice(vInd, 1);
112
+ rmNonArchVersionsIn(versions, ver);
113
+ }
114
+ exports.rmArchVersionsIn = rmArchVersionsIn;
115
+ function setCurrentVersionIn(versions, version, baseVer) {
116
+ if (baseVer !== undefined) {
117
+ // base->diff links should be added before removals
118
+ addBaseToDiffLinkInStatus(versions, version, baseVer);
119
+ }
120
+ const initCurrent = versions.current;
121
+ if (typeof initCurrent === 'number') {
122
+ rmNonArchVersionsIn(versions, initCurrent);
123
+ }
124
+ versions.current = version;
125
+ }
126
+ exports.setCurrentVersionIn = setCurrentVersionIn;
127
+ function rmCurrentVersionIn(versions) {
128
+ const current = versions.current;
129
+ if (typeof current === 'number') {
130
+ rmNonArchVersionsIn(versions, current);
131
+ delete versions.current;
132
+ }
133
+ return current;
134
+ }
135
+ exports.rmCurrentVersionIn = rmCurrentVersionIn;
136
+ function nonGarbageVersionsIn(versions) {
137
+ const nonGarbage = new Set();
138
+ addWithBasesTo(nonGarbage, versions.current, versions);
139
+ if (versions.archived) {
140
+ for (const archVer of versions.archived) {
141
+ addWithBasesTo(nonGarbage, archVer, versions);
142
+ }
143
+ }
144
+ return nonGarbage;
145
+ }
146
+ exports.nonGarbageVersionsIn = nonGarbageVersionsIn;
147
+ function addWithBasesTo(nonGarbage, ver, versions) {
148
+ while (typeof ver === 'number') {
149
+ if (nonGarbage.has(ver)) {
150
+ break;
151
+ }
152
+ nonGarbage.add(ver);
153
+ if (!versions.diffToBase) {
154
+ break;
155
+ }
156
+ ver = versions.diffToBase[ver];
157
+ }
158
+ }
159
+ exports.addWithBasesTo = addWithBasesTo;
44
160
  Object.freeze(exports);
@@ -280,7 +280,7 @@ class Storages {
280
280
  if (!this.synced) {
281
281
  return false;
282
282
  }
283
- await startObjProcs();
283
+ startObjProcs();
284
284
  return true;
285
285
  };
286
286
  }
@@ -13,11 +13,12 @@
13
13
  See the GNU General Public License for more details.
14
14
 
15
15
  You should have received a copy of the GNU General Public License along with
16
- this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
17
18
  Object.defineProperty(exports, "__esModule", { value: true });
18
19
  exports.GC = void 0;
19
20
  const fs = require("../../../lib-common/async-fs-node");
20
- const processes_1 = require("../../../lib-common/processes");
21
+ const synced_1 = require("../../../lib-common/processes/synced");
21
22
  const path_1 = require("path");
22
23
  class GC {
23
24
  constructor(rmObjFromCache, rmObjFolder) {
@@ -26,7 +27,7 @@ class GC {
26
27
  /**
27
28
  * All gc steps are done in this process.
28
29
  */
29
- this.gcProc = new processes_1.SingleProc();
30
+ this.gcProc = new synced_1.SingleProc();
30
31
  /**
31
32
  * wip are objects that are currently processed. When wip set is empty,
32
33
  * it gets swapped with non-empty scheduled set.
@@ -75,10 +76,10 @@ class GC {
75
76
  || (gcMaxVer && (ver >= gcMaxVer))) {
76
77
  continue;
77
78
  }
78
- rmProcs.push(fs.unlink(path_1.join(obj.objFolder, f)).catch(() => { }));
79
+ rmProcs.push(fs.unlink(path_1.join(obj.objFolder, f)).catch(noop));
79
80
  }
80
- while (rmProcs.length > 0) {
81
- await rmProcs.pop();
81
+ if (rmProcs.length > 0) {
82
+ await Promise.all(rmProcs);
82
83
  }
83
84
  return this.objCollecting();
84
85
  };
@@ -97,4 +98,5 @@ function getAndRemoveOneFrom(set) {
97
98
  set.delete(value);
98
99
  return value;
99
100
  }
101
+ function noop() { }
100
102
  Object.freeze(exports);
@@ -23,8 +23,8 @@ export declare class LocalObj {
23
23
  private readonly scheduleGC;
24
24
  private readonly verObjs;
25
25
  private constructor();
26
- static forExistingObj(objId: ObjId, objFolder: string, scheduleGC: GC['scheduleCollection']): Promise<LocalObj>;
27
- static forNewObj(objId: ObjId, objFolder: string, scheduleGC: GC['scheduleCollection']): Promise<LocalObj>;
26
+ static forExistingObj(objId: ObjId, objFolder: string, scheduleGC: GC['scheduleCollection'], logError: LogError | undefined): Promise<LocalObj>;
27
+ static forNewObj(objId: ObjId, objFolder: string, scheduleGC: GC['scheduleCollection'], logError: LogError | undefined): Promise<LocalObj>;
28
28
  private path;
29
29
  getObjSrc(version: number): Promise<ObjSource>;
30
30
  private objSegsGetterFromDisk;
@@ -32,7 +32,7 @@ export declare class LocalObj {
32
32
  isArchived(): boolean;
33
33
  getCurrentVersionOrThrow(): number;
34
34
  getNonGarbageVersions(): {
35
- gcMaxVer: number | undefined;
35
+ gcMaxVer?: number;
36
36
  nonGarbage: Set<number>;
37
37
  };
38
38
  removeCurrentVersion(): Promise<void>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- Copyright (C) 2016 - 2020 3NSoft Inc.
3
+ Copyright (C) 2016 - 2020, 2022 3NSoft Inc.
4
4
 
5
5
  This program is free software: you can redistribute it and/or modify it under
6
6
  the terms of the GNU General Public License as published by the Free Software
@@ -19,7 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.LocalObj = exports.ObjFiles = void 0;
20
20
  const obj_folders_1 = require("../../../lib-client/objs-on-disk/obj-folders");
21
21
  const obj_on_disk_1 = require("../../../lib-client/objs-on-disk/obj-on-disk");
22
- const processes_1 = require("../../../lib-common/processes");
22
+ const synced_1 = require("../../../lib-common/processes/synced");
23
23
  const path_1 = require("path");
24
24
  const obj_files_gc_1 = require("./obj-files-gc");
25
25
  const obj_status_1 = require("./obj-status");
@@ -29,7 +29,7 @@ class ObjFiles {
29
29
  this.folders = folders;
30
30
  this.logError = logError;
31
31
  this.objs = timed_cache_1.makeTimedCache(60 * 1000);
32
- this.folderAccessSyncProc = new processes_1.NamedProcs();
32
+ this.folderAccessSyncProc = new synced_1.NamedProcs();
33
33
  this.gc = new obj_files_gc_1.GC(obj => {
34
34
  if (this.objs.get(obj.objId) === obj) {
35
35
  this.objs.delete(obj.objId);
@@ -55,7 +55,7 @@ class ObjFiles {
55
55
  if (!folder) {
56
56
  return;
57
57
  }
58
- obj = await LocalObj.forExistingObj(objId, folder, this.gc.scheduleCollection);
58
+ obj = await LocalObj.forExistingObj(objId, folder, this.gc.scheduleCollection, this.logError);
59
59
  this.objs.set(objId, obj);
60
60
  return obj;
61
61
  });
@@ -63,7 +63,7 @@ class ObjFiles {
63
63
  async makeNewObj(objId) {
64
64
  return this.sync(objId, async () => {
65
65
  const folder = await this.folders.getFolderAccessFor(objId, true);
66
- const obj = await LocalObj.forNewObj(objId, folder, this.gc.scheduleCollection);
66
+ const obj = await LocalObj.forNewObj(objId, folder, this.gc.scheduleCollection, this.logError);
67
67
  this.objs.set(objId, obj);
68
68
  return obj;
69
69
  });
@@ -111,12 +111,12 @@ class LocalObj {
111
111
  };
112
112
  Object.freeze(this);
113
113
  }
114
- static async forExistingObj(objId, objFolder, scheduleGC) {
115
- const status = await obj_status_1.ObjStatus.readFrom(objFolder, objId);
114
+ static async forExistingObj(objId, objFolder, scheduleGC, logError) {
115
+ const status = await obj_status_1.ObjStatus.readFrom(objFolder, objId, logError);
116
116
  return new LocalObj(objId, objFolder, status, scheduleGC);
117
117
  }
118
- static async forNewObj(objId, objFolder, scheduleGC) {
119
- const status = await obj_status_1.ObjStatus.makeNew(objFolder, objId);
118
+ static async forNewObj(objId, objFolder, scheduleGC, logError) {
119
+ const status = await obj_status_1.ObjStatus.makeNew(objFolder, objId, logError);
120
120
  return new LocalObj(objId, objFolder, status, scheduleGC);
121
121
  }
122
122
  path(version) {
@@ -1,40 +1,24 @@
1
1
  import { ObjId } from '../../../lib-client/3nstorage/xsp-fs/common';
2
+ import { VersionsInfo } from '../common/obj-info-file';
3
+ import { LogError } from '../../../lib-client/logging/log-to-file';
2
4
  export interface ObjStatusInfo {
3
5
  objId: ObjId;
4
6
  isArchived?: boolean;
5
- /**
6
- * This field indicates current object version in cache.
7
- */
8
- currentVersion?: number;
9
- /**
10
- * This is a list of archived versions in the cache.
11
- */
12
- archivedVersions: number[];
13
- /**
14
- * This is a map from base version to diff-ed version(s), that use(s) base.
15
- */
16
- baseToDiff: {
17
- [baseVersion: number]: number[];
18
- };
19
- /**
20
- * This is a map from diff version to base version.
21
- */
22
- diffToBase: {
23
- [diffVersion: number]: number;
24
- };
7
+ versions: VersionsInfo;
25
8
  }
26
9
  export declare class ObjStatus {
27
10
  private readonly objFolder;
28
11
  private readonly status;
12
+ private readonly logError;
29
13
  private readonly saveProc;
30
- constructor(objFolder: string, status: ObjStatusInfo);
31
- static readFrom(objFolder: string, objId: ObjId): Promise<ObjStatus>;
32
- static makeNew(objFolder: string, objId: ObjId): Promise<ObjStatus>;
33
- private saveFile;
14
+ constructor(objFolder: string, status: ObjStatusInfo, logError: LogError | undefined);
15
+ static readFrom(objFolder: string, objId: ObjId, logError: LogError | undefined): Promise<ObjStatus>;
16
+ static makeNew(objFolder: string, objId: ObjId, logError: LogError | undefined): Promise<ObjStatus>;
17
+ private triggerSaveProc;
34
18
  isArchived(): boolean;
35
19
  getCurrentVersionOrThrow(): number;
36
20
  getNonGarbageVersions(): {
37
- gcMaxVer: number | undefined;
21
+ gcMaxVer?: number;
38
22
  nonGarbage: Set<number>;
39
23
  };
40
24
  setNewCurrentVersion(newVersion: number, baseVer: number | undefined): Promise<void>;
@@ -17,143 +17,75 @@
17
17
  */
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.ObjStatus = void 0;
20
- const fs = require("../../../lib-common/async-fs-node");
21
- const processes_1 = require("../../../lib-common/processes");
22
20
  const path_1 = require("path");
23
21
  const exceptions_1 = require("../../../lib-client/3nstorage/exceptions");
22
+ const json_saving_1 = require("../common/json-saving");
24
23
  const obj_info_file_1 = require("../common/obj-info-file");
25
24
  const STATUS_FILE_NAME = 'status';
26
25
  class ObjStatus {
27
- constructor(objFolder, status) {
26
+ constructor(objFolder, status, logError) {
28
27
  this.objFolder = objFolder;
29
28
  this.status = status;
30
- this.saveProc = new processes_1.DeduppedRunner(() => this.saveFile());
29
+ this.logError = logError;
30
+ this.saveProc = new json_saving_1.JSONSavingProc(path_1.join(this.objFolder, STATUS_FILE_NAME), () => this.status);
31
31
  Object.freeze(this);
32
32
  }
33
- static async readFrom(objFolder, objId) {
33
+ static async readFrom(objFolder, objId, logError) {
34
34
  const status = await readAndCheckStatus(objFolder, objId);
35
- return new ObjStatus(objFolder, status);
35
+ return new ObjStatus(objFolder, status, logError);
36
36
  }
37
- static async makeNew(objFolder, objId) {
37
+ static async makeNew(objFolder, objId, logError) {
38
38
  const status = {
39
39
  objId,
40
- archivedVersions: [],
41
- baseToDiff: {},
42
- diffToBase: {}
40
+ versions: {
41
+ baseToDiff: {},
42
+ diffToBase: {}
43
+ }
43
44
  };
44
- const s = new ObjStatus(objFolder, status);
45
- await s.saveProc.trigger();
45
+ const s = new ObjStatus(objFolder, status, logError);
46
+ await s.triggerSaveProc();
46
47
  return s;
47
48
  }
48
- async saveFile() {
49
- await fs.writeFile(path_1.join(this.objFolder, STATUS_FILE_NAME), JSON.stringify(this.status), { encoding: 'utf8' });
49
+ triggerSaveProc(captureErrors = false) {
50
+ let p = this.saveProc.trigger();
51
+ return (captureErrors ? p.catch(this.logError) : p);
50
52
  }
51
53
  isArchived() {
52
54
  return !!this.status.isArchived;
53
55
  }
54
56
  getCurrentVersionOrThrow() {
55
- if (typeof this.status.currentVersion !== 'number') {
57
+ if (typeof this.status.versions.current !== 'number') {
56
58
  throw new Error(`Object ${this.status.objId} has no current version.`);
57
59
  }
58
- return this.status.currentVersion;
60
+ return this.status.versions.current;
59
61
  }
60
62
  getNonGarbageVersions() {
61
63
  return {
62
- nonGarbage: nonGarbageVersions(this.status),
63
- gcMaxVer: this.status.currentVersion
64
+ nonGarbage: obj_info_file_1.nonGarbageVersionsIn(this.status.versions),
65
+ gcMaxVer: this.status.versions.current
64
66
  };
65
67
  }
66
68
  async setNewCurrentVersion(newVersion, baseVer) {
67
- if (baseVer !== undefined) {
68
- // base->diff links should be added before removals
69
- addBaseToDiffLinkInStatus(this.status, newVersion, baseVer);
70
- }
71
- if (typeof this.status.currentVersion === 'number') {
72
- rmNonArchVersionsIn(this.status, this.status.currentVersion);
73
- }
74
- this.status.currentVersion = newVersion;
75
- await this.saveProc.trigger();
69
+ obj_info_file_1.setCurrentVersionIn(this.status.versions, newVersion, baseVer);
70
+ await this.triggerSaveProc();
76
71
  }
77
72
  async removeCurrentVersion(verObjs) {
78
73
  this.status.isArchived = true;
79
- if (typeof this.status.currentVersion === 'number') {
80
- verObjs.delete(this.status.currentVersion);
81
- rmNonArchVersionsIn(this.status, this.status.currentVersion);
82
- delete this.status.currentVersion;
74
+ const current = obj_info_file_1.rmCurrentVersionIn(this.status.versions);
75
+ if (typeof current === 'number') {
76
+ verObjs.delete(current);
83
77
  }
84
- await this.saveProc.trigger();
78
+ await this.triggerSaveProc();
85
79
  }
86
80
  async removeArchivedVersion(version, verObjs) {
87
81
  verObjs.delete(version);
88
- const arch = this.status.archivedVersions;
89
- const vInd = arch.indexOf(version);
90
- if (vInd < 0) {
91
- return;
92
- }
93
- arch.splice(vInd, 1);
94
- rmNonArchVersionsIn(this.status, version);
95
- await this.saveProc.trigger();
82
+ obj_info_file_1.rmArchVersionsIn(this.status.versions, version);
83
+ await this.triggerSaveProc();
96
84
  }
97
85
  }
98
86
  exports.ObjStatus = ObjStatus;
99
87
  Object.freeze(ObjStatus.prototype);
100
88
  Object.freeze(ObjStatus);
101
- /**
102
- * This function adds base->diff link to status. Status object is changed in
103
- * this call.
104
- * @param status into which a link between versions should be added
105
- * @param diffVer
106
- * @param baseVer
107
- */
108
- function addBaseToDiffLinkInStatus(status, diffVer, baseVer) {
109
- if (diffVer <= baseVer) {
110
- throw new Error(`Given diff version ${diffVer} is not greater than base version ${baseVer}`);
111
- }
112
- status.diffToBase[diffVer] = baseVer;
113
- const diffs = status.baseToDiff[baseVer];
114
- if (diffs) {
115
- if (diffs.indexOf(diffVer) < 0) {
116
- diffs.push(diffVer);
117
- }
118
- }
119
- else {
120
- status.baseToDiff[baseVer] = [diffVer];
121
- }
122
- }
123
- /**
124
- * This function removes given version from status object, if it is neither
125
- * archived, nor is a base for another version. If given version is itself
126
- * based on another, this function is recursively applied to base version, as
127
- * well.
128
- * @param status in which version(s) should be removed
129
- * @param ver
130
- */
131
- function rmNonArchVersionsIn(status, ver) {
132
- if (status.archivedVersions.indexOf(ver) >= 0) {
133
- return;
134
- }
135
- if (status.baseToDiff[ver]) {
136
- return;
137
- }
138
- const base = status.diffToBase[ver];
139
- if (typeof base !== 'number') {
140
- return;
141
- }
142
- delete status.diffToBase[ver];
143
- const diffs = status.baseToDiff[base];
144
- if (!diffs) {
145
- return;
146
- }
147
- const diffInd = diffs.indexOf(ver);
148
- if (diffInd < 0) {
149
- return;
150
- }
151
- diffs.splice(diffInd, 1);
152
- if (diffs.length === 0) {
153
- delete status.baseToDiff[base];
154
- rmNonArchVersionsIn(status, base);
155
- }
156
- }
157
89
  async function readAndCheckStatus(objFolder, objId) {
158
90
  const status = await obj_info_file_1.readJSONInfoFileIn(objFolder, STATUS_FILE_NAME);
159
91
  if (!status) {
@@ -167,18 +99,4 @@ async function readAndCheckStatus(objFolder, objId) {
167
99
  }
168
100
  return status;
169
101
  }
170
- function nonGarbageVersions(status) {
171
- const nonGarbage = new Set();
172
- addWithBasesTo(nonGarbage, status.currentVersion, status);
173
- for (const archVer of status.archivedVersions) {
174
- addWithBasesTo(nonGarbage, archVer, status);
175
- }
176
- return nonGarbage;
177
- }
178
- function addWithBasesTo(nonGarbage, ver, status) {
179
- while (typeof ver === 'number') {
180
- nonGarbage.add(ver);
181
- ver = status.diffToBase[ver];
182
- }
183
- }
184
102
  Object.freeze(exports);
@@ -1,6 +1,9 @@
1
- import { Storage, NodesContainer, StorageGetter, ObjId } from '../../../lib-client/3nstorage/xsp-fs/common';
1
+ import { Storage, NodesContainer, StorageGetter, ObjId, NodeEvent } from '../../../lib-client/3nstorage/xsp-fs/common';
2
2
  import { LogError } from '../../../lib-client/logging/log-to-file';
3
3
  import { AsyncSBoxCryptor, Subscribe, ObjSource } from 'xsp-files';
4
+ import { Observable } from 'rxjs';
5
+ declare type FolderEvent = web3n.files.FolderEvent;
6
+ declare type FileEvent = web3n.files.FileEvent;
4
7
  export declare class LocalStorage implements Storage {
5
8
  private readonly files;
6
9
  private readonly getStorages;
@@ -9,8 +12,11 @@ export declare class LocalStorage implements Storage {
9
12
  readonly type: web3n.files.FSType;
10
13
  readonly versioned = true;
11
14
  readonly nodes: NodesContainer;
15
+ private readonly events;
12
16
  private constructor();
13
17
  static makeAndStart(path: string, getStorages: StorageGetter, cryptor: AsyncSBoxCryptor, logError: LogError): Promise<Storage>;
18
+ getNodeEvents(): Observable<NodeEvent>;
19
+ broadcastNodeEvent(objId: ObjId, parentObjId: ObjId | undefined, event: FolderEvent | FileEvent): void;
14
20
  storageForLinking(type: web3n.files.FSType, location?: string): Storage;
15
21
  generateNewObjId(): Promise<string>;
16
22
  private getObjNonArchOrThrow;
@@ -19,3 +25,4 @@ export declare class LocalStorage implements Storage {
19
25
  removeObj(objId: string): Promise<void>;
20
26
  close(): Promise<void>;
21
27
  }
28
+ export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- Copyright (C) 2016 - 2020 3NSoft Inc.
3
+ Copyright (C) 2016 - 2020, 2022 3NSoft Inc.
4
4
 
5
5
  This program is free software: you can redistribute it and/or modify it under
6
6
  the terms of the GNU General Public License as published by the Free Software
@@ -22,6 +22,7 @@ const random_node_1 = require("../../../lib-common/random-node");
22
22
  const ecma_nacl_1 = require("ecma-nacl");
23
23
  const buffer_utils_1 = require("../../../lib-common/buffer-utils");
24
24
  const obj_files_1 = require("./obj-files");
25
+ const utils_for_observables_1 = require("../../../lib-common/utils-for-observables");
25
26
  class LocalStorage {
26
27
  constructor(files, getStorages, cryptor, logError) {
27
28
  this.files = files;
@@ -31,6 +32,7 @@ class LocalStorage {
31
32
  this.type = 'local';
32
33
  this.versioned = true;
33
34
  this.nodes = new common_1.NodesContainer();
35
+ this.events = new utils_for_observables_1.Broadcast();
34
36
  Object.seal(this);
35
37
  }
36
38
  static async makeAndStart(path, getStorages, cryptor, logError) {
@@ -38,6 +40,12 @@ class LocalStorage {
38
40
  const s = new LocalStorage(files, getStorages, cryptor, logError);
39
41
  return common_1.wrapStorageImplementation(s);
40
42
  }
43
+ getNodeEvents() {
44
+ return this.events.event$;
45
+ }
46
+ broadcastNodeEvent(objId, parentObjId, event) {
47
+ this.events.next({ objId, parentObjId, event });
48
+ }
41
49
  storageForLinking(type, location) {
42
50
  if ((type === 'local') || (type === 'synced')) {
43
51
  return this.getStorages(type);
@@ -92,7 +100,7 @@ class LocalStorage {
92
100
  }
93
101
  async close() {
94
102
  try {
95
- // XXX add cleanups
103
+ this.events.done();
96
104
  }
97
105
  catch (err) {
98
106
  await this.logError(err);
@@ -13,10 +13,11 @@
13
13
  See the GNU General Public License for more details.
14
14
 
15
15
  You should have received a copy of the GNU General Public License along with
16
- this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
17
18
  Object.defineProperty(exports, "__esModule", { value: true });
18
19
  exports.Downloader = void 0;
19
- const processes_1 = require("../../../lib-common/processes");
20
+ const synced_1 = require("../../../lib-common/processes/synced");
20
21
  const buffer_utils_1 = require("../../../lib-common/buffer-utils");
21
22
  const MAX_GETTING_CHUNK = 512 * 1024;
22
23
  const DOWNLOAD_START_CHUNK = 128 * 1024;
@@ -28,7 +29,7 @@ class Downloader {
28
29
  * When it comes to the download start, if chain exists, it means that
29
30
  * process has already started.
30
31
  */
31
- this.downloadProcs = new processes_1.NamedProcs();
32
+ this.downloadProcs = new synced_1.NamedProcs();
32
33
  Object.seal(this);
33
34
  }
34
35
  async sync(objId, version, action) {
@@ -67,8 +68,8 @@ class Downloader {
67
68
  }
68
69
  });
69
70
  }
70
- getCurrentObjVersion(objId) {
71
- return this.sync(objId, -1, async () => {
71
+ async getCurrentObjVersion(objId) {
72
+ return await this.sync(objId, -1, async () => {
72
73
  const { header, segsTotalLen, version, segsChunk } = await this.remoteStorage.getCurrentObj(objId, DOWNLOAD_START_CHUNK);
73
74
  const layout = {
74
75
  sections: [{ src: 'new', ofs: 0, len: segsTotalLen }]
@@ -22,4 +22,5 @@ export declare class GC {
22
22
  scheduleCollection: (obj: SyncedObj) => void;
23
23
  stop(): Promise<void>;
24
24
  private objCollecting;
25
+ private collectIn;
25
26
  }