Repository navigation
Expand file tree
/
Copy pathstackql.ts
More file actions
185 lines (162 loc) · 5.37 KB
/
Copy pathstackql.ts
File metadata and controls
185 lines (162 loc) · 5.37 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { assertExists } from "https://deno.land/std@0.206.0/assert/assert_exists.ts";
import { Downloader } from "./services/downloader.ts";
import osUtils from "./utils/os.ts";
import { Server } from "./services/server.ts";
import { Client } from "https://deno.land/x/postgres@v0.17.0/client.ts";
export interface StackQLConfig {
binaryPath?: string;
serverMode?: boolean;
connectionString?: string;
maxResults?: number;
pageLimit?: number;
maxDepth?: number;
apiTimeout?: number;
proxyHost?: string;
proxyPort?: number;
proxyUser?: string;
proxyPassword?: string;
proxyScheme?: "http" | "https";
}
export class StackQL {
private binaryPath?: string; //The full path of the `stackql` executable (not supported in `server_mode`).
private downloader: Downloader = new Downloader();
private serverMode = false;
private connection?: Client;
private format: "object" = "object";
private params: string[] = [];
private version: string | undefined; // The version number of the `stackql` executable (not supported in `server_mode`)
private sha: string | undefined; // The commit (short) sha for the installed `stackql` binary build (not supported in `server_mode`).
constructor() {
}
getParams() {
return this.params;
}
getBinaryPath() {
return this.binaryPath;
}
async getVersion() {
if (!this.version) {
await this.updateVersion();
}
return { version: this.version, sha: this.sha };
}
private async updateVersion() {
if (!this.binaryPath) {
throw new Error("Binary path not found");
}
const output = await osUtils.runCommand(this.binaryPath, ["--version"]);
if (output) {
const versionTokens: string[] = output.split("\n")[0].split(" ");
const version: string = versionTokens[1];
const sha: string = versionTokens[3].replace("(", "").replace(")", "");
this.version = version;
this.sha = sha;
}
}
async upgrade() {
this.binaryPath = await this.downloader.upgradeStackQL();
await this.updateVersion();
return this.getVersion();
}
public async initialize(config: StackQLConfig) {
this.binaryPath = config.binaryPath;
this.serverMode = config.serverMode || false;
if (this.serverMode) {
await this.setupConnection(config.connectionString);
return;
}
if (this.binaryExist()) {
return;
}
this.binaryPath = await this.downloader.setupStackQL();
this.setProperties(config);
}
private binaryExist() {
return !!this.binaryPath && osUtils.fileExists(this.binaryPath);
}
private setProxyProperties(config: StackQLConfig): void {
if (config.proxyHost !== undefined) {
this.params.push("--http.proxy.host");
this.params.push(config.proxyHost);
}
if (config.proxyPort !== undefined) {
this.params.push("--http.proxy.port");
this.params.push(config.proxyPort.toString());
}
if (config.proxyUser !== undefined) {
this.params.push("--http.proxy.user");
this.params.push(config.proxyUser);
}
if (config.proxyPassword !== undefined) {
this.params.push("--http.proxy.password");
this.params.push(config.proxyPassword);
}
if (config.proxyScheme !== undefined) {
if (!["http", "https"].includes(config.proxyScheme)) {
throw new Error(
`Invalid proxyScheme. Expected one of ['http', 'https'], got ${config.proxyScheme}.`,
);
}
this.params.push("--http.proxy.scheme");
this.params.push(config.proxyScheme);
}
}
private setProperties(config: StackQLConfig): void {
if (config.maxResults !== undefined) {
this.params.push("--http.response.maxResults");
this.params.push(config.maxResults.toString());
}
if (config.pageLimit !== undefined) {
this.params.push("--http.response.pageLimit");
this.params.push(config.pageLimit.toString());
}
if (config.maxDepth !== undefined) {
this.params.push("--indirect.depth.max");
this.params.push(config.maxDepth.toString());
}
if (config.apiTimeout !== undefined) {
this.params.push("--apirequesttimeout");
this.params.push(config.apiTimeout.toString());
}
if (config.proxyHost !== undefined) {
this.setProxyProperties(config);
}
}
public async runQuery(query: string) {
assertExists(this.binaryPath);
const args = ["exec", query].concat(this.params);
try {
const result = await osUtils.runCommand(this.binaryPath, args);
return result;
} catch (error) {
console.error(error);
throw new Error(`StackQL query failed: ${error.message}`);
}
}
//////////////////////Server mode related methods
private async queryObjectFormat(query: string) {
assertExists(this.connection);
const pgResult = await this.connection.queryObject(query);
return pgResult.rows;
}
private async setupConnection(connectionString?: string) {
const server = new Server();
this.connection = await server.connect(connectionString);
}
public async closeConnection() {
if (this.connection) {
await this.connection.end();
}
}
public async runServerQuery(query: string) {
try {
if (this.format === "object") {
const result = await this.queryObjectFormat(query);
return result;
}
} catch (error) {
console.error(error);
throw new Error(`StackQL server query failed: ${error.message}`);
}
}
}