core-3nweb-client-lib 0.25.2 → 0.25.6

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.
@@ -75,7 +75,7 @@ var getUserId;
75
75
  function wrapService(fn) {
76
76
  return () => {
77
77
  const promise = fn()
78
- .then(userId => Buffer.from(userId, 'utf8'));
78
+ .then(userId => protobuf_msg_1.encodeToUtf8(userId));
79
79
  return { promise };
80
80
  };
81
81
  }
@@ -87,7 +87,7 @@ var getUserId;
87
87
  if (!buf) {
88
88
  throw connector_1.makeIPCException({ missingBodyBytes: true });
89
89
  }
90
- return buf.toString('utf8');
90
+ return protobuf_msg_1.decodeFromUtf8(buf);
91
91
  });
92
92
  }
93
93
  getUserId.makeCaller = makeCaller;
@@ -92,13 +92,13 @@ class ObjectsConnector {
92
92
  if (!this.messagingProc) {
93
93
  return;
94
94
  }
95
+ this.messagingProc.unsubscribe();
95
96
  if (err) {
96
97
  this.msgSink.error(err);
97
98
  }
98
99
  else {
99
100
  this.msgSink.complete();
100
101
  }
101
- this.messagingProc.unsubscribe();
102
102
  this.stop(false, err);
103
103
  }
104
104
  processIncomingMsg(msg) {
@@ -276,7 +276,7 @@ var readTxt;
276
276
  function wrapService(fn) {
277
277
  return () => {
278
278
  const promise = fn()
279
- .then(txt => Buffer.from(txt, 'utf8'));
279
+ .then(txt => protobuf_msg_1.encodeToUtf8(txt));
280
280
  return { promise };
281
281
  };
282
282
  }
@@ -285,13 +285,13 @@ var readTxt;
285
285
  const path = objPath.concat('readTxt');
286
286
  return () => caller
287
287
  .startPromiseCall(path, undefined)
288
- .then(buf => (buf ? buf.toString('utf8') : ''));
288
+ .then(buf => (buf ? protobuf_msg_1.decodeFromUtf8(buf) : ''));
289
289
  }
290
290
  readTxt.makeCaller = makeCaller;
291
291
  })(readTxt || (readTxt = {}));
292
292
  Object.freeze(readTxt);
293
293
  function packJSON(json) {
294
- return Buffer.from(JSON.stringify(json), 'utf8');
294
+ return protobuf_msg_1.encodeToUtf8(JSON.stringify(json));
295
295
  }
296
296
  exports.packJSON = packJSON;
297
297
  function unpackJSON(buf) {
@@ -299,7 +299,7 @@ function unpackJSON(buf) {
299
299
  throw connector_1.makeIPCException({ missingBodyBytes: true });
300
300
  }
301
301
  try {
302
- return JSON.parse(buf.toString('utf8'));
302
+ return JSON.parse(protobuf_msg_1.decodeFromUtf8(buf));
303
303
  }
304
304
  catch (err) {
305
305
  throw error_1.errWithCause(err, `Can't parse ipc reply as json`);
@@ -531,7 +531,7 @@ var readTxtFile;
531
531
  return buf => {
532
532
  const { path } = reqWithPathType.unpack(buf);
533
533
  const promise = fn(path)
534
- .then(txt => Buffer.from(txt, 'utf8'));
534
+ .then(txt => protobuf_msg_1.encodeToUtf8(txt));
535
535
  return { promise };
536
536
  };
537
537
  }
@@ -540,7 +540,7 @@ var readTxtFile;
540
540
  const ipcPath = objPath.concat('readTxtFile');
541
541
  return path => caller
542
542
  .startPromiseCall(ipcPath, reqWithPathType.pack({ path }))
543
- .then(buf => (buf ? buf.toString('utf8') : ''));
543
+ .then(buf => (buf ? protobuf_msg_1.decodeFromUtf8(buf) : ''));
544
544
  }
545
545
  readTxtFile.makeCaller = makeCaller;
546
546
  })(readTxtFile || (readTxtFile = {}));
@@ -20,6 +20,7 @@ exports.makeMailerIdCaller = exports.exposeMailerIdCAP = void 0;
20
20
  const connector_1 = require("./connector");
21
21
  const protobuf_type_1 = require("../lib-client/protobuf-type");
22
22
  const mailerid_proto_1 = require("../protos/mailerid.proto");
