mirror of
https://github.com/thomasnordquist/MQTT-Explorer.git
synced 2026-09-12 01:23:31 +00:00
31 lines
767 B
TypeScript
31 lines
767 B
TypeScript
const { Base64 } = require('js-base64')
|
|
|
|
export class Base64Message {
|
|
private base64Message: string
|
|
private unicodeValue: string
|
|
|
|
public length: number
|
|
|
|
private constructor(base64Str: string) {
|
|
this.base64Message = base64Str
|
|
this.unicodeValue = Base64.decode(base64Str)
|
|
this.length = base64Str.length
|
|
}
|
|
|
|
public static toUnicodeString(message: Base64Message) {
|
|
return message.unicodeValue || ''
|
|
}
|
|
|
|
public static fromBuffer(buffer: Buffer) {
|
|
return new Base64Message(buffer.toString('base64'))
|
|
}
|
|
|
|
public static fromString(str: string) {
|
|
return new Base64Message(Base64.encode(str))
|
|
}
|
|
|
|
public static toDataUri(message: Base64Message, mimeType: string) {
|
|
return `data:${mimeType};base64,${message.base64Message}`
|
|
}
|
|
}
|