| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -355,6 +355,17 @@ The optional `callback` will be called once the `'close'` event occurs. Unlike | |||
| 355 | 355 | that event, it will be called with an `Error` as its only argument if the server | |
| 356 | 356 | was not open when it was closed. | |
| 357 | 357 | ||
| 358 | + ### `server[Symbol.asyncDispose]()` | ||
| 359 | + | ||
| 360 | + <!-- YAML | ||
| 361 | + added: REPLACEME | ||
| 362 | + --> | ||
| 363 | + | ||
| 364 | + > Stability: 1 - Experimental | ||
| 365 | + | ||
| 366 | + Calls [`server.close()`][] and returns a promise that fulfills when the | ||
| 367 | + server has closed. | ||
| 368 | + | ||
| 358 | 369 | ### `server.getConnections(callback)` | |
| 359 | 370 | ||
| 360 | 371 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,7 @@ const { | |||
| 27 | 27 | ArrayPrototypePush, | |
| 28 | 28 | Boolean, | |
| 29 | 29 | FunctionPrototypeBind, | |
| 30 | + FunctionPrototypeCall, | ||
| 30 | 31 | MathMax, | |
| 31 | 32 | Number, | |
| 32 | 33 | NumberIsNaN, | |
@@ -35,6 +36,7 @@ const { | |||
| 35 | 36 | ObjectSetPrototypeOf, | |
| 36 | 37 | Symbol, | |
| 37 | 38 | ObjectCreate, | |
| 39 | + SymbolAsyncDispose, | ||
| 38 | 40 | } = primordials; | |
| 39 | 41 | ||
| 40 | 42 | const EventEmitter = require('events'); | |
@@ -110,7 +112,7 @@ const { | |||
| 110 | 112 | } = require('internal/errors'); | |
| 111 | 113 | const { isUint8Array } = require('internal/util/types'); | |
| 112 | 114 | const { queueMicrotask } = require('internal/process/task_queues'); | |
| 113 | - const { kEmptyObject } = require('internal/util'); | ||
| 115 | + const { kEmptyObject, promisify } = require('internal/util'); | ||
| 114 | 116 | const { | |
| 115 | 117 | validateAbortSignal, | |
| 116 | 118 | validateBoolean, | |
@@ -2191,6 +2193,13 @@ Server.prototype.close = function(cb) { | |||
| 2191 | 2193 | return this; | |
| 2192 | 2194 | }; | |
| 2193 | 2195 | ||
| 2196 | + Server.prototype[SymbolAsyncDispose] = async function() { | ||
| 2197 | + if (!this._handle) { | ||
| 2198 | + return; | ||
| 2199 | + } | ||
| 2200 | + return FunctionPrototypeCall(promisify(this.close), this); | ||
| 2201 | + }; | ||
| 2202 | + | ||
| 2194 | 2203 | Server.prototype._emitCloseIfDrained = function() { | |
| 2195 | 2204 | debug('SERVER _emitCloseIfDrained'); | |
| 2196 | 2205 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 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 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments