nesoi 3.1.8 → 3.2.2

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 (74) hide show
  1. package/lib/compiler/apps/distributed/distributed_compiler.d.ts +31 -0
  2. package/lib/compiler/apps/distributed/distributed_compiler.js +121 -0
  3. package/lib/compiler/apps/distributed/stages/1_mkdir_stage.d.ts +15 -0
  4. package/lib/compiler/apps/distributed/stages/1_mkdir_stage.js +96 -0
  5. package/lib/compiler/apps/distributed/stages/2_build_typescript_stage.d.ts +15 -0
  6. package/lib/compiler/apps/distributed/stages/2_build_typescript_stage.js +174 -0
  7. package/lib/compiler/apps/distributed/stages/3_copy_types_stage.d.ts +15 -0
  8. package/lib/compiler/apps/distributed/stages/3_copy_types_stage.js +82 -0
  9. package/lib/compiler/apps/distributed/stages/4_dump_modules_stage.d.ts +16 -0
  10. package/lib/compiler/apps/distributed/stages/4_dump_modules_stage.js +182 -0
  11. package/lib/compiler/apps/distributed/stages/5_copy_static_stage.d.ts +0 -0
  12. package/lib/compiler/apps/distributed/stages/5_copy_static_stage.js +31 -0
  13. package/lib/compiler/apps/distributed/stages/6_dump_cli_stage.d.ts +0 -0
  14. package/lib/compiler/apps/distributed/stages/6_dump_cli_stage.js +34 -0
  15. package/lib/compiler/apps/distributed/stages/7_dump_package_json_stage.d.ts +0 -0
  16. package/lib/compiler/apps/distributed/stages/7_dump_package_json_stage.js +45 -0
  17. package/lib/compiler/elements/element.js +2 -2
  18. package/lib/compiler/index.d.ts +1 -0
  19. package/lib/compiler/index.js +3 -1
  20. package/lib/elements/blocks/block.builder.d.ts +1 -0
  21. package/lib/elements/blocks/block.builder.js +16 -1
  22. package/lib/elements/blocks/block.schema.d.ts +4 -1
  23. package/lib/elements/blocks/job/internal/resource_job.js +1 -1
  24. package/lib/elements/blocks/job/job.builder.d.ts +1 -0
  25. package/lib/elements/blocks/job/job.builder.js +2 -1
  26. package/lib/elements/blocks/resource/resource.builder.js +1 -1
  27. package/lib/elements/blocks/resource/resource.js +1 -0
  28. package/lib/elements/entities/bucket/adapters/bucket_adapter.d.ts +2 -2
  29. package/lib/elements/entities/bucket/adapters/bucket_adapter.js +2 -2
  30. package/lib/elements/entities/bucket/adapters/memory.nql.d.ts +1 -1
  31. package/lib/elements/entities/bucket/adapters/memory.nql.js +9 -5
  32. package/lib/elements/entities/bucket/bucket.d.ts +13 -2
  33. package/lib/elements/entities/bucket/bucket.js +47 -3
  34. package/lib/elements/entities/bucket/graph/bucket_graph.d.ts +11 -0
  35. package/lib/elements/entities/bucket/graph/bucket_graph.js +49 -2
  36. package/lib/elements/entities/bucket/model/bucket_model.schema.js +14 -11
  37. package/lib/elements/entities/bucket/query/nql_engine.d.ts +2 -2
  38. package/lib/elements/entities/bucket/query/nql_engine.js +8 -4
  39. package/lib/elements/entities/bucket/view/bucket_view.js +65 -12
  40. package/lib/engine/apps/app.config.d.ts +24 -30
  41. package/lib/engine/apps/app.config.js +11 -21
  42. package/lib/engine/apps/app.d.ts +6 -5
  43. package/lib/engine/apps/app.js +1 -1
  44. package/lib/engine/apps/distributed/distributed.app.config.d.ts +44 -0
  45. package/lib/engine/apps/distributed/distributed.app.config.js +36 -0
  46. package/lib/engine/apps/distributed/distributed.app.d.ts +30 -0
  47. package/lib/engine/apps/distributed/distributed.app.js +52 -0
  48. package/lib/engine/apps/distributed/distributed_node.app.d.ts +67 -0
  49. package/lib/engine/apps/distributed/distributed_node.app.js +191 -0
  50. package/lib/engine/apps/distributed/elements/distributed_job.d.ts +7 -0
  51. package/lib/engine/apps/distributed/elements/distributed_job.js +11 -0
  52. package/lib/engine/apps/distributed/inc/inc.client.d.ts +17 -0
  53. package/lib/engine/apps/distributed/inc/inc.client.js +105 -0
  54. package/lib/engine/apps/distributed/inc/inc.server.d.ts +19 -0
  55. package/lib/engine/apps/distributed/inc/inc.server.js +174 -0
  56. package/lib/engine/apps/distributed/inc/sandbox.d.ts +1 -0
  57. package/lib/engine/apps/distributed/inc/sandbox.js +24 -0
  58. package/lib/engine/apps/inline.app.d.ts +2 -2
  59. package/lib/engine/apps/inline.app.js +2 -2
  60. package/lib/engine/apps/monolyth/monolyth.app.d.ts +2 -2
  61. package/lib/engine/apps/monolyth/monolyth.app.js +1 -1
  62. package/lib/engine/module.d.ts +11 -1
  63. package/lib/engine/module.js +17 -4
  64. package/lib/engine/transaction/nodes/bucket.trx_node.d.ts +5 -0
  65. package/lib/engine/transaction/nodes/bucket.trx_node.js +10 -0
  66. package/lib/engine/transaction/nodes/bucket_query.trx_node.d.ts +2 -0
  67. package/lib/engine/transaction/nodes/bucket_query.trx_node.js +15 -4
  68. package/lib/engine/util/log.d.ts +1 -1
  69. package/lib/engine/util/log.js +3 -0
  70. package/package.json +1 -1
  71. package/tools/joaquin/bucket.d.ts +23 -3
  72. package/tools/joaquin/bucket.js +48 -20
  73. package/tools/joaquin/job.js +4 -4
  74. package/tsconfig.build.tsbuildinfo +1 -1
