core-3nweb-client-lib 0.25.6 → 0.27.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 (215) hide show
  1. package/README.md +2 -2
  2. package/build/api-defs/asmail.d.ts +1 -1
  3. package/build/api-defs/files.d.ts +282 -70
  4. package/build/core/app-files.js +7 -7
  5. package/build/core/asmail/config/common.js +4 -4
  6. package/build/core/asmail/config/index.js +2 -2
  7. package/build/core/asmail/config/published-intro-key.js +1 -1
  8. package/build/core/asmail/delivery/common.js +7 -7
  9. package/build/core/asmail/delivery/index.js +7 -6
  10. package/build/core/asmail/delivery/msg.js +8 -7
  11. package/build/core/asmail/delivery/per-recipient-wip.js +3 -3
  12. package/build/core/asmail/inbox/attachments/fs.js +11 -1
  13. package/build/core/asmail/inbox/cached-msgs.js +3 -3
  14. package/build/core/asmail/inbox/inbox-events.js +5 -4
  15. package/build/core/asmail/inbox/index.js +12 -12
  16. package/build/core/asmail/inbox/msg-downloader.js +3 -3
  17. package/build/core/asmail/inbox/msg-indexing.js +4 -4
  18. package/build/core/asmail/inbox/msg-on-disk.js +7 -7
  19. package/build/core/asmail/index.d.ts +3 -3
  20. package/build/core/asmail/index.js +13 -8
  21. package/build/core/asmail/key-verification.js +5 -5
  22. package/build/core/asmail/keyring/common.js +7 -6
  23. package/build/core/asmail/keyring/correspondent-keys.js +8 -7
  24. package/build/core/asmail/keyring/id-to-email-map.js +2 -1
  25. package/build/core/asmail/keyring/index.d.ts +7 -8
  26. package/build/core/asmail/keyring/index.js +15 -14
  27. package/build/core/asmail/keyring/keyring-storage.js +4 -3
  28. package/build/core/asmail/msg/opener.js +3 -3
  29. package/build/core/asmail/msg/packer.js +13 -13
  30. package/build/core/asmail/sending-params/own-params.js +4 -4
  31. package/build/core/asmail/sending-params/params-from-others.js +3 -3
  32. package/build/core/id-manager.js +8 -5
  33. package/build/core/index.d.ts +2 -1
  34. package/build/core/index.js +14 -14
  35. package/build/core/sign-in.d.ts +5 -4
  36. package/build/core/sign-in.js +12 -14
  37. package/build/core/sign-up.d.ts +1 -0
  38. package/build/core/sign-up.js +15 -11
  39. package/build/core/storage/common/json-saving.d.ts +21 -0
  40. package/build/core/storage/common/json-saving.js +82 -0
  41. package/build/core/storage/common/obj-info-file.d.ts +51 -0
  42. package/build/core/storage/common/obj-info-file.js +153 -5
  43. package/build/core/storage/common/utils.d.ts +2 -0
  44. package/build/core/storage/common/utils.js +32 -0
  45. package/build/core/storage/index.d.ts +3 -17
  46. package/build/core/storage/index.js +57 -77
  47. package/build/core/storage/local/obj-files-gc.d.ts +2 -0
  48. package/build/core/storage/local/obj-files-gc.js +53 -39
  49. package/build/core/storage/local/obj-files.d.ts +6 -9
  50. package/build/core/storage/local/obj-files.js +16 -19
  51. package/build/core/storage/local/obj-status.d.ts +20 -30
  52. package/build/core/storage/local/obj-status.js +46 -113
  53. package/build/core/storage/local/storage.d.ts +15 -5
  54. package/build/core/storage/local/storage.js +37 -18
  55. package/build/core/storage/synced/downloader.js +7 -6
  56. package/build/core/storage/synced/obj-files-gc.d.ts +6 -1
  57. package/build/core/storage/synced/obj-files-gc.js +106 -13
  58. package/build/core/storage/synced/obj-files.d.ts +46 -47
  59. package/build/core/storage/synced/obj-files.js +207 -154
  60. package/build/core/storage/synced/obj-status.d.ts +103 -42
  61. package/build/core/storage/synced/obj-status.js +525 -137
  62. package/build/core/storage/synced/remote-events.d.ts +11 -12
  63. package/build/core/storage/synced/remote-events.js +80 -57
  64. package/build/core/storage/synced/storage.d.ts +24 -5
  65. package/build/core/storage/synced/storage.js +123 -38
  66. package/build/core/storage/synced/upload-header-file.d.ts +4 -0
  67. package/build/core/storage/synced/upload-header-file.js +64 -0
  68. package/build/core/storage/synced/upsyncer.d.ts +15 -9
  69. package/build/core/storage/synced/upsyncer.js +219 -246
  70. package/build/core/storage/system-folders/apps-data.d.ts +16 -0
  71. package/build/core/storage/system-folders/apps-data.js +110 -0
  72. package/build/core/storage/system-folders/index.d.ts +18 -0
  73. package/build/core/storage/system-folders/index.js +77 -0
  74. package/build/core-ipc/common-caps.js +3 -3
  75. package/build/core-ipc/generic.js +8 -8
  76. package/build/core-ipc/startup-caps.js +2 -2
  77. package/build/cryptors.js +6 -2
  78. package/build/ipc-via-protobuf/asmail-cap.js +67 -83
  79. package/build/ipc-via-protobuf/bytes.js +16 -17
  80. package/build/ipc-via-protobuf/connector-clients-side.d.ts +3 -0
  81. package/build/ipc-via-protobuf/connector-clients-side.js +62 -25
  82. package/build/ipc-via-protobuf/connector-services-side.js +10 -10
  83. package/build/ipc-via-protobuf/connector.js +4 -4
  84. package/build/ipc-via-protobuf/file.d.ts +48 -12
  85. package/build/ipc-via-protobuf/file.js +476 -120
  86. package/build/ipc-via-protobuf/fs.d.ts +8 -0
  87. package/build/ipc-via-protobuf/fs.js +592 -159
  88. package/build/ipc-via-protobuf/log-cap.js +2 -2
  89. package/build/ipc-via-protobuf/mailerid.js +3 -3
  90. package/build/ipc-via-protobuf/protobuf-msg.d.ts +1 -0
  91. package/build/ipc-via-protobuf/protobuf-msg.js +11 -7
  92. package/build/ipc-via-protobuf/startup-cap.js +23 -23
  93. package/build/ipc-via-protobuf/storage-cap.js +12 -12
  94. package/build/ipc.js +7 -2
  95. package/build/lib-client/3nstorage/exceptions.d.ts +12 -8
  96. package/build/lib-client/3nstorage/exceptions.js +31 -10
  97. package/build/lib-client/3nstorage/service.d.ts +16 -2
  98. package/build/lib-client/3nstorage/service.js +109 -39
  99. package/build/lib-client/3nstorage/util/file-based-json.d.ts +2 -1
  100. package/build/lib-client/3nstorage/util/file-based-json.js +1 -1
  101. package/build/lib-client/3nstorage/xsp-fs/attrs.js +17 -17
  102. package/build/lib-client/3nstorage/xsp-fs/common.d.ts +52 -14
  103. package/build/lib-client/3nstorage/xsp-fs/common.js +31 -16
  104. package/build/lib-client/3nstorage/xsp-fs/file-node.d.ts +1 -0
  105. package/build/lib-client/3nstorage/xsp-fs/file-node.js +18 -14
  106. package/build/lib-client/3nstorage/xsp-fs/file.d.ts +31 -6
  107. package/build/lib-client/3nstorage/xsp-fs/file.js +74 -23
  108. package/build/lib-client/3nstorage/xsp-fs/folder-node-serialization.js +4 -4
  109. package/build/lib-client/3nstorage/xsp-fs/folder-node.d.ts +24 -11
  110. package/build/lib-client/3nstorage/xsp-fs/folder-node.js +599 -189
  111. package/build/lib-client/3nstorage/xsp-fs/fs.d.ts +45 -9
  112. package/build/lib-client/3nstorage/xsp-fs/fs.js +326 -74
  113. package/build/lib-client/3nstorage/xsp-fs/link-node.d.ts +1 -0
  114. package/build/lib-client/3nstorage/xsp-fs/link-node.js +7 -2
  115. package/build/lib-client/3nstorage/xsp-fs/node-in-fs.d.ts +30 -20
  116. package/build/lib-client/3nstorage/xsp-fs/node-in-fs.js +239 -106
  117. package/build/lib-client/3nstorage/xsp-fs/node-persistence.d.ts +1 -1
  118. package/build/lib-client/3nstorage/xsp-fs/node-persistence.js +18 -19
  119. package/build/lib-client/3nstorage/xsp-fs/xsp-payload-v1.js +5 -5
  120. package/build/lib-client/3nstorage/xsp-fs/xsp-payload-v2.js +56 -56
  121. package/build/lib-client/3nweb-signup.js +4 -4
  122. package/build/lib-client/asmail/recipient.js +15 -15
  123. package/build/lib-client/asmail/sender.js +22 -22
  124. package/build/lib-client/asmail/service-config.js +3 -3
  125. package/build/lib-client/cryptor/cryptor-in-worker.js +19 -17
  126. package/build/lib-client/cryptor/cryptor-wasm.js +1 -1
  127. package/build/lib-client/cryptor/cryptor.js +4 -2
  128. package/build/lib-client/cryptor/cryptor.wasm +0 -0
  129. package/build/lib-client/cryptor/in-proc-js.js +1 -1
  130. package/build/lib-client/cryptor/in-proc-wasm.js +7 -7
  131. package/build/lib-client/cryptor/worker-js.js +2 -2
  132. package/build/lib-client/cryptor/worker-wasm.js +2 -2
  133. package/build/lib-client/files-select.js +1 -1
  134. package/build/lib-client/files.d.ts +1 -1
  135. package/build/lib-client/files.js +71 -4
  136. package/build/lib-client/fs-collection.js +3 -2
  137. package/build/lib-client/fs-sync-utils.d.ts +5 -0
  138. package/build/lib-client/fs-sync-utils.js +61 -0
  139. package/build/lib-client/fs-view.d.ts +14 -0
  140. package/build/lib-client/fs-view.js +33 -0
  141. package/build/lib-client/key-derivation.js +1 -1
  142. package/build/lib-client/local-files/dev-file-sink.js +9 -9
  143. package/build/lib-client/local-files/dev-file-src.js +2 -2
  144. package/build/lib-client/local-files/device-fs.d.ts +1 -1
  145. package/build/lib-client/local-files/device-fs.js +56 -54
  146. package/build/lib-client/logging/log-to-file.d.ts +1 -1
  147. package/build/lib-client/logging/log-to-file.js +11 -11
  148. package/build/lib-client/mailer-id/login.js +7 -7
  149. package/build/lib-client/mailer-id/provisioner.js +12 -12
  150. package/build/lib-client/objs-on-disk/file-writing-proc.js +5 -5
  151. package/build/lib-client/objs-on-disk/obj-folders.js +33 -33
  152. package/build/lib-client/objs-on-disk/obj-on-disk.d.ts +13 -2
  153. package/build/lib-client/objs-on-disk/obj-on-disk.js +24 -9
  154. package/build/lib-client/request-utils.d.ts +1 -0
  155. package/build/lib-client/request-utils.js +14 -14
  156. package/build/lib-client/server-events.d.ts +3 -3
  157. package/build/lib-client/server-events.js +12 -10
  158. package/build/lib-client/service-locator.js +10 -10
  159. package/build/lib-client/user-with-mid-session.js +7 -7
  160. package/build/lib-client/user-with-pkl-session.js +25 -25
  161. package/build/lib-client/ws-utils.js +3 -3
  162. package/build/lib-common/async-cryptor-wrap.js +4 -4
  163. package/build/lib-common/async-fs-node.d.ts +5 -3
  164. package/build/lib-common/async-fs-node.js +19 -18
  165. package/build/lib-common/byte-streaming/pipe.js +1 -1
  166. package/build/lib-common/byte-streaming/wrapping.js +17 -17
  167. package/build/lib-common/canonical-address.js +1 -1
  168. package/build/lib-common/exceptions/error.d.ts +1 -0
  169. package/build/lib-common/exceptions/error.js +7 -6
  170. package/build/lib-common/exceptions/file.js +10 -1
  171. package/build/lib-common/ipc/generic-ipc.js +2 -2
  172. package/build/lib-common/ipc/ws-ipc.js +2 -2
  173. package/build/lib-common/json-utils.js +2 -1
  174. package/build/lib-common/mid-sigs-NaCl-Ed.js +14 -14
  175. package/build/lib-common/objs-on-disk/file-layout.d.ts +19 -0
  176. package/build/lib-common/objs-on-disk/file-layout.js +130 -12
  177. package/build/lib-common/objs-on-disk/obj-file.d.ts +13 -2
  178. package/build/lib-common/objs-on-disk/obj-file.js +99 -37
  179. package/build/lib-common/objs-on-disk/utils.d.ts +1 -0
  180. package/build/lib-common/objs-on-disk/utils.js +4 -4
  181. package/build/lib-common/objs-on-disk/v1-obj-file-format.js +14 -14
  182. package/build/lib-common/processes/deferred.d.ts +6 -0
  183. package/build/lib-common/processes/deferred.js +30 -0
  184. package/build/lib-common/processes/labelled-exec-pools.d.ts +33 -0
  185. package/build/lib-common/processes/labelled-exec-pools.js +141 -0
  186. package/build/lib-common/processes/pressure.d.ts +7 -0
  187. package/build/lib-common/processes/pressure.js +56 -0
  188. package/build/lib-common/processes/sleep.d.ts +1 -0
  189. package/build/lib-common/processes/sleep.js +26 -0
  190. package/build/lib-common/{processes.d.ts → processes/synced.d.ts} +0 -40
  191. package/build/lib-common/{processes.js → processes/synced.js} +187 -204
  192. package/build/lib-common/processes/timeout.d.ts +1 -0
  193. package/build/lib-common/processes/timeout.js +51 -0
  194. package/build/lib-common/random-node.js +7 -7
  195. package/build/lib-common/service-api/3nstorage/owner.d.ts +100 -39
  196. package/build/lib-common/service-api/3nstorage/owner.js +85 -42
  197. package/build/lib-common/service-api/asmail/delivery.js +2 -2
  198. package/build/lib-common/service-api/asmail/retrieval.js +1 -1
  199. package/build/lib-common/timed-cache.d.ts +1 -0
  200. package/build/lib-common/timed-non-weak-cache.d.ts +1 -0
  201. package/build/lib-common/timed-non-weak-cache.js +11 -0
  202. package/build/lib-common/utils-for-observables.d.ts +15 -1
  203. package/build/lib-common/utils-for-observables.js +70 -19
  204. package/build/lib-common/weak-cache.d.ts +1 -0
  205. package/build/lib-common/weak-cache.js +12 -1
  206. package/build/lib-index.d.ts +2 -1
  207. package/build/lib-index.js +10 -7
  208. package/build/protos/asmail.proto.js +12912 -7127
  209. package/build/protos/file.proto.js +4848 -2399
  210. package/build/protos/fs.proto.js +9230 -3445
  211. package/package.json +8 -7
  212. package/protos/file.proto +91 -11
  213. package/protos/fs.proto +107 -8
  214. package/build/core/storage/synced/upsync-status.d.ts +0 -41
  215. package/build/core/storage/synced/upsync-status.js +0 -158
