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.
- package/LICENSE +201 -0
- package/README.md +8 -0
- package/lib/naming/index.js +14 -0
- package/lib/naming/naming.js +203 -0
- package/lib/naming/schemas.mjs +184 -0
- package/lib/naming/types.js +2 -0
- package/lib/naming/validate.js +9 -0
- package/lib/naming/validators.js +2065 -0
- package/lib/node/caller.js +267 -0
- package/lib/node/cartes.js +55 -0
- package/lib/node/index.js +13 -0
- package/lib/node/node.js +1405 -0
- package/lib/node/schemas.mjs +4582 -0
- package/lib/node/types.js +3 -0
- package/lib/node/validate.js +9 -0
- package/lib/node/validators.js +60225 -0
- package/lib/schema.js +20 -0
- package/lib/schemas-compile.mjs +42 -0
- package/lib/universal-location.js +164 -0
- package/lib/util.js +42 -0
- package/nodejs-moera-api/nodejs-moera-api +4 -0
- package/nodejs-moera-api/nodejsmoeraapi.py +578 -0
- package/package.json +65 -0
- package/src/naming/index.ts +12 -0
- package/src/naming/naming.ts +234 -0
- package/src/naming/schemas.mjs +194 -0
- package/src/naming/types.ts +39 -0
- package/src/naming/validate.ts +6 -0
- package/src/naming/validators.d.ts +3 -0
- package/src/naming/validators.js +2084 -0
- package/src/node/caller.ts +311 -0
- package/src/node/cartes.ts +51 -0
- package/src/node/index.ts +3 -0
- package/src/node/node.ts +1285 -0
- package/src/node/schemas.mjs +4715 -0
- package/src/node/types.ts +1544 -0
- package/src/node/validate.ts +6 -0
- package/src/node/validators.d.ts +3 -0
- package/src/node/validators.js +60484 -0
- package/src/schema.ts +30 -0
- package/src/schemas-compile.mjs +51 -0
- package/src/universal-location.ts +212 -0
- package/src/util.ts +42 -0
- package/tsconfig.json +112 -0
- package/typings/naming/index.d.ts +2 -0
- package/typings/naming/index.d.ts.map +1 -0
- package/typings/naming/naming.d.ts +31 -0
- package/typings/naming/naming.d.ts.map +1 -0
- package/typings/naming/schemas.d.mts +271 -0
- package/typings/naming/schemas.d.mts.map +1 -0
- package/typings/naming/types.d.ts +35 -0
- package/typings/naming/types.d.ts.map +1 -0
- package/typings/naming/validate.d.ts +3 -0
- package/typings/naming/validate.d.ts.map +1 -0
- package/typings/naming/validators.d.ts +73 -0
- package/typings/naming/validators.d.ts.map +1 -0
- package/typings/node/caller.d.ts +52 -0
- package/typings/node/caller.d.ts.map +1 -0
- package/typings/node/cartes.d.ts +13 -0
- package/typings/node/cartes.d.ts.map +1 -0
- package/typings/node/index.d.ts +4 -0
- package/typings/node/index.d.ts.map +1 -0
- package/typings/node/node.d.ts +176 -0
- package/typings/node/node.d.ts.map +1 -0
- package/typings/node/schemas.d.mts +6205 -0
- package/typings/node/schemas.d.mts.map +1 -0
- package/typings/node/types.d.ts +1340 -0
- package/typings/node/types.d.ts.map +1 -0
- package/typings/node/validate.d.ts +3 -0
- package/typings/node/validate.d.ts.map +1 -0
- package/typings/node/validators.d.ts +920 -0
- package/typings/node/validators.d.ts.map +1 -0
- package/typings/schema.d.ts +18 -0
- package/typings/schema.d.ts.map +1 -0
- package/typings/schemas-compile.d.mts +2 -0
- package/typings/schemas-compile.d.mts.map +1 -0
- package/typings/universal-location.d.ts +25 -0
- package/typings/universal-location.d.ts.map +1 -0
- package/typings/util.d.ts +6 -0
- package/typings/util.d.ts.map +1 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Caller = exports.moeraRoot = exports.MoeraCallError = exports.MoeraNodeConnectionError = exports.MoeraNodeApiError = exports.MoeraNodeError = void 0;
|
|
16
|
+
const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
|
|
17
|
+
const validate_1 = require("./validate");
|
|
18
|
+
const util_1 = require("../util");
|
|
19
|
+
const schema_1 = require("../schema");
|
|
20
|
+
class MoeraNodeError extends Error {
|
|
21
|
+
constructor(name, message) {
|
|
22
|
+
super(name + ": Node error: " + message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.MoeraNodeError = MoeraNodeError;
|
|
26
|
+
class MoeraNodeApiError extends MoeraNodeError {
|
|
27
|
+
constructor(name, result) {
|
|
28
|
+
super(name, result.message);
|
|
29
|
+
this.errorCode = result.errorCode;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.MoeraNodeApiError = MoeraNodeApiError;
|
|
33
|
+
class MoeraNodeConnectionError extends Error {
|
|
34
|
+
constructor(message) {
|
|
35
|
+
super("Node connection error: " + message);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.MoeraNodeConnectionError = MoeraNodeConnectionError;
|
|
39
|
+
class MoeraCallError extends Error {
|
|
40
|
+
constructor(message) {
|
|
41
|
+
super(message);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.MoeraCallError = MoeraCallError;
|
|
45
|
+
function encodeBody(decoded, format) {
|
|
46
|
+
var _a;
|
|
47
|
+
if (decoded == null) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
if (format === "application") {
|
|
51
|
+
return (_a = decoded.text) !== null && _a !== void 0 ? _a : null;
|
|
52
|
+
}
|
|
53
|
+
return JSON.stringify(decoded);
|
|
54
|
+
}
|
|
55
|
+
function encodeBodies(data) {
|
|
56
|
+
if (Array.isArray(data)) {
|
|
57
|
+
return data.map(item => encodeBodies(item));
|
|
58
|
+
}
|
|
59
|
+
const encoded = (0, lodash_clonedeep_1.default)(data);
|
|
60
|
+
if (data.body != null) {
|
|
61
|
+
encoded.body = encodeBody(data.body, data.bodyFormat);
|
|
62
|
+
}
|
|
63
|
+
if (data.bodyPreview != null) {
|
|
64
|
+
encoded.body = encodeBody(data.bodyPreview, data.bodyFormat);
|
|
65
|
+
}
|
|
66
|
+
if (data.bodySrc != null) {
|
|
67
|
+
encoded.bodySrc = encodeBody(data.bodySrc, data.bodySrcFormat);
|
|
68
|
+
}
|
|
69
|
+
return encoded;
|
|
70
|
+
}
|
|
71
|
+
function decodeBody(name, encoded, format) {
|
|
72
|
+
if (format === "application") {
|
|
73
|
+
return { text: encoded };
|
|
74
|
+
}
|
|
75
|
+
let body;
|
|
76
|
+
try {
|
|
77
|
+
body = JSON.parse(encoded);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
if (e instanceof SyntaxError) {
|
|
81
|
+
throw new MoeraNodeError(name, "Invalid body encoding: " + e.message);
|
|
82
|
+
}
|
|
83
|
+
throw e;
|
|
84
|
+
}
|
|
85
|
+
const { valid, errors } = (0, validate_1.validateSchema)("Body", body);
|
|
86
|
+
if (!valid) {
|
|
87
|
+
throw new MoeraNodeError(name, "Invalid body: " + (0, schema_1.formatSchemaErrors)(errors));
|
|
88
|
+
}
|
|
89
|
+
return body;
|
|
90
|
+
}
|
|
91
|
+
function decodeBodies(name, data) {
|
|
92
|
+
if (Array.isArray(data)) {
|
|
93
|
+
return data.map(item => decodeBodies(name, item));
|
|
94
|
+
}
|
|
95
|
+
const decoded = (0, lodash_clonedeep_1.default)(data);
|
|
96
|
+
if (data.stories != null) {
|
|
97
|
+
decoded.stories = data.stories.map((item) => decodeBodies(name, item));
|
|
98
|
+
}
|
|
99
|
+
if (data.comments != null) {
|
|
100
|
+
decoded.comments = data.comments.map((item) => decodeBodies(name, item));
|
|
101
|
+
}
|
|
102
|
+
if (data.comment != null) {
|
|
103
|
+
decoded.comment = decodeBodies(name, data.comment);
|
|
104
|
+
}
|
|
105
|
+
if (data.posting != null) {
|
|
106
|
+
decoded.posting = decodeBodies(name, data.posting);
|
|
107
|
+
}
|
|
108
|
+
if (data.body != null) {
|
|
109
|
+
decoded.body = decodeBody(name, data.body, data.bodyFormat);
|
|
110
|
+
}
|
|
111
|
+
if (data.bodyPreview != null) {
|
|
112
|
+
decoded.bodyPreview = decodeBody(name, data.bodyPreview, data.bodyFormat);
|
|
113
|
+
}
|
|
114
|
+
if (data.bodySrc != null) {
|
|
115
|
+
decoded.bodySrc = decodeBody(name, data.bodySrc, data.bodySrcFormat);
|
|
116
|
+
}
|
|
117
|
+
return decoded;
|
|
118
|
+
}
|
|
119
|
+
function moeraRoot(url) {
|
|
120
|
+
if (!url.startsWith("http:") && !url.startsWith("https:")) {
|
|
121
|
+
url = "https://" + url;
|
|
122
|
+
}
|
|
123
|
+
if (url.endsWith("/")) {
|
|
124
|
+
url = url.slice(0, -1);
|
|
125
|
+
}
|
|
126
|
+
if (url.endsWith("/api")) {
|
|
127
|
+
url = url.slice(0, -4);
|
|
128
|
+
}
|
|
129
|
+
if (!url.endsWith("/moera")) {
|
|
130
|
+
url += "/moera";
|
|
131
|
+
}
|
|
132
|
+
return url;
|
|
133
|
+
}
|
|
134
|
+
exports.moeraRoot = moeraRoot;
|
|
135
|
+
class Caller {
|
|
136
|
+
constructor() {
|
|
137
|
+
this.root = null;
|
|
138
|
+
this._rootSecret = null;
|
|
139
|
+
this._token = null;
|
|
140
|
+
this._carte = null;
|
|
141
|
+
this._carteSource = null;
|
|
142
|
+
this._authMethod = "none";
|
|
143
|
+
}
|
|
144
|
+
nodeUrl(url) {
|
|
145
|
+
this.root = moeraRoot(url);
|
|
146
|
+
}
|
|
147
|
+
rootSecret(secret) {
|
|
148
|
+
this._rootSecret = secret;
|
|
149
|
+
}
|
|
150
|
+
token(token) {
|
|
151
|
+
this._token = token;
|
|
152
|
+
}
|
|
153
|
+
carte(carte) {
|
|
154
|
+
this._carte = carte;
|
|
155
|
+
}
|
|
156
|
+
carteSource(carteSource) {
|
|
157
|
+
this._carteSource = carteSource;
|
|
158
|
+
}
|
|
159
|
+
authMethod(authMethod) {
|
|
160
|
+
this._authMethod = authMethod;
|
|
161
|
+
}
|
|
162
|
+
noAuth() {
|
|
163
|
+
this.authMethod("none");
|
|
164
|
+
}
|
|
165
|
+
auth() {
|
|
166
|
+
this.authMethod("peer");
|
|
167
|
+
}
|
|
168
|
+
authAdmin() {
|
|
169
|
+
this.authMethod("admin");
|
|
170
|
+
}
|
|
171
|
+
authRootAdmin() {
|
|
172
|
+
this.authMethod("root-admin");
|
|
173
|
+
}
|
|
174
|
+
call(name_1, location_1, _a) {
|
|
175
|
+
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
|
+
let bodyEncoded = null;
|
|
177
|
+
if (body != null) {
|
|
178
|
+
if (srcBodies) {
|
|
179
|
+
body = encodeBodies(body);
|
|
180
|
+
}
|
|
181
|
+
bodyEncoded = JSON.stringify(body);
|
|
182
|
+
}
|
|
183
|
+
const headers = {
|
|
184
|
+
"Accept": "application/json",
|
|
185
|
+
"Content-Type": contentType !== null && contentType !== void 0 ? contentType : "application/json"
|
|
186
|
+
};
|
|
187
|
+
let bearer = null;
|
|
188
|
+
if (auth) {
|
|
189
|
+
switch (this._authMethod) {
|
|
190
|
+
case "peer":
|
|
191
|
+
if (this._carteSource != null) {
|
|
192
|
+
bearer = "carte:" + (yield this._carteSource.getCarte());
|
|
193
|
+
}
|
|
194
|
+
else if (this._carte != null) {
|
|
195
|
+
bearer = "carte:" + this._carte;
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
throw new MoeraCallError("Carte is not set");
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
case "admin":
|
|
202
|
+
if (this._token == null) {
|
|
203
|
+
throw new MoeraCallError("Token is not set");
|
|
204
|
+
}
|
|
205
|
+
bearer = "token:" + this._token;
|
|
206
|
+
break;
|
|
207
|
+
case "root-admin":
|
|
208
|
+
if (this._rootSecret == null) {
|
|
209
|
+
throw new MoeraCallError("Root secret is not set");
|
|
210
|
+
}
|
|
211
|
+
bearer = "secret:" + this._rootSecret;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (bearer != null) {
|
|
216
|
+
headers["Authorization"] = `Bearer ${bearer}`;
|
|
217
|
+
}
|
|
218
|
+
if (this.root == null) {
|
|
219
|
+
throw new MoeraCallError("Node URL is not set");
|
|
220
|
+
}
|
|
221
|
+
let url = (0, util_1.urlWithParameters)(this.root + "/api" + location, params);
|
|
222
|
+
let response;
|
|
223
|
+
try {
|
|
224
|
+
response = yield fetch(url, { method, headers, body: bodyEncoded });
|
|
225
|
+
}
|
|
226
|
+
catch (e) {
|
|
227
|
+
throw new MoeraNodeConnectionError(String(e));
|
|
228
|
+
}
|
|
229
|
+
let data;
|
|
230
|
+
try {
|
|
231
|
+
if (schema === "blob" && response.ok) {
|
|
232
|
+
data = yield response.blob();
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
data = yield response.json();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
catch (e) {
|
|
239
|
+
if (!response.ok) {
|
|
240
|
+
throw new MoeraNodeError(name, "Server returned error status");
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
throw new MoeraNodeError(name, "Server returned empty result");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (!response.ok) {
|
|
247
|
+
const { valid } = (0, validate_1.validateSchema)("Result", data);
|
|
248
|
+
if (!valid) {
|
|
249
|
+
throw new MoeraNodeError(name, "Server returned error status");
|
|
250
|
+
}
|
|
251
|
+
throw new MoeraNodeApiError(name, data);
|
|
252
|
+
}
|
|
253
|
+
if (schema !== "blob") {
|
|
254
|
+
const result = (0, validate_1.validateSchema)(schema, data);
|
|
255
|
+
const { valid, errors } = result;
|
|
256
|
+
if (!valid) {
|
|
257
|
+
throw new MoeraNodeError(name, "Server returned incorrect response" + (0, schema_1.formatSchemaErrors)(errors));
|
|
258
|
+
}
|
|
259
|
+
if (bodies) {
|
|
260
|
+
data = decodeBodies(name, data);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return data;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
exports.Caller = Caller;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MoeraCarteSource = exports.MoeraCartesError = void 0;
|
|
13
|
+
class MoeraCartesError extends Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.MoeraCartesError = MoeraCartesError;
|
|
19
|
+
function isAdminCarte(carte) {
|
|
20
|
+
return carte.permissions == null || carte.permissions.includes("other");
|
|
21
|
+
}
|
|
22
|
+
class MoeraCarteSource {
|
|
23
|
+
constructor(node) {
|
|
24
|
+
this.cartes = [];
|
|
25
|
+
this.node = node;
|
|
26
|
+
}
|
|
27
|
+
renew() {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
this.cartes = (yield this.node.getCartes()).cartes;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
getCarte() {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
for (const renewed of [false, true]) {
|
|
35
|
+
const now = Math.floor(Date.now() / 1000);
|
|
36
|
+
this.cartes = this.cartes.filter(c => c.deadline > now && isAdminCarte(c));
|
|
37
|
+
if (this.cartes.length === 0) {
|
|
38
|
+
if (renewed) {
|
|
39
|
+
throw new MoeraCartesError("Could not obtain a valid carte from the node");
|
|
40
|
+
}
|
|
41
|
+
yield this.renew();
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
for (const c of this.cartes) {
|
|
45
|
+
if (c.beginning <= now) {
|
|
46
|
+
return c.carte;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
throw new MoeraCartesError("Could not obtain a carte valid for now");
|
|
50
|
+
}
|
|
51
|
+
throw new MoeraCartesError("Could not obtain a carte valid for now");
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.MoeraCarteSource = MoeraCarteSource;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MoeraCartesError = exports.MoeraCarteSource = exports.moeraRoot = exports.MoeraNodeConnectionError = exports.MoeraNodeApiError = exports.MoeraNodeError = exports.MoeraNode = void 0;
|
|
4
|
+
var node_1 = require("./node");
|
|
5
|
+
Object.defineProperty(exports, "MoeraNode", { enumerable: true, get: function () { return node_1.MoeraNode; } });
|
|
6
|
+
var caller_1 = require("./caller");
|
|
7
|
+
Object.defineProperty(exports, "MoeraNodeError", { enumerable: true, get: function () { return caller_1.MoeraNodeError; } });
|
|
8
|
+
Object.defineProperty(exports, "MoeraNodeApiError", { enumerable: true, get: function () { return caller_1.MoeraNodeApiError; } });
|
|
9
|
+
Object.defineProperty(exports, "MoeraNodeConnectionError", { enumerable: true, get: function () { return caller_1.MoeraNodeConnectionError; } });
|
|
10
|
+
Object.defineProperty(exports, "moeraRoot", { enumerable: true, get: function () { return caller_1.moeraRoot; } });
|
|
11
|
+
var cartes_1 = require("./cartes");
|
|
12
|
+
Object.defineProperty(exports, "MoeraCarteSource", { enumerable: true, get: function () { return cartes_1.MoeraCarteSource; } });
|
|
13
|
+
Object.defineProperty(exports, "MoeraCartesError", { enumerable: true, get: function () { return cartes_1.MoeraCartesError; } });
|