@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.
- package/README.md +11 -0
- package/dist/matter.d.ts +22 -0
- package/dist/matter.js +22 -0
- package/dist/schema/BitmapSchema.d.ts +42 -16
- package/dist/schema/BitmapSchema.js +32 -12
- package/dist/schema/Schema.d.ts +10 -1
- package/dist/schema/Schema.js +11 -6
- package/dist/spec/Specifications.d.ts +14 -0
- package/dist/spec/Specifications.js +6 -0
- package/dist/tlv/TlvAny.d.ts +13 -0
- package/dist/tlv/TlvAny.js +67 -0
- package/dist/tlv/TlvArray.d.ts +29 -0
- package/dist/tlv/TlvArray.js +47 -0
- package/dist/tlv/TlvBoolean.d.ts +18 -0
- package/dist/tlv/TlvBoolean.js +23 -0
- package/dist/tlv/TlvCodec.d.ts +98 -0
- package/dist/tlv/TlvCodec.js +244 -0
- package/dist/tlv/TlvNullable.d.ts +20 -0
- package/dist/tlv/TlvNullable.js +32 -0
- package/dist/tlv/TlvNumber.d.ts +46 -0
- package/dist/tlv/TlvNumber.js +76 -0
- package/dist/tlv/TlvObject.d.ts +56 -0
- package/dist/tlv/TlvObject.js +73 -0
- package/dist/tlv/TlvSchema.d.ts +67 -0
- package/dist/tlv/TlvSchema.js +84 -0
- package/dist/tlv/TlvString.d.ts +38 -0
- package/dist/tlv/TlvString.js +52 -0
- package/dist/tlv/TlvVoid.d.ts +17 -0
- package/dist/tlv/TlvVoid.js +22 -0
- package/dist/tlv/TlvWrapper.d.ts +16 -0
- package/dist/tlv/TlvWrapper.js +23 -0
- package/dist/util/ByteArray.d.ts +30 -0
- package/dist/util/ByteArray.js +45 -0
- package/dist/util/DataReader.d.ts +29 -0
- package/dist/util/DataReader.js +65 -0
- package/dist/util/DataWriter.d.ts +26 -0
- package/dist/util/DataWriter.js +98 -0
- package/dist/util/Number.d.ts +23 -0
- package/dist/util/Number.js +39 -0
- package/dist/util/Type.d.ts +9 -0
- package/dist/util/Type.js +6 -0
- package/dist-cjs/matter.js +38 -0
- package/dist-cjs/schema/BitmapSchema.js +55 -0
- package/dist-cjs/schema/Schema.js +26 -0
- package/dist-cjs/spec/Specifications.js +7 -0
- package/dist-cjs/tlv/TlvAny.js +71 -0
- package/dist-cjs/tlv/TlvArray.js +52 -0
- package/dist-cjs/tlv/TlvBoolean.js +27 -0
- package/dist-cjs/tlv/TlvCodec.js +248 -0
- package/dist-cjs/tlv/TlvNullable.js +37 -0
- package/dist-cjs/tlv/TlvNumber.js +83 -0
- package/dist-cjs/tlv/TlvObject.js +81 -0
- package/dist-cjs/tlv/TlvSchema.js +92 -0
- package/dist-cjs/tlv/TlvString.js +56 -0
- package/dist-cjs/tlv/TlvVoid.js +26 -0
- package/dist-cjs/tlv/TlvWrapper.js +27 -0
- package/dist-cjs/util/ByteArray.js +48 -0
- package/dist-cjs/util/DataReader.js +69 -0
- package/dist-cjs/util/DataWriter.js +102 -0
- package/dist-cjs/util/Number.js +46 -0
- package/dist-cjs/util/Type.js +7 -0
- package/package.json +19 -13
- package/dist/index.d.ts +0 -0
- package/dist/index.js +0 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { Endian } from "../util/ByteArray.js";
|
|
7
|
+
import { DataReader } from "../util/DataReader.js";
|
|
8
|
+
import { DataWriter } from "../util/DataWriter.js";
|
|
9
|
+
import { Schema } from "../schema/Schema.js";
|
|
10
|
+
import { TlvCodec } from "./TlvCodec.js";
|
|
11
|
+
export class TlvSchema extends Schema {
|
|
12
|
+
decodeInternal(encoded) {
|
|
13
|
+
return this.decodeTlvInternal(new TlvByteArrayReader(encoded)).value;
|
|
14
|
+
}
|
|
15
|
+
encodeInternal(value) {
|
|
16
|
+
const writer = new TlvByteArrayWriter();
|
|
17
|
+
this.encodeTlvInternal(writer, value);
|
|
18
|
+
return writer.toByteArray();
|
|
19
|
+
}
|
|
20
|
+
encodeTlv(value) {
|
|
21
|
+
const writer = new TlvArrayWriter();
|
|
22
|
+
this.encodeTlvInternal(writer, value);
|
|
23
|
+
return writer.toTlvArray();
|
|
24
|
+
}
|
|
25
|
+
decodeTlv(encoded) {
|
|
26
|
+
return this.decodeTlvInternal(new TlvArrayReader(encoded)).value;
|
|
27
|
+
}
|
|
28
|
+
decodeTlvInternal(reader) {
|
|
29
|
+
const { tag, typeLength } = reader.readTagType();
|
|
30
|
+
return { tag, value: this.decodeTlvInternalValue(reader, typeLength) };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class TlvArrayWriter {
|
|
34
|
+
constructor() {
|
|
35
|
+
this.tlvArray = new Array();
|
|
36
|
+
}
|
|
37
|
+
writeTag(typeLength, tag) {
|
|
38
|
+
this.tlvArray.push({ tag, typeLength });
|
|
39
|
+
}
|
|
40
|
+
writePrimitive(_typeLength, value) {
|
|
41
|
+
this.tlvArray[this.tlvArray.length - 1].value = value;
|
|
42
|
+
}
|
|
43
|
+
toTlvArray() {
|
|
44
|
+
return this.tlvArray;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export class TlvArrayReader {
|
|
48
|
+
constructor(tlvElements) {
|
|
49
|
+
this.tlvElements = tlvElements;
|
|
50
|
+
this.index = -1;
|
|
51
|
+
}
|
|
52
|
+
readTagType() {
|
|
53
|
+
this.index++;
|
|
54
|
+
return this.tlvElements[this.index];
|
|
55
|
+
}
|
|
56
|
+
readPrimitive(_typeLength) {
|
|
57
|
+
return this.tlvElements[this.index].value;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export class TlvByteArrayWriter {
|
|
61
|
+
constructor() {
|
|
62
|
+
this.writer = new DataWriter(Endian.Little);
|
|
63
|
+
}
|
|
64
|
+
writeTag(typeLength, tag) {
|
|
65
|
+
TlvCodec.writeTag(this.writer, typeLength, tag);
|
|
66
|
+
}
|
|
67
|
+
writePrimitive(typeLength, value) {
|
|
68
|
+
TlvCodec.writePrimitive(this.writer, typeLength, value);
|
|
69
|
+
}
|
|
70
|
+
toByteArray() {
|
|
71
|
+
return this.writer.toByteArray();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export class TlvByteArrayReader {
|
|
75
|
+
constructor(byteArray) {
|
|
76
|
+
this.reader = new DataReader(byteArray, Endian.Little);
|
|
77
|
+
}
|
|
78
|
+
readTagType() {
|
|
79
|
+
return TlvCodec.readTagType(this.reader);
|
|
80
|
+
}
|
|
81
|
+
readPrimitive(typeLength) {
|
|
82
|
+
return TlvCodec.readPrimitive(this.reader, typeLength);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvType, TlvTag, TlvTypeLength, TlvToPrimitive } from "./TlvCodec.js";
|
|
7
|
+
import { TlvReader, TlvSchema, TlvWriter } from "./TlvSchema.js";
|
|
8
|
+
type LengthConstraints = {
|
|
9
|
+
minLength?: number;
|
|
10
|
+
maxLength?: number;
|
|
11
|
+
length?: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Schema to encode an byte string or an Utf8 string in TLV.
|
|
15
|
+
*
|
|
16
|
+
* @see {@link MatterCoreSpecificationV1_0} § A.11.2
|
|
17
|
+
*/
|
|
18
|
+
export declare class StringSchema<T extends TlvType.ByteString | TlvType.Utf8String> extends TlvSchema<TlvToPrimitive[T]> {
|
|
19
|
+
private type;
|
|
20
|
+
private readonly minLength;
|
|
21
|
+
private readonly maxLength;
|
|
22
|
+
constructor(type: T, minLength?: number, maxLength?: number);
|
|
23
|
+
encodeTlvInternal(writer: TlvWriter, value: TlvToPrimitive[T], tag?: TlvTag): void;
|
|
24
|
+
decodeTlvInternalValue(reader: TlvReader, typeLength: TlvTypeLength): TlvToPrimitive[T];
|
|
25
|
+
validate({ length }: TlvToPrimitive[T]): void;
|
|
26
|
+
bound({ minLength, maxLength, length }: LengthConstraints): StringSchema<T>;
|
|
27
|
+
}
|
|
28
|
+
/** ByteString TLV schema. */
|
|
29
|
+
export declare const TlvByteString: StringSchema<TlvType.ByteString>;
|
|
30
|
+
/** String TLV schema. */
|
|
31
|
+
export declare const TlvString: StringSchema<TlvType.Utf8String>;
|
|
32
|
+
/** String TLV schema. */
|
|
33
|
+
export declare const TlvString32max: StringSchema<TlvType.Utf8String>;
|
|
34
|
+
/** String TLV schema. */
|
|
35
|
+
export declare const TlvString64max: StringSchema<TlvType.Utf8String>;
|
|
36
|
+
/** String TLV schema. */
|
|
37
|
+
export declare const TlvString256max: StringSchema<TlvType.Utf8String>;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvCodec } from "./TlvCodec.js";
|
|
7
|
+
import { TlvSchema } from "./TlvSchema.js";
|
|
8
|
+
import { maxValue, minValue } from "../util/Number.js";
|
|
9
|
+
/**
|
|
10
|
+
* Schema to encode an byte string or an Utf8 string in TLV.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link MatterCoreSpecificationV1_0} § A.11.2
|
|
13
|
+
*/
|
|
14
|
+
export class StringSchema extends TlvSchema {
|
|
15
|
+
constructor(type, minLength = 0, maxLength = 1024) {
|
|
16
|
+
super();
|
|
17
|
+
this.type = type;
|
|
18
|
+
this.minLength = minLength;
|
|
19
|
+
this.maxLength = maxLength;
|
|
20
|
+
if (minLength < 0)
|
|
21
|
+
throw new Error("Minimum length should be a positive number.");
|
|
22
|
+
}
|
|
23
|
+
encodeTlvInternal(writer, value, tag = {}) {
|
|
24
|
+
const typeLength = { type: this.type, length: TlvCodec.getUIntTlvLength(value.length) };
|
|
25
|
+
writer.writeTag(typeLength, tag);
|
|
26
|
+
writer.writePrimitive(typeLength, value);
|
|
27
|
+
}
|
|
28
|
+
decodeTlvInternalValue(reader, typeLength) {
|
|
29
|
+
if (typeLength.type !== this.type)
|
|
30
|
+
throw new Error(`Unexpected type ${typeLength.type}.`);
|
|
31
|
+
return reader.readPrimitive(typeLength);
|
|
32
|
+
}
|
|
33
|
+
validate({ length }) {
|
|
34
|
+
if (length > this.maxLength)
|
|
35
|
+
throw new Error(`Array is too long: ${length}, max ${this.maxLength}.`);
|
|
36
|
+
if (length < this.minLength)
|
|
37
|
+
throw new Error(`Array is too short: ${length}, min ${this.minLength}.`);
|
|
38
|
+
}
|
|
39
|
+
bound({ minLength, maxLength, length }) {
|
|
40
|
+
return new StringSchema(this.type, length !== null && length !== void 0 ? length : maxValue(this.minLength, minLength), length !== null && length !== void 0 ? length : minValue(this.maxLength, maxLength));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** ByteString TLV schema. */
|
|
44
|
+
export const TlvByteString = new StringSchema(16 /* TlvType.ByteString */);
|
|
45
|
+
/** String TLV schema. */
|
|
46
|
+
export const TlvString = new StringSchema(12 /* TlvType.Utf8String */);
|
|
47
|
+
/** String TLV schema. */
|
|
48
|
+
export const TlvString32max = TlvString.bound({ maxLength: 32 });
|
|
49
|
+
/** String TLV schema. */
|
|
50
|
+
export const TlvString64max = TlvString.bound({ maxLength: 64 });
|
|
51
|
+
/** String TLV schema. */
|
|
52
|
+
export const TlvString256max = TlvString.bound({ maxLength: 256 });
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvTag, TlvTypeLength } from "./TlvCodec.js";
|
|
7
|
+
import { TlvReader, TlvSchema, TlvStream, TlvWriter } from "./TlvSchema.js";
|
|
8
|
+
/**
|
|
9
|
+
* Schema to encode void.
|
|
10
|
+
*/
|
|
11
|
+
export declare class VoidSchema extends TlvSchema<void> {
|
|
12
|
+
encodeTlvInternal(_writer: TlvWriter, _value: void, _tag?: TlvTag): void;
|
|
13
|
+
decodeTlv(_encoded: TlvStream): void;
|
|
14
|
+
decodeTlvInternalValue(_reader: TlvReader, _typeLength: TlvTypeLength): void;
|
|
15
|
+
}
|
|
16
|
+
/** Void TLV schema. */
|
|
17
|
+
export declare const TlvVoid: VoidSchema;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvSchema } from "./TlvSchema.js";
|
|
7
|
+
/**
|
|
8
|
+
* Schema to encode void.
|
|
9
|
+
*/
|
|
10
|
+
export class VoidSchema extends TlvSchema {
|
|
11
|
+
encodeTlvInternal(_writer, _value, _tag) {
|
|
12
|
+
// Nothing to do
|
|
13
|
+
}
|
|
14
|
+
decodeTlv(_encoded) {
|
|
15
|
+
// Nothing to do
|
|
16
|
+
}
|
|
17
|
+
decodeTlvInternalValue(_reader, _typeLength) {
|
|
18
|
+
throw new Error("decodeTlvInternalValue should never be called");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** Void TLV schema. */
|
|
22
|
+
export const TlvVoid = new VoidSchema();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvTag, TlvTypeLength } from "./TlvCodec.js";
|
|
7
|
+
import { TlvReader, TlvSchema, TlvWriter } from "./TlvSchema.js";
|
|
8
|
+
export declare class TlvWrapper<O, T> extends TlvSchema<O> {
|
|
9
|
+
private readonly underlyingSchema;
|
|
10
|
+
private readonly wrap;
|
|
11
|
+
private readonly unwrap;
|
|
12
|
+
constructor(underlyingSchema: TlvSchema<T>, wrap: (object: O) => T, unwrap: (value: T) => O);
|
|
13
|
+
decodeTlvInternalValue(reader: TlvReader, typeLength: TlvTypeLength): O;
|
|
14
|
+
encodeTlvInternal(writer: TlvWriter, value: O, tag?: TlvTag | undefined): void;
|
|
15
|
+
validate(value: O): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvSchema } from "./TlvSchema.js";
|
|
7
|
+
export class TlvWrapper extends TlvSchema {
|
|
8
|
+
constructor(underlyingSchema, wrap, unwrap) {
|
|
9
|
+
super();
|
|
10
|
+
this.underlyingSchema = underlyingSchema;
|
|
11
|
+
this.wrap = wrap;
|
|
12
|
+
this.unwrap = unwrap;
|
|
13
|
+
}
|
|
14
|
+
decodeTlvInternalValue(reader, typeLength) {
|
|
15
|
+
return this.unwrap(this.underlyingSchema.decodeTlvInternalValue(reader, typeLength));
|
|
16
|
+
}
|
|
17
|
+
encodeTlvInternal(writer, value, tag) {
|
|
18
|
+
this.underlyingSchema.encodeTlvInternal(writer, this.wrap(value), tag);
|
|
19
|
+
}
|
|
20
|
+
validate(value) {
|
|
21
|
+
this.underlyingSchema.validate(this.wrap(value));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
declare global {
|
|
7
|
+
interface Uint8Array {
|
|
8
|
+
/** Displays as an hex string. */
|
|
9
|
+
toHex(): string;
|
|
10
|
+
/** Gets a {@link DataView} on this array. */
|
|
11
|
+
getDataView(): DataView;
|
|
12
|
+
/** Tests the deep equality of the two arrays. */
|
|
13
|
+
equals(other: Uint8Array): boolean;
|
|
14
|
+
}
|
|
15
|
+
interface Uint8ArrayConstructor {
|
|
16
|
+
/** Gets an {@link ByteArray} from an hex string. */
|
|
17
|
+
fromHex(hexString: string): ByteArray;
|
|
18
|
+
/** Gets an {@link ByteArray} from an hex string. */
|
|
19
|
+
fromString(string: string): ByteArray;
|
|
20
|
+
/** Concats {@link ByteArray}s. */
|
|
21
|
+
concat(...arrays: Uint8Array[]): Uint8Array;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Array of bytes, alias of {@link Uint8Array}. */
|
|
25
|
+
export type ByteArray = Uint8Array;
|
|
26
|
+
export declare const ByteArray: Uint8ArrayConstructor;
|
|
27
|
+
export declare enum Endian {
|
|
28
|
+
Little = 0,
|
|
29
|
+
Big = 1
|
|
30
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
Uint8Array.prototype.toHex = function () {
|
|
7
|
+
return this.reduce((result, byte) => result + byte.toString(16).padStart(2, "0"), "");
|
|
8
|
+
};
|
|
9
|
+
Uint8Array.prototype.getDataView = function () {
|
|
10
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength);
|
|
11
|
+
};
|
|
12
|
+
Uint8Array.prototype.equals = function (other) {
|
|
13
|
+
if (other.length !== this.length)
|
|
14
|
+
return false;
|
|
15
|
+
return this.every((value, index) => other[index] === value);
|
|
16
|
+
};
|
|
17
|
+
Uint8Array.fromHex = function (hexString) {
|
|
18
|
+
var _a;
|
|
19
|
+
if (hexString.length % 2 !== 0)
|
|
20
|
+
throw new Error("Hex string should have an even length.");
|
|
21
|
+
const bytes = (_a = hexString.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map(byteHex => parseInt(byteHex, 16));
|
|
22
|
+
if (bytes === undefined)
|
|
23
|
+
throw new Error("Failed to parse the hex string.");
|
|
24
|
+
return ByteArray.from(bytes);
|
|
25
|
+
};
|
|
26
|
+
Uint8Array.fromString = function (string) {
|
|
27
|
+
return new TextEncoder().encode(string);
|
|
28
|
+
};
|
|
29
|
+
Uint8Array.concat = function (...arrays) {
|
|
30
|
+
let length = 0;
|
|
31
|
+
arrays.forEach(array => length += array.length);
|
|
32
|
+
const result = new Uint8Array(length);
|
|
33
|
+
let offset = 0;
|
|
34
|
+
arrays.forEach(array => {
|
|
35
|
+
result.set(array, offset);
|
|
36
|
+
offset += array.length;
|
|
37
|
+
});
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
export const ByteArray = Uint8Array;
|
|
41
|
+
export var Endian;
|
|
42
|
+
(function (Endian) {
|
|
43
|
+
Endian[Endian["Little"] = 0] = "Little";
|
|
44
|
+
Endian[Endian["Big"] = 1] = "Big";
|
|
45
|
+
})(Endian || (Endian = {}));
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { ByteArray, Endian } from "./ByteArray.js";
|
|
7
|
+
/** Reader that auto-increments its offset after each read. */
|
|
8
|
+
export declare class DataReader<E extends Endian> {
|
|
9
|
+
private readonly buffer;
|
|
10
|
+
private readonly littleEndian;
|
|
11
|
+
private readonly dataView;
|
|
12
|
+
private offset;
|
|
13
|
+
constructor(buffer: ByteArray, endian: E);
|
|
14
|
+
readUInt8(): number;
|
|
15
|
+
readUInt16(): number;
|
|
16
|
+
readUInt32(): number;
|
|
17
|
+
readUInt64(): bigint;
|
|
18
|
+
readInt8(): number;
|
|
19
|
+
readInt16(): number;
|
|
20
|
+
readInt32(): number;
|
|
21
|
+
readInt64(): bigint;
|
|
22
|
+
readFloat(): number;
|
|
23
|
+
readDouble(): number;
|
|
24
|
+
readUtf8String(length: number): string;
|
|
25
|
+
readByteArray(length: number): Uint8Array;
|
|
26
|
+
getRemainingBytesCount(): number;
|
|
27
|
+
getRemainingBytes(): Uint8Array;
|
|
28
|
+
private getOffsetAndAdvance;
|
|
29
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { Endian } from "./ByteArray.js";
|
|
7
|
+
/** Reader that auto-increments its offset after each read. */
|
|
8
|
+
export class DataReader {
|
|
9
|
+
constructor(buffer, endian) {
|
|
10
|
+
this.buffer = buffer;
|
|
11
|
+
this.offset = 0;
|
|
12
|
+
this.dataView = buffer.getDataView();
|
|
13
|
+
this.littleEndian = endian === Endian.Little;
|
|
14
|
+
}
|
|
15
|
+
readUInt8() {
|
|
16
|
+
return this.dataView.getUint8(this.getOffsetAndAdvance(1));
|
|
17
|
+
}
|
|
18
|
+
readUInt16() {
|
|
19
|
+
return this.dataView.getUint16(this.getOffsetAndAdvance(2), this.littleEndian);
|
|
20
|
+
}
|
|
21
|
+
readUInt32() {
|
|
22
|
+
return this.dataView.getUint32(this.getOffsetAndAdvance(4), this.littleEndian);
|
|
23
|
+
}
|
|
24
|
+
readUInt64() {
|
|
25
|
+
return this.dataView.getBigUint64(this.getOffsetAndAdvance(8), this.littleEndian);
|
|
26
|
+
}
|
|
27
|
+
readInt8() {
|
|
28
|
+
return this.dataView.getInt8(this.getOffsetAndAdvance(1));
|
|
29
|
+
}
|
|
30
|
+
readInt16() {
|
|
31
|
+
return this.dataView.getInt16(this.getOffsetAndAdvance(2), this.littleEndian);
|
|
32
|
+
}
|
|
33
|
+
readInt32() {
|
|
34
|
+
return this.dataView.getInt32(this.getOffsetAndAdvance(4), this.littleEndian);
|
|
35
|
+
}
|
|
36
|
+
readInt64() {
|
|
37
|
+
return this.dataView.getBigInt64(this.getOffsetAndAdvance(8), this.littleEndian);
|
|
38
|
+
}
|
|
39
|
+
readFloat() {
|
|
40
|
+
return this.dataView.getFloat32(this.getOffsetAndAdvance(4), this.littleEndian);
|
|
41
|
+
}
|
|
42
|
+
readDouble() {
|
|
43
|
+
return this.dataView.getFloat64(this.getOffsetAndAdvance(8), this.littleEndian);
|
|
44
|
+
}
|
|
45
|
+
readUtf8String(length) {
|
|
46
|
+
const offset = this.getOffsetAndAdvance(length);
|
|
47
|
+
return new TextDecoder().decode(this.buffer.subarray(offset, offset + length));
|
|
48
|
+
}
|
|
49
|
+
readByteArray(length) {
|
|
50
|
+
const offset = this.getOffsetAndAdvance(length);
|
|
51
|
+
const result = this.buffer.subarray(offset, offset + length);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
getRemainingBytesCount() {
|
|
55
|
+
return this.dataView.byteLength - this.offset;
|
|
56
|
+
}
|
|
57
|
+
getRemainingBytes() {
|
|
58
|
+
return this.buffer.subarray(this.offset);
|
|
59
|
+
}
|
|
60
|
+
getOffsetAndAdvance(size) {
|
|
61
|
+
const result = this.offset;
|
|
62
|
+
this.offset += size;
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { ByteArray, Endian } from "./ByteArray.js";
|
|
7
|
+
/** Writer that auto-increments its offset after each write. */
|
|
8
|
+
export declare class DataWriter<E extends Endian> {
|
|
9
|
+
private readonly littleEndian;
|
|
10
|
+
private length;
|
|
11
|
+
private readonly chunks;
|
|
12
|
+
constructor(endian: E);
|
|
13
|
+
writeUInt8(value: number | bigint): void;
|
|
14
|
+
writeUInt16(value: number | bigint): void;
|
|
15
|
+
writeUInt32(value: number | bigint): void;
|
|
16
|
+
writeUInt64(value: number | bigint): void;
|
|
17
|
+
writeInt8(value: number | bigint): void;
|
|
18
|
+
writeInt16(value: number | bigint): void;
|
|
19
|
+
writeInt32(value: number | bigint): void;
|
|
20
|
+
writeInt64(value: number | bigint): void;
|
|
21
|
+
writeFloat(value: number): void;
|
|
22
|
+
writeDouble(value: number): void;
|
|
23
|
+
writeUtf8String(value: string): void;
|
|
24
|
+
writeByteArray(value: ByteArray): void;
|
|
25
|
+
toByteArray(): Uint8Array;
|
|
26
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { ByteArray, Endian } from "./ByteArray.js";
|
|
7
|
+
import { toBigInt, toNumber } from "./Number.js";
|
|
8
|
+
/** Writer that auto-increments its offset after each write. */
|
|
9
|
+
// TODO: some research should be done to make sure this is most performant implementation.
|
|
10
|
+
export class DataWriter {
|
|
11
|
+
constructor(endian) {
|
|
12
|
+
this.length = 0;
|
|
13
|
+
this.chunks = new Array();
|
|
14
|
+
this.littleEndian = endian === Endian.Little;
|
|
15
|
+
}
|
|
16
|
+
writeUInt8(value) {
|
|
17
|
+
this.chunks.push(new ByteArray([toNumber(value)]));
|
|
18
|
+
this.length += 1;
|
|
19
|
+
}
|
|
20
|
+
writeUInt16(value) {
|
|
21
|
+
const chunk = new ByteArray(2);
|
|
22
|
+
new DataView(chunk.buffer, 0, 2).setUint16(0, toNumber(value), this.littleEndian);
|
|
23
|
+
this.chunks.push(chunk);
|
|
24
|
+
this.length += 2;
|
|
25
|
+
}
|
|
26
|
+
writeUInt32(value) {
|
|
27
|
+
const chunk = new ByteArray(4);
|
|
28
|
+
new DataView(chunk.buffer, 0, 4).setUint32(0, toNumber(value), this.littleEndian);
|
|
29
|
+
this.chunks.push(chunk);
|
|
30
|
+
this.length += 4;
|
|
31
|
+
}
|
|
32
|
+
writeUInt64(value) {
|
|
33
|
+
const chunk = new ByteArray(8);
|
|
34
|
+
new DataView(chunk.buffer, 0, 8).setBigUint64(0, toBigInt(value), this.littleEndian);
|
|
35
|
+
this.chunks.push(chunk);
|
|
36
|
+
this.length += 8;
|
|
37
|
+
}
|
|
38
|
+
writeInt8(value) {
|
|
39
|
+
const chunk = new ByteArray(1);
|
|
40
|
+
new DataView(chunk.buffer, 0, 1).setInt8(0, toNumber(value));
|
|
41
|
+
this.chunks.push(chunk);
|
|
42
|
+
this.length += 1;
|
|
43
|
+
}
|
|
44
|
+
writeInt16(value) {
|
|
45
|
+
const chunk = new ByteArray(2);
|
|
46
|
+
new DataView(chunk.buffer, 0, 2).setInt16(0, toNumber(value), this.littleEndian);
|
|
47
|
+
this.chunks.push(chunk);
|
|
48
|
+
this.length += 2;
|
|
49
|
+
}
|
|
50
|
+
writeInt32(value) {
|
|
51
|
+
const chunk = new ByteArray(4);
|
|
52
|
+
new DataView(chunk.buffer, 0, 4).setInt32(0, toNumber(value), this.littleEndian);
|
|
53
|
+
this.chunks.push(chunk);
|
|
54
|
+
this.length += 4;
|
|
55
|
+
}
|
|
56
|
+
writeInt64(value) {
|
|
57
|
+
const chunk = new ByteArray(8);
|
|
58
|
+
new DataView(chunk.buffer, 0, 8).setBigInt64(0, toBigInt(value), this.littleEndian);
|
|
59
|
+
this.chunks.push(chunk);
|
|
60
|
+
this.length += 8;
|
|
61
|
+
}
|
|
62
|
+
writeFloat(value) {
|
|
63
|
+
const chunk = new ByteArray(4);
|
|
64
|
+
new DataView(chunk.buffer, 0, 4).setFloat32(0, value, this.littleEndian);
|
|
65
|
+
this.chunks.push(chunk);
|
|
66
|
+
this.length += 4;
|
|
67
|
+
}
|
|
68
|
+
writeDouble(value) {
|
|
69
|
+
const chunk = new ByteArray(8);
|
|
70
|
+
new DataView(chunk.buffer, 0, 8).setFloat64(0, value, this.littleEndian);
|
|
71
|
+
this.chunks.push(chunk);
|
|
72
|
+
this.length += 8;
|
|
73
|
+
}
|
|
74
|
+
writeUtf8String(value) {
|
|
75
|
+
const bytes = ByteArray.fromString(value);
|
|
76
|
+
this.chunks.push(bytes);
|
|
77
|
+
this.length += bytes.byteLength;
|
|
78
|
+
}
|
|
79
|
+
writeByteArray(value) {
|
|
80
|
+
this.chunks.push(value);
|
|
81
|
+
this.length += value.byteLength;
|
|
82
|
+
}
|
|
83
|
+
toByteArray() {
|
|
84
|
+
if (this.chunks.length === 0)
|
|
85
|
+
return new ByteArray(0);
|
|
86
|
+
if (this.chunks.length === 1)
|
|
87
|
+
return this.chunks[0];
|
|
88
|
+
const result = new ByteArray(this.length);
|
|
89
|
+
let offset = 0;
|
|
90
|
+
this.chunks.forEach(chunck => {
|
|
91
|
+
result.set(chunck, offset);
|
|
92
|
+
offset += chunck.byteLength;
|
|
93
|
+
});
|
|
94
|
+
this.chunks.length = 0;
|
|
95
|
+
this.chunks.push(result);
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export declare const UINT8_MAX = 255;
|
|
7
|
+
export declare const UINT16_MAX = 65535;
|
|
8
|
+
export declare const UINT32_MAX = 4294967295;
|
|
9
|
+
export declare const UINT64_MAX: bigint;
|
|
10
|
+
export declare const INT8_MIN = -128;
|
|
11
|
+
export declare const INT16_MIN = -32768;
|
|
12
|
+
export declare const INT32_MIN = -2147483648;
|
|
13
|
+
export declare const INT64_MIN: bigint;
|
|
14
|
+
export declare const INT8_MAX = 127;
|
|
15
|
+
export declare const INT16_MAX = 32767;
|
|
16
|
+
export declare const INT32_MAX = 2147483647;
|
|
17
|
+
export declare const INT64_MAX: bigint;
|
|
18
|
+
export declare const FLOAT32_MIN = -3.4028234663852886e+38;
|
|
19
|
+
export declare const FLOAT32_MAX = 3.4028234663852886e+38;
|
|
20
|
+
export declare function toNumber(value: bigint | number): number;
|
|
21
|
+
export declare function toBigInt(value: bigint | number): bigint;
|
|
22
|
+
export declare function minValue<T extends bigint | number>(a: T | undefined, b: T | undefined): T | undefined;
|
|
23
|
+
export declare function maxValue<T extends bigint | number>(a: T | undefined, b: T | undefined): T | undefined;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export const UINT8_MAX = 0xFF;
|
|
7
|
+
export const UINT16_MAX = 0xFFFF;
|
|
8
|
+
export const UINT32_MAX = 0xFFFFFFFF;
|
|
9
|
+
export const UINT64_MAX = BigInt("18446744073709551615");
|
|
10
|
+
export const INT8_MIN = -128;
|
|
11
|
+
export const INT16_MIN = -32768;
|
|
12
|
+
export const INT32_MIN = -2147483648;
|
|
13
|
+
export const INT64_MIN = BigInt("-9223372036854775808");
|
|
14
|
+
export const INT8_MAX = 127;
|
|
15
|
+
export const INT16_MAX = 32767;
|
|
16
|
+
export const INT32_MAX = 2147483647;
|
|
17
|
+
export const INT64_MAX = BigInt("9223372036854775807");
|
|
18
|
+
export const FLOAT32_MIN = -340282346638528859811704183484516925440.0;
|
|
19
|
+
export const FLOAT32_MAX = 340282346638528859811704183484516925440.0;
|
|
20
|
+
export function toNumber(value) {
|
|
21
|
+
return typeof value === "bigint" ? Number(value) : value;
|
|
22
|
+
}
|
|
23
|
+
export function toBigInt(value) {
|
|
24
|
+
return typeof value === "number" ? BigInt(value) : value;
|
|
25
|
+
}
|
|
26
|
+
export function minValue(a, b) {
|
|
27
|
+
if (a === undefined)
|
|
28
|
+
return b;
|
|
29
|
+
if (b === undefined)
|
|
30
|
+
return a;
|
|
31
|
+
return a < b ? a : b;
|
|
32
|
+
}
|
|
33
|
+
export function maxValue(a, b) {
|
|
34
|
+
if (a === undefined)
|
|
35
|
+
return b;
|
|
36
|
+
if (b === undefined)
|
|
37
|
+
return a;
|
|
38
|
+
return a > b ? a : b;
|
|
39
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2022 Project CHIP Authors
|
|
5
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
__exportStar(require("./schema/BitmapSchema.js"), exports);
|
|
23
|
+
__exportStar(require("./schema/Schema.js"), exports);
|
|
24
|
+
__exportStar(require("./spec/Specifications.js"), exports);
|
|
25
|
+
__exportStar(require("./util/Number.js"), exports);
|
|
26
|
+
__exportStar(require("./util/ByteArray.js"), exports);
|
|
27
|
+
__exportStar(require("./util/DataReader.js"), exports);
|
|
28
|
+
__exportStar(require("./util/DataWriter.js"), exports);
|
|
29
|
+
__exportStar(require("./tlv/TlvAny.js"), exports);
|
|
30
|
+
__exportStar(require("./tlv/TlvArray.js"), exports);
|
|
31
|
+
__exportStar(require("./tlv/TlvBoolean.js"), exports);
|
|
32
|
+
__exportStar(require("./tlv/TlvSchema.js"), exports);
|
|
33
|
+
__exportStar(require("./tlv/TlvNullable.js"), exports);
|
|
34
|
+
__exportStar(require("./tlv/TlvObject.js"), exports);
|
|
35
|
+
__exportStar(require("./tlv/TlvString.js"), exports);
|
|
36
|
+
__exportStar(require("./tlv/TlvNumber.js"), exports);
|
|
37
|
+
__exportStar(require("./tlv/TlvVoid.js"), exports);
|
|
38
|
+
__exportStar(require("./tlv/TlvWrapper.js"), exports);
|