Skip to content

Commit 08b8245

Browse files
feat: Fir 31841 dont append account id for system engine url in node sdk (#82)
1 parent 4f3e03f commit 08b8245

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

src/connection/base.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,16 @@ export abstract class Connection {
115115
this.parameters = remainingParameters;
116116
}
117117

118-
private async handleUpdateEndpointHeader(headerValue: string): Promise<void> {
118+
protected splitEndpoint(endpoint: string): [string, Record<string, string>] {
119119
const url = new URL(
120-
headerValue.startsWith("http") ? headerValue : `https://${headerValue}`
120+
endpoint.startsWith("http") ? endpoint : `https://${endpoint}`.trim()
121121
);
122-
const newParams = Object.fromEntries(url.searchParams.entries());
122+
const params = Object.fromEntries(url.searchParams.entries());
123+
return [url.toString().replace(url.search, ""), params];
124+
}
125+
126+
private async handleUpdateEndpointHeader(headerValue: string): Promise<void> {
127+
const [endpoint, newParams] = this.splitEndpoint(headerValue);
123128

124129
// Validate account_id if present
125130
const currentAccountId =
@@ -131,7 +136,7 @@ export abstract class Connection {
131136
}
132137

133138
// Remove url parameters and update engineEndpoint
134-
this.engineEndpoint = url.toString().replace(url.search, "");
139+
this.engineEndpoint = endpoint;
135140
this.parameters = {
136141
...this.parameters,
137142
...newParams

src/connection/connection_v2.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ export class ConnectionV2 extends BaseConnection {
2222
return this.options.account;
2323
}
2424

25-
private async getSystemEngineEndpoint(): Promise<string> {
25+
private async getSystemEngineEndpointAndParameters(): Promise<
26+
[string, Record<string, string>]
27+
> {
2628
const { apiEndpoint, httpClient } = this.context;
2729
const accountName = this.account;
2830
const url = `${apiEndpoint}/${ACCOUNT_SYSTEM_ENGINE(accountName)}`;
2931
try {
3032
const { engineUrl } = await httpClient
3133
.request<{ engineUrl: string }>("GET", url)
3234
.ready();
33-
// cut off query parameters that go after ?
34-
return engineUrl.split("?")[0];
35+
return this.splitEndpoint(engineUrl);
3536
} catch (e) {
3637
if (e instanceof ApiError && e.status == 404) {
3738
throw new AccountNotFoundError({ account_name: accountName });
@@ -142,8 +143,10 @@ export class ConnectionV2 extends BaseConnection {
142143
async resolveEngineEndpoint() {
143144
const { engineName, database } = this.options;
144145
// Connect to system engine first
145-
const systemUrl = await this.getSystemEngineEndpoint();
146-
this.engineEndpoint = path.join(systemUrl, QUERY_URL);
146+
const [systemUrl, systemParameters] =
147+
await this.getSystemEngineEndpointAndParameters();
148+
this.engineEndpoint = new URL(QUERY_URL, systemUrl).href;
149+
this.parameters = { ...this.parameters, ...systemParameters };
147150
this.accountInfo = await this.resolveAccountInfo();
148151

149152
if (this.accountInfo.infraVersion >= 2) {
@@ -189,9 +192,12 @@ export class ConnectionV2 extends BaseConnection {
189192
protected getBaseParameters(
190193
executeQueryOptions: ExecuteQueryOptions
191194
): Record<string, string | undefined> {
192-
return {
193-
account_id: this.accountInfo?.id,
194-
...super.getBaseParameters(executeQueryOptions)
195-
};
195+
if (this.accountInfo?.infraVersion == 1) {
196+
return {
197+
account_id: this.accountInfo?.id,
198+
...super.getBaseParameters(executeQueryOptions)
199+
};
200+
}
201+
return super.getBaseParameters(executeQueryOptions);
196202
}
197203
}

test/unit/v2/connection.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,63 @@ describe("Connection V2", () => {
210210
const accountInfo = await (connection as ConnectionV2).resolveAccountInfo();
211211
expect(accountInfo.infraVersion).toBe(1);
212212
});
213+
it("respects system engine query parameters for account version 2", async () => {
214+
let systemEngineParamsUsed = {};
215+
server.use(
216+
rest.post(`https://id.fake.firebolt.io/oauth/token`, (req, res, ctx) => {
217+
return res(
218+
ctx.json({
219+
access_token: "fake_access_token"
220+
})
221+
);
222+
}),
223+
rest.get(
224+
`https://api.fake.firebolt.io/web/v3/account/my_account/resolve`,
225+
(req, res, ctx) => {
226+
return res(
227+
ctx.json({
228+
id: "123",
229+
infraVersion: 2
230+
})
231+
);
232+
}
233+
),
234+
rest.get(
235+
`https://api.fake.firebolt.io/web/v3/account/my_account/engineUrl`,
236+
(req, res, ctx) => {
237+
return res(
238+
ctx.json({
239+
engineUrl: "https://some_system_engine.com?param=value"
240+
})
241+
);
242+
}
243+
),
244+
rest.post(
245+
`https://some_system_engine.com/${QUERY_URL}`,
246+
(req, res, ctx) => {
247+
systemEngineParamsUsed = Object.fromEntries(
248+
req.url.searchParams.entries()
249+
);
250+
return res(ctx.json(engineUrlResponse));
251+
}
252+
)
253+
);
254+
const firebolt = Firebolt({
255+
apiEndpoint
256+
});
257+
258+
const connectionParams: ConnectionOptions = {
259+
auth: {
260+
client_id: "dummy",
261+
client_secret: "dummy"
262+
},
263+
database: "dummy",
264+
account: "my_account"
265+
};
266+
267+
const connection = await firebolt.connect(connectionParams);
268+
await connection.execute("SELECT 1");
269+
expect(systemEngineParamsUsed).toHaveProperty("param");
270+
expect(systemEngineParamsUsed).not.toHaveProperty("account_id");
271+
});
213272
});

0 commit comments

Comments
 (0)