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

Function-based discovery API alternative #864

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b8e5d92
chore!: bump tsc target to es2018
JKRhb May 26, 2022
1030684
feat!: add proposal for new discovery API
JKRhb May 26, 2022
96be7f4
fixup! feat!: add proposal for new discovery API
JKRhb May 26, 2022
7f40323
fixup! feat!: add proposal for new discovery API
JKRhb May 26, 2022
d68f9df
fixup! feat!: add proposal for new discovery API
JKRhb May 26, 2022
880558e
fixup! feat!: add proposal for new discovery API
JKRhb May 26, 2022
d1ec05f
fixup! feat!: add proposal for new discovery API
JKRhb May 26, 2022
37a1620
fixup! feat!: add proposal for new discovery API
JKRhb Aug 9, 2022
6ee7d4b
fixup! feat!: add proposal for new discovery API
JKRhb Aug 9, 2022
cf5a391
fixup! feat!: add proposal for new discovery API
JKRhb Aug 9, 2022
734f86b
fixup! feat!: add proposal for new discovery API
JKRhb Aug 9, 2022
04b7b2f
fixup! feat!: add proposal for new discovery API
JKRhb Aug 9, 2022
5440bea
fixup! feat!: add proposal for new discovery API
JKRhb Nov 7, 2022
db320c1
fixup! feat!: add proposal for new discovery API
JKRhb Nov 7, 2022
74b0e37
fixup! feat!: add proposal for new discovery API
JKRhb Nov 7, 2022
2207a91
fixup! feat!: add proposal for new discovery API
JKRhb Nov 7, 2022
ad43508
fixup! feat!: add proposal for new discovery API
JKRhb Nov 7, 2022
b869b5c
fixup! feat!: add proposal for new discovery API
JKRhb Sep 19, 2023
961606e
fixup! feat!: add proposal for new discovery API
JKRhb Sep 19, 2023
950faa3
fixup! feat!: add proposal for new discovery API
JKRhb Sep 19, 2023
5636983
fixup! feat!: add proposal for new discovery API
JKRhb Oct 23, 2023
88252f0
fixup! feat!: add proposal for new discovery API
JKRhb Oct 23, 2023
44af619
fixup! feat!: add proposal for new discovery API
JKRhb Nov 21, 2023
495b857
fixup! feat!: add proposal for new discovery API
JKRhb Nov 21, 2023
ccd7751
fixup! feat!: add proposal for new discovery API
JKRhb Nov 21, 2023
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
15 changes: 15 additions & 0 deletions packages/binding-coap/src/coap-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@
registerFormat(ContentSerdes.JSON_LD, 2100);
}

discoverDirectly(uri: string): Promise<Content> {
const options: CoapRequestParams = this.uriToOptions(uri);
const req = this.agent.request(options);

req.setOption("Accept", "application/td+json");
return new Promise<Content>((resolve, reject) => {
req.on("response", (res: IncomingMessage) => {
const contentType = (res.headers["Content-Format"] as string) ?? "application/td+json";
resolve(new Content(contentType, Readable.from(res.payload)));
});
req.on("error", (err: Error) => reject(err));
req.end();
});
}

Check warning on line 71 in packages/binding-coap/src/coap-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-coap/src/coap-client.ts#L59-L71

Added lines #L59 - L71 were not covered by tests

public toString(): string {
return "[CoapClient]";
}
Expand Down
4 changes: 4 additions & 0 deletions packages/binding-coap/src/coaps-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,8 @@

return req;
}

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}

Check warning on line 241 in packages/binding-coap/src/coaps-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-coap/src/coaps-client.ts#L240-L241

Added lines #L240 - L241 were not covered by tests
}
4 changes: 4 additions & 0 deletions packages/binding-file/src/file-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@
}

public setSecurity = (metadata: Array<SecurityScheme>): boolean => false;

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}

Check warning on line 96 in packages/binding-file/src/file-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-file/src/file-client.ts#L93-L96

