@voxgig/system 0.0.1

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/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # System
2
+
@@ -0,0 +1,6 @@
1
+ interface Msg {
2
+ props: Record<string, any>;
3
+ pattern: string;
4
+ meta: any;
5
+ }
6
+ export type { Msg };
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ /* Copyright © 2022 Voxgig Ltd, MIT License. */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../lib/types.ts"],"names":[],"mappings":";AAAA,+CAA+C"}
@@ -0,0 +1,8 @@
1
+ import type { Msg } from './types';
2
+ declare function srvmsgs(srv: Record<string, any>, model: Record<string, any>): Msg[];
3
+ declare function listmsgs(point?: Record<string, any>): Msg[];
4
+ declare const Utility: {
5
+ srvmsgs: typeof srvmsgs;
6
+ listmsgs: typeof listmsgs;
7
+ };
8
+ export { Utility };
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ /* Copyright © 2022 Voxgig Ltd, MIT License. */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Utility = void 0;
5
+ const Seneca = require('seneca');
6
+ // TODO: perform this during model build?
7
+ function srvmsgs(srv, model) {
8
+ const allmsgs = listmsgs(model.main.msg);
9
+ const allpat = Seneca.util.Patrun();
10
+ allmsgs.forEach((msg) => allpat.add(msg.props, msg));
11
+ // TODO: need an option to listmsgs to just list patterns
12
+ const srvpats = listmsgs(srv.in).map(m => m.props);
13
+ const srvmsgs = [];
14
+ srvpats.reduce((a, pat) => (a.push(...allpat
15
+ .list(pat)
16
+ .map((o) => o.data)), a), srvmsgs);
17
+ return srvmsgs;
18
+ }
19
+ function listmsgs(point) {
20
+ if (null == point)
21
+ return [];
22
+ let msgs = [];
23
+ walkmsgs(point, [], (path, meta) => {
24
+ let msg = {
25
+ pattern: path.map((part) => part[0] + ':' + part[1]).join(','),
26
+ props: path.reduce((a, part) => (a[part[0]] = part[1], a), {}),
27
+ meta,
28
+ };
29
+ msgs.push(msg);
30
+ });
31
+ return msgs;
32
+ }
33
+ function walkmsgs(point, path, handle) {
34
+ // console.log('WM', point, path)
35
+ let points = Object.entries(point).filter(entry => !entry[0].includes('$'));
36
+ for (let step of points) {
37
+ let key = step[0];
38
+ // TODO: capture error log if step[1] empty (key with no vals)
39
+ for (let val of Object.keys(step[1])) {
40
+ walkmsgs(step[1][val], path.concat([[key, val]]), handle);
41
+ }
42
+ }
43
+ // if any $ meta props, or no points, we found a msg
44
+ if (0 === points.length) {
45
+ const meta = point.$ || {};
46
+ handle(path, meta);
47
+ }
48
+ }
49
+ const Utility = {
50
+ srvmsgs,
51
+ listmsgs,
52
+ };
53
+ exports.Utility = Utility;
54
+ //# sourceMappingURL=utility.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utility.js","sourceRoot":"","sources":["../../lib/utility.ts"],"names":[],"mappings":";AAAA,+CAA+C;;;AAQ/C,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;AAGhC,yCAAyC;AACzC,SAAS,OAAO,CAAC,GAAwB,EAAE,KAA0B;IACnE,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IAExC,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,CAAA;IACnC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAQ,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAA;IAEzD,yDAAyD;IACzD,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;IAElD,MAAM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,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAA;IAC3E,KAAK,IAAI,IAAI,IAAI,MAAM,EAAE;QACvB,IAAI,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;QACjB,8DAA8D;QAC9D,KAAK,IAAI,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE;YACpC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAA;SAC1D;KACF;IAED,oDAAoD;IACpD,IAAI,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE;QACvB,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,IAAI,EAAE,CAAA;QAC1B,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;KACnB;AACH,CAAC;AAID,MAAM,OAAO,GAAG;IACd,OAAO;IACP,QAAQ;CACT,CAAA;AAIC,0BAAO"}
@@ -0,0 +1,2 @@
1
+ declare function MakeSrv(name: string, require: any): (this: any, options: any) => void;
2
+ export { MakeSrv };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MakeSrv = void 0;
4
+ const system_1 = require("../system");
5
+ function MakeSrv(name, require) {
6
+ const srv = function (options) {
7
+ let reload = this.export('reload/make')(require);
8
+ system_1.System.messages(this, options, reload);
9
+ system_1.System.prepare(this, require);
10
+ };
11
+ Object.defineProperty(srv, 'name', { value: 'srv_' + name });
12
+ return srv;
13
+ }
14
+ exports.MakeSrv = MakeSrv;
15
+ //# sourceMappingURL=make.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"make.js","sourceRoot":"","sources":["../../srv/make.ts"],"names":[],"mappings":";;;AACA,sCAAkC;AAElC,SAAS,OAAO,CAAC,IAAY,EAAE,OAAY;IACzC,MAAM,GAAG,GAAG,UAAoB,OAAY;QAC1C,IAAI,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,CAAA;QAChD,eAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;QACtC,eAAM,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IAC/B,CAAC,CAAA;IAED,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,CAAC,CAAA;IAE5D,OAAO,GAAG,CAAA;AACZ,CAAC;AAGC,0BAAO"}
@@ -0,0 +1,2 @@
1
+ declare function make(name: string, require: any): (this: any, options: any) => void;
2
+ export { make };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.make = void 0;
4
+ const system_1 = require("../system");
5
+ function make(name, require) {
6
+ const srv = function (options) {
7
+ let reload = this.export('reload/make')(require);
8
+ system_1.System.messages(this, options, reload, require);
9
+ };
10
+ Object.defineProperty(srv, 'name', { value: name });
11
+ return srv;
12
+ }
13
+ exports.make = make;
14
+ //# sourceMappingURL=srv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"srv.js","sourceRoot":"","sources":["../../srv/srv.ts"],"names":[],"mappings":";;;AACA,sCAAkC;AAElC,SAAS,IAAI,CAAC,IAAY,EAAE,OAAY;IACtC,MAAM,GAAG,GAAG,UAAoB,OAAY;QAC1C,IAAI,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,CAAA;QAChD,eAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAA;IACjD,CAAC,CAAA;IAED,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;IACnD,OAAO,GAAG,CAAA;AACZ,CAAC;AAGC,oBAAI"}
@@ -0,0 +1,12 @@
1
+ import type { Msg } from './lib/types';
2
+ import { Utility } from './lib/utility';
3
+ import { MakeSrv } from './srv/make';
4
+ declare function messages(seneca: any, options: any, reload: any): void;
5
+ declare function prepare(seneca: any, require: any): void;
6
+ declare function Local(this: any, options: any): void;
7
+ declare const System: {
8
+ messages: typeof messages;
9
+ prepare: typeof prepare;
10
+ };
11
+ export type { Msg };
12
+ export { System, MakeSrv, Utility, Local, };
package/dist/system.js ADDED
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ /* Copyright © 2022 Voxgig Ltd, MIT License. */
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.Local = exports.Utility = exports.MakeSrv = exports.System = void 0;
8
+ const path_1 = __importDefault(require("path"));
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const utility_1 = require("./lib/utility");
11
+ Object.defineProperty(exports, "Utility", { enumerable: true, get: function () { return utility_1.Utility; } });
12
+ const make_1 = require("./srv/make");
13
+ Object.defineProperty(exports, "MakeSrv", { enumerable: true, get: function () { return make_1.MakeSrv; } });
14
+ const { srvmsgs } = utility_1.Utility;
15
+ function messages(seneca, options, reload) {
16
+ let srvname = seneca.fixedargs.plugin$.name.replace(/^srv_/, '');
17
+ let model = seneca.context.model;
18
+ let srvmodel = model.main.srv[srvname];
19
+ let msgs = srvmsgs(srvmodel, model);
20
+ for (let msg of msgs) {
21
+ seneca.message(msg.pattern, reload(actpath(msg), { options }));
22
+ }
23
+ }
24
+ function actpath(msg) {
25
+ if (msg.meta.file) {
26
+ return msg.meta.file;
27
+ }
28
+ let pairs = msg.pattern.split(/\s*,\s*/);
29
+ // TODO: maybe take more than just the last one!
30
+ let path = './' + pairs[pairs.length - 1].replace(/:/g, '_');
31
+ return path;
32
+ }
33
+ function prepare(seneca, require) {
34
+ let srvname = seneca.fixedargs.plugin$.name.replace(/^srv_/, '');
35
+ try {
36
+ const makePrepare = require('./' + srvname + '-prepare.js');
37
+ seneca.prepare(makePrepare());
38
+ }
39
+ catch (e) {
40
+ if ('MODULE_NOT_FOUND' !== e.code) {
41
+ throw e;
42
+ }
43
+ }
44
+ }
45
+ function Local(options) {
46
+ const model = this.context.model;
47
+ const folder = options.srv.folder;
48
+ for (const entry of Object.entries(model.main.srv)) {
49
+ let name = entry[0];
50
+ let srv = entry[1];
51
+ let srvpath = path_1.default.join(folder, name, name + '-srv.js');
52
+ if (fs_1.default.existsSync(srvpath)) {
53
+ this.root.use(srvpath, srv.options);
54
+ }
55
+ else {
56
+ this.log.warn('srv-not-found', { name, srvpath: srvpath });
57
+ }
58
+ }
59
+ }
60
+ exports.Local = Local;
61
+ const System = {
62
+ messages,
63
+ prepare,
64
+ };
65
+ exports.System = System;
66
+ //# sourceMappingURL=system.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"system.js","sourceRoot":"","sources":["../system.ts"],"names":[],"mappings":";AAAA,+CAA+C;;;;;;AAG/C,gDAAuB;AACvB,4CAAmB;AAOnB,2CAAuC;AAsFrC,wFAtFO,iBAAO,OAsFP;AApFT,qCAAoC;AAmFlC,wFAnFO,cAAO,OAmFP;AAhFT,MAAM,EAAE,OAAO,EAAE,GAAG,iBAAO,CAAA;AAG3B,SAAS,QAAQ,CAAC,MAAW,EAAE,OAAY,EAAE,MAAW;IACtD,IAAI,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;IAEhE,IAAI,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAA;IAChC,IAAI,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;IAEtC,I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AK,MAAM,KAAK,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QAClD,IAAI,IAAI,GAAW,KAAK,CAAC,CAAC,CAAC,CAAA;QAC3B,IAAI,GAAG,GAAQ,KAAK,CAAC,CAAC,CAAC,CAAA;QAEvB,IAAI,OAAO,GAAG,cAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC,CAAA;QAEvD,IAAI,YAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;YAC1B,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,CAAC,CAAA;SACpC;aACI;YACH,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAA;SAC3D;KACF;AAEH,CAAC;AAkBC,sBAAK;AAdP,MAAM,MAAM,GAAG;IACb,QAAQ;IACR,OAAO;CACR,CAAA;AAQC,wBAAM"}
package/lib/types.ts ADDED
@@ -0,0 +1,15 @@
1
+ /* Copyright © 2022 Voxgig Ltd, MIT License. */
2
+
3
+
4
+ interface Msg {
5
+ props: Record<string, any>
6
+ pattern: string
7
+ meta: any
8
+ }
9
+
10
+
11
+
12
+ export type {
13
+ Msg
14
+ }
15
+
package/lib/utility.ts ADDED
@@ -0,0 +1,86 @@
1
+ /* Copyright © 2022 Voxgig Ltd, MIT License. */
2
+
3
+
4
+ import type {
5
+ Msg
6
+ } from './types'
7
+
8
+
9
+ const Seneca = require('seneca')
10
+
11
+
12
+ // TODO: perform this during model build?
13
+ function srvmsgs(srv: Record<string, any>, model: Record<string, any>): Msg[] {
14
+ const allmsgs = listmsgs(model.main.msg)
15
+
16
+ const allpat = Seneca.util.Patrun()
17
+ allmsgs.forEach((msg: Msg) => allpat.add(msg.props, msg))
18
+
19
+ // TODO: need an option to listmsgs to just list patterns
20
+ const srvpats = listmsgs(srv.in).map(m => m.props)
21
+
22
+ const srvmsgs: Msg[] = []
23
+ srvpats.reduce((a, pat) =>
24
+ (a.push(...(allpat
25
+ .list(pat)
26
+ .map((o: any) => o.data) as Msg[])), a), srvmsgs)
27
+
28
+ return srvmsgs
29
+ }
30
+
31
+
32
+ function listmsgs(point?: Record<string, any>): Msg[] {
33
+ if (null == point) return []
34
+ let msgs: Msg[] = []
35
+
36
+ walkmsgs(point, [], (path: string[][], meta: any) => {
37
+ let msg = {
38
+ pattern:
39
+ path.map((part: string[]) => part[0] + ':' + part[1]).join(','),
40
+ props:
41
+ path.reduce((a: any, part: string[]) => (a[part[0]] = part[1], a), {}),
42
+ meta,
43
+ }
44
+ msgs.push(msg)
45
+ })
46
+
47
+ return msgs
48
+ }
49
+
50
+
51
+ function walkmsgs(
52
+ point: Record<string, any>,
53
+ path: string[][],
54
+ handle: (path: string[][], meta: any) => void) {
55
+
56
+ // console.log('WM', point, path)
57
+
58
+ let points = Object.entries(point).filter(entry => !entry[0].includes('$'))
59
+ for (let step of points) {
60
+ let key = step[0]
61
+ // TODO: capture error log if step[1] empty (key with no vals)
62
+ for (let val of Object.keys(step[1])) {
63
+ walkmsgs(step[1][val], path.concat([[key, val]]), handle)
64
+ }
65
+ }
66
+
67
+ // if any $ meta props, or no points, we found a msg
68
+ if (0 === points.length) {
69
+ const meta = point.$ || {}
70
+ handle(path, meta)
71
+ }
72
+ }
73
+
74
+
75
+
76
+ const Utility = {
77
+ srvmsgs,
78
+ listmsgs,
79
+ }
80
+
81
+
82
+ export {
83
+ Utility
84
+ }
85
+
86
+
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@voxgig/system",
3
+ "version": "0.0.1",
4
+ "main": "dist/system.js",
5
+ "type": "commonjs",
6
+ "types": "dist/system.d.ts",
7
+ "description": "Voxgig System.",
8
+ "homepage": "https://github.com/voxgig/system",
9
+ "keywords": [
10
+ "system"
11
+ ],
12
+ "author": "Richard Rodger (http://richardrodger.com)",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git://github.com/voxgig/system.git"
16
+ },
17
+ "scripts": {
18
+ "test": "jest --coverage",
19
+ "test-some": "jest -t",
20
+ "test-watch": "jest --coverage --watchAll",
21
+ "watch": "tsc -w -d",
22
+ "build": "tsc -d",
23
+ "clean": "rm -rf node_modules yarn.lock package-lock.json",
24
+ "reset": "npm run clean && npm i && npm run build && npm test",
25
+ "repo-tag": "REPO_VERSION=`node -e \"console.log(require('./package').version)\"` && echo TAG: v$REPO_VERSION && git commit -a -m v$REPO_VERSION && git push && git tag v$REPO_VERSION && git push --tags;",
26
+ "repo-publish": "npm run clean && npm i && npm run repo-publish-quick",
27
+ "repo-publish-quick": "npm run build && npm run test && npm run repo-tag && npm publish --registry https://registry.npmjs.org --access=public"
28
+ },
29
+ "license": "MIT",
30
+ "files": [
31
+ "system.ts",
32
+ "lib",
33
+ "srv",
34
+ "dist",
35
+ "LICENSE"
36
+ ],
37
+ "devDependencies": {
38
+ "@types/jest": "^27.5.1",
39
+ "esbuild": "^0.14.39",
40
+ "esbuild-jest": "^0.5.0",
41
+ "jest": "^28.1.0",
42
+ "seneca": "^3.27.1",
43
+ "tinyify": "^3.0.0",
44
+ "ts-jest": "^28.0.2",
45
+ "typescript": "^4.6.4"
46
+ }
47
+ }
package/srv/make.ts ADDED
@@ -0,0 +1,18 @@
1
+
2
+ import { System } from '../system'
3
+
4
+ function MakeSrv(name: string, require: any) {
5
+ const srv = function(this: any, options: any) {
6
+ let reload = this.export('reload/make')(require)
7
+ System.messages(this, options, reload)
8
+ System.prepare(this, require)
9
+ }
10
+
11
+ Object.defineProperty(srv, 'name', { value: 'srv_' + name })
12
+
13
+ return srv
14
+ }
15
+
16
+ export {
17
+ MakeSrv
18
+ }
package/system.ts ADDED
@@ -0,0 +1,100 @@
1
+ /* Copyright © 2022 Voxgig Ltd, MIT License. */
2
+
3
+
4
+ import Path from 'path'
5
+ import Fs from 'fs'
6
+
7
+
8
+ import type {
9
+ Msg,
10
+ } from './lib/types'
11
+
12
+ import { Utility } from './lib/utility'
13
+
14
+ import { MakeSrv } from './srv/make'
15
+
16
+
17
+ const { srvmsgs } = Utility
18
+
19
+
20
+ function messages(seneca: any, options: any, reload: any) {
21
+ let srvname = seneca.fixedargs.plugin$.name.replace(/^srv_/, '')
22
+
23
+ let model = seneca.context.model
24
+ let srvmodel = model.main.srv[srvname]
25
+
26
+ let msgs = srvmsgs(srvmodel, model)
27
+
28
+ for (let msg of msgs) {
29
+ seneca.message(msg.pattern, reload(actpath(msg), { options }))
30
+ }
31
+ }
32
+
33
+
34
+ function actpath(msg: Msg) {
35
+ if (msg.meta.file) {
36
+ return msg.meta.file
37
+ }
38
+
39
+ let pairs = msg.pattern.split(/\s*,\s*/)
40
+
41
+ // TODO: maybe take more than just the last one!
42
+ let path = './' + pairs[pairs.length - 1].replace(/:/g, '_')
43
+ return path
44
+ }
45
+
46
+
47
+ function prepare(seneca: any, require: any) {
48
+ let srvname = seneca.fixedargs.plugin$.name.replace(/^srv_/, '')
49
+
50
+ try {
51
+ const makePrepare = require('./' + srvname + '-prepare.js')
52
+ seneca.prepare(makePrepare())
53
+ }
54
+ catch (e: any) {
55
+ if ('MODULE_NOT_FOUND' !== e.code) {
56
+ throw e
57
+ }
58
+ }
59
+ }
60
+
61
+
62
+
63
+ function Local(this: any, options: any) {
64
+ const model = this.context.model
65
+ const folder = options.srv.folder
66
+
67
+ for (const entry of Object.entries(model.main.srv)) {
68
+ let name: string = entry[0]
69
+ let srv: any = entry[1]
70
+
71
+ let srvpath = Path.join(folder, name, name + '-srv.js')
72
+
73
+ if (Fs.existsSync(srvpath)) {
74
+ this.root.use(srvpath, srv.options)
75
+ }
76
+ else {
77
+ this.log.warn('srv-not-found', { name, srvpath: srvpath })
78
+ }
79
+ }
80
+
81
+ }
82
+
83
+
84
+
85
+ const System = {
86
+ messages,
87
+ prepare,
88
+ }
89
+
90
+
91
+ export type {
92
+ Msg
93
+ }
94
+
95
+ export {
96
+ System,
97
+ MakeSrv,
98
+ Utility,
99
+ Local,
100
+ }