@@ -30,17 +30,21 @@ binding.setupBufferJS(Buffer.prototype, bindingObj);
30
30
const zeroFill = bindingObj . zeroFill || [ 0 ] ;
31
31
32
32
function createUnsafeBuffer ( size ) {
33
+ return new FastBuffer ( createUnsafeArrayBuffer ( size ) ) ;
34
+ }
35
+
36
+ function createUnsafeArrayBuffer ( size ) {
33
37
zeroFill [ 0 ] = 0 ;
34
38
try {
35
- return new FastBuffer ( size ) ;
39
+ return new ArrayBuffer ( size ) ;
36
40
} finally {
37
41
zeroFill [ 0 ] = 1 ;
38
42
}
39
43
}
40
44
41
45
function createPool ( ) {
42
46
poolSize = Buffer . poolSize ;
43
- allocPool = createUnsafeBuffer ( poolSize ) ;
47
+ allocPool = createUnsafeArrayBuffer ( poolSize ) ;
44
48
poolOffset = 0 ;
45
49
}
46
50
createPool ( ) ;
@@ -183,7 +187,7 @@ function allocate(size) {
183
187
if ( size < ( Buffer . poolSize >>> 1 ) ) {
184
188
if ( size > ( poolSize - poolOffset ) )
185
189
createPool ( ) ;
186
- var b = allocPool . slice ( poolOffset , poolOffset + size ) ;
190
+ var b = new FastBuffer ( allocPool , poolOffset , size ) ;
187
191
poolOffset += size ;
188
192
alignPool ( ) ;
189
193
return b ;
@@ -210,8 +214,12 @@ function fromString(string, encoding) {
210
214
211
215
if ( length > ( poolSize - poolOffset ) )
212
216
createPool ( ) ;
213
- var actual = allocPool . write ( string , poolOffset , encoding ) ;
214
- var b = allocPool . slice ( poolOffset , poolOffset + actual ) ;
217
+ var b = new FastBuffer ( allocPool , poolOffset , length ) ;
218
+ var actual = b . write ( string , encoding ) ;
219
+ if ( actual !== length ) {
220
+ // byteLength() may overestimate. That’s a rare case, though.
221
+ b = new FastBuffer ( allocPool , poolOffset , actual ) ;
222
+ }
215
223
poolOffset += actual ;
216
224
alignPool ( ) ;
217
225
return b ;
0 commit comments