bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/connection-manager/component.tsx

141 lines
4.8 KiB
TypeScript
Raw Normal View History

import {
ApolloClient, ApolloProvider, InMemoryCache, NormalizedCacheObject, ApolloLink,
} from '@apollo/client';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { createClient } from 'graphql-ws';
import React, { useContext, useEffect } from 'react';
import { LoadingContext } from '/imports/ui/components/common/loading-screen/loading-screen-HOC/component';
import logger from '/imports/startup/client/logger';
import apolloContextHolder from '../../core/graphql/apolloContextHolder/apolloContextHolder';
interface ConnectionManagerProps {
children: React.ReactNode;
}
interface Response {
response: {
returncode: string;
version: string;
apiVersion: string;
bbbVersion: string;
graphqlWebsocketUrl: string;
}
}
const DEFAULT_MAX_MUTATION_PAYLOAD_SIZE = 10485760; // 10MB
const getMaxMutationPayloadSize = () => window.meetingClientSettings?.public?.app?.maxMutationPayloadSize
?? DEFAULT_MAX_MUTATION_PAYLOAD_SIZE;
const estimatePayloadSize = (variables: Record<string, unknown>) => {
const variablesAsString = JSON.stringify(variables);
const variablesAsBlob = new Blob([variablesAsString]);
return variablesAsBlob.size;
};
const payloadSizeCheckLink = new ApolloLink((operation, forward) => {
if (operation.query.definitions.some((def) => 'operation' in def && def.operation === 'mutation')) {
const size = estimatePayloadSize(operation.variables);
const maxPayloadSize = getMaxMutationPayloadSize();
if (size > maxPayloadSize) {
const errorMsg = `Mutation payload is too large: ${size} bytes. ${maxPayloadSize} maximum allowed.`;
logger.warn(errorMsg);
return null;
}
}
2024-04-20 04:34:43 +08:00
// logger.debug(`Valid ${operation.operationName} payload. Following with the query.`);
return forward(operation);
});
const ConnectionManager: React.FC<ConnectionManagerProps> = ({ children }): React.ReactNode => {
const [graphqlUrlApolloClient, setApolloClient] = React.useState<ApolloClient<NormalizedCacheObject> | null>(null);
const [graphqlUrl, setGraphqlUrl] = React.useState<string>('');
const loadingContextInfo = useContext(LoadingContext);
useEffect(() => {
fetch(`https://${window.location.hostname}/bigbluebutton/api`, {
headers: {
'Content-Type': 'application/json',
},
}).then(async (response) => {
const responseJson: Response = await response.json();
setGraphqlUrl(responseJson.response.graphqlWebsocketUrl);
}).catch((error) => {
loadingContextInfo.setLoading(false, '');
throw new Error('Error fetching GraphQL URL: '.concat(error.message || ''));
});
logger.info('Fetching GraphQL URL');
loadingContextInfo.setLoading(true, '1/4');
}, []);
useEffect(() => {
logger.info('Connecting to GraphQL server');
loadingContextInfo.setLoading(true, '2/4');
if (graphqlUrl) {
const urlParams = new URLSearchParams(window.location.search);
const sessionToken = urlParams.get('sessionToken');
if (!sessionToken) {
loadingContextInfo.setLoading(false, '');
throw new Error('Missing session token');
}
sessionStorage.setItem('sessionToken', sessionToken);
let wsLink;
try {
const subscription = createClient({
url: graphqlUrl,
connectionParams: {
headers: {
'X-Session-Token': sessionToken,
},
},
on: {
error: (error) => {
loadingContextInfo.setLoading(false, '');
throw new Error(`Error: on subscription to server: ${JSON.stringify(error)}`);
},
},
});
const graphWsLink = new GraphQLWsLink(
subscription,
);
wsLink = ApolloLink.from([payloadSizeCheckLink, graphWsLink]);
wsLink.setOnError((error) => {
loadingContextInfo.setLoading(false, '');
throw new Error('Error: on apollo connection'.concat(JSON.stringify(error) || ''));
});
apolloContextHolder.setLink(subscription);
} catch (error) {
loadingContextInfo.setLoading(false, '');
throw new Error('Error creating WebSocketLink: '.concat(JSON.stringify(error) || ''));
}
let client;
try {
client = new ApolloClient({
link: wsLink,
cache: new InMemoryCache(),
connectToDevTools: true,
});
setApolloClient(client);
apolloContextHolder.setClient(client);
} catch (error) {
loadingContextInfo.setLoading(false, '');
throw new Error('Error creating Apollo Client: '.concat(JSON.stringify(error) || ''));
}
}
},
[graphqlUrl]);
return (
graphqlUrlApolloClient
? (
<ApolloProvider
client={graphqlUrlApolloClient}
>
{children}
</ApolloProvider>
) : null
);
};
export default ConnectionManager;