@project-chip/matter.js 0.0.0 → 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.
Files changed (64) hide show
  1. package/README.md +11 -0
  2. package/dist/matter.d.ts +22 -0
  3. package/dist/matter.js +22 -0
  4. package/dist/schema/BitmapSchema.d.ts +42 -16
  5. package/dist/schema/BitmapSchema.js +32 -12
  6. package/dist/schema/Schema.d.ts +10 -1
  7. package/dist/schema/Schema.js +11 -6
  8. package/dist/spec/Specifications.d.ts +14 -0
  9. package/dist/spec/Specifications.js +6 -0
  10. package/dist/tlv/TlvAny.d.ts +13 -0
  11. package/dist/tlv/TlvAny.js +67 -0
  12. package/dist/tlv/TlvArray.d.ts +29 -0
  13. package/dist/tlv/TlvArray.js +47 -0
  14. package/dist/tlv/TlvBoolean.d.ts +18 -0
  15. package/dist/tlv/TlvBoolean.js +23 -0
  16. package/dist/tlv/TlvCodec.d.ts +98 -0
  17. package/dist/tlv/TlvCodec.js +244 -0
  18. package/dist/tlv/TlvNullable.d.ts +20 -0
  19. package/dist/tlv/TlvNullable.js +32 -0
  20. package/dist/tlv/TlvNumber.d.ts +46 -0
  21. package/dist/tlv/TlvNumber.js +76 -0
  22. package/dist/tlv/TlvObject.d.ts +56 -0
  23. package/dist/tlv/TlvObject.js +73 -0
  24. package/dist/tlv/TlvSchema.d.ts +67 -0
  25. package/dist/tlv/TlvSchema.js +84 -0
  26. package/dist/tlv/TlvString.d.ts +38 -0
  27. package/dist/tlv/TlvString.js +52 -0
  28. package/dist/tlv/TlvVoid.d.ts +17 -0
  29. package/dist/tlv/TlvVoid.js +22 -0
  30. package/dist/tlv/TlvWrapper.d.ts +16 -0
  31. package/dist/tlv/TlvWrapper.js +23 -0
  32. package/dist/util/ByteArray.d.ts +30 -0
  33. package/dist/util/ByteArray.js +45 -0
  34. package/dist/util/DataReader.d.ts +29 -0
  35. package/dist/util/DataReader.js +65 -0
  36. package/dist/util/DataWriter.d.ts +26 -0
  37. package/dist/util/DataWriter.js +98 -0
  38. package/dist/util/Number.d.ts +23 -0
  39. package/dist/util/Number.js +39 -0
  40. package/dist/util/Type.d.ts +9 -0
  41. package/dist/util/Type.js +6 -0
  42. package/dist-cjs/matter.js +38 -0
  43. package/dist-cjs/schema/BitmapSchema.js +55 -0
  44. package/dist-cjs/schema/Schema.js +26 -0
  45. package/dist-cjs/spec/Specifications.js +7 -0
  46. package/dist-cjs/tlv/TlvAny.js +71 -0
  47. package/dist-cjs/tlv/TlvArray.js +52 -0
  48. package/dist-cjs/tlv/TlvBoolean.js +27 -0
  49. package/dist-cjs/tlv/TlvCodec.js +248 -0
  50. package/dist-cjs/tlv/TlvNullable.js +37 -0
  51. package/dist-cjs/tlv/TlvNumber.js +83 -0
  52. package/dist-cjs/tlv/TlvObject.js +81 -0
  53. package/dist-cjs/tlv/TlvSchema.js +92 -0
  54. package/dist-cjs/tlv/TlvString.js +56 -0
  55. package/dist-cjs/tlv/TlvVoid.js +26 -0
  56. package/dist-cjs/tlv/TlvWrapper.js +27 -0
  57. package/dist-cjs/util/ByteArray.js +48 -0
  58. package/dist-cjs/util/DataReader.js +69 -0
  59. package/dist-cjs/util/DataWriter.js +102 -0
  60. package/dist-cjs/util/Number.js +46 -0
  61. package/dist-cjs/util/Type.js +7 -0
  62. package/package.json +19 -13
  63. package/dist/index.d.ts +0 -0
  64. package/dist/index.js +0 -1
@@ -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.0",
3
+ "version": "0.0.2",
4
4
  "description": "Matter protocol in pure js",
5
5
  "keywords": [
6
6
  "iot",
@@ -23,21 +23,27 @@
23
23
  "url": "https://github.com/project-chip/matter.js.git"
24
24
  },
25
25
  "scripts": {
26
- "clean": "rm -rf dist",
27
- "build": "npm run clean; tsc",
28
- "test": "TZ=utc mocha"
26
+ "clean": "rm -rf dist; rm -rf dist-cjs",
27
+ "build": "npm run clean; tsc --project tsconfig.dist.json; tsc --project tsconfig.dist-cjs.json",
28
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
29
+ "test-web": "karma start karma.conf.cjs"
29
30
  },
30
31
  "devDependencies": {
31
- "@types/assert": "^1.5.6",
32
- "@types/mocha": "^9.1.1",
33
- "assert": "^2.0.0",
34
- "mocha": "^10.0.0",
35
- "ts-node": "^10.9.1",
36
- "typescript": "^4.5.5"
32
+ "@types/jasmine": "^4.3.0",
33
+ "jest": "^29.3.1",
34
+ "karma": "^6.4.1",
35
+ "karma-chrome-launcher": "^3.1.1",
36
+ "karma-coverage": "^2.2.0",
37
+ "karma-jasmine": "^5.1.0",
38
+ "karma-typescript": "^5.5.3",
39
+ "ts-jest": "^29.0.3",
40
+ "typescript": "^4.9.3"
37
41
  },
38
42
  "files": [
39
- "dist/**/*"
43
+ "dist/**/*",
44
+ "dist-cjs/**/*"
40
45
  ],
41
- "main": "dist/index.js",
42
- "types": "dist/index.d.ts"
46
+ "main": "dist-cjs/matter.js",
47
+ "module": "dist/matter.js",
48
+ "types": "dist/matter.d.ts"
43
49
  }
package/dist/index.d.ts DELETED
File without changes
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
- "use strict";