Skip to content

Commit ca988e9

Browse files
committed
Bugfix #136 - _writev can sometimes be called with too many chunks
1 parent a931989 commit ca988e9

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ Since this isn't a module with tons of installs and dependent modules I hope we
132132

133133
## changelog
134134

135+
### version 6.0.3 - published 2022-09-05
136+
137+
- copy-from: fix issue #136 when the _writev mechanism was triggered with a very large number of chunks
138+
135139
### version 6.0.2 - published 2021-09-13
136140

137141
- copy-from : fix interaction with `pg` optional timeout mechanism

copy-from.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ class CopyStreamQuery extends Writable {
4040
}
4141

4242
_writev(chunks, cb) {
43-
this.chunks.push(...chunks)
43+
// this.chunks.push(...chunks)
44+
// => issue #136, RangeError: Maximum call stack size exceeded
45+
// Using hybrid approach as advised on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
46+
const QUANTUM = 32768
47+
for (let i = 0; i < chunks.length; i += QUANTUM) {
48+
this.chunks.push(...chunks.slice(i, Math.min(i + QUANTUM, chunks.length)))
49+
}
4450
if (this._gotCopyInResponse) {
4551
return this.flush(cb)
4652
}

test/copy-from.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('copy-from', () => {
103103
})
104104

105105
it('correctly handle more heavy scenario', (done) => {
106-
const top = 10000
106+
const top = 130000
107107
const chunks = []
108108
const expected = []
109109
for (let i = 0; i < top; i++) {
@@ -116,7 +116,7 @@ describe('copy-from', () => {
116116
assert.equal(stream.rowCount, top, 'should have rowCount ' + top + ' ')
117117
done()
118118
})
119-
})
119+
}).timeout(120000)
120120

121121
it('test client reuse', (done) => {
122122
const fromClient = getClient()

0 commit comments

Comments
 (0)