Fix base64 encoding on Node.js

Browser-based base64 encoding uses `window.btoa()`, which isn't available server-side. Detect when `window.btoa()` is unavailable and use `new Buffer(str).toString('base64')` as a fallback. As long as the execution environment is either a browser or Node.js, this should work fine.