覆盖低级别的node.js模块

Amazon S3允许静态网站托pipe,但要求存储桶名称必须与您的域名匹配。 这意味着您的存储桶名称将如下所示:mydomain.com。 Amazon S3还为* .s3.amazonaws.com提供通配符SSL证书。 根据TLS的规定,这意味着com.s3.amazonaws.com是由证书覆盖,但mybucket.com.s3.amazonaws.com不是。 像Knox这样的连接到* .com.s3.amazonaws.com的节点应用程序应该能够信任该证书,即使它打破了TLS的规则,因为knox库是一个“封闭的系统”:它只能连接到亚马逊物业。

Node模块https依赖于tls.js ,而tls.js具有这个function:

 function checkServerIdentity(host, cert) { ... // "The client SHOULD NOT attempt to match a presented identifier in // which the wildcard character comprises a label other than the // left-most label (eg, do not match bar.*.example.net)." // RFC6125 if (!wildcards && /*/.test(host) || /[.*].**/.test(host) || /*/.test(host) && !/*.*..+..+/.test(host)) { return /$./; } 

哪个会正确返回“证书不匹配”错误。 上层的Knox模块是否可以覆盖checkServerIdentity函数,该函数有几个层次,Knox不直接调用它? 我知道如何重写我需要的库中的函数,但不包括这些库包含的库。

有一个模块的全局caching,这意味着你覆盖的任何函数将被修改为所有其他模块。 我想你可以包括你自己和补丁checkServerIdentity

 // main.js
 var tls = require('tls'),
     mod = require('./mod.js');

 tls.checkServerIdentity = function(host,cert){
  返回true;
 };

 mod.test();
 // mod.js
 var tls = require('tls');

 exports.test = function(){
  的console.log(tls.checkServerIdentity());  // true
 };

如果你不想改变全局模块对象(根据你对Nik的回答的评论),也许你可以使用rewire模块。 我想像这样做:

  var knoxModule = rewire("./node_modules/knox/somefile.js"); knoxModule.__set__("tls", { checkServerIdentity: function (host, cert) { // some code } }); 

我从来没有用过它。