mirror of
https://github.com/LukeHagar/firecamp.git
synced 2025-12-09 04:19:49 +00:00
feat(platform): firecamp agent manager is now working as expeted
- firecamp extension agent is improved to manage error - firecamp agent selector ui will show download extension button if the extension is not instelled
This commit is contained in:
@@ -40,7 +40,7 @@
|
|||||||
"node": ">=10"
|
"node": ">=10"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@firecamp/rest-executor": "1.0.2",
|
"@firecamp/rest-executor": "^1.0.3",
|
||||||
"@firecamp/types": "^0.0.11",
|
"@firecamp/types": "^0.0.11",
|
||||||
"@firecamp/utils": "0.0.10"
|
"@firecamp/utils": "0.0.10"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -8,11 +8,16 @@ import RestExecutor from '@firecamp/rest-executor/dist/esm';
|
|||||||
const restExecutors: { [key: TId]: RestExecutor } = {};
|
const restExecutors: { [key: TId]: RestExecutor } = {};
|
||||||
|
|
||||||
// Request operations to handle
|
// Request operations to handle
|
||||||
enum ERequestOperation {
|
enum EReqEvent {
|
||||||
Send = 'send',
|
Send = 'send',
|
||||||
Cancel = 'cancel',
|
Cancel = 'cancel',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ExtResponse = {
|
||||||
|
error: any,
|
||||||
|
response: IRestResponse
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send the REST request to execute to the extension via
|
* Send the REST request to execute to the extension via
|
||||||
* message passing API
|
* message passing API
|
||||||
@@ -23,62 +28,40 @@ enum ERequestOperation {
|
|||||||
export const send = (request: IRest): Promise<IRestResponse> => {
|
export const send = (request: IRest): Promise<IRestResponse> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
chrome.runtime.sendMessage(
|
chrome.runtime.sendMessage(
|
||||||
process.env.CHROME_APP_ID,
|
process.env.FIRECAMP_EXTENSION_AGENT_ID,
|
||||||
{
|
{
|
||||||
requestOperation: ERequestOperation.Send,
|
requestOperation: EReqEvent.Send,
|
||||||
request,
|
request,
|
||||||
requestId: request._meta.id,
|
requestId: request._meta.id,
|
||||||
},
|
},
|
||||||
(response) => {
|
(result: ExtResponse) => {
|
||||||
// Reject if found any error in message passing
|
|
||||||
if (chrome.runtime.lastError) reject(chrome.runtime.lastError.message);
|
|
||||||
|
|
||||||
resolve(response);
|
if(result?.response) return resolve(result.response);
|
||||||
|
if(result?.error) return reject(result.error);
|
||||||
|
|
||||||
|
// reject if found any error in message passing
|
||||||
|
// console.log(chrome.runtime.lastError) // { message: ""}
|
||||||
|
if (chrome.runtime.lastError) return reject(chrome.runtime.lastError.message);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
export const ping = (ping: string= "ping"): Promise<string> => {
|
||||||
* Register the chrome message listener, which handles REST request send and cancel operation
|
return new Promise((resolve, reject) => {
|
||||||
*/
|
chrome.runtime.sendMessage(
|
||||||
if (_misc.firecampAgent() === EFirecampAgent.web) {
|
process.env.FIRECAMP_EXTENSION_AGENT_ID,
|
||||||
chrome?.runtime?.onMessageExternal?.addListener(async function (
|
ping,
|
||||||
{
|
(pong: string) => {
|
||||||
requestOperation,
|
if(pong === "pong") return resolve(pong);
|
||||||
request,
|
// reject if found any error in message passing
|
||||||
requestId,
|
if (chrome.runtime.lastError) return reject(chrome.runtime.lastError.message);
|
||||||
}: {
|
return reject("pong not received");
|
||||||
requestOperation: ERequestOperation;
|
|
||||||
request: IRest;
|
|
||||||
requestId: TId;
|
|
||||||
},
|
|
||||||
sender,
|
|
||||||
sendResponse
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
switch (requestOperation) {
|
|
||||||
case ERequestOperation.Send:
|
|
||||||
restExecutors[request._meta.id] = new RestExecutor();
|
|
||||||
|
|
||||||
const response = await restExecutors[request._meta.id].send(request);
|
|
||||||
|
|
||||||
delete restExecutors[requestId];
|
|
||||||
|
|
||||||
sendResponse(response);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ERequestOperation.Cancel:
|
|
||||||
restExecutors[requestId].cancel();
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
);
|
||||||
sendResponse(error);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop running Rest request
|
* Stop running Rest request
|
||||||
@@ -89,18 +72,16 @@ if (_misc.firecampAgent() === EFirecampAgent.web) {
|
|||||||
export const cancel = async (requestId: string): Promise<void> => {
|
export const cancel = async (requestId: string): Promise<void> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
chrome.runtime.sendMessage(
|
chrome.runtime.sendMessage(
|
||||||
process.env.CHROME_APP_ID,
|
process.env.FIRECAMP_EXTENSION_AGENT_ID,
|
||||||
{
|
{
|
||||||
requestOperation: ERequestOperation.Cancel,
|
requestOperation: EReqEvent.Cancel,
|
||||||
requestId,
|
requestId,
|
||||||
},
|
},
|
||||||
(response) => {
|
(result) => {
|
||||||
// Reject if found any error in message passing
|
// Reject if found any error in message passing
|
||||||
if (chrome.runtime.lastError) reject(chrome.runtime.lastError.message);
|
if (chrome.runtime.lastError) reject(chrome.runtime.lastError.message);
|
||||||
|
|
||||||
delete restExecutors[requestId];
|
delete restExecutors[requestId];
|
||||||
|
resolve(result);
|
||||||
resolve(response);
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ declare global {
|
|||||||
}
|
}
|
||||||
namespace NodeJS {
|
namespace NodeJS {
|
||||||
interface ProcessEnv {
|
interface ProcessEnv {
|
||||||
CHROME_APP_ID: string;
|
FIRECAMP_EXTENSION_AGENT_ID: string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import axios from 'axios';
|
||||||
import {
|
import {
|
||||||
EFirecampAgent,
|
EFirecampAgent,
|
||||||
ERestBodyTypes,
|
ERestBodyTypes,
|
||||||
@@ -6,9 +7,8 @@ import {
|
|||||||
TId,
|
TId,
|
||||||
} from '@firecamp/types';
|
} from '@firecamp/types';
|
||||||
import RestExecutor from '@firecamp/rest-executor/dist/esm';
|
import RestExecutor from '@firecamp/rest-executor/dist/esm';
|
||||||
import axios from 'axios';
|
|
||||||
import { _object } from '@firecamp/utils';
|
|
||||||
import parseBody from '@firecamp/rest-executor/dist/esm/helpers/body';
|
import parseBody from '@firecamp/rest-executor/dist/esm/helpers/body';
|
||||||
|
import { _object } from '@firecamp/utils';
|
||||||
import * as extension from './chrome';
|
import * as extension from './chrome';
|
||||||
|
|
||||||
const restExecutors: { [key: TId]: RestExecutor } = {};
|
const restExecutors: { [key: TId]: RestExecutor } = {};
|
||||||
@@ -90,3 +90,8 @@ export const cancel = async (
|
|||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export const pingExtension = (): Promise<string> => {
|
||||||
|
return extension.ping();
|
||||||
|
};
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { FC } from 'react';
|
import { FC, useEffect } from 'react';
|
||||||
import cx from 'classnames';
|
import cx from 'classnames';
|
||||||
import { VscInfo } from '@react-icons/all-files/vsc/VscInfo';
|
import { VscInfo } from '@react-icons/all-files/vsc/VscInfo';
|
||||||
import { EFirecampAgent } from '@firecamp/types';
|
import { EFirecampAgent } from '@firecamp/types';
|
||||||
@@ -21,14 +21,20 @@ const agentNamesMap = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const FcAgentSelector: FC<any> = () => {
|
const FcAgentSelector: FC<any> = () => {
|
||||||
let { agent, changeFirecampAgent } = usePlatformStore(
|
let { agent, isExtAgentInstalled, changeFirecampAgent, checkExtAgentIntalled } = usePlatformStore(
|
||||||
(s: IPlatformStore) => ({
|
(s: IPlatformStore) => ({
|
||||||
agent: s.meta.agent,
|
agent: s.meta.agent,
|
||||||
|
isExtAgentInstalled: s.meta.isExtAgentInstalled,
|
||||||
changeFirecampAgent: s.changeFirecampAgent,
|
changeFirecampAgent: s.changeFirecampAgent,
|
||||||
|
checkExtAgentIntalled: s.checkExtAgentIntalled
|
||||||
}),
|
}),
|
||||||
shallow
|
shallow
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(()=> {
|
||||||
|
checkExtAgentIntalled();
|
||||||
|
}, []);
|
||||||
|
|
||||||
let _onSelectAgent = (firecampAgent: EFirecampAgent) => {
|
let _onSelectAgent = (firecampAgent: EFirecampAgent) => {
|
||||||
changeFirecampAgent(firecampAgent);
|
changeFirecampAgent(firecampAgent);
|
||||||
};
|
};
|
||||||
@@ -56,22 +62,28 @@ const FcAgentSelector: FC<any> = () => {
|
|||||||
name={agentNamesMap[EFirecampAgent.proxy]}
|
name={agentNamesMap[EFirecampAgent.proxy]}
|
||||||
isSelected={agent == EFirecampAgent.proxy}
|
isSelected={agent == EFirecampAgent.proxy}
|
||||||
className={`mt-4 mb-2`}
|
className={`mt-4 mb-2`}
|
||||||
description={`Send Rest requests via Firecamp's <a href="">secure cloud servers</a>.`}
|
description={`Send rest requests via Firecamp's <a href="">secure cloud servers</a>.`}
|
||||||
onSelect={() => _onSelectAgent(EFirecampAgent.proxy)}
|
onSelect={() => _onSelectAgent(EFirecampAgent.proxy)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<AgentItem
|
<AgentItem
|
||||||
name={agentNamesMap[EFirecampAgent.extension]}
|
name={agentNamesMap[EFirecampAgent.extension]}
|
||||||
|
className={`mb-2`}
|
||||||
isSelected={agent == EFirecampAgent.extension}
|
isSelected={agent == EFirecampAgent.extension}
|
||||||
description={`Send Rest requests via Firecamp's browser extension.`}
|
description={`Send rest requests via Firecamp's browser extension.`}
|
||||||
onSelect={() => _onSelectAgent(EFirecampAgent.extension)}
|
onSelect={() => _onSelectAgent(EFirecampAgent.extension)}
|
||||||
>
|
>
|
||||||
<Button
|
{
|
||||||
text="Download Firecamp Extension"
|
!isExtAgentInstalled
|
||||||
size={EButtonSize.Medium}
|
? (
|
||||||
color={EButtonColor.Primary}
|
<Button
|
||||||
className="!w-full !min-w-full mt-2 mb-4"
|
text="Download Firecamp Extension"
|
||||||
/>
|
size={EButtonSize.Medium}
|
||||||
|
color={EButtonColor.Primary}
|
||||||
|
className="!w-full !min-w-full mt-2 mb-4"
|
||||||
|
/>
|
||||||
|
) : <></>
|
||||||
|
}
|
||||||
</AgentItem>
|
</AgentItem>
|
||||||
|
|
||||||
<AgentItem
|
<AgentItem
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import create from 'zustand';
|
import create from 'zustand';
|
||||||
import { EFirecampAgent, IOrganization } from '@firecamp/types';
|
import { EFirecampAgent, IOrganization } from '@firecamp/types';
|
||||||
import { _misc } from '@firecamp/utils';
|
import { _misc } from '@firecamp/utils';
|
||||||
|
import * as executor from "@firecamp/agent-manager";
|
||||||
import { DefaultTheme } from '../types';
|
import { DefaultTheme } from '../types';
|
||||||
|
|
||||||
export enum EPlatformScope {
|
export enum EPlatformScope {
|
||||||
@@ -27,6 +28,7 @@ const initialState = {
|
|||||||
appInfo: {},
|
appInfo: {},
|
||||||
meta: {
|
meta: {
|
||||||
agent: firecampAgent,
|
agent: firecampAgent,
|
||||||
|
isExtAgentInstalled: false
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -37,7 +39,7 @@ export interface IPlatformStore {
|
|||||||
organization?: Partial<IOrganization> | null;
|
organization?: Partial<IOrganization> | null;
|
||||||
switchingOrg: Partial<IOrganization> | null;
|
switchingOrg: Partial<IOrganization> | null;
|
||||||
appInfo: { [k: string]: any };
|
appInfo: { [k: string]: any };
|
||||||
meta: { agent: EFirecampAgent; [K: string]: any };
|
meta: { agent: EFirecampAgent; isExtAgentInstalled:boolean, [k: string]: any };
|
||||||
|
|
||||||
setClientId: (id: string) => void;
|
setClientId: (id: string) => void;
|
||||||
setOrg: (org: IOrganization) => void;
|
setOrg: (org: IOrganization) => void;
|
||||||
@@ -46,6 +48,7 @@ export interface IPlatformStore {
|
|||||||
updateAppInfo: (appInfo: any) => void;
|
updateAppInfo: (appInfo: any) => void;
|
||||||
changeFirecampAgent: (agent: EFirecampAgent) => void;
|
changeFirecampAgent: (agent: EFirecampAgent) => void;
|
||||||
getFirecampAgent: () => EFirecampAgent;
|
getFirecampAgent: () => EFirecampAgent;
|
||||||
|
checkExtAgentIntalled: ()=> void;
|
||||||
|
|
||||||
updateTheme: (theme: any) => void;
|
updateTheme: (theme: any) => void;
|
||||||
|
|
||||||
@@ -91,6 +94,17 @@ export const usePlatformStore = create<IPlatformStore>((set, get) => ({
|
|||||||
getFirecampAgent: (): EFirecampAgent =>
|
getFirecampAgent: (): EFirecampAgent =>
|
||||||
get().meta.agent || EFirecampAgent.proxy,
|
get().meta.agent || EFirecampAgent.proxy,
|
||||||
|
|
||||||
|
checkExtAgentIntalled: async()=> {
|
||||||
|
executor
|
||||||
|
.pingExtension()
|
||||||
|
.then(res=> {
|
||||||
|
set(s=> ({ meta: { ...s.meta, isExtAgentInstalled: res=="pong"? true: false }}))
|
||||||
|
})
|
||||||
|
.catch(e=> {
|
||||||
|
set(s=> ({ meta: { ...s.meta, isExtAgentInstalled: false }}))
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
// Theme
|
// Theme
|
||||||
updateTheme: async (theme = DefaultTheme) => {
|
updateTheme: async (theme = DefaultTheme) => {
|
||||||
set((s) => ({ theme }));
|
set((s) => ({ theme }));
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ exports.env = {
|
|||||||
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
|
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
|
||||||
FIRECAMP_API_HOST: JSON.stringify(process.env.FIRECAMP_API_HOST),
|
FIRECAMP_API_HOST: JSON.stringify(process.env.FIRECAMP_API_HOST),
|
||||||
FIRECAMP_PROXY_API_HOST: JSON.stringify(process.env.FIRECAMP_PROXY_API_HOST),
|
FIRECAMP_PROXY_API_HOST: JSON.stringify(process.env.FIRECAMP_PROXY_API_HOST),
|
||||||
CHROME_APP_ID: JSON.stringify(process.env.CHROME_APP_ID),
|
FIRECAMP_EXTENSION_AGENT_ID: JSON.stringify(process.env.FIRECAMP_EXTENSION_AGENT_ID),
|
||||||
APP_VERSION: JSON.stringify(metadata.version),
|
APP_VERSION: JSON.stringify(metadata.version),
|
||||||
APP_FORMAT: JSON.stringify(process.env.APP_FORMAT),
|
APP_FORMAT: JSON.stringify(process.env.APP_FORMAT),
|
||||||
SENTRY_DSN: JSON.stringify(process.env.SENTRY_DSN),
|
SENTRY_DSN: JSON.stringify(process.env.SENTRY_DSN),
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ module.exports = {
|
|||||||
new webpack.DefinePlugin({
|
new webpack.DefinePlugin({
|
||||||
'process.env': {
|
'process.env': {
|
||||||
...env,
|
...env,
|
||||||
CHROME_APP_ID: JSON.stringify(process.env.CHROME_APP_ID),
|
FIRECAMP_EXTENSION_AGENT_ID: JSON.stringify(process.env.FIRECAMP_EXTENSION_AGENT_ID),
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
// new BundleAnalyzerPlugin(),
|
// new BundleAnalyzerPlugin(),
|
||||||
|
|||||||
Reference in New Issue
Block a user