feat(util): stream based fileReader
ADD
packages/util/fileReader.helper.spec.ts
+import * as mockFs from 'mock-fs';
+import { readFile } from './fileReader.helper';
+
+describe('File reader', () => {
+
+ beforeEach(() => {
+ spyOn(console, 'log').and.stub();
+ spyOn(console, 'info').and.stub();
+ spyOn(console, 'error').and.stub();
+ });
+
+ it('#readFile returns error when not found', done => {
+ // given
+ mockFs();
+
+ // when
+ const subscription = readFile('test/url')('index.html');
+
+ // then
+ subscription.subscribe(
+ () => {
+ fail('Exceptions should be thrown');
+ done();
+ },
+ error => {
+ expect(error).toBeDefined();
+ expect(error.message).toBe('File not found');
+ done();
+ }
+ );
+ });
+
+ it('#readFile returns Buffer when found', done => {
+ // given
+ mockFs({ 'test/url': { 'index.html': 'test' } });
+
+ // when
+ const subscription = readFile('test/url')('index.html');
+
+ // then
+ subscription.subscribe(
+ data => {
+ expect(data).toBeDefined();
+ expect(Buffer.isBuffer(data)).toEqual(true);
+ expect(data.toString()).toEqual('test');
+ done();
+ },
+ error => {
+ fail(`Exceptions shouldn't be thrown`);
+ done();
+ }
+ );
+ });
+
+ afterEach(() => mockFs.restore());
+
+});

ADD
packages/util/fileReader.helper.ts
+import * as fs from 'fs';
+import * as path from 'path';
+import { Observable } from 'rxjs';
+import { HttpError, HttpStatus } from '../core/src';
+
+export const readFile = (basePath: string) => (dir: string) =>
+ new Observable<Buffer>(subscriber => {
+ const pathname = path.resolve(basePath, dir);
+
+ fs.readFile(pathname, (err, file) => {
+ if (err && err.code === 'ENOENT') {
+ const error = new HttpError('File not found', HttpStatus.NOT_FOUND);
+ subscriber.error(error);
+ }
+ subscriber.next(file);
+ subscriber.complete();
+ });
+ });

feat(util): stream based fileReader