@matter/protocol
Advanced tools
Comparing version 0.11.5-alpha.0-20241123-e56e38864 to 0.11.5-alpha.0-20241124-6e969cda5
@@ -390,42 +390,51 @@ "use strict"; | ||
async sendReadRequest(readRequest) { | ||
await this.send(2 /* ReadRequest */, import_types.TlvReadRequest.encode(readRequest)); | ||
await this.send(2 /* ReadRequest */, this.#encodeReadingRequest(import_types.TlvReadRequest, readRequest)); | ||
return this.readDataReports(); | ||
} | ||
#encodeSubscribeRequest(subscribeRequest) { | ||
const request = import_types.TlvSubscribeRequest.encode(subscribeRequest); | ||
if (request.length <= this.exchange.maxPayloadSize) { | ||
return request; | ||
#encodeReadingRequest(schema, request) { | ||
const encoded = schema.encode(request); | ||
if (encoded.length <= this.exchange.maxPayloadSize) { | ||
return encoded; | ||
} | ||
const dataVersionFilters = subscribeRequest.dataVersionFilters ?? []; | ||
subscribeRequest.dataVersionFilters = []; | ||
const requestWithoutDataVersionFilters = import_types.TlvSubscribeRequest.encode(subscribeRequest); | ||
const originalDataVersionFilters = [...request.dataVersionFilters ?? []]; | ||
const requestWithoutDataVersionFilters = schema.encode({ | ||
...request, | ||
dataVersionFilters: [] | ||
}); | ||
if (requestWithoutDataVersionFilters.length > this.exchange.maxPayloadSize) { | ||
throw new import_general.MatterFlowError( | ||
`SubscribeRequest is too long to fit in a single chunk, This should not happen! Data: ${import_general.Logger.toJSON( | ||
subscribeRequest | ||
)}` | ||
`Request is too long to fit in a single chunk, This should not happen! Data: ${import_general.Logger.toJSON(request)}` | ||
); | ||
} | ||
let remainingBytes = this.exchange.maxPayloadSize - requestWithoutDataVersionFilters.length; | ||
while (remainingBytes > 0 && dataVersionFilters.length > 0) { | ||
const dataVersionFilter = dataVersionFilters.shift(); | ||
return schema.encode({ | ||
...request, | ||
dataVersionFilters: this.#shortenDataVersionFilters( | ||
originalDataVersionFilters, | ||
this.exchange.maxPayloadSize - requestWithoutDataVersionFilters.length | ||
) | ||
}); | ||
} | ||
#shortenDataVersionFilters(originalDataVersionFilters, availableBytes) { | ||
const dataVersionFilters = new Array(); | ||
while (availableBytes > 0 && originalDataVersionFilters.length > 0) { | ||
const dataVersionFilter = originalDataVersionFilters.shift(); | ||
if (dataVersionFilter === void 0) { | ||
break; | ||
} | ||
const encodedDataVersionFilter = import_types.TlvDataVersionFilter.encodeTlv(dataVersionFilter); | ||
const encodedDataVersionFilter = import_types.TlvDataVersionFilter.encode(dataVersionFilter); | ||
const encodedDataVersionFilterLength = encodedDataVersionFilter.length; | ||
if (encodedDataVersionFilterLength > remainingBytes) { | ||
dataVersionFilters.unshift(dataVersionFilter); | ||
if (encodedDataVersionFilterLength > availableBytes) { | ||
originalDataVersionFilters.unshift(dataVersionFilter); | ||
break; | ||
} | ||
subscribeRequest.dataVersionFilters.push(dataVersionFilter); | ||
remainingBytes -= encodedDataVersionFilterLength; | ||
dataVersionFilters.push(dataVersionFilter); | ||
availableBytes -= encodedDataVersionFilterLength; | ||
} | ||
logger.debug( | ||
`Removed ${dataVersionFilters.length} DataVersionFilters from SubscribeRequest to fit into a single message` | ||
`Removed ${originalDataVersionFilters.length} DataVersionFilters from Request to fit into a single message` | ||
); | ||
return import_types.TlvSubscribeRequest.encode(subscribeRequest); | ||
return dataVersionFilters; | ||
} | ||
async sendSubscribeRequest(subscribeRequest) { | ||
const request = this.#encodeSubscribeRequest(subscribeRequest); | ||
const request = this.#encodeReadingRequest(import_types.TlvSubscribeRequest, subscribeRequest); | ||
await this.send(3 /* SubscribeRequest */, request); | ||
@@ -432,0 +441,0 @@ const report = await this.readDataReports(); |
@@ -389,42 +389,51 @@ /** | ||
async sendReadRequest(readRequest) { | ||
await this.send(2 /* ReadRequest */, TlvReadRequest.encode(readRequest)); | ||
await this.send(2 /* ReadRequest */, this.#encodeReadingRequest(TlvReadRequest, readRequest)); | ||
return this.readDataReports(); | ||
} | ||
#encodeSubscribeRequest(subscribeRequest) { | ||
const request = TlvSubscribeRequest.encode(subscribeRequest); | ||
if (request.length <= this.exchange.maxPayloadSize) { | ||
return request; | ||
#encodeReadingRequest(schema, request) { | ||
const encoded = schema.encode(request); | ||
if (encoded.length <= this.exchange.maxPayloadSize) { | ||
return encoded; | ||
} | ||
const dataVersionFilters = subscribeRequest.dataVersionFilters ?? []; | ||
subscribeRequest.dataVersionFilters = []; | ||
const requestWithoutDataVersionFilters = TlvSubscribeRequest.encode(subscribeRequest); | ||
const originalDataVersionFilters = [...request.dataVersionFilters ?? []]; | ||
const requestWithoutDataVersionFilters = schema.encode({ | ||
...request, | ||
dataVersionFilters: [] | ||
}); | ||
if (requestWithoutDataVersionFilters.length > this.exchange.maxPayloadSize) { | ||
throw new MatterFlowError( | ||
`SubscribeRequest is too long to fit in a single chunk, This should not happen! Data: ${Logger.toJSON( | ||
subscribeRequest | ||
)}` | ||
`Request is too long to fit in a single chunk, This should not happen! Data: ${Logger.toJSON(request)}` | ||
); | ||
} | ||
let remainingBytes = this.exchange.maxPayloadSize - requestWithoutDataVersionFilters.length; | ||
while (remainingBytes > 0 && dataVersionFilters.length > 0) { | ||
const dataVersionFilter = dataVersionFilters.shift(); | ||
return schema.encode({ | ||
...request, | ||
dataVersionFilters: this.#shortenDataVersionFilters( | ||
originalDataVersionFilters, | ||
this.exchange.maxPayloadSize - requestWithoutDataVersionFilters.length | ||
) | ||
}); | ||
} | ||
#shortenDataVersionFilters(originalDataVersionFilters, availableBytes) { | ||
const dataVersionFilters = new Array(); | ||
while (availableBytes > 0 && originalDataVersionFilters.length > 0) { | ||
const dataVersionFilter = originalDataVersionFilters.shift(); | ||
if (dataVersionFilter === void 0) { | ||
break; | ||
} | ||
const encodedDataVersionFilter = TlvDataVersionFilter.encodeTlv(dataVersionFilter); | ||
const encodedDataVersionFilter = TlvDataVersionFilter.encode(dataVersionFilter); | ||
const encodedDataVersionFilterLength = encodedDataVersionFilter.length; | ||
if (encodedDataVersionFilterLength > remainingBytes) { | ||
dataVersionFilters.unshift(dataVersionFilter); | ||
if (encodedDataVersionFilterLength > availableBytes) { | ||
originalDataVersionFilters.unshift(dataVersionFilter); | ||
break; | ||
} | ||
subscribeRequest.dataVersionFilters.push(dataVersionFilter); | ||
remainingBytes -= encodedDataVersionFilterLength; | ||
dataVersionFilters.push(dataVersionFilter); | ||
availableBytes -= encodedDataVersionFilterLength; | ||
} | ||
logger.debug( | ||
`Removed ${dataVersionFilters.length} DataVersionFilters from SubscribeRequest to fit into a single message` | ||
`Removed ${originalDataVersionFilters.length} DataVersionFilters from Request to fit into a single message` | ||
); | ||
return TlvSubscribeRequest.encode(subscribeRequest); | ||
return dataVersionFilters; | ||
} | ||
async sendSubscribeRequest(subscribeRequest) { | ||
const request = this.#encodeSubscribeRequest(subscribeRequest); | ||
const request = this.#encodeReadingRequest(TlvSubscribeRequest, subscribeRequest); | ||
await this.send(3 /* SubscribeRequest */, request); | ||
@@ -431,0 +440,0 @@ const report = await this.readDataReports(); |
{ | ||
"name": "@matter/protocol", | ||
"version": "0.11.5-alpha.0-20241123-e56e38864", | ||
"version": "0.11.5-alpha.0-20241124-6e969cda5", | ||
"description": "Low-level APIs for Matter interaction", | ||
@@ -43,10 +43,10 @@ "keywords": [ | ||
"dependencies": { | ||
"@matter/general": "0.11.5-alpha.0-20241123-e56e38864", | ||
"@matter/model": "0.11.5-alpha.0-20241123-e56e38864", | ||
"@matter/types": "0.11.5-alpha.0-20241123-e56e38864", | ||
"@matter/general": "0.11.5-alpha.0-20241124-6e969cda5", | ||
"@matter/model": "0.11.5-alpha.0-20241124-6e969cda5", | ||
"@matter/types": "0.11.5-alpha.0-20241124-6e969cda5", | ||
"@noble/curves": "^1.5.0" | ||
}, | ||
"devDependencies": { | ||
"@matter/tools": "0.11.5-alpha.0-20241123-e56e38864", | ||
"@matter/testing": "0.11.5-alpha.0-20241123-e56e38864" | ||
"@matter/tools": "0.11.5-alpha.0-20241124-6e969cda5", | ||
"@matter/testing": "0.11.5-alpha.0-20241124-6e969cda5" | ||
}, | ||
@@ -53,0 +53,0 @@ "files": [ |
@@ -483,3 +483,3 @@ /** | ||
async sendReadRequest(readRequest: ReadRequest) { | ||
await this.send(MessageType.ReadRequest, TlvReadRequest.encode(readRequest)); | ||
await this.send(MessageType.ReadRequest, this.#encodeReadingRequest(TlvReadRequest, readRequest)); | ||
@@ -489,42 +489,58 @@ return this.readDataReports(); | ||
#encodeSubscribeRequest(subscribeRequest: SubscribeRequest) { | ||
const request = TlvSubscribeRequest.encode(subscribeRequest); | ||
if (request.length <= this.exchange.maxPayloadSize) { | ||
return request; | ||
#encodeReadingRequest<T extends TlvSchema<any>>(schema: T, request: TypeFromSchema<T>) { | ||
const encoded = schema.encode(request); | ||
if (encoded.length <= this.exchange.maxPayloadSize) { | ||
return encoded; | ||
} | ||
const dataVersionFilters = subscribeRequest.dataVersionFilters ?? []; | ||
subscribeRequest.dataVersionFilters = []; | ||
const requestWithoutDataVersionFilters = TlvSubscribeRequest.encode(subscribeRequest); | ||
const originalDataVersionFilters = [...(request.dataVersionFilters ?? [])]; | ||
const requestWithoutDataVersionFilters = schema.encode({ | ||
...request, | ||
dataVersionFilters: [], | ||
}); | ||
if (requestWithoutDataVersionFilters.length > this.exchange.maxPayloadSize) { | ||
throw new MatterFlowError( | ||
`SubscribeRequest is too long to fit in a single chunk, This should not happen! Data: ${Logger.toJSON( | ||
subscribeRequest, | ||
)}`, | ||
`Request is too long to fit in a single chunk, This should not happen! Data: ${Logger.toJSON(request)}`, | ||
); | ||
} | ||
let remainingBytes = this.exchange.maxPayloadSize - requestWithoutDataVersionFilters.length; | ||
while (remainingBytes > 0 && dataVersionFilters.length > 0) { | ||
const dataVersionFilter = dataVersionFilters.shift(); | ||
return schema.encode({ | ||
...request, | ||
dataVersionFilters: this.#shortenDataVersionFilters( | ||
originalDataVersionFilters, | ||
this.exchange.maxPayloadSize - requestWithoutDataVersionFilters.length, | ||
), | ||
}); | ||
} | ||
#shortenDataVersionFilters( | ||
originalDataVersionFilters: TypeFromSchema<typeof TlvDataVersionFilter>[], | ||
availableBytes: number, | ||
) { | ||
const dataVersionFilters = new Array<TypeFromSchema<typeof TlvDataVersionFilter>>(); | ||
while (availableBytes > 0 && originalDataVersionFilters.length > 0) { | ||
const dataVersionFilter = originalDataVersionFilters.shift(); | ||
if (dataVersionFilter === undefined) { | ||
break; | ||
} | ||
const encodedDataVersionFilter = TlvDataVersionFilter.encodeTlv(dataVersionFilter); | ||
const encodedDataVersionFilter = TlvDataVersionFilter.encode(dataVersionFilter); | ||
const encodedDataVersionFilterLength = encodedDataVersionFilter.length; | ||
if (encodedDataVersionFilterLength > remainingBytes) { | ||
dataVersionFilters.unshift(dataVersionFilter); | ||
if (encodedDataVersionFilterLength > availableBytes) { | ||
originalDataVersionFilters.unshift(dataVersionFilter); | ||
break; | ||
} | ||
subscribeRequest.dataVersionFilters.push(dataVersionFilter); | ||
remainingBytes -= encodedDataVersionFilterLength; | ||
dataVersionFilters.push(dataVersionFilter); | ||
availableBytes -= encodedDataVersionFilterLength; | ||
} | ||
logger.debug( | ||
`Removed ${dataVersionFilters.length} DataVersionFilters from SubscribeRequest to fit into a single message`, | ||
`Removed ${originalDataVersionFilters.length} DataVersionFilters from Request to fit into a single message`, | ||
); | ||
return TlvSubscribeRequest.encode(subscribeRequest); | ||
return dataVersionFilters; | ||
} | ||
async sendSubscribeRequest(subscribeRequest: SubscribeRequest) { | ||
const request = this.#encodeSubscribeRequest(subscribeRequest); | ||
const request = this.#encodeReadingRequest(TlvSubscribeRequest, subscribeRequest); | ||
await this.send(MessageType.SubscribeRequest, request); | ||
@@ -531,0 +547,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
3881026
67290
+ Added@matter/general@0.11.5-alpha.0-20241124-6e969cda5(transitive)
+ Added@matter/model@0.11.5-alpha.0-20241124-6e969cda5(transitive)
+ Added@matter/types@0.11.5-alpha.0-20241124-6e969cda5(transitive)
- Removed@matter/general@0.11.5-alpha.0-20241123-e56e38864(transitive)
- Removed@matter/model@0.11.5-alpha.0-20241123-e56e38864(transitive)
- Removed@matter/types@0.11.5-alpha.0-20241123-e56e38864(transitive)