Skip to content

Commit

Permalink
feat: 圧縮処理をpakoからCompression Streams APIに移行
Browse files Browse the repository at this point in the history
  • Loading branch information
TK11235 committed Sep 4, 2024
1 parent fed2e49 commit a3b1d63
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 67 deletions.
1 change: 0 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"jszip",
"lzbase62",
"msgpack-lite",
"pako",
"skyway-js",
"vkbeautify"
]
Expand Down
24 changes: 0 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"jszip": "^3.10.1",
"lzbase62": "^2.0.0",
"msgpack-lite": "^0.1.26",
"pako": "^2.1.0",
"rxjs": "~7.8.0",
"skyway-js": "^4.4.5",
"tslib": "^2.3.0",
Expand All @@ -47,7 +46,6 @@
"@types/js-yaml": "^4.0.5",
"@types/lzbase62": "^2.0.0",
"@types/msgpack-lite": "^0.1.8",
"@types/pako": "^2.0.0",
"@types/vkbeautify": "^0.99.2",
"jasmine-core": "~5.1.0",
"karma": "~6.4.0",
Expand Down
47 changes: 7 additions & 40 deletions src/app/class/core/system/util/compress.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
import Pako from 'pako';

import { setZeroTimeout } from './zero-timeout';

export async function compressAsync(data: Uint8Array, chunkSize?: number): Promise<Uint8Array> {
const deflate = new Pako.Deflate({ level: 2, gzip: true });

try {
await processAsync(deflate, data, chunkSize);
return deflate.result as Uint8Array;
} catch (e) {
console.error(e);
}
return null;
}

export async function decompressAsync(data: Uint8Array, chunkSize?: number): Promise<Uint8Array> {
const inflate = new Pako.Inflate();

try {
await processAsync(inflate, data, chunkSize);
return inflate.result as Uint8Array;
} catch (e) {
console.error(e);
}
return null;
export async function compressAsync(data: Uint8Array): Promise<Uint8Array> {
return processAsync(new CompressionStream('gzip'), data);
}

async function processAsync<T extends Pako.Deflate | Pako.Inflate>(pakoObj: T, data: Uint8Array, chunkSize: number = 16 * 1024): Promise<T> {
let total = Math.ceil(data.byteLength / chunkSize);
let sliceData: Uint8Array = null;

for (let sliceIndex = 0; sliceIndex < total; sliceIndex++) {
await waitTickAsync();
sliceData = data.slice(sliceIndex * chunkSize, (sliceIndex + 1) * chunkSize);
pakoObj.push(sliceData, total <= sliceIndex + 1);
}

if (pakoObj.err) throw new Error(`error: ${pakoObj.err}`);
return pakoObj;
export async function decompressAsync(data: Uint8Array): Promise<Uint8Array> {
return processAsync(new DecompressionStream('gzip'), data);
}

function waitTickAsync(): Promise<void> {
return new Promise(resolve => setZeroTimeout(resolve));
async function processAsync(transform: ReadableWritablePair, data: Uint8Array): Promise<Uint8Array> {
const stream = new Blob([data]).stream().pipeThrough(transform);
return new Uint8Array(await new Response(stream).arrayBuffer());
}

0 comments on commit a3b1d63

Please sign in to comment.