Skip to content

Commit 1f1b077

Browse files
committedJun 6, 2022
Require Node.js 14 and move to ESM
1 parent 6a2d954 commit 1f1b077

File tree

6 files changed

+87
-96
lines changed

6 files changed

+87
-96
lines changed
 

‎.github/workflows/main.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 18
14+
- 16
1315
- 14
14-
- 12
15-
- 10
1616
steps:
17-
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
17+
- uses: actions/checkout@v3
18+
- uses: actions/setup-node@v3
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: npm install

‎index.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const processFn = (fn, options, proxy, unwrapped) => function (...arguments_) {
1+
const processFunction = (function_, options, proxy, unwrapped) => function (...arguments_) {
42
const P = options.promiseModule;
53

64
return new P((resolve, reject) => {
@@ -30,18 +28,18 @@ const processFn = (fn, options, proxy, unwrapped) => function (...arguments_) {
3028
}
3129

3230
const self = this === proxy ? unwrapped : this;
33-
Reflect.apply(fn, self, arguments_);
31+
Reflect.apply(function_, self, arguments_);
3432
});
3533
};
3634

3735
const filterCache = new WeakMap();
3836

39-
module.exports = (input, options) => {
37+
export default function pify(input, options) {
4038
options = {
4139
exclude: [/.+(?:Sync|Stream)$/],
4240
errorFirst: true,
4341
promiseModule: Promise,
44-
...options
42+
...options,
4543
};
4644

4745
const objectType = typeof input;
@@ -62,9 +60,9 @@ module.exports = (input, options) => {
6260
}
6361

6462
const match = pattern => (typeof pattern === 'string' || typeof key === 'symbol') ? key === pattern : pattern.test(key);
65-
const desc = Reflect.getOwnPropertyDescriptor(target, key);
66-
const writableOrConfigurableOwn = (desc === undefined || desc.writable || desc.configurable);
67-
const included = options.include ? options.include.some(match) : !options.exclude.some(match);
63+
const descriptor = Reflect.getOwnPropertyDescriptor(target, key);
64+
const writableOrConfigurableOwn = (descriptor === undefined || descriptor.writable || descriptor.configurable);
65+
const included = options.include ? options.include.some(element => match(element)) : !options.exclude.some(element => match(element));
6866
const shouldFilter = included && writableOrConfigurableOwn;
6967
cached[key] = shouldFilter;
7068
return shouldFilter;
@@ -80,7 +78,7 @@ module.exports = (input, options) => {
8078
return Reflect.apply(cached, thisArg, args);
8179
}
8280

83-
const pified = options.excludeMain ? target : processFn(target, options, proxy, target);
81+
const pified = options.excludeMain ? target : processFunction(target, options, proxy, target);
8482
cache.set(target, pified);
8583
return Reflect.apply(pified, thisArg, args);
8684
},
@@ -100,14 +98,14 @@ module.exports = (input, options) => {
10098
}
10199

102100
if (typeof property === 'function') {
103-
const pified = processFn(property, options, proxy, target);
101+
const pified = processFunction(property, options, proxy, target);
104102
cache.set(property, pified);
105103
return pified;
106104
}
107105

108106
return property;
109-
}
107+
},
110108
});
111109

112110
return proxy;
113-
};
111+
}

‎optimization-test.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable no-fallthrough */
2-
'use strict';
3-
const assert = require('assert');
4-
const v8 = require('v8-natives');
5-
const pify = require('.');
2+
import process from 'node:process';
3+
import assert from 'node:assert';
4+
import v8 from 'v8-natives';
5+
import pify from './index.js';
66

77
function assertOptimized(fn, name) {
88
const status = v8.getOptimizationStatus(fn);
@@ -27,19 +27,17 @@ function assertOptimized(fn, name) {
2727
}
2828

2929
const fn = pify({
30-
unicorn: callback => {
30+
unicorn(callback) {
3131
callback(null, 'unicorn');
32-
}
32+
},
3333
});
3434

35-
(async () => {
36-
try {
37-
await fn.unicorn();
38-
v8.optimizeFunctionOnNextCall(fn.unicorn);
39-
await fn.unicorn();
40-
assertOptimized(fn.unicorn, 'unicorn');
41-
} catch (error) {
42-
console.error(error);
43-
process.exit(1); // eslint-disable-line unicorn/no-process-exit
44-
}
45-
})();
35+
try {
36+
await fn.unicorn();
37+
v8.optimizeFunctionOnNextCall(fn.unicorn);
38+
await fn.unicorn();
39+
assertOptimized(fn.unicorn, 'unicorn');
40+
} catch (error) {
41+
console.error(error);
42+
process.exit(1); // eslint-disable-line unicorn/no-process-exit
43+
}

‎package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=10"
16+
"node": ">=14.16"
1517
},
1618
"scripts": {
1719
"test": "xo && ava",
@@ -41,9 +43,9 @@
4143
"bluebird"
4244
],
4345
"devDependencies": {
44-
"ava": "^2.4.0",
45-
"pinkie-promise": "^2.0.0",
46-
"v8-natives": "^1.1.0",
47-
"xo": "^0.26.1"
46+
"ava": "^4.3.0",
47+
"pinkie-promise": "^2.0.1",
48+
"v8-natives": "^1.2.5",
49+
"xo": "^0.49.0"
4850
}
4951
}

