diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000000..5ac9885534
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,7 @@
+language: node_js
+node_js:
+ - "0.8"
+ - "0.10"
+ - "0.11"
+ - "0.12"
+ - "iojs"
diff --git a/README.md b/README.md
index 3fb3e8023e..c4cbfd0824 100644
--- a/README.md
+++ b/README.md
@@ -2,14 +2,33 @@
***Node-core streams for userland***
+
+[](https://travis-ci.org/nodejs/readable-stream)
[](https://nodei.co/npm/readable-stream/)
[](https://nodei.co/npm/readable-stream/)
-This package is a mirror of the Streams2 and Streams3 implementations in Node-core.
+```bash
+npm install --save readable-stream
+```
+
+***Node-core streams for userland***
+
+This package is a mirror of the Streams2 and Streams3 implementations in
+Node-core.
-If you want to guarantee a stable streams base, regardless of what version of Node you, or the users of your libraries are using, use **readable-stream** *only* and avoid the *"stream"* module in Node-core.
+If you want to guarantee a stable streams base, regardless of what version of
+Node you, or the users of your libraries are using, use **readable-stream** *only* and avoid the *"stream"* module in Node-core.
-**readable-stream** comes in two major versions, v1.0.x and v1.1.x. The former tracks the Streams2 implementation in Node 0.10, including bug-fixes and minor improvements as they are added. The latter tracks Streams3 as it develops in Node 0.11; we will likely see a v1.2.x branch for Node 0.12.
+As of version 2.0.0 **readable-stream** uses semantic versioning.
-**readable-stream** uses proper patch-level versioning so if you pin to `"~1.0.0"` you’ll get the latest Node 0.10 Streams2 implementation, including any fixes and minor non-breaking improvements. The patch-level versions of 1.0.x and 1.1.x should mirror the patch-level versions of Node-core releases. You should prefer the **1.0.x** releases for now and when you’re ready to start using Streams3, pin to `"~1.1.0"`
+# Streams WG Team Members
+* **Chris Dickinson** ([@chrisdickinson](https://github.com/chrisdickinson)) <christopher.s.dickinson@gmail.com>
+ - Release GPG key: 9554F04D7259F04124DE6B476D5A82AC7E37093B
+* **Calvin Metcalf** ([@calvinmetcalf](https://github.com/calvinmetcalf)) $lt;calvin.metcalf@gmail.com@gt;
+ - Release GPG key: F3EF5F62A87FC27A22E643F714CE4FF5015AA242
+* **Rod Vagg** ([@rvagg](https://github.com/rvagg)) <rod@vagg.org>
+ - Release GPG key: DD8F2338BAE7501E3DD5AC78C273792F7D83545D
+* **Sam Newman** ([@sonewman](https://github.com/sonewman)) <newmansam@outlook.com>
+* **Mathias Buus** ([@mafintosh](https://github.com/mafintosh)) <mathiasbuus@gmail.com>
+* **Domenic Denicola** ([@domenic](https://github.com/domenic)) <d@domenic.me>
diff --git a/build/build.js b/build/build.js
index 511cfe8f24..aea2875dad 100755
--- a/build/build.js
+++ b/build/build.js
@@ -9,10 +9,10 @@ const hyperquest = require('hyperzip')(require('hyperdirect'))
, files = require('./files')
, testReplace = require('./test-replacements')
- , srcurlpfx = 'https://raw.githubusercontent.com/iojs/io.js/v' + process.argv[2] + '-release/'
+ , srcurlpfx = 'https://raw.githubusercontent.com/nodejs/io.js/v' + process.argv[2] + '/'
, libsrcurl = srcurlpfx + 'lib/'
, testsrcurl = srcurlpfx + 'test/parallel/'
- , testlisturl = 'https://github.com/iojs/io.js/tree/v' + process.argv[2] + '-release/test/parallel'
+ , testlisturl = 'https://github.com/nodejs/io.js/tree/v' + process.argv[2] + '/test/parallel'
, libourroot = path.join(__dirname, '../lib/')
, testourroot = path.join(__dirname, '../test/parallel/')
diff --git a/build/files.js b/build/files.js
index 20b3eaf77a..78182d2401 100644
--- a/build/files.js
+++ b/build/files.js
@@ -98,10 +98,47 @@ const headRegexp = /(^module.exports = \w+;?)/m
+ 'if (!EE.listenerCount) EE.listenerCount = function(emitter, type) {\n'
+ ' return emitter.listeners(type).length;\n'
+ '};\n/**/\n'
+ ]
+
+ , constReplacement = [
+ /const/g
+ , 'var'
+ ]
+
+ , bufferIsEncodingReplacement = [
+ /Buffer.isEncoding\((\w+)\)/
+ , '([\'hex\', \'utf8\', \'utf-8\', \'ascii\', \'binary\', \'base64\',\n'
+ + '\'ucs2\', \'ucs-2\',\'utf16le\', \'utf-16le\', \'raw\']\n'
+ + '.indexOf(($1 + \'\').toLowerCase()) > -1)'
+ ]
+
+ , requireStreamReplacement = [
+ /var Stream = require\('stream'\);/
+ , '\n\n/**/\n'
+ + 'var Stream;\n (function (){try{\n'
+ + 'Stream = require(\'st\' + \'ream\');\n'
+ + '}catch(_){Stream = require(\'events\').EventEmitter;}}())'
+ + '\n/**/\n'
+ ]
+
+ , isBufferReplacement = [
+ /(\w+) instanceof Buffer/
+ , 'Buffer.isBuffer($1)'
+ ]
+
+ , processNextTickImport = [
+ headRegexp
+ , '$1\n\n/**/\nvar processNextTick = require(\'process-nextick-args\');\n/**/\n'
+ ]
+
+ , processNextTickReplacement = [
+ /process.nextTick\(/g
+ , 'processNextTick('
]
module.exports['_stream_duplex.js'] = [
- requireReplacement
+ constReplacement
+ , requireReplacement
, instanceofReplacement
, utilReplacement
, stringDecoderReplacement
@@ -109,17 +146,21 @@ module.exports['_stream_duplex.js'] = [
, altForEachUseReplacement
, objectKeysReplacement
, objectKeysDefine
+ , processNextTickImport
+ , processNextTickReplacement
]
module.exports['_stream_passthrough.js'] = [
- requireReplacement
+ constReplacement
+ , requireReplacement
, instanceofReplacement
, utilReplacement
, stringDecoderReplacement
]
module.exports['_stream_readable.js'] = [
- addDuplexRequire
+ constReplacement
+ , addDuplexRequire
, requireReplacement
, instanceofReplacement
, bufferReplacement
@@ -134,20 +175,25 @@ module.exports['_stream_readable.js'] = [
, debugLogReplacement
, utilReplacement
, stringDecoderReplacement
- , debugLogReplacement
, eventEmittterReplacement
+ , requireStreamReplacement
+ , isBufferReplacement
+ , processNextTickImport
+ , processNextTickReplacement
]
module.exports['_stream_transform.js'] = [
- requireReplacement
+ constReplacement
+ , requireReplacement
, instanceofReplacement
, utilReplacement
, stringDecoderReplacement
]
module.exports['_stream_writable.js'] = [
- addDuplexRequire
+ constReplacement
+ , addDuplexRequire
, requireReplacement
, instanceofReplacement
, bufferReplacement
@@ -156,5 +202,10 @@ module.exports['_stream_writable.js'] = [
, debugLogReplacement
, deprecateReplacement
, objectDefinePropertyReplacement
+ , bufferIsEncodingReplacement
, [ /^var assert = require\('assert'\);$/m, '' ]
+ , requireStreamReplacement
+ , isBufferReplacement
+ , processNextTickImport
+ , processNextTickReplacement
]
diff --git a/build/test-replacements.js b/build/test-replacements.js
index e86fa67e1e..4b2ace251b 100644
--- a/build/test-replacements.js
+++ b/build/test-replacements.js
@@ -76,21 +76,27 @@ module.exports['common.js'] = [
]
// for streams2 on node 0.11
+ // and dtrace in 0.10
, [
/^( for \(var x in global\) \{|function leakedGlobals\(\) \{)$/m
, ' /**/\n'
+ ' if (typeof constructor == \'function\')\n'
+ ' knownGlobals.push(constructor);\n'
+ + ' if (typeof DTRACE_NET_SOCKET_READ == \'function\')\n'
+ + ' knownGlobals.push(DTRACE_NET_SOCKET_READ);\n'
+ + ' if (typeof DTRACE_NET_SOCKET_WRITE == \'function\')\n'
+ + ' knownGlobals.push(DTRACE_NET_SOCKET_WRITE);\n'
+ ' /**/\n\n$1'
]
// for node 0.8
, [
- /$/
+ /^/
, '/**/'
+ '\nif (!global.setImmediate) {\n'
+ ' global.setImmediate = function setImmediate(fn) {\n'
- + ' return setTimeout(fn, 0);\n'
+
+ + ' return setTimeout(fn.bind.apply(fn, arguments), 0);\n'
+ ' };\n'
+ '}\n'
+ 'if (!global.clearImmediate) {\n'
@@ -144,3 +150,10 @@ module.exports['test-stream2-stderr-sync.js'] = [
+ ' return console.error(\'child 0\\nfoo\\nbar\\nbaz\');\n'
]
]
+
+module.exports['test-stream-unshift-read-race.js'] = [
+ [
+ /data\.slice\(pos, pos \+ n\)/g,
+ 'data.slice(pos, Math.min(pos + n, data.length))'
+ ]
+]
diff --git a/examples/typer-fsr.js b/examples/typer-fsr.js
deleted file mode 100644
index 7e715844bd..0000000000
--- a/examples/typer-fsr.js
+++ /dev/null
@@ -1,15 +0,0 @@
-var fs = require('fs');
-var FSReadable = require('../fs.js');
-var rst = new FSReadable(__filename);
-
-rst.on('end', function() {
- process.stdin.pause();
-});
-
-process.stdin.setRawMode(true);
-process.stdin.on('data', function() {
- var c = rst.read(3);
- if (!c) return;
- process.stdout.write(c);
-});
-process.stdin.resume();
diff --git a/float.patch b/float.patch
deleted file mode 100644
index b984607a41..0000000000
--- a/float.patch
+++ /dev/null
@@ -1,923 +0,0 @@
-diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js
-index c5a741c..a2e0d8e 100644
---- a/lib/_stream_duplex.js
-+++ b/lib/_stream_duplex.js
-@@ -26,8 +26,8 @@
-
- module.exports = Duplex;
- var util = require('util');
--var Readable = require('_stream_readable');
--var Writable = require('_stream_writable');
-+var Readable = require('./_stream_readable');
-+var Writable = require('./_stream_writable');
-
- util.inherits(Duplex, Readable);
-
-diff --git a/lib/_stream_passthrough.js b/lib/_stream_passthrough.js
-index a5e9864..330c247 100644
---- a/lib/_stream_passthrough.js
-+++ b/lib/_stream_passthrough.js
-@@ -25,7 +25,7 @@
-
- module.exports = PassThrough;
-
--var Transform = require('_stream_transform');
-+var Transform = require('./_stream_transform');
- var util = require('util');
- util.inherits(PassThrough, Transform);
-
-diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js
-index 0c3fe3e..90a8298 100644
---- a/lib/_stream_readable.js
-+++ b/lib/_stream_readable.js
-@@ -23,10 +23,34 @@ module.exports = Readable;
- Readable.ReadableState = ReadableState;
-
- var EE = require('events').EventEmitter;
-+if (!EE.listenerCount) EE.listenerCount = function(emitter, type) {
-+ return emitter.listeners(type).length;
-+};
-+
-+if (!global.setImmediate) global.setImmediate = function setImmediate(fn) {
-+ return setTimeout(fn, 0);
-+};
-+if (!global.clearImmediate) global.clearImmediate = function clearImmediate(i) {
-+ return clearTimeout(i);
-+};
-+
- var Stream = require('stream');
- var util = require('util');
-+if (!util.isUndefined) {
-+ var utilIs = require('core-util-is');
-+ for (var f in utilIs) {
-+ util[f] = utilIs[f];
-+ }
-+}
- var StringDecoder;
--var debug = util.debuglog('stream');
-+var debug;
-+if (util.debuglog)
-+ debug = util.debuglog('stream');
-+else try {
-+ debug = require('debuglog')('stream');
-+} catch (er) {
-+ debug = function() {};
-+}
-
- util.inherits(Readable, Stream);
-
-@@ -380,7 +404,7 @@ function chunkInvalid(state, chunk) {
-
-
- function onEofChunk(stream, state) {
-- if (state.decoder && !state.ended) {
-+ if (state.decoder && !state.ended && state.decoder.end) {
- var chunk = state.decoder.end();
- if (chunk && chunk.length) {
- state.buffer.push(chunk);
-diff --git a/lib/_stream_transform.js b/lib/_stream_transform.js
-index b1f9fcc..b0caf57 100644
---- a/lib/_stream_transform.js
-+++ b/lib/_stream_transform.js
-@@ -64,8 +64,14 @@
-
- module.exports = Transform;
-
--var Duplex = require('_stream_duplex');
-+var Duplex = require('./_stream_duplex');
- var util = require('util');
-+if (!util.isUndefined) {
-+ var utilIs = require('core-util-is');
-+ for (var f in utilIs) {
-+ util[f] = utilIs[f];
-+ }
-+}
- util.inherits(Transform, Duplex);
-
-
-diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js
-index ba2e920..f49288b 100644
---- a/lib/_stream_writable.js
-+++ b/lib/_stream_writable.js
-@@ -27,6 +27,12 @@ module.exports = Writable;
- Writable.WritableState = WritableState;
-
- var util = require('util');
-+if (!util.isUndefined) {
-+ var utilIs = require('core-util-is');
-+ for (var f in utilIs) {
-+ util[f] = utilIs[f];
-+ }
-+}
- var Stream = require('stream');
-
- util.inherits(Writable, Stream);
-@@ -119,7 +125,7 @@ function WritableState(options, stream) {
- function Writable(options) {
- // Writable ctor is applied to Duplexes, though they're not
- // instanceof Writable, they're instanceof Readable.
-- if (!(this instanceof Writable) && !(this instanceof Stream.Duplex))
-+ if (!(this instanceof Writable) && !(this instanceof require('./_stream_duplex')))
- return new Writable(options);
-
- this._writableState = new WritableState(options, this);
-diff --git a/test/simple/test-stream-big-push.js b/test/simple/test-stream-big-push.js
-index e3787e4..8cd2127 100644
---- a/test/simple/test-stream-big-push.js
-+++ b/test/simple/test-stream-big-push.js
-@@ -21,7 +21,7 @@
-
- var common = require('../common');
- var assert = require('assert');
--var stream = require('stream');
-+var stream = require('../../');
- var str = 'asdfasdfasdfasdfasdf';
-
- var r = new stream.Readable({
-diff --git a/test/simple/test-stream-end-paused.js b/test/simple/test-stream-end-paused.js
-index bb73777..d40efc7 100644
---- a/test/simple/test-stream-end-paused.js
-+++ b/test/simple/test-stream-end-paused.js
-@@ -25,7 +25,7 @@ var gotEnd = false;
-
- // Make sure we don't miss the end event for paused 0-length streams
-
--var Readable = require('stream').Readable;
-+var Readable = require('../../').Readable;
- var stream = new Readable();
- var calledRead = false;
- stream._read = function() {
-diff --git a/test/simple/test-stream-pipe-after-end.js b/test/simple/test-stream-pipe-after-end.js
-index b46ee90..0be8366 100644
---- a/test/simple/test-stream-pipe-after-end.js
-+++ b/test/simple/test-stream-pipe-after-end.js
-@@ -22,8 +22,8 @@
- var common = require('../common');
- var assert = require('assert');
-
--var Readable = require('_stream_readable');
--var Writable = require('_stream_writable');
-+var Readable = require('../../lib/_stream_readable');
-+var Writable = require('../../lib/_stream_writable');
- var util = require('util');
-
- util.inherits(TestReadable, Readable);
-diff --git a/test/simple/test-stream-pipe-cleanup.js b/test/simple/test-stream-pipe-cleanup.js
-deleted file mode 100644
-index f689358..0000000
---- a/test/simple/test-stream-pipe-cleanup.js
-+++ /dev/null
-@@ -1,122 +0,0 @@
--// Copyright Joyent, Inc. and other Node contributors.
--//
--// 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.
--
--// This test asserts that Stream.prototype.pipe does not leave listeners
--// hanging on the source or dest.
--
--var common = require('../common');
--var stream = require('stream');
--var assert = require('assert');
--var util = require('util');
--
--function Writable() {
-- this.writable = true;
-- this.endCalls = 0;
-- stream.Stream.call(this);
--}
--util.inherits(Writable, stream.Stream);
--Writable.prototype.end = function() {
-- this.endCalls++;
--};
--
--Writable.prototype.destroy = function() {
-- this.endCalls++;
--};
--
--function Readable() {
-- this.readable = true;
-- stream.Stream.call(this);
--}
--util.inherits(Readable, stream.Stream);
--
--function Duplex() {
-- this.readable = true;
-- Writable.call(this);
--}
--util.inherits(Duplex, Writable);
--
--var i = 0;
--var limit = 100;
--
--var w = new Writable();
--
--var r;
--
--for (i = 0; i < limit; i++) {
-- r = new Readable();
-- r.pipe(w);
-- r.emit('end');
--}
--assert.equal(0, r.listeners('end').length);
--assert.equal(limit, w.endCalls);
--
--w.endCalls = 0;
--
--for (i = 0; i < limit; i++) {
-- r = new Readable();
-- r.pipe(w);
-- r.emit('close');
--}
--assert.equal(0, r.listeners('close').length);
--assert.equal(limit, w.endCalls);
--
--w.endCalls = 0;
--
--r = new Readable();
--
--for (i = 0; i < limit; i++) {
-- w = new Writable();
-- r.pipe(w);
-- w.emit('close');
--}
--assert.equal(0, w.listeners('close').length);
--
--r = new Readable();
--w = new Writable();
--var d = new Duplex();
--r.pipe(d); // pipeline A
--d.pipe(w); // pipeline B
--assert.equal(r.listeners('end').length, 2); // A.onend, A.cleanup
--assert.equal(r.listeners('close').length, 2); // A.onclose, A.cleanup
--assert.equal(d.listeners('end').length, 2); // B.onend, B.cleanup
--assert.equal(d.listeners('close').length, 3); // A.cleanup, B.onclose, B.cleanup
--assert.equal(w.listeners('end').length, 0);
--assert.equal(w.listeners('close').length, 1); // B.cleanup
--
--r.emit('end');
--assert.equal(d.endCalls, 1);
--assert.equal(w.endCalls, 0);
--assert.equal(r.listeners('end').length, 0);
--assert.equal(r.listeners('close').length, 0);
--assert.equal(d.listeners('end').length, 2); // B.onend, B.cleanup
--assert.equal(d.listeners('close').length, 2); // B.onclose, B.cleanup
--assert.equal(w.listeners('end').length, 0);
--assert.equal(w.listeners('close').length, 1); // B.cleanup
--
--d.emit('end');
--assert.equal(d.endCalls, 1);
--assert.equal(w.endCalls, 1);
--assert.equal(r.listeners('end').length, 0);
--assert.equal(r.listeners('close').length, 0);
--assert.equal(d.listeners('end').length, 0);
--assert.equal(d.listeners('close').length, 0);
--assert.equal(w.listeners('end').length, 0);
--assert.equal(w.listeners('close').length, 0);
-diff --git a/test/simple/test-stream-pipe-error-handling.js b/test/simple/test-stream-pipe-error-handling.js
-index c5d724b..c7d6b7d 100644
---- a/test/simple/test-stream-pipe-error-handling.js
-+++ b/test/simple/test-stream-pipe-error-handling.js
-@@ -21,7 +21,7 @@
-
- var common = require('../common');
- var assert = require('assert');
--var Stream = require('stream').Stream;
-+var Stream = require('../../').Stream;
-
- (function testErrorListenerCatches() {
- var source = new Stream();
-diff --git a/test/simple/test-stream-pipe-event.js b/test/simple/test-stream-pipe-event.js
-index cb9d5fe..56f8d61 100644
---- a/test/simple/test-stream-pipe-event.js
-+++ b/test/simple/test-stream-pipe-event.js
-@@ -20,7 +20,7 @@
- // USE OR OTHER DEALINGS IN THE SOFTWARE.
-
- var common = require('../common');
--var stream = require('stream');
-+var stream = require('../../');
- var assert = require('assert');
- var util = require('util');
-
-diff --git a/test/simple/test-stream-push-order.js b/test/simple/test-stream-push-order.js
-index f2e6ec2..a5c9bf9 100644
---- a/test/simple/test-stream-push-order.js
-+++ b/test/simple/test-stream-push-order.js
-@@ -20,7 +20,7 @@
- // USE OR OTHER DEALINGS IN THE SOFTWARE.
-
- var common = require('../common.js');
--var Readable = require('stream').Readable;
-+var Readable = require('../../').Readable;
- var assert = require('assert');
-
- var s = new Readable({
-diff --git a/test/simple/test-stream-push-strings.js b/test/simple/test-stream-push-strings.js
-index 06f43dc..1701a9a 100644
---- a/test/simple/test-stream-push-strings.js
-+++ b/test/simple/test-stream-push-strings.js
-@@ -22,7 +22,7 @@
- var common = require('../common');
- var assert = require('assert');
-
--var Readable = require('stream').Readable;
-+var Readable = require('../../').Readable;
- var util = require('util');
-
- util.inherits(MyStream, Readable);
-diff --git a/test/simple/test-stream-readable-event.js b/test/simple/test-stream-readable-event.js
-index ba6a577..a8e6f7b 100644
---- a/test/simple/test-stream-readable-event.js
-+++ b/test/simple/test-stream-readable-event.js
-@@ -22,7 +22,7 @@
- var common = require('../common');
- var assert = require('assert');
-
--var Readable = require('stream').Readable;
-+var Readable = require('../../').Readable;
-
- (function first() {
- // First test, not reading when the readable is added.
-diff --git a/test/simple/test-stream-readable-flow-recursion.js b/test/simple/test-stream-readable-flow-recursion.js
-index 2891ad6..11689ba 100644
---- a/test/simple/test-stream-readable-flow-recursion.js
-+++ b/test/simple/test-stream-readable-flow-recursion.js
-@@ -27,7 +27,7 @@ var assert = require('assert');
- // more data continuously, but without triggering a nextTick
- // warning or RangeError.
-
--var Readable = require('stream').Readable;
-+var Readable = require('../../').Readable;
-
- // throw an error if we trigger a nextTick warning.
- process.throwDeprecation = true;
-diff --git a/test/simple/test-stream-unshift-empty-chunk.js b/test/simple/test-stream-unshift-empty-chunk.js
-index 0c96476..7827538 100644
---- a/test/simple/test-stream-unshift-empty-chunk.js
-+++ b/test/simple/test-stream-unshift-empty-chunk.js
-@@ -24,7 +24,7 @@ var assert = require('assert');
-
- // This test verifies that stream.unshift(Buffer(0)) or
- // stream.unshift('') does not set state.reading=false.
--var Readable = require('stream').Readable;
-+var Readable = require('../../').Readable;
-
- var r = new Readable();
- var nChunks = 10;
-diff --git a/test/simple/test-stream-unshift-read-race.js b/test/simple/test-stream-unshift-read-race.js
-index 83fd9fa..17c18aa 100644
---- a/test/simple/test-stream-unshift-read-race.js
-+++ b/test/simple/test-stream-unshift-read-race.js
-@@ -29,7 +29,7 @@ var assert = require('assert');
- // 3. push() after the EOF signaling null is an error.
- // 4. _read() is not called after pushing the EOF null chunk.
-
--var stream = require('stream');
-+var stream = require('../../');
- var hwm = 10;
- var r = stream.Readable({ highWaterMark: hwm });
- var chunks = 10;
-@@ -51,7 +51,14 @@ r._read = function(n) {
-
- function push(fast) {
- assert(!pushedNull, 'push() after null push');
-- var c = pos >= data.length ? null : data.slice(pos, pos + n);
-+ var c;
-+ if (pos >= data.length)
-+ c = null;
-+ else {
-+ if (n + pos > data.length)
-+ n = data.length - pos;
-+ c = data.slice(pos, pos + n);
-+ }
- pushedNull = c === null;
- if (fast) {
- pos += n;
-diff --git a/test/simple/test-stream-writev.js b/test/simple/test-stream-writev.js
-index 5b49e6e..b5321f3 100644
---- a/test/simple/test-stream-writev.js
-+++ b/test/simple/test-stream-writev.js
-@@ -22,7 +22,7 @@
- var common = require('../common');
- var assert = require('assert');
-
--var stream = require('stream');
-+var stream = require('../../');
-
- var queue = [];
- for (var decode = 0; decode < 2; decode++) {
-diff --git a/test/simple/test-stream2-basic.js b/test/simple/test-stream2-basic.js
-index 3814bf0..248c1be 100644
---- a/test/simple/test-stream2-basic.js
-+++ b/test/simple/test-stream2-basic.js
-@@ -21,7 +21,7 @@
-
-
- var common = require('../common.js');
--var R = require('_stream_readable');
-+var R = require('../../lib/_stream_readable');
- var assert = require('assert');
-
- var util = require('util');
-diff --git a/test/simple/test-stream2-compatibility.js b/test/simple/test-stream2-compatibility.js
-index 6cdd4e9..f0fa84b 100644
---- a/test/simple/test-stream2-compatibility.js
-+++ b/test/simple/test-stream2-compatibility.js
-@@ -21,7 +21,7 @@
-
-
- var common = require('../common.js');
--var R = require('_stream_readable');
-+var R = require('../../lib/_stream_readable');
- var assert = require('assert');
-
- var util = require('util');
-diff --git a/test/simple/test-stream2-finish-pipe.js b/test/simple/test-stream2-finish-pipe.js
-index 39b274f..006a19b 100644
---- a/test/simple/test-stream2-finish-pipe.js
-+++ b/test/simple/test-stream2-finish-pipe.js
-@@ -20,7 +20,7 @@
- // USE OR OTHER DEALINGS IN THE SOFTWARE.
-
- var common = require('../common.js');
--var stream = require('stream');
-+var stream = require('../../');
- var Buffer = require('buffer').Buffer;
-
- var r = new stream.Readable();
-diff --git a/test/simple/test-stream2-fs.js b/test/simple/test-stream2-fs.js
-deleted file mode 100644
-index e162406..0000000
---- a/test/simple/test-stream2-fs.js
-+++ /dev/null
-@@ -1,72 +0,0 @@
--// Copyright Joyent, Inc. and other Node contributors.
--//
--// 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.
--
--
--var common = require('../common.js');
--var R = require('_stream_readable');
--var assert = require('assert');
--
--var fs = require('fs');
--var FSReadable = fs.ReadStream;
--
--var path = require('path');
--var file = path.resolve(common.fixturesDir, 'x1024.txt');
--
--var size = fs.statSync(file).size;
--
--var expectLengths = [1024];
--
--var util = require('util');
--var Stream = require('stream');
--
--util.inherits(TestWriter, Stream);
--
--function TestWriter() {
-- Stream.apply(this);
-- this.buffer = [];
-- this.length = 0;
--}
--
--TestWriter.prototype.write = function(c) {
-- this.buffer.push(c.toString());
-- this.length += c.length;
-- return true;
--};
--
--TestWriter.prototype.end = function(c) {
-- if (c) this.buffer.push(c.toString());
-- this.emit('results', this.buffer);
--}
--
--var r = new FSReadable(file);
--var w = new TestWriter();
--
--w.on('results', function(res) {
-- console.error(res, w.length);
-- assert.equal(w.length, size);
-- var l = 0;
-- assert.deepEqual(res.map(function (c) {
-- return c.length;
-- }), expectLengths);
-- console.log('ok');
--});
--
--r.pipe(w);
-diff --git a/test/simple/test-stream2-httpclient-response-end.js b/test/simple/test-stream2-httpclient-response-end.js
-deleted file mode 100644
-index 15cffc2..0000000
---- a/test/simple/test-stream2-httpclient-response-end.js
-+++ /dev/null
-@@ -1,52 +0,0 @@
--// Copyright Joyent, Inc. and other Node contributors.
--//
--// 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.
--
--var common = require('../common.js');
--var assert = require('assert');
--var http = require('http');
--var msg = 'Hello';
--var readable_event = false;
--var end_event = false;
--var server = http.createServer(function(req, res) {
-- res.writeHead(200, {'Content-Type': 'text/plain'});
-- res.end(msg);
--}).listen(common.PORT, function() {
-- http.get({port: common.PORT}, function(res) {
-- var data = '';
-- res.on('readable', function() {
-- console.log('readable event');
-- readable_event = true;
-- data += res.read();
-- });
-- res.on('end', function() {
-- console.log('end event');
-- end_event = true;
-- assert.strictEqual(msg, data);
-- server.close();
-- });
-- });
--});
--
--process.on('exit', function() {
-- assert(readable_event);
-- assert(end_event);
--});
--
-diff --git a/test/simple/test-stream2-large-read-stall.js b/test/simple/test-stream2-large-read-stall.js
-index 2fbfbca..667985b 100644
---- a/test/simple/test-stream2-large-read-stall.js
-+++ b/test/simple/test-stream2-large-read-stall.js
-@@ -30,7 +30,7 @@ var PUSHSIZE = 20;
- var PUSHCOUNT = 1000;
- var HWM = 50;
-
--var Readable = require('stream').Readable;
-+var Readable = require('../../').Readable;
- var r = new Readable({
- highWaterMark: HWM
- });
-@@ -39,23 +39,23 @@ var rs = r._readableState;
- r._read = push;
-
- r.on('readable', function() {
-- console.error('>> readable');
-+ //console.error('>> readable');
- do {
-- console.error(' > read(%d)', READSIZE);
-+ //console.error(' > read(%d)', READSIZE);
- var ret = r.read(READSIZE);
-- console.error(' < %j (%d remain)', ret && ret.length, rs.length);
-+ //console.error(' < %j (%d remain)', ret && ret.length, rs.length);
- } while (ret && ret.length === READSIZE);
-
-- console.error('<< after read()',
-- ret && ret.length,
-- rs.needReadable,
-- rs.length);
-+ //console.error('<< after read()',
-+ // ret && ret.length,
-+ // rs.needReadable,
-+ // rs.length);
- });
-
- var endEmitted = false;
- r.on('end', function() {
- endEmitted = true;
-- console.error('end');
-+ //console.error('end');
- });
-
- var pushes = 0;
-@@ -64,11 +64,11 @@ function push() {
- return;
-
- if (pushes++ === PUSHCOUNT) {
-- console.error(' push(EOF)');
-+ //console.error(' push(EOF)');
- return r.push(null);
- }
-
-- console.error(' push #%d', pushes);
-+ //console.error(' push #%d', pushes);
- if (r.push(new Buffer(PUSHSIZE)))
- setTimeout(push);
- }
-diff --git a/test/simple/test-stream2-objects.js b/test/simple/test-stream2-objects.js
-index 3e6931d..ff47d89 100644
---- a/test/simple/test-stream2-objects.js
-+++ b/test/simple/test-stream2-objects.js
-@@ -21,8 +21,8 @@
-
-
- var common = require('../common.js');
--var Readable = require('_stream_readable');
--var Writable = require('_stream_writable');
-+var Readable = require('../../lib/_stream_readable');
-+var Writable = require('../../lib/_stream_writable');
- var assert = require('assert');
-
- // tiny node-tap lookalike.
-diff --git a/test/simple/test-stream2-pipe-error-handling.js b/test/simple/test-stream2-pipe-error-handling.js
-index cf7531c..e3f3e4e 100644
---- a/test/simple/test-stream2-pipe-error-handling.js
-+++ b/test/simple/test-stream2-pipe-error-handling.js
-@@ -21,7 +21,7 @@
-
- var common = require('../common');
- var assert = require('assert');
--var stream = require('stream');
-+var stream = require('../../');
-
- (function testErrorListenerCatches() {
- var count = 1000;
-diff --git a/test/simple/test-stream2-pipe-error-once-listener.js b/test/simple/test-stream2-pipe-error-once-listener.js
-index 5e8e3cb..53b2616 100755
---- a/test/simple/test-stream2-pipe-error-once-listener.js
-+++ b/test/simple/test-stream2-pipe-error-once-listener.js
-@@ -24,7 +24,7 @@ var common = require('../common.js');
- var assert = require('assert');
-
- var util = require('util');
--var stream = require('stream');
-+var stream = require('../../');
-
-
- var Read = function() {
-diff --git a/test/simple/test-stream2-push.js b/test/simple/test-stream2-push.js
-index b63edc3..eb2b0e9 100644
---- a/test/simple/test-stream2-push.js
-+++ b/test/simple/test-stream2-push.js
-@@ -20,7 +20,7 @@
- // USE OR OTHER DEALINGS IN THE SOFTWARE.
-
- var common = require('../common.js');
--var stream = require('stream');
-+var stream = require('../../');
- var Readable = stream.Readable;
- var Writable = stream.Writable;
- var assert = require('assert');
-diff --git a/test/simple/test-stream2-read-sync-stack.js b/test/simple/test-stream2-read-sync-stack.js
-index e8a7305..9740a47 100644
---- a/test/simple/test-stream2-read-sync-stack.js
-+++ b/test/simple/test-stream2-read-sync-stack.js
-@@ -21,7 +21,7 @@
-
- var common = require('../common');
- var assert = require('assert');
--var Readable = require('stream').Readable;
-+var Readable = require('../../').Readable;
- var r = new Readable();
- var N = 256 * 1024;
-
-diff --git a/test/simple/test-stream2-readable-empty-buffer-no-eof.js b/test/simple/test-stream2-readable-empty-buffer-no-eof.js
-index cd30178..4b1659d 100644
---- a/test/simple/test-stream2-readable-empty-buffer-no-eof.js
-+++ b/test/simple/test-stream2-readable-empty-buffer-no-eof.js
-@@ -22,10 +22,9 @@
- var common = require('../common');
- var assert = require('assert');
-
--var Readable = require('stream').Readable;
-+var Readable = require('../../').Readable;
-
- test1();
--test2();
-
- function test1() {
- var r = new Readable();
-@@ -88,31 +87,3 @@ function test1() {
- console.log('ok');
- });
- }
--
--function test2() {
-- var r = new Readable({ encoding: 'base64' });
-- var reads = 5;
-- r._read = function(n) {
-- if (!reads--)
-- return r.push(null); // EOF
-- else
-- return r.push(new Buffer('x'));
-- };
--
-- var results = [];
-- function flow() {
-- var chunk;
-- while (null !== (chunk = r.read()))
-- results.push(chunk + '');
-- }
-- r.on('readable', flow);
-- r.on('end', function() {
-- results.push('EOF');
-- });
-- flow();
--
-- process.on('exit', function() {
-- assert.deepEqual(results, [ 'eHh4', 'eHg=', 'EOF' ]);
-- console.log('ok');
-- });
--}
-diff --git a/test/simple/test-stream2-readable-from-list.js b/test/simple/test-stream2-readable-from-list.js
-index 7c96ffe..04a96f5 100644
---- a/test/simple/test-stream2-readable-from-list.js
-+++ b/test/simple/test-stream2-readable-from-list.js
-@@ -21,7 +21,7 @@
-
- var assert = require('assert');
- var common = require('../common.js');
--var fromList = require('_stream_readable')._fromList;
-+var fromList = require('../../lib/_stream_readable')._fromList;
-
- // tiny node-tap lookalike.
- var tests = [];
-diff --git a/test/simple/test-stream2-readable-legacy-drain.js b/test/simple/test-stream2-readable-legacy-drain.js
-index 675da8e..51fd3d5 100644
---- a/test/simple/test-stream2-readable-legacy-drain.js
-+++ b/test/simple/test-stream2-readable-legacy-drain.js
-@@ -22,7 +22,7 @@
- var common = require('../common');
- var assert = require('assert');
-
--var Stream = require('stream');
-+var Stream = require('../../');
- var Readable = Stream.Readable;
-
- var r = new Readable();
-diff --git a/test/simple/test-stream2-readable-non-empty-end.js b/test/simple/test-stream2-readable-non-empty-end.js
-index 7314ae7..c971898 100644
---- a/test/simple/test-stream2-readable-non-empty-end.js
-+++ b/test/simple/test-stream2-readable-non-empty-end.js
-@@ -21,7 +21,7 @@
-
- var assert = require('assert');
- var common = require('../common.js');
--var Readable = require('_stream_readable');
-+var Readable = require('../../lib/_stream_readable');
-
- var len = 0;
- var chunks = new Array(10);
-diff --git a/test/simple/test-stream2-readable-wrap-empty.js b/test/simple/test-stream2-readable-wrap-empty.js
-index 2e5cf25..fd8a3dc 100644
---- a/test/simple/test-stream2-readable-wrap-empty.js
-+++ b/test/simple/test-stream2-readable-wrap-empty.js
-@@ -22,7 +22,7 @@
- var common = require('../common');
- var assert = require('assert');
-
--var Readable = require('_stream_readable');
-+var Readable = require('../../lib/_stream_readable');
- var EE = require('events').EventEmitter;
-
- var oldStream = new EE();
-diff --git a/test/simple/test-stream2-readable-wrap.js b/test/simple/test-stream2-readable-wrap.js
-index 90eea01..6b177f7 100644
---- a/test/simple/test-stream2-readable-wrap.js
-+++ b/test/simple/test-stream2-readable-wrap.js
-@@ -22,8 +22,8 @@
- var common = require('../common');
- var assert = require('assert');
-
--var Readable = require('_stream_readable');
--var Writable = require('_stream_writable');
-+var Readable = require('../../lib/_stream_readable');
-+var Writable = require('../../lib/_stream_writable');
- var EE = require('events').EventEmitter;
-
- var testRuns = 0, completedRuns = 0;
-diff --git a/test/simple/test-stream2-set-encoding.js b/test/simple/test-stream2-set-encoding.js
-index 5d2c32a..685531b 100644
---- a/test/simple/test-stream2-set-encoding.js
-+++ b/test/simple/test-stream2-set-encoding.js
-@@ -22,7 +22,7 @@
-
- var common = require('../common.js');
- var assert = require('assert');
--var R = require('_stream_readable');
-+var R = require('../../lib/_stream_readable');
- var util = require('util');
-
- // tiny node-tap lookalike.
-diff --git a/test/simple/test-stream2-transform.js b/test/simple/test-stream2-transform.js
-index 9c9ddd8..a0cacc6 100644
---- a/test/simple/test-stream2-transform.js
-+++ b/test/simple/test-stream2-transform.js
-@@ -21,8 +21,8 @@
-
- var assert = require('assert');
- var common = require('../common.js');
--var PassThrough = require('_stream_passthrough');
--var Transform = require('_stream_transform');
-+var PassThrough = require('../../').PassThrough;
-+var Transform = require('../../').Transform;
-
- // tiny node-tap lookalike.
- var tests = [];
-diff --git a/test/simple/test-stream2-unpipe-drain.js b/test/simple/test-stream2-unpipe-drain.js
-index d66dc3c..365b327 100644
---- a/test/simple/test-stream2-unpipe-drain.js
-+++ b/test/simple/test-stream2-unpipe-drain.js
-@@ -22,7 +22,7 @@
-
- var common = require('../common.js');
- var assert = require('assert');
--var stream = require('stream');
-+var stream = require('../../');
- var crypto = require('crypto');
-
- var util = require('util');
-diff --git a/test/simple/test-stream2-unpipe-leak.js b/test/simple/test-stream2-unpipe-leak.js
-index 99f8746..17c92ae 100644
---- a/test/simple/test-stream2-unpipe-leak.js
-+++ b/test/simple/test-stream2-unpipe-leak.js
-@@ -22,7 +22,7 @@
-
- var common = require('../common.js');
- var assert = require('assert');
--var stream = require('stream');
-+var stream = require('../../');
-
- var chunk = new Buffer('hallo');
-
-diff --git a/test/simple/test-stream2-writable.js b/test/simple/test-stream2-writable.js
-index 704100c..209c3a6 100644
---- a/test/simple/test-stream2-writable.js
-+++ b/test/simple/test-stream2-writable.js
-@@ -20,8 +20,8 @@
- // USE OR OTHER DEALINGS IN THE SOFTWARE.
-
- var common = require('../common.js');
--var W = require('_stream_writable');
--var D = require('_stream_duplex');
-+var W = require('../../').Writable;
-+var D = require('../../').Duplex;
- var assert = require('assert');
-
- var util = require('util');
-diff --git a/test/simple/test-stream3-pause-then-read.js b/test/simple/test-stream3-pause-then-read.js
-index b91bde3..2f72c15 100644
---- a/test/simple/test-stream3-pause-then-read.js
-+++ b/test/simple/test-stream3-pause-then-read.js
-@@ -22,7 +22,7 @@
- var common = require('../common');
- var assert = require('assert');
-
--var stream = require('stream');
-+var stream = require('../../');
- var Readable = stream.Readable;
- var Writable = stream.Writable;
-
diff --git a/fs.js b/fs.js
deleted file mode 100644
index a663af86ef..0000000000
--- a/fs.js
+++ /dev/null
@@ -1,1705 +0,0 @@
-// Copyright Joyent, Inc. and other Node contributors.
-//
-// 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.
-
-// Maintainers, keep in mind that octal literals are not allowed
-// in strict mode. Use the decimal value and add a comment with
-// the octal value. Example:
-//
-// var mode = 438; /* mode=0666 */
-
-var util = require('util');
-var pathModule = require('path');
-
-var binding = process.binding('fs');
-var constants = process.binding('constants');
-var fs = exports;
-var Stream = require('stream').Stream;
-var EventEmitter = require('events').EventEmitter;
-
-var Readable = require('./lib/_stream_readable.js');
-var Writable = require('./lib/_stream_writable.js');
-
-var kMinPoolSpace = 128;
-var kPoolSize = 40 * 1024;
-
-var O_APPEND = constants.O_APPEND || 0;
-var O_CREAT = constants.O_CREAT || 0;
-var O_DIRECTORY = constants.O_DIRECTORY || 0;
-var O_EXCL = constants.O_EXCL || 0;
-var O_NOCTTY = constants.O_NOCTTY || 0;
-var O_NOFOLLOW = constants.O_NOFOLLOW || 0;
-var O_RDONLY = constants.O_RDONLY || 0;
-var O_RDWR = constants.O_RDWR || 0;
-var O_SYMLINK = constants.O_SYMLINK || 0;
-var O_SYNC = constants.O_SYNC || 0;
-var O_TRUNC = constants.O_TRUNC || 0;
-var O_WRONLY = constants.O_WRONLY || 0;
-
-var isWindows = process.platform === 'win32';
-
-var DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
-
-function rethrow() {
- // Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
- // is fairly slow to generate.
- if (DEBUG) {
- var backtrace = new Error;
- return function(err) {
- if (err) {
- backtrace.message = err.message;
- err = backtrace;
- throw err;
- }
- };
- }
-
- return function(err) {
- if (err) {
- throw err; // Forgot a callback but don't know where? Use NODE_DEBUG=fs
- }
- };
-}
-
-function maybeCallback(cb) {
- return typeof cb === 'function' ? cb : rethrow();
-}
-
-// Ensure that callbacks run in the global context. Only use this function
-// for callbacks that are passed to the binding layer, callbacks that are
-// invoked from JS already run in the proper scope.
-function makeCallback(cb) {
- if (typeof cb !== 'function') {
- return rethrow();
- }
-
- return function() {
- return cb.apply(null, arguments);
- };
-}
-
-function assertEncoding(encoding) {
- if (encoding && !Buffer.isEncoding(encoding)) {
- throw new Error('Unknown encoding: ' + encoding);
- }
-}
-
-function nullCheck(path, callback) {
- if (('' + path).indexOf('\u0000') !== -1) {
- var er = new Error('Path must be a string without null bytes.');
- if (!callback)
- throw er;
- process.nextTick(function() {
- callback(er);
- });
- return false;
- }
- return true;
-}
-
-fs.Stats = binding.Stats;
-
-fs.Stats.prototype._checkModeProperty = function(property) {
- return ((this.mode & constants.S_IFMT) === property);
-};
-
-fs.Stats.prototype.isDirectory = function() {
- return this._checkModeProperty(constants.S_IFDIR);
-};
-
-fs.Stats.prototype.isFile = function() {
- return this._checkModeProperty(constants.S_IFREG);
-};
-
-fs.Stats.prototype.isBlockDevice = function() {
- return this._checkModeProperty(constants.S_IFBLK);
-};
-
-fs.Stats.prototype.isCharacterDevice = function() {
- return this._checkModeProperty(constants.S_IFCHR);
-};
-
-fs.Stats.prototype.isSymbolicLink = function() {
- return this._checkModeProperty(constants.S_IFLNK);
-};
-
-fs.Stats.prototype.isFIFO = function() {
- return this._checkModeProperty(constants.S_IFIFO);
-};
-
-fs.Stats.prototype.isSocket = function() {
- return this._checkModeProperty(constants.S_IFSOCK);
-};
-
-fs.exists = function(path, callback) {
- if (!nullCheck(path, cb)) return;
- binding.stat(pathModule._makeLong(path), cb);
- function cb(err, stats) {
- if (callback) callback(err ? false : true);
- }
-};
-
-fs.existsSync = function(path) {
- try {
- nullCheck(path);
- binding.stat(pathModule._makeLong(path));
- return true;
- } catch (e) {
- return false;
- }
-};
-
-fs.readFile = function(path, encoding_) {
- var encoding = typeof(encoding_) === 'string' ? encoding_ : null;
- var callback = maybeCallback(arguments[arguments.length - 1]);
-
- assertEncoding(encoding);
-
- // first, stat the file, so we know the size.
- var size;
- var buffer; // single buffer with file data
- var buffers; // list for when size is unknown
- var pos = 0;
- var fd;
-
- fs.open(path, constants.O_RDONLY, 438 /*=0666*/, function(er, fd_) {
- if (er) return callback(er);
- fd = fd_;
-
- fs.fstat(fd, function(er, st) {
- if (er) return callback(er);
- size = st.size;
- if (size === 0) {
- // the kernel lies about many files.
- // Go ahead and try to read some bytes.
- buffers = [];
- return read();
- }
-
- buffer = new Buffer(size);
- read();
- });
- });
-
- function read() {
- if (size === 0) {
- buffer = new Buffer(8192);
- fs.read(fd, buffer, 0, 8192, -1, afterRead);
- } else {
- fs.read(fd, buffer, pos, size - pos, -1, afterRead);
- }
- }
-
- function afterRead(er, bytesRead) {
- if (er) {
- return fs.close(fd, function(er2) {
- return callback(er);
- });
- }
-
- if (bytesRead === 0) {
- return close();
- }
-
- pos += bytesRead;
- if (size !== 0) {
- if (pos === size) close();
- else read();
- } else {
- // unknown size, just read until we don't get bytes.
- buffers.push(buffer.slice(0, bytesRead));
- read();
- }
- }
-
- function close() {
- fs.close(fd, function(er) {
- if (size === 0) {
- // collected the data into the buffers list.
- buffer = Buffer.concat(buffers, pos);
- } else if (pos < size) {
- buffer = buffer.slice(0, pos);
- }
-
- if (encoding) buffer = buffer.toString(encoding);
- return callback(er, buffer);
- });
- }
-};
-
-fs.readFileSync = function(path, encoding) {
- assertEncoding(encoding);
-
- var fd = fs.openSync(path, constants.O_RDONLY, 438 /*=0666*/);
-
- var size;
- var threw = true;
- try {
- size = fs.fstatSync(fd).size;
- threw = false;
- } finally {
- if (threw) fs.closeSync(fd);
- }
-
- var pos = 0;
- var buffer; // single buffer with file data
- var buffers; // list for when size is unknown
-
- if (size === 0) {
- buffers = [];
- } else {
- buffer = new Buffer(size);
- }
-
- var done = false;
- while (!done) {
- var threw = true;
- try {
- if (size !== 0) {
- var bytesRead = fs.readSync(fd, buffer, pos, size - pos);
- } else {
- // the kernel lies about many files.
- // Go ahead and try to read some bytes.
- buffer = new Buffer(8192);
- var bytesRead = fs.readSync(fd, buffer, 0, 8192);
- if (bytesRead) {
- buffers.push(buffer.slice(0, bytesRead));
- }
- }
- threw = false;
- } finally {
- if (threw) fs.closeSync(fd);
- }
-
- pos += bytesRead;
- done = (bytesRead === 0) || (size !== 0 && pos >= size);
- }
-
- fs.closeSync(fd);
-
- if (size === 0) {
- // data was collected into the buffers list.
- buffer = Buffer.concat(buffers, pos);
- } else if (pos < size) {
- buffer = buffer.slice(0, pos);
- }
-
- if (encoding) buffer = buffer.toString(encoding);
- return buffer;
-};
-
-
-// Used by binding.open and friends
-function stringToFlags(flag) {
- // Only mess with strings
- if (typeof flag !== 'string') {
- return flag;
- }
-
- // O_EXCL is mandated by POSIX, Windows supports it too.
- // Let's add a check anyway, just in case.
- if (!O_EXCL && ~flag.indexOf('x')) {
- throw errnoException('ENOSYS', 'fs.open(O_EXCL)');
- }
-
- switch (flag) {
- case 'r' : return O_RDONLY;
- case 'rs' : return O_RDONLY | O_SYNC;
- case 'r+' : return O_RDWR;
- case 'rs+' : return O_RDWR | O_SYNC;
-
- case 'w' : return O_TRUNC | O_CREAT | O_WRONLY;
- case 'wx' : // fall through
- case 'xw' : return O_TRUNC | O_CREAT | O_WRONLY | O_EXCL;
-
- case 'w+' : return O_TRUNC | O_CREAT | O_RDWR;
- case 'wx+': // fall through
- case 'xw+': return O_TRUNC | O_CREAT | O_RDWR | O_EXCL;
-
- case 'a' : return O_APPEND | O_CREAT | O_WRONLY;
- case 'ax' : // fall through
- case 'xa' : return O_APPEND | O_CREAT | O_WRONLY | O_EXCL;
-
- case 'a+' : return O_APPEND | O_CREAT | O_RDWR;
- case 'ax+': // fall through
- case 'xa+': return O_APPEND | O_CREAT | O_RDWR | O_EXCL;
- }
-
- throw new Error('Unknown file open flag: ' + flag);
-}
-
-// exported but hidden, only used by test/simple/test-fs-open-flags.js
-Object.defineProperty(exports, '_stringToFlags', {
- enumerable: false,
- value: stringToFlags
-});
-
-
-// Yes, the follow could be easily DRYed up but I provide the explicit
-// list to make the arguments clear.
-
-fs.close = function(fd, callback) {
- binding.close(fd, makeCallback(callback));
-};
-
-fs.closeSync = function(fd) {
- return binding.close(fd);
-};
-
-function modeNum(m, def) {
- switch (typeof m) {
- case 'number': return m;
- case 'string': return parseInt(m, 8);
- default:
- if (def) {
- return modeNum(def);
- } else {
- return undefined;
- }
- }
-}
-
-fs.open = function(path, flags, mode, callback) {
- callback = makeCallback(arguments[arguments.length - 1]);
- mode = modeNum(mode, 438 /*=0666*/);
-
- if (!nullCheck(path, callback)) return;
- binding.open(pathModule._makeLong(path),
- stringToFlags(flags),
- mode,
- callback);
-};
-
-fs.openSync = function(path, flags, mode) {
- mode = modeNum(mode, 438 /*=0666*/);
- nullCheck(path);
- return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
-};
-
-fs.read = function(fd, buffer, offset, length, position, callback) {
- if (!Buffer.isBuffer(buffer)) {
- // legacy string interface (fd, length, position, encoding, callback)
- var cb = arguments[4],
- encoding = arguments[3];
-
- assertEncoding(encoding);
-
- position = arguments[2];
- length = arguments[1];
- buffer = new Buffer(length);
- offset = 0;
-
- callback = function(err, bytesRead) {
- if (!cb) return;
-
- var str = (bytesRead > 0) ? buffer.toString(encoding, 0, bytesRead) : '';
-
- (cb)(err, str, bytesRead);
- };
- }
-
- function wrapper(err, bytesRead) {
- // Retain a reference to buffer so that it can't be GC'ed too soon.
- callback && callback(err, bytesRead || 0, buffer);
- }
-
- binding.read(fd, buffer, offset, length, position, wrapper);
-};
-
-fs.readSync = function(fd, buffer, offset, length, position) {
- var legacy = false;
- if (!Buffer.isBuffer(buffer)) {
- // legacy string interface (fd, length, position, encoding, callback)
- legacy = true;
- var encoding = arguments[3];
-
- assertEncoding(encoding);
-
- position = arguments[2];
- length = arguments[1];
- buffer = new Buffer(length);
-
- offset = 0;
- }
-
- var r = binding.read(fd, buffer, offset, length, position);
- if (!legacy) {
- return r;
- }
-
- var str = (r > 0) ? buffer.toString(encoding, 0, r) : '';
- return [str, r];
-};
-
-fs.write = function(fd, buffer, offset, length, position, callback) {
- if (!Buffer.isBuffer(buffer)) {
- // legacy string interface (fd, data, position, encoding, callback)
- callback = arguments[4];
- position = arguments[2];
- assertEncoding(arguments[3]);
-
- buffer = new Buffer('' + arguments[1], arguments[3]);
- offset = 0;
- length = buffer.length;
- }
-
- if (!length) {
- if (typeof callback == 'function') {
- process.nextTick(function() {
- callback(undefined, 0);
- });
- }
- return;
- }
-
- callback = maybeCallback(callback);
-
- function wrapper(err, written) {
- // Retain a reference to buffer so that it can't be GC'ed too soon.
- callback(err, written || 0, buffer);
- }
-
- binding.write(fd, buffer, offset, length, position, wrapper);
-};
-
-fs.writeSync = function(fd, buffer, offset, length, position) {
- if (!Buffer.isBuffer(buffer)) {
- // legacy string interface (fd, data, position, encoding)
- position = arguments[2];
- assertEncoding(arguments[3]);
-
- buffer = new Buffer('' + arguments[1], arguments[3]);
- offset = 0;
- length = buffer.length;
- }
- if (!length) return 0;
-
- return binding.write(fd, buffer, offset, length, position);
-};
-
-fs.rename = function(oldPath, newPath, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(oldPath, callback)) return;
- if (!nullCheck(newPath, callback)) return;
- binding.rename(pathModule._makeLong(oldPath),
- pathModule._makeLong(newPath),
- callback);
-};
-
-fs.renameSync = function(oldPath, newPath) {
- nullCheck(oldPath);
- nullCheck(newPath);
- return binding.rename(pathModule._makeLong(oldPath),
- pathModule._makeLong(newPath));
-};
-
-fs.truncate = function(path, len, callback) {
- if (typeof path === 'number') {
- // legacy
- return fs.ftruncate(path, len, callback);
- }
- if (typeof len === 'function') {
- callback = len;
- len = 0;
- } else if (typeof len === 'undefined') {
- len = 0;
- }
- callback = maybeCallback(callback);
- fs.open(path, 'w', function(er, fd) {
- if (er) return callback(er);
- binding.ftruncate(fd, len, function(er) {
- fs.close(fd, function(er2) {
- callback(er || er2);
- });
- });
- });
-};
-
-fs.truncateSync = function(path, len) {
- if (typeof path === 'number') {
- // legacy
- return fs.ftruncateSync(path, len);
- }
- if (typeof len === 'undefined') {
- len = 0;
- }
- // allow error to be thrown, but still close fd.
- var fd = fs.openSync(path, 'w');
- try {
- var ret = fs.ftruncateSync(fd, len);
- } finally {
- fs.closeSync(fd);
- }
- return ret;
-};
-
-fs.ftruncate = function(fd, len, callback) {
- if (typeof len === 'function') {
- callback = len;
- len = 0;
- } else if (typeof len === 'undefined') {
- len = 0;
- }
- binding.ftruncate(fd, len, makeCallback(callback));
-};
-
-fs.ftruncateSync = function(fd, len) {
- if (typeof len === 'undefined') {
- len = 0;
- }
- return binding.ftruncate(fd, len);
-};
-
-fs.rmdir = function(path, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.rmdir(pathModule._makeLong(path), callback);
-};
-
-fs.rmdirSync = function(path) {
- nullCheck(path);
- return binding.rmdir(pathModule._makeLong(path));
-};
-
-fs.fdatasync = function(fd, callback) {
- binding.fdatasync(fd, makeCallback(callback));
-};
-
-fs.fdatasyncSync = function(fd) {
- return binding.fdatasync(fd);
-};
-
-fs.fsync = function(fd, callback) {
- binding.fsync(fd, makeCallback(callback));
-};
-
-fs.fsyncSync = function(fd) {
- return binding.fsync(fd);
-};
-
-fs.mkdir = function(path, mode, callback) {
- if (typeof mode === 'function') callback = mode;
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.mkdir(pathModule._makeLong(path),
- modeNum(mode, 511 /*=0777*/),
- callback);
-};
-
-fs.mkdirSync = function(path, mode) {
- nullCheck(path);
- return binding.mkdir(pathModule._makeLong(path),
- modeNum(mode, 511 /*=0777*/));
-};
-
-fs.sendfile = function(outFd, inFd, inOffset, length, callback) {
- binding.sendfile(outFd, inFd, inOffset, length, makeCallback(callback));
-};
-
-fs.sendfileSync = function(outFd, inFd, inOffset, length) {
- return binding.sendfile(outFd, inFd, inOffset, length);
-};
-
-fs.readdir = function(path, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.readdir(pathModule._makeLong(path), callback);
-};
-
-fs.readdirSync = function(path) {
- nullCheck(path);
- return binding.readdir(pathModule._makeLong(path));
-};
-
-fs.fstat = function(fd, callback) {
- binding.fstat(fd, makeCallback(callback));
-};
-
-fs.lstat = function(path, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.lstat(pathModule._makeLong(path), callback);
-};
-
-fs.stat = function(path, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.stat(pathModule._makeLong(path), callback);
-};
-
-fs.fstatSync = function(fd) {
- return binding.fstat(fd);
-};
-
-fs.lstatSync = function(path) {
- nullCheck(path);
- return binding.lstat(pathModule._makeLong(path));
-};
-
-fs.statSync = function(path) {
- nullCheck(path);
- return binding.stat(pathModule._makeLong(path));
-};
-
-fs.readlink = function(path, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.readlink(pathModule._makeLong(path), callback);
-};
-
-fs.readlinkSync = function(path) {
- nullCheck(path);
- return binding.readlink(pathModule._makeLong(path));
-};
-
-function preprocessSymlinkDestination(path, type) {
- if (!isWindows) {
- // No preprocessing is needed on Unix.
- return path;
- } else if (type === 'junction') {
- // Junctions paths need to be absolute and \\?\-prefixed.
- return pathModule._makeLong(path);
- } else {
- // Windows symlinks don't tolerate forward slashes.
- return ('' + path).replace(/\//g, '\\');
- }
-}
-
-fs.symlink = function(destination, path, type_, callback) {
- var type = (typeof type_ === 'string' ? type_ : null);
- var callback = makeCallback(arguments[arguments.length - 1]);
-
- if (!nullCheck(destination, callback)) return;
- if (!nullCheck(path, callback)) return;
-
- binding.symlink(preprocessSymlinkDestination(destination, type),
- pathModule._makeLong(path),
- type,
- callback);
-};
-
-fs.symlinkSync = function(destination, path, type) {
- type = (typeof type === 'string' ? type : null);
-
- nullCheck(destination);
- nullCheck(path);
-
- return binding.symlink(preprocessSymlinkDestination(destination, type),
- pathModule._makeLong(path),
- type);
-};
-
-fs.link = function(srcpath, dstpath, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(srcpath, callback)) return;
- if (!nullCheck(dstpath, callback)) return;
-
- binding.link(pathModule._makeLong(srcpath),
- pathModule._makeLong(dstpath),
- callback);
-};
-
-fs.linkSync = function(srcpath, dstpath) {
- nullCheck(srcpath);
- nullCheck(dstpath);
- return binding.link(pathModule._makeLong(srcpath),
- pathModule._makeLong(dstpath));
-};
-
-fs.unlink = function(path, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.unlink(pathModule._makeLong(path), callback);
-};
-
-fs.unlinkSync = function(path) {
- nullCheck(path);
- return binding.unlink(pathModule._makeLong(path));
-};
-
-fs.fchmod = function(fd, mode, callback) {
- binding.fchmod(fd, modeNum(mode), makeCallback(callback));
-};
-
-fs.fchmodSync = function(fd, mode) {
- return binding.fchmod(fd, modeNum(mode));
-};
-
-if (constants.hasOwnProperty('O_SYMLINK')) {
- fs.lchmod = function(path, mode, callback) {
- callback = maybeCallback(callback);
- fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
- if (err) {
- callback(err);
- return;
- }
- // prefer to return the chmod error, if one occurs,
- // but still try to close, and report closing errors if they occur.
- fs.fchmod(fd, mode, function(err) {
- fs.close(fd, function(err2) {
- callback(err || err2);
- });
- });
- });
- };
-
- fs.lchmodSync = function(path, mode) {
- var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
-
- // prefer to return the chmod error, if one occurs,
- // but still try to close, and report closing errors if they occur.
- var err, err2;
- try {
- var ret = fs.fchmodSync(fd, mode);
- } catch (er) {
- err = er;
- }
- try {
- fs.closeSync(fd);
- } catch (er) {
- err2 = er;
- }
- if (err || err2) throw (err || err2);
- return ret;
- };
-}
-
-
-fs.chmod = function(path, mode, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.chmod(pathModule._makeLong(path),
- modeNum(mode),
- callback);
-};
-
-fs.chmodSync = function(path, mode) {
- nullCheck(path);
- return binding.chmod(pathModule._makeLong(path), modeNum(mode));
-};
-
-if (constants.hasOwnProperty('O_SYMLINK')) {
- fs.lchown = function(path, uid, gid, callback) {
- callback = maybeCallback(callback);
- fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
- if (err) {
- callback(err);
- return;
- }
- fs.fchown(fd, uid, gid, callback);
- });
- };
-
- fs.lchownSync = function(path, uid, gid) {
- var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
- return fs.fchownSync(fd, uid, gid);
- };
-}
-
-fs.fchown = function(fd, uid, gid, callback) {
- binding.fchown(fd, uid, gid, makeCallback(callback));
-};
-
-fs.fchownSync = function(fd, uid, gid) {
- return binding.fchown(fd, uid, gid);
-};
-
-fs.chown = function(path, uid, gid, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.chown(pathModule._makeLong(path), uid, gid, callback);
-};
-
-fs.chownSync = function(path, uid, gid) {
- nullCheck(path);
- return binding.chown(pathModule._makeLong(path), uid, gid);
-};
-
-// converts Date or number to a fractional UNIX timestamp
-function toUnixTimestamp(time) {
- if (typeof time == 'number') {
- return time;
- }
- if (time instanceof Date) {
- // convert to 123.456 UNIX timestamp
- return time.getTime() / 1000;
- }
- throw new Error('Cannot parse time: ' + time);
-}
-
-// exported for unit tests, not for public consumption
-fs._toUnixTimestamp = toUnixTimestamp;
-
-fs.utimes = function(path, atime, mtime, callback) {
- callback = makeCallback(callback);
- if (!nullCheck(path, callback)) return;
- binding.utimes(pathModule._makeLong(path),
- toUnixTimestamp(atime),
- toUnixTimestamp(mtime),
- callback);
-};
-
-fs.utimesSync = function(path, atime, mtime) {
- nullCheck(path);
- atime = toUnixTimestamp(atime);
- mtime = toUnixTimestamp(mtime);
- binding.utimes(pathModule._makeLong(path), atime, mtime);
-};
-
-fs.futimes = function(fd, atime, mtime, callback) {
- atime = toUnixTimestamp(atime);
- mtime = toUnixTimestamp(mtime);
- binding.futimes(fd, atime, mtime, makeCallback(callback));
-};
-
-fs.futimesSync = function(fd, atime, mtime) {
- atime = toUnixTimestamp(atime);
- mtime = toUnixTimestamp(mtime);
- binding.futimes(fd, atime, mtime);
-};
-
-function writeAll(fd, buffer, offset, length, position, callback) {
- callback = maybeCallback(arguments[arguments.length - 1]);
-
- // write(fd, buffer, offset, length, position, callback)
- fs.write(fd, buffer, offset, length, position, function(writeErr, written) {
- if (writeErr) {
- fs.close(fd, function() {
- if (callback) callback(writeErr);
- });
- } else {
- if (written === length) {
- fs.close(fd, callback);
- } else {
- offset += written;
- length -= written;
- position += written;
- writeAll(fd, buffer, offset, length, position, callback);
- }
- }
- });
-}
-
-fs.writeFile = function(path, data, encoding_, callback) {
- var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8');
- assertEncoding(encoding);
-
- callback = maybeCallback(arguments[arguments.length - 1]);
- fs.open(path, 'w', 438 /*=0666*/, function(openErr, fd) {
- if (openErr) {
- if (callback) callback(openErr);
- } else {
- var buffer = Buffer.isBuffer(data) ? data : new Buffer('' + data,
- encoding);
- writeAll(fd, buffer, 0, buffer.length, 0, callback);
- }
- });
-};
-
-fs.writeFileSync = function(path, data, encoding) {
- assertEncoding(encoding);
-
- var fd = fs.openSync(path, 'w');
- if (!Buffer.isBuffer(data)) {
- data = new Buffer('' + data, encoding || 'utf8');
- }
- var written = 0;
- var length = data.length;
- try {
- while (written < length) {
- written += fs.writeSync(fd, data, written, length - written, written);
- }
- } finally {
- fs.closeSync(fd);
- }
-};
-
-fs.appendFile = function(path, data, encoding_, callback) {
- var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8');
- assertEncoding(encoding);
-
- callback = maybeCallback(arguments[arguments.length - 1]);
-
- fs.open(path, 'a', 438 /*=0666*/, function(err, fd) {
- if (err) return callback(err);
- var buffer = Buffer.isBuffer(data) ? data : new Buffer('' + data, encoding);
- writeAll(fd, buffer, 0, buffer.length, null, callback);
- });
-};
-
-fs.appendFileSync = function(path, data, encoding) {
- assertEncoding(encoding);
-
- var fd = fs.openSync(path, 'a');
- if (!Buffer.isBuffer(data)) {
- data = new Buffer('' + data, encoding || 'utf8');
- }
- var written = 0;
- var position = null;
- var length = data.length;
-
- try {
- while (written < length) {
- written += fs.writeSync(fd, data, written, length - written, position);
- position += written; // XXX not safe with multiple concurrent writers?
- }
- } finally {
- fs.closeSync(fd);
- }
-};
-
-function errnoException(errorno, syscall) {
- // TODO make this more compatible with ErrnoException from src/node.cc
- // Once all of Node is using this function the ErrnoException from
- // src/node.cc should be removed.
- var e = new Error(syscall + ' ' + errorno);
- e.errno = e.code = errorno;
- e.syscall = syscall;
- return e;
-}
-
-
-function FSWatcher() {
- EventEmitter.call(this);
-
- var self = this;
- var FSEvent = process.binding('fs_event_wrap').FSEvent;
- this._handle = new FSEvent();
- this._handle.owner = this;
-
- this._handle.onchange = function(status, event, filename) {
- if (status) {
- self._handle.close();
- self.emit('error', errnoException(errno, 'watch'));
- } else {
- self.emit('change', event, filename);
- }
- };
-}
-util.inherits(FSWatcher, EventEmitter);
-
-FSWatcher.prototype.start = function(filename, persistent) {
- nullCheck(filename);
- var r = this._handle.start(pathModule._makeLong(filename), persistent);
-
- if (r) {
- this._handle.close();
- throw errnoException(errno, 'watch');
- }
-};
-
-FSWatcher.prototype.close = function() {
- this._handle.close();
-};
-
-fs.watch = function(filename) {
- nullCheck(filename);
- var watcher;
- var options;
- var listener;
-
- if ('object' == typeof arguments[1]) {
- options = arguments[1];
- listener = arguments[2];
- } else {
- options = {};
- listener = arguments[1];
- }
-
- if (options.persistent === undefined) options.persistent = true;
-
- watcher = new FSWatcher();
- watcher.start(filename, options.persistent);
-
- if (listener) {
- watcher.addListener('change', listener);
- }
-
- return watcher;
-};
-
-
-// Stat Change Watchers
-
-function StatWatcher() {
- EventEmitter.call(this);
-
- var self = this;
- this._handle = new binding.StatWatcher();
-
- // uv_fs_poll is a little more powerful than ev_stat but we curb it for
- // the sake of backwards compatibility
- var oldStatus = -1;
-
- this._handle.onchange = function(current, previous, newStatus) {
- if (oldStatus === -1 &&
- newStatus === -1 &&
- current.nlink === previous.nlink) return;
-
- oldStatus = newStatus;
- self.emit('change', current, previous);
- };
-
- this._handle.onstop = function() {
- self.emit('stop');
- };
-}
-util.inherits(StatWatcher, EventEmitter);
-
-
-StatWatcher.prototype.start = function(filename, persistent, interval) {
- nullCheck(filename);
- this._handle.start(pathModule._makeLong(filename), persistent, interval);
-};
-
-
-StatWatcher.prototype.stop = function() {
- this._handle.stop();
-};
-
-
-var statWatchers = {};
-function inStatWatchers(filename) {
- return Object.prototype.hasOwnProperty.call(statWatchers, filename) &&
- statWatchers[filename];
-}
-
-
-fs.watchFile = function(filename) {
- nullCheck(filename);
- var stat;
- var listener;
-
- var options = {
- // Poll interval in milliseconds. 5007 is what libev used to use. It's
- // a little on the slow side but let's stick with it for now to keep
- // behavioral changes to a minimum.
- interval: 5007,
- persistent: true
- };
-
- if ('object' == typeof arguments[1]) {
- options = util._extend(options, arguments[1]);
- listener = arguments[2];
- } else {
- listener = arguments[1];
- }
-
- if (!listener) {
- throw new Error('watchFile requires a listener function');
- }
-
- if (inStatWatchers(filename)) {
- stat = statWatchers[filename];
- } else {
- stat = statWatchers[filename] = new StatWatcher();
- stat.start(filename, options.persistent, options.interval);
- }
- stat.addListener('change', listener);
- return stat;
-};
-
-fs.unwatchFile = function(filename, listener) {
- nullCheck(filename);
- if (!inStatWatchers(filename)) return;
-
- var stat = statWatchers[filename];
-
- if (typeof listener === 'function') {
- stat.removeListener('change', listener);
- } else {
- stat.removeAllListeners('change');
- }
-
- if (stat.listeners('change').length === 0) {
- stat.stop();
- statWatchers[filename] = undefined;
- }
-};
-
-// Realpath
-// Not using realpath(2) because it's bad.
-// See: http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
-
-var normalize = pathModule.normalize;
-
-// Regexp that finds the next partion of a (partial) path
-// result is [base_with_slash, base], e.g. ['somedir/', 'somedir']
-if (isWindows) {
- var nextPartRe = /(.*?)(?:[\/\\]+|$)/g;
-} else {
- var nextPartRe = /(.*?)(?:[\/]+|$)/g;
-}
-
-// Regex to find the device root, including trailing slash. E.g. 'c:\\'.
-if (isWindows) {
- var splitRootRe = /^(?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?[\\\/]*/;
-} else {
- var splitRootRe = /^[\/]*/;
-}
-
-fs.realpathSync = function realpathSync(p, cache) {
- // make p is absolute
- p = pathModule.resolve(p);
-
- if (cache && Object.prototype.hasOwnProperty.call(cache, p)) {
- return cache[p];
- }
-
- var original = p,
- seenLinks = {},
- knownHard = {};
-
- // current character position in p
- var pos;
- // the partial path so far, including a trailing slash if any
- var current;
- // the partial path without a trailing slash (except when pointing at a root)
- var base;
- // the partial path scanned in the previous round, with slash
- var previous;
-
- start();
-
- function start() {
- // Skip over roots
- var m = splitRootRe.exec(p);
- pos = m[0].length;
- current = m[0];
- base = m[0];
- previous = '';
-
- // On windows, check that the root exists. On unix there is no need.
- if (isWindows && !knownHard[base]) {
- fs.lstatSync(base);
- knownHard[base] = true;
- }
- }
-
- // walk down the path, swapping out linked pathparts for their real
- // values
- // NB: p.length changes.
- while (pos < p.length) {
- // find the next part
- nextPartRe.lastIndex = pos;
- var result = nextPartRe.exec(p);
- previous = current;
- current += result[0];
- base = previous + result[1];
- pos = nextPartRe.lastIndex;
-
- // continue if not a symlink
- if (knownHard[base] || (cache && cache[base] === base)) {
- continue;
- }
-
- var resolvedLink;
- if (cache && Object.prototype.hasOwnProperty.call(cache, base)) {
- // some known symbolic link. no need to stat again.
- resolvedLink = cache[base];
- } else {
- var stat = fs.lstatSync(base);
- if (!stat.isSymbolicLink()) {
- knownHard[base] = true;
- if (cache) cache[base] = base;
- continue;
- }
-
- // read the link if it wasn't read before
- // dev/ino always return 0 on windows, so skip the check.
- var linkTarget = null;
- if (!isWindows) {
- var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
- if (seenLinks.hasOwnProperty(id)) {
- linkTarget = seenLinks[id];
- }
- }
- if (linkTarget === null) {
- fs.statSync(base);
- linkTarget = fs.readlinkSync(base);
- }
- resolvedLink = pathModule.resolve(previous, linkTarget);
- // track this, if given a cache.
- if (cache) cache[base] = resolvedLink;
- if (!isWindows) seenLinks[id] = linkTarget;
- }
-
- // resolve the link, then start over
- p = pathModule.resolve(resolvedLink, p.slice(pos));
- start();
- }
-
- if (cache) cache[original] = p;
-
- return p;
-};
-
-
-fs.realpath = function realpath(p, cache, cb) {
- if (typeof cb !== 'function') {
- cb = maybeCallback(cache);
- cache = null;
- }
-
- // make p is absolute
- p = pathModule.resolve(p);
-
- if (cache && Object.prototype.hasOwnProperty.call(cache, p)) {
- return process.nextTick(cb.bind(null, null, cache[p]));
- }
-
- var original = p,
- seenLinks = {},
- knownHard = {};
-
- // current character position in p
- var pos;
- // the partial path so far, including a trailing slash if any
- var current;
- // the partial path without a trailing slash (except when pointing at a root)
- var base;
- // the partial path scanned in the previous round, with slash
- var previous;
-
- start();
-
- function start() {
- // Skip over roots
- var m = splitRootRe.exec(p);
- pos = m[0].length;
- current = m[0];
- base = m[0];
- previous = '';
-
- // On windows, check that the root exists. On unix there is no need.
- if (isWindows && !knownHard[base]) {
- fs.lstat(base, function(err) {
- if (err) return cb(err);
- knownHard[base] = true;
- LOOP();
- });
- } else {
- process.nextTick(LOOP);
- }
- }
-
- // walk down the path, swapping out linked pathparts for their real
- // values
- function LOOP() {
- // stop if scanned past end of path
- if (pos >= p.length) {
- if (cache) cache[original] = p;
- return cb(null, p);
- }
-
- // find the next part
- nextPartRe.lastIndex = pos;
- var result = nextPartRe.exec(p);
- previous = current;
- current += result[0];
- base = previous + result[1];
- pos = nextPartRe.lastIndex;
-
- // continue if not a symlink
- if (knownHard[base] || (cache && cache[base] === base)) {
- return process.nextTick(LOOP);
- }
-
- if (cache && Object.prototype.hasOwnProperty.call(cache, base)) {
- // known symbolic link. no need to stat again.
- return gotResolvedLink(cache[base]);
- }
-
- return fs.lstat(base, gotStat);
- }
-
- function gotStat(err, stat) {
- if (err) return cb(err);
-
- // if not a symlink, skip to the next path part
- if (!stat.isSymbolicLink()) {
- knownHard[base] = true;
- if (cache) cache[base] = base;
- return process.nextTick(LOOP);
- }
-
- // stat & read the link if not read before
- // call gotTarget as soon as the link target is known
- // dev/ino always return 0 on windows, so skip the check.
- if (!isWindows) {
- var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
- if (seenLinks.hasOwnProperty(id)) {
- return gotTarget(null, seenLinks[id], base);
- }
- }
- fs.stat(base, function(err) {
- if (err) return cb(err);
-
- fs.readlink(base, function(err, target) {
- if (!isWindows) seenLinks[id] = target;
- gotTarget(err, target);
- });
- });
- }
-
- function gotTarget(err, target, base) {
- if (err) return cb(err);
-
- var resolvedLink = pathModule.resolve(previous, target);
- if (cache) cache[base] = resolvedLink;
- gotResolvedLink(resolvedLink);
- }
-
- function gotResolvedLink(resolvedLink) {
- // resolve the link, then start over
- p = pathModule.resolve(resolvedLink, p.slice(pos));
- start();
- }
-};
-
-
-
-var pool;
-
-function allocNewPool() {
- pool = new Buffer(kPoolSize);
- pool.used = 0;
-}
-
-
-
-fs.createReadStream = function(path, options) {
- return new ReadStream(path, options);
-};
-
-util.inherits(ReadStream, Readable);
-fs.ReadStream = ReadStream;
-
-function ReadStream(path, options) {
- if (!(this instanceof ReadStream))
- return new ReadStream(path, options);
-
- // a little bit bigger buffer and water marks by default
- options = util._extend({
- bufferSize: 64 * 1024,
- lowWaterMark: 16 * 1024,
- highWaterMark: 64 * 1024
- }, options || {});
-
- Readable.call(this, options);
-
- this.path = path;
- this.fd = options.hasOwnProperty('fd') ? options.fd : null;
- this.flags = options.hasOwnProperty('flags') ? options.flags : 'r';
- this.mode = options.hasOwnProperty('mode') ? options.mode : 438; /*=0666*/
-
- this.start = options.hasOwnProperty('start') ? options.start : undefined;
- this.end = options.hasOwnProperty('start') ? options.end : undefined;
- this.pos = undefined;
-
- if (this.start !== undefined) {
- if ('number' !== typeof this.start) {
- throw TypeError('start must be a Number');
- }
- if (this.end === undefined) {
- this.end = Infinity;
- } else if ('number' !== typeof this.end) {
- throw TypeError('end must be a Number');
- }
-
- if (this.start > this.end) {
- throw new Error('start must be <= end');
- }
-
- this.pos = this.start;
- }
-
- if (typeof this.fd !== 'number')
- this.open();
-
- this.on('end', function() {
- this.destroy();
- });
-}
-
-fs.FileReadStream = fs.ReadStream; // support the legacy name
-
-ReadStream.prototype.open = function() {
- var self = this;
- fs.open(this.path, this.flags, this.mode, function(er, fd) {
- if (er) {
- self.destroy();
- self.emit('error', er);
- return;
- }
-
- self.fd = fd;
- self.emit('open', fd);
- // start the flow of data.
- self.read();
- });
-};
-
-ReadStream.prototype._read = function(n, cb) {
- if (typeof this.fd !== 'number')
- return this.once('open', function() {
- this._read(n, cb);
- });
-
- if (this.destroyed)
- return;
-
- if (!pool || pool.length - pool.used < kMinPoolSpace) {
- // discard the old pool. Can't add to the free list because
- // users might have refernces to slices on it.
- pool = null;
- allocNewPool();
- }
-
- // Grab another reference to the pool in the case that while we're
- // in the thread pool another read() finishes up the pool, and
- // allocates a new one.
- var thisPool = pool;
- var toRead = Math.min(pool.length - pool.used, n);
- var start = pool.used;
-
- if (this.pos !== undefined)
- toRead = Math.min(this.end - this.pos + 1, toRead);
-
- // already read everything we were supposed to read!
- // treat as EOF.
- if (toRead <= 0)
- return cb();
-
- // the actual read.
- var self = this;
- fs.read(this.fd, pool, pool.used, toRead, this.pos, onread);
-
- // move the pool positions, and internal position for reading.
- if (this.pos !== undefined)
- this.pos += toRead;
- pool.used += toRead;
-
- function onread(er, bytesRead) {
- if (er) {
- self.destroy();
- return cb(er);
- }
-
- var b = null;
- if (bytesRead > 0)
- b = thisPool.slice(start, start + bytesRead);
-
- cb(null, b);
- }
-};
-
-
-ReadStream.prototype.destroy = function() {
- if (this.destroyed)
- return;
- this.destroyed = true;
- if ('number' === typeof this.fd)
- this.close();
-};
-
-
-ReadStream.prototype.close = function(cb) {
- if (cb)
- this.once('close', cb);
- if (this.closed || 'number' !== typeof this.fd) {
- if ('number' !== typeof this.fd)
- this.once('open', close);
- return process.nextTick(this.emit.bind(this, 'close'));
- }
- this.closed = true;
- var self = this;
- close();
-
- function close() {
- fs.close(self.fd, function(er) {
- if (er)
- self.emit('error', er);
- else
- self.emit('close');
- });
- }
-};
-
-
-
-
-fs.createWriteStream = function(path, options) {
- return new WriteStream(path, options);
-};
-
-util.inherits(WriteStream, Writable);
-fs.WriteStream = WriteStream;
-function WriteStream(path, options) {
- if (!(this instanceof WriteStream))
- return new WriteStream(path, options);
-
- // a little bit bigger buffer and water marks by default
- options = util._extend({
- bufferSize: 64 * 1024,
- lowWaterMark: 16 * 1024,
- highWaterMark: 64 * 1024
- }, options || {});
-
- Writable.call(this, options);
-
- this.path = path;
- this.fd = null;
-
- this.fd = options.hasOwnProperty('fd') ? options.fd : null;
- this.flags = options.hasOwnProperty('flags') ? options.flags : 'w';
- this.mode = options.hasOwnProperty('mode') ? options.mode : 438; /*=0666*/
-
- this.start = options.hasOwnProperty('start') ? options.start : undefined;
- this.pos = undefined;
- this.bytesWritten = 0;
-
- if (this.start !== undefined) {
- if ('number' !== typeof this.start) {
- throw TypeError('start must be a Number');
- }
- if (this.start < 0) {
- throw new Error('start must be >= zero');
- }
-
- this.pos = this.start;
- }
-
- if ('number' !== typeof this.fd)
- this.open();
-
- // dispose on finish.
- this.once('finish', this.close);
-}
-
-fs.FileWriteStream = fs.WriteStream; // support the legacy name
-
-
-WriteStream.prototype.open = function() {
- fs.open(this.path, this.flags, this.mode, function(er, fd) {
- if (er) {
- this.destroy();
- this.emit('error', er);
- return;
- }
-
- this.fd = fd;
- this.emit('open', fd);
- }.bind(this));
-};
-
-
-WriteStream.prototype._write = function(data, cb) {
- if (!Buffer.isBuffer(data))
- return this.emit('error', new Error('Invalid data'));
-
- if (typeof this.fd !== 'number')
- return this.once('open', this._write.bind(this, data, cb));
-
- fs.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) {
- if (er) {
- this.destroy();
- return cb(er);
- }
- this.bytesWritten += bytes;
- cb();
- }.bind(this));
-
- if (this.pos !== undefined)
- this.pos += data.length;
-};
-
-
-WriteStream.prototype.destroy = ReadStream.prototype.destroy;
-WriteStream.prototype.close = ReadStream.prototype.close;
-
-// There is no shutdown() for files.
-WriteStream.prototype.destroySoon = WriteStream.prototype.end;
-
-
-// SyncWriteStream is internal. DO NOT USE.
-// Temporary hack for process.stdout and process.stderr when piped to files.
-function SyncWriteStream(fd) {
- Stream.call(this);
-
- this.fd = fd;
- this.writable = true;
- this.readable = false;
-}
-
-util.inherits(SyncWriteStream, Stream);
-
-
-// Export
-fs.SyncWriteStream = SyncWriteStream;
-
-
-SyncWriteStream.prototype.write = function(data, arg1, arg2) {
- var encoding, cb;
-
- // parse arguments
- if (arg1) {
- if (typeof arg1 === 'string') {
- encoding = arg1;
- cb = arg2;
- } else if (typeof arg1 === 'function') {
- cb = arg1;
- } else {
- throw new Error('bad arg');
- }
- }
- assertEncoding(encoding);
-
- // Change strings to buffers. SLOW
- if (typeof data == 'string') {
- data = new Buffer(data, encoding);
- }
-
- fs.writeSync(this.fd, data, 0, data.length);
-
- if (cb) {
- process.nextTick(cb);
- }
-
- return true;
-};
-
-
-SyncWriteStream.prototype.end = function(data, arg1, arg2) {
- if (data) {
- this.write(data, arg1, arg2);
- }
- this.destroy();
-};
-
-
-SyncWriteStream.prototype.destroy = function() {
- fs.closeSync(this.fd);
- this.fd = null;
- this.emit('close');
- return true;
-};
-
-SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy;
diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js
index 775727131b..5ff12c0df7 100644
--- a/lib/_stream_duplex.js
+++ b/lib/_stream_duplex.js
@@ -7,6 +7,11 @@
module.exports = Duplex;
+/**/
+var processNextTick = require('process-nextick-args');
+/**/
+
+
/**/
var objectKeys = Object.keys || function (obj) {
var keys = [];
@@ -16,6 +21,7 @@ var objectKeys = Object.keys || function (obj) {
/**/
+
/**/
var util = require('core-util-is');
util.inherits = require('inherits');
@@ -62,7 +68,11 @@ function onend() {
// no more data can be written.
// But allow more writes to happen in this tick.
- process.nextTick(this.end.bind(this));
+ processNextTick(onEndNT, this);
+}
+
+function onEndNT(self) {
+ self.end();
}
function forEach (xs, f) {
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js
index c549105e15..b392d8035d 100644
--- a/lib/_stream_readable.js
+++ b/lib/_stream_readable.js
@@ -2,6 +2,11 @@
module.exports = Readable;
+/**/
+var processNextTick = require('process-nextick-args');
+/**/
+
+
/**/
var isArray = require('isarray');
/**/
@@ -21,14 +26,21 @@ if (!EE.listenerCount) EE.listenerCount = function(emitter, type) {
};
/**/
-var Stream = require('stream');
+
+
+/**/
+var Stream;
+ (function (){try{
+Stream = require('st' + 'ream');
+}catch(_){Stream = require('events').EventEmitter;}}())
+/**/
+
/**/
var util = require('core-util-is');
util.inherits = require('inherits');
/**/
-var StringDecoder;
/**/
@@ -40,6 +52,7 @@ if (debug && debug.debuglog) {
}
/**/
+var StringDecoder;
util.inherits(Readable, Stream);
@@ -121,6 +134,9 @@ function Readable(options) {
// legacy
this.readable = true;
+ if (options && typeof options.read === 'function')
+ this._read = options.read;
+
Stream.call(this);
}
@@ -131,7 +147,7 @@ function Readable(options) {
Readable.prototype.push = function(chunk, encoding) {
var state = this._readableState;
- if (util.isString(chunk) && !state.objectMode) {
+ if (!state.objectMode && typeof chunk === 'string') {
encoding = encoding || state.defaultEncoding;
if (encoding !== state.encoding) {
chunk = new Buffer(chunk, encoding);
@@ -158,8 +174,7 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) {
stream.emit('error', er);
} else if (chunk === null) {
state.reading = false;
- if (!state.ended)
- onEofChunk(stream, state);
+ onEofChunk(stream, state);
} else if (state.objectMode || chunk && chunk.length > 0) {
if (state.ended && !addToFront) {
var e = new Error('stream.push() after EOF');
@@ -245,7 +260,7 @@ function howMuchToRead(n, state) {
if (state.objectMode)
return n === 0 ? 0 : 1;
- if (util.isNull(n) || isNaN(n)) {
+ if (n === null || isNaN(n)) {
// only flow one buffer at a time
if (state.flowing && state.buffer.length)
return state.buffer[0].length;
@@ -268,8 +283,9 @@ function howMuchToRead(n, state) {
if (!state.ended) {
state.needReadable = true;
return 0;
- } else
+ } else {
return state.length;
+ }
}
return n;
@@ -281,7 +297,7 @@ Readable.prototype.read = function(n) {
var state = this._readableState;
var nOrig = n;
- if (!util.isNumber(n) || n > 0)
+ if (typeof n !== 'number' || n > 0)
state.emittedReadable = false;
// if we're doing read(0) to trigger a readable event, but we
@@ -369,7 +385,7 @@ Readable.prototype.read = function(n) {
else
ret = null;
- if (util.isNull(ret)) {
+ if (ret === null) {
state.needReadable = true;
n = 0;
}
@@ -385,7 +401,7 @@ Readable.prototype.read = function(n) {
if (nOrig !== n && state.ended && state.length === 0)
endReadable(this);
- if (!util.isNull(ret))
+ if (ret !== null)
this.emit('data', ret);
return ret;
@@ -393,9 +409,10 @@ Readable.prototype.read = function(n) {
function chunkInvalid(state, chunk) {
var er = null;
- if (!util.isBuffer(chunk) &&
- !util.isString(chunk) &&
- !util.isNullOrUndefined(chunk) &&
+ if (!(Buffer.isBuffer(chunk)) &&
+ typeof chunk !== 'string' &&
+ chunk !== null &&
+ chunk !== undefined &&
!state.objectMode) {
er = new TypeError('Invalid non-string/buffer chunk');
}
@@ -404,7 +421,8 @@ function chunkInvalid(state, chunk) {
function onEofChunk(stream, state) {
- if (state.decoder && !state.ended) {
+ if (state.ended) return;
+ if (state.decoder) {
var chunk = state.decoder.end();
if (chunk && chunk.length) {
state.buffer.push(chunk);
@@ -427,9 +445,7 @@ function emitReadable(stream) {
debug('emitReadable', state.flowing);
state.emittedReadable = true;
if (state.sync)
- process.nextTick(function() {
- emitReadable_(stream);
- });
+ processNextTick(emitReadable_, stream);
else
emitReadable_(stream);
}
@@ -451,9 +467,7 @@ function emitReadable_(stream) {
function maybeReadMore(stream, state) {
if (!state.readingMore) {
state.readingMore = true;
- process.nextTick(function() {
- maybeReadMore_(stream, state);
- });
+ processNextTick(maybeReadMore_, stream, state);
}
}
@@ -504,7 +518,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
var endFn = doEnd ? onend : cleanup;
if (state.endEmitted)
- process.nextTick(endFn);
+ processNextTick(endFn);
else
src.once('end', endFn);
@@ -699,11 +713,7 @@ Readable.prototype.on = function(ev, fn) {
state.emittedReadable = false;
state.needReadable = true;
if (!state.reading) {
- var self = this;
- process.nextTick(function() {
- debug('readable nexttick read 0');
- self.read(0);
- });
+ processNextTick(nReadingNextTick, this);
} else if (state.length) {
emitReadable(this, state);
}
@@ -714,6 +724,11 @@ Readable.prototype.on = function(ev, fn) {
};
Readable.prototype.addListener = Readable.prototype.on;
+function nReadingNextTick(self) {
+ debug('readable nexttick read 0');
+ self.read(0);
+}
+
// pause() and resume() are remnants of the legacy readable stream API
// If the user uses them, then switch into old mode.
Readable.prototype.resume = function() {
@@ -729,9 +744,7 @@ Readable.prototype.resume = function() {
function resume(stream, state) {
if (!state.resumeScheduled) {
state.resumeScheduled = true;
- process.nextTick(function() {
- resume_(stream, state);
- });
+ processNextTick(resume_, stream, state);
}
}
@@ -793,7 +806,6 @@ Readable.prototype.wrap = function(stream) {
chunk = state.decoder.write(chunk);
// don't skip over falsy values in objectMode
- //if (state.objectMode && util.isNullOrUndefined(chunk))
if (state.objectMode && (chunk === null || chunk === undefined))
return;
else if (!state.objectMode && (!chunk || !chunk.length))
@@ -809,10 +821,10 @@ Readable.prototype.wrap = function(stream) {
// proxy all the other methods.
// important when wrapping filters and duplexes.
for (var i in stream) {
- if (util.isFunction(stream[i]) && util.isUndefined(this[i])) {
+ if (this[i] === undefined && typeof stream[i] === 'function') {
this[i] = function(method) { return function() {
return stream[method].apply(stream, arguments);
- }}(i);
+ }; }(i);
}
}
@@ -916,14 +928,16 @@ function endReadable(stream) {
if (!state.endEmitted) {
state.ended = true;
- process.nextTick(function() {
- // Check that we didn't get one last unshift.
- if (!state.endEmitted && state.length === 0) {
- state.endEmitted = true;
- stream.readable = false;
- stream.emit('end');
- }
- });
+ processNextTick(endReadableNT, state, stream);
+ }
+}
+
+function endReadableNT(state, stream) {
+ // Check that we didn't get one last unshift.
+ if (!state.endEmitted && state.length === 0) {
+ state.endEmitted = true;
+ stream.readable = false;
+ stream.emit('end');
}
}
diff --git a/lib/_stream_transform.js b/lib/_stream_transform.js
index 923d31362c..3675d18d91 100644
--- a/lib/_stream_transform.js
+++ b/lib/_stream_transform.js
@@ -77,7 +77,7 @@ function afterTransform(stream, er, data) {
ts.writechunk = null;
ts.writecb = null;
- if (!util.isNullOrUndefined(data))
+ if (data !== null && data !== undefined)
stream.push(data);
if (cb)
@@ -110,8 +110,16 @@ function Transform(options) {
// sync guard flag.
this._readableState.sync = false;
+ if (options) {
+ if (typeof options.transform === 'function')
+ this._transform = options.transform;
+
+ if (typeof options.flush === 'function')
+ this._flush = options.flush;
+ }
+
this.once('prefinish', function() {
- if (util.isFunction(this._flush))
+ if (typeof this._flush === 'function')
this._flush(function(er) {
done(stream, er);
});
@@ -159,7 +167,7 @@ Transform.prototype._write = function(chunk, encoding, cb) {
Transform.prototype._read = function(n) {
var ts = this._transformState;
- if (!util.isNull(ts.writechunk) && ts.writecb && !ts.transforming) {
+ if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
ts.transforming = true;
this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
} else {
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js
index 45152845e1..44f698168e 100644
--- a/lib/_stream_writable.js
+++ b/lib/_stream_writable.js
@@ -6,6 +6,11 @@
module.exports = Writable;
+/**/
+var processNextTick = require('process-nextick-args');
+/**/
+
+
/**/
var Buffer = require('buffer').Buffer;
/**/
@@ -18,21 +23,20 @@ var util = require('core-util-is');
util.inherits = require('inherits');
/**/
-var Stream = require('stream');
/**/
-var debug = require('util');
-if (debug && debug.debuglog) {
- debug = debug.debuglog('stream');
-} else {
- debug = function () {};
-}
+var Stream;
+ (function (){try{
+Stream = require('st' + 'ream');
+}catch(_){Stream = require('events').EventEmitter;}}())
/**/
util.inherits(Writable, Stream);
+function nop() {}
+
function WriteReq(chunk, encoding, cb) {
this.chunk = chunk;
this.encoding = encoding;
@@ -159,6 +163,14 @@ function Writable(options) {
// legacy.
this.writable = true;
+ if (options) {
+ if (typeof options.write === 'function')
+ this._write = options.write;
+
+ if (typeof options.writev === 'function')
+ this._writev = options.writev;
+ }
+
Stream.call(this);
}
@@ -168,13 +180,11 @@ Writable.prototype.pipe = function() {
};
-function writeAfterEnd(stream, state, cb) {
+function writeAfterEnd(stream, cb) {
var er = new Error('write after end');
// TODO: defer error events consistently everywhere, not just the cb
stream.emit('error', er);
- process.nextTick(function() {
- cb(er);
- });
+ processNextTick(cb, er);
}
// If we get something that is not a buffer, string, null, or undefined,
@@ -184,15 +194,15 @@ function writeAfterEnd(stream, state, cb) {
// how many bytes or characters.
function validChunk(stream, state, chunk, cb) {
var valid = true;
- if (!util.isBuffer(chunk) &&
- !util.isString(chunk) &&
- !util.isNullOrUndefined(chunk) &&
+
+ if (!(Buffer.isBuffer(chunk)) &&
+ typeof chunk !== 'string' &&
+ chunk !== null &&
+ chunk !== undefined &&
!state.objectMode) {
var er = new TypeError('Invalid non-string/buffer chunk');
stream.emit('error', er);
- process.nextTick(function() {
- cb(er);
- });
+ processNextTick(cb, er);
valid = false;
}
return valid;
@@ -202,21 +212,21 @@ Writable.prototype.write = function(chunk, encoding, cb) {
var state = this._writableState;
var ret = false;
- if (util.isFunction(encoding)) {
+ if (typeof encoding === 'function') {
cb = encoding;
encoding = null;
}
- if (util.isBuffer(chunk))
+ if (chunk instanceof Buffer)
encoding = 'buffer';
else if (!encoding)
encoding = state.defaultEncoding;
- if (!util.isFunction(cb))
- cb = function() {};
+ if (typeof cb !== 'function')
+ cb = nop;
if (state.ended)
- writeAfterEnd(this, state, cb);
+ writeAfterEnd(this, cb);
else if (validChunk(this, state, chunk, cb)) {
state.pendingcb++;
ret = writeOrBuffer(this, state, chunk, encoding, cb);
@@ -250,7 +260,9 @@ Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
// node::ParseEncoding() requires lower case.
if (typeof encoding === 'string')
encoding = encoding.toLowerCase();
- if (!Buffer.isEncoding(encoding))
+ if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64',
+'ucs2', 'ucs-2','utf16le', 'utf-16le', 'raw']
+.indexOf((encoding + '').toLowerCase()) > -1))
throw new TypeError('Unknown encoding: ' + encoding);
this._writableState.defaultEncoding = encoding;
};
@@ -258,7 +270,7 @@ Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
function decodeChunk(state, chunk, encoding) {
if (!state.objectMode &&
state.decodeStrings !== false &&
- util.isString(chunk)) {
+ typeof chunk === 'string') {
chunk = new Buffer(chunk, encoding);
}
return chunk;
@@ -269,7 +281,8 @@ function decodeChunk(state, chunk, encoding) {
// If we return false, then we need a drain event, so set that flag.
function writeOrBuffer(stream, state, chunk, encoding, cb) {
chunk = decodeChunk(state, chunk, encoding);
- if (util.isBuffer(chunk))
+
+ if (chunk instanceof Buffer)
encoding = 'buffer';
var len = state.objectMode ? 1 : chunk.length;
@@ -288,9 +301,9 @@ function writeOrBuffer(stream, state, chunk, encoding, cb) {
} else {
state.bufferedRequest = state.lastBufferedRequest;
}
- }
- else
+ } else {
doWrite(stream, state, false, len, chunk, encoding, cb);
+ }
return ret;
}
@@ -308,15 +321,11 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) {
}
function onwriteError(stream, state, sync, er, cb) {
+ --state.pendingcb;
if (sync)
- process.nextTick(function() {
- state.pendingcb--;
- cb(er);
- });
- else {
- state.pendingcb--;
+ processNextTick(cb, er);
+ else
cb(er);
- }
stream._writableState.errorEmitted = true;
stream.emit('error', er);
@@ -340,7 +349,7 @@ function onwrite(stream, er) {
onwriteError(stream, state, sync, er, cb);
else {
// Check if we're actually ready to finish, but don't emit yet
- var finished = needFinish(stream, state);
+ var finished = needFinish(state);
if (!finished &&
!state.corked &&
@@ -350,9 +359,7 @@ function onwrite(stream, er) {
}
if (sync) {
- process.nextTick(function() {
- afterWrite(stream, state, finished, cb);
- });
+ processNextTick(afterWrite, stream, state, finished, cb);
} else {
afterWrite(stream, state, finished, cb);
}
@@ -433,7 +440,6 @@ function clearBuffer(stream, state) {
Writable.prototype._write = function(chunk, encoding, cb) {
cb(new Error('not implemented'));
-
};
Writable.prototype._writev = null;
@@ -441,16 +447,16 @@ Writable.prototype._writev = null;
Writable.prototype.end = function(chunk, encoding, cb) {
var state = this._writableState;
- if (util.isFunction(chunk)) {
+ if (typeof chunk === 'function') {
cb = chunk;
chunk = null;
encoding = null;
- } else if (util.isFunction(encoding)) {
+ } else if (typeof encoding === 'function') {
cb = encoding;
encoding = null;
}
- if (!util.isNullOrUndefined(chunk))
+ if (chunk !== null && chunk !== undefined)
this.write(chunk, encoding);
// .end() fully uncorks
@@ -465,7 +471,7 @@ Writable.prototype.end = function(chunk, encoding, cb) {
};
-function needFinish(stream, state) {
+function needFinish(state) {
return (state.ending &&
state.length === 0 &&
state.bufferedRequest === null &&
@@ -481,14 +487,15 @@ function prefinish(stream, state) {
}
function finishMaybe(stream, state) {
- var need = needFinish(stream, state);
+ var need = needFinish(state);
if (need) {
if (state.pendingcb === 0) {
prefinish(stream, state);
state.finished = true;
stream.emit('finish');
- } else
+ } else {
prefinish(stream, state);
+ }
}
return need;
}
@@ -498,7 +505,7 @@ function endWritable(stream, state, cb) {
finishMaybe(stream, state);
if (cb) {
if (state.finished)
- process.nextTick(cb);
+ processNextTick(cb);
else
stream.once('finish', cb);
}
diff --git a/package.json b/package.json
index a3b877ff99..595f2aaeda 100644
--- a/package.json
+++ b/package.json
@@ -1,10 +1,11 @@
{
"name": "readable-stream",
- "version": "1.1.13",
- "description": "Streams3, a user-land copy of the stream library from Node.js v0.11.x",
+ "version": "2.0.0",
+ "description": "Streams3, a user-land copy of the stream library from iojs v2.x",
"main": "readable.js",
"dependencies": {
"core-util-is": "~1.0.0",
+ "process-nextick-args": "~1.0.0",
"inherits": "~2.0.1",
"isarray": "0.0.1",
"string_decoder": "~0.10.x",
@@ -18,7 +19,7 @@
},
"repository": {
"type": "git",
- "url": "git://github.com/iojs/readable-stream"
+ "url": "git://github.com/nodejs/readable-stream"
},
"keywords": [
"readable",
@@ -28,6 +29,5 @@
"browser": {
"util": false
},
- "author": "Isaac Z. Schlueter (http://blog.izs.me/)",
"license": "MIT"
}
diff --git a/readable.js b/readable.js
index 8b5337b5c5..700b431e4b 100644
--- a/readable.js
+++ b/readable.js
@@ -1,6 +1,9 @@
-var Stream = require('stream'); // hack to fix a circular dependency issue when used with browserify
+(function (){
+ try {
+ exports.Stream = require('st' + 'ream'); // hack to fix a circular dependency issue when used with browserify
+ } catch(_){}
+}());
exports = module.exports = require('./lib/_stream_readable.js');
-exports.Stream = Stream;
exports.Readable = exports;
exports.Writable = require('./lib/_stream_writable.js');
exports.Duplex = require('./lib/_stream_duplex.js');
diff --git a/test/common.js b/test/common.js
index f8ac8c7ae7..e2ef6257bb 100644
--- a/test/common.js
+++ b/test/common.js
@@ -1,43 +1,135 @@
-// Copyright Joyent, Inc. and other Node contributors.
-//
-// 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.
-
+/**/
+if (!global.setImmediate) {
+ global.setImmediate = function setImmediate(fn) {
+ return setTimeout(fn.bind.apply(fn, arguments), 0);
+ };
+}
+if (!global.clearImmediate) {
+ global.clearImmediate = function clearImmediate(i) {
+ return clearTimeout(i);
+ };
+}
+/**/
+'use strict';
var path = require('path');
var fs = require('fs');
var assert = require('assert');
+var os = require('os');
+var child_process = require('child_process');
exports.testDir = path.dirname(__filename);
exports.fixturesDir = path.join(exports.testDir, 'fixtures');
exports.libDir = path.join(exports.testDir, '../lib');
-exports.tmpDir = path.join(exports.testDir, 'tmp');
+exports.tmpDirName = 'tmp';
exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
+if (process.env.TEST_THREAD_ID) {
+ // Distribute ports in parallel tests
+ if (!process.env.NODE_COMMON_PORT)
+ exports.PORT += +process.env.TEST_THREAD_ID * 100;
+
+ exports.tmpDirName += '.' + process.env.TEST_THREAD_ID;
+}
+exports.tmpDir = path.join(exports.testDir, exports.tmpDirName);
+
+var opensslCli = null;
+var inFreeBSDJail = null;
+var localhostIPv4 = null;
+
+Object.defineProperty(exports, 'inFreeBSDJail', {
+ get: function() {
+ if (inFreeBSDJail !== null) return inFreeBSDJail;
+
+ if (process.platform === 'freebsd' &&
+ child_process.execSync('sysctl -n security.jail.jailed').toString() ===
+ '1\n') {
+ inFreeBSDJail = true;
+ } else {
+ inFreeBSDJail = false;
+ }
+ return inFreeBSDJail;
+ }
+});
+
+Object.defineProperty(exports, 'localhostIPv4', {
+ get: function() {
+ if (localhostIPv4 !== null) return localhostIPv4;
+
+ if (exports.inFreeBSDJail) {
+ // Jailed network interfaces are a bit special - since we need to jump
+ // through loops, as well as this being an exception case, assume the
+ // user will provide this instead.
+ if (process.env.LOCALHOST) {
+ localhostIPv4 = process.env.LOCALHOST;
+ } else {
+ console.error('Looks like we\'re in a FreeBSD Jail. ' +
+ 'Please provide your default interface address ' +
+ 'as LOCALHOST or expect some tests to fail.');
+ }
+ }
+
+ if (localhostIPv4 === null) localhostIPv4 = '127.0.0.1';
+
+ return localhostIPv4;
+ }
+});
+
+// opensslCli defined lazily to reduce overhead of spawnSync
+Object.defineProperty(exports, 'opensslCli', {get: function() {
+ if (opensslCli !== null) return opensslCli;
+
+ if (process.config.variables.node_shared_openssl) {
+ // use external command
+ opensslCli = 'openssl';
+ } else {
+ // use command built from sources included in io.js repository
+ opensslCli = path.join(path.dirname(process.execPath), 'openssl-cli');
+ }
+
+ if (process.platform === 'win32') opensslCli += '.exe';
+
+ var openssl_cmd = child_process.spawnSync(opensslCli, ['version']);
+ if (openssl_cmd.status !== 0 || openssl_cmd.error !== undefined) {
+ // openssl command cannot be executed
+ opensslCli = false;
+ }
+ return opensslCli;
+}, enumerable: true });
+
+Object.defineProperty(exports, 'hasCrypto', {get: function() {
+ return process.versions.openssl ? true : false;
+}});
+
if (process.platform === 'win32') {
exports.PIPE = '\\\\.\\pipe\\libuv-test';
- exports.opensslCli = path.join(process.execPath, '..', 'openssl-cli.exe');
} else {
exports.PIPE = exports.tmpDir + '/test.sock';
- exports.opensslCli = path.join(process.execPath, '..', 'openssl-cli');
}
-if (!fs.existsSync(exports.opensslCli))
- exports.opensslCli = false;
+
+if (process.env.NODE_COMMON_PIPE) {
+ exports.PIPE = process.env.NODE_COMMON_PIPE;
+ // Remove manually, the test runner won't do it
+ // for us like it does for files in test/tmp.
+ try {
+ fs.unlinkSync(exports.PIPE);
+ } catch (e) {
+ // Ignore.
+ }
+}
+
+if (process.platform === 'win32') {
+ exports.faketimeCli = false;
+} else {
+ exports.faketimeCli = path.join(__dirname, '..', 'tools', 'faketime', 'src',
+ 'faketime');
+}
+
+var ifaces = os.networkInterfaces();
+exports.hasIPv6 = Object.keys(ifaces).some(function(name) {
+ return /lo/.test(name) && ifaces[name].some(function(info) {
+ return info.family === 'IPv6';
+ });
+});
var util = require('util');
for (var i in util) exports[i] = util[i];
@@ -79,6 +171,17 @@ exports.spawnCat = function(options) {
};
+exports.spawnSyncCat = function(options) {
+ var spawnSync = require('child_process').spawnSync;
+
+ if (process.platform === 'win32') {
+ return spawnSync('more', [], options);
+ } else {
+ return spawnSync('cat', [], options);
+ }
+};
+
+
exports.spawnPwd = function(options) {
var spawn = require('child_process').spawn;
@@ -89,6 +192,16 @@ exports.spawnPwd = function(options) {
}
};
+exports.platformTimeout = function(ms) {
+ if (process.arch !== 'arm')
+ return ms;
+
+ if (process.config.variables.arm_version === '6')
+ return 7 * ms; // ARMv6
+
+ return 2 * ms; // ARMv7 and up.
+};
+
var knownGlobals = [setTimeout,
setInterval,
setImmediate,
@@ -112,8 +225,6 @@ if (global.DTRACE_HTTP_SERVER_RESPONSE) {
knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST);
knownGlobals.push(DTRACE_NET_STREAM_END);
knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
- knownGlobals.push(DTRACE_NET_SOCKET_READ);
- knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
}
if (global.COUNTER_NET_SERVER_CONNECTION) {
@@ -125,6 +236,15 @@ if (global.COUNTER_NET_SERVER_CONNECTION) {
knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE);
}
+if (global.LTTNG_HTTP_SERVER_RESPONSE) {
+ knownGlobals.push(LTTNG_HTTP_SERVER_RESPONSE);
+ knownGlobals.push(LTTNG_HTTP_SERVER_REQUEST);
+ knownGlobals.push(LTTNG_HTTP_CLIENT_RESPONSE);
+ knownGlobals.push(LTTNG_HTTP_CLIENT_REQUEST);
+ knownGlobals.push(LTTNG_NET_STREAM_END);
+ knownGlobals.push(LTTNG_NET_SERVER_CONNECTION);
+}
+
if (global.ArrayBuffer) {
knownGlobals.push(ArrayBuffer);
knownGlobals.push(Int8Array);
@@ -151,6 +271,10 @@ if (global.Symbol) {
/**/
if (typeof constructor == 'function')
knownGlobals.push(constructor);
+ if (typeof DTRACE_NET_SOCKET_READ == 'function')
+ knownGlobals.push(DTRACE_NET_SOCKET_READ);
+ if (typeof DTRACE_NET_SOCKET_WRITE == 'function')
+ knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
/**/
function leakedGlobals() {
@@ -205,7 +329,7 @@ exports.mustCall = function(fn, expected) {
var context = {
expected: expected,
actual: 0,
- stack: (new Error).stack,
+ stack: (new Error()).stack,
name: fn.name || ''
};
@@ -225,6 +349,87 @@ exports.checkSpawnSyncRet = function(ret) {
assert.strictEqual(ret.error, undefined);
};
+var etcServicesFileName = path.join('/etc', 'services');
+if (process.platform === 'win32') {
+ etcServicesFileName = path.join(process.env.SystemRoot, 'System32', 'drivers',
+ 'etc', 'services');
+}
+
+/*
+ * Returns a string that represents the service name associated
+ * to the service bound to port "port" and using protocol "protocol".
+ *
+ * If the service is not defined in the services file, it returns
+ * the port number as a string.
+ *
+ * Returns undefined if /etc/services (or its equivalent on non-UNIX
+ * platforms) can't be read.
+ */
+exports.getServiceName = function getServiceName(port, protocol) {
+ if (port == null) {
+ throw new Error('Missing port number');
+ }
+
+ if (typeof protocol !== 'string') {
+ throw new Error('Protocol must be a string');
+ }
+
+ /*
+ * By default, if a service can't be found in /etc/services,
+ * its name is considered to be its port number.
+ */
+ var serviceName = port.toString();
+
+ try {
+ /*
+ * I'm not a big fan of readFileSync, but reading /etc/services
+ * asynchronously here would require implementing a simple line parser,
+ * which seems overkill for a simple utility function that is not running
+ * concurrently with any other one.
+ */
+ var servicesContent = fs.readFileSync(etcServicesFileName,
+ { encoding: 'utf8'});
+ var regexp = util.format('^(\\w+)\\s+\\s%d/%s\\s', port, protocol);
+ var re = new RegExp(regexp, 'm');
+
+ var matches = re.exec(servicesContent);
+ if (matches && matches.length > 1) {
+ serviceName = matches[1];
+ }
+ } catch(e) {
+ console.error('Cannot read file: ', etcServicesFileName);
+ return undefined;
+ }
+
+ return serviceName;
+};
+
+exports.hasMultiLocalhost = function hasMultiLocalhost() {
+ var TCP = process.binding('tcp_wrap').TCP;
+ var t = new TCP();
+ var ret = t.bind('127.0.0.2', exports.PORT);
+ t.close();
+ return ret === 0;
+};
+
+exports.isValidHostname = function(str) {
+ // See http://stackoverflow.com/a/3824105
+ var re = new RegExp(
+ '^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])' +
+ '(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*$');
+
+ return !!str.match(re) && str.length <= 255;
+};
+
+exports.fileExists = function(pathname) {
+ try {
+ fs.accessSync(pathname);
+ return true;
+ } catch (err) {
+ return false;
+ }
+};
+
function forEach (xs, f) {
for (var i = 0, l = xs.length; i < l; i++) {
f(xs[i], i);
@@ -243,15 +448,3 @@ if (!util._errnoException) {
return e;
};
}
-/**/
-if (!global.setImmediate) {
- global.setImmediate = function setImmediate(fn) {
- return setTimeout(fn, 0);
- };
-}
-if (!global.clearImmediate) {
- global.clearImmediate = function clearImmediate(i) {
- return clearTimeout(i);
- };
-}
-/**/
diff --git a/test/parallel/test-stream-big-packet.js b/test/parallel/test-stream-big-packet.js
index 5c2adcdb33..b2ef864b23 100644
--- a/test/parallel/test-stream-big-packet.js
+++ b/test/parallel/test-stream-big-packet.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
var util = require('util');
@@ -5,20 +6,20 @@ var stream = require('../../');
var passed = false;
-function PassThrough () {
+function PassThrough() {
stream.Transform.call(this);
};
util.inherits(PassThrough, stream.Transform);
-PassThrough.prototype._transform = function (chunk, encoding, done) {
+PassThrough.prototype._transform = function(chunk, encoding, done) {
this.push(chunk);
done();
};
-function TestStream () {
+function TestStream() {
stream.Transform.call(this);
};
util.inherits(TestStream, stream.Transform);
-TestStream.prototype._transform = function (chunk, encoding, done) {
+TestStream.prototype._transform = function(chunk, encoding, done) {
if (!passed) {
// Char 'a' only exists in the last write
passed = indexOf(chunk.toString(), 'a') >= 0;
@@ -47,7 +48,7 @@ assert(s2.write('tiny'));
setImmediate(s1.write.bind(s1), 'later');
// Assert after two IO loops when all operations have been done.
-process.on('exit', function () {
+process.on('exit', function() {
assert(passed, 'Large buffer is not handled properly by Writable Stream');
});
diff --git a/test/parallel/test-stream-big-push.js b/test/parallel/test-stream-big-push.js
index aa5b88f3fd..c762fce42f 100644
--- a/test/parallel/test-stream-big-push.js
+++ b/test/parallel/test-stream-big-push.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
var stream = require('../../');
diff --git a/test/parallel/test-stream-duplex.js b/test/parallel/test-stream-duplex.js
index 7f38a57376..1abc835f6a 100644
--- a/test/parallel/test-stream-duplex.js
+++ b/test/parallel/test-stream-duplex.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
@@ -11,21 +12,21 @@ assert(stream._writableState.objectMode);
var written;
var read;
-stream._write = function (obj, _, cb) {
+stream._write = function(obj, _, cb) {
written = obj;
cb();
};
-stream._read = function () {};
+stream._read = function() {};
-stream.on('data', function (obj) {
+stream.on('data', function(obj) {
read = obj;
});
stream.push({ val: 1 });
stream.end({ val: 2 });
-process.on('exit', function () {
+process.on('exit', function() {
assert(read.val === 1);
assert(written.val === 2);
});
diff --git a/test/parallel/test-stream-end-paused.js b/test/parallel/test-stream-end-paused.js
index 9f9d7337a0..cbd98575f0 100644
--- a/test/parallel/test-stream-end-paused.js
+++ b/test/parallel/test-stream-end-paused.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
var gotEnd = false;
diff --git a/test/parallel/test-stream-ispaused.js b/test/parallel/test-stream-ispaused.js
index dba895bd09..4a6c2868b8 100644
--- a/test/parallel/test-stream-ispaused.js
+++ b/test/parallel/test-stream-ispaused.js
@@ -1,9 +1,10 @@
+'use strict';
var assert = require('assert');
var common = require('../common');
var stream = require('../../');
-var readable = new stream.Readable;
+var readable = new stream.Readable();
// _read is a noop, here.
readable._read = Function();
diff --git a/test/parallel/test-stream-pipe-after-end.js b/test/parallel/test-stream-pipe-after-end.js
index cfd98ea56a..be9361da8a 100644
--- a/test/parallel/test-stream-pipe-after-end.js
+++ b/test/parallel/test-stream-pipe-after-end.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
diff --git a/test/parallel/test-stream-pipe-cleanup.js b/test/parallel/test-stream-pipe-cleanup.js
index b7abca2451..fd5644e3c4 100644
--- a/test/parallel/test-stream-pipe-cleanup.js
+++ b/test/parallel/test-stream-pipe-cleanup.js
@@ -1,3 +1,4 @@
+'use strict';
// This test asserts that Stream.prototype.pipe does not leave listeners
// hanging on the source or dest.
diff --git a/test/parallel/test-stream-pipe-error-handling.js b/test/parallel/test-stream-pipe-error-handling.js
index d44333afea..316b15c598 100644
--- a/test/parallel/test-stream-pipe-error-handling.js
+++ b/test/parallel/test-stream-pipe-error-handling.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
var Stream = require('stream').Stream;
@@ -41,8 +42,8 @@ var Stream = require('stream').Stream;
var R = require('../../').Readable;
var W = require('../../').Writable;
- var r = new R;
- var w = new W;
+ var r = new R();
+ var w = new W();
var removed = false;
var didTest = false;
@@ -76,8 +77,8 @@ var Stream = require('stream').Stream;
var R = require('../../').Readable;
var W = require('../../').Writable;
- var r = new R;
- var w = new W;
+ var r = new R();
+ var w = new W();
var removed = false;
var didTest = false;
var caught = false;
diff --git a/test/parallel/test-stream-pipe-event.js b/test/parallel/test-stream-pipe-event.js
index 72672a2cdb..1a52c63b5f 100644
--- a/test/parallel/test-stream-pipe-event.js
+++ b/test/parallel/test-stream-pipe-event.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var stream = require('../../');
var assert = require('assert');
diff --git a/test/parallel/test-stream-push-order.js b/test/parallel/test-stream-push-order.js
index 3254cf15b9..b9e8074be3 100644
--- a/test/parallel/test-stream-push-order.js
+++ b/test/parallel/test-stream-push-order.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var Readable = require('../../').Readable;
var assert = require('assert');
@@ -9,7 +10,7 @@ var s = new Readable({
var list = ['1', '2', '3', '4', '5', '6'];
-s._read = function (n) {
+s._read = function(n) {
var one = list.shift();
if (!one) {
s.push(null);
@@ -24,8 +25,8 @@ var v = s.read(0);
// ACTUALLY [1, 3, 5, 6, 4, 2]
-process.on("exit", function () {
+process.on('exit', function() {
assert.deepEqual(s._readableState.buffer,
['1', '2', '3', '4', '5', '6']);
- console.log("ok");
+ console.log('ok');
});
diff --git a/test/parallel/test-stream-push-strings.js b/test/parallel/test-stream-push-strings.js
index 6aa1db2fb1..8244a23986 100644
--- a/test/parallel/test-stream-push-strings.js
+++ b/test/parallel/test-stream-push-strings.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
diff --git a/test/parallel/test-stream-readable-constructor-set-methods.js b/test/parallel/test-stream-readable-constructor-set-methods.js
new file mode 100644
index 0000000000..9dead92111
--- /dev/null
+++ b/test/parallel/test-stream-readable-constructor-set-methods.js
@@ -0,0 +1,19 @@
+'use strict';
+var common = require('../common');
+var assert = require('assert');
+
+var Readable = require('../../').Readable;
+
+var _readCalled = false;
+function _read(n) {
+ _readCalled = true;
+ this.push(null);
+}
+
+var r = new Readable({ read: _read });
+r.resume();
+
+process.on('exit', function() {
+ assert.equal(r._read, _read);
+ assert(_readCalled);
+});
diff --git a/test/parallel/test-stream-readable-event.js b/test/parallel/test-stream-readable-event.js
index 8c87952f9b..ad4593956a 100644
--- a/test/parallel/test-stream-readable-event.js
+++ b/test/parallel/test-stream-readable-event.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
diff --git a/test/parallel/test-stream-readable-flow-recursion.js b/test/parallel/test-stream-readable-flow-recursion.js
index c2cdaa48cb..abf6795cc3 100644
--- a/test/parallel/test-stream-readable-flow-recursion.js
+++ b/test/parallel/test-stream-readable-flow-recursion.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
diff --git a/test/parallel/test-stream-transform-constructor-set-methods.js b/test/parallel/test-stream-transform-constructor-set-methods.js
new file mode 100644
index 0000000000..876fd5a91e
--- /dev/null
+++ b/test/parallel/test-stream-transform-constructor-set-methods.js
@@ -0,0 +1,32 @@
+'use strict';
+var common = require('../common');
+var assert = require('assert');
+
+var Transform = require('../../').Transform;
+
+var _transformCalled = false;
+function _transform(d, e, n) {
+ _transformCalled = true;
+ n();
+}
+
+var _flushCalled = false;
+function _flush(n) {
+ _flushCalled = true;
+ n();
+}
+
+var t = new Transform({
+ transform: _transform,
+ flush: _flush
+});
+
+t.end(new Buffer('blerg'));
+t.resume();
+
+process.on('exit', function() {
+ assert.equal(t._transform, _transform);
+ assert.equal(t._flush, _flush);
+ assert(_transformCalled);
+ assert(_flushCalled);
+});
diff --git a/test/parallel/test-stream-transform-objectmode-falsey-value.js b/test/parallel/test-stream-transform-objectmode-falsey-value.js
index 0d4e79aeaa..e29a072068 100644
--- a/test/parallel/test-stream-transform-objectmode-falsey-value.js
+++ b/test/parallel/test-stream-transform-objectmode-falsey-value.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
diff --git a/test/parallel/test-stream-transform-split-objectmode.js b/test/parallel/test-stream-transform-split-objectmode.js
index cdac2515f2..63df697e7c 100644
--- a/test/parallel/test-stream-transform-split-objectmode.js
+++ b/test/parallel/test-stream-transform-split-objectmode.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
@@ -10,19 +11,19 @@ assert(!parser._writableState.objectMode);
assert(parser._readableState.highWaterMark === 16);
assert(parser._writableState.highWaterMark === (16 * 1024));
-parser._transform = function (chunk, enc, callback) {
+parser._transform = function(chunk, enc, callback) {
callback(null, { val : chunk[0] });
};
var parsed;
-parser.on('data', function (obj) {
+parser.on('data', function(obj) {
parsed = obj;
});
parser.end(new Buffer([42]));
-process.on('exit', function () {
+process.on('exit', function() {
assert(parsed.val === 42);
});
@@ -34,18 +35,18 @@ assert(serializer._writableState.objectMode);
assert(serializer._readableState.highWaterMark === (16 * 1024));
assert(serializer._writableState.highWaterMark === 16);
-serializer._transform = function (obj, _, callback) {
+serializer._transform = function(obj, _, callback) {
callback(null, new Buffer([obj.val]));
};
var serialized;
-serializer.on('data', function (chunk) {
+serializer.on('data', function(chunk) {
serialized = chunk;
});
serializer.write({ val : 42 });
-process.on('exit', function () {
+process.on('exit', function() {
assert(serialized[0] === 42);
});
diff --git a/test/parallel/test-stream-unshift-empty-chunk.js b/test/parallel/test-stream-unshift-empty-chunk.js
index 8b7114ba74..0c0cc9a791 100644
--- a/test/parallel/test-stream-unshift-empty-chunk.js
+++ b/test/parallel/test-stream-unshift-empty-chunk.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
diff --git a/test/parallel/test-stream-unshift-read-race.js b/test/parallel/test-stream-unshift-read-race.js
index 345c532e3a..2667235ca6 100644
--- a/test/parallel/test-stream-unshift-read-race.js
+++ b/test/parallel/test-stream-unshift-read-race.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
@@ -30,7 +31,7 @@ r._read = function(n) {
function push(fast) {
assert(!pushedNull, 'push() after null push');
- var c = pos >= data.length ? null : data.slice(pos, pos + n);
+ var c = pos >= data.length ? null : data.slice(pos, Math.min(pos + n, data.length));
pushedNull = c === null;
if (fast) {
pos += n;
diff --git a/test/parallel/test-stream-writable-change-default-encoding.js b/test/parallel/test-stream-writable-change-default-encoding.js
index b34a481cde..5d80abc1cb 100644
--- a/test/parallel/test-stream-writable-change-default-encoding.js
+++ b/test/parallel/test-stream-writable-change-default-encoding.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
@@ -11,7 +12,7 @@ function MyWritable(fn, options) {
util.inherits(MyWritable, stream.Writable);
-MyWritable.prototype._write = function (chunk, encoding, callback) {
+MyWritable.prototype._write = function(chunk, encoding, callback) {
this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding);
callback();
};
diff --git a/test/parallel/test-stream-writable-constructor-set-methods.js b/test/parallel/test-stream-writable-constructor-set-methods.js
new file mode 100644
index 0000000000..ea35dfce79
--- /dev/null
+++ b/test/parallel/test-stream-writable-constructor-set-methods.js
@@ -0,0 +1,35 @@
+'use strict';
+var common = require('../common');
+var assert = require('assert');
+
+var Writable = require('../../').Writable;
+
+var _writeCalled = false;
+function _write(d, e, n) {
+ _writeCalled = true;
+}
+
+var w = new Writable({ write: _write });
+w.end(new Buffer('blerg'));
+
+var _writevCalled = false;
+var dLength = 0;
+function _writev(d, n) {
+ dLength = d.length;
+ _writevCalled = true;
+}
+
+var w2 = new Writable({ writev: _writev });
+w2.cork();
+
+w2.write(new Buffer('blerg'));
+w2.write(new Buffer('blerg'));
+w2.end();
+
+process.on('exit', function() {
+ assert.equal(w._write, _write);
+ assert(_writeCalled);
+ assert.equal(w2._writev, _writev);
+ assert.equal(dLength, 2);
+ assert(_writevCalled);
+});
diff --git a/test/parallel/test-stream-writable-decoded-encoding.js b/test/parallel/test-stream-writable-decoded-encoding.js
index 7fc49d246a..06d11e2328 100644
--- a/test/parallel/test-stream-writable-decoded-encoding.js
+++ b/test/parallel/test-stream-writable-decoded-encoding.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
@@ -11,7 +12,7 @@ function MyWritable(fn, options) {
util.inherits(MyWritable, stream.Writable);
-MyWritable.prototype._write = function (chunk, encoding, callback) {
+MyWritable.prototype._write = function(chunk, encoding, callback) {
this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding);
callback();
};
diff --git a/test/parallel/test-stream-writev.js b/test/parallel/test-stream-writev.js
index 4e1f13c8e6..5dea49b965 100644
--- a/test/parallel/test-stream-writev.js
+++ b/test/parallel/test-stream-writev.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
@@ -45,19 +46,24 @@ function test(decode, uncork, multi, next) {
};
var expectChunks = decode ?
- [{ encoding: 'buffer',
- chunk: [104, 101, 108, 108, 111, 44, 32] },
- { encoding: 'buffer', chunk: [119, 111, 114, 108, 100] },
- { encoding: 'buffer', chunk: [33] },
- { encoding: 'buffer',
- chunk: [10, 97, 110, 100, 32, 116, 104, 101, 110, 46, 46, 46] },
- { encoding: 'buffer',
- chunk: [250, 206, 190, 167, 222, 173, 190, 239, 222, 202, 251, 173] }] :
- [{ encoding: 'ascii', chunk: 'hello, ' },
+ [
+ { encoding: 'buffer',
+ chunk: [104, 101, 108, 108, 111, 44, 32] },
+ { encoding: 'buffer',
+ chunk: [119, 111, 114, 108, 100] },
+ { encoding: 'buffer',
+ chunk: [33] },
+ { encoding: 'buffer',
+ chunk: [10, 97, 110, 100, 32, 116, 104, 101, 110, 46, 46, 46] },
+ { encoding: 'buffer',
+ chunk: [250, 206, 190, 167, 222, 173, 190, 239, 222, 202, 251, 173]}
+ ] : [
+ { encoding: 'ascii', chunk: 'hello, ' },
{ encoding: 'utf8', chunk: 'world' },
{ encoding: 'buffer', chunk: [33] },
{ encoding: 'binary', chunk: '\nand then...' },
- { encoding: 'hex', chunk: 'facebea7deadbeefdecafbad' }];
+ { encoding: 'hex', chunk: 'facebea7deadbeefdecafbad' }
+ ];
var actualChunks;
w._writev = function(chunks, cb) {
diff --git a/test/parallel/test-stream2-base64-single-char-read-end.js b/test/parallel/test-stream2-base64-single-char-read-end.js
index 0c428563b7..8c1794a9ab 100644
--- a/test/parallel/test-stream2-base64-single-char-read-end.js
+++ b/test/parallel/test-stream2-base64-single-char-read-end.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var R = require('../../lib/_stream_readable');
var W = require('../../lib/_stream_writable');
var assert = require('assert');
@@ -27,7 +28,7 @@ dst._write = function(chunk, enc, cb) {
src.on('end', function() {
assert.equal(Buffer.concat(accum) + '', 'MQ==');
clearTimeout(timeout);
-})
+});
src.pipe(dst);
diff --git a/test/parallel/test-stream2-compatibility.js b/test/parallel/test-stream2-compatibility.js
index 69724bb084..e0a3a91bfd 100644
--- a/test/parallel/test-stream2-compatibility.js
+++ b/test/parallel/test-stream2-compatibility.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var R = require('../../lib/_stream_readable');
var assert = require('assert');
diff --git a/test/parallel/test-stream2-finish-pipe.js b/test/parallel/test-stream2-finish-pipe.js
index 5b5c491f34..bfdd212bd8 100644
--- a/test/parallel/test-stream2-finish-pipe.js
+++ b/test/parallel/test-stream2-finish-pipe.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var stream = require('../../');
var Buffer = require('buffer').Buffer;
diff --git a/test/parallel/test-stream2-large-read-stall.js b/test/parallel/test-stream2-large-read-stall.js
index 493eefbdf0..334eff86d4 100644
--- a/test/parallel/test-stream2-large-read-stall.js
+++ b/test/parallel/test-stream2-large-read-stall.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var assert = require('assert');
// If everything aligns so that you do a read(n) of exactly the
diff --git a/test/parallel/test-stream2-objects.js b/test/parallel/test-stream2-objects.js
index 491dd31dee..1c3e66cd4a 100644
--- a/test/parallel/test-stream2-objects.js
+++ b/test/parallel/test-stream2-objects.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var Readable = require('../../lib/_stream_readable');
var Writable = require('../../lib/_stream_writable');
var assert = require('assert');
diff --git a/test/parallel/test-stream2-pipe-error-handling.js b/test/parallel/test-stream2-pipe-error-handling.js
index 04ade1844d..98d452c111 100644
--- a/test/parallel/test-stream2-pipe-error-handling.js
+++ b/test/parallel/test-stream2-pipe-error-handling.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
var stream = require('../../');
diff --git a/test/parallel/test-stream2-pipe-error-once-listener.js b/test/parallel/test-stream2-pipe-error-once-listener.js
index 1f2bba1e4c..0b6766da49 100644
--- a/test/parallel/test-stream2-pipe-error-once-listener.js
+++ b/test/parallel/test-stream2-pipe-error-once-listener.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var assert = require('assert');
var util = require('util');
diff --git a/test/parallel/test-stream2-push.js b/test/parallel/test-stream2-push.js
index 43300539e3..a4ba95d3fc 100644
--- a/test/parallel/test-stream2-push.js
+++ b/test/parallel/test-stream2-push.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var stream = require('../../');
var Readable = stream.Readable;
var Writable = stream.Writable;
@@ -15,7 +16,7 @@ var stream = new Readable({
encoding: 'utf8'
});
-var source = new EE;
+var source = new EE();
stream._read = function() {
console.error('stream._read');
@@ -80,7 +81,7 @@ writer.on('finish', finish);
// now emit some chunks.
-var chunk = "asdfg";
+var chunk = 'asdfg';
var set = 0;
readStart();
diff --git a/test/parallel/test-stream2-read-sync-stack.js b/test/parallel/test-stream2-read-sync-stack.js
index b2bb06c195..cf1bcf2e2e 100644
--- a/test/parallel/test-stream2-read-sync-stack.js
+++ b/test/parallel/test-stream2-read-sync-stack.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
var Readable = require('../../').Readable;
diff --git a/test/parallel/test-stream2-readable-empty-buffer-no-eof.js b/test/parallel/test-stream2-readable-empty-buffer-no-eof.js
index 701fe8f333..30f9b32ebf 100644
--- a/test/parallel/test-stream2-readable-empty-buffer-no-eof.js
+++ b/test/parallel/test-stream2-readable-empty-buffer-no-eof.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
diff --git a/test/parallel/test-stream2-readable-from-list.js b/test/parallel/test-stream2-readable-from-list.js
index 9bd942b902..331fd3212f 100644
--- a/test/parallel/test-stream2-readable-from-list.js
+++ b/test/parallel/test-stream2-readable-from-list.js
@@ -1,5 +1,6 @@
+'use strict';
var assert = require('assert');
-var common = require('../common.js');
+var common = require('../common');
var fromList = require('../../lib/_stream_readable')._fromList;
// tiny node-tap lookalike.
@@ -22,7 +23,7 @@ function run() {
fn({
same: assert.deepEqual,
equal: assert.equal,
- end: function () {
+ end: function() {
count--;
run();
}
@@ -30,7 +31,7 @@ function run() {
}
// ensure all tests have run
-process.on("exit", function () {
+process.on('exit', function() {
assert.equal(count, 0);
});
diff --git a/test/parallel/test-stream2-readable-legacy-drain.js b/test/parallel/test-stream2-readable-legacy-drain.js
index 183f9a100e..cbbde51963 100644
--- a/test/parallel/test-stream2-readable-legacy-drain.js
+++ b/test/parallel/test-stream2-readable-legacy-drain.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
diff --git a/test/parallel/test-stream2-readable-non-empty-end.js b/test/parallel/test-stream2-readable-non-empty-end.js
index 9bc02e0f71..5d3812b336 100644
--- a/test/parallel/test-stream2-readable-non-empty-end.js
+++ b/test/parallel/test-stream2-readable-non-empty-end.js
@@ -1,11 +1,12 @@
+'use strict';
var assert = require('assert');
-var common = require('../common.js');
+var common = require('../common');
var Readable = require('../../lib/_stream_readable');
var len = 0;
var chunks = new Array(10);
for (var i = 1; i <= 10; i++) {
- chunks[i-1] = new Buffer(i);
+ chunks[i - 1] = new Buffer(i);
len += i;
}
diff --git a/test/parallel/test-stream2-readable-wrap-empty.js b/test/parallel/test-stream2-readable-wrap-empty.js
index 1f084ca6ec..0bcd48c1d8 100644
--- a/test/parallel/test-stream2-readable-wrap-empty.js
+++ b/test/parallel/test-stream2-readable-wrap-empty.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
@@ -5,18 +6,18 @@ var Readable = require('../../lib/_stream_readable');
var EE = require('events').EventEmitter;
var oldStream = new EE();
-oldStream.pause = function(){};
-oldStream.resume = function(){};
+oldStream.pause = function() {};
+oldStream.resume = function() {};
var newStream = new Readable().wrap(oldStream);
var ended = false;
newStream
- .on('readable', function(){})
- .on('end', function(){ ended = true; });
+ .on('readable', function() {})
+ .on('end', function() { ended = true; });
oldStream.emit('end');
-process.on('exit', function(){
+process.on('exit', function() {
assert.ok(ended);
});
diff --git a/test/parallel/test-stream2-readable-wrap.js b/test/parallel/test-stream2-readable-wrap.js
index cd1d449e02..e466e75b44 100644
--- a/test/parallel/test-stream2-readable-wrap.js
+++ b/test/parallel/test-stream2-readable-wrap.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
@@ -9,8 +10,9 @@ var testRuns = 0, completedRuns = 0;
function runTest(highWaterMark, objectMode, produce) {
testRuns++;
- var old = new EE;
- var r = new Readable({ highWaterMark: highWaterMark, objectMode: objectMode });
+ var old = new EE();
+ var r = new Readable({ highWaterMark: highWaterMark,
+ objectMode: objectMode });
assert.equal(r, r.wrap(old));
var ended = false;
@@ -50,7 +52,8 @@ function runTest(highWaterMark, objectMode, produce) {
}
}
- var w = new Writable({ highWaterMark: highWaterMark * 2, objectMode: objectMode });
+ var w = new Writable({ highWaterMark: highWaterMark * 2,
+ objectMode: objectMode });
var written = [];
w._write = function(chunk, encoding, cb) {
console.log('_write', chunk);
@@ -67,19 +70,19 @@ function runTest(highWaterMark, objectMode, produce) {
flow();
- function performAsserts() {
+ function performAsserts() {
assert(ended);
assert(oldEnded);
assert.deepEqual(written, expected);
}
}
-runTest(100, false, function(){ return new Buffer(100); });
-runTest(10, false, function(){ return new Buffer('xxxxxxxxxx'); });
-runTest(1, true, function(){ return { foo: 'bar' }; });
+runTest(100, false, function() { return new Buffer(100); });
+runTest(10, false, function() { return new Buffer('xxxxxxxxxx'); });
+runTest(1, true, function() { return { foo: 'bar' }; });
var objectChunks = [ 5, 'a', false, 0, '', 'xyz', { x: 4 }, 7, [], 555 ];
-runTest(1, true, function(){ return objectChunks.shift() });
+runTest(1, true, function() { return objectChunks.shift(); });
process.on('exit', function() {
assert.equal(testRuns, completedRuns);
diff --git a/test/parallel/test-stream2-set-encoding.js b/test/parallel/test-stream2-set-encoding.js
index d7aca4337c..937a377ef5 100644
--- a/test/parallel/test-stream2-set-encoding.js
+++ b/test/parallel/test-stream2-set-encoding.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var assert = require('assert');
var R = require('../../lib/_stream_readable');
var util = require('util');
@@ -23,7 +24,7 @@ function run() {
fn({
same: assert.deepEqual,
equal: assert.equal,
- end: function () {
+ end: function() {
count--;
run();
}
@@ -31,7 +32,7 @@ function run() {
}
// ensure all tests have run
-process.on("exit", function () {
+process.on('exit', function() {
assert.equal(count, 0);
});
@@ -68,7 +69,7 @@ TestReader.prototype._read = function(n) {
var ret = new Buffer(n);
ret.fill('a');
- console.log("this.push(ret)", ret)
+ console.log('this.push(ret)', ret);
return this.push(ret);
}.bind(this), 1);
@@ -146,32 +147,32 @@ test('setEncoding hex with read(13)', function(t) {
tr.setEncoding('hex');
var out = [];
var expect =
- [ "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "16161" ];
+ [ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '16161' ];
tr.on('readable', function flow() {
- console.log("readable once")
+ console.log('readable once');
var chunk;
while (null !== (chunk = tr.read(13)))
out.push(chunk);
});
tr.on('end', function() {
- console.log("END")
+ console.log('END');
t.same(out, expect);
t.end();
});
@@ -278,22 +279,22 @@ test('encoding: hex with read(13)', function(t) {
var tr = new TestReader(100, { encoding: 'hex' });
var out = [];
var expect =
- [ "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "1616161616161",
- "6161616161616",
- "16161" ];
+ [ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '1616161616161',
+ '6161616161616',
+ '16161' ];
tr.on('readable', function flow() {
var chunk;
diff --git a/test/parallel/test-stream2-transform.js b/test/parallel/test-stream2-transform.js
index 219243ce56..20873b135d 100644
--- a/test/parallel/test-stream2-transform.js
+++ b/test/parallel/test-stream2-transform.js
@@ -1,5 +1,6 @@
+'use strict';
var assert = require('assert');
-var common = require('../common.js');
+var common = require('../common');
var PassThrough = require('../../lib/_stream_passthrough');
var Transform = require('../../lib/_stream_transform');
@@ -24,7 +25,7 @@ function run() {
same: assert.deepEqual,
equal: assert.equal,
ok: assert,
- end: function () {
+ end: function() {
count--;
run();
}
@@ -32,7 +33,7 @@ function run() {
}
// ensure all tests have run
-process.on("exit", function () {
+process.on('exit', function() {
assert.equal(count, 0);
});
@@ -83,7 +84,7 @@ test('passthrough', function(t) {
t.end();
});
-test('object passthrough', function (t) {
+test('object passthrough', function(t) {
var pt = new PassThrough({ objectMode: true });
pt.write(1);
@@ -106,7 +107,7 @@ test('object passthrough', function (t) {
});
test('simple transform', function(t) {
- var pt = new Transform;
+ var pt = new Transform();
pt._transform = function(c, e, cb) {
var ret = new Buffer(c.length);
ret.fill('x');
@@ -154,7 +155,7 @@ test('simple object transform', function(t) {
});
test('async passthrough', function(t) {
- var pt = new Transform;
+ var pt = new Transform();
pt._transform = function(chunk, encoding, cb) {
setTimeout(function() {
pt.push(chunk);
@@ -178,7 +179,7 @@ test('async passthrough', function(t) {
});
test('assymetric transform (expand)', function(t) {
- var pt = new Transform;
+ var pt = new Transform();
// emit each chunk 2 times.
pt._transform = function(chunk, encoding, cb) {
@@ -187,7 +188,7 @@ test('assymetric transform (expand)', function(t) {
setTimeout(function() {
pt.push(chunk);
cb();
- }, 10)
+ }, 10);
}, 10);
};
@@ -210,7 +211,7 @@ test('assymetric transform (expand)', function(t) {
});
test('assymetric transform (compress)', function(t) {
- var pt = new Transform;
+ var pt = new Transform();
// each output is the first char of 3 consecutive chunks,
// or whatever's left.
@@ -348,10 +349,10 @@ test('passthrough event emission', function(t) {
});
test('passthrough event emission reordered', function(t) {
- var pt = new PassThrough;
+ var pt = new PassThrough();
var emits = 0;
pt.on('readable', function() {
- console.error('emit readable', emits)
+ console.error('emit readable', emits);
emits++;
});
@@ -390,7 +391,7 @@ test('passthrough event emission reordered', function(t) {
test('passthrough facaded', function(t) {
console.error('passthrough facaded');
- var pt = new PassThrough;
+ var pt = new PassThrough();
var datas = [];
pt.on('data', function(chunk) {
datas.push(chunk.toString());
@@ -433,8 +434,8 @@ test('object transform (json parse)', function(t) {
var objects = [
{ foo: 'bar' },
100,
- "string",
- { nested: { things: [ { foo: 'bar' }, 100, "string" ] } }
+ 'string',
+ { nested: { things: [ { foo: 'bar' }, 100, 'string' ] } }
];
var ended = false;
@@ -455,7 +456,7 @@ test('object transform (json parse)', function(t) {
process.nextTick(function() {
t.ok(ended);
t.end();
- })
+ });
});
test('object transform (json stringify)', function(t) {
@@ -475,8 +476,8 @@ test('object transform (json stringify)', function(t) {
var objects = [
{ foo: 'bar' },
100,
- "string",
- { nested: { things: [ { foo: 'bar' }, 100, "string" ] } }
+ 'string',
+ { nested: { things: [ { foo: 'bar' }, 100, 'string' ] } }
];
var ended = false;
@@ -497,7 +498,7 @@ test('object transform (json stringify)', function(t) {
process.nextTick(function() {
t.ok(ended);
t.end();
- })
+ });
});
function forEach (xs, f) {
diff --git a/test/parallel/test-stream2-unpipe-drain.js b/test/parallel/test-stream2-unpipe-drain.js
index 6ed04ba8c9..6e10db6a79 100644
--- a/test/parallel/test-stream2-unpipe-drain.js
+++ b/test/parallel/test-stream2-unpipe-drain.js
@@ -1,16 +1,22 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var assert = require('assert');
var stream = require('../../');
+
+if (!common.hasCrypto) {
+ console.log('1..0 # Skipped: missing crypto');
+ process.exit();
+}
var crypto = require('crypto');
var util = require('util');
function TestWriter() {
- stream.Writable.call(this);
+ stream.Writable.call(this);
}
util.inherits(TestWriter, stream.Writable);
-TestWriter.prototype._write = function (buffer, encoding, callback) {
+TestWriter.prototype._write = function(buffer, encoding, callback) {
console.log('write called');
// super slow write stream (callback never called)
};
@@ -18,12 +24,12 @@ TestWriter.prototype._write = function (buffer, encoding, callback) {
var dest = new TestWriter();
function TestReader(id) {
- stream.Readable.call(this);
- this.reads = 0;
+ stream.Readable.call(this);
+ this.reads = 0;
}
util.inherits(TestReader, stream.Readable);
-TestReader.prototype._read = function (size) {
+TestReader.prototype._read = function(size) {
this.reads += 1;
this.push(crypto.randomBytes(size));
};
@@ -33,13 +39,13 @@ var src2 = new TestReader();
src1.pipe(dest);
-src1.once('readable', function () {
- process.nextTick(function () {
+src1.once('readable', function() {
+ process.nextTick(function() {
src2.pipe(dest);
- src2.once('readable', function () {
- process.nextTick(function () {
+ src2.once('readable', function() {
+ process.nextTick(function() {
src1.unpipe(dest);
});
@@ -48,7 +54,7 @@ src1.once('readable', function () {
});
-process.on('exit', function () {
+process.on('exit', function() {
assert.equal(src1.reads, 2);
assert.equal(src2.reads, 2);
});
diff --git a/test/parallel/test-stream2-unpipe-leak.js b/test/parallel/test-stream2-unpipe-leak.js
index c5ca17079b..4677b5743a 100644
--- a/test/parallel/test-stream2-unpipe-leak.js
+++ b/test/parallel/test-stream2-unpipe-leak.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var assert = require('assert');
var stream = require('../../');
diff --git a/test/parallel/test-stream2-writable.js b/test/parallel/test-stream2-writable.js
index 417365e840..2b92fbf2ce 100644
--- a/test/parallel/test-stream2-writable.js
+++ b/test/parallel/test-stream2-writable.js
@@ -1,4 +1,5 @@
-var common = require('../common.js');
+'use strict';
+var common = require('../common');
var W = require('../../lib/_stream_writable');
var D = require('../../lib/_stream_duplex');
var assert = require('assert');
@@ -46,7 +47,7 @@ function run() {
fn({
same: assert.deepEqual,
equal: assert.equal,
- end: function () {
+ end: function() {
count--;
run();
}
@@ -54,7 +55,7 @@ function run() {
}
// ensure all tests have run
-process.on("exit", function () {
+process.on('exit', function() {
assert.equal(count, 0);
});
@@ -196,7 +197,7 @@ test('write no bufferize', function(t) {
t.end();
});
-test('write callbacks', function (t) {
+test('write callbacks', function(t) {
var callbacks = chunks.map(function(chunk, i) {
return [i, function(er) {
callbacks._called[i] = chunk;
@@ -225,42 +226,42 @@ test('write callbacks', function (t) {
tw.end();
});
-test('end callback', function (t) {
+test('end callback', function(t) {
var tw = new TestWriter();
- tw.end(function () {
+ tw.end(function() {
t.end();
});
});
-test('end callback with chunk', function (t) {
+test('end callback with chunk', function(t) {
var tw = new TestWriter();
- tw.end(new Buffer('hello world'), function () {
+ tw.end(new Buffer('hello world'), function() {
t.end();
});
});
-test('end callback with chunk and encoding', function (t) {
+test('end callback with chunk and encoding', function(t) {
var tw = new TestWriter();
- tw.end('hello world', 'ascii', function () {
+ tw.end('hello world', 'ascii', function() {
t.end();
});
});
-test('end callback after .write() call', function (t) {
+test('end callback after .write() call', function(t) {
var tw = new TestWriter();
tw.write(new Buffer('hello world'));
- tw.end(function () {
+ tw.end(function() {
t.end();
});
});
-test('end callback called after write callback', function (t) {
+test('end callback called after write callback', function(t) {
var tw = new TestWriter();
var writeCalledback = false;
tw.write(new Buffer('hello world'), function() {
writeCalledback = true;
});
- tw.end(function () {
+ tw.end(function() {
t.equal(writeCalledback, true);
t.end();
});
diff --git a/test/parallel/test-stream3-pause-then-read.js b/test/parallel/test-stream3-pause-then-read.js
index 750465da82..f6598ca29c 100644
--- a/test/parallel/test-stream3-pause-then-read.js
+++ b/test/parallel/test-stream3-pause-then-read.js
@@ -1,3 +1,4 @@
+'use strict';
var common = require('../common');
var assert = require('assert');
diff --git a/zlib.js b/zlib.js
deleted file mode 100644
index a30ca2091e..0000000000
--- a/zlib.js
+++ /dev/null
@@ -1,452 +0,0 @@
-// Copyright Joyent, Inc. and other Node contributors.
-//
-// 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.
-
-var Transform = require('./lib/_stream_transform.js');
-
-var binding = process.binding('zlib');
-var util = require('util');
-var assert = require('assert').ok;
-
-// zlib doesn't provide these, so kludge them in following the same
-// const naming scheme zlib uses.
-binding.Z_MIN_WINDOWBITS = 8;
-binding.Z_MAX_WINDOWBITS = 15;
-binding.Z_DEFAULT_WINDOWBITS = 15;
-
-// fewer than 64 bytes per chunk is stupid.
-// technically it could work with as few as 8, but even 64 bytes
-// is absurdly low. Usually a MB or more is best.
-binding.Z_MIN_CHUNK = 64;
-binding.Z_MAX_CHUNK = Infinity;
-binding.Z_DEFAULT_CHUNK = (16 * 1024);
-
-binding.Z_MIN_MEMLEVEL = 1;
-binding.Z_MAX_MEMLEVEL = 9;
-binding.Z_DEFAULT_MEMLEVEL = 8;
-
-binding.Z_MIN_LEVEL = -1;
-binding.Z_MAX_LEVEL = 9;
-binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION;
-
-// expose all the zlib constants
-Object.keys(binding).forEach(function(k) {
- if (k.match(/^Z/)) exports[k] = binding[k];
-});
-
-// translation table for return codes.
-exports.codes = {
- Z_OK: binding.Z_OK,
- Z_STREAM_END: binding.Z_STREAM_END,
- Z_NEED_DICT: binding.Z_NEED_DICT,
- Z_ERRNO: binding.Z_ERRNO,
- Z_STREAM_ERROR: binding.Z_STREAM_ERROR,
- Z_DATA_ERROR: binding.Z_DATA_ERROR,
- Z_MEM_ERROR: binding.Z_MEM_ERROR,
- Z_BUF_ERROR: binding.Z_BUF_ERROR,
- Z_VERSION_ERROR: binding.Z_VERSION_ERROR
-};
-
-Object.keys(exports.codes).forEach(function(k) {
- exports.codes[exports.codes[k]] = k;
-});
-
-exports.Deflate = Deflate;
-exports.Inflate = Inflate;
-exports.Gzip = Gzip;
-exports.Gunzip = Gunzip;
-exports.DeflateRaw = DeflateRaw;
-exports.InflateRaw = InflateRaw;
-exports.Unzip = Unzip;
-
-exports.createDeflate = function(o) {
- return new Deflate(o);
-};
-
-exports.createInflate = function(o) {
- return new Inflate(o);
-};
-
-exports.createDeflateRaw = function(o) {
- return new DeflateRaw(o);
-};
-
-exports.createInflateRaw = function(o) {
- return new InflateRaw(o);
-};
-
-exports.createGzip = function(o) {
- return new Gzip(o);
-};
-
-exports.createGunzip = function(o) {
- return new Gunzip(o);
-};
-
-exports.createUnzip = function(o) {
- return new Unzip(o);
-};
-
-
-// Convenience methods.
-// compress/decompress a string or buffer in one step.
-exports.deflate = function(buffer, callback) {
- zlibBuffer(new Deflate(), buffer, callback);
-};
-
-exports.gzip = function(buffer, callback) {
- zlibBuffer(new Gzip(), buffer, callback);
-};
-
-exports.deflateRaw = function(buffer, callback) {
- zlibBuffer(new DeflateRaw(), buffer, callback);
-};
-
-exports.unzip = function(buffer, callback) {
- zlibBuffer(new Unzip(), buffer, callback);
-};
-
-exports.inflate = function(buffer, callback) {
- zlibBuffer(new Inflate(), buffer, callback);
-};
-
-exports.gunzip = function(buffer, callback) {
- zlibBuffer(new Gunzip(), buffer, callback);
-};
-
-exports.inflateRaw = function(buffer, callback) {
- zlibBuffer(new InflateRaw(), buffer, callback);
-};
-
-function zlibBuffer(engine, buffer, callback) {
- var buffers = [];
- var nread = 0;
-
- engine.on('error', onError);
- engine.on('end', onEnd);
-
- engine.end(buffer);
- flow();
-
- function flow() {
- var chunk;
- while (null !== (chunk = engine.read())) {
- buffers.push(chunk);
- nread += chunk.length;
- }
- engine.once('readable', flow);
- }
-
- function onError(err) {
- engine.removeListener('end', onEnd);
- engine.removeListener('readable', flow);
- callback(err);
- }
-
- function onEnd() {
- var buf = Buffer.concat(buffers, nread);
- buffers = [];
- callback(null, buf);
- }
-}
-
-
-// generic zlib
-// minimal 2-byte header
-function Deflate(opts) {
- if (!(this instanceof Deflate)) return new Deflate(opts);
- Zlib.call(this, opts, binding.DEFLATE);
-}
-
-function Inflate(opts) {
- if (!(this instanceof Inflate)) return new Inflate(opts);
- Zlib.call(this, opts, binding.INFLATE);
-}
-
-
-
-// gzip - bigger header, same deflate compression
-function Gzip(opts) {
- if (!(this instanceof Gzip)) return new Gzip(opts);
- Zlib.call(this, opts, binding.GZIP);
-}
-
-function Gunzip(opts) {
- if (!(this instanceof Gunzip)) return new Gunzip(opts);
- Zlib.call(this, opts, binding.GUNZIP);
-}
-
-
-
-// raw - no header
-function DeflateRaw(opts) {
- if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
- Zlib.call(this, opts, binding.DEFLATERAW);
-}
-
-function InflateRaw(opts) {
- if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
- Zlib.call(this, opts, binding.INFLATERAW);
-}
-
-
-// auto-detect header.
-function Unzip(opts) {
- if (!(this instanceof Unzip)) return new Unzip(opts);
- Zlib.call(this, opts, binding.UNZIP);
-}
-
-
-// the Zlib class they all inherit from
-// This thing manages the queue of requests, and returns
-// true or false if there is anything in the queue when
-// you call the .write() method.
-
-function Zlib(opts, mode) {
- this._opts = opts = opts || {};
- this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK;
-
- Transform.call(this, opts);
-
- // means a different thing there.
- this._readableState.chunkSize = null;
-
- if (opts.chunkSize) {
- if (opts.chunkSize < exports.Z_MIN_CHUNK ||
- opts.chunkSize > exports.Z_MAX_CHUNK) {
- throw new Error('Invalid chunk size: ' + opts.chunkSize);
- }
- }
-
- if (opts.windowBits) {
- if (opts.windowBits < exports.Z_MIN_WINDOWBITS ||
- opts.windowBits > exports.Z_MAX_WINDOWBITS) {
- throw new Error('Invalid windowBits: ' + opts.windowBits);
- }
- }
-
- if (opts.level) {
- if (opts.level < exports.Z_MIN_LEVEL ||
- opts.level > exports.Z_MAX_LEVEL) {
- throw new Error('Invalid compression level: ' + opts.level);
- }
- }
-
- if (opts.memLevel) {
- if (opts.memLevel < exports.Z_MIN_MEMLEVEL ||
- opts.memLevel > exports.Z_MAX_MEMLEVEL) {
- throw new Error('Invalid memLevel: ' + opts.memLevel);
- }
- }
-
- if (opts.strategy) {
- if (opts.strategy != exports.Z_FILTERED &&
- opts.strategy != exports.Z_HUFFMAN_ONLY &&
- opts.strategy != exports.Z_RLE &&
- opts.strategy != exports.Z_FIXED &&
- opts.strategy != exports.Z_DEFAULT_STRATEGY) {
- throw new Error('Invalid strategy: ' + opts.strategy);
- }
- }
-
- if (opts.dictionary) {
- if (!Buffer.isBuffer(opts.dictionary)) {
- throw new Error('Invalid dictionary: it should be a Buffer instance');
- }
- }
-
- this._binding = new binding.Zlib(mode);
-
- var self = this;
- this._hadError = false;
- this._binding.onerror = function(message, errno) {
- // there is no way to cleanly recover.
- // continuing only obscures problems.
- self._binding = null;
- self._hadError = true;
-
- var error = new Error(message);
- error.errno = errno;
- error.code = exports.codes[errno];
- self.emit('error', error);
- };
-
- this._binding.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
- opts.level || exports.Z_DEFAULT_COMPRESSION,
- opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
- opts.strategy || exports.Z_DEFAULT_STRATEGY,
- opts.dictionary);
-
- this._buffer = new Buffer(this._chunkSize);
- this._offset = 0;
- this._closed = false;
-
- this.once('end', this.close);
-}
-
-util.inherits(Zlib, Transform);
-
-Zlib.prototype.reset = function reset() {
- return this._binding.reset();
-};
-
-Zlib.prototype._flush = function(output, callback) {
- var rs = this._readableState;
- var self = this;
- this._transform(null, output, function(er) {
- if (er)
- return callback(er);
-
- // now a weird thing happens... it could be that you called flush
- // but everything had already actually been consumed, but it wasn't
- // enough to get over the Readable class's lowWaterMark.
- // In that case, we emit 'readable' now to make sure it's consumed.
- if (rs.length &&
- rs.length < rs.lowWaterMark &&
- !rs.ended &&
- rs.needReadable)
- self.emit('readable');
-
- callback();
- });
-};
-
-Zlib.prototype.flush = function(callback) {
- var ws = this._writableState;
- var ts = this._transformState;
-
- if (ws.writing) {
- ws.needDrain = true;
- var self = this;
- this.once('drain', function() {
- self._flush(ts.output, callback);
- });
- return;
- }
-
- this._flush(ts.output, callback || function() {});
-};
-
-Zlib.prototype.close = function(callback) {
- if (callback)
- process.nextTick(callback);
-
- if (this._closed)
- return;
-
- this._closed = true;
-
- this._binding.close();
-
- var self = this;
- process.nextTick(function() {
- self.emit('close');
- });
-};
-
-Zlib.prototype._transform = function(chunk, output, cb) {
- var flushFlag;
- var ws = this._writableState;
- var ending = ws.ending || ws.ended;
- var last = ending && (!chunk || ws.length === chunk.length);
-
- if (chunk !== null && !Buffer.isBuffer(chunk))
- return cb(new Error('invalid input'));
-
- // If it's the last chunk, or a final flush, we use the Z_FINISH flush flag.
- // If it's explicitly flushing at some other time, then we use
- // Z_FULL_FLUSH. Otherwise, use Z_NO_FLUSH for maximum compression
- // goodness.
- if (last)
- flushFlag = binding.Z_FINISH;
- else if (chunk === null)
- flushFlag = binding.Z_FULL_FLUSH;
- else
- flushFlag = binding.Z_NO_FLUSH;
-
- var availInBefore = chunk && chunk.length;
- var availOutBefore = this._chunkSize - this._offset;
- var inOff = 0;
-
- var req = this._binding.write(flushFlag,
- chunk, // in
- inOff, // in_off
- availInBefore, // in_len
- this._buffer, // out
- this._offset, //out_off
- availOutBefore); // out_len
-
- req.buffer = chunk;
- req.callback = callback;
-
- var self = this;
- function callback(availInAfter, availOutAfter, buffer) {
- if (self._hadError)
- return;
-
- var have = availOutBefore - availOutAfter;
- assert(have >= 0, 'have should not go down');
-
- if (have > 0) {
- var out = self._buffer.slice(self._offset, self._offset + have);
- self._offset += have;
- // serve some output to the consumer.
- output(out);
- }
-
- // exhausted the output buffer, or used all the input create a new one.
- if (availOutAfter === 0 || self._offset >= self._chunkSize) {
- availOutBefore = self._chunkSize;
- self._offset = 0;
- self._buffer = new Buffer(self._chunkSize);
- }
-
- if (availOutAfter === 0) {
- // Not actually done. Need to reprocess.
- // Also, update the availInBefore to the availInAfter value,
- // so that if we have to hit it a third (fourth, etc.) time,
- // it'll have the correct byte counts.
- inOff += (availInBefore - availInAfter);
- availInBefore = availInAfter;
-
- var newReq = self._binding.write(flushFlag,
- chunk,
- inOff,
- availInBefore,
- self._buffer,
- self._offset,
- self._chunkSize);
- newReq.callback = callback; // this same function
- newReq.buffer = chunk;
- return;
- }
-
- // finished with the chunk.
- cb();
- }
-};
-
-util.inherits(Deflate, Zlib);
-util.inherits(Inflate, Zlib);
-util.inherits(Gzip, Zlib);
-util.inherits(Gunzip, Zlib);
-util.inherits(DeflateRaw, Zlib);
-util.inherits(InflateRaw, Zlib);
-util.inherits(Unzip, Zlib);