serverhttp2session.altsvc(alt, originOrStream)


  • alt <string> RFC 7838 定义的替代服务配置的描述。
  • originOrStream <number> | <string> | <URL> | <Object> 指定来源的 URL 字符串(或具有 origin 属性的 Object)或由 http2stream.id 属性给出的活动 Http2Stream 的数字标识符。

向连接的客户端提交 ALTSVC 帧(由 RFC 7838 定义)。

const http2 = require('http2');

const server = http2.createServer();
server.on('session', (session) => {
  // 为源 https://example.org:80 设置 altsvc
  session.altsvc('h2=":8000"', 'https://example.org:80');
});

server.on('stream', (stream) => {
  // 为特定流设置 altsvc
  stream.session.altsvc('h2=":8000"', stream.id);
});

发送带有特定流 ID 的 ALTSVC 帧表示备用服务与给定 Http2Stream 的来源相关联。

alt 和原点字符串必须只包含 ASCII 字节,并且严格解释为 ASCII 字节序列。 可以传入特殊值 'clear' 以清除给定域的任何先前设置的替代服务。

当为 originOrStream 参数传入字符串时,则它将被解析为 URL 并导出来源。 例如,HTTP URL 'https://example.org/foo/bar' 的来源是 ASCII 字符串 'https://example.org'。 如果给定的字符串无法解析为 URL,或者无法导出有效的来源,则会抛出错误。

URL 对象,或任何具有 origin 属性的对象,都可以作为 originOrStream 传入,在这种情况下,将使用 origin 属性的值。 origin 属性的值必须是正确序列化的 ASCII 源。

  • alt <string> A description of the alternative service configuration as defined by RFC 7838.
  • originOrStream <number> | <string> | <URL> | <Object> Either a URL string specifying the origin (or an Object with an origin property) or the numeric identifier of an active Http2Stream as given by the http2stream.id property.

Submits an ALTSVC frame (as defined by RFC 7838) to the connected client.

const http2 = require('http2');

const server = http2.createServer();
server.on('session', (session) => {
  // Set altsvc for origin https://example.org:80
  session.altsvc('h2=":8000"', 'https://example.org:80');
});

server.on('stream', (stream) => {
  // Set altsvc for a specific stream
  stream.session.altsvc('h2=":8000"', stream.id);
});

Sending an ALTSVC frame with a specific stream ID indicates that the alternate service is associated with the origin of the given Http2Stream.

The alt and origin string must contain only ASCII bytes and are strictly interpreted as a sequence of ASCII bytes. The special value 'clear' may be passed to clear any previously set alternative service for a given domain.

When a string is passed for the originOrStream argument, it will be parsed as a URL and the origin will be derived. For instance, the origin for the HTTP URL 'https://example.org/foo/bar' is the ASCII string 'https://example.org'. An error will be thrown if either the given string cannot be parsed as a URL or if a valid origin cannot be derived.

A URL object, or any object with an origin property, may be passed as originOrStream, in which case the value of the origin property will be used. The value of the origin property must be a properly serialized ASCII origin.