Skip to content

Commit

Permalink
chore: revision based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSun90 committed Oct 16, 2023
1 parent 03de398 commit 25a387b
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 276 deletions.
1 change: 0 additions & 1 deletion src/token/loginack-token-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function loginAckParser(buf: Buffer, offset: number, _options: ParserOptions): R
let tokenLength;
({ offset, value: tokenLength } = readUInt16LE(buf, offset));

// mocha --inspect test/integration/connection-test.js
if (buf.length < tokenLength + offset) {
throw new NotEnoughDataError(tokenLength + offset);

Check warning on line 19 in src/token/loginack-token-parser.ts

View check run for this annotation

Codecov / codecov/patch

src/token/loginack-token-parser.ts#L19

Added line #L19 was not covered by tests
}
Expand Down
274 changes: 0 additions & 274 deletions src/token/stream-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,280 +399,6 @@ class Parser {

this.position = 0;
}

suspend(next: () => void) {

}

awaitData(length: number, callback: () => void) {
if (this.position + length <= this.buffer.length) {
callback();
} else {
this.suspend(() => {
this.awaitData(length, callback);
});
}
}

readInt8(callback: (data: number) => void) {
this.awaitData(1, () => {
const data = this.buffer.readInt8(this.position);
this.position += 1;
callback(data);
});
}

readUInt8(callback: (data: number) => void) {
this.awaitData(1, () => {
const data = this.buffer.readUInt8(this.position);
this.position += 1;
callback(data);
});
}

readInt16LE(callback: (data: number) => void) {
this.awaitData(2, () => {
const data = this.buffer.readInt16LE(this.position);
this.position += 2;
callback(data);
});
}

readInt16BE(callback: (data: number) => void) {
this.awaitData(2, () => {
const data = this.buffer.readInt16BE(this.position);
this.position += 2;
callback(data);
});
}

readUInt16LE(callback: (data: number) => void) {
this.awaitData(2, () => {
const data = this.buffer.readUInt16LE(this.position);
this.position += 2;
callback(data);
});
}

readUInt16BE(callback: (data: number) => void) {
this.awaitData(2, () => {
const data = this.buffer.readUInt16BE(this.position);
this.position += 2;
callback(data);
});
}

readInt32LE(callback: (data: number) => void) {
this.awaitData(4, () => {
const data = this.buffer.readInt32LE(this.position);
this.position += 4;
callback(data);
});
}

readInt32BE(callback: (data: number) => void) {
this.awaitData(4, () => {
const data = this.buffer.readInt32BE(this.position);
this.position += 4;
callback(data);
});
}

readUInt32LE(callback: (data: number) => void) {
this.awaitData(4, () => {
const data = this.buffer.readUInt32LE(this.position);
this.position += 4;
callback(data);
});
}

readUInt32BE(callback: (data: number) => void) {
this.awaitData(4, () => {
const data = this.buffer.readUInt32BE(this.position);
this.position += 4;
callback(data);
});
}

readBigInt64LE(callback: (data: bigint) => void) {
this.awaitData(8, () => {
const data = this.buffer.readBigInt64LE(this.position);
this.position += 8;
callback(data);
});
}

readInt64LE(callback: (data: number) => void) {
this.awaitData(8, () => {
const data = Math.pow(2, 32) * this.buffer.readInt32LE(this.position + 4) + ((this.buffer[this.position + 4] & 0x80) === 0x80 ? 1 : -1) * this.buffer.readUInt32LE(this.position);
this.position += 8;
callback(data);
});
}

readInt64BE(callback: (data: number) => void) {
this.awaitData(8, () => {
const data = Math.pow(2, 32) * this.buffer.readInt32BE(this.position) + ((this.buffer[this.position] & 0x80) === 0x80 ? 1 : -1) * this.buffer.readUInt32BE(this.position + 4);
this.position += 8;
callback(data);
});
}

readBigUInt64LE(callback: (data: bigint) => void) {
this.awaitData(8, () => {
const data = this.buffer.readBigUInt64LE(this.position);
this.position += 8;
callback(data);
});
}

readUInt64LE(callback: (data: number) => void) {
this.awaitData(8, () => {
const data = Math.pow(2, 32) * this.buffer.readUInt32LE(this.position + 4) + this.buffer.readUInt32LE(this.position);
this.position += 8;
callback(data);
});
}

