Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/io/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ export class Directory extends IOBase {
getFileAsStream(relativeFilePath) {
return this.getPath(relativeFilePath)
.then((filePath) => {
return Promise.resolve(createReadStream(filePath, {
const opt = {
flags: 'r',
encoding: 'utf8',
encoding: null,
autoClose: true,
}).pipe(stripBomStream()));
};
const isAscii =
/\.(?:css|html?|js(?:on)?|mml|svg|txt|x(?:ht)?ml)$/i.test(filePath);
let func;
if (isAscii) {
opt.encoding = 'utf8';
func = createReadStream(filePath, opt).pipe(stripBomStream());
} else {
func = createReadStream(filePath, opt);
}
return Promise.resolve(func);
});
}

Expand Down
Binary file added tests/fixtures/io/dir2/dir3/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 16 additions & 2 deletions tests/io/test.directory.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { EventEmitter } from 'events';

import probeImageSize from 'probe-image-size';

import { Directory } from 'io';

import { unexpectedSuccess } from '../helpers';


describe('Directory.getFiles()', () => {
it('should return cached data when available', () => {
const myDirectory = new Directory('tests/fixtures/io/');
Expand Down Expand Up @@ -112,7 +113,7 @@ describe('Directory._getPath()', () => {
});

describe('Directory.getFileAsStream()', () => {
it('should return a stream', () => {
it('should return a readable stream', () => {
const myDirectory = new Directory('tests/fixtures/io/');
return myDirectory.getFiles()
.then(() => {
Expand Down Expand Up @@ -142,6 +143,19 @@ describe('Directory.getFileAsStream()', () => {
});
});

it('should return a binary stream', () => {
const myDirectory = new Directory('tests/fixtures/io/');
return myDirectory.getFiles()
.then(() => {
return myDirectory.getFileAsStream('dir2/dir3/test.png');
})
.then(probeImageSize)
.then((result) => {
const { mime } = result;
expect(mime).toEqual('image/png');
});
});

it('should reject if file is too big', () => {
const myDirectory = new Directory('tests/fixtures/io/');
const fakeFileMeta = {
Expand Down