[node] Add Buffer.poolSize() and correct Buffer.byteLength() (#20419)

* [node] Add Buffer.poolSize

* [node] Allow more types for Buffer.byteLength()

* fix lint issue
This commit is contained in:
Flarna
2017-10-16 20:18:42 +02:00
committed by Andy
parent 55b81a75c5
commit 2466ddbcff
2 changed files with 32 additions and 2 deletions

View File

@@ -239,10 +239,10 @@ declare var Buffer: {
* Gives the actual byte length of a string. encoding defaults to 'utf8'.
* This is not the same as String.prototype.length since that returns the number of characters in a string.
*
* @param string string to test.
* @param string string to test. (TypedArray is also allowed, but it is only available starting ES2017)
* @param encoding encoding used to evaluate (defaults to 'utf8')
*/
byteLength(string: string, encoding?: string): number;
byteLength(string: string | Buffer | DataView | ArrayBuffer, encoding?: string): number;
/**
* Returns a buffer which is the result of concatenating all the buffers in the list together.
*
@@ -282,6 +282,10 @@ declare var Buffer: {
* @param size count of octets to allocate
*/
allocUnsafeSlow(size: number): Buffer;
/**
* This is the number of bytes used to determine the size of pre-allocated, internal Buffer instances used for pooling. This value may be modified.
*/
poolSize: number;
};
/************************************************

View File

@@ -396,6 +396,32 @@ function bufferTests() {
const buf2: Buffer = Buffer.from('7468697320697320612074c3a97374', 'hex');
}
// Class Method byteLenght
{
let len: number;
len = Buffer.byteLength("foo");
len = Buffer.byteLength("foo", "utf8");
const b = Buffer.from("bar");
len = Buffer.byteLength(b);
len = Buffer.byteLength(b, "utf16le");
const ab = new ArrayBuffer(15);
len = Buffer.byteLength(ab);
len = Buffer.byteLength(ab, "ascii");
const dv = new DataView(ab);
len = Buffer.byteLength(dv);
len = Buffer.byteLength(dv, "utf16le");
}
// Class Method poolSize
{
let s: number;
s = Buffer.poolSize;
Buffer.poolSize = 4096;
}
// Test that TS 1.6 works with the 'as Buffer' annotation
// on isBuffer.
var a: Buffer | number;