ostrio:base64

v2.0.1Published 6 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Isomorphic Base64 implementation

Highly efficient isomorphic implementation of Base64 string encoding and decoding. With the support of Unicode, and non-blocking execution via WebWorker. This library has 100% tests coverage, including speed tests.

Features

  1. 100% tests coverage
  2. Isomorphic, same API for Server and Browser
  3. Blazing fast, see speed tests
  4. Non-blocking browser experience, via WebWorkers
  5. No external dependencies
  6. Could replace default Meteor's base64 package

Installation

meteor add ostrio:base64

ES6 Import

1import { Base64 } from 'meteor/ostrio:base64';

Native code support

Native code is disabled by default for both NodeJS and browser. Native code represented as atob, btoa (with extension to support Unicode) in a browser, and Buffer at NodeJS.

Although native code is 10x times faster, its support is disabled, as natively base64 encoding supports only ASCII symbols in a Browser and Node.js. To enable native code - use constructor in next form:

1// Note - first "b" (lowercase)
2import { base64 } from 'meteor/ostrio:base64';
3const nativeB64 = new base64({ useNative: true });

Non-blocking via WebWorker

WebWorker is enabled by default, for all encode/decode calls with the callback. WebWorker is used only if supported by a browser, otherwise, it will fall-back to the main thread. In the real-world application WebWorker, usage will gain you extra FPS, and UI will act more smoothly.

API

.encode()

1Base64.encode(plainString [, callback]);
1Base64.encode('My Plain String'); // Returns 'TXkgUGxhaW4gU3RyaW5n'
2
3// Async, non-blocking via WebWorker (if supported) at browser:
4Base64.encode('My Plain String', (error, b64) => {
5  // b64 === 'TXkgUGxhaW4gU3RyaW5n'
6});

.decode()

1Base64.decode(base64EncodedString [, callback]);
1Base64.decode('TXkgUGxhaW4gU3RyaW5n'); // Returns 'My Plain String'
2
3// Async, non-blocking via WebWorker (if supported) at browser:
4Base64.decode('TXkgUGxhaW4gU3RyaW5n', (error, str) => {
5  // str === 'My Plain String'
6});

Constructor new base64()

1new base64({ allowWebWorker, useNative, supportNonASCII, ejsonCompatible });
  • opts.allowWebWorker {Boolean} - Default: true. Use WebWorker in a Browser if available;
  • opts.useNative {Boolean} - Default: false. Use native atob, btoa and Buffer.from, if available;
  • opts.supportNonASCII {Boolean} - Default: true. Decreases speed, but gives support for whole utf-8 table;
  • opts.ejsonCompatible {Boolean} - Default: false. Compatible mode with EJSON "binary" format, .encode() method will result as Uint8Array if ejsonCompatible is true.
1// Note - first "b" (lowercase)
2import { base64 } from 'meteor/ostrio:base64';
3// Native with WebWorker
4const nativeB64 = new base64({ allowWebWorker: true, useNative: true });
5
6// Native without WebWorker
7const mtNativeB64 = new base64({ allowWebWorker: false, useNative: true });
8
9// Use main thread, no WebWorker
10const mtB64 = new base64({ allowWebWorker: false });

Default base64 package replacement

  1. Download base64-replacement package and place into meteor-app/packages directory, that's it. Run meteor update to make sure new package is applied'
  2. In case of version incompatibility, change base64-replacement version, to the latest available on the mainstream channel;
  3. For more info see base64-replacement package

100% Tests coverage

To run built-in tests clone this repository and run:

meteor test-packages ./

Tests include synchronous, asynchronous and speed tests for Browser and NodeJS, for cases with/out the Native code and/or WebWorker usage.

Support this project:

This project wouldn't be possible without ostr.io.

Using ostr.io you are not only protecting domain names, monitoring websites and servers, using Prerendering for better SEO of your JavaScript website, but support our Open Source activity, and great packages like this one could be available for free.