Added lines #L93 - L96 were not covered by tests
}
11 changes: 11 additions & 0 deletions packages/binding-http/src/http-client-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,15 @@

return url;
}

async discoverDirectly(uri: string): Promise<Content> {
// Note: This is still work in progress
const headers: HeadersInit = {
Accept: "application/td+json",
};
const response = await fetch(uri, { headers });
// TODO: Result should be validated
const body = ProtocolHelpers.toNodeStream(response.body as Readable);
return new Content(response.headers.get("content-type") ?? "application/td+json", body);
}

Check warning on line 422 in packages/binding-http/src/http-client-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-http/src/http-client-impl.ts#L414-L422

Added lines #L414 - L422 were not covered by tests
}
4 changes: 4 additions & 0 deletions packages/binding-mbus/src/mbus-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,8 @@

return result;
}

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}

Check warning on line 150 in packages/binding-mbus/src/mbus-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-mbus/src/mbus-client.ts#L149-L150

Added lines #L149 - L150 were not covered by tests
}
4 changes: 4 additions & 0 deletions packages/binding-modbus/src/modbus-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,8 @@

return result as ModbusFormWithDefaults;
}

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}

Check warning on line 315 in packages/binding-modbus/src/modbus-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-modbus/src/modbus-client.ts#L314-L315

Added lines #L314 - L315 were not covered by tests
}
4 changes: 4 additions & 0 deletions packages/binding-mqtt/src/mqtt-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,8 @@
return (qos = 0);
}
}

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}

Check warning on line 205 in packages/binding-mqtt/src/mqtt-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-mqtt/src/mqtt-client.ts#L204-L205

Added lines #L204 - L205 were not covered by tests
}
4 changes: 4 additions & 0 deletions packages/binding-netconf/src/netconf-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,8 @@
this.credentials = credentials;
return true;
}

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}

Check warning on line 181 in packages/binding-netconf/src/netconf-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-netconf/src/netconf-client.ts#L180-L181

Added lines #L180 - L181 were not covered by tests
}
4 changes: 4 additions & 0 deletions packages/binding-opcua/src/opcua-protocol-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,4 +620,8 @@

return new Content("application/json", Readable.from(JSON.stringify(body)));
}

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}

Check warning on line 626 in packages/binding-opcua/src/opcua-protocol-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-opcua/src/opcua-protocol-client.ts#L625-L626

Added lines #L625 - L626 were not covered by tests
}
4 changes: 4 additions & 0 deletions packages/binding-websockets/src/ws-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export default class WebSocketClient implements ProtocolClient {
// TODO: implement and remove eslint-ignore-useless-constructor
}

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}

public toString(): string {
return `[WebSocketClient]`;
}
Expand Down
28 changes: 28 additions & 0 deletions packages/core/example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { Servient } = require("@node-wot/core");
const { HttpClientFactory } = require("@node-wot/binding-http");
const { CoapClientFactory } = require("@node-wot/binding-coap");

// Note: This example is just for testing/demonstration purposes and
// will eventually be removed/moved to the examples package.
async function discover() {
const servient = new Servient();
servient.addClientFactory(new HttpClientFactory());
servient.addClientFactory(new CoapClientFactory());
const wot = await servient.start();

const httpUrl = "http://plugfest.thingweb.io:8083/smart-coffee-machine";
const coapUrl = "coap://plugfest.thingweb.io:5683/smart-coffee-machine";

for (const url of [httpUrl, coapUrl]) {
console.log(await wot.discovery.direct(url));
}

// Alternative approach
for (const url of [httpUrl, coapUrl]) {
for await (const result of wot.discovery.directIterator(url)) {
console.log(result);
}
}
}

