-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
net: add experimental net/promises API #63965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ethan-Arrowood
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
Ethan-Arrowood:net-promise
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| 'use strict'; | ||
|
|
||
| const { once } = require('events'); | ||
| const { | ||
| validateAbortSignal, | ||
| validateObject, | ||
| } = require('internal/validators'); | ||
| const { kEmptyObject } = require('internal/util'); | ||
|
|
||
| // Lazily loaded to avoid a require cycle with the `net` module, which exposes | ||
| // this namespace through its `promises` getter. | ||
| let net; | ||
| function lazyNet() { | ||
| net ??= require('net'); | ||
| return net; | ||
| } | ||
|
|
||
| // Resolves with a connected `net.Socket` once the `'connect'` event fires, and | ||
| // rejects if the connection fails or the optional `signal` is aborted. | ||
| async function connect(...args) { | ||
| const lazy = lazyNet(); | ||
| const options = lazy._normalizeArgs(args)[0]; | ||
| const { signal } = options; | ||
| if (signal !== undefined) { | ||
| validateAbortSignal(signal, 'options.signal'); | ||
| signal.throwIfAborted(); | ||
| } | ||
|
|
||
| // Strip the signal so the socket does not also install its own abort | ||
| // handling; rejecting and destroying below fully tears the socket down. | ||
| const socket = lazy.connect({ ...options, signal: undefined }); | ||
|
|
||
| try { | ||
| await once(socket, 'connect', signal !== undefined ? { signal } : kEmptyObject); | ||
| } catch (err) { | ||
| socket.destroy(); | ||
| throw err; | ||
| } | ||
| return socket; | ||
| } | ||
|
|
||
| // Creates a server and resolves with it once it is listening, rejecting if it | ||
| // fails to bind or the optional `signal` is aborted. | ||
| async function listen(options = kEmptyObject) { | ||
| validateObject(options, 'options'); | ||
| const { signal, connectionListener } = options; | ||
| if (signal !== undefined) { | ||
| validateAbortSignal(signal, 'options.signal'); | ||
| signal.throwIfAborted(); | ||
| } | ||
|
|
||
| const lazy = lazyNet(); | ||
| const server = lazy.createServer(options, connectionListener); | ||
|
|
||
| try { | ||
| // Pass `signal` through to listen() so net installs its own | ||
| // close-on-abort handler: the signal aborts the server for its entire | ||
| // lifetime, not just the pending listen. | ||
| server.listen(options); | ||
| await once(server, 'listening', signal !== undefined ? { signal } : kEmptyObject); | ||
| } catch (err) { | ||
| // On abort, net's signal handler already closes the server, so closing | ||
| // again would be redundant; on other failures (e.g. a bind error) there | ||
| // is no such handler, so close it here. | ||
| if (!signal?.aborted) { | ||
| server.close(); | ||
| } | ||
| throw err; | ||
| } | ||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
| connect, | ||
| listen, | ||
| get isIP() { return lazyNet().isIP; }, | ||
| get isIPv4() { return lazyNet().isIPv4; }, | ||
| get isIPv6() { return lazyNet().isIPv6; }, | ||
| get BlockList() { return lazyNet().BlockList; }, | ||
| get SocketAddress() { return lazyNet().SocketAddress; }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = require('internal/net/promises'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const net = require('net'); | ||
| const { once } = require('events'); | ||
| const { connect } = require('net/promises'); | ||
|
|
||
| (async () => { | ||
| // Resolves with a connected socket and round-trips data. | ||
| { | ||
| const server = net.createServer((socket) => { | ||
| socket.end('hello'); | ||
| }).listen(0); | ||
| await once(server, 'listening'); | ||
| const socket = await connect({ port: server.address().port }); | ||
| assert.strictEqual(socket.connecting, false); | ||
| const chunks = []; | ||
| for await (const chunk of socket) { | ||
| chunks.push(chunk); | ||
| } | ||
| assert.strictEqual(Buffer.concat(chunks).toString(), 'hello'); | ||
| server.close(); | ||
| } | ||
|
|
||
| // net.promises is the same object as require('net/promises'). | ||
| assert.strictEqual(net.promises, require('net/promises')); | ||
|
|
||
| // Rejects when the connection is refused. | ||
| { | ||
| const server = net.createServer().listen(0); | ||
| await once(server, 'listening'); | ||
| const { port } = server.address(); | ||
| server.close(); | ||
| await once(server, 'close'); | ||
| await assert.rejects(connect({ port }), { code: 'ECONNREFUSED' }); | ||
| } | ||
|
|
||
| // A pre-aborted signal rejects with an AbortError. | ||
| { | ||
| await assert.rejects( | ||
| connect({ port: 0, signal: AbortSignal.abort() }), | ||
| { name: 'AbortError' }); | ||
| } | ||
|
|
||
| // Aborting while connecting rejects with an AbortError. | ||
| { | ||
| const server = net.createServer().listen(0); | ||
| await once(server, 'listening'); | ||
| const controller = new AbortController(); | ||
| const promise = connect({ port: server.address().port, signal: controller.signal }); | ||
| controller.abort(); | ||
| await assert.rejects(promise, { name: 'AbortError' }); | ||
| server.close(); | ||
| } | ||
|
|
||
| // An invalid signal throws. | ||
| { | ||
| await assert.rejects( | ||
| connect({ port: 0, signal: 'INVALID_SIGNAL' }), | ||
| { code: 'ERR_INVALID_ARG_TYPE' }); | ||
| } | ||
| })().then(common.mustCall()); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the
socketerrors before theconnectevent, would this end up hanging?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I believe this correctly rejects. The
ECONNREFUSEDtest intest-net-promises-connect.jsshould be covering this (https://github.com/nodejs/node/pull/63965/changes#diff-20589a91d7b722a33615840a6188115a9a297c2942b6d49ce1615e6dd8dfec71R35)