wordlist-iterator
Supports only ES6 or higher.
wordlist-iterator's only export is async generator function which gives you ability to continue iteration of wordlist after breaks, returns and throws in for await...of loop from where you left off, and thus get around the Do not reuse generators problem.
Usage
The only export of wordlist-iterator is wordlistIterator async generator function which accepts two arguments - wordlistPath, WordListIteratorOptions, and returns async Iterable to iterate through the words of wordlist.
Arguments:
- wordlistPath - absolute path to wordlist
- WordListIteratorOptions? - Optional object which takes only property highWaterMark, which will be passed to openReadStream function under the hood, for more details see fs.createReadStream options.
This examples below are very self explanatory:
const wordlistIterator testWordlist = const execute = async { const wordlist = let index = 0 for { console // do some async stuff... if index++ === 2 break // stop on 3rd word if reached. } // do stuff between iterations... // ... await stuff1() // ... stuff2() // do some logs... console // continue iteration from where you left off ( where you break, return or throw ). // in this case from 4th word. for { console if index++ === 4 break // break on 5th word if reached. } console // and so on.}
If you need you can pass iterable to function and after it returns, or throws, you can continue to use your iterable of words from outside. e.g.
const wordlistIterator testWordlist = const execute = async { const wordlist = let currentIterationState let index = 0 // 1. First Iteration. for { console // do some async stuff... if index++ === 2 break // break on 3-rd word. } console // 2. Second Iteration. currentIterationState = wordlist index try const result = await console catch error console // 3. Third Iteration currentIterationState = wordlist index const result = await console // continue to do something... } const innerFunctionSuccess = async { for { console if index++ === 4 break // break on 5-th word if reached. } console return 'Success'} const innerFunctionFail = async { for { console throw 'innerFunctionFail: Error occure' // throw error on 1st iteration. } // This will be reached only if theres no words left to iterate through. console return 'Success'}
Important
To close underlying ReadableStream of wordlist before iteration is finished ensure to call one of the generator methods - generator.next(), generator.return() with truthy value, or generator.throw() with any value ( including case without value ), e.g.
wordlistnexttrue// orwordlist// or as an exception for wordlist.throw().wordlist // truthywordlist // falsywordlist // without value
This will ensure that all underlying resources used by generator are freed.
NOTE: If Iteration is finished this will be done automatically.