Coder Social home page Coder Social logo

buffer-from's Introduction

Buffer From

A ponyfill for Buffer.from, uses native implementation if available.

Installation

npm install --save buffer-from

Usage

const bufferFrom = require('buffer-from')

console.log(bufferFrom([1, 2, 3, 4]))
//=> <Buffer 01 02 03 04>

const arr = new Uint8Array([1, 2, 3, 4])
console.log(bufferFrom(arr.buffer, 1, 2))
//=> <Buffer 02 03>

console.log(bufferFrom('test', 'utf8'))
//=> <Buffer 74 65 73 74>

const buf = bufferFrom('test')
console.log(bufferFrom(buf))
//=> <Buffer 74 65 73 74>

API

bufferFrom(array)

  • array <Array>

Allocates a new Buffer using an array of octets.

bufferFrom(arrayBuffer[, byteOffset[, length]])

  • arrayBuffer <ArrayBuffer> The .buffer property of a TypedArray or ArrayBuffer
  • byteOffset <Integer> Where to start copying from arrayBuffer. Default: 0
  • length <Integer> How many bytes to copy from arrayBuffer. Default: arrayBuffer.length - byteOffset

When passed a reference to the .buffer property of a TypedArray instance, the newly created Buffer will share the same allocated memory as the TypedArray.

The optional byteOffset and length arguments specify a memory range within the arrayBuffer that will be shared by the Buffer.

bufferFrom(buffer)

  • buffer <Buffer> An existing Buffer to copy data from

Copies the passed buffer data onto a new Buffer instance.

bufferFrom(string[, encoding])

  • string <String> A string to encode.
  • encoding <String> The encoding of string. Default: 'utf8'

Creates a new Buffer containing the given JavaScript string string. If provided, the encoding parameter identifies the character encoding of string.

See also

buffer-from's People

Contributors

linusu avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

buffer-from's Issues

Can't find variable: Buffer

An error occurs when attempting to run a react-native project.

Can't find variable: Buffer

It is originating from index.js, line 4.

typeof Buffer.alloc === 'function' &&

For whatever reason, it seems like Buffer is not available in the global scope.

I was able to resolve this by adding the following before var isModern = (

if (typeof Buffer === 'undefined') {
  global.Buffer = require('buffer').Buffer
}

Afterwards my project runs correctly.

Get error "TypeError: utf8 is not a function"

Triggering condition:
When Buffer.alloc and the Buffer.allocUnsafe function are polyfilled by other modules, and the version of node is less than v4.5.0.

Cause:
The Buffer.from method isn't realized until v4.5.0, but it is a function befor v4.5.0.

I think you maybe already know this and use two methods ,Buffer.alloc and Buffer.allocUnsafe, also added in v4.5.0 as auxiliary conditions to judge if the environment is modern.
Is it better to judge the version of node or try the from method to judge if it is really usable?

LICENSE file is not part of published npm module

Sorry for annoying you with the LICENSE topic again

installing [email protected]
npm install [email protected]

and checking the retrieved files
ls node_modules/buffer-from

turns out, that the LICENSE file is not part of the distributed module.
Only these files are present:
index.js package.json readme.md

I think it would be a good thing to include the LICENSE file with the distributed module

Buffer is undefined in some environments

@LinusU Hi! I've found this package because it's used in html-tokenize, which is used in @emotion/server. This last package is related to server side rendering stuff and I'm trying to make it work in non-Node.js envrionments, such as Cloudflare Workers.

My problem is that this library (buffer-from) tries to access Buffer on import. Therefore, even if the rest of the functions are never called (they are not in this scenario), the app crashes due to having Buffer undefined.

Would you consider adding typeof Buffer !== 'undefined' && in the logic for isModern?
Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.