diff --git a/stream-buffers/index.d.ts b/stream-buffers/index.d.ts new file mode 100644 index 0000000000..d3871b1efa --- /dev/null +++ b/stream-buffers/index.d.ts @@ -0,0 +1,40 @@ +// Type definitions for stream-buffers 3.0 +// Project: https://github.com/samcday/node-stream-buffer#readme +// Definitions by: Jason Dent +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +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; diff --git a/stream-buffers/stream-buffers-tests.ts b/stream-buffers/stream-buffers-tests.ts new file mode 100644 index 0000000000..2a1a1f35f8 --- /dev/null +++ b/stream-buffers/stream-buffers-tests.ts @@ -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(); diff --git a/stream-buffers/tsconfig.json b/stream-buffers/tsconfig.json new file mode 100644 index 0000000000..827ea2a3fe --- /dev/null +++ b/stream-buffers/tsconfig.json @@ -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" + ] +} diff --git a/stream-buffers/tslint.json b/stream-buffers/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/stream-buffers/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" }