Skip to content

Commit

Permalink
improve demo, prepare for publish
Browse files Browse the repository at this point in the history
  • Loading branch information
101arrowz committed Feb 7, 2024
1 parent d60ed05 commit d324365
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 2,166 deletions.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/fflate@0.8.0"></script>
<script src="https://cdn.jsdelivr.net/npm/fflate@0.8.0/umd/index.js"></script>
<script src="https://unpkg.com/fflate@0.8.2"></script>
<script src="https://cdn.jsdelivr.net/npm/fflate@0.8.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/fflate@0.8.0?min';
import * as fflate from 'https://cdn.skypack.dev/fflate@0.8.2?min';
</script>
```

Expand All @@ -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/fflate@0.8.0/lib/index.d.ts"
import * as fflate from 'https://cdn.skypack.dev/fflate@0.8.0?min';
// @deno-types="https://cdn.skypack.dev/fflate@0.8.2/lib/index.d.ts"
import * as fflate from 'https://cdn.skypack.dev/fflate@0.8.2?min';
```


Expand Down Expand Up @@ -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) {
Expand Down
57 changes: 57 additions & 0 deletions demo/components/code-box/stream-adapter.ts
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 };
}
17 changes: 0 additions & 17 deletions demo/components/code-box/stream-adapter.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fflate",
"version": "0.8.1",
"version": "0.8.2",
"description": "High performance (de)compression in an 8kB package",
"main": "./lib/index.cjs",
"module": "./esm/browser.js",
Expand Down
19 changes: 0 additions & 19 deletions rs/fflate-wasm/Cargo.toml

This file was deleted.

43 changes: 0 additions & 43 deletions rs/fflate-wasm/src/lib.rs

This file was deleted.

37 changes: 0 additions & 37 deletions rs/fflate/Cargo.toml

This file was deleted.

Loading

0 comments on commit d324365

Please sign in to comment.