ostrio:base64

v1.1.0Published 8 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

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

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) at browser, and Buffer at NodeJS. Both versions of new Buffer via Constructor and Buffer.from is supported for node >= 7.*.

Native code support is disabled - as tests indicates up to 3x slower execution, than JS-implementation. To run tests - clone this repository and run meteor test-packages ./. To enable native code - use constructor in next form:

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

Non-blocking via WebWorker

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

API

Base64.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});

Base64.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([allowWebWorker, useNative])

1// Note - first "b" (lowercase)
2import { base64 } from 'meteor/ostrio:base64';
3// Native with WebWorker
4const nativeB64 = new base64(true, true);
5
6// Native without WebWorker
7const mtNativeB64 = new base64(false, true);
8
9// Use main thread, no WebWorker
10const mtB64 = new base64(false);

100% Tests coverage

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

meteor test-packages ./

Tests includes synchronous, asynchronous and speed tests for Browser and NodeJS, for cases with/out Native code and/or WebWoker usage.