Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

node.js - Getting TypeError: this is not a typed array using Buffer.from in mocha

I'm using Mocha / Chai to unit test a library that has recently started using nodejs' Buffer objects to solve a different problem.

I get this error message in the unit test:

TypeError: this is not a typed array. 
at Function.from (native) 
at Object.hashesMatch (index.js:29:18
at Context.<anonymous> (test/test.js:25:22)

Line 29 of index.js is where I'm using nodejs' Buffer...

var b = Buffer.from ('some string or other');

I can't find a polyfill or workaround so would be grateful for suggestions.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You might be using an old version of Node.js.

Buffer.from was introduced in version 6.0.0:

To make the creation of Buffer objects more reliable and less error prone, the various forms of the new Buffer() constructor have been deprecated and replaced by separate Buffer.from(), Buffer.alloc(), and Buffer.allocUnsafe() methods.

There's no reference to this method in previous versions of documentiation.

You could either update to 6.0.0 or use a deprecated constructor API, which has the following signature:

new Buffer(str[, encoding])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...