moeralib 0.15.2 → 0.15.4

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.
package/lib/index.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.shorten = exports.resolve = exports.expand = exports.parseNodeName = exports.DEV_NAMING_SERVER = exports.MAIN_NAMING_SERVER = exports.MoeraNamingConnectionError = exports.MoeraNamingApiError = exports.MoeraNamingError = exports.MoeraNaming = void 0;
3
+ exports.validateNamingSchema = exports.shorten = exports.resolve = exports.expand = exports.parseNodeName = exports.DEV_NAMING_SERVER = exports.MAIN_NAMING_SERVER = exports.MoeraNamingConnectionError = exports.MoeraNamingApiError = exports.MoeraNamingError = exports.MoeraNaming = void 0;
4
4
  var naming_1 = require("./naming");
5
5
  Object.defineProperty(exports, "MoeraNaming", { enumerable: true, get: function () { return naming_1.MoeraNaming; } });
6
6
  Object.defineProperty(exports, "MoeraNamingError", { enumerable: true, get: function () { return naming_1.MoeraNamingError; } });
@@ -12,3 +12,5 @@ Object.defineProperty(exports, "parseNodeName", { enumerable: true, get: functio
12
12
  Object.defineProperty(exports, "expand", { enumerable: true, get: function () { return naming_1.expand; } });
13
13
  Object.defineProperty(exports, "resolve", { enumerable: true, get: function () { return naming_1.resolve; } });
14
14
  Object.defineProperty(exports, "shorten", { enumerable: true, get: function () { return naming_1.shorten; } });
15
+ var validate_1 = require("./validate");
16
+ Object.defineProperty(exports, "validateNamingSchema", { enumerable: true, get: function () { return validate_1.validateSchema; } });
@@ -13,28 +13,60 @@ exports.resolve = exports.expand = exports.shorten = exports.parseNodeName = exp
13
13
  const json_rpc_2_0_1 = require("json-rpc-2.0");
14
14
  const schema_1 = require("../schema");
15
15
  const validate_1 = require("./validate");
16
+ /**
17
+ * Main Moera naming server.
18
+ */
16
19
  exports.MAIN_NAMING_SERVER = "https://naming.moera.org/moera-naming";
20
+ /**
21
+ * Moera developers' naming server.
22
+ */
17
23
  exports.DEV_NAMING_SERVER = "https://naming-dev.moera.org/moera-naming";
24
+ /**
25
+ * Generic naming server error.
26
+ */
18
27
  class MoeraNamingError extends Error {
28
+ /**
29
+ * @param {string} method - API method name
30
+ * @param {string} message - error message
31
+ */
19
32
  constructor(method, message) {
20
33
  super(method + ": Naming server error: " + message);
21
34
  }
22
35
  }
23
36
  exports.MoeraNamingError = MoeraNamingError;
37
+ /**
38
+ * Naming server returned an error response.
39
+ */
24
40
  class MoeraNamingApiError extends MoeraNamingError {
41
+ /**
42
+ * @param {string} method - API method name
43
+ * @param {ErrorResult} result - server response
44
+ */
25
45
  constructor(method, result) {
26
46
  super(method, result.error.message);
27
47
  this.errorCode = result.error.code;
28
48
  }
29
49
  }
30
50
  exports.MoeraNamingApiError = MoeraNamingApiError;
51
+ /**
52
+ * Naming server connection error.
53
+ */
31
54
  class MoeraNamingConnectionError extends Error {
55
+ /**
56
+ * @param {string} message - error message
57
+ */
32
58
  constructor(message) {
33
59
  super("Naming server connection error: " + message);
34
60
  }
35
61
  }
36
62
  exports.MoeraNamingConnectionError = MoeraNamingConnectionError;
63
+ /**
64
+ * Naming API interface.
65
+ */
37
66
  class MoeraNaming {
67
+ /**
68
+ * @param {string} server - the naming server URL
69
+ */
38
70
  constructor(server = exports.MAIN_NAMING_SERVER) {
39
71
  this.rpcClient = new json_rpc_2_0_1.JSONRPCClient((request_1, _a) => __awaiter(this, [request_1, _a], void 0, function* (request, [method, schema]) {
40
72
  let response;
@@ -107,46 +139,125 @@ class MoeraNaming {
107
139
  this.rpcClient.receive(data);
108
140
  }));
109
141
  }
142
+ /**
143
+ * Register or update the name. See Architecture Overview for the `detailed description
144
+ * {@link https://moera.org/overview/naming.html} of the algorithm.
145
+ *
146
+ * @param {string} name - the name to be registered/updated
147
+ * @param {number} generation - the name generation to be registered/updated
148
+ * @param {string | null} updatingKey - the public key for verifying signatures of further updates of the name. May
149
+ * be ``null`` if the current generation of the name is updated – the current key is preserved in this case.
150
+ * @param {string | null} nodeUri - URI of the REST API endpoint of the node to which the name is assigned. May be
151
+ * ``null`` - the current URI is preserved in this case.
152
+ * @param {string | null} signingKey - the public key of the name owner. May be ``null`` – the current key is
153
+ * preserved in this case.
154
+ * @param {number | null} validFrom - the moment in time the owner's key is valid from. May be ``null`` if
155
+ * ``signingKey`` is also ``null``.
156
+ * @param {string | null} previousDigest - the unique identifier as reported by a naming server of the current state
157
+ * of the name. Used to detect the situations when the name was changed by someone else between sending
158
+ * the request and processing it. May be ``null`` if the name was never registered before.
159
+ * @param {string | null} signature - the signature, if required, ``null`` otherwise
160
+ * @return {Promise<string>} identifier of the operation that was created
161
+ */
110
162
  put(name_1, generation_1) {
111
163
  return __awaiter(this, arguments, void 0, function* (name, generation, updatingKey = null, nodeUri = null, signingKey = null, validFrom = null, previousDigest = null, signature = null) {
112
164
  return yield this.rpcClient.request("put", { name, generation, updatingKey, nodeUri, signingKey, validFrom, previousDigest, signature }, ["put", "string"]);
113
165
  });
114
166
  }
167
+ /**
168
+ * Get the current status of the operation.
169
+ *
170
+ * @param {string} operationId
171
+ * @return {Promise<OperationStatusInfo |null>} the operation status or ``null``, if the operation ID is unknown
172
+ */
115
173
  getStatus(operationId) {
116
174
  return __awaiter(this, void 0, void 0, function* () {
117
175
  return yield this.rpcClient.request("getStatus", { operationId }, ["getStatus", "OperationStatusInfo"]);
118
176
  });
119
177
  }
178
+ /**
179
+ * Get current information about the given generation of the name.
180
+ *
181
+ * @param {string} name
182
+ * @param {number} generation
183
+ * @return {Promise<RegisteredNameInfo>} the information or ``null``, if the name/generation is not found
184
+ */
120
185
  getCurrent(name, generation) {
121
186
  return __awaiter(this, void 0, void 0, function* () {
122
187
  return yield this.rpcClient.request("getCurrent", { name, generation }, ["getCurrent", "RegisteredNameInfo"]);
123
188
  });
124
189
  }
190
+ /**
191
+ * Get past information about the given generation of the name.
192
+ *
193
+ * @param {string} name
194
+ * @param {number} generation
195
+ * @param {number} at - the moment in time the information is related to
196
+ * @return {Promise<RegisteredNameInfo | null>} the information or ``null``, if the name/generation did not exist at
197
+ * the given moment
198
+ */
125
199
  getPast(name, generation, at) {
126
200
  return __awaiter(this, void 0, void 0, function* () {
127
201
  return yield this.rpcClient.request("getPast", { name, generation, at }, ["getPast", "RegisteredNameInfo"]);
128
202
  });
129
203
  }
204
+ /**
205
+ * Check if the given name is available for registration.
206
+ *
207
+ * @param {string} name
208
+ * @param {number} generation
209
+ * @return {Promise<boolean>} ``true``, if the name is free, ``false`` otherwise
210
+ */
130
211
  isFree(name, generation) {
131
212
  return __awaiter(this, void 0, void 0, function* () {
132
213
  return yield this.rpcClient.request("isFree", { name, generation }, ["isFree", "boolean"]);
133
214
  });
134
215
  }
216
+ /**
217
+ * Find a name that is close to the given name.
218
+ *
219
+ * @param {string} name
220
+ * @return {Promise<RegisteredNameInfo | null>} information about the name or ``null``, if no name found that is
221
+ * close enough
222
+ */
135
223
  getSimilar(name) {
136
224
  return __awaiter(this, void 0, void 0, function* () {
137
225
  return yield this.rpcClient.request("getSimilar", { name }, ["getSimilar", "RegisteredNameInfo"]);
138
226
  });
139
227
  }
228
+ /**
229
+ * Get the whole history of signing keys for the given name.
230
+ *
231
+ * @param {string} name
232
+ * @param {number} generation
233
+ * @return {Promise<SigningKeyInfo[]>} the keys
234
+ */
140
235
  getAllKeys(name, generation) {
141
236
  return __awaiter(this, void 0, void 0, function* () {
142
237
  return yield this.rpcClient.request("getAllKeys", { name, generation }, ["getAllKeys", "SigningKeyInfoArray"]);
143
238
  });
144
239
  }
240
+ /**
241
+ * Get the list of all registered names at the given moment. The list is returned in pages, one per call.
242
+ *
243
+ * @param {number} at - the moment in time the information is related to
244
+ * @param {number} page - number of the page to be returned (starting from 0)
245
+ * @param {number} size - size of the page
246
+ * @return {Promise<RegisteredNameInfo[]>}
247
+ */
145
248
  getAll(at, page, size) {
146
249
  return __awaiter(this, void 0, void 0, function* () {
147
250
  return yield this.rpcClient.request("getAll", { at, page, size }, ["getAll", "RegisteredNameInfoArray"]);
148
251
  });
149
252
  }
253
+ /**
254
+ * Get the list of all names registered after the given moment. The list is returned in pages, one per call.
255
+ *
256
+ * @param {number} at - the moment in time the information is related to
257
+ * @param {number} page - number of the page to be returned (starting from 0)
258
+ * @param {number} size - size of the page
259
+ * @return {Promise<RegisteredNameInfo[]>}
260
+ */
150
261
  getAllNewer(at, page, size) {
151
262
  return __awaiter(this, void 0, void 0, function* () {
152
263
  return yield this.rpcClient.request("getAllNewer", { at, page, size }, ["getAllNewer", "RegisteredNameInfoArray"]);
@@ -154,6 +265,15 @@ class MoeraNaming {
154
265
  }
155
266
  }
156
267
  exports.MoeraNaming = MoeraNaming;
268
+ /**
269
+ * Parse a node name and return its name and generation parts.
270
+ *
271
+ * If the node name does not include a generation, generation 0 is returned. If name syntax is invalid, ``Error``
272
+ * is thrown.
273
+ *
274
+ * @param {string} nodeName - the node name to be parsed
275
+ * @return {[string, number]} [name, generation]
276
+ */
157
277
  function parseNodeName(nodeName) {
158
278
  let name = nodeName;
159
279
  let generation = 0;
@@ -192,6 +312,13 @@ function expand(nodeName) {
192
312
  return `${name}_${gen}`;
193
313
  }
194
314
  exports.expand = expand;
315
+ /**
316
+ * Shortcut function to resolve a node name and get the node URI.
317
+ *
318
+ * @param name {string} - the node name
319
+ * @param namingServer {string} - a naming server to be used
320
+ * @return {Promise<string | null>} the node URI, or ``null`` if the name does not exist
321
+ */
195
322
  function resolve(name_1) {
196
323
  return __awaiter(this, arguments, void 0, function* (name, namingServer = exports.MAIN_NAMING_SERVER) {
197
324
  var _a, _b;
@@ -12,36 +12,62 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.Caller = exports.moeraRoot = exports.MoeraCallError = exports.MoeraNodeConnectionError = exports.MoeraNodeApiError = exports.MoeraNodeError = void 0;
15
+ exports.Caller = exports.moeraRoot = exports.MoeraNodeCallError = exports.MoeraNodeConnectionError = exports.MoeraNodeApiError = exports.MoeraNodeError = void 0;
16
16
  const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
17
17
  const validate_1 = require("./validate");
18
18
  const util_1 = require("../util");
19
19
  const schema_1 = require("../schema");
20
+ /**
21
+ * Generic node error.
22
+ */
20
23
  class MoeraNodeError extends Error {
24
+ /**
25
+ * @param {string} name - request name
26
+ * @param {string} message - error message
27
+ */
21
28
  constructor(name, message) {
22
29
  super(name + ": Node error: " + message);
23
30
  }
24
31
  }
25
32
  exports.MoeraNodeError = MoeraNodeError;
33
+ /**
34
+ * Node returned an error response.
35
+ */
26
36
  class MoeraNodeApiError extends MoeraNodeError {
37
+ /**
38
+ * @param {string} name - request name
39
+ * @param {Result} result - node response
40
+ */
27
41
  constructor(name, result) {
28
42
  super(name, result.message);
29
43
  this.errorCode = result.errorCode;
30
44
  }
31
45
  }
32
46
  exports.MoeraNodeApiError = MoeraNodeApiError;
47
+ /**
48
+ * Error while connecting the node.
49
+ */
33
50
  class MoeraNodeConnectionError extends Error {
51
+ /**
52
+ * @param {string} message - error message
53
+ */
34
54
  constructor(message) {
35
55
  super("Node connection error: " + message);
36
56
  }
37
57
  }
38
58
  exports.MoeraNodeConnectionError = MoeraNodeConnectionError;
39
- class MoeraCallError extends Error {
59
+ /**
60
+ * Missing context of the call (authentication parameters or node URL).
61
+ */
62
+ class MoeraNodeCallError extends Error {
63
+ /**
64
+ * @param {string} message - error message
65
+ */
40
66
  constructor(message) {
41
67
  super(message);
42
68
  }
43
69
  }
44
- exports.MoeraCallError = MoeraCallError;
70
+ exports.MoeraNodeCallError = MoeraNodeCallError;
45
71
  function encodeBody(decoded, format) {
46
72
  var _a;
47
73
  if (decoded == null) {
@@ -116,6 +142,12 @@ function decodeBodies(name, data) {
116
142
  }
117
143
  return decoded;
118
144
  }
145
+ /**
146
+ * Convert partial node URL to a standardized form.
147
+ *
148
+ * @param {string} url - partial URL
149
+ * @return {string} standard URL
150
+ */
119
151
  function moeraRoot(url) {
120
152
  if (!url.startsWith("http:") && !url.startsWith("https:")) {
121
153
  url = "https://" + url;
@@ -134,6 +166,9 @@ function moeraRoot(url) {
134
166
  exports.moeraRoot = moeraRoot;
135
167
  class Caller {
136
168
  constructor() {
169
+ /**
170
+ * API endpoint URL of the node.
171
+ */
137
172
  this.root = null;
138
173
  this._rootSecret = null;
139
174
  this._token = null;
@@ -141,36 +176,93 @@ class Caller {
141
176
  this._carteSource = null;
142
177
  this._authMethod = "none";
143
178
  }
179
+ /**
180
+ * Set node URL.
181
+ *
182
+ * @param {string} url
183
+ */
144
184
  nodeUrl(url) {
145
185
  this.root = moeraRoot(url);
146
186
  }
187
+ /**
188
+ * Set root secret for authentication.
189
+ *
190
+ * @param {string} secret
191
+ */
147
192
  rootSecret(secret) {
148
193
  this._rootSecret = secret;
149
194
  }
195
+ /**
196
+ * Set admin token for authentication.
197
+ *
198
+ * @param {string} token
199
+ */
150
200
  token(token) {
151
201
  this._token = token;
152
202
  }
203
+ /**
204
+ * Set carte for authentication.
205
+ *
206
+ * @param {string} carte
207
+ */
153
208
  carte(carte) {
154
209
  this._carte = carte;
155
210
  }
211
+ /**
212
+ * Set a source of cartes for authentication.
213
+ * @param {CarteSource} carteSource
214
+ */
156
215
  carteSource(carteSource) {
157
216
  this._carteSource = carteSource;
158
217
  }
218
+ /**
219
+ * Select authentication method for the following requests.
220
+ *
221
+ * @param {NodeAuth} authMethod
222
+ */
159
223
  authMethod(authMethod) {
160
224
  this._authMethod = authMethod;
161
225
  }
226
+ /**
227
+ * Switch off authentication for the following requests.
228
+ */
162
229
  noAuth() {
163
230
  this.authMethod("none");
164
231
  }
232
+ /**
233
+ * Select carte authentication for the following requests.
234
+ */
165
235
  auth() {
166
236
  this.authMethod("peer");
167
237
  }
238
+ /**
239
+ * Select admin token authentication for the following requests.
240
+ */
168
241
  authAdmin() {
169
242
  this.authMethod("admin");
170
243
  }
244
+ /**
245
+ * Select root admin secret authentication for the following requests.
246
+ */
171
247
  authRootAdmin() {
172
248
  this.authMethod("root-admin");
173
249
  }
250
+ /**
251
+ * Generic method for making node API requests.
252
+ *
253
+ * @param {string} name - request name (for error messages)
254
+ * @param {string} location - request path
255
+ * @param {Partial<Record<string, string | number | boolean | null>> | null} params - query parameters, mapping
256
+ * name to value, ``null`` values are skipped
257
+ * @param {string} method - request method (one of 'GET', 'POST', 'PUT', 'DELETE'), the default is 'GET'
258
+ * @param {Structure | Structure[] | Buffer | null} body - request body
259
+ * @param {string | null} contentType - content-type of the request body, when it is not a JSON structure
260
+ * @param {boolean} auth - `true` (default) to authenticate the request, `false` otherwise
261
+ * @param {string} schema - JSON schema to validate the response
262
+ * @param {boolean} bodies - `true` to decode `Body` structures in the response, `false` (default) otherwise
263
+ * @param {boolean} srcBodies - `true` to encode `Body` structures in the request, `false` (default) otherwise
264
+ * @return {Promise<any>}
265
+ */
174
266
  call(name_1, location_1, _a) {
175
267
  return __awaiter(this, arguments, void 0, function* (name, location, { params = null, method = "GET", body = null, contentType = null, auth = true, schema, bodies = false, srcBodies = false }) {
176
268
  let bodyEncoded = null;
@@ -195,18 +287,18 @@ class Caller {
195
287
  bearer = "carte:" + this._carte;
196
288
  }
197
289
  else {
198
- throw new MoeraCallError("Carte is not set");
290
+ throw new MoeraNodeCallError("Carte is not set");
199
291
  }
200
292
  break;
201
293
  case "admin":
202
294
  if (this._token == null) {
203
- throw new MoeraCallError("Token is not set");
295
+ throw new MoeraNodeCallError("Token is not set");
204
296
  }
205
297
  bearer = "token:" + this._token;
206
298
  break;
207
299
  case "root-admin":
208
300
  if (this._rootSecret == null) {
209
- throw new MoeraCallError("Root secret is not set");
301
+ throw new MoeraNodeCallError("Root secret is not set");
210
302
  }
211
303
  bearer = "secret:" + this._rootSecret;
212
304
  break;
@@ -216,7 +308,7 @@ class Caller {
216
308
  headers["Authorization"] = `Bearer ${bearer}`;
217
309
  }
218
310
  if (this.root == null) {
219
- throw new MoeraCallError("Node URL is not set");
311
+ throw new MoeraNodeCallError("Node URL is not set");
220
312
  }
221
313
  let url = (0, util_1.urlWithParameters)(this.root + "/api" + location, params);
222
314
  let response;
@@ -10,7 +10,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MoeraCarteSource = exports.MoeraCartesError = void 0;
13
+ /**
14
+ * Error obtaining valid cartes.
15
+ */
13
16
  class MoeraCartesError extends Error {
17
+ /**
18
+ * @param {string} message - error message
19
+ */
14
20
  constructor(message) {
15
21
  super(message);
16
22
  }
@@ -19,16 +25,30 @@ exports.MoeraCartesError = MoeraCartesError;
19
25
  function isAdminCarte(carte) {
20
26
  return carte.permissions == null || carte.permissions.includes("other");
21
27
  }
28
+ /**
29
+ * Class that gets cartes from the given node, caches them and supplies them for authentication.
30
+ */
22
31
  class MoeraCarteSource {
32
+ /**
33
+ * @param {MoeraNode} node node to get cartes from
34
+ */
23
35
  constructor(node) {
24
36
  this.cartes = [];
25
37
  this.node = node;
26
38
  }
39
+ /**
40
+ * Force renewing the cached list of cartes.
41
+ */
27
42
  renew() {
28
43
  return __awaiter(this, void 0, void 0, function* () {
29
44
  this.cartes = (yield this.node.getCartes()).cartes;
30
45
  });
31
46
  }
47
+ /**
48
+ * Get a valid carte. Use one of the cached ones, if possible.
49
+ *
50
+ * @return {Promise<string>} the carte
51
+ */
32
52
  getCarte() {
33
53
  return __awaiter(this, void 0, void 0, function* () {
34
54
  for (const renewed of [false, true]) {
package/lib/node/index.js CHANGED
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MoeraCartesError = exports.MoeraCarteSource = exports.moeraRoot = exports.MoeraNodeConnectionError = exports.MoeraNodeApiError = exports.MoeraNodeError = exports.MoeraNode = void 0;
3
+ exports.validateNodeSchema = exports.MoeraCartesError = exports.MoeraCarteSource = exports.moeraRoot = exports.MoeraNodeCallError = exports.MoeraNodeConnectionError = exports.MoeraNodeApiError = exports.MoeraNodeError = exports.MoeraNode = void 0;
4
4
  var node_1 = require("./node");
5
5
  Object.defineProperty(exports, "MoeraNode", { enumerable: true, get: function () { return node_1.MoeraNode; } });
6
6
  var caller_1 = require("./caller");
7
7
  Object.defineProperty(exports, "MoeraNodeError", { enumerable: true, get: function () { return caller_1.MoeraNodeError; } });
8
8
  Object.defineProperty(exports, "MoeraNodeApiError", { enumerable: true, get: function () { return caller_1.MoeraNodeApiError; } });
9
9
  Object.defineProperty(exports, "MoeraNodeConnectionError", { enumerable: true, get: function () { return caller_1.MoeraNodeConnectionError; } });
10
+ Object.defineProperty(exports, "MoeraNodeCallError", { enumerable: true, get: function () { return caller_1.MoeraNodeCallError; } });
10
11
  Object.defineProperty(exports, "moeraRoot", { enumerable: true, get: function () { return caller_1.moeraRoot; } });
11
12
  var cartes_1 = require("./cartes");
12
13
  Object.defineProperty(exports, "MoeraCarteSource", { enumerable: true, get: function () { return cartes_1.MoeraCarteSource; } });
13
14
  Object.defineProperty(exports, "MoeraCartesError", { enumerable: true, get: function () { return cartes_1.MoeraCartesError; } });
15
+ var validate_1 = require("./validate");
16
+ Object.defineProperty(exports, "validateNodeSchema", { enumerable: true, get: function () { return validate_1.validateSchema; } });