Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 855d98f

Browse files
committed
fixup! quic: implement sendFD() support
1 parent 92d1d4f commit 855d98f

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

doc/api/quic.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ The `QuicServerSession` or `QuicClientSession`.
11111111
added: REPLACEME
11121112
-->
11131113

1114-
* `fd` {number} A readable file descriptor.
1114+
* `fd` {number|FileHandle} A readable file descriptor.
11151115
* `options` {Object}
11161116
* `offset` {number} The offset position at which to begin reading.
11171117
Default: `-1`.
@@ -1126,8 +1126,8 @@ and the file offset will not be advanced.
11261126
If `length` is set to a non-negative number, it gives the maximum number of
11271127
bytes that are read from the file.
11281128

1129-
The file descriptor is not closed when the stream is closed, so it will need
1130-
to be closed manually once it is no longer needed.
1129+
The file descriptor or `FileHandle` is not closed when the stream is closed,
1130+
so it will need to be closed manually once it is no longer needed.
11311131
Using the same file descriptor concurrently for multiple streams
11321132
is not supported and may result in data loss. Re-using a file descriptor
11331133
after a stream has finished is supported.
@@ -1137,7 +1137,7 @@ after a stream has finished is supported.
11371137
added: REPLACEME
11381138
-->
11391139

1140-
* `path` {string|Buffer}
1140+
* `path` {string|Buffer|URL}
11411141
* `options` {Object}
11421142
* `onError` {Function} Callback function invoked in the case of an
11431143
error before send.

lib/internal/quic/core.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const util = require('util');
2626
const assert = require('internal/assert');
2727
const EventEmitter = require('events');
2828
const fs = require('fs');
29+
const fsPromisesInternal = require('internal/fs/promises');
2930
const { Duplex } = require('stream');
3031
const {
3132
createSecureContext: _createSecureContext
@@ -2292,7 +2293,11 @@ class QuicStream extends Duplex {
22922293
throw new ERR_INVALID_OPT_VALUE('options.offset', offset);
22932294
if (typeof length !== 'number')
22942295
throw new ERR_INVALID_OPT_VALUE('options.length', length);
2295-
validateNumber(fd, 'fd');
2296+
2297+
if (fd instanceof fsPromisesInternal.FileHandle)
2298+
fd = fd.fd;
2299+
else if (typeof fd !== 'number')
2300+
throw new ERR_INVALID_ARG_TYPE('fd', ['number', 'FileHandle'], fd);
22962301

22972302
this[kUpdateTimer]();
22982303
this.ownsFd = ownsFd;

test/parallel/test-quic-send-fd.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const cert = fixtures.readKey('agent1-cert.pem', 'binary');
1313
const ca = fixtures.readKey('ca1-cert.pem', 'binary');
1414

1515
const variants = [];
16-
for (const variant of ['sendFD', 'sendFile']) {
16+
for (const variant of ['sendFD', 'sendFile', 'sendFD+fileHandle']) {
1717
for (const offset of [-1, 0, 100]) {
1818
for (const length of [-1, 100]) {
1919
variants.push({ variant, offset, length });
@@ -46,7 +46,13 @@ for (const { variant, offset, length } of variants) {
4646
if (variant === 'sendFD') {
4747
fd = fs.openSync(__filename, 'r');
4848
stream.sendFD(fd, { offset, length });
49+
} else if (variant === 'sendFD+fileHandle') {
50+
fs.promises.open(__filename, 'r').then(common.mustCall((handle) => {
51+
fd = handle;
52+
stream.sendFD(handle, { offset, length });
53+
}));
4954
} else {
55+
assert.strictEqual(variant, 'sendFile');
5056
stream.sendFile(__filename, { offset, length });
5157
}
5258
}));
@@ -82,7 +88,10 @@ for (const { variant, offset, length } of variants) {
8288
stream.end();
8389
client.close();
8490
server.close();
85-
if (fd !== undefined) fs.closeSync(fd);
91+
if (fd !== undefined) {
92+
if (fd.close) fd.close().then(common.mustCall());
93+
else fs.closeSync(fd);
94+
}
8695
}));
8796
}));
8897

0 commit comments

Comments
 (0)