-
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.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,13 +60,13 @@ Note that tree shaking is completely unsupported from the CDN. If you want | |
a small build without build tools, please ask me and I will make one manually | ||
with only the features you need. This build is about 31kB, or 11.5kB gzipped. | ||
--> | ||
<script src="https://unpkg.com/[email protected].0"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/umd/index.js"></script> | ||
<script src="https://unpkg.com/[email protected].2"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/umd/index.js"></script> | ||
<!-- Now, the global variable fflate contains the library --> | ||
|
||
<!-- If you're going buildless but want ESM, import from Skypack --> | ||
<script type="module"> | ||
import * as fflate from 'https://cdn.skypack.dev/[email protected].0?min'; | ||
import * as fflate from 'https://cdn.skypack.dev/[email protected].2?min'; | ||
</script> | ||
``` | ||
|
||
|
@@ -75,8 +75,8 @@ If you are using Deno: | |
// Don't use the ?dts Skypack flag; it isn't necessary for Deno support | ||
// The @deno-types comment adds TypeScript typings | ||
|
||
// @deno-types="https://cdn.skypack.dev/[email protected].0/lib/index.d.ts" | ||
import * as fflate from 'https://cdn.skypack.dev/[email protected].0?min'; | ||
// @deno-types="https://cdn.skypack.dev/[email protected].2/lib/index.d.ts" | ||
import * as fflate from 'https://cdn.skypack.dev/[email protected].2?min'; | ||
``` | ||
|
||
|
||
|
@@ -376,19 +376,16 @@ unzipper.push(zipChunk2); | |
unzipper.push(zipChunk3, true); | ||
``` | ||
|
||
As you may have guessed, there is an asynchronous version of every method as well. Unlike most libraries, this will cause the compression or decompression run in a separate thread entirely and automatically by using Web (or Node) Workers (as of now, Deno is unsupported). This means that the processing will not block the main thread at all. | ||
As you may have guessed, there is an asynchronous version of every method as well. Unlike most libraries, this will cause the compression or decompression run in a separate thread entirely and automatically by using Web (or Node) Workers. This means that the processing will not block the main thread at all. | ||
|
||
Note that there is a significant initial overhead to using workers of about 70ms for each asynchronous function. For instance, if you call `unzip` ten times, the overhead only applies for the first call, but if you call `unzip` and `zlib`, they will each cause the 70ms delay. Therefore, it's best to avoid the asynchronous API unless necessary. However, if you're compressing multiple large files at once, or the synchronous API causes the main thread to hang for too long, the callback APIs are an order of magnitude better. | ||
Note that there is a significant initial overhead to using workers of about 50ms for each asynchronous function. For instance, if you call `unzip` ten times, the overhead only applies for the first call, but if you call `unzip` and `zlib`, they will each cause the 50ms delay. For small (under about 50kB) payloads, the asynchronous APIs will be much slower. However, if you're compressing larger files/multiple files at once, or if the synchronous API causes the main thread to hang for too long, the callback APIs are an order of magnitude better. | ||
```js | ||
import { | ||
gzip, zlib, AsyncGzip, zip, unzip, strFromU8, | ||
Zip, AsyncZipDeflate, Unzip, AsyncUnzipInflate | ||
} from 'fflate'; | ||
|
||
// Workers will work in almost any browser (even IE11!) | ||
// However, they fail below Node v12 without the --experimental-worker | ||
// CLI flag, and will fail entirely on Node below v10. | ||
|
||
// All of the async APIs use a node-style callback as so: | ||
const terminate = gzip(aMassiveFile, (err, data) => { | ||
if (err) { | ||
|
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.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.