moeralib 0.15.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 (80) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +8 -0
  3. package/lib/naming/index.js +14 -0
  4. package/lib/naming/naming.js +203 -0
  5. package/lib/naming/schemas.mjs +184 -0
  6. package/lib/naming/types.js +2 -0
  7. package/lib/naming/validate.js +9 -0
  8. package/lib/naming/validators.js +2065 -0
  9. package/lib/node/caller.js +267 -0
  10. package/lib/node/cartes.js +55 -0
  11. package/lib/node/index.js +13 -0
  12. package/lib/node/node.js +1405 -0
  13. package/lib/node/schemas.mjs +4582 -0
  14. package/lib/node/types.js +3 -0
  15. package/lib/node/validate.js +9 -0
  16. package/lib/node/validators.js +60225 -0
  17. package/lib/schema.js +20 -0
  18. package/lib/schemas-compile.mjs +42 -0
  19. package/lib/universal-location.js +164 -0
  20. package/lib/util.js +42 -0
  21. package/nodejs-moera-api/nodejs-moera-api +4 -0
  22. package/nodejs-moera-api/nodejsmoeraapi.py +578 -0
  23. package/package.json +65 -0
  24. package/src/naming/index.ts +12 -0
  25. package/src/naming/naming.ts +234 -0
  26. package/src/naming/schemas.mjs +194 -0
  27. package/src/naming/types.ts +39 -0
  28. package/src/naming/validate.ts +6 -0
  29. package/src/naming/validators.d.ts +3 -0
  30. package/src/naming/validators.js +2084 -0
  31. package/src/node/caller.ts +311 -0
  32. package/src/node/cartes.ts +51 -0
  33. package/src/node/index.ts +3 -0
  34. package/src/node/node.ts +1285 -0
  35. package/src/node/schemas.mjs +4715 -0
  36. package/src/node/types.ts +1544 -0
  37. package/src/node/validate.ts +6 -0
  38. package/src/node/validators.d.ts +3 -0
  39. package/src/node/validators.js +60484 -0
  40. package/src/schema.ts +30 -0
  41. package/src/schemas-compile.mjs +51 -0
  42. package/src/universal-location.ts +212 -0
  43. package/src/util.ts +42 -0
  44. package/tsconfig.json +112 -0
  45. package/typings/naming/index.d.ts +2 -0
  46. package/typings/naming/index.d.ts.map +1 -0
  47. package/typings/naming/naming.d.ts +31 -0
  48. package/typings/naming/naming.d.ts.map +1 -0
  49. package/typings/naming/schemas.d.mts +271 -0
  50. package/typings/naming/schemas.d.mts.map +1 -0
  51. package/typings/naming/types.d.ts +35 -0
  52. package/typings/naming/types.d.ts.map +1 -0
  53. package/typings/naming/validate.d.ts +3 -0
  54. package/typings/naming/validate.d.ts.map +1 -0
  55. package/typings/naming/validators.d.ts +73 -0
  56. package/typings/naming/validators.d.ts.map +1 -0
  57. package/typings/node/caller.d.ts +52 -0
  58. package/typings/node/caller.d.ts.map +1 -0
  59. package/typings/node/cartes.d.ts +13 -0
  60. package/typings/node/cartes.d.ts.map +1 -0
  61. package/typings/node/index.d.ts +4 -0
  62. package/typings/node/index.d.ts.map +1 -0
  63. package/typings/node/node.d.ts +176 -0
  64. package/typings/node/node.d.ts.map +1 -0
  65. package/typings/node/schemas.d.mts +6205 -0
  66. package/typings/node/schemas.d.mts.map +1 -0
  67. package/typings/node/types.d.ts +1340 -0
  68. package/typings/node/types.d.ts.map +1 -0
  69. package/typings/node/validate.d.ts +3 -0
  70. package/typings/node/validate.d.ts.map +1 -0
  71. package/typings/node/validators.d.ts +920 -0
  72. package/typings/node/validators.d.ts.map +1 -0
  73. package/typings/schema.d.ts +18 -0
  74. package/typings/schema.d.ts.map +1 -0
  75. package/typings/schemas-compile.d.mts +2 -0
  76. package/typings/schemas-compile.d.mts.map +1 -0
  77. package/typings/universal-location.d.ts +25 -0
  78. package/typings/universal-location.d.ts.map +1 -0
  79. package/typings/util.d.ts +6 -0
  80. package/typings/util.d.ts.map +1 -0
