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 support to stream factory #2898

Merged
merged 1 commit into from
Jan 23, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/pg/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ class Connection extends EventEmitter {
constructor(config) {
super()
config = config || {}

this.stream = config.stream || new net.Socket()
if (typeof this.stream === 'function') {
this.stream = this.stream(config)
}

this._keepAlive = config.keepAlive
this._keepAliveInitialDelayMillis = config.keepAliveInitialDelayMillis
this.lastBuffer = false
Expand Down
12 changes: 12 additions & 0 deletions packages/pg/test/unit/connection/startup-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ test('connection can take existing stream', function () {
assert.equal(con.stream, stream)
})

test('connection can take stream factory method', function () {
var stream = new MemoryStream()
var connectionOpts = {}
var makeStream = function (opts) {
assert.equal(connectionOpts, opts)
return stream
}
connectionOpts.stream = makeStream
var con = new Connection(connectionOpts)
assert.equal(con.stream, stream)
})

test('using any stream', function () {
var makeStream = function () {
var stream = new MemoryStream()
Expand Down