Commit cfa69bd 1 parent c47b2cb commit cfa69bd Copy full SHA for cfa69bd
File tree 3 files changed +51
-1
lines changed
3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -357,6 +357,17 @@ The optional `callback` will be called once the `'close'` event occurs. Unlike
357
357
that event, it will be called with an ` Error ` as its only argument if the server
358
358
was not open when it was closed.
359
359
360
+ ### ` server[Symbol.asyncDispose]() `
361
+
362
+ <!-- YAML
363
+ added: REPLACEME
364
+ -->
365
+
366
+ > Stability: 1 - Experimental
367
+
368
+ Calls [ ` server.close() ` ] [ ] and returns a promise that fulfills when the
369
+ server has closed.
370
+
360
371
### ` server.getConnections(callback) `
361
372
362
373
<!-- YAML
Original file line number Diff line number Diff line change @@ -28,13 +28,15 @@ const {
28
28
ArrayPrototypePush,
29
29
Boolean,
30
30
FunctionPrototypeBind,
31
+ FunctionPrototypeCall,
31
32
MathMax,
32
33
Number,
33
34
NumberIsNaN,
34
35
NumberParseInt,
35
36
ObjectDefineProperty,
36
37
ObjectSetPrototypeOf,
37
38
Symbol,
39
+ SymbolAsyncDispose,
38
40
SymbolDispose,
39
41
} = primordials ;
40
42
@@ -112,7 +114,7 @@ const {
112
114
} = require ( 'internal/errors' ) ;
113
115
const { isUint8Array } = require ( 'internal/util/types' ) ;
114
116
const { queueMicrotask } = require ( 'internal/process/task_queues' ) ;
115
- const { kEmptyObject, guessHandleType } = require ( 'internal/util' ) ;
117
+ const { kEmptyObject, guessHandleType, promisify } = require ( 'internal/util' ) ;
116
118
const {
117
119
validateAbortSignal,
118
120
validateBoolean,
@@ -2243,6 +2245,13 @@ Server.prototype.close = function(cb) {
2243
2245
return this ;
2244
2246
} ;
2245
2247
2248
+ Server . prototype [ SymbolAsyncDispose ] = async function ( ) {
2249
+ if ( ! this . _handle ) {
2250
+ return ;
2251
+ }
2252
+ return FunctionPrototypeCall ( promisify ( this . close ) , this ) ;
2253
+ } ;
2254
+
2246
2255
Server . prototype . _emitCloseIfDrained = function ( ) {
2247
2256
debug ( 'SERVER _emitCloseIfDrained' ) ;
2248
2257
Original file line number Diff line number Diff line change
1
+ import * as common from '../common/index.mjs' ;
2
+ import assert from 'node:assert' ;
3
+ import net from 'node:net' ;
4
+ import { describe , it } from 'node:test' ;
5
+
6
+ describe ( 'net.Server[Symbol.asyncDispose]()' , ( ) => {
7
+ it ( 'should close the server' , async ( ) => {
8
+ const server = net . createServer ( ) ;
9
+ const timeoutRef = setTimeout ( common . mustNotCall ( ) , 2 ** 31 - 1 ) ;
10
+
11
+ server . listen ( 0 , common . mustCall ( async ( ) => {
12
+ await server [ Symbol . asyncDispose ] ( ) . then ( common . mustCall ( ) ) ;
13
+ assert . strictEqual ( server . address ( ) , null ) ;
14
+ clearTimeout ( timeoutRef ) ;
15
+ } ) ) ;
16
+
17
+ server . on ( 'close' , common . mustCall ( ) ) ;
18
+ } ) ;
19
+
20
+ it ( 'should resolve even if the server is already closed' , async ( ) => {
21
+ const server = net . createServer ( ) ;
22
+ const timeoutRef = setTimeout ( common . mustNotCall ( ) , 2 ** 31 - 1 ) ;
23
+
24
+ server . listen ( 0 , common . mustCall ( async ( ) => {
25
+ await server [ Symbol . asyncDispose ] ( ) . then ( common . mustCall ( ) ) ;
26
+ await server [ Symbol . asyncDispose ] ( ) . then ( common . mustCall ( ) , common . mustNotCall ( ) ) ;
27
+ clearTimeout ( timeoutRef ) ;
28
+ } ) ) ;
29
+ } ) ;
30
+ } ) ;
You can’t perform that action at this time.
0 commit comments