Codec Module

The codec module provides codec-related helper methods.

Basic-Auth Header

To generate basic authentication header for use in XHR requests, use the basic() method.

Usage: basic(username, password), where:

  • username: username to use for the basic auth
  • password: password to use for the basic auth
const basicAuth = require('codec/auth');
console.log(basicAuth.basic('many', 'blocks')); // Basic: bWFueTpibG9ja3M=

Encode Base64

To convert a text string into a base64-compliant string, use the btoa() method.

usage: btoa(unencoded), where:

  • unencoded: input string to be encoded
const base64Codec = require('codec/base64');
console.log(base64Codec.btoa('hello')); // aGVsbG8=

Decode Base64

To convert a base64 string into a text string, use the decode() method.

usage: atob(encoded), where:

  • encoded: input string to be decoded.
const base64Codec = require('codec/base64');
console.log(base64Codec.atob('aGVsbG8=')); // hello

Encode String

To encode string to be URL-safe, use the encodeString() method.

usage: encodeString(input), where:

  • input: string to be URL-encoded
const base64Codec = require('codec/base64');
console.log(base64Codec.encodeString('+')); // _

Parse Query String

To convert a stringified parameters list to a JavaScript object, use the parse() method.

usage: parse(query_string, defaults), where:

  • query_string: input query string
  • defaults: default values to be used if the value is missing in the query_string
const queryStringCodec = require('codec/query_string');
console.log(queryStringCodec.parse('a=5&b=10', {c: 15})); // {a: 5, b: 10, c: 15}

Stringify Query String

To stringify a query string, use the stringify() method.

usage: stringify(params), where:

  • params: object of key/value items to stringify as query string
const queryStringCodec = require('codec/query_string');
console.log(queryStringCodec.stringify({ a: 10, b: 15 })); // a=10&b=15
Functions support

Functions provides a rich set of tools, and this documentation does not cover all of the potential situations you may encounter. If you need help with a situation not covered by the documentation, please contact PubNub Support

Last updated on