@@ -16,7 +16,7 @@ import * as util from "util";
1616import * as WoT from "wot-typescript-definitions" ;
1717import { ContentSerdes } from "./content-serdes" ;
1818import { ProtocolHelpers } from "./core" ;
19- import { DataSchemaError , NotSupportedError } from "./errors" ;
19+ import { DataSchemaError , NotReadableError , NotSupportedError } from "./errors" ;
2020import { Content } from "./content" ;
2121import Ajv from "ajv" ;
2222import { createLoggers } from "./logger" ;
@@ -33,7 +33,7 @@ const ajv = new Ajv({ strict: false });
3333
3434export class InteractionOutput implements WoT . InteractionOutput {
3535 private content : Content ;
36- private parsedValue : unknown ;
36+ #value : unknown ;
3737 private buffer ?: ArrayBuffer ;
3838 private _stream ?: ReadableStream ;
3939 dataUsed : boolean ;
@@ -79,43 +79,47 @@ export class InteractionOutput implements WoT.InteractionOutput {
7979
8080 async value < T extends WoT . DataSchemaValue > ( ) : Promise < T > {
8181 // the value has been already read?
82- if ( this . parsedValue !== undefined ) {
83- return this . parsedValue as T ;
82+ if ( this . #value !== undefined ) {
83+ return this . #value as T ;
8484 }
8585
8686 if ( this . dataUsed ) {
87- throw new Error ( "Can't read the stream once it has been already used" ) ;
87+ throw new NotReadableError ( "Can't read the stream once it has been already used" ) ;
88+ }
89+
90+ if ( this . form == null ) {
91+ throw new NotReadableError ( "No form defined" ) ;
92+ }
93+
94+ if ( this . schema == null || this . schema . type == null ) {
95+ throw new NotReadableError ( "No schema defined" ) ;
8896 }
8997
9098 // is content type valid?
91- if ( ! this . form || ! ContentSerdes . get ( ) . isSupported ( this . content . type ) ) {
92- const message = ! this . form ? "Missing form" : `Content type ${ this . content . type } not supported` ;
99+ if ( ! ContentSerdes . get ( ) . isSupported ( this . content . type ) ) {
100+ const message = `Content type ${ this . content . type } not supported` ;
93101 throw new NotSupportedError ( message ) ;
94102 }
95103
96104 // read fully the stream
97- const data = await this . content . toBuffer ( ) ;
105+ const bytes = await this . content . toBuffer ( ) ;
98106 this . dataUsed = true ;
99- this . buffer = data ;
107+ this . buffer = bytes ;
100108
101109 // call the contentToValue
102- // TODO: should be fixed contentToValue MUST define schema as nullable
103- const value = ContentSerdes . get ( ) . contentToValue ( { type : this . content . type , body : data } , this . schema ?? { } ) ;
104-
105- // any data (schema)?
106- if ( this . schema ) {
107- // validate the schema
108- const validate = ajv . compile < T > ( this . schema ) ;
109-
110- if ( ! validate ( value ) ) {
111- debug ( `schema = ${ util . inspect ( this . schema , { depth : 10 , colors : true } ) } ` ) ;
112- debug ( `value: ${ value } ` ) ;
113- debug ( `Errror: ${ validate . errors } ` ) ;
114- throw new DataSchemaError ( "Invalid value according to DataSchema" , value as WoT . DataSchemaValue ) ;
115- }
110+ const json = ContentSerdes . get ( ) . contentToValue ( { type : this . content . type , body : bytes } , this . schema ) ;
111+
112+ // validate the schema
113+ const validate = ajv . compile < T > ( this . schema ) ;
114+
115+ if ( ! validate ( json ) ) {
116+ debug ( `schema = ${ util . inspect ( this . schema , { depth : 10 , colors : true } ) } ` ) ;
117+ debug ( `value: ${ json } ` ) ;
118+ debug ( `Errror: ${ validate . errors } ` ) ;
119+ throw new DataSchemaError ( "Invalid value according to DataSchema" , json as WoT . DataSchemaValue ) ;
116120 }
117121
118- this . parsedValue = value ;
119- return this . parsedValue as T ;
122+ this . #value = json ;
123+ return this . #value as T ;
120124 }
121125}
0 commit comments