Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@node-wot/core

Package Overview
Dependencies
Maintainers
3
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@node-wot/core - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

31

dist/consumed-thing.d.ts

@@ -1,22 +0,23 @@

import { Observable } from "rxjs/Observable";
import * as WoT from "wot-typescript-definitions";
import { ThingDescription } from "@node-wot/td-tools";
import * as TD from "@node-wot/td-tools";
import Servient from "./servient";
export default class ConsumedThing implements WoT.ConsumedThing {
readonly name: string;
readonly url: USVString;
readonly description: WoT.ThingDescription;
protected readonly td: ThingDescription;
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
export default class ConsumedThing extends TD.Thing implements WoT.ConsumedThing {
protected readonly td: WoT.ThingDescription;
protected readonly srv: Servient;
private clients;
constructor(servient: Servient, td: ThingDescription);
private getClientFor(links);
protected observablesEvent: Map<string, Subject<any>>;
protected observablesPropertyChange: Map<string, Subject<any>>;
protected observablesTDChange: Subject<any>;
constructor(servient: Servient, td: WoT.ThingDescription);
getThingDescription(): WoT.ThingDescription;
private getClientFor(forms);
private findInteraction(name, type);
getProperty(propertyName: string): Promise<any>;
setProperty(propertyName: string, newValue: any): Promise<any>;
readProperty(propertyName: string): Promise<any>;
writeProperty(propertyName: string, newValue: any): Promise<void>;
onPropertyChange(name: string): Observable<any>;
invokeAction(actionName: string, parameter?: any): Promise<any>;
addListener(eventName: string, listener: WoT.ThingEventListener): ConsumedThing;
removeListener(eventName: string, listener: WoT.ThingEventListener): ConsumedThing;
removeAllListeners(eventName: string): ConsumedThing;
observe(name: string, requestType: WoT.RequestType): Observable<any>;
onEvent(name: string): Observable<any>;
onTDChange(): Observable<any>;
}
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });

@@ -6,25 +16,47 @@ var TD = require("@node-wot/td-tools");