@@ -0,0 +1,30 @@
1
+ import { $Space } from "../../../schema";
2
+ import { Space } from '../../space';
3
+ import { Daemon } from "../../daemon";
4
+ import { DistributedAppConfig } from './distributed.app.config';
5
+ import { App } from '../app';
6
+ import { DistributedAppNode, DistributedAppNodeDef, DistributedNodeDaemon } from './distributed_node.app';
7
+ /**
8
+ * @category App
9
+ * @subcategory Distributed
10
+ */
11
+ export declare class DistributedApp<S extends $Space, Name extends string, Nodes extends Record<string, DistributedAppNode<any, any, any, any>> = {}> extends App<S, never, never> {
12
+ nodes: Nodes;
13
+ constructor(name: Name, space?: Space<S>);
14
+ boot(): DistributedApp<S, Name, Nodes>;
15
+ daemon(): Promise<DistributedDaemon<S, { [N in keyof Nodes as `${Name}-${N & string}`]: Nodes[N]; }>>;
16
+ node<N extends string, ModuleNames extends string, Services extends Record<string, any>, Def extends DistributedAppNodeDef<S, Nodes, ModuleNames, Services>>(name: N, def: Def): DistributedApp<S, Name, Nodes & {
17
+ [K in N]: ReturnType<Def>;
18
+ }>;
19
+ }
20
+ /**
21
+ * @category App
22
+ * @subcategory Distributed
23
+ */
24
+ export declare class DistributedDaemon<S extends $Space, Nodes extends Record<string, DistributedAppNode<any, any, any, any>>> extends Daemon<S, never> {
25
+ nodes: {
26
+ [K in keyof Nodes]: DistributedNodeDaemon<S, Nodes[K] extends DistributedAppNode<any, any, infer X, any> ? X : never>;
27
+ };
28
+ constructor(name: string, config: DistributedAppConfig<any, any, any, any>);
29
+ boot(nodes: Nodes): Promise<void>;
30
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DistributedDaemon = exports.DistributedApp = void 0;
4
+ const daemon_1 = require("../../daemon");
5
+ const app_1 = require("../app");
6
+ const distributed_node_app_1 = require("./distributed_node.app");
7
+ /**
8
+ * @category App
9
+ * @subcategory Distributed
10
+ */
11
+ class DistributedApp extends app_1.App {
12
+ constructor(name, space) {
13
+ super(name, { builders: [] });
14
+ this.nodes = {};
15
+ this.builders = undefined;
16
+ this.space = space;
17
+ }
18
+ // Override App abstract methods
19
+ boot() {
20
+ return this;
21
+ }
22
+ async daemon() {
23
+ const config = this._config;
24
+ const daemon = new DistributedDaemon(this.name, config);
25
+ await daemon.boot(this.nodes);
26
+ return daemon;
27
+ }
28
+ // Type Builder Overrides
29
+ node(name, def) {
30
+ let node = new distributed_node_app_1.DistributedAppNode(`${this.name}-${name}`, this.space);
31
+ node = def(node);
32
+ this.nodes[node.name] = node;
33
+ return this;
34
+ }
35
+ }
36
+ exports.DistributedApp = DistributedApp;
37
+ /**
38
+ * @category App
39
+ * @subcategory Distributed
40
+ */
41
+ class DistributedDaemon extends daemon_1.Daemon {
42
+ constructor(name, config) {
43
+ super(name, {}, {}, config);
44
+ this.nodes = {};
45
+ }
46
+ async boot(nodes) {
47
+ for (const name in nodes) {
48
+ this.nodes[name] = await nodes[name].daemon();
49
+ }
50
+ }
51
+ }
52
+ exports.DistributedDaemon = DistributedDaemon;
@@ -0,0 +1,67 @@
1
+ import { $Module, $Space, ModuleName } from "../../../schema";
2
+ import { IService } from '../service';
3
+ import { DistributedAppConfig, DistributedAppConfigBuilder } from './distributed.app.config';
4
+ import { App } from '../app';
5
+ import { MonolythDaemon } from '../monolyth/monolyth.app';
6
+ import { AnyTrxEngine } from "../../transaction/trx_engine";
7
+ import { AnyModule, Module } from "../../module";
8
+ import { Space } from "../../space";
9
+ export declare class DistributedAppNode<S extends $Space, Nodes extends Record<string, DistributedAppNode<any, any, any, any>>, ModuleNames extends string, Services extends Record<string, any>> extends App<S, ModuleNames, Services> {
10
+ _host: {
11
+ host: string;
12
+ port: number;
13
+ };
14
+ protected _daemon?: DistributedNodeDaemon<S, ModuleNames>;
15
+ protected _modules: Record<string, AnyModule>;
16
+ protected packageJson?: Record<string, any>;
17
+ protected bootPromise?: Promise<void>;
18
+ constructor(name: string, space?: Space<S>);
19
+ /**
20
+ * Treeshake and build modules declared for this application.
21
+ */
22
+ protected build(): Promise<void>;
23
+ boot(): DistributedAppNode<S, Nodes, ModuleNames, Services>;
24
+ /**
25
+ * Build the application, start services and trx engines.
26
+ * Returns references to start a daemon.
27
+ */
28
+ protected make(): Promise<{
29
+ modules: Record<string, AnyModule>;
30
+ services: Record<string, any>;
31
+ trxEngines: Record<ModuleNames, AnyTrxEngine>;
32
+ }>;
33
+ /**
34
+ * This method injects elements flagged as externals by referencing them
35
+ * from the other module directly, given this is a single-threaded App.
36
+ *
37
+ * TODO: allow overriding this behavior with adapters
38
+ */
39
+ protected linkExternals(modules: Record<string, Module<S, $Module>>): void;
40
+ /**
41
+ * This method injects values from environment variables into each module's
42
+ * app constants.
43
+ */
44
+ protected linkAppValues(modules: Record<string, Module<S, $Module>>): void;
45
+ daemon(): Promise<DistributedNodeDaemon<S, ModuleNames>>;
46
+ host(config: {
47
+ host: string;
48
+ port: number;
49
+ }): this;
50
+ modules<M extends ModuleName<S>>(modules: M[]): DistributedAppNode<S, Nodes, M & ModuleNames, Services>;
51
+ service<T extends IService>($: T): DistributedAppNode<S, Nodes, ModuleNames, Services & { [K in T["name"]]: T; }>;
52
+ package(_package: Record<string, any>): this;
53
+ get config(): DistributedAppConfigBuilder<S, Nodes, ModuleNames, Services>;
54
+ }
55
+ export type DistributedAppNodeDef<S extends $Space, Nodes extends Record<string, DistributedAppNode<any, any, any, any>>, ModuleNames extends string, Services extends Record<string, any>> = (builder: DistributedAppNode<S, Nodes, keyof S['modules'] & string, {}>) => DistributedAppNode<S, Nodes, ModuleNames, Services>;
56
+ /**
57
+ * @category App
58
+ * @subcategory Distributed
59
+ */
60
+ export declare class DistributedNodeDaemon<S extends $Space, Modules extends ModuleName<S>> extends MonolythDaemon<S, Modules> {
61
+ private inc;
62
+ constructor(name: string, host: {
63
+ host: string;
64
+ port: number;
65
+ }, trxEngines: Record<Modules, AnyTrxEngine>, services: Record<string, IService>, config?: DistributedAppConfig<any, any, any, any> | undefined);
66
+ boot(): Promise<void>;
67
+ }
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DistributedNodeDaemon = exports.DistributedAppNode = void 0;
7
+ const distributed_app_config_1 = require("./distributed.app.config");
8
+ const app_1 = require("../app");
9
+ const monolyth_app_1 = require("../monolyth/monolyth.app");
10
+ const inc_server_1 = require("./inc/inc.server");
11
+ const trx_engine_1 = require("../../transaction/trx_engine");
12
+ const tree_1 = require("../../tree");
13
+ const log_1 = require("../../util/log");
14
+ const promise_1 = __importDefault(require("../../util/promise"));
15
+ const distributed_job_1 = require("./elements/distributed_job");
16
+ class DistributedAppNode extends app_1.App {
17
+ constructor(name, space) {
18
+ super(name, { builders: [] });
19
+ this._modules = {};
20
+ this.space = space;
21
+ }
22
+ /**
23
+ * Treeshake and build modules declared for this application.
24
+ */
25
+ async build() {
26
+ log_1.Log.info('app', this.name, 'Booting');
27
+ this._modules = await this.makeModules();
28
+ log_1.Log.debug('app', this.name, 'Building');
29
+ const tree = new tree_1.ModuleTree(this._modules, {
30
+ exclude: ['*.test.ts']
31
+ });
32
+ await tree.resolve();
33
+ await tree.traverse('Building', async (node) => {
34
+ // Inline nodes are built by their root builder
35
+ if (node.isInline) {
36
+ return;
37
+ }
38
+ const module = this._modules[node.module];
39
+ await module.buildNode(node, tree);
40
+ });
41
+ }
42
+ boot() {
43
+ if (!this.bootPromise) {
44
+ this.bootPromise = this.build();
45
+ }
46
+ return this;
47
+ }
48
+ /**
49
+ * Build the application, start services and trx engines.
50
+ * Returns references to start a daemon.
51
+ */
52
+ async make() {
53
+ if (this.space || this.builders) {
54
+ await this.boot().bootPromise;
55
+ }
56
+ const modules = this._modules;
57
+ for (const key in this._injectedModules) {
58
+ const mod = this._injectedModules[key];
59
+ modules[mod.name] = mod;
60
+ }
61
+ const services = {};
62
+ for (const key in this._services) {
63
+ const service = this._services[key];
64
+ await promise_1.default.solve(service.up({
65
+ modules
66
+ }));
67
+ services[key] = service;
68
+ }
69
+ log_1.Log.debug('app', this.name, 'Starting transaction engines');
70
+ const trxEngines = {};
71
+ for (const m in modules) {
72
+ const module = modules[m];
73
+ module.start(this, services);
74
+ const trxConfig = this._config.modules?.[m]?.trx;
75
+ const authn = {};
76
+ for (const a in this._config?.authn || {}) {
77
+ const prov = this._config.authn?.[a]?.();
78
+ if (prov) {
79
+ authn[a] = prov;
80
+ }
81
+ }
82
+ trxEngines[m] = new trx_engine_1.TrxEngine(`app:${this.name}`, module, authn, trxConfig, services);
83
+ }
84
+ log_1.Log.debug('app', this.name, 'Linking externals');
85
+ this.linkExternals(modules);
86
+ log_1.Log.debug('app', this.name, 'Linking app values');
87
+ this.linkAppValues(modules);
88
+ return {
89
+ modules,
90
+ services,
91
+ trxEngines
92
+ };
93
+ }
94
+ /**
95
+ * This method injects elements flagged as externals by referencing them
96
+ * from the other module directly, given this is a single-threaded App.
97
+ *
98
+ * TODO: allow overriding this behavior with adapters
99
+ */
100
+ linkExternals(modules) {
101
+ Object.values(modules).forEach(module => {
102
+ const jobs = {};
103
+ for (const name in module.schema.externals.jobs) {
104
+ const dep = module.schema.externals.jobs[name];
105
+ const config = this._config;
106
+ const node = config?.modules?.[dep.module]?.jobs?.[dep.name];
107
+ if (!node) {
108
+ throw new Error(`External job '${dep.tag}' not configured on module '${this.name}'`);
109
+ }
110
+ jobs[name] = new distributed_job_1.DistributedJob(module, node);
111
+ }
112
+ module.injectRunners({
113
+ // buckets: Object.values(module.schema.externals.buckets),
114
+ // messages: Object.values(module.schema.externals.messages),
115
+ jobs
116
+ // machines: Object.values(module.schema.externals.machines),
117
+ });
118
+ // const buckets = module.schema.externals.buckets;
119
+ // Object.values(buckets).forEach(bucket => {
120
+ // module.nql.linkExternal(modules[bucket.module].buckets[bucket.name]);
121
+ // })
122
+ });
123
+ }
124
+ /**
125
+ * This method injects values from environment variables into each module's
126
+ * app constants.
127
+ */
128
+ linkAppValues(modules) {
129
+ Object.values(modules).forEach(module => {
130
+ const values = module.schema.constants.values;
131
+ Object.values(values).forEach(value => {
132
+ if (value.scope !== 'app')
133
+ return;
134
+ value.value = process.env[value.key];
135
+ });
136
+ });
137
+ }
138
+ // Override App abstract methods
139
+ async daemon() {
140
+ if (this._daemon) {
141
+ return this._daemon;
142
+ }
143
+ const app = await this.make();
144
+ log_1.Log.debug('app', this.name, 'Spawning daemon');
145
+ this._daemon = new DistributedNodeDaemon(this.name, this._host, app.trxEngines, app.services, this._config);
146
+ await this._daemon.boot();
147
+ // Link daemon to modules
148
+ for (const m in app.modules) {
149
+ const module = app.modules[m];
150
+ module.daemon = this._daemon;
151
+ }
152
+ return this._daemon;
153
+ }
154
+ host(config) {
155
+ this._host = config;
156
+ return this;
157
+ }
158
+ modules(modules) {
159
+ super.modules(modules);
160
+ return this;
161
+ }
162
+ service($) {
163
+ super.service($);
164
+ return this;
165
+ }
166
+ package(_package) {
167
+ this.packageJson = _package;
168
+ return this;
169
+ }
170
+ get config() {
171
+ return new distributed_app_config_1.DistributedAppConfigBuilder(this);
172
+ }
173
+ }
174
+ exports.DistributedAppNode = DistributedAppNode;
175
+ /**
176
+ * @category App
177
+ * @subcategory Distributed
178
+ */
179
+ class DistributedNodeDaemon extends monolyth_app_1.MonolythDaemon {
180
+ constructor(name, host, trxEngines, services, config) {
181
+ super(name, trxEngines, services, config);
182
+ this.inc = new inc_server_1.INCServer({
183
+ name,
184
+ ...host
185
+ });
186
+ }
187
+ async boot() {
188
+ await this.inc.start();
189
+ }
190
+ }
191
+ exports.DistributedNodeDaemon = DistributedNodeDaemon;
@@ -0,0 +1,7 @@
1
+ import { $Job, $Module, $Space } from "../../../../elements";
2
+ import { Job } from "../../../../elements/blocks/job/job";
3
+ import { Module } from "../../../module";
4
+ export declare class DistributedJob<S extends $Space, M extends $Module, $ extends $Job> extends Job<S, M, $> {
5
+ node: string;
6
+ constructor(module: Module<S, M>, node: string);
7
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DistributedJob = void 0;
4
+ const job_1 = require("../../../../elements/blocks/job/job");
5
+ class DistributedJob extends job_1.Job {
6
+ constructor(module, node) {
7
+ super(module, {});
8
+ this.node = node;
9
+ }
10
+ }
11
+ exports.DistributedJob = DistributedJob;
@@ -0,0 +1,17 @@
1
+ export type INCClientConfig = {
2
+ name: string;
3
+ server: {
4
+ host: string;
5
+ port: number;
6
+ };
7
+ };
8
+ export declare class INCClient {
9
+ config: INCClientConfig;
10
+ private client;
11
+ constructor(config: INCClientConfig);
12
+ start(): Promise<void>;
13
+ stop(): Promise<void>;
14
+ send(data: string | Buffer): Promise<void>;
15
+ private onError;
16
+ private onData;
17
+ }
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.INCClient = void 0;
37
+ const net = __importStar(require("net"));
38
+ const log_1 = require("../../../util/log");
39
+ class INCClient {
40
+ constructor(config) {
41
+ this.config = config;
42
+ this.client = new net.Socket();
43
+ this.client.setEncoding('utf8');
44
+ this.client.on('data', this.onData.bind(this));
45
+ }
46
+ start() {
47
+ log_1.Log.info('inc.client', this.config.name, 'Starting...');
48
+ return new Promise((resolve, reject) => {
49
+ this.client.connect({
50
+ host: this.config.server.host,
51
+ port: this.config.server.port
52
+ });
53
+ const errorFn = (error) => {
54
+ log_1.Log.error('inc.server', this.config.name, error.toString(), error);
55
+ reject(error);
56
+ };
57
+ this.client.on('error', errorFn);
58
+ this.client.on('connect', () => {
59
+ const address = this.client.address();
60
+ log_1.Log.debug('inc.client', this.config.name, 'Connected', {
61
+ server: `${this.client.remoteAddress}:${this.client.remotePort}`,
62
+ client: `${address.address}:${address.port}`
63
+ });
64
+ this.client.off('error', errorFn);
65
+ this.client.on('error', this.onError.bind(this));
66
+ resolve();
67
+ });
68
+ });
69
+ }
70
+ stop() {
71
+ log_1.Log.info('inc.client', this.config.name, 'Stop');
72
+ return new Promise((resolve) => {
73
+ this.client.end('Bye bye server', () => {
74
+ resolve();
75
+ });
76
+ });
77
+ }
78
+ send(data) {
79
+ log_1.Log.debug('inc.client', this.config.name, 'Sending data', {
80
+ data
81
+ });
82
+ return new Promise((resolve, reject) => {
83
+ this.client.write(data, error => {
84
+ if (error) {
85
+ reject(error);
86
+ }
87
+ else {
88
+ resolve();
89
+ }
90
+ });
91
+ });
92
+ }
93
+ // Callbacks
94
+ onError(error) {
95
+ log_1.Log.error('inc.client', this.config.name, error.toString(), {
96
+ error
97
+ });
98
+ }
99
+ onData(data) {
100
+ log_1.Log.debug('inc.client', this.config.name, 'Data received', {
101
+ data
102
+ });
103
+ }
104
+ }
105
+ exports.INCClient = INCClient;
@@ -0,0 +1,19 @@
1
+ import * as net from 'net';
2
+ export type INCServerConfig = {
3
+ name: string;
4
+ port: number;
5
+ maxConnections?: number;
6
+ };
7
+ export declare class INCServer {
8
+ config: INCServerConfig;
9
+ private server;
10
+ clients: Record<string, net.Socket>;
11
+ constructor(config: INCServerConfig);
12
+ start(): Promise<void>;
13
+ stop(): Promise<void>;
14
+ send(client: string, data: string | Buffer): Promise<void>;
15
+ private getClientCount;
16
+ private onError;
17
+ private onClose;
18
+ private onConnection;
19
+ }
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.INCServer = void 0;
37
+ const crypto_1 = require("crypto");
38
+ const net = __importStar(require("net"));
39
+ const log_1 = require("../../../util/log");
40
+ class INCServer {
41
+ constructor(config) {
42
+ this.config = config;
43
+ this.clients = {};
44
+ this.server = net.createServer();
45
+ this.server.on('close', this.onClose.bind(this));
46
+ this.server.on('connection', this.onConnection.bind(this));
47
+ this.server.maxConnections = this.config.maxConnections || 1;
48
+ }
49
+ start() {
50
+ log_1.Log.info('inc.server', this.config.name, 'Starting...');
51
+ return new Promise((resolve, reject) => {
52
+ this.server.listen(this.config.port);
53
+ const errorFn = (error) => {
54
+ log_1.Log.error('inc.server', this.config.name, error.toString(), error);
55
+ reject(error);
56
+ };
57
+ this.server.on('error', errorFn);
58
+ this.server.on('listening', () => {
59
+ log_1.Log.info('inc.server', this.config.name, `Listening on port ${this.config.port}`);
60
+ this.server.off('error', errorFn);
61
+ this.server.on('error', this.onError.bind(this));
62
+ resolve();
63
+ });
64
+ if (!this.server.listening) {
65
+ throw new Error('Failed to start INC server');
66
+ }
67
+ });
68
+ }
69
+ stop() {
70
+ log_1.Log.info('inc.server', this.config.name, 'Stop');
71
+ return new Promise((resolve) => {
72
+ this.server.close(() => {
73
+ resolve();
74
+ });
75
+ });
76
+ }
77
+ send(client, data) {
78
+ log_1.Log.debug('inc.server', this.config.name, 'Sending data', {
79
+ data
80
+ });
81
+ return new Promise((resolve, reject) => {
82
+ this.clients[client].write(data, error => {
83
+ if (error) {
84
+ reject(error);
85
+ }
86
+ else {
87
+ resolve();
88
+ }
89
+ });
90
+ });
91
+ }
92
+ // Utils
93
+ getClientCount() {
94
+ return new Promise((resolve, reject) => {
95
+ this.server.getConnections(function (error, count) {
96
+ if (error) {
97
+ reject(error);
98
+ }
99
+ resolve(count);
100
+ });
101
+ });
102
+ }
103
+ // Callbacks
104
+ onError(error) {
105
+ log_1.Log.error('inc.server', this.config.name, error.toString(), error);
106
+ }
107
+ onClose() {
108
+ log_1.Log.warn('inc.server', this.config.name, 'Closed');
109
+ }
110
+ async onConnection(socket) {
111
+ const id = (0, crypto_1.randomUUID)();
112
+ socket.id = id;
113
+ this.clients[id] = socket;
114
+ const serverAddress = this.server.address();
115
+ log_1.Log.debug('inc.server', this.config.name, 'New client connected', {
116
+ id,
117
+ server: `${serverAddress.address}:${serverAddress.port}`,
118
+ client: `${socket.remoteAddress}:${socket.remotePort}`,
119
+ clientCount: await this.getClientCount()
120
+ });
121
+ socket.setEncoding('utf8');
122
+ socket.setTimeout(1000 * 60 * 15); // 15 mins
123
+ socket.on('data', (data) => {
124
+ log_1.Log.debug('inc.server', this.config.name, 'Data received', {
125
+ client: socket.id,
126
+ data
127
+ });
128
+ });
129
+ socket.on('drain', function () {
130
+ socket.resume();
131
+ });
132
+ socket.on('error', (error) => {
133
+ delete this.clients[id];
134
+ log_1.Log.error('inc.server', this.config.name, 'Client error', {
135
+ client: socket.id,
136
+ error
137
+ });
138
+ });
139
+ socket.on('timeout', () => {
140
+ delete this.clients[id];
141
+ log_1.Log.warn('inc.server', this.config.name, 'Client timed-out', {
142
+ client: socket.id
143
+ });
144
+ socket.end('{"$":"__inc.timed_out"}');
145
+ });
146
+ socket.on('end', (data) => {
147
+ delete this.clients[id];
148
+ log_1.Log.debug('inc.server', this.config.name, 'Client disconnected', {
149
+ client: socket.id,
150
+ bytes_read: socket.bytesRead,
151
+ bytes_written: socket.bytesWritten,
152
+ data
153
+ });
154
+ });
155
+ socket.on('close', (error) => {
156
+ delete this.clients[id];
157
+ if (error) {
158
+ log_1.Log.error('inc.server', this.config.name, 'Client disconnected with error', {
159
+ client: socket.id,
160
+ bytes_read: socket.bytesRead,
161
+ bytes_written: socket.bytesWritten,
162
+ error
163
+ });
164
+ return;
165
+ }
166
+ log_1.Log.debug('inc.server', this.config.name, 'Client disconnected', {
167
+ client: socket.id,
168
+ bytes_read: socket.bytesRead,
169
+ bytes_written: socket.bytesWritten
170
+ });
171
+ });
172
+ }
173
+ }
174
+ exports.INCServer = INCServer;
@@ -0,0 +1 @@
1
+ export {};