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

feat: async parsing of bru files [non-blocking] -- wip #3618

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const StyledWrapper = styled.div`
display: flex;
}
}

.partial-request-overlay {
background: ${(props) => props.theme.bg};
}
`;

export default StyledWrapper;
17 changes: 16 additions & 1 deletion packages/bruno-app/src/components/RequestTabPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ResponsePane from 'components/ResponsePane';
import Welcome from 'components/Welcome';
import { findItemInCollection } from 'utils/collections';
import { updateRequestPaneTabWidth } from 'providers/ReduxStore/slices/tabs';
import { sendRequest } from 'providers/ReduxStore/slices/collections/actions';
import { loadRequest, sendRequest } from 'providers/ReduxStore/slices/collections/actions';
import RequestNotFound from './RequestNotFound';
import QueryUrl from 'components/RequestPane/QueryUrl';
import NetworkError from 'components/ResponsePane/NetworkError';
Expand All @@ -22,6 +22,7 @@ import SecuritySettings from 'components/SecuritySettings';
import FolderSettings from 'components/FolderSettings';
import { getGlobalEnvironmentVariables } from 'utils/collections/index';
import { produce } from 'immer';
import { IconLoader2 } from '@tabler/icons';

const MIN_LEFT_PANE_WIDTH = 300;
const MIN_RIGHT_PANE_WIDTH = 350;
Expand Down Expand Up @@ -170,8 +171,22 @@ const RequestTabPanel = () => {
);
};

const handleLoadRequest = () => {
dispatch(loadRequest({ collectionUid: collection?.uid, pathname: item?.pathname }));
}

return (
<StyledWrapper className={`flex flex-col flex-grow relative ${dragging ? 'dragging' : ''}`}>
{item?.partial ?
<>
<div className='partial-request-overlay absolute top-0 left-0 blur-lg z-10 opacity-60 h-full w-full'>
</div>
<button className="submit btn btn-sm btn-secondary w-fit h-fit z-20 m-auto absolute top-0 bottom-0 left-0 right-0 flex flex-row gap-2" onClick={handleLoadRequest}>
{item?.loading ? `Loading Request` : `Load Request`}
{item?.loading ? <IconLoader2 className="animate-spin" size={18} strokeWidth={1.5} /> : null}
</button>
</>
: null}
<div className="pt-4 pb-3 px-4">
<QueryUrl item={item} collection={collection} handleRun={handleRun} />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import range from 'lodash/range';
import filter from 'lodash/filter';
import classnames from 'classnames';
import { useDrag, useDrop } from 'react-dnd';
import { IconChevronRight, IconDots } from '@tabler/icons';
import { IconChevronRight, IconDots, IconLoader2, IconRefresh } from '@tabler/icons';
import { useSelector, useDispatch } from 'react-redux';
import { addTab, focusTab } from 'providers/ReduxStore/slices/tabs';
import { moveItem, sendRequest } from 'providers/ReduxStore/slices/collections/actions';
Expand Down Expand Up @@ -280,7 +280,15 @@ const CollectionItem = ({ item, collection, searchText }) => {
</div>

<div className="ml-1 flex items-center overflow-hidden">
<RequestMethod item={item} />
{item?.partial && !item?.loading ?
<IconRefresh size={18} className="w-fit mr-2" strokeWidth={1.5} />
: null}
{item?.loading ?
<IconLoader2 className="animate-spin w-fit mr-2" size={18} strokeWidth={1.5} />
: null}
{!item?.partial && !item?.loading ?
<RequestMethod item={item} />
: null}
<span className="item-name" title={item.name}>
{item.name}
</span>
Expand Down
2 changes: 1 addition & 1 deletion packages/bruno-app/src/components/Sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const Sidebar = () => {
Star
</GitHubButton> */}
</div>
<div className="flex flex-grow items-center justify-end text-xs mr-2">v1.36.0</div>
<div className="flex flex-grow items-center justify-end text-xs mr-2">v1.36.1</div>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,4 +1192,12 @@ export const hydrateCollectionWithUiStateSnapshot = (payload) => (dispatch, getS
reject(error);
}
});
};
};

