http2stream.respondWithFD(fd[, headers[, options]])
fd<number> | <FileHandle> 一个可读的文件描述符。headersHTTP/2 头对象options<Object>statCheck<Function>waitForTrailers<boolean> 当设置为true时,Http2Stream会在最后一个DATA帧发送后触发'wantTrailers'事件。offset<number> 开始读取的偏移位置。length<number> 要从文件描述符发送的数据量。
启动一个响应,其数据从给定的文件描述符读取。不会对给定的文件描述符进行任何验证。如果在使用文件描述符读取数据时发生错误,Http2Stream 将通过使用标准 INTERNAL_ERROR 代码的 RST_STREAM 帧关闭。
【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.】
使用时,Http2Stream 对象的 Duplex 接口将会被自动关闭。
【When used, the Http2Stream object's Duplex interface will be closed
automatically.】
import { createServer } from 'node:http2';
import { openSync, fstatSync, closeSync } from 'node:fs';
const server = createServer();
server.on('stream', (stream) => {
const fd = openSync('/some/file', 'r');
const stat = 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', () => closeSync(fd));
});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() 调用以收集提供的文件描述符的详细信息。
【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.】
offset 和 length 选项可用于将响应限制在特定范围的子集。例如,这可以用于支持 HTTP 范围请求。
【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.】
流关闭时,文件描述符或 FileHandle 不会被关闭,因此在不再需要时需要手动关闭。 不支持在多个流中并发使用同一个文件描述符,并且可能导致数据丢失。 在流结束后重用文件描述符是支持的。
【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.】
当设置 options.waitForTrailers 选项时,在将要发送的最后一块负载数据入队之后,将立即触发 'wantTrailers' 事件。然后可以使用 http2stream.sendTrailers() 方法向对端发送尾部头字段。
【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.】
当设置 options.waitForTrailers 时,Http2Stream 在最后的 DATA 帧发送完毕后不会自动关闭。用户代码必须调用 http2stream.sendTrailers() 或 http2stream.close() 来关闭 Http2Stream。
【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.】
import { createServer } from 'node:http2';
import { openSync, fstatSync, closeSync } from 'node:fs';
const server = createServer();
server.on('stream', (stream) => {
const fd = openSync('/some/file', 'r');
const stat = 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', () => closeSync(fd));
});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));
});