diff --git a/types/tar-stream/index.d.ts b/types/tar-stream/index.d.ts new file mode 100644 index 0000000000..21643fa906 --- /dev/null +++ b/types/tar-stream/index.d.ts @@ -0,0 +1,42 @@ +// Type definitions for tar-stream 1.6 +// Project: https://github.com/mafintosh/tar-stream +// Definitions by: Guy Lichtman +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.6 + +/// + +import stream = require('stream'); + +export type Callback = (err?: Error | null) => any; + +// see https://github.com/mafintosh/tar-stream/blob/master/headers.js +export interface Headers { + name: string; + mode?: number; + uid?: number; + gid?: number; + size?: number; + mtime?: Date; + linkname?: string | null; + type?: 'file' | 'link' | 'symlink' | 'character-device' | 'block-device' | 'directory' | 'fifo' | + 'contiguous-file' | 'pax-header' | 'pax-global-header' | 'gnu-long-link-path' | 'gnu-long-path' | null; + uname?: string; + gname?: string; + devmajor?: number; + devminor?: number; +} + +export interface Pack extends stream.Readable { + entry(headers: Headers, buffer?: string | Buffer | Callback, callback?: Callback): stream.Writable; + finalize(): void; +} + +export interface Extract extends stream.Writable { + on(event: string, listener: (...args: any[]) => void): this; + on(event: "entry", listener: (headers: Headers, stream: stream.PassThrough, next: () => void ) => void): this; +} + +export function extract(opts?: stream.WritableOptions): stream.Writable; + +export function pack(opts?: stream.ReadableOptions): Pack; diff --git a/types/tar-stream/tar-stream-tests.ts b/types/tar-stream/tar-stream-tests.ts new file mode 100644 index 0000000000..537c08bc0d --- /dev/null +++ b/types/tar-stream/tar-stream-tests.ts @@ -0,0 +1,44 @@ +import tar = require('tar-stream'); + +const pack = tar.pack(); + +// add a file called my-test.txt with the content "Hello World!" +pack.entry({ name: 'my-test1.txt' }, 'Hello World!'); + +// add a file called my-stream-test.txt from a stream +const entry = pack.entry({ name: 'my-test2.txt', size: 11 }, (err) => { + // the stream was added + // no more entries + pack.finalize(); +}); + +entry.write('hello'); +entry.write(' '); +entry.write('world'); +entry.end(); + +const extract = tar.extract(); + +const entries: any = {}; + +extract.on('entry', (header, stream, next) => { + // header is the tar header + // stream is the content body (might be an empty stream) + // call next when you are done with this entry + entries[header.name] = true; + stream.on('end', () => { + next(); + }); + + stream.resume(); // just auto drain the stream +}); + +extract.on('finish', () => { + // all entries read + console.assert(entries['my-test1.txt'], "missing entry"); + console.assert(entries['my-test2.txt'], "missing entry"); + console.log("Found all entries in extract"); +}); + +// pipe the pack stream +pack.pipe(extract); diff --git a/types/tar-stream/tsconfig.json b/types/tar-stream/tsconfig.json new file mode 100644 index 0000000000..efc9382e30 --- /dev/null +++ b/types/tar-stream/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "tar-stream-tests.ts" + ] +} diff --git a/types/tar-stream/tslint.json b/types/tar-stream/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/tar-stream/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }