On this page

Codec Module

The codec module provides helpers for Basic Auth, Base64, and query‑string utilities in PubNub Functions.

Basic Auth header (basic)

Generate a Basic Authentication header for XHR requests with basic().

Usage: basic(username, password)

  • username: Basic Auth username
  • password: Basic Auth password
1const basicAuth = require('codec/auth');
2console.log(basicAuth.basic('many', 'blocks')); // Basic: bWFueTpibG9ja3M=

Encode Base64 (btoa)

Convert a text string to Base64 with btoa().

Usage: btoa(unencoded)

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

Decode Base64 (atob)

Convert a Base64 string to text with atob().

Usage: atob(encoded)

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

Encode string (URL‑safe)

Encode a string to be URL‑safe with encodeString().

Usage: encodeString(input)

  • input: string to URL‑encode
1const base64Codec = require('codec/base64');
2console.log(base64Codec.encodeString('+')); // _

Parse query string (parse)

Convert a query string to a JavaScript object with parse().

Usage: parse(queryString, defaults)

  • queryString: input query string
  • defaults: default values used when keys are missing
1const queryStringCodec = require('codec/query_string');
2console.log(queryStringCodec.parse('a=5&b=10', {c: 15})); // {a: 5, b: 10, c: 15}

Stringify query string (stringify)

Create a query string from an object with stringify().

Usage: stringify(params)

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

If you need help with a scenario not covered here, contact PubNub Support.

Last updated on