@vara/custom-logic-sdk
Version:
Server Side JavaScript SDK for Custom Business Logic
38 lines (28 loc) • 984 B
JavaScript
/**
* Created by stevenchin on 2/2/17.
*/
const CHAR_ENCODINGS = require('../constants/constants').CUSTOM_LOGIC.FUNCTIONS.CHAR_ENCODINGS;
const protocolHandler = {};
const protocolHandlers = {};
/**
* Encodes body parameter for transport. Returns the encoded body and the encoding used
* @param body {*} - body to encode, it can be a string, buffer, object, etc.
* @returns {{encoding, body}}
*/
protocolHandler.getEncodedBody = function getEncodedBody(body) {
let data = body;
let encoding = CHAR_ENCODINGS.UTF8;
if (data instanceof Buffer) {
encoding = CHAR_ENCODINGS.BASE64;
data = data.toString(encoding);
}
// TODO: handle a stream
return { encoding, body: data };
};
protocolHandler.getHandler = function getHandler(protocolVersion) {
return protocolHandlers[protocolVersion];
};
protocolHandler.addHandler = function addHandler(protocolVersion, handler) {
protocolHandlers[protocolVersion] = handler;
};
module.exports = protocolHandler;