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

mendersoftware / gui / 1057188406

01 Nov 2023 04:24AM UTC coverage: 82.824% (-17.1%) from 99.964%
1057188406

Pull #4134

gitlab-ci

web-flow
chore: Bump uuid from 9.0.0 to 9.0.1

Bumps [uuid](https://github.com/uuidjs/uuid) from 9.0.0 to 9.0.1.
- [Changelog](https://github.com/uuidjs/uuid/blob/main/CHANGELOG.md)
- [Commits](https://github.com/uuidjs/uuid/compare/v9.0.0...v9.0.1)

---
updated-dependencies:
- dependency-name: uuid
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #4134: chore: Bump uuid from 9.0.0 to 9.0.1

4349 of 6284 branches covered (0.0%)

8313 of 10037 relevant lines covered (82.82%)

200.97 hits per line

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

26.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, 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✔
27
  new Promise(resolve => {
×
28
    let fr = new FileReader();
×
29
    fr.onload = () => {
×
30
      resolve(fr.result);
×
31
    };
32
    fr.readAsArrayBuffer(blob);
×
33
  });
34

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

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

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

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

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

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

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

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

132
  const connect = useCallback(
6✔
133
    deviceId => {
134
      const uri = `wss://${window.location.host}${apiUrl.v1}/deviceconnect/devices/${deviceId}/connect`;
×
135
      setSessionId();
×
136
      try {
×
137
        socketRef.current = new WebSocket(uri);
×
138
        socketRef.current.addEventListener('close', onSocketClose);
×
139
        socketRef.current.addEventListener('error', onSocketError);
×
140
        socketRef.current.addEventListener('message', onSocketMessage);
×
141
        socketRef.current.addEventListener('open', onSocketOpen);
×
142
      } catch (error) {
143
        console.log(error);
×
144
      }
145
      return () => {
×
146
        socketRef.current.removeEventListener('close', onSocketClose);
×
147
        socketRef.current.removeEventListener('error', onSocketError);
×
148
        socketRef.current.removeEventListener('message', onSocketMessage);
×
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];
6✔
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