import { IDataOperator } from "src/interfaces/IDataOperator" ; import { Connection } from "./Connection" ; import { EndpointOperator , EndpointOptions } from "./EndpointOperator" ; import { Endpoint } from "./Endpoint" ; import { HTTPMethod } from "./enums/HTTPMethod" ; import { Port } from "./Port" ; import { RequestData } from "./RequestData" ; export class CloudStorage extends EndpointOperator implements IDataOperator { inputPort : Port ; options : CloudStorageOptions ; connectionTable : { [ id ] : Connection ; } = { } ; fillColor = false ; color = "#dd1818" ; constructor ( ) { super ( ) ; this . inputPort = new Port ( this , false , true ) ; this . options = new CloudStorageOptions ( ) ; this . options . title = "Cloud Storage" ; this . options . endpoints = [ new Endpoint ( "/cloud" , [ HTTPMethod . GET , HTTPMethod . POST , HTTPMethod . PUT , HTTPMethod . DELETE ] ) ] ; } async receiveData ( data ) { let targetEndpoint = this . getTargetEndpoint ( data ) ; if ( targetEndpoint == null ) return ; this . fireReceiveData ( data ) ; this . requestReceived ( ) ; this . connectionTable [ data . requestId ] = data . origin ; if ( ! await this . throttleThroughput ( 5000 ) ) { this . requestProcessed ( ) ; return ; } this . requestProcessed ( ) ; if ( data . sendResponse ) await this . sendData ( this . getResponse ( data ) ) ; } async sendData ( response ) { let targetConnection = this . connectionTable [ response . responseId ] ; if ( targetConnection == null ) throw new Error ( "Target connection can not be null" ) ; this . connectionTable [ response . responseId ] = null ; await this . inputPort . sendData ( response , targetConnection ) ; } } export class CloudStorageOptions extends EndpointOptions { }