diff --git a/README.md b/README.md index 8c9785f..cb3b679 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ -# 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 @@ -9,32 +7,29 @@ [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 @@ -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). diff --git a/index.d.ts b/index.d.ts index 7065c69..e658650 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,2 +1,2 @@ -declare function isBuffer(obj: any): boolean +declare function isBuffer(obj: unknown): boolean export = isBuffer diff --git a/index.js b/index.js index da9bfdd..3c4d328 100644 --- a/index.js +++ b/index.js @@ -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 + } } diff --git a/package.json b/package.json index 7cd70d4..02968f8 100644 --- a/package.json +++ b/package.json @@ -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", @@ -17,7 +17,7 @@ "tape": "^5.0.1" }, "engines": { - "node": ">=4" + "node": ">=6" }, "keywords": [ "arraybuffer", @@ -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", diff --git a/test/basic.js b/test/basic.js index 203f3eb..f8845b6 100644 --- a/test/basic.js +++ b/test/basic.js @@ -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() })