Skip to content

Commit

Permalink
feat(core): implement exploreDirectory method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 14, 2023
1 parent bfcf85c commit 77b916f
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion packages/core/src/wot-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,60 @@ import Helpers from "./helpers";
import { createLoggers } from "./logger";
import ContentManager from "./content-serdes";
import { ErrorObject } from "ajv";
import { ThingDescription } from "wot-thing-description-types";

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

// @ts-expect-error Typescript currently encounters an error here that *should* be a false positve
class ExploreDirectoryDatasource implements UnderlyingDefaultSource<WoT.ThingDescription> {
constructor(rawThingDescriptions: WoT.DataSchemaValue) {
this.rawThingDescriptions = rawThingDescriptions;
}

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

rawThingDescriptions: WoT.DataSchemaValue;

start(controller: ReadableStreamDefaultController<WoT.ThingDescription>) {
if (!(this.rawThingDescriptions instanceof Array)) {
controller.error(new Error("Encountered an invalid output value."));
controller.close();
return;
}

for (const outputValue of this.rawThingDescriptions) {
// TODO: Add validation
controller.enqueue(outputValue as WoT.ThingDescription);
}

controller.close();
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L38 - L50 were not covered by tests
}

class ThingDiscoveryProcess implements WoT.ThingDiscoveryProcess {
constructor(thingDescriptionStream: ReadableStream<ThingDescription>, filter?: WoT.ThingFilter) {
this.filter = filter;
this.done = false;
this.thingDescriptionStream = thingDescriptionStream;
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L55 - L58 were not covered by tests

thingDescriptionStream: ReadableStream<WoT.ThingDescription>;

filter?: WoT.ThingFilter | undefined;
done: boolean;
error?: Error | undefined;
async stop(): Promise<void> {
await this.thingDescriptionStream.cancel();
this.done = true;
}

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#L66-L68

Added lines #L66 - L68 were not covered by tests

async *[Symbol.asyncIterator](): AsyncIterator<WoT.ThingDescription> {
// @ts-expect-error Typescript currently encounters an error here that *should* be a false positve
for await (const thingDescription of this.thingDescriptionStream) {
yield thingDescription;
}
this.done = true;
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L71-L76

Added lines #L71 - L76 were not covered by tests
}

export default class WoTImpl {
private srv: Servient;
constructor(srv: Servient) {
Expand All @@ -38,7 +89,15 @@ export default class WoTImpl {

/** @inheritDoc */
async exploreDirectory(url: string, filter?: WoT.ThingFilter): Promise<WoT.ThingDiscoveryProcess> {
throw new Error("not implemented");
const directoyThingDescription = await this.requestThingDescription(url);
const consumedDirectoy = await this.consume(directoyThingDescription);

const thingsPropertyOutput = await consumedDirectoy.readProperty("things");
const rawThingDescriptions = await thingsPropertyOutput.value();
const thingDescriptionDataSource = new ExploreDirectoryDatasource(rawThingDescriptions);

const thingDescriptionStream = new ReadableStream(thingDescriptionDataSource);
return new ThingDiscoveryProcess(thingDescriptionStream, filter);

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L92-L100

Added lines #L92 - L100 were not covered by tests
}

/** @inheritDoc */
Expand Down

0 comments on commit 77b916f

Please sign in to comment.