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 Sep 17, 2021
1 parent 7017e44 commit 7dd2e29
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 45 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 16
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
10 changes: 4 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {AsyncReturnType} from 'type-fest';

// TODO: Move these to https://github.com/sindresorhus/type-fest
type AsyncFunction = (...args: any[]) => Promise<unknown>;
type AnyAsyncFunction = (...args: any[]) => Promise<unknown | void>;
type ReplaceReturnType<T extends (...arguments_: any) => unknown, NewReturnType> = (...arguments_: Parameters<T>) => NewReturnType;

/**
Expand All @@ -13,10 +13,10 @@ It uses the V8 serialization API to transfer arguments, return values, errors be
@example
```
import makeSynchronous = require('make-synchronous');
import makeSynchronous from 'make-synchronous';
const fn = makeSynchronous(async number => {
const delay = require('delay');
const {default: delay} = await import('delay');
await delay(100);
Expand All @@ -27,6 +27,4 @@ console.log(fn(2));
//=> 4
```
*/
declare function makeSynchronous<T extends AsyncFunction>(asyncFunction: T): ReplaceReturnType<T, AsyncReturnType<T>>;

export = makeSynchronous;
export default function makeSynchronous<T extends AnyAsyncFunction>(asyncFunction: T): ReplaceReturnType<T, AsyncReturnType<T>>;
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
'use strict';
const childProcess = require('child_process');
const v8 = require('v8');
const Subsume = require('subsume');
import {Buffer} from 'node:buffer';
import childProcess from 'node:child_process';
import v8 from 'node:v8';
import process from 'node:process';
import Subsume from 'subsume';

const HUNDRED_MEGABYTES = 1000 * 1000 * 100;

module.exports = function_ => {
export default function makeSynchronous(function_) {
return (...arguments_) => {
const serializedArguments = v8.serialize(arguments_).toString('hex');
const subsume = new Subsume();

// TODO: Use top-level await here when targeting Node.js 14.
const input = `
const v8 = require('v8');
const Subsume = require('subsume');
import v8 from 'node:v8';
import Subsume from 'subsume';
const subsume = new Subsume('${subsume.id}');
Expand All @@ -32,14 +34,14 @@ module.exports = function_ => {
})();
`;

const {error: subprocessError, stdout, stderr} = childProcess.spawnSync(process.execPath, ['-'], {
const {error: subprocessError, stdout, stderr} = childProcess.spawnSync(process.execPath, ['--input-type=module', '-'], {
input,
encoding: 'utf8',
maxBuffer: HUNDRED_MEGABYTES,
env: {
...process.env,
ELECTRON_RUN_AS_NODE: '1'
}
ELECTRON_RUN_AS_NODE: '1',
},
});

if (subprocessError) {
Expand All @@ -63,4 +65,4 @@ module.exports = function_ => {

return result;
};
};
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import makeSynchronous = require('.');
import makeSynchronous from './index.js';

const fn = makeSynchronous(async (number: number) => number * 2);

Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -33,15 +35,15 @@
"async"
],
"dependencies": {
"subsume": "^3.0.0",
"type-fest": "^0.16.0"
"subsume": "^4.0.0",
"type-fest": "^2.3.3"
},
"devDependencies": {
"ava": "^2.4.0",
"delay": "^4.4.0",
"in-range": "^2.0.0",
"time-span": "^4.0.0",
"tsd": "^0.13.1",
"xo": "^0.33.0"
"ava": "^3.15.0",
"delay": "^5.0.0",
"in-range": "^3.0.0",
"time-span": "^5.0.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ This package works in Node.js only, not the browser.

## Install

```
$ npm install make-synchronous
```sh
npm install make-synchronous
```

## Usage

```js
const makeSynchronous = require('make-synchronous');
import makeSynchronous from 'make-synchronous';

const fn = makeSynchronous(async number => {
const delay = require('delay');
const {default: delay} = await import('delay');

await delay(100);

Expand All @@ -37,7 +37,7 @@ console.log(fn(2));

Returns a wrapped version of the given async function which executes synchronously. This means no other code will execute (not even async code) until the given async function is done.

The given function is executed in a subprocess, so you cannot use any variables/imports from outside the scope of the function. You can pass in arguments to the function. To import dependencies, use either `require(…)` or `await import(…)` in the function body.
The given function is executed in a subprocess, so you cannot use any variables/imports from outside the scope of the function. You can pass in arguments to the function. To import dependencies, use `await import(…)` in the function body.

It uses the V8 serialization API to transfer arguments, return values, errors between the subprocess and the current process. It supports most values, but not functions and symbols.

Expand Down
15 changes: 8 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {serial as test} from 'ava';
import test from 'ava';
import timeSpan from 'time-span';
import inRange from 'in-range';
import makeSynchronous from '.';
import makeSynchronous from './index.js';

test('main', t => {
test.serial('main', t => {
const fixture = '🦄';
const end = timeSpan();

const result = makeSynchronous(async fixture => {
const delay = require('delay');
// eslint-disable-next-line node/no-unsupported-features/es-syntax
const {default: delay} = await import('delay');

await delay(200);

Expand All @@ -19,7 +20,7 @@ test('main', t => {
t.is(result, fixture);
});

test('error', t => {
test.serial('error', t => {
t.throws(
() => {
makeSynchronous(async () => {
Expand All @@ -28,7 +29,7 @@ test('error', t => {
},
{
instanceOf: TypeError,
message: 'unicorn'
}
message: 'unicorn',
},
);
});

0 comments on commit 7dd2e29

Please sign in to comment.