http2stream.respondWithFD(fd[, headers[, options]])
fd
<number> | <FileHandle> 可读的文件描述符。headers
<HTTP/2 Headers Object>options
<Object>statCheck
<Function>waitForTrailers
<boolean> 当为true
时,Http2Stream
将在发送完最后的DATA
帧后触发'wantTrailers'
事件。offset
<number> 开始读取的偏移位置。length
<number> 从文件描述符发送的数据量。
启动响应,其数据从给定的文件描述符中读取。
不对给定的文件描述符进行验证。
如果在尝试使用文件描述符读取数据时发生错误,则 Http2Stream
将使用标准 INTERNAL_ERROR
代码使用 RST_STREAM
帧关闭。
当使用时,Http2Stream
对象的 Duplex
接口会自动关闭。
const http2 = require('node:http2');
const fs = require('node:fs');
const server = http2.createServer();
server.on('stream', (stream) => {
const fd = fs.openSync('/some/file', 'r');
const stat = fs.fstatSync(fd);
const headers = {
'content-length': stat.size,
'last-modified': stat.mtime.toUTCString(),
'content-type': 'text/plain; charset=utf-8'
};
stream.respondWithFD(fd, headers);
stream.on('close', () => fs.closeSync(fd));
});
可以指定可选的 options.statCheck
函数,让用户代码有机会根据给定文件描述符的 fs.Stat
详细信息设置其他内容标头。
如果提供了 statCheck
函数,则 http2stream.respondWithFD()
方法将执行 fs.fstat()
调用以收集有关提供的文件描述符的详细信息。
offset
和 length
选项可用于限制对特定范围子集的响应。
例如,这可用于支持 HTTP 范围请求。
文件描述符或 FileHandle
在流关闭时没有关闭,所以一旦不再需要它就需要手动关闭。
不支持对多个流同时使用相同的文件描述符,这可能会导致数据丢失。
支持在流结束后重新使用文件描述符。
当设置了 options.waitForTrailers
选项时,'wantTrailers'
事件将在将要发送的最后一块有效负载数据排队后立即发出。
然后可以使用 http2stream.sendTrailers()
方法将尾随标头字段发送到对等方。
当设置了 options.waitForTrailers
,则传输完最后的 DATA
帧时,Http2Stream
不会自动关闭。
用户代码必须调用 http2stream.sendTrailers()
或 http2stream.close()
来关闭 Http2Stream
const http2 = require('node:http2');
const fs = require('node:fs');
const server = http2.createServer();
server.on('stream', (stream) => {
const fd = fs.openSync('/some/file', 'r');
const stat = fs.fstatSync(fd);
const headers = {
'content-length': stat.size,
'last-modified': stat.mtime.toUTCString(),
'content-type': 'text/plain; charset=utf-8'
};
stream.respondWithFD(fd, headers, { waitForTrailers: true });
stream.on('wantTrailers', () => {
stream.sendTrailers({ ABC: 'some value to send' });
});
stream.on('close', () => fs.closeSync(fd));
});
fd
<number> | <FileHandle> A readable file descriptor.headers
<HTTP/2 Headers Object>options
<Object>statCheck
<Function>waitForTrailers
<boolean> Whentrue
, theHttp2Stream
will emit the'wantTrailers'
event after the finalDATA
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.
const http2 = require('node:http2');
const fs = require('node:fs');
const server = http2.createServer();
server.on('stream', (stream) => {
const fd = fs.openSync('/some/file', 'r');
const stat = fs.fstatSync(fd);
const headers = {
'content-length': stat.size,
'last-modified': stat.mtime.toUTCString(),
'content-type': 'text/plain; charset=utf-8'
};
stream.respondWithFD(fd, headers);
stream.on('close', () => fs.closeSync(fd));
});
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
.
const http2 = require('node:http2');
const fs = require('node:fs');
const server = http2.createServer();
server.on('stream', (stream) => {
const fd = fs.openSync('/some/file', 'r');
const stat = fs.fstatSync(fd);
const headers = {
'content-length': stat.size,
'last-modified': stat.mtime.toUTCString(),
'content-type': 'text/plain; charset=utf-8'
};
stream.respondWithFD(fd, headers, { waitForTrailers: true });
stream.on('wantTrailers', () => {
stream.sendTrailers({ ABC: 'some value to send' });
});
stream.on('close', () => fs.closeSync(fd));
});