core-3nweb-client-lib 0.26.1 → 0.27.3

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