Comparing version 0.2.0 to 0.3.0
@@ -13,3 +13,3 @@ { | ||
"args": [ | ||
"test" | ||
"test/spec/write.js" | ||
] | ||
@@ -16,0 +16,0 @@ } |
{ | ||
"name": "wrote", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Promise-based writing to filesystem for Node.js", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "zoroaster test", | ||
"test-watch": "zoroaster test --watch" | ||
"test": "zoroaster test/spec", | ||
"test-watch": "zoroaster test/spec --watch", | ||
"tw": "npm run test-watch" | ||
}, | ||
@@ -27,7 +28,8 @@ "repository": { | ||
"devDependencies": { | ||
"catchment": "1.0.0", | ||
"zoroaster": "0.4.4" | ||
}, | ||
"dependencies": { | ||
"makepromise": "^1.0.0" | ||
"makepromise": "1.0.0" | ||
} | ||
} |
@@ -69,2 +69,31 @@ # wrote | ||
## wrote.write(ws:Writable, data:any|Readable) => Promise<Writable> | ||
Pipe a `Readable` to the `Writable` stream and wait until it is finished, or end `Writable` with | ||
given data (pass `null` to end stream without any more data). | ||
```js | ||
const wrote = require('wrote') | ||
const assert = require('assert') | ||
const Writable = require('stream').Writable | ||
const testString = 'hello world' | ||
const buffer = Buffer.from(testString) | ||
const allRawData = [] | ||
const ws = new Writable({ | ||
write: (chunk, encoding, next) => { | ||
allRawData.push(chunk) | ||
next() | ||
}, | ||
}) | ||
wrote.write(ws, buffer) | ||
.then(() => { | ||
console.log(allRawData.map(d => String(d))) // [ 'hello world' ] | ||
assert.deepEqual(allRawData, [ | ||
buffer, | ||
]) | ||
}) | ||
.catch(console.error) | ||
``` | ||
## todo | ||
@@ -71,0 +100,0 @@ |
const fs = require('fs') | ||
const os = require('os') | ||
const path = require('path') | ||
const Readable = require('stream').Readable | ||
const Writable = require('stream').Writable | ||
const makePromise = require('makepromise') | ||
@@ -65,3 +67,32 @@ const TEMP_DIR = os.tmpdir() | ||
/** | ||
* Write data to the stream, and resolve when it's ended. | ||
* @param {Writable} ws write stream | ||
* @param {string|Readable} source read source | ||
* @returns {Promise<Writable>} A promise resolved with the writable stream, or rejected | ||
* when an error occurred while reading or writing. | ||
*/ | ||
function write(ws, source) { | ||
if (!(ws instanceof Writable)) { | ||
return Promise.reject(new Error('Writable stream expected')) | ||
} | ||
if (source instanceof Readable) { | ||
if (!source.readable) { | ||
return Promise.reject(new Error('Stream is not readable')) | ||
} | ||
return new Promise((resolve, reject) => { | ||
ws.on('finish', () => { | ||
resolve(ws) | ||
}) | ||
ws.on('error', reject) | ||
source.on('error', reject) | ||
source.pipe(ws) | ||
}) | ||
} | ||
return makePromise(ws.end.bind(ws), source, ws) | ||
} | ||
Object.defineProperty(wrote, 'write', { get: () => write }) | ||
Object.defineProperty(wrote, 'erase', { get: () => erase }) | ||
module.exports = wrote |
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
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
19174
14
446
107
2
+ Addedmakepromise@1.0.0(transitive)
- Removedclean-stack@1.3.0(transitive)
- Removederotic@0.2.0(transitive)
- Removedmakepromise@1.1.1(transitive)
Updatedmakepromise@1.0.0