From 8fddeaaee2d4e0e350dad4b97a45447ee5b42c43 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Wed, 29 Nov 2023 13:40:44 +0100 Subject: [PATCH 1/3] refactor: use a default contentType of application/json in Forms --- packages/td-tools/src/thing-description.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/td-tools/src/thing-description.ts b/packages/td-tools/src/thing-description.ts index adc234b37..8e7fd9d95 100644 --- a/packages/td-tools/src/thing-description.ts +++ b/packages/td-tools/src/thing-description.ts @@ -56,7 +56,7 @@ export type ThingInteraction = TDT.PropertyElement | TDT.ActionElement | TDT.Eve export class Form implements TDT.FormElementBase { op?: string | string[]; href: TDT.AnyUri; - contentType?: string; + contentType: string; contentCoding?: string; subprotocol?: TDT.Subprotocol; security?: TDT.Security; @@ -67,7 +67,7 @@ export class Form implements TDT.FormElementBase { constructor(href: string, contentType?: string) { this.href = href; - if (contentType != null) this.contentType = contentType; + this.contentType = contentType ?? "application/json"; } } export interface ExpectedResponse { From 7b385e497e306a9362d457d5180b8821a4d5d45c Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Wed, 29 Nov 2023 13:52:54 +0100 Subject: [PATCH 2/3] refactor: remove now obsolete nullish coalescing --- packages/binding-coap/src/coap-client.ts | 4 ++-- packages/binding-coap/src/coap-server.ts | 2 +- packages/binding-coap/src/coaps-client.ts | 6 +++--- packages/binding-http/src/subscription-protocols.ts | 2 +- packages/binding-modbus/src/modbus-connection.ts | 2 +- packages/binding-mqtt/src/mqtt-broker-server.ts | 8 ++++---- packages/binding-mqtt/src/mqtt-client.ts | 2 +- packages/binding-opcua/src/opcua-protocol-client.ts | 6 ++---- packages/core/src/exposed-thing.ts | 2 +- 9 files changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/binding-coap/src/coap-client.ts b/packages/binding-coap/src/coap-client.ts index 9cddc9033..b79f58f23 100644 --- a/packages/binding-coap/src/coap-client.ts +++ b/packages/binding-coap/src/coap-client.ts @@ -159,7 +159,7 @@ export default class CoapClient implements ProtocolClient { debug(`CoapClient received Content-Format: ${res.headers["Content-Format"]}`); // FIXME does not work with blockwise because of node-coap - const contentType = res.headers["Content-Format"] ?? form.contentType ?? ContentSerdes.DEFAULT; + const contentType = res.headers["Content-Format"] ?? form.contentType; res.on("data", (data: Buffer) => { next(new Content(`${contentType}`, Readable.from(res.payload))); @@ -253,7 +253,7 @@ export default class CoapClient implements ProtocolClient { const method = form["cov:method"] ?? defaultMethod; debug(`CoapClient got Form "method" ${method}`); - const contentFormat = form["cov:contentFormat"] ?? form.contentType ?? "application/json"; + const contentFormat = form["cov:contentFormat"] ?? form.contentType; debug(`"CoapClient got Form 'contentType' ${contentFormat} `); const accept = form["cov:accept"]; diff --git a/packages/binding-coap/src/coap-server.ts b/packages/binding-coap/src/coap-server.ts index 6340f9265..342570377 100644 --- a/packages/binding-coap/src/coap-server.ts +++ b/packages/binding-coap/src/coap-server.ts @@ -939,7 +939,7 @@ export default class CoapServer implements ProtocolServer { ); } - return contentType ?? ContentSerdes.DEFAULT; + return contentType; } private checkContentTypeSupportForInput(method: string, contentType: string): boolean { diff --git a/packages/binding-coap/src/coaps-client.ts b/packages/binding-coap/src/coaps-client.ts index 6379f6a9c..154007b2d 100644 --- a/packages/binding-coap/src/coaps-client.ts +++ b/packages/binding-coap/src/coaps-client.ts @@ -47,7 +47,7 @@ export default class CoapsClient implements ProtocolClient { debug(`CoapsClient received ${res.code} from ${form.href}`); // FIXME: Add toString conversion for response Content-Format - const contentType = form.contentType ?? ContentSerdes.DEFAULT; + const contentType = form.contentType; const body = Readable.from(res.payload ?? Buffer.alloc(0)); resolve(new Content(contentType, body)); @@ -79,7 +79,7 @@ export default class CoapsClient implements ProtocolClient { debug(`CoapsClient received ${res.code} from ${form.href}`); // FIXME: Add toString conversion for response Content-Format - const contentType = form.contentType ?? ContentSerdes.DEFAULT; + const contentType = form.contentType; const body = Readable.from(res.payload ?? Buffer.alloc(0)); resolve(new Content(contentType, body)); @@ -118,7 +118,7 @@ export default class CoapsClient implements ProtocolClient { const callback = (resp: CoapResponse) => { if (resp.payload != null) { - next(new Content(form?.contentType ?? ContentSerdes.DEFAULT, Readable.from(resp.payload))); + next(new Content(form?.contentType, Readable.from(resp.payload))); } }; diff --git a/packages/binding-http/src/subscription-protocols.ts b/packages/binding-http/src/subscription-protocols.ts index 024447be9..d68d43d75 100644 --- a/packages/binding-http/src/subscription-protocols.ts +++ b/packages/binding-http/src/subscription-protocols.ts @@ -117,7 +117,7 @@ export class SSESubscription implements InternalSubscription { }; this.eventSource.onmessage = (event) => { debug(`HttpClient received ${JSON.stringify(event)} from ${this.form.href}`); - const output = new Content(this.form.contentType ?? ContentSerdes.DEFAULT, Readable.from(event.data)); + const output = new Content(this.form.contentType, Readable.from(event.data)); next(output); }; this.eventSource.onerror = function (event) { diff --git a/packages/binding-modbus/src/modbus-connection.ts b/packages/binding-modbus/src/modbus-connection.ts index 71cc813f7..28a078a02 100644 --- a/packages/binding-modbus/src/modbus-connection.ts +++ b/packages/binding-modbus/src/modbus-connection.ts @@ -451,7 +451,7 @@ export class PropertyOperation { this.quantity = form["modv:quantity"]; this.function = form["modv:function"] as ModbusFunction; this.endianness = endianness; - this.contentType = form.contentType ?? ContentSerdes.DEFAULT; + this.contentType = form.contentType; this.content = content; this.transaction = null; } diff --git a/packages/binding-mqtt/src/mqtt-broker-server.ts b/packages/binding-mqtt/src/mqtt-broker-server.ts index af092ae13..dbbc6e305 100644 --- a/packages/binding-mqtt/src/mqtt-broker-server.ts +++ b/packages/binding-mqtt/src/mqtt-broker-server.ts @@ -263,7 +263,7 @@ export default class MqttBrokerServer implements ProtocolServer { * https://github.com/mqttjs/MQTT.js/pull/1103 * For further discussion see https://github.com/eclipse-thingweb/node-wot/pull/253 */ - const contentType = packet?.properties?.contentType ?? ContentSerdes.DEFAULT; + const contentType = packet?.properties?.contentType; const options: InteractionOptions & { formIndex: number } = { formIndex: ProtocolHelpers.findRequestMatchingFormIndex( @@ -274,7 +274,7 @@ export default class MqttBrokerServer implements ProtocolServer { ), }; - const formContentType = action.forms[options.formIndex].contentType ?? ContentSerdes.DEFAULT; + const formContentType = action.forms[options.formIndex].contentType; const inputContent = new Content(formContentType, Readable.from(payload)); thing @@ -306,7 +306,7 @@ export default class MqttBrokerServer implements ProtocolServer { ) { const readOnly = property.readOnly ?? false; if (!readOnly) { - const contentType = packet?.properties?.contentType ?? ContentSerdes.DEFAULT; + const contentType = packet?.properties?.contentType; const options: InteractionOptions & { formIndex: number } = { formIndex: ProtocolHelpers.findRequestMatchingFormIndex( @@ -317,7 +317,7 @@ export default class MqttBrokerServer implements ProtocolServer { ), }; - const formContentType = property.forms[options.formIndex].contentType ?? ContentSerdes.DEFAULT; + const formContentType = property.forms[options.formIndex].contentType; const inputContent = new Content(formContentType, Readable.from(payload)); try { diff --git a/packages/binding-mqtt/src/mqtt-client.ts b/packages/binding-mqtt/src/mqtt-client.ts index 7cdc12d2a..a351a3827 100644 --- a/packages/binding-mqtt/src/mqtt-client.ts +++ b/packages/binding-mqtt/src/mqtt-client.ts @@ -50,7 +50,7 @@ export default class MqttClient implements ProtocolClient { error?: (error: Error) => void, complete?: () => void ): Promise { - const contentType = form.contentType ?? ContentSerdes.DEFAULT; + const contentType = form.contentType; const requestUri = new url.URL(form.href); const brokerUri: string = `${this.scheme}://` + requestUri.host; // Keeping the path as the topic for compatibility reasons. diff --git a/packages/binding-opcua/src/opcua-protocol-client.ts b/packages/binding-opcua/src/opcua-protocol-client.ts index 85ac6813d..67748932b 100644 --- a/packages/binding-opcua/src/opcua-protocol-client.ts +++ b/packages/binding-opcua/src/opcua-protocol-client.ts @@ -466,7 +466,7 @@ export class OPCUAProtocolClient implements ProtocolClient { /// private async _dataValueToContent(form: OPCUAForm, dataValue: DataValue): Promise { - const contentType = form.contentType ?? "application/json"; + const contentType = form.contentType; // QUESTION: how can we extend the default contentSerDes.valueToContent for application/json, const contentSerDes = ContentSerdes.get(); @@ -615,13 +615,11 @@ export class OPCUAProtocolClient implements ProtocolClient { ): Promise { const outputArguments = (argumentDefinition.outputArguments ?? []) as unknown as Argument[]; - const contentType = form.contentType ?? "application/json"; - const body: Record = {}; for (let index = 0; index < outputArguments.length; index++) { const argument = outputArguments[index]; const { name } = argument; - const element = _variantToJSON(outputVariants[index], contentType); + const element = _variantToJSON(outputVariants[index], form.contentType); body[name ?? "null"] = element; } diff --git a/packages/core/src/exposed-thing.ts b/packages/core/src/exposed-thing.ts index 57074c84d..a17ec95e6 100644 --- a/packages/core/src/exposed-thing.ts +++ b/packages/core/src/exposed-thing.ts @@ -412,7 +412,7 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing { return ContentManager.valueToContent( result, this.properties[propertyName], - form?.contentType ?? "application/json" + form.contentType ); } else { throw new Error(`ExposedThing '${this.title}' has no readHandler for Property '${propertyName}'`); From 01cc16e89a9695384783af189ea68b09946277b6 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Wed, 29 Nov 2023 13:53:57 +0100 Subject: [PATCH 3/3] fixup! refactor: remove now obsolete nullish coalescing --- packages/binding-opcua/src/opcua-protocol-client.ts | 2 +- packages/core/src/exposed-thing.ts | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/binding-opcua/src/opcua-protocol-client.ts b/packages/binding-opcua/src/opcua-protocol-client.ts index 67748932b..4d221a2b8 100644 --- a/packages/binding-opcua/src/opcua-protocol-client.ts +++ b/packages/binding-opcua/src/opcua-protocol-client.ts @@ -619,7 +619,7 @@ export class OPCUAProtocolClient implements ProtocolClient { for (let index = 0; index < outputArguments.length; index++) { const argument = outputArguments[index]; const { name } = argument; - const element = _variantToJSON(outputVariants[index], form.contentType); + const element = _variantToJSON(outputVariants[index], form.contentType); body[name ?? "null"] = element; } diff --git a/packages/core/src/exposed-thing.ts b/packages/core/src/exposed-thing.ts index a17ec95e6..796b61f21 100644 --- a/packages/core/src/exposed-thing.ts +++ b/packages/core/src/exposed-thing.ts @@ -409,11 +409,7 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing { const form = this.properties[propertyName]?.forms[options.formIndex] ?? { contentType: "application/json", }; - return ContentManager.valueToContent( - result, - this.properties[propertyName], - form.contentType - ); + return ContentManager.valueToContent(result, this.properties[propertyName], form.contentType); } else { throw new Error(`ExposedThing '${this.title}' has no readHandler for Property '${propertyName}'`); }