‎readme.md

+25-31
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@
44
55
## Install
66

7-
```
8-
$ npm install pify
7+
```sh
8+
npm install pify
99
```
1010

1111
## Usage
1212

1313
```js
14-
const fs = require('fs');
15-
const pify = require('pify');
16-
17-
(async () => {
18-
// Promisify a single function.
19-
const data = await pify(fs.readFile)('package.json', 'utf8');
20-
console.log(JSON.parse(data).name);
21-
//=> 'pify'
22-
23-
// Promisify all methods in a module.
24-
const data2 = await pify(fs).readFile('package.json', 'utf8');
25-
console.log(JSON.parse(data2).name);
26-
//=> 'pify'
27-
})();
14+
import fs from 'fs';
15+
import pify from 'pify';
16+
17+
// Promisify a single function.
18+
const data = await pify(fs.readFile)('package.json', 'utf8');
19+
console.log(JSON.parse(data).name);
20+
//=> 'pify'
21+
22+
// Promisify all methods in a module.
23+
const data2 = await pify(fs).readFile('package.json', 'utf8');
24+
console.log(JSON.parse(data2).name);
25+
//=> 'pify'
2826
```
2927

3028
## API
@@ -51,14 +49,12 @@ Default: `false`
5149
By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like `request` that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument. This also applies to rejections, where it returns an array of all the callback arguments, including the error.
5250

5351
```js
54-
const request = require('request');
55-
const pify = require('pify');
52+
import request from 'request';
53+
import pify from 'pify';
5654

5755
const pRequest = pify(request, {multiArgs: true});
5856

59-
(async () => {
60-
const [httpResponse, body] = await pRequest('https://sindresorhus.com');
61-
})();
57+
const [httpResponse, body] = await pRequest('https://sindresorhus.com');
6258
```
6359

6460
##### include
@@ -82,7 +78,7 @@ Default: `false`
8278
If the given module is a function itself, it will be promisified. Enable this option if you want to promisify only methods of the module.
8379

8480
```js
85-
const pify = require('pify');
81+
import pify from 'pify';
8682

8783
function fn() {
8884
return true;
@@ -94,14 +90,12 @@ fn.method = (data, callback) => {
9490
});
9591
};
9692

97-
(async () => {
98-
// Promisify methods but not `fn()`.
99-
const promiseFn = pify(fn, {excludeMain: true});
93+
// Promisify methods but not `fn()`.
94+
const promiseFn = pify(fn, {excludeMain: true});
10095

101-
if (promiseFn()) {
102-
console.log(await promiseFn.method('hi'));
103-
}
104-
})();
96+
if (promiseFn()) {
97+
console.log(await promiseFn.method('hi'));
98+
}
10599
```
106100

107101
##### errorFirst
@@ -132,8 +126,8 @@ Custom promise module to use instead of the native one.
132126
Class methods are not bound, so when they're not called on the class itself, they don't have any context. You can either promisify the whole class or use `.bind()`.
133127

134128
```js
135-
const pify = require('pify');
136-
const SomeClass = require('./some-class');
129+
import pify from 'pify';
130+
import SomeClass from './some-class.js';
137131

138132
const someInstance = new SomeClass();
139133

‎test.js

+25-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
/* eslint-disable promise/prefer-await-to-then */
2-
import util from 'util';
3-
import fs from 'fs';
4-
import stream from 'stream';
1+
import util from 'node:util';
2+
import fs from 'node:fs';
3+
import stream from 'node:stream';
54
import test from 'ava';
65
import pinkiePromise from 'pinkie-promise';
7-
import pify from '.';
6+
import pify from './index.js';
87

