core-3nweb-client-lib 0.27.3 → 0.27.5

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 (102) hide show
  1. package/build/api-defs/files.d.ts +92 -29
  2. package/build/core/asmail/config/index.d.ts +2 -2
  3. package/build/core/asmail/config/index.js +2 -2
  4. package/build/core/asmail/config/invitations-anon.d.ts +10 -24
  5. package/build/core/asmail/config/invitations-anon.js +43 -31
  6. package/build/core/asmail/config/published-intro-key.d.ts +11 -22
  7. package/build/core/asmail/config/published-intro-key.js +47 -38
  8. package/build/core/asmail/delivery/per-recipient-wip.js +2 -2
  9. package/build/core/asmail/inbox/inbox-events.js +10 -5
  10. package/build/core/asmail/inbox/index.d.ts +3 -2
  11. package/build/core/asmail/inbox/index.js +15 -11
  12. package/build/core/asmail/inbox/msg-indexing.d.ts +17 -64
  13. package/build/core/asmail/inbox/msg-indexing.js +440 -311
  14. package/build/core/asmail/inbox/msg-on-disk.js +2 -1
  15. package/build/core/asmail/index.d.ts +1 -1
  16. package/build/core/asmail/index.js +5 -4
  17. package/build/core/asmail/keyring/correspondent-keys.d.ts +2 -2
  18. package/build/core/asmail/keyring/correspondent-keys.js +4 -2
  19. package/build/core/asmail/keyring/index.d.ts +10 -33
  20. package/build/core/asmail/keyring/index.js +88 -75
  21. package/build/core/asmail/msg/opener.js +4 -3
  22. package/build/core/asmail/msg/packer.d.ts +1 -0
  23. package/build/core/asmail/msg/packer.js +8 -4
  24. package/build/core/index.js +3 -5
  25. package/build/core/sign-in.js +1 -1
  26. package/build/core/storage/local/obj-files.js +2 -1
  27. package/build/core/storage/synced/obj-files.d.ts +2 -0
  28. package/build/core/storage/synced/obj-files.js +18 -1
  29. package/build/core/storage/synced/obj-status.d.ts +1 -0
  30. package/build/core/storage/synced/obj-status.js +10 -0
  31. package/build/core/storage/synced/storage.js +7 -23
  32. package/build/core/storage/synced/upload-header-file.js +4 -2
  33. package/build/core/storage/synced/upsyncer.js +3 -5
  34. package/build/ipc-via-protobuf/asmail-cap.js +14 -15
  35. package/build/ipc-via-protobuf/bytes.js +42 -18
  36. package/build/ipc-via-protobuf/file.d.ts +7 -0
  37. package/build/ipc-via-protobuf/file.js +103 -66
  38. package/build/ipc-via-protobuf/fs.js +127 -105
  39. package/build/ipc-via-protobuf/mailerid.js +2 -2
  40. package/build/ipc-via-protobuf/protobuf-msg.d.ts +1 -0
  41. package/build/ipc-via-protobuf/protobuf-msg.js +5 -1
  42. package/build/ipc-via-protobuf/startup-cap.js +8 -8
  43. package/build/ipc-via-protobuf/storage-cap.js +4 -4
  44. package/build/lib-client/3nstorage/xsp-fs/common.js +2 -0
  45. package/build/lib-client/3nstorage/xsp-fs/file-node.d.ts +5 -10
  46. package/build/lib-client/3nstorage/xsp-fs/file-node.js +26 -45
  47. package/build/lib-client/3nstorage/xsp-fs/file.d.ts +8 -7
  48. package/build/lib-client/3nstorage/xsp-fs/file.js +12 -18
  49. package/build/lib-client/3nstorage/xsp-fs/folder-node.d.ts +6 -1
  50. package/build/lib-client/3nstorage/xsp-fs/folder-node.js +20 -8
  51. package/build/lib-client/3nstorage/xsp-fs/fs.d.ts +19 -18
  52. package/build/lib-client/3nstorage/xsp-fs/fs.js +18 -19
  53. package/build/lib-client/3nstorage/xsp-fs/node-in-fs.d.ts +13 -4
  54. package/build/lib-client/3nstorage/xsp-fs/node-in-fs.js +63 -8
  55. package/build/lib-client/3nstorage/xsp-fs/node-persistence.d.ts +1 -0
  56. package/build/lib-client/3nstorage/xsp-fs/node-persistence.js +7 -5
  57. package/build/lib-client/3nstorage/xsp-fs/xsp-payload-v1.js +3 -4
  58. package/build/lib-client/3nstorage/xsp-fs/xsp-payload-v2.js +20 -15
  59. package/build/lib-client/cryptor/cryptor-in-worker.js +37 -47
  60. package/build/lib-client/cryptor/cryptor-wasm.js +1 -1
  61. package/build/lib-client/cryptor/cryptor.wasm +0 -0
  62. package/build/lib-client/cryptor/in-proc-js.js +15 -12
  63. package/build/lib-client/cryptor/in-proc-wasm.js +11 -8
  64. package/build/lib-client/cryptor/serialization-for-wasm.d.ts +36 -0
  65. package/build/lib-client/cryptor/serialization-for-wasm.js +58 -0
  66. package/build/lib-client/cryptor-work-labels.d.ts +26 -0
  67. package/build/lib-client/cryptor-work-labels.js +152 -0
  68. package/build/lib-client/fs-sync-utils.d.ts +7 -1
  69. package/build/lib-client/fs-sync-utils.js +18 -7
  70. package/build/lib-client/local-files/dev-file-src.d.ts +2 -1
  71. package/build/lib-client/local-files/dev-file-src.js +5 -1
  72. package/build/lib-client/local-files/device-fs.js +2 -1
  73. package/build/lib-client/objs-on-disk/obj-on-disk.d.ts +5 -2
  74. package/build/lib-client/objs-on-disk/obj-on-disk.js +21 -2
  75. package/build/lib-client/request-utils.js +14 -14
  76. package/build/lib-common/async-cryptor-wrap.d.ts +9 -9
  77. package/build/lib-common/async-cryptor-wrap.js +13 -13
  78. package/build/lib-common/byte-streaming/pipe.d.ts +1 -1
  79. package/build/lib-common/byte-streaming/pipe.js +3 -3
  80. package/build/lib-common/byte-streaming/wrapping.js +4 -2
  81. package/build/lib-common/json-utils.js +0 -3
  82. package/build/lib-common/objs-on-disk/file-layout.js +1 -1
  83. package/build/lib-common/processes/synced.js +0 -184
  84. package/build/lib-sqlite-on-3nstorage/index.d.ts +32 -0
  85. package/build/lib-sqlite-on-3nstorage/index.js +117 -0
  86. package/build/lib-sqlite-on-3nstorage/sqljs.d.ts +279 -0
  87. package/build/lib-sqlite-on-3nstorage/sqljs.js +223 -0
  88. package/build/protos/asmail.proto.js +10145 -5923
  89. package/build/protos/bytes.proto.js +731 -204
  90. package/build/protos/common.proto.js +192 -44
  91. package/build/protos/cryptor.proto.js +184 -61
  92. package/build/protos/file.proto.js +2388 -502
  93. package/build/protos/fs.proto.js +9894 -6046
  94. package/build/protos/ipc.proto.js +244 -61
  95. package/build/protos/logger.proto.js +219 -53
  96. package/build/protos/mailerid.proto.js +230 -50
  97. package/build/protos/startup.proto.js +341 -77
  98. package/build/protos/storage.proto.js +276 -62
  99. package/package.json +8 -7
  100. package/protos/bytes.proto +13 -4
  101. package/protos/file.proto +27 -8
  102. package/protos/fs.proto +31 -14
