【发布时间】:2015-02-06 22:26:03
【问题描述】:
我正在尝试在 chrome 打包应用程序中使用一些 node.js 模块。 (我说的是串口)
我已扩展 Buffer 原型以添加“indexOf”方法。
我正在使用 Browserify,似乎正在发生的事情是它没有获取我的原型扩展。我的缓冲区最终成为没有 indexOf 可用的 Uint8Arrays。
以 Browserify 会采用的方式扩展 Buffer 有什么技巧吗?
我的扩展看起来像这样,但我也尝试过做同样事情的 npm 包(下面的代码是从一个中提取的),所以我认为问题不一定出在我的代码中:
Buffer.indexOf = function(haystack, needle, i) {
if (!Buffer.isBuffer(needle)) {
needle = new Buffer(needle);
}
if (typeof i === 'undefined') {
i = 0;
}
var l = haystack.length - needle.length + 1;
while (i < l) {
var good = true;
for (var j = 0, n = needle.length; j < n; j++) {
if (haystack.get(i + j) !== needle.get(j)) {
good = false;
break;
}
}
if (good) {
return i;
}
i++;
}
return -1;
};
Buffer.prototype.indexOf = function(needle, i) {
return Buffer.indexOf(this, needle, i);
}
【问题讨论】:
-
它是 Chrome 应用程序还是 Chrome 扩展程序?请编辑以使用正确的标签。
标签: node.js google-chrome-app browserify