1- import { deflateSync , inflateSync } from 'node:zlib'
1+ import { promisify } from 'node:util'
2+ import { deflate , inflate } from 'node:zlib'
23import {
34 UNIFIED_DISPLAY_WAVEFORM_CACHE_VERSION ,
45 UNIFIED_DISPLAY_WAVEFORM_PARAMETER_VERSION ,
@@ -40,6 +41,8 @@ type UnifiedDisplayWaveformRow = {
4041
4142const TABLE = 'unified_display_waveform_cache'
4243const MAGIC = 'UDW1'
44+ const deflateAsync = promisify ( deflate )
45+ const inflateAsync = promisify ( inflate )
4346
4447const normalizeMeta = ( row : unknown ) : UnifiedDisplayWaveformMeta | null => {
4548 if ( ! isSqliteRow ( row ) ) return null
@@ -81,16 +84,16 @@ const toBuffer = (value: unknown): Buffer | null => {
8184 return null
8285}
8386
84- const writeHeader = ( payload : Buffer , header : Record < string , unknown > ) => {
87+ const writeHeader = async ( payload : Buffer , header : Record < string , unknown > ) => {
8588 const headerBuffer = Buffer . from ( JSON . stringify ( header ) , 'utf8' )
8689 const prefix = Buffer . alloc ( 8 )
8790 prefix . write ( MAGIC , 0 , 4 , 'ascii' )
8891 prefix . writeUInt32LE ( headerBuffer . byteLength , 4 )
89- return deflateSync ( Buffer . concat ( [ prefix , headerBuffer , payload ] ) )
92+ return await deflateAsync ( Buffer . concat ( [ prefix , headerBuffer , payload ] ) )
9093}
9194
92- const readPayload = ( compressed : Buffer ) => {
93- const decoded = inflateSync ( compressed )
95+ const readPayload = async ( compressed : Buffer ) => {
96+ const decoded = await inflateAsync ( compressed )
9497 if ( decoded . byteLength < 8 || decoded . subarray ( 0 , 4 ) . toString ( 'ascii' ) !== MAGIC ) return null
9598 const headerLength = decoded . readUInt32LE ( 4 )
9699 const payloadStart = 8 + headerLength
@@ -121,9 +124,9 @@ const readPayload = (compressed: Buffer) => {
121124 return { header, readBytes }
122125}
123126
124- function encodeUnifiedDisplayWaveformCacheData (
127+ async function encodeUnifiedDisplayWaveformCacheData (
125128 data : UnifiedDisplayWaveformDetailData
126- ) : Buffer | null {
129+ ) : Promise < Buffer | null > {
127130 if ( ! data || data . version !== UNIFIED_DISPLAY_WAVEFORM_CACHE_VERSION ) return null
128131 if ( data . parameterVersion !== UNIFIED_DISPLAY_WAVEFORM_PARAMETER_VERSION ) return null
129132 if ( ! data . height ?. length || ! data . attack ?. length || ! data . colorIndex ?. length ) return null
@@ -160,10 +163,10 @@ function encodeUnifiedDisplayWaveformCacheData(
160163 } )
161164}
162165
163- function decodeUnifiedDisplayWaveformCacheData (
166+ async function decodeUnifiedDisplayWaveformCacheData (
164167 meta : UnifiedDisplayWaveformMeta ,
165168 payload : Buffer
166- ) : UnifiedDisplayWaveformDetailData | null {
169+ ) : Promise < UnifiedDisplayWaveformDetailData | null > {
167170 if ( ! meta || ! payload ) return null
168171 if (
169172 meta . cacheVersion !== UNIFIED_DISPLAY_WAVEFORM_CACHE_VERSION ||
@@ -172,7 +175,7 @@ function decodeUnifiedDisplayWaveformCacheData(
172175 ) {
173176 return null
174177 }
175- const decoded = readPayload ( payload )
178+ const decoded = await readPayload ( payload )
176179 if ( ! decoded ) return null
177180 const height = decoded . readBytes ( decoded . header . heightLength )
178181 const attack = decoded . readBytes ( decoded . header . attackLength )
@@ -329,7 +332,7 @@ export async function loadUnifiedDisplayWaveformCacheData(
329332 await removeUnifiedDisplayWaveformCacheEntry ( listRoot , filePath )
330333 return null
331334 }
332- const decoded = decodeUnifiedDisplayWaveformCacheData ( meta , payload )
335+ const decoded = await decodeUnifiedDisplayWaveformCacheData ( meta , payload )
333336 if ( ! decoded ) {
334337 await removeUnifiedDisplayWaveformCacheEntry ( listRoot , filePath )
335338 return null
@@ -350,7 +353,7 @@ export async function upsertUnifiedDisplayWaveformCacheEntry(
350353 const db = getLibraryDb ( )
351354 const keys = resolveCacheKeys ( listRoot , filePath )
352355 if ( ! db || ! keys || ! data ) return false
353- const payload = encodeUnifiedDisplayWaveformCacheData ( data )
356+ const payload = await encodeUnifiedDisplayWaveformCacheData ( data )
354357 if ( ! payload ) return false
355358 try {
356359 const upsertMain = db . prepare (
0 commit comments