98
const fixture = callback => setImmediate(() => {
109
callback(null, 'unicorn');
@@ -38,7 +37,7 @@ const fixture5 = () => 'rainbow';
3837
const fixtureModule = {
3938
method1: fixture,
4039
method2: fixture,
41-
method3: fixture5
40+
method3: fixture5,
4241
};
4342

4443
function FixtureGrandparent() {}
@@ -120,16 +119,16 @@ test('module support - doesn\'t transform *Stream methods by default', t => {
120119

121120
test('module support - preserves non-function members', t => {
122121
const module = {
123-
method: () => {},
124-
nonMethod: 3
122+
method() {},
123+
nonMethod: 3,
125124
};
126125

127126
t.deepEqual(Object.keys(module), Object.keys(pify(module)));
128127
});
129128

130129
test('module support - transforms only members in options.include', t => {
131130
const pModule = pify(fixtureModule, {
132-
include: ['method1', 'method2']
131+
include: ['method1', 'method2'],
133132
});
134133

135134
t.is(typeof pModule.method1().then, 'function');
@@ -139,7 +138,7 @@ test('module support - transforms only members in options.include', t => {
139138

140139
test('module support - doesn\'t transform members in options.exclude', t => {
141140
const pModule = pify(fixtureModule, {
142-
exclude: ['method3']
141+
exclude: ['method3'],
143142
});
144143

145144
t.is(typeof pModule.method1().then, 'function');
@@ -150,7 +149,7 @@ test('module support - doesn\'t transform members in options.exclude', t => {
150149
test('module support - options.include over options.exclude', t => {
151150
const pModule = pify(fixtureModule, {
152151
include: ['method1', 'method2'],
153-
exclude: ['method2', 'method3']
152+
exclude: ['method2', 'method3'],
154153
});
155154

156155
t.is(typeof pModule.method1().then, 'function');
@@ -167,7 +166,7 @@ test('module support — function modules', t => {
167166

168167
test('module support — function modules exclusion', t => {
169168
const pModule = pify(fixture4, {
170-
excludeMain: true
169+
excludeMain: true,
171170
});
172171

173172
t.is(typeof pModule.meow().then, 'function');
@@ -189,22 +188,22 @@ test('`errorFirst` option and `multiArgs`', async t => {
189188

190189
t.deepEqual(await pify(fixture, {
191190
errorFirst: false,
192-
multiArgs: true
191+
multiArgs: true,
193192
})('🦄', '🌈'), ['🦄', '🌈']);
194193
});
195194

196195
test('class support - does not create a copy', async t => {
197-
const obj = {
196+
const object = {
198197
x: 'foo',
199198
y(callback) {
200199
setImmediate(() => {
201200
callback(null, this.x);
202201
});
203-
}
202+
},
204203
};
205204

206-
const pified = pify(obj);
207-
obj.x = 'bar';
205+
const pified = pify(object);
206+
object.x = 'bar';
208207

209208
t.is(await pified.y(), 'bar');
210209
t.is(pified.x, 'bar');
@@ -239,7 +238,7 @@ test('class support — respects inheritance order', async t => {
239238
test('class support - transforms only members in options.include, copies all', t => {
240239
const instance = new FixtureClass();
241240
const pInstance = pify(instance, {
242-
include: ['parentMethod1']
241+
include: ['parentMethod1'],
243242
});
244243

245244
t.is(typeof pInstance.parentMethod1().then, 'function');
@@ -250,7 +249,7 @@ test('class support - transforms only members in options.include, copies all', t
250249
test('class support - doesn\'t transform members in options.exclude', t => {
251250
const instance = new FixtureClass();
252251
const pInstance = pify(instance, {
253-
exclude: ['grandparentMethod1']
252+
exclude: ['grandparentMethod1'],
254253
});
255254

256255
t.not(typeof pInstance.grandparentMethod1(() => {}).then, 'function');
@@ -261,7 +260,7 @@ test('class support - options.include over options.exclude', t => {
261260
const instance = new FixtureClass();
262261
const pInstance = pify(instance, {
263262
include: ['method1', 'parentMethod1'],
264-
exclude: ['parentMethod1', 'grandparentMethod1']
263+
exclude: ['parentMethod1', 'grandparentMethod1'],
265264
});
266265

267266
t.is(typeof pInstance.method1().then, 'function');
@@ -280,7 +279,7 @@ test('method mutation', async t => {
280279
setImmediate(() => {
281280
callback(null, 'original');
282281
});
283-
}
282+
},
284283
};
285284
const pified = pify(object);
286285

@@ -296,9 +295,9 @@ test('symbol keys', async t => {
296295
const symbol = Symbol('symbol');
297296

298297
const object = {
299-
[symbol]: callback => {
298+
[symbol](callback) {
300299
setImmediate(callback);
301-
}
300+
},
302301
};
303302

304303
const pified = pify(object);
@@ -313,11 +312,11 @@ test('symbol keys', async t => {
313312
test('non-writable non-configurable property', t => {
314313
const object = {};
315314
Object.defineProperty(object, 'prop', {
316-
value: callback => {
315+
value(callback) {
317316
setImmediate(callback);
318317
},
319318
writable: false,
320-
configurable: false
319+
configurable: false,
321320
});
322321

323322
const pified = pify(object);
@@ -343,7 +342,7 @@ test('do not break internal callback usage', async t => {
343342
bar(...arguments_) {
344343
const callback = arguments_.pop();
345344
callback(null, 42);
346-
}
345+
},
347346
};
348347
t.is(await pify(object).foo(), 42);
349348
});

0 commit comments

Comments
 (0)
Please sign in to comment.