diff --git a/types/lzma-native/index.d.ts b/types/lzma-native/index.d.ts new file mode 100644 index 0000000000..33165885da --- /dev/null +++ b/types/lzma-native/index.d.ts @@ -0,0 +1,129 @@ +// Type definitions for lzma-native 4.0 +// Project: https://github.com/addaleax/lzma-native +// Definitions by: Evan Cameron +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 +/// + +import { Stream } from "stream"; +export interface LzmaOptions { + synchronous?: boolean; + bufsize?: number; + memlimit?: number; + check?: Check; + preset?: Preset; + flags?: + | "TELL_NO_CHECK" + | "TELL_UNSUPPORTED_CHECK" + | "TELL_ANY_CHECK" + | "CONCATENATED"; + threads?: number; + blockSize?: number; + timeout?: number; +} + +export type Check = + | "CHECK_CRC32" + | "CHECK_CRC64" + | "CHECK_NONE" + | "CHECK_SHA256"; + +export type Coders = + | "easyEncoder" + | "aloneDecoder" + | "rawEncoder" + | "autoDecoder" + | "aloneEncoder" + | "streamEncoder" + | "streamDecoder"; + +export type Preset = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; + +export interface FileOptions { + fileSize: number; + memlimit?: number; + read: ( + count: number, + offset: number, + cb: (err: any, buffer: Buffer) => void + ) => void; +} + +export interface StreamInfo { + streamPadding: number; + memlimit: number; + streams: number; + blocks: number; + fileSize: number; + uncompressedSize: number; + checks: number; +} + +export function createStream( + coder: Coders, + options?: LzmaOptions +): JSLzmaStream; + +export function createCompressor(options?: LzmaOptions): JSLzmaStream; +export function createDecompressor(options?: LzmaOptions): JSLzmaStream; +export function crc32( + input: string, + encoding?: string, + previous?: number +): string; +export function isXZ(buf: Buffer | string): boolean; +export function versionString(): string; +export function versionNumber(): number; +export function checkSize(check: Check): number; +export function easyDecoderMemusage(preset: Preset): number; +export function easyEncoderMemusage(preset: Preset): number; +export function rawDecoderMemusage(preset: Preset): number; +export function rawEncoderMemusage(preset: Preset): number; + +export function Compressor( + preset?: Preset, + options?: LzmaOptions +): JSLzmaStream; +export function Decompressor(options?: LzmaOptions): JSLzmaStream; + +export function parseFileIndex( + options: FileOptions, + callback?: (err: any, info?: StreamInfo) => void +): void; +export function parseFileIndexFD( + fileDescriptor: number, + callback?: (err: any, info?: StreamInfo) => void +): void; + +export function compress( + buf: Buffer | string, + options?: LzmaOptions | Preset, + on_finish?: (result: Buffer) => void +): void; +export function decompress( + buf: Buffer | string, + options?: LzmaOptions | Preset, + on_finish?: (result: Buffer) => void +): void; +export function LZMA(): { + compress( + buf: Buffer | string, + mode: Preset, + on_finish: (result: Buffer) => void, + on_progress?: (progress: number) => void + ): void; + decompress( + buf: Buffer | string, + on_finish: (result: Buffer) => void, + on_progress?: (progress: number) => void + ): void; +}; + +export class JSLzmaStream extends Stream.Transform { + constructor(nativeStream: Stream, options: LzmaOptions); + bufsize(): number; + bufsize(size: number): void; + totalInt(): number; + totalOut(): number; + cleanUp(): void; +} diff --git a/types/lzma-native/lzma-native-tests.ts b/types/lzma-native/lzma-native-tests.ts new file mode 100644 index 0000000000..44a5eab221 --- /dev/null +++ b/types/lzma-native/lzma-native-tests.ts @@ -0,0 +1,55 @@ +import * as lzma from "lzma-native"; +import * as fs from "fs"; + +const compressor = lzma.createCompressor(); +const input = fs.createReadStream("tsconfig.json"); +const output = fs.createWriteStream("tsconfig.json.xz"); + +input.pipe(compressor).pipe(output); + +lzma.compress("Banana", undefined, result => { + console.log(result); // +}); + +lzma.compress("Bananas", 6, result => { + lzma.decompress(result, undefined, decompressedResult => { + console.log(decompressedResult.toString() === "Bananas"); + }); +}); + +lzma.LZMA().compress("Bananas", 4, result => { + lzma.LZMA().decompress(result, decompressedResult => { + console.log("Bananas" === decompressedResult.toString()); + }); +}); + +const comp = lzma.Compressor(); + +process.stdin.pipe(comp).pipe(process.stdout); +lzma.crc32("Banana"); // => 69690105 +lzma.checkSize("CHECK_SHA256"); // => 16 +lzma.checkSize("CHECK_CRC32"); // => 4 +lzma.easyDecoderMemusage(6); // => 8454192 +lzma.easyEncoderMemusage(6); // => 97620499 +lzma.versionString(); // => '5.2.3' +lzma.versionNumber(); // => 50020012 +lzma.isXZ("Banana"); // => false + +fs.open("test/hamlet.txt.xz", "r", (err: any, fd: number) => { + if (err) return; + // handle error + lzma.parseFileIndexFD(fd, (err, info) => { + // handle error + if (err) { + console.log(err); + } + // do something with e.g. info.uncompressedSize + + fs.close(fd, (err: any) => { + /* handle error */ + if (err) { + console.log(err); + } + }); + }); +}); diff --git a/types/lzma-native/tsconfig.json b/types/lzma-native/tsconfig.json new file mode 100644 index 0000000000..fa7667f123 --- /dev/null +++ b/types/lzma-native/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "noImplicitThis": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "lzma-native-tests.ts"] +} diff --git a/types/lzma-native/tslint.json b/types/lzma-native/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/lzma-native/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }