@codedread/bitjs

中文文档
1.2.6发布于 2026/03/19MITTypeScriptES Module零依赖近 30 天下载 2582

Binary Tools for JavaScript

原始使用文档
文档目录11 节
  1. Introduction
  2. Installation
  3. CommonJS/ESM in Node
  4. Packages
  5. bitjs.archive
  6. bitjs.codecs
  7. bitjs.file
  8. bitjs.image
  9. bitjs.io
  10. Reference
  11. History

Node.js CI

bitjs: Binary Tools for JavaScript

Introduction

A set of dependency-free JavaScript modules to work with binary data in JS (using Typed Arrays). Includes:

  • bitjs/archive: Decompressing files (unzip, unrar, untar, gunzip) in JavaScript, implemented as Web Workers where supported, and allowing progressive unarchiving while streaming.
  • bitjs/codecs: Get the codec info of media containers in a ISO RFC6381 MIME type string.
  • bitjs/file: Detect the type of file from its binary signature.
  • bitjs/image: Parsing GIF, JPEG, PNG. Conversion of WebP to PNG or JPEG.
  • bitjs/io: Low-level classes for interpreting binary data (BitStream, ByteStream). For example, reading or peeking at N bits at a time.

Installation

Install it using your favourite package manager, the package is registered under @codedread/bitjs.

npm install @codedread/bitjs

or

yarn add @codedread/bitjs

CommonJS/ESM in Node

This module is an ES Module. If your project uses CommonJS modules, it's a little trickier to use. One example of this is if a TypeScript project compiles to CommonJS, it will try to turn imports into require() statements, which will break. The fix for this (unfortunately) is to update your tsconfig.json:

 "moduleResolution": "Node16",

and use a Dynamic Import:

const { getFullMIMEString } = await import('@codedread/bitjs');

Packages

bitjs.archive

This package includes objects for decompressing and compressing binary data in popular archive formats (zip, rar, tar, gzip). Here is a simple example of unrar:

Decompressing

import { Unrarrer } from './bitjs/archive/decompress.js';
const unrar = new Unrarrer(rarFileArrayBuffer);
unrar.addEventListener('extract', (e) => {
  const {filename, fileData} = e.unarchivedFile;
  console.log(`Extracted ${filename} (${fileData.byteLength} bytes)`);
  // Do something with fileData...
});
unrar.addEventListener('finish', () => console.log('Done'));
unrar.start();

More details and examples are located on the API page.

bitjs.codecs

This package includes code for dealing with media files (audio/video). It is useful for deriving ISO RFC6381 MIME type strings, including the codec information. Currently supports a limited subset of MP4 and WEBM.

How to use:

  • First, install ffprobe (ffmpeg) on your system.
  • Then:

import { getFullMIMEString } from 'bitjs/codecs/codecs.js';
/**
 * @typedef {import('bitjs/codecs/codecs.js').ProbeInfo} ProbeInfo
 */

const cmd = 'ffprobe -show_format -show_streams -print_format json -v quiet foo.mp4';
exec(cmd, (error, stdout) => {
  /** @type {ProbeInfo} */
  const info = JSON.parse(stdout);
  // 'video/mp4; codecs="avc1.4D4028, mp4a.40.2"'
  const contentType = getFullMIMEString(info);
  ...
});

bitjs.file

This package includes code for dealing with files. It includes a sniffer which detects the type of file, given an ArrayBuffer.

import { findMimeType } from './bitjs/file/sniffer.js';
const mimeType = findMimeType(someArrayBuffer);

bitjs.image

This package includes code for dealing with image files. It includes low-level, event-based parsers for GIF, JPEG, and PNG images.

It also includes a module for converting WebP images into alternative raster graphics formats (PNG/JPG), though this latter module is deprecated, now that WebP images are well-supported in all browsers.

GIF Parser

import { GifParser } from './bitjs/image/parsers/gif.js'

const parser = new GifParser(someArrayBuffer);
parser.onApplicationExtension(evt => {
  const appId = evt.detail.applicationIdentifier;
  const appAuthCode = new TextDecoder().decode(evt.detail.applicationAuthenticationCode);
  if (appId === 'XMP Data' && appAuthCode === 'XMP') {
    /** @type {Uint8Array} */
    const appData = evt.detail.applicationData;
    // Do something with appData (parse the XMP).
  }
});
parser.start();

JPEG Parser

import { JpegParser } from './bitjs/image/parsers/jpeg.js'
import { ExifTagNumber } from './bitjs/image/parsers/exif.js';

const parser = new JpegParser(someArrayBuffer)
    .onApp1Exif(evt => console.log(evt.detail.get(ExifTagNumber.IMAGE_DESCRIPTION).stringValue));
await parser.start();

PNG Parser

import { PngParser } from './bitjs/image/parsers/png.js'
import { ExifTagNumber } from './bitjs/image/parsers/exif.js';

const parser = new PngParser(someArrayBuffer);
    .onExifProfile(evt => console.log(evt.detail.get(ExifTagNumber.IMAGE_DESCRIPTION).stringValue))
    .onTextualData(evt => console.dir(evt.detail));
await parser.start();

WebP Converter

import { convertWebPtoPNG, convertWebPtoJPG } from './bitjs/image/webp-shim/webp-shim.js';
// convertWebPtoPNG() takes in an ArrayBuffer containing the bytes of a WebP
// image and returns a Promise that resolves with an ArrayBuffer containing the
// bytes of an equivalent PNG image.
convertWebPtoPNG(webpBuffer).then(pngBuf => {
  const pngUrl = URL.createObjectURL(new Blob([pngBuf], {type: 'image/png'}));
  someImgElement.setAttribute(src, pngUrl);
});

bitjs.io

This package includes stream objects for reading and writing binary data at the bit and byte level: BitStream, ByteStream.

import { BitStream } from './bitjs/io/bitstream.js';
const bstream = new BitStream(someArrayBuffer, true /** most-significant-bit-to-least */ );
const crc = bstream.readBits(12); // Read in 12 bits as CRC. Advance pointer.
const flagbits = bstream.peekBits(6); // Look ahead at next 6 bits. Do not advance pointer.

More details and examples are located on the API page.

Reference

  • UnRar: A work-in-progress description of the RAR file format.

History

This project grew out of another project of mine, kthoom (a comic book reader implemented in the browser). This repository was automatically exported from my original repository on GoogleCode and has undergone considerable changes and improvements since then.

版本与发布记录

@codedread/bitjs 共发布 26 个版本,首次发布于 2020/04/03;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.2.6latest2026/03/197 个月前
1.2.52025/10/061.1 年前
1.2.32024/02/052 年前
1.2.22024/01/272 年前
1.2.12024/01/202 年前
1.2.02024/01/162 年前

关于 @codedread/bitjs 的常见问题

@codedread/bitjs 是什么?

Binary Tools for JavaScript

如何安装 @codedread/bitjs?

在项目目录运行 npm install @codedread/bitjs,或使用 pnpm add / yarn add / bun add。

@codedread/bitjs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codedread/bitjs 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@codedread/bitjs 可以商用吗?

@codedread/bitjs 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codedread/bitjs 的下载量有多少?

根据 npm 官方下载统计,@codedread/bitjs 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,582 次,日均约 86 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codedread/bitjs 还在维护吗?

最新版本 1.2.6 发布于 2026/03/19(7 个月前);至今共发布 26 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/04/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。