shortercode / zip

A modern Typescript library for creating, reading and editing ZIP archives.
MIT License
16 stars 3 forks source link

Isomorphic build #6

Open digable1 opened 11 months ago

digable1 commented 11 months ago

First off: I would not be surprised if this was 'operator error'. I just don't know why, as so far I think I'm doing it right (famous last words)...

I'm getting the following error message:

ReferenceError: Worker is not defined
    at create_worker (c:\projects\qso-logbook-next\node_modules\@shortercode\webzip\index.js:747:20)
    at get_worker (c:\projects\qso-logbook-next\node_modules\@shortercode\webzip\index.js:757:25)
    at c:\projects\qso-logbook-next\node_modules\@shortercode\webzip\index.js:773:24
    at new Promise (<anonymous>)
    at decompress (c:\projects\qso-logbook-next\node_modules\@shortercode\webzip\index.js:772:12)
    at decompress$1 (c:\projects\qso-logbook-next\node_modules\@shortercode\webzip\index.js:40:12)
    at ZipEntry.decompress (c:\projects\qso-logbook-next\node_modules\@shortercode\webzip\index.js:124:34)
    at ZipEntry.get_blob (c:\projects\qso-logbook-next\node_modules\@shortercode\webzip\index.js:208:25)
    at ZipEntry.get_string (c:\projects\qso-logbook-next\node_modules\@shortercode\webzip\index.js:218:33)
    at getZipFile (c:\projects\qso-logbook-next\tools\demo.ts:15:39)

demo.ts:

import { ZipArchive } from "@shortercode/webzip";

enum ZipentryType {
    string,
    blob,
    arrayBuffer
}

async function getZipFile(zipFile: string, zipContents: ZipArchive, output = ZipentryType.string): Promise<string | Blob | undefined> {
    console.log(`getZipFile - zipFile: ${zipFile}`);
    const zipEntry = zipContents.get(zipFile);
    if (zipEntry) {
        try {
            if (output === ZipentryType.string) {
                return await zipEntry.get_string()
            }
        } catch(e) {
            console.error(`Could not get the contents of zip file '${zipFile}' in archive`);
            console.error();
            console.log(`Zip Entry: ${JSON.stringify(zipEntry, null, 4)}`);
            console.error();
            console.error(`Directory file names (just to prove it really is a blob zip): ${getZipDirectory(zipContents).join("\n     ")}`);
            console.error();
            console.error(`Error:`)
            console.error(e);
        }
    }
    return new Promise((resolve) => resolve(undefined));

    function getZipDirectory(zipContentsParm: ZipArchive): Array<string> {
        const directoryNames: Array<string> = [];
        const directoryIterator = zipContentsParm.files();
        let next = directoryIterator.next();
        while (next && next.done !== undefined && !next.done) {
            directoryNames.push(next.value[0]);
            next = directoryIterator.next();
        }
        return directoryNames;
    }
}

async function main(): Promise<void> {
    const fccDataUrl = 'https://data.fcc.gov/download/pub/uls/complete/l_amat.zip';
    console.log(`Fetching url '${fccDataUrl}' - be patient...`)
    const fccRespone = await fetch(fccDataUrl);
    const blobContents = await fccRespone.blob();
    const archive = await ZipArchive.from_blob(blobContents);
    await getZipFile('EN.dat', archive);
}

main().then(
    () => console.log(`Done`)
)

tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "allowJs": false,
    "skipLibCheck": false,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "ESNext",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "incremental": true,
  },
  "ts-node": {
    "esm": true,
    "compilerOptions": {
      "module": "nodenext"
    }
  }
}

Invoked by: npx ts-node --project tools/tsconfig.json tools/demo.ts

Anybody know what's up?

shortercode commented 11 months ago

