Codec Module
The codec
module provides essential Base64, Basic Auth, and query‑string utilities in PubNub Functions. Use these helpers to transform data and authenticate calls in real‑time workflows.
Basic Auth header (basic)
Generate a Basic Authentication header for XHR
requests with basic()
.
Usage: basic(username, password)
username
: Basic Auth usernamepassword
: Basic Auth password
Use when your Function calls third‑party APIs that require Basic Auth.
const basicAuth = require('codec/auth');
console.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
Use to carry binary or mixed‑charset data over JSON.
const base64Codec = require('codec/base64');
console.log(base64Codec.btoa('hello')); // aGVsbG8=
Decode Base64 (atob)
Convert a Base64 string to text with atob()
.
Usage: atob(encoded)
encoded
: input string to decode
Use to read Base64 payloads received from external services.
const base64Codec = require('codec/base64');
console.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
Use to safely place values in query parameters.
const base64Codec = require('codec/base64');
console.log(base64Codec.encodeString('+')); // _
Parse query string (parse)
Convert a query string to a JavaScript object with parse()
.
Usage: parse(queryString, defaults)
queryString
: input query stringdefaults
: default values used when keys are missing
Use to read incoming query parameters into a typed object.
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 (stringify)
Create a query string from an object with stringify()
.
Usage: stringify(params)
params
: object of key/value items to stringify
Use to build outbound URLs with query parameters.
const queryStringCodec = require('codec/query_string');
console.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.