You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
'use strict' ;
// We define these manually to ensure they're always copied
// even if they would move up the prototype chain
// https://nodejs.org/api/http.html#http_class_http_incomingmessage
// 定义一个包含已知属性名称的数组,这些属性可能在后续的处理中有特定用途或需要关注
const knownProps = [
'destroy' ,
'setTimeout' ,
'socket' ,
'headers' ,
'trailers' ,
'rawHeaders' ,
'statusCode' ,
'httpVersion' ,
'httpVersionMinor' ,
'httpVersionMajor' ,
'rawTrailers' ,
'statusMessage'
] ;
// 定义一个函数并通过module.exports进行导出, 使其可以在其他模块中被引入和使用
// 该函数接收两个参数, fromStream和toStream, 从名字可以推测是两个类似流对象的数据结构( 但具体取决于实际传入的内容)
module . exports = ( fromStream , toStream ) => {
// 创建一个Set数据结构, 它包含了fromStream对象自身所有属性名以及预定义的knownProps数组中的属性名
// 通过Object.keys获取fromStream的属性名数组并使用concat方法将knownProps合并进来
const fromProps = new Set ( Object . keys ( fromStream ) . concat ( knownProps ) ) ;
// 遍历fromProps这个Set集合, 依次处理每个属性
for ( const prop of fromProps ) {
// 检查要处理的属性( prop) 是否已经存在于toStream对象中, 如果已经存在, 则跳过当前属性的处理, 进入下一次循环
// 目的是避免覆盖toStream对象中已有的同名属性
if ( prop in toStream ) {
continue ;
}
// 根据fromStream中对应属性( prop) 的类型进行不同处理
// 如果属性值是一个函数, 那么使用bind方法将该函数的this指向绑定为fromStream对象本身, 然后赋值给toStream对象对应的属性
// 如果属性值不是函数, 直接将fromStream对象中该属性的值赋给toStream对象对应的属性
toStream [ prop ] = typeof fromStream [ prop ] === 'function' ? fromStream [ prop ] . bind ( fromStream ) : fromStream [ prop ] ;
}
} ;