Reference Source Test
import blobCompare from 'blob-compare/src/index.js'
public class | source

blobCompare

Utility class to nest all methods

Conversion tools

All conversions are run asynchronously.

Method Description
blobCompare::toArrayBuffer Converts a blob to an ArrayBuffer. it can be optionnally chunked and assigned to a web worker. Conversion is run asynchronously.
blobCompare::toBinaryString Converts a blob to a BinaryString. it can be optionnally chunked and assigned to a web worker. Conversion is run asynchronously.

Comparison tools

Method Description Sync/Async
blobCompare::sizeEqual Compares size of two blobs sync
blobCompare::typeEqual Compares types of two blobs. Types are not really reliable as they can be tricked when creating a blob sync
blobCompare::magicNumbersEqual Compares magic numbers of two blobs. A quick comparison is done, therefore weird data types may not be compared with 100% accuracy. In that case, simply clone repo and override this function to fit your needs async
blobCompare::bytesEqualWithArrayBuffer Converts blobs or chunk blobs to ArrayBuffers and performs a byte to byte comparison async
blobCompare::bytesEqualWithBinaryString Converts blobs or chunk blobs to BinaryString and performs a byte to byte comparison async
blobCompare::isEqual The swiss army knife to bundle multiple comparison methods above in one single call async

Static Method Summary

Static Public Methods
public static

async bytesEqualWithArrayBuffer(b1: Blob, b2: Blob, size: Number, worker: Boolean): Promise<Boolean>

Compares two blobs by using arraybuffers

version 1.0.0 since 1.0.0
public static

async bytesEqualWithBinaryString(b1: Blob, b2: Blob, size: Number, worker: Boolean): Promise<Boolean>

Compares two blobs by using binary strings

version 1.0.0 since 1.0.0
public static

Compares two buffers byte to byte

version 1.0.0 since 1.0.0
public static

Compares two buffers byte through web worker

version 1.0.0 since 1.0.0
public static

Compares two buffers byte to byte through main thread

version 1.0.0 since 1.0.0
public static

async isEqual(b1: Blob, b2: Blob, options: Object): Promise<Boolean>

Automatically compares two blobs by using the given methods

version 1.1.0 since 1.0.0
public static

async magicNumbersEqual(b1: Blob, b2: Blob, worker: Boolean): Promise<Boolean>

Compares the magic numbers of two blobs

version 1.0.0 since 1.0.0
public static

sizeEqual(b1: Blob, b2: Blob): Boolean

Compare size of two blobs

version 1.0.0 since 1.0.0
public static

toArrayBuffer(blob: Blob, chunk: Number, worker: Boolean): Promise<ArrayBuffer>

Convert a blob to an ArrayBuffer

version 1.0.0 since 1.0.0
public static

async toArrayBufferWithWorker(blob: Blob, chunk: Number): Promise<ArrayBuffer>

Convert a blob to an ArrayBuffer through a web worker

version 1.0.0 since 1.0.0
public static

Convert a blob to an ArrayBuffer through main thread

version 1.0.0 since 1.0.0
public static

toBinaryString(blob: Blob, chunk: Number, worker: Boolean): Promise<String>

Convert a blob to a binary string

version 1.0.0 since 1.0.0
public static

async toBinaryStringWithWorker(blob: Blob, chunk: Number): Promise<String>

Convert a blob to a binary string through a web worker thread

version 1.0.0 since 1.0.0
public static

Convert a blob to a binary string through main thread

version 1.0.0 since 1.0.0
public static

typeEqual(b1: Blob, b2: Blob): Boolean

Compare type of two blobs

version 1.0.0 since 1.0.0

Static Public Methods

public static async bytesEqualWithArrayBuffer(b1: Blob, b2: Blob, size: Number, worker: Boolean): Promise<Boolean> version 1.0.0 since 1.0.0 source

Compares two blobs by using arraybuffers

This is the default comparison method

Params:

NameTypeAttributeDescription
b1 Blob

First blob

b2 Blob

Second blob

