function decode(base64) { // Add removed at end '=' base64 += Array(5 - base64.length % 4).join('='); base64 = base64 .replace(/\-/g, '+') // Convert '-' to '+' .replace(/\_/g, '/'); // Convert '_' to '/' return new Buffer(base64, 'base64'); }
...
throw err;
return;
};
randomURLSafeBase64 = URLSafeBase64.encode(buf);
});
```
### .decode(string)
Decodes a URL Safe Base64 string as a buffer.
``` javascript
var someURLSafeBase64 = '';
URLSafeBase64.decode(someURLSafeBase64); // returns a buffer
```
...
function encode(buffer) { return buffer.toString('base64') .replace(/\+/g, '-') // Convert '+' to '-' .replace(/\//g, '_') // Convert '/' to '_' .replace(/=+$/, ''); // Remove ending '=' }
...
Require it within your module:
``` javascript
var URLSafeBase64 = require('urlsafe-base64');
```
### .encode(buffer)
Encodes a buffer as a URL Safe Base64 string. This function encodes to
the RFC 4648 Spec where '+' is encoded as '-' and '/' is encoded as '_'.
The padding character '=' is removed.
``` javascript
var randomURLSafeBase64;
...
function validate(base64) { return /^[A-Za-z0-9\-_]+$/.test(base64); }
...
Decodes a URL Safe Base64 string as a buffer.
``` javascript
var someURLSafeBase64 = '';
URLSafeBase64.decode(someURLSafeBase64); // returns a buffer
```
### .validate(string)
Validates a string if it is URL Safe Base64 encoded.
``` javascript
var validURLSafeBase64 = '';
URLSafeBase64.validate(validURLSafeBase64); // returns true
...
function decode(base64) { // Add removed at end '=' base64 += Array(5 - base64.length % 4).join('='); base64 = base64 .replace(/\-/g, '+') // Convert '-' to '+' .replace(/\_/g, '/'); // Convert '_' to '/' return new Buffer(base64, 'base64'); }
...
throw err;
return;
};
randomURLSafeBase64 = URLSafeBase64.encode(buf);
});
```
### .decode(string)
Decodes a URL Safe Base64 string as a buffer.
``` javascript
var someURLSafeBase64 = '';
URLSafeBase64.decode(someURLSafeBase64); // returns a buffer
```
...
function encode(buffer) { return buffer.toString('base64') .replace(/\+/g, '-') // Convert '+' to '-' .replace(/\//g, '_') // Convert '/' to '_' .replace(/=+$/, ''); // Remove ending '=' }
...
Require it within your module:
``` javascript
var URLSafeBase64 = require('urlsafe-base64');
```
### .encode(buffer)
Encodes a buffer as a URL Safe Base64 string. This function encodes to
the RFC 4648 Spec where '+' is encoded as '-' and '/' is encoded as '_'.
The padding character '=' is removed.
``` javascript
var randomURLSafeBase64;
...
function validate(base64) { return /^[A-Za-z0-9\-_]+$/.test(base64); }
...
Decodes a URL Safe Base64 string as a buffer.
``` javascript
var someURLSafeBase64 = '';
URLSafeBase64.decode(someURLSafeBase64); // returns a buffer
```
### .validate(string)
Validates a string if it is URL Safe Base64 encoded.
``` javascript
var validURLSafeBase64 = '';
URLSafeBase64.validate(validURLSafeBase64); // returns true
...