-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #361 from mrmlnc/use_async_method_instead_of_stream
perf: use fs.walk instead of fs.walkStream for async provider
- Loading branch information
Showing
4 changed files
with
124 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as assert from 'assert'; | ||
|
||
import { PassThrough } from 'stream'; | ||
import * as sinon from 'sinon'; | ||
import * as fsWalk from '@nodelib/fs.walk'; | ||
import Settings, { Options } from '../settings'; | ||
import { ReaderOptions } from '../types'; | ||
import * as tests from '../tests'; | ||
import ReaderAsync from './async'; | ||
import ReaderStream from './stream'; | ||
|
||
type WalkSignature = typeof fsWalk.walk; | ||
|
||
class TestReader extends ReaderAsync { | ||
protected _walkAsync: WalkSignature = sinon.stub() as unknown as WalkSignature; | ||
protected _readerStream: ReaderStream = sinon.createStubInstance(ReaderStream) as unknown as ReaderStream; | ||
|
||
constructor(options?: Options) { | ||
super(new Settings(options)); | ||
} | ||
|
||
public get walkAsync(): sinon.SinonStub { | ||
return this._walkAsync as unknown as sinon.SinonStub; | ||
} | ||
|
||
public get readerStream(): sinon.SinonStubbedInstance<ReaderStream> { | ||
return this._readerStream as unknown as sinon.SinonStubbedInstance<ReaderStream>; | ||
} | ||
} | ||
|
||
function getReader(options?: Options): TestReader { | ||
return new TestReader(options); | ||
} | ||
|
||
function getReaderOptions(options: Partial<ReaderOptions> = {}): ReaderOptions { | ||
return { ...options } as unknown as ReaderOptions; | ||
} | ||
|
||
describe('Readers → ReaderAsync', () => { | ||
describe('Constructor', () => { | ||
it('should create instance of class', () => { | ||
const reader = getReader(); | ||
|
||
assert.ok(reader instanceof TestReader); | ||
}); | ||
}); | ||
|
||
describe('.dynamic', () => { | ||
it('should call fs.walk method', async () => { | ||
const reader = getReader(); | ||
const readerOptions = getReaderOptions(); | ||
|
||
reader.walkAsync.yields(null, []); | ||
|
||
await reader.dynamic('root', readerOptions); | ||
|
||
assert.ok(reader.walkAsync.called); | ||
}); | ||
}); | ||
|
||
describe('.static', () => { | ||
it('should call stream reader method', async () => { | ||
const entry = tests.entry.builder().path('root/file.txt').build(); | ||
|
||
const reader = getReader(); | ||
const readerOptions = getReaderOptions(); | ||
const readerStream = new PassThrough({ objectMode: true }); | ||
|
||
readerStream.push(entry); | ||
readerStream.push(null); | ||
|
||
reader.readerStream.static.returns(readerStream); | ||
|
||
await reader.static(['a.txt'], readerOptions); | ||
|
||
assert.ok(reader.readerStream.static.called); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as fsWalk from '@nodelib/fs.walk'; | ||
import { Entry, ReaderOptions, Pattern } from '../types'; | ||
import Reader from './reader'; | ||
import ReaderStream from './stream'; | ||
|
||
export default class ReaderAsync extends Reader<Promise<Entry[]>> { | ||
protected _walkAsync: typeof fsWalk.walk = fsWalk.walk; | ||
protected _readerStream: ReaderStream = new ReaderStream(this._settings); | ||
|
||
public dynamic(root: string, options: ReaderOptions): Promise<Entry[]> { | ||
return new Promise((resolve, reject) => { | ||
this._walkAsync(root, options, (error, entries) => { | ||
if (error === null) { | ||
resolve(entries); | ||
} else { | ||
reject(error); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
public async static(patterns: Pattern[], options: ReaderOptions): Promise<Entry[]> { | ||
const entries: Entry[] = []; | ||
|
||
const stream = this._readerStream.static(patterns, options); | ||
|
||
// After #235, replace it with an asynchronous iterator. | ||
return new Promise((resolve, reject) => { | ||
stream.once('error', reject); | ||
stream.on('data', (entry: Entry) => entries.push(entry)); | ||
stream.once('end', () => resolve(entries)); | ||
}); | ||
} | ||
} |