From d0ef70473d212d9d1c2d64d998040b25c6a378f5 Mon Sep 17 00:00:00 2001 From: lohxt1 Date: Mon, 18 Nov 2024 13:39:55 +0530 Subject: [PATCH] fix: remove json bigint from cli --- packages/bruno-cli/package.json | 1 - .../bruno-cli/src/runner/prepare-request.js | 14 ++----- .../src/runner/run-single-request.js | 7 +++- packages/bruno-cli/src/utils/common.js | 29 ++++++++++++- .../collection/echo/echo bigint.bru | 42 ------------------- 5 files changed, 38 insertions(+), 55 deletions(-) delete mode 100644 packages/bruno-tests/collection/echo/echo bigint.bru diff --git a/packages/bruno-cli/package.json b/packages/bruno-cli/package.json index 5020f8475..b182e4acc 100644 --- a/packages/bruno-cli/package.json +++ b/packages/bruno-cli/package.json @@ -37,7 +37,6 @@ "fs-extra": "^10.1.0", "http-proxy-agent": "^7.0.0", "https-proxy-agent": "^7.0.2", - "json-bigint": "^1.0.0", "lodash": "^4.17.21", "qs": "^6.11.0", "socks-proxy-agent": "^8.0.2", diff --git a/packages/bruno-cli/src/runner/prepare-request.js b/packages/bruno-cli/src/runner/prepare-request.js index bc2b22886..ffcae0360 100644 --- a/packages/bruno-cli/src/runner/prepare-request.js +++ b/packages/bruno-cli/src/runner/prepare-request.js @@ -1,6 +1,5 @@ const { get, each, filter } = require('lodash'); const fs = require('fs'); -var JSONbig = require('json-bigint'); const decomment = require('decomment'); const crypto = require('node:crypto'); @@ -31,7 +30,8 @@ const prepareRequest = (request, collectionRoot) => { method: request.method, url: request.url, headers: headers, - pathParams: request?.params?.filter((param) => param.type === 'path') + pathParams: request?.params?.filter((param) => param.type === 'path'), + responseType: 'arraybuffer' }; const collectionAuth = get(collectionRoot, 'request.auth'); @@ -96,16 +96,10 @@ const prepareRequest = (request, collectionRoot) => { if (!contentTypeDefined) { axiosRequest.headers['content-type'] = 'application/json'; } - let jsonBody; try { - jsonBody = decomment(request?.body?.json); + axiosRequest.data = decomment(request?.body?.json); } catch (error) { - jsonBody = request?.body?.json; - } - try { - axiosRequest.data = JSONbig.parse(jsonBody); - } catch (error) { - axiosRequest.data = jsonBody; + axiosRequest.data = request?.body?.json; } } diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index cb59c78ba..0705f5270 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -19,7 +19,7 @@ const { makeAxiosInstance } = require('../utils/axios-instance'); const { addAwsV4Interceptor, resolveAwsV4Credentials } = require('./awsv4auth-helper'); const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../utils/proxy-util'); const path = require('path'); -const { createFormData } = require('../utils/common'); +const { createFormData, parseDataFromResponse } = require('../utils/common'); const protocolRegex = /^([-+\w]{1,25})(:?\/\/|:)/; const onConsoleLog = (type, args) => { @@ -217,11 +217,16 @@ const runSingleRequest = async function ( /** @type {import('axios').AxiosResponse} */ response = await axiosInstance(request); + const { data } = parseDataFromResponse(response, request.__brunoDisableParsingResponseJson); + response.data = data; + // Prevents the duration on leaking to the actual result responseTime = response.headers.get('request-duration'); response.headers.delete('request-duration'); } catch (err) { if (err?.response) { + const { data } = parseDataFromResponse(err?.response); + err.response.data = data; response = err.response; // Prevents the duration on leaking to the actual result diff --git a/packages/bruno-cli/src/utils/common.js b/packages/bruno-cli/src/utils/common.js index 16c2d1a7b..dd45be501 100644 --- a/packages/bruno-cli/src/utils/common.js +++ b/packages/bruno-cli/src/utils/common.js @@ -2,6 +2,7 @@ const fs = require('fs'); const FormData = require('form-data'); const { forOwn } = require('lodash'); const path = require('path'); +const iconv = require('iconv-lite'); const lpad = (str, width) => { let paddedStr = str; @@ -43,9 +44,35 @@ const createFormData = (datas, collectionPath) => { return form; }; +const parseDataFromResponse = (response, disableParsingResponseJson = false) => { + // Parse the charset from content type: https://stackoverflow.com/a/33192813 + const charsetMatch = /charset=([^()<>@,;:"/[\]?.=\s]*)/i.exec(response.headers['content-type'] || ''); + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#using_exec_with_regexp_literals + const charsetValue = charsetMatch?.[1]; + const dataBuffer = Buffer.from(response.data); + // Overwrite the original data for backwards compatibility + let data; + if (iconv.encodingExists(charsetValue)) { + data = iconv.decode(dataBuffer, charsetValue); + } else { + data = iconv.decode(dataBuffer, 'utf-8'); + } + // Try to parse response to JSON, this can quietly fail + try { + // Filter out ZWNBSP character + // https://gist.github.com/antic183/619f42b559b78028d1fe9e7ae8a1352d + data = data.replace(/^\uFEFF/, ''); + if (!disableParsingResponseJson) { + data = JSON.parse(data); + } + } catch { } + + return { data, dataBuffer }; +}; module.exports = { lpad, rpad, - createFormData + createFormData, + parseDataFromResponse }; diff --git a/packages/bruno-tests/collection/echo/echo bigint.bru b/packages/bruno-tests/collection/echo/echo bigint.bru deleted file mode 100644 index ef981c723..000000000 --- a/packages/bruno-tests/collection/echo/echo bigint.bru +++ /dev/null @@ -1,42 +0,0 @@ -meta { - name: echo bigint - type: http - seq: 6 -} - -post { - url: {{host}}/api/echo/json - body: json - auth: none -} - -headers { - foo: bar -} - -auth:basic { - username: asd - password: j -} - -auth:bearer { - token: -} - -body:json { - { - "hello": 990531470713421825, - "decimal": 1.0, - "decimal2": 1.00, - "decimal3": 1.00200, - "decimal4": 0.00 - } -} - -assert { - res.status: eq 200 -} - -tests { - // todo: add tests once lossless json echo server is ready -}