23
+ const protobuf_msg_1 = require("./protobuf-msg");
23
24
  function exposeMailerIdCAP(cap) {
24
25
  return {
25
26
  getUserId: getUserId.wrapService(cap.getUserId),
@@ -39,7 +40,7 @@ var getUserId;
39
40
  function wrapService(fn) {
40
41
  return () => {
41
42
  const promise = fn()
42
- .then(userId => Buffer.from(userId, 'utf8'));
43
+ .then(userId => protobuf_msg_1.encodeToUtf8(userId));
43
44
  return { promise };
44
45
  };
45
46
  }
@@ -51,7 +52,7 @@ var getUserId;
51
52
  if (!buf) {
52
53
  throw connector_1.makeIPCException({ missingBodyBytes: true });
53
54
  }
54
- return buf.toString('utf8');
55
+ return protobuf_msg_1.decodeFromUtf8(buf);
55
56
  });
56
57
  }
57
58
  getUserId.makeCaller = makeCaller;
@@ -42,4 +42,6 @@ export interface AnyValue {
42
42
  }
43
43
  export declare function toOptAny(value: any | undefined): AnyValue | undefined;
44
44
  export declare function valOfOptAny(valObj: AnyValue | undefined): any | undefined;
45
+ export declare function encodeToUtf8(str: string): Uint8Array;
46
+ export declare function decodeFromUtf8(bytes: Uint8Array): string;
45
47
  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.valOfOptAny = exports.toOptAny = exports.toOptJson = exports.valOfOptJson = exports.valOfOpt = exports.valOf = exports.toOptVal = exports.toVal = exports.errFromMsg = exports.errToMsg = exports.errBodyType = exports.unpackInt = exports.packInt = exports.valOfOptInt = exports.fixInt = exports.fixArray = exports.strArrValType = exports.boolValType = exports.objRefType = void 0;
19
+ exports.decodeFromUtf8 = exports.encodeToUtf8 = exports.valOfOptAny = exports.toOptAny = exports.toOptJson = exports.valOfOptJson = exports.valOfOpt = exports.valOf = exports.toOptVal = exports.toVal = exports.errFromMsg = exports.errToMsg = exports.errBodyType = exports.unpackInt = exports.packInt = exports.valOfOptInt = exports.fixInt = exports.fixArray = exports.strArrValType = exports.boolValType = exports.objRefType = void 0;
20
20
  const connector_1 = require("./connector");
21
21
  const error_1 = require("../lib-common/exceptions/error");
22
22
  const buffer_utils_1 = require("../lib-common/buffer-utils");
@@ -152,4 +152,14 @@ function valOfOptAny(valObj) {
152
152
  }
153
153
  }
154
154
  exports.valOfOptAny = valOfOptAny;
155
+ function encodeToUtf8(str) {
156
+ const enc = new TextEncoder();
157
+ return enc.encode(str);
158
+ }
159
+ exports.encodeToUtf8 = encodeToUtf8;
160
+ function decodeFromUtf8(bytes) {
161
+ const dec = new TextDecoder();
162
+ return dec.decode(bytes);
163
+ }
164
+ exports.decodeFromUtf8 = decodeFromUtf8;
155
165
  Object.freeze(exports);
