Skip to content
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

fix: Workaround JSON.stringify failing on large arrays with the error… #2051

Merged
Merged
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
21 changes: 16 additions & 5 deletions packages/cli-hermes/src/profileHermes/downloadProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ function execSyncWithLog(command: string) {
return execSync(command);
}

/**
* A wrapper that converts an object to JSON with 4 spaces for indentation.
*
* @param obj Any object that can be represented as JSON
* @returns A JSON string
*/
function jsonStringify(obj: any) {
return JSON.stringify(obj, undefined, 4);
}

/**
* Pull and convert a Hermes tracing profile to Chrome tracing profile
* @param ctx
Expand Down Expand Up @@ -123,11 +133,12 @@ export async function downloadProfile(
file,
'.cpuprofile',
)}-converted.json`;
fs.writeFileSync(
transformedFilePath,
JSON.stringify(events, undefined, 4),
'utf-8',
);

// Convert to JSON in chunks because JSON.stringify() will fail for large
// arrays with the error "RangeError: Invalid string length"
const out = events.map(jsonStringify).join(',');

fs.writeFileSync(transformedFilePath, '[' + out + ']', 'utf-8');
logger.success(
`Successfully converted to Chrome tracing format and pulled the file to ${transformedFilePath}`,
);
Expand Down