@@ -32,6 +32,7 @@ const pipe_1 = require("../../../lib-common/byte-streaming/pipe");
32
32
  const buffer_utils_1 = require("../../../lib-common/buffer-utils");
33
33
  const rxjs_1 = require("rxjs");
34
34
  const operators_1 = require("rxjs/operators");
35
+ const node_in_fs_1 = require("./node-in-fs");
35
36
  const utils_for_observables_1 = require("../../../lib-common/utils-for-observables");
36
37
  function splitPathIntoParts(path) {
37
38
  return path_1.posix.resolve('/', path).split('/').filter(part => !!part);
@@ -550,38 +551,36 @@ class V {
550
551
  const node = await this.get(path);
551
552
  return node.updateXAttrs(changes);
552
553
  }
553
- async getXAttr(path, xaName) {
554
+ async getXAttr(path, xaName, flags) {
554
555
  const node = await this.get(path);
555
- const attr = node.getXAttr(xaName);
556
- return { attr, version: node.version };
556
+ return await node.getXAttr(xaName, flags);
557
557
  }
558
- async listXAttrs(path) {
558
+ async listXAttrs(path, flags) {
559
559
  const node = await this.get(path);
560
- return {
561
- lst: node.listXAttrs(),
562
- version: node.version
563
- };
560
+ return node.listXAttrs(flags);
564
561
  }
565
- async listFolder(path) {
562
+ async listFolder(path, flags) {
566
563
  const root = this.getRootIfNotClosed(path);
567
564
  const folder = await root.getFolderInThisSubTree(splitPathIntoParts(path), false).catch(setExcPath(path));
568
- return folder.list();
565
+ return ((0, node_in_fs_1.shouldReadCurrentVersion)(flags) ?
566
+ folder.list() :
567
+ folder.listNonCurrent(flags));
569
568
  }
570
569
  async writeBytes(path, bytes, flags = WRITE_NONEXCL_FLAGS) {
571
570
  this.ensureIsWritable();
572
571
  const f = await this.getOrCreateFile(path, flags);
573
572
  return f.save(bytes);
574
573
  }
575
- async readBytes(path, start, end) {
574
+ async readBytes(path, start, end, flags) {
576
575
  const file = await this.getOrCreateFile(path, {});
577
- return await file.readBytes(start, end);
576
+ return await file.readBytes(start, end, flags);
578
577
  }
579
578
  writeTxtFile(path, txt, flags = WRITE_NONEXCL_FLAGS) {
580
579
  const bytes = buffer_utils_1.utf8.pack(txt);
581
580
  return this.writeBytes(path, bytes, flags);
582
581
  }
583
- async readTxtFile(path) {
584
- const { bytes, version } = await this.readBytes(path);
582
+ async readTxtFile(path, flags) {
583
+ const { bytes, version } = await this.readBytes(path, undefined, undefined, flags);
585
584
  try {
586
585
  const txt = (bytes ? buffer_utils_1.utf8.open(bytes) : '');
587
586
  return { txt, version };
@@ -594,8 +593,8 @@ class V {
594
593
  const txt = JSON.stringify(json);
595
594
  return this.writeTxtFile(path, txt, flags);
596
595
  }
597
- async readJSONFile(path) {
598
- const { txt, version } = await this.readTxtFile(path);
596
+ async readJSONFile(path, flags) {
597
+ const { txt, version } = await this.readTxtFile(path, flags);
599
598
  try {
600
599
  const json = JSON.parse(txt);
601
600
  return { json, version };
@@ -609,9 +608,9 @@ class V {
609
608
  const f = await this.getOrCreateFile(path, flags);
610
609
  return f.writeSink(flags.truncate, flags.currentVersion);
611
610
  }
612
- async getByteSource(path) {
611
+ async getByteSource(path, flags) {
613
612
  const f = await this.getOrCreateFile(path, {});
614
- return f.readSrc();
613
+ return f.readSrc(flags);
615
614
  }
616
615
  }
617
616
  Object.freeze(V.prototype);
@@ -723,7 +722,7 @@ class S {
723
722
  this.n.ensureIsWritable();
724
723
  const node = await this.n.get(path);
725
724
  try {
726
- await node.upload(opts);
725
+ return await node.upload(opts);
727
726
  }
728
727
  catch (exc) {
729
728
  throw (0, common_1.setPathInExc)(exc, path);
@@ -9,6 +9,7 @@ declare type XAttrsChanges = web3n.files.XAttrsChanges;
9
9
  declare type SyncStatus = web3n.files.SyncStatus;
10
10
  declare type OptionsToAdopteRemote = web3n.files.OptionsToAdopteRemote;
11
11
  declare type OptionsToUploadLocal = web3n.files.OptionsToUploadLocal;
12
+ declare type VersionedReadFlags = web3n.files.VersionedReadFlags;
12
13
  export declare abstract class NodeInFS<P extends NodePersistance> implements Node {
13
14
  protected readonly storage: Storage;
14
15
  readonly type: NodeType;
@@ -24,6 +25,7 @@ export declare abstract class NodeInFS<P extends NodePersistance> implements Nod
24
25
  protected setCurrentVersion(newVersion: number): void;
25
26
  readonly isInSyncedStorage: boolean;
26
27
  protected constructor(storage: Storage, type: NodeType, name: string, objId: string, currentVersion: number, parentId: string | undefined);
28
+ protected getObjSrcOfVersion(flags: VersionedReadFlags | undefined): Promise<ObjSource>;
27
29
  private updatedXAttrs;
28
30
  protected setUpdatedParams(version: number, attrs: CommonAttrs | undefined, xattrs: XAttrs | undefined): void;
29
31
  protected getParamsForUpdate(changes: XAttrsChanges | undefined): {
@@ -32,8 +34,14 @@ export declare abstract class NodeInFS<P extends NodePersistance> implements Nod
32
34
  xattrs?: XAttrs;
33
35
  };
34
36
  updateXAttrs(changes: XAttrsChanges): Promise<number>;
35
- getXAttr(xaName: string): any;
36
- listXAttrs(): string[];
37
+ getXAttr(xaName: string, flags: VersionedReadFlags | undefined): Promise<{
38
+ attr: any;
39
+ version: number;
40
+ }>;
41
+ listXAttrs(flags: VersionedReadFlags | undefined): Promise<{
42
+ lst: string[];
43
+ version: number;
44
+ }>;
37
45
  getAttrs(): CommonAttrs;
38
46
  listVersions(): Promise<{
39
47
  current?: number;
@@ -41,7 +49,7 @@ export declare abstract class NodeInFS<P extends NodePersistance> implements Nod
41
49
  }>;
42
50
  archiveCurrent(version?: number): Promise<number>;
43
51
  removeNonFolderObj(src: FSChangeSrc): Promise<void>;
44
- protected removeThisNodeAsLeaf(src: FSChangeSrc): Promise<void>;
52
+ protected removeThisFromStorageNodes(src: FSChangeSrc): Promise<void>;
45
53
  /**
46
54
  * This method runs node changing function in an exclusive manner.
47
55
  * Returned promise resolves to whatever change function returns.
@@ -74,7 +82,8 @@ export declare abstract class NodeInFS<P extends NodePersistance> implements Nod
74
82
  uploadVersion: number;
75
83
  createOnRemote: boolean;
76
84
  } | undefined>;
77
- upload(opts: OptionsToUploadLocal | undefined): Promise<void>;
85
+ upload(opts: OptionsToUploadLocal | undefined): Promise<number | undefined>;
78
86
  protected uploadHeaderChange(localVersion: number, uploadVersion: number): Promise<UploadHeaderChange | undefined>;
79
87
  }
88
+ export declare function shouldReadCurrentVersion(flags: VersionedReadFlags | undefined): boolean;
80
89
  export {};
@@ -16,7 +16,7 @@
16
16
  this program. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.NodeInFS = void 0;
19
+ exports.shouldReadCurrentVersion = exports.NodeInFS = void 0;
20
20
  /**
21
21
  * Everything in this module is assumed to be inside of a file system
22
22
  * reliance set.
@@ -58,6 +58,19 @@ class NodeInFS {
58
58
  }
59
59
  this.currentVersion = newVersion;
60
60
  }
61
+ getObjSrcOfVersion(flags) {
62
+ if (flags) {
63
+ const { remoteVersion, archivedVersion } = flags;
64
+ if (remoteVersion) {
65
+ const store = this.syncedStorage();
66
+ return store.getObjSrcOfRemoteVersion(this.objId, remoteVersion);
67
+ }
68
+ else if (archivedVersion) {
69
+ return this.storage.getObjSrc(this.objId, archivedVersion, true);
70
+ }
71
+ }
72
+ return this.storage.getObjSrc(this.objId);
73
+ }
61
74
  updatedXAttrs(changes) {
62
75
  return (this.xattrs ?
63
76
  (changes ? this.xattrs.makeUpdated(changes) : this.xattrs) :
@@ -90,11 +103,39 @@ class NodeInFS {
90
103
  return this.version;
91
104
  });
92
105
  }
93
- getXAttr(xaName) {
94
- return (this.xattrs ? this.xattrs.get(xaName) : undefined);
106
+ async getXAttr(xaName, flags) {
107
+ if (shouldReadCurrentVersion(flags)) {
108
+ return {
109
+ attr: (this.xattrs ? this.xattrs.get(xaName) : undefined),
110
+ version: this.version
111
+ };
112
+ }
113
+ else {
114
+ const src = await this.getObjSrcOfVersion(flags);
115
+ const payload = await this.crypto.readonlyPayload(src);
116
+ const xattrs = await payload.getXAttrs();
117
+ return {
118
+ attr: xattrs.get(xaName),
119
+ version: src.version
120
+ };
121
+ }
95
122
  }
96
- listXAttrs() {
97
- return (this.xattrs ? this.xattrs.list() : []);
123
+ async listXAttrs(flags) {
124
+ if (shouldReadCurrentVersion(flags)) {
125
+ return {
126
+ lst: (this.xattrs ? this.xattrs.list() : []),
127
+ version: this.version
128
+ };
129
+ }
130
+ else {
131
+ const src = await this.getObjSrcOfVersion(flags);
132
+ const payload = await this.crypto.readonlyPayload(src);
133
+ const xattrs = await payload.getXAttrs();
134
+ return {
135
+ lst: xattrs.list(),
136
+ version: src.version
137
+ };
138
+ }
98
139
  }
99
140
  getAttrs() {
100
141
  return this.attrs;
@@ -138,9 +179,12 @@ class NodeInFS {
138
179
  }
139
180
  removeNonFolderObj(src) {
140
181
  (0, assert_1.assert)(this.type !== 'folder');
141
- return this.doChange(true, () => this.removeThisNodeAsLeaf(src));
182
+ return this.doChange(true, () => this.removeThisFromStorageNodes(src));
142
183
  }
143
- async removeThisNodeAsLeaf(src) {
184
+ async removeThisFromStorageNodes(src) {
185
+ if (this.currentVersion < 0) {
186
+ return;
187
+ }
144
188
  await this.storage.removeObj(this.objId);
145
189
  this.storage.nodes.delete(this);
146
190
  this.currentVersion = -1;
@@ -350,11 +394,12 @@ class NodeInFS {
350
394
  const uploadHeader = await this.uploadHeaderChange(localVersion, uploadVersion);
351
395
  const storage = this.syncedStorage();
352
396
  await storage.upload(this.objId, localVersion, uploadVersion, uploadHeader, createOnRemote);
353
- await this.doChange(true, async () => {
397
+ return await this.doChange(true, async () => {
354
398
  storage.dropCachedLocalObjVersionsLessOrEqual(this.objId, localVersion);
355
399
  if (this.currentVersion === localVersion) {
356
400
  this.currentVersion = uploadVersion;
357
401
  }
402
+ return uploadVersion;
358
403
  });
359
404
  }
360
405
  catch (exc) {
@@ -388,4 +433,14 @@ function copyWithPathIfRemoteEvent(e, path) {
388
433
  return e;
389
434
  }
390
435
  }
436
+ function shouldReadCurrentVersion(flags) {
437
+ if (flags) {
438
+ const { archivedVersion, remoteVersion } = flags;
439
+ if (archivedVersion || remoteVersion) {
440
+ return false;
441
+ }
442
+ }
443
+ return true;
444
+ }
445
+ exports.shouldReadCurrentVersion = shouldReadCurrentVersion;
391
446
  Object.freeze(exports);
@@ -12,6 +12,7 @@ export declare abstract class NodePersistance {
12
12
  private zerothHeaderNonce;
13
13
  private key;
14
14
  private cryptor;
15
+ private workLabel;
15
16
  protected constructor(zerothHeaderNonce: Uint8Array, key: Uint8Array, cryptor: AsyncSBoxCryptor);
16
17
  wipe(): void;
17
18
  compareKey(keyB64: string): boolean;
@@ -25,6 +25,7 @@ const xsp_files_1 = require("xsp-files");
25
25
  const buffer_utils_1 = require("../../../lib-common/buffer-utils");
26
26
  const deferred_1 = require("../../../lib-common/processes/deferred");
27
27
  const random = require("../../../lib-common/random-node");
28
+ const cryptor_work_labels_1 = require("../../cryptor-work-labels");
28
29
  const pv1 = require("./xsp-payload-v1");
29
30
  const pv2 = require("./xsp-payload-v2");
30
31
  const SEG_SIZE = 16; // in 256-byte blocks = 4K in bytes
@@ -37,6 +38,7 @@ class NodePersistance {
37
38
  this.zerothHeaderNonce = zerothHeaderNonce;
38
39
  this.key = key;
39
40
  this.cryptor = cryptor;
41
+ this.workLabel = cryptor_work_labels_1.cryptoWorkLabels.makeForNonce('storage', this.zerothHeaderNonce);
40
42
  }
41
43
  wipe() {
42
44
  if (this.key) {
@@ -60,13 +62,13 @@ class NodePersistance {
60
62
  if (!this.key) {
61
63
  throw new Error("Cannot use wiped object.");
62
64
  }
63
- return (0, xsp_files_1.makeSegmentsWriter)(this.key, this.zerothHeaderNonce, version, { type: 'new', segSize: SEG_SIZE, payloadFormat: 2 }, random.bytes, this.cryptor);
65
+ return (0, xsp_files_1.makeSegmentsWriter)(this.key, this.zerothHeaderNonce, version, { type: 'new', segSize: SEG_SIZE, payloadFormat: 2 }, random.bytes, this.cryptor, this.workLabel);
64
66
  }
65
67
  async segWriterWithBase(newVersion, base) {
66
68
  if (!this.key) {
67
69
  throw new Error("Cannot use wiped object.");
68
70
  }
69
- return (0, xsp_files_1.makeSegmentsWriter)(this.key, this.zerothHeaderNonce, newVersion, { type: 'update', base, payloadFormat: 2 }, random.bytes, this.cryptor);
71
+ return (0, xsp_files_1.makeSegmentsWriter)(this.key, this.zerothHeaderNonce, newVersion, { type: 'update', base, payloadFormat: 2 }, random.bytes, this.cryptor, this.workLabel);
70
72
  }
71
73
  async decryptedByteSrc(src) {
72
74
  if (!this.key) {
@@ -74,7 +76,7 @@ class NodePersistance {
74
76
  }
75
77
  const version = src.version;
76
78
  const header = await src.readHeader();
77
- const segReader = await (0, xsp_files_1.makeSegmentsReader)(this.key, this.zerothHeaderNonce, version, header, this.cryptor);
79
+ const segReader = await (0, xsp_files_1.makeSegmentsReader)(this.key, this.zerothHeaderNonce, version, header, this.cryptor, this.workLabel);
78
80
  return {
79
81
  version,
80
82
  byteSrc: (0, xsp_files_1.makeDecryptedByteSource)(src.segSrc, segReader),
@@ -169,9 +171,9 @@ class NodePersistance {
169
171
  if (!this.key) {
170
172
  throw new Error("Cannot use wiped object.");
171
173
  }
172
- const headerContent = await this.cryptor.formatWN.open(initHeader, this.key);
174
+ const headerContent = await this.cryptor.formatWN.open(initHeader, this.key, this.workLabel);
173
175
  const n = (0, xsp_files_1.calculateNonce)(this.zerothHeaderNonce, newVersion);
174
- return this.cryptor.formatWN.pack(headerContent, n, this.key);
176
+ return this.cryptor.formatWN.pack(headerContent, n, this.key, this.workLabel);
175
177
  }
176
178
  }
177
179
  exports.NodePersistance = NodePersistance;
@@ -67,14 +67,13 @@ class ReadonlyPayloadV1 {
67
67
  return;
68
68
  }
69
69
  return await this.syncProc.startOrChain(async () => {
70
- await this.src.seek(start);
71
- return await this.src.read(end - start);
70
+ return await this.src.readAt(start, end - start);
72
71
  });
73
72
  }
74
73
  makeFileByteSource() {
75
- const { getPosition, seek, read } = this.src;
74
+ const { getPosition, seek, readNext, readAt } = this.src;
76
75
  return {
77
- seek, getPosition, read,
76
+ seek, getPosition, readNext, readAt,
78
77
  getSize: async () => this.size,
79
78
  };
80
79
  }
@@ -140,22 +140,28 @@ class ReadonlyPayloadV2 {
140
140
  }
141
141
  makeFileByteSource() {
142
142
  let pos = 0;
143
+ const seek = async (ofs) => {
144
+ (0, assert_1.assert)(Number.isInteger(ofs) && (ofs >= 0) && (ofs <= this.size), `Offset must be an integer from 0 to size value, inclusive`);
145
+ pos = ofs;
146
+ };
147
+ const readNext = async (len) => {
148
+ if (len === undefined) {
149
+ len = this.size - pos;
150
+ }
151
+ const bytes = await this.readSomeContentBytes(pos, pos + len);
152
+ if (bytes) {
153
+ pos += bytes.length;
154
+ }
155
+ return bytes;
156
+ };
143
157
  return (0, wrapping_1.wrapAndSyncFileSource)({
144
- seek: async (ofs) => {
145
- (0, assert_1.assert)(Number.isInteger(ofs) && (ofs >= 0) && (ofs <= this.size), `Offset must be an integer from 0 to size value, inclusive`);
146
- pos = ofs;
147
- },
158
+ seek,
148
159
  getSize: async () => this.size,
149
160
  getPosition: async () => pos,
150
- read: async (len) => {
151
- if (len === undefined) {
152
- len = this.size - pos;
153
- }
154
- const bytes = await this.readSomeContentBytes(pos, pos + len);
155
- if (bytes) {
156
- pos += bytes.length;
157
- }
158
- return bytes;
161
+ readNext,
162
+ readAt: async (pos, len) => {
163
+ await seek(pos);
164
+ return await readNext(len);
159
165
  }
160
166
  });
161
167
  }
@@ -784,8 +790,7 @@ function payloadLayoutException(msg, cause) {
784
790
  };
785
791
  }
786
792
  async function sureReadOfBytesFrom(src, ofs, len) {
787
- await src.seek(ofs);
788
- const bytes = await src.read(len);
793
+ const bytes = await src.readAt(ofs, len);
789
794
  (0, assert_1.assert)(!!bytes && (bytes.length === len));
790
795
  return bytes;
791
796
  }
@@ -22,9 +22,9 @@ const os_1 = require("os");
22
22
  const deferred_1 = require("../../lib-common/processes/deferred");
23
23
  const error_1 = require("../../lib-common/exceptions/error");
24
24
  const path_1 = require("path");
25
- const protobuf_type_1 = require("../protobuf-type");
26
- const cryptor_proto_1 = require("../../protos/cryptor.proto");
27
25
  const assert_1 = require("../../lib-common/assert");
26
+ const serialization_for_wasm_1 = require("./serialization-for-wasm");
27
+ const cryptor_work_labels_1 = require("../cryptor-work-labels");
28
28
  const MAX_IDLE_MILLIS = 60 * 1000;
29
29
  const jsWorkerFName = 'worker-js.js';
30
30
  const wasmWorkerFName = 'worker-wasm.js';
@@ -73,6 +73,10 @@ class Workers {
73
73
  this.maxThreads = Math.max(1, ((typeof maxThreads === 'number') ?
74
74
  maxThreads : (0, os_1.cpus)().length - 1));
75
75
  }
76
+ numOfIdle() {
77
+ return this.idleWorkers.length +
78
+ Math.max(this.maxThreads - this.allWorkers.size, 0);
79
+ }
76
80
  async getIdleWorker() {
77
81
  const idle = this.idleWorkers.pop();
78
82
  if (idle) {
@@ -90,7 +94,7 @@ class Workers {
90
94
  }
91
95
  async doRequest(request, trans, d) {
92
96
  if (this.isClosed) {
93
- new Error(`Async cryptor is already closed`);
97
+ throw new Error(`Async cryptor is already closed`);
94
98
  }
95
99
  const worker = await this.getIdleWorker();
96
100
  this.replySinks.set(worker, d);
@@ -240,6 +244,7 @@ const makeInWorkerCryptor = (logErr, logWarning, maxThreads) => {
240
244
  (0, assert_1.assert)(typeof logWarning === 'function');
241
245
  const workers = new JsWorkers(logErr, logWarning, maxThreads);
242
246
  const close = workers.close.bind(workers);
247
+ const execCounter = new cryptor_work_labels_1.ExecCounter(() => workers.numOfIdle());
243
248
  const cryptor = {
244
249
  scrypt: (passwd, salt, logN, r, p, dkLen, progressCB) => workers.call('scrypt', [passwd, salt, logN, r, p, dkLen], undefined, progressCB),
245
250
  box: {
@@ -247,11 +252,12 @@ const makeInWorkerCryptor = (logErr, logWarning, maxThreads) => {
247
252
  generate_pubkey: (sk) => workers.call('box.generate_pubkey', [sk])
248
253
  },
249
254
  sbox: {
250
- open: (c, n, k) => workers.call('sbox.open', [c, n, k]),
251
- pack: (m, n, k) => workers.call('sbox.pack', [m, n, k]),
255
+ canStartUnderWorkLabel: l => execCounter.canStartUnderWorkLabel(l),
256
+ open: (c, n, k, workLabel) => execCounter.wrapOpPromise(workLabel, workers.call('sbox.open', [c, n, k])),
257
+ pack: (m, n, k, workLabel) => execCounter.wrapOpPromise(workLabel, workers.call('sbox.pack', [m, n, k])),
252
258
  formatWN: {
253
- open: (cn, k) => workers.call('sbox.formatWN.open', [cn, k]),
254
- pack: (m, n, k) => workers.call('sbox.formatWN.pack', [m, n, k])
259
+ open: (cn, k, workLabel) => execCounter.wrapOpPromise(workLabel, workers.call('sbox.formatWN.open', [cn, k])),
260
+ pack: (m, n, k, workLabel) => execCounter.wrapOpPromise(workLabel, workers.call('sbox.formatWN.pack', [m, n, k]))
255
261
  }
256
262
  },
257
263
  signing: {
@@ -266,45 +272,28 @@ exports.makeInWorkerCryptor = makeInWorkerCryptor;
266
272
  class WasmWorkers extends Workers {
267
273
  constructor(logErr, logWarning, maxThreads) {
268
274
  super(workerScriptFor('wasm'), logErr, logWarning, maxThreads);
269
- this.reqType = protobuf_type_1.ProtoType.for(cryptor_proto_1.cryptor.Request);
270
- this.replyType = protobuf_type_1.ProtoType.for(cryptor_proto_1.cryptor.Reply);
271
275
  }
272
276
  call(req, interim) {
273
- const msg = this.reqType.pack(req);
277
+ const msg = (0, serialization_for_wasm_1.packRequestToWASM)(req);
274
278
  return this.doRequest(msg, undefined, { res: (0, deferred_1.defer)(), interim });
275
279
  }
276
280
  processReply(replyBytes) {
277
- const { res, interim, err } = this.replyType.unpack(replyBytes);
281
+ const { res, interim, err } = (0, serialization_for_wasm_1.unpackReplyFromWASM)(replyBytes);
278
282
  return {
279
283
  res: res ? res.val : undefined,
280
284
  interim: interim ? interim.val : undefined,
281
- err: err ? toLocalErr(err) : undefined,
285
+ err: err ? (0, serialization_for_wasm_1.toLocalErr)(err) : undefined,
282
286
  };
283
287
  }
284
288
  }
285
289
  Object.freeze(WasmWorkers.prototype);
286
290
  Object.freeze(WasmWorkers);
287
- function toArgs(...args) {
288
- return args.map(val => ({ val }));
289
- }
290
- function toLocalErr(replyErr) {
291
- if (replyErr.condition === 'cipher-verification') {
292
- return { failedCipherVerification: true };
293
- }
294
- else if (replyErr.condition === 'signature-verification') {
295
- return { failedSignatureVerification: true };
296
- }
297
- else {
298
- return new Error(`WASM cryptor ${replyErr.condition}: ${replyErr.message}`);
299
- }
300
- }
301
291
  const makeInWorkerWasmCryptor = (logErr, logWarning, maxThreads) => {
302
292
  (0, assert_1.assert)(typeof logErr === 'function');
303
293
  (0, assert_1.assert)(typeof logWarning === 'function');
304
294
  const workers = new WasmWorkers(logErr, logWarning, maxThreads);
305
295
  const close = workers.close.bind(workers);
306
- const boolValType = protobuf_type_1.ProtoType.for(cryptor_proto_1.cryptor.BoolVal);
307
- const kpairType = protobuf_type_1.ProtoType.for(cryptor_proto_1.cryptor.Keypair);
296
+ const execCounter = new cryptor_work_labels_1.ExecCounter(() => workers.numOfIdle());
308
297
  const cryptor = {
309
298
  scrypt: (passwd, salt, logN, r, p, dkLen, progressCB) => workers.call({
310
299
  func: 1,
@@ -313,51 +302,52 @@ const makeInWorkerWasmCryptor = (logErr, logWarning, maxThreads) => {
313
302
  box: {
314
303
  calc_dhshared_key: (pk, sk) => workers.call({
315
304
  func: 2,
316
- byteArgs: toArgs(pk, sk)
305
+ byteArgs: (0, serialization_for_wasm_1.toArgs)(pk, sk)
317
306
  }),
318
307
  generate_pubkey: (sk) => workers.call({
319
308
  func: 3,
320
- byteArgs: toArgs(sk)
309
+ byteArgs: (0, serialization_for_wasm_1.toArgs)(sk)
321
310
  })
322
311
  },
323
312
  sbox: {
324
- open: (c, n, k) => workers.call({
313
+ canStartUnderWorkLabel: l => execCounter.canStartUnderWorkLabel(l),
314
+ open: (c, n, k, workLabel) => execCounter.wrapOpPromise(workLabel, workers.call({
325
315
  func: 4,
326
- byteArgs: toArgs(c, n, k)
327
- }),
328
- pack: (m, n, k) => workers.call({
316
+ byteArgs: (0, serialization_for_wasm_1.toArgs)(c, n, k)
317
+ })),
318
+ pack: (m, n, k, workLabel) => execCounter.wrapOpPromise(workLabel, workers.call({
329
319
  func: 5,
330
- byteArgs: toArgs(m, n, k)
331
- }),
320
+ byteArgs: (0, serialization_for_wasm_1.toArgs)(m, n, k)
321
+ })),
332
322
  formatWN: {
333
- open: (cn, k) => workers.call({
323
+ open: (cn, k, workLabel) => execCounter.wrapOpPromise(workLabel, workers.call({
334
324
  func: 6,
335
- byteArgs: toArgs(cn, k)
336
- }),
337
- pack: (m, n, k) => workers.call({
325
+ byteArgs: (0, serialization_for_wasm_1.toArgs)(cn, k)
326
+ })),
327
+ pack: (m, n, k, workLabel) => execCounter.wrapOpPromise(workLabel, workers.call({
338
328
  func: 7,
339
- byteArgs: toArgs(m, n, k)
340
- })
329
+ byteArgs: (0, serialization_for_wasm_1.toArgs)(m, n, k)
330
+ }))
341
331
  }
342
332
  },
343
333
  signing: {
344
334
  generate_keypair: async (seed) => {
345
335
  const rep = await workers.call({
346
336
  func: 8,
347
- byteArgs: toArgs(seed)
337
+ byteArgs: (0, serialization_for_wasm_1.toArgs)(seed)
348
338
  });
349
- return kpairType.unpack(rep);
339
+ return (0, serialization_for_wasm_1.unpackSigningKeyPair)(rep);
350
340
  },
351
341
  signature: (m, sk) => workers.call({
352
342
  func: 9,
353
- byteArgs: toArgs(m, sk)
343
+ byteArgs: (0, serialization_for_wasm_1.toArgs)(m, sk)
354
344
  }),
355
345
  verify: async (sig, m, pk) => {
356
346
  const rep = await workers.call({
357
347
  func: 10,
358
- byteArgs: toArgs(sig, m, pk)
348
+ byteArgs: (0, serialization_for_wasm_1.toArgs)(sig, m, pk)
359
349
  });
360
- return boolValType.unpack(rep).val;
350
+ return (0, serialization_for_wasm_1.unpackBoolVal)(rep);
361
351
  }
362
352
  }
363
353
  };