serverhttp2session.altsvc(alt, originOrStream)


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

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

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

import { createServer } from 'node:http2';

const server = 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);
});const http2 = require('node: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);
});

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

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

alt 和 origin 字符串 必须 只包含 ASCII 字节,并且严格按照 ASCII 字节序列进行解释。特殊值 'clear' 可以用于清除之前为某个域设置的任何备用服务。

【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.】

当为 originOrStream 参数传递一个字符串时,该字符串将被解析为 URL,并从中获取源。例如,HTTP URL 'https://example.org/foo/bar' 的源是 ASCII 字符串 'https://example.org'。如果给定的字符串无法被解析为 URL,或无法推导出有效的源,将会抛出错误。

【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.】

一个 URL 对象,或者任何具有 origin 属性的对象,都可以作为 originOrStream 传入,此时将使用 origin 属性的值。origin 属性的值必须是正确序列化的 ASCII 源。

【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.】