@aloma.io/integration-sdk 3.0.0-4

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 (56) hide show
  1. package/build/builder/index.d.mts +11 -0
  2. package/build/builder/index.mjs +51 -0
  3. package/build/builder/runtime-context.d.mts +7 -0
  4. package/build/builder/runtime-context.mjs +42 -0
  5. package/build/builder/transform/index.d.mts +5 -0
  6. package/build/builder/transform/index.mjs +72 -0
  7. package/build/controller/index.d.mts +19 -0
  8. package/build/controller/index.mjs +44 -0
  9. package/build/index.d.mts +2 -0
  10. package/build/index.mjs +2 -0
  11. package/build/internal/dispatcher/index.cjs +151 -0
  12. package/build/internal/dispatcher/index.d.cts +32 -0
  13. package/build/internal/index.cjs +461 -0
  14. package/build/internal/index.d.cts +14 -0
  15. package/build/internal/util/jwe/cli.cjs +13 -0
  16. package/build/internal/util/jwe/cli.d.cts +1 -0
  17. package/build/internal/util/jwe/index.cjs +57 -0
  18. package/build/internal/util/jwe/index.d.cts +32 -0
  19. package/build/internal/websocket/config.cjs +79 -0
  20. package/build/internal/websocket/config.d.cts +41 -0
  21. package/build/internal/websocket/connection/constants.cjs +21 -0
  22. package/build/internal/websocket/connection/constants.d.cts +2 -0
  23. package/build/internal/websocket/connection/index.cjs +53 -0
  24. package/build/internal/websocket/connection/index.d.cts +11 -0
  25. package/build/internal/websocket/connection/registration.cjs +31 -0
  26. package/build/internal/websocket/connection/registration.d.cts +5 -0
  27. package/build/internal/websocket/index.cjs +41 -0
  28. package/build/internal/websocket/index.d.cts +16 -0
  29. package/build/internal/websocket/transport/durable.cjs +61 -0
  30. package/build/internal/websocket/transport/durable.d.cts +20 -0
  31. package/build/internal/websocket/transport/index.cjs +148 -0
  32. package/build/internal/websocket/transport/index.d.cts +37 -0
  33. package/build/internal/websocket/transport/packet.cjs +44 -0
  34. package/build/internal/websocket/transport/packet.d.cts +16 -0
  35. package/build/internal/websocket/transport/processor.cjs +58 -0
  36. package/build/internal/websocket/transport/processor.d.cts +11 -0
  37. package/package.json +44 -0
  38. package/src/builder/index.mts +70 -0
  39. package/src/builder/runtime-context.mts +45 -0
  40. package/src/builder/transform/index.mts +89 -0
  41. package/src/controller/index.mts +58 -0
  42. package/src/index.mts +2 -0
  43. package/src/internal/dispatcher/index.cjs +189 -0
  44. package/src/internal/index.cjs +547 -0
  45. package/src/internal/util/jwe/cli.cjs +14 -0
  46. package/src/internal/util/jwe/index.cjs +69 -0
  47. package/src/internal/websocket/config.cjs +103 -0
  48. package/src/internal/websocket/connection/constants.cjs +25 -0
  49. package/src/internal/websocket/connection/index.cjs +70 -0
  50. package/src/internal/websocket/connection/registration.cjs +40 -0
  51. package/src/internal/websocket/index.cjs +46 -0
  52. package/src/internal/websocket/transport/durable.cjs +71 -0
  53. package/src/internal/websocket/transport/index.cjs +183 -0
  54. package/src/internal/websocket/transport/packet.cjs +54 -0
  55. package/src/internal/websocket/transport/processor.cjs +66 -0
  56. package/tsconfig.json +27 -0
