How to use the @node-wot/core.ContentSerdes.get function in @node-wot/core

To help you get started, we’ve selected a few @node-wot/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eclipse / thingweb.node-wot / packages / binding-http / src / http-server.ts View on Github external
let mediaType: string = Array.isArray(contentTypeHeader) ? contentTypeHeader[0] : contentTypeHeader;

    console.log(`HttpServer on port ${this.getPort()} received ${req.method} ${requestUri.pathname} from ${req.socket.remoteAddress} port ${req.socket.remotePort}`);
    
    // FIXME must be rejected with 415 Unsupported Media Type, guessing not allowed -> debug/testing flag
    if ((req.method === "PUT" || req.method === "POST") && (!mediaType || mediaType.length == 0)) {
      console.warn(`HttpServer on port ${this.getPort()} got no Media Type for ${req.method}`);
      mediaType = ContentSerdes.DEFAULT;
    }

    if (requestHandler === undefined) {
      res.writeHead(404);
      res.end("Not Found");

    } else if ( (req.method === "PUT" || req.method === "POST")
              && ContentSerdes.get().getSupportedMediaTypes().indexOf(ContentSerdes.get().isolateMediaType(mediaType))<0) {
      res.writeHead(415);
      res.end("Unsupported Media Type");

    } else {
      if (req.method === "GET" && (requestHandler.getType()==="Property" || requestHandler.getType()==="Asset" ||(requestHandler.getType()==="TD"))) {
        requestHandler.onRead()
          .then(content => {
            if (!content.mediaType) {
              console.warn(`HttpServer on port ${this.getPort()} got no Media Type from ${req.socket.remoteAddress} port ${req.socket.remotePort}`);
            } else {
              res.setHeader("Content-Type", content.mediaType);
            }
            res.writeHead(200);
            res.end(content.body);
          })
          .catch(err => {
github eclipse / thingweb.node-wot / packages / binding-mqtt / src / mqtt-broker-server.ts View on Github external
(data) => {
            let content;
            try {
              content = ContentSerdes.get().valueToContent(data, event.data);
            } catch(err) {
              console.warn(`HttpServer on port ${this.getPort()} cannot process data for Event '${eventName}: ${err.message}'`);
              // subscription.unsubscribe();
              thing.unsubscribeEvent(eventName);
              return;
            }
            // send event data
            console.log(`MqttBrokerServer at ${this.brokerURI} publishing to Event topic '${eventName}' `);
            this.broker.publish(topic, content.body);
          }
        );