@@ -114,20 +114,12 @@ ${error_1.stringifyErr(err)}`;
114
114
  }
115
115
  }
116
116
  function recordUnhandledRejectionsInProcess() {
117
- const unhandlePromiseRejectionLogWait = 200;
118
- const unhandledRejections = new Map();
119
- process.on('unhandledRejection', (reason, p) => {
120
- unhandledRejections.set(p, reason);
121
- setTimeout(() => {
122
- if (!unhandledRejections.has(p)) {
123
- return;
124
- }
125
- unhandledRejections.delete(p);
126
- logError(reason, `Unhandled exception in promise (logged after ${unhandlePromiseRejectionLogWait} milliseconds wait)`).catch(noop);
127
- }, unhandlePromiseRejectionLogWait).unref();
117
+ process.on('unhandledRejection', (err, p) => {
118
+ console.log(' -- Unhandled rejection of promise:', err);
119
+ logError(err, 'Unhandled rejection of promise');
128
120
  });
129
- process.on('rejectionHandled', p => unhandledRejections.delete(p));
130
121
  process.on('uncaughtException', err => {
122
+ console.log(' -- Unhandled exception:', err);
131
123
  logError(err, 'Unhandled exception');
132
124
  });
133
125
  }
@@ -8,6 +8,17 @@ export interface Exception extends web3n.RuntimeException {
8
8
  }
9
9
  export declare function makeNotFoundExc(msg: string): Exception;
10
10
  export declare function makeAlreadyExistExc(msg: string): Exception;
11
+ export interface GenerationCfg {
12
+ period: number;
13
+ lastDone: number;
14
+ }
15
+ export interface Cfg {
16
+ numOfSplits: number;
17
+ charsInSplit: number;
18
+ }
19
+ export interface CfgWithGens extends Cfg {
20
+ generations: GenerationCfg[];
21
+ }
11
22
  export declare type CanMoveObjToDeeperCache = (objId: string, folderPath: string) => Promise<boolean>;
12
23
  export declare class ObjFolders {
13
24
  private readonly path;
@@ -27,7 +38,7 @@ export declare class ObjFolders {
27
38
  static makeSimple(path: string, logError: LogError): Promise<ObjFolders>;
28
39
  private readonly saveCfg;
29
40
  private idToPathSections;
30
- private genBacketPath;
41
+ private genBucketPath;
31
42
  private findObjFolder;
32
43
  getFolderAccessFor(objId: ObjId, createIfMissing?: boolean): Promise<string | undefined>;
33
44
  removeFolderOf(objId: string): Promise<void>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- Copyright (C) 2016 - 2020 3NSoft Inc.
3
+ Copyright (C) 2016 - 2020, 2022 3NSoft Inc.
4
4
 
5
5
  This program is free software: you can redistribute it and/or modify it under
6
6
  the terms of the GNU General Public License as published by the Free Software
@@ -13,7 +13,8 @@
13
13
  See the GNU General Public License for more details.
14
14
 
15
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/>. */
16
+ this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
17
18
  Object.defineProperty(exports, "__esModule", { value: true });
18
19
  exports.ObjFolders = exports.makeAlreadyExistExc = exports.makeNotFoundExc = void 0;
19
20
  const fs = require("../../lib-common/async-fs-node");
@@ -79,6 +80,8 @@ class ObjFolders {
79
80
  numOfSplits: this.numOfSplits,
80
81
  generations: this.generations
81
82
  });
83
+ assert_1.assert(Number.isInteger(this.numOfSplits) && (this.numOfSplits > 0) &&
84
+ Number.isInteger(this.charsInSplit) && (this.charsInSplit > 0));
82
85
  this.accessFolder = path_1.join(this.path, ACCESS_DIR);
83
86
  if (this.generations) {
84
87
  if (!canMove) {
@@ -123,8 +126,8 @@ class ObjFolders {
123
126
  path.push(objId.substring(this.numOfSplits * this.charsInSplit));
124
127
  return path;
125
128
  }
126
- genBacketPath(backetIndex) {
127
- return path_1.join(this.generationsFolder, `${backetIndex}`);
129
+ genBucketPath(bucketIndex) {
130
+ return path_1.join(this.generationsFolder, `${bucketIndex}`);
128
131
  }
129
132
  async findObjFolder(pathSections) {
130
133
  const folder = path_1.join(this.accessFolder, ...pathSections);
@@ -135,7 +138,7 @@ class ObjFolders {
135
138
  return;
136
139
  }
137
140
  for (let i = 0; i < this.generations.length; i += 1) {
138
- const folder = path_1.join(this.genBacketPath(i), ...pathSections);
141
+ const folder = path_1.join(this.genBucketPath(i), ...pathSections);
139
142
  if (await folderExists(folder)) {
140
143
  return { folder, genBacket: i };
141
144
  }
@@ -162,7 +165,7 @@ class ObjFolders {
162
165
  if (found.genBacket === undefined) {
163
166
  return found.folder;
164
167
  }
165
- const folder = await move(this.genBacketPath(found.genBacket), pathSections, this.accessFolder);
168
+ const folder = await move(this.genBucketPath(found.genBacket), pathSections, this.accessFolder);
166
169
  return folder;
167
170
  }
168
171
  if (!createIfMissing) {
@@ -181,20 +184,20 @@ class ObjFolders {
181
184
  return;
182
185
  }
183
186
  const backetPath = ((found.genBacket === undefined) ?
184
- this.accessFolder : this.genBacketPath(found.genBacket));
187
+ this.accessFolder : this.genBucketPath(found.genBacket));
185
188
  await deleteTree(backetPath, pathSections);
186
189
  });
187
190
  }
188
191
  async listRecent() {
189
- const lst = (await allTreePaths(this.genBacketPath(0), this.numOfSplits, this.logError))
192
+ const lst = (await allTreePaths(this.accessFolder, this.numOfSplits, this.logError))
190
193
  .map(pathSections => ({
191
- path: path_1.join(this.genBacketPath(0), ...pathSections),
192
- objId: pathSections.join()
194
+ path: path_1.join(this.accessFolder, ...pathSections),
195
+ objId: pathSections.join('')
193
196
  }));
194
- lst.unshift({
195
- objId: null,
196
- path: path_1.join(this.accessFolder, ROOT_OBJ_DIR)
197
- });
197
+ const rootObjPath = path_1.join(this.accessFolder, ROOT_OBJ_DIR);
198
+ if (await fs.existsFolder(rootObjPath)) {
199
+ lst.unshift({ objId: null, path: rootObjPath });
200
+ }
198
201
  return lst;
199
202
  }
200
203
  }
@@ -475,19 +478,23 @@ function checkCfg(cfg, needGens = false) {
475
478
  async function allTreePaths(path, numOfSplits, logError) {
476
479
  const lst = await fs.readdir(path)
477
480
  .catch(async (exc) => {
478
- if (!exc.notFound) {
479
- await logError(exc, `Enumerating a split tree of objects, got an error while trying to directory list ${path}`);
481
+ if (exc.notFound) {
482
+ return;
480
483
  }
484
+ await logError(exc, `Enumerating a split tree of objects, got an error while trying to list directory ${path}`);
481
485
  });
482
486
  if (!lst) {
483
487
  return [];
484
488
  }
485
- if (numOfSplits <= 1) {
489
+ if (numOfSplits <= 0) {
486
490
  return lst.map(f => [f]);
487
491
  }
488
492
  const sections = [];
489
493
  for (let i = 0; i < lst.length; i += 1) {
490
494
  const f = lst[i];
495
+ if (f === ROOT_OBJ_DIR) {
496
+ continue;
497
+ }
491
498
  const treePaths = await allTreePaths(path_1.join(path, f), numOfSplits - 1, logError);
492
499
  for (let j = 0; j < treePaths.length; j += 1) {
493
500
  const branch = treePaths[j];
@@ -64,6 +64,12 @@ export declare function writeFromBuf(fd: number, pos: number, buf: Buffer): Prom
64
64
  * else return value is undefined and should be ignored.
65
65
  */
66
66
  export declare function createEmptyFile(filePath: string, fileSize: number, keepFileOpen?: boolean): Promise<number | undefined>;
67
+ /**
68
+ * @param path
69
+ * @return a promise, resolvable to true, if given path represents directory,
70
+ * or false, if it does not.
71
+ */
72
+ export declare function existsFolder(path: string): Promise<boolean>;
67
73
  /**
68
74
  * @param path
69
75
  * @return true, if given path represents directory, or false, if it does not.
@@ -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.ensureFolderExists = exports.copyTree = exports.copyFile = exports.streamFromFile = exports.rmDirWithContent = exports.read = exports.streamToExistingFile = exports.append = exports.write = exports.getFolderContentSize = exports.getFileSize = exports.existsFileSync = exports.existsFolderSync = exports.createEmptyFile = exports.writeFromBuf = exports.readToBuf = exports.ftruncate = exports.truncate = exports.rename = exports.unlink = exports.rmdir = exports.readdir = exports.fstat = exports.stat = exports.lstat = exports.readlink = exports.symlink = exports.close = exports.open = exports.mkdir = exports.appendFile = exports.writeFile = exports.readFile = void 0;
19
+ exports.ensureFolderExists = exports.copyTree = exports.copyFile = exports.streamFromFile = exports.rmDirWithContent = exports.read = exports.streamToExistingFile = exports.append = exports.write = exports.getFolderContentSize = exports.getFileSize = exports.existsFileSync = exports.existsFolderSync = exports.existsFolder = exports.createEmptyFile = exports.writeFromBuf = exports.readToBuf = exports.ftruncate = exports.truncate = exports.rename = exports.unlink = exports.rmdir = exports.readdir = exports.fstat = exports.stat = exports.lstat = exports.readlink = exports.symlink = exports.close = exports.open = exports.mkdir = exports.appendFile = exports.writeFile = exports.readFile = void 0;
20
20
  const fs = require("fs");
21
21
  const fs_1 = require("fs");
22
22
  const file_1 = require("./exceptions/file");
@@ -366,6 +366,29 @@ async function createEmptyFile(filePath, fileSize, keepFileOpen) {
366
366
  }
367
367
  }
368
368
  exports.createEmptyFile = createEmptyFile;
369
+ /**
370
+ * @param path
371
+ * @return a promise, resolvable to true, if given path represents directory,
372
+ * or false, if it does not.
373
+ */
374
+ async function existsFolder(path) {
375
+ try {
376
+ const stats = await stat(path);
377
+ if (stats.isDirectory()) {
378
+ return true;
379
+ }
380
+ else {
381
+ throw file_1.makeFileException(file_1.Code.notDirectory, path);
382
+ }
383
+ }
384
+ catch (e) {
385
+ if (e.code === file_1.Code.notFound) {
386
+ return false;
387
+ }
388
+ throw e;
389
+ }
390
+ }
391
+ exports.existsFolder = existsFolder;
369
392
  /**
370
393
  * @param path
371
394
  * @return true, if given path represents directory, or false, if it does not.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-3nweb-client-lib",
3
- "version": "0.25.2",
3
+ "version": "0.25.6",
4
4
  "description": "3NWeb client core library, embeddable into different environments",
5
5
  "main": "build/lib-index.js",
6
6
  "types": "build/lib-index.d.ts",