Buffer.swap16不是一个函数

我已经在NodeJS中testing了一些缓冲区交换function,但是在我的Meteor项目中它不起作用。

有人可以告诉我原因吗?

的NodeJS:

var CRC = new Buffer([0x20,0x40]); console.log(CRC.swap16()); // OUTPUT: <Buffer 40 20> 

Meteor.js

 var CRC = new Buffer([0x20,0x40]); console.log(CRC.swap16()); // OUTPUT: TypeError: CRC.swap16 is not a function 

有人可以帮我吗? 知道这可能是一个版本问题。 但目前我无法解决这个问题。

有没有办法让这个东西与meteor运行?

Buffer.swap16()添加了Buffer.swap16()方法,而Meteor使用NodeJS版本4。

你可以从NodeJS源复制这个方法的实现,它很简单(只需要很小的修改):

 function swap(b, n, m) { const i = b[n]; b[n] = b[m]; b[m] = i; } Buffer.prototype.swap16 = function swap16() { const len = this.length; if (len % 2 !== 0) { throw new RangeError('ERR_INVALID_BUFFER_SIZE', '16-bits'); } for (var i = 0; i < len; i += 2) { swap(this, i, i + 1); } return this; };