// Copyright (c) 2014-2018, MyMonero.com
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
"use strict" ;
//
const ENVIRONMENT _IS _WEB = typeof window === "object" ;
const ENVIRONMENT _IS _WORKER = typeof importScripts === "function" ;
const ENVIRONMENT _IS _NODE = typeof process === "object" && process . browser !== true && typeof require === "function" && ENVIRONMENT _IS _WORKER == false ; // we want this to be true for Electron but not for a WebView
const wants _electronRemote = ( ENVIRONMENT _IS _NODE && ENVIRONMENT _IS _WEB ) /*this may become insufficient*/
|| ( typeof window !== 'undefined' && window . IsElectronRendererProcess == true ) ;
//
const moneroUtils _promise = new Promise ( function ( resolve , reject )
{
function _didLoad ( coreBridge _instance )
{
if ( coreBridge _instance == null ) {
throw "Unable to make coreBridge_instance"
}
const local _fns = { } ;
const fn _names = require ( './__bridged_fns_spec' ) . bridgedFn _names ;
for ( const i in fn _names ) {
const name = fn _names [ i ]
local _fns [ name ] = function ( )
{
const retVal = coreBridge _instance [ name ] . apply ( coreBridge _instance , arguments ) ; // called on the cached value
if ( typeof retVal === "object" ) {
const err _msg = retVal . err _msg
if ( typeof err _msg !== 'undefined' && err _msg ) {
throw err _msg ; // because we can't throw from electron remote w/o killing fn call
// ... and because parsing out this err_msg everywhere is sorta inefficient
}
}
return retVal ;
}
}
resolve ( local _fns ) ;
}
if ( wants _electronRemote ) {
// Require file again except on the main process ...
// this avoids a host of issues running wasm on the renderer side,
// for right now until we can load such files raw w/o unsafe-eval
// script-src CSP. makes calls synchronous. if that is a perf problem
// we can make API async.
//
// Resolves relative to the entrypoint of the main process.
const remoteModule = require ( 'electron' ) . remote . require (
"../mymonero_core_js/monero_utils/__IPCSafe_remote_monero_utils"
) ;
// Oftentimes this will be ready right away.. somehow.. but just in case.. the readiness
// state promise behavior should be preserved by the following codepath...
var _try ;
function _ _retryAfter ( attemptN )
{
console . warn ( "Checking remote module readiness again after a few ms..." )
setTimeout ( function ( )
{
_try ( attemptN + 1 )
} , 10 )
}
_try = function ( attemptN )
{
if ( attemptN > 10000 ) {
throw "Expected remote module to be ready"
}
if ( remoteModule . isReady ) {
_didLoad ( remoteModule ) ;
} else {
_ _retryAfter ( attemptN )
}
}
_try ( 0 )
} else {
const coreBridgeLoading _promise = require ( './MyMoneroCoreBridge' ) ( { } ) ; // this returns a promise
coreBridgeLoading _promise . catch ( function ( e )
{
console . error ( "Error: " , e ) ;
// this may be insufficient… being able to throw would be nice
reject ( e ) ;
} ) ;
coreBridgeLoading _promise . then ( _didLoad ) ;
}
} ) ;
//
//
// Since we actually are constructing bridge functions we technically have the export ready
// synchronously but that would lose the ability to wait until the core bridge is actually ready.
//
// TODO: in future, possibly return function which takes options instead to support better env.
//
module . exports = moneroUtils _promise ;