0
# is-buffer
1
2
is-buffer provides a lightweight utility function for determining whether an object is a Buffer instance without requiring the full buffer module in browserify environments. It offers cross-platform compatibility between Node.js and browser environments while maintaining zero runtime dependencies and minimal bundle size.
3
4
## Package Information
5
6
- **Package Name**: is-buffer
7
- **Package Type**: npm
8
- **Language**: JavaScript (with TypeScript definitions)
9
- **Installation**: `npm install is-buffer`
10
11
## Core Imports
12
13
```javascript
14
const isBuffer = require('is-buffer');
15
```
16
17
For ESM:
18
19
```javascript
20
import isBuffer from 'is-buffer';
21
```
22
23
For TypeScript:
24
25
```typescript
26
import isBuffer = require('is-buffer');
27
// or modern syntax:
28
import isBuffer from 'is-buffer';
29
```
30
31
## Basic Usage
32
33
```javascript
34
const isBuffer = require('is-buffer');
35
36
// Check various types
37
console.log(isBuffer(Buffer.from('hello'))); // true
38
console.log(isBuffer(Buffer.alloc(4))); // true
39
console.log(isBuffer('string')); // false
40
console.log(isBuffer(null)); // false
41
console.log(isBuffer({})); // false
42
```
43
44
## Capabilities
45
46
### Buffer Detection
47
48
Determines if an object is a Buffer instance by checking the object's constructor and its isBuffer method, providing reliable detection without including the full buffer module.
49
50
```javascript { .api }
51
/**
52
* Determine if an object is a Buffer
53
* @param obj - The object to test
54
* @returns true if the object is a Buffer, false otherwise
55
*/
56
function isBuffer(obj: any): boolean;
57
```
58
59
**Parameters:**
60
- `obj` (any): The object to test for Buffer instance
61
62
**Returns:**
63
- `boolean`: `true` if the object is a Buffer, `false` otherwise
64
65
**Implementation Details:**
66
- Returns `false` immediately if `obj` is `null` or `undefined`
67
- Checks that `obj.constructor` exists and is not `null`
68
- Verifies that `obj.constructor.isBuffer` is a function
69
- Calls `obj.constructor.isBuffer(obj)` to perform the actual Buffer check
70
- Works with both Node.js Buffer and browserify Buffer implementations
71
72
**Supported Buffer Types:**
73
- `Buffer.from()` - Creates Buffer from various sources
74
- `Buffer.alloc()` - Creates zero-filled Buffer of specified size
75
- `Buffer.allocUnsafeSlow()` - Creates uninitialized Buffer
76
- Any object with a constructor implementing the `isBuffer` method
77
78
**Non-Buffer Types (returns false):**
79
- Primitive types: `undefined`, `null`, `string`, `number`, `boolean`
80
- Objects: `{}`, `[]`, functions
81
- Objects with malformed `isBuffer` properties