@@ -1,9 +1,14 @@
1
- import { Node, NodeType, Storage, RemoteEvent } from './common';
1
+ import { FSChangeSrc, Node, NodeType, Storage, SyncedStorage } from './common';
2
2
  import { Observable } from 'rxjs';
3
3
  import { CommonAttrs, XAttrs } from './attrs';
4
4
  import { NodePersistance } from './node-persistence';
5
+ import { ObjSource } from 'xsp-files';
5
6
  export declare type FSEvent = web3n.files.FolderEvent | web3n.files.FileEvent;
7
+ declare type RemoteEvent = web3n.files.RemoteEvent;
6
8
  declare type XAttrsChanges = web3n.files.XAttrsChanges;
9
+ declare type SyncStatus = web3n.files.SyncStatus;
10
+ declare type OptionsToAdopteRemote = web3n.files.OptionsToAdopteRemote;
11
+ declare type OptionsToUploadLocal = web3n.files.OptionsToUploadLocal;
7
12
  export declare abstract class NodeInFS<P extends NodePersistance> implements Node {
8
13
  protected readonly storage: Storage;
9
14
  readonly type: NodeType;
@@ -17,7 +22,7 @@ export declare abstract class NodeInFS<P extends NodePersistance> implements Nod
17
22
  private writeProc;
18
23
  get version(): number;
19
24
  protected setCurrentVersion(newVersion: number): void;
20
- private remoteEvents;
25
+ readonly isInSyncedStorage: boolean;
21
26
  protected constructor(storage: Storage, type: NodeType, name: string, objId: string, currentVersion: number, parentId: string | undefined);
22
27
  private updatedXAttrs;
23
28
  protected setUpdatedParams(version: number, attrs: CommonAttrs | undefined, xattrs: XAttrs | undefined): void;
@@ -30,16 +35,18 @@ export declare abstract class NodeInFS<P extends NodePersistance> implements Nod
30
35
  getXAttr(xaName: string): any;
31
36
  listXAttrs(): string[];
32
37
  getAttrs(): CommonAttrs;
33
- processRemoteEvent(event: RemoteEvent): Promise<void>;
34
- private bufferRemoteEvent;
35
- private getBufferedEvent;
36
- localDelete(): Promise<void>;
38
+ listVersions(): Promise<{
39
+ current?: number;
40
+ archived?: number[];
41
+ }>;
42
+ archiveCurrent(version?: number): Promise<number>;
43
+ removeObj(src?: FSChangeSrc): Promise<void>;
37
44
  /**
38
45
  * This non-synchronized method deletes object from storage, and detaches
39
46
  * this node from storage. Make sure to call it inside access synchronization
40
47
  * construct.
41
48
  */
42
- protected delete(remoteEvent?: boolean): Promise<void>;
49
+ protected delete(src: FSChangeSrc): Promise<void>;
43
50
  /**
44
51
  * This method runs node changing function in an exclusive manner.
45
52
  * Returned promise resolves to whatever change function returns.
@@ -53,23 +60,26 @@ export declare abstract class NodeInFS<P extends NodePersistance> implements Nod
53
60
  * new current version, when change has been successful.
54
61
  */
55
62
  protected doChange<T>(awaitPrevChange: boolean, change: () => Promise<T>): Promise<T>;
56
- /**
57
- * This method is called on conflict with remote version. This method
58
- * is called at ordered point in time requiring no further synchronization.
59
- * @param remoteVersion is an object version on server
60
- */
61
- protected doOnConflict(remoteVersion: number): Promise<void>;
62
- /**
63
- * This non-synchronized method resolves conflict with remote version.
64
- * @param remoteVersion
65
- */
66
- protected doOnExternalChange(remoteVersion: number): Promise<void>;
67
- protected broadcastEvent(event: FSEvent, complete?: boolean): void;
63
+ protected broadcastEvent(event: FSEvent, complete?: boolean, childObjId?: string): void;
68
64
  /**
69
65
  * This is a lazily initialized field, when there is an external entity
70
66
  * that wants to see this node's events.
71
67
  */
72
68
  private events;
73
- get event$(): Observable<FSEvent>;
69
+ get event$(): Observable<FSEvent | RemoteEvent>;
70
+ protected syncedStorage(): SyncedStorage;
71
+ syncStatus(): Promise<SyncStatus>;
72
+ updateStatusInfo(): Promise<SyncStatus>;
73
+ isSyncedVersionOnDisk(version: number): Promise<'partial' | 'complete' | 'none'>;
74
+ download(version: number): Promise<void>;
75
+ protected abstract setCurrentStateFrom(src: ObjSource): Promise<void>;
76
+ adoptRemote(opts: OptionsToAdopteRemote | undefined): Promise<void>;
77
+ protected needUpload(localVersion: number | undefined): Promise<{
78
+ localVersion: number;
79
+ uploadVersion: number;
80
+ createOnRemote: boolean;
81
+ } | undefined>;
82
+ upload(opts: OptionsToUploadLocal | undefined): Promise<void>;
83
+ private uploadHeaderChange;
74
84
  }
