Skip to content

Commit 2436048

Browse files
committed
Require Node.js 12.20 and move to ESM
1 parent b35646b commit 2436048

File tree

8 files changed

+51
-68
lines changed

8 files changed

+51
-68
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
1415
- 12
15-
- 10
1616
steps:
1717
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
18+
- uses: actions/setup-node@v2
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: npm install

index.d.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
/// <reference types="node"/>
2-
import {IncomingMessage, IncomingHttpHeaders} from 'http';
1+
import {IncomingMessage, IncomingHttpHeaders} from 'node:http';
32

4-
declare namespace decompressResponse {
5-
interface UncompressedIncomingHttpHeaders extends IncomingHttpHeaders {
6-
'content-encoding'?: never;
7-
}
3+
export interface UncompressedIncomingHttpHeaders extends IncomingHttpHeaders {
4+
'content-encoding'?: never;
5+
}
86

9-
interface UncompressedIncomingMessage extends IncomingMessage {
10-
headers: UncompressedIncomingHttpHeaders;
11-
}
7+
export interface UncompressedIncomingMessage extends IncomingMessage {
8+
headers: UncompressedIncomingHttpHeaders;
129
}
1310

1411
/**
@@ -19,14 +16,12 @@ Decompress a HTTP response if needed.
1916
2017
@example
2118
```
22-
import {http} from 'http';
23-
import decompressResponse = require('decompress-response');
19+
import http from 'node:http';
20+
import decompressResponse from 'decompress-response';
2421
2522
http.get('https://sindresorhus.com', response => {
2623
response = decompressResponse(response);
2724
});
2825
```
2926
*/
30-
declare function decompressResponse(response: IncomingMessage): decompressResponse.UncompressedIncomingMessage;
31-
32-
export = decompressResponse;
27+
export default function decompressResponse(response: IncomingMessage): UncompressedIncomingMessage;

index.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
'use strict';
2-
const {Transform, PassThrough} = require('stream');
3-
const zlib = require('zlib');
4-
const mimicResponse = require('mimic-response');
1+
import {Transform as TransformStream, PassThrough as PassThroughStream} from 'node:stream';
2+
import zlib from 'node:zlib';
3+
import mimicResponse from 'mimic-response';
54

6-
module.exports = response => {
5+
export default function decompressResponse(response) {
76
const contentEncoding = (response.headers['content-encoding'] || '').toLowerCase();
87

98
if (!['gzip', 'deflate', 'br'].includes(contentEncoding)) {
@@ -12,16 +11,9 @@ module.exports = response => {
1211

1312
delete response.headers['content-encoding'];
1413

15-
// TODO: Remove this when targeting Node.js 12.
16-
const isBrotli = contentEncoding === 'br';
17-
if (isBrotli && typeof zlib.createBrotliDecompress !== 'function') {
18-
response.destroy(new Error('Brotli is not supported on Node.js < 12'));
19-
return response;
20-
}
21-
2214
let isEmpty = true;
2315

24-
const checker = new Transform({
16+
const checker = new TransformStream({
2517
transform(data, _encoding, callback) {
2618
isEmpty = false;
2719

@@ -30,19 +22,19 @@ module.exports = response => {
3022

3123
flush(callback) {
3224
callback();
33-
}
25+
},
3426
});
3527

36-
const finalStream = new PassThrough({
28+
const finalStream = new PassThroughStream({
3729
autoDestroy: false,
3830
destroy(error, callback) {
3931
response.destroy();
4032

4133
callback(error);
42-
}
34+
},
4335
});
4436

45-
const decompressStream = isBrotli ? zlib.createBrotliDecompress() : zlib.createUnzip();
37+
const decompressStream = contentEncoding === 'br' ? zlib.createBrotliDecompress() : zlib.createUnzip();
4638

4739
decompressStream.once('error', error => {
4840
if (isEmpty && !response.readable) {
@@ -57,4 +49,4 @@ module.exports = response => {
5749
response.pipe(checker).pipe(decompressStream).pipe(finalStream);
5850

5951
return finalStream;
60-
};
52+
}

index.test-d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as http from 'http';
1+
import http from 'node:http';
22
import {expectType} from 'tsd';
3-
import decompressResponse = require('.');
3+
import decompressResponse, {UncompressedIncomingMessage} from './index.js';
44

55
http.get('localhost', response => {
6-
expectType<decompressResponse.UncompressedIncomingMessage>(decompressResponse(response));
6+
expectType<UncompressedIncomingMessage>(decompressResponse(response));
77
});

package.json

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"email": "[email protected]",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=10"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1517
},
1618
"scripts": {
1719
"test": "xo && ava && tsd"
@@ -38,19 +40,15 @@
3840
"brotli"
3941
],
4042
"dependencies": {
41-
"mimic-response": "^3.1.0"
43+
"mimic-response": "^4.0.0"
4244
},
4345
"devDependencies": {
44-
"@types/node": "^14.0.1",
45-
"ava": "^2.2.0",
46-
"get-stream": "^5.0.0",
46+
"@types/node": "^16.11.6",
47+
"ava": "^3.15.0",
48+
"get-stream": "^6.0.1",
4749
"pify": "^5.0.0",
48-
"tsd": "^0.11.0",
49-
"xo": "^0.30.0"
50-
},
51-
"xo": {
52-
"rules": {
53-
"@typescript-eslint/prefer-readonly-parameter-types": "off"
54-
}
50+
"tsd": "^0.18.0",
51+
"typescript": "^4.4.4",
52+
"xo": "^0.45.0"
5553
}
5654
}

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Used by [`got`](https://github.com/sindresorhus/got).
88

99
## Install
1010

11-
```
12-
$ npm install decompress-response
11+
```sh
12+
npm install decompress-response
1313
```
1414

1515
## Usage
1616

1717
```js
18-
const http = require('http');
19-
const decompressResponse = require('decompress-response');
18+
import http from 'node:http';
19+
import decompressResponse from 'decompress-response';
2020

2121
http.get('https://sindresorhus.com', response => {
2222
response = decompressResponse(response);

test/_server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import http from 'http';
1+
import http from 'node:http';
22
import pify from 'pify';
33

44
export const host = 'localhost';

test/test.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import http from 'http';
2-
import zlib from 'zlib';
1+
import http from 'node:http';
2+
import zlib from 'node:zlib';
33
import test from 'ava';
44
import getStream from 'get-stream';
55
import pify from 'pify';
6-
import {createServer} from './_server';
7-
import decompressResponse from '..';
6+
import decompressResponse from '../index.js';
7+
import {createServer} from './_server.js';
88

99
const zlibP = pify(zlib);
1010
const httpGetP = pify(http.get, {errorFirst: false});
@@ -91,18 +91,16 @@ test('decompress deflated content', async t => {
9191
t.is(await getStream(response), fixture);
9292
});
9393

94-
if (typeof zlib.brotliCompress === 'function') {
95-
test('decompress brotli content', async t => {
96-
const response = decompressResponse(await httpGetP(`${server.url}/brotli`));
94+
test('decompress brotli content', async t => {
95+
const response = decompressResponse(await httpGetP(`${server.url}/brotli`));
9796

98-
t.is(typeof response.httpVersion, 'string');
99-
t.truthy(response.headers);
97+
t.is(typeof response.httpVersion, 'string');
98+
t.truthy(response.headers);
10099

101-
response.setEncoding('utf8');
100+
response.setEncoding('utf8');
102101

103-
t.is(await getStream(response), fixture);
104-
});
105-
}
102+
t.is(await getStream(response), fixture);
103+
});
106104

107105
test('does not ignore missing data', async t => {
108106
const response = decompressResponse(await httpGetP(`${server.url}/missing-data`));
@@ -114,7 +112,7 @@ test('does not ignore missing data', async t => {
114112

115113
await t.throwsAsync(getStream(response), {
116114
message: 'unexpected end of file',
117-
code: 'Z_BUF_ERROR'
115+
code: 'Z_BUF_ERROR',
118116
});
119117
});
120118

0 commit comments

Comments
 (0)