var content_serdes_1 = require("./content-serdes");
var ConsumedThing = (function () {
var Subject_1 = require("rxjs/Subject");
var ConsumedThing = (function (_super) {
__extends(ConsumedThing, _super);
function ConsumedThing(servient, td) {
this.clients = new Map();
this.srv = servient;
this.name = td.name;
this.td = td;
this.description = JSON.stringify(td);
var _this = _super.call(this) || this;
_this.clients = new Map();
_this.observablesEvent = new Map();
_this.observablesPropertyChange = new Map();
_this.observablesTDChange = new Subject_1.Subject();
_this.srv = servient;
_this.td = td;
var tdObj = TD.parseTDString(td, true);
_this.context = tdObj.context;
_this.semanticType = tdObj.semanticType;
_this.name = tdObj.name;
_this.id = tdObj.id;
if (Array.isArray(tdObj.security) && tdObj.security.length >= 1) {
_this.security = tdObj.security;
}
else if (typeof tdObj.security === "object") {
_this.security = [tdObj.security];
}
_this.metadata = tdObj.metadata;
_this.interaction = tdObj.interaction;
_this.link = tdObj.link;
return _this;
}
ConsumedThing.prototype.getClientFor = function (links) {
ConsumedThing.prototype.getThingDescription = function () {
return this.td;
};
ConsumedThing.prototype.getClientFor = function (forms) {
var _this = this;
if (links.length === 0) {
if (forms.length === 0) {
throw new Error("ConsumedThing '${this.name}' has no links for this interaction");
}
var schemes = links.map(function (link) { return Helpers.extractScheme(link.href); });
var schemes = forms.map(function (link) { return Helpers.extractScheme(link.href); });
var cacheIdx = schemes.findIndex(function (scheme) { return _this.clients.has(scheme); });
if (cacheIdx !== -1) {
console.log("ConsumedThing '" + this.name + "' chose cached client for '" + schemes[cacheIdx] + "'");
console.debug("ConsumedThing '" + this.name + "' chose cached client for '" + schemes[cacheIdx] + "'");
var client = this.clients.get(schemes[cacheIdx]);
var link = links[cacheIdx];
return { client: client, link: link };
var form = forms[cacheIdx];
return { client: client, form: form };
}
else {
console.log("ConsumedThing '" + this.name + "' has no client in cache (" + cacheIdx + ")");
console.debug("ConsumedThing '" + this.name + "' has no client in cache (" + cacheIdx + ")");
var srvIdx = schemes.findIndex(function (scheme) { return _this.srv.hasClientFor(scheme); });

@@ -36,10 +68,9 @@ if (srvIdx === -1)

console.log("ConsumedThing '" + this.name + "' got new client for '" + schemes[srvIdx] + "'");
if (this.td.security) {
console.warn("ConsumedThing applying security metadata");
console.dir(this.td.security);
client.setSecurity(this.td.security, this.srv.getCredentials(this.td.id));
if (this.security) {
console.log("ConsumedThing applying security metadata");
client.setSecurity(this.security, this.srv.getCredentials(this.id));
}
this.clients.set(schemes[srvIdx], client);
var link = links[srvIdx];
return { client: client, link: link };
var form = forms[srvIdx];
return { client: client, form: form };
}

@@ -52,6 +83,6 @@ else {

ConsumedThing.prototype.findInteraction = function (name, type) {
var res = this.td.interaction.filter(function (ia) { return ia.pattern === type && ia.name === name; });
var res = this.interaction.filter(function (ia) { return ia.pattern === type && ia.name === name; });
return (res.length > 0) ? res[0] : null;
};
ConsumedThing.prototype.getProperty = function (propertyName) {
ConsumedThing.prototype.readProperty = function (propertyName) {
var _this = this;

@@ -64,12 +95,12 @@ return new Promise(function (resolve, reject) {

else {
var _a = _this.getClientFor(property.link), client = _a.client, link_1 = _a.link;
var _a = _this.getClientFor(property.form), client = _a.client, form_1 = _a.form;
if (!client) {
reject(new Error("ConsumedThing '" + _this.name + "' did not get suitable client for " + link_1.href));
reject(new Error("ConsumedThing '" + _this.name + "' did not get suitable client for " + form_1.href));
}
else {
console.info("ConsumedThing '" + _this.name + "' reading " + link_1.href);
client.readResource(link_1.href).then(function (content) {
console.log("ConsumedThing '" + _this.name + "' reading " + form_1.href);
client.readResource(form_1).then(function (content) {
if (!content.mediaType)
content.mediaType = link_1.mediaType;
var value = content_serdes_1.default.bytesToValue(content);
content.mediaType = form_1.mediaType;
var value = content_serdes_1.default.contentToValue(content);
resolve(value);

@@ -82,3 +113,3 @@ })

};
ConsumedThing.prototype.setProperty = function (propertyName, newValue) {
ConsumedThing.prototype.writeProperty = function (propertyName, newValue) {
var _this = this;

@@ -91,10 +122,14 @@ return new Promise(function (resolve, reject) {

else {
var _a = _this.getClientFor(property.link), client = _a.client, link = _a.link;
var _a = _this.getClientFor(property.form), client = _a.client, form = _a.form;
if (!client) {
reject(new Error("ConsumedThing '" + _this.name + "' did not get suitable client for " + link.href));
reject(new Error("ConsumedThing '" + _this.name + "' did not get suitable client for " + form.href));
}
else {
console.info("ConsumedThing '" + _this.name + "' writing " + link.href + " with '" + newValue + "'");
var payload = content_serdes_1.default.valueToBytes(newValue, link.mediaType);
resolve(client.writeResource(link.href, payload));
console.log("ConsumedThing '" + _this.name + "' writing " + form.href + " with '" + newValue + "'");
var content = content_serdes_1.default.valueToContent(newValue, form.mediaType);
resolve(client.writeResource(form, content));
if (_this.observablesPropertyChange.get(propertyName)) {
_this.observablesPropertyChange.get(propertyName).next(newValue);
}
;
}

@@ -104,2 +139,9 @@ }

};
ConsumedThing.prototype.onPropertyChange = function (name) {
if (!this.observablesPropertyChange.get(name)) {
console.log("Create propertyChange observable for " + name);
this.observablesPropertyChange.set(name, new Subject_1.Subject());
}
return this.observablesPropertyChange.get(name).asObservable();
};
ConsumedThing.prototype.invokeAction = function (actionName, parameter) {

@@ -113,14 +155,14 @@ var _this = this;

else {
var _a = _this.getClientFor(action.link), client = _a.client, link_2 = _a.link;
var _a = _this.getClientFor(action.form), client = _a.client, form_2 = _a.form;
if (!client) {
reject(new Error("ConsumedThing '" + _this.name + "' did not get suitable client for " + link_2.href));
reject(new Error("ConsumedThing '" + _this.name + "' did not get suitable client for " + form_2.href));
}
else {
console.info("ConsumedThing '" + _this.name + "' invoking " + link_2.href + " with '" + parameter + "'");
var mediaType = link_2.mediaType;
var input = content_serdes_1.default.valueToBytes(parameter, link_2.mediaType.toString());
client.invokeResource(link_2.href, input).then(function (output) {
console.log("ConsumedThing '" + _this.name + "' invoking " + form_2.href + " with '" + parameter + "'");
var mediaType = form_2.mediaType;
var input = content_serdes_1.default.valueToContent(parameter, form_2.mediaType);
client.invokeResource(form_2, input).then(function (output) {
if (!output.mediaType)
output.mediaType = link_2.mediaType;
var value = content_serdes_1.default.bytesToValue(output);
output.mediaType = form_2.mediaType;
var value = content_serdes_1.default.contentToValue(output);
resolve(value);

@@ -132,11 +174,15 @@ });

};
ConsumedThing.prototype.addListener = function (eventName, listener) {
return this;
ConsumedThing.prototype.onEvent = function (name) {
if (!this.observablesEvent.get(name)) {
console.log("Create event observable for " + name);
this.observablesEvent.set(name, new Subject_1.Subject());
}
return this.observablesEvent.get(name).asObservable();
};
ConsumedThing.prototype.removeListener = function (eventName, listener) { return this; };
ConsumedThing.prototype.removeAllListeners = function (eventName) { return this; };
ConsumedThing.prototype.observe = function (name, requestType) { return null; };
ConsumedThing.prototype.onTDChange = function () {
return this.observablesTDChange.asObservable();
};
return ConsumedThing;
}());
}(TD.Thing));
exports.default = ConsumedThing;
//# sourceMappingURL=consumed-thing.js.map

@@ -15,2 +15,3 @@ /// <reference types="node" />

static readonly DEFAULT: string;
readonly DEFAUT: string;
private codecs;

@@ -21,7 +22,7 @@ private constructor();

getSupportedMediaTypes(): Array<string>;
bytesToValue(content: Content): any;
contentToValue(content: Content): any;
isolateMediaType(mediaTypeValue: string): string;
valueToBytes(value: any, mediaType?: string): Content;
valueToContent(value: any, mediaType?: string): Content;
}
declare const _default: ContentSerdes;
export default _default;

@@ -12,6 +12,12 @@ "use strict";

var JsonCodec = (function () {
function JsonCodec() {
function JsonCodec(subMediaType) {
if (!subMediaType) {
this.subMediaType = ContentSerdes.DEFAULT;
}
else {
this.subMediaType = subMediaType;
}
}
JsonCodec.prototype.getMediaType = function () {
return 'application/json';
return this.subMediaType;
};

@@ -43,3 +49,2 @@ JsonCodec.prototype.bytesToValue = function (bytes) {

JsonCodec.prototype.valueToBytes = function (value) {
console.log("JsonCodec serializing", value);
var body = "";

@@ -60,3 +65,2 @@ if (value !== undefined) {

TextCodec.prototype.bytesToValue = function (bytes) {
console.log("TextCodec parsing '" + bytes.toString() + "'");
var parsed;

@@ -67,3 +71,2 @@ parsed = bytes.toString();

TextCodec.prototype.valueToBytes = function (value) {
console.log("TextCodec serializing '" + value + "'");
var body = "";

@@ -79,2 +82,3 @@ if (value !== undefined) {

function ContentSerdes() {
this.DEFAUT = ContentSerdes.DEFAULT;
this.codecs = new Map();

@@ -86,2 +90,3 @@ }

this.instance.addCodec(new JsonCodec());
this.instance.addCodec(new JsonCodec('application/senml+json'));
this.instance.addCodec(new TextCodec());

@@ -97,3 +102,3 @@ }

};
ContentSerdes.prototype.bytesToValue = function (content) {
ContentSerdes.prototype.contentToValue = function (content) {
if (content.mediaType === undefined) {

@@ -107,2 +112,3 @@ if (content.body.byteLength > 0) {

}
console.debug("ContentSerdes deserializing from " + content.mediaType);
var isolMediaType = this.isolateMediaType(content.mediaType);

@@ -125,7 +131,7 @@ if (!this.codecs.has(isolMediaType)) {

};
ContentSerdes.prototype.valueToBytes = function (value, mediaType) {
ContentSerdes.prototype.valueToContent = function (value, mediaType) {
if (mediaType === void 0) { mediaType = ContentSerdes.DEFAULT; }
if (value === undefined)
console.warn("ContentSerdes valueToBytes got no value");
console.log("ContentSerdes serializing to " + mediaType);
console.warn("ContentSerdes valueToContent got no value");
console.debug("ContentSerdes serializing to " + mediaType);
if (!this.codecs.has(mediaType)) {

@@ -138,3 +144,3 @@ throw new Error("Unsupported serialization format: " + mediaType);

};
ContentSerdes.DEFAULT = 'application/json';
ContentSerdes.DEFAULT = "application/json";
return ContentSerdes;

@@ -141,0 +147,0 @@ }());

@@ -11,3 +11,2 @@ "use strict";

__export(require("./content-serdes"));
__export(require("./resource-listeners/protocol-interfaces"));
var consumed_thing_1 = require("./consumed-thing");

@@ -14,0 +13,0 @@ exports.ConsumedThing = consumed_thing_1.default;

@@ -1,32 +0,32 @@

import { ThingDescription } from "@node-wot/td-tools";
import * as WoT from "wot-typescript-definitions";
import * as TD from "@node-wot/td-tools";
import Servient from "./servient";
import ConsumedThing from "./consumed-thing";
export default class ExposedThing extends ConsumedThing implements WoT.ExposedThing {
private interactions;
private interactionStates;
export default class ExposedThing extends ConsumedThing implements WoT.ConsumedThing, WoT.ExposedThing {
private propertyStates;
private actionStates;
private interactionObservables;
private restListeners;
constructor(servient: Servient, td: ThingDescription);
constructor(servient: Servient, td: WoT.ThingDescription);
getThingDescription(): WoT.ThingDescription;
private addResourceListener(path, resourceListener);
private removeResourceListener(path);
getInteractions(): Array<TD.Interaction>;
readProperty(propertyName: string): Promise<any>;
writeProperty(propertyName: string, newValue: any): Promise<void>;
invokeAction(actionName: string, parameter?: any): Promise<any>;
setProperty(propertyName: string, newValue: any): Promise<any>;
getProperty(propertyName: string): Promise<any>;
register(directory?: USVString): Promise<void>;
unregister(directory?: USVString): Promise<void>;
start(directory?: USVString): Promise<void>;
stop(directory?: USVString): Promise<void>;
emitEvent(eventName: string, payload: any): Promise<void>;
onRetrieveProperty(handler: WoT.RequestHandler): ExposedThing;
onInvokeAction(handler: WoT.RequestHandler): ExposedThing;
onUpdateProperty(handler: WoT.RequestHandler): ExposedThing;
onObserve(handler: WoT.RequestHandler): ExposedThing;
getDescription(): Object;
addProperty(property: WoT.ThingPropertyInit): ExposedThing;
addAction(action: WoT.ThingActionInit): ExposedThing;
addEvent(event: WoT.ThingEventInit): ExposedThing;
removeProperty(propertyName: string): ExposedThing;
removeAction(actionName: string): ExposedThing;
removeEvent(eventName: string): ExposedThing;
start(): Promise<void>;
stop(): Promise<void>;
emitEvent(eventName: string, value: any): Promise<void>;
addProperty(property: WoT.ThingProperty): WoT.ExposedThing;
addAction(action: WoT.ThingAction): WoT.ExposedThing;
addEvent(event: WoT.ThingEvent): WoT.ExposedThing;
removeProperty(propertyName: string): WoT.ExposedThing;
removeAction(actionName: string): WoT.ExposedThing;
removeEvent(eventName: string): WoT.ExposedThing;
setActionHandler(actionName: string, action: WoT.ActionHandler): WoT.ExposedThing;
setPropertyReadHandler(propertyName: string, readHandler: WoT.PropertyReadHandler): WoT.ExposedThing;
setPropertyWriteHandler(propertyName: string, writeHandler: WoT.PropertyWriteHandler): WoT.ExposedThing;
}

@@ -13,2 +13,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var Subject_1 = require("rxjs/Subject");
var TD = require("@node-wot/td-tools");

@@ -18,2 +19,3 @@ var consumed_thing_1 = require("./consumed-thing");

var Rest = require("./resource-listeners/all-resource-listeners");
var content_serdes_1 = require("./content-serdes");
var ExposedThing = (function (_super) {

@@ -23,8 +25,29 @@ __extends(ExposedThing, _super);

var _this = _super.call(this, servient, td) || this;
_this.interactions = [];
_this.interactionStates = {};
_this.propertyStates = new Map();
_this.actionStates = new Map();
_this.interactionObservables = new Map();
_this.restListeners = new Map();
_this.addResourceListener("/" + _this.name, new Rest.TDResourceListener(_this));
var initialInteractions = _this.interaction.slice(0);
_this.interaction = [];
for (var _i = 0, initialInteractions_1 = initialInteractions; _i < initialInteractions_1.length; _i++) {
var inter = initialInteractions_1[_i];
if (inter.pattern === TD.InteractionPattern.Property) {
_this.addProperty(inter);
}
else if (inter.pattern === TD.InteractionPattern.Action) {
_this.addAction(inter);
}
else if (inter.pattern === TD.InteractionPattern.Event) {
_this.addEvent(inter);
}
else {
console.error("ExposedThing '" + _this.name + "' ignoring unknown Interaction '" + inter.name + "':", inter);
}
}
_this.addResourceListener("/" + encodeURIComponent(_this.name), new Rest.TDResourceListener(_this));
return _this;
}
ExposedThing.prototype.getThingDescription = function () {
return TD.serializeTD(TDGenerator.generateTD(this, this.srv));
};
ExposedThing.prototype.addResourceListener = function (path, resourceListener) {

@@ -39,32 +62,47 @@ this.restListeners.set(path, resourceListener);

ExposedThing.prototype.getInteractions = function () {
return this.interactions.slice(0);
return this.interaction.slice(0);
};
ExposedThing.prototype.invokeAction = function (actionName, parameter) {
ExposedThing.prototype.readProperty = function (propertyName) {
var _this = this;
return new Promise(function (resolve, reject) {
var state = _this.interactionStates[actionName];
var state = _this.propertyStates.get(propertyName);
if (state) {
console.log("ExposedThing '" + _this.name + "' Action state of '" + actionName + "':", state);
if (state.handlers.length) {
var handler = state.handlers[0];
resolve(handler(parameter));
if (state.readHandler != null) {
console.log("ExposedThing '" + _this.name + "' calls registered readHandler for property " + propertyName);
state.readHandler.call(state.that).then(function (result) {
state.value = result;
resolve(state.value);
}).catch(function (err) {
reject(err);
});
}
else {
reject(new Error("No handler for " + actionName + " on " + _this.name));
console.log("ExposedThing '" + _this.name + "' reports value " + state.value + " for property " + propertyName);
resolve(state.value);
}
}
else {
reject(new Error("No action " + actionName + " on " + _this.name));
reject(new Error("No property called " + propertyName));
}
});
};
ExposedThing.prototype.setProperty = function (propertyName, newValue) {
ExposedThing.prototype.writeProperty = function (propertyName, newValue) {
var _this = this;
return new Promise(function (resolve, reject) {
var state = _this.interactionStates[propertyName];
var state = _this.propertyStates.get(propertyName);
if (state) {
var oldValue_1 = state.value;
state.value = newValue;
state.handlers.forEach(function (handler) { return handler.apply(_this, [newValue, oldValue_1]); });
resolve(newValue);
if (state.writeHandler != null) {
console.log("ExposedThing '" + _this.name + "' calls registered writeHandler for property " + propertyName);
state.writeHandler.call(state.that, newValue).then(function (result) {
state.value = result;
resolve();
}).catch(function (err) {
reject(err);
});
}
else {
console.log("ExposedThing '" + _this.name + "' sets new value " + newValue + " for property " + propertyName);
state.value = newValue;
resolve();
}
}

@@ -76,11 +114,18 @@ else {

};
ExposedThing.prototype.getProperty = function (propertyName) {
ExposedThing.prototype.invokeAction = function (actionName, parameter) {
var _this = this;
return new Promise(function (resolve, reject) {
var state = _this.interactionStates[propertyName];
var state = _this.actionStates.get(actionName);
if (state) {
resolve(state.value);
console.debug("ExposedThing '" + _this.name + "' Action state of '" + actionName + "':", state);
if (state.handler != null) {
var handler = state.handler;
resolve(handler(parameter));
}
else {
reject(new Error("ExposedThing '" + _this.name + "' has no action handler for '" + actionName + "'"));
}
}
else {
reject(new Error("No property called " + propertyName));
reject(new Error("ExposedThing '" + _this.name + "' has no Action '" + actionName + "'"));
}

@@ -97,46 +142,22 @@ });

};
ExposedThing.prototype.start = function (directory) {
ExposedThing.prototype.start = function () {
return new Promise(function (resolve, reject) {
});
};
ExposedThing.prototype.stop = function (directory) {
ExposedThing.prototype.stop = function () {
return new Promise(function (resolve, reject) {
});
};
ExposedThing.prototype.emitEvent = function (eventName, payload) {
ExposedThing.prototype.emitEvent = function (eventName, value) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.interactionObservables.get(eventName).next(content_serdes_1.ContentSerdes.get().valueToContent(value));
resolve();
});
};
ExposedThing.prototype.onRetrieveProperty = function (handler) {
return this;
};
ExposedThing.prototype.onInvokeAction = function (handler) {
var state = this.interactionStates[handler.request.name];
if (state) {
if (state.handlers.length > 0)
state.handlers.splice(0);
state.handlers.push(handler.callback);
}
else {
console.error("ExposedThing '" + this.name + "' has no Action '" + handler.request.name);
}
return this;
};
ExposedThing.prototype.onUpdateProperty = function (handler) {
var state = this.interactionStates[handler.request.name];
if (state) {
state.handlers.push(handler.callback);
}
else {
console.error("ExposedThing '" + this.name + "' has no Property '" + handler.request.name + "'");
}
return this;
};
ExposedThing.prototype.onObserve = function (handler) {
return this;
};
ExposedThing.prototype.getDescription = function () {
return JSON.parse(TD.serializeTD(TDGenerator.generateTD(this, this.srv)));
};
ExposedThing.prototype.addProperty = function (property) {
if (property.name === undefined)
throw new Error("addProperty() requires ThingProperty 'name'");
if (property.schema === undefined)
throw new Error("addProperty() requires ThingProperty 'schema'");
console.log("ExposedThing '" + this.name + "' adding Property '" + property.name + "'");

@@ -146,15 +167,25 @@ var newProp = new TD.Interaction();

newProp.name = property.name;
newProp.outputData = property.description;
newProp.writable = property.writable;
this.interactions.push(newProp);
var propState = new InteractionState();
propState.value = property.value;
propState.handlers = [];
if (property.onWrite)
propState.handlers.push(property.onWrite);
this.interactionStates[property.name] = propState;
this.addResourceListener("/" + this.name + "/properties/" + property.name, new Rest.PropertyResourceListener(this, newProp));
newProp.schema = (typeof property.schema === "string") ? JSON.parse(property.schema) : property.schema;
newProp.writable = property.writable === undefined ? false : property.writable;
newProp.observable = property.observable === undefined ? false : property.observable;
if (Array.isArray(property.semanticType)) {
newProp.semanticType = property.semanticType.slice(0);
}
if (Array.isArray(property.metadata)) {
newProp.metadata = property.metadata.slice(0);
}
this.interaction.push(newProp);
var state = new PropertyState();
if (property.value != null) {
state.value = property.value;
console.log("ExposedThing '" + this.name + "' sets initial property '" + property.name + "' to '" + state.value + "'");
}
this.propertyStates.set(newProp.name, state);
this.addResourceListener("/" + encodeURIComponent(this.name) + "/properties/" + encodeURIComponent(newProp.name), new Rest.PropertyResourceListener(this, newProp.name));
this.observablesTDChange.next(this.getThingDescription());
return this;
};
ExposedThing.prototype.addAction = function (action) {
if (action.name === undefined)
throw new Error("addAction() requires ThingAction 'name'");
console.log("ExposedThing '" + this.name + "' adding Action '" + action.name + "'");

@@ -164,45 +195,112 @@ var newAction = new TD.Interaction();

newAction.name = action.name;
newAction.inputData = action.inputDataDescription ? action.inputDataDescription : null;
newAction.outputData = action.outputDataDescription ? action.outputDataDescription : null;
this.interactions.push(newAction);
var actionState = new InteractionState();
actionState.handlers = [];
if (action.action)
actionState.handlers.push(action.action);
this.interactionStates[action.name] = actionState;
this.addResourceListener("/" + this.name + "/actions/" + action.name, new Rest.ActionResourceListener(this, newAction));
newAction.inputSchema = (typeof action.inputSchema === "string") ? JSON.parse(action.inputSchema) : action.inputSchema;
newAction.outputSchema = (typeof action.outputSchema === "string") ? JSON.parse(action.outputSchema) : action.outputSchema;
if (Array.isArray(action.semanticType)) {
newAction.semanticType = action.semanticType.slice(0);
}
if (Array.isArray(action.metadata)) {
newAction.metadata = action.metadata.slice(0);
}
this.interaction.push(newAction);
this.actionStates.set(newAction.name, new ActionState());
this.addResourceListener("/" + encodeURIComponent(this.name) + "/actions/" + encodeURIComponent(newAction.name), new Rest.ActionResourceListener(this, newAction.name));
this.observablesTDChange.next(this.getThingDescription());
return this;
};
ExposedThing.prototype.addEvent = function (event) {
if (event.name === undefined)
throw new Error("addEvent() requires ThingEvent 'name'");
if (event.schema === undefined)
throw new Error("addEvent() requires ThingEvent 'schema'");
console.log("ExposedThing '" + this.name + "' adding Event '" + event.name + "'");
var newEvent = new TD.Interaction();
newEvent.pattern = TD.InteractionPattern.Event;
newEvent.name = event.name;
this.interactions.push(newEvent);
var eventState = new InteractionState();
eventState.handlers = [];
this.interactionStates[event.name] = eventState;
newEvent.schema = (typeof event.schema === "string") ? JSON.parse(event.schema) : event.schema;
if (Array.isArray(event.semanticType)) {
newEvent.semanticType = event.semanticType.slice(0);
}
if (Array.isArray(event.metadata)) {
newEvent.metadata = event.metadata.slice(0);
}
this.interaction.push(newEvent);
var subject = new Subject_1.Subject();
this.interactionObservables.set(newEvent.name, subject);
this.addResourceListener("/" + encodeURIComponent(this.name) + "/events/" + encodeURIComponent(newEvent.name), new Rest.EventResourceListener(newEvent.name, subject));
this.observablesTDChange.next(this.getThingDescription());
return this;
};
ExposedThing.prototype.removeProperty = function (propertyName) {
delete this.interactionStates[propertyName];
this.removeResourceListener(this.name + "/properties/" + propertyName);
this.interactionObservables.get(propertyName).complete();
this.interactionObservables.delete(propertyName);
this.propertyStates.delete(propertyName);
this.removeResourceListener("/" + encodeURIComponent(this.name) + "/properties/" + encodeURIComponent(propertyName));
this.observablesTDChange.next(this.getThingDescription());
return this;
};
ExposedThing.prototype.removeAction = function (actionName) {
delete this.interactionStates[actionName];
this.removeResourceListener(this.name + "/actions/" + actionName);
this.actionStates.delete(actionName);
this.removeResourceListener("/" + encodeURIComponent(this.name) + "/actions/" + encodeURIComponent(actionName));
this.observablesTDChange.next(this.getThingDescription());
return this;
};
ExposedThing.prototype.removeEvent = function (eventName) {
this.interactionObservables.get(eventName).complete();
this.interactionObservables.delete(eventName);
this.removeResourceListener("/" + encodeURIComponent(this.name) + "/events/" + encodeURIComponent(eventName));
this.observablesTDChange.next(this.getThingDescription());
return this;
};
ExposedThing.prototype.setActionHandler = function (actionName, action) {
console.log("ExposedThing '" + this.name + "' setting action Handler for '" + actionName + "'");
var state = this.actionStates.get(actionName);
if (state) {
state.handler = action;
}
else {
throw Error("ExposedThing '" + this.name + "' cannot set action handler for unknown '" + actionName + "'");
}
return this;
};
ExposedThing.prototype.setPropertyReadHandler = function (propertyName, readHandler) {
console.log("ExposedThing '" + this.name + "' setting read handler for '" + propertyName + "'");
var state = this.propertyStates.get(propertyName);
if (state) {
state.readHandler = readHandler;
}
else {
throw Error("ExposedThing '" + this.name + "' cannot set read handler for unknown '" + propertyName + "'");
}
return this;
};
ExposedThing.prototype.setPropertyWriteHandler = function (propertyName, writeHandler) {
console.log("ExposedThing '" + this.name + "' setting write handler for '" + propertyName + "'");
var state = this.propertyStates.get(propertyName);
if (state) {
state.writeHandler = writeHandler;
}
else {
throw Error("ExposedThing '" + this.name + "' cannot set write handler for unknown '" + propertyName + "'");
}
return this;
};
return ExposedThing;
}(consumed_thing_1.default));
exports.default = ExposedThing;
var InteractionState = (function () {
function InteractionState() {
this.handlers = [];
var PropertyState = (function () {
function PropertyState() {
this.that = new Function();
this.value = null;
this.writeHandler = null;
this.readHandler = null;
}
return InteractionState;
return PropertyState;
}());
var ActionState = (function () {
function ActionState() {
this.that = new Function();
this.handler = null;
}
return ActionState;
}());
//# sourceMappingURL=exposed-thing.js.map

@@ -11,3 +11,3 @@ "use strict";

var scheme = parsed.protocol.slice(0, -1);
console.log("Helpers found scheme '" + scheme + "'");
console.debug("Helpers found scheme '" + scheme + "'");
return scheme;

@@ -21,3 +21,3 @@ }

interfaces[iface].forEach(function (entry) {
console.log("AddressHelper found " + entry.address);
console.debug("AddressHelper found " + entry.address);
if (entry.internal === false) {

@@ -33,4 +33,3 @@ if (entry.family === 'IPv4') {

}
addresses.push('127.0.0.1');
console.log("AddressHelper identified " + addresses);
console.debug("AddressHelper identified " + addresses);
return addresses;

@@ -37,0 +36,0 @@ }

@@ -1,2 +0,1 @@

import * as TD from "@node-wot/td-tools";
import { ResourceListener, Content } from "./protocol-interfaces";

@@ -6,7 +5,7 @@ import BasicResourceListener from "./basic-resource-listener";

export default class ActionResourceListener extends BasicResourceListener implements ResourceListener {
readonly name: string;
private readonly thing;
private readonly description;
private readonly name;
constructor(thing: ExposedThing, action: TD.Interaction);
onInvoke(value: Content): Promise<Content>;
constructor(thing: ExposedThing, name: string);
getType(): string;
onInvoke(input: Content): Promise<Content>;
}

@@ -17,17 +17,25 @@ "use strict";

__extends(ActionResourceListener, _super);
function ActionResourceListener(thing, action) {
function ActionResourceListener(thing, name) {
var _this = _super.call(this) || this;
_this.thing = thing;
_this.description = action;
_this.name = action.name;
_this.name = name;
return _this;
}
ActionResourceListener.prototype.onInvoke = function (value) {
var param = content_serdes_1.default.bytesToValue(value);
return this.thing.invokeAction(this.name, param).then(function (value) {
if (value === undefined) {
ActionResourceListener.prototype.getType = function () {
return "Action";
};
ActionResourceListener.prototype.onInvoke = function (input) {
var param;
try {
param = content_serdes_1.default.contentToValue(input);
}
catch (err) {
return new Promise(function (resolve, reject) { reject(err); });
}
return this.thing.invokeAction(this.name, param).then(function (output) {
if (output === undefined) {
return { mediaType: null, body: null };
}
else {
return content_serdes_1.default.valueToBytes(value);
return content_serdes_1.default.valueToContent(output);
}

@@ -34,0 +42,0 @@ });

@@ -0,4 +1,6 @@

export { default as PropertyResourceListener } from "./property-resource-listener";
export { default as ActionResourceListener } from "./action-resource-listener";
export { default as PropertyResourceListener } from "./property-resource-listener";
export { default as EventResourceListener } from "./event-resource-listener";
export { default as TDResourceListener } from "./td-resource-listener";
export { default as AssetResourceListener } from "./asset-resource-listener";
export { default as BasicResourceListener } from "./basic-resource-listener";
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var property_resource_listener_1 = require("./property-resource-listener");
exports.PropertyResourceListener = property_resource_listener_1.default;
var action_resource_listener_1 = require("./action-resource-listener");
exports.ActionResourceListener = action_resource_listener_1.default;
var property_resource_listener_1 = require("./property-resource-listener");
exports.PropertyResourceListener = property_resource_listener_1.default;
var event_resource_listener_1 = require("./event-resource-listener");
exports.EventResourceListener = event_resource_listener_1.default;
var td_resource_listener_1 = require("./td-resource-listener");

@@ -11,2 +13,4 @@ exports.TDResourceListener = td_resource_listener_1.default;

exports.AssetResourceListener = asset_resource_listener_1.default;
var basic_resource_listener_1 = require("./basic-resource-listener");
exports.BasicResourceListener = basic_resource_listener_1.default;
//# sourceMappingURL=all-resource-listeners.js.map

@@ -7,2 +7,3 @@ import BasicResourceListener from "./basic-resource-listener";

constructor(asset: string, mediaType?: string);
getType(): string;
onRead(): Promise<Content>;

@@ -9,0 +10,0 @@ onWrite(content: Content): Promise<void>;

@@ -23,2 +23,5 @@ "use strict";

}
AssetResourceListener.prototype.getType = function () {
return "Asset";
};
AssetResourceListener.prototype.onRead = function () {

@@ -25,0 +28,0 @@ var _this = this;

import { ResourceListener, Content } from "./protocol-interfaces";
export default class BasicResourceListener implements ResourceListener {
constructor();
getType(): string;
onRead(): Promise<Content>;

@@ -5,0 +6,0 @@ onWrite(content: Content): Promise<void>;

@@ -6,2 +6,5 @@ "use strict";

}
BasicResourceListener.prototype.getType = function () {
return "Basic";
};
BasicResourceListener.prototype.onRead = function () {

@@ -8,0 +11,0 @@ return Promise.reject(new Error("Method Not Allowed"));

@@ -0,1 +1,31 @@

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var basic_resource_listener_1 = require("./basic-resource-listener");
var EventResourceListener = (function (_super) {
__extends(EventResourceListener, _super);
function EventResourceListener(name, subject) {
var _this = _super.call(this) || this;
_this.name = name;
_this.subject = subject;
return _this;
}
EventResourceListener.prototype.getType = function () {
return "Event";
};
EventResourceListener.prototype.subscribe = function (obs) {
return this.subject.subscribe(obs);
};
return EventResourceListener;
}(basic_resource_listener_1.default));
exports.default = EventResourceListener;
//# sourceMappingURL=event-resource-listener.js.map

@@ -1,2 +0,1 @@

import * as TD from "@node-wot/td-tools";
import { Content, ResourceListener } from "./protocol-interfaces";

@@ -6,8 +5,8 @@ import BasicResourceListener from "./basic-resource-listener";

export default class PropertyResourceListener extends BasicResourceListener implements ResourceListener {
readonly name: string;
private readonly thing;
private readonly description;
private readonly name;
constructor(thing: ExposedThing, property: TD.Interaction);
constructor(thing: ExposedThing, name: string);
getType(): string;
onRead(): Promise<Content>;
onWrite(input: Content): Promise<void>;
}

@@ -17,20 +17,28 @@ "use strict";

__extends(PropertyResourceListener, _super);
function PropertyResourceListener(thing, property) {
function PropertyResourceListener(thing, name) {
var _this = _super.call(this) || this;
_this.thing = thing;
_this.description = property;
_this.name = property.name;
_this.name = name;
return _this;
}
PropertyResourceListener.prototype.getType = function () {
return "Property";
};
PropertyResourceListener.prototype.onRead = function () {
return this.thing
.getProperty(this.name)
.readProperty(this.name)
.then(function (value) {
var bytes = content_serdes_1.default.valueToBytes(value);
return Promise.resolve(bytes);
var content = content_serdes_1.default.valueToContent(value);
return Promise.resolve(content);
});
};
PropertyResourceListener.prototype.onWrite = function (input) {
var value = content_serdes_1.default.bytesToValue(input);
return this.thing.setProperty(this.name, value);
var value;
try {
value = content_serdes_1.default.contentToValue(input);
}
catch (err) {
return new Promise(function (resolve, reject) { reject(err); });
}
return this.thing.writeProperty(this.name, value);
};

@@ -37,0 +45,0 @@ return PropertyResourceListener;

/// <reference types="node" />
import { InteractionForm } from "@node-wot/td-tools";
export interface ProtocolClient {
readResource(uri: string): Promise<Content>;
writeResource(uri: string, content: Content): Promise<void>;
invokeResource(uri: String, content: Content): Promise<Content>;
unlinkResource(uri: string): Promise<void>;
readResource(form: InteractionForm): Promise<Content>;
writeResource(form: InteractionForm, content: Content): Promise<void>;
invokeResource(form: InteractionForm, content: Content): Promise<Content>;
unlinkResource(form: InteractionForm): Promise<void>;
start(): boolean;

@@ -30,2 +31,3 @@ stop(): boolean;

export interface ResourceListener {
getType(): string;
onRead(): Promise<Content>;

@@ -36,3 +38,1 @@ onWrite(value: Content): Promise<void>;

}
export { default as BasicResourceListener } from './basic-resource-listener';
export { default as AssetResourceListener } from './asset-resource-listener';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var basic_resource_listener_1 = require("./basic-resource-listener");
exports.BasicResourceListener = basic_resource_listener_1.default;
var asset_resource_listener_1 = require("./asset-resource-listener");
exports.AssetResourceListener = asset_resource_listener_1.default;
//# sourceMappingURL=protocol-interfaces.js.map

@@ -7,3 +7,4 @@ import BasicResourceListener from "./basic-resource-listener";

constructor(thing: ExposedThing);
getType(): string;
onRead(): Promise<Content>;
}

@@ -14,3 +14,2 @@ "use strict";

var basic_resource_listener_1 = require("./basic-resource-listener");
var content_serdes_1 = require("../content-serdes");
var TDResourceListener = (function (_super) {

@@ -23,4 +22,7 @@ __extends(TDResourceListener, _super);

}
TDResourceListener.prototype.getType = function () {
return "TD";
};
TDResourceListener.prototype.onRead = function () {
return Promise.resolve(content_serdes_1.default.valueToBytes(this.thing.getDescription()));
return Promise.resolve({ mediaType: "application/ld+json", body: new Buffer(this.thing.getThingDescription()) });
};

@@ -27,0 +29,0 @@ return TDResourceListener;

import ExposedThing from "./exposed-thing";
import { ProtocolClientFactory, ProtocolServer, ResourceListener, ProtocolClient } from "./resource-listeners/protocol-interfaces";
import { ContentCodec } from "./content-serdes";
import { ThingDescription } from "@node-wot/td-tools";
import { Thing } from "@node-wot/td-tools";
import * as TD from "@node-wot/td-tools";

@@ -11,8 +11,10 @@ export default class Servient {

private listeners;
private offeredMediaTypes;
private credentialStore;
runScript(code: string, filename?: string): void;
runPriviledgedScript(code: string, filename?: string): void;
addMediaType(codec: ContentCodec): void;
runPrivilegedScript(code: string, filename?: string): void;
addMediaType(codec: ContentCodec, offered?: boolean): void;
getSupportedMediaTypes(): Array<string>;
chooseLink(links: Array<TD.InteractionLink>): string;
getOffereddMediaTypes(): Array<string>;
chooseLink(links: Array<TD.InteractionForm>): string;
addResourceListener(path: string, resourceListener: ResourceListener): void;

@@ -26,3 +28,3 @@ removeResourceListener(path: string): void;

getClientSchemes(): string[];
addThingFromTD(thing: ThingDescription): boolean;
addThingFromTD(thing: Thing): boolean;
addThing(thing: ExposedThing): boolean;

@@ -29,0 +31,0 @@ getThing(name: string): ExposedThing;

@@ -12,2 +12,3 @@ "use strict";

this.listeners = new Map();
this.offeredMediaTypes = [content_serdes_1.default.DEFAUT];
this.credentialStore = new Map();

@@ -28,5 +29,11 @@ }

};
script.runInContext(context, options);
try {
script.runInContext(context, options);
}
catch (err) {
var scriptPosition = err.stack.match(/at evalmachine\.<anonymous>\:([0-9]+\:[0-9]+)\n/)[1];
console.error("Servient caught error in privileged '" + filename + "' and halted at line " + scriptPosition + "\n " + err);
}
};
Servient.prototype.runPriviledgedScript = function (code, filename) {
Servient.prototype.runPrivilegedScript = function (code, filename) {
if (filename === void 0) { filename = 'script'; }

@@ -45,6 +52,15 @@ var script = new vm.Script(code);

};
script.runInContext(context, options);
try {
script.runInContext(context, options);
}
catch (err) {
var scriptPosition = err.stack.match(/at evalmachine\.<anonymous>\:([0-9]+\:[0-9]+)\n/)[1];
console.error("Servient caught error in privileged '" + filename + "' and halted at line " + scriptPosition + "\n " + err);
}
};
Servient.prototype.addMediaType = function (codec) {
Servient.prototype.addMediaType = function (codec, offered) {
if (offered === void 0) { offered = false; }
content_serdes_1.default.addCodec(codec);
if (offered)
this.offeredMediaTypes.push(codec.getMediaType());
};

@@ -54,2 +70,5 @@ Servient.prototype.getSupportedMediaTypes = function () {

};
Servient.prototype.getOffereddMediaTypes = function () {
return this.offeredMediaTypes.slice(0);
};
Servient.prototype.chooseLink = function (links) {

@@ -122,3 +141,6 @@ return (links.length > 0) ? links[0].href : "nope://none";

Servient.prototype.getCredentials = function (identifier) {
return this.credentialStore.get(identifier);
var credentials = this.credentialStore.get(identifier);
if (!credentials)
console.error("Servient missing credentials for '" + identifier + "'");
return credentials;
};

@@ -125,0 +147,0 @@ Servient.prototype.start = function () {

import Servient from "./servient";
import ExposedThing from "./exposed-thing";
import { ThingDescription } from "@node-wot/td-tools";
export declare function generateTD(thing: ExposedThing, servient: Servient): ThingDescription;
import { Thing } from "@node-wot/td-tools";
export declare function generateTD(thing: ExposedThing, servient: Servient): Thing;

@@ -7,12 +7,20 @@ "use strict";

function generateTD(thing, servient) {
console.log("generateTD() \n```\n" + thing + "\n```");
var genTD = new td_tools_1.ThingDescription();
console.log("generateTD() assign name " + thing.name);
var genTD = new td_tools_1.Thing();
genTD.context = thing.context.slice(0);
genTD.semanticType = thing.semanticType.slice(0);
genTD.name = thing.name;
genTD.interaction = thing.getInteractions();
console.log("generateTD() found " + genTD.interaction.length + " Interaction" + (genTD.interaction.length == 1 ? "" : "s"));
genTD.id = thing.id;
if (thing.security) {
genTD.security = thing.security;
}
else {
genTD.security = [{ description: "node-wot development Servient, no security" }];
}
genTD.metadata = thing.metadata.slice(0);
genTD.interaction = thing.interaction.slice(0);
genTD.link = thing.link.slice(0);
console.debug("generateTD() found " + genTD.interaction.length + " Interaction" + (genTD.interaction.length == 1 ? "" : "s"));
for (var _i = 0, _a = genTD.interaction; _i < _a.length; _i++) {
var interaction = _a[_i];
interaction.semanticTypes = [];
var l = 0;
interaction.form = [];
for (var _b = 0, _c = Helpers.getAddresses(); _b < _c.length; _b++) {

@@ -22,23 +30,18 @@ var address = _c[_b];

var server = _e[_d];
for (var _f = 0, _g = servient.getSupportedMediaTypes(); _f < _g.length; _f++) {
for (var _f = 0, _g = servient.getOffereddMediaTypes(); _f < _g.length; _f++) {
var type = _g[_f];
if (server.getPort() !== -1) {
var href = server.scheme + "://" + address + ":" + server.getPort() + "/" + thing.name;
var href = server.scheme + "://" + address + ":" + server.getPort() + "/" + encodeURIComponent(thing.name);
var pattern = void 0;
if (interaction.pattern === TD.InteractionPattern.Property) {
interaction.link[l] = new TD.InteractionLink();
interaction.link[l].href = href + "/properties/" + interaction.name;
interaction.link[l].mediaType = type;
pattern = "/properties/";
}
else if (interaction.pattern === TD.InteractionPattern.Action) {
interaction.link[l] = new TD.InteractionLink();
interaction.link[l].href = href + "/actions/" + interaction.name;
interaction.link[l].mediaType = type;
pattern = "/actions/";
}
if (interaction.pattern === TD.InteractionPattern.Event) {
interaction.link[l] = new TD.InteractionLink();
interaction.link[l].href = href + "/events/" + interaction.name;
interaction.link[l].mediaType = type;
else if (interaction.pattern === TD.InteractionPattern.Event) {
pattern = "/events/";
}
console.log("generateTD() assigns href '" + interaction.link[l].href + "' to Interaction '" + interaction.name + "'");
++l;
interaction.form.push(new TD.InteractionForm(href + pattern + encodeURIComponent(interaction.name), type));
console.debug("generateTD() assigns href '" + interaction.form[interaction.form.length - 1].href + "' to Interaction '" + interaction.name + "'");
}

@@ -48,3 +51,2 @@ }

}
l = 0;
}

@@ -51,0 +53,0 @@ return genTD;

import { Observable } from "rxjs/Observable";
import * as WoT from "wot-typescript-definitions";
import { ThingDescription } from "@node-wot/td-tools";
import Servient from "./servient";
import ExposedThing from "./exposed-thing";
import ConsumedThing from "./consumed-thing";
export default class WoTImpl implements WoT.WoTFactory {
private srv;
constructor(srv: Servient);
discover(filter?: WoT.ThingFilter): Observable<ConsumedThing>;
consume(url: USVString): Promise<ConsumedThing>;
consumeDescription(thingDescription: string): Promise<ConsumedThing>;
expose(init: WoT.ThingInit): Promise<ExposedThing>;
createFromDescription(thingDescription: ThingDescription): Promise<ExposedThing>;
discover(filter?: WoT.ThingFilter): Observable<WoT.ConsumedThing>;
fetch(uri: USVString): Promise<WoT.ThingDescription>;
consume(td: WoT.ThingDescription): WoT.ConsumedThing;
isWoTThingDescription(arg: any): arg is WoT.ThingDescription;
isWoTThingTemplate(arg: any): arg is WoT.ThingTemplate;
produce(model: WoT.ThingModel): WoT.ExposedThing;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Observable_1 = require("rxjs/Observable");
var td_tools_1 = require("@node-wot/td-tools");
var TD = require("@node-wot/td-tools");
var exposed_thing_1 = require("./exposed-thing");
var consumed_thing_1 = require("./consumed-thing");
var Helpers = require("./helpers");
var TDParser = require("@node-wot/td-tools");
var WoTImpl = (function () {

@@ -18,15 +17,14 @@ function WoTImpl(srv) {

};
WoTImpl.prototype.consume = function (url) {
WoTImpl.prototype.fetch = function (uri) {
var _this = this;
return new Promise(function (resolve, reject) {
var client = _this.srv.getClientFor(Helpers.extractScheme(url));
console.info("WoTImpl consuming TD from " + url + " with " + client);
client.readResource(url)
var client = _this.srv.getClientFor(Helpers.extractScheme(uri));
console.info("WoTImpl fetching TD from '" + uri + "' with " + client);
client.readResource(new TD.InteractionForm(uri, "application/ld+json"))
.then(function (content) {
if (content.mediaType !== "application/json")
if (content.mediaType !== "application/ld+json") {
console.warn("WoTImpl parsing TD from '" + content.mediaType + "' media type");
var td = TDParser.parseTDString(content.body.toString());
var thing = new consumed_thing_1.default(_this.srv, td);
}
client.stop();
resolve(thing);
resolve(content.body.toString());
})

@@ -36,89 +34,37 @@ .catch(function (err) { console.error(err); });

};
WoTImpl.prototype.consumeDescription = function (thingDescription) {
var _this = this;
return new Promise(function (resolve, reject) {
console.info("WoTImpl consuming TD from object");
var td = TDParser.parseTDString(thingDescription);
var thing = new consumed_thing_1.default(_this.srv, td);
resolve(thing);
});
WoTImpl.prototype.consume = function (td) {
var trimmedTD = td.trim();
if (td[0] !== '{') {
throw new Error("WoT.consume() takes a Thing Description. Use WoT.fetch() for URIs.");
}
var newThing = new consumed_thing_1.default(this.srv, td);
console.info("WoTImpl consuming TD " + (newThing.id ? "'" + newThing.id + "'" : "without @id") + " for ConsumedThing '" + newThing.name + "'");
return newThing;
};
WoTImpl.prototype.expose = function (init) {
var _this = this;
if (init.description) {
console.info("WoTImpl creating new ExposedThing based on a given description");
var td = TDParser.parseTDObject(init.description);
return this.createFromDescription(td);
WoTImpl.prototype.isWoTThingDescription = function (arg) {
return arg.length !== undefined;
};
WoTImpl.prototype.isWoTThingTemplate = function (arg) {
return arg.name !== undefined;
};
WoTImpl.prototype.produce = function (model) {
var td = null;
if (this.isWoTThingDescription(model)) {
td = model;
}
else if (this.isWoTThingTemplate(model)) {
td = "{\"name\": \"" + model.name + "\"}";
}
else {
return new Promise(function (resolve, reject) {
console.info("WoTImpl creating new ExposedThing '" + init.name + "'");
var td = new td_tools_1.ThingDescription();
td.name = init.name;
var mything = new exposed_thing_1.default(_this.srv, td);
if (_this.srv.addThing(mything)) {
resolve(mything);
}
else {
reject(new Error("WoTImpl could not create Thing: " + mything));
}
});
throw new Error("WoTImpl could not create Thing because of unknown model argument " + model);
}
var newThing = new exposed_thing_1.default(this.srv, td);
console.info("WoTImpl producing new ExposedThing '" + newThing.name + "'");
if (this.srv.addThing(newThing)) {
return newThing;
}
else {
throw new Error("Thing already exists: " + newThing.name);
}
};
WoTImpl.prototype.createFromDescription = function (thingDescription) {
var _this = this;
return new Promise(function (resolve, reject) {
console.info("WoTImpl creating new ExposedThing from object");
var myThing = new exposed_thing_1.default(_this.srv, thingDescription);
if (_this.srv.addThing(myThing)) {
var interactions = thingDescription.interaction;
for (var i = 0; i < interactions.length; i++) {
var currentInter = interactions[i];
var interTypes = currentInter['semanticTypes'];
if (interTypes.indexOf("Action") > -1) {
var actionName = currentInter.name;
var inputValueType = null;
if (currentInter.inputData) {
inputValueType = currentInter.inputData.valueType;
}
var outputValueType = null;
if (currentInter.outputData) {
outputValueType = currentInter.outputData.valueType;
}
var init = {
name: actionName,
inputDataDescription: JSON.stringify(inputValueType),
outputDataDescription: JSON.stringify(outputValueType),
action: undefined
};
myThing.addAction(init);
}
else if (interTypes.indexOf("Property") > -1) {
var propertyName = currentInter.name;
var outputValueType = currentInter.outputData.valueType;
var init = {
name: propertyName,
description: JSON.stringify(outputValueType),
value: undefined
};
myThing.addProperty(init);
}
else if (interTypes.indexOf("Event") > -1) {
var eventName = currentInter.name;
var init = {
name: eventName
};
myThing.addEvent(init);
}
else {
console.info("Wrong interaction type for number ", i);
}
}
resolve(myThing);
}
else {
reject(new Error("WoTImpl could not create Thing from object: " + myThing));
}
});
};
return WoTImpl;

@@ -125,0 +71,0 @@ }());

{
"name": "@node-wot/core",
"version": "0.3.0",
"version": "0.4.0",
"description": "W3C Web of Things (WoT) Servient framework",
"author": "the thingweb community",
"author": "Eclipse Thingweb <thingweb-dev@eclipse.org> (https://thingweb.io/)",
"license": "W3C-20150513",

@@ -27,4 +27,5 @@ "repository": "https://github.com/thingweb/node-wot/tree/master/packages/core",

"dependencies": {
"@node-wot/td-tools": "^0.3.0",
"wot-typescript-definitions": "0.3.0"
"@node-wot/td-tools": "^0.4.0",
"rxjs": "5.4.3",
"wot-typescript-definitions": "0.4.0"
},

@@ -31,0 +32,0 @@ "scripts": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc