package.json
9.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
"_args": [
[
{
"raw": "fd-slicer@~1.0.1",
"scope": null,
"escapedName": "fd-slicer",
"name": "fd-slicer",
"rawSpec": "~1.0.1",
"spec": ">=1.0.1 <1.1.0",
"type": "range"
},
"/Users/fzy/project/koa2_Sequelize_project/node_modules/multiparty"
]
],
"_from": "fd-slicer@>=1.0.1 <1.1.0",
"_id": "fd-slicer@1.0.1",
"_inCache": true,
"_location": "/fd-slicer",
"_npmUser": {
"name": "superjoe",
"email": "superjoe30@gmail.com"
},
"_npmVersion": "1.4.21",
"_phantomChildren": {},
"_requested": {
"raw": "fd-slicer@~1.0.1",
"scope": null,
"escapedName": "fd-slicer",
"name": "fd-slicer",
"rawSpec": "~1.0.1",
"spec": ">=1.0.1 <1.1.0",
"type": "range"
},
"_requiredBy": [
"/multiparty"
],
"_resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.0.1.tgz",
"_shasum": "8b5bcbd9ec327c5041bf9ab023fd6750f1177e65",
"_shrinkwrap": null,
"_spec": "fd-slicer@~1.0.1",
"_where": "/Users/fzy/project/koa2_Sequelize_project/node_modules/multiparty",
"author": {
"name": "Andrew Kelley",
"email": "superjoe30@gmail.com"
},
"bugs": {
"url": "https://github.com/andrewrk/node-fd-slicer/issues"
},
"dependencies": {
"pend": "~1.2.0"
},
"description": "safely create multiple ReadStream or WriteStream objects from the same file descriptor",
"devDependencies": {
"istanbul": "~0.3.3",
"mocha": "~2.0.1",
"stream-equal": "~0.1.5",
"streamsink": "~1.2.0"
},
"directories": {
"test": "test"
},
"dist": {
"shasum": "8b5bcbd9ec327c5041bf9ab023fd6750f1177e65",
"tarball": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.0.1.tgz"
},
"gitHead": "b7a28cb5bda986748ad756c39961f46c2fd28ec6",
"homepage": "https://github.com/andrewrk/node-fd-slicer#readme",
"keywords": [
"createReadStream",
"createWriteStream"
],
"license": "MIT",
"main": "index.js",
"maintainers": [
{
"name": "superjoe",
"email": "superjoe30@gmail.com"
},
{
"name": "thejoshwolfe",
"email": "thejoshwolfe@gmail.com"
}
],
"name": "fd-slicer",
"optionalDependencies": {},
"readme": "# fd-slicer\n\n[](https://travis-ci.org/andrewrk/node-fd-slicer)\n[](https://coveralls.io/r/andrewrk/node-fd-slicer)\n\nSafe `fs.ReadStream` and `fs.WriteStream` using the same fd.\n\nLet's say that you want to perform a parallel upload of a file to a remote\nserver. To do this, we want to create multiple read streams. The first thing\nyou might think of is to use the `{start: 0, end: 0}` API of\n`fs.createReadStream`. This gives you two choices:\n\n 0. Use the same file descriptor for all `fs.ReadStream` objects.\n 0. Open the file multiple times, resulting in a separate file descriptor\n for each read stream.\n\nNeither of these are acceptable options. The first one is a severe bug,\nbecause the API docs for `fs.write` state:\n\n> Note that it is unsafe to use `fs.write` multiple times on the same file\n> without waiting for the callback. For this scenario, `fs.createWriteStream`\n> is strongly recommended.\n\n`fs.createWriteStream` will solve the problem if you only create one of them\nfor the file descriptor, but it will exhibit this unsafety if you create\nmultiple write streams per file descriptor.\n\nThe second option suffers from a race condition. For each additional time the\nfile is opened after the first, it is possible that the file is modified. So\nin our parallel uploading example, we might upload a corrupt file that never\nexisted on the client's computer.\n\nThis module solves this problem by providing `createReadStream` and\n`createWriteStream` that operate on a shared file descriptor and provides\nthe convenient stream API while still allowing slicing and dicing.\n\nThis module also gives you some additional power that the builtin\n`fs.createWriteStream` do not give you. These features are:\n\n * Emitting a 'progress' event on write.\n * Ability to set a maximum size and emit an error if this size is exceeded.\n * Ability to create an `FdSlicer` instance from a `Buffer`. This enables you\n to provide API for handling files as well as buffers using the same API.\n\n## Usage\n\n```js\nvar fdSlicer = require('fd-slicer');\nvar fs = require('fs');\n\nfs.open(\"file.txt\", 'r', function(err, fd) {\n if (err) throw err;\n var slicer = fdSlicer.createFromFd(fd);\n var firstPart = slicer.createReadStream({start: 0, end: 100});\n var secondPart = slicer.createReadStream({start: 100});\n var firstOut = fs.createWriteStream(\"first.txt\");\n var secondOut = fs.createWriteStream(\"second.txt\");\n firstPart.pipe(firstOut);\n secondPart.pipe(secondOut);\n});\n```\n\nYou can also create from a buffer:\n\n```js\nvar fdSlicer = require('fd-slicer');\nvar slicer = FdSlicer.createFromBuffer(someBuffer);\nvar firstPart = slicer.createReadStream({start: 0, end: 100});\nvar secondPart = slicer.createReadStream({start: 100});\nvar firstOut = fs.createWriteStream(\"first.txt\");\nvar secondOut = fs.createWriteStream(\"second.txt\");\nfirstPart.pipe(firstOut);\nsecondPart.pipe(secondOut);\n```\n\n## API Documentation\n\n### fdSlicer.createFromFd(fd, [options])\n\n```js\nvar fdSlicer = require('fd-slicer');\nfs.open(\"file.txt\", 'r', function(err, fd) {\n if (err) throw err;\n var slicer = fdSlicer.createFromFd(fd);\n // ...\n});\n```\n\nMake sure `fd` is a properly initialized file descriptor. If you want to\nuse `createReadStream` make sure you open it for reading and if you want\nto use `createWriteStream` make sure you open it for writing.\n\n`options` is an optional object which can contain:\n\n * `autoClose` - if set to `true`, the file descriptor will be automatically\n closed once the last stream that references it is closed. Defaults to\n `false`. `ref()` and `unref()` can be used to increase or decrease the\n reference count, respectively.\n\n### fdSlicer.createFromBuffer(buffer)\n\n```js\nvar fdSlicer = require('fd-slicer');\nvar slicer = fdSlicer.createFromBuffer(someBuffer);\n// ...\n```\n\n#### Properties\n\n##### fd\n\nThe file descriptor passed in. `undefined` if created from a buffer.\n\n#### Methods\n\n##### createReadStream(options)\n\nAvailable `options`:\n\n * `start` - Number. The offset into the file to start reading from. Defaults\n to 0.\n * `end` - Number. Exclusive upper bound offset into the file to stop reading\n from.\n * `highWaterMark` - Number. The maximum number of bytes to store in the\n internal buffer before ceasing to read from the underlying resource.\n Defaults to 16 KB.\n * `encoding` - String. If specified, then buffers will be decoded to strings\n using the specified encoding. Defaults to `null`.\n\nThe ReadableStream that this returns has these additional methods:\n\n * `destroy(err)` - stop streaming. `err` is optional and is the error that\n will be emitted in order to cause the streaming to stop. Defaults to\n `new Error(\"stream destroyed\")`.\n\n##### createWriteStream(options)\n\nAvailable `options`:\n\n * `start` - Number. The offset into the file to start writing to. Defaults to\n 0.\n * `end` - Number. Exclusive upper bound offset into the file. If this offset\n is reached, the write stream will emit an 'error' event and stop functioning.\n In this situation, `err.code === 'ETOOBIG'`. Defaults to `Infinity`.\n * `highWaterMark` - Number. Buffer level when `write()` starts returning\n false. Defaults to 16KB.\n * `decodeStrings` - Boolean. Whether or not to decode strings into Buffers\n before passing them to` _write()`. Defaults to `true`.\n\nThe WritableStream that this returns has these additional methods:\n\n * `destroy()` - stop streaming\n\nAnd these additional properties:\n\n * `bytesWritten` - number of bytes written to the stream\n\nAnd these additional events:\n\n * 'progress' - emitted when `bytesWritten` changes.\n\n##### read(buffer, offset, length, position, callback)\n\nEquivalent to `fs.read`, but with concurrency protection.\n`callback` must be defined.\n\n##### write(buffer, offset, length, position, callback)\n\nEquivalent to `fs.write`, but with concurrency protection.\n`callback` must be defined.\n\n##### ref()\n\nIncrease the `autoClose` reference count by 1.\n\n##### unref()\n\nDecrease the `autoClose` reference count by 1.\n\n#### Events\n\n##### 'error'\n\nEmitted if `fs.close` returns an error when auto closing.\n\n##### 'close'\n\nEmitted when fd-slicer closes the file descriptor due to `autoClose`. Never\nemitted if created from a buffer.\n",
"readmeFilename": "README.md",
"repository": {
"type": "git",
"url": "git://github.com/andrewrk/node-fd-slicer.git"
},
"scripts": {
"test": "mocha --reporter spec --check-leaks",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/test.js",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --timeout 10000 --reporter spec --check-leaks test/test.js"
},
"version": "1.0.1"
}