123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- "use strict";
- module.exports = {
- /**
- * True if this is running in Nodejs, will be undefined in a browser.
- * In a browser, browserify won't include this file and the whole module
- * will be resolved an empty object.
- */
- isNode : typeof Buffer !== "undefined",
- /**
- * Create a new nodejs Buffer from an existing content.
- * @param {Object} data the data to pass to the constructor.
- * @param {String} encoding the encoding to use.
- * @return {Buffer} a new Buffer.
- */
- newBufferFrom: function(data, encoding) {
- if (Buffer.from && Buffer.from !== Uint8Array.from) {
- return Buffer.from(data, encoding);
- } else {
- if (typeof data === "number") {
- // Safeguard for old Node.js versions. On newer versions,
- // Buffer.from(number) / Buffer(number, encoding) already throw.
- throw new Error("The \"data\" argument must not be a number");
- }
- return new Buffer(data, encoding);
- }
- },
- /**
- * Create a new nodejs Buffer with the specified size.
- * @param {Integer} size the size of the buffer.
- * @return {Buffer} a new Buffer.
- */
- allocBuffer: function (size) {
- if (Buffer.alloc) {
- return Buffer.alloc(size);
- } else {
- var buf = new Buffer(size);
- buf.fill(0);
- return buf;
- }
- },
- /**
- * Find out if an object is a Buffer.
- * @param {Object} b the object to test.
- * @return {Boolean} true if the object is a Buffer, false otherwise.
- */
- isBuffer : function(b){
- return Buffer.isBuffer(b);
- },
- isStream : function (obj) {
- return obj &&
- typeof obj.on === "function" &&
- typeof obj.pause === "function" &&
- typeof obj.resume === "function";
- }
- };
|