Socket
Socket
Sign inDemoInstall

@node-wot/core

Package Overview
Dependencies
21
Maintainers
5
Versions
39
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.8.11 to 0.8.12

7

dist/interaction-output.d.ts
import * as WoT from "wot-typescript-definitions";
import { Content } from "./content";
export declare class InteractionOutput implements WoT.InteractionOutput {
private content;
private parsedValue;
private buffer?;
private _stream?;
#private;
dataUsed: boolean;

@@ -14,3 +11,3 @@ form?: WoT.Form;

arrayBuffer(): Promise<ArrayBuffer>;
value<T>(): Promise<T>;
value<T extends WoT.DataSchemaValue>(): Promise<T>;
}

@@ -34,5 +34,17 @@ "use strict";

};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _InteractionOutput_content, _InteractionOutput_value, _InteractionOutput_buffer, _InteractionOutput_stream;
Object.defineProperty(exports, "__esModule", { value: true });

@@ -50,3 +62,7 @@ exports.InteractionOutput = void 0;

constructor(content, form, schema) {
this.content = content;
_InteractionOutput_content.set(this, void 0);
_InteractionOutput_value.set(this, void 0);
_InteractionOutput_buffer.set(this, void 0);
_InteractionOutput_stream.set(this, void 0);
__classPrivateFieldSet(this, _InteractionOutput_content, content, "f");
this.form = form;

@@ -57,4 +73,4 @@ this.schema = schema;

get data() {
if (this._stream) {
return this._stream;
if (__classPrivateFieldGet(this, _InteractionOutput_stream, "f")) {
return __classPrivateFieldGet(this, _InteractionOutput_stream, "f");
}

@@ -65,8 +81,8 @@ if (this.dataUsed) {

this.dataUsed = true;
return (this._stream = core_1.ProtocolHelpers.toWoTStream(this.content.body));
return (__classPrivateFieldSet(this, _InteractionOutput_stream, core_1.ProtocolHelpers.toWoTStream(__classPrivateFieldGet(this, _InteractionOutput_content, "f").body), "f"));
}
arrayBuffer() {
return __awaiter(this, void 0, void 0, function* () {
if (this.buffer) {
return this.buffer;
if (__classPrivateFieldGet(this, _InteractionOutput_buffer, "f")) {
return __classPrivateFieldGet(this, _InteractionOutput_buffer, "f");
}

@@ -76,5 +92,5 @@ if (this.dataUsed) {

}
const data = yield this.content.toBuffer();
const data = yield __classPrivateFieldGet(this, _InteractionOutput_content, "f").toBuffer();
this.dataUsed = true;
this.buffer = data;
__classPrivateFieldSet(this, _InteractionOutput_buffer, data, "f");
return data;

@@ -84,28 +100,32 @@ });

value() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (this.parsedValue != null)
return this.parsedValue;
if (__classPrivateFieldGet(this, _InteractionOutput_value, "f") !== undefined) {
return __classPrivateFieldGet(this, _InteractionOutput_value, "f");
}
if (this.dataUsed) {
throw new Error("Can't read the stream once it has been already used");
throw new errors_1.NotReadableError("Can't read the stream once it has been already used");
}
if (!this.form || !content_serdes_1.ContentSerdes.get().isSupported(this.content.type)) {
const message = !this.form ? "Missing form" : `Content type ${this.content.type} not supported`;
if (this.form == null) {
throw new errors_1.NotReadableError("No form defined");
}
if (this.schema == null || this.schema.type == null) {
throw new errors_1.NotReadableError("No schema defined");
}
if (!content_serdes_1.ContentSerdes.get().isSupported(__classPrivateFieldGet(this, _InteractionOutput_content, "f").type)) {
const message = `Content type ${__classPrivateFieldGet(this, _InteractionOutput_content, "f").type} not supported`;
throw new errors_1.NotSupportedError(message);
}
const data = yield this.content.toBuffer();
const bytes = yield __classPrivateFieldGet(this, _InteractionOutput_content, "f").toBuffer();
this.dataUsed = true;
this.buffer = data;
const value = content_serdes_1.ContentSerdes.get().contentToValue({ type: this.content.type, body: data }, (_a = this.schema) !== null && _a !== void 0 ? _a : {});
if (this.schema) {
const validate = ajv.compile(this.schema);
if (!validate(value)) {
debug(`schema = ${util.inspect(this.schema, { depth: 10, colors: true })}`);
debug(`value: ${value}`);
debug(`Errror: ${validate.errors}`);
throw new errors_1.DataSchemaError("Invalid value according to DataSchema", value);
}
__classPrivateFieldSet(this, _InteractionOutput_buffer, bytes, "f");
const json = content_serdes_1.ContentSerdes.get().contentToValue({ type: __classPrivateFieldGet(this, _InteractionOutput_content, "f").type, body: bytes }, this.schema);
const validate = ajv.compile(this.schema);
if (!validate(json)) {
debug(`schema = ${util.inspect(this.schema, { depth: 10, colors: true })}`);
debug(`value: ${json}`);
debug(`Errror: ${validate.errors}`);
throw new errors_1.DataSchemaError("Invalid value according to DataSchema", json);
}
this.parsedValue = value;
return this.parsedValue;
__classPrivateFieldSet(this, _InteractionOutput_value, json, "f");
return json;
});

@@ -115,2 +135,3 @@ }

exports.InteractionOutput = InteractionOutput;
_InteractionOutput_content = new WeakMap(), _InteractionOutput_value = new WeakMap(), _InteractionOutput_buffer = new WeakMap(), _InteractionOutput_stream = new WeakMap();
//# sourceMappingURL=interaction-output.js.map

@@ -6,2 +6,3 @@ import * as WoT from "wot-typescript-definitions";

export default class Servient {
#private;
private servers;

@@ -8,0 +9,0 @@ private clientFactories;

@@ -11,5 +11,17 @@ "use strict";

};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _Servient_wotInstance, _Servient_shutdown;
Object.defineProperty(exports, "__esModule", { value: true });

@@ -27,2 +39,4 @@ const wot_impl_1 = __importDefault(require("./wot-impl"));

this.credentialStore = new Map();
_Servient_wotInstance.set(this, void 0);
_Servient_shutdown.set(this, false);
}

@@ -173,2 +187,9 @@ addMediaType(codec, offered = false) {

return __awaiter(this, void 0, void 0, function* () {
if (__classPrivateFieldGet(this, _Servient_wotInstance, "f") !== undefined) {
debug("Servient started already -> nop -> returning previous WoT implementation");
return __classPrivateFieldGet(this, _Servient_wotInstance, "f");
}
if (__classPrivateFieldGet(this, _Servient_shutdown, "f")) {
throw Error("Servient cannot be started (again) since it was already stopped");
}
const serverStatus = [];

@@ -178,3 +199,3 @@ this.servers.forEach((server) => serverStatus.push(server.start(this)));

yield Promise.all(serverStatus);
return new wot_impl_1.default(this);
return (__classPrivateFieldSet(this, _Servient_wotInstance, new wot_impl_1.default(this), "f"));
});

@@ -184,5 +205,14 @@ }

return __awaiter(this, void 0, void 0, function* () {
if (__classPrivateFieldGet(this, _Servient_wotInstance, "f") === undefined) {
throw Error("Servient cannot be shutdown, wasn't even started");
}
if (__classPrivateFieldGet(this, _Servient_shutdown, "f")) {
debug("Servient shutdown already -> nop");
return;
}
this.clientFactories.forEach((clientFactory) => clientFactory.destroy());
const promises = this.servers.map((server) => server.stop());
yield Promise.all(promises);
__classPrivateFieldSet(this, _Servient_shutdown, true, "f");
__classPrivateFieldSet(this, _Servient_wotInstance, undefined, "f");
});

@@ -192,2 +222,3 @@ }

exports.default = Servient;
_Servient_wotInstance = new WeakMap(), _Servient_shutdown = new WeakMap();
//# sourceMappingURL=servient.js.map

@@ -58,8 +58,10 @@ "use strict";

const validation_1 = require("./validation");
const util_1 = require("util");
const { debug } = (0, logger_1.createLoggers)("core", "wot-impl");
class ThingDiscoveryProcess {
constructor(rawThingDescriptions, filter) {
constructor(directory, filter) {
this.directory = directory;
this.filter = filter;
this.filter = filter;
this.done = false;
this.rawThingDescriptions = rawThingDescriptions;
}

@@ -73,8 +75,13 @@ stop() {

return __asyncGenerator(this, arguments, function* _a() {
if (!(this.rawThingDescriptions instanceof Array)) {
this.error = new Error("Encountered an invalid output value.");
let rawThingDescriptions;
try {
const thingsPropertyOutput = yield __await(this.directory.readProperty("things"));
rawThingDescriptions = (yield __await(thingsPropertyOutput.value()));
}
catch (error) {
this.error = error instanceof Error ? error : new Error((0, util_1.inspect)(error));
this.done = true;
return yield __await(void 0);
}
for (const outputValue of this.rawThingDescriptions) {
for (const outputValue of rawThingDescriptions) {
if (this.done) {

@@ -106,5 +113,3 @@ return yield __await(void 0);

const consumedDirectoy = yield this.consume(directoyThingDescription);
const thingsPropertyOutput = yield consumedDirectoy.readProperty("things");
const rawThingDescriptions = yield thingsPropertyOutput.value();
return new ThingDiscoveryProcess(rawThingDescriptions, filter);
return new ThingDiscoveryProcess(consumedDirectoy, filter);
});

@@ -111,0 +116,0 @@ }

{
"name": "@node-wot/core",
"version": "0.8.11",
"version": "0.8.12",
"description": "W3C Web of Things (WoT) Servient framework",

@@ -23,3 +23,3 @@ "author": "Eclipse Thingweb <thingweb-dev@eclipse.org> (https://thingweb.io/)",

"dependencies": {
"@node-wot/td-tools": "0.8.11",
"@node-wot/td-tools": "0.8.12",
"@petamoriken/float16": "^3.1.1",

@@ -26,0 +26,0 @@ "ajv": "^8.11.0",

@@ -24,4 +24,38 @@ # Core of node-wot

## Codecs
### Octet-Stream
The octet-stream codec enables deserialization and serialization of binary data. To encode and decode sequences of bytes, the octet-stream codec uses the following content type parameters:
| Parameter | Description | Default |
| --- | --- | --- |
| `length` | Number of bytes produced during serialization or consumed during deserialization | |
| `signed` | Signedness of the data type, `true` or `false` | `true` |
| `byteSeq` | Endianness, enum of `BIG_ENDIAN`, `LITTLE_ENDIAN`, `BIG_ENDIAN_BYTE_SWAP`, `LITTLE_ENDIAN_BYTE_SWAP` | `BIG_ENDIAN` |
Additionally, the octet-stream codec supports the data schema terms below for addressing bit-fields:
| Term | Description | Default |
| --- | --- | --- |
| `ex:bitLength` | Number of bits produced during serialization or consumed during deserialization | |
| `ex:bitOffset` | Offset in bits from the beginning of the byte sequence | `0` |
With the help of the terms and parameters above, the octet-stream codec can be used to serialize and deserialize objects containing bit-fields and sequences of bytes, like in the following example.
#### Example
To serialize the object `{ flag1: true, numberProperty: 99, stringProperty: "Web" }` to a sequence of bytes, the content type `application/octet-stream;length=4;signed=false;`, along with the following data schema can be used:
```json
{
"type": "object",
"properties": {
"flag1": { "type": "boolean", "ex:bitOffset": 0, "ex:bitLength": 1 },
"numberProperty": { "type": "integer", "ex:bitOffset": 1, "ex:bitLength": 7 },
"stringProperty": { "type": "string", "ex:bitOffset": 8, "ex:bitLength": 24 }
}
}
```
## More Details
See <https://github.com/eclipse-thingweb/node-wot/>

@@ -19,3 +19,3 @@ /********************************************************************************

import { ProtocolHelpers } from "./core";
import { DataSchemaError, NotSupportedError } from "./errors";
import { DataSchemaError, NotReadableError, NotSupportedError } from "./errors";
import { Content } from "./content";

@@ -36,6 +36,6 @@ import Ajv from "ajv";

export class InteractionOutput implements WoT.InteractionOutput {
private content: Content;
private parsedValue: unknown;
private buffer?: ArrayBuffer;
private _stream?: ReadableStream;
#content: Content;
#value: unknown;
#buffer?: ArrayBuffer;
#stream?: ReadableStream;
dataUsed: boolean;

@@ -46,4 +46,4 @@ form?: WoT.Form;

public get data(): ReadableStream {
if (this._stream) {
return this._stream;
if (this.#stream) {
return this.#stream;
}

@@ -57,7 +57,7 @@

this.dataUsed = true;
return (this._stream = ProtocolHelpers.toWoTStream(this.content.body) as ReadableStream);
return (this.#stream = ProtocolHelpers.toWoTStream(this.#content.body) as ReadableStream);
}
constructor(content: Content, form?: WoT.Form, schema?: WoT.DataSchema) {
this.content = content;
this.#content = content;
this.form = form;

@@ -69,4 +69,4 @@ this.schema = schema;

async arrayBuffer(): Promise<ArrayBuffer> {
if (this.buffer) {
return this.buffer;
if (this.#buffer) {
return this.#buffer;
}

@@ -78,5 +78,5 @@

const data = await this.content.toBuffer();
const data = await this.#content.toBuffer();
this.dataUsed = true;
this.buffer = data;
this.#buffer = data;

@@ -86,13 +86,23 @@ return data;

async value<T>(): Promise<T> {
async value<T extends WoT.DataSchemaValue>(): Promise<T> {
// the value has been already read?
if (this.parsedValue != null) return this.parsedValue as T;
if (this.#value !== undefined) {
return this.#value as T;
}
if (this.dataUsed) {
throw new Error("Can't read the stream once it has been already used");
throw new NotReadableError("Can't read the stream once it has been already used");
}
if (this.form == null) {
throw new NotReadableError("No form defined");
}
if (this.schema == null || this.schema.type == null) {
throw new NotReadableError("No schema defined");
}
// is content type valid?
if (!this.form || !ContentSerdes.get().isSupported(this.content.type)) {
const message = !this.form ? "Missing form" : `Content type ${this.content.type} not supported`;
if (!ContentSerdes.get().isSupported(this.#content.type)) {
const message = `Content type ${this.#content.type} not supported`;
throw new NotSupportedError(message);

@@ -102,26 +112,21 @@ }

// read fully the stream
const data = await this.content.toBuffer();
const bytes = await this.#content.toBuffer();
this.dataUsed = true;
this.buffer = data;
this.#buffer = bytes;
// call the contentToValue
// TODO: should be fixed contentToValue MUST define schema as nullable
const value = ContentSerdes.get().contentToValue({ type: this.content.type, body: data }, this.schema ?? {});
const json = ContentSerdes.get().contentToValue({ type: this.#content.type, body: bytes }, this.schema);
// any data (schema)?
if (this.schema) {
// validate the schema
const validate = ajv.compile<T>(this.schema);
// validate the schema
const validate = ajv.compile<T>(this.schema);
if (!validate(value)) {
debug(`schema = ${util.inspect(this.schema, { depth: 10, colors: true })}`);
debug(`value: ${value}`);
debug(`Errror: ${validate.errors}`);
throw new DataSchemaError("Invalid value according to DataSchema", value as WoT.DataSchemaValue);
}
if (!validate(json)) {
debug(`schema = ${util.inspect(this.schema, { depth: 10, colors: true })}`);
debug(`value: ${json}`);
debug(`Errror: ${validate.errors}`);
throw new DataSchemaError("Invalid value according to DataSchema", json as WoT.DataSchemaValue);
}
this.parsedValue = value;
return this.parsedValue as T;
this.#value = json;
return json;
}
}

@@ -33,2 +33,5 @@ /********************************************************************************

#wotInstance?: typeof WoT;
#shutdown = false;
/** add a new codec to support a mediatype; offered mediatypes are listed in TDs */

@@ -214,2 +217,10 @@ public addMediaType(codec: ContentCodec, offered = false): void {

public async start(): Promise<typeof WoT> {
if (this.#wotInstance !== undefined) {
debug("Servient started already -> nop -> returning previous WoT implementation");
return this.#wotInstance;
}
if (this.#shutdown) {
throw Error("Servient cannot be started (again) since it was already stopped");
}
const serverStatus: Array<Promise<void>> = [];

@@ -220,6 +231,14 @@ this.servers.forEach((server) => serverStatus.push(server.start(this)));

await Promise.all(serverStatus);
return new WoTImpl(this);
return (this.#wotInstance = new WoTImpl(this));
}
public async shutdown(): Promise<void> {
if (this.#wotInstance === undefined) {
throw Error("Servient cannot be shutdown, wasn't even started");
}
if (this.#shutdown) {
debug("Servient shutdown already -> nop");
return;
}
this.clientFactories.forEach((clientFactory) => clientFactory.destroy());

@@ -229,3 +248,5 @@

await Promise.all(promises);
this.#shutdown = true;
this.#wotInstance = undefined; // clean-up reference
}
}

@@ -25,2 +25,3 @@ /********************************************************************************

import { getLastValidationErrors, isThingDescription } from "./validation";
import { inspect } from "util";

@@ -30,11 +31,7 @@ const { debug } = createLoggers("core", "wot-impl");

class ThingDiscoveryProcess implements WoT.ThingDiscoveryProcess {
constructor(rawThingDescriptions: WoT.DataSchemaValue, filter?: WoT.ThingFilter) {
constructor(private directory: ConsumedThing, public filter?: WoT.ThingFilter) {
this.filter = filter;
this.done = false;
this.rawThingDescriptions = rawThingDescriptions;
}
rawThingDescriptions: WoT.DataSchemaValue;
filter?: WoT.ThingFilter | undefined;
done: boolean;

@@ -47,4 +44,8 @@ error?: Error | undefined;

async *[Symbol.asyncIterator](): AsyncIterator<WoT.ThingDescription> {
if (!(this.rawThingDescriptions instanceof Array)) {
this.error = new Error("Encountered an invalid output value.");
let rawThingDescriptions: WoT.ThingDescription[];
try {
const thingsPropertyOutput = await this.directory.readProperty("things");
rawThingDescriptions = (await thingsPropertyOutput.value()) as WoT.ThingDescription[];
} catch (error) {
this.error = error instanceof Error ? error : new Error(inspect(error));
this.done = true;

@@ -54,3 +55,3 @@ return;

for (const outputValue of this.rawThingDescriptions) {
for (const outputValue of rawThingDescriptions) {
if (this.done) {

@@ -88,6 +89,3 @@ return;

const thingsPropertyOutput = await consumedDirectoy.readProperty("things");
const rawThingDescriptions = await thingsPropertyOutput.value();
return new ThingDiscoveryProcess(rawThingDescriptions, filter);
return new ThingDiscoveryProcess(consumedDirectoy, filter);
}

@@ -94,0 +92,0 @@

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc