Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert common.js modules to new standard ESM module syntax #815

Merged
merged 1 commit into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 50 additions & 56 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
/* @flow */
'use strict' // eslint-disable-line strict

const _ = require('lodash')
const EventEmitter = require('events').EventEmitter
const common = require('./common')
const server = require('./server/server')
import * as _ from 'lodash'
import events from 'events'
import {Connection, errors, validate} from './common'
import * as server from './server/server'
const connect = server.connect
const disconnect = server.disconnect
const getServerInfo = server.getServerInfo
const getFee = server.getFee
const isConnected = server.isConnected
const getLedgerVersion = server.getLedgerVersion
const getTransaction = require('./ledger/transaction')
const getTransactions = require('./ledger/transactions')
const getTrustlines = require('./ledger/trustlines')
const getBalances = require('./ledger/balances')
const getBalanceSheet = require('./ledger/balance-sheet')
const getPaths = require('./ledger/pathfind')
const getOrders = require('./ledger/orders')
const getOrderbook = require('./ledger/orderbook')
const getSettings = require('./ledger/settings')
const getAccountInfo = require('./ledger/accountinfo')
const getPaymentChannel = require('./ledger/payment-channel')
const preparePayment = require('./transaction/payment')
const prepareTrustline = require('./transaction/trustline')
const prepareOrder = require('./transaction/order')
const prepareOrderCancellation = require('./transaction/ordercancellation')
const prepareEscrowCreation =
require('./transaction/escrow-creation')
const prepareEscrowExecution =
require('./transaction/escrow-execution')
const prepareEscrowCancellation =
require('./transaction/escrow-cancellation')
const preparePaymentChannelCreate =
require('./transaction/payment-channel-create')
const preparePaymentChannelFund =
require('./transaction/payment-channel-fund')
const preparePaymentChannelClaim =
require('./transaction/payment-channel-claim')
const prepareSettings = require('./transaction/settings')
const sign = require('./transaction/sign')
const combine = require('./transaction/combine')
const submit = require('./transaction/submit')
const errors = require('./common').errors
const generateAddress =
require('./offline/generate-address').generateAddressAPI
const computeLedgerHash = require('./offline/ledgerhash')
const signPaymentChannelClaim =
require('./offline/sign-payment-channel-claim')
const verifyPaymentChannelClaim =
require('./offline/verify-payment-channel-claim')
const getLedger = require('./ledger/ledger')
import getTransaction from './ledger/transaction'
import getTransactions from './ledger/transactions'
import getTrustlines from './ledger/trustlines'
import getBalances from './ledger/balances'
import getBalanceSheet from './ledger/balance-sheet'
import getPaths from './ledger/pathfind'
import getOrders from './ledger/orders'
import getOrderbook from './ledger/orderbook'
import getSettings from './ledger/settings'
import getAccountInfo from './ledger/accountinfo'
import getPaymentChannel from './ledger/payment-channel'
import preparePayment from './transaction/payment'
import prepareTrustline from './transaction/trustline'
import prepareOrder from './transaction/order'
import prepareOrderCancellation from './transaction/ordercancellation'
import prepareEscrowCreation from './transaction/escrow-creation'
import prepareEscrowExecution from './transaction/escrow-execution'
import prepareEscrowCancellation from './transaction/escrow-cancellation'
import preparePaymentChannelCreate from './transaction/payment-channel-create'
import preparePaymentChannelFund from './transaction/payment-channel-fund'
import preparePaymentChannelClaim from './transaction/payment-channel-claim'
import prepareSettings from './transaction/settings'
import sign from './transaction/sign'
import combine from './transaction/combine'
import submit from './transaction/submit'
import {generateAddressAPI} from './offline/generate-address'
import computeLedgerHash from './offline/ledgerhash'
import signPaymentChannelClaim from './offline/sign-payment-channel-claim'
import verifyPaymentChannelClaim from './offline/verify-payment-channel-claim'
import getLedger from './ledger/ledger'


import RangeSet from './common/rangeset'
import * as ledgerUtils from './ledger/utils'
import * as schemaValidator from './common/schema-validator'

