diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/stream-slice | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/stream-slice')
| -rw-r--r-- | node_modules/stream-slice/.npmignore | 3 | ||||
| -rw-r--r-- | node_modules/stream-slice/.travis.yml | 4 | ||||
| -rw-r--r-- | node_modules/stream-slice/README.md | 35 | ||||
| -rw-r--r-- | node_modules/stream-slice/index.js | 53 | ||||
| -rw-r--r-- | node_modules/stream-slice/package.json | 26 | ||||
| -rw-r--r-- | node_modules/stream-slice/test.js | 46 |
6 files changed, 167 insertions, 0 deletions
diff --git a/node_modules/stream-slice/.npmignore b/node_modules/stream-slice/.npmignore new file mode 100644 index 00000000..1e52a0dd --- /dev/null +++ b/node_modules/stream-slice/.npmignore @@ -0,0 +1,3 @@ + +node_modules +*.DS_Store
\ No newline at end of file diff --git a/node_modules/stream-slice/.travis.yml b/node_modules/stream-slice/.travis.yml new file mode 100644 index 00000000..1416d607 --- /dev/null +++ b/node_modules/stream-slice/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - "0.10" + - "0.11"
\ No newline at end of file diff --git a/node_modules/stream-slice/README.md b/node_modules/stream-slice/README.md new file mode 100644 index 00000000..8c9e692a --- /dev/null +++ b/node_modules/stream-slice/README.md @@ -0,0 +1,35 @@ + +stream-slice [](https://travis-ci.org/yorkie/koa-range) +========================== +slicing stream like buffer/string + +### Installation + +```sh +$ npm install stream-slice --save +``` + +### Usage + +```js +var fs = require('fs'); +var slice = require('stream-slice').slice; +fs.createReadStream('your path') + .pipe(slice(10, 100)) + .on('data', function() { + // here we only get the buffer from 10th to 100th. + }); +``` + +slicing file +```js +var fs = require('fs'); +var slice = require('stream-slice').slice; +fs.createReadStream('sourc file path') + .pipe(slice(0, 200)) + .pipe(fs.createWriteStream('dest file path')); +``` + +### License + +MIT
\ No newline at end of file diff --git a/node_modules/stream-slice/index.js b/node_modules/stream-slice/index.js new file mode 100644 index 00000000..44c4ae02 --- /dev/null +++ b/node_modules/stream-slice/index.js @@ -0,0 +1,53 @@ + +var util = require('util'); +var Transform = require('stream').Transform; + +util.inherits(SliceStream, Transform); +function SliceStream(start, end) { + + if (!(this instanceof SliceStream)) { + return new SliceStream(); + } + + Transform.call(this); + this._start = start || 0; + this._end = end || Infinity; + this._offset = 0; + this._state = 0; + + this._emitUp = false; + this._emitDown = false; +} + +SliceStream.prototype._transform = function(chunk, encoding, done) { + this._offset += chunk.length; + if (!this._emitUp && this._offset >= this._start) { + this._emitUp = true; + var start = chunk.length - (this._offset - this._start); + if(this._offset > this._end) + { + var end = chunk.length - (this._offset - this._end); + this._emitDown = true; + this.push(chunk.slice(start, end)); + } + else + { + this.push(chunk.slice(start, chunk.length)); + } + return done(); + } + if (this._emitUp && !this._emitDown) { + if (this._offset >= this._end) { + this._emitDown = true; + this.push(chunk.slice(0, chunk.length - (this._offset - this._end))); + } else { + this.push(chunk); + } + return done(); + } + return done(); +} + +exports.slice = function(start, end) { + return new SliceStream(start, end); +} diff --git a/node_modules/stream-slice/package.json b/node_modules/stream-slice/package.json new file mode 100644 index 00000000..5917eca8 --- /dev/null +++ b/node_modules/stream-slice/package.json @@ -0,0 +1,26 @@ +{ + "name": "stream-slice", + "version": "0.1.2", + "description": "slice stream like buffer/string", + "main": "index.js", + "scripts": { + "test": "tape test.js" + }, + "repository": { + "type": "git", + "url": "git@github.com:yorkie/stream-slice.git" + }, + "keywords": [ + "slice", + "stream" + ], + "author": "Yorkie Neil <yorkiefixer@gmail.com>", + "license": "MIT", + "bugs": { + "url": "https://github.com/yorkie/stream-slice/issues" + }, + "homepage": "https://github.com/yorkie/stream-slice", + "devDependencies": { + "tape": "^2.13.2" + } +} diff --git a/node_modules/stream-slice/test.js b/node_modules/stream-slice/test.js new file mode 100644 index 00000000..e857d2d6 --- /dev/null +++ b/node_modules/stream-slice/test.js @@ -0,0 +1,46 @@ + +var test = require('tape'); +var fs = require('fs'); +var slice = require('./index').slice; +var bufferReady = fs.readFileSync('./README.md'); + +test('normal test', function(t) { + var chunks = []; + fs.createReadStream('./README.md') + .pipe(slice(0, 10)) + .on('data', chunks.push.bind(chunks)) + .on('end', function() { + var res = Buffer.concat(chunks); + t.equal(res.length, 10); + t.equal(bufferEq(res, bufferReady.slice(0, 10)), true); + t.end(); + }); +}); + +test('normal test', function(t) { + var chunks = []; + fs.createReadStream('./README.md') + .pipe(slice(10, 20)) + .on('data', chunks.push.bind(chunks)) + .on('end', function() { + var res = Buffer.concat(chunks); + t.equal(res.length, 10); + t.equal(bufferEq(res, bufferReady.slice(10, 20)), true); + t.end(); + }); +}); + +function bufferEq(foo, bar) { + if (!Buffer.isBuffer(foo) || !Buffer.isBuffer(bar)) { + throw new TypeError('Arguments must be a buffer'); + } + if (foo.length !== bar.length) { + return false; + } + for (var i = 0; i < foo.length; i++) { + if (foo[i] !== bar[i]) { + return false; + } + } + return true; +}
\ No newline at end of file |
