Skip to content

Commit

Permalink
transform wrapped result later before returning the response
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Jan 9, 2025
1 parent 6c51eb9 commit 83dccc3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
15 changes: 7 additions & 8 deletions packages/cubejs-api-gateway/src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1814,11 +1814,10 @@ class ApiGateway {
if (props.queryType === 'multi') {
// We prepare the final json result on native side
const resultMulti = new ResultMultiWrapper(results, { queryType, slowQuery });
res(await resultMulti.getFinalResult());
res(resultMulti);
} else {
// We prepare the full final json result on native side
const r = results[0];
res(await r.getFinalResult());
res(results[0]);
}
} catch (e: any) {
this.handleError({
Expand Down Expand Up @@ -1953,7 +1952,7 @@ class ApiGateway {
} else {
// We prepare the final json result on native side
const resultArray = new ResultArrayWrapper(results);
res(await resultArray.getFinalResult());
res(resultArray);
}
}
} catch (e: any) {
Expand Down Expand Up @@ -2001,7 +2000,7 @@ class ApiGateway {
query,
context,
res: (message, opts) => {
if (!Array.isArray(message) && message.error) {
if (!Array.isArray(message) && 'error' in message && message.error) {
error = { message, opts };
} else {
result = { message, opts };
Expand All @@ -2025,14 +2024,14 @@ class ApiGateway {
}

protected resToResultFn(res: ExpressResponse) {
return (message, { status }: { status?: number } = {}) => {
return async (message, { status }: { status?: number } = {}) => {
if (status) {
res.status(status);
}

if (message instanceof ArrayBuffer) {
if (message.isWrapper) {
res.set('Content-Type', 'application/json');
res.send(Buffer.from(message));
res.send(Buffer.from(await message.getFinalResult()));
} else {
res.json(message);
}
Expand Down
7 changes: 1 addition & 6 deletions packages/cubejs-api-gateway/src/sql-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,7 @@ export class SQLServer {
return;
}

if (response instanceof ArrayBuffer) {
const json = JSON.parse(new TextDecoder().decode(response));
resolve(json);
} else {
resolve(response);
}
resolve(response);
},
apiType: 'sql',
});
Expand Down
3 changes: 2 additions & 1 deletion packages/cubejs-api-gateway/src/types/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

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

Expand Down Expand Up @@ -105,7 +106,7 @@ type MetaResponseResultFn = (message: MetaResponse | ErrorResponse) => void;
*/
type ResponseResultFn =
(
message: (Record<string, any> | Record<string, any>[]) | ErrorResponse,
message: (Record<string, any> | Record<string, any>[]) | DataResult | ErrorResponse,
extra?: { status: number }
) => void;

Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-backend-native/js/ResultWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from './index';

export interface DataResult {
isWrapper: boolean;
getFinalResult(): Promise<any>;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/cubejs-backend-native/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,11 @@ function wrapNativeFunctionWithStream(
});
} else if (response.error) {
writerOrChannel.reject(errorString(response));
} else if (response.isWrapper) { // Native wrapped result
const resArBuf = await response.getFinalResult();
const resStr = new TextDecoder().decode(resArBuf);
writerOrChannel.resolve(resStr);
} else {
// TODO remove JSON.stringify()
writerOrChannel.resolve(JSON.stringify(response));
}
} catch (e: any) {
Expand Down

0 comments on commit 83dccc3

Please sign in to comment.