Skip to content

Commit

Permalink
chore(binding-coap): enable strict-boolean-expressions and null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Sep 18, 2023
1 parent 227be53 commit fd72a5d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
5 changes: 4 additions & 1 deletion packages/binding-coap/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "../../.eslintrc.js"
"extends": "../../.eslintrc.js",
"rules": {
"@typescript-eslint/strict-boolean-expressions": ["error"]
}
}
13 changes: 6 additions & 7 deletions packages/binding-coap/src/coap-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default class CoapClient implements ProtocolClient {
});
req.on("error", (err: Error) => reject(err));
(async () => {
if (content && content.body) {
if (content != null) {
const buffer = await content.toBuffer();
req.setOption("Content-Format", content.type);
req.write(buffer);
Expand Down Expand Up @@ -159,8 +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
let contentType = res.headers["Content-Format"];
if (!contentType) contentType = form.contentType;
const contentType = res.headers["Content-Format"] ?? form.contentType ?? ContentSerdes.DEFAULT;

res.on("data", (data: Buffer) => {
next(new Content(`${contentType}`, Readable.from(res.payload)));
Expand Down Expand Up @@ -203,10 +202,10 @@ export default class CoapClient implements ProtocolClient {

const options: CoapRequestParams = {
agent: this.agent,
hostname: requestUri.hostname || "",
port: requestUri.port ? parseInt(requestUri.port, 10) : 5683,
pathname: requestUri.pathname || "",
query: requestUri.query || "",
hostname: requestUri.hostname ?? "",
port: requestUri.port != null ? parseInt(requestUri.port, 10) : 5683,
pathname: requestUri.pathname ?? "",
query: requestUri.query ?? "",
observe: false,
multicast: false,
confirmable: true,
Expand Down
12 changes: 5 additions & 7 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export default class CoapServer implements ProtocolServer {
this.sendContentResponse(res, payload, contentType);
}

private processAcceptValue(req: IncomingMessage) {
private processAcceptValue(req: IncomingMessage): { contentType: string; isSupported: boolean } {
const accept = req.headers.Accept;

if (typeof accept !== "string") {
Expand All @@ -371,7 +371,7 @@ export default class CoapServer implements ProtocolServer {
};
}

const isSupported = ContentSerdes.get().isSupported(accept);
const isSupported: boolean = ContentSerdes.get().isSupported(accept);

if (!isSupported) {
debug(`Request contained an accept option with value ${accept} which is not supported.`);
Expand Down Expand Up @@ -550,9 +550,7 @@ export default class CoapServer implements ProtocolServer {
res.end();

res.on("finish", (err: Error) => {
if (err) {
error(`CoapServer on port ${this.port} failed on observe with: ${err.message}`);
}
error(`CoapServer on port ${this.port} failed on observe with: ${err.message}`);

Check warning on line 553 in packages/binding-coap/src/coap-server.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-coap/src/coap-server.ts#L553

Added line #L553 was not covered by tests
thing.handleUnobserveProperty(affordanceKey, listener, interactionOptions);
});

Expand Down Expand Up @@ -653,7 +651,7 @@ export default class CoapServer implements ProtocolServer {
new Content(contentType, Readable.from(req.payload)),
interactionOptions
);
if (output) {
if (output != null) {
this.streamContentResponse(res, output, { end: true });
} else {
this.sendChangedResponse(res);
Expand Down Expand Up @@ -791,7 +789,7 @@ export default class CoapServer implements ProtocolServer {
return contentType ?? ContentSerdes.DEFAULT;
}

private checkContentTypeSupportForInput(method: string, contentType: string) {
private checkContentTypeSupportForInput(method: string, contentType: string): boolean {
const methodsWithPayload: string[] = ["PUT", "POST", "FETCH", "iPATCH", "PATCH"];
const notAMethodWithPayload = !methodsWithPayload.includes(method);

Expand Down
3 changes: 2 additions & 1 deletion packages/binding-coap/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
"rootDir": "src",
"strictNullChecks": true
},
"include": ["src/**/*"],
"references": [{ "path": "../td-tools" }, { "path": "../core" }]
Expand Down

0 comments on commit fd72a5d

Please sign in to comment.