如何loggingNodeJS原生(V8)模块?

我开发了一个本地的NodeJS模块(使用NAN助手)。 现在我想知道什么是最好的方式来logging它。

模块导出的方法只存在于C ++源代码中,但是我希望导出Javascript文档。

编辑:我发现另一种方式,我认为是更好的:

所有的JSDoc需要的是能够将doclet附加到SOMETHING,所以你可以做到这一点(如果你忽略JSHint警告预期分配或呼吁,而不是expression):

var nativeStuff = require('some/native/stuff'); /** * My Cool Class * @class */ nativeStuff.MyCoolClass; /** * My Cool Method * @function * @param {String} [name] The argument */ nativeStuff.MyCoolClass.prototype.CoolMethod; /** * Some Other Value * @type {String} */ nativeStuff.someStringValue; module.exports = nativeStuff; 

这具有与IDE(至lessWebStorm)一起工作的优点,并且不需要您复制或代理对象本身。 但请注意,您必须明确地告诉它每个条目是什么types的(使用@function或@class或@type),否则它不能自动推断它。


原始答复

有几个我知道的方式,但我承认没有看起来特别优雅。

在小的适配器文件中,您require()代码的本地部分并将其作为模块提供,您可以单独分配每个组件并以这种方式logging它们:

 var nativeStuff = require('some/native/stuff'); // If your module only has one thing /** * My Cool Class * @class MyCoolClass */ module.exports = nativeStuff; // If it has several things module.exports = { /** * My cool class * @class */ MyCoolClass: nativeStuff.MyCoolClass, /** * My Other Cool Member */ OtherCoolMember: nativeStuff.OtherCoolMember }; 

如果你想挑选课堂并重新组装它,你也可以用这种方式logging课程的成员,但是如果你想要深入的话,它会变得有点笨拙。

我知道的另一种方法是将每个类的方法都包装在本地JS中,这个方法带有一个几乎可以忽略的性能命中:

 var nativeStuff = require('some/native/stuff'); /** * My Cool Class */ class MyCoolClass { constructor() { this._nativeObj = new nativeStuff.MyCoolClass(); } /** * Some Cool Method * @param {String} [name] My name */ coolMethod(name) { return this._nativeObj(name); } } module.exports = MyCoolClass; 

(注意,这也适用于较老的JS版本,但ES6类使东西的方式更容易理解视觉:)

你也可以尝试使用@typedef指令来定义一些东西,因为关于typedef的doclet可以与它所描述的对象分开,但是我不认为typedef可以通常logging方法或类,它们只是用于数据对象。

我不得不创build一个简单的文档生成器,以类似Javadoc的风格扫描源代码中的注释: https : //github.com/aspectron/gendoc

这是这样的文档的一部分,看起来像这样:

 /** @module crypto **/ /** @class Hash Hash generator @function Hash(algorithm) @param algorithm {String} Constructor. Initiaize hash generator with specified algorithm, see #getHashes Throws exception on unknown algorithm **/ v8pp::class_<hash_generator> hash_class(bindings.isolate(), v8pp::v8_args_ctor); hash_class /** @function reset([algorithm]) @param [algorithm] {String} @return {Hash} Reset hash generator to initial state optionaly changing generator algorithm. Throws exception on unknown algorithm. **/ .set("reset", &hash_generator::reset_v8) ... 

最后我select了一个不太优雅的解决scheme。 我创build了一个单独的JavaScript文件,其中只有通过我的本地API导出的方法。

这个文件看起来像这样:

 /** @namespace AwesomeLibrary * * @description API overview */ AwesomeLibrary = { /** * @param {string} param Input parameter * @return combobulates {@link param} */ combobulate: function (param) {} } 

然后我使用JsDoc生成项目的文档,并将这个JavaScript文件作为input而不是我的本地代码。 最后,我将文档与我的模块的二进制分发捆绑在一起。

这个解决scheme并不理想,因为文档和源代码必须单独维护,但是它具有零开销和(相当)干净的文件的优点。 我也禁用了JsDoc中的源代码生成,因为这显然是无用的,只显示空的存根。