readUInt64BE(callback: (data: number) => void) {
this.awaitData(8, () => {
const data = Math.pow(2, 32) * this.buffer.readUInt32BE(this.position) + this.buffer.readUInt32BE(this.position + 4);
this.position += 8;
callback(data);
});
}

readFloatLE(callback: (data: number) => void) {
this.awaitData(4, () => {
const data = this.buffer.readFloatLE(this.position);
this.position += 4;
callback(data);
});
}

readFloatBE(callback: (data: number) => void) {
this.awaitData(4, () => {
const data = this.buffer.readFloatBE(this.position);
this.position += 4;
callback(data);
});
}

readDoubleLE(callback: (data: number) => void) {
this.awaitData(8, () => {
const data = this.buffer.readDoubleLE(this.position);
this.position += 8;
callback(data);
});
}

readDoubleBE(callback: (data: number) => void) {
this.awaitData(8, () => {
const data = this.buffer.readDoubleBE(this.position);
this.position += 8;
callback(data);
});
}

readUInt24LE(callback: (data: number) => void) {
this.awaitData(3, () => {
const low = this.buffer.readUInt16LE(this.position);
const high = this.buffer.readUInt8(this.position + 2);

this.position += 3;

callback(low | (high << 16));
});
}

readUInt40LE(callback: (data: number) => void) {
this.awaitData(5, () => {
const low = this.buffer.readUInt32LE(this.position);
const high = this.buffer.readUInt8(this.position + 4);

this.position += 5;

callback((0x100000000 * high) + low);
});
}

readUNumeric64LE(callback: (data: number) => void) {
this.awaitData(8, () => {
const low = this.buffer.readUInt32LE(this.position);
const high = this.buffer.readUInt32LE(this.position + 4);

this.position += 8;

callback((0x100000000 * high) + low);
});
}

readUNumeric96LE(callback: (data: number) => void) {
this.awaitData(12, () => {
const dword1 = this.buffer.readUInt32LE(this.position);
const dword2 = this.buffer.readUInt32LE(this.position + 4);
const dword3 = this.buffer.readUInt32LE(this.position + 8);

this.position += 12;

callback(dword1 + (0x100000000 * dword2) + (0x100000000 * 0x100000000 * dword3));
});
}

readUNumeric128LE(callback: (data: number) => void) {
this.awaitData(16, () => {
const dword1 = this.buffer.readUInt32LE(this.position);
const dword2 = this.buffer.readUInt32LE(this.position + 4);
const dword3 = this.buffer.readUInt32LE(this.position + 8);
const dword4 = this.buffer.readUInt32LE(this.position + 12);

this.position += 16;

callback(dword1 + (0x100000000 * dword2) + (0x100000000 * 0x100000000 * dword3) + (0x100000000 * 0x100000000 * 0x100000000 * dword4));
});
}

// Variable length data

readBuffer(length: number, callback: (data: Buffer) => void) {
this.awaitData(length, () => {
const data = this.buffer.slice(this.position, this.position + length);
this.position += length;
callback(data);
});
}

// Read a Unicode String (BVARCHAR)
readBVarChar(callback: (data: string) => void) {
this.readUInt8((length) => {
this.readBuffer(length * 2, (data) => {
callback(data.toString('ucs2'));
});
});
}

// Read a Unicode String (USVARCHAR)
readUsVarChar(callback: (data: string) => void) {
this.readUInt16LE((length) => {
this.readBuffer(length * 2, (data) => {
callback(data.toString('ucs2'));
});
});
}

// Read binary data (BVARBYTE)
readBVarByte(callback: (data: Buffer) => void) {
this.readUInt8((length) => {
this.readBuffer(length, callback);
});
}

// Read binary data (USVARBYTE)
readUsVarByte(callback: (data: Buffer) => void) {
this.readUInt16LE((length) => {
this.readBuffer(length, callback);
});
}
}

export default Parser;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/token/env-change-token-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Env Change Token Parser', () => {
assert.strictEqual(token.newValue, 2048);
});

it.only('should be of bad type', async () => {
it('should be of bad type', async () => {
const buffer = new WritableTrackingBuffer(50, 'ucs2');

buffer.writeUInt8(0xe3);
Expand Down

0 comments on commit 25a387b

Please sign in to comment.