0.9.1 • Published 9 days ago

@e-mc/compress v0.9.1

Weekly downloads
-
License
BSD 3-Clause
Repository
github
Last release
9 days ago

@e-mc/compress

  • NodeJS 16
  • ES2020

General Usage

Interface

import type { CompressLevel } from "./squared";

import type { IModule, ModuleConstructor } from "./index";
import type { BufferResult, CompressFormat, TryFileCompressor } from "./compress";
import type { CompressModule, CompressSettings } from "./settings";

import type { WriteStream } from "fs";
import type { Readable } from "stream";
import type { BrotliCompress, Gzip } from "zlib";

interface ICompress extends IModule {
    module: CompressModule;
    level: Record<string, number>;
    compressors: Record<string, TryFileCompressor>;
    chunkSize?: number;
    init(...args: unknown[]): this;
    register(format: string, callback: TryFileCompressor): void;
    getLevel(value: string, fallback?: number): number | undefined;
    getReadable(file: string | URL | Buffer): Readable;
    createGzip(file: string | Buffer, options?: CompressLevel): Gzip;
    createBrotliCompress(file: string | Buffer, options?: CompressLevel): BrotliCompress;
    createWriteStreamAsGzip(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
    createWriteStreamAsBrotli(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
    writeGzip(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
    writeBrotli(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
    tryFile(file: string | Buffer, options: CompressFormat): Promise<BufferResult>;
    tryFile(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
    tryImage(file: string, options: CompressFormat): Promise<BufferResult>;
    tryImage(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
    get settings(): CompressSettings;
}

interface CompressConstructor extends ModuleConstructor {
    singleton(): ICompress;
    readonly prototype: ICompress;
    new(module?: CompressModule): ICompress;
}

Settings

import type { BrotliOptions, ZlibOptions } from "zlib";
import type { Options as ZopfliOptions } from "node-zopfli";

interface CompressModule {
    gzip?: ZlibOptions;
    brotli?: BrotliOptions;
    zopfli?: ZopfliOptions;
    tinify?: {
        api_key?: string;
        proxy?: string;
    };
    settings?: {
        broadcast_id?: string | string[];
        cache?: boolean;
        cache_expires?: number | string;
        gzip_level?: number;
        brotli_quality?: number;
        zopfli_iterations?: number;
        chunk_size?: number | string;
    };
}

Example usage

const Compress = require("@e-mc/compress");

const instance = new Compress({
    gzip: {
        memLevel: 1,
        windowBits: 16
    },
    tinify: {
        api_key: "**********"
    },
    settings: {
        gzip_level: 9, // Lowest priority
        brotli_quality: 11,
        chunk_size: "16kb" // All compression types
    }
});
instance.init();

const stream = instance.createWriteStreamAsGzip("/tmp/archive.tar", "/path/output/archive.tar.gz", { level: 5, chunkSize: 4 * 1024 }); // Override settings
stream
    .on("finish", () => console.log("finish"))
    .on("error", err => console.error(err));

const options = {
    plugin: "tinify",
    format: "png", // Optional with extension
    timeout: 60 * 1000, // 1m
    options: {
        apiKey: "**********" // Override settings
    }
};
instance.tryImage("/tmp/image.png", "/path/output/compressed.png", options)
    .then(data => {
        console.log(Buffer.byteLength(data));
    })
    .catch(err => console.error(err));

References

LICENSE

BSD 3-Clause

0.8.9

9 days ago

0.8.8

9 days ago

0.7.6

9 days ago

0.7.5

9 days ago

0.8.10

9 days ago

0.6.3

9 days ago

0.7.4

9 days ago

0.5.6

9 days ago

0.9.1

9 days ago

0.9.0

10 days ago

0.8.7

1 month ago

0.6.2

1 month ago

0.7.3

1 month ago

0.5.5

1 month ago

0.8.6

2 months ago

0.7.2

2 months ago

0.5.4

2 months ago

0.6.1

2 months ago

0.8.5

2 months ago

0.8.4

3 months ago

0.8.3

3 months ago

0.8.2

3 months ago

0.8.1

4 months ago

0.8.0

5 months ago

0.7.1

5 months ago

0.7.0

5 months ago

0.6.0

9 months ago

0.5.3

12 months ago

0.5.2

1 year ago

0.5.1

1 year ago

0.5.0

1 year ago

0.4.2

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago