Skip to content

feat: decode msgpack response type to JSON #4704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/bruno-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"@babel/preset-env": "^7.26.0",
"@fontsource/inter": "^5.0.15",
"@msgpack/msgpack": "^3.1.1",
"@prantlf/jsonlint": "^16.0.0",
"@reduxjs/toolkit": "^1.8.0",
"@tabler/icons": "^1.46.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,15 @@
import { debounce } from 'lodash';
import QueryResultFilter from './QueryResultFilter';
import { JSONPath } from 'jsonpath-plus';
import React from 'react';
import classnames from 'classnames';
import iconv from 'iconv-lite';
import { getContentType, safeStringifyJSON, safeParseXML } from 'utils/common';
import { getContentType } from 'utils/common';
import { getCodeMirrorModeBasedOnContentType } from 'utils/common/codemirror';
import QueryResultPreview from './QueryResultPreview';
import StyledWrapper from './StyledWrapper';
import { useState, useMemo, useEffect } from 'react';
import { useTheme } from 'providers/Theme/index';
import { getEncoding, uuid } from 'utils/common/index';

const formatResponse = (data, dataBuffer, encoding, mode, filter) => {
if (data === undefined || !dataBuffer || !mode) {
return '';
}

// TODO: We need a better way to get the raw response-data here instead
// of using this dataBuffer param.
// Also, we only need the raw response-data and content-type to show the preview.
const rawData = iconv.decode(
Buffer.from(dataBuffer, "base64"),
iconv.encodingExists(encoding) ? encoding : "utf-8"
);

if (mode.includes('json')) {
try {
JSON.parse(rawData);
} catch (error) {
// If the response content-type is JSON and it fails parsing, its an invalid JSON.
// In that case, just show the response as it is in the preview.
return rawData;
}

if (filter) {
try {
data = JSONPath({ path: filter, json: data });
} catch (e) {
console.warn('Could not apply JSONPath filter:', e.message);
}
}

return safeStringifyJSON(data, true);
}

if (mode.includes('xml')) {
let parsed = safeParseXML(data, { collapseContent: true });
if (typeof parsed === 'string') {
return parsed;
}
return safeStringifyJSON(parsed, true);
}

if (typeof data === 'string') {
return data;
}

return safeStringifyJSON(data, true);
};
import { formatResponse } from './utils';

const formatErrorMessage = (error) => {
if (!error) return 'Something went wrong';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { JSONPath } from 'jsonpath-plus';
import { decode } from '@msgpack/msgpack';
import iconv from 'iconv-lite';
import { safeParseXML, safeStringifyJSON } from 'utils/common/index';

export const formatResponse = (data, dataBuffer, encoding, mode, filter) => {
if (data === undefined || !dataBuffer || !mode) {
return '';
}

// TODO: We need a better way to get the raw response-data here instead
// of using this dataBuffer param.
// Also, we only need the raw response-data and content-type to show the preview.
const rawData = iconv.decode(
Buffer.from(dataBuffer, "base64"),
iconv.encodingExists(encoding) ? encoding : "utf-8"
);

if (mode.includes('json')) {
try {
JSON.parse(rawData);
} catch (error) {
// If the response content-type is JSON and it fails parsing, its an invalid JSON.
// In that case, just show the response as it is in the preview.
return rawData;
}

if (filter) {
try {
data = JSONPath({ path: filter, json: data });
} catch (e) {
console.warn('Could not apply JSONPath filter:', e.message);
}
}

return safeStringifyJSON(data, true);
}

if (mode.includes('xml')) {
let parsed = safeParseXML(data, { collapseContent: true });
if (typeof parsed === 'string') {
return parsed;
}
return safeStringifyJSON(parsed, true);
}

if (mode.includes('msgpack')) {
try {
const decodedData = decode(Buffer.from(dataBuffer, "base64"));
return safeStringifyJSON(decodedData, true);
} catch (error) {
console.warn('Error decoding msgpack data:', error);
return rawData;
}
}

if (typeof data === 'string') {
return data;
}

return safeStringifyJSON(data, true);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { describe, it, expect } = require('@jest/globals');

import { formatResponse } from './utils';

describe('formatResponse', () => {
describe('msgpack', () => {
it('should decode msgpack data and return a formatted string', () => {
const dataBuffer = Buffer.from([0x81, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72]);
const encoding = 'utf-8';
const mode = 'msgpack';
const result = formatResponse(null, dataBuffer.toString('base64'), encoding, mode);
expect(result).toBe(`{
"foo": "bar"
}`
);
});

it('should show raw data when invalid msgpack', () => {
const dataBuffer = Buffer.from([0x81, 0xa3, 0x66, 0x6f, 0x6f]);
const encoding = 'utf-8';
const mode = 'msgpack';
const result = formatResponse(null, dataBuffer.toString('base64'), encoding, mode);
expect(result).toBe('��foo');
});
})
});
2 changes: 2 additions & 0 deletions packages/bruno-app/src/utils/common/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ export const getCodeMirrorModeBasedOnContentType = (contentType, body) => {
return 'application/xml';
} else if (contentType.includes('yaml')) {
return 'application/yaml';
} else if (contentType.includes('msgpack')) {
return 'application/msgpack';
} else {
return 'application/text';
}
Expand Down
Loading