Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add packet stream parser #2044

Merged
merged 11 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
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": {}
}
44 changes: 44 additions & 0 deletions packages/pg-packet-stream/src/BufferReader.ts
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;
}
}
Loading