How to use the buffer.Buffer.prototype function in buffer

To help you get started, we’ve selected a few buffer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pmq20 / node-packer / node / lib / zlib.js View on Github external
function zlibBuffer(engine, buffer, callback) {
  // Streams do not support non-Buffer ArrayBufferViews yet. Convert it to a
  // Buffer without copying.
  if (ArrayBuffer.isView(buffer) &&
      Object.getPrototypeOf(buffer) !== Buffer.prototype) {
    buffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  }

  var buffers = [];
  var nread = 0;

  engine.on('error', onError);
  engine.on('end', onEnd);

  engine.end(buffer);
  flow();

  function flow() {
    var chunk;
    while (null !== (chunk = engine.read())) {
      buffers.push(chunk);
github graalvm / graaljs / lib / zlib.js View on Github external
function zlibBuffer(engine, buffer, callback) {
  if (typeof callback !== 'function')
    throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
  // Streams do not support non-Buffer ArrayBufferViews yet. Convert it to a
  // Buffer without copying.
  if (isArrayBufferView(buffer) &&
      Object.getPrototypeOf(buffer) !== Buffer.prototype) {
    buffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  } else if (isAnyArrayBuffer(buffer)) {
    buffer = Buffer.from(buffer);
  }
  engine.buffers = null;
  engine.nread = 0;
  engine.cb = callback;
  engine.on('data', zlibBufferOnData);
  engine.on('error', zlibBufferOnError);
  engine.on('end', zlibBufferOnEnd);
  engine.end(buffer);
}
github spdy-http2 / node-spdy / lib / spdy / protocol.js View on Github external
*/

var Buffer = require('buffer').Buffer,
    enums = require('../spdy').enums;

/**
 * Compatibility with older versions of node
 */
if (!Buffer.prototype.writeUInt32BE) {
  Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
    this.writeUInt32(value, offset, 'big');
  };
  Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
    this.writeUInt32(value, offset, 'little');
  };
  Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
    this.writeUInt16(value, offset, 'big');
  };
}

/**
 * Create and return dataframe buffer
 */
exports.createDataFrame = function(zlib, headers, data) {
  if (headers.flags & enums.DATA_FLAG_COMPRESSED) {
    data = zlib.deflate(data);
  }

  var result = insertCommonData(headers, data, result);

  // Insert stream id
  result.writeUInt32BE(headers.streamID & 0x7fffffff, 0);
github nodejs / readable-stream / lib / _stream_readable.js View on Github external
function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
  debug('readableAddChunk', chunk);
  var state = stream._readableState;

  if (chunk === null) {
    state.reading = false;
    onEofChunk(stream, state);
  } else {
    var er;
    if (!skipChunkCheck) er = chunkInvalid(state, chunk);

    if (er) {
      stream.emit('error', er);
    } else if (state.objectMode || chunk && chunk.length > 0) {
      if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) {
        chunk = _uint8ArrayToBuffer(chunk);
      }

      if (addToFront) {
        if (state.endEmitted) stream.emit('error', new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());else addChunk(stream, state, chunk, true);
      } else if (state.ended) {
        stream.emit('error', new ERR_STREAM_PUSH_AFTER_EOF());
      } else if (state.destroyed) {
        return false;
      } else {
        state.reading = false;

        if (state.decoder && !encoding) {
          chunk = state.decoder.write(chunk);
          if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state);
        } else {
github spdy-http2 / node-spdy / lib / spdy / protocol.js View on Github external
/**
 * Protocol classes
 */

var Buffer = require('buffer').Buffer,
    enums = require('../spdy').enums;

/**
 * Compatibility with older versions of node
 */
if (!Buffer.prototype.writeUInt32BE) {
  Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
    this.writeUInt32(value, offset, 'big');
  };
  Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
    this.writeUInt32(value, offset, 'little');
  };
  Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
    this.writeUInt16(value, offset, 'big');
  };
}

/**
 * Create and return dataframe buffer
 */
exports.createDataFrame = function(zlib, headers, data) {
  if (headers.flags & enums.DATA_FLAG_COMPRESSED) {
github spdy-http2 / node-spdy / lib / spdy / protocol.js View on Github external
/**
 * Protocol classes
 */

var Buffer = require('buffer').Buffer,
    enums = require('../spdy').enums;

/**
 * Compatibility with older versions of node
 */
if (!Buffer.prototype.writeUInt32BE) {
  Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
    this.writeUInt32(value, offset, 'big');
  };
  Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
    this.writeUInt32(value, offset, 'little');
  };
  Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
    this.writeUInt16(value, offset, 'big');
  };
}

/**
 * Create and return dataframe buffer
 */
exports.createDataFrame = function(zlib, headers, data) {
  if (headers.flags & enums.DATA_FLAG_COMPRESSED) {
    data = zlib.deflate(data);
  }

  var result = insertCommonData(headers, data, result);
github standard-things / esm / src / util / to-string.js View on Github external
import { Buffer } from "buffer"

import binding from "../binding.js"

const bufferToString = Buffer.prototype.toString
const utilBinding = binding.util

const _String = String
const _safeToString = utilBinding.safeToString

const useSafeToString = typeof safeToString === "function"

function safeToString(value) {
  if (useSafeToString) {
    try {
      return _safeToString.call(utilBinding, value)
    } catch (e) {}
  }

  return _String(value)
}
github graalvm / graaljs / lib / stream.js View on Github external
Stream._uint8ArrayToBuffer = function _uint8ArrayToBuffer(chunk) {
      return Buffer.prototype.slice.call(chunk);
    };
  }
github spdy-http2 / node-spdy / lib / spdy / protocol.js View on Github external
/**
 * Protocol classes
 */

var Buffer = require('buffer').Buffer,
    enums = require('../spdy').enums;

/**
 * Compatibility with older versions of node
 */
if (!Buffer.prototype.writeUInt32BE) {
  Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
    this.writeUInt32(value, offset, 'big');
  };
  Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
    this.writeUInt32(value, offset, 'little');
  };
  Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
    this.writeUInt16(value, offset, 'big');
  };
}

/**
 * Create and return dataframe buffer
 */
exports.createDataFrame = function(zlib, headers, data) {
  if (headers.flags & enums.DATA_FLAG_COMPRESSED) {
    data = zlib.deflate(data);
github sidorares / node-mysql2 / lib / packets / packet.js View on Github external
static MockBuffer() {
    const noop = function() {};
    const res = Buffer.alloc(0);
    for (const op in NativeBuffer.prototype) {
      if (typeof res[op] === 'function') {
        res[op] = noop;
      }
    }
    return res;
  }
}