1111
1212import { XMLParser , XMLBuilder } from 'fast-xml-parser' ;
1313import { getZipEntriesFromAdapter , resolveGridsetPasswordFromEnv } from './password' ;
14- import { openZipFromInput , type ZipAdapter } from '../../utils/zip' ;
15- import { getNodeRequire , isNodeRuntime , type ProcessorInput } from '../../utils/io' ;
14+ import { type ProcessorInput } from '../../utils/io' ;
1615import { decodeText } from '../../utils/io' ;
16+ import { getZipAdapter , ZipAdapter , ZipFile } from '../../utils/zip' ;
1717
1818/**
1919 * Represents a single item in a wordlist
@@ -131,15 +131,13 @@ export function wordlistToXml(wordlist: WordList): string {
131131export async function extractWordlists (
132132 gridsetBuffer : Uint8Array ,
133133 password = resolveGridsetPasswordFromEnv ( ) ,
134- zipAdapter ?: ( input : ProcessorInput ) => Promise < { zip : ZipAdapter } >
134+ zipAdapter ?: ( input : ProcessorInput ) => Promise < ZipAdapter >
135135) : Promise < Map < string , WordList > > {
136136 const wordlists = new Map < string , WordList > ( ) ;
137137 const parser = new XMLParser ( ) ;
138138
139139 try {
140- const { zip } = zipAdapter
141- ? await zipAdapter ( gridsetBuffer )
142- : await openZipFromInput ( gridsetBuffer ) ;
140+ const zip = zipAdapter ? await zipAdapter ( gridsetBuffer ) : await getZipAdapter ( gridsetBuffer ) ;
143141 const entries = getZipEntriesFromAdapter ( zip , password ) ;
144142
145143 // Process each grid file
@@ -212,7 +210,8 @@ export async function updateWordlist(
212210 gridsetBuffer : Uint8Array ,
213211 gridName : string ,
214212 wordlist : WordList ,
215- password = resolveGridsetPasswordFromEnv ( )
213+ password = resolveGridsetPasswordFromEnv ( ) ,
214+ zipAdapter ?: ( input : ProcessorInput ) => Promise < ZipAdapter >
216215) : Promise < Uint8Array > {
217216 const parser = new XMLParser ( ) ;
218217 const builder = new XMLBuilder ( {
@@ -222,50 +221,9 @@ export async function updateWordlist(
222221 suppressEmptyNode : false ,
223222 } ) ;
224223
225- let entries : Array < {
226- entryName : string ;
227- getData : ( ) => Promise < Uint8Array > ;
228- } > ;
229- let saveZip : ( ( ) => Promise < Uint8Array > ) | null = null ;
230- let updateEntry : ( ( entryName : string , xml : string ) => void ) | null = null ;
231-
232- try {
233- if ( isNodeRuntime ( ) ) {
234- const AdmZip = getNodeRequire ( ) ( 'adm-zip' ) as typeof import ( 'adm-zip' ) ;
235- const zip = new AdmZip ( Buffer . from ( gridsetBuffer ) ) ;
236- entries = zip . getEntries ( ) . map ( ( entry ) => ( {
237- entryName : entry . entryName ,
238- getData : ( ) => Promise . resolve ( entry . getData ( ) ) ,
239- } ) ) ;
240- updateEntry = ( entryName : string , xml : string ) => {
241- zip . addFile ( entryName , Buffer . from ( xml , 'utf8' ) ) ;
242- } ;
243- saveZip = ( ) => Promise . resolve ( zip . toBuffer ( ) ) ;
244- } else {
245- const module = await import ( 'jszip' ) ;
246- const JSZip = module . default || module ;
247- const zip = await JSZip . loadAsync ( gridsetBuffer ) ;
248- entries = getZipEntriesFromAdapter (
249- {
250- listFiles : ( ) => Object . keys ( zip . files ) ,
251- readFile : async ( name : string ) => {
252- const file = zip . file ( name ) ;
253- if ( ! file ) {
254- throw new Error ( `Zip entry not found: ${ name } ` ) ;
255- }
256- return file . async ( 'uint8array' ) ;
257- } ,
258- } ,
259- password
260- ) ;
261- updateEntry = ( entryName : string , xml : string ) => {
262- zip . file ( entryName , xml , { binary : false } ) ;
263- } ;
264- saveZip = async ( ) => zip . generateAsync ( { type : 'uint8array' } ) ;
265- }
266- } catch ( error : any ) {
267- throw new Error ( `Invalid gridset buffer: ${ error . message } ` ) ;
268- }
224+ const updatedEntries : ZipFile [ ] = [ ] ;
225+ const zip = zipAdapter ? await zipAdapter ( gridsetBuffer ) : await getZipAdapter ( gridsetBuffer ) ;
226+ const entries = getZipEntriesFromAdapter ( zip , password ) ;
269227
270228 let found = false ;
271229
@@ -308,9 +266,10 @@ export async function updateWordlist(
308266
309267 // Rebuild the XML
310268 const updatedXml = builder . build ( data ) ;
311- if ( updateEntry ) {
312- updateEntry ( entry . entryName , updatedXml ) ;
313- }
269+ updatedEntries . push ( {
270+ name : entry . entryName ,
271+ data : updatedXml ,
272+ } ) ;
314273 found = true ;
315274 } catch ( error ) {
316275 const message = error instanceof Error ? error . message : String ( error ) ;
@@ -324,8 +283,5 @@ export async function updateWordlist(
324283 throw new Error ( `Grid "${ gridName } " not found in gridset` ) ;
325284 }
326285
327- if ( ! saveZip ) {
328- throw new Error ( 'Failed to serialize updated gridset.' ) ;
329- }
330- return await saveZip ( ) ;
286+ return await zip . writeFiles ( updatedEntries ) ;
331287}
0 commit comments