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

Commit

Permalink
fixup! quic: implement sendFD() support
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Oct 9, 2019
1 parent 3a68f29 commit 446b1e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
8 changes: 4 additions & 4 deletions doc/api/quic.md
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ The `QuicServerSession` or `QuicClientSession`.
added: REPLACEME
-->

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

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

* `path` {string|Buffer}
* `path` {string|Buffer|URL}
* `options` {Object}
* `onError` {Function} Callback function invoked in the case of an
error before send.
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/quic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const util = require('util');
const assert = require('internal/assert');
const EventEmitter = require('events');
const fs = require('fs');
const fsPromisesInternal = require('internal/fs/promises');
const { Duplex } = require('stream');
const {
createSecureContext: _createSecureContext
Expand Down Expand Up @@ -2292,7 +2293,11 @@ class QuicStream extends Duplex {
throw new ERR_INVALID_OPT_VALUE('options.offset', offset);
if (typeof length !== 'number')
throw new ERR_INVALID_OPT_VALUE('options.length', length);
validateNumber(fd, 'fd');

if (fd instanceof fsPromisesInternal.FileHandle)
fd = fd.fd;
else if (typeof fd !== 'number')
throw new ERR_INVALID_ARG_TYPE('fd', ['number', 'FileHandle'], fd);

this[kUpdateTimer]();
this.ownsFd = ownsFd;
Expand Down
13 changes: 11 additions & 2 deletions test/parallel/test-quic-send-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const cert = fixtures.readKey('agent1-cert.pem', 'binary');
const ca = fixtures.readKey('ca1-cert.pem', 'binary');

const variants = [];
for (const variant of ['sendFD', 'sendFile']) {
for (const variant of ['sendFD', 'sendFile', 'sendFD+fileHandle']) {
for (const offset of [-1, 0, 100]) {
for (const length of [-1, 100]) {
variants.push({ variant, offset, length });
Expand Down Expand Up @@ -46,7 +46,13 @@ for (const { variant, offset, length } of variants) {
if (variant === 'sendFD') {
fd = fs.openSync(__filename, 'r');
stream.sendFD(fd, { offset, length });
} else if (variant === 'sendFD+fileHandle') {
fs.promises.open(__filename, 'r').then(common.mustCall((handle) => {
fd = handle;
stream.sendFD(handle, { offset, length });
}));
} else {
assert.strictEqual(variant, 'sendFile');
stream.sendFile(__filename, { offset, length });
}
}));
Expand Down Expand Up @@ -82,7 +88,10 @@ for (const { variant, offset, length } of variants) {
stream.end();
client.close();
server.close();
if (fd !== undefined) fs.closeSync(fd);
if (fd !== undefined) {
if (fd.close) fd.close().then(common.mustCall());
else fs.closeSync(fd);
}
}));
}));

Expand Down

0 comments on commit 446b1e4

Please sign in to comment.