Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ditch iltorb and expect node >=11.8 #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Some environments may not be able to build these dependencies, but
`shrink-ray` tries to run even when they are absent; it just falls back to
gzip.

Therefore, the `iltorb` and `node-zopfli-es` modules are listed as
Therefore, the `node-zopfli-es` module is listed as
`peerDependencies` in `package.json`. This is for two reasons:

- `shrink-ray` will install successfully without them and fall back to gzip
Expand All @@ -93,18 +93,12 @@ Add them manually to your `package.json` as `optionalDependencies`:

```js
"optionalDependencies": {
"iltorb": "~2.0.0",
"node-zopfli-es": "~1.0.3"
}
```

Then, run `npm install` again.

_(Node `>=11.8` has Brotli compression built in, but `shrink-ray` supports
prior versions of Node as well. If your version of Node is `>=11.8`, the
`iltorb` module is not necessary at runtime; however, you'll still see a
warning at install time.)_

# API

```js
Expand Down
64 changes: 6 additions & 58 deletions brotli-compat.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,8 @@
module.exports = function brotliCompat() {
const zlib = require('zlib');

if (typeof zlib.createBrotliCompress === 'function') {
/**
* Map an options object for `iltorb` into an options object for `brotli`.
*/
const ILTORB_OPTION_NAMES_TO_BROTLI_PARAM_NAMES = {
mode: zlib.constants.BROTLI_PARAM_MODE,
quality: zlib.constants.BROTLI_PARAM_QUALITY,
lgwin: zlib.constants.BROTLI_PARAM_LGWIN,
lgblock: zlib.constants.BROTLI_PARAM_LGBLOCK,
disable_literal_context_modeling:
zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING,
large_window: zlib.constants.BROTLI_PARAM_LARGE_WINDOW
};
const iltorbOptionsToNodeZlibBrotliOpts = iltorbOpts => {
if (!iltorbOpts) return iltorbOpts;
const params = {};
Object.keys(iltorbOpts).forEach(key => {
if (ILTORB_OPTION_NAMES_TO_BROTLI_PARAM_NAMES.hasOwnProperty(key)) {
params[ILTORB_OPTION_NAMES_TO_BROTLI_PARAM_NAMES[key]] = iltorbOpts[
key
];
}
});
return { params };
};

/**
* Replicate the 'iltorb' interface for backwards compatibility.
*/
return {
compressStream: opts => zlib.createBrotliCompress(
iltorbOptionsToNodeZlibBrotliOpts(opts)
),
decompressStream: opts => zlib.createBrotliDecompress(
iltorbOptionsToNodeZlibBrotliOpts(opts)
)
};
const zlib = require("zlib");

}

// If we get here, then our NodeJS does not support brotli natively.
try {
return require('iltorb');
} catch (e) {
process.emitWarning('Module "iltorb" was unavailable.',
{
type: 'MISSING_MODULE',
code: 'BROTLI_COMPAT',
detail: 'Brotli compression unavailable; will fall back to gzip.'
}
);
}

// Return a signal value instead of throwing an exception, so the code in the
// index file doesn't have to try/catch again.
return false;
module.exports = function brotliCompat() {
return {
compressStream: (opts) => zlib.createBrotliCompress(opts),
decompressStream: (opts) => zlib.createBrotliDecompress(opts),
};
};
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const zlib = require('zlib');
* the module should work!
* Known dependency issues:
* - node-zopfli-es is not compatible with Node <8.11.
* - iltorb is not required for Node >= 11.8, whose zlib has brotli built in.
*/

const brotliCompat = require('./brotli-compat');
Expand Down
Loading