npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tokenizer/inflate

v0.2.4

Published

Tokenized zip support

Downloads

269

Readme

Node.js CI NPM version npm downloads Bundle Size

@tokenizer/inflate

@tokenizer/inflate is a package designed for handling and extracting data from ZIP files efficiently using a tokenizer-based approach. The library provides a customizable way to parse ZIP archives and extract compressed data while minimizing memory usage.

Features

  • Read and extract files from ZIP archives.
  • Filter files based on custom criteria using callback functions.
  • Handle extracted data using user-defined handlers.
  • Interrupt the extraction process conditionally.

Installation

npm install @tokenizer/inflate

Usage

Example: Extracting Specific Files

The following example demonstrates how to use the library to extract .txt files and stop processing when encountering a .stop file.

import { ZipHandler, InflateFileFilter } from "@tokenizer/inflate";
import { fromFile } from "strtok3";

const fileFilter: InflateFileFilter = (file) => {
  console.log(`Processing file: ${file.filename}`);

  if (file.filename?.endsWith(".stop")) {
    console.log(`Stopping processing due to file: ${file.filename}`);
    return { handler: false, stop: true }; // Stop the unzip process
  }

  if (file.filename?.endsWith(".txt")) {
    return {
      handler: async (data) => {
        console.log(`Extracted text file: ${file.filename}`);
        console.log(new TextDecoder().decode(data));
      },
    };
  }

  return { handler: false }; // Ignore other files
};

async function extractFiles(zipFilePath: string) {
  const tokenizer = await fromFile(zipFilePath);
  const zipHandler = new ZipHandler(tokenizer);

  await zipHandler.unzip(fileFilter);
}

extractFiles("example.zip").catch(console.error);

Example: Custom File Handling

Define custom logic to handle specific files or stop extraction based on file attributes.

const fileFilter: InflateFileFilter = (file) => {
  if (file.filename?.endsWith(".log")) {
    return {
      handler: async (data) => {
        console.log(`Processing log file: ${file.filename}`);
        const content = new TextDecoder().decode(data);
        console.log(content);
      },
    };
  }

  return { handler: false }; // Skip other files
};

API

ZipHandler

A class for handling ZIP file parsing and extraction.

Constructor

new ZipHandler(tokenizer: ITokenizer)
  • tokenizer: An instance of ITokenizer to read the ZIP archive.

Methods

  • isZip(): Promise<boolean>

    Determines whether the input file is a ZIP archive.

  • unzip(fileCb: InflateFileFilter): Promise<void>

    Extracts files from the ZIP archive, applying the provided InflateFileFilter callback to each file.

InflatedDataHandler

Types

InflateFileFilter

type InflateFileFilter = (file: IFullZipHeader) => InflateFileFilterResult;

Callback function to determine whether a file should be handled or ignored.

InflateFileFilterResult

type InflateFileFilterResult = {
  handler: InflatedDataHandler | false; // Handle file data or ignore
  stop?: boolean; // Stop processing further files
};

Returned from InflateFileFilter to control file handling and extraction flow.

InflatedDataHandler

type InflatedDataHandler = (fileData: Uint8Array) => Promise<void>;

Handler for processing uncompressed file data.

Compatibility

Starting with version 7, the module has migrated from CommonJS to pure ECMAScript Module (ESM). The distributed JavaScript codebase is compliant with the ECMAScript 2020 (11th Edition) standard.

License

This project is licensed under the MIT License. See the LICENSE file for details.