Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3486824
D10938.id36875.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D10938.id36875.diff
View Options
diff --git a/lib/facts/identity-search.js b/lib/facts/identity-search.js
new file mode 100644
--- /dev/null
+++ b/lib/facts/identity-search.js
@@ -0,0 +1,9 @@
+// @flow
+
+import { isDev } from '../utils/dev-utils.js';
+
+const identitySearchURL: string = isDev
+ ? 'wss://identity.staging.commtechnologies.org:51004'
+ : 'wss://identity.commtechnologies.org:51004';
+
+export { identitySearchURL };
diff --git a/lib/identity-search/identity-search-context.js b/lib/identity-search/identity-search-context.js
new file mode 100644
--- /dev/null
+++ b/lib/identity-search/identity-search-context.js
@@ -0,0 +1,169 @@
+// @flow
+
+import invariant from 'invariant';
+import * as React from 'react';
+
+import { identitySearchURL } from '../facts/identity-search.js';
+import { identitySearchHeartbeatTimeout } from '../shared/timeouts.js';
+import type { AuthMessage } from '../types/identity-search/auth-message-types.js';
+import {
+ type IdentitySearchMessageToClient,
+ identitySearchMessageToClientTypes,
+ identitySearchMessageToClientValidator,
+} from '../types/identity-search/messages.js';
+import type { Heartbeat } from '../types/websocket/heartbeat-types.js';
+
+export type IdentitySearchSocketListener = (
+ message: IdentitySearchMessageToClient,
+) => mixed;
+
+type IdentitySearchContextType = {
+ +addListener: (listener: IdentitySearchSocketListener) => void,
+ +removeListener: (listener: IdentitySearchSocketListener) => void,
+ +connected: boolean,
+};
+
+const IdentitySearchContext: React.Context<?IdentitySearchContextType> =
+ React.createContext<?IdentitySearchContextType>();
+
+type Props = {
+ +children: React.Node,
+ +authMessage: ?AuthMessage,
+};
+
+function IdentitySearchProvider(props: Props): React.Node {
+ const { children, authMessage } = props;
+ const [connected, setConnected] = React.useState(false);
+ const listeners = React.useRef<Set<IdentitySearchSocketListener>>(new Set());
+ const socket = React.useRef<?WebSocket>(null);
+ const heartbeatTimeoutID = React.useRef<?TimeoutID>();
+
+ const stopHeartbeatTimeout = React.useCallback(() => {
+ if (heartbeatTimeoutID.current) {
+ clearTimeout(heartbeatTimeoutID.current);
+ heartbeatTimeoutID.current = null;
+ }
+ }, []);
+
+ const resetHeartbeatTimeout = React.useCallback(() => {
+ stopHeartbeatTimeout();
+ heartbeatTimeoutID.current = setTimeout(() => {
+ socket.current?.close();
+ setConnected(false);
+ }, identitySearchHeartbeatTimeout);
+ }, [stopHeartbeatTimeout]);
+
+ React.useEffect(() => {
+ if (connected || !authMessage) {
+ return;
+ }
+
+ const identitySearchSocket = new WebSocket(identitySearchURL);
+
+ identitySearchSocket.onopen = () => {
+ identitySearchSocket.send(JSON.stringify(authMessage));
+ };
+
+ identitySearchSocket.onclose = () => {
+ setConnected(false);
+ };
+
+ identitySearchSocket.onerror = e => {
+ setConnected(false);
+ console.log('Identity Search socket error', e.message);
+ };
+
+ identitySearchSocket.onmessage = (event: MessageEvent) => {
+ if (typeof event.data !== 'string') {
+ console.log('socket received a non-string message');
+ return;
+ }
+ let rawMessage;
+ try {
+ rawMessage = JSON.parse(event.data);
+ } catch (e) {
+ console.log('error while parsing Identity Search message:', e.message);
+ return;
+ }
+
+ if (!identitySearchMessageToClientValidator.is(rawMessage)) {
+ console.log('invalid Identity Search message');
+ return;
+ }
+
+ const message: IdentitySearchMessageToClient = rawMessage;
+
+ resetHeartbeatTimeout();
+
+ for (const listener of listeners.current) {
+ listener(message);
+ }
+
+ if (
+ message.type ===
+ identitySearchMessageToClientTypes.CONNECTION_INITIALIZATION_RESPONSE
+ ) {
+ if (message.status.type === 'Success' && !connected) {
+ setConnected(true);
+ } else if (message.status.type === 'Success' && connected) {
+ console.log(
+ 'received ConnectionInitializationResponse with status: Success for already connected socket',
+ );
+ } else {
+ setConnected(false);
+ console.log(
+ 'creating session with Identity Search error:',
+ message.status.data,
+ );
+ }
+ } else if (
+ message.type === identitySearchMessageToClientTypes.HEARTBEAT
+ ) {
+ const heartbeat: Heartbeat = {
+ type: identitySearchMessageToClientTypes.HEARTBEAT,
+ };
+ socket.current?.send(JSON.stringify(heartbeat));
+ }
+ };
+
+ socket.current = identitySearchSocket;
+ }, [connected, authMessage, resetHeartbeatTimeout, stopHeartbeatTimeout]);
+
+ const addListener = React.useCallback(
+ (listener: IdentitySearchSocketListener) => {
+ listeners.current.add(listener);
+ },
+ [],
+ );
+
+ const removeListener = React.useCallback(
+ (listener: IdentitySearchSocketListener) => {
+ listeners.current.delete(listener);
+ },
+ [],
+ );
+
+ const value: IdentitySearchContextType = React.useMemo(
+ () => ({
+ connected,
+ addListener,
+ removeListener,
+ }),
+ [connected, addListener, removeListener],
+ );
+
+ return (
+ <IdentitySearchContext.Provider value={value}>
+ {children}
+ </IdentitySearchContext.Provider>
+ );
+}
+
+function useIdentitySearch(): IdentitySearchContextType {
+ const context = React.useContext(IdentitySearchContext);
+ invariant(context, 'IdentitySearchContext not found');
+
+ return context;
+}
+
+export { IdentitySearchProvider, useIdentitySearch };
diff --git a/lib/shared/timeouts.js b/lib/shared/timeouts.js
--- a/lib/shared/timeouts.js
+++ b/lib/shared/timeouts.js
@@ -36,3 +36,7 @@
// Time after which the client consider the Tunnelbroker connection
// as unhealthy and chooses to close the socket.
export const tunnelbrokerHeartbeatTimeout = 9000; // in milliseconds
+
+// Time after which the client consider the Identity Search connection
+// as unhealthy and chooses to close the socket.
+export const identitySearchHeartbeatTimeout = 9000; // in milliseconds
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Dec 19, 6:06 AM (21 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2675617
Default Alt Text
D10938.id36875.diff (6 KB)
Attached To
Mode
D10938: [lib] Basic implementation of identity search context provider without message sending
Attached
Detach File
Event Timeline
Log In to Comment