Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor (opts: StreamOpts) {
// Half-opened streams are not supported, support may be added in the future.
super({ allowHalfOpen: false })
this.id = opts.id
this.isServer = opts.isServer
this.log = createLogger(`ilp-protocol-stream:${this.isServer ? 'Server' : 'Client'}:Stream:${this.id}`)
this.log.info('new stream created')
this._totalSent = Long.UZERO
this._totalReceived = Long.UZERO
this._sendMax = Long.UZERO
this._receiveMax = Long.UZERO
this._outgoingHeldAmount = Long.UZERO
this._sentEnd = false
this._remoteSentEnd = false
this.closed = false
this.holds = {}
this._incomingData = new OffsetSorter()
this._outgoingData = new DataQueue()
// TODO we might want to merge this with the _outgoingData queue data structure
this._outgoingDataToRetry = []
this.outgoingOffset = 0
protected async loadAndSendPacket (): Promise {
// Actually send on the next tick of the event loop in case multiple streams
// have their limits raised at the same time
await new Promise((resolve, reject) => setImmediate(resolve))
this.log.trace('loadAndSendPacket')
let amountToSend = Long.UZERO
// TODO make sure the queued frames aren't too big
const requestPacket = new Packet(this.getNextPacketSequence(), IlpPacketType.Prepare, undefined, this.queuedFrames)
this.queuedFrames = []
// This is usually handled by the first test packet, but when the exchange rate
// is fixed it happens here instead.
this.maybePushAccountFrames(requestPacket)
// Send control frames
// TODO only send the max amount when it changes
for (let [_, stream] of this.streams) {
if (stream.isOpen()) {
requestPacket.frames.push(new StreamMaxMoneyFrame(stream.id, stream.receiveMax, stream.totalReceived))
requestPacket.frames.push(new StreamMaxDataFrame(stream.id, stream._getIncomingOffsets().maxAcceptable))
}
export function multiplyDivide (a: Long, b: Long, c: Long): {
quo: Long,
rem: Long
} {
let quo = Long.UZERO // quotient
let rem = Long.UZERO // remainder
let qn = b.divide(c)
let rn = b.modulo(c)
while (!a.isZero()) {
let oldQuo = quo
if (!a.and(Long.UONE).isZero()) {
quo = quo.add(qn)
rem = rem.add(rn)
if (rem.greaterThanOrEqual(c)) {
quo = quo.add(Long.UONE)
rem = rem.subtract(c)
}
}
// Overflow.
const array = charArray(word);
let entry = array.length > 0 ?
((array[0] | 32) - A) & 0xFF :
0;
let firstByte = 0;
if (entry < LETTERS)
firstByte = INJECTIVE_PHONES[entry];
else if (entry >= 0xDF && entry < 0xFF)
firstByte = INJECTIVE_PHONES_C1[entry - 0xDF];
firstByte = new Long(firstByte);
let res = Long.UZERO,
n = 0,
b = 1;
while (n < 8 && b < array.length) {
entry = ((array[b] | 32) - A) & 0xFF;
if (entry <= Z) {
let x;
if (entry < LETTERS)
x = PHONES[entry];
else if (entry >= 0xDF && entry < 0xFF)
x = PHONES_C1[entry - 0xDF];
else
continue;
export function checkedSubtract (a: Long, b: Long): {
difference: Long,
underflow: boolean
} {
const difference = a.subtract(b)
const underflow = difference.greaterThan(a) && difference.greaterThan(b)
return {
difference: underflow ? Long.UZERO : difference,
underflow
}
}
_getAmountStreamCanReceive (): Long {
if (this._receiveMax.lessThan(this._totalReceived)) {
return Long.UZERO
}
return checkedSubtract(this._receiveMax, this._totalReceived).difference
}
_getAmountAvailableToSend (): Long {
if (this.closed) {
return Long.UZERO
}
const amountAvailable = checkedSubtract(
checkedSubtract(this._sendMax, this._totalSent).difference,
this._outgoingHeldAmount
).difference
return amountAvailable
}
this.queuedFrames = []
this.congestion = new CongestionController({
maximumPacketAmount: opts.maximumPacketAmount === undefined
? undefined
: Long.fromString(opts.maximumPacketAmount, true)
})
this.retryDelay = RETRY_DELAY_START
this.remoteKnowsOurAccount = this.isServer
this.remoteMaxStreamId = DEFAULT_MAX_REMOTE_STREAMS * 2
this.remoteMaxOffset = this.maxBufferedData
this._totalReceived = Long.UZERO
this._totalSent = Long.UZERO
this._totalDelivered = Long.UZERO
this._lastPacketExchangeRate = Rational.UZERO
}
this.closed = true
this.queuedFrames = []
this.congestion = new CongestionController({
maximumPacketAmount: opts.maximumPacketAmount === undefined
? undefined
: Long.fromString(opts.maximumPacketAmount, true)
})
this.retryDelay = RETRY_DELAY_START
this.remoteKnowsOurAccount = this.isServer
this.remoteMaxStreamId = DEFAULT_MAX_REMOTE_STREAMS * 2
this.remoteMaxOffset = this.maxBufferedData
this._totalReceived = Long.UZERO
this._totalSent = Long.UZERO
this._totalDelivered = Long.UZERO
this._lastPacketExchangeRate = Rational.UZERO
}
function GCounter() {
let currentValue = Long.UZERO;
let delta = Long.UZERO;
/**
* The value as a long.
*
* @name module:cloudstate.crdt.GCounter#longValue
* @type {Long}
* @readonly
*/
Object.defineProperty(this, "longValue", {
get: function () {
return currentValue;
}
});
/**