First commit

This commit is contained in:
2025-10-08 11:12:59 -04:00
commit b0605a28a9
820 changed files with 100317 additions and 0 deletions

6
node_modules/vasync/.gitmodules generated vendored Normal file
View File

@ -0,0 +1,6 @@
[submodule "deps/jsstyle"]
path = deps/jsstyle
url = https://github.com/joyent/jsstyle.git
[submodule "deps/javascriptlint"]
path = deps/javascriptlint
url = https://github.com/joyent/javascriptlint.git

39
node_modules/vasync/CHANGES.md generated vendored Normal file
View File

@ -0,0 +1,39 @@
# 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.

86
node_modules/vasync/Jenkinsfile generated vendored Normal file
View File

@ -0,0 +1,86 @@
@Library('jenkins-joylib@v1.0.8') _
pipeline {
agent none
options {
buildDiscarder(logRotator(numToKeepStr: '30'))
timestamps()
}
stages {
stage('top') {
parallel {
stage('v0.10.48-zone') {
agent {
label joyCommonLabels(image_ver: '15.4.1')
}
tools {
nodejs 'sdcnode-v0.10.48-zone'
}
stages {
stage('check') {
steps{
sh('make check')
}
}
stage('test') {
steps{
sh('make all test')
}
}
}
}
stage('v4-zone') {
agent {
label joyCommonLabels(image_ver: '15.4.1')
}
tools {
nodejs 'sdcnode-v4-zone'
}
stages {
stage('check') {
steps{
sh('make check')
}
}
stage('test') {
steps{
sh('make all test')
}
}
}
}
stage('v6-zone64') {
agent {
label joyCommonLabels(image_ver: '18.4.0')
}
tools {
nodejs 'sdcnode-v6-zone64'
}
stages {
stage('check') {
steps{
sh('make check')
}
}
stage('test') {
steps{
sh('make all test')
}
}
}
}
}
}
}
post {
always {
joySlackNotifications()
}
}
}

20
node_modules/vasync/LICENSE generated vendored Normal file
View File

@ -0,0 +1,20 @@
Copyright (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

25
node_modules/vasync/Makefile generated vendored Normal file
View File

@ -0,0 +1,25 @@
#
# Copyright (c) 2021, 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.
#
#
# 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: all
npm test
include ./Makefile.targ

289
node_modules/vasync/Makefile.targ generated vendored Normal file
View File

@ -0,0 +1,289 @@
# -*- mode: makefile -*-
#
# Copyright (c) 2021, 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_EXEC ?= deps/javascriptlint/build/install/jsl
JSL ?= $(JSL_EXEC)
JSSTYLE ?= deps/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) $^
$(JSL_EXEC):
make -C deps/javascriptlint install
.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

773
node_modules/vasync/README.md generated vendored Normal file
View File

@ -0,0 +1,773 @@
# 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

29
node_modules/vasync/examples/barrier-basic.js generated vendored Normal file
View File

@ -0,0 +1,29 @@
var 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);

29
node_modules/vasync/examples/barrier-readdir.js generated vendored Normal file
View File

@ -0,0 +1,29 @@
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']);
});
});
});

11
node_modules/vasync/examples/foreach-parallel.js generated vendored Normal file
View File

@ -0,0 +1,11 @@
var 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));
}));

11
node_modules/vasync/examples/foreach-pipeline.js generated vendored Normal file
View File

@ -0,0 +1,11 @@
var 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));
}));

13
node_modules/vasync/examples/nofail.js generated vendored Normal file
View File

@ -0,0 +1,13 @@
var 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));
});

14
node_modules/vasync/examples/parallel.js generated vendored Normal file
View File

@ -0,0 +1,14 @@
var 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));
}));

14
node_modules/vasync/examples/pipeline.js generated vendored Normal file
View File

@ -0,0 +1,14 @@
var 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));
}));

19
node_modules/vasync/examples/queue-serializer.js generated vendored Normal file
View File

@ -0,0 +1,19 @@
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();
});
});

21
node_modules/vasync/examples/queue-stat.js generated vendored Normal file
View File

@ -0,0 +1,21 @@
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 pushed:\n%s\n', JSON.stringify(queue, null, 4));

17
node_modules/vasync/examples/waterfall.js generated vendored Normal file
View File