@@ -0,0 +1,234 @@
1
+ import { JSONRPCClient } from 'json-rpc-2.0';
2
+
3
+ import { formatSchemaErrors } from "../schema";
4
+ import { validateSchema } from "./validate";
5
+ import { ErrorResult, OperationStatusInfo, RegisteredNameInfo, SigningKeyInfo } from "./types";
6
+
7
+ export const MAIN_NAMING_SERVER = "https://naming.moera.org/moera-naming";
8
+ export const DEV_NAMING_SERVER = "https://naming-dev.moera.org/moera-naming";
9
+
10
+ export class MoeraNamingError extends Error {
11
+
12
+ constructor(method: string, message: string) {
13
+ super(method + ": Naming server error: " + message);
14
+ }
15
+
16
+ }
17
+
18
+ export class MoeraNamingApiError extends MoeraNamingError {
19
+
20
+ errorCode: number;
21
+
22
+ constructor(method: string, result: ErrorResult) {
23
+ super(method, result.error.message);
24
+ this.errorCode = result.error.code;
25
+ }
26
+
27
+ }
28
+
29
+ export class MoeraNamingConnectionError extends Error {
30
+
31
+ constructor(message: string) {
32
+ super("Naming server connection error: " + message);
33
+ }
34
+
35
+ }
36
+
37
+ export class MoeraNaming {
38
+
39
+ private rpcClient: JSONRPCClient<[string, string]>;
40
+
41
+ constructor(server: string = MAIN_NAMING_SERVER) {
42
+ this.rpcClient = new JSONRPCClient(async (request, [method, schema]) => {
43
+ let response;
44
+ try {
45
+ response = await fetch(server, {
46
+ method: "POST",
47
+ headers: {
48
+ "accept": "application/json",
49
+ "content-type": "application/json"
50
+ },
51
+ body: JSON.stringify(request),
52
+ });
53
+ } catch (e) {
54
+ return Promise.reject(new MoeraNamingConnectionError(String(e)));
55
+ }
56
+
57
+ let data: any;
58
+ try {
59
+ data = await response.json();
60
+ } catch (e) {
61
+ if (!response.ok) {
62
+ return Promise.reject(new MoeraNamingError(method, "Server returned error status"));
63
+ } else {
64
+ return Promise.reject(new MoeraNamingError(method, "Server returned empty result"));
65
+ }
66
+ }
67
+
68
+ if ("error" in data) {
69
+ const {valid, errors} = validateSchema("ErrorResult", data)
70
+ if (!valid) {
71
+ return Promise.reject(
72
+ new MoeraNamingError(
73
+ method,
74
+ "Server returned invalid error response: " + formatSchemaErrors(errors)
75
+ )
76
+ );
77
+ }
78
+ return Promise.reject(new MoeraNamingApiError(method, data));
79
+ } else if (!response.ok && request.id !== undefined) {
80
+ return Promise.reject(
81
+ new MoeraNamingError(method, "Server returned invalid error response: " + JSON.stringify(data))
82
+ );
83
+ }
84
+
85
+ if (schema == "boolean") {
86
+ const {valid, errors} = validateSchema("BooleanResult", data);
87
+ if (!valid) {
88
+ return Promise.reject(
89
+ new MoeraNamingError(
90
+ method,
91
+ "Server returned invalid boolean response: " + formatSchemaErrors(errors)
92
+ )
93
+ );
94
+ }
95
+ this.rpcClient.receive(data);
96
+ return;
97
+ }
98
+
99
+ if (schema == "string") {
100
+ const {valid, errors} = validateSchema("StringResult", data);
101
+ if (!valid) {
102
+ return Promise.reject(
103
+ new MoeraNamingError(
104
+ method,
105
+ "Server returned invalid string response: " + formatSchemaErrors(errors)
106
+ )
107
+ );
108
+ }
109
+ this.rpcClient.receive(data);
110
+ return;
111
+ }
112
+
113
+ {
114
+ const {valid, errors} = validateSchema("ObjectResult", data);
115
+ if (!valid) {
116
+ return Promise.reject(
117
+ new MoeraNamingError(
118
+ method,
119
+ "Server returned invalid object response: " + formatSchemaErrors(errors)
120
+ )
121
+ );
122
+ }
123
+ }
124
+
125
+ const result = data.result;
126
+
127
+ if (result == null) {
128
+ this.rpcClient.receive(data);
129
+ return;
130
+ }
131
+
132
+ const {valid, errors} = validateSchema(schema, result);
133
+ if (!valid) {
134
+ return Promise.reject(
135
+ new MoeraNamingError(
136
+ method,
137
+ "Server returned invalid object: " + formatSchemaErrors(errors)
138
+ )
139
+ );
140
+ }
141
+
142
+ this.rpcClient.receive(data);
143
+ });
144
+ }
145
+
146
+ async put(
147
+ name: string, generation: number, updatingKey: string | null = null, nodeUri: string | null = null,
148
+ signingKey: string | null = null, validFrom: number | null = null, previousDigest: string | null = null,
149
+ signature: string | null = null
150
+ ): Promise<string> {
151
+ return await this.rpcClient.request(
152
+ "put",
153
+ {name, generation, updatingKey, nodeUri, signingKey, validFrom, previousDigest, signature},
154
+ ["put", "string"]
155
+ );
156
+ }
157
+
158
+ async getStatus(operationId: string): Promise<OperationStatusInfo | null> {
159
+ return await this.rpcClient.request("getStatus", {operationId}, ["getStatus", "OperationStatusInfo"]);
160
+ }
161
+
162
+ async getCurrent(name: string, generation: number): Promise<RegisteredNameInfo> {
163
+ return await this.rpcClient.request("getCurrent", {name, generation}, ["getCurrent", "RegisteredNameInfo"]);
164
+ }
165
+
166
+ async getPast(name: string, generation: number, at: number): Promise<RegisteredNameInfo | null> {
167
+ return await this.rpcClient.request("getPast", {name, generation, at}, ["getPast", "RegisteredNameInfo"]);
168
+ }
169
+
170
+ async isFree(name: string, generation: number): Promise<boolean> {
171
+ return await this.rpcClient.request("isFree", {name, generation}, ["isFree", "boolean"]);
172
+ }
173
+
174
+ async getSimilar(name: string): Promise<RegisteredNameInfo | null> {
175
+ return await this.rpcClient.request("getSimilar", {name}, ["getSimilar", "RegisteredNameInfo"]);
176
+ }
177
+
178
+ async getAllKeys(name: string, generation: number): Promise<SigningKeyInfo[]> {
179
+ return await this.rpcClient.request("getAllKeys", {name, generation}, ["getAllKeys", "SigningKeyInfoArray"]);
180
+ }
181
+
182
+ async getAll(at: number, page: number, size: number): Promise<RegisteredNameInfo[]> {
183
+ return await this.rpcClient.request("getAll", {at, page, size}, ["getAll", "RegisteredNameInfoArray"]);
184
+ }
185
+
186
+ async getAllNewer(at: number, page: number, size: number): Promise<RegisteredNameInfo[]> {
187
+ return await this.rpcClient.request("getAllNewer", {at, page, size}, ["getAllNewer", "RegisteredNameInfoArray"]);
188
+ }
189
+
190
+ }
191
+
192
+ export function parseNodeName(nodeName: string): [string, number] {
193
+ let name = nodeName;
194
+ let generation = 0;
195
+
196
+ const pos = nodeName.lastIndexOf('_');
197
+ if (pos >= 0) {
198
+ const gen = nodeName.substring(pos + 1);
199
+ name = nodeName.substring(0, pos);
200
+ try {
201
+ generation = parseInt(gen, 10);
202
+ } catch (error) {
203
+ throw new Error(`invalid generation: "${gen}"`);
204
+ }
205
+ }
206
+
207
+ return [name, generation];
208
+ }
209
+
210
+ export function shorten(nodeName: string | null): string | null {
211
+ if (nodeName === null) {
212
+ return null;
213
+ }
214
+ const [name, gen] = parseNodeName(nodeName);
215
+ if (gen === 0) {
216
+ return name;
217
+ } else {
218
+ return nodeName;
219
+ }
220
+ }
221
+
222
+ export function expand(nodeName: string | null): string | null {
223
+ if (nodeName === null) {
224
+ return null;
225
+ }
226
+ const [name, gen] = parseNodeName(nodeName);
227
+ return `${name}_${gen}`;
228
+ }
229
+
230
+ export async function resolve(name: string, namingServer: string = MAIN_NAMING_SERVER): Promise<string | null> {
231
+ const [parsedName, gen] = parseNodeName(name);
232
+ const naming = new MoeraNaming(namingServer);
233
+ return (await naming.getCurrent(parsedName, gen))?.nodeUri ?? null;
234
+ }
@@ -0,0 +1,194 @@
1
+ // This file is for schema compiler only, do not use directly
2
+
3
+ export const NAMING_API_SCHEMAS = {
4
+ $id: "naming",
5
+ definitions: {
6
+ ObjectResult: {
7
+ type: "object",
8
+ properties: {
9
+ "jsonrpc": {
10
+ type: "string"
11
+ },
12
+ "result": {
13
+ type: "object",
14
+ nullable: true
15
+ },
16
+ "id": {
17
+ type: "integer"
18
+ }
19
+ },
20
+ additionalProperties: false,
21
+ required: ["jsonrpc", "id"]
22
+ },
23
+
24
+ BooleanResult: {
25
+ type: "object",
26
+ properties: {
27
+ "jsonrpc": {
28
+ type: "string"
29
+ },
30
+ "result": {
31
+ type: "boolean",
32
+ nullable: true
33
+ },
34
+ "id": {
35
+ type: "integer"
36
+ }
37
+ },
38
+ additionalProperties: false,
39
+ required: ["jsonrpc", "id"]
40
+ },
41
+
42
+ StringResult: {
43
+ type: "object",
44
+ properties: {
45
+ "jsonrpc": {
46
+ type: "string"
47
+ },
48
+ "result": {
49
+ type: "string",
50
+ nullable: true
51
+ },
52
+ "id": {
53
+ type: "integer"
54
+ }
55
+ },
56
+ additionalProperties: false,
57
+ required: ["jsonrpc", "id"]
58
+ },
59
+
60
+ ErrorResult: {
61
+ type: "object",
62
+ properties: {
63
+ "jsonrpc": {
64
+ type: "string"
65
+ },
66
+ "error": {
67
+ type: "object",
68
+ properties: {
69
+ "code": {
70
+ type: "integer"
71
+ },
72
+ "message": {
73
+ type: "string"
74
+ }
75
+ },
76
+ additionalProperties: false,
77
+ required: ["code", "message"]
78
+ },
79
+ "id": {
80
+ type: "integer"
81
+ }
82
+ },
83
+ additionalProperties: false,
84
+ required: ["jsonrpc", "error", "id"]
85
+ },
86
+
87
+ OperationStatus: {
88
+ type: "string",
89
+ enum: ["WAITING", "ADDED", "STARTED", "SUCCEEDED", "FAILED", "UNKNOWN"]
90
+ },
91
+
92
+ OperationStatusInfo: {
93
+ type: "object",
94
+ properties: {
95
+ "operationId": {
96
+ type: "string"
97
+ },
98
+ "name": {
99
+ type: "string"
100
+ },
101
+ "generation": {
102
+ type: "number"
103
+ },
104
+ "status": {
105
+ $ref: "naming#/definitions/OperationStatus"
106
+ },
107
+ "added": {
108
+ type: "integer",
109
+ nullable: true
110
+ },
111
+ "completed": {
112
+ type: "integer",
113
+ nullable: true
114
+ },
115
+ "errorCode": {
116
+ type: "string",
117
+ nullable: true
118
+ },
119
+ "errorMessage": {
120
+ type: "string",
121
+ nullable: true
122
+ }
123
+ },
124
+ additionalProperties: false,
125
+ required: ["operationId", "name", "generation", "status"]
126
+ },
127
+
128
+ RegisteredNameInfo: {
129
+ type: "object",
130
+ properties: {
131
+ "name": {
132
+ type: "string"
133
+ },
134
+ "generation": {
135
+ type: "integer",
136
+ minimum: 0
137
+ },
138
+ "updatingKey": {
139
+ type: "string",
140
+ nullable: true
141
+ },
142
+ "nodeUri": {
143
+ type: "string"
144
+ },
145
+ "created": {
146
+ type: "integer",
147
+ nullable: true
148
+ },
149
+ "signingKey": {
150
+ type: "string",
151
+ nullable: true
152
+ },
153
+ "validFrom": {
154
+ type: "integer",
155
+ nullable: true
156
+ },
157
+ "digest": {
158
+ type: "string",
159
+ nullable: true
160
+ }
161
+ },
162
+ additionalProperties: false,
163
+ required: ["name", "generation", "nodeUri"]
164
+ },
165
+
166
+ RegisteredNameInfoArray: {
167
+ type: "array",
168
+ items: {
169
+ $ref: "naming#/definitions/RegisteredNameInfo"
170
+ }
171
+ },
172
+
173
+ SigningKeyInfo: {
174
+ type: "object",
175
+ properties: {
176
+ key: {
177
+ "type": "string"
178
+ },
179
+ validFrom: {
180
+ "type": "number"
181
+ }
182
+ },
183
+ additionalProperties: false,
184
+ required: ["key", "validFrom"]
185
+ },
186
+
187
+ SigningKeyInfoArray: {
188
+ type: "array",
189
+ items: {
190
+ $ref: "naming#/definitions/SigningKeyInfo"
191
+ }
192
+ }
193
+ }
194
+ }
@@ -0,0 +1,39 @@
1
+ import { JSONRPCErrorResponse, JSONRPCSuccessResponse } from "json-rpc-2.0";
2
+
3
+ interface Result<T> extends JSONRPCSuccessResponse {
4
+ result: T | null;
5
+ }
6
+
7
+ export type ObjectResult = Result<object>;
8
+ export type BooleanResult = Result<boolean>;
9
+ export type StringResult = Result<string>;
10
+ export type ErrorResult = JSONRPCErrorResponse;
11
+
12
+ export type OperationStatus = "WAITING" | "ADDED" | "STARTED" | "SUCCEEDED" | "FAILED" | "UNKNOWN";
13
+
14
+ export interface OperationStatusInfo {
15
+ operationId: string;
16
+ name: string;
17
+ generation: number;
18
+ status: OperationStatus;
19
+ added?: number | null;
20
+ completed?: number | null;
21
+ errorCode?: string | null;
22
+ errorMessage?: string | null;
23
+ }
24
+
25
+ export interface RegisteredNameInfo {
26
+ name: string;
27
+ generation: number;
28
+ updatingKey?: string | null;
29
+ nodeUri: string;
30
+ created?: number | null;
31
+ signingKey?: string | null;
32
+ validFrom?: number | null;
33
+ digest?: string | null;
34
+ }
35
+
36
+ export interface SigningKeyInfo {
37
+ key: string;
38
+ validFrom: number;
39
+ }
@@ -0,0 +1,6 @@
1
+ import { validateSchemaFromMap, ValidationResult } from "../schema";
2
+ import { NAMING_API_VALIDATORS } from "./validators";
3
+
4
+ export function validateSchema(schemaName: string, data: any): ValidationResult {
5
+ return validateSchemaFromMap(NAMING_API_VALIDATORS, schemaName, data);
6
+ }
@@ -0,0 +1,3 @@
1
+ import { ValidatorsMap } from "../schema";
2
+
3
+ declare const NAMING_API_VALIDATORS: ValidatorsMap;