0
# Vinyl Buffer
1
2
Vinyl Buffer converts streaming vinyl files to use buffers instead of streams. It's designed for use in build systems like Gulp, providing a simple transform stream that converts file contents from streams to buffers while maintaining the vinyl file format.
3
4
## Package Information
5
6
- **Package Name**: vinyl-buffer
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install vinyl-buffer`
10
11
## Core Imports
12
13
```javascript
14
const vinylBuffer = require('vinyl-buffer');
15
```
16
17
Note: This package uses CommonJS exports only. For ES modules, use dynamic import:
18
19
```javascript
20
const vinylBuffer = (await import('vinyl-buffer')).default;
21
```
22
23
## Basic Usage
24
25
```javascript
26
const browserify = require('browserify');
27
const source = require('vinyl-source-stream');
28
const buffer = require('vinyl-buffer');
29
const gulp = require('gulp');
30
31
gulp.task('build', function() {
32
const bundler = browserify('./index.js');
33
34
return bundler.bundle()
35
.pipe(source('index.js'))
36
.pipe(buffer()) // Convert stream to buffer
37
.pipe(gulp.dest('dist/'));
38
});
39
```
40
41
## Capabilities
42
43
### Stream to Buffer Conversion
44
45
Creates a transform stream that converts streaming vinyl files to use buffers, allowing compatibility with plugins that require buffer-based input.
46
47
```javascript { .api }
48
/**
49
* Creates a transform stream that converts streaming vinyl files to use buffers
50
* @returns {NodeJS.Transform} Through2 object stream that processes vinyl files
51
*/
52
function vinylBuffer(): NodeJS.Transform;
53
```
54
55
**File Processing Behavior:**
56
57
- **Null files** (`file.isNull()`) - Passed through unchanged
58
- **Buffer files** (`file.isBuffer()`) - Passed through unchanged
59
- **Stream files** (`file.isStream()`) - Contents converted to Buffer using 'bl' module
60
61
**Usage Example:**
62
63
```javascript
64
const vinylBuffer = require('vinyl-buffer');
65
const through = require('through2');
66
67
// Create the transform stream
68
const bufferTransform = vinylBuffer();
69
70
// Use in a stream pipeline
71
someVinylStream
72
.pipe(bufferTransform)
73
.pipe(through.obj(function(file, enc, callback) {
74
// file.contents is now a Buffer instead of a Stream
75
console.log('File is buffer:', file.isBuffer()); // true
76
console.log('File is stream:', file.isStream()); // false
77
callback(null, file);
78
}));
79
```
80
81
**Error Handling:**
82
83
The transform stream will emit errors if the buffer conversion process fails, typically due to issues reading from the source stream.
84
85
## Types
86
87
This package works with vinyl file objects:
88
89
```javascript { .api }
90
// Vinyl file interface (from vinyl package)
91
interface VinylFile {
92
isNull(): boolean;
93
isBuffer(): boolean;
94
isStream(): boolean;
95
contents: Buffer | NodeJS.ReadableStream | null;
96
}
97
```