CoolFace
Apppublic

TrinetraLabs/Placebo_AI

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
nodejsUtils.js58 linesDownload Raw Back to lib
1"use strict";2 3module.exports = {4    /**5     * True if this is running in Nodejs, will be undefined in a browser.6     * In a browser, browserify won't include this file and the whole module7     * will be resolved an empty object.8     */9    isNode : typeof Buffer !== "undefined",10    /**11     * Create a new nodejs Buffer from an existing content.12     * @param {Object} data the data to pass to the constructor.13     * @param {String} encoding the encoding to use.14     * @return {Buffer} a new Buffer.15     */16    newBufferFrom: function(data, encoding) {17        if (Buffer.from && Buffer.from !== Uint8Array.from) {18            return Buffer.from(data, encoding);19        } else {20            if (typeof data === "number") {21                // Safeguard for old Node.js versions. On newer versions,22                // Buffer.from(number) / Buffer(number, encoding) already throw.23                throw new Error("The \"data\" argument must not be a number");24            }25            return new Buffer(data, encoding);26        }27    },28    /**29     * Create a new nodejs Buffer with the specified size.30     * @param {Integer} size the size of the buffer.31     * @return {Buffer} a new Buffer.32     */33    allocBuffer: function (size) {34        if (Buffer.alloc) {35            return Buffer.alloc(size);36        } else {37            var buf = new Buffer(size);38            buf.fill(0);39            return buf;40        }41    },42    /**43     * Find out if an object is a Buffer.44     * @param {Object} b the object to test.45     * @return {Boolean} true if the object is a Buffer, false otherwise.46     */47    isBuffer : function(b){48        return Buffer.isBuffer(b);49    },50 51    isStream : function (obj) {52        return obj &&53            typeof obj.on === "function" &&54            typeof obj.pause === "function" &&55            typeof obj.resume === "function";56    }57};58