• fd {number|FileHandle} A readable file descriptor.
    • headers {HTTP/2 Headers Object}
    • options {Object}
      • statCheck {Function}
      • waitForTrailers {boolean} When true, the Http2Stream will emit the 'wantTrailers' event after the final DATA frame has been sent.
      • offset {number} The offset position at which to begin reading.
      • length {number} The amount of data from the fd to send.

    Initiates a response whose data is read from the given file descriptor. No validation is performed on the given file descriptor. If an error occurs while attempting to read data using the file descriptor, the Http2Stream will be closed using an RST_STREAM frame using the standard INTERNAL_ERROR code.

    When used, the Http2Stream object’s Duplex interface will be closed automatically.

    1. const http2 = require('http2');
    2. const fs = require('fs');
    3. const server = http2.createServer();
    4. server.on('stream', (stream) => {
    5. const fd = fs.openSync('/some/file', 'r');
    6. const stat = fs.fstatSync(fd);
    7. const headers = {
    8. 'content-length': stat.size,
    9. 'last-modified': stat.mtime.toUTCString(),
    10. 'content-type': 'text/plain; charset=utf-8'
    11. };
    12. stream.respondWithFD(fd, headers);
    13. stream.on('close', () => fs.closeSync(fd));
    14. });

    The optional options.statCheck function may be specified to give user code an opportunity to set additional content headers based on the fs.Stat details of the given fd. If the statCheck function is provided, the http2stream.respondWithFD() method will perform an fs.fstat() call to collect details on the provided file descriptor.

    The offset and length options may be used to limit the response to a specific range subset. This can be used, for instance, to support HTTP Range requests.

    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.

    When the options.waitForTrailers option is set, the 'wantTrailers' event will be emitted immediately after queuing the last chunk of payload data to be sent. The http2stream.sendTrailers() method can then be used to sent trailing header fields to the peer.

    When options.waitForTrailers is set, the Http2Stream will not automatically close when the final DATA frame is transmitted. User code must call either http2stream.sendTrailers() or http2stream.close() to close the Http2Stream.

    1. const http2 = require('http2');
    2. const fs = require('fs');
    3. const server = http2.createServer();
    4. server.on('stream', (stream) => {
    5. const fd = fs.openSync('/some/file', 'r');
    6. const stat = fs.fstatSync(fd);
    7. const headers = {
    8. 'content-length': stat.size,
    9. 'last-modified': stat.mtime.toUTCString(),
    10. 'content-type': 'text/plain; charset=utf-8'
    11. };
    12. stream.respondWithFD(fd, headers, { waitForTrailers: true });
    13. stream.on('wantTrailers', () => {
    14. stream.sendTrailers({ ABC: 'some value to send' });
    15. });
    16. stream.on('close', () => fs.closeSync(fd));
    17. });