size Number

Size in bytes to slice blobs

worker Boolean
  • optional
  • default: true

Wether to use webworkers if available

Return:

Promise<Boolean>

Evaluates to true if blobs (or sliced blobs) are equals byte to byte

Throw:

*

Error When comparison method is not recognized

Test:

public static async bytesEqualWithBinaryString(b1: Blob, b2: Blob, size: Number, worker: Boolean): Promise<Boolean> version 1.0.0 since 1.0.0 source

Compares two blobs by using binary strings

This is not the default method to byte compare two blobs as benchmarks shows it's a little bit slower than using array buffers in most cases.

There's still at least two cases where using binary string is much faster :

  • Empty blobs
  • Blobs much prone to have difference at the start of the data

Params:

NameTypeAttributeDescription
b1 Blob

First blob

b2 Blob

Second blob

size Number

Size in bytes to slice blobs

worker Boolean
  • optional
  • default: true

Wether to use webworkers if available

Return:

Promise<Boolean>

Evaluates to true if blobs (or sliced blobs) are equals byte to byte

Test:

public static compareBuffers(buf1: ArrayBuffer, buf2: ArrayBuffer, worker: Boolean): Promise<Boolean> | Boolean version 1.0.0 since 1.0.0 source

Compares two buffers byte to byte

Params:

NameTypeAttributeDescription
buf1 ArrayBuffer

First buffer

buf2 ArrayBuffer

Second buffer

worker Boolean
  • optional
  • default: true

Whether to use worker or not

Return:

Promise<Boolean> | Boolean

true if buffers are equal

Test:

public static async compareBuffersWithWorker(buf1: ArrayBuffer, buf2: ArrayBuffer): Promise<Boolean> version 1.0.0 since 1.0.0 source

Compares two buffers byte through web worker

Params:

NameTypeAttributeDescription
buf1 ArrayBuffer

First buffer

buf2 ArrayBuffer

Second buffer

Return:

Promise<Boolean>

true if buffers are equal

public static compareBuffersWithoutWorker(buf1: ArrayBuffer, buf2: ArrayBuffer): Boolean version 1.0.0 since 1.0.0 source

Compares two buffers byte to byte through main thread

Params:

NameTypeAttributeDescription
buf1 ArrayBuffer

First buffer

buf2 ArrayBuffer

Second buffer

Return:

Boolean

true if buffers are equal

public static async isEqual(b1: Blob, b2: Blob, options: Object): Promise<Boolean> version 1.1.0 since 1.0.0 source

Automatically compares two blobs by using the given methods

Allowed methods are with aliases :

Using the partial option can be tricky as it's easy to have false positive.

As default, isEqual performs first a check on size method to discrimate blobs, then type, then magic and fallback on byte comparison on full data. This default order ensures the most optimized resource cost, though performing a complete comparison. For huge blobs, one may think about doing chunks comparison.

Workers can be disabled through options

Params:

NameTypeAttributeDescription
b1 Blob

First blob

b2 Blob

Second blob

options Object
  • optional

Configuration to use when performing comparison

options.methods Array
  • optional
  • default: ['size', 'type', 'magic', 'byte']

Default methods used for comparison. Methods are applied in the same order

options.byte String
  • optional
  • default: 'buffer'

If set to buffer, byte comparison will be based on arraybuffers. Otherwise, it will use binary strings

options.partial Boolean
  • optional
  • default: false

When set to true, the first successful comparison method will prevent further evaluations and return true immediately

options.chunks Array
  • optional
  • default: null

Custom sizes to use when performing a byte comparison. It really have few usage as one must ensure a regular pattern in blobs data to avoid false positive

options.worker Boolean
  • optional
  • default: true

Wether to use web workers if available

Return:

Promise<Boolean>

If true, blobs are equals given the used methods

Test:

public static async magicNumbersEqual(b1: Blob, b2: Blob, worker: Boolean): Promise<Boolean> version 1.0.0 since 1.0.0 source

Compares the magic numbers of two blobs

