Comparing version 1.0.1 to 2.0.0
123
index.js
@@ -7,2 +7,4 @@ var os = require('os') | ||
var fdSlicer = require('fd-slicer') | ||
var assign = require('lodash.assign') | ||
var DEFAULT_BLOCK_SIZE = 204800 // 200kb | ||
@@ -29,9 +31,43 @@ /** | ||
try { | ||
fs.accessSync(path, fs.F_OK) | ||
return fs.statSync(path).isFile() | ||
} catch (e) { | ||
return false | ||
} | ||
return true | ||
} | ||
var DEFAULT_OPTIONS = { | ||
blockSize: DEFAULT_BLOCK_SIZE, | ||
destPath: os.tmpdir(), | ||
filename: Date.now() + '' | ||
} | ||
function FsSlice (filename, opts) { | ||
if (!filename) { | ||
throw new Error('require filename') | ||
} | ||
if (typeof filename !== 'string' && !Buffer.isBuffer(filename)) { | ||
throw new Error('filename must be a string or Buffer') | ||
} | ||
if (!opts) opts = {} | ||
this.OPTIONS = assign(DEFAULT_OPTIONS, opts) | ||
if (typeof filename === 'string') { | ||
try { | ||
var stat = fs.statSync(filename) | ||
if (!stat.isFile()) throw new Error(`no such file, stat '${filename}'`) | ||
this.slicer = fdSlicer.createFromFd(fs.openSync(filename, 'r')) | ||
this.filename = filename | ||
this.filesize = stat.size | ||
} catch (e) { | ||
throw e | ||
} | ||
} else { | ||
this.filename = this.OPTIONS.filename | ||
this.filesize = filename.length | ||
this.slicer = fdSlicer.createFromBuffer(filename) | ||
} | ||
} | ||
/** | ||
@@ -44,5 +80,5 @@ * Gets the block's size | ||
*/ | ||
function getBlockInterval (index, blockSize) { | ||
FsSlice.prototype._getBlockInterval = function (index, blockSize) { | ||
index = index <= 0 ? 1 : index | ||
blockSize = blockSize || this.options.blockSize | ||
blockSize = blockSize || this.OPTIONS.blockSize | ||
@@ -57,42 +93,10 @@ var start = blockSize * (index - 1) | ||
* The size of the block, calculate the number of blocks | ||
* @param filename | ||
* @param blockSize Size of the block | ||
* @returns {number} | ||
*/ | ||
function getBlockNum (filename, blockSize) { | ||
blockSize = blockSize || this.options.blockSize | ||
return Math.ceil(fs.statSync(filename).size / blockSize) | ||
FsSlice.prototype._getBlockNum = function () { | ||
return Math.ceil(this.filesize / this.OPTIONS.blockSize) | ||
} | ||
/** | ||
* Expose `FsSlice` | ||
*/ | ||
module.exports = function (filename, opts) { | ||
if (!filename) { | ||
throw new Error('require filename') | ||
} | ||
if ((typeof filename) !== 'string') { | ||
throw new Error('filename must be a string') | ||
} | ||
if (!opts) opts = {} | ||
if (opts.blockSize === undefined) opts.blockSize = 204800 // 200kb | ||
if (opts.destPath === undefined) opts.destPath = os.tmpdir() | ||
if (opts.fd === undefined) opts.fd = fs.openSync(filename, 'r') | ||
return new FsSlice(filename, opts) | ||
} | ||
function FsSlice (filename, options) { | ||
this.options = options | ||
this.filename = filename | ||
this.fd = options.fd | ||
} | ||
// Format File Name | ||
FsSlice.formatFilename = function (filename, index) { | ||
filename = filename.split('/').pop() | ||
FsSlice.prototype.formatFilename = function (index) { | ||
var filename = this.filename.split('/').pop() | ||
index = index === undefined ? 1 : index | ||
@@ -104,15 +108,9 @@ | ||
FsSlice.prototype.slice = function (opts) { | ||
opts = (typeof opts) === 'object' ? opts : {} | ||
if (!opts) opts = {} | ||
opts = assign({ | ||
start: 0, | ||
end: this._getBlockInterval(1).end | ||
}, opts) | ||
if (opts.start === undefined) { | ||
opts.start = 0 | ||
} | ||
if (opts.end === undefined) { | ||
opts.end = getBlockInterval.call(this, 1).end | ||
} | ||
var slicer = fdSlicer.createFromFd(this.fd) | ||
return slicer.createReadStream(opts) | ||
return this.slicer.createReadStream(opts) | ||
} | ||
@@ -149,7 +147,5 @@ | ||
var self = this | ||
var blockSize = opts.blockSize || this.options.blockSize | ||
var blockNum = getBlockNum.call(this, this.filename, blockSize) | ||
var destPath = opts.destPath || this.options.destPath | ||
var blockNum = this._getBlockNum() | ||
var newFilePath = [] | ||
var index = opts.index || 1 | ||
var newFilePath = [] | ||
@@ -160,4 +156,4 @@ return new Promise(function (resolve, reject) { | ||
}, function (callback) { | ||
var newFilename = path.join(destPath, FsSlice.formatFilename(self.filename, index)) | ||
var blockInterval = getBlockInterval.call(self, index, blockSize) | ||
var newFilename = path.join(self.OPTIONS.destPath, self.formatFilename(index)) | ||
var blockInterval = self._getBlockInterval(index, self.OPTIONS.blockSize) | ||
@@ -187,2 +183,6 @@ self | ||
if (!filenameArray.length) { | ||
throw new Error('filenameArray is empty') | ||
} | ||
for (var i in filenameArray) { | ||
@@ -229,1 +229,8 @@ if (!fsExistsSync(filenameArray[i])) { | ||
} | ||
/** | ||
* Expose `FsSlice` | ||
*/ | ||
module.exports = function (filename, opts) { | ||
return new FsSlice(filename, opts) | ||
} |
{ | ||
"name": "fs-slice", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "slice file or join files or avg slice file.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/ava -v ./test/test.js" | ||
"test": "./node_modules/.bin/ava -v ./test/*.js" | ||
}, | ||
@@ -13,2 +13,5 @@ "repository": { | ||
}, | ||
"engines": { | ||
"node": ">=4.0.0" | ||
}, | ||
"keywords": [ | ||
@@ -35,7 +38,8 @@ "slice", | ||
"async": "^2.4.0", | ||
"fd-slicer": "^1.0.1" | ||
"fd-slicer": "^1.0.1", | ||
"lodash.assign": "^4.2.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.19.1" | ||
"ava": "^0.25.0" | ||
} | ||
} |
205
README.md
@@ -9,3 +9,99 @@ # node-fs-slice | ||
[![Build Status](https://api.travis-ci.org/SunilWang/node-fs-slice.svg?branch=master)](https://travis-ci.org/SunilWang/node-fs-slice) | ||
[![NPM Version][npm-image]][npm-url] | ||
# install | ||
With [npm](https://www.npmjs.com/package/fs-slice) do: | ||
``` | ||
npm install fs-slice --save | ||
``` | ||
## Features | ||
- ✔︎ Promise、async、await | ||
# methods | ||
``` js | ||
var fss = require('fs-slice'); | ||
``` | ||
## var fssObj = fss(targetFile, opts={}) | ||
Create a fs-slice instance `fssObj` from a `filename` and some options `opts`. | ||
* `targetFile` - String | Buffer. target file. | ||
* `opts.blockSize` - Number. set how much data to read in each chunk. Default 204800. | ||
* `opts.destPath` - String. make sure that it is a folder path. Default `os.tmpdir();`. | ||
## var stream = fssObj.slice(opts={}) | ||
Return a readable stream that emits | ||
Available `opts`: | ||
* `start` - Number. The offset into the file to start reading from. Defaults 0. | ||
* `end` - Number. Exclusive upper bound offset into the file to stop reading | ||
from. Defaults 204800. | ||
`opts` is an object or string with the following defaults: | ||
``` | ||
{ | ||
flags: 'r', | ||
encoding: null, | ||
fd: fssObj.fd, | ||
mode: 0o666, | ||
autoClose: true | ||
} | ||
``` | ||
to see [fs.createReadStream](https://nodejs.org/dist/latest-v6.x/docs/api/fs.html#fs_fs_createreadstream_path_options) | ||
## fssObj.sliceAsFile(filepath, rOptions={}, wOptions={}).then(function(){}); | ||
* `filepath` - String. Make sure `filepath` is a file path. | ||
* `rOptions` - Object. read stream options. `wOptions` - Object. write stream options. | ||
`rOptions wOptions` is an object or string with the following defaults: | ||
``` | ||
rOptions: | ||
{ | ||
start: 0, | ||
end: 204800, | ||
flags: 'r', | ||
encoding: null, | ||
fd: fssObj.fd, | ||
mode: 0o666, | ||
autoClose: true | ||
} | ||
wOptions: | ||
{ | ||
flags: 'w', | ||
defaultEncoding: 'utf8', | ||
fd: fs.openSync(filepath, 'w'), | ||
mode: 0o666, | ||
autoClose: true | ||
} | ||
``` | ||
to see [fs.createReadStream](https://nodejs.org/dist/latest-v6.x/docs/api/fs.html#fs_fs_createreadstream_path_options) [fs.createWriteStream](https://nodejs.org/dist/latest-v6.x/docs/api/fs.html#fs_fs_createwritestream_path_options) | ||
## fssObj.avgSliceAsFile(opts={}).then(function(files){}); | ||
Available `opts`: | ||
* `index` - Number. block start index. Default 1. | ||
## fssObj.join(filenameArray, writable).then(function(){}); | ||
* `filenameArray` - Array[String]. multiple file fragments path, make sure `filenameArray` is a Array. | ||
* `writable` - WriteStream. make sure `writable` is a write stream. | ||
## fssObj.joinAsFile(filenameArray, filepath).then(function(){}); | ||
* `filenameArray` - Array[String]. multiple file fragments path. | ||
* `filepath` - WriteStream. Make sure `filepath` is a file path. | ||
# example | ||
@@ -107,3 +203,3 @@ | ||
//blockSize default : 204800 byte (200kb) | ||
.avgSliceAsFile({blockSize: 100000}) | ||
.avgSliceAsFile() | ||
.then(function (files) { | ||
@@ -130,12 +226,6 @@ console.info('##############', IMAGE_FILENAME, 'size: ', fs.statSync(IMAGE_FILENAME).size); | ||
'/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/f47c7e739b6aa75df173751c9788d746_3_image.jpg', | ||
'/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/b0e54e4076f0112c8e1e6493c481c13e_4_image.jpg', | ||
'/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/a3e4360fbd8c70f3173e41bf342b329f_5_image.jpg', | ||
'/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/81463aa7e4f0eeb6448619769bdb2bfa_6_image.jpg' ] | ||
############## files size: | ||
/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/a7ed4d23c8d3ddf3761db74823196a54_1_image.jpg size: 100000 | ||
/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/260d0a57d94d31aea6e3892b239e1a5a_2_image.jpg size: 100000 | ||
/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/f47c7e739b6aa75df173751c9788d746_3_image.jpg size: 100000 | ||
/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/b0e54e4076f0112c8e1e6493c481c13e_4_image.jpg size: 100000 | ||
/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/a3e4360fbd8c70f3173e41bf342b329f_5_image.jpg size: 100000 | ||
/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/81463aa7e4f0eeb6448619769bdb2bfa_6_image.jpg size: 16943 | ||
/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/a7ed4d23c8d3ddf3761db74823196a54_1_image.jpg size: 204800 | ||
/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/260d0a57d94d31aea6e3892b239e1a5a_2_image.jpg size: 204800 | ||
/var/folders/02/g9c5v3jn3fv7q1zbnxhf7n280000gn/T/f47c7e739b6aa75df173751c9788d746_3_image.jpg size: 107343 | ||
@@ -246,95 +336,2 @@ ``` | ||
# methods | ||
``` js | ||
var fss = require('fs-slice'); | ||
``` | ||
## var fssObj = fss(filename, opts={}) | ||
Create a fs-slice instance `fssObj` from a `filename` and some options `opts`. | ||
* `opts.blockSize` - Number. set how much data to read in each chunk. Default 204800. | ||
* `opts.destPath` - String. make sure that it is a folder path. Default `os.tmpdir();`. | ||
* `opts.fd` - make sure `fd` is a properly initialized file descriptor. | ||
## var stream = fssObj.slice(opts={}) | ||
Return a readable stream that emits | ||
Available `opts`: | ||
* `start` - Number. The offset into the file to start reading from. Defaults 0. | ||
* `end` - Number. Exclusive upper bound offset into the file to stop reading | ||
from. Defaults 204800. | ||
`opts` is an object or string with the following defaults: | ||
``` | ||
{ | ||
flags: 'r', | ||
encoding: null, | ||
fd: fssObj.fd, | ||
mode: 0o666, | ||
autoClose: true | ||
} | ||
``` | ||
to see [fs.createReadStream](https://nodejs.org/dist/latest-v6.x/docs/api/fs.html#fs_fs_createreadstream_path_options) | ||
## fssObj.sliceAsFile(filepath, rOptions={}, wOptions={}).then(function(){}); | ||
* `filepath` - String. Make sure `filepath` is a file path. | ||
* `rOptions` - Object. read stream options. `wOptions` - Object. write stream options. | ||
`rOptions wOptions` is an object or string with the following defaults: | ||
``` | ||
rOptions: | ||
{ | ||
start: 0, | ||
end: 204800, | ||
flags: 'r', | ||
encoding: null, | ||
fd: fssObj.fd, | ||
mode: 0o666, | ||
autoClose: true | ||
} | ||
wOptions: | ||
{ | ||
flags: 'w', | ||
defaultEncoding: 'utf8', | ||
fd: fs.openSync(filepath, 'w'), | ||
mode: 0o666, | ||
autoClose: true | ||
} | ||
``` | ||
to see [fs.createReadStream](https://nodejs.org/dist/latest-v6.x/docs/api/fs.html#fs_fs_createreadstream_path_options) [fs.createWriteStream](https://nodejs.org/dist/latest-v6.x/docs/api/fs.html#fs_fs_createwritestream_path_options) | ||
## fssObj.avgSliceAsFile(opts={}).then(function(files){}); | ||
Available `opts`: | ||
* `blockSize` - Number. set how much data to read in each chunk. Default 204800. | ||
* `destPath` - String. make sure that it is a folder path. Default `os.tmpdir();`. | ||
## fssObj.join(filenameArray, writable).then(function(){}); | ||
* `filenameArray` - Array[String]. multiple file fragments path, make sure `filenameArray` is a Array. | ||
* `writable` - WriteStream. make sure `writable` is a write stream. | ||
## fssObj.joinAsFile(filenameArray, filepath).then(function(){}); | ||
* `filenameArray` - Array[String]. multiple file fragments path. | ||
* `filepath` - WriteStream. Make sure `filepath` is a file path. | ||
# install | ||
With [npm](https://www.npmjs.com/package/fs-slice) do: | ||
``` | ||
npm install fs-slice --save | ||
``` | ||
# license | ||
@@ -344,1 +341,3 @@ | ||
[npm-image]: https://img.shields.io/npm/v/fs-slice.svg | ||
[npm-url]: https://www.npmjs.com/package/fs-slice |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1479019
12
382
3
339
3
1
+ Addedlodash.assign@^4.2.0
+ Addedlodash.assign@4.2.0(transitive)