Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 961B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. const fs = require('fs');
  3. const pify = require('pify');
  4. const withOpenFile = require('with-open-file');
  5. const fsReadP = pify(fs.read, {multiArgs: true});
  6. const readChunk = (filePath, startPosition, length) => {
  7. const buffer = Buffer.alloc(length);
  8. return withOpenFile(filePath, 'r', fileDescriptor =>
  9. fsReadP(fileDescriptor, buffer, 0, length, startPosition)
  10. )
  11. .then(([bytesRead, buffer]) => {
  12. if (bytesRead < length) {
  13. buffer = buffer.slice(0, bytesRead);
  14. }
  15. return buffer;
  16. });
  17. };
  18. module.exports = readChunk;
  19. // TODO: Remove this for the next major release
  20. module.exports.default = readChunk;
  21. module.exports.sync = (filePath, startPosition, length) => {
  22. let buffer = Buffer.alloc(length);
  23. const bytesRead = withOpenFile.sync(filePath, 'r', fileDescriptor =>
  24. fs.readSync(fileDescriptor, buffer, 0, length, startPosition)
  25. );
  26. if (bytesRead < length) {
  27. buffer = buffer.slice(0, bytesRead);
  28. }
  29. return buffer;
  30. };