From 0933324489d2b4368bb955ec1696d16b9eecdf21 Mon Sep 17 00:00:00 2001 From: Evan Cameron Date: Fri, 2 Nov 2018 16:21:36 -0400 Subject: [PATCH] Add lzma-native types --- types/lzma-native/index.d.ts | 130 +++++++++++++++++++++++++ types/lzma-native/lzma-native-tests.ts | 55 +++++++++++ types/lzma-native/tsconfig.json | 13 +++ types/lzma-native/tslint.json | 1 + 4 files changed, 199 insertions(+) create mode 100644 types/lzma-native/index.d.ts create mode 100644 types/lzma-native/lzma-native-tests.ts create mode 100644 types/lzma-native/tsconfig.json create mode 100644 types/lzma-native/tslint.json diff --git a/types/lzma-native/index.d.ts b/types/lzma-native/index.d.ts new file mode 100644 index 0000000000..2581f4dc65 --- /dev/null +++ b/types/lzma-native/index.d.ts @@ -0,0 +1,130 @@ +// Type definitions for lzma-native 4.0 +// Project: https://github.com/addaleax/lzma-native +// Definitions by: Evan Cameron +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + +declare module "lzma-native" { + import { Stream } from "stream"; + export type 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 type FileOptions = { + fileSize: number; + memlimit?: number; + read: ( + count: number, + offset: number, + cb: (err: any, buffer: Buffer) => void + ) => void; + }; + + export type 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..2b94092c00 --- /dev/null +++ b/types/lzma-native/lzma-native-tests.ts @@ -0,0 +1,55 @@ +import * as lzma from "lzma-native"; +import { CHECK_CRC32 } from "lzma-native"; +var fs = require("fs"); + +var compressor = lzma.createCompressor(); +var input = fs.createReadStream("tsconfig.json"); +var output = fs.createWriteStream("tsconfig.json.xz"); + +input.pipe(compressor).pipe(output); + +lzma.compress("Banana", undefined, function(result) { + console.log(result); // +}); + +lzma.compress("Bananas", 6, function(result) { + lzma.decompress(result, undefined, function(decompressedResult) { + console.log(decompressedResult.toString() == "Bananas"); + }); +}); + +lzma.LZMA().compress("Bananas", 4, function(result) { + lzma.LZMA().decompress(result, function(decompressedResult) { + console.log("Bananas" == decompressedResult.toString()); + }); +}); + +var compressor = lzma.Compressor(); + +process.stdin.pipe(compressor).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", function(err: string, fd: number) { + // handle error + lzma.parseFileIndexFD(fd, function(err, info) { + // handle error + if (err) { + console.log(err); + } + // do something with e.g. info.uncompressedSize + + fs.close(fd, function(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..524764224f --- /dev/null +++ b/types/lzma-native/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "strict": 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" }