@project-chip/matter.js
Advanced tools
Comparing version 0.0.0 to 0.0.1
@@ -1,21 +0,47 @@ | ||
export interface BitPosition { | ||
position: number; | ||
/** | ||
* @license | ||
* Copyright 2022 Project CHIP Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { Schema } from "./Schema.js"; | ||
declare const enum BitRangeType { | ||
Flag = 0, | ||
Number = 1, | ||
Enum = 2 | ||
} | ||
export declare const BitPosition: (position: number) => { | ||
position: number; | ||
type BitRange<T> = { | ||
type: BitRangeType; | ||
offset: number; | ||
mask: number; | ||
}; | ||
declare type BitSchema = { | ||
[key: string]: BitPosition; | ||
declare const BitRange: <T>(type: BitRangeType, offset: number, length: number) => BitRange<T>; | ||
/** Defines the bit position of a boolean flag. */ | ||
export interface BitFlag extends BitRange<boolean> { | ||
type: BitRangeType.Flag; | ||
} | ||
export declare const BitFlag: (offset: number) => BitFlag; | ||
/** Defines the bit position and bit length of a numeric value. */ | ||
export interface BitField extends BitRange<number> { | ||
type: BitRangeType.Number; | ||
} | ||
export declare const BitField: (offset: number, length: number) => BitField; | ||
/** Defines the bit position and bit length of an enum flag. */ | ||
export interface BitFieldEnum<E extends number> extends BitRange<E> { | ||
type: BitRangeType.Enum; | ||
} | ||
export declare const BitFieldEnum: <E extends number>(offset: number, length: number) => BitFieldEnum<E>; | ||
export type BitSchema = { | ||
[key: string]: BitFlag | BitField | BitFieldEnum<any>; | ||
}; | ||
declare type TypeFromBitSchema<T extends BitSchema> = { | ||
[K in keyof T]: boolean; | ||
export type TypeFromBitSchema<T extends BitSchema> = { | ||
[K in keyof T]: T[K] extends BitFieldEnum<infer E> ? E : (T[K] extends BitField ? number : boolean); | ||
}; | ||
export declare const BitmapSchema: (bitSchemas: BitSchema) => { | ||
readonly bitSchemas: BitSchema; | ||
encodeInternal(value: TypeFromBitSchema<BitSchema>): number; | ||
decodeInternal(bitmap: number): TypeFromBitSchema<BitSchema>; | ||
encode(value: TypeFromBitSchema<BitSchema>): number; | ||
decode(encoded: number): TypeFromBitSchema<BitSchema>; | ||
validate(value: TypeFromBitSchema<BitSchema>): void; | ||
}; | ||
declare class BitmapSchemaInternal<T extends BitSchema> extends Schema<TypeFromBitSchema<T>, number> { | ||
readonly bitSchemas: T; | ||
constructor(bitSchemas: T); | ||
encodeInternal(value: TypeFromBitSchema<T>): number; | ||
decodeInternal(bitmap: number): TypeFromBitSchema<T>; | ||
} | ||
/** Declares a bitmap schema by indicating the bit position and their names. */ | ||
export declare const BitmapSchema: <T extends BitSchema>(bitSchemas: T) => BitmapSchemaInternal<T>; | ||
export {}; |
@@ -1,11 +0,16 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BitmapSchema = exports.BitPosition = void 0; | ||
const Schema_1 = require("./Schema"); | ||
const BitPosition = (position) => ({ position }); | ||
exports.BitPosition = BitPosition; | ||
const BitmapSchema = (bitSchemas) => new class extends Schema_1.Schema { | ||
/** | ||
* @license | ||
* Copyright 2022 Project CHIP Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { Schema } from "./Schema.js"; | ||
const BitRange = (type, offset, length) => ({ type, mask: ((1 << length) - 1) << offset, offset }); | ||
export const BitFlag = (offset) => BitRange(0 /* BitRangeType.Flag */, offset, 1); | ||
export const BitField = (offset, length) => BitRange(1 /* BitRangeType.Number */, offset, length); | ||
export const BitFieldEnum = (offset, length) => BitRange(2 /* BitRangeType.Enum */, offset, length); | ||
class BitmapSchemaInternal extends Schema { | ||
constructor(bitSchemas) { | ||
super(); | ||
this.bitSchemas = bitSchemas; | ||
// TODO: validate that bitSchemas is coherent | ||
} | ||
@@ -15,4 +20,12 @@ encodeInternal(value) { | ||
for (const name in this.bitSchemas) { | ||
if (value[name]) | ||
result |= 1 << this.bitSchemas[name].position; | ||
const { type, offset, mask } = this.bitSchemas[name]; | ||
switch (type) { | ||
case 0 /* BitRangeType.Flag */: | ||
if (value[name]) | ||
result |= mask; | ||
break; | ||
case 2 /* BitRangeType.Enum */: | ||
case 1 /* BitRangeType.Number */: | ||
result |= value[name] << offset; | ||
} | ||
} | ||
@@ -24,7 +37,14 @@ return result; | ||
for (const name in this.bitSchemas) { | ||
result[name] = (bitmap & (1 << this.bitSchemas[name].position)) !== 0; | ||
const { type, offset, mask } = this.bitSchemas[name]; | ||
if (type === 0 /* BitRangeType.Flag */) { | ||
result[name] = (bitmap & mask) !== 0; | ||
} | ||
else { | ||
result[name] = (bitmap & mask) >> offset; | ||
} | ||
} | ||
return result; | ||
} | ||
}(bitSchemas); | ||
exports.BitmapSchema = BitmapSchema; | ||
} | ||
/** Declares a bitmap schema by indicating the bit position and their names. */ | ||
export const BitmapSchema = (bitSchemas) => new BitmapSchemaInternal(bitSchemas); |
@@ -0,7 +1,16 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Project CHIP Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
/** Define a schema to encode / decode convert type T to type E. */ | ||
export declare abstract class Schema<T, E> { | ||
/** Encodes the value using the schema. */ | ||
encode(value: T): E; | ||
/** Decodes the encoded data using the schema. */ | ||
decode(encoded: E): T; | ||
protected abstract encodeInternal(value: T): E; | ||
protected abstract decodeInternal(encoded: E): T; | ||
validate(value: T): void; | ||
/** Optional validator that can be used to enforce constraints on the data before encoding / after decoding. */ | ||
validate(_value: T): void; | ||
} |
@@ -1,5 +0,9 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Schema = void 0; | ||
class Schema { | ||
/** | ||
* @license | ||
* Copyright 2022 Project CHIP Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
/** Define a schema to encode / decode convert type T to type E. */ | ||
export class Schema { | ||
/** Encodes the value using the schema. */ | ||
encode(value) { | ||
@@ -9,2 +13,3 @@ this.validate(value); | ||
} | ||
/** Decodes the encoded data using the schema. */ | ||
decode(encoded) { | ||
@@ -15,5 +20,5 @@ const result = this.decodeInternal(encoded); | ||
} | ||
validate(value) { | ||
/** Optional validator that can be used to enforce constraints on the data before encoding / after decoding. */ | ||
validate(_value) { | ||
} | ||
} | ||
exports.Schema = Schema; |
{ | ||
"name": "@project-chip/matter.js", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Matter protocol in pure js", | ||
@@ -26,13 +26,17 @@ "keywords": [ | ||
"scripts": { | ||
"clean": "rm -rf dist", | ||
"build": "npm run clean; tsc", | ||
"test": "TZ=utc mocha" | ||
"clean": "rm -rf dist; rm -rf dist-cjs", | ||
"build": "npm run clean; tsc --project tsconfig.dist.json; tsc --project tsconfig.dist-cjs.json", | ||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js", | ||
"test-web": "karma start karma.conf.cjs" | ||
}, | ||
"devDependencies": { | ||
"@types/assert": "^1.5.6", | ||
"@types/mocha": "^9.1.1", | ||
"assert": "^2.0.0", | ||
"mocha": "^10.0.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.5.5" | ||
"@types/jasmine": "^4.3.0", | ||
"jest": "^29.3.1", | ||
"karma": "^6.4.1", | ||
"karma-chrome-launcher": "^3.1.1", | ||
"karma-coverage": "^2.2.0", | ||
"karma-jasmine": "^5.1.0", | ||
"karma-typescript": "^5.5.3", | ||
"ts-jest": "^29.0.3", | ||
"typescript": "^4.9.3" | ||
}, | ||
@@ -42,4 +46,5 @@ "files": [ | ||
], | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts" | ||
"main": "dist-cjs/matter.js", | ||
"module": "dist/matter.js", | ||
"types": "dist/matter.d.ts" | ||
} |
# matter.js | ||
![experimental](https://img.shields.io/badge/status-Experimental-red) [![license](https://img.shields.io/badge/license-Apache2-green.svg)](https://raw.githubusercontent.com/project-chip/matter.js/master/LICENSE) | ||
Implementation of Matter protocol in typescript with no native dependencies (and very limited dependencies). | ||
## Tests | ||
Run tests in node.js: npm run test | ||
Run tests in the browser: npm run test-web | ||
test-web will create a /coverage directory conatining the test code coverage |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
81927
44
1766
14
9
1