From 51cb1b32828b99b1d201a9c1e374427e75ece0a7 Mon Sep 17 00:00:00 2001 From: Markus Tacker Date: Mon, 11 Dec 2023 12:41:30 +0100 Subject: [PATCH] fix(lwm2m): improve message handling --- lwm2m/transformShadowUpdateToLwM2M.ts | 73 +++++++++++++++++++++++---- package-lock.json | 14 ++--- package.json | 2 +- 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/lwm2m/transformShadowUpdateToLwM2M.ts b/lwm2m/transformShadowUpdateToLwM2M.ts index 4ddea7f..6432184 100644 --- a/lwm2m/transformShadowUpdateToLwM2M.ts +++ b/lwm2m/transformShadowUpdateToLwM2M.ts @@ -19,20 +19,71 @@ export const transformShadowUpdateToLwM2M = ( transformers: Readonly>, ): ((update: Update) => Promise>) => { // Turn the JSONata in the transformers into executable functions - const transformerFns = transformers.map(({ match, transform }) => ({ - match: jsonata(match), - transform: jsonata(transform), - })) + const transformerFns: Array<{ + match: ReturnType + matchExpression: string + transform: ReturnType + transformExpression: string + }> = [] + + for (const { + match: matchExpression, + transform: transformExpression, + } of transformers) { + let match: ReturnType + let transform: ReturnType + try { + match = jsonata(matchExpression) + } catch { + throw new Error(`Failed to parse match expression '${matchExpression}'`) + } + try { + transform = jsonata(transformExpression) + } catch { + throw new Error( + `Failed to parse match expression '${transformExpression}'`, + ) + } + transformerFns.push({ + match, + matchExpression, + transform, + transformExpression, + }) + } return async (input: Update): Promise> => Promise.all( - transformerFns.map(async ({ match, transform }) => { - // Check if the `matched` JSONata returns `true`. - const matched = await match.evaluate(input) - if (typeof matched !== 'boolean' || matched !== true) return null - // Apply the transform - return transform.evaluate(input) - }), + transformerFns.map( + async ({ match, matchExpression, transform, transformExpression }) => { + // Check if the `matched` JSONata returns `true`. + try { + const matched = await match.evaluate(input) + if (typeof matched !== 'boolean') return null + if (matched === false) return null + } catch (err) { + console.error(err) + console.error( + `Failed to match ${JSON.stringify( + input, + )} using expression '${matchExpression}'!`, + ) + return false + } + // Transform + try { + return await transform.evaluate(input) + } catch (err) { + console.error(err) + console.error( + `Failed to transform ${JSON.stringify( + input, + )} using expression '${transformExpression}'!`, + ) + return null + } + }, + ), ) .then((result) => result.flat()) // Ignore unmatched transformers diff --git a/package-lock.json b/package-lock.json index e506697..d2981b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.0-development", "license": "BSD-3-Clause", "dependencies": { - "@hello.nrfcloud.com/proto-lwm2m": "2.2.0", + "@hello.nrfcloud.com/proto-lwm2m": "2.2.1", "@nordicsemiconductor/from-env": "3.0.0", "@nordicsemiconductor/timestream-helpers": "6.0.1", "@sinclair/typebox": "0.31.28", @@ -5231,9 +5231,9 @@ } }, "node_modules/@hello.nrfcloud.com/proto-lwm2m": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@hello.nrfcloud.com/proto-lwm2m/-/proto-lwm2m-2.2.0.tgz", - "integrity": "sha512-LZCVuCe8CPkHO2+5dsUZ+O40kE2v74YI4uM14SPZKWMEjqGURexfkEC+hz5P5V4gPryyCXoYcxuFn/ohdBBioA==", + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/@hello.nrfcloud.com/proto-lwm2m/-/proto-lwm2m-2.2.1.tgz", + "integrity": "sha512-dyFso6Vyoz2I89YVrCRS3itkCsUkq+IWHAzv5WzfZ9XBmiXfOjr0bT887Omg7tHvsK1H3xBtKZmXcSrRIGvYUQ==", "dependencies": { "@hello.nrfcloud.com/proto": "5.5.24", "@sinclair/typebox": "0.31.28", @@ -18317,9 +18317,9 @@ } }, "@hello.nrfcloud.com/proto-lwm2m": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@hello.nrfcloud.com/proto-lwm2m/-/proto-lwm2m-2.2.0.tgz", - "integrity": "sha512-LZCVuCe8CPkHO2+5dsUZ+O40kE2v74YI4uM14SPZKWMEjqGURexfkEC+hz5P5V4gPryyCXoYcxuFn/ohdBBioA==", + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/@hello.nrfcloud.com/proto-lwm2m/-/proto-lwm2m-2.2.1.tgz", + "integrity": "sha512-dyFso6Vyoz2I89YVrCRS3itkCsUkq+IWHAzv5WzfZ9XBmiXfOjr0bT887Omg7tHvsK1H3xBtKZmXcSrRIGvYUQ==", "requires": { "@hello.nrfcloud.com/proto": "5.5.24", "@sinclair/typebox": "0.31.28", diff --git a/package.json b/package.json index aa310df..61f8401 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ }, "prettier": "@nordicsemiconductor/asset-tracker-cloud-code-style/.prettierrc", "dependencies": { - "@hello.nrfcloud.com/proto-lwm2m": "2.2.0", + "@hello.nrfcloud.com/proto-lwm2m": "2.2.1", "@nordicsemiconductor/from-env": "3.0.0", "@nordicsemiconductor/timestream-helpers": "6.0.1", "@sinclair/typebox": "0.31.28",