# @stackline/cfb > Read and write compound files and ZIP containers with the SheetJS CFB API and bounded FAT traversal. [![npm version](https://img.shields.io/npm/v/@stackline/cfb.svg?style=flat-square)](https://www.npmjs.com/package/@stackline/cfb) [![license](https://img.shields.io/npm/l/@stackline/cfb.svg?style=flat-square)](https://github.com/alexandroit/stackline-cfb) [![GitHub repository](https://img.shields.io/badge/GitHub-repository-181717?style=flat-square&logo=github)](https://github.com/alexandroit/stackline-cfb) [![Docs](https://img.shields.io/badge/docs-alexandro.net-0f766e?style=flat-square)](https://alexandro.net/docs/vanilla/cfb/) [![Reddit community](https://img.shields.io/badge/community-r%2FStackline-ff4500?style=flat-square&logo=reddit&logoColor=white)](https://www.reddit.com/r/Stackline/) **[Documentation](https://alexandro.net/docs/vanilla/cfb/)** | **[npm](https://www.npmjs.com/package/@stackline/cfb)** | **[Issues](https://github.com/alexandroit/stackline-cfb/issues)** | **[Repository](https://github.com/alexandroit/stackline-cfb)** **Current package version:** `1.0.4` --- ## Why this package? An independent Apache-2.0 maintenance fork of `cfb@1.2.2`. ```sh npm install @stackline/cfb ``` ```js const CFB = require('@stackline/cfb'); ``` Version 1.0.0 preserves the upstream API and rejects cyclic FAT chains instead of hanging while parsing a damaged compound file (upstream issue [SheetJS/js-cfb#11](https://github.com/SheetJS/js-cfb/issues/11)). The declared checksum runtime dependencies were unused because the implementation already embeds its checksum code; this package has no runtime dependencies. Node.js 20.19 or newer is the supported development/runtime target. `npm test` builds the CommonJS/browser artifacts, runs the upstream API suite against three pinned public fixtures, and checks corrupted input and archive round trips. This selected fixture set is not the full upstream document corpus. The original source and attribution are retained below. --- Pure JS implementation of various container file formats, including ZIP and CFB. ## Compatibility | Item | Value | | --- | --- | | Package | `@stackline/cfb@1.0.4` | | Supported Node.js | `>=20.19.0` | | Module entry | `./cfb` (CommonJS) | | Runtime dependencies | 0 direct dependencies | | Types | `types` | ## Installation ```bash npm install @stackline/cfb ``` In the browser: ```html ``` With [npm](https://www.npmjs.com/package/@stackline/cfb): ```bash $ npm install @stackline/cfb ``` The `xlscfb.js` file is designed to be embedded in [js-xlsx](http://git.io/xlsx) ## Usage ```js const CFB = require('@stackline/cfb'); const archive = CFB.utils.cfb_new(); CFB.utils.cfb_add(archive, 'hello.txt', Buffer.from('Hello')); const bytes = CFB.write(archive, { type: 'buffer', fileType: 'zip' }); ``` ### Library Usage In node: ```js var CFB = require('@stackline/cfb'); ``` For example, to get the Workbook content from an Excel 2003 XLS file: ```js var cfb = CFB.read(filename, {type: 'file'}); var workbook = CFB.find(cfb, 'Workbook'); var data = workbook.content; ``` ## Security The parser rejects cyclic FAT chains instead of looping indefinitely. See the security policy for supported reporting and parser limitations. See the [security policy](https://github.com/alexandroit/stackline-cfb/blob/main/SECURITY.md). ## API Surface ### Command-Line Utility Usage The [`cfb-cli`](https://www.npmjs.com/package/cfb-cli) module ships with a CLI tool for manipulating and inspecting supported files. ### JS API TypeScript definitions are maintained in `types/index.d.ts`. The CFB object exposes the following methods and properties: `CFB.parse(blob)` takes a nodejs Buffer or an array of bytes and returns an parsed representation of the data. `CFB.read(blob, opts)` wraps `parse`. `CFB.find(cfb, path)` performs a case-insensitive match for the path (or file name, if there are no slashes) and returns an entry object or null if not found. `CFB.write(cfb, opts)` generates a file based on the container. `CFB.writeFile(cfb, filename, opts)` creates a file with the specified name. ### Parse Options `CFB.read` takes an options argument. `opts.type` controls the behavior: | `type` | expected input | |------------|:----------------------------------------------------------------| | `"base64"` | string: Base64 encoding of the file | | `"binary"` | string: binary string (byte `n` is `data.charCodeAt(n)`) | | `"buffer"` | nodejs Buffer | | `"file"` | string: path of file that will be read (nodejs only) | | (default) | buffer or array of 8-bit unsigned int (byte `n` is `data[n]`) | ### Write Options `CFB.write` and `CFB.writeFile` take options argument. `opts.type` controls the behavior: | `type` | output | |------------|:----------------------------------------------------------------| | `"base64"` | string: Base64 encoding of the file | | `"binary"` | string: binary string (byte `n` is `data.charCodeAt(n)`) | | `"buffer"` | nodejs Buffer | | `"file"` | string: path of file that will be created (nodejs only) | | (default) | buffer if available, array of 8-bit unsigned int otherwise | `opts.fileType` controls the output file type: | `fileType` | output | |:-------------------|:------------------------| | `'cfb'` (default) | CFB container | | `'zip'` | ZIP file | | `'mad'` | MIME aggregate document | `opts.compression` enables DEFLATE compression for ZIP file type. ### Utility Functions The utility functions are available in the `CFB.utils` object. Functions that accept a `name` argument strictly deal with absolute file names: - `.cfb_new(?opts)` creates a new container object. - `.cfb_add(cfb, name, ?content, ?opts)` adds a new file to the `cfb`. Set the option `{unsafe:true}` to skip existence checks (for bulk additions) - `.cfb_del(cfb, name)` deletes the specified file - `.cfb_mov(cfb, old_name, new_name)` moves the old file to new path and name - `.use_zlib(require("zlib"))` loads a nodejs `zlib` instance. By default, the library uses a pure JS inflate/deflate implementation. NodeJS `zlib.InflateRaw` exposes the number of bytes read in versions after `8.11.0`. If a supplied `zlib` does not support the required features, a warning will be displayed in the console and the pure JS fallback will be used. ### Container Object Description The objects returned by `parse` and `read` have the following properties: - `.FullPaths` is an array of the names of all of the streams (files) and storages (directories) in the container. The paths are properly prefixed from the root entry (so the entries are unique) - `.FileIndex` is an array, in the same order as `.FullPaths`, whose values are objects following the schema: ```typescript interface CFBEntry { name: string; /** Case-sensitive internal name */ type: number; /** 1 = dir, 2 = file, 5 = root ; see [MS-CFB] 2.6.1 */ content: Buffer | number[] | Uint8Array; /** Raw Content */ ct?: Date; /** Creation Time */ mt?: Date; /** Modification Time */ ctype?: String; /** Content-Type (for MAD) */ } ``` ### References - `MS-CFB`: Compound File Binary File Format - ZIP `APPNOTE.TXT`: .ZIP File Format Specification - RFC1951: https://www.ietf.org/rfc/rfc1951.txt - RFC2045: https://www.ietf.org/rfc/rfc2045.txt - RFC2557: https://www.ietf.org/rfc/rfc2557.txt ## Local Development Clone the [repository](https://github.com/alexandroit/stackline-cfb) and run the following commands from its root: ```bash npm ci npm run build npm test ``` ## Consumer Smoke Test `npm run test:package` packs the library and exercises an isolated consumer using the repository fixture. ## Release Checklist 1. Update the package version, lockfile, generated version fields, and changelog together. 2. Run the development checks above and audit both `npm audit` and `npm audit --omit=dev`. 3. Use the [GitHub publish workflow](https://github.com/alexandroit/stackline-cfb/actions/workflows/publish.yml) with its `Prod` environment to publish the exact CI tarball. 4. Verify public npm bytes, package identity, provenance, and the immutable GitHub release evidence. ## License [Apache-2.0](https://github.com/alexandroit/stackline-cfb/blob/main/LICENSE). Original copyright notices and upstream attribution are retained. Please consult the attached LICENSE file for details. All rights not explicitly granted by the Apache 2.0 License are reserved by the Original Author. ## Credits and original authors - Original project: [js-cfb](https://github.com/SheetJS/js-cfb). - sheetjs. - Copyright (C) 2013-present SheetJS LLC. - Stackline maintenance: [Alexandro Paixao Marques](https://www.linkedin.com/in/aleinfo/) and [Stackline contributors](https://github.com/alexandroit). Original copyright, license notices and contributor acknowledgements remain part of this distribution. Stackline maintenance does not replace authorship of the original work. ## Community and Links - [Stackline website](https://alexandro.net/) - [GitHub projects](https://github.com/alexandroit) - [npm packages](https://www.npmjs.com/~alex360qc) - [Reddit community — r/Stackline](https://www.reddit.com/r/Stackline/) - [Maintainer LinkedIn](https://www.linkedin.com/in/aleinfo/) Use this repository's issue tracker for reproducible bugs and feature requests. Join r/Stackline for examples, usage questions and release discussions.