Skip to content

Commit 6cae86d

Browse files
committed
Revert "wip: try revert all changes"
This reverts commit b92f0a7.
1 parent 487e331 commit 6cae86d

File tree

3 files changed

+153
-5
lines changed

3 files changed

+153
-5
lines changed

packages/pg/lib/connection.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ class Connection extends EventEmitter {
110110
}
111111

112112
attachListeners(stream) {
113-
// // Use the appropriate implementation based on whether maxResultSize is enabled
114-
// if (this._maxResultSize && this._maxResultSize > 0) {
115-
// this._attachListenersWithSizeLimit(stream)
116-
// } else {
113+
// Use the appropriate implementation based on whether maxResultSize is enabled
114+
if (this._maxResultSize && this._maxResultSize > 0) {
115+
this._attachListenersWithSizeLimit(stream)
116+
} else {
117117
this._attachListenersStandard(stream)
118-
// }
118+
}
119119
}
120120

121121
// Original implementation with no overhead
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict'
2+
var helper = require('./test-helper')
3+
var assert = require('assert')
4+
const { Client } = helper.pg
5+
const suite = new helper.Suite()
6+
7+
// Basic test to check if the _maxResultSize property is passed to Connection
8+
suite.test('client passes maxResultSize to connection', function (done) {
9+
var client = new Client({
10+
...helper.args,
11+
maxResultSize: 1024 * 1024, // 1MB limit
12+
})
13+
14+
client.connect(
15+
assert.success(function () {
16+
assert.equal(client.connection._maxResultSize, 1024 * 1024, 'maxResultSize should be passed to the connection')
17+
client.end(done)
18+
})
19+
)
20+
})
21+
22+
// Check if the correct attachListeners method is being used based on maxResultSize
23+
suite.test('connection uses correct listener implementation', function (done) {
24+
var client = new Client({
25+
...helper.args,
26+
maxResultSize: 1024 * 1024, // 1MB limit
27+
})
28+
29+
client.connect(
30+
assert.success(function () {
31+
// Just a simple check to see if our methods exist on the connection object
32+
assert(typeof client.connection._attachListenersStandard === 'function', 'Standard listener method should exist')
33+
assert(
34+
typeof client.connection._attachListenersWithSizeLimit === 'function',
35+
'Size-limiting listener method should exist'
36+
)
37+
client.end(done)
38+
})
39+
)
40+
})
41+
42+
// Test that small result sets complete successfully with maxResultSize set
43+
suite.test('small result with maxResultSize', function (done) {
44+
var client = new Client({
45+
...helper.args,
46+
maxResultSize: 1024 * 1024, // 1MB limit
47+
})
48+
49+
client.connect(
50+
assert.success(function () {
51+
client.query(
52+
'SELECT generate_series(1, 10) as num',
53+
assert.success(function (result) {
54+
assert.equal(result.rows.length, 10)
55+
client.end(done)
56+
})
57+
)
58+
})
59+
)
60+
})
61+
62+
// Test for large result size
63+
// Using a separate test to avoid issue with callback being called twice
64+
suite.testAsync('large result triggers error', async () => {
65+
const client = new Client({
66+
...helper.args,
67+
maxResultSize: 500, // Very small limit
68+
})
69+
70+
// Setup error handler
71+
const errorPromise = new Promise((resolve) => {
72+
client.on('error', (err) => {
73+
assert.equal(err.message, 'Query result size exceeded the configured limit')
74+
assert.equal(err.code, 'RESULT_SIZE_EXCEEDED')
75+
resolve()
76+
})
77+
})
78+
79+
await client.connect()
80+
81+
// Start the query but don't await it (it will error)
82+
const queryPromise = client.query("SELECT repeat('x', 1000) as data FROM generate_series(1, 100)").catch(() => {
83+
// We expect this to error out, silence the rejection
84+
return null
85+
})
86+
87+
// Wait for error event
88+
await errorPromise
89+
90+
// Make sure the query is done before we end
91+
await queryPromise
92+
93+
// Clean up
94+
await client.end().catch(() => {})
95+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict'
2+
3+
const helper = require('../test-helper')
4+
const pg = helper.pg
5+
var assert = require('assert')
6+
7+
const suite = new helper.Suite()
8+
9+
suite.testAsync('large result triggers error', async () => {
10+
const pool = new pg.Pool({
11+
...helper.args,
12+
maxResultSize: 1024, // very small limit
13+
})
14+
15+
const largeQuery = `
16+
SELECT generate_series(1, 1000) as num,
17+
repeat('x', 100) as data
18+
`
19+
20+
pool.on('error', (err) => {
21+
assert.equal(err.code, 'RESULT_SIZE_EXCEEDED')
22+
})
23+
24+
try {
25+
await pool.query(largeQuery)
26+
throw new Error('should have raised RESULT_SIZE_EXCEEDED error')
27+
} catch (err) {
28+
assert.equal(err.code, 'RESULT_SIZE_EXCEEDED')
29+
}
30+
})
31+
32+
suite.test('pool query works with adequate maxResultSize', (done) => {
33+
// Create a pool with a much larger limit
34+
const pool = new pg.Pool({
35+
...helper.args,
36+
maxResultSize: 100 * 1024,
37+
})
38+
39+
// Use a very simple query that returns a single value
40+
const simpleQuery = `SELECT 1 as num`
41+
42+
// This should succeed
43+
pool.query(simpleQuery, (err, result) => {
44+
if (err) {
45+
return done(err)
46+
}
47+
48+
// Verify we got the expected result
49+
assert.deepEqual(result.rows, [{ num: 1 }])
50+
// Test passed, clean up
51+
pool.end(done)
52+
})
53+
})

0 commit comments

Comments
 (0)