logo

AracnaBlob

The AracnaBlob class is built on top of the Blob class.

  • The data contained in the Blob can be resolved asynchronously and accessed at a later time from the instance itself.
  • The instance supports JSON serialization and deserialization out of the box unlike the Blob class.

Properties

  • blob (Blob): The Blob instance. It contains the actual Blob data.
  • id (string): The unique identifier of the instance. It is generated when the instance is created.

Getters

  • arrayBuffer (ArrayBuffer): The data contained in the Blob as an ArrayBuffer. You need to call the “resolveArrayBuffer” method before accessing this property. If the ArrayBuffer has not been resolved yet, it returns an empty ArrayBuffer.
  • size (number): The size of the Blob in bytes. MDN Reference
  • text (string): The data contained in the Blob as a string. You need to call the “resolveText” method before accessing this property. If the text has not been resolved yet, it returns an empty string.
  • type (string): The MIME type of the Blob. If the type is not defined, it returns ‘application/octet-stream’. MDN Reference
  • uInt8Array (Uint8Array): The data contained in the Blob as a Uint8Array. You need to call the “resolveArrayBuffer” or “resolveText” method before accessing this property. If the ArrayBuffer or text has not been resolved yet, it returns an empty Uint8Array.

Read as ArrayBuffer

The resolveArrayBuffer method resolves the data contained in the Blob as an ArrayBuffer.

import { AracnaBlob } from '@aracna/core';

(async () => {
  let binary, blob;

  binary = new Uint8Array([
    0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64
  ]);
  blob = new AracnaBlob(new Blob([binary]));

  await blob.resolveArrayBuffer();

  // will log ArrayBuffer
  console.log(blob.arrayBuffer);

  // will log Uint8Array
  console.log(blob.uInt8Array);
})();

Read as String

The resolveText method resolves the data contained in the Blob as a string.

import { AracnaBlob } from '@aracna/core';

(async () => {
  let blob;

  blob = new AracnaBlob(new Blob(['hello']));
  await blob.resolveText();

  // will log "hello"
  console.log(blob.text);

  // will log Uint8Array
  console.log(blob.uInt8Array);
})();

Read as Uint8Array

To read the data contained in the Blob as a Uint8Array, you need to call either the resolveArrayBuffer or resolveText method before accessing the uInt8Array property.

import { AracnaBlob } from '@aracna/core';

(async () => {
  let blob;

  blob = new AracnaBlob(new Blob(['hello']));
  await blob.resolveText();

  // will log Uint8Array
  console.log(blob.uInt8Array);
})();

Serialization & Deserialization

In the AracnaBlob class, JSON serialization and deserialization are used to convert the instance into a JSON object and create an instance from a JSON object.

The resolveArrayBuffer or resolveText method must be called before serializing the instance.

import { AracnaBlob } from '@aracna/core';

(async () => {
  let blob, serialized, deserialized;

  blob = new AracnaBlob(new Blob(['hello']));
  await blob.resolveText();

  // will log "hello"
  console.log(blob.text);

  // will log Uint8Array
  console.log(blob.uInt8Array);

  serialized = JSON.stringify(blob, null, 2);

  // will log JSON string
  console.log(serialized);

  deserialized = new AracnaBlob(JSON.parse(serialized));

  // will log AracnaBlob
  console.log(deserialized);

  // will log "hello"
  console.log(deserialized.text);

  // will log Uint8Array
  console.log(deserialized.uInt8Array);
})();