75
85
  export {};
@@ -21,9 +21,11 @@ exports.NodeInFS = void 0;
21
21
  * Everything in this module is assumed to be inside of a file system
22
22
  * reliance set.
23
23
  */
24
- const processes_1 = require("../../../lib-common/processes");
24
+ const synced_1 = require("../../../lib-common/processes/synced");
25
+ const common_1 = require("./common");
25
26
  const file_1 = require("../../../lib-common/exceptions/file");
26
27
  const error_1 = require("../../../lib-common/exceptions/error");
28
+ const exceptions_1 = require("../exceptions");
27
29
  const rxjs_1 = require("rxjs");
28
30
  const operators_1 = require("rxjs/operators");
29
31
  const attrs_1 = require("./attrs");
@@ -39,12 +41,12 @@ class NodeInFS {
39
41
  this.attrs = undefined;
40
42
  this.xattrs = undefined;
41
43
  this.writeProc = undefined;
42
- this.remoteEvents = undefined;
43
44
  /**
44
45
  * This is a lazily initialized field, when there is an external entity
45
46
  * that wants to see this node's events.
46
47
  */
47
48
  this.events = undefined;
49
+ this.isInSyncedStorage = (0, common_1.isSyncedStorage)(this.storage);
48
50
  }
49
51
  get version() {
50
52
  return this.currentVersion;
@@ -80,7 +82,7 @@ class NodeInFS {
80
82
  }
81
83
  return this.doChange(true, async () => {
82
84
  const { xattrs, newVersion } = this.getParamsForUpdate(changes);
83
- const base = await this.storage.getObj(this.objId);
85
+ const base = await this.storage.getObjSrc(this.objId);
84
86
  const sub = await this.crypto.writeXAttrs(xattrs, newVersion, base);
85
87
  await this.storage.saveObj(this.objId, newVersion, sub);
86
88
  this.setUpdatedParams(newVersion, undefined, xattrs);
@@ -96,66 +98,59 @@ class NodeInFS {
96
98
  getAttrs() {
97
99
  return this.attrs;
98
100
  }
99
- processRemoteEvent(event) {
100
- this.bufferRemoteEvent(event);
101
- return this.doChange(true, async () => {
102
- const event = this.getBufferedEvent();
103
- if (!event) {
104
- return;
105
- }
106
- if (event.type === 'remote-change') {
107
- // XXX should we use here synced storage methods here?
108
- // This method is called only in synced storage.
109
- // XXX detect if there is a conflict
110
- // need obj status info here
111
- // if (conflict) {
112
- // await this.doOnConflict();
113
- // } else {
114
- // await this.doOnExternalChange();
115
- // }
101
+ async listVersions() {
102
+ return (await this.storage.status(this.objId)).listVersions();
103
+ }
104
+ async archiveCurrent(version) {
105
+ if (this.isInSyncedStorage) {
106
+ const storage = this.syncedStorage();
107
+ const status = await storage.status(this.objId);
108
+ const { state, synced } = status.syncStatus();
109
+ if (state !== 'synced') {
110
+ throw (0, exceptions_1.makeFSSyncException)(this.name, { notSynced: true });
116
111
  }
117
- else if (event.type === 'remote-delete') {
118
- await this.delete(true);
112
+ if (version) {
113
+ if (synced.latest !== version) {
114
+ throw (0, exceptions_1.makeFSSyncException)(this.name, { versionMismatch: true });
115
+ }
119
116
  }
120
117
  else {
121
- throw new Error(`Unknown remote bufferred event type ${JSON.stringify(event)}`);
118
+ version = synced.latest;
122
119
  }
123
- });
124
- }
125
- bufferRemoteEvent(event) {
126
- if (!this.remoteEvents) {
127
- this.remoteEvents = [];
128
- }
129
- this.remoteEvents.push(event);
130
- }
131
- getBufferedEvent() {
132
- if (!this.remoteEvents) {
133
- return;
120
+ await storage.archiveVersionOnServer(this.objId, version);
121
+ await status.archiveCurrentVersion();
122
+ return version;
134
123
  }
135
- const event = this.remoteEvents.shift();
136
- if (this.remoteEvents.length === 0) {
137
- this.remoteEvents = undefined;
124
+ else {
125
+ if (version) {
126
+ if (this.currentVersion !== version) {
127
+ throw (0, file_1.makeFileException)(file_1.Code.versionMismatch, this.name);
128
+ }
129
+ }
130
+ else {
131
+ version = this.currentVersion;
132
+ }
133
+ const status = await this.storage.status(this.objId);
134
+ await status.archiveCurrentVersion();
135
+ return version;
138
136
  }
139
- return event;
140
137
  }
141
- localDelete() {
142
- return this.doChange(true, () => this.delete());
138
+ removeObj(src = 'local') {
139
+ return this.doChange(true, () => this.delete(src));
143
140
  }
144
141
  /**
145
142
  * This non-synchronized method deletes object from storage, and detaches
146
143
  * this node from storage. Make sure to call it inside access synchronization
147
144
  * construct.
148
145
  */
149
- async delete(remoteEvent) {
150
- if (!remoteEvent) {
151
- await this.storage.removeObj(this.objId);
152
- }
146
+ async delete(src) {
147
+ await this.storage.removeObj(this.objId);
153
148
  this.storage.nodes.delete(this);
154
149
  this.currentVersion = -1;
155
150
  const event = {
156
151
  type: 'removed',
157
152
  path: this.name,
158
- isRemote: remoteEvent
153
+ src
159
154
  };
160
155
  this.broadcastEvent(event, true);
161
156
  }
@@ -173,87 +168,225 @@ class NodeInFS {
173
168
  */
174
169
  async doChange(awaitPrevChange, change) {
175
170
  if (!this.writeProc) {
176
- this.writeProc = new processes_1.SingleProc();
171
+ this.writeProc = new synced_1.SingleProc();
177
172
  }
178
173
  if (!awaitPrevChange && this.writeProc.isProcessing()) {
179
- throw file_1.makeFileException(file_1.Code.concurrentUpdate, this.name + ` type ${this.type}`);
180
- }
181
- try {
182
- const res = await this.writeProc.startOrChain(() => {
183
- if (this.currentVersion < 0) {
184
- throw file_1.makeFileException(file_1.Code.notFound, this.name);
185
- }
186
- return change();
187
- });
188
- return res;
174
+ throw (0, file_1.makeFileException)(file_1.Code.concurrentUpdate, this.name + ` type ${this.type}`);
189
175
  }
190
- catch (exc) {
191
- if (!exc.runtimeException) {
192
- throw error_1.errWithCause(exc, `Cannot save changes to ${this.type} ${this.name}, version ${this.version}`);
176
+ const res = await this.writeProc.startOrChain(async () => {
177
+ if (this.currentVersion < 0) {
178
+ throw (0, file_1.makeFileException)(file_1.Code.notFound, this.name, `Object is marked removed`);
179
+ }
180
+ try {
181
+ const res = await change();
182
+ return res;
193
183
  }
194
- if (exc.type === 'storage') {
195
- if (exc.concurrentTransaction) {
196
- throw file_1.makeFileException(file_1.Code.concurrentUpdate, this.name, exc);
184
+ catch (exc) {
185
+ if (!exc.runtimeException) {
186
+ throw (0, error_1.errWithCause)(exc, `Cannot save changes to ${this.type} ${this.name}, version ${this.version}`);
187
+ }
188
+ if (exc.type === 'storage') {
189
+ if (exc.concurrentTransaction) {
190
+ throw (0, file_1.makeFileException)(file_1.Code.concurrentUpdate, this.name, exc);
191
+ }
192
+ else if (exc.objNotFound) {
193
+ throw (0, file_1.makeFileException)(file_1.Code.notFound, this.name, exc);
194
+ }
197
195
  }
198
- else if (exc.objNotFound) {
199
- throw file_1.makeFileException(file_1.Code.notFound, this.name, exc);
196
+ else if ((exc.type === 'file')
197
+ || (exc.type === 'fs-sync')) {
198
+ throw exc;
200
199
  }
200
+ throw (0, file_1.makeFileException)(file_1.Code.ioError, this.name, exc);
201
201
  }
202
- throw file_1.makeFileException(file_1.Code.ioError, this.name, exc);
202
+ });
203
+ return res;
204
+ }
205
+ broadcastEvent(event, complete, childObjId) {
206
+ if (this.events && complete) {
207
+ this.events.sink.next(event);
208
+ this.events.sink.complete();
209
+ this.events = undefined;
203
210
  }
211
+ this.storage.broadcastNodeEvent(this.objId, this.parentId, childObjId, event);
204
212
  }
205
- /**
206
- * This method is called on conflict with remote version. This method
207
- * is called at ordered point in time requiring no further synchronization.
208
- * @param remoteVersion is an object version on server
209
- */
210
- async doOnConflict(remoteVersion) {
211
- // XXX
212
- throw new Error('Unimplemented, yet');
213
- // OLD CODE BELOW
214
- // if (remoteVersion < this.version) { return; }
215
- // await (this.storage as SyncedStorage).setCurrentSyncedVersion(
216
- // this.objId, remoteVersion);
217
- // this.setCurrentVersion(remoteVersion);
213
+ get event$() {
214
+ if (!this.events) {
215
+ const sink = new rxjs_1.Subject();
216
+ const out = sink.asObservable().pipe((0, operators_1.share)());
217
+ const storeSub = this.storage.getNodeEvents()
218
+ .pipe((0, operators_1.filter)(({ objId }) => (this.objId === objId)), (0, operators_1.map)(({ event }) => copyWithPathIfRemoteEvent(event, this.name)))
219
+ .subscribe({
220
+ next: event => sink.next(event),
221
+ complete: () => {
222
+ var _a;
223
+ sink.complete();
224
+ if (((_a = this.events) === null || _a === void 0 ? void 0 : _a.sink) === sink) {
225
+ this.events = undefined;
226
+ }
227
+ },
228
+ error: err => {
229
+ var _a;
230
+ sink.error(err);
231
+ if (((_a = this.events) === null || _a === void 0 ? void 0 : _a.sink) === sink) {
232
+ this.events = undefined;
233
+ }
234
+ }
235
+ });
236
+ this.events = { sink, out, storeSub };
237
+ }
238
+ return this.events.out;
218
239
  }
219
- /**
220
- * This non-synchronized method resolves conflict with remote version.
221
- * @param remoteVersion
222
- */
223
- async doOnExternalChange(remoteVersion) {
224
- if (remoteVersion <= this.version) {
225
- return;
240
+ syncedStorage() {
241
+ if (!this.isInSyncedStorage) {
242
+ throw new Error(`Storage is not synced`);
226
243
  }
227
- const src = await this.storage.getObj(this.objId);
228
- const newVersion = src.version;
229
- if (newVersion <= this.version) {
230
- return;
244
+ return this.storage;
245
+ }
246
+ async syncStatus() {
247
+ const storage = this.syncedStorage();
248
+ const status = (await storage.status(this.objId)).syncStatus();
249
+ if (this.parentId) {
250
+ const parent = storage.nodes.get(this.parentId);
251
+ if (parent) {
252
+ status.existsInSyncedParent =
253
+ await parent.childExistsInSyncedVersion(this.objId);
254
+ }
231
255
  }
232
- this.setCurrentVersion(newVersion);
233
- const event = {
234
- type: 'file-change',
235
- path: this.name,
236
- isRemote: true
237
- };
238
- this.broadcastEvent(event);
256
+ return status;
239
257
  }
240
- broadcastEvent(event, complete) {
241
- if (!this.events) {
242
- return;
258
+ async updateStatusInfo() {
259
+ const storage = this.syncedStorage();
260
+ const status = await storage.updateStatusInfo(this.objId);
261
+ return await this.syncStatus();
262
+ }
263
+ isSyncedVersionOnDisk(version) {
264
+ const storage = this.syncedStorage();
265
+ return storage.isRemoteVersionOnDisk(this.objId, version);
266
+ }
267
+ async download(version) {
268
+ const storage = this.syncedStorage();
269
+ return storage.download(this.objId, version);
270
+ }
271
+ adoptRemote(opts) {
272
+ return this.doChange(true, async () => {
273
+ const storage = this.syncedStorage();
274
+ try {
275
+ const adopted = await storage.adoptRemote(this.objId, opts);
276
+ if (!adopted) {
277
+ return;
278
+ }
279
+ const src = await this.storage.getObjSrc(this.objId, adopted);
280
+ await this.setCurrentStateFrom(src);
281
+ const event = {
282
+ type: 'file-change',
283
+ src: 'sync',
284
+ path: this.name,
285
+ newVersion: this.version
286
+ };
287
+ this.broadcastEvent(event);
288
+ }
289
+ catch (exc) {
290
+ throw (0, common_1.setPathInExc)(exc, this.name);
291
+ }
292
+ });
293
+ }
294
+ async needUpload(localVersion) {
295
+ const { local, remote, synced } = await this.syncStatus();
296
+ if (localVersion) {
297
+ if (localVersion !== this.currentVersion) {
298
+ throw (0, exceptions_1.makeFSSyncException)(this.name, {
299
+ versionMismatch: true,
300
+ localVersion: this.currentVersion,
301
+ message: `Given local version ${localVersion} is not equal to current version ${this.currentVersion}`
302
+ });
303
+ }
304
+ if (!local || (local.latest !== localVersion)) {
305
+ throw (0, exceptions_1.makeFSSyncException)(this.name, {
306
+ versionMismatch: true,
307
+ message: `No local version ${localVersion} to upload`
308
+ });
309
+ }
310
+ }
311
+ else {
312
+ if (!local || !this.currentVersion) {
313
+ return;
314
+ }
315
+ localVersion = this.currentVersion;
316
+ }
317
+ if (remote) {
318
+ if (remote.latest) {
319
+ const uploadVersion = remote.latest + 1;
320
+ return { createOnRemote: false, localVersion, uploadVersion };
321
+ }
322
+ else {
323
+ throw (0, exceptions_1.makeFSSyncException)(this.name, {
324
+ versionMismatch: true,
325
+ removedOnServer: true
326
+ });
327
+ }
243
328
  }
244
- this.events.next(event);
245
- if (complete) {
246
- this.events.complete();
329
+ else if (synced) {
330
+ if (synced.latest) {
331
+ const uploadVersion = synced.latest + 1;
332
+ return { createOnRemote: false, localVersion, uploadVersion };
333
+ }
334
+ else {
335
+ throw (0, exceptions_1.makeFSSyncException)(this.name, {
336
+ versionMismatch: true,
337
+ removedOnServer: true
338
+ });
339
+ }
340
+ }
341
+ else {
342
+ const uploadVersion = 1;
343
+ return { createOnRemote: true, localVersion, uploadVersion };
247
344
  }
248
345
  }
249
- get event$() {
250
- if (!this.events) {
251
- this.events = new rxjs_1.Subject();
346
+ async upload(opts) {
347
+ try {
348
+ const toUpload = await this.needUpload(opts === null || opts === void 0 ? void 0 : opts.localVersion);
349
+ if (!toUpload) {
350
+ return;
351
+ }
352
+ const { localVersion, createOnRemote, uploadVersion } = toUpload;
353
+ const uploadHeader = ((localVersion === uploadVersion) ? undefined :
354
+ await this.uploadHeaderChange(localVersion, uploadVersion));
355
+ const storage = this.syncedStorage();
356
+ await storage.upload(this.objId, localVersion, uploadVersion, uploadHeader, createOnRemote);
357
+ await this.doChange(true, async () => {
358
+ storage.dropCachedLocalObjVersionsLessOrEqual(this.objId, localVersion);
359
+ if (this.currentVersion === localVersion) {
360
+ this.currentVersion = uploadVersion;
361
+ }
362
+ });
252
363
  }
253
- return this.events.asObservable().pipe(operators_1.share());
364
+ catch (exc) {
365
+ throw (0, common_1.setPathInExc)(exc, this.name);
366
+ }
367
+ }
368
+ async uploadHeaderChange(localVersion, uploadVersion) {
369
+ const currentSrc = await this.storage.getObjSrc(this.objId, localVersion);
370
+ const localHeader = await currentSrc.readHeader();
371
+ const uploadHeader = await this.crypto.reencryptHeader(localHeader, uploadVersion);
372
+ return { localHeader, localVersion, uploadHeader, uploadVersion };
254
373
  }
255
374
  }
256
375
  exports.NodeInFS = NodeInFS;
257
376
  Object.freeze(NodeInFS.prototype);
258
377
  Object.freeze(NodeInFS);
378
+ function copyWithPathIfRemoteEvent(e, path) {
379
+ switch (e.type) {
380
+ case 'remote-change':
381
+ return { type: e.type, path, newVersion: e.newVersion };
382
+ case 'remote-removal':
383
+ return { type: e.type, path };
384
+ case 'remote-version-archival':
385
+ return { type: e.type, path, archivedVersion: e.archivedVersion };
386
+ case 'remote-arch-ver-removal':
387
+ return { type: e.type, path, removedArchVer: e.removedArchVer };
388
+ default:
389
+ return e;
390
+ }
391
+ }
259
392
  Object.freeze(exports);
@@ -32,7 +32,7 @@ export declare abstract class NodePersistance {
32
32
  sinkPromise: Promise<FileByteSink>;
33
33
  sub: Subscribe;
34
34
  }>;
35
- reencryptHeader: (initHeader: Uint8Array, newVersion: number) => Promise<Uint8Array>;
35
+ reencryptHeader(initHeader: Uint8Array, newVersion: number): Promise<Uint8Array>;
36
36
  }
37
37
  declare type FileByteSource = web3n.files.FileByteSource;
38
38
  declare type FileByteSink = web3n.files.FileByteSink;
@@ -23,12 +23,11 @@ exports.NodePersistance = void 0;
23
23
  */
24
24
  const xsp_files_1 = require("xsp-files");
25
25
  const buffer_utils_1 = require("../../../lib-common/buffer-utils");
26
- const processes_1 = require("../../../lib-common/processes");
26
+ const deferred_1 = require("../../../lib-common/processes/deferred");
27
27
  const random = require("../../../lib-common/random-node");
28
28
  const pv1 = require("./xsp-payload-v1");
29
29
  const pv2 = require("./xsp-payload-v2");
30
30
  const SEG_SIZE = 16; // in 256-byte blocks = 4K in bytes
31
- const EMPTY_BYTE_ARR = new Uint8Array(0);
32
31
  /**
33
32
  * This does reading and writing, keeping keys. This and extending objects are
34
33
  * used in file system nodes as thematic place with persistence functionality.
@@ -38,14 +37,6 @@ class NodePersistance {
38
37
  this.zerothHeaderNonce = zerothHeaderNonce;
39
38
  this.key = key;
40
39
  this.cryptor = cryptor;
41
- this.reencryptHeader = async (initHeader, newVersion) => {
42
- if (!this.key) {
43
- throw new Error("Cannot use wiped object.");
44
- }
45
- const headerContent = await this.cryptor.formatWN.open(initHeader, this.key);
46
- const n = xsp_files_1.calculateNonce(this.zerothHeaderNonce, newVersion);
47
- return this.cryptor.formatWN.pack(headerContent, n, this.key);
48
- };
49
40
  }
50
41
  wipe() {
51
42
  if (this.key) {
@@ -57,7 +48,7 @@ class NodePersistance {
57
48
  }
58
49
  compareKey(keyB64) {
59
50
  const k = buffer_utils_1.base64.open(keyB64);
60
- return xsp_files_1.compareVectors(k, this.key);
51
+ return (0, xsp_files_1.compareVectors)(k, this.key);
61
52
  }
62
53
  fileKeyInBase64() {
63
54
  if (!this.key) {
@@ -69,13 +60,13 @@ class NodePersistance {
69
60
  if (!this.key) {
70
61
  throw new Error("Cannot use wiped object.");
71
62
  }
72
- return xsp_files_1.makeSegmentsWriter(this.key, this.zerothHeaderNonce, version, { type: 'new', segSize: SEG_SIZE, payloadFormat: 2 }, random.bytes, this.cryptor);
63
+ return (0, xsp_files_1.makeSegmentsWriter)(this.key, this.zerothHeaderNonce, version, { type: 'new', segSize: SEG_SIZE, payloadFormat: 2 }, random.bytes, this.cryptor);
73
64
  }
74
65
  async segWriterWithBase(newVersion, base) {
75
66
  if (!this.key) {
76
67
  throw new Error("Cannot use wiped object.");
77
68
  }
78
- return xsp_files_1.makeSegmentsWriter(this.key, this.zerothHeaderNonce, newVersion, { type: 'update', base, payloadFormat: 2 }, random.bytes, this.cryptor);
69
+ return (0, xsp_files_1.makeSegmentsWriter)(this.key, this.zerothHeaderNonce, newVersion, { type: 'update', base, payloadFormat: 2 }, random.bytes, this.cryptor);
79
70
  }
80
71
  async decryptedByteSrc(src) {
81
72
  if (!this.key) {
@@ -83,10 +74,10 @@ class NodePersistance {
83
74
  }
84
75
  const version = src.version;
85
76
  const header = await src.readHeader();
86
- const segReader = await xsp_files_1.makeSegmentsReader(this.key, this.zerothHeaderNonce, version, header, this.cryptor);
77
+ const segReader = await (0, xsp_files_1.makeSegmentsReader)(this.key, this.zerothHeaderNonce, version, header, this.cryptor);
87
78
  return {
88
79
  version,
89
- byteSrc: xsp_files_1.makeDecryptedByteSource(src.segSrc, segReader),
80
+ byteSrc: (0, xsp_files_1.makeDecryptedByteSource)(src.segSrc, segReader),
90
81
  payloadFormat: segReader.payloadFormat
91
82
  };
92
83
  }
@@ -123,7 +114,7 @@ class NodePersistance {
123
114
  async writeWhole(content, newVersion, attrs, xattrs) {
124
115
  const bytes = (Array.isArray(content) ? content : [content]);
125
116
  const segWriter = await this.segWriter(newVersion);
126
- const { sink, sub } = xsp_files_1.makeEncryptingByteSink(segWriter);
117
+ const { sink, sub } = (0, xsp_files_1.makeEncryptingByteSink)(segWriter);
127
118
  return (obs, backpressure) => {
128
119
  // sub must be called before payload creation that uses sink
129
120
  const unsub = sub(obs, backpressure);
@@ -134,7 +125,7 @@ class NodePersistance {
134
125
  }
135
126
  async writeXAttrs(xattrs, newVersion, base) {
136
127
  const segWriter = await this.segWriterWithBase(newVersion, base);
137
- const { sink, sub } = xsp_files_1.makeEncryptingByteSink(segWriter);
128
+ const { sink, sub } = (0, xsp_files_1.makeEncryptingByteSink)(segWriter);
138
129
  return (obs, backpressure) => {
139
130
  // sub must be called before payload creation that uses sink
140
131
  const unsub = sub(obs, backpressure);
@@ -147,8 +138,8 @@ class NodePersistance {
147
138
  const segWriter = await (base ?
148
139
  this.segWriterWithBase(newVersion, base) :
149
140
  this.segWriter(newVersion));
150
- const { sink, sub } = xsp_files_1.makeEncryptingByteSink(segWriter);
151
- const defSink = processes_1.defer();
141
+ const { sink, sub } = (0, xsp_files_1.makeEncryptingByteSink)(segWriter);
142
+ const defSink = (0, deferred_1.defer)();
152
143
  return {
153
144
  sinkPromise: defSink.promise,
154
145
  sub: (obs, backpressure) => {
@@ -174,6 +165,14 @@ class NodePersistance {
174
165
  }
175
166
  };
176
167
  }
168
+ async reencryptHeader(initHeader, newVersion) {
169
+ if (!this.key) {
170
+ throw new Error("Cannot use wiped object.");
171
+ }
172
+ const headerContent = await this.cryptor.formatWN.open(initHeader, this.key);
173
+ const n = (0, xsp_files_1.calculateNonce)(this.zerothHeaderNonce, newVersion);
174
+ return this.cryptor.formatWN.pack(headerContent, n, this.key);
175
+ }
177
176
  }
178
177
  exports.NodePersistance = NodePersistance;
179
178
  Object.freeze(NodePersistance.prototype);
@@ -19,7 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.makeReadonlyPayload = void 0;
20
20
  const assert_1 = require("../../../lib-common/assert");
21
21
  const file_1 = require("../../../lib-common/exceptions/file");
22
- const processes_1 = require("../../../lib-common/processes");
22
+ const synced_1 = require("../../../lib-common/processes/synced");
23
23
  const attrs_1 = require("./attrs");
24
24
  async function makeReadonlyPayload(src) {
25
25
  const payload = await ReadonlyPayloadV1.makeFor(src);
@@ -31,7 +31,7 @@ class ReadonlyPayloadV1 {
31
31
  this.src = src;
32
32
  this.size = size;
33
33
  this.isEndless = isEndless;
34
- this.syncProc = new processes_1.SingleProc();
34
+ this.syncProc = new synced_1.SingleProc();
35
35
  this.xattrs = undefined;
36
36
  this.isEndless = (this.size === undefined);
37
37
  Object.seal(this);
@@ -61,8 +61,8 @@ class ReadonlyPayloadV1 {
61
61
  if (this.isEndless) {
62
62
  throw makeEndlessException();
63
63
  }
64
- assert_1.assert(Number.isInteger(start) && (start >= 0) && (start <= this.size));
65
- assert_1.assert(Number.isInteger(end) && (end >= start) && (end <= this.size));
64
+ (0, assert_1.assert)(Number.isInteger(start) && (start >= 0) && (start <= this.size));
65
+ (0, assert_1.assert)(Number.isInteger(end) && (end >= start) && (end <= this.size));
66
66
  if (end === start) {
67
67
  return;
68
68
  }
@@ -82,6 +82,6 @@ class ReadonlyPayloadV1 {
82
82
  Object.freeze(ReadonlyPayloadV1.prototype);
83
83
  Object.freeze(ReadonlyPayloadV1);
84
84
  function makeEndlessException() {
85
- return file_1.makeFileException(file_1.Code.isEndless, '');
85
+ return (0, file_1.makeFileException)(file_1.Code.isEndless, '');
86
86
  }
87
87
  Object.freeze(exports);