Skip to content

Commit 4deb0c4

Browse files
committed
transform wrapped result later before returning the response
1 parent fb2c692 commit 4deb0c4

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

packages/cubejs-api-gateway/src/gateway.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,11 +1814,10 @@ class ApiGateway {
18141814
if (props.queryType === 'multi') {
18151815
// We prepare the final json result on native side
18161816
const resultMulti = new ResultMultiWrapper(results, { queryType, slowQuery });
1817-
res(await resultMulti.getFinalResult());
1817+
res(resultMulti);
18181818
} else {
18191819
// We prepare the full final json result on native side
1820-
const r = results[0];
1821-
res(await r.getFinalResult());
1820+
res(results[0]);
18221821
}
18231822
} catch (e: any) {
18241823
this.handleError({
@@ -1953,7 +1952,7 @@ class ApiGateway {
19531952
} else {
19541953
// We prepare the final json result on native side
19551954
const resultArray = new ResultArrayWrapper(results);
1956-
res(await resultArray.getFinalResult());
1955+
res(resultArray);
19571956
}
19581957
}
19591958
} catch (e: any) {
@@ -2001,7 +2000,7 @@ class ApiGateway {
20012000
query,
20022001
context,
20032002
res: (message, opts) => {
2004-
if (!Array.isArray(message) && message.error) {
2003+
if (!Array.isArray(message) && 'error' in message && message.error) {
20052004
error = { message, opts };
20062005
} else {
20072006
result = { message, opts };
@@ -2025,14 +2024,14 @@ class ApiGateway {
20252024
}
20262025

20272026
protected resToResultFn(res: ExpressResponse) {
2028-
return (message, { status }: { status?: number } = {}) => {
2027+
return async (message, { status }: { status?: number } = {}) => {
20292028
if (status) {
20302029
res.status(status);
20312030
}
20322031

2033-
if (message instanceof ArrayBuffer) {
2032+
if (message.isWrapper) {
20342033
res.set('Content-Type', 'application/json');
2035-
res.send(Buffer.from(message));
2034+
res.send(Buffer.from(await message.getFinalResult()));
20362035
} else {
20372036
res.json(message);
20382037
}

packages/cubejs-api-gateway/src/sql-server.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ export class SQLServer {
193193
return;
194194
}
195195

196-
if (response instanceof ArrayBuffer) {
197-
const json = JSON.parse(new TextDecoder().decode(response));
198-
resolve(json);
199-
} else {
200-
resolve(response);
201-
}
196+
resolve(response);
202197
},
203198
apiType: 'sql',
204199
});

packages/cubejs-api-gateway/src/types/request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import type { Request as ExpressRequest } from 'express';
9+
import type { DataResult } from '@cubejs-backend/native';
910
import { RequestType, ApiType, ResultType } from './strings';
1011
import { Query } from './query';
1112

@@ -105,7 +106,7 @@ type MetaResponseResultFn = (message: MetaResponse | ErrorResponse) => void;
105106
*/
106107
type ResponseResultFn =
107108
(
108-
message: (Record<string, any> | Record<string, any>[]) | ErrorResponse,
109+
message: (Record<string, any> | Record<string, any>[]) | DataResult | ErrorResponse,
109110
extra?: { status: number }
110111
) => void;
111112

packages/cubejs-backend-native/js/ResultWrapper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from './index';
88

99
export interface DataResult {
10+
isWrapper: boolean;
1011
getFinalResult(): Promise<any>;
1112
}
1213

packages/cubejs-backend-native/js/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,11 @@ function wrapNativeFunctionWithStream(
266266
});
267267
} else if (response.error) {
268268
writerOrChannel.reject(errorString(response));
269+
} else if (response.isWrapper) { // Native wrapped result
270+
const resArBuf = await response.getFinalResult();
271+
const resStr = new TextDecoder().decode(resArBuf);
272+
writerOrChannel.resolve(resStr);
269273
} else {
270-
// TODO remove JSON.stringify()
271274
writerOrChannel.resolve(JSON.stringify(response));
272275
}
273276
} catch (e: any) {

0 commit comments

Comments
 (0)