All files / static / packet_conv / lane_created.js

100.00% Branches 14/14
100.00% Lines 99/99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x4
x4
x5
x5
x1
x6
x4
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x2
x1
x2
x2
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x13
x13
x13
x15
x15
x1
x23
x13
x15
x15
x1
x21
x13
x15
x15
x1
x19
x13
x15
x15
x13
x15
x15
x1
x13
x23
x28
x28
x28
x28
x23
x1
x15
x13
































































































/**
 * @file Encoding/decoding functions for lane-created packet
 *
 * @author aKuad
 */

import { typeof_detail } from "../util/typeof_detail.js";
import { LaneInfo } from "./LaneInfo.js";


/**
 * Packet type ID of lane-created packet
 */
export const LANE_CREATED_PACKET_TYPE_ID = 0x31;


/**
 * Create lane-created packet
 *
 * @param {LaneInfo} lane_info LaneInfo to encode
 * @returns {Uint8Array} Encoded packet
 *
 * @throws {TypeError} If `lane_created` is not `LaneInfo`
 */
export function packet_lane_created_encode(lane_info) {
  // Arguments type checking
  if(!(lane_info instanceof LaneInfo)) {
    throw new TypeError(`lane_info must be LaneInfo, but got ${typeof_detail(lane_info)}`);
  }

  return Uint8Array.of(LANE_CREATED_PACKET_TYPE_ID, ...lane_info.to_bytes());
}


/**
 * Unpack lane-created packet
 *
 * Note: About raises, see reference of `is_lane_created_packet`.
 *
 * @param {Uint8Array} raw_packet Encoded packet
 * @returns {LaneInfo} Decoded data - Lane ID, lane name, current volume
 */
export function packet_lane_created_decode(raw_packet) {
  is_lane_created_packet(raw_packet, true);

  return LaneInfo.from_bytes(raw_packet.slice(1));
}


/**
 * Verify the packet is lane_created packet
 *
 * Note: It won't check lane_name field contain non-ascii or control-ascii characters
 *
 * @param {Uint8Array} raw_packet Packet to verify
 * @param {boolean} throw_on_invalid Toggle behavior when packet is invalid, true: raise exception, false: return false
 * @returns {boolean} It is an lane_created: true, otherwise: false (if throw_on_invalid === true, error will be thrown)
 *
 * @throws {TypeError} If `raw_packet` is not `Uint8Array`
 * @throws {RangeError} If `raw_packet` is an empty array
 * @throws {RangeError} If `raw_packet` has not an lane_created packet type ID
 * @throws {RangeError} If `raw_packet` is invalid length as lane_created
 */
export function is_lane_created_packet(raw_packet, throw_on_invalid = false) {
  try {
    // Arguments type checking
    if(!(raw_packet instanceof Uint8Array)) {
      throw new TypeError(`raw_packet must be Uint8Array, but got ${typeof_detail(raw_packet)}`);
    }

    // Packet content availability checking
    if(raw_packet.length === 0) {
      throw new RangeError("Empty array passed");
    }

    // Packet type ID checking
    if(raw_packet[0] !== LANE_CREATED_PACKET_TYPE_ID) {
      throw new RangeError(`It has not an lane_created packet type ID - should be ${LANE_CREATED_PACKET_TYPE_ID}, but got ${raw_packet[0]}`);
    }

    // Packet length checking
    if(raw_packet.length < 6) {
      throw new RangeError(`Too short bytes as lane created packet - expected 6, but got ${raw_packet.length}`);
    }
    if(raw_packet.length > 6) {
      throw new RangeError(`Too long bytes as lane created packet - expected 6, but got ${raw_packet.length}`);
    }

  } catch(e) {
    if(throw_on_invalid) {
      throw e;
    } else {
      return false;
    }
  }

  return true;
}