mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
net: server add asyncDispose
PR-URL: https://github.com/nodejs/node/pull/48717 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
parent
17f6b8c49c
commit
0e9138d173
3 changed files with 51 additions and 1 deletions
|
@ -357,6 +357,17 @@ The optional `callback` will be called once the `'close'` event occurs. Unlike
|
||||||
that event, it will be called with an `Error` as its only argument if the server
|
that event, it will be called with an `Error` as its only argument if the server
|
||||||
was not open when it was closed.
|
was not open when it was closed.
|
||||||
|
|
||||||
|
### `server[Symbol.asyncDispose]()`
|
||||||
|
|
||||||
|
<!-- YAML
|
||||||
|
added: REPLACEME
|
||||||
|
-->
|
||||||
|
|
||||||
|
> Stability: 1 - Experimental
|
||||||
|
|
||||||
|
Calls [`server.close()`][] and returns a promise that fulfills when the
|
||||||
|
server has closed.
|
||||||
|
|
||||||
### `server.getConnections(callback)`
|
### `server.getConnections(callback)`
|
||||||
|
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
|
|
11
lib/net.js
11
lib/net.js
|
@ -28,6 +28,7 @@ const {
|
||||||
ArrayPrototypePush,
|
ArrayPrototypePush,
|
||||||
Boolean,
|
Boolean,
|
||||||
FunctionPrototypeBind,
|
FunctionPrototypeBind,
|
||||||
|
FunctionPrototypeCall,
|
||||||
MathMax,
|
MathMax,
|
||||||
Number,
|
Number,
|
||||||
NumberIsNaN,
|
NumberIsNaN,
|
||||||
|
@ -35,6 +36,7 @@ const {
|
||||||
ObjectDefineProperty,
|
ObjectDefineProperty,
|
||||||
ObjectSetPrototypeOf,
|
ObjectSetPrototypeOf,
|
||||||
Symbol,
|
Symbol,
|
||||||
|
SymbolAsyncDispose,
|
||||||
SymbolDispose,
|
SymbolDispose,
|
||||||
} = primordials;
|
} = primordials;
|
||||||
|
|
||||||
|
@ -112,7 +114,7 @@ const {
|
||||||
} = require('internal/errors');
|
} = require('internal/errors');
|
||||||
const { isUint8Array } = require('internal/util/types');
|
const { isUint8Array } = require('internal/util/types');
|
||||||
const { queueMicrotask } = require('internal/process/task_queues');
|
const { queueMicrotask } = require('internal/process/task_queues');
|
||||||
const { kEmptyObject, guessHandleType } = require('internal/util');
|
const { kEmptyObject, guessHandleType, promisify } = require('internal/util');
|
||||||
const {
|
const {
|
||||||
validateAbortSignal,
|
validateAbortSignal,
|
||||||
validateBoolean,
|
validateBoolean,
|
||||||
|
@ -2243,6 +2245,13 @@ Server.prototype.close = function(cb) {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Server.prototype[SymbolAsyncDispose] = async function() {
|
||||||
|
if (!this._handle) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return FunctionPrototypeCall(promisify(this.close), this);
|
||||||
|
};
|
||||||
|
|
||||||
Server.prototype._emitCloseIfDrained = function() {
|
Server.prototype._emitCloseIfDrained = function() {
|
||||||
debug('SERVER _emitCloseIfDrained');
|
debug('SERVER _emitCloseIfDrained');
|
||||||
|
|
||||||
|
|
30
test/parallel/test-net-server-async-dispose.mjs
Normal file
30
test/parallel/test-net-server-async-dispose.mjs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import * as common from '../common/index.mjs';
|
||||||
|
import assert from 'node:assert';
|
||||||
|
import net from 'node:net';
|
||||||
|
import { describe, it } from 'node:test';
|
||||||
|
|
||||||
|
describe('net.Server[Symbol.asyncDispose]()', () => {
|
||||||
|
it('should close the server', async () => {
|
||||||
|
const server = net.createServer();
|
||||||
|
const timeoutRef = setTimeout(common.mustNotCall(), 2 ** 31 - 1);
|
||||||
|
|
||||||
|
server.listen(0, common.mustCall(async () => {
|
||||||
|
await server[Symbol.asyncDispose]().then(common.mustCall());
|
||||||
|
assert.strictEqual(server.address(), null);
|
||||||
|
clearTimeout(timeoutRef);
|
||||||
|
}));
|
||||||
|
|
||||||
|
server.on('close', common.mustCall());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should resolve even if the server is already closed', async () => {
|
||||||
|
const server = net.createServer();
|
||||||
|
const timeoutRef = setTimeout(common.mustNotCall(), 2 ** 31 - 1);
|
||||||
|
|
||||||
|
server.listen(0, common.mustCall(async () => {
|
||||||
|
await server[Symbol.asyncDispose]().then(common.mustCall());
|
||||||
|
await server[Symbol.asyncDispose]().then(common.mustCall(), common.mustNotCall());
|
||||||
|
clearTimeout(timeoutRef);
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue