http2stream.pushStream(headers[, options], callback)


  • headers HTTP/2 头对象
  • options <Object>
    • exclusive <boolean> 当值为 trueparent 指定了一个父流时,创建的流将成为父流的唯一直接依赖,所有其他现有的依赖将成为新创建流的依赖。 默认值: false
    • parent <number> 指定新创建的流所依赖的流的数字标识符。
  • callback <Function> 推送流启动后调用的回调函数。
    • err <Error>
    • pushStream <ServerHttp2Stream> 返回的 pushStream 对象。
    • headers HTTP/2 Headers 对象 pushStream 启动时使用的 Headers 对象。

启动一个推送流。回调会被调用,并传入为推送流创建的新 Http2Stream 实例作为第二个参数,或者传入一个 Error 作为第一个参数。

【Initiates a push stream. The callback is invoked with the new Http2Stream instance created for the push stream passed as the second argument, or an Error passed as the first argument.】

const http2 = require('node:http2');
const server = http2.createServer();
server.on('stream', (stream) => {
  stream.respond({ ':status': 200 });
  stream.pushStream({ ':path': '/' }, (err, pushStream, headers) => {
    if (err) throw err;
    pushStream.respond({ ':status': 200 });
    pushStream.end('some pushed data');
  });
  stream.end('some data');
}); 

HEADERS 帧中不允许设置推送流的权重。请将 weight 值传递给 http2stream.priority,并将 silent 选项设置为 true,以启用服务器端并发流之间的带宽平衡。

【Setting the weight of a push stream is not allowed in the HEADERS frame. Pass a weight value to http2stream.priority with the silent option set to true to enable server-side bandwidth balancing between concurrent streams.】

在已推送的流中调用 http2stream.pushStream() 是不允许的,会抛出错误。

【Calling http2stream.pushStream() from within a pushed stream is not permitted and will throw an error.】