diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 010c5fc6..fea91654 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -30,6 +30,7 @@ "cors": "2.8.5", "dayjs": "1.11.13", "fs-extra": "^11.1.1", + "json-cycle": "^1.5.0", "lodash": "^4.17.21", "open": "^8.4.2", "serve-static": "1.16.2", diff --git a/packages/sdk/src/sdk/sdk/core.ts b/packages/sdk/src/sdk/sdk/core.ts index fdcef4ff..65447f78 100644 --- a/packages/sdk/src/sdk/sdk/core.ts +++ b/packages/sdk/src/sdk/sdk/core.ts @@ -5,6 +5,7 @@ import path from 'path'; import { createHash } from 'crypto'; import process from 'process'; import { AsyncSeriesHook } from 'tapable'; +const jc = require('json-cycle'); import { debug } from '@rsdoctor/utils/logger'; import { transformDataUrls } from '../utils'; import { RsdoctorSDKOptions, DataWithUrl } from './types'; @@ -142,6 +143,9 @@ export abstract class SDKCore } const jsonStr: string | string[] = await (async () => { try { + if (key === 'configs') { + return JSON.stringify(jc.decycle(data)); + } return JSON.stringify(data); } catch (error) { // use the stream json stringify when call JSON.stringify failed due to the json is too large. diff --git a/packages/sdk/src/sdk/sdk/index.ts b/packages/sdk/src/sdk/sdk/index.ts index b756c661..fe829ab3 100644 --- a/packages/sdk/src/sdk/sdk/index.ts +++ b/packages/sdk/src/sdk/sdk/index.ts @@ -12,6 +12,7 @@ import { RsdoctorWebpackSDKOptions } from './types'; import { SDKCore } from './core'; import { Algorithm } from '@rsdoctor/utils/common'; import { isNumber } from 'lodash'; +const jc = require('json-cycle'); export * from '../utils/openBrowser'; @@ -504,6 +505,9 @@ export class RsdoctorSDK< const jsonStrFn = () => { try { + if (key === 'configs') { + return JSON.stringify(jc.decycle(data)); + } return JSON.stringify(data); } catch (error) { console.error(error); diff --git a/packages/utils/src/common/algorithm.ts b/packages/utils/src/common/algorithm.ts index 63616e7e..f777ba88 100644 --- a/packages/utils/src/common/algorithm.ts +++ b/packages/utils/src/common/algorithm.ts @@ -1,5 +1,6 @@ import { deflateSync, inflateSync } from 'zlib'; import { Buffer } from 'buffer'; +import { debug } from '../logger'; export function mergeIntervals(intervals: [number, number][]) { // Sort from small to large @@ -25,9 +26,13 @@ export function mergeIntervals(intervals: [number, number][]) { } export function compressText(input: string): string { - return deflateSync(input).toString('base64'); + try { + return deflateSync(input).toString('base64'); + } catch (e) { + debug(() => `compressText error: ${e}`); + return ''; + } } - export function decompressText(input: string): string { return inflateSync(Buffer.from(input, 'base64')).toString(); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2f04aa5d..c9c828ce 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -926,6 +926,9 @@ importers: fs-extra: specifier: ^11.1.1 version: 11.2.0 + json-cycle: + specifier: ^1.5.0 + version: 1.5.0 lodash: specifier: ^4.17.21 version: 4.17.21 @@ -4174,7 +4177,7 @@ packages: dependencies: '@babel/core': 7.26.0 '@babel/helper-plugin-utils': 7.24.8 - '@babel/helper-validator-option': 7.25.9 + '@babel/helper-validator-option': 7.24.8 '@babel/plugin-transform-react-display-name': 7.24.7(@babel/core@7.26.0) '@babel/plugin-transform-react-jsx': 7.25.2(@babel/core@7.26.0) '@babel/plugin-transform-react-jsx-development': 7.24.7(@babel/core@7.26.0) @@ -4206,7 +4209,7 @@ packages: dependencies: '@babel/core': 7.26.0 '@babel/helper-plugin-utils': 7.24.8 - '@babel/helper-validator-option': 7.25.9 + '@babel/helper-validator-option': 7.24.8 '@babel/plugin-syntax-jsx': 7.24.7(@babel/core@7.26.0) '@babel/plugin-transform-modules-commonjs': 7.24.8(@babel/core@7.26.0) '@babel/plugin-transform-typescript': 7.25.2(@babel/core@7.26.0) @@ -13969,6 +13972,11 @@ packages: resolution: {integrity: sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==} dev: true + /json-cycle@1.5.0: + resolution: {integrity: sha512-GOehvd5PO2FeZ5T4c+RxobeT5a1PiGpF4u9/3+UvrMU4bhnVqzJY7hm39wg8PDCqkU91fWGH8qjWR4bn+wgq9w==} + engines: {node: '>= 4'} + dev: false + /json-parse-even-better-errors@2.3.1: resolution: {integrity: sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==}