buffer

repository·master·Indexed 23 days ago

https://github.com/feross/buffer

A browser-side implementation of the Node.js Buffer API (version 6.0.3) that allows developers to manipulate binary data in the browser. It is a subclass of Uint8Array and provides a 100% identical API to Node.js, including methods for allocation (Buffer.alloc, Buffer.allocUnsafe), data conversion (Buffer.from), and reading/writing various integer and floating-point types in Little-Endian and Big-Endian formats.

Tokens
3.4K
Snippets
8
Records
32
Agent score
34%

What's inside buffer

  1. How Buffer and Uint8Array work together

    master

    The Buffer constructor returns instances of Uint8Array that have their prototype changed to Buffer.prototype. Because Buffer is a subclass of Uint8Array, the returned instances possess all both Node Buffer methods and standard Uint8Array methods.

    Key behaviors:

    • Square bracket notation (e.g., buf[4]) works and returns a single octet.
    • The Uint8Array prototype itself remains unmodified.
    • The implementation is backed by Typed Arrays (Uint8Array/ArrayBuffer) for high performance.
  2. Run tests in a browser

    master

    To test the project locally in a browser, use one of the following commands depending on the browser's ES6 support. These commands will provide a URL that you can open in a browser to run tests via airtap.

    • For ES5 browsers (no ES6 support): npm run test-browser-old-local
    • For ES6 compliant browsers: npm run test-browser-new-local
    npm run test-browser-old-local
    npm run test-browser-new-local
  3. Use the buffer module in your code

    master

    The module's API is designed to be 100% identical to Node.js's Buffer API.

    If you are using Browserify, you can simply require('buffer') or use the Buffer global; the module will be automatically included in your bundle.

    If you need to depend on this module explicitly (without Browserify), you must use the trailing slash in the require statement to ensure the npm module is used instead of the Node.js core module:

    var Buffer = require('buffer/').Buffer  // note: the trailing slash is important!
  4. Run automated browser tests using Saucelabs

    master

    To run automated browser tests via Saucelabs (the same method used in Travis CI), you must first set your SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables. Then, run the standard test command.

    npm test
  5. Convert Buffer to ArrayBuffer

    master

    To convert a Buffer to an ArrayBuffer, you can use the .buffer property (available on all Uint8Array objects). To ensure you get the correct view of the data, you should slice the underlying buffer using the byteOffset and byteLength of the buffer:

    var arrayBuffer = buffer.buffer.slice(
      buffer.byteOffset, buffer.byteOffset + buffer.byteLength
    )
  6. Allocate a new Buffer with Buffer.alloc()

    master

    Use Buffer.alloc(size[, fill[, encoding]]) to create a new Buffer of a specified size and initialize it with fill.

    • size: The number of bytes to allocate.
    • fill: The value used to fill the buffer. If fill is a string, encoding must be provided.
    • encoding: The encoding used if fill is a string.
  7. Slice a Buffer with Buffer.prototype.slice()

    master

    Use buf.slice(start[, end]) to return a new Buffer that references the same memory as the original, but with a different start and end offset.

    • start: The starting index (inclusive).
    • end: The ending index (exclusive). If omitted, it defaults to the buffer length.

    Warning: The new buffer shares the same underlying memory as the original. Modifying the slice will modify the original buffer.