-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
65 additions
and
2,166 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { AsyncDeflate } from '../../..'; | ||
|
||
export default (stream: AsyncDeflate, highWaterMark = 65536) => { | ||
// whether backpressure was observed on readable stream | ||
let backpressure = false; | ||
let resolveBackpressure: () => void = () => {}; | ||
|
||
// fflate has built-in buffering; don't use WHATWG highWaterMark implementation | ||
const writable = new WritableStream({ | ||
async write(dat: Uint8Array) { | ||
stream.push(dat); | ||
|
||
const blockers: Promise<void>[] = []; | ||
|
||
if (stream.queuedSize >= highWaterMark) { | ||
blockers.push(new Promise(resolve => { | ||
stream.ondrain = () => { | ||
if (stream.queuedSize < highWaterMark) resolve(); | ||
} | ||
})); | ||
} | ||
|
||
if (backpressure) { | ||
blockers.push(new Promise(resolve => { | ||
resolveBackpressure = resolve; | ||
})); | ||
} | ||
|
||
await Promise.all(blockers); | ||
}, | ||
close() { | ||
stream.push(new Uint8Array(0), true); | ||
} | ||
}); | ||
|
||
const readable = new ReadableStream({ | ||
start(controller: ReadableStreamDefaultController<Uint8Array>) { | ||
stream.ondata = (err, chunk, final) => { | ||
if (err) writable.abort(err.message); | ||
controller.enqueue(chunk); | ||
if (controller.desiredSize != null && controller.desiredSize <= 0) { | ||
backpressure = true; | ||
} else { | ||
backpressure = false; | ||
resolveBackpressure(); | ||
} | ||
if (final) controller.close(); | ||
} | ||
}, | ||
pull() { | ||
backpressure = false; | ||
resolveBackpressure(); | ||
} | ||
}); | ||
|
||
return { readable, writable }; | ||
} |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.