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

mendersoftware / gui / 1015445845

25 Sep 2023 09:43AM UTC coverage: 82.537% (-17.4%) from 99.964%
1015445845

Pull #4028

gitlab-ci

mzedel
chore: aligned release retrieval with v2 api models

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4028: MEN-6455

4355 of 6315 branches covered (0.0%)

184 of 206 new or added lines in 19 files covered. (89.32%)

1724 existing lines in 164 files now uncovered.

8323 of 10084 relevant lines covered (82.54%)

208.49 hits per line

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

42.31
/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, useRef, useState } from 'react';
15

16
import msgpack5 from 'msgpack5';
17

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

22
const MessagePack = msgpack5();
16✔
23

24
export const byteArrayToString = body => String.fromCharCode(...body);
16✔
25

26
export const blobToString = blob =>
16✔
UNCOV
27
  new Promise(resolve => {
×
UNCOV
28
    let fr = new FileReader();
×
UNCOV
29
    fr.onload = () => {
×
UNCOV
30
      resolve(fr.result);
×
31
    };
UNCOV
32
    fr.readAsArrayBuffer(blob);
×
33
  });
34

35
export const useSession = ({ onClose, onHealthCheckFailed, onMessageReceived, onNotify, onOpen }) => {
16✔
36
  const [sessionId, setSessionId] = useState();
2✔
37
  const healthcheckTimeout = useRef();
2✔
38
  const socketRef = useRef();
2✔
39

40
  const sendMessage = useCallback(({ typ, body, props }) => {
2✔
41
    if (!socketRef.current) {
1!
UNCOV
42
      return;
×
43
    }
44
    const proto_header = { proto: MessageProtocols.Shell, typ, sid: socketRef.current.sessionId, props };
1✔
45
    const encodedData = MessagePack.encode({ hdr: proto_header, body });
1✔
46
    socketRef.current.send(encodedData);
1✔
47
  }, []);
48

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

58
  const healthcheckFailed = useCallback(() => {
2✔
UNCOV
59
    onHealthCheckFailed();
×
UNCOV
60
    close();
×
61
  }, [close, onHealthCheckFailed]);
62

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

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

118
  const onSocketOpen = useCallback(() => {
2✔
UNCOV
119
    sendMessage({ typ: MessageTypes.New, props: {} });
×
UNCOV
120
    onOpen(true);
×
121
  }, [onOpen, sendMessage]);
122

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

132
  const connect = useCallback(
2✔
133
    deviceId => {
134
      const uri = `wss://${window.location.host}${apiUrl.v1}/deviceconnect/devices/${deviceId}/connect`;
1✔
135
      setSessionId();
1✔
136
      try {
1✔
137
        socketRef.current = new WebSocket(uri);
1✔
138
        socketRef.current.addEventListener('close', onSocketClose);
1✔
139
        socketRef.current.addEventListener('error', onSocketError);
1✔
140
        socketRef.current.addEventListener('message', onSocketMessage);
1✔
141
        socketRef.current.addEventListener('open', onSocketOpen);
1✔
142
      } catch (error) {
UNCOV
143
        console.log(error);
×
144
      }
145
      return () => {
1✔
UNCOV
146
        socketRef.current.removeEventListener('close', onSocketClose);
×
UNCOV
147
        socketRef.current.removeEventListener('error', onSocketError);
×
UNCOV
148
        socketRef.current.removeEventListener('message', onSocketMessage);
×
UNCOV
149
        socketRef.current.removeEventListener('open', onSocketOpen);
×
150
      };
151
    },
152
    [onSocketClose, onSocketOpen, onSocketError, onSocketMessage]
153
  );
154

155
  return [connect, sendMessage, close, socketRef.current?.readyState ?? WebSocket.CLOSED, sessionId];
2✔
156
};
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