mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-25 22:55:49 +00:00
Initial change from Babel/JS to TypeScript (#70)
* will compile as typescript * migrated test suite to use JestJS * Migrated to Jest testing framework and typescript source files * updated deps * updated prepublish * resolved 1 failing test * changed decimal .0 on four tests, it appears that these were the only four tests expecting integer values to have '.0' * added linter * added package-lock * removed tslint in favor of eslint * changed yarn to npm * updated version 2.6->3.0 * removing package lock * updated node version in nvmrc and jest version in package * removed nvmrc * removed some unused functions * replaced data driven with file from master * commitint yarn.lock * removing babel as a dependency in favor of typescript compiling to es5 * removing babel deps * resolved testing issues by migrating helper function * added partial linting functionality for test suite * updated imports for decodeLedgerData * updated test * updated yarn.lock * removed a console.log * added eslint-jest-plugin to package * reverting to old linting, will add linting in next PR * removed comments in shamap * re-adding .nvmrc * npm -> yarn * added . to .eslintrc * added .eslintrc * removing linting for this PR * Changed linting to print a message so that linting doesnt fail in CI * changing back * added newline so diff wont show * removed eslint deps, since linting will be dealt with in a later PR * changed function calls to describe(...)
This commit is contained in:
98
packages/ripple-binary-codec/src/serdes/binary-parser.ts
Normal file
98
packages/ripple-binary-codec/src/serdes/binary-parser.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { strict as assert } from 'assert'
|
||||
import { makeClass } from '../utils/make-class'
|
||||
import { Enums } from '../enums'
|
||||
import { slice, parseBytes } from '../utils/bytes-utils'
|
||||
|
||||
const BinaryParser = makeClass({
|
||||
BinaryParser (buf) {
|
||||
this._buf = parseBytes(buf, Uint8Array)
|
||||
this._length = this._buf.length
|
||||
this._cursor = 0
|
||||
},
|
||||
skip (n) {
|
||||
this._cursor += n
|
||||
},
|
||||
read (n, to = Uint8Array) {
|
||||
const start = this._cursor
|
||||
const end = this._cursor + n
|
||||
assert(end <= this._buf.length)
|
||||
this._cursor = end
|
||||
return slice(this._buf, start, end, to)
|
||||
},
|
||||
readUIntN (n) {
|
||||
return this.read(n, Array).reduce((a, b) => a << 8 | b) >>> 0
|
||||
},
|
||||
readUInt8 () {
|
||||
return this._buf[this._cursor++]
|
||||
},
|
||||
readUInt16 () {
|
||||
return this.readUIntN(2)
|
||||
},
|
||||
readUInt32 () {
|
||||
return this.readUIntN(4)
|
||||
},
|
||||
pos () {
|
||||
return this._cursor
|
||||
},
|
||||
size () {
|
||||
return this._buf.length
|
||||
},
|
||||
end (customEnd) {
|
||||
const cursor = this.pos()
|
||||
return (cursor >= this._length) || (customEnd !== null &&
|
||||
cursor >= customEnd)
|
||||
},
|
||||
readVL () {
|
||||
return this.read(this.readVLLength())
|
||||
},
|
||||
readVLLength () {
|
||||
const b1 = this.readUInt8()
|
||||
if (b1 <= 192) {
|
||||
return b1
|
||||
} else if (b1 <= 240) {
|
||||
const b2 = this.readUInt8()
|
||||
return 193 + (b1 - 193) * 256 + b2
|
||||
} else if (b1 <= 254) {
|
||||
const b2 = this.readUInt8()
|
||||
const b3 = this.readUInt8()
|
||||
return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3
|
||||
}
|
||||
throw new Error('Invalid varint length indicator')
|
||||
},
|
||||
readFieldOrdinal () {
|
||||
const tagByte = this.readUInt8()
|
||||
const type = (tagByte & 0xF0) >>> 4 || this.readUInt8()
|
||||
const nth = tagByte & 0x0F || this.readUInt8()
|
||||
return type << 16 | nth
|
||||
},
|
||||
readField () {
|
||||
return Enums.Field.from(this.readFieldOrdinal())
|
||||
},
|
||||
readType (type) {
|
||||
return type.fromParser(this)
|
||||
},
|
||||
typeForField (field) {
|
||||
return field.associatedType
|
||||
},
|
||||
readFieldValue (field) {
|
||||
const kls = this.typeForField(field)
|
||||
if (!kls) {
|
||||
throw new Error(`unsupported: (${field.name}, ${field.type.name})`)
|
||||
}
|
||||
const sizeHint = field.isVLEncoded ? this.readVLLength() : null
|
||||
const value = kls.fromParser(this, sizeHint)
|
||||
if (value === undefined) {
|
||||
throw new Error(
|
||||
`fromParser for (${field.name}, ${field.type.name}) -> undefined `)
|
||||
}
|
||||
return value
|
||||
},
|
||||
readFieldAndValue () {
|
||||
const field = this.readField()
|
||||
return [field, this.readFieldValue(field)]
|
||||
}
|
||||
}, undefined)
|
||||
|
||||
export {
|
||||
BinaryParser
|
||||
}
|
||||
Reference in New Issue
Block a user