pax_global_header 0000666 0000000 0000000 00000000064 13754456551 0014531 g ustar 00root root 0000000 0000000 52 comment=5f8da23f89896eb4915cc9e17d64c540cfc18f0f
gzip-size-6.0.0/ 0000775 0000000 0000000 00000000000 13754456551 0013455 5 ustar 00root root 0000000 0000000 gzip-size-6.0.0/.editorconfig 0000664 0000000 0000000 00000000257 13754456551 0016136 0 ustar 00root root 0000000 0000000 root = 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/.gitattributes 0000664 0000000 0000000 00000000023 13754456551 0016343 0 ustar 00root root 0000000 0000000 * text=auto eol=lf
gzip-size-6.0.0/.github/ 0000775 0000000 0000000 00000000000 13754456551 0015015 5 ustar 00root root 0000000 0000000 gzip-size-6.0.0/.github/funding.yml 0000664 0000000 0000000 00000000163 13754456551 0017172 0 ustar 00root root 0000000 0000000 github: sindresorhus
open_collective: sindresorhus
tidelift: npm/gzip-size
custom: https://sindresorhus.com/donate
gzip-size-6.0.0/.github/security.md 0000664 0000000 0000000 00000000263 13754456551 0017207 0 ustar 00root root 0000000 0000000 # 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/.gitignore 0000664 0000000 0000000 00000000027 13754456551 0015444 0 ustar 00root root 0000000 0000000 node_modules
yarn.lock
gzip-size-6.0.0/.npmrc 0000664 0000000 0000000 00000000023 13754456551 0014570 0 ustar 00root root 0000000 0000000 package-lock=false
gzip-size-6.0.0/.travis.yml 0000664 0000000 0000000 00000000066 13754456551 0015570 0 ustar 00root root 0000000 0000000 language: node_js
node_js:
- '14'
- '12'
- '10'
gzip-size-6.0.0/index.d.ts 0000664 0000000 0000000 00000005415 13754456551 0015363 0 ustar 00root root 0000000 0000000 ///
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.js 0000664 0000000 0000000 00000002665 13754456551 0015133 0 ustar 00root root 0000000 0000000 '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.ts 0000664 0000000 0000000 00000001406 13754456551 0016334 0 ustar 00root root 0000000 0000000 import {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/license 0000664 0000000 0000000 00000002135 13754456551 0015023 0 ustar 00root root 0000000 0000000 MIT 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.json 0000664 0000000 0000000 00000001327 13754456551 0015746 0 ustar 00root root 0000000 0000000 {
"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.md 0000664 0000000 0000000 00000003400 13754456551 0015231 0 ustar 00root root 0000000 0000000 # gzip-size [](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
---
gzip-size-6.0.0/test.js 0000664 0000000 0000000 00000003217 13754456551 0014775 0 ustar 00root root 0000000 0000000 import 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));
});