Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
# is-buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
# is-buffer [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]

[travis-image]: https://img.shields.io/travis/feross/is-buffer/master.svg
[travis-url]: https://travis-ci.org/feross/is-buffer
[npm-image]: https://img.shields.io/npm/v/is-buffer.svg
[npm-url]: https://npmjs.org/package/is-buffer
[downloads-image]: https://img.shields.io/npm/dm/is-buffer.svg
[downloads-url]: https://npmjs.org/package/is-buffer
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
[standard-url]: https://standardjs.com

#### Determine if an object is a [`Buffer`](http://nodejs.org/api/buffer.html) (including the [browserify Buffer](https://github.com/feross/buffer))

[![saucelabs][saucelabs-image]][saucelabs-url]

[saucelabs-image]: https://saucelabs.com/browser-matrix/is-buffer.svg
[saucelabs-url]: https://saucelabs.com/u/is-buffer
Determine if an object is a [`Buffer`](https://nodejs.org/api/buffer.html) (including browser-compatible Buffer implementations).

## Why not use `Buffer.isBuffer`?

This module lets you check if an object is a `Buffer` without using `Buffer.isBuffer` (which includes the whole [buffer](https://github.com/feross/buffer) module in [browserify](http://browserify.org/)).
This module provides a tiny compatibility wrapper for checking whether a value is a `Buffer`.

It's future-proof and works in node too!
- It prefers the native `Buffer.isBuffer` check when available.
- It falls back to `constructor.isBuffer` for browser-compatible Buffer implementations.
- It avoids throwing on malformed or spoofed objects.

## install
## Install

```bash
npm install is-buffer
```

## usage
## Usage

```js
var isBuffer = require('is-buffer')

isBuffer(new Buffer(4)) // true
isBuffer(Buffer.alloc(4)) //true
isBuffer(Buffer.alloc(4)) // true
isBuffer(Buffer.from([1, 2, 3])) // true

isBuffer(undefined) // false
isBuffer(null) // false
Expand All @@ -47,8 +42,13 @@ isBuffer(1.0) // false
isBuffer('string') // false
isBuffer({}) // false
isBuffer(function foo () {}) // false
isBuffer(new Uint8Array([1, 2, 3])) // false
```

## license
## Notes

In modern Node.js projects, `Buffer.isBuffer` is often enough on its own. This package is most useful when you want a tiny wrapper that also tolerates browser-style Buffer implementations.

## License

MIT. Copyright (C) [Feross Aboukhadijeh](http://feross.org).
MIT. Copyright (C) [Feross Aboukhadijeh](https://feross.org).
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare function isBuffer(obj: any): boolean
declare function isBuffer(obj: unknown): boolean
export = isBuffer
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@
*/

module.exports = function isBuffer (obj) {
return obj != null && obj.constructor != null &&
typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
if (typeof Buffer !== 'undefined' && typeof Buffer.isBuffer === 'function') {
return Buffer.isBuffer(obj)
}

if (obj == null || obj.constructor == null) {
return false
}

if (typeof obj.constructor.isBuffer !== 'function') {
return false
}

try {
return obj.constructor.isBuffer(obj)
} catch (_) {
return false
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "is-buffer",
"description": "Determine if an object is a Buffer",
"description": "Determine if an object is a Buffer with safe native fallback",
"version": "2.0.5",
"author": {
"name": "Feross Aboukhadijeh",
Expand All @@ -17,7 +17,7 @@
"tape": "^5.0.1"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"keywords": [
"arraybuffer",
Expand All @@ -40,7 +40,7 @@
"main": "index.js",
"repository": {
"type": "git",
"url": "git://github.com/feross/is-buffer.git"
"url": "https://github.com/feross/is-buffer.git"
},
"scripts": {
"test": "standard && npm run test-node && npm run test-browser",
Expand Down
3 changes: 3 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ test('is-buffer', function (t) {
t.equal(isBuffer(function foo () {}), false, 'function foo () {}')
t.equal(isBuffer({ isBuffer: null }), false, '{ isBuffer: null }')
t.equal(isBuffer({ isBuffer: function () { throw new Error() } }), false, '{ isBuffer: function () { throw new Error() } }')
t.equal(isBuffer(new Uint8Array([1, 2, 3])), false, 'Uint8Array')
t.equal(isBuffer(new ArrayBuffer(8)), false, 'ArrayBuffer')
t.equal(isBuffer({ constructor: { isBuffer: function () { return true } } }), false, 'fake constructor.isBuffer')

t.end()
})