pax_global_header00006660000000000000000000000064137544565510014531gustar00rootroot0000000000000052 comment=5f8da23f89896eb4915cc9e17d64c540cfc18f0f gzip-size-6.0.0/000077500000000000000000000000001375445655100134555ustar00rootroot00000000000000gzip-size-6.0.0/.editorconfig000066400000000000000000000002571375445655100161360ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 gzip-size-6.0.0/.gitattributes000066400000000000000000000000231375445655100163430ustar00rootroot00000000000000* text=auto eol=lf gzip-size-6.0.0/.github/000077500000000000000000000000001375445655100150155ustar00rootroot00000000000000gzip-size-6.0.0/.github/funding.yml000066400000000000000000000001631375445655100171720ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/gzip-size custom: https://sindresorhus.com/donate gzip-size-6.0.0/.github/security.md000066400000000000000000000002631375445655100172070ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. gzip-size-6.0.0/.gitignore000066400000000000000000000000271375445655100154440ustar00rootroot00000000000000node_modules yarn.lock gzip-size-6.0.0/.npmrc000066400000000000000000000000231375445655100145700ustar00rootroot00000000000000package-lock=false gzip-size-6.0.0/.travis.yml000066400000000000000000000000661375445655100155700ustar00rootroot00000000000000language: node_js node_js: - '14' - '12' - '10' gzip-size-6.0.0/index.d.ts000066400000000000000000000054151375445655100153630ustar00rootroot00000000000000/// import * as stream from 'stream'; import {ZlibOptions} from 'zlib'; declare namespace gzipSize { type Options = ZlibOptions; interface GzipSizeStream extends stream.PassThrough { /** Contains the gzip size of the stream after it is finished. Since this happens asynchronously, it is recommended you use the `gzip-size` event instead. */ gzipSize?: number; addListener(event: 'gzip-size', listener: (size: number) => void): this; addListener( event: string | symbol, listener: (...args: any[]) => void ): this; on(event: 'gzip-size', listener: (size: number) => void): this; on(event: string | symbol, listener: (...args: any[]) => void): this; once(event: 'gzip-size', listener: (size: number) => void): this; once(event: string | symbol, listener: (...args: any[]) => void): this; removeListener(event: 'gzip-size', listener: (size: number) => void): this; removeListener( event: string | symbol, listener: (...args: any[]) => void ): this; off(event: 'gzip-size', listener: (size: number) => void): this; off(event: string | symbol, listener: (...args: any[]) => void): this; emit(event: 'gzip-size', size: number): boolean; emit(event: string | symbol, ...args: any[]): boolean; prependListener(event: 'gzip-size', listener: (size: number) => void): this; prependListener( event: string | symbol, listener: (...args: any[]) => void ): this; prependOnceListener( event: 'gzip-size', listener: (size: number) => void ): this; prependOnceListener( event: string | symbol, listener: (...args: any[]) => void ): this; } } declare const gzipSize: { /** Get the gzipped size of a string or buffer. @returns The gzipped size of `input`. */ (input: string | Buffer, options?: gzipSize.Options): Promise; /** Synchronously get the gzipped size of a string or buffer. @returns The gzipped size of `input`. @example ``` import gzipSize = require('gzip-size'); const text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.'; console.log(text.length); //=> 191 console.log(gzipSize.sync(text)); //=> 78 ``` */ sync(input: string | Buffer, options?: gzipSize.Options): number; /** @returns A stream that emits a `gzip-size` event and has a `gzipSize` property. */ stream(options?: gzipSize.Options): gzipSize.GzipSizeStream; /** Get the gzipped size of a file. @returns The size of the file. */ file(path: string, options?: gzipSize.Options): Promise; /** Synchronously get the gzipped size of a file. @returns The size of the file. */ fileSync(path: string, options?: gzipSize.Options): number; }; export = gzipSize; gzip-size-6.0.0/index.js000066400000000000000000000026651375445655100151330ustar00rootroot00000000000000'use strict'; const fs = require('fs'); const stream = require('stream'); const zlib = require('zlib'); const {promisify} = require('util'); const duplexer = require('duplexer'); const getOptions = options => ({level: 9, ...options}); const gzip = promisify(zlib.gzip); module.exports = async (input, options) => { if (!input) { return 0; } const data = await gzip(input, getOptions(options)); return data.length; }; module.exports.sync = (input, options) => zlib.gzipSync(input, getOptions(options)).length; module.exports.stream = options => { const input = new stream.PassThrough(); const output = new stream.PassThrough(); const wrapper = duplexer(input, output); let gzipSize = 0; const gzip = zlib.createGzip(getOptions(options)) .on('data', buf => { gzipSize += buf.length; }) .on('error', () => { wrapper.gzipSize = 0; }) .on('end', () => { wrapper.gzipSize = gzipSize; wrapper.emit('gzip-size', gzipSize); output.end(); }); input.pipe(gzip); input.pipe(output, {end: false}); return wrapper; }; module.exports.file = (path, options) => { return new Promise((resolve, reject) => { const stream = fs.createReadStream(path); stream.on('error', reject); const gzipStream = stream.pipe(module.exports.stream(options)); gzipStream.on('error', reject); gzipStream.on('gzip-size', resolve); }); }; module.exports.fileSync = (path, options) => module.exports.sync(fs.readFileSync(path), options); gzip-size-6.0.0/index.test-d.ts000066400000000000000000000014061375445655100163340ustar00rootroot00000000000000import {expectType} from 'tsd'; import * as fs from 'fs'; import gzipSize = require('.'); expectType>(gzipSize('foo')); expectType>(gzipSize('foo', {chunkSize: 1})); expectType(gzipSize.sync('foo')); expectType(gzipSize.sync('foo', {chunkSize: 1})); const gstream = fs .createReadStream('index.d.ts') .pipe(gzipSize.stream()) .pipe(gzipSize.stream({chunkSize: 1})) .on('gzip-size', size => expectType(size)); expectType(gstream.gzipSize); expectType>(gzipSize.file('index.d.ts')); expectType>(gzipSize.file('index.d.ts', {chunkSize: 1})); expectType(gzipSize.fileSync('index.d.ts')); expectType(gzipSize.fileSync('index.d.ts', {chunkSize: 1})); gzip-size-6.0.0/license000066400000000000000000000021351375445655100150230ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. gzip-size-6.0.0/package.json000066400000000000000000000013271375445655100157460ustar00rootroot00000000000000{ "name": "gzip-size", "version": "6.0.0", "description": "Get the gzipped size of a string or buffer", "license": "MIT", "repository": "sindresorhus/gzip-size", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "engines": { "node": ">=10" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "app", "tool", "zlib", "gzip", "compressed", "size", "string", "buffer" ], "dependencies": { "duplexer": "^0.1.2" }, "devDependencies": { "ava": "^2.4.0", "p-event": "^4.2.0", "tsd": "^0.13.1", "xo": "^0.34.2" } } gzip-size-6.0.0/readme.md000066400000000000000000000034001375445655100152310ustar00rootroot00000000000000# gzip-size [![Build Status](https://travis-ci.com/sindresorhus/gzip-size.svg?branch=master)](https://travis-ci.com/github/sindresorhus/gzip-size) > Get the gzipped size of a string or buffer ## Install ``` $ npm install gzip-size ``` ## Usage ```js const gzipSize = require('gzip-size'); const text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.'; console.log(text.length); //=> 191 console.log(gzipSize.sync(text)); //=> 78 ``` ## API ### gzipSize(input, options?) Returns a `Promise` with the size. ### gzipSize.sync(input, options?) Returns the size. #### input Type: `string | Buffer` #### options Type: `object` Any [`zlib` option](https://nodejs.org/api/zlib.html#zlib_class_options). ### gzipSize.stream(options?) Returns a [`stream.PassThrough`](https://nodejs.org/api/stream.html#stream_class_stream_passthrough). The stream emits a `gzip-size` event and has a `gzipSize` property. ### gzipSize.file(path, options?) Returns a `Promise` with the size of the file. #### path Type: `string` ### gzipSize.fileSync(path, options?) Returns the size of the file. ## Related - [gzip-size-cli](https://github.com/sindresorhus/gzip-size-cli) - CLI for this module ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
gzip-size-6.0.0/test.js000066400000000000000000000032171375445655100147750ustar00rootroot00000000000000import fs from 'fs'; import test from 'ava'; import pEvent from 'p-event'; import gzipSize from '.'; const fixture = fs.readFileSync('test.js', 'utf8'); test('get the gzipped size', async t => { t.true(await gzipSize(fixture) < fixture.length); }); test('gzip compression level', async t => { t.true(await gzipSize(fixture, {level: 6}) < await gzipSize(fixture, {level: 1})); }); test('sync - get the gzipped size', t => { t.true(gzipSize.sync(fixture) < fixture.length); }); test('sync - match async version', async t => { t.is(gzipSize.sync(fixture), await gzipSize(fixture)); }); test('sync - gzip compression level', t => { t.true(gzipSize.sync(fixture, {level: 6}) < gzipSize.sync(fixture, {level: 1})); }); test('stream', async t => { const stream = fs.createReadStream('test.js').pipe(gzipSize.stream()); await pEvent(stream, 'end'); t.is(stream.gzipSize, gzipSize.sync(fixture)); }); test('gzip-size event', async t => { const stream = fs.createReadStream('test.js').pipe(gzipSize.stream()); const size = await pEvent(stream, 'gzip-size'); t.is(size, gzipSize.sync(fixture)); }); test('passthrough', async t => { let out = ''; const stream = fs.createReadStream('test.js') .pipe(gzipSize.stream()) .on('data', buffer => { out += buffer; }); await pEvent(stream, 'end'); t.is(out, fixture); }); test('file - get the gzipped size', async t => { t.true(await gzipSize.file('test.js') < fixture.length); }); test('fileSync - get the gzipped size', t => { t.is(gzipSize.fileSync('test.js'), gzipSize.sync(fixture)); }); test('file - match async version', async t => { t.is(await gzipSize.file('test.js'), await gzipSize(fixture)); });