3.0.1 • Published 2 years ago

@etomon/encode-tools v3.0.1

Weekly downloads
-
License
LGPL-3.0
Repository
github
Last release
2 years ago

DEPRECATED

This package has been deprecated, see @znetstar/encode-tools-native for a drop-in replacement.

Etomon Encode Tools

NPM

Build Status

This package aggregates different libraries for encoding, serializing, compressing, generating ids and hashing things, exposing a common interface.

Many other packages serve the same purpose, but our objective is to ensure a consistent experience in both node.js and the browser and standardize the api so functions work the same way across different underlying libraries.

Etomon Encode Tools also has a command line wrapper encode-cli.

Examples

Encoding a Buffer as base64url

  let enc = new EncodeTools();
  let buf = Buffer.from('hello world', 'utf8');
  let newBuf = enc.encodeBuffer(buf, BinaryEncoding.base64url);
  console.log(newBuf.toString('utf8'));

Hashing an object wth xxhash

let enc = new EncodeTools();
let obj = { foo: 'bar' };
let newBuf = await enc.hashObject(obj, HashAlgorithm.xxhash64);
console.log(newBuf.toString('utf8'));

Serializing an object wth msgpack

let enc = new EncodeTools();
let obj = { foo: 'bar' };
let newBuf = await enc.serializeObject(obj, SerializationFormat.msgpack);
console.log(newBuf.toString('base64'));

Generating a base64-encoded UUID v4

let enc = new EncodeTools();
let newBuf = await enc.uniqueId(IDFormat.uuidv4);
console.log(newBuf.toString('base64'));

Compressing a buffer with lzma

let enc = new EncodeTools();
let newBuf = await enc.compress(Buffer.from('hi', 'utf8'), CompressionFormat.lzma);
console.log(newBuf.toString('base64'));

Resizing a png image

let enc = new EncodeTools();
let imageBuf = await (await new Promise((resolve, reject) => {
  new (Jimp)(500, 500, '#FFFFFF', (err: unknown, image: any) => {
    if (err) reject(err);
    else resolve(image);
  });
})).getBufferAsync('image/png');

let myResizedPng = await enc.resizeImage(imageBuf, { width: 250 }, ImageFormat.png);

Algorithms

Below are a list of supported algorithms, their backing library, and their support in the browser.

Binary Encoding

NameBrowser?Underlying Package
nodeBufferbuffer/(built-in)
base64(built-in)
base64url(built-in)
hex(built-in)
base32base32.js
hashidshashids
arrayBuffer(built-in)
base85 (ascii85)base85
ascii85base85

Hashing

NameBrowser?Underlying Package
crc32hash-wasm
xxhash3xxhash-addon
xxhash64xxhash-addon/hash-wasm
xxhash32xxhash-addon/hash-wasm
md5hash-wasm
sha1hash-wasm
sha2hash-wasm
sha3hash-wasm
bcrypthash-wasm

ID Generation

NameBrowser?Underlying Package
uuidv4uuid
uuidv2uuid
uuidv4stringuuid
uuidv2stringuuid
objectIdbson-ext/bson
nanoidnanoid
timestamp(built in)

Serialization

NameBrowser?Underlying Package
json(built in)
cborcbor/cbor-web
msgpack@msgpack/msgpack
bsonbson-ext/bson

Compression

NameBrowser?Underlying Package
zstdzstd-codec
lzmalzma/lzma-native

Image Manipulation

NameBrowser?Underlying Package
pngjimp/sharp
jpegjimp/sharp
webpsharp
avifsharp
tiffsharp
gif*sharp

Requirements

Etomon Encode Tools runs in the browser and in node.js, with a few exceptions. The bson-ext, lzma-native, xxhash-addon and cbor-extract packages have native bindings, and so cannot run in the browser. For browser compatibility, the EncodeTools class uses the pure javascript bson, lzma, hash-wsam, and cbor-x packages, respectively, to provide equivalent support albeit at the cost of performance. Additionally, hash-wsam lacks support for xxhash3.

The EncodeToolsAuto class will use the native packages bson-ext, lzma-native and xxhash-addon (and any future native packages). bson-ext, lzma-native and xxhash-addon are listed as optional dependencies, and NPM will attempt to install them automatically.

The constructor of EncodeToolsAuto takes a second set of default EncodingOptions to use as a fallback if it cannot find the needed module.

const enc = new EncodeToolsAuto({ hashAlgorithm: HashAlgorithm.xxhash3 }, { hashAlgorithm: HashAlgorithm.xxhash64 });
if (enc.availableNativeModules.xxhashAddon)
    console.log('should be xxhash3', await enc.hashString('Test'));
else
    console.log('should be xxhash64', await enc.hashString('Test'));

The gif image format in EncodeToolsAuto requires libvips compiled with ImageMagick support (as described here). I haven't had time to re-build libvips on my machine, so there are no mocha tests for the gif format.

Usage

Please see the documentation located at https://etomonusa.github.io/encode-tools/

Webpack

For issues with Webpack, try adding all the native dependencies to the externals section.

{
  externals: {
      'xxhash-addon': 'commonjs xxhash-addon',
      'bson-ext': 'commonjs bson-ext',
      'shelljs': 'commonjs shelljs',
      'lzma-native': 'commonjs lzma-native',
      'sharp': 'commonjs sharp'
  },
  // For Webpack 5+ only, add `node-polyfill-webpack-plugin`
  plugins: [
    new (require('node-polyfill-webpack-plugin'))()
  ]
}

Next.js

For Next.js, you can insert into next.config.js

{
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.resolve.fallback = {
        fs: false
      }
      config.externals = {
        ...config.externals,
        'xxhash-addon': 'commonjs xxhash-addon',
        'bson-ext': 'commonjs bson-ext',
        'shelljs': 'commonjs shelljs',
        'lzma-native': 'commonjs lzma-native',
        'sharp': 'commonjs sharp'
      }
      config.plugins = [
        ...config.plugins,
        new (require('node-polyfill-webpack-plugin'))()
      ]
    }
    return config;
  }
}

Tests

Tests are written in Mocha, to run use npm test.

License

Etomon Encode Tools is licensed under the GNU LGPL-3.0, a copy of which can be found at https://www.gnu.org/licenses/.

3.0.1

2 years ago

3.0.0

2 years ago

2.7.8

2 years ago

2.7.2

3 years ago

2.7.1

3 years ago

2.7.4

3 years ago

2.7.3

3 years ago

2.7.6

3 years ago

2.7.5

3 years ago

2.7.7

3 years ago

2.7.0

3 years ago

2.5.0

3 years ago

2.6.1

3 years ago

2.5.1

3 years ago

2.4.0

3 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.1.0

3 years ago

2.0.11

3 years ago

2.0.7

3 years ago

2.0.9

3 years ago

2.0.10

3 years ago

2.0.8

3 years ago

2.0.5

3 years ago

2.0.6

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago