-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin.ts
More file actions
137 lines (130 loc) · 4.48 KB
/
plugin.ts
File metadata and controls
137 lines (130 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { pluginV3 } from '@cloudquery/plugin-pb-javascript';
import grpc = require('@grpc/grpc-js');
import { Plugin } from '../plugin/plugin.js';
export class PluginServer extends pluginV3.cloudquery.plugin.v3.UnimplementedPluginService {
// Needed due to some TypeScript nonsense
private plugin: Plugin & grpc.UntypedHandleCall;
constructor(plugin: Plugin) {
super();
this.plugin = plugin as Plugin & grpc.UntypedHandleCall;
}
GetName(
call: grpc.ServerUnaryCall<
pluginV3.cloudquery.plugin.v3.GetName.Request,
pluginV3.cloudquery.plugin.v3.GetName.Response
>,
callback: grpc.sendUnaryData<pluginV3.cloudquery.plugin.v3.GetName.Response>,
): void {
return callback(null, new pluginV3.cloudquery.plugin.v3.GetName.Response({ name: this.plugin.name() }));
}
GetVersion(
call: grpc.ServerUnaryCall<
pluginV3.cloudquery.plugin.v3.GetVersion.Request,
pluginV3.cloudquery.plugin.v3.GetVersion.Response
>,
callback: grpc.sendUnaryData<pluginV3.cloudquery.plugin.v3.GetVersion.Response>,
): void {
return callback(null, new pluginV3.cloudquery.plugin.v3.GetVersion.Response({ version: this.plugin.version() }));
}
Init(
call: grpc.ServerUnaryCall<pluginV3.cloudquery.plugin.v3.Init.Request, pluginV3.cloudquery.plugin.v3.Init.Response>,
callback: grpc.sendUnaryData<pluginV3.cloudquery.plugin.v3.Init.Response>,
): void {
const {
request: { spec, no_connection: noConnection },
} = call;
const stringSpec = new TextDecoder().decode(spec);
this.plugin
.init(stringSpec, { noConnection })
.then(() => {
// eslint-disable-next-line promise/no-callback-in-promise
return callback(null, new pluginV3.cloudquery.plugin.v3.Init.Response());
})
.catch((error) => {
// eslint-disable-next-line promise/no-callback-in-promise
return callback(error, null);
});
}
GetTables(
call: grpc.ServerUnaryCall<
pluginV3.cloudquery.plugin.v3.GetTables.Request,
pluginV3.cloudquery.plugin.v3.GetTables.Response
>,
callback: grpc.sendUnaryData<pluginV3.cloudquery.plugin.v3.GetTables.Response>,
): void {
const {
request: { tables, skip_tables: skipTables, skip_dependent_tables: skipDependentTables },
} = call;
this.plugin
.tables({ tables, skipTables, skipDependentTables })
.then((tables) => {
const encodedTables = tables.map((table) => new TextEncoder().encode(table));
// eslint-disable-next-line promise/no-callback-in-promise
return callback(null, new pluginV3.cloudquery.plugin.v3.GetTables.Response({ tables: encodedTables }));
})
.catch((error) => {
// eslint-disable-next-line promise/no-callback-in-promise
return callback(error, null);
});
}
Sync(
call: grpc.ServerWritableStream<
pluginV3.cloudquery.plugin.v3.Sync.Request,
pluginV3.cloudquery.plugin.v3.Sync.Response
>,
): void {
const {
request: {
tables,
skip_tables: skipTables,
skip_dependent_tables: skipDependentTables,
deterministic_cq_id: deterministicCQId,
backend: { connection, table_name: tableName },
},
} = call;
this.plugin.sync({
tables,
skipTables,
skipDependentTables,
deterministicCQId,
backendOptions: { connection, tableName },
stream: call,
});
}
Read(
call: grpc.ServerWritableStream<
pluginV3.cloudquery.plugin.v3.Read.Request,
pluginV3.cloudquery.plugin.v3.Read.Response
>,
): void {
this.plugin.read(call);
}
Write(
call: grpc.ServerReadableStream<
pluginV3.cloudquery.plugin.v3.Write.Request,
pluginV3.cloudquery.plugin.v3.Write.Response
>,
callback: grpc.sendUnaryData<pluginV3.cloudquery.plugin.v3.Write.Response>,
): void {
this.plugin.write(call);
callback(null, new pluginV3.cloudquery.plugin.v3.Write.Response());
}
Close(
call: grpc.ServerUnaryCall<
pluginV3.cloudquery.plugin.v3.Close.Request,
pluginV3.cloudquery.plugin.v3.Close.Response
>,
callback: grpc.sendUnaryData<pluginV3.cloudquery.plugin.v3.Close.Response>,
): void {
this.plugin
.close()
.then(() => {
// eslint-disable-next-line promise/no-callback-in-promise
return callback(null, new pluginV3.cloudquery.plugin.v3.Close.Response());
})
.catch((error) => {
// eslint-disable-next-line promise/no-callback-in-promise
return callback(error, null);
});
}
}