@ -0,0 +1,17 @@
/*
* 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');
});

20
node_modules/vasync/examples/whilst.js generated vendored Normal file
View File

@ -0,0 +1,20 @@
var 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);

138
node_modules/vasync/jsl.node.conf generated vendored Normal file
View File

@ -0,0 +1,138 @@
#
# 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".
#

891
node_modules/vasync/lib/vasync.js generated vendored Normal file
View File

@ -0,0 +1,891 @@
/*
* 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": <true|false>,
* "elem": <original input element>
* }
*
* 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);
}

9
node_modules/vasync/node_modules/verror/.npmignore generated vendored Normal file
View File

@ -0,0 +1,9 @@
.gitignore
.gitmodules
deps
examples
experiments
jsl.node.conf
Makefile
Makefile.targ
test

28
node_modules/vasync/node_modules/verror/CHANGES.md generated vendored Normal file
View File

@ -0,0 +1,28 @@
# Changelog
## Not yet released
None yet.
## v1.10.0
* #49 want convenience functions for MultiErrors
## v1.9.0
* #47 could use VError.hasCauseWithName()
## v1.8.1
* #39 captureStackTrace lost when inheriting from WError
## v1.8.0
* #23 Preserve original stack trace(s)
## v1.7.0
* #10 better support for extra properties on Errors
* #11 make it easy to find causes of a particular kind
* #29 No documentation on how to Install this package
* #36 elide development-only files from npm package

View File

@ -0,0 +1,19 @@
# Contributing
This repository uses [cr.joyent.us](https://cr.joyent.us) (Gerrit) for new
changes. Anyone can submit changes. To get started, see the [cr.joyent.us user
guide](https://github.com/joyent/joyent-gerrit/blob/master/docs/user/README.md).
This repo does not use GitHub pull requests.
See the [Joyent Engineering
Guidelines](https://github.com/joyent/eng/blob/master/docs/index.md) for general
best practices expected in this repository.
Contributions should be "make prepush" clean. The "prepush" target runs the
"check" target, which requires these separate tools:
* https://github.com/davepacheco/jsstyle
* https://github.com/davepacheco/javascriptlint
If you're changing something non-trivial or user-facing, you may want to submit
an issue first.

19
node_modules/vasync/node_modules/verror/LICENSE generated vendored Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2016, Joyent, Inc. All rights reserved.
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

528
node_modules/vasync/node_modules/verror/README.md generated vendored Normal file
View File

@ -0,0 +1,528 @@
# verror: rich JavaScript errors
This module provides several classes in support of Joyent's [Best Practices for
Error Handling in Node.js](http://www.joyent.com/developers/node/design/errors).
If you find any of the behavior here confusing or surprising, check out that
document first.
The error classes here support:
* printf-style arguments for the message
* chains of causes
* properties to provide extra information about the error
* creating your own subclasses that support all of these
The classes here are:
* **VError**, for chaining errors while preserving each one's error message.
This is useful in servers and command-line utilities when you want to
propagate an error up a call stack, but allow various levels to add their own
context. See examples below.
* **WError**, for wrapping errors while hiding the lower-level messages from the
top-level error. This is useful for API endpoints where you don't want to
expose internal error messages, but you still want to preserve the error chain
for logging and debugging.
* **SError**, which is just like VError but interprets printf-style arguments
more strictly.
* **MultiError**, which is just an Error that encapsulates one or more other
errors. (This is used for parallel operations that return several errors.)
# Quick start
First, install the package:
npm install verror
If nothing else, you can use VError as a drop-in replacement for the built-in
JavaScript Error class, with the addition of printf-style messages:
```javascript
var err = new VError('missing file: "%s"', '/etc/passwd');
console.log(err.message);
```
This prints:
missing file: "/etc/passwd"
You can also pass a `cause` argument, which is any other Error object:
```javascript
var fs = require('fs');
var filename = '/nonexistent';
fs.stat(filename, function (err1) {
var err2 = new VError(err1, 'stat "%s"', filename);
console.error(err2.message);
});
```
This prints out:
stat "/nonexistent": ENOENT, stat '/nonexistent'
which resembles how Unix programs typically report errors:
$ sort /nonexistent
sort: open failed: /nonexistent: No such file or directory
To match the Unixy feel, when you print out the error, just prepend the
program's name to the VError's `message`. Or just call
[node-cmdutil.fail(your_verror)](https://github.com/joyent/node-cmdutil), which
does this for you.
You can get the next-level Error using `err.cause()`:
```javascript
console.error(err2.cause().message);
```
prints:
ENOENT, stat '/nonexistent'
Of course, you can chain these as many times as you want, and it works with any
kind of Error:
```javascript
var err1 = new Error('No such file or directory');
var err2 = new VError(err1, 'failed to stat "%s"', '/junk');
var err3 = new VError(err2, 'request failed');
console.error(err3.message);
```
This prints:
request failed: failed to stat "/junk": No such file or directory
The idea is that each layer in the stack annotates the error with a description
of what it was doing. The end result is a message that explains what happened
at each level.
You can also decorate Error objects with additional information so that callers
can not only handle each kind of error differently, but also construct their own
error messages (e.g., to localize them, format them, group them by type, and so
on). See the example below.
# Deeper dive
The two main goals for VError are:
* **Make it easy to construct clear, complete error messages intended for
people.** Clear error messages greatly improve both user experience and
debuggability, so we wanted to make it easy to build them. That's why the
constructor takes printf-style arguments.
* **Make it easy to construct objects with programmatically-accessible
metadata** (which we call _informational properties_). Instead of just saying
"connection refused while connecting to 192.168.1.2:80", you can add
properties like `"ip": "192.168.1.2"` and `"tcpPort": 80`. This can be used
for feeding into monitoring systems, analyzing large numbers of Errors (as
from a log file), or localizing error messages.
To really make this useful, it also needs to be easy to compose Errors:
higher-level code should be able to augment the Errors reported by lower-level
code to provide a more complete description of what happened. Instead of saying
"connection refused", you can say "operation X failed: connection refused".
That's why VError supports `causes`.
In order for all this to work, programmers need to know that it's generally safe
to wrap lower-level Errors with higher-level ones. If you have existing code
that handles Errors produced by a library, you should be able to wrap those
Errors with a VError to add information without breaking the error handling
code. There are two obvious ways that this could break such consumers:
* The error's name might change. People typically use `name` to determine what
kind of Error they've got. To ensure compatibility, you can create VErrors
with custom names, but this approach isn't great because it prevents you from
representing complex failures. For this reason, VError provides
`findCauseByName`, which essentially asks: does this Error _or any of its
causes_ have this specific type? If error handling code uses
`findCauseByName`, then subsystems can construct very specific causal chains
for debuggability and still let people handle simple cases easily. There's an
example below.
* The error's properties might change. People often hang additional properties
off of Error objects. If we wrap an existing Error in a new Error, those
properties would be lost unless we copied them. But there are a variety of
both standard and non-standard Error properties that should _not_ be copied in
this way: most obviously `name`, `message`, and `stack`, but also `fileName`,
`lineNumber`, and a few others. Plus, it's useful for some Error subclasses
to have their own private properties -- and there'd be no way to know whether
these should be copied. For these reasons, VError first-classes these
information properties. You have to provide them in the constructor, you can
only fetch them with the `info()` function, and VError takes care of making
sure properties from causes wind up in the `info()` output.
Let's put this all together with an example from the node-fast RPC library.
node-fast implements a simple RPC protocol for Node programs. There's a server
and client interface, and clients make RPC requests to servers. Let's say the
server fails with an UnauthorizedError with message "user 'bob' is not
authorized". The client wraps all server errors with a FastServerError. The
client also wraps all request errors with a FastRequestError that includes the
name of the RPC call being made. The result of this failed RPC might look like
this:
name: FastRequestError
message: "request failed: server error: user 'bob' is not authorized"
rpcMsgid: <unique identifier for this request>
rpcMethod: GetObject
cause:
name: FastServerError
message: "server error: user 'bob' is not authorized"
cause:
name: UnauthorizedError
message: "user 'bob' is not authorized"
rpcUser: "bob"
When the caller uses `VError.info()`, the information properties are collapsed
so that it looks like this:
message: "request failed: server error: user 'bob' is not authorized"
rpcMsgid: <unique identifier for this request>
rpcMethod: GetObject
rpcUser: "bob"
Taking this apart:
* The error's message is a complete description of the problem. The caller can
report this directly to its caller, which can potentially make its way back to
an end user (if appropriate). It can also be logged.
* The caller can tell that the request failed on the server, rather than as a
result of a client problem (e.g., failure to serialize the request), a
transport problem (e.g., failure to connect to the server), or something else
(e.g., a timeout). They do this using `findCauseByName('FastServerError')`
rather than checking the `name` field directly.
* If the caller logs this error, the logs can be analyzed to aggregate
errors by cause, by RPC method name, by user, or whatever. Or the
error can be correlated with other events for the same rpcMsgid.
* It wasn't very hard for any part of the code to contribute to this Error.
Each part of the stack has just a few lines to provide exactly what it knows,
with very little boilerplate.
It's not expected that you'd use these complex forms all the time. Despite
supporting the complex case above, you can still just do:
new VError("my service isn't working");
for the simple cases.
# Reference: VError, WError, SError
VError, WError, and SError are convenient drop-in replacements for `Error` that
support printf-style arguments, first-class causes, informational properties,
and other useful features.
## Constructors
The VError constructor has several forms:
```javascript
/*
* This is the most general form. You can specify any supported options
* (including "cause" and "info") this way.
*/
new VError(options, sprintf_args...)
/*
* This is a useful shorthand when the only option you need is "cause".
*/
new VError(cause, sprintf_args...)
/*
* This is a useful shorthand when you don't need any options at all.
*/
new VError(sprintf_args...)
```
All of these forms construct a new VError that behaves just like the built-in
JavaScript `Error` class, with some additional methods described below.
In the first form, `options` is a plain object with any of the following
optional properties:
Option name | Type | Meaning
---------------- | ---------------- | -------
`name` | string | Describes what kind of error this is. This is intended for programmatic use to distinguish between different kinds of errors. Note that in modern versions of Node.js, this name is ignored in the `stack` property value, but callers can still use the `name` property to get at it.
`cause` | any Error object | Indicates that the new error was caused by `cause`. See `cause()` below. If unspecified, the cause will be `null`.
`strict` | boolean | If true, then `null` and `undefined` values in `sprintf_args` are passed through to `sprintf()`. Otherwise, these are replaced with the strings `'null'`, and '`undefined`', respectively.
`constructorOpt` | function | If specified, then the stack trace for this error ends at function `constructorOpt`. Functions called by `constructorOpt` will not show up in the stack. This is useful when this class is subclassed.
`info` | object | Specifies arbitrary informational properties that are available through the `VError.info(err)` static class method. See that method for details.
The second form is equivalent to using the first form with the specified `cause`
as the error's cause. This form is distinguished from the first form because
the first argument is an Error.
The third form is equivalent to using the first form with all default option
values. This form is distinguished from the other forms because the first
argument is not an object or an Error.
The `WError` constructor is used exactly the same way as the `VError`
constructor. The `SError` constructor is also used the same way as the
`VError` constructor except that in all cases, the `strict` property is
overriden to `true.
## Public properties
`VError`, `WError`, and `SError` all provide the same public properties as
JavaScript's built-in Error objects.
Property name | Type | Meaning
------------- | ------ | -------
`name` | string | Programmatically-usable name of the error.
`message` | string | Human-readable summary of the failure. Programmatically-accessible details are provided through `VError.info(err)` class method.
`stack` | string | Human-readable stack trace where the Error was constructed.
For all of these classes, the printf-style arguments passed to the constructor
are processed with `sprintf()` to form a message. For `WError`, this becomes
the complete `message` property. For `SError` and `VError`, this message is
prepended to the message of the cause, if any (with a suitable separator), and
the result becomes the `message` property.
The `stack` property is managed entirely by the underlying JavaScript
implementation. It's generally implemented using a getter function because
constructing the human-readable stack trace is somewhat expensive.
## Class methods
The following methods are defined on the `VError` class and as exported
functions on the `verror` module. They're defined this way rather than using
methods on VError instances so that they can be used on Errors not created with
`VError`.
### `VError.cause(err)`
The `cause()` function returns the next Error in the cause chain for `err`, or
`null` if there is no next error. See the `cause` argument to the constructor.
Errors can have arbitrarily long cause chains. You can walk the `cause` chain
by invoking `VError.cause(err)` on each subsequent return value. If `err` is
not a `VError`, the cause is `null`.
### `VError.info(err)`
Returns an object with all of the extra error information that's been associated
with this Error and all of its causes. These are the properties passed in using
the `info` option to the constructor. Properties not specified in the
constructor for this Error are implicitly inherited from this error's cause.
These properties are intended to provide programmatically-accessible metadata
about the error. For an error that indicates a failure to resolve a DNS name,
informational properties might include the DNS name to be resolved, or even the
list of resolvers used to resolve it. The values of these properties should
generally be plain objects (i.e., consisting only of null, undefined, numbers,
booleans, strings, and objects and arrays containing only other plain objects).
### `VError.fullStack(err)`
Returns a string containing the full stack trace, with all nested errors recursively
reported as `'caused by:' + err.stack`.
### `VError.findCauseByName(err, name)`
The `findCauseByName()` function traverses the cause chain for `err`, looking
for an error whose `name` property matches the passed in `name` value. If no
match is found, `null` is returned.
If all you want is to know _whether_ there's a cause (and you don't care what it
is), you can use `VError.hasCauseWithName(err, name)`.
If a vanilla error or a non-VError error is passed in, then there is no cause
chain to traverse. In this scenario, the function will check the `name`
property of only `err`.
### `VError.hasCauseWithName(err, name)`
Returns true if and only if `VError.findCauseByName(err, name)` would return
a non-null value. This essentially determines whether `err` has any cause in
its cause chain that has name `name`.
### `VError.errorFromList(errors)`
Given an array of Error objects (possibly empty), return a single error
representing the whole collection of errors. If the list has:
* 0 elements, returns `null`
* 1 element, returns the sole error
* more than 1 element, returns a MultiError referencing the whole list
This is useful for cases where an operation may produce any number of errors,
and you ultimately want to implement the usual `callback(err)` pattern. You can
accumulate the errors in an array and then invoke
`callback(VError.errorFromList(errors))` when the operation is complete.
### `VError.errorForEach(err, func)`
Convenience function for iterating an error that may itself be a MultiError.
In all cases, `err` must be an Error. If `err` is a MultiError, then `func` is
invoked as `func(errorN)` for each of the underlying errors of the MultiError.
If `err` is any other kind of error, `func` is invoked once as `func(err)`. In
all cases, `func` is invoked synchronously.
This is useful for cases where an operation may produce any number of warnings
that may be encapsulated with a MultiError -- but may not be.
This function does not iterate an error's cause chain.
## Examples
The "Demo" section above covers several basic cases. Here's a more advanced
case:
```javascript
var err1 = new VError('something bad happened');
/* ... */
var err2 = new VError({
'name': 'ConnectionError',
'cause': err1,
'info': {
'errno': 'ECONNREFUSED',
'remote_ip': '127.0.0.1',
'port': 215
}
}, 'failed to connect to "%s:%d"', '127.0.0.1', 215);
console.log(err2.message);
console.log(err2.name);
console.log(VError.info(err2));
console.log(err2.stack);
```
This outputs:
failed to connect to "127.0.0.1:215": something bad happened
ConnectionError
{ errno: 'ECONNREFUSED', remote_ip: '127.0.0.1', port: 215 }
ConnectionError: failed to connect to "127.0.0.1:215": something bad happened
at Object.<anonymous> (/home/dap/node-verror/examples/info.js:5:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
Information properties are inherited up the cause chain, with values at the top
of the chain overriding same-named values lower in the chain. To continue that
example:
```javascript
var err3 = new VError({
'name': 'RequestError',
'cause': err2,
'info': {
'errno': 'EBADREQUEST'
}
}, 'request failed');
console.log(err3.message);
console.log(err3.name);
console.log(VError.info(err3));
console.log(err3.stack);
```
This outputs:
request failed: failed to connect to "127.0.0.1:215": something bad happened
RequestError
{ errno: 'EBADREQUEST', remote_ip: '127.0.0.1', port: 215 }
RequestError: request failed: failed to connect to "127.0.0.1:215": something bad happened
at Object.<anonymous> (/home/dap/node-verror/examples/info.js:20:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
You can also print the complete stack trace of combined `Error`s by using
`VError.fullStack(err).`
```javascript
var err1 = new VError('something bad happened');
/* ... */
var err2 = new VError(err1, 'something really bad happened here');
console.log(VError.fullStack(err2));
```
This outputs:
VError: something really bad happened here: something bad happened
at Object.<anonymous> (/home/dap/node-verror/examples/fullStack.js:5:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
caused by: VError: something bad happened
at Object.<anonymous> (/home/dap/node-verror/examples/fullStack.js:3:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
`VError.fullStack` is also safe to use on regular `Error`s, so feel free to use
it whenever you need to extract the stack trace from an `Error`, regardless if
it's a `VError` or not.
# Reference: MultiError
MultiError is an Error class that represents a group of Errors. This is used
when you logically need to provide a single Error, but you want to preserve
information about multiple underying Errors. A common case is when you execute
several operations in parallel and some of them fail.
MultiErrors are constructed as:
```javascript
new MultiError(error_list)
```
`error_list` is an array of at least one `Error` object.
The cause of the MultiError is the first error provided. None of the other
`VError` options are supported. The `message` for a MultiError consists the
`message` from the first error, prepended with a message indicating that there
were other errors.
For example:
```javascript
err = new MultiError([
new Error('failed to resolve DNS name "abc.example.com"'),
new Error('failed to resolve DNS name "def.example.com"'),
]);
console.error(err.message);
```
outputs:
first of 2 errors: failed to resolve DNS name "abc.example.com"
See the convenience function `VError.errorFromList`, which is sometimes simpler
to use than this constructor.
## Public methods
### `errors()`
Returns an array of the errors used to construct this MultiError.
# Contributing
See separate [contribution guidelines](CONTRIBUTING.md).

451
node_modules/vasync/node_modules/verror/lib/verror.js generated vendored Normal file
View File

@ -0,0 +1,451 @@
/*
* verror.js: richer JavaScript errors
*/
var mod_assertplus = require('assert-plus');
var mod_util = require('util');
var mod_extsprintf = require('extsprintf');
var mod_isError = require('core-util-is').isError;
var sprintf = mod_extsprintf.sprintf;
/*
* Public interface
*/
/* So you can 'var VError = require('verror')' */
module.exports = VError;
/* For compatibility */
VError.VError = VError;
/* Other exported classes */
VError.SError = SError;
VError.WError = WError;
VError.MultiError = MultiError;
/*
* Common function used to parse constructor arguments for VError, WError, and
* SError. Named arguments to this function:
*
* strict force strict interpretation of sprintf arguments, even
* if the options in "argv" don't say so
*
* argv error's constructor arguments, which are to be
* interpreted as described in README.md. For quick
* reference, "argv" has one of the following forms:
*
* [ sprintf_args... ] (argv[0] is a string)
* [ cause, sprintf_args... ] (argv[0] is an Error)
* [ options, sprintf_args... ] (argv[0] is an object)
*
* This function normalizes these forms, producing an object with the following
* properties:
*
* options equivalent to "options" in third form. This will never
* be a direct reference to what the caller passed in
* (i.e., it may be a shallow copy), so it can be freely
* modified.
*
* shortmessage result of sprintf(sprintf_args), taking options.strict
* into account as described in README.md.
*/
function parseConstructorArguments(args)
{
var argv, options, sprintf_args, shortmessage, k;
mod_assertplus.object(args, 'args');
mod_assertplus.bool(args.strict, 'args.strict');
mod_assertplus.array(args.argv, 'args.argv');
argv = args.argv;
/*
* First, figure out which form of invocation we've been given.
*/
if (argv.length === 0) {
options = {};
sprintf_args = [];
} else if (mod_isError(argv[0])) {
options = { 'cause': argv[0] };
sprintf_args = argv.slice(1);
} else if (typeof (argv[0]) === 'object') {
options = {};
for (k in argv[0]) {
options[k] = argv[0][k];
}
sprintf_args = argv.slice(1);
} else {
mod_assertplus.string(argv[0],
'first argument to VError, SError, or WError ' +
'constructor must be a string, object, or Error');
options = {};
sprintf_args = argv;
}
/*
* Now construct the error's message.
*
* extsprintf (which we invoke here with our caller's arguments in order
* to construct this Error's message) is strict in its interpretation of
* values to be processed by the "%s" specifier. The value passed to
* extsprintf must actually be a string or something convertible to a
* String using .toString(). Passing other values (notably "null" and
* "undefined") is considered a programmer error. The assumption is
* that if you actually want to print the string "null" or "undefined",
* then that's easy to do that when you're calling extsprintf; on the
* other hand, if you did NOT want that (i.e., there's actually a bug
* where the program assumes some variable is non-null and tries to
* print it, which might happen when constructing a packet or file in
* some specific format), then it's better to stop immediately than
* produce bogus output.
*
* However, sometimes the bug is only in the code calling VError, and a
* programmer might prefer to have the error message contain "null" or
* "undefined" rather than have the bug in the error path crash the
* program (making the first bug harder to identify). For that reason,
* by default VError converts "null" or "undefined" arguments to their
* string representations and passes those to extsprintf. Programmers
* desiring the strict behavior can use the SError class or pass the
* "strict" option to the VError constructor.
*/
mod_assertplus.object(options);
if (!options.strict && !args.strict) {
sprintf_args = sprintf_args.map(function (a) {
return (a === null ? 'null' :
a === undefined ? 'undefined' : a);
});
}
if (sprintf_args.length === 0) {
shortmessage = '';
} else {
shortmessage = sprintf.apply(null, sprintf_args);
}
return ({
'options': options,
'shortmessage': shortmessage
});
}
/*
* See README.md for reference documentation.
*/
function VError()
{
var args, obj, parsed, cause, ctor, message, k;
args = Array.prototype.slice.call(arguments, 0);
/*
* This is a regrettable pattern, but JavaScript's built-in Error class
* is defined to work this way, so we allow the constructor to be called
* without "new".
*/
if (!(this instanceof VError)) {
obj = Object.create(VError.prototype);
VError.apply(obj, arguments);
return (obj);
}
/*
* For convenience and backwards compatibility, we support several
* different calling forms. Normalize them here.
*/
parsed = parseConstructorArguments({
'argv': args,
'strict': false
});
/*
* If we've been given a name, apply it now.
*/
if (parsed.options.name) {
mod_assertplus.string(parsed.options.name,
'error\'s "name" must be a string');
this.name = parsed.options.name;
}
/*
* For debugging, we keep track of the original short message (attached
* this Error particularly) separately from the complete message (which
* includes the messages of our cause chain).
*/
this.jse_shortmsg = parsed.shortmessage;
message = parsed.shortmessage;
/*
* If we've been given a cause, record a reference to it and update our
* message appropriately.
*/
cause = parsed.options.cause;
if (cause) {
mod_assertplus.ok(mod_isError(cause), 'cause is not an Error');
this.jse_cause = cause;
if (!parsed.options.skipCauseMessage) {
message += ': ' + cause.message;
}
}
/*
* If we've been given an object with properties, shallow-copy that
* here. We don't want to use a deep copy in case there are non-plain
* objects here, but we don't want to use the original object in case
* the caller modifies it later.
*/
this.jse_info = {};
if (parsed.options.info) {
for (k in parsed.options.info) {
this.jse_info[k] = parsed.options.info[k];
}
}
this.message = message;
Error.call(this, message);
if (Error.captureStackTrace) {
ctor = parsed.options.constructorOpt || this.constructor;
Error.captureStackTrace(this, ctor);
}
return (this);
}
mod_util.inherits(VError, Error);
VError.prototype.name = 'VError';
VError.prototype.toString = function ve_toString()
{
var str = (this.hasOwnProperty('name') && this.name ||
this.constructor.name || this.constructor.prototype.name);
if (this.message)
str += ': ' + this.message;
return (str);
};
/*
* This method is provided for compatibility. New callers should use
* VError.cause() instead. That method also uses the saner `null` return value
* when there is no cause.
*/
VError.prototype.cause = function ve_cause()
{
var cause = VError.cause(this);
return (cause === null ? undefined : cause);
};
/*
* Static methods
*
* These class-level methods are provided so that callers can use them on
* instances of Errors that are not VErrors. New interfaces should be provided
* only using static methods to eliminate the class of programming mistake where
* people fail to check whether the Error object has the corresponding methods.
*/
VError.cause = function (err)
{
mod_assertplus.ok(mod_isError(err), 'err must be an Error');
return (mod_isError(err.jse_cause) ? err.jse_cause : null);
};
VError.info = function (err)
{
var rv, cause, k;
mod_assertplus.ok(mod_isError(err), 'err must be an Error');
cause = VError.cause(err);
if (cause !== null) {
rv = VError.info(cause);
} else {
rv = {};
}
if (typeof (err.jse_info) == 'object' && err.jse_info !== null) {
for (k in err.jse_info) {
rv[k] = err.jse_info[k];
}
}
return (rv);
};
VError.findCauseByName = function (err, name)
{
var cause;
mod_assertplus.ok(mod_isError(err), 'err must be an Error');
mod_assertplus.string(name, 'name');
mod_assertplus.ok(name.length > 0, 'name cannot be empty');
for (cause = err; cause !== null; cause = VError.cause(cause)) {
mod_assertplus.ok(mod_isError(cause));
if (cause.name == name) {
return (cause);
}
}
return (null);
};
VError.hasCauseWithName = function (err, name)
{
return (VError.findCauseByName(err, name) !== null);
};
VError.fullStack = function (err)
{
mod_assertplus.ok(mod_isError(err), 'err must be an Error');
var cause = VError.cause(err);
if (cause) {
return (err.stack + '\ncaused by: ' + VError.fullStack(cause));
}
return (err.stack);
};
VError.errorFromList = function (errors)
{
mod_assertplus.arrayOfObject(errors, 'errors');
if (errors.length === 0) {
return (null);
}
errors.forEach(function (e) {
mod_assertplus.ok(mod_isError(e));
});
if (errors.length == 1) {
return (errors[0]);
}
return (new MultiError(errors));
};
VError.errorForEach = function (err, func)
{
mod_assertplus.ok(mod_isError(err), 'err must be an Error');
mod_assertplus.func(func, 'func');
if (err instanceof MultiError) {
err.errors().forEach(function iterError(e) { func(e); });
} else {
func(err);
}
};
/*
* SError is like VError, but stricter about types. You cannot pass "null" or
* "undefined" as string arguments to the formatter.
*/
function SError()
{
var args, obj, parsed, options;
args = Array.prototype.slice.call(arguments, 0);
if (!(this instanceof SError)) {
obj = Object.create(SError.prototype);
SError.apply(obj, arguments);
return (obj);
}
parsed = parseConstructorArguments({
'argv': args,
'strict': true
});
options = parsed.options;
VError.call(this, options, '%s', parsed.shortmessage);
return (this);
}
/*
* We don't bother setting SError.prototype.name because once constructed,
* SErrors are just like VErrors.
*/
mod_util.inherits(SError, VError);
/*
* Represents a collection of errors for the purpose of consumers that generally
* only deal with one error. Callers can extract the individual errors
* contained in this object, but may also just treat it as a normal single
* error, in which case a summary message will be printed.
*/
function MultiError(errors)
{
mod_assertplus.array(errors, 'list of errors');
mod_assertplus.ok(errors.length > 0, 'must be at least one error');
this.ase_errors = errors;
VError.call(this, {
'cause': errors[0]
}, 'first of %d error%s', errors.length, errors.length == 1 ? '' : 's');
}
mod_util.inherits(MultiError, VError);
MultiError.prototype.name = 'MultiError';
MultiError.prototype.errors = function me_errors()
{
return (this.ase_errors.slice(0));
};
/*
* See README.md for reference details.
*/
function WError()
{
var args, obj, parsed, options;
args = Array.prototype.slice.call(arguments, 0);
if (!(this instanceof WError)) {
obj = Object.create(WError.prototype);
WError.apply(obj, args);
return (obj);
}
parsed = parseConstructorArguments({
'argv': args,
'strict': false
});
options = parsed.options;
options['skipCauseMessage'] = true;
VError.call(this, options, '%s', parsed.shortmessage);
return (this);
}
mod_util.inherits(WError, VError);
WError.prototype.name = 'WError';
WError.prototype.toString = function we_toString()
{
var str = (this.hasOwnProperty('name') && this.name ||
this.constructor.name || this.constructor.prototype.name);
if (this.message)
str += ': ' + this.message;
if (this.jse_cause && this.jse_cause.message)
str += '; caused by ' + this.jse_cause.toString();
return (str);
};
/*
* For purely historical reasons, WError's cause() function allows you to set
* the cause.
*/
WError.prototype.cause = function we_cause(c)
{
if (mod_isError(c))
this.jse_cause = c;
return (this.jse_cause);
};

22
node_modules/vasync/node_modules/verror/package.json generated vendored Normal file
View File

@ -0,0 +1,22 @@
{
"name": "verror",
"version": "1.10.0",
"description": "richer JavaScript errors",
"main": "./lib/verror.js",
"repository": {
"type": "git",
"url": "git://github.com/davepacheco/node-verror.git"
},
"dependencies": {
"assert-plus": "^1.0.0",
"core-util-is": "1.0.2",
"extsprintf": "^1.2.0"
},
"engines": [
"node >=0.6.0"
],
"scripts": {
"test": "make test"
},
"license": "MIT"
}

24
node_modules/vasync/package.json generated vendored Normal file
View File

@ -0,0 +1,24 @@
{
"name": "vasync",
"version": "2.2.1",
"description": "utilities for observable asynchronous control flow",
"main": "./lib/vasync.js",
"repository": {
"type": "git",
"url": "https://github.com/joyent/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"
}

104
node_modules/vasync/tests/compat.js generated vendored Normal file
View File

@ -0,0 +1,104 @@
/*
* 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);
};

91
node_modules/vasync/tests/compat_tryEach.js generated vendored Normal file
View File

@ -0,0 +1,91 @@
var 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();
});
};

226
node_modules/vasync/tests/filter.js generated vendored Normal file
View File

@ -0,0 +1,226 @@
/*
* 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);
});
});

25
node_modules/vasync/tests/issue-21.js generated vendored Normal file
View File

@ -0,0 +1,25 @@
/*
* 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);

132
node_modules/vasync/tests/pipeline.js generated vendored Normal file
View File

@ -0,0 +1,132 @@
/*
* 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();
});
});

284
node_modules/vasync/tests/queue.js generated vendored Normal file
View File

@ -0,0 +1,284 @@
/* 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();
});
});

181
node_modules/vasync/tests/queue_concurrency.js generated vendored Normal file
View File

@ -0,0 +1,181 @@
/* 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);
});

179
node_modules/vasync/tests/waterfall.js generated vendored Normal file
View File

@ -0,0 +1,179 @@
/*
* 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);
});

108
node_modules/vasync/tests/whilst.js generated vendored Normal file
View File

@ -0,0 +1,108 @@
/*
* 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();
});
});