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
@@ -23,6 +23,7 @@ const buffer_utils_1 = require("./buffer-utils");
23
23
  * This returns an encryptor that packs bytes according to "with-nonce" format,
24
24
  * keeping track and automatically advancing nonce.
25
25
  * @param cryptor is an async cryptor that will be used by created encryptor.
26
+ * @param workLabel
26
27
  * @param key for new encryptor.
27
28
  * Note that key will be copied, thus, if given array shall never be used
28
29
  * anywhere, it should be wiped after this call.
@@ -33,7 +34,7 @@ const buffer_utils_1 = require("./buffer-utils");
33
34
  * @param delta is a number between 1 and 255 inclusive, used to advance nonce.
34
35
  * When missing, it defaults to one.
35
36
  */
36
- function makeEncryptor(cryptor, key, nextNonce, delta) {
37
+ function makeEncryptor(cryptor, workLabel, key, nextNonce, delta) {
37
38
  if (!(nextNonce instanceof Uint8Array)) {
38
39
  throw new TypeError("Nonce array nextNonce must be Uint8Array.");
39
40
  }
@@ -64,7 +65,7 @@ function makeEncryptor(cryptor, key, nextNonce, delta) {
64
65
  if (counter > counterMax) {
65
66
  throw new Error(`This encryptor has been used too many times. Further use may lead to duplication of nonces.`);
66
67
  }
67
- const c = await cryptor.formatWN.pack(m, nextNonce, key);
68
+ const c = await cryptor.formatWN.pack(m, nextNonce, key, workLabel);
68
69
  (0, xsp_files_1.advanceNonce)(nextNonce, delta);
69
70
  counter += 1;
70
71
  return c;
@@ -86,15 +87,14 @@ function makeEncryptor(cryptor, key, nextNonce, delta) {
86
87
  }
87
88
  exports.makeEncryptor = makeEncryptor;
88
89
  /**
89
- *
90
+ * @param cryptor
91
+ * @param workLabel
90
92
  * @param key for new decryptor.
91
- * @param arrFactory is typed arrays factory, used to allocated/find an array for use.
92
- * It may be undefined, in which case an internally created one is used.
93
- * Note that key will be copied, thus, if given array shall never be used anywhere,
94
- * it should be wiped after this call.
93
+ * Note that key will be copied, thus, if given array shall never be used
94
+ * anywhere, it should be wiped after this call.
95
95
  * @return a frozen object with pack & open and destroy functions.
96
96
  */
97
- function makeDecryptor(cryptor, key) {
97
+ function makeDecryptor(cryptor, workLabel, key) {
98
98
  if (!(key instanceof Uint8Array)) {
99
99
  throw new TypeError("Key array key must be Uint8Array.");
100
100
  }
@@ -107,7 +107,7 @@ function makeDecryptor(cryptor, key) {
107
107
  if (!key) {
108
108
  throw new Error(`This decryptor cannot be used, as it had already been destroyed.`);
109
109
  }
110
- return cryptor.formatWN.open(c, key);
110
+ return cryptor.formatWN.open(c, key, workLabel);
111
111
  },
112
112
  destroy: () => {
113
113
  if (!key) {
@@ -120,16 +120,16 @@ function makeDecryptor(cryptor, key) {
120
120
  return Object.freeze(decryptor);
121
121
  }
122
122
  exports.makeDecryptor = makeDecryptor;
123
- function makeDecryptorForKeyPair(cryptor, pkey, skey) {
123
+ function makeDecryptorForKeyPair(cryptor, workLabel, pkey, skey) {
124
124
  const dhSharedKey = ecma_nacl_1.box.calc_dhshared_key(pkey, skey);
125
- const decryptor = makeDecryptor(cryptor, dhSharedKey);
125
+ const decryptor = makeDecryptor(cryptor, workLabel, dhSharedKey);
126
126
  dhSharedKey.fill(0);
127
127
  return decryptor;
128
128
  }
129
129
  exports.makeDecryptorForKeyPair = makeDecryptorForKeyPair;
130
- function makeEncryptorWithKeyPair(cryptor, pkey, skey, nextNonce, delta) {
130
+ function makeEncryptorWithKeyPair(cryptor, workLabel, pkey, skey, nextNonce, delta) {
131
131
  const dhSharedKey = ecma_nacl_1.box.calc_dhshared_key(pkey, skey);
132
- const decryptor = makeEncryptor(cryptor, dhSharedKey, nextNonce, delta);
132
+ const decryptor = makeEncryptor(cryptor, workLabel, dhSharedKey, nextNonce, delta);
133
133
  dhSharedKey.fill(0);
134
134
  return decryptor;
135
135
  }
@@ -11,5 +11,5 @@ declare type FileByteSink = web3n.files.FileByteSink;
11
11
  * @param bufSize is an optional parameter for buffer, used for byte transfer.
12
12
  * Default value is 64K.
13
13
  */
14
- export declare function pipe(src: FileByteSource, sink: FileByteSink, progressCB?: ((bytesPiped: number) => void) | undefined, closeSink?: boolean, bufSize?: number): Promise<number>;
14
+ export declare function pipe(src: FileByteSource, sink: FileByteSink, progressCB?: ((bytesPiped: number) => void), closeSink?: boolean, bufSize?: number): Promise<number>;
15
15
  export {};
@@ -28,10 +28,10 @@ const sink_utils_1 = require("../obj-streaming/sink-utils");
28
28
  * @param bufSize is an optional parameter for buffer, used for byte transfer.
29
29
  * Default value is 64K.
30
30
  */
31
- async function pipe(src, sink, progressCB = undefined, closeSink = true, bufSize = 64 * 1024) {
31
+ async function pipe(src, sink, progressCB, closeSink = true, bufSize = 64 * 1024) {
32
32
  const contSink = (0, sink_utils_1.makeContinuousSink)(sink);
33
33
  try {
34
- let buf = await src.read(bufSize);
34
+ let buf = await src.readNext(bufSize);
35
35
  let bytesPiped = 0;
36
36
  while (buf) {
37
37
  await contSink(buf);
@@ -39,7 +39,7 @@ async function pipe(src, sink, progressCB = undefined, closeSink = true, bufSize
39
39
  if (progressCB) {
40
40
  progressCB(bytesPiped);
41
41
  }
42
- buf = await src.read(bufSize);
42
+ buf = await src.readNext(bufSize);
43
43
  }
44
44
  if (closeSink) {
45
45
  await contSink(null);
@@ -35,7 +35,8 @@ function wrapAndSyncSource(src) {
35
35
  const w = {
36
36
  getPosition: (0, synced_1.makeSyncedFunc)(syncProc, src, src.getPosition),
37
37
  getSize: (0, synced_1.makeSyncedFunc)(syncProc, src, src.getSize),
38
- read: (0, synced_1.makeSyncedFunc)(syncProc, src, src.read),
38
+ readNext: (0, synced_1.makeSyncedFunc)(syncProc, src, src.readNext),
39
+ readAt: (0, synced_1.makeSyncedFunc)(syncProc, src, src.readAt),
39
40
  seek: (0, synced_1.makeSyncedFunc)(syncProc, src, src.seek)
40
41
  };
41
42
  return w;
@@ -46,7 +47,8 @@ function wrapAndSyncFileSource(src) {
46
47
  const w = {
47
48
  getPosition: (0, synced_1.makeSyncedFunc)(syncProc, src, src.getPosition),
48
49
  getSize: (0, synced_1.makeSyncedFunc)(syncProc, src, src.getSize),
49
- read: (0, synced_1.makeSyncedFunc)(syncProc, src, src.read),
50
+ readNext: (0, synced_1.makeSyncedFunc)(syncProc, src, src.readNext),
51
+ readAt: (0, synced_1.makeSyncedFunc)(syncProc, src, src.readAt),
50
52
  seek: (0, synced_1.makeSyncedFunc)(syncProc, src, src.seek),
51
53
  };
52
54
  return w;
@@ -32,9 +32,6 @@ function copy(orig, excludeTopFields) {
32
32
  if (orig === null) {
33
33
  return null;
34
34
  }
35
- if (Buffer.isBuffer(orig)) {
36
- return orig;
37
- }
38
35
  if (ArrayBuffer.isView(orig)) {
39
36
  return orig;
40
37
  }
@@ -83,7 +83,7 @@ class ObjVersionBytesLayout {
83
83
  isFileComplete() {
84
84
  for (const chunk of this.segsChunks) {
85
85
  if ((chunk.type === 'new') || (chunk.type === 'new-endless')) {
86
- return true;
86
+ return false;
87
87
  }
88
88
  }
89
89
  return true;
@@ -158,49 +158,6 @@ class SingleProc {
158
158
  exports.SingleProc = SingleProc;
159
159
  Object.freeze(SingleProc.prototype);
160
160
  Object.freeze(SingleProc);
161
- // export class DeduppedRunner<T> {
162
- // private proc: Promise<T>|undefined = undefined;
163
- // private waiting: Deferred<T>|undefined = undefined;
164
- // constructor(
165
- // private readonly action: Action<T>
166
- // ) {
167
- // Object.seal(this);
168
- // }
169
- // trigger(): Promise<T> {
170
- // if (this.proc) {
171
- // if (!this.waiting) {
172
- // this.waiting = defer();
173
- // }
174
- // return this.waiting.promise;
175
- // } else {
176
- // this.proc = this.startAction();
177
- // return this.proc;
178
- // }
179
- // }
180
- // private async startAction(): Promise<T> {
181
- // const deferredBeforeStart = this.waiting;
182
- // this.waiting = undefined;
183
- // try {
184
- // const result = await this.action();
185
- // if (deferredBeforeStart) {
186
- // deferredBeforeStart.resolve(result);
187
- // }
188
- // return result;
189
- // } catch (err) {
190
- // if (deferredBeforeStart) {
191
- // deferredBeforeStart.reject(err);
192
- // }
193
- // throw err;
194
- // } finally {
195
- // this.proc = undefined;
196
- // if (this.waiting) {
197
- // this.proc = this.startAction();
198
- // }
199
- // }
200
- // }
201
- // }
202
- // Object.freeze(DeduppedRunner.prototype);
203
- // Object.freeze(DeduppedRunner);
204
161
  /**
205
162
  * This wraps given function/method into syncing wrap.
206
163
  */
@@ -208,145 +165,4 @@ function makeSyncedFunc(syncProc, thisArg, func) {
208
165
  return ((...args) => syncProc.startOrChain(() => func.apply(thisArg, args)));
209
166
  }
210
167
  exports.makeSyncedFunc = makeSyncedFunc;
211
- // export interface Deferred<T> {
212
- // promise: Promise<T>;
213
- // resolve: (result?: T|PromiseLike<T>) => void;
214
- // reject: (err: any) => void;
215
- // }
216
- // export function defer<T>(): Deferred<T> {
217
- // const d = <Deferred<T>> {};
218
- // d.promise = new Promise<T>((resolve, reject) => {
219
- // d.resolve = resolve;
220
- // d.reject = reject;
221
- // });
222
- // Object.freeze(d);
223
- // return d;
224
- // }
225
- // export class PressureValve {
226
- // private stopper: Deferred<void>|undefined = undefined;
227
- // constructor() {
228
- // Object.seal(this);
229
- // }
230
- // toggle(flag: boolean): void {
231
- // if (flag) {
232
- // if (this.stopper) { return; }
233
- // this.stopper = defer();
234
- // } else {
235
- // if (!this.stopper) { return; }
236
- // this.stopper.resolve();
237
- // this.stopper = undefined;
238
- // }
239
- // }
240
- // pressWithError(err: any): void {
241
- // this.toggle(true);
242
- // this.stopper!.reject(errWithCause(err, `Backpressure error`));
243
- // }
244
- // readonly pressure = async (): Promise<void> => {
245
- // if (!this.stopper) { return; }
246
- // await this.stopper.promise;
247
- // }
248
- // }
249
- // Object.freeze(PressureValve.prototype);
250
- // Object.freeze(PressureValve);
251
- // export function callWithTimeout<T>(
252
- // f: () => Promise<T>, timeout: number, timeoutErr: () => any
253
- // ): Promise<T> {
254
- // let isDone = false;
255
- // const deferred = defer<T>();
256
- // f().then(res => {
257
- // if (isDone) { return; }
258
- // isDone = true;
259
- // deferred.resolve(res);
260
- // }, err => {
261
- // if (isDone) { return; }
262
- // isDone = true;
263
- // deferred.reject(err);
264
- // });
265
- // sleep(timeout).then(() => {
266
- // if (isDone) { return; }
267
- // isDone = true;
268
- // const err = timeoutErr();
269
- // if (err) {
270
- // deferred.reject(err);
271
- // }
272
- // });
273
- // return deferred.promise;
274
- // }
275
- // export interface WorkerTask<WorkerLabel extends string> {
276
- // neededWorker(): WorkerLabel|undefined;
277
- // process(): Promise<void>;
278
- // cancel(): Promise<void>;
279
- // }
280
- // interface Worker {
281
- // readonly procs: Promise<void>[];
282
- // readonly maxProcs: number;
283
- // }
284
- // export class QueueAndWorkers<WorkerLabel extends string> {
285
- // private readonly queue: WorkerTask<WorkerLabel>[] = [];
286
- // private readonly workers = new Map<WorkerLabel, Worker>();
287
- // // private readonly set = new Set<WorkerTask>();
288
- // private isRunning = false;
289
- // // private readonly procs: Promise<void>[] = [];
290
- // // private maxProcs = 1;
291
- // constructor() {
292
- // Object.seal(this);
293
- // }
294
- // add(w: WorkerTask<WorkerLabel>, queueIfNotRunning = false): void {
295
- // if (this.isRunning) {
296
- // this.queue.push();
297
- // this.processNextQueued();
298
- // } else if (queueIfNotRunning) {
299
- // this.addToQueueOnce(w);
300
- // }
301
- // }
302
- // private addToQueueOnce(w: WorkerTask<WorkerLabel>): void {
303
- // if (!this.set.has(w)) {
304
- // this.queue.push(w);
305
- // this.set.add(w);
306
- // }
307
- // }
308
- // private getFromQueueHead(): WorkerTask<WorkerLabel>|undefined {
309
- // const w = this.queue.shift();
310
- // if (!w) { return; }
311
- // this.set.delete(w);
312
- // return w;
313
- // }
314
- // private async processNextQueued(): Promise<void> {
315
- // if (!this.isRunning || (this.procs.length >= this.maxProcs)) { return; }
316
- // const w = this.getFromQueueHead();
317
- // if (!w) { return; }
318
- // const proc = w.process();
319
- // this.procs.push(proc);
320
- // try {
321
- // await proc;
322
- // } finally {
323
- // const i = this.procs.indexOf(proc);
324
- // if (i >= 0) {
325
- // this.procs.splice(i, 1);
326
- // }
327
- // this.processNextQueued();
328
- // }
329
- // }
330
- // start(maxNumOfProcs = 1): void {
331
- // this.maxProcs = maxNumOfProcs;
332
- // this.isRunning = true;
333
- // this.processNextQueued();
334
- // }
335
- // pause(): void {
336
- // this.isRunning = false;
337
- // }
338
- // async stop(): Promise<void> {
339
- // this.isRunning = false;
340
- // const queue = this.queue.splice(0, this.queue.length);
341
- // this.set.clear();
342
- // for (const w of queue) {
343
- // await w.cancel();
344
- // }
345
- // while (this.procs.length > 0) {
346
- // await this.procs.pop();
347
- // }
348
- // }
349
- // }
350
- // Object.freeze(Worker.prototype);
351
- // Object.freeze(Worker);
352
168
  Object.freeze(exports);
@@ -0,0 +1,32 @@
1
+ import { Action, SingleProc } from '../lib-common/processes/synced';
2
+ import { Database as DBClass, BindParams as QueryParams, QueryExecResult as QueryResult } from './sqljs';
3
+ export declare type Database = DBClass;
4
+ export declare type BindParams = QueryParams;
5
+ export declare type QueryExecResult = QueryResult;
6
+ declare type WritableFile = web3n.files.WritableFile;
7
+ declare type ReadonlyFile = web3n.files.ReadonlyFile;
8
+ export declare abstract class SQLiteOn3NStorage {
9
+ protected readonly database: Database;
10
+ protected readonly file: WritableFile;
11
+ protected readonly syncProc: SingleProc;
12
+ protected constructor(database: Database, file: WritableFile);
13
+ get db(): Database;
14
+ get dbFile(): WritableFile;
15
+ sync<T>(action: Action<T>): Promise<T>;
16
+ listTables(): string[];
17
+ }
18
+ export declare class SQLiteOnUnversionedFS extends SQLiteOn3NStorage {
19
+ static makeAndStart(file: WritableFile): Promise<SQLiteOnUnversionedFS>;
20
+ saveToFile(): Promise<void>;
21
+ }
22
+ export declare class SQLiteOnVersionedFS extends SQLiteOn3NStorage {
23
+ protected constructor(db: Database, file: WritableFile);
24
+ static makeAndStart(file: WritableFile): Promise<SQLiteOnVersionedFS>;
25
+ saveToFile(): Promise<number>;
26
+ }
27
+ export declare class SQLiteOnSyncedFS extends SQLiteOnVersionedFS {
28
+ protected constructor(db: Database, file: WritableFile);
29
+ static makeAndStart(file: WritableFile): Promise<SQLiteOnSyncedFS>;
30
+ }
31
+ export declare function readDbFrom(file: ReadonlyFile): Promise<Database>;
32
+ export {};
@@ -0,0 +1,117 @@
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.readDbFrom = exports.SQLiteOnSyncedFS = exports.SQLiteOnVersionedFS = exports.SQLiteOnUnversionedFS = exports.SQLiteOn3NStorage = void 0;
20
+ const synced_1 = require("../lib-common/processes/synced");
21
+ const sqljs_1 = require("./sqljs");
22
+ class SQLiteOn3NStorage {
23
+ constructor(database, file) {
24
+ this.database = database;
25
+ this.file = file;
26
+ this.syncProc = new synced_1.SingleProc();
27
+ }
28
+ get db() {
29
+ return this.database;
30
+ }
31
+ get dbFile() {
32
+ return this.file;
33
+ }
34
+ sync(action) {
35
+ return this.syncProc.startOrChain(action);
36
+ }
37
+ listTables() {
38
+ const result = this.database.exec(`SELECT tbl_name FROM sqlite_schema WHERE type='table' AND name NOT LIKE 'sqlite_%'`);
39
+ return ((result.length > 0) ?
40
+ result[0].values.map(row => row[0]) :
41
+ []);
42
+ }
43
+ }
44
+ exports.SQLiteOn3NStorage = SQLiteOn3NStorage;
45
+ Object.freeze(SQLiteOn3NStorage.prototype);
46
+ Object.freeze(SQLiteOn3NStorage);
47
+ class SQLiteOnUnversionedFS extends SQLiteOn3NStorage {
48
+ static async makeAndStart(file) {
49
+ const db = await readDbFrom(file);
50
+ return new SQLiteOnUnversionedFS(db, file);
51
+ }
52
+ async saveToFile() {
53
+ await this.syncProc.startOrChain(async () => {
54
+ const dbFileContent = this.database.export();
55
+ await this.file.writeBytes(dbFileContent);
56
+ });
57
+ }
58
+ }
59
+ exports.SQLiteOnUnversionedFS = SQLiteOnUnversionedFS;
60
+ Object.freeze(SQLiteOnUnversionedFS.prototype);
61
+ Object.freeze(SQLiteOnUnversionedFS);
62
+ class SQLiteOnVersionedFS extends SQLiteOn3NStorage {
63
+ constructor(db, file) {
64
+ super(db, file);
65
+ if (!file.v) {
66
+ throw new Error(`Given file is not versioned`);
67
+ }
68
+ }
69
+ static async makeAndStart(file) {
70
+ const db = await readDbFrom(file);
71
+ return new SQLiteOnVersionedFS(db, file);
72
+ }
73
+ async saveToFile() {
74
+ return await this.syncProc.startOrChain(async () => {
75
+ const dbFileContent = this.database.export();
76
+ return await this.file.v.writeBytes(dbFileContent);
77
+ });
78
+ }
79
+ }
80
+ exports.SQLiteOnVersionedFS = SQLiteOnVersionedFS;
81
+ Object.freeze(SQLiteOnVersionedFS.prototype);
82
+ Object.freeze(SQLiteOnVersionedFS);
83
+ class SQLiteOnSyncedFS extends SQLiteOnVersionedFS {
84
+ constructor(db, file) {
85
+ var _a;
86
+ super(db, file);
87
+ if (!((_a = file.v) === null || _a === void 0 ? void 0 : _a.sync)) {
88
+ throw new Error(`Given file is not synced`);
89
+ }
90
+ }
91
+ static async makeAndStart(file) {
92
+ const db = await readDbFrom(file);
93
+ return new SQLiteOnSyncedFS(db, file);
94
+ }
95
+ }
96
+ exports.SQLiteOnSyncedFS = SQLiteOnSyncedFS;
97
+ Object.freeze(SQLiteOnSyncedFS.prototype);
98
+ Object.freeze(SQLiteOnSyncedFS);
99
+ async function readDbFrom(file) {
100
+ const SQL = await (0, sqljs_1.default)(true);
101
+ const fileContent = await readFileContent(file);
102
+ return new SQL.Database(fileContent);
103
+ }
104
+ exports.readDbFrom = readDbFrom;
105
+ async function readFileContent(file) {
106
+ try {
107
+ return await file.readBytes();
108
+ }
109
+ catch (exc) {
110
+ if (exc.notFound) {
111
+ return undefined;
112
+ }
113
+ else {
114
+ throw exc;
115
+ }
116
+ }
117
+ }