0.10.3 • Published 11 months ago

@e-mc/compress v0.10.3

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
11 months ago

@e-mc/compress

  • NodeJS 16
  • ES2020

General Usage

Interface

import type { IModule, ModuleConstructor } from "./index";
import type { BufferResult, CompressFormat, CompressLevel, ReadableOptions, 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, options?: ReadableOptions): 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>;
    set chunkSize(value: number | string | undefined): void;
    get chunkSize(): number | undefined;
    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;
    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: "@pi-r/tinify",
    format: "png", // Recommended
    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.5.10

1 year ago

0.5.11

1 year ago

0.5.16

11 months ago

0.5.14

11 months ago

0.5.15

11 months ago

0.5.12

1 year ago

0.5.13

12 months ago

0.9.8

1 year ago

0.7.11

1 year ago

0.9.7

1 year ago

0.7.10

1 year ago

0.7.13

1 year ago

0.9.9

1 year ago

0.7.12

1 year ago

0.9.4

1 year ago

0.5.8

1 year ago

0.9.3

1 year ago

0.5.7

1 year ago

0.9.6

1 year ago

0.5.9

1 year ago

0.9.5

1 year ago

0.7.15

11 months ago

0.7.14

12 months ago

0.7.17

11 months ago

0.7.16

11 months ago

0.10.1

1 year ago

0.10.2

12 months ago

0.10.3

11 months ago

0.10.0

1 year ago

0.9.12

11 months ago

0.9.13

11 months ago

0.6.7

1 year ago

0.6.6

1 year ago

0.9.10

12 months ago

0.6.9

1 year ago

0.9.11

11 months ago

0.6.8

1 year ago

0.6.10

1 year ago

0.6.12

11 months ago

0.6.11

12 months ago

0.6.14

11 months ago

0.6.13

11 months ago

0.9.2

1 year ago

0.7.9

1 year ago

0.8.21

11 months ago

0.7.8

1 year ago

0.8.20

11 months ago

0.7.7

1 year ago

0.8.12

1 year ago

0.8.11

1 year ago

0.8.14

1 year ago

0.8.13

1 year ago

0.8.19

11 months ago

0.8.16

1 year ago

0.8.15

1 year ago

0.8.18

12 months ago

0.8.17

1 year ago

0.6.5

1 year ago

0.6.4

1 year ago

0.8.9

1 year ago

0.8.8

1 year ago

0.7.6

1 year ago

0.7.5

1 year ago

0.8.10

1 year ago

0.6.3

1 year ago

0.7.4

1 year ago

0.5.6

1 year ago

0.9.1

1 year ago

0.9.0

1 year ago

0.8.7

1 year ago

0.6.2

1 year ago

0.7.3

1 year ago

0.5.5

1 year ago

0.8.6

2 years ago

0.7.2

2 years ago

0.5.4

2 years ago

0.6.1

2 years ago

0.8.5

2 years ago

0.8.4

2 years ago

0.8.3

2 years ago

0.8.2

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.7.1

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.3

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago