Arjun Barrett 4 years ago
parent
commit
55c38eb5e2

+ 4 - 0
CHANGELOG.md

@@ -0,0 +1,4 @@
+## 0.1.0
+- Rewrote API: added support for asynchronous (Worker) compression/decompression, fixed critical bug involving fixed Huffman trees
+## 0.0.1
+- Created, works on basic input

+ 55 - 22
README.md

@@ -1,24 +1,24 @@
-# fflate ![npm bundle size](https://img.shields.io/bundlephobia/min/fflate)
+# fflate
 High performance (de)compression in an 8kB package
 
 ## Why fflate?
 `fflate` (short for fast flate) is the **fastest, smallest, and most versatile** pure JavaScript compression and decompression library in existence, handily beating [`pako`](https://npmjs.com/package/pako), [`tiny-inflate`](https://npmjs.com/package/tiny-inflate), and [`UZIP.js`](https://github.com/photopea/UZIP.js) in performance benchmarks while being multiple times more lightweight. Its compression ratios are often better than even the original Zlib C library. It includes support for DEFLATE, GZIP, and Zlib data. Data compressed by `fflate` can be decompressed by other tools, and vice versa.
 
-|                           | `pako` | `tiny-inflate`       | `UZIP.js`             | `fflate`                       |
-|---------------------------|--------|----------------------|-----------------------|--------------------------------|
-| Decompression performance | 1x     | Up to 40% slower     | **Up to 40% faster**  | **Up to 40% faster**           |
-| Compression performance   | 1x     | N/A                  | Up to 5% faster       | **Up to 50% faster**           |
-| Bundle size (minified)    | 44.5kB | **3kB**              | 14.2kB                | 8kB **(3kB for only inflate)** |
-| Compression support       | ✅     | ❌                    | ✅                    | ✅                             |
-| Thread/Worker safe        | ✅     | ✅                    | ❌                    | ✅                             |
-| GZIP/Zlib support         | ✅     | ❌                    | ❌                    | ✅                             |
-| Uses ES Modules           | ❌     | ❌                    | ❌                    | ✅                             |
+|                            | `pako` | `tiny-inflate`       | `UZIP.js`             | `fflate`                       |
+|----------------------------|--------|----------------------|-----------------------|--------------------------------|
+| Decompression performance  | 1x     | Up to 40% slower     | **Up to 40% faster**  | **Up to 40% faster**           |
+| Compression performance    | 1x     | N/A                  | Up to 5% faster       | **Up to 50% faster**           |
+| Bundle size (minzipped)    | 45.6kB | **3kB**              | 14.2kB                | 8kB **(3kB for only inflate)** |
+| Compression support        | ✅     | ❌                    | ✅                    | ✅                             |
+| Thread/Worker safe         | ✅     | ✅                    | ❌                    | ✅                             |
+| GZIP/Zlib support          | ✅     | ❌                    | ❌                    | ✅                             |
+| Uses ES Modules            | ❌     | ❌                    | ❌                    | ✅                             |
 
 ## Usage
 
 Install `fflate`:
 ```sh
-npm install --save fflate # or yarn add fflate, or pnpm add fflate
+npm i fflate # or yarn add fflate, or pnpm add fflate
 ```
 
 Import:
@@ -26,7 +26,7 @@ Import:
 import * as fflate from 'fflate';
 // ALWAYS import only what you need to minimize bundle size.
 // So, if you just need GZIP compression support:
-import { gzip } from 'fflate';
+import { gzipSync } from 'fflate';
 ```
 If your environment doesn't support ES Modules (e.g. Node.js):
 ```js
@@ -47,8 +47,8 @@ const massiveFile = new Uint8Array(massiveFileBuf);
 // Higher level means lower performance but better compression
 // The level ranges from 0 (no compression) to 9 (max compression)
 // The default level is 6
-const notSoMassive = fflate.zlib(massiveFile, { level: 9 });
-const massiveAgain = fflate.unzlib(notSoMassive);
+const notSoMassive = fflate.zlibSync(massiveFile, { level: 9 });
+const massiveAgain = fflate.unzlibSync(notSoMassive);
 ```
 `fflate` can autodetect a compressed file's format as well:
 ```js
@@ -58,7 +58,7 @@ const compressed = new Uint8Array(
 // Above example with Node.js Buffers:
 // Buffer.from('H4sIAAAAAAAAE8tIzcnJBwCGphA2BQAAAA==', 'base64');
 
-const decompressed = fflate.decompress(compressed);
+const decompressed = fflate.decompressSync(compressed);
 ```
 
 Using strings is easy with `TextEncoder` and `TextDecoder`:
@@ -69,10 +69,10 @@ const buf = enc.encode('Hello world!');
 // The default compression method is gzip
 // Increasing mem may increase performance at the cost of memory
 // The mem ranges from 0 to 12, where 4 is the default
-const compressed = fflate.compress(buf, { level: 6, mem: 8 });
+const compressed = fflate.compressSync(buf, { level: 6, mem: 8 });
 
 // When you need to decompress:
-const decompressed = fflate.decompress(compressed);
+const decompressed = fflate.decompressSync(compressed);
 const origText = dec.decode(decompressed);
 console.log(origText); // Hello world!
 ```
@@ -81,9 +81,8 @@ Note that encoding the compressed data as a string, like in `pako`, is not nearl
 // data to string
 const dts = data => {
   let result = '';
-  for (let value of data) {
+  for (let value of data)
     result += String.fromCharCode(data);
-  }
   return result;
 }
 // string to data
@@ -91,11 +90,45 @@ const std = str => {
   let result = new Uint8Array(str.length);
   for (let i = 0; i < str.length; ++i)
     result[i] = str.charCodeAt(i);
-  return result.
+  return result;
 }
-const compressedString = dts(fflate.compress(buf));
-const decompressed = fflate.decompress(std(compressedString));
+const compressedString = dts(fflate.compressSync(buf));
+const decompressed = fflate.decompressSync(std(compressedString));
 ```
+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.
+```js
+import { gzip } from 'fflate';
+
+// Workers will work in almost any browser (even IE10!)
+// However, they fail on Node below v12 without the `--experimental-worker`
+// CLI flag, and will fail entirely on Node below v10.
+
+// Note that there is a significant initial overhead to using workers for both
+// performance (about 70ms) and bundle size (about 5kB), so it's best to avoid
+// the asynchronous API unless necessary.
+
+// For data under about 2MB, the main thread is blocked for so short a time
+// (under 100ms) during both compression and decompression that most users
+// cannot notice it anyway, so using the synchronous API is better.
+
+// However, if you're compressing multiple files at once, or are compressing
+// large amounts of data, the callback APIs are an order of magnitude faster.
+
+// All of the async APIs use a node-style callback as so:
+gzip(aMassiveFile, (err, data) => {
+  if (err) {
+    // Note that for now, this rarely, if ever, happens. Header validation
+    // throws an error synchronously, so you need to try-catch the entire
+    // block. This will only occur upon an exception in the worker (which
+    // is typically a bug.)
+    return;
+  }
+  // Use data however you like
+  console.log(data.length);
+});
+```
+
+Try not to use both the asynchronous and synchronous APIs. , they are about 9kB and 8kB respectively
 
 See the [documentation](https://github.com/101arrowz/fflate/blob/master/docs/README.md) for more detailed information about the API.
 

+ 282 - 9
docs/README.md

@@ -4,25 +4,85 @@
 
 ### Interfaces
 
+* [AsyncDeflateOptions](interfaces/asyncdeflateoptions.md)
+* [AsyncGunzipOptions](interfaces/asyncgunzipoptions.md)
+* [AsyncGzipOptions](interfaces/asyncgzipoptions.md)
+* [AsyncInflateOptions](interfaces/asyncinflateoptions.md)
+* [AsyncUnzlibOptions](interfaces/asyncunzliboptions.md)
+* [AsyncZlibOptions](interfaces/asynczliboptions.md)
 * [DeflateOptions](interfaces/deflateoptions.md)
-* [GZIPOptions](interfaces/gzipoptions.md)
+* [GzipOptions](interfaces/gzipoptions.md)
 * [ZlibOptions](interfaces/zliboptions.md)
 
+### Type aliases
+
+* [FlateCallback](README.md#flatecallback)
+
 ### Functions
 
 * [decompress](README.md#decompress)
+* [decompressSync](README.md#decompresssync)
 * [deflate](README.md#deflate)
+* [deflateSync](README.md#deflatesync)
 * [gunzip](README.md#gunzip)
+* [gunzipSync](README.md#gunzipsync)
 * [gzip](README.md#gzip)
+* [gzipSync](README.md#gzipsync)
 * [inflate](README.md#inflate)
+* [inflateSync](README.md#inflatesync)
 * [unzlib](README.md#unzlib)
+* [unzlibSync](README.md#unzlibsync)
 * [zlib](README.md#zlib)
+* [zlibSync](README.md#zlibsync)
+
+## Type aliases
+
+### FlateCallback
+
+Ƭ  **FlateCallback**: (err: Error,data: Uint8Array) => unknown
+
+Callback for asynchronous comrpession methods
+
+**`param`** Any error that occurred
+
+**`param`** The resulting data. Only present if `err` is null
 
 ## Functions
 
 ### decompress
 
-▸ **decompress**(`data`: Uint8Array, `out?`: Uint8Array): Uint8Array
+▸ **decompress**(`data`: Uint8Array, `opts`: [AsyncInflateOptions](interfaces/asyncinflateoptions.md), `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchrononously expands compressed GZIP, Zlib, or raw DEFLATE data, automatically detecting the format
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to decompress |
+`opts` | [AsyncInflateOptions](interfaces/asyncinflateoptions.md) | The decompression options |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon decompression completion  |
+
+**Returns:** void
+
+▸ **decompress**(`data`: Uint8Array, `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchrononously expands compressed GZIP, Zlib, or raw DEFLATE data, automatically detecting the format
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to decompress |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon decompression completion  |
+
+**Returns:** void
+
+___
+
+### decompressSync
+
+▸ **decompressSync**(`data`: Uint8Array, `out?`: Uint8Array): Uint8Array
 
 Expands compressed GZIP, Zlib, or raw DEFLATE data, automatically detecting the format
 
@@ -39,7 +99,38 @@ ___
 
 ### deflate
 
-▸ **deflate**(`data`: Uint8Array, `opts?`: [DeflateOptions](interfaces/deflateoptions.md)): Uint8Array
+▸ **deflate**(`data`: Uint8Array, `opts`: [AsyncDeflateOptions](interfaces/asyncdeflateoptions.md), `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously compresses data with DEFLATE without any wrapper
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to compress |
+`opts` | [AsyncDeflateOptions](interfaces/asyncdeflateoptions.md) | The compression options |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon compression completion  |
+
+**Returns:** void
+
+▸ **deflate**(`data`: Uint8Array, `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously compresses data with DEFLATE without any wrapper
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to compress |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon compression completion  |
+
+**Returns:** void
+
+___
+
+### deflateSync
+
+▸ **deflateSync**(`data`: Uint8Array, `opts?`: [DeflateOptions](interfaces/deflateoptions.md)): Uint8Array
 
 Compresses data with DEFLATE without any wrapper
 
@@ -56,7 +147,38 @@ ___
 
 ### gunzip
 
-▸ **gunzip**(`data`: Uint8Array, `out?`: Uint8Array): Uint8Array
+▸ **gunzip**(`data`: Uint8Array, `opts`: [AsyncGunzipOptions](interfaces/asyncgunzipoptions.md), `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously expands GZIP data
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to decompress |
+`opts` | [AsyncGunzipOptions](interfaces/asyncgunzipoptions.md) | The decompression options |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon decompression completion  |
+
+**Returns:** void
+
+▸ **gunzip**(`data`: Uint8Array, `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously expands GZIP data
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to decompress |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon decompression completion  |
+
+**Returns:** void
+
+___
+
+### gunzipSync
+
+▸ **gunzipSync**(`data`: Uint8Array, `out?`: Uint8Array): Uint8Array
 
 Expands GZIP data
 
@@ -73,7 +195,65 @@ ___
 
 ### gzip
 
-▸ **gzip**(`data`: Uint8Array, `opts?`: [GZIPOptions](interfaces/gzipoptions.md)): Uint8Array
+▸ **gzip**(`data`: Uint8Array, `opts`: [AsyncGzipOptions](interfaces/asyncgzipoptions.md), `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously compresses data with GZIP
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to compress |
+`opts` | [AsyncGzipOptions](interfaces/asyncgzipoptions.md) | The compression options |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon compression completion  |
+
+**Returns:** void
+
+▸ **gzip**(`data`: Uint8Array, `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously compresses data with GZIP
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to compress |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon compression completion  |
+
+**Returns:** void
+
+▸ **gzip**(`data`: Uint8Array, `opts`: [AsyncGzipOptions](interfaces/asyncgzipoptions.md), `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously compresses data with GZIP
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to compress |
+`opts` | [AsyncGzipOptions](interfaces/asyncgzipoptions.md) | The compression options |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon compression completion  |
+
+**Returns:** void
+
+▸ **gzip**(`data`: Uint8Array, `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously compresses data with GZIP
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to compress |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon compression completion  |
+
+**Returns:** void
+
+___
+
+### gzipSync
+
+▸ **gzipSync**(`data`: Uint8Array, `opts?`: [GzipOptions](interfaces/gzipoptions.md)): Uint8Array
 
 Compresses data with GZIP
 
@@ -82,7 +262,7 @@ Compresses data with GZIP
 Name | Type | Description |
 ------ | ------ | ------ |
 `data` | Uint8Array | The data to compress |
-`opts?` | [GZIPOptions](interfaces/gzipoptions.md) | The compression options |
+`opts?` | [GzipOptions](interfaces/gzipoptions.md) | The compression options |
 
 **Returns:** Uint8Array
 
@@ -90,7 +270,38 @@ ___
 
 ### inflate
 
-▸ **inflate**(`data`: Uint8Array, `out?`: Uint8Array): Uint8Array
+▸ **inflate**(`data`: Uint8Array, `opts`: [AsyncInflateOptions](interfaces/asyncinflateoptions.md), `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously expands DEFLATE data with no wrapper
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to decompress |
+`opts` | [AsyncInflateOptions](interfaces/asyncinflateoptions.md) | The decompression options |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon decompression completion  |
+
+**Returns:** void
+
+▸ **inflate**(`data`: Uint8Array, `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously expands DEFLATE data with no wrapper
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to decompress |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon decompression completion  |
+
+**Returns:** void
+
+___
+
+### inflateSync
+
+▸ **inflateSync**(`data`: Uint8Array, `out?`: Uint8Array): Uint8Array
 
 Expands DEFLATE data with no wrapper
 
@@ -107,7 +318,38 @@ ___
 
 ### unzlib
 
-▸ **unzlib**(`data`: Uint8Array, `out?`: Uint8Array): Uint8Array
+▸ **unzlib**(`data`: Uint8Array, `opts`: [AsyncGunzipOptions](interfaces/asyncgunzipoptions.md), `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously expands Zlib data
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to decompress |
+`opts` | [AsyncGunzipOptions](interfaces/asyncgunzipoptions.md) | The decompression options |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon decompression completion  |
+
+**Returns:** void
+
+▸ **unzlib**(`data`: Uint8Array, `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously expands Zlib data
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to decompress |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon decompression completion  |
+
+**Returns:** void
+
+___
+
+### unzlibSync
+
+▸ **unzlibSync**(`data`: Uint8Array, `out?`: Uint8Array): Uint8Array
 
 Expands Zlib data
 
@@ -124,7 +366,38 @@ ___
 
 ### zlib
 
-▸ **zlib**(`data`: Uint8Array, `opts?`: [ZlibOptions](interfaces/zliboptions.md)): Uint8Array
+▸ **zlib**(`data`: Uint8Array, `opts`: [AsyncZlibOptions](interfaces/asynczliboptions.md), `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously compresses data with Zlib
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to compress |
+`opts` | [AsyncZlibOptions](interfaces/asynczliboptions.md) | The compression options |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon compression completion  |
+
+**Returns:** void
+
+▸ **zlib**(`data`: Uint8Array, `cb`: [FlateCallback](README.md#flatecallback)): void
+
+Asynchronously compresses data with Zlib
+
+#### Parameters:
+
+Name | Type | Description |
+------ | ------ | ------ |
+`data` | Uint8Array | The data to compress |
+`cb` | [FlateCallback](README.md#flatecallback) | The function to be called upon compression completion  |
+
+**Returns:** void
+
+___
+
+### zlibSync
+
+▸ **zlibSync**(`data`: Uint8Array, `opts?`: [ZlibOptions](interfaces/zliboptions.md)): Uint8Array
 
 Compress data with Zlib
 

+ 68 - 0
docs/interfaces/asyncdeflateoptions.md

@@ -0,0 +1,68 @@
+# Interface: AsyncDeflateOptions
+
+Options for compressing data asynchronously into a DEFLATE format
+
+## Hierarchy
+
+* [DeflateOptions](deflateoptions.md)
+
+* AsyncOptions
+
+  ↳ **AsyncDeflateOptions**
+
+## Index
+
+### Properties
+
+* [consume](asyncdeflateoptions.md#consume)
+* [level](asyncdeflateoptions.md#level)
+* [mem](asyncdeflateoptions.md#mem)
+
+## Properties
+
+### consume
+
+• `Optional` **consume**: boolean
+
+*Inherited from [AsyncDeflateOptions](asyncdeflateoptions.md).[consume](asyncdeflateoptions.md#consume)*
+
+Whether or not to "consume" the source data. This will make the typed array/buffer you pass in
+unusable but will increase performance and reduce memory usage.
+
+___
+
+### level
+
+• `Optional` **level**: 0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9
+
+*Inherited from [DeflateOptions](deflateoptions.md).[level](deflateoptions.md#level)*
+
+The level of compression to use, ranging from 0-9.
+
+0 will store the data without compression.
+1 is fastest but compresses the worst, 9 is slowest but compresses the best.
+The default level is 6.
+
+Typically, binary data benefits much more from higher values than text data.
+In both cases, higher values usually take disproportionately longer than the reduction in final size that results.
+
+For example, a 1 MB text file could:
+- become 1.01 MB with level 0 in 1ms
+- become 400 kB with level 1 in 10ms
+- become 320 kB with level 9 in 100ms
+
+___
+
+### mem
+
+• `Optional` **mem**: 0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9 \| 10 \| 11 \| 12
+
+*Inherited from [DeflateOptions](deflateoptions.md).[mem](deflateoptions.md#mem)*
+
+The memory level to use, ranging from 0-12. Increasing this increases speed and compression ratio at the cost of memory.
+
+Note that this is exponential: while level 0 uses 4 kB, level 4 uses 64 kB, level 8 uses 1 MB, and level 12 uses 16 MB.
+It is recommended not to lower the value below 4, since that tends to hurt performance.
+In addition, values above 8 tend to help very little on most data and can even hurt performance.
+
+The default value is automatically determined based on the size of the input data.

+ 26 - 0
docs/interfaces/asyncgunzipoptions.md

@@ -0,0 +1,26 @@
+# Interface: AsyncGunzipOptions
+
+Options for decompressing GZIP data asynchronously
+
+## Hierarchy
+
+* AsyncOptions
+
+  ↳ **AsyncGunzipOptions**
+
+## Index
+
+### Properties
+
+* [consume](asyncgunzipoptions.md#consume)
+
+## Properties
+
+### consume
+
+• `Optional` **consume**: boolean
+
+*Inherited from [AsyncDeflateOptions](asyncdeflateoptions.md).[consume](asyncdeflateoptions.md#consume)*
+
+Whether or not to "consume" the source data. This will make the typed array/buffer you pass in
+unusable but will increase performance and reduce memory usage.

+ 92 - 0
docs/interfaces/asyncgzipoptions.md

@@ -0,0 +1,92 @@
+# Interface: AsyncGzipOptions
+
+Options for compressing data asynchronously into a GZIP format
+
+## Hierarchy
+
+* [GzipOptions](gzipoptions.md)
+
+* AsyncOptions
+
+  ↳ **AsyncGzipOptions**
+
+## Index
+
+### Properties
+
+* [consume](asyncgzipoptions.md#consume)
+* [filename](asyncgzipoptions.md#filename)
+* [level](asyncgzipoptions.md#level)
+* [mem](asyncgzipoptions.md#mem)
+* [mtime](asyncgzipoptions.md#mtime)
+
+## Properties
+
+### consume
+
+• `Optional` **consume**: boolean
+
+*Inherited from [AsyncDeflateOptions](asyncdeflateoptions.md).[consume](asyncdeflateoptions.md#consume)*
+
+Whether or not to "consume" the source data. This will make the typed array/buffer you pass in
+unusable but will increase performance and reduce memory usage.
+
+___
+
+### filename
+
+• `Optional` **filename**: string
+
+*Inherited from [GzipOptions](gzipoptions.md).[filename](gzipoptions.md#filename)*
+
+The filename of the data. If the `gunzip` command is used to decompress the data, it will output a file
+with this name instead of the name of the compressed file.
+
+___
+
+### level
+
+• `Optional` **level**: 0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9
+
+*Inherited from [DeflateOptions](deflateoptions.md).[level](deflateoptions.md#level)*
+
+The level of compression to use, ranging from 0-9.
+
+0 will store the data without compression.
+1 is fastest but compresses the worst, 9 is slowest but compresses the best.
+The default level is 6.
+
+Typically, binary data benefits much more from higher values than text data.
+In both cases, higher values usually take disproportionately longer than the reduction in final size that results.
+
+For example, a 1 MB text file could:
+- become 1.01 MB with level 0 in 1ms
+- become 400 kB with level 1 in 10ms
+- become 320 kB with level 9 in 100ms
+
+___
+
+### mem
+
+• `Optional` **mem**: 0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9 \| 10 \| 11 \| 12
+
+*Inherited from [DeflateOptions](deflateoptions.md).[mem](deflateoptions.md#mem)*
+
+The memory level to use, ranging from 0-12. Increasing this increases speed and compression ratio at the cost of memory.
+
+Note that this is exponential: while level 0 uses 4 kB, level 4 uses 64 kB, level 8 uses 1 MB, and level 12 uses 16 MB.
+It is recommended not to lower the value below 4, since that tends to hurt performance.
+In addition, values above 8 tend to help very little on most data and can even hurt performance.
+
+The default value is automatically determined based on the size of the input data.
+
+___
+
+### mtime
+
+• `Optional` **mtime**: Date \| string \| number
+
+*Inherited from [GzipOptions](gzipoptions.md).[mtime](gzipoptions.md#mtime)*
+
+When the file was last modified. Defaults to the current time.
+Set this to 0 to avoid specifying a modification date entirely.

+ 39 - 0
docs/interfaces/asyncinflateoptions.md

@@ -0,0 +1,39 @@
+# Interface: AsyncInflateOptions
+
+Options for decompressing DEFLATE data asynchronously
+
+## Hierarchy
+
+* AsyncOptions
+
+  ↳ **AsyncInflateOptions**
+
+  ↳↳ [AsyncUnzlibOptions](asyncunzliboptions.md)
+
+## Index
+
+### Properties
+
+* [consume](asyncinflateoptions.md#consume)
+* [size](asyncinflateoptions.md#size)
+
+## Properties
+
+### consume
+
+• `Optional` **consume**: boolean
+
+*Inherited from [AsyncDeflateOptions](asyncdeflateoptions.md).[consume](asyncdeflateoptions.md#consume)*
+
+Whether or not to "consume" the source data. This will make the typed array/buffer you pass in
+unusable but will increase performance and reduce memory usage.
+
+___
+
+### size
+
+• `Optional` **size**: number
+
+The original size of the data. Currently, the asynchronous API disallows
+writing into a buffer you provide; the best you can do is provide the
+size in bytes and be given back a new typed array.

+ 39 - 0
docs/interfaces/asyncunzliboptions.md

@@ -0,0 +1,39 @@
+# Interface: AsyncUnzlibOptions
+
+Options for decompressing Zlib data asynchronously
+
+## Hierarchy
+
+* [AsyncInflateOptions](asyncinflateoptions.md)
+
+  ↳ **AsyncUnzlibOptions**
+
+## Index
+
+### Properties
+
+* [consume](asyncunzliboptions.md#consume)
+* [size](asyncunzliboptions.md#size)
+
+## Properties
+
+### consume
+
+• `Optional` **consume**: boolean
+
+*Inherited from [AsyncDeflateOptions](asyncdeflateoptions.md).[consume](asyncdeflateoptions.md#consume)*
+
+Whether or not to "consume" the source data. This will make the typed array/buffer you pass in
+unusable but will increase performance and reduce memory usage.
+
+___
+
+### size
+
+• `Optional` **size**: number
+
+*Inherited from [AsyncInflateOptions](asyncinflateoptions.md).[size](asyncinflateoptions.md#size)*
+
+The original size of the data. Currently, the asynchronous API disallows
+writing into a buffer you provide; the best you can do is provide the
+size in bytes and be given back a new typed array.

+ 68 - 0
docs/interfaces/asynczliboptions.md

@@ -0,0 +1,68 @@
+# Interface: AsyncZlibOptions
+
+Options for compressing data asynchronously into a Zlib format
+
+## Hierarchy
+
+* [ZlibOptions](zliboptions.md)
+
+* AsyncOptions
+
+  ↳ **AsyncZlibOptions**
+
+## Index
+
+### Properties
+
+* [consume](asynczliboptions.md#consume)
+* [level](asynczliboptions.md#level)
+* [mem](asynczliboptions.md#mem)
+
+## Properties
+
+### consume
+
+• `Optional` **consume**: boolean
+
+*Inherited from [AsyncDeflateOptions](asyncdeflateoptions.md).[consume](asyncdeflateoptions.md#consume)*
+
+Whether or not to "consume" the source data. This will make the typed array/buffer you pass in
+unusable but will increase performance and reduce memory usage.
+
+___
+
+### level
+
+• `Optional` **level**: 0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9
+
+*Inherited from [DeflateOptions](deflateoptions.md).[level](deflateoptions.md#level)*
+
+The level of compression to use, ranging from 0-9.
+
+0 will store the data without compression.
+1 is fastest but compresses the worst, 9 is slowest but compresses the best.
+The default level is 6.
+
+Typically, binary data benefits much more from higher values than text data.
+In both cases, higher values usually take disproportionately longer than the reduction in final size that results.
+
+For example, a 1 MB text file could:
+- become 1.01 MB with level 0 in 1ms
+- become 400 kB with level 1 in 10ms
+- become 320 kB with level 9 in 100ms
+
+___
+
+### mem
+
+• `Optional` **mem**: 0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9 \| 10 \| 11 \| 12
+
+*Inherited from [DeflateOptions](deflateoptions.md).[mem](deflateoptions.md#mem)*
+
+The memory level to use, ranging from 0-12. Increasing this increases speed and compression ratio at the cost of memory.
+
+Note that this is exponential: while level 0 uses 4 kB, level 4 uses 64 kB, level 8 uses 1 MB, and level 12 uses 16 MB.
+It is recommended not to lower the value below 4, since that tends to hurt performance.
+In addition, values above 8 tend to help very little on most data and can even hurt performance.
+
+The default value is automatically determined based on the size of the input data.

+ 3 - 1
docs/interfaces/deflateoptions.md

@@ -6,10 +6,12 @@ Options for compressing data into a DEFLATE format
 
 * **DeflateOptions**
 
-  ↳ [GZIPOptions](gzipoptions.md)
+  ↳ [GzipOptions](gzipoptions.md)
 
   ↳ [ZlibOptions](zliboptions.md)
 
+  ↳ [AsyncDeflateOptions](asyncdeflateoptions.md)
+
 ## Index
 
 ### Properties

+ 4 - 2
docs/interfaces/gzipoptions.md

@@ -1,4 +1,4 @@
-# Interface: GZIPOptions
+# Interface: GzipOptions
 
 Options for compressing data into a GZIP format
 
@@ -6,7 +6,9 @@ Options for compressing data into a GZIP format
 
 * [DeflateOptions](deflateoptions.md)
 
-  ↳ **GZIPOptions**
+  ↳ **GzipOptions**
+
+  ↳↳ [AsyncGzipOptions](asyncgzipoptions.md)
 
 ## Index
 

+ 2 - 0
docs/interfaces/zliboptions.md

@@ -8,6 +8,8 @@ Options for compressing data into a Zlib format
 
   ↳ **ZlibOptions**
 
+  ↳↳ [AsyncZlibOptions](asynczliboptions.md)
+
 ## Index
 
 ### Properties

+ 8 - 4
package.json

@@ -1,10 +1,14 @@
 {
   "name": "fflate",
-  "version": "0.0.4",
+  "version": "0.1.0",
   "description": "High performance (de)compression in an 8kB package",
   "main": "lib/index.js",
   "module": "esm/index.js",
   "types": "lib/index.d.ts",
+  "browser": {
+    "./lib/node-worker.js": "./lib/worker.js",
+    "./esm/node-worker.js": "./esm/worker.js"
+  },
   "sideEffects": false,
   "repository": "https://github.com/101arrowz/fflate",
   "author": "Arjun Barrett",
@@ -25,9 +29,9 @@
   "scripts": {
     "build": "yarn build:lib && yarn build:docs",
     "build:lib": "tsc && tsc --project tsconfig.esm.json",
-    "build:docs": "typedoc --mode library --plugin typedoc-plugin-markdown --hideProjectName --hideBreadcrumbs --readme none --disableSources",
-    "test": "TS_NODE_PROJECT=test/tsconfig.json uvu test -- -b -r ts-node/register",
-    "prepack": "yarn build"
+    "build:docs": "typedoc --mode library --plugin typedoc-plugin-markdown --hideProjectName --hideBreadcrumbs --readme none --disableSources src/index.ts",
+    "test": "TS_NODE_PROJECT=test/tsconfig.json uvu -b -r ts-node/register test",
+    "prepack": "yarn build && yarn test"
   },
   "devDependencies": {
     "@types/node": "^14.11.2",

File diff suppressed because it is too large
+ 116 - 8
src/index.ts


+ 18 - 0
src/node-worker.ts

@@ -0,0 +1,18 @@
+// Mediocre shim
+import { FlateCallback } from '.';
+import { Worker } from 'worker_threads';
+const workerAdd = ";var __w=require('worker_threads');__w.parentPort.on('message',function(m){onmessage({data:m})}),postMessage=function(m,t){__w.parentPort.postMessage(m,t)};self=global";
+export default (c: string, msg: unknown, transfer: ArrayBuffer[], cb: FlateCallback) => {
+  let done = false;
+  const cb2: typeof cb = (e, d) => {
+    done = true;
+    cb(e, d);
+  }
+  new Worker(c + workerAdd, { eval: true })
+    .on('error', e => cb2(e, null))
+    .on('message', m => cb2(null, m))
+    .on('exit', c => {
+      if (!done) cb2(new Error('Exited with code ' + c), null);
+    })
+    .postMessage(msg, transfer);
+}

+ 14 - 0
src/worker.ts

@@ -0,0 +1,14 @@
+import { FlateCallback } from '.';
+
+export default (c: string, msg: unknown, transfer: ArrayBuffer[], cb: FlateCallback) => {
+  const u = URL.createObjectURL(new Blob([c], { type: 'text/javascript' }));
+  const w = new Worker(u);
+  const cb2: typeof cb = (e, d) => {
+    w.terminate();
+    URL.revokeObjectURL(u);
+    cb(e, d);
+  }
+  w.onerror = e => cb2(e.error, null);
+  w.onmessage = e => cb2(null, e.data);
+  w.postMessage(msg, transfer);
+}

+ 6 - 6
test/util.ts

@@ -147,12 +147,12 @@ const fflate = resolve(__dirname, '..');
 
 export const workers = {
   fflate: {
-    deflate: wc(fflate, 'deflate'),
-    inflate: wc(fflate, 'inflate'),
-    gzip: wc(fflate, 'gzip'),
-    gunzip: wc(fflate, 'gunzip'),
-    zlib: wc(fflate, 'zlib'),
-    unzlib: wc(fflate, 'unzlib')
+    deflate: wc(fflate, 'deflateSync'),
+    inflate: wc(fflate, 'inflateSync'),
+    gzip: wc(fflate, 'gzipSync'),
+    gunzip: wc(fflate, 'gunzipSync'),
+    zlib: wc(fflate, 'zlibSync'),
+    unzlib: wc(fflate, 'unzlibSync')
   },
   pako: {
     deflate: wc('pako', 'deflateRaw'),

+ 1 - 0
tsconfig.esm.json

@@ -2,6 +2,7 @@
   "extends": "./tsconfig.json",
   "compilerOptions": {
     "declaration": false,
+    "moduleResolution": "node",
     "module": "ESNext",
     "outDir": "esm"
   }

+ 3 - 3
tsconfig.json

@@ -1,8 +1,8 @@
 {
   "compilerOptions": {
     "declaration": true,
-    "outDir": "lib/",
-    "lib": ["ES5"]
+    "moduleResolution": "node",
+    "outDir": "lib/"
   },
-  "include": ["src/index.ts"]
+  "include": ["src/*.ts"]
 }

Some files were not shown because too many files changed in this diff