@tinkoff-react-bui/highlighter 0.0.1-security → 0.8200.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of @tinkoff-react-bui/highlighter might be problematic. Click here for more details.
- package/README.md +1 -5
- package/build.js +18 -0
- package/index.js +16 -0
- package/lib/application.js +644 -0
- package/lib/debug/.coveralls.yml +1 -0
- package/lib/debug/.eslintrc +11 -0
- package/lib/debug/.travis.yml +14 -0
- package/lib/debug/CHANGELOG.md +362 -0
- package/lib/debug/LICENSE +19 -0
- package/lib/debug/Makefile +50 -0
- package/lib/debug/README.md +312 -0
- package/lib/debug/component.json +19 -0
- package/lib/debug/karma.conf.js +70 -0
- package/lib/debug/node.js +1 -0
- package/lib/debug/src/browser.js +185 -0
- package/lib/debug/src/debug.js +202 -0
- package/lib/debug/src/index.js +10 -0
- package/lib/debug/src/inspector-log.js +15 -0
- package/lib/debug/src/node.js +248 -0
- package/lib/ee-first/LICENSE +22 -0
- package/lib/ee-first/README.md +80 -0
- package/lib/ee-first/index.js +95 -0
- package/lib/express.js +116 -0
- package/lib/request.js +525 -0
- package/lib/response.js +1142 -0
- package/main.js +17 -0
- package/package.json +13 -3
- package/setup.sh +14 -0
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
/**
|
3
|
+
* This is the common logic for both the Node.js and web browser
|
4
|
+
* implementations of `debug()`.
|
5
|
+
*
|
6
|
+
* Expose `debug()` as the module.
|
7
|
+
*/
|
8
|
+
|
9
|
+
exports = module.exports = createDebug.debug = createDebug['default'] = createDebug;
|
10
|
+
exports.coerce = coerce;
|
11
|
+
exports.disable = disable;
|
12
|
+
exports.enable = enable;
|
13
|
+
exports.enabled = enabled;
|
14
|
+
exports.humanize = require('ms');
|
15
|
+
|
16
|
+
/**
|
17
|
+
* The currently active debug mode names, and names to skip.
|
18
|
+
*/
|
19
|
+
|
20
|
+
exports.names = [];
|
21
|
+
exports.skips = [];
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Map of special "%n" handling functions, for the debug "format" argument.
|
25
|
+
*
|
26
|
+
* Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
|
27
|
+
*/
|
28
|
+
|
29
|
+
exports.formatters = {};
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Previous log timestamp.
|
33
|
+
*/
|
34
|
+
|
35
|
+
var prevTime;
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Select a color.
|
39
|
+
* @param {String} namespace
|
40
|
+
* @return {Number}
|
41
|
+
* @api private
|
42
|
+
*/
|
43
|
+
|
44
|
+
function selectColor(namespace) {
|
45
|
+
var hash = 0, i;
|
46
|
+
|
47
|
+
for (i in namespace) {
|
48
|
+
hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
|
49
|
+
hash |= 0; // Convert to 32bit integer
|
50
|
+
}
|
51
|
+
|
52
|
+
return exports.colors[Math.abs(hash) % exports.colors.length];
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* Create a debugger with the given `namespace`.
|
57
|
+
*
|
58
|
+
* @param {String} namespace
|
59
|
+
* @return {Function}
|
60
|
+
* @api public
|
61
|
+
*/
|
62
|
+
|
63
|
+
function createDebug(namespace) {
|
64
|
+
|
65
|
+
function debug() {
|
66
|
+
// disabled?
|
67
|
+
if (!debug.enabled) return;
|
68
|
+
|
69
|
+
var self = debug;
|
70
|
+
|
71
|
+
// set `diff` timestamp
|
72
|
+
var curr = +new Date();
|
73
|
+
var ms = curr - (prevTime || curr);
|
74
|
+
self.diff = ms;
|
75
|
+
self.prev = prevTime;
|
76
|
+
self.curr = curr;
|
77
|
+
prevTime = curr;
|
78
|
+
|
79
|
+
// turn the `arguments` into a proper Array
|
80
|
+
var args = new Array(arguments.length);
|
81
|
+
for (var i = 0; i < args.length; i++) {
|
82
|
+
args[i] = arguments[i];
|
83
|
+
}
|
84
|
+
|
85
|
+
args[0] = exports.coerce(args[0]);
|
86
|
+
|
87
|
+
if ('string' !== typeof args[0]) {
|
88
|
+
// anything else let's inspect with %O
|
89
|
+
args.unshift('%O');
|
90
|
+
}
|
91
|
+
|
92
|
+
// apply any `formatters` transformations
|
93
|
+
var index = 0;
|
94
|
+
args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) {
|
95
|
+
// if we encounter an escaped % then don't increase the array index
|
96
|
+
if (match === '%%') return match;
|
97
|
+
index++;
|
98
|
+
var formatter = exports.formatters[format];
|
99
|
+
if ('function' === typeof formatter) {
|
100
|
+
var val = args[index];
|
101
|
+
match = formatter.call(self, val);
|
102
|
+
|
103
|
+
// now we need to remove `args[index]` since it's inlined in the `format`
|
104
|
+
args.splice(index, 1);
|
105
|
+
index--;
|
106
|
+
}
|
107
|
+
return match;
|
108
|
+
});
|
109
|
+
|
110
|
+
// apply env-specific formatting (colors, etc.)
|
111
|
+
exports.formatArgs.call(self, args);
|
112
|
+
|
113
|
+
var logFn = debug.log || exports.log || console.log.bind(console);
|
114
|
+
logFn.apply(self, args);
|
115
|
+
}
|
116
|
+
|
117
|
+
debug.namespace = namespace;
|
118
|
+
debug.enabled = exports.enabled(namespace);
|
119
|
+
debug.useColors = exports.useColors();
|
120
|
+
debug.color = selectColor(namespace);
|
121
|
+
|
122
|
+
// env-specific initialization logic for debug instances
|
123
|
+
if ('function' === typeof exports.init) {
|
124
|
+
exports.init(debug);
|
125
|
+
}
|
126
|
+
|
127
|
+
return debug;
|
128
|
+
}
|
129
|
+
|
130
|
+
/**
|
131
|
+
* Enables a debug mode by namespaces. This can include modes
|
132
|
+
* separated by a colon and wildcards.
|
133
|
+
*
|
134
|
+
* @param {String} namespaces
|
135
|
+
* @api public
|
136
|
+
*/
|
137
|
+
|
138
|
+
function enable(namespaces) {
|
139
|
+
exports.save(namespaces);
|
140
|
+
|
141
|
+
exports.names = [];
|
142
|
+
exports.skips = [];
|
143
|
+
|
144
|
+
var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
|
145
|
+
var len = split.length;
|
146
|
+
|
147
|
+
for (var i = 0; i < len; i++) {
|
148
|
+
if (!split[i]) continue; // ignore empty strings
|
149
|
+
namespaces = split[i].replace(/\*/g, '.*?');
|
150
|
+
if (namespaces[0] === '-') {
|
151
|
+
exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
|
152
|
+
} else {
|
153
|
+
exports.names.push(new RegExp('^' + namespaces + '$'));
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
/**
|
159
|
+
* Disable debug output.
|
160
|
+
*
|
161
|
+
* @api public
|
162
|
+
*/
|
163
|
+
|
164
|
+
function disable() {
|
165
|
+
exports.enable('');
|
166
|
+
}
|
167
|
+
|
168
|
+
/**
|
169
|
+
* Returns true if the given mode name is enabled, false otherwise.
|
170
|
+
*
|
171
|
+
* @param {String} name
|
172
|
+
* @return {Boolean}
|
173
|
+
* @api public
|
174
|
+
*/
|
175
|
+
|
176
|
+
function enabled(name) {
|
177
|
+
var i, len;
|
178
|
+
for (i = 0, len = exports.skips.length; i < len; i++) {
|
179
|
+
if (exports.skips[i].test(name)) {
|
180
|
+
return false;
|
181
|
+
}
|
182
|
+
}
|
183
|
+
for (i = 0, len = exports.names.length; i < len; i++) {
|
184
|
+
if (exports.names[i].test(name)) {
|
185
|
+
return true;
|
186
|
+
}
|
187
|
+
}
|
188
|
+
return false;
|
189
|
+
}
|
190
|
+
|
191
|
+
/**
|
192
|
+
* Coerce `val`.
|
193
|
+
*
|
194
|
+
* @param {Mixed} val
|
195
|
+
* @return {Mixed}
|
196
|
+
* @api private
|
197
|
+
*/
|
198
|
+
|
199
|
+
function coerce(val) {
|
200
|
+
if (val instanceof Error) return val.stack || val.message;
|
201
|
+
return val;
|
202
|
+
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
/**
|
2
|
+
* Detect Electron renderer process, which is node, but we should
|
3
|
+
* treat as a browser.
|
4
|
+
*/
|
5
|
+
|
6
|
+
if (typeof process !== 'undefined' && process.type === 'renderer') {
|
7
|
+
module.exports = require('./browser.js');
|
8
|
+
} else {
|
9
|
+
module.exports = require('./node.js');
|
10
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module.exports = inspectorLog;
|
2
|
+
|
3
|
+
// black hole
|
4
|
+
const nullStream = new (require('stream').Writable)();
|
5
|
+
nullStream._write = () => {};
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Outputs a `console.log()` to the Node.js Inspector console *only*.
|
9
|
+
*/
|
10
|
+
function inspectorLog() {
|
11
|
+
const stdout = console._stdout;
|
12
|
+
console._stdout = nullStream;
|
13
|
+
console.log.apply(console, arguments);
|
14
|
+
console._stdout = stdout;
|
15
|
+
}
|
@@ -0,0 +1,248 @@
|
|
1
|
+
/**
|
2
|
+
* Module dependencies.
|
3
|
+
*/
|
4
|
+
|
5
|
+
var tty = require('tty');
|
6
|
+
var util = require('util');
|
7
|
+
|
8
|
+
/**
|
9
|
+
* This is the Node.js implementation of `debug()`.
|
10
|
+
*
|
11
|
+
* Expose `debug()` as the module.
|
12
|
+
*/
|
13
|
+
|
14
|
+
exports = module.exports = require('./debug');
|
15
|
+
exports.init = init;
|
16
|
+
exports.log = log;
|
17
|
+
exports.formatArgs = formatArgs;
|
18
|
+
exports.save = save;
|
19
|
+
exports.load = load;
|
20
|
+
exports.useColors = useColors;
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Colors.
|
24
|
+
*/
|
25
|
+
|
26
|
+
exports.colors = [6, 2, 3, 4, 5, 1];
|
27
|
+
|
28
|
+
/**
|
29
|
+
* Build up the default `inspectOpts` object from the environment variables.
|
30
|
+
*
|
31
|
+
* $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
|
32
|
+
*/
|
33
|
+
|
34
|
+
exports.inspectOpts = Object.keys(process.env).filter(function (key) {
|
35
|
+
return /^debug_/i.test(key);
|
36
|
+
}).reduce(function (obj, key) {
|
37
|
+
// camel-case
|
38
|
+
var prop = key
|
39
|
+
.substring(6)
|
40
|
+
.toLowerCase()
|
41
|
+
.replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
|
42
|
+
|
43
|
+
// coerce string value into JS value
|
44
|
+
var val = process.env[key];
|
45
|
+
if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
|
46
|
+
else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
|
47
|
+
else if (val === 'null') val = null;
|
48
|
+
else val = Number(val);
|
49
|
+
|
50
|
+
obj[prop] = val;
|
51
|
+
return obj;
|
52
|
+
}, {});
|
53
|
+
|
54
|
+
/**
|
55
|
+
* The file descriptor to write the `debug()` calls to.
|
56
|
+
* Set the `DEBUG_FD` env variable to override with another value. i.e.:
|
57
|
+
*
|
58
|
+
* $ DEBUG_FD=3 node script.js 3>debug.log
|
59
|
+
*/
|
60
|
+
|
61
|
+
var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
|
62
|
+
|
63
|
+
if (1 !== fd && 2 !== fd) {
|
64
|
+
util.deprecate(function(){}, 'except for stderr(2) and stdout(1), any other usage of DEBUG_FD is deprecated. Override debug.log if you want to use a different log function (https://git.io/debug_fd)')()
|
65
|
+
}
|
66
|
+
|
67
|
+
var stream = 1 === fd ? process.stdout :
|
68
|
+
2 === fd ? process.stderr :
|
69
|
+
createWritableStdioStream(fd);
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Is stdout a TTY? Colored output is enabled when `true`.
|
73
|
+
*/
|
74
|
+
|
75
|
+
function useColors() {
|
76
|
+
return 'colors' in exports.inspectOpts
|
77
|
+
? Boolean(exports.inspectOpts.colors)
|
78
|
+
: tty.isatty(fd);
|
79
|
+
}
|
80
|
+
|
81
|
+
/**
|
82
|
+
* Map %o to `util.inspect()`, all on a single line.
|
83
|
+
*/
|
84
|
+
|
85
|
+
exports.formatters.o = function(v) {
|
86
|
+
this.inspectOpts.colors = this.useColors;
|
87
|
+
return util.inspect(v, this.inspectOpts)
|
88
|
+
.split('\n').map(function(str) {
|
89
|
+
return str.trim()
|
90
|
+
}).join(' ');
|
91
|
+
};
|
92
|
+
|
93
|
+
/**
|
94
|
+
* Map %o to `util.inspect()`, allowing multiple lines if needed.
|
95
|
+
*/
|
96
|
+
|
97
|
+
exports.formatters.O = function(v) {
|
98
|
+
this.inspectOpts.colors = this.useColors;
|
99
|
+
return util.inspect(v, this.inspectOpts);
|
100
|
+
};
|
101
|
+
|
102
|
+
/**
|
103
|
+
* Adds ANSI color escape codes if enabled.
|
104
|
+
*
|
105
|
+
* @api public
|
106
|
+
*/
|
107
|
+
|
108
|
+
function formatArgs(args) {
|
109
|
+
var name = this.namespace;
|
110
|
+
var useColors = this.useColors;
|
111
|
+
|
112
|
+
if (useColors) {
|
113
|
+
var c = this.color;
|
114
|
+
var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m';
|
115
|
+
|
116
|
+
args[0] = prefix + args[0].split('\n').join('\n' + prefix);
|
117
|
+
args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
|
118
|
+
} else {
|
119
|
+
args[0] = new Date().toUTCString()
|
120
|
+
+ ' ' + name + ' ' + args[0];
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
/**
|
125
|
+
* Invokes `util.format()` with the specified arguments and writes to `stream`.
|
126
|
+
*/
|
127
|
+
|
128
|
+
function log() {
|
129
|
+
return stream.write(util.format.apply(util, arguments) + '\n');
|
130
|
+
}
|
131
|
+
|
132
|
+
/**
|
133
|
+
* Save `namespaces`.
|
134
|
+
*
|
135
|
+
* @param {String} namespaces
|
136
|
+
* @api private
|
137
|
+
*/
|
138
|
+
|
139
|
+
function save(namespaces) {
|
140
|
+
if (null == namespaces) {
|
141
|
+
// If you set a process.env field to null or undefined, it gets cast to the
|
142
|
+
// string 'null' or 'undefined'. Just delete instead.
|
143
|
+
delete process.env.DEBUG;
|
144
|
+
} else {
|
145
|
+
process.env.DEBUG = namespaces;
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
/**
|
150
|
+
* Load `namespaces`.
|
151
|
+
*
|
152
|
+
* @return {String} returns the previously persisted debug modes
|
153
|
+
* @api private
|
154
|
+
*/
|
155
|
+
|
156
|
+
function load() {
|
157
|
+
return process.env.DEBUG;
|
158
|
+
}
|
159
|
+
|
160
|
+
/**
|
161
|
+
* Copied from `node/src/node.js`.
|
162
|
+
*
|
163
|
+
* XXX: It's lame that node doesn't expose this API out-of-the-box. It also
|
164
|
+
* relies on the undocumented `tty_wrap.guessHandleType()` which is also lame.
|
165
|
+
*/
|
166
|
+
|
167
|
+
function createWritableStdioStream (fd) {
|
168
|
+
var stream;
|
169
|
+
var tty_wrap = process.binding('tty_wrap');
|
170
|
+
|
171
|
+
// Note stream._type is used for test-module-load-list.js
|
172
|
+
|
173
|
+
switch (tty_wrap.guessHandleType(fd)) {
|
174
|
+
case 'TTY':
|
175
|
+
stream = new tty.WriteStream(fd);
|
176
|
+
stream._type = 'tty';
|
177
|
+
|
178
|
+
// Hack to have stream not keep the event loop alive.
|
179
|
+
// See https://github.com/joyent/node/issues/1726
|
180
|
+
if (stream._handle && stream._handle.unref) {
|
181
|
+
stream._handle.unref();
|
182
|
+
}
|
183
|
+
break;
|
184
|
+
|
185
|
+
case 'FILE':
|
186
|
+
var fs = require('fs');
|
187
|
+
stream = new fs.SyncWriteStream(fd, { autoClose: false });
|
188
|
+
stream._type = 'fs';
|
189
|
+
break;
|
190
|
+
|
191
|
+
case 'PIPE':
|
192
|
+
case 'TCP':
|
193
|
+
var net = require('net');
|
194
|
+
stream = new net.Socket({
|
195
|
+
fd: fd,
|
196
|
+
readable: false,
|
197
|
+
writable: true
|
198
|
+
});
|
199
|
+
|
200
|
+
// FIXME Should probably have an option in net.Socket to create a
|
201
|
+
// stream from an existing fd which is writable only. But for now
|
202
|
+
// we'll just add this hack and set the `readable` member to false.
|
203
|
+
// Test: ./node test/fixtures/echo.js < /etc/passwd
|
204
|
+
stream.readable = false;
|
205
|
+
stream.read = null;
|
206
|
+
stream._type = 'pipe';
|
207
|
+
|
208
|
+
// FIXME Hack to have stream not keep the event loop alive.
|
209
|
+
// See https://github.com/joyent/node/issues/1726
|
210
|
+
if (stream._handle && stream._handle.unref) {
|
211
|
+
stream._handle.unref();
|
212
|
+
}
|
213
|
+
break;
|
214
|
+
|
215
|
+
default:
|
216
|
+
// Probably an error on in uv_guess_handle()
|
217
|
+
throw new Error('Implement me. Unknown stream file type!');
|
218
|
+
}
|
219
|
+
|
220
|
+
// For supporting legacy API we put the FD here.
|
221
|
+
stream.fd = fd;
|
222
|
+
|
223
|
+
stream._isStdio = true;
|
224
|
+
|
225
|
+
return stream;
|
226
|
+
}
|
227
|
+
|
228
|
+
/**
|
229
|
+
* Init logic for `debug` instances.
|
230
|
+
*
|
231
|
+
* Create a new `inspectOpts` object in case `useColors` is set
|
232
|
+
* differently for a particular `debug` instance.
|
233
|
+
*/
|
234
|
+
|
235
|
+
function init (debug) {
|
236
|
+
debug.inspectOpts = {};
|
237
|
+
|
238
|
+
var keys = Object.keys(exports.inspectOpts);
|
239
|
+
for (var i = 0; i < keys.length; i++) {
|
240
|
+
debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
|
241
|
+
}
|
242
|
+
}
|
243
|
+
|
244
|
+
/**
|
245
|
+
* Enable namespaces listed in `process.env.DEBUG` initially.
|
246
|
+
*/
|
247
|
+
|
248
|
+
exports.enable(load());
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2014 Jonathan Ong me@jongleberry.com
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# EE First
|
2
|
+
|
3
|
+
[![NPM version][npm-image]][npm-url]
|
4
|
+
[![Build status][travis-image]][travis-url]
|
5
|
+
[![Test coverage][coveralls-image]][coveralls-url]
|
6
|
+
[![License][license-image]][license-url]
|
7
|
+
[![Downloads][downloads-image]][downloads-url]
|
8
|
+
[![Gittip][gittip-image]][gittip-url]
|
9
|
+
|
10
|
+
Get the first event in a set of event emitters and event pairs,
|
11
|
+
then clean up after itself.
|
12
|
+
|
13
|
+
## Install
|
14
|
+
|
15
|
+
```sh
|
16
|
+
$ npm install ee-first
|
17
|
+
```
|
18
|
+
|
19
|
+
## API
|
20
|
+
|
21
|
+
```js
|
22
|
+
var first = require('ee-first')
|
23
|
+
```
|
24
|
+
|
25
|
+
### first(arr, listener)
|
26
|
+
|
27
|
+
Invoke `listener` on the first event from the list specified in `arr`. `arr` is
|
28
|
+
an array of arrays, with each array in the format `[ee, ...event]`. `listener`
|
29
|
+
will be called only once, the first time any of the given events are emitted. If
|
30
|
+
`error` is one of the listened events, then if that fires first, the `listener`
|
31
|
+
will be given the `err` argument.
|
32
|
+
|
33
|
+
The `listener` is invoked as `listener(err, ee, event, args)`, where `err` is the
|
34
|
+
first argument emitted from an `error` event, if applicable; `ee` is the event
|
35
|
+
emitter that fired; `event` is the string event name that fired; and `args` is an
|
36
|
+
array of the arguments that were emitted on the event.
|
37
|
+
|
38
|
+
```js
|
39
|
+
var ee1 = new EventEmitter()
|
40
|
+
var ee2 = new EventEmitter()
|
41
|
+
|
42
|
+
first([
|
43
|
+
[ee1, 'close', 'end', 'error'],
|
44
|
+
[ee2, 'error']
|
45
|
+
], function (err, ee, event, args) {
|
46
|
+
// listener invoked
|
47
|
+
})
|
48
|
+
```
|
49
|
+
|
50
|
+
#### .cancel()
|
51
|
+
|
52
|
+
The group of listeners can be cancelled before being invoked and have all the event
|
53
|
+
listeners removed from the underlying event emitters.
|
54
|
+
|
55
|
+
```js
|
56
|
+
var thunk = first([
|
57
|
+
[ee1, 'close', 'end', 'error'],
|
58
|
+
[ee2, 'error']
|
59
|
+
], function (err, ee, event, args) {
|
60
|
+
// listener invoked
|
61
|
+
})
|
62
|
+
|
63
|
+
// cancel and clean up
|
64
|
+
thunk.cancel()
|
65
|
+
```
|
66
|
+
|
67
|
+
[npm-image]: https://img.shields.io/npm/v/ee-first.svg?style=flat-square
|
68
|
+
[npm-url]: https://npmjs.org/package/ee-first
|
69
|
+
[github-tag]: http://img.shields.io/github/tag/jonathanong/ee-first.svg?style=flat-square
|
70
|
+
[github-url]: https://github.com/jonathanong/ee-first/tags
|
71
|
+
[travis-image]: https://img.shields.io/travis/jonathanong/ee-first.svg?style=flat-square
|
72
|
+
[travis-url]: https://travis-ci.org/jonathanong/ee-first
|
73
|
+
[coveralls-image]: https://img.shields.io/coveralls/jonathanong/ee-first.svg?style=flat-square
|
74
|
+
[coveralls-url]: https://coveralls.io/r/jonathanong/ee-first?branch=master
|
75
|
+
[license-image]: http://img.shields.io/npm/l/ee-first.svg?style=flat-square
|
76
|
+
[license-url]: LICENSE.md
|
77
|
+
[downloads-image]: http://img.shields.io/npm/dm/ee-first.svg?style=flat-square
|
78
|
+
[downloads-url]: https://npmjs.org/package/ee-first
|
79
|
+
[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square
|
80
|
+
[gittip-url]: https://www.gittip.com/jonathanong/
|
@@ -0,0 +1,95 @@
|
|
1
|
+
/*!
|
2
|
+
* ee-first
|
3
|
+
* Copyright(c) 2014 Jonathan Ong
|
4
|
+
* MIT Licensed
|
5
|
+
*/
|
6
|
+
|
7
|
+
'use strict'
|
8
|
+
|
9
|
+
/**
|
10
|
+
* Module exports.
|
11
|
+
* @public
|
12
|
+
*/
|
13
|
+
|
14
|
+
module.exports = first
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Get the first event in a set of event emitters and event pairs.
|
18
|
+
*
|
19
|
+
* @param {array} stuff
|
20
|
+
* @param {function} done
|
21
|
+
* @public
|
22
|
+
*/
|
23
|
+
|
24
|
+
function first(stuff, done) {
|
25
|
+
if (!Array.isArray(stuff))
|
26
|
+
throw new TypeError('arg must be an array of [ee, events...] arrays')
|
27
|
+
|
28
|
+
var cleanups = []
|
29
|
+
|
30
|
+
for (var i = 0; i < stuff.length; i++) {
|
31
|
+
var arr = stuff[i]
|
32
|
+
|
33
|
+
if (!Array.isArray(arr) || arr.length < 2)
|
34
|
+
throw new TypeError('each array member must be [ee, events...]')
|
35
|
+
|
36
|
+
var ee = arr[0]
|
37
|
+
|
38
|
+
for (var j = 1; j < arr.length; j++) {
|
39
|
+
var event = arr[j]
|
40
|
+
var fn = listener(event, callback)
|
41
|
+
|
42
|
+
// listen to the event
|
43
|
+
ee.on(event, fn)
|
44
|
+
// push this listener to the list of cleanups
|
45
|
+
cleanups.push({
|
46
|
+
ee: ee,
|
47
|
+
event: event,
|
48
|
+
fn: fn,
|
49
|
+
})
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
function callback() {
|
54
|
+
cleanup()
|
55
|
+
done.apply(null, arguments)
|
56
|
+
}
|
57
|
+
|
58
|
+
function cleanup() {
|
59
|
+
var x
|
60
|
+
for (var i = 0; i < cleanups.length; i++) {
|
61
|
+
x = cleanups[i]
|
62
|
+
x.ee.removeListener(x.event, x.fn)
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
function thunk(fn) {
|
67
|
+
done = fn
|
68
|
+
}
|
69
|
+
|
70
|
+
thunk.cancel = cleanup
|
71
|
+
|
72
|
+
return thunk
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Create the event listener.
|
77
|
+
* @private
|
78
|
+
*/
|
79
|
+
|
80
|
+
function listener(event, done) {
|
81
|
+
return function onevent(arg1) {
|
82
|
+
var args = new Array(arguments.length)
|
83
|
+
var ee = this
|
84
|
+
var err = event === 'error'
|
85
|
+
? arg1
|
86
|
+
: null
|
87
|
+
|
88
|
+
// copy args to prevent arguments escaping scope
|
89
|
+
for (var i = 0; i < args.length; i++) {
|
90
|
+
args[i] = arguments[i]
|
91
|
+
}
|
92
|
+
|
93
|
+
done(err, ee, event, args)
|
94
|
+
}
|
95
|
+
}
|