export const loadRequest = ({ collectionUid, pathname }) => (dispatch, getState) => {
return new Promise(async (resolve, reject) => {
const { ipcRenderer } = window;
ipcRenderer.invoke('renderer:load-request-init', { collectionUid, pathname }).then(resolve).catch(reject);
ipcRenderer.invoke('renderer:load-request', { collectionUid, pathname }).then(resolve).catch(reject);
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ export const collectionsSlice = createSlice({
name: directoryName,
collapsed: true,
type: 'folder',
items: []
items: [],
};
currentSubItems.push(childItem);
}
Expand All @@ -1471,6 +1471,8 @@ export const collectionsSlice = createSlice({
currentItem.filename = file.meta.name;
currentItem.pathname = file.meta.pathname;
currentItem.draft = null;
currentItem.partial = file.partial;
currentItem.loading = file.loading;
} else {
currentSubItems.push({
uid: file.data.uid,
Expand All @@ -1480,7 +1482,9 @@ export const collectionsSlice = createSlice({
request: file.data.request,
filename: file.meta.name,
pathname: file.meta.pathname,
draft: null
draft: null,
partial: file.partial,
loading: file.loading
});
}
}
Expand Down
24 changes: 9 additions & 15 deletions packages/bruno-cli/src/runner/prepare-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,18 @@ const mergeScripts = (collection, request, requestTreePath, scriptFlow) => {
}
}

if (combinedPreReqScript.length) {
request.script.req = compact([collectionPreReqScript, ...combinedPreReqScript, request?.script?.req || '']).join(os.EOL);
}
request.script.req = compact([collectionPreReqScript, ...combinedPreReqScript, request?.script?.req || '']).join(os.EOL);

if (combinedPostResScript.length) {
if (scriptFlow === 'sequential') {
request.script.res = compact([collectionPostResScript, ...combinedPostResScript, request?.script?.res || '']).join(os.EOL);
} else {
request.script.res = compact([request?.script?.res || '', ...combinedPostResScript.reverse(), collectionPostResScript]).join(os.EOL);
}
if (scriptFlow === 'sequential') {
request.script.res = compact([collectionPostResScript, ...combinedPostResScript, request?.script?.res || '']).join(os.EOL);
} else {
request.script.res = compact([request?.script?.res || '', ...combinedPostResScript.reverse(), collectionPostResScript]).join(os.EOL);
}

if (combinedTests.length) {
if (scriptFlow === 'sequential') {
request.tests = compact([collectionTests, ...combinedTests, request?.tests || '']).join(os.EOL);
} else {
request.tests = compact([request?.tests || '', ...combinedTests.reverse(), collectionTests]).join(os.EOL);
}
if (scriptFlow === 'sequential') {
request.tests = compact([collectionTests, ...combinedTests, request?.tests || '']).join(os.EOL);
} else {
request.tests = compact([request?.tests || '', ...combinedTests.reverse(), collectionTests]).join(os.EOL);
}
};

Expand Down
55 changes: 24 additions & 31 deletions packages/bruno-electron/src/app/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { setDotEnvVars } = require('../store/process-env');
const { setBrunoConfig } = require('../store/bruno-config');
const EnvironmentSecretsStore = require('../store/env-secrets');
const UiStateSnapshot = require('../store/ui-state-snapshot');
const { getBruFileMeta, hydrateRequestWithUuid } = require('../utils/collection');

const environmentSecretsStore = new EnvironmentSecretsStore();

Expand Down Expand Up @@ -44,28 +45,6 @@ const isCollectionRootBruFile = (pathname, collectionPath) => {
return dirname === collectionPath && basename === 'collection.bru';
};

const hydrateRequestWithUuid = (request, pathname) => {
request.uid = getRequestUid(pathname);

const params = _.get(request, 'request.params', []);
const headers = _.get(request, 'request.headers', []);
const requestVars = _.get(request, 'request.vars.req', []);
const responseVars = _.get(request, 'request.vars.res', []);
const assertions = _.get(request, 'request.assertions', []);
const bodyFormUrlEncoded = _.get(request, 'request.body.formUrlEncoded', []);
const bodyMultipartForm = _.get(request, 'request.body.multipartForm', []);

params.forEach((param) => (param.uid = uuid()));
headers.forEach((header) => (header.uid = uuid()));
requestVars.forEach((variable) => (variable.uid = uuid()));
responseVars.forEach((variable) => (variable.uid = uuid()));
assertions.forEach((assertion) => (assertion.uid = uuid()));
bodyFormUrlEncoded.forEach((param) => (param.uid = uuid()));
bodyMultipartForm.forEach((param) => (param.uid = uuid()));

return request;
};

const hydrateBruCollectionFileWithUuid = (collectionRoot) => {
const params = _.get(collectionRoot, 'request.params', []);
const headers = _.get(collectionRoot, 'request.headers', []);
Expand Down Expand Up @@ -99,7 +78,7 @@ const addEnvironmentFile = async (win, pathname, collectionUid, collectionPath)

let bruContent = fs.readFileSync(pathname, 'utf8');

file.data = bruToEnvJson(bruContent);
file.data = await bruToEnvJson(bruContent);
file.data.name = basename.substring(0, basename.length - 4);
file.data.uid = getRequestUid(pathname);

Expand Down Expand Up @@ -134,7 +113,7 @@ const changeEnvironmentFile = async (win, pathname, collectionUid, collectionPat
};

const bruContent = fs.readFileSync(pathname, 'utf8');
file.data = bruToEnvJson(bruContent);
file.data = await bruToEnvJson(bruContent);
file.data.name = basename.substring(0, basename.length - 4);
file.data.uid = getRequestUid(pathname);
_.each(_.get(file, 'data.variables', []), (variable) => (variable.uid = uuid()));
Expand Down Expand Up @@ -228,7 +207,7 @@ const add = async (win, pathname, collectionUid, collectionPath) => {
try {
let bruContent = fs.readFileSync(pathname, 'utf8');

file.data = collectionBruToJson(bruContent);
file.data = await collectionBruToJson(bruContent);

hydrateBruCollectionFileWithUuid(file.data);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
Expand All @@ -254,7 +233,7 @@ const add = async (win, pathname, collectionUid, collectionPath) => {
try {
let bruContent = fs.readFileSync(pathname, 'utf8');

file.data = collectionBruToJson(bruContent);
file.data = await collectionBruToJson(bruContent);

hydrateBruCollectionFileWithUuid(file.data);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
Expand All @@ -276,11 +255,25 @@ const add = async (win, pathname, collectionUid, collectionPath) => {

try {
let bruContent = fs.readFileSync(pathname, 'utf8');

file.data = bruToJson(bruContent);

const stats = fs.statSync(pathname);
const metaJson = await bruToJson(getBruFileMeta(bruContent), true);
file.data = metaJson;
file.partial = true;
file.loading = false;
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
if (stats.size < 1 * 1024 * 1024) {
file.data = metaJson;
file.partial = false;
file.loading = true;
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
file.data = await bruToJson(bruContent);
file.partial = false;
file.loading = false;
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
}
} catch (err) {
console.error(err);
}
Expand Down Expand Up @@ -357,7 +350,7 @@ const change = async (win, pathname, collectionUid, collectionPath) => {
try {
let bruContent = fs.readFileSync(pathname, 'utf8');

file.data = collectionBruToJson(bruContent);
file.data = await collectionBruToJson(bruContent);
hydrateBruCollectionFileWithUuid(file.data);
win.webContents.send('main:collection-tree-updated', 'change', file);
return;
Expand All @@ -378,7 +371,7 @@ const change = async (win, pathname, collectionUid, collectionPath) => {
};

const bru = fs.readFileSync(pathname, 'utf8');
file.data = bruToJson(bru);
file.data = await bruToJson(bru);

hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'change', file);
Expand Down
Loading
Loading