Skip to content

Commit 573eb4b

Browse files
atlowChemiruyadorno
authored andcommitted
dgram: socket add asyncDispose
PR-URL: #48717 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent f74c2fc commit 573eb4b

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎doc/api/dgram.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,17 @@ added: v0.1.99
372372
Close the underlying socket and stop listening for data on it. If a callback is
373373
provided, it is added as a listener for the [`'close'`][] event.
374374

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+
375386
### `socket.connect(port[, address][, callback])`
376387

377388
<!-- YAML
@@ -988,4 +999,5 @@ and `udp6` sockets). The bound address and port can be retrieved using
988999
[`socket.address().address`]: #socketaddress
9891000
[`socket.address().port`]: #socketaddress
9901001
[`socket.bind()`]: #socketbindport-address-callback
1002+
[`socket.close()`]: #socketclosecallback
9911003
[byte length]: buffer.md#static-method-bufferbytelengthstring-encoding

‎lib/dgram.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
ObjectDefineProperty,
3131
ObjectSetPrototypeOf,
3232
ReflectApply,
33+
SymbolAsyncDispose,
3334
SymbolDispose,
3435
}=primordials;
3536

@@ -60,7 +61,7 @@ const {
6061
validatePort,
6162
}=require('internal/validators');
6263
const{ Buffer }=require('buffer');
63-
const{ deprecate }=require('internal/util');
64+
const{ deprecate, promisify}=require('internal/util');
6465
const{ isArrayBufferView }=require('internal/util/types');
6566
constEventEmitter=require('events');
6667
const{
@@ -753,6 +754,13 @@ Socket.prototype.close = function(callback) {
753754
returnthis;
754755
};
755756

757+
Socket.prototype[SymbolAsyncDispose]=asyncfunction(){
758+
if(!this[kStateSymbol].handle){
759+
return;
760+
}
761+
returnFunctionPrototypeCall(promisify(this.close),this);
762+
};
763+
756764

757765
functionsocketCloseNT(self){
758766
self.emit('close');
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import*ascommonfrom'../common/index.mjs';
2+
importassertfrom'node:assert';
3+
importdgramfrom'node:dgram';
4+
import{describe,it}from'node:test';
5+
6+
describe('dgram.Socket[Symbol.asyncDispose]()',()=>{
7+
it('should close the socket',async()=>{
8+
constserver=dgram.createSocket({type: 'udp4'});
9+
server.on('close',common.mustCall());
10+
awaitserver[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+
constserver=dgram.createSocket({type: 'udp4'});
17+
awaitserver[Symbol.asyncDispose]().then(common.mustCall());
18+
awaitserver[Symbol.asyncDispose]().then(common.mustCall(),common.mustNotCall());
19+
});
20+
});

0 commit comments

Comments
 (0)