discover();
2 changes: 2 additions & 0 deletions packages/core/src/protocol-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export interface ProtocolClient {
/** this client is requested to perform an "unlink" on the resource with the given URI */
unlinkResource(form: TD.Form): Promise<void>;

discoverDirectly(uri: string): Promise<Content>;

subscribeResource(
form: TD.Form,
next: (content: Content) => void,
Expand Down
53 changes: 49 additions & 4 deletions packages/core/src/wot-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,73 @@
import ExposedThing from "./exposed-thing";
import ConsumedThing from "./consumed-thing";
import Helpers from "./helpers";
import { ThingDescription } from "wot-thing-description-types";
import { createLoggers } from "./logger";
import ContentManager from "./content-serdes";

const { debug } = createLoggers("core", "wot-impl");

class ThingDiscoveryProcess implements WoT.ThingDiscoveryProcess {
#done = false;

get done() {
return this.#done;
}

Check warning on line 33 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L32-L33

Added lines #L32 - L33 were not covered by tests

#error?: Error;

get error() {
return this.#error;
}

Check warning on line 39 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L38-L39

Added lines #L38 - L39 were not covered by tests

#filter: WoT.ThingFilter;

constructor(filter?: WoT.ThingFilter) {
this.#filter = filter ?? {};
}

Check warning on line 45 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L44-L45

Added lines #L44 - L45 were not covered by tests

filter?: WoT.ThingFilter | undefined;

[Symbol.asyncIterator](): AsyncIterator<ThingDescription> {
throw new Error("Method not implemented.");
}

Check warning on line 51 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L50-L51

Added lines #L50 - L51 were not covered by tests

stop(): void {
this.#done = true;
}

Check warning on line 55 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L54-L55

Added lines #L54 - L55 were not covered by tests
}

export default class WoTImpl {
private srv: Servient;

constructor(srv: Servient) {
this.srv = srv;
}

/** @inheritDoc */
async discover(filter?: WoT.ThingFilter): Promise<WoT.ThingDiscoveryProcess> {
throw new Error("not implemented");
// TODO: Implement this function
return new ThingDiscoveryProcess(filter);

Check warning on line 68 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L67-L68

Added lines #L67 - L68 were not covered by tests
}

/** @inheritDoc */
async exploreDirectory(url: string, filter?: WoT.ThingFilter): Promise<WoT.ThingDiscoveryProcess> {
throw new Error("not implemented");
return new ThingDiscoveryProcess(filter);

Check warning on line 73 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L73

Added line #L73 was not covered by tests
}

async requestThingDescription(url: string): Promise<WoT.ThingDescription> {
throw new Error("not implemented");
/** @inheritDoc */
async requestThingDescription(url: string): Promise<ThingDescription> {
const uriScheme = new URL(url).protocol.split(":")[0];
const client = this.srv.getClientFor(uriScheme);
const result = await client.discoverDirectly(url);

const value = ContentManager.contentToValue({ type: result.type, body: await result.toBuffer() }, {});

if (value instanceof Object) {
return value as ThingDescription;
}

throw new Error("Not found.");

Check warning on line 88 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L78-L88

Added lines #L78 - L88 were not covered by tests
}

/** @inheritDoc */
Expand Down
12 changes: 12 additions & 0 deletions packages/core/test/ClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ class TDClient implements ProtocolClient {
public toString(): string {
return "TDClient";
}

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("discoverDirectly not implemented.");
}
}

class TDClientFactory implements ProtocolClientFactory {
Expand Down Expand Up @@ -239,6 +243,10 @@ class TrapClient implements ProtocolClient {
}

public setSecurity = (metadata: SecurityScheme[]) => false;

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("discoverDirectly not implemented.");
}
}

class TrapClientFactory implements ProtocolClientFactory {
Expand Down Expand Up @@ -306,6 +314,10 @@ class TestProtocolClient implements ProtocolClient {
this.securitySchemes = securitySchemes;
return true;
}

async discoverDirectly(uri: string): Promise<Content> {
throw new Error("discoverDirectly not implemented.");
}
}

@suite("client flow of servient")
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es6",
"target": "es2018",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real review yet.. just since I stumbled over this change

I am not sure but I think we used to have issues with the browser-bundle when increasing the target version.. I might be wrong though...

"lib": ["dom"],
"skipLibCheck": false,
"module": "commonjs",
Expand Down
Loading