@@ -0,0 +1,11 @@
1
+ import RuntimeContext from './runtime-context.mjs';
2
+ export declare class Builder {
3
+ private data;
4
+ id(what: string): Builder;
5
+ version(what: string): Builder;
6
+ config(arg: any): Builder;
7
+ options(arg: any): Builder;
8
+ auth(arg: any): Builder;
9
+ build(): Promise<RuntimeContext>;
10
+ private discoverTypes;
11
+ }
@@ -0,0 +1,51 @@
1
+ import fs from 'fs';
2
+ import parseTypes from './transform/index.mjs';
3
+ import RuntimeContext from './runtime-context.mjs';
4
+ import { fileURLToPath } from 'node:url';
5
+ import path from 'node:path';
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ const notEmpty = (what, name) => {
8
+ if (!what?.trim())
9
+ throw new Error(`${name} cannot be empty`);
10
+ return what;
11
+ };
12
+ export class Builder {
13
+ data = { controller: './build/controller/.controller-for-types.mts' };
14
+ id(what) {
15
+ this.data.id = notEmpty(what, 'id');
16
+ return this;
17
+ }
18
+ version(what) {
19
+ this.data.version = notEmpty(what, 'version');
20
+ return this;
21
+ }
22
+ config(arg) {
23
+ return this;
24
+ }
25
+ options(arg) {
26
+ return this;
27
+ }
28
+ auth(arg) {
29
+ return this;
30
+ }
31
+ async build() {
32
+ const data = this.data;
33
+ notEmpty(data.id, 'id');
34
+ notEmpty(data.version, 'version');
35
+ await this.discoverTypes();
36
+ // validate config
37
+ // validate options
38
+ // validate auth
39
+ console.log('dirname', __dirname);
40
+ // @ts-ignore
41
+ const Controller = (await import(__dirname + '/../../../../../build/controller/index.mjs')).default;
42
+ return new RuntimeContext(new Controller(), this.data);
43
+ }
44
+ async discoverTypes() {
45
+ notEmpty(this.data.controller, 'controller');
46
+ const content = fs.readFileSync(this.data.controller);
47
+ const { text, methods } = parseTypes(this.data.controller);
48
+ this.data.types = text;
49
+ this.data.methods = methods;
50
+ }
51
+ }
@@ -0,0 +1,7 @@
1
+ import { AbstractController } from '../controller/index.mjs';
2
+ export default class RuntimeContext {
3
+ private controller;
4
+ private data;
5
+ constructor(controller: AbstractController, data: any);
6
+ start(): Promise<void>;
7
+ }
@@ -0,0 +1,42 @@
1
+ import { Connector } from '../internal/index.cjs';
2
+ export default class RuntimeContext {
3
+ controller;
4
+ data;
5
+ constructor(controller, data) {
6
+ this.controller = controller;
7
+ this.data = data;
8
+ }
9
+ async start() {
10
+ const controller = this.controller;
11
+ const connector = new Connector({
12
+ id: this.data.id,
13
+ version: this.data.version,
14
+ name: `${this.data.id}/${this.data.version}`,
15
+ });
16
+ const resolvers = {};
17
+ const methods = [...this.data.methods, '__endpoint', '__configQuery', '__default'];
18
+ methods.forEach((method) => {
19
+ resolvers[method] = async (args) => {
20
+ console.log('call', method, args);
21
+ if (!methods.includes(method))
22
+ throw new Error(`${method} not found`);
23
+ return controller[method](args);
24
+ };
25
+ });
26
+ console.log(this.data.types);
27
+ connector
28
+ .configure()
29
+ .types(this.data.types)
30
+ .resolvers(resolvers)
31
+ .main(async ({ newTask, updateTask, config, oauth, getClient }) => {
32
+ try {
33
+ await controller._doStop();
34
+ await controller._doStart(config, oauth, newTask, updateTask, getClient);
35
+ }
36
+ catch (e) {
37
+ console.log(e);
38
+ }
39
+ });
40
+ connector.run();
41
+ }
42
+ }
@@ -0,0 +1,5 @@
1
+ declare const _default: (path: string) => {
2
+ text: any;
3
+ methods: string[];
4
+ };
5
+ export default _default;
@@ -0,0 +1,72 @@
1
+ import { parseFromFiles } from '@ts-ast-parser/core';
2
+ const transform = (meta) => {
3
+ if (!meta?.length)
4
+ throw new Error('metadata is empty');
5
+ meta = meta[0];
6
+ if (meta.getDeclarations()?.length !== 1) {
7
+ throw new Error('connector file needs to export default class');
8
+ }
9
+ const methods = {};
10
+ const decl = meta.getDeclarations()[0];
11
+ // todo validate getConstructors
12
+ const members = decl.getMethods().filter((member) => {
13
+ return !(member.isStatic() ||
14
+ member.isInherited() ||
15
+ member.getKind() !== 'Method' ||
16
+ member.getModifier() !== 'public' ||
17
+ member.getName().startsWith('_'));
18
+ });
19
+ const text = members
20
+ .map((member) => {
21
+ methods[member.getName()] = true;
22
+ return member
23
+ .getSignatures()
24
+ .map((sig) => {
25
+ const docs = sig.getJSDoc().serialize() || [];
26
+ const desc = docs.find((what) => what.kind === 'description')?.value;
27
+ const paramDocs = docs
28
+ .filter((what) => what.kind === 'param')
29
+ .map((what) => {
30
+ return ` * @param {${what.value.type}} ${what.value.name} - ${what.value.description || ''}`;
31
+ })
32
+ .join('\n') || ' *';
33
+ const params = sig
34
+ .getParameters()
35
+ .map((param) => {
36
+ const serialized = param.serialize();
37
+ switch (!!param.isNamed()) {
38
+ case true:
39
+ const tmp = param
40
+ .getNamedElements()
41
+ .map((p) => {
42
+ const defaultVal = p.default != null ? ' = ' + p.default : '';
43
+ return `${p.name}${defaultVal}`;
44
+ })
45
+ .join('; ');
46
+ return `{${tmp}}: ${param.getType().text}`;
47
+ case false:
48
+ return `${param.getName()}: ${param.getType().text}`;
49
+ }
50
+ })
51
+ .join(', ');
52
+ const retVal = sig
53
+ .getReturnType()
54
+ .type.text.replace(/^Promise</, '')
55
+ .replace(/>$/, '');
56
+ return `
57
+ /**
58
+ * ${desc || ''}
59
+ *
60
+ ${paramDocs}
61
+ **/
62
+ declare function ${member.getName()}(${params}): ${retVal};
63
+ `;
64
+ })
65
+ .join('\n');
66
+ })
67
+ .join('');
68
+ return { text, methods: Object.keys(methods) };
69
+ };
70
+ export default (path) => {
71
+ return transform(parseFromFiles([path]));
72
+ };
@@ -0,0 +1,19 @@
1
+ export declare abstract class AbstractController {
2
+ protected config: any;
3
+ protected client: any;
4
+ protected start(): Promise<void>;
5
+ protected stop(): Promise<void>;
6
+ protected configQuery(arg: any): Promise<any>;
7
+ protected fallback(arg: any): Promise<any>;
8
+ protected endpoint(arg: any): Promise<any>;
9
+ protected newTask(name: string, data: any): Promise<string>;
10
+ protected getClient({ baseUrl }: {
11
+ baseUrl: string;
12
+ }): Promise<any>;
13
+ protected updateTask(name: string, data: any): Promise<string>;
14
+ __endpoint(arg: any): Promise<any | null>;
15
+ __configQuery(arg: any): Promise<any | null>;
16
+ __default(arg: any): Promise<any | null>;
17
+ _doStart(config: any, client: any, newTask: any, updateTask: any, getClient: any): Promise<void>;
18
+ _doStop(): Promise<void>;
19
+ }
@@ -0,0 +1,44 @@
1
+ export class AbstractController {
2
+ config;
3
+ client;
4
+ async start() { }
5
+ async stop() { }
6
+ configQuery(arg) {
7
+ return Promise.resolve({});
8
+ }
9
+ fallback(arg) {
10
+ throw new Error('method not found');
11
+ }
12
+ endpoint(arg) {
13
+ throw new Error('method not found');
14
+ }
15
+ async newTask(name, data) {
16
+ throw new Error('not implemented');
17
+ }
18
+ getClient({ baseUrl }) {
19
+ throw new Error('not implemented');
20
+ }
21
+ async updateTask(name, data) {
22
+ throw new Error('not implemented');
23
+ }
24
+ async __endpoint(arg) {
25
+ return Promise.resolve(null);
26
+ }
27
+ async __configQuery(arg) {
28
+ return this.configQuery(arg);
29
+ }
30
+ async __default(arg) {
31
+ return this.fallback(arg);
32
+ }
33
+ async _doStart(config, client, newTask, updateTask, getClient) {
34
+ this.config = config;
35
+ this.client = client;
36
+ this.newTask = newTask;
37
+ this.updateTask = updateTask;
38
+ this.getClient = getClient;
39
+ await this.start();
40
+ }
41
+ async _doStop() {
42
+ await this.stop();
43
+ }
44
+ }
@@ -0,0 +1,2 @@
1
+ export * from './builder/index.mjs';
2
+ export * from './controller/index.mjs';
@@ -0,0 +1,2 @@
1
+ export * from './builder/index.mjs';
2
+ export * from './controller/index.mjs';
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Dispatcher {
4
+ constructor() {
5
+ this._config = { fields: {} };
6
+ }
7
+ main(what) {
8
+ this._main = what;
9
+ return this;
10
+ }
11
+ oauth(arg) {
12
+ if (arg.platformOAuth) {
13
+ this.config({
14
+ oauth: true,
15
+ fields: {
16
+ oauthResult: {
17
+ name: 'OAuth Result',
18
+ placeholder: 'will be set by finishing the oauth flow',
19
+ type: 'managed',
20
+ },
21
+ },
22
+ });
23
+ return this;
24
+ }
25
+ if (!arg.authorizationURL)
26
+ throw new Error('need a authorizationURL');
27
+ if (!arg.tokenURL && !arg.finishOAuth)
28
+ throw new Error('need a tokenURL or finishOAuth()');
29
+ this._oauth = { ...arg };
30
+ this.config({
31
+ oauth: true,
32
+ fields: {
33
+ oauthResult: {
34
+ name: 'OAuth Result',
35
+ placeholder: 'will be set by finishing the oauth flow',
36
+ type: 'managed',
37
+ },
38
+ },
39
+ });
40
+ if (arg.configurableClient) {
41
+ this.config({
42
+ fields: {
43
+ clientId: {
44
+ name: 'OAuth Client ID',
45
+ placeholder: 'e.g. 1234',
46
+ type: 'line',
47
+ },
48
+ clientSecret: {
49
+ name: 'OAuth Client Secret',
50
+ placeholder: 'e.g. axd5xde',
51
+ type: 'line',
52
+ },
53
+ },
54
+ });
55
+ }
56
+ return this;
57
+ }
58
+ types(what) {
59
+ this._types = what;
60
+ return this;
61
+ }
62
+ config({ fields, oauth }) {
63
+ this._config.oauth = this._config.oauth || oauth;
64
+ this._config.fields = { ...fields, ...this._config.fields };
65
+ return this;
66
+ }
67
+ resolvers(what) {
68
+ this._resolvers = { ...this._resolvers, ...what };
69
+ return this;
70
+ }
71
+ endpoint(what) {
72
+ this.config({
73
+ fields: {
74
+ _endpointToken: {
75
+ name: 'Endpoint Token (set to enable the endpoint)',
76
+ placeholder: 'e.g. 1234',
77
+ type: 'line',
78
+ plain: true,
79
+ optional: true,
80
+ },
81
+ },
82
+ });
83
+ this.resolvers({ _endpoint: what });
84
+ return this;
85
+ }
86
+ startOAuth() {
87
+ throw new Error('oauth not configured');
88
+ }
89
+ finishOAuth() {
90
+ throw new Error('oauth not configured');
91
+ }
92
+ build() {
93
+ if (!this._types || !this._resolvers)
94
+ throw new Error('missing types or resolvers');
95
+ var local = this;
96
+ const _resolvers = { ...this._resolvers };
97
+ const main = this._main || (() => { });
98
+ const start = async (transport) => {
99
+ console.log('starting ...');
100
+ await main(transport);
101
+ };
102
+ const resolveMethod = (query) => {
103
+ let current = _resolvers;
104
+ while (query.length && current) {
105
+ current = current[query.shift()];
106
+ }
107
+ return current;
108
+ };
109
+ const execute = async ({ query, variables }) => {
110
+ if (!Array.isArray(query))
111
+ query = [query];
112
+ query = query
113
+ .filter((what) => !!what?.trim() && !['constructor', '__proto__', 'toString', 'toSource', 'prototype'].includes(what))
114
+ .slice(0, 20);
115
+ const method = resolveMethod(query);
116
+ console.log('found method', query, method);
117
+ if (!method && !_resolvers.__default)
118
+ throw new Error(`${query} not found`);
119
+ return method ? method(variables) : _resolvers.__default(variables ? { ...variables, __method: query } : variables);
120
+ };
121
+ const introspect = () => local._types;
122
+ const configSchema = () => local._config;
123
+ const processPacket = async (packet) => {
124
+ switch (packet.method()) {
125
+ case 'connector.introspect':
126
+ const intro = await introspect({});
127
+ return { configSchema: local._config, introspect: intro };
128
+ case 'connector.start-oauth':
129
+ return await local.startOAuth(packet.args());
130
+ case 'connector.finish-oauth':
131
+ return await local.finishOAuth(packet.args());
132
+ case 'connector.query':
133
+ const ret = await execute(packet.args());
134
+ return typeof ret === 'object' && !Array.isArray(ret) ? ret : { [packet.args().query]: ret };
135
+ case 'connector.set-config':
136
+ await local.onConfig({ ...packet.args().secrets });
137
+ return;
138
+ }
139
+ console.dir(packet, { depth: null });
140
+ throw new Error('cannot handle packet');
141
+ };
142
+ return {
143
+ introspect,
144
+ configSchema,
145
+ execute,
146
+ processPacket,
147
+ start,
148
+ };
149
+ }
150
+ }
151
+ module.exports = { Dispatcher };
@@ -0,0 +1,32 @@
1
+ export class Dispatcher {
2
+ _config: {
3
+ fields: {};
4
+ };
5
+ main(what: any): this;
6
+ _main: any;
7
+ oauth(arg: any): this;
8
+ _oauth: any;
9
+ types(what: any): this;
10
+ _types: any;
11
+ config({ fields, oauth }: {
12
+ fields: any;
13
+ oauth: any;
14
+ }): this;
15
+ resolvers(what: any): this;
16
+ _resolvers: any;
17
+ endpoint(what: any): this;
18
+ startOAuth(): void;
19
+ finishOAuth(): void;
20
+ build(): {
21
+ introspect: () => any;
22
+ configSchema: () => {
23
+ fields: {};
24
+ };
25
+ execute: ({ query, variables }: {
26
+ query: any;
27
+ variables: any;
28
+ }) => Promise<any>;
29
+ processPacket: (packet: any) => Promise<any>;
30
+ start: (transport: any) => Promise<void>;
31
+ };
32
+ }