Don’t think I’ll have a chance to look at this today but I’m guessing your trying to use the library in Node.js right?The library was designed to utilise browser APIs such as Worker and Blob. It could probably be adapted to work without, but it will take a bit of work.Sent from my iPhoneOn 23 Sep 2023, at 01:05, digable1 @.> wrote: First off: I would not be surprised if this was 'operator error'. I just don't know why, as so far I think I'm doing it right (famous last words)... I'm getting the following error message: ReferenceError: Worker is not defined at create_worker @.\webzip\index.js:747:20) at get_worker @.\webzip\index.js:757:25) at @.\webzip\index.js:773:24 at new Promise () at decompress @.\webzip\index.js:772:12) at decompress$1 @.\webzip\index.js:40:12) at ZipEntry.decompress @.\webzip\index.js:124:34) at ZipEntry.get_blob @.\webzip\index.js:208:25) at ZipEntry.get_string @.***\webzip\index.js:218:33) at getZipFile (c:\projects\qso-logbook-next\tools\demo.ts:15:39)

demo.ts: import { ZipArchive } from @.***/webzip";

enum ZipentryType { string, blob, arrayBuffer }

async function getZipFile(zipFile: string, zipContents: ZipArchive, output = ZipentryType.string): Promise<string | Blob | undefined> { console.log(getZipFile - zipFile: ${zipFile}); const zipEntry = zipContents.get(zipFile); if (zipEntry) { try { if (output === ZipentryType.string) { return await zipEntry.get_string() } } catch(e) { console.error(Could not get the contents of zip file '${zipFile}' in archive); console.error(); console.log(Zip Entry: ${JSON.stringify(zipEntry, null, 4)}); console.error(); console.error(Directory file names (just to prove it really is a blob zip): ${getZipDirectory(zipContents).join("\n ")}); console.error(); console.error(Error:) console.error(e); } } return new Promise((resolve) => resolve(undefined));

function getZipDirectory(zipContentsParm: ZipArchive): Array<string> {
    const directoryNames: Array<string> = [];
    const directoryIterator = zipContentsParm.files();
    let next = directoryIterator.next();
    while (next && next.done !== undefined && !next.done) {
        directoryNames.push(next.value[0]);
        next = directoryIterator.next();
    }
    return directoryNames;
}

}

async function main(): Promise { const fccDataUrl = 'https://data.fcc.gov/download/pub/uls/complete/l_amat.zip'; console.log(Fetching url '${fccDataUrl}' - be patient...) const fccRespone = await fetch(fccDataUrl); const blobContents = await fccRespone.blob(); const archive = await ZipArchive.from_blob(blobContents); await getZipFile('EN.dat', archive); }

main().then( () => console.log(Done) ) tsconfig.json: { "compilerOptions": { "target": "ESNext", "allowJs": false, "skipLibCheck": false, "strict": true, "noEmit": true, "esModuleInterop": true, "module": "ESNext", "resolveJsonModule": true, "isolatedModules": true, "incremental": true, }, "ts-node": { "esm": true, "compilerOptions": { "module": "nodenext" } } } Invoked by: npx ts-node --project tools/tsconfig.json tools/demo.ts Anybody know what's up?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

digable1 commented 11 months ago

Yup - using node ('Invoked by: npx ts-node --project tools/tsconfig.json tools/demo.ts'). Didn't see that callout anywhere in the doc.

Might help to call this out, short-term. Even better short-term would also be a quick check at startup and stop if node is identified - with a helpful error message.

However: Longer term, would appreciate the ability for Node to support this as well. I like that this is in TypeScript and the ZipArchive.from_blob() capability can make life much easier when working with zip files from the internet - like mine (I didn't have to think about this - until it didn't work).

shortercode commented 2 months ago

My WIP overhaul should work in Node and the browser 😄 Node has moved on a bit and it has some the browser standard parts available in the global scope now. As well as support for Compression/Decompression stream.

Still got a bit of work to do but it's looking promising. I'll keep this issue open as a tracker for an isomorphic build