This method simply compare byte to byte at the start of data where magic numbers are usually located in most cases. You can find a quite exhaustive list of file signatures on wikipedia

It does not provide any informations about file type, but you can easily use a library like file-type to parse more informations about data if needed.

Be warned that this method can lead to false negative/positive for some file types given the currently naive algorithm.

Params:

NameTypeAttributeDescription
b1 Blob

First blob

b2 Blob

Second blob

worker Boolean
  • optional
  • default: true

Wether to use webworkers if available

Return:

Promise<Boolean>

true if magic numbers string is matching between two blogs *

Test:

public static sizeEqual(b1: Blob, b2: Blob): Boolean version 1.0.0 since 1.0.0 source

Compare size of two blobs

Obviously, two different blobs in content can have the same size and this method is only useful to discriminate blobs

Params:

NameTypeAttributeDescription
b1 Blob

First blob

b2 Blob

Second Blob

Return:

Boolean

true if sizes are equal

Test:

public static toArrayBuffer(blob: Blob, chunk: Number, worker: Boolean): Promise<ArrayBuffer> version 1.0.0 since 1.0.0 source

Convert a blob to an ArrayBuffer

The blob can optionnally be sliced with the chunkargument

Params:

NameTypeAttributeDescription
blob Blob

Blob

chunk Number

Size in bytes to slice blob

worker Boolean
  • optional
  • default: true

Wether to use webworkers if available

Return:

Promise<ArrayBuffer>

Binary data as a buffer

Test:

public static async toArrayBufferWithWorker(blob: Blob, chunk: Number): Promise<ArrayBuffer> version 1.0.0 since 1.0.0 source

Convert a blob to an ArrayBuffer through a web worker

The blob can optionnally be sliced with the chunkargument

Params:

NameTypeAttributeDescription
blob Blob

Blob

chunk Number

Size in bytes to slice blob

Return:

Promise<ArrayBuffer>

Binary data as a buffer

public static toArrayBufferWithoutWorker(blob: Blob, chunk: Number): Promise<ArrayBuffer> version 1.0.0 since 1.0.0 source

Convert a blob to an ArrayBuffer through main thread

The blob can optionnally be sliced with the chunkargument

Params:

NameTypeAttributeDescription
blob Blob

Blob

chunk Number

Size in bytes to slice blob

Return:

Promise<ArrayBuffer>

Binary data as a buffer

public static toBinaryString(blob: Blob, chunk: Number, worker: Boolean): Promise<String> version 1.0.0 since 1.0.0 source

Convert a blob to a binary string

The blob can optionnaly be sliced with the chunk arguments

Params:

NameTypeAttributeDescription
blob Blob

Blob to convert and optionnally sample

chunk Number

Size in bytes to slice blob

worker Boolean
  • optional
  • default: true

Wether to use webworkers if available

Return:

Promise<String>

Binary data as a string

Test:

public static async toBinaryStringWithWorker(blob: Blob, chunk: Number): Promise<String> version 1.0.0 since 1.0.0 source

Convert a blob to a binary string through a web worker thread

Params:

NameTypeAttributeDescription
blob Blob

Blob

chunk Number

Size in bytes to slice blob

Return:

Promise<String>

Raw binary data as a string

public static toBinaryStringWithoutWorker(blob: Blob, chunk: Number): Promise<String> version 1.0.0 since 1.0.0 source

Convert a blob to a binary string through main thread

The blob can optionnaly be sliced with the chunk arguments

Params:

NameTypeAttributeDescription
blob Blob

Blob to convert and optionnally sample

chunk Number

Size in bytes to slice blob

Return:

Promise<String>

Binary data as a string

public static typeEqual(b1: Blob, b2: Blob): Boolean version 1.0.0 since 1.0.0 source

Compare type of two blobs

Never rely solely on this method to discriminate blobs or, worse, consider them as equal

Params:

NameTypeAttributeDescription
b1 Blob

First blob

b2 Blob

Second blob

Return:

Boolean

true if types are equal

Test: