Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 23, 2021
1 parent fdb1ec1 commit 6b32b3f
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 147 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
169 changes: 88 additions & 81 deletions index.d.ts
@@ -1,96 +1,103 @@
/// <reference types="node"/>
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;
}
}
import {Buffer} from 'node:buffer';
import {PassThrough as PassThroughStream} from 'node:stream';
import {ZlibOptions} from 'node:zlib';

declare const gzipSize: {
/**
Get the gzipped size of a string or buffer.
export type Options = ZlibOptions;

@returns The gzipped size of `input`.
export interface GzipSizeStream extends PassThroughStream {
/**
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.
*/
(input: string | Buffer, options?: gzipSize.Options): Promise<number>;
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;
}

/**
Synchronously get the gzipped size of a string or buffer.
/**
Get the gzipped size of a string or buffer.
@returns The gzipped size of `input`.
@returns The gzipped size of `input`.
@example
```
import gzipSize = require('gzip-size');
@example
```
import {gzipSize} from '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.';
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(text.length);
//=> 191
console.log(gzipSize.sync(text));
//=> 78
```
*/
sync(input: string | Buffer, options?: gzipSize.Options): number;
console.log(await gzipSize(text));
//=> 78
```
*/
export function gzipSize(input: string | Buffer, options?: Options): Promise<number>;

/**
@returns A stream that emits a `gzip-size` event and has a `gzipSize` property.
*/
stream(options?: gzipSize.Options): gzipSize.GzipSizeStream;
/**
Synchronously get the gzipped size of a string or buffer.
/**
Get the gzipped size of a file.
@returns The gzipped size of `input`.
@returns The size of the file.
*/
file(path: string, options?: gzipSize.Options): Promise<number>;
@example
```
import {gzipSizeSync} from 'gzip-size';
/**
Synchronously get the gzipped size of a file.
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.';
@returns The size of the file.
*/
fileSync(path: string, options?: gzipSize.Options): number;
};
console.log(text.length);
//=> 191
console.log(gzipSizeSync(text));
//=> 78
```
*/
export function gzipSizeSync(input: string | Buffer, options?: Options): number;

/**
Get the gzipped size of a file.
@returns The size of the file.
*/
export function gzipSizeFromFile(filePath: string, options?: Options): Promise<number>;

/**
Synchronously get the gzipped size of a file.
@returns The size of the file.
*/
export function gzipSizeFromFileSync(filePath: string, options?: Options): number;

export = gzipSize;
/**
@returns A stream that emits a `gzip-size` event and has a `gzipSize` property.
*/
export function gzipSizeStream(options?: Options): GzipSizeStream;
55 changes: 31 additions & 24 deletions index.js
@@ -1,25 +1,45 @@
'use strict';
const fs = require('fs');
const stream = require('stream');
const zlib = require('zlib');
const {promisify} = require('util');
const duplexer = require('duplexer');
import fs from 'node:fs';
import stream from 'node:stream';
import zlib from 'node:zlib';
import {promisify} from 'node:util';
import duplexer from 'duplexer';

const getOptions = options => ({level: 9, ...options});
const gzip = promisify(zlib.gzip);

module.exports = async (input, options) => {
export async function gzipSize(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;
export function gzipSizeSync(input, options) {
return zlib.gzipSync(input, getOptions(options)).length;
}

export function gzipSizeFromFile(path, options) {
// TODO: Use `stream.pipeline` here.

return new Promise((resolve, reject) => {
const stream = fs.createReadStream(path);
stream.on('error', reject);

const gzipStream = stream.pipe(gzipSizeStream(options));
gzipStream.on('error', reject);
gzipStream.on('gzip-size', resolve);
});
}

export function gzipSizeFromFileSync(path, options) {
return gzipSizeSync(fs.readFileSync(path), options);
}

export function gzipSizeStream(options) {
// TODO: Use `stream.pipeline` here.

module.exports.stream = options => {
const input = new stream.PassThrough();
const output = new stream.PassThrough();
const wrapper = duplexer(input, output);
Expand All @@ -42,17 +62,4 @@ module.exports.stream = options => {
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);
}
21 changes: 11 additions & 10 deletions index.test-d.ts
@@ -1,18 +1,19 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-return */
import fs from 'node:fs';
import {expectType} from 'tsd';
import * as fs from 'fs';
import gzipSize = require('.');
import {gzipSize, gzipSizeSync, gzipSizeFromFile, gzipSizeFromFileSync, gzipSizeStream} from './index.js';

expectType<Promise<number>>(gzipSize('foo'));
expectType<Promise<number>>(gzipSize('foo', {chunkSize: 1}));
expectType<number>(gzipSize.sync('foo'));
expectType<number>(gzipSize.sync('foo', {chunkSize: 1}));
expectType<number>(gzipSizeSync('foo'));
expectType<number>(gzipSizeSync('foo', {chunkSize: 1}));
const gstream = fs
.createReadStream('index.d.ts')
.pipe(gzipSize.stream())
.pipe(gzipSize.stream({chunkSize: 1}))
.pipe(gzipSizeStream())
.pipe(gzipSizeStream({chunkSize: 1}))
.on('gzip-size', size => expectType<number>(size));
expectType<number | undefined>(gstream.gzipSize);
expectType<Promise<number>>(gzipSize.file('index.d.ts'));
expectType<Promise<number>>(gzipSize.file('index.d.ts', {chunkSize: 1}));
expectType<number>(gzipSize.fileSync('index.d.ts'));
expectType<number>(gzipSize.fileSync('index.d.ts', {chunkSize: 1}));
expectType<Promise<number>>(gzipSizeFromFile('index.d.ts'));
expectType<Promise<number>>(gzipSizeFromFile('index.d.ts', {chunkSize: 1}));
expectType<number>(gzipSizeFromFileSync('index.d.ts'));
expectType<number>(gzipSizeFromFileSync('index.d.ts', {chunkSize: 1}));
12 changes: 7 additions & 5 deletions package.json
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -34,9 +36,9 @@
"duplexer": "^0.1.2"
},
"devDependencies": {
"ava": "^2.4.0",
"p-event": "^4.2.0",
"tsd": "^0.13.1",
"xo": "^0.34.2"
"ava": "^3.15.0",
"p-event": "^5.0.1",
"tsd": "^0.19.0",
"xo": "^0.46.4"
}
}
22 changes: 11 additions & 11 deletions readme.md
Expand Up @@ -4,21 +4,21 @@
## Install

```
$ npm install gzip-size
```sh
npm install gzip-size
```

## Usage

```js
const gzipSize = require('gzip-size');
import {gzipSize, gzipSizeSync} from '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));
console.log(gzipSizeSync(text));
//=> 78
```

Expand All @@ -28,7 +28,7 @@ console.log(gzipSize.sync(text));

Returns a `Promise<number>` with the size.

### gzipSize.sync(input, options?)
### gzipSizeSync(input, options?)

Returns the size.

Expand All @@ -42,22 +42,22 @@ 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?)
### gzipSizeFromFile(path, options?)

Returns a `Promise<number>` with the size of the file.

#### path

Type: `string`

### gzipSize.fileSync(path, options?)
### gzipSizeFromFileSync(path, options?)

Returns the size of the file.

### gzipSizeStream(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.

## Related

- [gzip-size-cli](https://github.com/sindresorhus/gzip-size-cli) - CLI for this module
Expand Down

0 comments on commit 6b32b3f

Please sign in to comment.