Skip to content

Commit

Permalink
Update types & move some configs around
Browse files Browse the repository at this point in the history
  • Loading branch information
brianc committed Dec 27, 2019
1 parent e034010 commit 766e48f
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 153 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
15 changes: 7 additions & 8 deletions packages/pg-packet-stream/package.json
Expand Up @@ -2,21 +2,20 @@
"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",
"mocha": "^6.2.2",
"typescript": "^3.7.3"
},
"scripts": {
"test": "mocha -r ts-node/register src/**/*.test.ts"
},
"dependencies": {
"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": {}
}
103 changes: 0 additions & 103 deletions packages/pg-packet-stream/src/index.test.ts

This file was deleted.

40 changes: 23 additions & 17 deletions packages/pg-packet-stream/src/index.ts
@@ -1,5 +1,5 @@
import { Transform, TransformCallback, TransformOptions } from 'stream';
import { Mode, bindComplete, parseComplete, closeComplete, noData, portalSuspended, copyDone, replicationStart, emptyQuery, ReadyForQueryMessage, CommandCompleteMessage, CopyDataMessage, CopyResponse, NotificationResponseMessage, RowDescriptionMessage, Field, DataRowMessage, ParameterStatusMessage, BackendKeyDataMessage, DatabaseError, BackendMessage } from './messages';
import { Mode, bindComplete, parseComplete, closeComplete, noData, portalSuspended, copyDone, replicationStart, emptyQuery, ReadyForQueryMessage, CommandCompleteMessage, CopyDataMessage, CopyResponse, NotificationResponseMessage, RowDescriptionMessage, Field, DataRowMessage, ParameterStatusMessage, BackendKeyDataMessage, DatabaseError, BackendMessage, MessageName, AuthenticationMD5Password } from './messages';
import { BufferReader } from './BufferReader';
import assert from 'assert'

Expand Down Expand Up @@ -63,7 +63,12 @@ export class PgPacketStream extends Transform {
}

public _transform(buffer: Buffer, encoding: string, callback: TransformCallback) {
const combinedBuffer: Buffer = this.remainingBuffer.byteLength ? Buffer.concat([this.remainingBuffer, buffer], this.remainingBuffer.length + buffer.length) : buffer;
let combinedBuffer = buffer;
if (this.remainingBuffer.byteLength) {
combinedBuffer = Buffer.allocUnsafe(this.remainingBuffer.byteLength + buffer.byteLength);
this.remainingBuffer.copy(combinedBuffer)
buffer.copy(combinedBuffer, this.remainingBuffer.byteLength)
}
let offset = 0;
while ((offset + HEADER_LENGTH) <= combinedBuffer.byteLength) {
// code is 1 byte long - it identifies the message type
Expand Down Expand Up @@ -125,9 +130,9 @@ export class PgPacketStream extends Transform {
case MessageCodes.BackendKeyData:
return this.parseBackendKeyData(offset, length, bytes);
case MessageCodes.ErrorMessage:
return this.parseErrorMessage(offset, length, bytes, 'error');
return this.parseErrorMessage(offset, length, bytes, MessageName.error);
case MessageCodes.NoticeMessage:
return this.parseErrorMessage(offset, length, bytes, 'notice');
return this.parseErrorMessage(offset, length, bytes, MessageName.notice);
case MessageCodes.RowDescriptionMessage:
return this.parseRowDescriptionMessage(offset, length, bytes);
case MessageCodes.CopyIn:
Expand All @@ -142,7 +147,7 @@ export class PgPacketStream extends Transform {
}

public _flush(callback: TransformCallback) {
this._transform(Buffer.alloc(0), 'utf-i', callback)
this._transform(Buffer.alloc(0), 'utf-8', callback)
}

private parseReadyForQueryMessage(offset: number, length: number, bytes: Buffer) {
Expand All @@ -163,14 +168,14 @@ export class PgPacketStream extends Transform {
}

private parseCopyInMessage(offset: number, length: number, bytes: Buffer) {
return this.parseCopyMessage(offset, length, bytes, 'copyInResponse')
return this.parseCopyMessage(offset, length, bytes, MessageName.copyInResponse)
}

private parseCopyOutMessage(offset: number, length: number, bytes: Buffer) {
return this.parseCopyMessage(offset, length, bytes, 'copyOutResponse')
return this.parseCopyMessage(offset, length, bytes, MessageName.copyOutResponse)
}

private parseCopyMessage(offset: number, length: number, bytes: Buffer, messageName: string) {
private parseCopyMessage(offset: number, length: number, bytes: Buffer, messageName: MessageName) {
this.reader.setBuffer(offset, bytes);
const isBinary = this.reader.byte() !== 0;
const columnCount = this.reader.int16()
Expand Down Expand Up @@ -244,8 +249,8 @@ export class PgPacketStream extends Transform {
this.reader.setBuffer(offset, bytes);
const code = this.reader.int32()
// TODO(bmc): maybe better types here
const message: any = {
name: 'authenticationOk',
const message: BackendMessage & any = {
name: MessageName.authenticationOk,
length,
};

Expand All @@ -254,17 +259,18 @@ export class PgPacketStream extends Transform {
break;
case 3: // AuthenticationCleartextPassword
if (message.length === 8) {
message.name = 'authenticationCleartextPassword'
message.name = MessageName.authenticationCleartextPassword
}
break
case 5: // AuthenticationMD5Password
if (message.length === 12) {
message.name = 'authenticationMD5Password'
message.salt = this.reader.bytes(4);
message.name = MessageName.authenticationMD5Password
const salt = this.reader.bytes(4);
return new AuthenticationMD5Password(length, salt);
}
break
case 10: // AuthenticationSASL
message.name = 'authenticationSASL'
message.name = MessageName.authenticationSASL
message.mechanisms = []
let mechanism: string;
do {
Expand All @@ -276,11 +282,11 @@ export class PgPacketStream extends Transform {
} while (mechanism)
break;
case 11: // AuthenticationSASLContinue
message.name = 'authenticationSASLContinue'
message.name = MessageName.authenticationSASLContinue
message.data = this.reader.string(length - 4)
break;
case 12: // AuthenticationSASLFinal
message.name = 'authenticationSASLFinal'
message.name = MessageName.authenticationSASLFinal
message.data = this.reader.string(length - 4)
break;
default:
Expand All @@ -289,7 +295,7 @@ export class PgPacketStream extends Transform {
return message;
}

private parseErrorMessage(offset: number, length: number, bytes: Buffer, name: string) {
private parseErrorMessage(offset: number, length: number, bytes: Buffer, name: MessageName) {
this.reader.setBuffer(offset, bytes);
var fields: Record<string, string> = {}
var fieldType = this.reader.string(1)
Expand Down

0 comments on commit 766e48f

Please sign in to comment.