@syncot/util
Advanced tools
Comparing version 0.0.13 to 0.1.0
@@ -6,2 +6,23 @@ # Change Log | ||
# [0.1.0](https://github.com/SyncOT/SyncOT/compare/@syncot/util@0.0.13...@syncot/util@0.1.0) (2019-10-21) | ||
### Bug Fixes | ||
* make BufferReader/Writer string encoding optional ([7bc5d18](https://github.com/SyncOT/SyncOT/commit/7bc5d181cb2004a14df0c753a3798ad7794aaec8)) | ||
### Features | ||
* expose BufferReader/Writer length and offset ([a8cc936](https://github.com/SyncOT/SyncOT/commit/a8cc93693bd6e918ee11ad6867a1c826ee05a1e0)) | ||
### Performance Improvements | ||
* speed up TSON ([71a9789](https://github.com/SyncOT/SyncOT/commit/71a978925decf44b35a48ec2eca2287ece458960)) | ||
## [0.0.13](https://github.com/SyncOT/SyncOT/compare/@syncot/util@0.0.12...@syncot/util@0.0.13) (2019-09-12) | ||
@@ -8,0 +29,0 @@ |
@@ -27,1 +27,32 @@ /// <reference types="node" /> | ||
export declare function isSharedArrayBuffer(value: any): value is ArrayBuffer; | ||
export interface BufferWriter { | ||
readonly offset: number; | ||
readonly length: number; | ||
toBuffer(): Buffer; | ||
writeBuffer(value: Buffer): void; | ||
writeString(value: string, encoding?: BufferEncoding): void; | ||
writeUInt8(value: number): void; | ||
writeInt8(value: number): void; | ||
writeUInt16LE(value: number): void; | ||
writeInt16LE(value: number): void; | ||
writeUInt32LE(value: number): void; | ||
writeInt32LE(value: number): void; | ||
writeFloatLE(value: number): void; | ||
writeDoubleLE(value: number): void; | ||
} | ||
export declare function createBufferWriter(initialSize?: number): BufferWriter; | ||
export interface BufferReader { | ||
readonly offset: number; | ||
readonly length: number; | ||
readBuffer(size: number): Buffer; | ||
readString(size: number, encoding?: BufferEncoding): string; | ||
readUInt8(): number; | ||
readInt8(): number; | ||
readUInt16LE(): number; | ||
readInt16LE(): number; | ||
readUInt32LE(): number; | ||
readInt32LE(): number; | ||
readFloatLE(): number; | ||
readDoubleLE(): number; | ||
} | ||
export declare function createBufferReader(buffer: Buffer): BufferReader; |
@@ -38,1 +38,143 @@ export function toBuffer(binary) { | ||
} | ||
export function createBufferWriter(initialSize = 1024) { | ||
return new Writer(initialSize); | ||
} | ||
class Writer { | ||
constructor(initialSize) { | ||
this.offset = 0; | ||
this.buffer = Buffer.allocUnsafe(initialSize); | ||
} | ||
get length() { | ||
return this.offset; | ||
} | ||
toBuffer() { | ||
return this.buffer.slice(0, this.offset); | ||
} | ||
writeBuffer(value) { | ||
const size = value.length; | ||
this.ensure(size); | ||
value.copy(this.buffer, this.offset); | ||
this.offset += size; | ||
} | ||
writeString(value, encoding) { | ||
const size = Buffer.byteLength(value, encoding); | ||
this.ensure(size); | ||
this.buffer.write(value, this.offset, size, encoding); | ||
this.offset += size; | ||
} | ||
writeUInt8(value) { | ||
this.ensure(1); | ||
this.buffer[this.offset] = value; | ||
this.offset += 1; | ||
} | ||
writeInt8(value) { | ||
this.ensure(1); | ||
this.buffer[this.offset] = value; | ||
this.offset += 1; | ||
} | ||
writeUInt16LE(value) { | ||
this.writeNumber(this.buffer.writeUInt16LE, value, 2); | ||
} | ||
writeInt16LE(value) { | ||
this.writeNumber(this.buffer.writeInt16LE, value, 2); | ||
} | ||
writeUInt32LE(value) { | ||
this.writeNumber(this.buffer.writeUInt32LE, value, 4); | ||
} | ||
writeInt32LE(value) { | ||
this.writeNumber(this.buffer.writeInt32LE, value, 4); | ||
} | ||
writeFloatLE(value) { | ||
this.writeNumber(this.buffer.writeFloatLE, value, 4); | ||
} | ||
writeDoubleLE(value) { | ||
this.writeNumber(this.buffer.writeDoubleLE, value, 8); | ||
} | ||
writeNumber(fn, value, size) { | ||
this.ensure(size); | ||
fn.call(this.buffer, value, this.offset); | ||
this.offset += size; | ||
} | ||
ensure(size) { | ||
const minLength = this.offset + size; | ||
const oldLength = this.buffer.length; | ||
if (minLength > oldLength) { | ||
// tslint:disable-next-line:no-bitwise | ||
let newLength = ((oldLength * 3) / 2 + 1) | 0; | ||
if (newLength < minLength) { | ||
newLength = minLength; | ||
} | ||
const newBuffer = Buffer.allocUnsafe(newLength); | ||
this.buffer.copy(newBuffer); | ||
this.buffer = newBuffer; | ||
} | ||
} | ||
} | ||
export function createBufferReader(buffer) { | ||
return new Reader(buffer); | ||
} | ||
class Reader { | ||
constructor(buffer) { | ||
this.offset = 0; | ||
this.buffer = buffer; | ||
} | ||
get length() { | ||
return this.buffer.length; | ||
} | ||
readBuffer(size) { | ||
this.check(size); | ||
const result = this.buffer.slice(this.offset, this.offset + size); | ||
this.offset += size; | ||
return result; | ||
} | ||
readString(size, encoding) { | ||
this.check(size); | ||
const result = this.buffer | ||
.slice(this.offset, this.offset + size) | ||
.toString(encoding); | ||
this.offset += size; | ||
return result; | ||
} | ||
readUInt8() { | ||
this.check(1); | ||
const result = this.buffer[this.offset]; | ||
this.offset += 1; | ||
return result; | ||
} | ||
readInt8() { | ||
this.check(1); | ||
// tslint:disable-next-line:no-bitwise | ||
const result = (this.buffer[this.offset] << 24) >> 24; | ||
this.offset += 1; | ||
return result; | ||
} | ||
readUInt16LE() { | ||
return this.readNumber(this.buffer.readUInt16LE, 2); | ||
} | ||
readInt16LE() { | ||
return this.readNumber(this.buffer.readInt16LE, 2); | ||
} | ||
readUInt32LE() { | ||
return this.readNumber(this.buffer.readUInt32LE, 4); | ||
} | ||
readInt32LE() { | ||
return this.readNumber(this.buffer.readInt32LE, 4); | ||
} | ||
readFloatLE() { | ||
return this.readNumber(this.buffer.readFloatLE, 4); | ||
} | ||
readDoubleLE() { | ||
return this.readNumber(this.buffer.readDoubleLE, 8); | ||
} | ||
readNumber(fn, size) { | ||
this.check(size); | ||
const result = fn.call(this.buffer, this.offset); | ||
this.offset += size; | ||
return result; | ||
} | ||
check(size) { | ||
if (this.offset + size > this.buffer.length) { | ||
throw new RangeError('Insufficient data to read.'); | ||
} | ||
} | ||
} |
@@ -23,3 +23,3 @@ /** | ||
*/ | ||
export declare const validate: <T>(validators: Validator<T>[]) => (target: T) => Error | undefined; | ||
export declare const validate: <T>(validators: Validator<T>[]) => (target: T) => ValidationResult; | ||
/** | ||
@@ -26,0 +26,0 @@ * Keeps only public properties. |
{ | ||
"name": "@syncot/util", | ||
"version": "0.0.13", | ||
"version": "0.1.0", | ||
"description": "A collection of utilities required by other @syncot modules.", | ||
@@ -34,3 +34,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "d68eb77d7261147fdfae63420e0a791c2a48f831" | ||
"gitHead": "5ddb4179d53c84ccf7929fddeb5447d30890812e" | ||
} |
46140
1207