Skip to content

Commit d324365

Browse files
committed
improve demo, prepare for publish
1 parent d60ed05 commit d324365

12 files changed

Lines changed: 65 additions & 2166 deletions

File tree

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ Note that tree shaking is completely unsupported from the CDN. If you want
6060
a small build without build tools, please ask me and I will make one manually
6161
with only the features you need. This build is about 31kB, or 11.5kB gzipped.
6262
-->
63-
<script src="https://unpkg.com/[email protected].0"></script>
64-
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/umd/index.js"></script>
63+
<script src="https://unpkg.com/[email protected].2"></script>
64+
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/umd/index.js"></script>
6565
<!-- Now, the global variable fflate contains the library -->
6666

6767
<!-- If you're going buildless but want ESM, import from Skypack -->
6868
<script type="module">
69-
import * as fflate from 'https://cdn.skypack.dev/[email protected].0?min';
69+
import * as fflate from 'https://cdn.skypack.dev/[email protected].2?min';
7070
</script>
7171
```
7272

@@ -75,8 +75,8 @@ If you are using Deno:
7575
// Don't use the ?dts Skypack flag; it isn't necessary for Deno support
7676
// The @deno-types comment adds TypeScript typings
7777

78-
// @deno-types="https://cdn.skypack.dev/[email protected].0/lib/index.d.ts"
79-
import * as fflate from 'https://cdn.skypack.dev/[email protected].0?min';
78+
// @deno-types="https://cdn.skypack.dev/[email protected].2/lib/index.d.ts"
79+
import * as fflate from 'https://cdn.skypack.dev/[email protected].2?min';
8080
```
8181

8282

@@ -376,19 +376,16 @@ unzipper.push(zipChunk2);
376376
unzipper.push(zipChunk3, true);
377377
```
378378

379-
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.
379+
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.
380380

381-
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.
381+
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.
382382
```js
383383
import {
384384
gzip, zlib, AsyncGzip, zip, unzip, strFromU8,
385385
Zip, AsyncZipDeflate, Unzip, AsyncUnzipInflate
386386
} from 'fflate';
387387

388388
// Workers will work in almost any browser (even IE11!)
389-
// However, they fail below Node v12 without the --experimental-worker
390-
// CLI flag, and will fail entirely on Node below v10.
391-
392389
// All of the async APIs use a node-style callback as so:
393390
const terminate = gzip(aMassiveFile, (err, data) => {
394391
if (err) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { AsyncDeflate } from '../../..';
2+
3+
export default (stream: AsyncDeflate, highWaterMark = 65536) => {
4+
// whether backpressure was observed on readable stream
5+
let backpressure = false;
6+
let resolveBackpressure: () => void = () => {};
7+
8+
// fflate has built-in buffering; don't use WHATWG highWaterMark implementation
9+
const writable = new WritableStream({
10+
async write(dat: Uint8Array) {
11+
stream.push(dat);
12+
13+
const blockers: Promise<void>[] = [];
14+
15+
if (stream.queuedSize >= highWaterMark) {
16+
blockers.push(new Promise(resolve => {
17+
stream.ondrain = () => {
18+
if (stream.queuedSize < highWaterMark) resolve();
19+
}
20+
}));
21+
}
22+
23+
if (backpressure) {
24+
blockers.push(new Promise(resolve => {
25+
resolveBackpressure = resolve;
26+
}));
27+
}
28+
29+
await Promise.all(blockers);
30+
},
31+
close() {
32+
stream.push(new Uint8Array(0), true);
33+
}
34+
});
35+
36+
const readable = new ReadableStream({
37+
start(controller: ReadableStreamDefaultController<Uint8Array>) {
38+
stream.ondata = (err, chunk, final) => {
39+
if (err) writable.abort(err.message);
40+
controller.enqueue(chunk);
41+
if (controller.desiredSize != null && controller.desiredSize <= 0) {
42+
backpressure = true;
43+
} else {
44+
backpressure = false;
45+
resolveBackpressure();
46+
}
47+
if (final) controller.close();
48+
}
49+
},
50+
pull() {
51+
backpressure = false;
52+
resolveBackpressure();
53+
}
54+
});
55+
56+
return { readable, writable };
57+
}

demo/components/code-box/stream-adapter.tsx

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fflate",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"description": "High performance (de)compression in an 8kB package",
55
"main": "./lib/index.cjs",
66
"module": "./esm/browser.js",

rs/fflate-wasm/Cargo.toml

Lines changed: 0 additions & 19 deletions
This file was deleted.

rs/fflate-wasm/src/lib.rs

Lines changed: 0 additions & 43 deletions
This file was deleted.

rs/fflate/Cargo.toml

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)