diff --git a/javascript/src/iframe/stringUtilities.js b/javascript/src/iframe/stringUtilities.js new file mode 100644 index 0000000000000000000000000000000000000000..a3691305e3b9de6c1e22b688d489fb7715902f7a --- /dev/null +++ b/javascript/src/iframe/stringUtilities.js @@ -0,0 +1,53 @@ +export function stringToUtf8ByteArray(str) { + if (typeof str !== 'string') { + str = str.toString(); + } + const res = Buffer.from(str, 'utf-8'); + return res; +} + +export function utf8ByteArrayToString(ba) { + if (!Buffer.isBuffer(ba)) { + ba = Buffer.from(ba); + } + const res = ba.toString('utf-8'); + return res; +} + +export function stringToUtf8Base64(str) { + if (!Buffer.isBuffer(str)) { + if (typeof str !== 'string') { + str = str.toString(); + } + str = Buffer.from(str, 'utf-8'); + } + const res = str.toString('base64'); + return res; +} + +export function utf8Base64ToString(strBase64) { + if (!Buffer.isBuffer(strBase64)) { + if (typeof strBase64 !== 'string') { + strBase64 = strBase64.toString(); + } + strBase64 = Buffer.from(strBase64, 'base64'); + } + const res = strBase64.toString('utf-8'); + return res; +} + +export function base64ToByteArray(strBase64) { + if (typeof strBase64 !== 'string') { + strBase64 = strBase64.toString(); + } + const res = Buffer.from(strBase64, 'base64'); + return res; +} + +export function byteArrayToBase64(ba) { + if (!Buffer.isBuffer(ba)) { + ba = Buffer.from(ba); + } + const res = ba.toString('base64'); + return res; +} diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js index 5840a699fb30461dc22447e3d090bbaffc6b5636..4fb851d638ac0139490b6f8a4a7a99f67d4610cb 100644 --- a/javascript/src/iframe/viamapi-iframe.js +++ b/javascript/src/iframe/viamapi-iframe.js @@ -1,4 +1,12 @@ import { parseSMIME } from '../utilities/emailUtilities'; +import { + stringToUtf8ByteArray, + utf8ByteArrayToString, + stringToUtf8Base64, + utf8Base64ToString, + base64ToByteArray, + byteArrayToBase64 +} from '../utilities/stringUtilities'; const QRCode = require('qrcode'); const Penpal = require('penpal').default; @@ -434,60 +442,6 @@ function getCertificateForPassport(passportUUID, internal) { }); } -function stringToUtf8ByteArray(str) { - if (typeof str !== 'string') { - str = str.toString(); - } - const res = Buffer.from(str, 'utf-8'); - return res; -} - -function utf8ByteArrayToString(ba) { - if (!Buffer.isBuffer(ba)) { - ba = Buffer.from(ba); - } - const res = ba.toString('utf-8'); - return res; -} - -function stringToUtf8Base64(str) { - if (!Buffer.isBuffer(str)) { - if (typeof str !== 'string') { - str = str.toString(); - } - str = Buffer.from(str, 'utf-8'); - } - const res = str.toString('base64'); - return res; -} - -function utf8Base64ToString(strBase64) { - if (!Buffer.isBuffer(strBase64)) { - if (typeof strBase64 !== 'string') { - strBase64 = strBase64.toString(); - } - strBase64 = Buffer.from(strBase64, 'base64'); - } - const res = strBase64.toString('utf-8'); - return res; -} - -function base64ToByteArray(strBase64) { - if (typeof strBase64 !== 'string') { - strBase64 = strBase64.toString(); - } - const res = Buffer.from(strBase64, 'base64'); - return res; -} - -function byteArrayToBase64(ba) { - if (!Buffer.isBuffer(ba)) { - ba = Buffer.from(ba); - } - const res = ba.toString('base64'); - return res; -} - const connection = Penpal.connectToParent({ // Methods child is exposing to parent methods: {