• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1315496247

03 Jun 2024 07:49AM UTC coverage: 83.437% (-16.5%) from 99.964%
1315496247

Pull #4434

gitlab-ci

mzedel
chore: aligned snapshots with updated mui version

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4434: chore: Bump the mui group with 3 updates

4476 of 6391 branches covered (70.04%)

8488 of 10173 relevant lines covered (83.44%)

140.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.92
/src/js/utils/sockethook.js
1
// Copyright 2022 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import { useCallback, useEffect, useRef, useState } from 'react';
15

16
import msgpack5 from 'msgpack5';
17
import Cookies from 'universal-cookie';
18

19
import { apiUrl } from '../api/general-api';
20
import { TIMEOUTS } from '../constants/appConstants';
21
import { DEVICE_MESSAGE_PROTOCOLS as MessageProtocols, DEVICE_MESSAGE_TYPES as MessageTypes } from '../constants/deviceConstants';
22

23
const cookies = new Cookies();
15✔
24

25
const MessagePack = msgpack5();
15✔
26

27
export const byteArrayToString = body => String.fromCharCode(...body);
15✔
28

29
export const blobToString = blob =>
15✔
30
  new Promise(resolve => {
×
31
    let fr = new FileReader();
×
32
    fr.onload = () => {
×
33
      resolve(fr.result);
×
34
    };
35
    fr.readAsArrayBuffer(blob);
×
36
  });
37

38
export const useSession = ({ onClose, onHealthCheckFailed, onMessageReceived, onNotify, onOpen, token }) => {
15✔
39
  const [sessionId, setSessionId] = useState();
6✔
40
  const healthcheckTimeout = useRef();
6✔
41
  const socketRef = useRef();
6✔
42
  const sendMessage = useCallback(({ typ, body, props }) => {
6✔
43
    if (!socketRef.current) {
4!
44
      return;
4✔
45
    }
46
    const proto_header = { proto: MessageProtocols.Shell, typ, sid: socketRef.current.sessionId, props };
×
47
    const encodedData = MessagePack.encode({ hdr: proto_header, body });
×
48
    socketRef.current.send(encodedData);
×
49
  }, []);
50

51
  const close = useCallback(() => {
6✔
52
    if (!socketRef.current || socketRef.current?.readyState !== WebSocket.OPEN) {
5!
53
      return;
5✔
54
    }
55
    sendMessage({ typ: MessageTypes.Stop, body: {}, props: {} });
×
56
    socketRef.current.close();
×
57
    setSessionId();
×
58
  }, [sendMessage]);
59

60
  const healthcheckFailed = useCallback(() => {
6✔
61
    onHealthCheckFailed();
×
62
    close();
×
63
  }, [close, onHealthCheckFailed]);
64

65
  const onSocketMessage = useCallback(
6✔
66
    event =>
67
      blobToString(event.data).then(data => {
×
68
        const {
69
          hdr: { props = {}, proto, sid, typ },
×
70
          body
71
        } = MessagePack.decode(data);
×
72
        if (proto !== MessageProtocols.Shell) {
×
73
          return;
×
74
        }
75
        switch (typ) {
×
76
          case MessageTypes.New: {
77
            if (props.status == WebSocket.CLOSING) {
×
78
              onNotify(`Error: ${byteArrayToString(body)}`);
×
79
              setSessionId();
×
80
              return close();
×
81
            } else {
82
              clearTimeout(healthcheckTimeout.current);
×
83
              healthcheckTimeout.current = setTimeout(healthcheckFailed, 65 * TIMEOUTS.oneSecond);
×
84
              socketRef.current.sessionId = sid;
×
85
              return setSessionId(sid);
×
86
            }
87
          }
88
          case MessageTypes.Shell:
89
            return onMessageReceived(body);
×
90
          case MessageTypes.Stop:
91
            return close();
×
92
          case MessageTypes.Ping: {
93
            if (healthcheckTimeout.current) {
×
94
              clearTimeout(healthcheckTimeout.current);
×
95
            }
96
            sendMessage({ typ: MessageTypes.Pong });
×
97
            //
98
            const timeout = parseInt(props.timeout);
×
99
            if (timeout > 0) {
×
100
              healthcheckTimeout.current = setTimeout(healthcheckFailed, timeout * TIMEOUTS.oneSecond);
×
101
            }
102
            return;
×
103
          }
104
          default:
105
            break;
×
106
        }
107
      }),
108
    [close, healthcheckFailed, onMessageReceived, onNotify, sendMessage]
109
  );
110

111
  const onSocketError = useCallback(
6✔
112
    error => {
113
      onNotify(`WebSocket error: ${error.message}`);
×
114
      close();
×
115
      clearTimeout(healthcheckTimeout.current);
×
116
    },
117
    [close, onNotify]
118
  );
119

120
  const onSocketOpen = useCallback(() => {
6✔
121
    sendMessage({ typ: MessageTypes.New, props: {} });
×
122
    onOpen(true);
×
123
  }, [onOpen, sendMessage]);
124

125
  const onSocketClose = useCallback(
6✔
126
    e => {
127
      console.log('closing');
×
128
      onClose(e);
×
129
      close();
×
130
    },
131
    [close, onClose]
132
  );
133

134
  const connect = useCallback(
6✔
135
    deviceId => {
136
      const uri = `wss://${window.location.host}${apiUrl.v1}/deviceconnect/devices/${deviceId}/connect`;
×
137
      setSessionId();
×
138
      cookies.set('JWT', token, { path: '/', secure: true, sameSite: 'strict', maxAge: 5 });
×
139
      try {
×
140
        socketRef.current = new WebSocket(uri);
×
141
      } catch (error) {
142
        console.log(error);
×
143
      }
144
    },
145
    [token]
146
  );
147

148
  useEffect(() => {
6✔
149
    if (!socketRef.current) {
4!
150
      return;
4✔
151
    }
152

153
    socketRef.current.addEventListener('close', onSocketClose);
×
154
    socketRef.current.addEventListener('error', onSocketError);
×
155
    socketRef.current.addEventListener('message', onSocketMessage);
×
156
    socketRef.current.addEventListener('open', onSocketOpen);
×
157

158
    return () => {
×
159
      socketRef.current.removeEventListener('close', onSocketClose);
×
160
      socketRef.current.removeEventListener('error', onSocketError);
×
161
      socketRef.current.removeEventListener('message', onSocketMessage);
×
162
      socketRef.current.removeEventListener('open', onSocketOpen);
×
163
    };
164
  }, [onSocketClose, onSocketError, onSocketMessage, onSocketOpen, socketRef.current?.readyState]);
165

166
  return [connect, sendMessage, close, socketRef.current?.readyState ?? WebSocket.CLOSED, sessionId];
6✔
167
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc