Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(coap): fix eslint issues #588

Merged
merged 2 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions packages/binding-coap/src/coap-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class CoapClient implements ProtocolClient {
});
}

public writeResource(form: CoapForm, content: Content): Promise<any> {
public writeResource(form: CoapForm, content: Content): Promise<void> {
return new Promise<void>((resolve, reject) => {
ProtocolHelpers.readStreamFully(content.body)
.then((buffer) => {
Expand Down Expand Up @@ -105,23 +105,13 @@ export default class CoapClient implements ProtocolClient {

console.debug("[binding-coap]", `CoapClient sending ${req.statusCode} to ${form.href}`);

req.on(
"response",
(res: {
code: string;
headers: { "Content-Format"?: string };
payload: Iterable<any> | AsyncIterable<any>;
}) => {
console.debug("[binding-coap]", `CoapClient received ${res.code} from ${form.href}`);
console.debug(
"[binding-coap]",
`CoapClient received Content-Format: ${res.headers["Content-Format"]}`
);
console.debug("[binding-coap]", `CoapClient received headers: ${JSON.stringify(res.headers)}`);
const contentType = res.headers["Content-Format"];
resolve({ type: contentType || "", body: Readable.from(res.payload) });
}
);
req.on("response", (res: { code: string; headers: { "Content-Format"?: string }; payload: Buffer }) => {
console.debug("[binding-coap]", `CoapClient received ${res.code} from ${form.href}`);
console.debug("[binding-coap]", `CoapClient received Content-Format: ${res.headers["Content-Format"]}`);
console.debug("[binding-coap]", `CoapClient received headers: ${JSON.stringify(res.headers)}`);
const contentType = res.headers["Content-Format"];
resolve({ type: contentType || "", body: Readable.from(res.payload) });
});
req.on("error", (err: Error) => reject(err));
(async () => {
if (content && content.body) {
Expand All @@ -134,7 +124,7 @@ export default class CoapClient implements ProtocolClient {
});
}

public unlinkResource(form: CoapForm): Promise<any> {
public unlinkResource(form: CoapForm): Promise<void> {
return new Promise<void>((resolve, reject) => {
const req = this.generateRequest(form, "GET", false);

Expand All @@ -152,8 +142,8 @@ export default class CoapClient implements ProtocolClient {

public subscribeResource(
form: CoapForm,
next: (value: any) => void,
error?: (error: any) => void,
next: (value: Content) => void,
error?: (error: Error) => void,
complete?: () => void
): Promise<Subscription> {
return new Promise<Subscription>((resolve, reject) => {
Expand All @@ -169,7 +159,7 @@ export default class CoapClient implements ProtocolClient {
let contentType = res.headers["Content-Format"];
if (!contentType) contentType = form.contentType;

res.on("data", (data: any) => {
res.on("data", (data: Buffer) => {
next({ type: contentType, body: Readable.from(res.payload) });
});

Expand Down
13 changes: 6 additions & 7 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
* CoAP Server based on coap by mcollina
*/

import * as url from "url";

import * as TD from "@node-wot/td-tools";
import Servient, { ProtocolServer, ContentSerdes, ExposedThing, Helpers, ProtocolHelpers } from "@node-wot/core";
import coap = require("coap");
import slugify from "slugify";
import { Socket } from "dgram";

export default class CoapServer implements ProtocolServer {
public readonly scheme: string = "coap";
Expand Down Expand Up @@ -86,7 +85,7 @@ export default class CoapServer implements ProtocolServer {
}

/** returns socket to be re-used by CoapClients */
public getSocket(): any {
public getSocket(): Socket {
return this.server._sock;
}

Expand Down Expand Up @@ -214,7 +213,7 @@ export default class CoapServer implements ProtocolServer {
);
});

const requestUri = url.parse(req.url);
const requestUri = req.url;
let contentType = req.options["Content-Format"];

if (req.method === "PUT" || req.method === "POST") {
Expand Down Expand Up @@ -299,7 +298,7 @@ export default class CoapServer implements ProtocolServer {
);
const content = ContentSerdes.get().valueToContent(
value,
<any>property,
property,
contentType
);
res.setOption("Content-Format", content.type);
Expand Down Expand Up @@ -330,7 +329,7 @@ export default class CoapServer implements ProtocolServer {
);
const content = ContentSerdes.get().valueToContent(
value,
<any>property,
property,
contentType
);
res.setOption("Content-Format", content.type);
Expand Down Expand Up @@ -367,7 +366,7 @@ export default class CoapServer implements ProtocolServer {
try {
value = ContentSerdes.get().contentToValue(
{ type: contentType, body: req.payload },
<any>property
property
);
} catch (err) {
console.warn(
Expand Down
2 changes: 1 addition & 1 deletion packages/binding-coap/src/coap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export * from "./coaps-client";

export class CoapOption {
public "cov:optionName": string;
public "cov:optionValue": any;
public "cov:optionValue": string | number | Buffer;
}

export type CoapMethodName = "GET" | "POST" | "PUT" | "DELETE" | "FETCH" | "PATCH" | "iPATCH";
Expand Down
53 changes: 34 additions & 19 deletions packages/binding-coap/src/coaps-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ import { Subscription } from "rxjs/Subscription";

import { ProtocolClient, Content, ProtocolHelpers } from "@node-wot/core";
import { CoapForm, CoapMethodName, isValidCoapMethod, isSupportedCoapMethod } from "./coap";
import { CoapClient as coaps, RequestMethod } from "node-coap-client";
import { CoapClient as coaps, CoapResponse, RequestMethod, SecurityParameters } from "node-coap-client";
import { Readable } from "stream";

declare interface pskSecurityParameters {
[identity: string]: string;
}

export default class CoapsClient implements ProtocolClient {
// FIXME coap Agent closes socket when no messages in flight -> new socket with every request
private authorization: any;
private authorization: SecurityParameters;

public toString(): string {
return "[CoapsClient]";
Expand All @@ -36,30 +41,30 @@ export default class CoapsClient implements ProtocolClient {
public readResource(form: CoapForm): Promise<Content> {
return new Promise<Content>((resolve, reject) => {
this.generateRequest(form, "GET")
.then((res: any) => {
.then((res: CoapResponse) => {
console.debug("[binding-coap]", `CoapsClient received ${res.code} from ${form.href}`);

// FIXME node-coap-client does not support options
let contentType; // = res.format[...]
if (!contentType) contentType = form.contentType;

resolve({ type: contentType, body: res.payload });
resolve({ type: contentType, body: Readable.from(res.payload) });
})
.catch((err: any) => {
.catch((err: Error) => {
reject(err);
});
});
}

public writeResource(form: CoapForm, content: Content): Promise<any> {
public writeResource(form: CoapForm, content: Content): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.generateRequest(form, "PUT", content)
.then((res: any) => {
.then((res: CoapResponse) => {
console.debug("[binding-coap]", `CoapsClient received ${res.code} from ${form.href}`);

resolve();
})
.catch((err: any) => {
.catch((err: Error) => {
reject(err);
});
});
Expand All @@ -68,47 +73,53 @@ export default class CoapsClient implements ProtocolClient {
public invokeResource(form: CoapForm, content?: Content): Promise<Content> {
return new Promise<Content>((resolve, reject) => {
this.generateRequest(form, "POST", content)
.then((res: any) => {
.then((res: CoapResponse) => {
console.debug("[binding-coap]", `CoapsClient received ${res.code} from ${form.href}`);

// FIXME node-coap-client does not support options
let contentType; // = res.format[...]
if (!contentType) contentType = form.contentType;

resolve({ type: contentType, body: res.payload });
resolve({ type: contentType, body: Readable.from(res.payload) });
})
.catch((err: any) => {
.catch((err: Error) => {
reject(err);
});
});
}

public unlinkResource(form: CoapForm): Promise<any> {
public unlinkResource(form: CoapForm): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.generateRequest(form, "DELETE")
.then((res: any) => {
.then((res: CoapResponse) => {
console.debug("[binding-coap]", `CoapsClient received ${res.code} from ${form.href}`);
console.debug("[binding-coap]", `CoapsClient received headers: ${JSON.stringify(res.format)}`);
resolve();
})
.catch((err: any) => {
.catch((err: Error) => {
reject(err);
});
});
}

public subscribeResource(
form: CoapForm,
next: (value: any) => void,
error?: (error: any) => void,
next: (value: Content) => void,
error?: (error: Error) => void,
complete?: () => void
): Promise<Subscription> {
return new Promise<Subscription>((resolve, reject) => {
const requestUri = new URL(form.href.replace(/$coaps/, "https"));
coaps.setSecurityParams(requestUri.hostname, this.authorization);

const callback = (resp: CoapResponse) => {
if (resp.payload != null) {
next({ type: form?.contentType, body: Readable.from(resp.payload) });
}
};

coaps
.observe(form.href, "get", next)
.observe(form.href, "get", callback)
.then(() => {
resolve(
new Subscription(() => {
Expand All @@ -132,7 +143,7 @@ export default class CoapsClient implements ProtocolClient {
// FIXME coap does not provide proper API to close Agent
}

public setSecurity(metadata: Array<TD.SecurityScheme>, credentials?: any): boolean {
public setSecurity(metadata: Array<TD.SecurityScheme>, credentials?: pskSecurityParameters): boolean {
if (metadata === undefined || !Array.isArray(metadata) || metadata.length === 0) {
console.warn("[binding-coap]", `CoapsClient received empty security metadata`);
return false;
Expand Down Expand Up @@ -193,7 +204,11 @@ export default class CoapsClient implements ProtocolClient {
return defaultMethod;
}

private async generateRequest(form: CoapForm, defaultMethod: CoapMethodName, content?: Content): Promise<any> {
private async generateRequest(
form: CoapForm,
defaultMethod: CoapMethodName,
content?: Content
): Promise<CoapResponse> {
// url only works with http*
const requestUri = new URL(form.href.replace(/$coaps/, "https"));
coaps.setSecurityParams(requestUri.hostname, this.authorization);
Expand Down
1 change: 0 additions & 1 deletion packages/binding-coap/test/coap-client-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import { suite, test, timeout } from "@testdeck/mocha";
import { Done } from "mocha";
import { expect } from "chai";

import CoapServer from "../src/coap-server";
Expand Down