Skip to content

Commit

Permalink
Merge pull request #2044 from brianc/bmc/packet-stream-parser
Browse files Browse the repository at this point in the history
Add packet stream parser
  • Loading branch information
brianc committed Dec 27, 2019
2 parents 2431a63 + 6168f2e commit 3278dce
Show file tree
Hide file tree
Showing 19 changed files with 1,990 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Expand Up @@ -32,7 +32,7 @@ RUN apt-get update \
&& curl -sS https://dl.yarnpkg.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/pubkey.gpg | apt-key add - 2>/dev/null \
&& echo "deb https://dl.yarnpkg.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
&& apt-get update \
&& apt-get -y install --no-install-recommends yarn tmux locales \
&& apt-get -y install --no-install-recommends yarn tmux locales postgresql \
#
# Install eslint globally
&& npm install -g eslint \
Expand Down
21 changes: 17 additions & 4 deletions .eslintrc
@@ -1,8 +1,18 @@
{
"plugins": ["node"],
"extends": ["standard", "eslint:recommended", "plugin:node/recommended"],
"plugins": [
"node"
],
"extends": [
"standard",
"eslint:recommended",
"plugin:node/recommended"
],
"ignorePatterns": [
"**/*.ts"
],
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2017,
"sourceType": "module"
},
"env": {
"node": true,
Expand All @@ -11,10 +21,13 @@
},
"rules": {
"space-before-function-paren": "off",
"node/no-unsupported-features/es-syntax": "off",
"node/no-unpublished-require": [
"error",
{
"allowModules": ["pg"]
"allowModules": [
"pg"
]
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -5,3 +5,5 @@ build/
node_modules/
package-lock.json
*.swp
dist
.DS_Store
21 changes: 21 additions & 0 deletions packages/pg-packet-stream/package.json
@@ -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": {}
}
44 changes: 44 additions & 0 deletions packages/pg-packet-stream/src/BufferReader.ts
@@ -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;
}
}

0 comments on commit 3278dce

Please sign in to comment.