Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
Accept partially updated newRequest messages
Browse files Browse the repository at this point in the history
Summary:
# Context

D64736668 changed IG4A to send two `newRequest` commands with the same ID: the first one when the request starts, and the second one when the request body finished. This caused request bodies to disappear from Flipper. D66176444 reverted the IG4A change.

https://fb.workplace.com/groups/503431980400629/posts/1800245397385941/?comment_id=1800491244028023

# This diff

Handle repeated `newRequest` command Flipper-side.

Reviewed By: LukeDefeo

Differential Revision: D66232498

fbshipit-source-id: 3ab58dff5986485782f8d66394f9d3a154ea82b7
  • Loading branch information
Gergo Ertli authored and facebook-github-bot committed Nov 20, 2024
1 parent a925604 commit eb05e53
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
70 changes: 70 additions & 0 deletions desktop/plugins/public/network/__tests__/datastorage.node.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

import {createDataSource} from 'flipper-plugin';
import {RequestDataDB} from '../RequestDataDB';
import {createRequestFromRequestInfo} from '../index';

import {decodeBody} from '../utils';

import {Request} from '../types';
import {Base64} from 'js-base64';

// IndexedDB polyfill
global.structuredClone = (val) => (val ? JSON.parse(JSON.stringify(val)) : val);
require('fake-indexeddb/auto');

const firstRequest = {
id: '17',
timestamp: 1234567890,
method: 'GET',
url: 'https://fbflipper.com/',
headers: [{key: 'Content-Type', value: 'text/plain'}],
data: undefined,
};

const secondRequest = {
id: '17',
timestamp: 1234567890,
method: 'GET', // same as before
url: 'https://fallback.fbflipper.com/', // changed from before
headers: [{key: 'Content-Type', value: 'text/plain'}],
data: Base64.encode('hello world'), // new field (not stored in DataSource)
};

test('DataSource handles updates', () => {
const requests = createDataSource<Request, 'id'>([], {
key: 'id',
indices: [['method'], ['status']],
});
const customColumns = [];

requests.upsert(createRequestFromRequestInfo(firstRequest, customColumns));
requests.upsert(createRequestFromRequestInfo(secondRequest, customColumns));

const result = requests.getById('17');
expect(result?.method).toEqual('GET');
expect(result?.url).toEqual('https://fallback.fbflipper.com/');
});

test('RequestDataDB handles updates', async () => {
const db = new RequestDataDB();

db.storeRequestData(
firstRequest.id,
decodeBody(firstRequest.headers, firstRequest.data),
);
db.storeRequestData(
secondRequest.id,
decodeBody(secondRequest.headers, secondRequest.data),
);

const result = await db.getRequestData('17');
expect(result).toEqual('hello world');
});
13 changes: 5 additions & 8 deletions desktop/plugins/public/network/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,10 @@ export function plugin(client: PluginClient<Events, Methods>) {
});

client.onMessage('newRequest', (data) => {
// Some network stacks may send duplicate data, so we filter them out.
if (requests.has(data.id)) {
console.warn(`Ignoring duplicate request with id ${data.id}:`, data);
} else {
requests.append(createRequestFromRequestInfo(data, customColumns.get()));
db.storeRequestData(data.id, decodeBody(data.headers, data.data));
}
// Network stacks might emit newRequest multiple times with incremental
// data, hence the upsert.
requests.upsert(createRequestFromRequestInfo(data, customColumns.get()));
db.storeRequestData(data.id, decodeBody(data.headers, data.data));
});

function storeResponse(response: ResponseInfo) {
Expand Down Expand Up @@ -542,7 +539,7 @@ function showCustomColumnDialog(
renderReactRoot((unmount) => <CustomColumnDialog unmount={unmount} />);
}

function createRequestFromRequestInfo(
export function createRequestFromRequestInfo(
data: RequestInfo,
customColumns: CustomColumnConfig[],
): Request {
Expand Down

0 comments on commit eb05e53

Please sign in to comment.