2021-10-20 22:20:43 +03:00
/ *
* Copyright 2021 Fluence Labs Limited
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
2021-09-08 12:42:30 +03:00
import log from 'loglevel' ;
2021-10-20 22:20:43 +03:00
import { CallServiceData , CallServiceResult , CallServiceResultType , ResultCodes } from './commonTypes' ;
2021-12-28 20:53:25 +03:00
import { FluencePeer } from './FluencePeer' ;
2021-11-09 14:37:44 +03:00
import { Particle , ParticleExecutionStage } from './Particle' ;
2022-02-17 10:54:38 +03:00
import Buffer from './Buffer' ;
2022-03-31 23:37:25 +03:00
import platform from 'platform' ;
2021-10-20 22:20:43 +03:00
export const MakeServiceCall = ( fn : ( args : any [ ] ) = > CallServiceResultType ) = > {
return ( req : CallServiceData ) : CallServiceResult = > {
return {
retCode : ResultCodes.success ,
result : fn ( req . args ) ,
} ;
} ;
2021-09-08 12:42:30 +03:00
} ;
2021-11-09 14:37:44 +03:00
export const handleTimeout = ( fn : Function ) = > ( stage : ParticleExecutionStage ) = > {
if ( stage . stage === 'expired' ) {
fn ( ) ;
}
} ;
export const doNothing = ( stage : ParticleExecutionStage ) = > { } ;
2021-09-08 12:42:30 +03:00
/ * *
2021-10-21 17:56:21 +03:00
* Checks the network connection by sending a ping - like request to relay node
2021-09-08 12:42:30 +03:00
* @param { FluenceClient } peer - The Fluence Client instance .
* /
export const checkConnection = async ( peer : FluencePeer , ttl? : number ) : Promise < boolean > = > {
2021-09-10 19:21:45 +03:00
if ( ! peer . getStatus ( ) . isConnected ) {
2021-09-08 12:42:30 +03:00
return false ;
}
const msg = Math . random ( ) . toString ( 36 ) . substring ( 7 ) ;
2021-10-20 22:20:43 +03:00
const promise = new Promise < string > ( ( resolve , reject ) = > {
const script = `
( xor
( seq
( call % init_peer_id % ( "load" "relay" ) [ ] init_relay )
( seq
( call % init_peer_id % ( "load" "msg" ) [ ] msg )
( seq
( call init_relay ( "op" "identity" ) [ msg ] result )
( call % init_peer_id % ( "callback" "callback" ) [ result ] )
)
)
2021-09-08 12:42:30 +03:00
)
2021-10-20 22:20:43 +03:00
( seq
( call init_relay ( "op" "identity" ) [ ] )
( call % init_peer_id % ( "callback" "error" ) [ % last_error % ] )
)
) ` ;
const particle = Particle . createNew ( script , ttl ) ;
peer . internals . regHandler . forParticle (
particle . id ,
'load' ,
'relay' ,
MakeServiceCall ( ( ) = > {
return peer . getStatus ( ) . relayPeerId ;
} ) ,
) ;
peer . internals . regHandler . forParticle (
particle . id ,
'load' ,
'msg' ,
MakeServiceCall ( ( ) = > {
return msg ;
} ) ,
) ;
peer . internals . regHandler . forParticle (
particle . id ,
'callback' ,
'callback' ,
MakeServiceCall ( ( args ) = > {
const [ val ] = args ;
setTimeout ( ( ) = > {
resolve ( val ) ;
} , 0 ) ;
return { } ;
} ) ,
) ;
2021-09-08 12:42:30 +03:00
2021-10-20 22:20:43 +03:00
peer . internals . regHandler . forParticle (
particle . id ,
'callback' ,
'error' ,
MakeServiceCall ( ( args ) = > {
const [ error ] = args ;
setTimeout ( ( ) = > {
reject ( error ) ;
} , 0 ) ;
return { } ;
} ) ,
) ;
2021-11-09 14:37:44 +03:00
peer . internals . initiateParticle (
particle ,
handleTimeout ( ( ) = > {
reject ( 'particle timed out' ) ;
} ) ,
) ;
2021-10-20 22:20:43 +03:00
} ) ;
2021-09-08 12:42:30 +03:00
try {
2021-10-21 17:56:21 +03:00
const result = await promise ;
2021-09-08 12:42:30 +03:00
if ( result != msg ) {
log . warn ( "unexpected behavior. 'identity' must return the passed arguments." ) ;
}
return true ;
} catch ( e ) {
log . error ( 'Error on establishing connection: ' , e ) ;
return false ;
}
} ;
2021-10-20 22:20:43 +03:00
export function dataToString ( data : Uint8Array ) {
2021-12-28 20:53:25 +03:00
const text = new TextDecoder ( ) . decode ( Buffer . from ( data ) ) ;
// try to treat data as json and pretty-print it
try {
return JSON . stringify ( JSON . parse ( text ) , null , 4 ) ;
} catch {
return text ;
}
}
export function jsonify ( obj ) {
return JSON . stringify ( obj , null , 4 ) ;
2021-10-20 22:20:43 +03:00
}
2022-03-31 23:37:25 +03:00
export function throwIfNotSupported() {
if ( platform . name === 'Node.js' ) {
const version = platform . version . split ( '.' ) . map ( Number ) ;
const major = version [ 0 ] ;
if ( major < 16 ) {
throw new Error ( 'FluenceJS requires node.js version >= "16.x"; Detected ' + platform . description + ' Please update node.js to version 16 or higher.\nYou can use https://nvm.sh utility to update node.js version: "nvm install 17 && nvm use 17 && nvm alias default 17"' ) ;
}
}
}