Skip to content

Commit

Permalink
fixup! refactor(coap-server): refactor request handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jul 24, 2023
1 parent 1b1766b commit 6e02263
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Servient, {
createLoggers,
} from "@node-wot/core";
import { Socket } from "dgram";
import { Server, createServer, registerFormat, IncomingMessage, OutgoingMessage } from "coap";
import { Server, createServer, registerFormat, IncomingMessage, OutgoingMessage, OptionValue } from "coap";

Check failure on line 31 in packages/binding-coap/src/coap-server.ts

View workflow job for this annotation

GitHub Actions / eslint

'OptionValue' is defined but never used
import slugify from "slugify";
import { Readable } from "stream";
import { MdnsIntroducer } from "./mdns-introducer";
Expand Down Expand Up @@ -339,26 +339,42 @@ export default class CoapServer implements ProtocolServer {
return;
}

const accept = req.headers.Accept;
const { contentType, isSupported } = this.processAcceptValue(req);

if (!isSupported) {
this.sendResponse(res, "4.06", `Content-Format ${contentType} is not supported by this resource.`);
return;
}

const contentSerdes = ContentSerdes.get();
const content = ContentSerdes.get().valueToContent(thing.getThingDescription(), undefined, contentType);
const payload = await ProtocolHelpers.readStreamFully(content.body);

const isUnsupportedAcceptValue = typeof accept === "string" && !contentSerdes.isSupported(accept);
debug(`Sending CoAP response for TD with Content-Format ${contentType}.`);
this.sendContentResponse(res, payload, contentType);
}

if (isUnsupportedAcceptValue) {
debug(`Request contained an accept option with value ${accept} which is not supported.`);
this.sendResponse(res, "4.06", `Content-Format ${accept} is not supported by this resource.`);
return;
private processAcceptValue(req: IncomingMessage) {
const accept = req.headers.Accept;

if (typeof accept !== "string") {
debug(`Request contained no Accept option.`);
return {
contentType: ContentSerdes.TD,
isSupported: true,
};
}

debug(`Received an available or no Content-Format (${accept}) in Accept option.`);
const contentFormat = (accept as string) ?? ContentSerdes.TD;
const isSupported = ContentSerdes.get().isSupported(accept);

const content = contentSerdes.valueToContent(thing.getThingDescription(), undefined, contentFormat);
const payload = await ProtocolHelpers.readStreamFully(content.body);
if (!isSupported) {
debug(`Request contained an accept option with value ${accept} which is not supported.`);
}

debug(`Sending CoAP response for TD with Content-Format ${contentFormat}.`);
this.sendContentResponse(res, payload, contentFormat);
debug(`Received an available Content-Format ${accept} in Accept option.`);
return {
contentType: accept,
isSupported,
};
}

private async handleRequest(req: IncomingMessage, res: OutgoingMessage) {
Expand Down

0 comments on commit 6e02263

Please sign in to comment.