Skip to content

Commit 9326e5d

Browse files
committed
transform wrapped result later before returning the response
1 parent 2eee432 commit 9326e5d

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
@@ -1834,11 +1834,10 @@ class ApiGateway {
18341834
if (props.queryType === 'multi') {
18351835
// We prepare the final json result on native side
18361836
const resultMulti = new ResultMultiWrapper(results, { queryType, slowQuery });
1837-
res(await resultMulti.getFinalResult());
1837+
res(resultMulti);
18381838
} else {
18391839
// We prepare the full final json result on native side
1840-
const r = results[0];
1841-
res(await r.getFinalResult());
1840+
res(results[0]);
18421841
}
18431842
} catch (e: any) {
18441843
this.handleError({
@@ -1973,7 +1972,7 @@ class ApiGateway {
19731972
} else {
19741973
// We prepare the final json result on native side
19751974
const resultArray = new ResultArrayWrapper(results);
1976-
res(await resultArray.getFinalResult());
1975+
res(resultArray);
19771976
}
19781977
}
19791978
} catch (e: any) {
@@ -2021,7 +2020,7 @@ class ApiGateway {
20212020
query,
20222021
context,
20232022
res: (message, opts) => {
2024-
if (!Array.isArray(message) && message.error) {
2023+
if (!Array.isArray(message) && 'error' in message && message.error) {
20252024
error = { message, opts };
20262025
} else {
20272026
result = { message, opts };
@@ -2045,14 +2044,14 @@ class ApiGateway {
20452044
}
20462045

20472046
protected resToResultFn(res: ExpressResponse) {
2048-
return (message, { status }: { status?: number } = {}) => {
2047+
return async (message, { status }: { status?: number } = {}) => {
20492048
if (status) {
20502049
res.status(status);
20512050
}
20522051

2053-
if (message instanceof ArrayBuffer) {
2052+
if (message.isWrapper) {
20542053
res.set('Content-Type', 'application/json');
2055-
res.send(Buffer.from(message));
2054+
res.send(Buffer.from(await message.getFinalResult()));
20562055
} else {
20572056
res.json(message);
20582057
}

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)