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
package/lib/schema.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateSchemaFromMap = exports.formatSchemaErrors = exports.isSchemaValid = void 0;
4
+ function isSchemaValid(schema, data) {
5
+ return schema(data);
6
+ }
7
+ exports.isSchemaValid = isSchemaValid;
8
+ function formatSchemaErrors(errors) {
9
+ return errors != null ? errors.map(({ message }) => message).join(", ") : "";
10
+ }
11
+ exports.formatSchemaErrors = formatSchemaErrors;
12
+ function validateSchemaFromMap(validators, schemaName, data) {
13
+ const schema = validators[schemaName];
14
+ if (schema == null) {
15
+ return { valid: false, errors: [{ message: `Schema ${schemaName} is not found` }] };
16
+ }
17
+ const valid = isSchemaValid(schema, data);
18
+ return { valid, errors: schema.errors };
19
+ }
20
+ exports.validateSchemaFromMap = validateSchemaFromMap;
@@ -0,0 +1,42 @@
1
+ import { writeFileSync } from 'fs';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import Ajv from 'ajv';
5
+ import standaloneCode from 'ajv/dist/standalone/index.js';
6
+ import { NAMING_API_SCHEMAS } from "./naming/schemas.mjs";
7
+ import { NODE_API_SCHEMAS } from "./node/schemas.mjs";
8
+ const apiDir = path.dirname(fileURLToPath(import.meta.url));
9
+ function compileNamingApiSchemas() {
10
+ const ajv = new Ajv({
11
+ schemas: [NAMING_API_SCHEMAS],
12
+ code: { source: true, esm: true, lines: true },
13
+ removeAdditional: true,
14
+ useDefaults: true,
15
+ coerceTypes: "array"
16
+ });
17
+ const idMapping = Object.fromEntries(Object.keys(NAMING_API_SCHEMAS.definitions).map(k => [k, `naming#/definitions/${k}`]));
18
+ let moduleCode = '// This file is generated\n\n';
19
+ moduleCode += standaloneCode(ajv, idMapping);
20
+ moduleCode += '\nexport const NAMING_API_VALIDATORS = {\n';
21
+ Object.keys(NAMING_API_SCHEMAS.definitions).forEach(k => moduleCode += ` "${k}": ${k},\n`);
22
+ moduleCode += '};\n';
23
+ writeFileSync(path.resolve(apiDir, "naming/validators.js"), moduleCode);
24
+ }
25
+ function compileNodeApiSchemas() {
26
+ const ajv = new Ajv({
27
+ schemas: [NODE_API_SCHEMAS],
28
+ code: { source: true, esm: true, lines: true },
29
+ removeAdditional: true,
30
+ useDefaults: true,
31
+ coerceTypes: "array"
32
+ });
33
+ const idMapping = Object.fromEntries(Object.keys(NODE_API_SCHEMAS.definitions).map(k => [k, `node#/definitions/${k}`]));
34
+ let moduleCode = '// This file is generated\n\n';
35
+ moduleCode += standaloneCode(ajv, idMapping);
36
+ moduleCode += '\nexport const NODE_API_VALIDATORS = {\n';
37
+ Object.keys(NODE_API_SCHEMAS.definitions).forEach(k => moduleCode += ` "${k}": ${k},\n`);
38
+ moduleCode += '};\n';
39
+ writeFileSync(path.resolve(apiDir, "node/validators.js"), moduleCode);
40
+ }
41
+ compileNamingApiSchemas();
42
+ compileNodeApiSchemas();
@@ -0,0 +1,164 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.redirectTo = exports.redirectToUrl = exports.parse = exports.REDIRECTOR = void 0;
4
+ const url_1 = require("url");
5
+ const naming_1 = require("./naming");
6
+ exports.REDIRECTOR = "moera.page";
7
+ class UniversalLocation {
8
+ constructor(nodeName = null, scheme = null, authority = null, path = null, query = null, fragment = null) {
9
+ this._nodeName = null;
10
+ this._scheme = "https";
11
+ this._authority = null;
12
+ this._path = null;
13
+ this.nodeName = nodeName;
14
+ this.scheme = scheme;
15
+ this.authority = authority;
16
+ this.path = path;
17
+ this.query = query;
18
+ this.fragment = fragment;
19
+ }
20
+ get nodeName() {
21
+ return this._nodeName;
22
+ }
23
+ set nodeName(nodeName) {
24
+ this._nodeName = (0, naming_1.shorten)(nodeName);
25
+ }
26
+ get scheme() {
27
+ return this._scheme;
28
+ }
29
+ set scheme(scheme) {
30
+ this._scheme = scheme || "https";
31
+ }
32
+ get authority() {
33
+ return this._authority;
34
+ }
35
+ set authority(authority) {
36
+ this._authority = authority || null;
37
+ }
38
+ get path() {
39
+ return this._path;
40
+ }
41
+ set path(path) {
42
+ if (path === null || path === void 0 ? void 0 : path.startsWith("/moera")) {
43
+ path = path.substring(6);
44
+ }
45
+ this._path = path || "/";
46
+ }
47
+ get location() {
48
+ let loc = "/@";
49
+ if (this._nodeName != null) {
50
+ loc += encodeURIComponent(this._nodeName);
51
+ }
52
+ loc += "/";
53
+ if (this._authority != null) {
54
+ if (this._scheme != null && this._scheme.toLowerCase() !== "https") {
55
+ loc += this._scheme + ":";
56
+ }
57
+ loc += this._authority;
58
+ }
59
+ else {
60
+ loc += "~";
61
+ }
62
+ loc += this._path;
63
+ return loc;
64
+ }
65
+ toString() {
66
+ let url = this.location;
67
+ if (this.query != null) {
68
+ url += "?" + this.query;
69
+ }
70
+ if (this.fragment != null) {
71
+ url += "#" + this.fragment;
72
+ }
73
+ return url;
74
+ }
75
+ }
76
+ function stripFirst(s) {
77
+ return s != null && s.length > 1 ? s.substring(1) : null;
78
+ }
79
+ function parse(url) {
80
+ if (url == null) {
81
+ return new UniversalLocation();
82
+ }
83
+ const parts = new url_1.URL(url);
84
+ let path = parts.pathname.replace(/^\/|\/$/g, "");
85
+ if (path === "") {
86
+ return new UniversalLocation();
87
+ }
88
+ const dirs = path.split("/");
89
+ if (!dirs[0].startsWith('@')) {
90
+ return new UniversalLocation();
91
+ }
92
+ let nodeName = null;
93
+ if (dirs[0].length > 1) {
94
+ nodeName = decodeURIComponent(dirs[0].substring(1));
95
+ }
96
+ let scheme = null;
97
+ let authority = null;
98
+ let host = null;
99
+ let port = null;
100
+ if (dirs.length > 1 && dirs[1] !== "~") {
101
+ const parts = dirs[1].split(":");
102
+ let i = 0;
103
+ if (!parts[i].includes(".")) {
104
+ scheme = parts[i];
105
+ i++;
106
+ }
107
+ if (i < parts.length) {
108
+ host = parts[i];
109
+ i++;
110
+ }
111
+ if (i < parts.length) {
112
+ port = parts[i];
113
+ }
114
+ }
115
+ if (host) {
116
+ authority = host;
117
+ if (port) {
118
+ authority += ":" + port;
119
+ }
120
+ }
121
+ path = "";
122
+ dirs.slice(2).forEach(dir => path += `/${dir}`);
123
+ if (path === "") {
124
+ path = "/";
125
+ }
126
+ const query = stripFirst(parts.search);
127
+ const fragment = stripFirst(parts.hash);
128
+ return new UniversalLocation(nodeName, scheme, authority, path, query, fragment);
129
+ }
130
+ exports.parse = parse;
131
+ function redirectToUrl(nodeName, url = null) {
132
+ try {
133
+ let uni;
134
+ if (url != null) {
135
+ const parts = new url_1.URL(url);
136
+ uni = new UniversalLocation(nodeName, parts.protocol.slice(0, -1), parts.host, parts.pathname, stripFirst(parts.search), stripFirst(parts.hash));
137
+ }
138
+ else {
139
+ uni = new UniversalLocation(nodeName);
140
+ }
141
+ return `https://${exports.REDIRECTOR}${uni}`;
142
+ }
143
+ catch (e) {
144
+ return `https://${exports.REDIRECTOR}`;
145
+ }
146
+ }
147
+ exports.redirectToUrl = redirectToUrl;
148
+ function redirectTo(nodeName, rootUrl, path = null, query = null, fragment = null) {
149
+ try {
150
+ let uni;
151
+ if (rootUrl != null) {
152
+ const parts = new url_1.URL(rootUrl);
153
+ uni = new UniversalLocation(nodeName, parts.protocol.slice(0, -1), parts.host, path, query, fragment);
154
+ }
155
+ else {
156
+ uni = new UniversalLocation(nodeName, null, null, path, query, fragment);
157
+ }
158
+ return `https://${exports.REDIRECTOR}${uni}`;
159
+ }
160
+ catch (error) {
161
+ return `https://${exports.REDIRECTOR}`;
162
+ }
163
+ }
164
+ exports.redirectTo = redirectTo;
package/lib/util.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.commaSeparatedFlags = exports.ut = exports.urlWithParameters = void 0;
4
+ function urlWithParameters(url, parameters) {
5
+ if (parameters == null) {
6
+ return url;
7
+ }
8
+ let query = "";
9
+ for (let name in parameters) {
10
+ if (parameters.hasOwnProperty(name)) {
11
+ const value = parameters[name];
12
+ if (value != null) {
13
+ query += (query === "" ? "" : "&") + name + "=" + encodeURIComponent(value);
14
+ }
15
+ }
16
+ }
17
+ if (query === "") {
18
+ return url;
19
+ }
20
+ return url + (url.indexOf("?") < 0 ? "?" : "&") + query;
21
+ }
22
+ exports.urlWithParameters = urlWithParameters;
23
+ function ut(strings, ...args) {
24
+ const all = [];
25
+ let i = 0;
26
+ while (i < strings.length || i < args.length) {
27
+ if (i < strings.length) {
28
+ all.push(strings[i]);
29
+ }
30
+ if (i < args.length) {
31
+ all.push(encodeURIComponent(args[i]));
32
+ }
33
+ i++;
34
+ }
35
+ return all.join("");
36
+ }
37
+ exports.ut = ut;
38
+ function commaSeparatedFlags(flags) {
39
+ const names = Object.entries(flags).filter(([_, value]) => value).map(([name]) => name);
40
+ return names.length > 0 ? names.join(',') : null;
41
+ }
42
+ exports.commaSeparatedFlags = commaSeparatedFlags;
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+
3
+ . .venv/bin/activate
4
+ python3 nodejsmoeraapi.py "$@"