-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 圧縮処理をpakoからCompression Streams APIに移行
- Loading branch information
Showing
4 changed files
with
7 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,6 @@ | |
"jszip", | ||
"lzbase62", | ||
"msgpack-lite", | ||
"pako", | ||
"skyway-js", | ||
"vkbeautify" | ||
] | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |