How to use the createReadStream function from graceful-fs

Find comprehensive JavaScript graceful-fs.createReadStream code examples handpicked from public code repositorys.

graceful-fs.createReadStream is a method used to create a readable stream to read data from a file.

442
443
444
445
446
447
448
449
450
451
452
453
// copy
exports.copyDir = copyDir;
exports.copyFile = copyFile;


// createStream
exports.createReadStream = fs.createReadStream;
exports.createWriteStream = fs.createWriteStream;


// emptyDir
exports.emptyDir = emptyDir;
fork icon9
star icon48
watch icon0

+ 7 other calls in file

113
114
115
116
117
118
119
120
121
122
123


function checkHeader(tarball, params, next, cb0) {
  const debug = params.log
  let error
  let nextResult
  const rstr = fs.createReadStream(tarball)
  const setResult = (err, data) => {
    if (err) {
      // Ensure that any previous error is preserved
      /* istanbul ignore else - difficult to create multiple error case */
fork icon3
star icon4
watch icon1

+ 24 other calls in file

How does graceful-fs.createReadStream work?

graceful-fs.createReadStream is a method in the graceful-fs library that creates a readable stream to read data from a file in a non-blocking way, with graceful error handling and retrying when faced with errors. When a file is opened with this method, the method returns a ReadableStream instance that emits data events as data is read from the file. It takes the file path as the first argument and options for reading the file as the second argument, such as the encoding to use, highWaterMark, and start and end positions. The method internally uses fs.createReadStream, with some additional logic for handling errors and retries.

13
14
15
16
17
18
19
20
21
22
const mkdirAsync = Promise.promisify(fs.mkdir);
const writeFileAsync = Promise.promisify(fs.writeFile);
const appendFileAsync = Promise.promisify(fs.appendFile);
const rmdirAsync = Promise.promisify(fs.rmdir);
const readFileAsync = Promise.promisify(fs.readFile);
const createReadStream = fs.createReadStream;
const createWriteStream = fs.createWriteStream;
const accessSync = fs.accessSync;
const accessAsync = Promise.promisify(fs.access);

fork icon0
star icon1
watch icon0

+ 7 other calls in file

92
93
94
95
96
97
98
99
100
101
    }
  })
}

function copyFile (file, target) {
  var readStream = fs.createReadStream(file.name)
  var writeStream = fs.createWriteStream(target, { mode: file.mode })

  readStream.on('error', onError)
  writeStream.on('error', onError)
fork icon1
star icon0
watch icon0

Ai Example

1
2
3
4
5
const fs = require("graceful-fs");
const readStream = fs.createReadStream("file.txt");
const writeStream = fs.createWriteStream("copy.txt");

readStream.pipe(writeStream);

In this example, the createReadStream method is used to create a readable stream from a file named 'file.txt'. The resulting stream is then piped to a writable stream created by createWriteStream, which writes the contents of the readable stream to a new file named 'copy.txt'.

280
281
282
283
284
285
286
287
288
289
290
function untarStream (tarball, cb) {
  validate('SF', arguments)
  cb = once(cb)


  var stream
  var file = stream = fs.createReadStream(tarball)
  var tounpipe = [file]
  file.on('error', function (er) {
    er = new Error('Error extracting ' + tarball + ' archive: ' + er.message)
    er.code = 'EREADFILE'
fork icon0
star icon1
watch icon1

+ 14 other calls in file

109
110
111
112
113
114
115
116
117

fs.createReadStream(writeStream.__atomicTmp)
  .on('data', function (data, enc) { tmpFileHash.update(data, enc) })
  .on('error', fileHashError)
  .on('end', fileHashComplete)
fs.createReadStream(writeStream.__atomicTarget)
  .on('data', function (data, enc) { targetFileHash.update(data, enc) })
  .on('error', fileHashError)
  .on('end', fileHashComplete)
fork icon0
star icon0
watch icon0

+ 3 other calls in file

7
8
9
10
11
12
13
14
15
16
17
18
19


exports.closeSync = fs.closeSync.bind(fs);
exports.createWriteStream = fs.createWriteStream.bind(fs);


exports.createReadStream = (path, options) => new Promise((resolve, reject) => {
	const read = fs.createReadStream(path, options);


	read.once('error', error => {
		reject(new CpFileError(`Cannot read from \`${path}\`: ${error.message}`, error));
	});
fork icon0
star icon0
watch icon0