{"name":"@tokenizer/inflate","version":"0.5.0","keywords":["zip","unzip","decompress","inflate","strtok3","tokenizer","stream","S3"],"author":{"url":"https://github.com/Borewit","name":"Borewit"},"license":"MIT","_id":"@tokenizer/inflate@0.5.0","maintainers":[{"name":"borewit","email":"borewit@xs4all.nl"}],"homepage":"https://github.com/Borewit/tokenizer-inflate#readme","bugs":{"url":"hhttps://github.com/Borewit/tokenizer-inflate/issues"},"dist":{"shasum":"485412cdcb645bc9d769517a0a1585c12bd02acc","tarball":"http://registry-npm.ywwl.com/@tokenizer/inflate/download/@tokenizer/inflate-0.5.0.tgz","fileCount":13,"integrity":"sha512-Thpse3RMiSv5CzIHp12So3JwuJ9Qt08copHFbNGS1PKL54FZgrcnMegwLZM+gxRAeNwSHNUip1hLNc09y7Gntw==","signatures":[{"sig":"MEQCIAckDS5pko/7RXLsC/nBf53wuQhPIO6j8QF4oPsuSOKSAiBmaJUniNS5gB5SkEPzci1AxLmFUWHXTcZ07FG+N1GONQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"sig":"MEQCIGN0nA8MoVepP0sshupwbxVld8tJZNw+qoFBwHdUgsyyAiAWitOTp3NvRP28lN1+ub+zkqxaKHtfroJzXIQs50YYxQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":35043,"size":9463,"noattachment":false,"key":"/@tokenizer/inflate/-/@tokenizer/inflate-0.5.0.tgz"},"type":"module","engines":{"node":">=18"},"exports":"./lib/index.js","funding":{"url":"https://github.com/sponsors/Borewit","type":"github"},"gitHead":"ec9f8b4d85417e23a79a02d5a0957f2405a4cd2f","scripts":{"fix":"yarn run lint:ts:fix","test":"mocha","build":"yarn run clean && yarn run compile","clean":"del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'test/**/*.js' 'test/**/*.js.map'","start":"yarn run compile && yarn run lint && yarn run cover-test","eslint":"eslint lib test","compile":"yarn run compile-src && yarn run compile-test","lint:md":"remark -u preset-lint-recommended .","lint:ts":"biome check","compile-src":"tsc -p lib","lint:ts:fix":"biome check --write","compile-test":"tsc -p test","update-biome":"yarn add -D --exact @biomejs/biome && npx @biomejs/biome migrate --write","prepublishOnly":"yarn run build"},"_npmUser":{"name":"borewit","email":"borewit@xs4all.nl"},"repository":{"url":"git+https://github.com/Borewit/tokenizer-inflate.git","type":"git"},"_npmVersion":"10.9.9","description":"Tokenized zip support","directories":{},"_nodeVersion":"22.23.3","dependencies":{"debug":"^4.4.3","token-types":"^6.1.2"},"_hasShrinkwrap":false,"packageManager":"yarn@4.18.1","devDependencies":{"tsx":"^4.23.15","chai":"^6.3.0","mocha":"^12.0.3","del-cli":"^8.0.1","strtok3":"^10.4.1","file-type":"^22.1.1","typescript":"^7.0.2","@types/chai":"^5.2.3","@types/node":"^26.6.4","@types/debug":"^4","@types/mocha":"^10.0.10","@tokenizer/s3":"^1.0.4","@biomejs/biome":"2.5.15","@aws-sdk/client-s3":"^3.1148.0"},"_npmOperationalInternal":{"tmp":"tmp/inflate_0.5.0_1791825017342_0.8165191601868085","host":"s3://npm-registry-packages-npm-production"},"readme":"[![Node.js CI](https://github.com/Borewit/tokenizer-inflate/actions/workflows/nodejs-ci.yml/badge.svg)](https://github.com/Borewit/tokenizer-inflate/actions/workflows/nodejs-ci.yml)\n[![NPM version](https://badge.fury.io/js/%40tokenizer%2Finflate.svg)](https://npmjs.org/package/@tokenizer/inflate)\n[![npm downloads](https://img.shields.io/npm/dm/@tokenizer%2Finflate.svg)](https://npmcharts.com/compare/%40tokenizer%2Finflate?start=1200&interval=30)\n\n# @tokenizer/inflate\n\n`@tokenizer/inflate` is a package designed for handling and extracting data from ZIP files efficiently using a tokenizer-based approach.\nThe library provides a customizable way to parse ZIP archives and extract compressed data while minimizing memory usage.\n\n## Features\n- Efficient Decompression: Handles streams compressed with DEFLATE and related formats (e.g., gzip).\n- Tokenizer Compatibility: Seamlessly integrates with [strtok3](https://github.com/Borewit/strtok3). For example, use  [@tokenizer/s3](https://github.com/Borewit/tokenizer-s3) for efficient partial extraction of a Zip stored on AWS S3 cloud file storage.\n- Streamlined Interface: Provides an intuitive API for working with compressed data in streaming and random-access scenarios.\n- Chunked Data Access: Leverages the underlying media's capabilities to offer chunked or random access to data, unlike traditional streams.\n- Plug-and-Play: Easily integrate with existing tokenizer-based workflows for parsing file metadata or binary structures.\n- Interrupt the extraction process conditionally.\n\n## Installation\n```bash\nnpm install @tokenizer/inflate\n```\n\n## Usage\n\n### Example: Extracting Specific Files\n\nThe following example demonstrates how to use the library to extract .txt files and stop processing when encountering a .stop file.\n\n```js\nimport { ZipHandler } from '@tokenizer/inflate';\nimport { fromFile } from 'strtok3';\n\nconst fileFilter = (file) => {\n  console.log(`Processing file: ${file.filename}`);\n\n  if (file.filename?.endsWith(\".stop\")) {\n    console.log(`Stopping processing due to file: ${file.filename}`);\n    return { handler: false, stop: true }; // Stop the unzip process\n  }\n\n  if (file.filename?.endsWith(\".txt\")) {\n    return {\n      handler: async (data) => {\n        console.log(`Extracted text file: ${file.filename}`);\n        console.log(new TextDecoder().decode(data));\n      },\n    };\n  }\n\n  return { handler: false }; // Ignore other files\n};\n\nasync function extractFiles(zipFilePath) {\n  const tokenizer = await fromFile(zipFilePath);\n  const zipHandler = new ZipHandler(tokenizer);\n  await zipHandler.unzip(fileFilter);\n}\n\nextractFiles('example.zip').catch(console.error);\n```\n\n## API\n\n### `ZipHandler`\nA class for handling ZIP file parsing and extraction.\n#### Constructor\n```ts\nnew ZipHandler(tokenizer: ITokenizer, options?: ZipHandlerOptions)\n```\n- **tokenizer**: An instance of ITokenizer to read the ZIP archive.\n- **options**: Optional per-entry byte limits. Each accepts a positive safe integer or `Infinity`.\n  Omitted limits default to `Infinity`, preserving unlimited processing.\n\n```ts\nconst zipHandler = new ZipHandler(tokenizer, {\n  maxDataDescriptorScanBytes: 1024 * 1024,\n  maxCompressedSize: 1024 * 1024,\n  maxUncompressedSize: 4 * 1024 * 1024\n});\n```\n\n`maxDataDescriptorScanBytes` bounds bytes before the descriptor signature when the compressed\nsize is unknown, even for ignored entries. Signature lookahead uses up to four additional bytes.\n`maxCompressedSize` bounds compressed data buffered for selected entries, and\n`maxUncompressedSize` bounds their decompressed output, including stored entries.\nLimits apply to local-header and central-directory sizes before allocation, and to actual\noutput before retaining chunks or combining buffers. Decompression is canceled on overflow.\nThese are per-entry limits, not a total process memory budget; tokenizer lookahead and native\ndecompression have their own buffers. Configure tokenizer limits separately where available.\n\nA limit violation throws the exported `ZipLimitError` (a `RangeError`), with `limit`,\n`requestedSize`, and `maxSize` properties. No data handler is called for the rejected entry.\nInvalid options throw `RangeError` when constructing the handler. After a processing failure,\nclose the tokenizer; extraction is not resumable.\n\n#### Methods\n \n- `isZip(): Promise<boolean>`\n\n   Determines whether the input file is a ZIP archive.\n\n- `unzip(fileCb: InflateFileFilter): Promise<void>`\n\n  Extracts files from the ZIP archive, applying the provided `InflateFileFilter` callback to each file.\n\n```InflatedDataHandler``` \n\n## Types\n\n### `InflateFileFilter`\n```ts\ntype InflateFileFilter = (file: IFullZipHeader) => InflateFileFilterResult;\n```\nCallback function to determine whether a file should be handled or ignored.\n\n### `InflateFileFilterResult`\n```ts\ntype InflateFileFilterResult = {\n  handler: InflatedDataHandler | false; // Handle file data or ignore\n  stop?: boolean; // Stop processing further files\n};\n\n```\nReturned from `InflateFileFilter` to control file handling and extraction flow.\n\n### `InflatedDataHandler`\n```ts\ntype InflatedDataHandler = (fileData: Uint8Array) => Promise<void>;\n```\nHandler for processing uncompressed file data.\n\n## Compatibility\n\nThis module is a [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).\nThe distributed JavaScript codebase is compliant with the [ECMAScript 2020 (11th Edition)](https://en.wikipedia.org/wiki/ECMAScript_version_history#11th_Edition_%E2%80%93_ECMAScript_2020) standard.\nIf used with Node.js, it requires version ≥ 18.\n\n## Contributing\n\nSee the [contribution guide](CONTRIBUTING.md) for code style and development checks.\n\n## License\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.","publish_time":1791825017441,"_cnpm_publish_time":1791825017441,"dist-tags":{"latest":"0.6.0"}}