2022-08-30 16:13:11 +05:00
|
|
|
/* This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2022 Streetwriters (Private) Limited
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-21 13:41:08 +05:00
|
|
|
import { decode, encode } from "base64-arraybuffer";
|
2021-12-22 09:54:34 +05:00
|
|
|
import { compressSync, decompressSync, strToU8, strFromU8 } from "fflate";
|
2021-12-21 13:41:08 +05:00
|
|
|
|
|
|
|
|
/**
|
2021-12-21 14:31:11 +05:00
|
|
|
*
|
2021-12-21 13:41:08 +05:00
|
|
|
* @param {string} data
|
|
|
|
|
* @returns {string | null} An object containing compressed data
|
|
|
|
|
*/
|
|
|
|
|
export const compress = (data) => {
|
|
|
|
|
try {
|
2021-12-21 14:31:11 +05:00
|
|
|
return encode(compressSync(strToU8(data)).buffer);
|
2021-12-21 13:41:08 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2021-12-21 14:31:11 +05:00
|
|
|
*
|
|
|
|
|
* @param {string} compressed
|
2021-12-21 13:41:08 +05:00
|
|
|
* @returns {string} decompressed string
|
|
|
|
|
*/
|
|
|
|
|
export const decompress = (compressed) => {
|
2021-12-21 14:31:11 +05:00
|
|
|
return strFromU8(decompressSync(new Uint8Array(decode(compressed))));
|
2021-12-21 13:41:08 +05:00
|
|
|
};
|