pax_global_header00006660000000000000000000000064132100542010014476gustar00rootroot0000000000000052 comment=b267712029d0b7e3e5fe30a8426400027076dfe0 node-vasync-2.2.0/000077500000000000000000000000001321005420100137255ustar00rootroot00000000000000node-vasync-2.2.0/.gitignore000066400000000000000000000000331321005420100157110ustar00rootroot00000000000000node_modules npm-debug.log node-vasync-2.2.0/.gitmodules000066400000000000000000000000001321005420100160700ustar00rootroot00000000000000node-vasync-2.2.0/.npmignore000066400000000000000000000000221321005420100157160ustar00rootroot00000000000000node_modules deps node-vasync-2.2.0/CHANGES.md000066400000000000000000000016521321005420100153230ustar00rootroot00000000000000# Changelog ## Not yet released None yet. ## v2.2.0 * #37 want whilst ## v2.1.0 * #33 want filter, filterLimit, and filterSeries * #35 pipeline does not pass rv-object to final callback ## v2.0.0 ** WARNING Do not use this version (v2.0.0), as it has broken pipeline and forEachPipeline functions. **Breaking Changes:** * The `waterfall` function's terminating callback no longer receives a status-object as its second argument. This is the behavior of `node-async` and we wish to match it as closely as possible. If you used the second argument of waterfall's terminating callback (instead of waterfall's return value) to extract job-statuses, this will break you. More specifically, this is only true if you called `waterfall` on an empty array of function. **Other Changes:** * #32 Would like a tryEach function. ## v1 and earlier Major version 1 and earlier did not have their changes logged in a changelog. node-vasync-2.2.0/LICENSE000066400000000000000000000021611321005420100147320ustar00rootroot00000000000000Copyright (c) 2014, Joyent, Inc. All rights reserved. Compatibility tests copyright (c) 2010-2014 Caolan McMahon. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE node-vasync-2.2.0/Makefile000066400000000000000000000011241321005420100153630ustar00rootroot00000000000000# # Copyright (c) 2012, Joyent, Inc. All rights reserved. # # Makefile: top-level Makefile # # This Makefile contains only repo-specific logic and uses included makefiles # to supply common targets (javascriptlint, jsstyle, restdown, etc.), which are # used by other repos as well. # # # Tools must be installed on the path # JSL = jsl JSSTYLE = jsstyle # # Files # JS_FILES := $(shell find lib tests -name '*.js' -not -name compat\*.js) JSL_FILES_NODE = $(JS_FILES) JSSTYLE_FILES = $(JS_FILES) JSL_CONF_NODE = jsl.node.conf all: npm install test: npm test include ./Makefile.targ node-vasync-2.2.0/Makefile.targ000066400000000000000000000203061321005420100163220ustar00rootroot00000000000000# -*- mode: makefile -*- # # Copyright (c) 2012, Joyent, Inc. All rights reserved. # # Makefile.targ: common targets. # # NOTE: This makefile comes from the "eng" repo. It's designed to be dropped # into other repos as-is without requiring any modifications. If you find # yourself changing this file, you should instead update the original copy in # eng.git and then update your repo to use the new version. # # This Makefile defines several useful targets and rules. You can use it by # including it from a Makefile that specifies some of the variables below. # # Targets defined in this Makefile: # # check Checks JavaScript files for lint and style # Checks bash scripts for syntax # Checks SMF manifests for validity against the SMF DTD # # clean Removes built files # # docs Builds restdown documentation in docs/ # # prepush Depends on "check" and "test" # # test Does nothing (you should override this) # # xref Generates cscope (source cross-reference index) # # For details on what these targets are supposed to do, see the Joyent # Engineering Guide. # # To make use of these targets, you'll need to set some of these variables. Any # variables left unset will simply not be used. # # BASH_FILES Bash scripts to check for syntax # (paths relative to top-level Makefile) # # CLEAN_FILES Files to remove as part of the "clean" target. Note # that files generated by targets in this Makefile are # automatically included in CLEAN_FILES. These include # restdown-generated HTML and JSON files. # # DOC_FILES Restdown (documentation source) files. These are # assumed to be contained in "docs/", and must NOT # contain the "docs/" prefix. # # JSL_CONF_NODE Specify JavaScriptLint configuration files # JSL_CONF_WEB (paths relative to top-level Makefile) # # Node.js and Web configuration files are separate # because you'll usually want different global variable # configurations. If no file is specified, none is given # to jsl, which causes it to use a default configuration, # which probably isn't what you want. # # JSL_FILES_NODE JavaScript files to check with Node config file. # JSL_FILES_WEB JavaScript files to check with Web config file. # # You can also override these variables: # # BASH Path to bash (default: bash) # # CSCOPE_DIRS Directories to search for source files for the cscope # index. (default: ".") # # JSL Path to JavaScriptLint (default: "jsl") # # JSL_FLAGS_NODE Additional flags to pass through to JSL # JSL_FLAGS_WEB # JSL_FLAGS # # JSSTYLE Path to jsstyle (default: jsstyle) # # JSSTYLE_FLAGS Additional flags to pass through to jsstyle # # # Defaults for the various tools we use. # BASH ?= bash BASHSTYLE ?= tools/bashstyle CP ?= cp CSCOPE ?= cscope CSCOPE_DIRS ?= . JSL ?= jsl JSSTYLE ?= jsstyle MKDIR ?= mkdir -p MV ?= mv RESTDOWN_FLAGS ?= RMTREE ?= rm -rf JSL_FLAGS ?= --nologo --nosummary ifeq ($(shell uname -s),SunOS) TAR ?= gtar else TAR ?= tar endif # # Defaults for other fixed values. # BUILD = build DISTCLEAN_FILES += $(BUILD) DOC_BUILD = $(BUILD)/docs/public # # Configure JSL_FLAGS_{NODE,WEB} based on JSL_CONF_{NODE,WEB}. # ifneq ($(origin JSL_CONF_NODE), undefined) JSL_FLAGS_NODE += --conf=$(JSL_CONF_NODE) endif ifneq ($(origin JSL_CONF_WEB), undefined) JSL_FLAGS_WEB += --conf=$(JSL_CONF_WEB) endif # # Targets. For descriptions on what these are supposed to do, see the # Joyent Engineering Guide. # # # Instruct make to keep around temporary files. We have rules below that # automatically update git submodules as needed, but they employ a deps/*/.git # temporary file. Without this directive, make tries to remove these .git # directories after the build has completed. # .SECONDARY: $($(wildcard deps/*):%=%/.git) # # This rule enables other rules that use files from a git submodule to have # those files depend on deps/module/.git and have "make" automatically check # out the submodule as needed. # deps/%/.git: git submodule update --init deps/$* # # These recipes make heavy use of dynamically-created phony targets. The parent # Makefile defines a list of input files like BASH_FILES. We then say that each # of these files depends on a fake target called filename.bashchk, and then we # define a pattern rule for those targets that runs bash in check-syntax-only # mode. This mechanism has the nice properties that if you specify zero files, # the rule becomes a noop (unlike a single rule to check all bash files, which # would invoke bash with zero files), and you can check individual files from # the command line with "make filename.bashchk". # .PHONY: check-bash check-bash: $(BASH_FILES:%=%.bashchk) $(BASH_FILES:%=%.bashstyle) %.bashchk: % $(BASH) -n $^ %.bashstyle: % $(BASHSTYLE) $^ .PHONY: check-jsl check-jsl-node check-jsl-web check-jsl: check-jsl-node check-jsl-web check-jsl-node: $(JSL_FILES_NODE:%=%.jslnodechk) check-jsl-web: $(JSL_FILES_WEB:%=%.jslwebchk) %.jslnodechk: % $(JSL_EXEC) $(JSL) $(JSL_FLAGS) $(JSL_FLAGS_NODE) $< %.jslwebchk: % $(JSL_EXEC) $(JSL) $(JSL_FLAGS) $(JSL_FLAGS_WEB) $< .PHONY: check-jsstyle check-jsstyle: $(JSSTYLE_FILES:%=%.jsstylechk) %.jsstylechk: % $(JSSTYLE_EXEC) $(JSSTYLE) $(JSSTYLE_FLAGS) $< .PHONY: check check: check-jsl check-jsstyle check-bash @echo check ok .PHONY: clean clean:: -$(RMTREE) $(CLEAN_FILES) .PHONY: distclean distclean:: clean -$(RMTREE) $(DISTCLEAN_FILES) CSCOPE_FILES = cscope.in.out cscope.out cscope.po.out CLEAN_FILES += $(CSCOPE_FILES) .PHONY: xref xref: cscope.files $(CSCOPE) -bqR .PHONY: cscope.files cscope.files: find $(CSCOPE_DIRS) -name '*.c' -o -name '*.h' -o -name '*.cc' \ -o -name '*.js' -o -name '*.s' -o -name '*.cpp' > $@ # # The "docs" target is complicated because we do several things here: # # (1) Use restdown to build HTML and JSON files from each of DOC_FILES. # # (2) Copy these files into $(DOC_BUILD) (build/docs/public), which # functions as a complete copy of the documentation that could be # mirrored or served over HTTP. # # (3) Then copy any directories and media from docs/media into # $(DOC_BUILD)/media. This allows projects to include their own media, # including files that will override same-named files provided by # restdown. # # Step (3) is the surprisingly complex part: in order to do this, we need to # identify the subdirectories in docs/media, recreate them in # $(DOC_BUILD)/media, then do the same with the files. # DOC_MEDIA_DIRS := $(shell find docs/media -type d 2>/dev/null | grep -v "^docs/media$$") DOC_MEDIA_DIRS := $(DOC_MEDIA_DIRS:docs/media/%=%) DOC_MEDIA_DIRS_BUILD := $(DOC_MEDIA_DIRS:%=$(DOC_BUILD)/media/%) DOC_MEDIA_FILES := $(shell find docs/media -type f 2>/dev/null) DOC_MEDIA_FILES := $(DOC_MEDIA_FILES:docs/media/%=%) DOC_MEDIA_FILES_BUILD := $(DOC_MEDIA_FILES:%=$(DOC_BUILD)/media/%) # # Like the other targets, "docs" just depends on the final files we want to # create in $(DOC_BUILD), leveraging other targets and recipes to define how # to get there. # .PHONY: docs docs: \ $(DOC_FILES:%.restdown=$(DOC_BUILD)/%.html) \ $(DOC_FILES:%.restdown=$(DOC_BUILD)/%.json) \ $(DOC_MEDIA_FILES_BUILD) # # We keep the intermediate files so that the next build can see whether the # files in DOC_BUILD are up to date. # .PRECIOUS: \ $(DOC_FILES:%.restdown=docs/%.html) \ $(DOC_FILES:%.restdown=docs/%json) # # We do clean those intermediate files, as well as all of DOC_BUILD. # CLEAN_FILES += \ $(DOC_BUILD) \ $(DOC_FILES:%.restdown=docs/%.html) \ $(DOC_FILES:%.restdown=docs/%.json) # # Before installing the files, we must make sure the directories exist. The | # syntax tells make that the dependency need only exist, not be up to date. # Otherwise, it might try to rebuild spuriously because the directory itself # appears out of date. # $(DOC_MEDIA_FILES_BUILD): | $(DOC_MEDIA_DIRS_BUILD) $(DOC_BUILD)/%: docs/% | $(DOC_BUILD) $(CP) $< $@ docs/%.json docs/%.html: docs/%.restdown | $(DOC_BUILD) $(RESTDOWN_EXEC) $(RESTDOWN) $(RESTDOWN_FLAGS) -m $(DOC_BUILD) $< $(DOC_BUILD): $(MKDIR) $@ $(DOC_MEDIA_DIRS_BUILD): $(MKDIR) $@ # # The default "test" target does nothing. This should usually be overridden by # the parent Makefile. It's included here so we can define "prepush" without # requiring the repo to define "test". # .PHONY: test test: .PHONY: prepush prepush: check test node-vasync-2.2.0/README.md000066400000000000000000000606331321005420100152140ustar00rootroot00000000000000# vasync: observable asynchronous control flow This module provides several functions for asynchronous control flow. There are many modules that do this already (notably async.js). This one's claim to fame is improved debuggability. ## Observability is important Working with Node's asynchronous, callback-based model is much easier with a handful of simple control-flow abstractions, like: * waterfalls and pipelines (which invoke a list of asynchronous callbacks sequentially) * parallel pipelines (which invoke a list of asynchronous callbacks in parallel and invoke a top-level callback when the last one completes). * queues * barriers But these structures also introduce new types of programming errors: failing to invoke the callback can cause the program to hang, and inadvertently invoking it twice can cause all kinds of mayhem that's very difficult to debug. The functions in this module keep track of what's going on so that you can figure out what happened when your program goes wrong. They generally return an object describing details of the current state. If your program goes wrong, you have several ways of getting at this state: * On illumos-based systems, use MDB to [find the status object](http://dtrace.org/blogs/bmc/2012/05/05/debugging-node-js-memory-leaks/) and then [print it out](http://dtrace.org/blogs/dap/2012/01/13/playing-with-nodev8-postmortem-debugging/). * Provide an HTTP API (or AMQP, or whatever) that returns these pending status objects as JSON (see [kang](https://github.com/davepacheco/kang)). * Incorporate a REPL into your program and print out the status object. * Use the Node debugger to print out the status object. ## Functions * [parallel](#parallel-invoke-n-functions-in-parallel): invoke N functions in parallel (and merge the results) * [forEachParallel](#foreachparallel-invoke-the-same-function-on-n-inputs-in-parallel): invoke the same function on N inputs in parallel * [pipeline](#pipeline-invoke-n-functions-in-series-and-stop-on-failure): invoke N functions in series (and stop on failure) * [tryEach](#tryeach-invoke-n-functions-in-series-and-stop-on-success): invoke N functions in series (and stop on success) * [forEachPipeline](#foreachpipeline-invoke-the-same-function-on-n-inputs-in-series-and-stop-on-failure): invoke the same function on N inputs in series (and stop on failure) * [filter/filterSeries/filterLimit](#filterfilterlimitfilterseries-filter-n-inputs-serially-or-concurrently): filter N inputs serially or concurrently * [whilst](#whilst-invoke-a-function-repeatedly-until-a-stopping-condition-is-met): invoke a function repeatedly until a stopping condition is met * [waterfall](#waterfall-invoke-n-functions-in-series-stop-on-failure-and-propagate-results): like pipeline, but propagating results between stages * [barrier](#barrier-coordinate-multiple-concurrent-operations): coordinate multiple concurrent operations * [queue/queuev](#queuequeuev-fixed-size-worker-queue): fixed-size worker queue ### parallel: invoke N functions in parallel Synopsis: `parallel(args, callback)` This function takes a list of input functions (specified by the "funcs" property of "args") and runs them all. These input functions are expected to be asynchronous: they get a "callback" argument and should invoke it as `callback(err, result)`. The error and result will be saved and made available to the original caller when all of these functions complete. This function returns the same "result" object it passes to the callback, and you can use the fields in this object to debug or observe progress: * `operations`: array corresponding to the input functions, with * `func`: input function, * `status`: "pending", "ok", or "fail", * `err`: returned "err" value, if any, and * `result`: returned "result" value, if any * `successes`: "result" field for each of "operations" where "status" == "ok" (in no particular order) * `ndone`: number of input operations that have completed * `nerrors`: number of input operations that have failed This status object lets you see in a debugger exactly which functions have completed, what they returned, and which ones are outstanding. All errors are combined into a single "err" parameter to the final callback (see below). Example usage: ```js console.log(mod_vasync.parallel({ 'funcs': [ function f1 (callback) { mod_dns.resolve('joyent.com', callback); }, function f2 (callback) { mod_dns.resolve('github.com', callback); }, function f3 (callback) { mod_dns.resolve('asdfaqsdfj.com', callback); } ] }, function (err, results) { console.log('error: %s', err.message); console.log('results: %s', mod_util.inspect(results, null, 3)); })); ``` In the first tick, this outputs: ```js status: { operations: [ { func: [Function: f1], status: 'pending' }, { func: [Function: f2], status: 'pending' }, { func: [Function: f3], status: 'pending' } ], successes: [], ndone: 0, nerrors: 0 } ``` showing that there are three operations pending and none has yet been started. When the program finishes, it outputs this error: error: first of 1 error: queryA ENOTFOUND which encapsulates all of the intermediate failures. This model allows you to write the final callback like you normally would: ```js if (err) return (callback(err)); ``` and still propagate useful information to callers that don't deal with multiple errors (i.e. most callers). The example also prints out the detailed final status, including all of the errors and return values: ```js results: { operations: [ { func: [Function: f1], funcname: 'f1', status: 'ok', err: null, result: [ '165.225.132.33' ] }, { func: [Function: f2], funcname: 'f2', status: 'ok', err: null, result: [ '207.97.227.239' ] }, { func: [Function: f3], funcname: 'f3', status: 'fail', err: { [Error: queryA ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'queryA' }, result: undefined } ], successes: [ [ '165.225.132.33' ], [ '207.97.227.239' ] ], ndone: 3, nerrors: 1 } ``` You can use this if you want to handle all of the errors individually or to get at all of the individual return values. Note that "successes" is provided as a convenience and the order of items in that array may not correspond to the order of the inputs. To consume output in an ordered manner, you should iterate over "operations" and pick out the result from each item. ### forEachParallel: invoke the same function on N inputs in parallel Synopsis: `forEachParallel(args, callback)` This function is exactly like `parallel`, except that the input is specified as a *single* function ("func") and a list of inputs ("inputs"). The function is invoked on each input in parallel. This example is exactly equivalent to the one above: ```js console.log(mod_vasync.forEachParallel({ 'func': mod_dns.resolve, 'inputs': [ 'joyent.com', 'github.com', 'asdfaqsdfj.com' ] }, function (err, results) { console.log('error: %s', err.message); console.log('results: %s', mod_util.inspect(results, null, 3)); })); ``` ### pipeline: invoke N functions in series (and stop on failure) Synopsis: `pipeline(args, callback)` The named arguments (that go inside `args`) are: * `funcs`: input functions, to be invoked in series * `arg`: arbitrary argument that will be passed to each function The functions are invoked in order as `func(arg, callback)`, where "arg" is the user-supplied argument from "args" and "callback" should be invoked in the usual way. If any function emits an error, the whole pipeline stops. The return value and the arguments to the final callback are exactly the same as for `parallel`. The error object for the final callback is just the error returned by whatever pipeline function failed (if any). This example is similar to the one above, except that it runs the steps in sequence and stops early because `pipeline` stops on the first error: ```js console.log(mod_vasync.pipeline({ 'funcs': [ function f1 (_, callback) { mod_fs.stat('/tmp', callback); }, function f2 (_, callback) { mod_fs.stat('/noexist', callback); }, function f3 (_, callback) { mod_fs.stat('/var', callback); } ] }, function (err, results) { console.log('error: %s', err.message); console.log('results: %s', mod_util.inspect(results, null, 3)); })); ``` As a result, the status after the first tick looks like this: ```js { operations: [ { func: [Function: f1], status: 'pending' }, { func: [Function: f2], status: 'waiting' }, { func: [Function: f3], status: 'waiting' } ], successes: [], ndone: 0, nerrors: 0 } ``` Note that the second and third stages are now "waiting", rather than "pending" in the `parallel` case. The error and complete result look just like the parallel case. ### tryEach: invoke N functions in series (and stop on success) Synopsis: `tryEach(funcs, callback)` The `tryEach` function invokes each of the asynchronous functions in `funcs` serially. Each function takes a single argument: an interstitial-callback. `tryEach` will keep calling the functions until one of them succeeds (or they all fail). At the end, the terminating-callback is invoked with the error and/or results provided by the last function that was called (either the last one that failed or the first one that succeeded). This example is similar to the one above, except that it runs the steps in sequence and stops early because `tryEach` stops on the first success: ```js console.log(mod_vasync.tryEach([ function f1 (callback) { mod_fs.stat('/notreal', callback); }, function f2 (callback) { mod_fs.stat('/noexist', callback); }, function f3 (callback) { mod_fs.stat('/var', callback); }, function f4 (callback) { mod_fs.stat('/noexist', callback); } ], function (err, results) { console.log('error: %s', err); console.log('results: %s', mod_util.inspect(results)); })); ``` The above code will stop when it finishes f3, and we will only print a single result and no errors: ```js error: null results: { dev: 65760, mode: 16877, nlink: 41, uid: 0, gid: 3, rdev: -1, blksize: 2560, ino: 11, size: 41, blocks: 7, atime: Thu May 28 2015 16:21:25 GMT+0000 (UTC), mtime: Thu Jan 21 2016 22:08:50 GMT+0000 (UTC), ctime: Thu Jan 21 2016 22:08:50 GMT+0000 (UTC) } ``` If we comment out `f3`, we get the following output: ```js error: Error: ENOENT, stat '/noexist' results: undefined ``` Note that: there is a mismatch (inherited from `async`) between the semantics of the interstitial callback and the sematics of the terminating callback. See the following example: ```js console.log(mod_vasync.tryEach([ function f1 (callback) { callback(new Error()); }, function f2 (callback) { callback(new Error()); }, function f3 (callback) { callback(null, 1, 2, 3); }, function f4 (callback) { callback(null, 1); } ], function (err, results) { console.log('error: %s', err); console.log('results: %s', mod_util.inspect(results)); })); ``` We pass one or more results to the terminating-callback via the interstitial-callback's arglist -- `(err, res1, res2, ...)`. From the callback-implementor's perspective, the results get wrapped up in an array `(err, [res1, res2, ...])` -- unless there is only one result, which simply gets passed through as the terminating callback's second argument. This means that when we call the callback in `f3` above, the terminating callback receives the list `[1, 2, 3]` as its second argument. If, we comment out `f3`, we will end up calling the callback in `f4` which will end up invoking the terminating callback with a single result: `1`, instead of `[1]`. In short, be mindful that there is not always a 1:1 correspondence between the terminating callback that you define, and the interstitial callback that gets called from the function. ### forEachPipeline: invoke the same function on N inputs in series (and stop on failure) Synopsis: `forEachPipeline(args, callback)` This function is exactly like `pipeline`, except that the input is specified as a *single* function ("func") and a list of inputs ("inputs"). The function is invoked on each input in series. This example is exactly equivalent to the one above: ```js console.log(mod_vasync.forEachPipeline({ 'func': mod_dns.resolve, 'inputs': [ 'joyent.com', 'github.com', 'asdfaqsdfj.com' ] }, function (err, results) { console.log('error: %s', err.message); console.log('results: %s', mod_util.inspect(results, null, 3)); })); ``` ### waterfall: invoke N functions in series, stop on failure, and propagate results Synopsis: `waterfall(funcs, callback)` This function works like `pipeline` except for argument passing. Each function is passed any values emitted by the previous function (none for the first function), followed by the callback to invoke upon completion. This callback must be invoked exactly once, regardless of success or failure. As conventional in Node, the first argument to the callback indicates an error (if non-null). Subsequent arguments are passed to the next function in the "funcs" chain. If any function fails (i.e., calls its callback with an Error), then the remaining functions are not invoked and "callback" is invoked with the error. The only difference between waterfall() and pipeline() are the arguments passed to each function in the chain. pipeline() always passes the same argument followed by the callback, while waterfall() passes whatever values were emitted by the previous function followed by the callback. Here's an example: ```js mod_vasync.waterfall([ function func1(callback) { setImmediate(function () { callback(null, 37); }); }, function func2(extra, callback) { console.log('func2 got "%s" from func1', extra); callback(); } ], function () { console.log('done'); }); ``` This prints: ``` func2 got "37" from func1 better stop early ``` ### filter/filterLimit/filterSeries: filter N inputs serially or concurrently Synopsis: `filter(inputs, filterFunc, callback)` Synopsis: `filterSeries(inputs, filterFunc, callback)` Synopsis: `filterLimit(inputs, limit, filterFunc, callback)` These functions take an array (of anything) and a function to call on each element of the array. The function must callback with a true or false value as the second argument or an error object as the first argument. False values will result in the element being filtered out of the results array. An error object passed as the first argument will cause the filter function to stop processing new elements and callback to the caller with the error immediately. Original input array order is maintained. `filter` and `filterSeries` are analogous to calling `filterLimit` with a limit of `Infinity` and `1` respectively. ```js var inputs = [ 'joyent.com', 'github.com', 'asdfaqsdfj.com' ]; function filterFunc(input, cb) { mod_dns.resolve(input, function (err, results) { if (err) { cb(null, false); } else { cb(null, true); } } } mod_vasync.filter(inputs, filterFunc, function (err, results) { // err => undefined // results => ['joyent.com', 'github.com'] }); ``` ### whilst: invoke a function repeatedly until a stopping condition is met Synopsis: `whilst(testFunc, iterateFunc, callback)` Repeatedly invoke `iterateFunc` while `testFunc` returns a true value. `iterateFunc` is an asychronous function that must call its callback (the first and only argument given to it) when it is finished with an optional error object as the first argument, and any other arbitrary arguments. If an error object is given as the first argument, `whilst` will finish and call `callback` with the error object. `testFunc` is a synchronous function that must return a value - if the value resolves to true `whilst` will invoke `iterateFunc`, if it resolves to false `whilst` will finish and invoke `callback` with the last set of arguments `iterateFunc` called back with. `whilst` also returns an object suitable for introspecting the current state of the specific `whilst` invocation which contains the following properties: * `finished`: boolean if this invocation has finished or is in progress * `iterations`: number of iterations performed (calls to `iterateFunc`) Compatible with `async.whilst` ```js var n = 0; var w = mod_vasync.whilst( function testFunc() { return (n < 5); }, function iterateFunc(cb) { n++; cb(null, {n: n}); }, function whilstDone(err, arg) { // err => undefined // arg => {n: 5} // w => {finished: true, iterations: 5} } ); // w => {finished: false, iterations: 0} ``` ### barrier: coordinate multiple concurrent operations Synopsis: `barrier([args])` Returns a new barrier object. Like `parallel`, barriers are useful for coordinating several concurrent operations, but instead of specifying a list of functions to invoke, you just say how many (and optionally which ones) are outstanding, and this object emits `'drain'` when they've all completed. This is syntactically lighter-weight, and more flexible. * Methods: * start(name): Indicates that the named operation began. The name must not match an operation which is already ongoing. * done(name): Indicates that the named operation ended. * Read-only public properties (for debugging): * pending: Set of pending operations. Keys are names passed to "start", and values are timestamps when the operation began. * recent: Array of recent completed operations. Each element is an object with a "name", "start", and "done" field. By default, 10 operations are remembered. * Options: * nrecent: number of recent operations to remember (for debugging) Example: printing sizes of files in a directory ```js var mod_fs = require('fs'); var mod_path = require('path'); var mod_vasync = require('../lib/vasync'); var barrier = mod_vasync.barrier(); barrier.on('drain', function () { console.log('all files checked'); }); barrier.start('readdir'); mod_fs.readdir(__dirname, function (err, files) { barrier.done('readdir'); if (err) throw (err); files.forEach(function (file) { barrier.start('stat ' + file); var path = mod_path.join(__dirname, file); mod_fs.stat(path, function (err2, stat) { barrier.done('stat ' + file); console.log('%s: %d bytes', file, stat['size']); }); }); }); ``` This emits: barrier-readdir.js: 602 bytes foreach-parallel.js: 358 bytes barrier-basic.js: 552 bytes nofail.js: 384 bytes pipeline.js: 490 bytes parallel.js: 481 bytes queue-serializer.js: 441 bytes queue-stat.js: 529 bytes all files checked ### queue/queuev: fixed-size worker queue Synopsis: `queue(worker, concurrency)` Synopsis: `queuev(args)` This function returns an object that allows up to a fixed number of tasks to be dispatched at any given time. The interface is compatible with that provided by the "async" Node library, except that the returned object's fields represent a public interface you can use to introspect what's going on. * Arguments * worker: a function invoked as `worker(task, callback)`, where `task` is a task dispatched to this queue and `callback` should be invoked when the task completes. * concurrency: a positive integer indicating the maximum number of tasks that may be dispatched at any time. With concurrency = 1, the queue serializes all operations. * Methods * push(task, [callback]): add a task (or array of tasks) to the queue, with an optional callback to be invoked when each task completes. If a list of tasks are added, the callback is invoked for each one. * length(): for compatibility with node-async. * close(): signal that no more tasks will be enqueued. Further attempts to enqueue tasks to this queue will throw. Once all pending and queued tasks are completed the object will emit the "end" event. The "end" event is the last event the queue will emit, and it will be emitted even if no tasks were ever enqueued. * kill(): clear enqueued tasks and implicitly close the queue. Several caveats apply when kill() is called: * The completion callback will _not_ be called for items purged from the queue. * The drain handler is cleared (for node-async compatibility) * Subsequent calls to kill() or close() are no-ops. * As with close(), it is not legal to call push() after kill(). * Read-only public properties (for debugging): * concurrency: for compatibility with node-async * worker: worker function, as passed into "queue"/"queuev" * worker\_name: worker function's "name" field * npending: the number of tasks currently being processed * pending: an object (*not* an array) describing the tasks currently being processed * queued: array of tasks currently queued for processing * closed: true when close() has been called on the queue * ended: true when all tasks have completed processing, and no more processing will occur * killed: true when kill() has been called on the queue * Hooks (for compatibility with node-async): * saturated * empty * drain * Events * 'end': see close() If the tasks are themselves simple objects, then the entire queue may be serialized (as via JSON.stringify) for debugging and monitoring tools. Using the above fields, you can see what this queue is doing (worker\_name), which tasks are queued, which tasks are being processed, and so on. ### Example 1: Stat several files Here's an example demonstrating the queue: ```js var mod_fs = require('fs'); var mod_vasync = require('../lib/vasync'); var queue; function doneOne() { console.log('task completed; queue state:\n%s\n', JSON.stringify(queue, null, 4)); } queue = mod_vasync.queue(mod_fs.stat, 2); console.log('initial queue state:\n%s\n', JSON.stringify(queue, null, 4)); queue.push('/tmp/file1', doneOne); queue.push('/tmp/file2', doneOne); queue.push('/tmp/file3', doneOne); queue.push('/tmp/file4', doneOne); console.log('all tasks dispatched:\n%s\n', JSON.stringify(queue, null, 4)); ``` The initial queue state looks like this: ```js initial queue state: { "nextid": 0, "worker_name": "anon", "npending": 0, "pending": {}, "queued": [], "concurrency": 2 } ``` After four tasks have been pushed, we see that two of them have been dispatched and the remaining two are queued up: ```js all tasks pushed: { "nextid": 4, "worker_name": "anon", "npending": 2, "pending": { "1": { "id": 1, "task": "/tmp/file1" }, "2": { "id": 2, "task": "/tmp/file2" } }, "queued": [ { "id": 3, "task": "/tmp/file3" }, { "id": 4, "task": "/tmp/file4" } ], "concurrency": 2 } ``` As they complete, we see tasks moving from "queued" to "pending", and completed tasks disappear: ```js task completed; queue state: { "nextid": 4, "worker_name": "anon", "npending": 1, "pending": { "3": { "id": 3, "task": "/tmp/file3" } }, "queued": [ { "id": 4, "task": "/tmp/file4" } ], "concurrency": 2 } ``` When all tasks have completed, the queue state looks like it started: ```js task completed; queue state: { "nextid": 4, "worker_name": "anon", "npending": 0, "pending": {}, "queued": [], "concurrency": 2 } ``` ### Example 2: A simple serializer You can use a queue with concurrency 1 and where the tasks are themselves functions to ensure that an arbitrary asynchronous function never runs concurrently with another one, no matter what each one does. Since the tasks are the actual functions to be invoked, the worker function just invokes each one: ```js var mod_vasync = require('../lib/vasync'); var queue = mod_vasync.queue( function (task, callback) { task(callback); }, 1); queue.push(function (callback) { console.log('first task begins'); setTimeout(function () { console.log('first task ends'); callback(); }, 500); }); queue.push(function (callback) { console.log('second task begins'); process.nextTick(function () { console.log('second task ends'); callback(); }); }); ``` This example outputs: $ node examples/queue-serializer.js first task begins first task ends second task begins second task ends node-vasync-2.2.0/examples/000077500000000000000000000000001321005420100155435ustar00rootroot00000000000000node-vasync-2.2.0/examples/barrier-basic.js000066400000000000000000000010501321005420100206020ustar00rootroot00000000000000var mod_vasync = require('../lib/vasync'); var barrier = mod_vasync.barrier(); barrier.on('drain', function () { console.log('barrier drained!'); }); console.log('barrier', barrier); barrier.start('op1'); console.log('op1 started', barrier); barrier.start('op2'); console.log('op2 started', barrier); barrier.done('op2'); console.log('op2 done', barrier); barrier.done('op1'); console.log('op1 done', barrier); barrier.start('op3'); console.log('op3 started'); setTimeout(function () { barrier.done('op3'); console.log('op3 done'); }, 10); node-vasync-2.2.0/examples/barrier-readdir.js000066400000000000000000000011321321005420100211340ustar00rootroot00000000000000var mod_fs = require('fs'); var mod_path = require('path'); var mod_vasync = require('../lib/vasync'); var barrier = mod_vasync.barrier(); barrier.on('drain', function () { console.log('all files checked'); }); barrier.start('readdir'); mod_fs.readdir(__dirname, function (err, files) { barrier.done('readdir'); if (err) throw (err); files.forEach(function (file) { barrier.start('stat ' + file); var path = mod_path.join(__dirname, file); mod_fs.stat(path, function (err2, stat) { barrier.done('stat ' + file); console.log('%s: %d bytes', file, stat['size']); }); }); }); node-vasync-2.2.0/examples/foreach-parallel.js000066400000000000000000000005721321005420100213060ustar00rootroot00000000000000var mod_dns = require('dns'); var mod_util = require('util'); var mod_vasync = require('../lib/vasync'); console.log(mod_vasync.forEachParallel({ 'func': mod_dns.resolve, 'inputs': [ 'joyent.com', 'github.com', 'asdfaqsdfj.com' ] }, function (err, results) { console.log('error: %s', err.message); console.log('results: %s', mod_util.inspect(results, null, 3)); })); node-vasync-2.2.0/examples/foreach-pipeline.js000066400000000000000000000005721321005420100213170ustar00rootroot00000000000000var mod_dns = require('dns'); var mod_util = require('util'); var mod_vasync = require('../lib/vasync'); console.log(mod_vasync.forEachPipeline({ 'func': mod_dns.resolve, 'inputs': [ 'joyent.com', 'github.com', 'asdfaqsdfj.com' ] }, function (err, results) { console.log('error: %s', err.message); console.log('results: %s', mod_util.inspect(results, null, 3)); })); node-vasync-2.2.0/examples/nofail.js000066400000000000000000000006001321005420100173450ustar00rootroot00000000000000var mod_vasync = require('../lib/vasync'); var mod_util = require('util'); var mod_fs = require('fs'); var status = mod_vasync.parallel({ funcs: [ function f1 (callback) { mod_fs.stat('/tmp', callback); }, function f2 (callback) { mod_fs.stat('/var', callback); } ] }, function (err, results) { console.log(err); console.log(mod_util.inspect(results, false, 8)); }); node-vasync-2.2.0/examples/parallel.js000066400000000000000000000010011321005420100176650ustar00rootroot00000000000000var mod_dns = require('dns'); var mod_util = require('util'); var mod_vasync = require('../lib/vasync'); console.log(mod_vasync.parallel({ 'funcs': [ function f1 (callback) { mod_dns.resolve('joyent.com', callback); }, function f2 (callback) { mod_dns.resolve('github.com', callback); }, function f3 (callback) { mod_dns.resolve('asdfaqsdfj.com', callback); } ] }, function (err, results) { console.log('error: %s', err.message); console.log('results: %s', mod_util.inspect(results, null, 3)); })); node-vasync-2.2.0/examples/pipeline.js000066400000000000000000000010121321005420100177000ustar00rootroot00000000000000var mod_dns = require('dns'); var mod_util = require('util'); var mod_vasync = require('../lib/vasync'); console.log(mod_vasync.pipeline({ 'funcs': [ function f1 (_, callback) { mod_dns.resolve('joyent.com', callback); }, function f2 (_, callback) { mod_dns.resolve('github.com', callback); }, function f3 (_, callback) { mod_dns.resolve('asdfaqsdfj.com', callback); } ] }, function (err, results) { console.log('error: %s', err.message); console.log('results: %s', mod_util.inspect(results, null, 3)); })); node-vasync-2.2.0/examples/queue-serializer.js000066400000000000000000000006711321005420100214000ustar00rootroot00000000000000var mod_vasync = require('../lib/vasync'); var queue = mod_vasync.queue(function (task, callback) { task(callback); }, 1); queue.push(function (callback) { console.log('first task begins'); setTimeout(function () { console.log('first task ends'); callback(); }, 500); }); queue.push(function (callback) { console.log('second task begins'); process.nextTick(function () { console.log('second task ends'); callback(); }); }); node-vasync-2.2.0/examples/queue-stat.js000066400000000000000000000010211321005420100201700ustar00rootroot00000000000000var mod_fs = require('fs'); var mod_vasync = require('../lib/vasync'); var queue; function doneOne() { console.log('task completed; queue state:\n%s\n', JSON.stringify(queue, null, 4)); } queue = mod_vasync.queue(mod_fs.stat, 2); console.log('initial queue state:\n%s\n', JSON.stringify(queue, null, 4)); queue.push('/tmp/file1', doneOne); queue.push('/tmp/file2', doneOne); queue.push('/tmp/file3', doneOne); queue.push('/tmp/file4', doneOne); console.log('all tasks pushed:\n%s\n', JSON.stringify(queue, null, 4)); node-vasync-2.2.0/examples/waterfall.js000066400000000000000000000005451321005420100200660ustar00rootroot00000000000000/* * examples/waterfall.js: simple waterfall example */ var mod_vasync = require('..'); mod_vasync.waterfall([ function func1(callback) { setImmediate(function () { callback(null, 37); }); }, function func2(extra, callback) { console.log('func2 got "%s" from func1', extra); callback(); } ], function () { console.log('done'); }); node-vasync-2.2.0/examples/whilst.js000066400000000000000000000006311321005420100174130ustar00rootroot00000000000000var mod_vasync = require('../lib/vasync'); var n = 0; var w = mod_vasync.whilst( function testFunc() { return (n < 5); }, function iterateFunc(cb) { n++; cb(null, {n: n}); }, function whilstDone(err, arg) { console.log('err: %j', err); console.log('arg: %j', arg); console.log('w (end): %j', w); } ); console.log('w (start): %j', w); node-vasync-2.2.0/jsl.node.conf000066400000000000000000000155331321005420100163170ustar00rootroot00000000000000# # Configuration File for JavaScript Lint # # This configuration file can be used to lint a collection of scripts, or to enable # or disable warnings for scripts that are linted via the command line. # ### Warnings # Enable or disable warnings based on requirements. # Use "+WarningName" to display or "-WarningName" to suppress. # +ambiguous_else_stmt # the else statement could be matched with one of multiple if statements (use curly braces to indicate intent +ambiguous_nested_stmt # block statements containing block statements should use curly braces to resolve ambiguity +ambiguous_newline # unexpected end of line; it is ambiguous whether these lines are part of the same statement +anon_no_return_value # anonymous function does not always return value +assign_to_function_call # assignment to a function call -block_without_braces # block statement without curly braces +comma_separated_stmts # multiple statements separated by commas (use semicolons?) +comparison_type_conv # comparisons against null, 0, true, false, or an empty string allowing implicit type conversion (use === or !==) +default_not_at_end # the default case is not at the end of the switch statement +dup_option_explicit # duplicate "option explicit" control comment +duplicate_case_in_switch # duplicate case in switch statement +duplicate_formal # duplicate formal argument {name} +empty_statement # empty statement or extra semicolon +identifier_hides_another # identifer {name} hides an identifier in a parent scope -inc_dec_within_stmt # increment (++) and decrement (--) operators used as part of greater statement +incorrect_version # Expected /*jsl:content-type*/ control comment. The script was parsed with the wrong version. +invalid_fallthru # unexpected "fallthru" control comment +invalid_pass # unexpected "pass" control comment +jsl_cc_not_understood # couldn't understand control comment using /*jsl:keyword*/ syntax +leading_decimal_point # leading decimal point may indicate a number or an object member +legacy_cc_not_understood # couldn't understand control comment using /*@keyword@*/ syntax +meaningless_block # meaningless block; curly braces have no impact +mismatch_ctrl_comments # mismatched control comment; "ignore" and "end" control comments must have a one-to-one correspondence +misplaced_regex # regular expressions should be preceded by a left parenthesis, assignment, colon, or comma +missing_break # missing break statement +missing_break_for_last_case # missing break statement for last case in switch +missing_default_case # missing default case in switch statement +missing_option_explicit # the "option explicit" control comment is missing +missing_semicolon # missing semicolon +missing_semicolon_for_lambda # missing semicolon for lambda assignment +multiple_plus_minus # unknown order of operations for successive plus (e.g. x+++y) or minus (e.g. x---y) signs +nested_comment # nested comment +no_return_value # function {name} does not always return a value +octal_number # leading zeros make an octal number +parseint_missing_radix # parseInt missing radix parameter +partial_option_explicit # the "option explicit" control comment, if used, must be in the first script tag +redeclared_var # redeclaration of {name} +trailing_comma_in_array # extra comma is not recommended in array initializers +trailing_decimal_point # trailing decimal point may indicate a number or an object member +undeclared_identifier # undeclared identifier: {name} +unreachable_code # unreachable code -unreferenced_argument # argument declared but never referenced: {name} -unreferenced_function # function is declared but never referenced: {name} +unreferenced_variable # variable is declared but never referenced: {name} +unsupported_version # JavaScript {version} is not supported +use_of_label # use of label +useless_assign # useless assignment +useless_comparison # useless comparison; comparing identical expressions -useless_quotes # the quotation marks are unnecessary +useless_void # use of the void type may be unnecessary (void is always undefined) +var_hides_arg # variable {name} hides argument +want_assign_or_call # expected an assignment or function call +with_statement # with statement hides undeclared variables; use temporary variable instead ### Output format # Customize the format of the error message. # __FILE__ indicates current file path # __FILENAME__ indicates current file name # __LINE__ indicates current line # __COL__ indicates current column # __ERROR__ indicates error message (__ERROR_PREFIX__: __ERROR_MSG__) # __ERROR_NAME__ indicates error name (used in configuration file) # __ERROR_PREFIX__ indicates error prefix # __ERROR_MSG__ indicates error message # # For machine-friendly output, the output format can be prefixed with # "encode:". If specified, all items will be encoded with C-slashes. # # Visual Studio syntax (default): +output-format __FILE__(__LINE__): __ERROR__ # Alternative syntax: #+output-format __FILE__:__LINE__: __ERROR__ ### Context # Show the in-line position of the error. # Use "+context" to display or "-context" to suppress. # +context ### Control Comments # Both JavaScript Lint and the JScript interpreter confuse each other with the syntax for # the /*@keyword@*/ control comments and JScript conditional comments. (The latter is # enabled in JScript with @cc_on@). The /*jsl:keyword*/ syntax is preferred for this reason, # although legacy control comments are enabled by default for backward compatibility. # -legacy_control_comments ### Defining identifiers # By default, "option explicit" is enabled on a per-file basis. # To enable this for all files, use "+always_use_option_explicit" -always_use_option_explicit # Define certain identifiers of which the lint is not aware. # (Use this in conjunction with the "undeclared identifier" warning.) # # Common uses for webpages might be: +define __dirname +define clearInterval +define clearTimeout +define console +define exports +define global +define process +define require +define setImmediate +define setInterval +define setTimeout +define Buffer +define JSON +define Math ### JavaScript Version # To change the default JavaScript version: #+default-type text/javascript;version=1.5 #+default-type text/javascript;e4x=1 ### Files # Specify which files to lint # Use "+recurse" to enable recursion (disabled by default). # To add a set of files, use "+process FileName", "+process Folder\Path\*.js", # or "+process Folder\Path\*.htm". # node-vasync-2.2.0/lib/000077500000000000000000000000001321005420100144735ustar00rootroot00000000000000node-vasync-2.2.0/lib/vasync.js000066400000000000000000000567661321005420100163600ustar00rootroot00000000000000/* * vasync.js: utilities for observable asynchronous control flow */ var mod_assert = require('assert'); var mod_events = require('events'); var mod_util = require('util'); var mod_verror = require('verror'); /* * Public interface */ exports.parallel = parallel; exports.forEachParallel = forEachParallel; exports.pipeline = pipeline; exports.tryEach = tryEach; exports.forEachPipeline = forEachPipeline; exports.filter = filter; exports.filterLimit = filterLimit; exports.filterSeries = filterSeries; exports.whilst = whilst; exports.queue = queue; exports.queuev = queuev; exports.barrier = barrier; exports.waterfall = waterfall; if (!global.setImmediate) { global.setImmediate = function (func) { var args = Array.prototype.slice.call(arguments, 1); args.unshift(0); args.unshift(func); setTimeout.apply(this, args); }; } /* * This is incorporated here from jsprim because jsprim ends up pulling in a lot * of dependencies. If we end up needing more from jsprim, though, we should * add it back and rip out this function. */ function isEmpty(obj) { var key; for (key in obj) return (false); return (true); } /* * Given a set of functions that complete asynchronously using the standard * callback(err, result) pattern, invoke them all and merge the results. See * README.md for details. */ function parallel(args, callback) { var funcs, rv, doneOne, i; mod_assert.equal(typeof (args), 'object', '"args" must be an object'); mod_assert.ok(Array.isArray(args['funcs']), '"args.funcs" must be specified and must be an array'); mod_assert.equal(typeof (callback), 'function', 'callback argument must be specified and must be a function'); funcs = args['funcs'].slice(0); rv = { 'operations': new Array(funcs.length), 'successes': [], 'ndone': 0, 'nerrors': 0 }; if (funcs.length === 0) { setImmediate(function () { callback(null, rv); }); return (rv); } doneOne = function (entry) { return (function (err, result) { mod_assert.equal(entry['status'], 'pending'); entry['err'] = err; entry['result'] = result; entry['status'] = err ? 'fail' : 'ok'; if (err) rv['nerrors']++; else rv['successes'].push(result); if (++rv['ndone'] < funcs.length) return; var errors = rv['operations'].filter(function (ent) { return (ent['status'] == 'fail'); }).map(function (ent) { return (ent['err']); }); if (errors.length > 0) callback(new mod_verror.MultiError(errors), rv); else callback(null, rv); }); }; for (i = 0; i < funcs.length; i++) { rv['operations'][i] = { 'func': funcs[i], 'funcname': funcs[i].name || '(anon)', 'status': 'pending' }; funcs[i](doneOne(rv['operations'][i])); } return (rv); } /* * Exactly like parallel, except that the input is specified as a single * function to invoke on N different inputs (rather than N functions). "args" * must have the following fields: * * func asynchronous function to invoke on each input value * * inputs array of input values */ function forEachParallel(args, callback) { var func, funcs; mod_assert.equal(typeof (args), 'object', '"args" must be an object'); mod_assert.equal(typeof (args['func']), 'function', '"args.func" must be specified and must be a function'); mod_assert.ok(Array.isArray(args['inputs']), '"args.inputs" must be specified and must be an array'); func = args['func']; funcs = args['inputs'].map(function (input) { return (function (subcallback) { return (func(input, subcallback)); }); }); return (parallel({ 'funcs': funcs }, callback)); } /* * Like parallel, but invokes functions in sequence rather than in parallel * and aborts if any function exits with failure. Arguments include: * * funcs invoke the functions in parallel * * arg first argument to each pipeline function */ function pipeline(args, callback) { mod_assert.equal(typeof (args), 'object', '"args" must be an object'); mod_assert.ok(Array.isArray(args['funcs']), '"args.funcs" must be specified and must be an array'); var opts = { 'funcs': args['funcs'].slice(0), 'callback': callback, 'args': { impl: 'pipeline', uarg: args['arg'] }, 'stop_when': 'error', 'res_type': 'rv' }; return (waterfall_impl(opts)); } function tryEach(funcs, callback) { mod_assert.ok(Array.isArray(funcs), '"funcs" must be specified and must be an array'); mod_assert.ok(arguments.length == 1 || typeof (callback) == 'function', '"callback" must be a function'); var opts = { 'funcs': funcs.slice(0), 'callback': callback, 'args': { impl: 'tryEach' }, 'stop_when': 'success', 'res_type': 'array' }; return (waterfall_impl(opts)); } /* * Exactly like pipeline, except that the input is specified as a single * function to invoke on N different inputs (rather than N functions). "args" * must have the following fields: * * func asynchronous function to invoke on each input value * * inputs array of input values */ function forEachPipeline(args, callback) { mod_assert.equal(typeof (args), 'object', '"args" must be an object'); mod_assert.equal(typeof (args['func']), 'function', '"args.func" must be specified and must be a function'); mod_assert.ok(Array.isArray(args['inputs']), '"args.inputs" must be specified and must be an array'); mod_assert.equal(typeof (callback), 'function', 'callback argument must be specified and must be a function'); var func = args['func']; var funcs = args['inputs'].map(function (input) { return (function (_, subcallback) { return (func(input, subcallback)); }); }); return (pipeline({'funcs': funcs}, callback)); } /* * async.js compatible filter, filterLimit, and filterSeries. Takes an input * array, optionally a limit, and a single function to filter an array and will * callback with a new filtered array. This is effectively an asynchronous * version of Array.prototype.filter. */ function filter(inputs, filterFunc, callback) { return (filterLimit(inputs, Infinity, filterFunc, callback)); } function filterSeries(inputs, filterFunc, callback) { return (filterLimit(inputs, 1, filterFunc, callback)); } function filterLimit(inputs, limit, filterFunc, callback) { mod_assert.ok(Array.isArray(inputs), '"inputs" must be specified and must be an array'); mod_assert.equal(typeof (limit), 'number', '"limit" must be a number'); mod_assert.equal(isNaN(limit), false, '"limit" must be a number'); mod_assert.equal(typeof (filterFunc), 'function', '"filterFunc" must be specified and must be a function'); mod_assert.equal(typeof (callback), 'function', '"callback" argument must be specified as a function'); var errors = []; var q = queue(processInput, limit); var results = []; function processInput(input, cb) { /* * If the errors array has any members, an error was * encountered in a previous invocation of filterFunc, so all * future filtering will be skipped. */ if (errors.length > 0) { cb(); return; } filterFunc(input.elem, function inputFiltered(err, ans) { /* * We ensure here that a filterFunc callback is only * ever invoked once. */ if (results.hasOwnProperty(input.idx)) { throw (new mod_verror.VError( 'vasync.filter*: filterFunc idx %d ' + 'invoked its callback twice', input.idx)); } /* * The original element, as well as the answer "ans" * (truth value) is stored to later be filtered when * all outstanding jobs are finished. */ results[input.idx] = { elem: input.elem, ans: !!ans }; /* * Any error encountered while filtering will result in * all future operations being skipped, and the error * object being returned in the users callback. */ if (err) { errors.push(err); cb(); return; } cb(); }); } q.once('end', function queueDrained() { if (errors.length > 0) { callback(mod_verror.errorFromList(errors)); return; } /* * results is now an array of objects in the same order of the * inputs array, where each object looks like: * * { * "ans": , * "elem": * } * * we filter out elements that have a false "ans" value, and * then map the array to contain only the input elements. */ results = results.filter(function filterFalseInputs(input) { return (input.ans); }).map(function mapInputElements(input) { return (input.elem); }); callback(null, results); }); inputs.forEach(function iterateInput(elem, idx) { /* * We retain the array index to ensure that order is * maintained. */ q.push({ elem: elem, idx: idx }); }); q.close(); return (q); } /* * async-compatible "whilst" function, with a few notable exceptions/addons. * * 1. More strict typing of arguments (functions *must* be supplied). * 2. A callback function is required, not optional. * 3. An object is returned, not undefined. */ function whilst(testFunc, iterateFunc, callback) { mod_assert.equal(typeof (testFunc), 'function', '"testFunc" must be specified and must be a function'); mod_assert.equal(typeof (iterateFunc), 'function', '"iterateFunc" must be specified and must be a function'); mod_assert.equal(typeof (callback), 'function', '"callback" argument must be specified as a function'); /* * The object returned to the caller that provides a read-only * interface to introspect this specific invocation of "whilst". */ var o = { 'finished': false, 'iterations': 0 }; /* * Store the last set of arguments from the final call to "iterateFunc". * The arguments will be passed to the final callback when an error is * encountered or when the testFunc returns false. */ var args = []; function iterate() { var shouldContinue = testFunc(); if (!shouldContinue) { /* * The test condition is false - break out of the loop. */ done(); return; } /* Bump iterations after testFunc but before iterateFunc. */ o.iterations++; iterateFunc(function whilstIteration(err) { /* Store the latest set of arguments seen. */ args = Array.prototype.slice.call(arguments); /* Any error with iterateFunc will break the loop. */ if (err) { done(); return; } /* Try again. */ setImmediate(iterate); }); } function done() { mod_assert.ok(!o.finished, 'whilst already finished'); o.finished = true; callback.apply(this, args); } setImmediate(iterate); return (o); } /* * async-compatible "queue" function. */ function queue(worker, concurrency) { return (new WorkQueue({ 'worker': worker, 'concurrency': concurrency })); } function queuev(args) { return (new WorkQueue(args)); } function WorkQueue(args) { mod_assert.ok(args.hasOwnProperty('worker')); mod_assert.equal(typeof (args['worker']), 'function'); mod_assert.ok(args.hasOwnProperty('concurrency')); mod_assert.equal(typeof (args['concurrency']), 'number'); mod_assert.equal(Math.floor(args['concurrency']), args['concurrency']); mod_assert.ok(args['concurrency'] > 0); mod_events.EventEmitter.call(this); this.nextid = 0; this.worker = args['worker']; this.worker_name = args['worker'].name || 'anon'; this.npending = 0; this.pending = {}; this.queued = []; this.closed = false; this.ended = false; /* user-settable fields inherited from "async" interface */ this.concurrency = args['concurrency']; this.saturated = undefined; this.empty = undefined; this.drain = undefined; } mod_util.inherits(WorkQueue, mod_events.EventEmitter); WorkQueue.prototype.push = function (tasks, callback) { if (!Array.isArray(tasks)) return (this.pushOne(tasks, callback)); var wq = this; return (tasks.map(function (task) { return (wq.pushOne(task, callback)); })); }; WorkQueue.prototype.updateConcurrency = function (concurrency) { if (this.closed) throw new mod_verror.VError( 'update concurrency invoked after queue closed'); this.concurrency = concurrency; this.dispatchNext(); }; WorkQueue.prototype.close = function () { var wq = this; if (wq.closed) return; wq.closed = true; /* * If the queue is already empty, just fire the "end" event on the * next tick. */ if (wq.npending === 0 && wq.queued.length === 0) { setImmediate(function () { if (!wq.ended) { wq.ended = true; wq.emit('end'); } }); } }; /* private */ WorkQueue.prototype.pushOne = function (task, callback) { if (this.closed) throw new mod_verror.VError('push invoked after queue closed'); var id = ++this.nextid; var entry = { 'id': id, 'task': task, 'callback': callback }; this.queued.push(entry); this.dispatchNext(); return (id); }; /* private */ WorkQueue.prototype.dispatchNext = function () { var wq = this; if (wq.npending === 0 && wq.queued.length === 0) { if (wq.drain) wq.drain(); wq.emit('drain'); /* * The queue is closed; emit the final "end" * event before we come to rest: */ if (wq.closed) { wq.ended = true; wq.emit('end'); } } else if (wq.queued.length > 0) { while (wq.queued.length > 0 && wq.npending < wq.concurrency) { var next = wq.queued.shift(); wq.dispatch(next); if (wq.queued.length === 0) { if (wq.empty) wq.empty(); wq.emit('empty'); } } } }; WorkQueue.prototype.dispatch = function (entry) { var wq = this; mod_assert.ok(!this.pending.hasOwnProperty(entry['id'])); mod_assert.ok(this.npending < this.concurrency); mod_assert.ok(!this.ended); this.npending++; this.pending[entry['id']] = entry; if (this.npending === this.concurrency) { if (this.saturated) this.saturated(); this.emit('saturated'); } /* * We invoke the worker function on the next tick so that callers can * always assume that the callback is NOT invoked during the call to * push() even if the queue is not at capacity. It also avoids O(n) * stack usage when used with synchronous worker functions. */ setImmediate(function () { wq.worker(entry['task'], function (err) { --wq.npending; delete (wq.pending[entry['id']]); if (entry['callback']) entry['callback'].apply(null, arguments); wq.dispatchNext(); }); }); }; WorkQueue.prototype.length = function () { return (this.queued.length); }; WorkQueue.prototype.kill = function () { this.killed = true; this.queued = []; this.drain = undefined; this.close(); }; /* * Barriers coordinate multiple concurrent operations. */ function barrier(args) { return (new Barrier(args)); } function Barrier(args) { mod_assert.ok(!args || !args['nrecent'] || typeof (args['nrecent']) == 'number', '"nrecent" must have type "number"'); mod_events.EventEmitter.call(this); var nrecent = args && args['nrecent'] ? args['nrecent'] : 10; if (nrecent > 0) { this.nrecent = nrecent; this.recent = []; } this.pending = {}; this.scheduled = false; } mod_util.inherits(Barrier, mod_events.EventEmitter); Barrier.prototype.start = function (name) { mod_assert.ok(!this.pending.hasOwnProperty(name), 'operation "' + name + '" is already pending'); this.pending[name] = Date.now(); }; Barrier.prototype.done = function (name) { mod_assert.ok(this.pending.hasOwnProperty(name), 'operation "' + name + '" is not pending'); if (this.recent) { this.recent.push({ 'name': name, 'start': this.pending[name], 'done': Date.now() }); if (this.recent.length > this.nrecent) this.recent.shift(); } delete (this.pending[name]); /* * If we executed at least one operation and we're now empty, we should * emit "drain". But most code doesn't deal well with events being * processed while they're executing, so we actually schedule this event * for the next tick. * * We use the "scheduled" flag to avoid emitting multiple "drain" events * on consecutive ticks if the user starts and ends another task during * this tick. */ if (!isEmpty(this.pending) || this.scheduled) return; this.scheduled = true; var self = this; setImmediate(function () { self.scheduled = false; /* * It's also possible that the user has started another task on * the previous tick, in which case we really shouldn't emit * "drain". */ if (isEmpty(self.pending)) self.emit('drain'); }); }; /* * waterfall([ funcs ], callback): invoke each of the asynchronous functions * "funcs" in series. Each function is passed any values emitted by the * previous function (none for the first function), followed by the callback to * invoke upon completion. This callback must be invoked exactly once, * regardless of success or failure. As conventional in Node, the first * argument to the callback indicates an error (if non-null). Subsequent * arguments are passed to the next function in the "funcs" chain. * * If any function fails (i.e., calls its callback with an Error), then the * remaining functions are not invoked and "callback" is invoked with the error. * * The only difference between waterfall() and pipeline() are the arguments * passed to each function in the chain. pipeline() always passes the same * argument followed by the callback, while waterfall() passes whatever values * were emitted by the previous function followed by the callback. */ function waterfall(funcs, callback) { mod_assert.ok(Array.isArray(funcs), '"funcs" must be specified and must be an array'); mod_assert.ok(arguments.length == 1 || typeof (callback) == 'function', '"callback" must be a function'); var opts = { 'funcs': funcs.slice(0), 'callback': callback, 'args': { impl: 'waterfall' }, 'stop_when': 'error', 'res_type': 'values' }; return (waterfall_impl(opts)); } /* * This function is used to implement vasync-functions that need to execute a * list of functions in a sequence, but differ in how they make use of the * intermediate callbacks and finall callback, as well as under what conditions * they stop executing the functions in the list. Examples of such functions * are `pipeline`, `waterfall`, and `tryEach`. See the documentation for those * functions to see how they operate. * * This function's behavior is influenced via the `opts` object that we pass * in. This object has the following layout: * * { * 'funcs': array of functions * 'callback': the final callback * 'args': { * 'impl': 'pipeline' or 'tryEach' or 'waterfall' * 'uarg': the arg passed to each func for 'pipeline' * } * 'stop_when': 'error' or 'success' * 'res_type': 'values' or 'arrays' or 'rv' * } * * In the object, 'res_type' is used to indicate what the type of the result * values(s) is that we pass to the final callback. We secondarily use * 'args.impl' to adjust this behavior in an implementation-specific way. For * example, 'tryEach' only returns an array if it has more than 1 result passed * to the final callback. Otherwise, it passes a solitary value to the final * callback. * * In case it's not clear, 'rv' in the `res_type` member, is just the * result-value that we also return. This is the convention in functions that * originated in `vasync` (pipeline), but not in functions that originated in * `async` (waterfall, tryEach). */ function waterfall_impl(opts) { mod_assert.ok(typeof (opts) === 'object'); var rv, current, next; var funcs = opts.funcs; var callback = opts.callback; mod_assert.ok(Array.isArray(funcs), '"opts.funcs" must be specified and must be an array'); mod_assert.ok(arguments.length == 1, 'Function "waterfall_impl" must take only 1 arg'); mod_assert.ok(opts.res_type === 'values' || opts.res_type === 'array' || opts.res_type == 'rv', '"opts.res_type" must either be "values", "array", or "rv"'); mod_assert.ok(opts.stop_when === 'error' || opts.stop_when === 'success', '"opts.stop_when" must either be "error" or "success"'); mod_assert.ok(opts.args.impl === 'pipeline' || opts.args.impl === 'waterfall' || opts.args.impl === 'tryEach', '"opts.args.impl" must be "pipeline", "waterfall", or "tryEach"'); if (opts.args.impl === 'pipeline') { mod_assert.ok(typeof (opts.args.uarg) !== undefined, '"opts.args.uarg" should be defined when pipeline is used'); } rv = { 'operations': funcs.map(function (func) { return ({ 'func': func, 'funcname': func.name || '(anon)', 'status': 'waiting' }); }), 'successes': [], 'ndone': 0, 'nerrors': 0 }; if (funcs.length === 0) { if (callback) setImmediate(function () { var res = (opts.args.impl === 'pipeline') ? rv : undefined; callback(null, res); }); return (rv); } next = function (idx, err) { /* * Note that nfunc_args contains the args we will pass to the * next func in the func-list the user gave us. Except for * 'tryEach', which passes cb's. However, it will pass * 'nfunc_args' to its final callback -- see below. */ var res_key, nfunc_args, entry, nextentry; if (err === undefined) err = null; if (idx != current) { throw (new mod_verror.VError( 'vasync.waterfall: function %d ("%s") invoked ' + 'its callback twice', idx, rv['operations'][idx].funcname)); } mod_assert.equal(idx, rv['ndone'], 'idx should be equal to ndone'); entry = rv['operations'][rv['ndone']++]; if (opts.args.impl === 'tryEach' || opts.args.impl === 'waterfall') { nfunc_args = Array.prototype.slice.call(arguments, 2); res_key = 'results'; entry['results'] = nfunc_args; } else if (opts.args.impl === 'pipeline') { nfunc_args = [ opts.args.uarg ]; res_key = 'result'; entry['result'] = arguments[2]; } mod_assert.equal(entry['status'], 'pending', 'status should be pending'); entry['status'] = err ? 'fail' : 'ok'; entry['err'] = err; if (err) { rv['nerrors']++; } else { rv['successes'].push(entry[res_key]); } if ((opts.stop_when === 'error' && err) || (opts.stop_when === 'success' && rv['successes'].length > 0) || rv['ndone'] == funcs.length) { if (callback) { if (opts.res_type === 'values' || (opts.res_type === 'array' && nfunc_args.length <= 1)) { nfunc_args.unshift(err); callback.apply(null, nfunc_args); } else if (opts.res_type === 'array') { callback(err, nfunc_args); } else if (opts.res_type === 'rv') { callback(err, rv); } } } else { nextentry = rv['operations'][rv['ndone']]; nextentry['status'] = 'pending'; current++; nfunc_args.push(next.bind(null, current)); setImmediate(function () { var nfunc = nextentry['func']; /* * At first glance it may seem like this branch * is superflous with the code above that * branches on `opts.args.impl`. It may also * seem like calling `nfunc.apply` is * sufficient for both cases (after all we * pushed `next.bind(null, current)` to the * `nfunc_args` array), before we call * `setImmediate()`. However, this is not the * case, because the interface exposed by * tryEach is different from the others. The * others pass argument(s) from task to task. * tryEach passes nothing but a callback * (`next.bind` below). However, the callback * itself _can_ be called with one or more * results, which we collect into `nfunc_args` * using the aformentioned `opts.args.impl` * branch above, and which we pass to the * callback via the `opts.res_type` branch * above (where res_type is set to 'array'). */ if (opts.args.impl !== 'tryEach') { nfunc.apply(null, nfunc_args); } else { nfunc(next.bind(null, current)); } }); } }; rv['operations'][0]['status'] = 'pending'; current = 0; if (opts.args.impl !== 'pipeline') { funcs[0](next.bind(null, current)); } else { funcs[0](opts.args.uarg, next.bind(null, current)); } return (rv); } node-vasync-2.2.0/package.json000066400000000000000000000010761321005420100162170ustar00rootroot00000000000000{ "name": "vasync", "version": "2.2.0", "description": "utilities for observable asynchronous control flow", "main": "./lib/vasync.js", "repository": { "type": "git", "url": "git://github.com/davepacheco/node-vasync.git" }, "scripts": { "test": "./node_modules/.bin/tap --stdout tests/ && ./node_modules/.bin/nodeunit tests/compat.js && ./node_modules/.bin/nodeunit tests/compat_tryEach.js" }, "devDependencies": { "tap": "~0.4.8", "nodeunit": "0.8.7" }, "dependencies": { "verror": "1.10.0" }, "engines": [ "node >=0.6.0" ], "license": "MIT" } node-vasync-2.2.0/tests/000077500000000000000000000000001321005420100150675ustar00rootroot00000000000000node-vasync-2.2.0/tests/compat.js000066400000000000000000000066211321005420100167150ustar00rootroot00000000000000/* * tests/compat.js: Some of the APIs provided by vasync are intended to be * API-compatible with node-async, so we incorporate the tests from node-async * directly here. These are copied from https://github.com/caolan/async, * available under the MIT license. To make it easy to update this from the * source, this file should remain unchanged from the source except for this * header comment, the change to the "require" line, and deleted lines for * unimplemented functions. * * The following tests are deliberately omitted: * * o "waterfall non-array": Per Joyent's Best Practices for Node.js Error * Handling, we're strict about argument types and throw on these programmer * errors rather than emitting them asynchronously. * * o "waterfall multiple callback calls": We deliberately disallow a waterfall * function to invoke its callback more than once, so we don't test for that * here. The behavior that node-async allows can potentially be used to fork * the waterfall, which may be useful, but it's often used instead as an * excuse to write code sloppily. And the downside is that it makes it really * hard to understand bugs where the waterfall was resumed too early. For * now, we're disallowing it, but if the forking behavior becomes useful, we * can always make our version less strict. */ var async = require('../lib/vasync'); exports['waterfall'] = function(test){ test.expect(6); var call_order = []; async.waterfall([ function(callback){ call_order.push('fn1'); setTimeout(function(){callback(null, 'one', 'two');}, 0); }, function(arg1, arg2, callback){ call_order.push('fn2'); test.equals(arg1, 'one'); test.equals(arg2, 'two'); setTimeout(function(){callback(null, arg1, arg2, 'three');}, 25); }, function(arg1, arg2, arg3, callback){ call_order.push('fn3'); test.equals(arg1, 'one'); test.equals(arg2, 'two'); test.equals(arg3, 'three'); callback(null, 'four'); }, function(arg4, callback){ call_order.push('fn4'); test.same(call_order, ['fn1','fn2','fn3','fn4']); callback(null, 'test'); } ], function(err){ test.done(); }); }; exports['waterfall empty array'] = function(test){ async.waterfall([], function(err){ test.done(); }); }; exports['waterfall no callback'] = function(test){ async.waterfall([ function(callback){callback();}, function(callback){callback(); test.done();} ]); }; exports['waterfall async'] = function(test){ var call_order = []; async.waterfall([ function(callback){ call_order.push(1); callback(); call_order.push(2); }, function(callback){ call_order.push(3); callback(); }, function(){ test.same(call_order, [1,2,3]); test.done(); } ]); }; exports['waterfall error'] = function(test){ test.expect(1); async.waterfall([ function(callback){ callback('error'); }, function(callback){ test.ok(false, 'next function should not be called'); callback(); } ], function(err){ test.equals(err, 'error'); }); setTimeout(test.done, 50); }; node-vasync-2.2.0/tests/compat_tryEach.js000066400000000000000000000047001321005420100203700ustar00rootroot00000000000000var async = require('../lib/vasync'); /* * tryEach tests, transliterated from mocha to tap. * * They are nearly identical except for some details related to vasync. For * example, we don't support calling the callback more than once from any of * the given functions. */ exports['tryEach no callback'] = function (test) { async.tryEach([]); test.done(); }; exports['tryEach empty'] = function (test) { async.tryEach([], function (err, results) { test.equals(err, null); test.same(results, undefined); test.done(); }); }; exports['tryEach one task, multiple results'] = function (test) { var RESULTS = ['something', 'something2']; async.tryEach([ function (callback) { callback(null, RESULTS[0], RESULTS[1]); } ], function (err, results) { test.equals(err, null); test.same(results, RESULTS); test.done(); }); }; exports['tryEach one task'] = function (test) { var RESULT = 'something'; async.tryEach([ function (callback) { callback(null, RESULT); } ], function (err, results) { test.equals(err, null); test.same(results, RESULT); test.done(); }); }; exports['tryEach two tasks, one failing'] = function (test) { var RESULT = 'something'; async.tryEach([ function (callback) { callback(new Error('Failure'), {}); }, function (callback) { callback(null, RESULT); } ], function (err, results) { test.equals(err, null); test.same(results, RESULT); test.done(); }); }; exports['tryEach two tasks, both failing'] = function (test) { var ERROR_RESULT = new Error('Failure2'); async.tryEach([ function (callback) { callback(new Error('Should not stop here')); }, function (callback) { callback(ERROR_RESULT); } ], function (err, results) { test.equals(err, ERROR_RESULT); test.same(results, undefined); test.done(); }); }; exports['tryEach two tasks, non failing'] = function (test) { var RESULT = 'something'; async.tryEach([ function (callback) { callback(null, RESULT); }, function () { test.fail('Should not been called'); } ], function (err, results) { test.equals(err, null); test.same(results, RESULT); test.done(); }); }; node-vasync-2.2.0/tests/filter.js000066400000000000000000000122631321005420100167160ustar00rootroot00000000000000/* * Tests the "filter", "filterSeries", and "filterLimit" functions */ var mod_util = require('util'); var mod_tap = require('tap'); var mod_vasync = require('..'); mod_tap.test('filterSeries', function (test) { var inputs = [0, 1, 2, 3, 4, 5]; var curTasks = 0; var maxTasks = 0; // filterSeries has an implicit limit of 1 concurrent operation var limit = 1; function filterFunc(input, cb) { curTasks++; if (curTasks > maxTasks) { maxTasks = curTasks; } test.ok(curTasks <= limit, mod_util.format( 'input %d: current tasks %d <= %d', input, curTasks, limit)); setTimeout(function () { curTasks--; cb(null, input < 2 || input === 4); }, 50); } mod_vasync.filterSeries(inputs, filterFunc, function filterDone(err, results) { test.ok(!err, 'error unset'); test.equal(maxTasks, limit, 'max tasks reached limit'); test.deepEqual(results, [0, 1, 4], 'results array correct'); test.end(); }); }); mod_tap.test('filterLimit', function (test) { var inputs = [0, 1, 2, 3, 4, 5]; var curTasks = 0; var maxTasks = 0; var limit = 2; function filterFunc(input, cb) { curTasks++; if (curTasks > maxTasks) { maxTasks = curTasks; } test.ok(curTasks <= limit, mod_util.format( 'input %d: current tasks %d <= %d', input, curTasks, limit)); setTimeout(function () { curTasks--; cb(null, input < 2 || input === 4); }, 50); } mod_vasync.filterLimit(inputs, limit, filterFunc, function filterDone(err, results) { test.ok(!err, 'error unset'); test.equal(maxTasks, limit, 'max tasks reached limit'); test.deepEqual(results, [0, 1, 4], 'results array correct'); test.end(); }); }); mod_tap.test('filter (maintain order)', function (test) { var inputs = [0, 1, 2, 3, 4, 5]; var limit = inputs.length; var storedValues = []; function filterFunc(input, cb) { /* * Hold every callback in an array to be called when all * filterFunc's have run. This way, we can ensure that all * tasks have started without waiting for any others to finish. */ storedValues.push({ input: input, cb: cb }); test.ok(storedValues.length <= limit, mod_util.format( 'input %d: current tasks %d <= %d', input, storedValues.length, limit)); /* * When this constraint is true, all filterFunc's have run for * each input. We now call all callbacks in a pre-determined * order (out of order of the original) to ensure the final * array is in the correct order. */ if (storedValues.length === inputs.length) { [5, 2, 0, 1, 4, 3].forEach(function (i) { var o = storedValues[i]; o.cb(null, o.input < 2 || o.input === 4); }); } } mod_vasync.filter(inputs, filterFunc, function filterDone(err, results) { test.ok(!err, 'error unset'); test.equal(storedValues.length, inputs.length, 'max tasks reached limit'); test.deepEqual(results, [0, 1, 4], 'results array correct'); test.end(); }); }); mod_tap.test('filterSeries error handling', function (test) { /* * We will error half way through the list of inputs to ensure that * first half are processed while the second half are ignored. */ var inputs = [0, 1, 2, 3, 4, 5]; function filterFunc(input, cb) { switch (input) { case 0: case 1: case 2: cb(null, true); break; case 3: cb(new Error('error on ' + input)); break; case 4: case 5: test.ok(false, 'processed too many inputs'); cb(new Error('processed too many inputs')); break; default: test.ok(false, 'unexpected input: ' + input); cb(new Error('unexpected input')); break; } } mod_vasync.filterSeries(inputs, filterFunc, function filterDone(err, results) { test.ok(err, 'error set'); test.ok(err.message === 'error on 3', 'error on input 3'); test.ok(results === undefined, 'results is unset'); test.end(); }); }); mod_tap.test('filterSeries double callback', function (test) { var inputs = [0, 1, 2, 3, 4, 5]; function filterFunc(input, cb) { switch (input) { case 0: case 1: case 2: cb(null, true); break; case 3: /* * The first call to cb() should "win" - meaning this * value will be filtered out of the final array of * results. */ cb(null, false); test.throws(function () { cb(null, true); }); break; case 4: /* * Like input 3, all subsequent calls to cb() will * throw an error and not affect the original call to * cb(). */ cb(null, true); test.throws(function () { cb(new Error('uh oh')); }); break; case 5: cb(null, true); break; default: test.ok(false, 'unexpected input: ' + input); cb(new Error('unexpected input')); break; } } mod_vasync.filterSeries(inputs, filterFunc, function filterDone(err, results) { test.ok(!err, 'error not set'); test.deepEqual(results, [0, 1, 2, 4, 5], 'results array correct'); test.end(); }); }); mod_tap.test('filter push to queue object error', function (test) { var inputs = [0, 1, 2, 3, 4, 5]; function filterFunc(input, cb) { cb(null, true); } var q = mod_vasync.filterSeries(inputs, filterFunc, function filterDone(err, results) { test.end(); }); test.equal(q.closed, true, 'queue is closed'); test.throws(function () { q.push(6); }); }); node-vasync-2.2.0/tests/issue-21.js000066400000000000000000000007331321005420100170000ustar00rootroot00000000000000/* * Tests that if the user modifies the list of functions passed to * vasync.pipeline, vasync ignores the changes and does not crash. */ var assert = require('assert'); var vasync = require('../lib/vasync'); var count = 0; var funcs; function doStuff(_, callback) { count++; setImmediate(callback); } funcs = [ doStuff, doStuff, doStuff ]; vasync.pipeline({ 'funcs': funcs }, function (err) { assert.ok(!err); assert.ok(count === 3); }); funcs.push(doStuff); node-vasync-2.2.0/tests/pipeline.js000066400000000000000000000100241321005420100172270ustar00rootroot00000000000000/* * Tests the "pipeline" primitive. */ var mod_tap = require('tap'); var mod_vasync = require('..'); var st; mod_tap.test('empty pipeline', function (test) { var count = 0; st = mod_vasync.pipeline({'funcs': [], 'arg': null}, function (err, result) { test.ok(err === null); test.ok(result.ndone === 0); test.ok(result.nerrors === 0); test.ok(result.operations.length === 0); test.ok(result.successes.length === 0); test.equal(count, 1); test.end(); }); count++; test.ok(st.ndone === 0); test.ok(st.nerrors === 0); test.ok(st.operations.length === 0); test.ok(st.successes.length === 0); }); mod_tap.test('normal 4-stage pipeline', function (test) { var count = 0; st = mod_vasync.pipeline({'funcs': [ function func1(_, cb) { test.equal(st.successes[0], undefined, 'func1: successes'); test.ok(count === 0, 'func1: count === 0'); test.ok(st.ndone === 0); count++; setImmediate(cb, null, count); }, function func2(_, cb) { test.equal(st.successes[0], 1, 'func2: successes'); test.ok(count == 1, 'func2: count == 1'); test.ok(st.ndone === 1); test.ok(st.operations[0].status == 'ok'); test.ok(st.operations[1].status == 'pending'); test.ok(st.operations[2].status == 'waiting'); count++; setImmediate(cb, null, count); }, function (_, cb) { test.equal(st.successes[0], 1, 'func3: successes'); test.equal(st.successes[1], 2, 'func3: successes'); test.ok(count == 2, 'func3: count == 2'); test.ok(st.ndone === 2); count++; setImmediate(cb, null, count); }, function func4(_, cb) { test.equal(st.successes[0], 1, 'func4: successes'); test.equal(st.successes[1], 2, 'func4: successes'); test.equal(st.successes[2], 3, 'func4: successes'); test.ok(count == 3, 'func4: count == 3'); test.ok(st.ndone === 3); count++; setImmediate(cb, null, count); } ]}, function (err, result) { test.ok(count == 4, 'final: count == 4'); test.ok(err === null, 'no error'); test.ok(result === st); test.equal(result, st, 'final-cb: st == result'); test.equal(st.successes[0], 1, 'final-cb: successes'); test.equal(st.successes[1], 2, 'final-cb: successes'); test.equal(st.successes[2], 3, 'final-cb: successes'); test.equal(st.successes[3], 4, 'final-cb: successes'); test.ok(st.ndone === 4); test.ok(st.nerrors === 0); test.ok(st.operations.length === 4); test.ok(st.successes.length === 4); test.ok(st.operations[0].status == 'ok'); test.ok(st.operations[1].status == 'ok'); test.ok(st.operations[2].status == 'ok'); test.ok(st.operations[3].status == 'ok'); test.end(); }); test.ok(st.ndone === 0); test.ok(st.nerrors === 0); test.ok(st.operations.length === 4); test.ok(st.operations[0].funcname == 'func1', 'func1 name'); test.ok(st.operations[0].status == 'pending'); test.ok(st.operations[1].funcname == 'func2', 'func2 name'); test.ok(st.operations[1].status == 'waiting'); test.ok(st.operations[2].funcname == '(anon)', 'anon name'); test.ok(st.operations[2].status == 'waiting'); test.ok(st.operations[3].funcname == 'func4', 'func4 name'); test.ok(st.operations[3].status == 'waiting'); test.ok(st.successes.length === 0); }); mod_tap.test('bailing out early', function (test) { var count = 0; st = mod_vasync.pipeline({'funcs': [ function func1(_, cb) { test.ok(count === 0, 'func1: count === 0'); count++; setImmediate(cb, null, count); }, function func2(_, cb) { test.ok(count == 1, 'func2: count == 1'); count++; setImmediate(cb, new Error('boom!')); }, function func3(_, cb) { test.ok(count == 2, 'func3: count == 2'); count++; setImmediate(cb, null, count); } ]}, function (err, result) { test.ok(count == 2, 'final: count == 3'); test.equal(err.message, 'boom!'); test.ok(result === st); test.equal(result, st, 'final-cb: st == result'); test.ok(st.ndone == 2); test.ok(st.nerrors == 1); test.ok(st.operations[0].status == 'ok'); test.ok(st.operations[1].status == 'fail'); test.ok(st.operations[2].status == 'waiting'); test.ok(st.successes.length == 1); test.end(); }); }); node-vasync-2.2.0/tests/queue.js000066400000000000000000000125561321005420100165620ustar00rootroot00000000000000/* vim: set ts=8 sts=8 sw=8 noet: */ var mod_tap = require('tap'); var mod_vasync = require('..'); function immediate_worker(task, next) { setImmediate(function () { next(); }); } function sametick_worker(task, next) { next(); } function random_delay_worker(task, next) { setTimeout(function () { next(); }, Math.floor(Math.random() * 250)); } mod_tap.test('must not push after close', function (test) { test.plan(3); var q = mod_vasync.queuev({ worker: immediate_worker, concurrency: 10 }); test.ok(q); test.doesNotThrow(function () { q.push({}); }, 'push should not throw _before_ close()'); q.close(); /* * If we attempt to add tasks to the queue _after_ calling close(), * we should get an exception: */ test.throws(function () { q.push({}); }, 'push should throw _after_ close()'); test.end(); }); mod_tap.test('get \'end\' event with close()', function (test) { var task_count = 45; var tasks_finished = 0; var seen_end = false; var seen_drain = false; test.plan(14 + task_count); var q = mod_vasync.queuev({ worker: random_delay_worker, concurrency: 5 }); test.ok(q); /* * Enqueue a bunch of tasks; more than our concurrency: */ for (var i = 0; i < 45; i++) { q.push({}, function () { tasks_finished++; test.ok(true); }); } /* * Close the queue to signify that we're done now. */ test.equal(q.ended, false); test.equal(q.closed, false); q.close(); test.equal(q.closed, true); test.equal(q.ended, false); q.on('drain', function () { /* * 'drain' should fire before 'end': */ test.notOk(seen_drain); test.notOk(seen_end); seen_drain = true; }); q.on('end', function () { /* * 'end' should fire after 'drain': */ test.ok(seen_drain); test.notOk(seen_end); seen_end = true; /* * Check the public state: */ test.equal(q.closed, true); test.equal(q.ended, true); /* * We should have fired the callbacks for _all_ enqueued * tasks by now: */ test.equal(task_count, tasks_finished); test.end(); }); /* * Check that we see neither the 'drain', nor the 'end' event before * the end of this tick: */ test.notOk(seen_drain); test.notOk(seen_end); }); mod_tap.test('get \'end\' event with close() and no tasks', function (test) { var seen_drain = false; var seen_end = false; test.plan(10); var q = mod_vasync.queuev({ worker: immediate_worker, concurrency: 10 }); setImmediate(function () { test.notOk(seen_end); }); test.equal(q.ended, false); test.equal(q.closed, false); q.close(); test.equal(q.closed, true); test.equal(q.ended, false); test.notOk(seen_end); q.on('drain', function () { seen_drain = true; }); q.on('end', function () { /* * We do not expect to see a 'drain' event, as there were no * tasks pushed onto the queue before we closed it. */ test.notOk(seen_drain); test.notOk(seen_end); test.equal(q.closed, true); test.equal(q.ended, true); seen_end = true; test.end(); }); }); /* * We want to ensure that both the 'drain' event and the q.drain() hook are * called the same number of times: */ mod_tap.test('equivalence of on(\'drain\') and q.drain()', function (test) { var enqcount = 4; var drains = 4; var ee_count = 0; var fn_count = 0; test.plan(enqcount + drains + 3); var q = mod_vasync.queuev({ worker: immediate_worker, concurrency: 10 }); var enq = function () { if (--enqcount < 0) return; q.push({}, function () { test.ok(true, 'task completion'); }); }; var draino = function () { test.ok(true, 'drain called'); if (--drains === 0) { test.equal(q.closed, false, 'not closed'); test.equal(q.ended, false, 'not ended'); test.equal(fn_count, ee_count, 'same number of calls'); test.end(); } }; enq(); enq(); q.on('drain', function () { ee_count++; enq(); draino(); }); q.drain = function () { fn_count++; enq(); draino(); }; }); /* * In the past, we've only handed on the _first_ argument to the task completion * callback. Make sure we hand on _all_ of the arguments now: */ mod_tap.test('ensure all arguments passed to push() callback', function (test) { test.plan(13); var q = mod_vasync.queuev({ worker: function (task, callback) { if (task.fail) { callback(new Error('guru meditation')); return; } callback(null, 1, 2, 3, 5, 8); }, concurrency: 1 }); q.push({ fail: true }, function (err, a, b, c, d, e) { test.ok(err, 'got the error'); test.equal(err.message, 'guru meditation'); test.type(a, 'undefined'); test.type(b, 'undefined'); test.type(c, 'undefined'); test.type(d, 'undefined'); test.type(e, 'undefined'); }); q.push({ fail: false }, function (err, a, b, c, d, e) { test.notOk(err, 'got no error'); test.equal(a, 1); test.equal(b, 2); test.equal(c, 3); test.equal(d, 5); test.equal(e, 8); }); q.drain = function () { test.end(); }; }); mod_tap.test('queue kill', function (test) { // Derived from async queue.kill test var count = 0; var q = mod_vasync.queuev({ worker: function (task, callback) { setImmediate(function () { test.ok(++count < 2, 'Function should be called once'); callback(); }); }, concurrency: 1 }); q.drain = function () { test.ok(false, 'Function should never be called'); }; // Queue twice, the first will exec immediately q.push(0); q.push(0); q.kill(); q.on('end', function () { test.ok(q.killed); test.end(); }); }); node-vasync-2.2.0/tests/queue_concurrency.js000066400000000000000000000052161321005420100211670ustar00rootroot00000000000000/* vim: set ts=8 sts=8 sw=8 noet: */ var mod_tap = require('tap'); var mod_vasync = require('..'); function latched_worker(task, cb) { if (task.immediate) { cb(); } else { task.latched = true; task.unlatch = function () { task.latched = false; cb(); }; } } function unlatchAll(tasks) { tasks.forEach(function (t) { if (t.latched) { t.unlatch(); } }); } function setAllImmediate(tasks) { tasks.forEach(function (t) { t.immediate = true; }); } mod_tap.test('test serial tasks', function (test) { test.plan(2); var q = mod_vasync.queuev({ worker: latched_worker, concurrency: 1 }); test.ok(q); var tasks = []; for (var i = 0; i < 2; ++i) { tasks.push({ 'id': i, 'latched': false, 'immediate': false }); } setTimeout(function () { var latched = 0; tasks.forEach(function (t) { if (t.latched) { ++latched; } }); test.ok(latched === 1); unlatchAll(tasks); setAllImmediate(tasks); }, 10); q.on('drain', function () { q.close(); }); q.on('end', function () { test.end(); }); q.push(tasks); }); mod_tap.test('test parallel tasks', function (test) { test.plan(2); var q = mod_vasync.queuev({ worker: latched_worker, concurrency: 2 }); test.ok(q); var tasks = []; for (var i = 0; i < 3; ++i) { tasks.push({ 'id': i, 'latched': false, 'immediate': false }); } setTimeout(function () { var latched = 0; tasks.forEach(function (t) { if (t.latched) { ++latched; } }); test.ok(latched === 2); unlatchAll(tasks); setAllImmediate(tasks); }, 10); q.on('drain', function () { q.close(); }); q.on('end', function () { test.end(); }); q.push(tasks); }); mod_tap.test('test ratchet up and down', function (test) { test.plan(8); var q = mod_vasync.queuev({ worker: latched_worker, concurrency: 2 }); test.ok(q); var bounced = 0; var tasks = []; for (var i = 0; i < 21; ++i) { tasks.push({ 'id': i, 'latched': false, 'immediate': false }); } function count() { var latched = 0; tasks.forEach(function (t) { if (t.latched) { ++latched; } }); return (latched); } function fiveLatch() { if (!q.closed) { ++bounced; test.ok(count() === 5); q.updateConcurrency(2); unlatchAll(tasks); setTimeout(twoLatch, 10); } } function twoLatch() { if (!q.closed) { ++bounced; test.ok(count() === 2); q.updateConcurrency(5); unlatchAll(tasks); setTimeout(fiveLatch, 10); } } setTimeout(twoLatch, 10); q.on('drain', function () { q.close(); }); q.on('end', function () { // 21 tasks === 5 * 3 + 2 * 3 === 6 bounces test.ok(bounced === 6); test.end(); }); q.push(tasks); }); node-vasync-2.2.0/tests/waterfall.js000066400000000000000000000113471321005420100174140ustar00rootroot00000000000000/* * Tests the "waterfall" primitive. */ var mod_tap = require('tap'); var mod_vasync = require('..'); var count = 0; var st; mod_tap.test('empty waterfall', function (test) { st = mod_vasync.waterfall([], function (err) { test.ok(err === null); test.ok(st.ndone === 0); test.ok(st.nerrors === 0); test.ok(st.operations.length === 0); test.ok(st.successes.length === 0); test.equal(count, 1); test.end(); }); count++; test.ok(st.ndone === 0); test.ok(st.nerrors === 0); test.ok(st.operations.length === 0); test.ok(st.successes.length === 0); }); mod_tap.test('normal 4-stage waterfall', function (test) { count = 0; st = mod_vasync.waterfall([ function func1(cb) { test.ok(count === 0, 'func1: count === 0'); test.ok(st.ndone === 0); count++; setTimeout(cb, 20, null, { 'hello': 'world' }); }, function func2(extra, cb) { test.equal(extra.hello, 'world', 'func2: extra arg'); test.ok(count == 1, 'func2: count == 1'); test.ok(st.ndone === 1); test.ok(st.operations[0].status == 'ok'); test.ok(st.operations[1].status == 'pending'); test.ok(st.operations[2].status == 'waiting'); count++; setTimeout(cb, 20, null, 5, 6, 7); }, function (five, six, seven, cb) { test.equal(five, 5, 'func3: extra arg'); test.equal(six, 6, 'func3: extra arg'); test.equal(seven, 7, 'func3: extra arg'); test.ok(count == 2, 'func3: count == 2'); test.ok(st.ndone === 2); count++; setTimeout(cb, 20); }, function func4(cb) { test.ok(count == 3, 'func4: count == 2'); test.ok(st.ndone === 3); count++; setTimeout(cb, 20, null, 8, 9); } ], function (err, eight, nine) { test.ok(count == 4, 'final: count == 4'); test.ok(err === null, 'no error'); test.ok(eight == 8); test.ok(nine == 9); test.ok(st.ndone === 4); test.ok(st.nerrors === 0); test.ok(st.operations.length === 4); test.ok(st.successes.length === 4); test.ok(st.operations[0].status == 'ok'); test.ok(st.operations[1].status == 'ok'); test.ok(st.operations[2].status == 'ok'); test.ok(st.operations[3].status == 'ok'); test.end(); }); test.ok(st.ndone === 0); test.ok(st.nerrors === 0); test.ok(st.operations.length === 4); test.ok(st.operations[0].funcname == 'func1', 'func1 name'); test.ok(st.operations[0].status == 'pending'); test.ok(st.operations[1].funcname == 'func2', 'func2 name'); test.ok(st.operations[1].status == 'waiting'); test.ok(st.operations[2].funcname == '(anon)', 'anon name'); test.ok(st.operations[2].status == 'waiting'); test.ok(st.operations[3].funcname == 'func4', 'func4 name'); test.ok(st.operations[3].status == 'waiting'); test.ok(st.successes.length === 0); }); mod_tap.test('bailing out early', function (test) { count = 0; st = mod_vasync.waterfall([ function func1(cb) { test.ok(count === 0, 'func1: count === 0'); count++; setTimeout(cb, 20); }, function func2(cb) { test.ok(count == 1, 'func2: count == 1'); count++; setTimeout(cb, 20, new Error('boom!')); }, function func3(cb) { test.ok(count == 2, 'func3: count == 2'); count++; setTimeout(cb, 20); } ], function (err) { test.ok(count == 2, 'final: count == 3'); test.equal(err.message, 'boom!'); test.ok(st.ndone == 2); test.ok(st.nerrors == 1); test.ok(st.operations[0].status == 'ok'); test.ok(st.operations[1].status == 'fail'); test.ok(st.operations[2].status == 'waiting'); test.ok(st.successes.length == 1); test.end(); }); }); mod_tap.test('bad function', function (test) { count = 0; st = mod_vasync.waterfall([ function func1(cb) { count++; cb(); setTimeout(function () { test.throws( function () { cb(); process.abort(); }, 'vasync.waterfall: ' + 'function 0 ("func1") invoked its ' + 'callback twice'); test.equal(count, 2); test.end(); }, 100); }, function func2(cb) { count++; /* do nothing -- we'll throw an exception first */ } ], function (err) { /* not reached */ console.error('didn\'t expect to finish'); process.abort(); }); }); mod_tap.test('badargs', function (test) { test.throws(function () { mod_vasync.waterfall(); }); test.throws(function () { mod_vasync.waterfall([], 'foo'); }); test.throws(function () { mod_vasync.waterfall('foo', 'bar'); }); test.end(); }); mod_tap.test('normal waterfall, no callback', function (test) { count = 0; st = mod_vasync.waterfall([ function func1(cb) { test.ok(count === 0); count++; setImmediate(cb); }, function func2(cb) { test.ok(count == 1); count++; setImmediate(cb); setTimeout(function () { test.ok(count == 2); test.end(); }, 100); } ]); }); mod_tap.test('empty waterfall, no callback', function (test) { st = mod_vasync.waterfall([]); setTimeout(function () { test.end(); }, 100); }); node-vasync-2.2.0/tests/whilst.js000066400000000000000000000046771321005420100167550ustar00rootroot00000000000000/* * Tests the "whilst" function */ var mod_util = require('util'); var mod_tap = require('tap'); var mod_vasync = require('..'); mod_tap.test('basic whilst', function (test) { var n = 0; mod_vasync.whilst( function condition() { return (n < 5); }, function body(cb) { n++; cb(null, n); }, function done(err, arg) { test.ok(!err, 'error unset'); test.equal(n, 5, 'n == 5'); test.equal(n, arg, 'n == arg'); test.end(); }); }); mod_tap.test('whilst return object', function (test) { var n = 0; var w = mod_vasync.whilst( function condition() { return (n < 5); }, function body(cb) { n++; test.equal(n, w.iterations, 'n == w.iterations: ' + n); cb(null, n, 'foo'); }, function done(err, arg1, arg2, arg3) { test.ok(!err, 'error unset'); test.equal(w.iterations, 5, 'whilst had 5 iterations'); test.equal(w.finished, true, 'whilst has finished'); test.equal(arg1, n, 'whilst arg1 == n'); test.equal(arg2, 'foo', 'whilst arg2 == "foo"'); test.equal(arg3, undefined, 'whilst arg3 == undefined'); test.end(); }); test.equal(typeof (w), 'object', 'whilst returns an object'); test.equal(w.finished, false, 'whilst is not finished'); test.equal(w.iterations, 0, 'whilst has not started yet'); }); mod_tap.test('whilst false condition', function (test) { mod_vasync.whilst( function condition() { return (false); }, function body(cb) { cb(); }, function done(err, arg) { test.ok(!err, 'error is unset'); test.ok(!arg, 'arg is unset'); test.end(); }); }); mod_tap.test('whilst error', function (test) { var n = 0; var w = mod_vasync.whilst( function condition() { return (true); }, function body(cb) { n++; if (n > 5) { cb(new Error('n > 5'), 'bar'); } else { cb(null, 'foo'); } }, function done(err, arg) { test.ok(err, 'error is set'); test.equal(err.message, 'n > 5'); test.equal(arg, 'bar'); test.equal(w.finished, true, 'whilst is finished'); /* * Iterations is bumped after the test condition is run and * before the iteration function is run. Because the condition * in this example is inside the iteration function (the test * condition always returns true), the iteration count will be * 1 higher than expected, since it will fail when (n > 5), or * when iterations is 6. */ test.equal(w.iterations, 6, 'whilst had 6 iterations'); test.end(); }); });