type APIOptions = {
server?: string,
Expand All @@ -61,7 +55,7 @@ type APIOptions = {
}

// prevent access to non-validated ledger versions
class RestrictedConnection extends common.Connection {
class RestrictedConnection extends Connection {
request(request, timeout) {
const ledger_index = request.ledger_index
if (ledger_index !== undefined && ledger_index !== 'validated') {
Expand All @@ -75,21 +69,21 @@ class RestrictedConnection extends common.Connection {
}
}

class RippleAPI extends EventEmitter {
class RippleAPI extends events.EventEmitter {

_feeCushion: number;
connection: RestrictedConnection;
_feeCushion: number
connection: RestrictedConnection

// these are exposed only for use by unit tests; they are not part of the API.
static _PRIVATE = {
validate: common.validate,
RangeSet: require('./common/rangeset').RangeSet,
ledgerUtils: require('./ledger/utils'),
schemaValidator: require('./common/schema-validator')
validate: validate,
RangeSet,
ledgerUtils,
schemaValidator
};

constructor(options: APIOptions = {}) {
common.validate.apiOptions(options)
validate.apiOptions(options)
super()
this._feeCushion = options.feeCushion || 1.2
const serverURL = options.server
Expand Down Expand Up @@ -151,13 +145,13 @@ _.assign(RippleAPI.prototype, {
combine,
submit,

generateAddress,
generateAddress: generateAddressAPI,
computeLedgerHash,
signPaymentChannelClaim,
verifyPaymentChannelClaim,
errors
})

module.exports = {
export {
RippleAPI
}
8 changes: 4 additions & 4 deletions src/broadcast.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict' // eslint-disable-line strict
const _ = require('lodash')
const RippleAPI = require('./api').RippleAPI

import * as _ from 'lodash'
import {RippleAPI} from './api'

class RippleAPIBroadcast extends RippleAPI {
constructor(servers, options) {
Expand Down Expand Up @@ -65,6 +65,6 @@ class RippleAPIBroadcast extends RippleAPI {
}
}

module.exports = {
export {
RippleAPIBroadcast
}
6 changes: 3 additions & 3 deletions src/common/browser-hacks.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict' // eslint-disable-line strict


function setPrototypeOf(object, prototype) {
// Object.setPrototypeOf not supported on Internet Explorer 9
/* eslint-disable */
Object.setPrototypeOf ? Object.setPrototypeOf(object, prototype) :
object.__proto__ = prototype;
object.__proto__ = prototype
/* eslint-enable */
}

Expand All @@ -15,7 +15,7 @@ function getConstructorName(object) {
object.constructor.name
}

module.exports = {
export {
getConstructorName,
setPrototypeOf
}
26 changes: 13 additions & 13 deletions src/common/connection.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict' // eslint-disable-line strict

const _ = require('lodash')
const {EventEmitter} = require('events')
const WebSocket = require('ws')
const parseURL = require('url').parse
const RangeSet = require('./rangeset').RangeSet
const {RippledError, DisconnectedError, NotConnectedError,


import * as _ from 'lodash'
import events from 'events'
import WebSocket from 'ws'
import urlLib from 'url'
import RangeSet from './rangeset'
import {RippledError, DisconnectedError, NotConnectedError,
TimeoutError, ResponseFormatError, ConnectionError,
RippledNotInitializedError} = require('./errors')
RippledNotInitializedError} from './errors'

function isStreamMessageType(type) {
return type === 'ledgerClosed' ||
type === 'transaction' ||
type === 'path_find'
}

class Connection extends EventEmitter {
class Connection extends events.EventEmitter {
constructor(url, options = {}) {
super()
this.setMaxListeners(Infinity)
Expand Down Expand Up @@ -226,8 +226,8 @@ class Connection extends EventEmitter {
_createWebSocket() {
const options = {}
if (this._proxyURL !== undefined) {
const parsedURL = parseURL(this._url)
const parsedProxyURL = parseURL(this._proxyURL)
const parsedURL = urlLib.parse(this._url)
const parsedProxyURL = urlLib.parse(this._proxyURL)
const proxyOverrides = _.omitBy({
secureEndpoint: (parsedURL.protocol === 'wss:'),
secureProxy: (parsedProxyURL.protocol === 'https:'),
Expand Down Expand Up @@ -446,4 +446,4 @@ class Connection extends EventEmitter {
}
}

module.exports = Connection
export default Connection
22 changes: 11 additions & 11 deletions src/common/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict' // eslint-disable-line strict
const flagIndices = require('./txflags').txFlagIndices.AccountSet

import {txFlagIndices} from './txflags'

const accountRootFlags = {
PasswordSpent: 0x00010000, // password set fee is spent
Expand All @@ -24,14 +24,14 @@ const AccountFlags = {
}

const AccountFlagIndices = {
requireDestinationTag: flagIndices.asfRequireDest,
requireAuthorization: flagIndices.asfRequireAuth,
disallowIncomingXRP: flagIndices.asfDisallowXRP,
disableMasterKey: flagIndices.asfDisableMaster,
enableTransactionIDTracking: flagIndices.asfAccountTxnID,
noFreeze: flagIndices.asfNoFreeze,
globalFreeze: flagIndices.asfGlobalFreeze,
defaultRipple: flagIndices.asfDefaultRipple
requireDestinationTag: txFlagIndices.AccountSet.asfRequireDest,
requireAuthorization: txFlagIndices.AccountSet.asfRequireAuth,
disallowIncomingXRP: txFlagIndices.AccountSet.asfDisallowXRP,
disableMasterKey: txFlagIndices.AccountSet.asfDisableMaster,
enableTransactionIDTracking: txFlagIndices.AccountSet.asfAccountTxnID,
noFreeze: txFlagIndices.AccountSet.asfNoFreeze,
globalFreeze: txFlagIndices.AccountSet.asfGlobalFreeze,
defaultRipple: txFlagIndices.AccountSet.asfDefaultRipple
}

const AccountFields = {
Expand All @@ -42,7 +42,7 @@ const AccountFields = {
TransferRate: {name: 'transferRate', defaults: 0, shift: 9}
}

module.exports = {
export {
AccountFields,
AccountFlagIndices,
AccountFlags
Expand Down
8 changes: 4 additions & 4 deletions src/common/errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict' // eslint-disable-line strict
const util = require('util')
const browserHacks = require('./browser-hacks')

import util from 'util'
import * as browserHacks from './browser-hacks'

// this is needed because extending builtins doesn't work in babel 6.x
function extendableBuiltin(cls) {
Expand Down Expand Up @@ -80,7 +80,7 @@ class PendingLedgerVersionError extends RippleError {
}
}

module.exports = {
export {
RippleError,
UnexpectedError,
ConnectionError,
Expand Down
43 changes: 22 additions & 21 deletions src/common/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
'use strict' // eslint-disable-line strict
const utils = require('./utils')

module.exports = {
Connection: require('./connection'),
constants: require('./constants'),
errors: require('./errors'),
validate: require('./validate'),
txFlags: require('./txflags').txFlags,
serverInfo: require('./serverinfo'),
dropsToXrp: utils.dropsToXrp,
xrpToDrops: utils.xrpToDrops,
toRippledAmount: utils.toRippledAmount,
generateAddress: utils.generateAddress,
generateAddressAPI: utils.generateAddressAPI,
removeUndefined: utils.removeUndefined,
convertKeysFromSnakeCaseToCamelCase:
utils.convertKeysFromSnakeCaseToCamelCase,
iso8601ToRippleTime: utils.iso8601ToRippleTime,
rippleTimeToISO8601: utils.rippleTimeToISO8601,
isValidSecret: utils.isValidSecret
import * as constants from './constants'
import * as errors from './errors'
import * as validate from './validate'
import * as serverInfo from './serverinfo'
export {
constants,
errors,
validate,
serverInfo
}

export {
dropsToXrp,
xrpToDrops,
toRippledAmount,
removeUndefined,
convertKeysFromSnakeCaseToCamelCase,
iso8601ToRippleTime,
rippleTimeToISO8601
} from './utils'
export {default as Connection} from './connection'
export {txFlags} from './txflags'

10 changes: 5 additions & 5 deletions src/common/rangeset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */
'use strict' // eslint-disable-line strict
const _ = require('lodash')
const assert = require('assert')

import * as _ from 'lodash'
import assert from 'assert'

function mergeIntervals(intervals: Array<[number, number]>) {
const stack = [[-Infinity, -Infinity]]
Expand All @@ -19,7 +19,7 @@ function mergeIntervals(intervals: Array<[number, number]>) {

class RangeSet {

ranges: Array<[number, number]>;
ranges: Array<[number, number]>

constructor() {
this.reset()
Expand Down Expand Up @@ -60,4 +60,4 @@ class RangeSet {
}
}

module.exports.RangeSet = RangeSet
export default RangeSet
16 changes: 8 additions & 8 deletions src/common/schema-validator.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// flow is disabled for this file until support for requiring json is added:
// https://github.com/facebook/flow/issues/167
'use strict' // eslint-disable-line strict
const _ = require('lodash')
const assert = require('assert')
const Validator = require('jsonschema').Validator
const ValidationError = require('./errors').ValidationError
const {isValidAddress} = require('ripple-address-codec')
const {isValidSecret} = require('./utils')

import * as _ from 'lodash'
import assert from 'assert'
import {Validator} from 'jsonschema'
import {ValidationError} from './errors'
import {isValidAddress} from 'ripple-address-codec'
import {isValidSecret} from './utils'

function loadSchemas() {
// listed explicitly for webpack (instead of scanning schemas directory)
Expand Down Expand Up @@ -150,7 +150,7 @@ function schemaValidate(schemaName: string, object: any): void {
}
}

module.exports = {
export {
schemaValidate,
isValidSecret
}
Loading