-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2044 from brianc/bmc/packet-stream-parser
Add packet stream parser
- Loading branch information
Showing
19 changed files
with
1,990 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ build/ | |
node_modules/ | ||
package-lock.json | ||
*.swp | ||
dist | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "pg-packet-stream", | ||
"version": "1.0.0", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^12.12.21", | ||
"chunky": "^0.0.0", | ||
"typescript": "^3.7.3", | ||
"@types/chai": "^4.2.7", | ||
"@types/mocha": "^5.2.7", | ||
"chai": "^4.2.0", | ||
"mocha": "^6.2.2", | ||
"ts-node": "^8.5.4" | ||
}, | ||
"scripts": { | ||
"test": "mocha -r ts-node/register src/**/*.test.ts" | ||
}, | ||
"dependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const emptyBuffer = Buffer.allocUnsafe(0); | ||
|
||
export class BufferReader { | ||
private buffer: Buffer = emptyBuffer; | ||
// TODO(bmc): support non-utf8 encoding | ||
private encoding: string = 'utf-8'; | ||
constructor(private offset: number = 0) { | ||
} | ||
public setBuffer(offset: number, buffer: Buffer): void { | ||
this.offset = offset; | ||
this.buffer = buffer; | ||
} | ||
public int16() { | ||
const result = this.buffer.readInt16BE(this.offset); | ||
this.offset += 2; | ||
return result; | ||
} | ||
public byte() { | ||
const result = this.buffer[this.offset]; | ||
this.offset++; | ||
return result; | ||
} | ||
public int32() { | ||
const result = this.buffer.readInt32BE(this.offset); | ||
this.offset += 4; | ||
return result; | ||
} | ||
public string(length: number): string { | ||
const result = this.buffer.toString(this.encoding, this.offset, this.offset + length); | ||
this.offset += length; | ||
return result; | ||
} | ||
public cstring(): string { | ||
var start = this.offset; | ||
var end = this.buffer.indexOf(0, start); | ||
this.offset = end + 1; | ||
return this.buffer.toString(this.encoding, start, end); | ||
} | ||
public bytes(length: number): Buffer { | ||
const result = this.buffer.slice(this.offset, this.offset + length); | ||
this.offset += length; | ||
return result; | ||
} | ||
} |
Oops, something went wrong.