Skip to content

Commit

Permalink
Other: Just polyfill Buffer.from / .allocUnsafe for good
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 26, 2016
1 parent c3023a2 commit b5a068f
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,29 @@ util.isIE8 = false; try { util.isIE8 = eval("!-[1,]"); } catch (e) {} // eslint-
* Node's Buffer class if available.
* @type {?function(new: Buffer)}
*/
util.Buffer = (util.Buffer = util.inquire("buffer")) && util.Buffer.Buffer || null;

if (util.Buffer) {
// Don't use browser-buffer for performance
if (!util.Buffer.prototype.utf8Write)
util.Buffer = null;
// Polyfill Buffer.from
else if (!util.Buffer.from)
util.Buffer.from = function from(value, encoding) { return new util.Buffer(value, encoding); };
}
util.Buffer = (function() {
try {
var Buffer = util.inquire("buffer").Buffer;

/* istanbul ignore next */
if (!Buffer.prototype.utf8Write) // refuse to use non-node buffers (performance)
return null;

/* istanbul ignore next */
if (!Buffer.from)
Buffer.from = function from(value, encoding) { return new Buffer(value, encoding); };

/* istanbul ignore next */
if (!Buffer.allocUnsafe)
Buffer.allocUnsafe = function allocUnsafe(size) { return new Buffer(size); };

return Buffer;

/* istanbul ignore next */
} catch (e) {
return null;
}
})();

/**
* Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
Expand Down

0 comments on commit b5a068f

Please sign in to comment.