Commit c47b2cb 1 parent 1295c76 commit c47b2cb Copy full SHA for c47b2cb
File tree 3 files changed +41
-1
lines changed
3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -372,6 +372,17 @@ added: v0.1.99
372
372
Close the underlying socket and stop listening for data on it. If a callback is
373
373
provided, it is added as a listener for the [ ` 'close' ` ] [ ] event.
374
374
375
+ ### ` socket[Symbol.asyncDispose]() `
376
+
377
+ <!-- YAML
378
+ added: REPLACEME
379
+ -->
380
+
381
+ > Stability: 1 - Experimental
382
+
383
+ Calls [ ` socket.close() ` ] [ ] and returns a promise that fulfills when the
384
+ socket has closed.
385
+
375
386
### ` socket.connect(port[, address][, callback]) `
376
387
377
388
<!-- YAML
@@ -992,4 +1003,5 @@ and `udp6` sockets). The bound address and port can be retrieved using
992
1003
[ `socket.address().address` ] : #socketaddress
993
1004
[ `socket.address().port` ] : #socketaddress
994
1005
[ `socket.bind()` ] : #socketbindport-address-callback
1006
+ [ `socket.close()` ] : #socketclosecallback
995
1007
[ byte length ] : buffer.md#static-method-bufferbytelengthstring-encoding
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ const {
30
30
ObjectDefineProperty,
31
31
ObjectSetPrototypeOf,
32
32
ReflectApply,
33
+ SymbolAsyncDispose,
33
34
SymbolDispose,
34
35
} = primordials ;
35
36
@@ -59,7 +60,7 @@ const {
59
60
validatePort,
60
61
} = require ( 'internal/validators' ) ;
61
62
const { Buffer } = require ( 'buffer' ) ;
62
- const { deprecate, guessHandleType } = require ( 'internal/util' ) ;
63
+ const { deprecate, guessHandleType, promisify } = require ( 'internal/util' ) ;
63
64
const { isArrayBufferView } = require ( 'internal/util/types' ) ;
64
65
const EventEmitter = require ( 'events' ) ;
65
66
const {
@@ -752,6 +753,13 @@ Socket.prototype.close = function(callback) {
752
753
return this ;
753
754
} ;
754
755
756
+ Socket . prototype [ SymbolAsyncDispose ] = async function ( ) {
757
+ if ( ! this [ kStateSymbol ] . handle ) {
758
+ return ;
759
+ }
760
+ return FunctionPrototypeCall ( promisify ( this . close ) , this ) ;
761
+ } ;
762
+
755
763
756
764
function socketCloseNT ( self ) {
757
765
self . emit ( 'close' ) ;
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 dgram from 'node:dgram' ;
4
+ import { describe , it } from 'node:test' ;
5
+
6
+ describe ( 'dgram.Socket[Symbol.asyncDispose]()' , ( ) => {
7
+ it ( 'should close the socket' , async ( ) => {
8
+ const server = dgram . createSocket ( { type : 'udp4' } ) ;
9
+ server . on ( 'close' , common . mustCall ( ) ) ;
10
+ await server [ Symbol . asyncDispose ] ( ) . then ( common . mustCall ( ) ) ;
11
+
12
+ assert . throws ( ( ) => server . address ( ) , { code : 'ERR_SOCKET_DGRAM_NOT_RUNNING' } ) ;
13
+ } ) ;
14
+
15
+ it ( 'should resolve even if the socket is already closed' , async ( ) => {
16
+ const server = dgram . createSocket ( { type : 'udp4' } ) ;
17
+ await server [ Symbol . asyncDispose ] ( ) . then ( common . mustCall ( ) ) ;
18
+ await server [ Symbol . asyncDispose ] ( ) . then ( common . mustCall ( ) , common . mustNotCall ( ) ) ;
19
+ } ) ;
20
+ } ) ;
You can’t perform that action at this time.
0 commit comments