From c2e134a2296344c26017fd4bd57bf530c6729002 Mon Sep 17 00:00:00 2001 From: Camden Mecklem Date: Thu, 8 Aug 2024 19:37:19 -0400 Subject: [PATCH] replace fflate with native APIs --- package-lock.json | 7 ------- package.json | 1 - src/helpers/imports.ts | 40 +++++++++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0c573da..a69b284 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "1.0.0", "dependencies": { "bootstrap-icons-vue": "^1.11.3", - "fflate": "^0.8.2", "topster": "^5.3.0", "vue": "^3.4.33 ", "vuex": "^4.1.0" @@ -2503,12 +2502,6 @@ "reusify": "^1.0.4" } }, - "node_modules/fflate": { - "version": "0.8.2", - "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz", - "integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==", - "license": "MIT" - }, "node_modules/file-entry-cache": { "version": "6.0.1", "dev": true, diff --git a/package.json b/package.json index 79231e3..9440bcd 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,6 @@ }, "dependencies": { "bootstrap-icons-vue": "^1.11.3", - "fflate": "^0.8.2", "topster": "^5.3.0", "vue": "^3.4.33 ", "vuex": "^4.1.0" diff --git a/src/helpers/imports.ts b/src/helpers/imports.ts index 05a2d14..9a5d0d6 100644 --- a/src/helpers/imports.ts +++ b/src/helpers/imports.ts @@ -1,12 +1,25 @@ // Functions related to importing and exporting charts -import { zlibSync, unzlibSync } from 'fflate' import { appendChart, findByUuid, getActiveChart, getActiveChartUuid, getNewestChartUuid, setActiveChart, updateStoredChart } from './localStorage' import { BackgroundTypes, ChartItem, StoredChart, StoredCharts } from '../types' import { Store } from 'vuex' import { State } from '../store' import { forceRefresh } from './chart' +const unzlib = async (data: Uint8Array) => { + const stream = new Response(data).body + .pipeThrough(new DecompressionStream('deflate')) + + return new Uint8Array(await new Response(stream).arrayBuffer()); +} + +const zlib = async (data: Uint8Array) => { + const stream = new Response(data).body + .pipeThrough(new CompressionStream('deflate')) + + return new Uint8Array(await new Response(stream).arrayBuffer()); +} + const downloadChartData = (data: string, title: string, timestamp: number) => { const blob = new Blob([data]) @@ -22,24 +35,32 @@ const downloadChartData = (data: string, title: string, timestamp: number) => { link.remove() } -export const exportCurrentChart = () => { +export const exportCurrentChart = async () => { const uuid = getActiveChartUuid() const exportObj: StoredCharts = { [uuid]: getActiveChart() } - const compressed = btoa(zlibSync(new TextEncoder().encode(JSON.stringify(exportObj))).toString()) + const str = JSON.stringify(exportObj) + const arr = new TextEncoder().encode(str) + const zlibbed = await zlib(arr) + + const compressed = btoa(zlibbed.toString()) downloadChartData(compressed, exportObj[uuid].data.title, exportObj[uuid].timestamp) } -export const parseUploadedText = (text: string) => { +export const parseUploadedText = async (text: string) => { const uintArray = Uint8Array.from(atob(text).split(',').map(num => parseInt(num))) const textDecoder = new TextDecoder() - return textDecoder.decode(unzlibSync(uintArray)) + const unzlibbed = await unzlib(uintArray) + + const decoded = textDecoder.decode(unzlibbed) + + return decoded } export const importChart = async (event: Event, store: Store) => { @@ -47,7 +68,7 @@ export const importChart = async (event: Event, store: Store) => { try { const text = await files[0].text() - const results = parseUploadedText(text) + const results = await parseUploadedText(text) const json = JSON.parse(results) as StoredCharts const newChartUuid = Object.keys(json)[0] @@ -79,12 +100,12 @@ export const importChart = async (event: Event, store: Store) => { } } -export const importTopsters2 = (event: Event, store: Store) => { +export const importTopsters2 = async (event: Event, store: Store) => { if (event.target === null) return const files = (event.target as HTMLInputElement).files if (files === null) return const fileReader = new FileReader() - fileReader.addEventListener('load', () => { + fileReader.addEventListener('load', async () => { try { // Topsters 2 exports have their charcodes shifted up // 17 points, and then are encoded in base64. This @@ -155,7 +176,8 @@ export const importTopsters2 = (event: Event, store: Store) => { // Chart cards are compressed with zlib + encoded with base64 const chartCards = charts[prefix + 'cards'] // Get base64 string const cardsCompressed = Uint8Array.from(atob(chartCards.substring(1, chartCards.length - 1)), c => c.charCodeAt(0)) // Convert base64 to bytes - const cardsDecompressed = textDecoder.decode(unzlibSync(cardsCompressed)) // Decompress and convert to text + const unzlibbed = await unzlib(cardsCompressed) + const cardsDecompressed = textDecoder.decode(unzlibbed) // Decompress and convert to text const cards = JSON.parse(cardsDecompressed) // Parse cards // Create chart items