@project-chip/matter.js 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.BitmapSchema = exports.BitFieldEnum = exports.BitField = exports.BitFlag = void 0;
9
+ const Schema_js_1 = require("./Schema.js");
10
+ const BitRange = (type, offset, length) => ({ type, mask: ((1 << length) - 1) << offset, offset });
11
+ const BitFlag = (offset) => BitRange(0 /* BitRangeType.Flag */, offset, 1);
12
+ exports.BitFlag = BitFlag;
13
+ const BitField = (offset, length) => BitRange(1 /* BitRangeType.Number */, offset, length);
14
+ exports.BitField = BitField;
15
+ const BitFieldEnum = (offset, length) => BitRange(2 /* BitRangeType.Enum */, offset, length);
16
+ exports.BitFieldEnum = BitFieldEnum;
17
+ class BitmapSchemaInternal extends Schema_js_1.Schema {
18
+ constructor(bitSchemas) {
19
+ super();
20
+ this.bitSchemas = bitSchemas;
21
+ // TODO: validate that bitSchemas is coherent
22
+ }
23
+ encodeInternal(value) {
24
+ let result = 0;
25
+ for (const name in this.bitSchemas) {
26
+ const { type, offset, mask } = this.bitSchemas[name];
27
+ switch (type) {
28
+ case 0 /* BitRangeType.Flag */:
29
+ if (value[name])
30
+ result |= mask;
31
+ break;
32
+ case 2 /* BitRangeType.Enum */:
33
+ case 1 /* BitRangeType.Number */:
34
+ result |= value[name] << offset;
35
+ }
36
+ }
37
+ return result;
38
+ }
39
+ decodeInternal(bitmap) {
40
+ const result = {};
41
+ for (const name in this.bitSchemas) {
42
+ const { type, offset, mask } = this.bitSchemas[name];
43
+ if (type === 0 /* BitRangeType.Flag */) {
44
+ result[name] = (bitmap & mask) !== 0;
45
+ }
46
+ else {
47
+ result[name] = (bitmap & mask) >> offset;
48
+ }
49
+ }
50
+ return result;
51
+ }
52
+ }
53
+ /** Declares a bitmap schema by indicating the bit position and their names. */
54
+ const BitmapSchema = (bitSchemas) => new BitmapSchemaInternal(bitSchemas);
55
+ exports.BitmapSchema = BitmapSchema;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.Schema = void 0;
9
+ /** Define a schema to encode / decode convert type T to type E. */
10
+ class Schema {
11
+ /** Encodes the value using the schema. */
12
+ encode(value) {
13
+ this.validate(value);
14
+ return this.encodeInternal(value);
15
+ }
16
+ /** Decodes the encoded data using the schema. */
17
+ decode(encoded) {
18
+ const result = this.decodeInternal(encoded);
19
+ this.validate(result);
20
+ return result;
21
+ }
22
+ /** Optional validator that can be used to enforce constraints on the data before encoding / after decoding. */
23
+ validate(_value) {
24
+ }
25
+ }
26
+ exports.Schema = Schema;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvAny = exports.AnySchema = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ class AnySchema extends TlvSchema_js_1.TlvSchema {
11
+ encodeTlvInternal(writer, tlvStream, tagAssigned) {
12
+ tlvStream.forEach(({ tag, typeLength, value }) => {
13
+ if (tagAssigned !== undefined) {
14
+ // Assign the tag to the 1st TLV element in the stream
15
+ tag = tagAssigned;
16
+ tagAssigned = undefined;
17
+ }
18
+ switch (typeLength.type) {
19
+ case 20 /* TlvType.Null */:
20
+ case 8 /* TlvType.Boolean */:
21
+ case 22 /* TlvType.Array */:
22
+ case 21 /* TlvType.Structure */:
23
+ case 23 /* TlvType.List */:
24
+ case 24 /* TlvType.EndOfContainer */:
25
+ writer.writeTag(typeLength, tagAssigned !== null && tagAssigned !== void 0 ? tagAssigned : tag);
26
+ break;
27
+ case 4 /* TlvType.UnsignedInt */:
28
+ case 0 /* TlvType.SignedInt */:
29
+ case 10 /* TlvType.Float */:
30
+ case 12 /* TlvType.Utf8String */:
31
+ case 16 /* TlvType.ByteString */:
32
+ writer.writeTag(typeLength, tagAssigned !== null && tagAssigned !== void 0 ? tagAssigned : tag);
33
+ writer.writePrimitive(typeLength, value);
34
+ break;
35
+ }
36
+ });
37
+ }
38
+ decodeTlvInternalValue(reader, typeLength) {
39
+ return this.decodeTlvValueRec(reader, typeLength, new Array());
40
+ }
41
+ decodeTlvValueRec(reader, typeLength, tlvStream, tag) {
42
+ switch (typeLength.type) {
43
+ case 20 /* TlvType.Null */:
44
+ case 8 /* TlvType.Boolean */:
45
+ case 4 /* TlvType.UnsignedInt */:
46
+ case 0 /* TlvType.SignedInt */:
47
+ case 10 /* TlvType.Float */:
48
+ case 12 /* TlvType.Utf8String */:
49
+ case 16 /* TlvType.ByteString */:
50
+ tlvStream.push({ tag, typeLength, value: reader.readPrimitive(typeLength) });
51
+ break;
52
+ case 22 /* TlvType.Array */:
53
+ case 21 /* TlvType.Structure */:
54
+ case 23 /* TlvType.List */:
55
+ tlvStream.push({ tag, typeLength });
56
+ while (true) {
57
+ const { tag: elementTag, typeLength: typeLengthElement } = reader.readTagType();
58
+ this.decodeTlvValueRec(reader, typeLengthElement, tlvStream, elementTag);
59
+ if (typeLengthElement.type === 24 /* TlvType.EndOfContainer */)
60
+ break;
61
+ }
62
+ break;
63
+ case 24 /* TlvType.EndOfContainer */:
64
+ tlvStream.push({ tag, typeLength });
65
+ break;
66
+ }
67
+ return tlvStream;
68
+ }
69
+ }
70
+ exports.AnySchema = AnySchema;
71
+ exports.TlvAny = new AnySchema();
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvArray = exports.ArraySchema = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ /**
11
+ * Schema to encode an array or string in TLV.
12
+ *
13
+ * @see {@link MatterCoreSpecificationV1_0} § A.11.2 and A.11.4
14
+ */
15
+ class ArraySchema extends TlvSchema_js_1.TlvSchema {
16
+ constructor(elementSchema, minLength = 0, maxLength = 1024) {
17
+ super();
18
+ this.elementSchema = elementSchema;
19
+ this.minLength = minLength;
20
+ this.maxLength = maxLength;
21
+ }
22
+ encodeTlvInternal(writer, value, tag = {}) {
23
+ writer.writeTag({ type: 22 /* TlvType.Array */ }, tag);
24
+ value.forEach(element => this.elementSchema.encodeTlvInternal(writer, element));
25
+ writer.writeTag({ type: 24 /* TlvType.EndOfContainer */ });
26
+ }
27
+ decodeTlvInternalValue(reader, typeLength) {
28
+ if (typeLength.type !== 22 /* TlvType.Array */)
29
+ throw new Error(`Unexpected type ${typeLength.type}.`);
30
+ const result = new Array();
31
+ while (true) {
32
+ const { tag: elementTag, typeLength: elementTypeLength } = reader.readTagType();
33
+ if (elementTag !== undefined)
34
+ throw new Error("Array element tags should be anonymous.");
35
+ if (elementTypeLength.type === 24 /* TlvType.EndOfContainer */)
36
+ break;
37
+ result.push(this.elementSchema.decodeTlvInternalValue(reader, elementTypeLength));
38
+ }
39
+ this.validate(result);
40
+ return result;
41
+ }
42
+ validate({ length }) {
43
+ if (length > this.maxLength)
44
+ throw new Error(`Array is too long: ${length}, max ${this.maxLength}.`);
45
+ if (length < this.minLength)
46
+ throw new Error(`Array is too short: ${length}, min ${this.minLength}.`);
47
+ }
48
+ }
49
+ exports.ArraySchema = ArraySchema;
50
+ /** Array TLV schema. */
51
+ const TlvArray = (elementSchema, { minLength, maxLength, length } = {}) => new ArraySchema(elementSchema, length !== null && length !== void 0 ? length : minLength, length !== null && length !== void 0 ? length : maxLength);
52
+ exports.TlvArray = TlvArray;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvBoolean = exports.BooleanSchema = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ /**
11
+ * Schema to encode a boolean in TLV.
12
+ *
13
+ * @see {@link MatterCoreSpecificationV1_0} § A.11.3
14
+ */
15
+ class BooleanSchema extends TlvSchema_js_1.TlvSchema {
16
+ encodeTlvInternal(writer, value, tag = {}) {
17
+ writer.writeTag({ type: 8 /* TlvType.Boolean */, value }, tag);
18
+ }
19
+ decodeTlvInternalValue(_reader, typeLength) {
20
+ if (typeLength.type !== 8 /* TlvType.Boolean */)
21
+ throw new Error(`Unexpected type ${typeLength.type}.`);
22
+ return typeLength.value;
23
+ }
24
+ }
25
+ exports.BooleanSchema = BooleanSchema;
26
+ /** Boolean TLV schema. */
27
+ exports.TlvBoolean = new BooleanSchema();
@@ -0,0 +1,248 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvCodec = void 0;
9
+ const Number_js_1 = require("../util/Number.js");
10
+ const BitmapSchema_js_1 = require("../schema/BitmapSchema.js");
11
+ /**
12
+ * Schema of the ControlByte.
13
+ *
14
+ * @see {@link MatterCoreSpecificationV1_0} § A.7.2
15
+ */
16
+ const ControlByteSchema = (0, BitmapSchema_js_1.BitmapSchema)({
17
+ typeLength: (0, BitmapSchema_js_1.BitField)(0, 5),
18
+ tagControl: (0, BitmapSchema_js_1.BitFieldEnum)(5, 3),
19
+ });
20
+ /** {@link MatterCoreSpecificationV1_0} § 2.5.2 and § A.8.3 */
21
+ const MATTER_COMMON_PROFILE = 0x00000000;
22
+ class TlvCodec {
23
+ static getUIntTlvLength(value) {
24
+ if (value <= Number_js_1.UINT8_MAX) {
25
+ return 0 /* TlvLength.OneByte */;
26
+ }
27
+ else if (value <= Number_js_1.UINT16_MAX) {
28
+ return 1 /* TlvLength.TwoBytes */;
29
+ }
30
+ else if (value <= Number_js_1.UINT32_MAX) {
31
+ return 2 /* TlvLength.FourBytes */;
32
+ }
33
+ else {
34
+ return 3 /* TlvLength.EightBytes */;
35
+ }
36
+ }
37
+ static getIntTlvLength(value) {
38
+ if (value > 0) {
39
+ if (value <= Number_js_1.INT8_MAX) {
40
+ return 0 /* TlvLength.OneByte */;
41
+ }
42
+ else if (value <= Number_js_1.INT16_MAX) {
43
+ return 1 /* TlvLength.TwoBytes */;
44
+ }
45
+ else if (value <= Number_js_1.INT32_MAX) {
46
+ return 2 /* TlvLength.FourBytes */;
47
+ }
48
+ else {
49
+ return 3 /* TlvLength.EightBytes */;
50
+ }
51
+ }
52
+ else {
53
+ if (value >= Number_js_1.INT8_MIN) {
54
+ return 0 /* TlvLength.OneByte */;
55
+ }
56
+ else if (value >= Number_js_1.INT16_MIN) {
57
+ return 1 /* TlvLength.TwoBytes */;
58
+ }
59
+ else if (value >= Number_js_1.INT32_MIN) {
60
+ return 2 /* TlvLength.FourBytes */;
61
+ }
62
+ else {
63
+ return 3 /* TlvLength.EightBytes */;
64
+ }
65
+ }
66
+ }
67
+ /** @see {@link MatterCoreSpecificationV1_0} § A.7 */
68
+ static readTagType(reader) {
69
+ const { tagControl, typeLength } = ControlByteSchema.decode(reader.readUInt8());
70
+ return { tag: this.readTag(reader, tagControl), typeLength: this.parseTypeLength(typeLength) };
71
+ }
72
+ static readTag(reader, tagControl) {
73
+ switch (tagControl) {
74
+ case 0 /* TagControl.Anonymous */:
75
+ return undefined;
76
+ case 1 /* TagControl.ContextSpecific */:
77
+ return { id: reader.readUInt8() };
78
+ case 2 /* TagControl.CommonProfile16 */:
79
+ return { profile: MATTER_COMMON_PROFILE, id: reader.readUInt16() };
80
+ case 3 /* TagControl.CommonProfile32 */:
81
+ return { profile: MATTER_COMMON_PROFILE, id: reader.readUInt32() };
82
+ case 4 /* TagControl.ImplicitProfile16 */:
83
+ case 5 /* TagControl.ImplicitProfile32 */:
84
+ throw new Error(`Unsupported implicit profile ${tagControl}`);
85
+ case 6 /* TagControl.FullyQualified48 */:
86
+ return { profile: reader.readUInt32(), id: reader.readUInt16() };
87
+ case 6 /* TagControl.FullyQualified48 */:
88
+ return { profile: reader.readUInt32(), id: reader.readUInt32() };
89
+ default:
90
+ throw new Error(`Unexpected tagControl ${tagControl}`);
91
+ }
92
+ }
93
+ static parseTypeLength(typeLength) {
94
+ const length = (typeLength & 0x03);
95
+ const type = typeLength & 0xFC;
96
+ switch (type) {
97
+ case 12 /* TlvType.Utf8String */:
98
+ case 16 /* TlvType.ByteString */:
99
+ case 0 /* TlvType.SignedInt */:
100
+ case 4 /* TlvType.UnsignedInt */:
101
+ return { type, length };
102
+ case 8 /* TlvType.Boolean */:
103
+ switch (length) {
104
+ case 0 /* TlvLength.OneByte */: return { type, value: false };
105
+ case 1 /* TlvLength.TwoBytes */: return { type, value: true };
106
+ case 2 /* TlvLength.FourBytes */: return { type: 10 /* TlvType.Float */, length };
107
+ case 3 /* TlvLength.EightBytes */: return { type: 10 /* TlvType.Float */, length };
108
+ }
109
+ default:
110
+ return { type: typeLength };
111
+ }
112
+ }
113
+ static readPrimitive(reader, typeLength) {
114
+ switch (typeLength.type) {
115
+ case 0 /* TlvType.SignedInt */:
116
+ switch (typeLength.length) {
117
+ case 0 /* TlvLength.OneByte */: return reader.readInt8();
118
+ case 1 /* TlvLength.TwoBytes */: return reader.readInt16();
119
+ case 2 /* TlvLength.FourBytes */: return reader.readInt32();
120
+ case 3 /* TlvLength.EightBytes */: return reader.readInt64();
121
+ }
122
+ case 4 /* TlvType.UnsignedInt */:
123
+ switch (typeLength.length) {
124
+ case 0 /* TlvLength.OneByte */: return reader.readUInt8();
125
+ case 1 /* TlvLength.TwoBytes */: return reader.readUInt16();
126
+ case 2 /* TlvLength.FourBytes */: return reader.readUInt32();
127
+ case 3 /* TlvLength.EightBytes */: return reader.readUInt64();
128
+ }
129
+ case 10 /* TlvType.Float */:
130
+ switch (typeLength.length) {
131
+ case 2 /* TlvLength.FourBytes */: return reader.readFloat();
132
+ case 3 /* TlvLength.EightBytes */: return reader.readDouble();
133
+ }
134
+ case 12 /* TlvType.Utf8String */:
135
+ switch (typeLength.length) {
136
+ case 0 /* TlvLength.OneByte */: return reader.readUtf8String(reader.readUInt8());
137
+ case 1 /* TlvLength.TwoBytes */: return reader.readUtf8String(reader.readUInt16());
138
+ case 2 /* TlvLength.FourBytes */: return reader.readUtf8String(reader.readUInt32());
139
+ case 3 /* TlvLength.EightBytes */: return reader.readUtf8String(Number(reader.readUInt64()));
140
+ }
141
+ case 16 /* TlvType.ByteString */:
142
+ switch (typeLength.length) {
143
+ case 0 /* TlvLength.OneByte */: return reader.readByteArray(reader.readUInt8());
144
+ case 1 /* TlvLength.TwoBytes */: return reader.readByteArray(reader.readUInt16());
145
+ case 2 /* TlvLength.FourBytes */: return reader.readByteArray(reader.readUInt32());
146
+ case 3 /* TlvLength.EightBytes */: return reader.readByteArray(Number(reader.readUInt64()));
147
+ }
148
+ case 8 /* TlvType.Boolean */:
149
+ return typeLength.value;
150
+ case 20 /* TlvType.Null */:
151
+ return null;
152
+ default:
153
+ throw new Error(`Unexpected TLV type ${typeLength.type}`);
154
+ }
155
+ }
156
+ /** @see {@link MatterCoreSpecificationV1_0} § A.7 & A.8 */
157
+ static writeTag(writer, typeLengthValue, { profile, id } = {}) {
158
+ let typeLength;
159
+ switch (typeLengthValue.type) {
160
+ case 12 /* TlvType.Utf8String */:
161
+ case 16 /* TlvType.ByteString */:
162
+ case 0 /* TlvType.SignedInt */:
163
+ case 4 /* TlvType.UnsignedInt */:
164
+ case 10 /* TlvType.Float */:
165
+ typeLength = typeLengthValue.type | typeLengthValue.length;
166
+ break;
167
+ case 8 /* TlvType.Boolean */:
168
+ typeLength = typeLengthValue.type + (typeLengthValue.value ? 1 : 0);
169
+ break;
170
+ default:
171
+ typeLength = typeLengthValue.type;
172
+ }
173
+ if (profile === undefined && id === undefined) {
174
+ writer.writeUInt8(ControlByteSchema.encode({ tagControl: 0 /* TagControl.Anonymous */, typeLength }));
175
+ }
176
+ else if (profile === undefined) {
177
+ if (id === undefined)
178
+ throw new Error("Invalid TLV tag: id should be defined for a context specific tag.");
179
+ writer.writeUInt8(ControlByteSchema.encode({ tagControl: 1 /* TagControl.ContextSpecific */, typeLength }));
180
+ writer.writeUInt8(id);
181
+ }
182
+ else if (profile === MATTER_COMMON_PROFILE) {
183
+ if (id === undefined)
184
+ throw new Error("Invalid TLV tag: id should be defined for a common profile.");
185
+ if ((id & 0xFFFF0000) === 0) {
186
+ writer.writeUInt8(ControlByteSchema.encode({ tagControl: 2 /* TagControl.CommonProfile16 */, typeLength }));
187
+ writer.writeUInt16(id);
188
+ }
189
+ else {
190
+ writer.writeUInt8(ControlByteSchema.encode({ tagControl: 3 /* TagControl.CommonProfile32 */, typeLength }));
191
+ writer.writeUInt32(id);
192
+ }
193
+ }
194
+ else {
195
+ if (id === undefined)
196
+ throw new Error("Invalid TLV tag: id should be defined for a custom profile.");
197
+ if ((id & 0xFFFF0000) === 0) {
198
+ writer.writeUInt8(ControlByteSchema.encode({ tagControl: 6 /* TagControl.FullyQualified48 */, typeLength }));
199
+ writer.writeUInt32(profile);
200
+ writer.writeUInt16(id);
201
+ }
202
+ else {
203
+ writer.writeUInt8(ControlByteSchema.encode({ tagControl: 7 /* TagControl.FullyQualified64 */, typeLength }));
204
+ writer.writeUInt32(profile);
205
+ writer.writeUInt32(id);
206
+ }
207
+ }
208
+ }
209
+ static writePrimitive(writer, typeLength, value) {
210
+ switch (typeLength.type) {
211
+ case 0 /* TlvType.SignedInt */:
212
+ return this.writeUInt(writer, typeLength.length, value);
213
+ case 4 /* TlvType.UnsignedInt */:
214
+ switch (typeLength.length) {
215
+ case 0 /* TlvLength.OneByte */: return writer.writeUInt8(value);
216
+ case 1 /* TlvLength.TwoBytes */: return writer.writeUInt16(value);
217
+ case 2 /* TlvLength.FourBytes */: return writer.writeUInt32(value);
218
+ case 3 /* TlvLength.EightBytes */: return writer.writeUInt64(value);
219
+ }
220
+ case 10 /* TlvType.Float */:
221
+ switch (typeLength.length) {
222
+ case 2 /* TlvLength.FourBytes */: return writer.writeFloat(value);
223
+ case 3 /* TlvLength.EightBytes */: return writer.writeDouble(value);
224
+ }
225
+ case 12 /* TlvType.Utf8String */:
226
+ const string = value;
227
+ this.writeUInt(writer, typeLength.length, string.length);
228
+ return writer.writeUtf8String(string);
229
+ case 16 /* TlvType.ByteString */:
230
+ const byteArray = value;
231
+ this.writeUInt(writer, typeLength.length, byteArray.length);
232
+ return writer.writeByteArray(byteArray);
233
+ case 8 /* TlvType.Boolean */:
234
+ return;
235
+ default:
236
+ throw new Error(`Unexpected TLV type ${typeLength.type}`);
237
+ }
238
+ }
239
+ static writeUInt(writer, length, value) {
240
+ switch (length) {
241
+ case 0 /* TlvLength.OneByte */: return writer.writeInt8(value);
242
+ case 1 /* TlvLength.TwoBytes */: return writer.writeInt16(value);
243
+ case 2 /* TlvLength.FourBytes */: return writer.writeInt32(value);
244
+ case 3 /* TlvLength.EightBytes */: return writer.writeInt64(value);
245
+ }
246
+ }
247
+ }
248
+ exports.TlvCodec = TlvCodec;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvNullable = exports.NullableSchema = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ /**
11
+ * Schema to encode a nulable value in TLV.
12
+ *
13
+ * @see {@link MatterCoreSpecificationV1_0} § A.11.6
14
+ */
15
+ class NullableSchema extends TlvSchema_js_1.TlvSchema {
16
+ constructor(schema) {
17
+ super();
18
+ this.schema = schema;
19
+ }
20
+ encodeTlvInternal(writer, value, tag = {}) {
21
+ if (value === null) {
22
+ writer.writeTag({ type: 20 /* TlvType.Null */ }, tag);
23
+ }
24
+ else {
25
+ this.schema.encodeTlvInternal(writer, value, tag);
26
+ }
27
+ }
28
+ decodeTlvInternalValue(reader, typeLength) {
29
+ if (typeLength.type === 20 /* TlvType.Null */)
30
+ return null;
31
+ return this.schema.decodeTlvInternalValue(reader, typeLength);
32
+ }
33
+ }
34
+ exports.NullableSchema = NullableSchema;
35
+ /** Nullable TLV schema. */
36
+ const TlvNullable = (schema) => new NullableSchema(schema);
37
+ exports.TlvNullable = TlvNullable;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TlvBitmap = exports.TlvEnum = exports.TlvUInt64 = exports.TlvUInt32 = exports.TlvUInt16 = exports.TlvUInt8 = exports.TlvInt64 = exports.TlvInt32 = exports.TlvInt16 = exports.TlvInt8 = exports.TlvDouble = exports.TlvFloat = exports.TlvLongNumberSchema = exports.TlvNumberSchema = exports.TlvNumericSchema = void 0;
4
+ /**
5
+ * @license
6
+ * Copyright 2022 Project CHIP Authors
7
+ * SPDX-License-Identifier: Apache-2.0
8
+ */
9
+ const Number_js_1 = require("../util/Number.js");
10
+ const TlvCodec_js_1 = require("./TlvCodec.js");
11
+ const TlvSchema_js_1 = require("./TlvSchema.js");
12
+ const TlvWrapper_js_1 = require("./TlvWrapper.js");
13
+ const BitmapSchema_js_1 = require("../schema/BitmapSchema.js");
14
+ /**
15
+ * Schema to encode an unsigned integer in TLV.
16
+ *
17
+ * @see {@link MatterCoreSpecificationV1_0} § A.11.1
18
+ */
19
+ class TlvNumericSchema extends TlvSchema_js_1.TlvSchema {
20
+ constructor(type, lengthProvider, min, max) {
21
+ super();
22
+ this.type = type;
23
+ this.lengthProvider = lengthProvider;
24
+ this.min = min;
25
+ this.max = max;
26
+ }
27
+ encodeTlvInternal(writer, value, tag = {}) {
28
+ const typeLength = { type: this.type, length: this.lengthProvider(value) };
29
+ writer.writeTag(typeLength, tag);
30
+ writer.writePrimitive(typeLength, value);
31
+ }
32
+ decodeTlvInternalValue(reader, typeLength) {
33
+ if (typeLength.type !== this.type)
34
+ throw new Error(`Unexpected type ${typeLength.type}, was expecting ${this.type}.`);
35
+ const value = reader.readPrimitive(typeLength);
36
+ this.validate(value);
37
+ return value;
38
+ }
39
+ validate(value) {
40
+ super.validate(value);
41
+ if (this.min !== undefined && value < this.min)
42
+ throw new Error(`Invalid value: ${value} is below the minimum, ${this.min}.`);
43
+ if (this.max !== undefined && value > this.max)
44
+ throw new Error(`Invalid value: ${value} is above the maximum, ${this.max}.`);
45
+ }
46
+ /** Restrict value range. */
47
+ bound({ min, max }) {
48
+ return new TlvNumericSchema(this.type, this.lengthProvider, (0, Number_js_1.maxValue)(min, this.min), (0, Number_js_1.minValue)(max, this.max));
49
+ }
50
+ }
51
+ exports.TlvNumericSchema = TlvNumericSchema;
52
+ class TlvNumberSchema extends TlvNumericSchema {
53
+ constructor(type, lengthProvider, min, max) {
54
+ super(type, lengthProvider, min, max);
55
+ }
56
+ decodeTlvInternalValue(reader, typeLength) {
57
+ const value = super.decodeTlvInternalValue(reader, typeLength);
58
+ return typeof value === "bigint" ? Number(value) : value;
59
+ }
60
+ bound({ min, max }) {
61
+ return new TlvNumberSchema(this.type, this.lengthProvider, (0, Number_js_1.maxValue)(min, this.min), (0, Number_js_1.minValue)(max, this.max));
62
+ }
63
+ }
64
+ exports.TlvNumberSchema = TlvNumberSchema;
65
+ exports.TlvLongNumberSchema = (TlvNumericSchema);
66
+ /** Unsigned integer TLV schema. */
67
+ exports.TlvFloat = new TlvNumberSchema(10 /* TlvType.Float */, _value => 2 /* TlvLength.FourBytes */, Number_js_1.FLOAT32_MIN, Number_js_1.FLOAT32_MAX);
68
+ exports.TlvDouble = new TlvNumberSchema(10 /* TlvType.Float */, _value => 3 /* TlvLength.EightBytes */);
69
+ exports.TlvInt8 = new TlvNumberSchema(0 /* TlvType.SignedInt */, value => TlvCodec_js_1.TlvCodec.getIntTlvLength(value), Number_js_1.INT8_MIN, Number_js_1.INT8_MAX);
70
+ exports.TlvInt16 = new TlvNumberSchema(0 /* TlvType.SignedInt */, value => TlvCodec_js_1.TlvCodec.getIntTlvLength(value), Number_js_1.INT16_MIN, Number_js_1.INT16_MAX);
71
+ exports.TlvInt32 = new TlvNumberSchema(0 /* TlvType.SignedInt */, value => TlvCodec_js_1.TlvCodec.getIntTlvLength(value), Number_js_1.INT32_MIN, Number_js_1.INT32_MAX);
72
+ exports.TlvInt64 = new exports.TlvLongNumberSchema(0 /* TlvType.SignedInt */, value => TlvCodec_js_1.TlvCodec.getIntTlvLength(value), Number_js_1.INT64_MIN, Number_js_1.INT64_MAX);
73
+ exports.TlvUInt8 = new TlvNumberSchema(4 /* TlvType.UnsignedInt */, value => TlvCodec_js_1.TlvCodec.getUIntTlvLength(value), 0, Number_js_1.UINT8_MAX);
74
+ exports.TlvUInt16 = new TlvNumberSchema(4 /* TlvType.UnsignedInt */, value => TlvCodec_js_1.TlvCodec.getUIntTlvLength(value), 0, Number_js_1.UINT16_MAX);
75
+ exports.TlvUInt32 = new TlvNumberSchema(4 /* TlvType.UnsignedInt */, value => TlvCodec_js_1.TlvCodec.getUIntTlvLength(value), 0, Number_js_1.UINT32_MAX);
76
+ exports.TlvUInt64 = new exports.TlvLongNumberSchema(4 /* TlvType.UnsignedInt */, value => TlvCodec_js_1.TlvCodec.getUIntTlvLength(value), 0, Number_js_1.UINT64_MAX);
77
+ const TlvEnum = () => exports.TlvUInt32;
78
+ exports.TlvEnum = TlvEnum;
79
+ const TlvBitmap = (underlyingSchema, bitSchema) => {
80
+ const bitmapSchema = (0, BitmapSchema_js_1.BitmapSchema)(bitSchema);
81
+ return new TlvWrapper_js_1.TlvWrapper(underlyingSchema, (bitmapData) => bitmapSchema.encode(bitmapData), value => bitmapSchema.decode(value));
82
+ };
83
+ exports.TlvBitmap = TlvBitmap;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvOptionalField = exports.TlvField = exports.TlvList = exports.TlvObject = exports.ObjectSchema = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ ;
11
+ /**
12
+ * Schema to encode an object in TLV.
13
+ *
14
+ * @see {@link MatterCoreSpecificationV1_0} § A.5.1 and § A.11.4
15
+ */
16
+ class ObjectSchema extends TlvSchema_js_1.TlvSchema {
17
+ constructor(fieldDefinitions, type = 21 /* TlvType.Structure */) {
18
+ super();
19
+ this.fieldDefinitions = fieldDefinitions;
20
+ this.type = type;
21
+ this.fieldById = new Array();
22
+ for (const name in this.fieldDefinitions) {
23
+ const field = this.fieldDefinitions[name];
24
+ this.fieldById[field.id] = { name, field };
25
+ }
26
+ }
27
+ encodeTlvInternal(writer, value, tag = {}) {
28
+ writer.writeTag({ type: this.type }, tag);
29
+ for (const name in this.fieldDefinitions) {
30
+ const { id, schema, optional: isOptional } = this.fieldDefinitions[name];
31
+ const fieldValue = value[name];
32
+ if (fieldValue === undefined) {
33
+ if (!isOptional)
34
+ throw new Error(`Missing mandatory field ${name}`);
35
+ continue;
36
+ }
37
+ schema.encodeTlvInternal(writer, fieldValue, { id });
38
+ }
39
+ writer.writeTag({ type: 24 /* TlvType.EndOfContainer */ });
40
+ }
41
+ decodeTlvInternalValue(reader, typeLength) {
42
+ if (typeLength.type !== this.type)
43
+ throw new Error(`Unexpected type ${typeLength.type}.`);
44
+ const result = {};
45
+ while (true) {
46
+ const { tag: { profile, id } = {}, typeLength: elementTypeLength } = reader.readTagType();
47
+ if (elementTypeLength.type === 24 /* TlvType.EndOfContainer */)
48
+ break;
49
+ if (profile !== undefined)
50
+ throw new Error("Structure element tags should be context-specific.");
51
+ if (id === undefined)
52
+ throw new Error("Structure element tags should have an id.");
53
+ const fieldName = this.fieldById[id];
54
+ if (fieldName === undefined)
55
+ throw new Error(`Unknown field ${id}`);
56
+ const { field, name } = fieldName;
57
+ result[name] = field.schema.decodeTlvInternalValue(reader, elementTypeLength);
58
+ }
59
+ this.validate(result);
60
+ return result;
61
+ }
62
+ validate(value) {
63
+ for (const name in this.fieldDefinitions) {
64
+ if (!this.fieldDefinitions[name].optional && value[name] === undefined)
65
+ throw new Error(`Missing mandatory field ${name}`);
66
+ }
67
+ }
68
+ }
69
+ exports.ObjectSchema = ObjectSchema;
70
+ /** Object TLV schema. */
71
+ const TlvObject = (fields) => new ObjectSchema(fields, 21 /* TlvType.Structure */);
72
+ exports.TlvObject = TlvObject;
73
+ /** List TLV schema. */
74
+ const TlvList = (fields) => new ObjectSchema(fields, 23 /* TlvType.List */);
75
+ exports.TlvList = TlvList;
76
+ /** Object TLV mandatory field. */
77
+ const TlvField = (id, schema) => ({ id, schema, optional: false });
78
+ exports.TlvField = TlvField;
79
+ /** Object TLV optional field. */
80
+ const TlvOptionalField = (id, schema) => ({ id, schema, optional: true });
81
+ exports.TlvOptionalField = TlvOptionalField;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvByteArrayReader = exports.TlvByteArrayWriter = exports.TlvArrayReader = exports.TlvArrayWriter = exports.TlvSchema = void 0;
9
+ const ByteArray_js_1 = require("../util/ByteArray.js");
10
+ const DataReader_js_1 = require("../util/DataReader.js");
11
+ const DataWriter_js_1 = require("../util/DataWriter.js");
12
+ const Schema_js_1 = require("../schema/Schema.js");
13
+ const TlvCodec_js_1 = require("./TlvCodec.js");
14
+ class TlvSchema extends Schema_js_1.Schema {
15
+ decodeInternal(encoded) {
16
+ return this.decodeTlvInternal(new TlvByteArrayReader(encoded)).value;
17
+ }
18
+ encodeInternal(value) {
19
+ const writer = new TlvByteArrayWriter();
20
+ this.encodeTlvInternal(writer, value);
21
+ return writer.toByteArray();
22
+ }
23
+ encodeTlv(value) {
24
+ const writer = new TlvArrayWriter();
25
+ this.encodeTlvInternal(writer, value);
26
+ return writer.toTlvArray();
27
+ }
28
+ decodeTlv(encoded) {
29
+ return this.decodeTlvInternal(new TlvArrayReader(encoded)).value;
30
+ }
31
+ decodeTlvInternal(reader) {
32
+ const { tag, typeLength } = reader.readTagType();
33
+ return { tag, value: this.decodeTlvInternalValue(reader, typeLength) };
34
+ }
35
+ }
36
+ exports.TlvSchema = TlvSchema;
37
+ class TlvArrayWriter {
38
+ constructor() {
39
+ this.tlvArray = new Array();
40
+ }
41
+ writeTag(typeLength, tag) {
42
+ this.tlvArray.push({ tag, typeLength });
43
+ }
44
+ writePrimitive(_typeLength, value) {
45
+ this.tlvArray[this.tlvArray.length - 1].value = value;
46
+ }
47
+ toTlvArray() {
48
+ return this.tlvArray;
49
+ }
50
+ }
51
+ exports.TlvArrayWriter = TlvArrayWriter;
52
+ class TlvArrayReader {
53
+ constructor(tlvElements) {
54
+ this.tlvElements = tlvElements;
55
+ this.index = -1;
56
+ }
57
+ readTagType() {
58
+ this.index++;
59
+ return this.tlvElements[this.index];
60
+ }
61
+ readPrimitive(_typeLength) {
62
+ return this.tlvElements[this.index].value;
63
+ }
64
+ }
65
+ exports.TlvArrayReader = TlvArrayReader;
66
+ class TlvByteArrayWriter {
67
+ constructor() {
68
+ this.writer = new DataWriter_js_1.DataWriter(ByteArray_js_1.Endian.Little);
69
+ }
70
+ writeTag(typeLength, tag) {
71
+ TlvCodec_js_1.TlvCodec.writeTag(this.writer, typeLength, tag);
72
+ }
73
+ writePrimitive(typeLength, value) {
74
+ TlvCodec_js_1.TlvCodec.writePrimitive(this.writer, typeLength, value);
75
+ }
76
+ toByteArray() {
77
+ return this.writer.toByteArray();
78
+ }
79
+ }
80
+ exports.TlvByteArrayWriter = TlvByteArrayWriter;
81
+ class TlvByteArrayReader {
82
+ constructor(byteArray) {
83
+ this.reader = new DataReader_js_1.DataReader(byteArray, ByteArray_js_1.Endian.Little);
84
+ }
85
+ readTagType() {
86
+ return TlvCodec_js_1.TlvCodec.readTagType(this.reader);
87
+ }
88
+ readPrimitive(typeLength) {
89
+ return TlvCodec_js_1.TlvCodec.readPrimitive(this.reader, typeLength);
90
+ }
91
+ }
92
+ exports.TlvByteArrayReader = TlvByteArrayReader;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvString256max = exports.TlvString64max = exports.TlvString32max = exports.TlvString = exports.TlvByteString = exports.StringSchema = void 0;
9
+ const TlvCodec_js_1 = require("./TlvCodec.js");
10
+ const TlvSchema_js_1 = require("./TlvSchema.js");
11
+ const Number_js_1 = require("../util/Number.js");
12
+ /**
13
+ * Schema to encode an byte string or an Utf8 string in TLV.
14
+ *
15
+ * @see {@link MatterCoreSpecificationV1_0} § A.11.2
16
+ */
17
+ class StringSchema extends TlvSchema_js_1.TlvSchema {
18
+ constructor(type, minLength = 0, maxLength = 1024) {
19
+ super();
20
+ this.type = type;
21
+ this.minLength = minLength;
22
+ this.maxLength = maxLength;
23
+ if (minLength < 0)
24
+ throw new Error("Minimum length should be a positive number.");
25
+ }
26
+ encodeTlvInternal(writer, value, tag = {}) {
27
+ const typeLength = { type: this.type, length: TlvCodec_js_1.TlvCodec.getUIntTlvLength(value.length) };
28
+ writer.writeTag(typeLength, tag);
29
+ writer.writePrimitive(typeLength, value);
30
+ }
31
+ decodeTlvInternalValue(reader, typeLength) {
32
+ if (typeLength.type !== this.type)
33
+ throw new Error(`Unexpected type ${typeLength.type}.`);
34
+ return reader.readPrimitive(typeLength);
35
+ }
36
+ validate({ length }) {
37
+ if (length > this.maxLength)
38
+ throw new Error(`Array is too long: ${length}, max ${this.maxLength}.`);
39
+ if (length < this.minLength)
40
+ throw new Error(`Array is too short: ${length}, min ${this.minLength}.`);
41
+ }
42
+ bound({ minLength, maxLength, length }) {
43
+ return new StringSchema(this.type, length !== null && length !== void 0 ? length : (0, Number_js_1.maxValue)(this.minLength, minLength), length !== null && length !== void 0 ? length : (0, Number_js_1.minValue)(this.maxLength, maxLength));
44
+ }
45
+ }
46
+ exports.StringSchema = StringSchema;
47
+ /** ByteString TLV schema. */
48
+ exports.TlvByteString = new StringSchema(16 /* TlvType.ByteString */);
49
+ /** String TLV schema. */
50
+ exports.TlvString = new StringSchema(12 /* TlvType.Utf8String */);
51
+ /** String TLV schema. */
52
+ exports.TlvString32max = exports.TlvString.bound({ maxLength: 32 });
53
+ /** String TLV schema. */
54
+ exports.TlvString64max = exports.TlvString.bound({ maxLength: 64 });
55
+ /** String TLV schema. */
56
+ exports.TlvString256max = exports.TlvString.bound({ maxLength: 256 });
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvVoid = exports.VoidSchema = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ /**
11
+ * Schema to encode void.
12
+ */
13
+ class VoidSchema extends TlvSchema_js_1.TlvSchema {
14
+ encodeTlvInternal(_writer, _value, _tag) {
15
+ // Nothing to do
16
+ }
17
+ decodeTlv(_encoded) {
18
+ // Nothing to do
19
+ }
20
+ decodeTlvInternalValue(_reader, _typeLength) {
21
+ throw new Error("decodeTlvInternalValue should never be called");
22
+ }
23
+ }
24
+ exports.VoidSchema = VoidSchema;
25
+ /** Void TLV schema. */
26
+ exports.TlvVoid = new VoidSchema();
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvWrapper = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ class TlvWrapper extends TlvSchema_js_1.TlvSchema {
11
+ constructor(underlyingSchema, wrap, unwrap) {
12
+ super();
13
+ this.underlyingSchema = underlyingSchema;
14
+ this.wrap = wrap;
15
+ this.unwrap = unwrap;
16
+ }
17
+ decodeTlvInternalValue(reader, typeLength) {
18
+ return this.unwrap(this.underlyingSchema.decodeTlvInternalValue(reader, typeLength));
19
+ }
20
+ encodeTlvInternal(writer, value, tag) {
21
+ this.underlyingSchema.encodeTlvInternal(writer, this.wrap(value), tag);
22
+ }
23
+ validate(value) {
24
+ this.underlyingSchema.validate(this.wrap(value));
25
+ }
26
+ }
27
+ exports.TlvWrapper = TlvWrapper;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.Endian = exports.ByteArray = void 0;
9
+ Uint8Array.prototype.toHex = function () {
10
+ return this.reduce((result, byte) => result + byte.toString(16).padStart(2, "0"), "");
11
+ };
12
+ Uint8Array.prototype.getDataView = function () {
13
+ return new DataView(this.buffer, this.byteOffset, this.byteLength);
14
+ };
15
+ Uint8Array.prototype.equals = function (other) {
16
+ if (other.length !== this.length)
17
+ return false;
18
+ return this.every((value, index) => other[index] === value);
19
+ };
20
+ Uint8Array.fromHex = function (hexString) {
21
+ var _a;
22
+ if (hexString.length % 2 !== 0)
23
+ throw new Error("Hex string should have an even length.");
24
+ const bytes = (_a = hexString.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map(byteHex => parseInt(byteHex, 16));
25
+ if (bytes === undefined)
26
+ throw new Error("Failed to parse the hex string.");
27
+ return exports.ByteArray.from(bytes);
28
+ };
29
+ Uint8Array.fromString = function (string) {
30
+ return new TextEncoder().encode(string);
31
+ };
32
+ Uint8Array.concat = function (...arrays) {
33
+ let length = 0;
34
+ arrays.forEach(array => length += array.length);
35
+ const result = new Uint8Array(length);
36
+ let offset = 0;
37
+ arrays.forEach(array => {
38
+ result.set(array, offset);
39
+ offset += array.length;
40
+ });
41
+ return result;
42
+ };
43
+ exports.ByteArray = Uint8Array;
44
+ var Endian;
45
+ (function (Endian) {
46
+ Endian[Endian["Little"] = 0] = "Little";
47
+ Endian[Endian["Big"] = 1] = "Big";
48
+ })(Endian = exports.Endian || (exports.Endian = {}));
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.DataReader = void 0;
9
+ const ByteArray_js_1 = require("./ByteArray.js");
10
+ /** Reader that auto-increments its offset after each read. */
11
+ class DataReader {
12
+ constructor(buffer, endian) {
13
+ this.buffer = buffer;
14
+ this.offset = 0;
15
+ this.dataView = buffer.getDataView();
16
+ this.littleEndian = endian === ByteArray_js_1.Endian.Little;
17
+ }
18
+ readUInt8() {
19
+ return this.dataView.getUint8(this.getOffsetAndAdvance(1));
20
+ }
21
+ readUInt16() {
22
+ return this.dataView.getUint16(this.getOffsetAndAdvance(2), this.littleEndian);
23
+ }
24
+ readUInt32() {
25
+ return this.dataView.getUint32(this.getOffsetAndAdvance(4), this.littleEndian);
26
+ }
27
+ readUInt64() {
28
+ return this.dataView.getBigUint64(this.getOffsetAndAdvance(8), this.littleEndian);
29
+ }
30
+ readInt8() {
31
+ return this.dataView.getInt8(this.getOffsetAndAdvance(1));
32
+ }
33
+ readInt16() {
34
+ return this.dataView.getInt16(this.getOffsetAndAdvance(2), this.littleEndian);
35
+ }
36
+ readInt32() {
37
+ return this.dataView.getInt32(this.getOffsetAndAdvance(4), this.littleEndian);
38
+ }
39
+ readInt64() {
40
+ return this.dataView.getBigInt64(this.getOffsetAndAdvance(8), this.littleEndian);
41
+ }
42
+ readFloat() {
43
+ return this.dataView.getFloat32(this.getOffsetAndAdvance(4), this.littleEndian);
44
+ }
45
+ readDouble() {
46
+ return this.dataView.getFloat64(this.getOffsetAndAdvance(8), this.littleEndian);
47
+ }
48
+ readUtf8String(length) {
49
+ const offset = this.getOffsetAndAdvance(length);
50
+ return new TextDecoder().decode(this.buffer.subarray(offset, offset + length));
51
+ }
52
+ readByteArray(length) {
53
+ const offset = this.getOffsetAndAdvance(length);
54
+ const result = this.buffer.subarray(offset, offset + length);
55
+ return result;
56
+ }
57
+ getRemainingBytesCount() {
58
+ return this.dataView.byteLength - this.offset;
59
+ }
60
+ getRemainingBytes() {
61
+ return this.buffer.subarray(this.offset);
62
+ }
63
+ getOffsetAndAdvance(size) {
64
+ const result = this.offset;
65
+ this.offset += size;
66
+ return result;
67
+ }
68
+ }
69
+ exports.DataReader = DataReader;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.DataWriter = void 0;
9
+ const ByteArray_js_1 = require("./ByteArray.js");
10
+ const Number_js_1 = require("./Number.js");
11
+ /** Writer that auto-increments its offset after each write. */
12
+ // TODO: some research should be done to make sure this is most performant implementation.
13
+ class DataWriter {
14
+ constructor(endian) {
15
+ this.length = 0;
16
+ this.chunks = new Array();
17
+ this.littleEndian = endian === ByteArray_js_1.Endian.Little;
18
+ }
19
+ writeUInt8(value) {
20
+ this.chunks.push(new ByteArray_js_1.ByteArray([(0, Number_js_1.toNumber)(value)]));
21
+ this.length += 1;
22
+ }
23
+ writeUInt16(value) {
24
+ const chunk = new ByteArray_js_1.ByteArray(2);
25
+ new DataView(chunk.buffer, 0, 2).setUint16(0, (0, Number_js_1.toNumber)(value), this.littleEndian);
26
+ this.chunks.push(chunk);
27
+ this.length += 2;
28
+ }
29
+ writeUInt32(value) {
30
+ const chunk = new ByteArray_js_1.ByteArray(4);
31
+ new DataView(chunk.buffer, 0, 4).setUint32(0, (0, Number_js_1.toNumber)(value), this.littleEndian);
32
+ this.chunks.push(chunk);
33
+ this.length += 4;
34
+ }
35
+ writeUInt64(value) {
36
+ const chunk = new ByteArray_js_1.ByteArray(8);
37
+ new DataView(chunk.buffer, 0, 8).setBigUint64(0, (0, Number_js_1.toBigInt)(value), this.littleEndian);
38
+ this.chunks.push(chunk);
39
+ this.length += 8;
40
+ }
41
+ writeInt8(value) {
42
+ const chunk = new ByteArray_js_1.ByteArray(1);
43
+ new DataView(chunk.buffer, 0, 1).setInt8(0, (0, Number_js_1.toNumber)(value));
44
+ this.chunks.push(chunk);
45
+ this.length += 1;
46
+ }
47
+ writeInt16(value) {
48
+ const chunk = new ByteArray_js_1.ByteArray(2);
49
+ new DataView(chunk.buffer, 0, 2).setInt16(0, (0, Number_js_1.toNumber)(value), this.littleEndian);
50
+ this.chunks.push(chunk);
51
+ this.length += 2;
52
+ }
53
+ writeInt32(value) {
54
+ const chunk = new ByteArray_js_1.ByteArray(4);
55
+ new DataView(chunk.buffer, 0, 4).setInt32(0, (0, Number_js_1.toNumber)(value), this.littleEndian);
56
+ this.chunks.push(chunk);
57
+ this.length += 4;
58
+ }
59
+ writeInt64(value) {
60
+ const chunk = new ByteArray_js_1.ByteArray(8);
61
+ new DataView(chunk.buffer, 0, 8).setBigInt64(0, (0, Number_js_1.toBigInt)(value), this.littleEndian);
62
+ this.chunks.push(chunk);
63
+ this.length += 8;
64
+ }
65
+ writeFloat(value) {
66
+ const chunk = new ByteArray_js_1.ByteArray(4);
67
+ new DataView(chunk.buffer, 0, 4).setFloat32(0, value, this.littleEndian);
68
+ this.chunks.push(chunk);
69
+ this.length += 4;
70
+ }
71
+ writeDouble(value) {
72
+ const chunk = new ByteArray_js_1.ByteArray(8);
73
+ new DataView(chunk.buffer, 0, 8).setFloat64(0, value, this.littleEndian);
74
+ this.chunks.push(chunk);
75
+ this.length += 8;
76
+ }
77
+ writeUtf8String(value) {
78
+ const bytes = ByteArray_js_1.ByteArray.fromString(value);
79
+ this.chunks.push(bytes);
80
+ this.length += bytes.byteLength;
81
+ }
82
+ writeByteArray(value) {
83
+ this.chunks.push(value);
84
+ this.length += value.byteLength;
85
+ }
86
+ toByteArray() {
87
+ if (this.chunks.length === 0)
88
+ return new ByteArray_js_1.ByteArray(0);
89
+ if (this.chunks.length === 1)
90
+ return this.chunks[0];
91
+ const result = new ByteArray_js_1.ByteArray(this.length);
92
+ let offset = 0;
93
+ this.chunks.forEach(chunck => {
94
+ result.set(chunck, offset);
95
+ offset += chunck.byteLength;
96
+ });
97
+ this.chunks.length = 0;
98
+ this.chunks.push(result);
99
+ return result;
100
+ }
101
+ }
102
+ exports.DataWriter = DataWriter;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.maxValue = exports.minValue = exports.toBigInt = exports.toNumber = exports.FLOAT32_MAX = exports.FLOAT32_MIN = exports.INT64_MAX = exports.INT32_MAX = exports.INT16_MAX = exports.INT8_MAX = exports.INT64_MIN = exports.INT32_MIN = exports.INT16_MIN = exports.INT8_MIN = exports.UINT64_MAX = exports.UINT32_MAX = exports.UINT16_MAX = exports.UINT8_MAX = void 0;
9
+ exports.UINT8_MAX = 0xFF;
10
+ exports.UINT16_MAX = 0xFFFF;
11
+ exports.UINT32_MAX = 0xFFFFFFFF;
12
+ exports.UINT64_MAX = BigInt("18446744073709551615");
13
+ exports.INT8_MIN = -128;
14
+ exports.INT16_MIN = -32768;
15
+ exports.INT32_MIN = -2147483648;
16
+ exports.INT64_MIN = BigInt("-9223372036854775808");
17
+ exports.INT8_MAX = 127;
18
+ exports.INT16_MAX = 32767;
19
+ exports.INT32_MAX = 2147483647;
20
+ exports.INT64_MAX = BigInt("9223372036854775807");
21
+ exports.FLOAT32_MIN = -340282346638528859811704183484516925440.0;
22
+ exports.FLOAT32_MAX = 340282346638528859811704183484516925440.0;
23
+ function toNumber(value) {
24
+ return typeof value === "bigint" ? Number(value) : value;
25
+ }
26
+ exports.toNumber = toNumber;
27
+ function toBigInt(value) {
28
+ return typeof value === "number" ? BigInt(value) : value;
29
+ }
30
+ exports.toBigInt = toBigInt;
31
+ function minValue(a, b) {
32
+ if (a === undefined)
33
+ return b;
34
+ if (b === undefined)
35
+ return a;
36
+ return a < b ? a : b;
37
+ }
38
+ exports.minValue = minValue;
39
+ function maxValue(a, b) {
40
+ if (a === undefined)
41
+ return b;
42
+ if (b === undefined)
43
+ return a;
44
+ return a > b ? a : b;
45
+ }
46
+ exports.maxValue = maxValue;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@project-chip/matter.js",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Matter protocol in pure js",
5
5
  "keywords": [
6
6
  "iot",
@@ -40,7 +40,8 @@
40
40
  "typescript": "^4.9.3"
41
41
  },
42
42
  "files": [
43
- "dist/**/*"
43
+ "dist/**/*",
44
+ "dist-cjs/**/*"
44
45
  ],
45
46
  "main": "dist-cjs/matter.js",
46
47
  "module": "dist/matter.js",