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
@@ -23,16 +23,16 @@ var headerChunkInfo;
23
23
  (function (headerChunkInfo) {
24
24
  function toBytes(hInfo) {
25
25
  const buf = Buffer.allocUnsafe(12);
26
- big_endian_1.packUintTo4Bytes(hInfo.len, buf, 0);
27
- big_endian_1.packUintTo8Bytes(hInfo.fileOfs, buf, 4);
26
+ (0, big_endian_1.packUintTo4Bytes)(hInfo.len, buf, 0);
27
+ (0, big_endian_1.packUintTo8Bytes)(hInfo.fileOfs, buf, 4);
28
28
  return buf;
29
29
  }
30
30
  headerChunkInfo.toBytes = toBytes;
31
31
  function fromBytes(b, i) {
32
32
  let bytesRead = 0;
33
- const len = big_endian_1.uintFrom4Bytes(b, i + bytesRead);
33
+ const len = (0, big_endian_1.uintFrom4Bytes)(b, i + bytesRead);
34
34
  bytesRead += 4;
35
- const fileOfs = big_endian_1.uintFrom8Bytes(b, i + bytesRead);
35
+ const fileOfs = (0, big_endian_1.uintFrom8Bytes)(b, i + bytesRead);
36
36
  bytesRead += 8;
37
37
  const hInfo = { len, fileOfs };
38
38
  return { hInfo: Object.freeze(hInfo), bytesRead };
@@ -60,18 +60,18 @@ var segsChunkInfo;
60
60
  let i = 0;
61
61
  buf[i] = flag;
62
62
  i += 1;
63
- big_endian_1.packUintTo8Bytes(sInfo.thisVerOfs, buf, i);
63
+ (0, big_endian_1.packUintTo8Bytes)(sInfo.thisVerOfs, buf, i);
64
64
  i += 8;
65
65
  if (sInfo.type !== 'new-endless') {
66
- big_endian_1.packUintTo8Bytes(sInfo.len, buf, 9);
66
+ (0, big_endian_1.packUintTo8Bytes)(sInfo.len, buf, 9);
67
67
  i += 8;
68
68
  }
69
69
  if ((sInfo.type === 'new-on-disk') || (sInfo.type === 'base-on-disk')) {
70
- big_endian_1.packUintTo8Bytes(sInfo.fileOfs, buf, i);
70
+ (0, big_endian_1.packUintTo8Bytes)(sInfo.fileOfs, buf, i);
71
71
  i += 8;
72
72
  }
73
73
  if ((sInfo.type === 'base') || (sInfo.type === 'base-on-disk')) {
74
- big_endian_1.packUintTo8Bytes(sInfo.baseVerOfs, buf, i);
74
+ (0, big_endian_1.packUintTo8Bytes)(sInfo.baseVerOfs, buf, i);
75
75
  }
76
76
  return buf;
77
77
  }
@@ -80,21 +80,21 @@ var segsChunkInfo;
80
80
  let bytesRead = 0;
81
81
  const flag = b[i + bytesRead];
82
82
  bytesRead += 1;
83
- const thisVerOfs = big_endian_1.uintFrom8Bytes(b, i + bytesRead);
83
+ const thisVerOfs = (0, big_endian_1.uintFrom8Bytes)(b, i + bytesRead);
84
84
  bytesRead += 8;
85
85
  let len = undefined;
86
86
  if ((flag & IS_ENDLESS_BITMASK) === 0) {
87
- len = big_endian_1.uintFrom8Bytes(b, i + bytesRead);
87
+ len = (0, big_endian_1.uintFrom8Bytes)(b, i + bytesRead);
88
88
  bytesRead += 8;
89
89
  }
90
90
  let fileOfs = undefined;
91
91
  if (flag & FILE_OFS_PRESENT_BITMASK) {
92
- fileOfs = big_endian_1.uintFrom8Bytes(b, i + bytesRead);
92
+ fileOfs = (0, big_endian_1.uintFrom8Bytes)(b, i + bytesRead);
93
93
  bytesRead += 8;
94
94
  }
95
95
  let baseVerOfs = undefined;
96
96
  if (flag & BASE_VER_OFS_PRESENT_BITMASK) {
97
- baseVerOfs = big_endian_1.uintFrom8Bytes(b, i + bytesRead);
97
+ baseVerOfs = (0, big_endian_1.uintFrom8Bytes)(b, i + bytesRead);
98
98
  bytesRead += 8;
99
99
  }
100
100
  const isOnDisk = (fileOfs !== undefined);
@@ -168,7 +168,7 @@ var layoutV1;
168
168
  if (a.baseVersion !== undefined) {
169
169
  flag |= BASE_PRESENT_BITMASK;
170
170
  baseBytes = Buffer.allocUnsafe(8);
171
- big_endian_1.packUintTo8Bytes(a.baseVersion, baseBytes, 0);
171
+ (0, big_endian_1.packUintTo8Bytes)(a.baseVersion, baseBytes, 0);
172
172
  if (a.allBaseBytesInFile) {
173
173
  flag |= ALL_BASE_BYTES_IN_FILE_BITMASK;
174
174
  }
@@ -214,7 +214,7 @@ var layoutV1;
214
214
  i += 1;
215
215
  let baseVersion = undefined;
216
216
  if (flag & BASE_PRESENT_BITMASK) {
217
- baseVersion = big_endian_1.uintFrom8Bytes(b, i);
217
+ baseVersion = (0, big_endian_1.uintFrom8Bytes)(b, i);
218
218
  i += 8;
219
219
  }
220
220
  let headerChunk = undefined;
@@ -0,0 +1,6 @@
1
+ export interface Deferred<T> {
2
+ promise: Promise<T>;
3
+ resolve: (result?: T | PromiseLike<T>) => void;
4
+ reject: (err: any) => void;
5
+ }
6
+ export declare function defer<T>(): Deferred<T>;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ /*
3
+ Copyright (C) 2015, 2017, 2019 - 2022 3NSoft Inc.
4
+
5
+ This program is free software: you can redistribute it and/or modify it under
6
+ the terms of the GNU General Public License as published by the Free Software
7
+ Foundation, either version 3 of the License, or (at your option) any later
8
+ version.
9
+
10
+ This program is distributed in the hope that it will be useful, but
11
+ WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+ See the GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License along with
16
+ this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.defer = void 0;
20
+ function defer() {
21
+ const d = {};
22
+ d.promise = new Promise((resolve, reject) => {
23
+ d.resolve = resolve;
24
+ d.reject = reject;
25
+ });
26
+ Object.freeze(d);
27
+ return d;
28
+ }
29
+ exports.defer = defer;
30
+ Object.freeze(exports);
@@ -0,0 +1,33 @@
1
+ export interface Task<PoolLabel extends string> {
2
+ neededExecutor(): PoolLabel | undefined;
3
+ process(): Promise<void>;
4
+ cancel(): Promise<void>;
5
+ }
6
+ declare class ProcessingPool<PoolLabel extends string> {
7
+ readonly label: PoolLabel;
8
+ private readonly maxProcs;
9
+ private readonly logError?;
10
+ private readonly inProcess;
11
+ private readonly queue;
12
+ isRunning: boolean;
13
+ constructor(label: PoolLabel, maxProcs: number, logError?: ((err: any, msg?: string) => Promise<void>) | undefined);
14
+ add(task: Task<any>): void;
15
+ private processNextQueued;
16
+ pause(): void;
17
+ start(): void;
18
+ private readonly doOnError;
19
+ stop(): Promise<void>;
20
+ }
21
+ export declare class LabelledExecPools<PoolLabel extends string> {
22
+ private readonly pools;
23
+ private isRunning;
24
+ constructor(setup: {
25
+ label: PoolLabel;
26
+ maxProcs: number;
27
+ }[], logError?: ProcessingPool<PoolLabel>['logError']);
28
+ add(task: Task<PoolLabel>, queueIfNotRunning?: boolean): void;
29
+ start(): void;
30
+ pause(): void;
31
+ stop(): Promise<void>;
32
+ }
33
+ export {};
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ /*
3
+ Copyright (C) 2022 3NSoft Inc.
4
+
5
+ This program is free software: you can redistribute it and/or modify it under
6
+ the terms of the GNU General Public License as published by the Free Software
7
+ Foundation, either version 3 of the License, or (at your option) any later
8
+ version.
9
+
10
+ This program is distributed in the hope that it will be useful, but
11
+ WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+ See the GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License along with
16
+ this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.LabelledExecPools = void 0;
20
+ const assert_1 = require("../assert");
21
+ class ProcessingPool {
22
+ constructor(label, maxProcs, logError) {
23
+ this.label = label;
24
+ this.maxProcs = maxProcs;
25
+ this.logError = logError;
26
+ this.inProcess = new Set();
27
+ this.queue = [];
28
+ this.isRunning = false;
29
+ this.doOnError = async (err) => {
30
+ if (this.logError) {
31
+ await this.logError(err, `Error in pool ${this.label}`);
32
+ }
33
+ };
34
+ Object.seal(this);
35
+ }
36
+ add(task) {
37
+ this.queue.push(task);
38
+ if (this.isRunning) {
39
+ this.processNextQueued();
40
+ }
41
+ }
42
+ async processNextQueued() {
43
+ if (!this.isRunning || (this.inProcess.size >= this.maxProcs)) {
44
+ return;
45
+ }
46
+ const task = this.queue.shift();
47
+ if (!task) {
48
+ return;
49
+ }
50
+ if (task.neededExecutor() !== this.label) {
51
+ return this.processNextQueued();
52
+ }
53
+ let proc = undefined;
54
+ try {
55
+ proc = task.process().catch(this.doOnError);
56
+ this.inProcess.add(proc);
57
+ await proc;
58
+ }
59
+ catch (err) {
60
+ await this.doOnError(err);
61
+ }
62
+ finally {
63
+ if (proc) {
64
+ this.inProcess.delete(proc);
65
+ }
66
+ }
67
+ return this.processNextQueued();
68
+ }
69
+ pause() {
70
+ this.isRunning = false;
71
+ }
72
+ start() {
73
+ this.isRunning = true;
74
+ const triggerCount = Math.min(this.maxProcs, this.queue.length);
75
+ for (let i = 0; i < triggerCount; i += 1) {
76
+ this.processNextQueued();
77
+ }
78
+ }
79
+ async stop() {
80
+ if (!this.isRunning) {
81
+ return;
82
+ }
83
+ this.isRunning = false;
84
+ const cancellations = this.queue.splice(0, this.queue.length)
85
+ .map(t => t.cancel().catch(this.doOnError));
86
+ await Promise.all(cancellations.concat(Array.from(this.inProcess)));
87
+ }
88
+ }
89
+ Object.freeze(ProcessingPool.prototype);
90
+ Object.freeze(ProcessingPool);
91
+ class LabelledExecPools {
92
+ constructor(setup, logError) {
93
+ this.pools = new Map();
94
+ this.isRunning = false;
95
+ (0, assert_1.assert)(setup.length > 0);
96
+ for (const { label, maxProcs } of setup) {
97
+ const pool = new ProcessingPool(label, maxProcs, logError);
98
+ this.pools.set(pool.label, pool);
99
+ }
100
+ Object.seal(this);
101
+ }
102
+ add(task, queueIfNotRunning = false) {
103
+ if (!this.isRunning && !queueIfNotRunning) {
104
+ return;
105
+ }
106
+ const label = task.neededExecutor();
107
+ if (!label) {
108
+ return;
109
+ }
110
+ const pool = this.pools.get(label);
111
+ if (!pool) {
112
+ throw new Error(`Task needs unknown pool ${label}`);
113
+ }
114
+ pool.add(task);
115
+ }
116
+ start() {
117
+ if (this.isRunning) {
118
+ return;
119
+ }
120
+ this.isRunning = true;
121
+ for (const pool of this.pools.values()) {
122
+ pool.start();
123
+ }
124
+ }
125
+ pause() {
126
+ this.isRunning = false;
127
+ }
128
+ async stop() {
129
+ if (!this.isRunning) {
130
+ return;
131
+ }
132
+ this.isRunning = false;
133
+ const cancelations = Array.from(this.pools.values())
134
+ .map(pool => pool.stop());
135
+ await Promise.all(cancelations);
136
+ }
137
+ }
138
+ exports.LabelledExecPools = LabelledExecPools;
139
+ Object.freeze(ProcessingPool.prototype);
140
+ Object.freeze(ProcessingPool);
141
+ Object.freeze(exports);
@@ -0,0 +1,7 @@
1
+ export declare class PressureValve {
2
+ private stopper;
3
+ constructor();
4
+ toggle(flag: boolean): void;
5
+ pressWithError(err: any): void;
6
+ readonly pressure: () => Promise<void>;
7
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ /*
3
+ Copyright (C) 2015, 2017, 2019 - 2022 3NSoft Inc.
4
+
5
+ This program is free software: you can redistribute it and/or modify it under
6
+ the terms of the GNU General Public License as published by the Free Software
7
+ Foundation, either version 3 of the License, or (at your option) any later
8
+ version.
9
+
10
+ This program is distributed in the hope that it will be useful, but
11
+ WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+ See the GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License along with
16
+ this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.PressureValve = void 0;
20
+ const error_1 = require("../exceptions/error");
21
+ const deferred_1 = require("./deferred");
22
+ class PressureValve {
23
+ constructor() {
24
+ this.stopper = undefined;
25
+ this.pressure = async () => {
26
+ if (!this.stopper) {
27
+ return;
28
+ }
29
+ await this.stopper.promise;
30
+ };
31
+ Object.seal(this);
32
+ }
33
+ toggle(flag) {
34
+ if (flag) {
35
+ if (this.stopper) {
36
+ return;
37
+ }
38
+ this.stopper = (0, deferred_1.defer)();
39
+ }
40
+ else {
41
+ if (!this.stopper) {
42
+ return;
43
+ }
44
+ this.stopper.resolve();
45
+ this.stopper = undefined;
46
+ }
47
+ }
48
+ pressWithError(err) {
49
+ this.toggle(true);
50
+ this.stopper.reject((0, error_1.errWithCause)(err, `Backpressure error`));
51
+ }
52
+ }
53
+ exports.PressureValve = PressureValve;
54
+ Object.freeze(PressureValve.prototype);
55
+ Object.freeze(PressureValve);
56
+ Object.freeze(exports);
@@ -0,0 +1 @@
1
+ export declare function sleep(millis: number): Promise<void>;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /*
3
+ Copyright (C) 2015 3NSoft Inc.
4
+
5
+ This program is free software: you can redistribute it and/or modify it under
6
+ the terms of the GNU General Public License as published by the Free Software
7
+ Foundation, either version 3 of the License, or (at your option) any later
8
+ version.
9
+
10
+ This program is distributed in the hope that it will be useful, but
11
+ WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+ See the GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License along with
16
+ this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.sleep = void 0;
20
+ function sleep(millis) {
21
+ return new Promise((resolve) => {
22
+ setTimeout(resolve, millis).unref();
23
+ });
24
+ }
25
+ exports.sleep = sleep;
26
+ Object.freeze(exports);
@@ -1,4 +1,3 @@
1
- export declare function sleep(millis: number): Promise<void>;
2
1
  /**
3
2
  * This represents a function that will create a promise, potentially starting
4
3
  * some background process, only when it is called. Such wrap of code is needed
@@ -68,46 +67,7 @@ export declare class SingleProc {
68
67
  private runIfIdle;
69
68
  startOrChain<T>(action: Action<T>): Promise<T>;
70
69
  }
71
- export declare class DeduppedRunner<T> {
72
- private readonly action;
73
- private proc;
74
- private waiting;
75
- constructor(action: Action<T>);
76
- trigger(): Promise<T>;
77
- private startAction;
78
- }
79
70
  /**
80
71
  * This wraps given function/method into syncing wrap.
81
72
  */
82
73
  export declare function makeSyncedFunc<T extends Function>(syncProc: SingleProc, thisArg: any, func: T): T;
83
- export interface Deferred<T> {
84
- promise: Promise<T>;
85
- resolve: (result?: T | PromiseLike<T>) => void;
86
- reject: (err: any) => void;
87
- }
88
- export declare function defer<T>(): Deferred<T>;
89
- export declare class PressureValve {
90
- private stopper;
91
- constructor();
92
- toggle(flag: boolean): void;
93
- pressWithError(err: any): void;
94
- readonly pressure: () => Promise<void>;
95
- }
96
- export declare function callWithTimeout<T>(f: () => Promise<T>, timeout: number, timeoutErr: () => any): Promise<T>;
97
- export declare class Worker<T> {
98
- private readonly process;
99
- private readonly stopPendingIn;
100
- private readonly queue;
101
- private readonly set;
102
- private isRunning;
103
- private readonly procs;
104
- private maxProcs;
105
- constructor(process: (w: T) => Promise<void>, stopPendingIn: (queue: T[]) => Promise<void>);
106
- add(w: T, queueIfNotRunning?: boolean): void;
107
- private addToQueueOnce;
108
- private getFromQueueHead;
109
- private runQueued;
110
- start(maxNumOfProcs: number): void;
111
- pause(): void;
112
- stop(): Promise<void>;
113
- }