Convert network plugin to Sandy

Summary:
converted the network plugin to use DataSource / DataTable. Restructured the storage to contain a single flat normalised object that will be much more efficient for rendering / filtering (as columns currently don't support nested keys yet, and lazy columns are a lot less flexible)

lint errors and further `flipper` package usages will be cleaned up in the next diff to make sure this diff doesn't become too large.

The rest of the plugin is converted in the next diff

Reviewed By: nikoant

Differential Revision: D27938581

fbshipit-source-id: 2e0e2ba75ef13d88304c6566d4519b121daa215b
This commit is contained in:
Michel Weststrate
2021-05-06 04:26:41 -07:00
committed by Facebook GitHub Bot
parent bef1885395
commit 23402dfff6
12 changed files with 608 additions and 763 deletions

View File

@@ -8,10 +8,16 @@
*/
import pako from 'pako';
import {Request, Response, Header} from './types';
import {Request, Header, ResponseInfo} from './types';
import {Base64} from 'js-base64';
export function getHeaderValue(headers: Array<Header>, key: string): string {
export function getHeaderValue(
headers: Array<Header> | undefined,
key: string,
): string {
if (!headers) {
return '';
}
for (const header of headers) {
if (header.key.toLowerCase() === key.toLowerCase()) {
return header.value;
@@ -20,7 +26,10 @@ export function getHeaderValue(headers: Array<Header>, key: string): string {
return '';
}
export function decodeBody(container: Request | Response): string {
export function decodeBody(container: {
headers?: Array<Header>;
data: string | null | undefined;
}): string {
if (!container.data) {
return '';
}
@@ -59,16 +68,21 @@ export function decodeBody(container: Request | Response): string {
}
}
export function convertRequestToCurlCommand(request: Request): string {
export function convertRequestToCurlCommand(
request: Pick<Request, 'method' | 'url' | 'requestHeaders' | 'requestData'>,
): string {
let command: string = `curl -v -X ${request.method}`;
command += ` ${escapedString(request.url)}`;
// Add headers
request.headers.forEach((header: Header) => {
request.requestHeaders.forEach((header: Header) => {
const headerStr = `${header.key}: ${header.value}`;
command += ` -H ${escapedString(headerStr)}`;
});
// Add body. TODO: we only want this for non-binary data! See D23403095
const body = decodeBody(request);
const body = decodeBody({
headers: request.requestHeaders,
data: request.requestData,
});
if (body) {
command += ` -d ${escapedString(body)}`;
}
@@ -101,3 +115,15 @@ function escapedString(str: string) {
// Simply use singly quoted string.
return "'" + str + "'";
}
export function getResponseLength(request: ResponseInfo): number {
const lengthString = request.headers
? getHeaderValue(request.headers, 'content-length')
: undefined;
if (lengthString) {
return parseInt(lengthString, 10);
} else if (request.data) {
return Buffer.byteLength(request.data, 'base64');
}
return 0;
}