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 |
compareBuffers(buf1: ArrayBuffer, buf2: ArrayBuffer, worker: Boolean): Promise<Boolean> | Boolean Compares two buffers byte to byte |
version 1.0.0 since 1.0.0 |
public static |
async compareBuffersWithWorker(buf1: ArrayBuffer, buf2: ArrayBuffer): Promise<Boolean> Compares two buffers byte through web worker |
version 1.0.0 since 1.0.0 |
public static |
compareBuffersWithoutWorker(buf1: ArrayBuffer, buf2: ArrayBuffer): Boolean Compares two buffers byte to byte through main thread |
version 1.0.0 since 1.0.0 |
public static |
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 |
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 |
toArrayBufferWithoutWorker(blob: Blob, chunk: Number): Promise<ArrayBuffer> 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 |
toBinaryStringWithoutWorker(blob: Blob, chunk: Number): Promise<String> Convert a blob to a binary string through main thread |
version 1.0.0 since 1.0.0 |
public static |
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
Throw:
* |
Error When comparison method is not recognized |
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
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:
Name | Type | Attribute | Description |
buf1 | ArrayBuffer | First buffer |
|
buf2 | ArrayBuffer | Second buffer |
|
worker | Boolean |
|
Whether to use worker or not |
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:
Name | Type | Attribute | Description |
buf1 | ArrayBuffer | First buffer |
|
buf2 | ArrayBuffer | Second buffer |
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:
Name | Type | Attribute | Description |
buf1 | ArrayBuffer | First buffer |
|
buf2 | ArrayBuffer | Second buffer |
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 :
byte
,bytes
,content
: Performs a byte comparison between the two blogs. The optionalsizes
option parameter can be used to provide sizes to perform comparison on sliced blobs. See blobCompare.bytesEqualWithArrayBuffer and blobCompare.bytesEqualWithBinaryString for more informations;magic
,headers
,numbers
,mime
: Compare two blobs based on magic numbers. See blobCompare.magicNumbersEqual for more informations;size
,sizes
: Compare two blobs based on their size in bytes. See blobCompare.sizeEqual for more informations;type
,types
: Compare two blobs based on their type. See blobCompare.typeEqual for more informations.
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:
Name | Type | Attribute | Description |
b1 | Blob | First blob |
|
b2 | Blob | Second blob |
|
options | Object |
|
Configuration to use when performing comparison |
options.methods | Array |
|
Default methods used for comparison. Methods are applied in the same order |
options.byte | String |
|
If set to |
options.partial | Boolean |
|
When set to |
options.chunks | Array |
|
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 |
|
Wether to use web workers if available |
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:
Name | Type | Attribute | Description |
b1 | Blob | First blob |
|
b2 | Blob | Second blob |
|
worker | Boolean |
|
Wether to use webworkers if available |
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:
Name | Type | Attribute | Description |
b1 | Blob | First blob |
|
b2 | Blob | Second Blob |
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 chunk
argument
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 chunk
argument
Params:
Name | Type | Attribute | Description |
blob | Blob | Blob |
|
chunk | Number | Size in bytes to slice blob |
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 chunk
argument
Params:
Name | Type | Attribute | Description |
blob | Blob | Blob |
|
chunk | Number | Size in bytes to slice blob |
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
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:
Name | Type | Attribute | Description |
blob | Blob | Blob |
|
chunk | Number | Size in bytes to slice blob |
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:
Name | Type | Attribute | Description |
blob | Blob | Blob to convert and optionnally sample |
|
chunk | Number | Size in bytes to slice blob |