PouchDB utilities for operating on binary strings and Buffers/Blobs
npx @tessl/cli install tessl/npm-pouchdb-binary-utils@9.0.0PouchDB Binary Utils provides cross-platform utilities for operating on binary strings, Buffers, and Blobs within the PouchDB ecosystem. It handles binary data conversion between different formats (base64, binary strings, ArrayBuffers, Blobs, Buffers) with environment-specific optimizations for both browser and Node.js environments.
Note: This package is conceptually an internal API used by PouchDB and its plugins. It is part of the PouchDB monorepo and does not follow semantic versioning, with versions pegged to PouchDB releases.
npm install pouchdb-binary-utils --save-exactimport {
atob,
btoa,
base64StringToBlobOrBuffer,
binaryStringToArrayBuffer,
binaryStringToBlobOrBuffer,
blob,
blobOrBufferToBase64,
blobOrBufferToBinaryString,
readAsArrayBuffer,
readAsBinaryString,
typedBuffer
} from "pouchdb-binary-utils";For CommonJS:
const {
atob,
btoa,
base64StringToBlobOrBuffer,
binaryStringToArrayBuffer,
binaryStringToBlobOrBuffer,
blob,
blobOrBufferToBase64,
blobOrBufferToBinaryString,
readAsArrayBuffer,
readAsBinaryString,
typedBuffer
} = require("pouchdb-binary-utils");import { atob, btoa, blobOrBufferToBase64, typedBuffer } from "pouchdb-binary-utils";
// Convert between base64 and binary strings
const base64Data = "SGVsbG8gV29ybGQ=";
const binaryString = atob(base64Data); // "Hello World"
const backToBase64 = btoa(binaryString); // "SGVsbG8gV29ybGQ="
// Create typed buffer
const buffer = typedBuffer("Hello", "binary", "text/plain");
// Convert buffer to base64 (async)
blobOrBufferToBase64(buffer, (base64Result) => {
console.log(base64Result); // Base64 encoded string
});PouchDB Binary Utils is designed around environment-specific implementations:
Standard base64 encoding and decoding with cross-platform implementations.
/**
* Decodes a base64-encoded string to binary string
* @param {string} str - Base64 encoded string
* @returns {string} Binary string
*/
function atob(str);
/**
* Encodes a binary string to base64
* @param {string} str - Binary string
* @returns {string} Base64 encoded string
*/
function btoa(str);Converting binary strings to other formats for cross-platform data handling.
/**
* Converts binary string to ArrayBuffer (browser only)
* @param {string} binaryString - Binary string data
* @returns {ArrayBuffer} ArrayBuffer containing the binary data
*/
function binaryStringToArrayBuffer(binaryString);
/**
* Converts binary string to Blob/Buffer with specified MIME type
* @param {string} binaryString - Binary string data
* @param {string} type - MIME type string
* @returns {Buffer|Blob} Buffer (Node.js) or Blob (browser)
*/
function binaryStringToBlobOrBuffer(binaryString, type);Converting base64 strings directly to Blob/Buffer objects.
/**
* Converts base64 string to Blob/Buffer with specified MIME type
* @param {string} b64 - Base64 encoded string
* @param {string} type - MIME type string
* @returns {Buffer|Blob} Buffer (Node.js) or Blob (browser)
*/
function base64StringToBlobOrBuffer(b64, type);Creating and manipulating binary objects across environments.
/**
* Creates Blob objects with cross-platform compatibility
* @param {Array} parts - Array of data parts
* @param {Object} properties - Blob properties including type
* @param {string} [properties.type] - MIME type string
* @param {string} [properties.endings] - Line ending normalization ('transparent' or 'native')
* @returns {Blob|Function} Blob (browser) or empty function (Node.js)
*/
function blob(parts, properties);
/**
* Creates Buffer with optional type property (Node.js only)
* @param {string} binString - Binary string data
* @param {string} buffType - Either 'binary' or 'base64'
* @param {string} type - MIME type to assign as property
* @returns {Buffer|undefined} Buffer with type property (Node.js) or undefined (browser)
*/
function typedBuffer(binString, buffType, type);Converting binary objects back to string formats via callbacks.
/**
* Converts Blob/Buffer to base64 string via callback
* @param {Blob|Buffer} blobOrBuffer - Binary data object
* @param {function} callback - Callback function receiving base64 string
* @param {string} callback.result - The base64 encoded result
*/
function blobOrBufferToBase64(blobOrBuffer, callback);
/**
* Converts Blob/Buffer to binary string via callback (browser only)
* @param {Blob|Buffer} blobOrBuffer - Binary data object
* @param {function} callback - Callback function receiving binary string
* @param {string} callback.result - The binary string result
*/
function blobOrBufferToBinaryString(blobOrBuffer, callback);Reading Blob objects via FileReader API (browser only).
/**
* Reads Blob as ArrayBuffer via callback using FileReader (browser only)
* @param {Blob} blob - Blob object to read
* @param {function} callback - Callback function receiving ArrayBuffer
* @param {ArrayBuffer} callback.result - The ArrayBuffer result
*/
function readAsArrayBuffer(blob, callback);
/**
* Reads Blob as binary string via callback using FileReader (browser only)
* @param {Blob} blob - Blob object to read
* @param {function} callback - Callback function receiving binary string
* @param {string} callback.result - The binary string result
*/
function readAsBinaryString(blob, callback);/**
* @typedef {Object} BlobPropertyBag
* @property {string} [type] - MIME type string
* @property {string} [endings] - Line ending normalization ('transparent' or 'native')
*/Buffer objects for binary dataatob/btoa using Buffer.from() with validationreadAsArrayBuffer, readAsBinaryString) are exported but will throw errors if used (browser-only functionality)blob() function returns empty function since Blobs are browser-specifictypedBuffer() function creates Buffer objects with type propertyBlob objects for binary dataatob/btoa functions when availablebinaryStringToArrayBuffer available for ArrayBuffer conversiontypedBuffer() function returns undefined (no-op in browser)The package includes validation for base64 strings:
atob() throws an Error if the input is not a valid base64 string--save-exact flag as it follows PouchDB versioning, not semantic versioning