1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-03 09:39:16 +02:00
No description
Find a file
2020-04-02 23:42:02 -07:00
archive Turn archive.js into an ES module for Issue #16 2020-04-02 17:29:02 -07:00
build Make ByteStream and ByteBuffer into modules with Worker veresions via build for Issue #16 2020-04-02 00:07:14 -07:00
docs Update link in unrar doc to bitjs on github. 2017-02-21 18:24:17 -08:00
file Update file sniffer to be an ES module 2020-04-02 19:30:21 -07:00
image/webp-shim Random update to the wasm module 2020-04-02 23:41:43 -07:00
io Make ByteStream and ByteBuffer into modules with Worker veresions via build for Issue #16 2020-04-02 00:07:14 -07:00
tests Update test files to be ES modules for Issue #16 2020-04-02 19:46:14 -07:00
index.js First attempt at adding a npm package.json for Issue #16 2020-04-02 23:42:02 -07:00
LICENSE Accidentally didn't add the updated files. 2016-04-19 18:00:27 -07:00
package.json First attempt at adding a npm package.json for Issue #16 2020-04-02 23:42:02 -07:00
README.md Update README to reflect things are all ES modules now 2020-04-02 19:53:43 -07:00

bitjs: Binary Tools for JavaScript

Introduction

A set of JavaScript modules to handle binary data in JS (using Typed Arrays).

Example Usage

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, offset, length);
const crc = bstream.readBits(12); // read in 12 bits as CRC, advancing the pointer
const flagbits = bstream.peekBits(6); // look ahead at next 6 bits, but do not advance the pointer

bitjs.archive

This package includes objects for unarchiving binary data in popular archive formats (zip, rar, tar) providing unzip, unrar and untar capabilities via JavaScript in the browser. The unarchiving actually happens inside a Web Worker.

import { Unzipper } from './bitjs/archive/archive.js';
const unzipper = new Unzipper(zipFileArrayBuffer);
unzipper.addEventListener('progress', updateProgress);
unzipper.addEventListener('extract', receiveOneFile);
unzipper.addEventListener('finish', displayZipContents);
unzipper.start();

function updateProgress(e) {
  // e.totalCompressedBytesRead has how many bytes have been unzipped so far
}

function receiveOneFile(e) {
  // e.unarchivedFile.filename: string
  // e.unarchivedFile.fileData: Uint8Array
}

function displayZipContents() {
  // Now sort your received files and show them or whatever...
}

The unarchivers also support streaming, if you are receiving the zipped file from a slow place (a Cloud API, for instance). For example:

import { Unzipper } from './bitjs/archive/archive.js';
const unzipper = new Unzipper(anArrayBufferWithStartingBytes);
unzipper.addEventListener('progress', updateProgress);
unzipper.addEventListener('extract', receiveOneFile);
unzipper.addEventListener('finish', displayZipContents);
unzipper.start();
...
// after some time
unzipper.update(anArrayBufferWithMoreBytes);
...
// after some more time
unzipper.update(anArrayBufferWithYetMoreBytes);

bitjs.image

This package includes code for dealing with binary images. It includes a module for converting WebP images into alternative raster graphics formats (PNG/JPG).

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.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);

Tests

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, including adding streaming support, starter RarVM support, tests, many bug fixes, and updating the code to ES6.