fix network stackoverflow #215 (#474)

Summary:
fixed issue: https://github.com/facebook/flipper/issues/215

## Changelog

1. use pako to inflate direct to unicode string to avoid 'stack size limit exceed' problem
Pull Request resolved: https://github.com/facebook/flipper/pull/474

Test Plan:
Tested with Meetup trace that crashes reproducibly without this fix:
{F165395994}

Tested gzipped body with fb4a:

{F165396053}

Reviewed By: jknoxville

Differential Revision: D16090209

Pulled By: passy

fbshipit-source-id: 64b8a61fa9d80d9e8b62105b3f1dc3289a29cc07
This commit is contained in:
henry_zheng
2019-07-04 05:12:10 -07:00
committed by Facebook Github Bot
parent 3ba71cea9c
commit 1b3ae77c40

View File

@@ -24,17 +24,17 @@ export function decodeBody(container: Request | Response): string {
} }
const b64Decoded = atob(container.data); const b64Decoded = atob(container.data);
const body = try {
getHeaderValue(container.headers, 'Content-Encoding') === 'gzip' if (getHeaderValue(container.headers, 'Content-Encoding') === 'gzip') {
? decompress(b64Decoded) // for gzip, use pako to decompress directly to unicode string
: b64Decoded; return decompress(b64Decoded);
}
// Data is transferred as base64 encoded bytes to support unicode characters, // Data is transferred as base64 encoded bytes to support unicode characters,
// we need to decode the bytes here to display the correct unicode characters. // we need to decode the bytes here to display the correct unicode characters.
try { return decodeURIComponent(escape(b64Decoded));
return decodeURIComponent(escape(body));
} catch (e) { } catch (e) {
console.warn('Discarding malformed body:', escape(body)); console.warn('Discarding malformed body:', escape(b64Decoded));
return ''; return '';
} }
} }
@@ -44,20 +44,18 @@ function decompress(body: string): string {
const byteArray = new Uint8Array(charArray); const byteArray = new Uint8Array(charArray);
let data;
try { try {
if (body) { if (body) {
data = pako.inflate(byteArray); return pako.inflate(byteArray, {to: 'string'});
} else { } else {
return body; return body;
} }
} catch (e) { } catch (e) {
// Sometimes Content-Encoding is 'gzip' but the body is already decompressed. // Sometimes Content-Encoding is 'gzip' but the body is already decompressed.
// Assume this is the case when decompression fails. // Assume this is the case when decompression fails.
return body;
} }
return String.fromCharCode.apply(null, new Uint8Array(data)); return body;
} }
export function convertRequestToCurlCommand(request: Request): string { export function convertRequestToCurlCommand(request: Request): string {