Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wrote

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wrote - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

CHANGELOG.md

2

.vscode/launch.json

@@ -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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc