UNPKG

@truffle/dashboard-message-bus-common

Version:

Message types that are used when communicating with the Truffle dashboard message bus

31 lines 1.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMessage = exports.base64ToJson = exports.jsonToBase64 = void 0; const buffer_1 = require("buffer"); /** * Convert any JS object or value to a base64 representation of it */ const jsonToBase64 = (json) => { const stringifiedJson = JSON.stringify(json); const buffer = buffer_1.Buffer.from(stringifiedJson); const base64 = buffer.toString("base64"); return base64; }; exports.jsonToBase64 = jsonToBase64; /** * Convert the base64 representation of a JS object or value to its JS representation * @dev This is the reverse of `jsonToBase64` and is not expected to work with other base64 formats */ const base64ToJson = (base64) => { const buffer = buffer_1.Buffer.from(base64, "base64"); const stringifiedJson = buffer.toString("utf8"); const json = JSON.parse(stringifiedJson); return json; }; exports.base64ToJson = base64ToJson; const createMessage = (type, payload) => { const id = Math.random(); return { id, type, payload }; }; exports.createMessage = createMessage; //# sourceMappingURL=utils.js.map