Merge pull request #14617 from Jason3S/master

Add types for stream-buffers
This commit is contained in:
Mine Starks
2017-03-08 00:35:54 -08:00
committed by GitHub
4 changed files with 111 additions and 0 deletions

40
stream-buffers/index.d.ts vendored Normal file
View File

@@ -0,0 +1,40 @@
// Type definitions for stream-buffers 3.0
// Project: https://github.com/samcday/node-stream-buffer#readme
// Definitions by: Jason Dent <https://github.com/Jason3S>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import * as stream from 'stream';
export interface WritableStreamBufferOptions extends stream.WritableOptions {
initialSize?: number;
incrementAmount?: number;
}
export declare class WritableStreamBuffer extends stream.Writable {
constructor(options?: WritableStreamBufferOptions);
size(): number;
maxSize(): number;
getContents(length?: number): any;
getContentsAsString(encoding?: string, length?: number): string;
}
export interface ReadableStreamBufferOptions extends stream.ReadableOptions {
frequency?: number;
chunkSize?: number;
initialSize?: number;
incrementAmount?: number;
}
export declare class ReadableStreamBuffer extends stream.Readable {
constructor(options?: ReadableStreamBufferOptions);
put(data: string | Buffer, encoding?: string): void;
stop(): void;
size(): number;
maxSize(): number;
}
export declare const DEFAULT_INITIAL_SIZE: number;
export declare const DEFAULT_INCREMENT_AMOUNT: number;
export declare const DEFAULT_FREQUENCY: number;
export declare const DEFAULT_CHUNK_SIZE: number;

View File

@@ -0,0 +1,48 @@
import * as streamBuffers from 'stream-buffers';
// The following are examples from README.md
// https://github.com/samcday/node-stream-buffer
var myWritableStreamBuffer = new streamBuffers.WritableStreamBuffer({
initialSize: (100 * 1024), // start at 100 kilobytes.
incrementAmount: (10 * 1024) // grow by 10 kilobytes each time buffer overflows.
});
var a = streamBuffers.DEFAULT_INITIAL_SIZE; // (8 * 1024)
var b = streamBuffers.DEFAULT_INCREMENT_AMOUNT; // (8 * 1024)
var c = streamBuffers.DEFAULT_CHUNK_SIZE; // (1024)
var d = streamBuffers.DEFAULT_FREQUENCY; // (1)
const buffer = new Buffer('ASDF');
myWritableStreamBuffer.write('ASDF');
myWritableStreamBuffer.write(buffer);
myWritableStreamBuffer.size();
myWritableStreamBuffer.maxSize();
// Gets all held data as a Buffer.
myWritableStreamBuffer.getContents();
// Gets all held data as a utf8 string.
myWritableStreamBuffer.getContentsAsString('utf8');
// Gets first 5 bytes as a Buffer.
myWritableStreamBuffer.getContents(5);
// Gets first 5 bytes as a utf8 string.
myWritableStreamBuffer.getContentsAsString('utf8', 5);
var myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({
frequency: 10, // in milliseconds.
chunkSize: 2048 // in bytes.
});
myReadableStreamBuffer.put('A String', 'utf8');
myReadableStreamBuffer.put(buffer);
myReadableStreamBuffer.on('data', function(data) {
// streams1.x style data
// assert.isTrue(data instanceof Buffer);
});
myReadableStreamBuffer.put('the last data this stream will ever see');
myReadableStreamBuffer.stop();

View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"stream-buffers-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "../tslint.json" }