-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(store): wrap store.createWriteStream to make callback optional
- Loading branch information
Showing
6 changed files
with
100 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Writable } from 'stream' | ||
|
||
const SIGNAL_LAST_WRITE = new Buffer([0]) | ||
|
||
/** | ||
* Wraps a write stream and waits until stream.unblock([err]) has been called | ||
* before ending and emitting the 'finish' event. | ||
*/ | ||
export default class extends Writable { | ||
constructor(dest) { | ||
super() | ||
this.dest = dest | ||
this.blockingState = { | ||
blocked: true, | ||
err: null, | ||
cb: false, | ||
} | ||
} | ||
|
||
_write(chunk, encoding, cb) { | ||
if (chunk === SIGNAL_LAST_WRITE) return this._lastWrite(cb) | ||
this.dest.write(chunk, encoding, cb) | ||
} | ||
|
||
_lastWrite(cb) { | ||
const { blockingState } = this | ||
this.dest.end((err) => { | ||
if (err) return cb(err) | ||
if (blockingState.blocked) { | ||
blockingState.cb = cb | ||
} else { | ||
cb(blockingState.err) | ||
} | ||
}) | ||
} | ||
|
||
unblock(err) { | ||
const { blockingState } = this | ||
if (blockingState.cb) return blockingState.cb(err) | ||
blockingState.blocked = false | ||
blockingState.err = err | ||
} | ||
|
||
end(data, enc, cb) { | ||
if (typeof data === 'function') return this.end(null, null, data) | ||
if (typeof enc === 'function') return this.end(data, null, enc) | ||
if (data) this.write(data) | ||
this.write(SIGNAL_LAST_WRITE) | ||
super.end(cb) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { PassThrough } from 'stream' | ||
import BlockingWritable from './blocking-writable' | ||
|
||
/** | ||
* asbtract-blob-store's API requires waiting for a callback on writes. | ||
* | ||
* Cloud-cache however does not have this requirement and instead uses a pure | ||
* sync streaming/event API. Since we cannot rely on abstract-blob-store's | ||
* 'finish' events, we must wait for the callback before our own write stream | ||
* ends and emits 'finish'. | ||
*/ | ||
export default (store, key, cb) => { | ||
// go to normal mode with callback | ||
if (typeof cb === 'function') return store.createWriteStream(key, cb) | ||
// no call back mode... | ||
|
||
const through = new PassThrough() | ||
const ws = new BlockingWritable(through) | ||
|
||
through.pipe(store.createWriteStream(key, (err) => { | ||
ws.unblock(err) | ||
})) | ||
|
||
return ws | ||
} |