2.2.1 • Published 5 years ago

opacity-streamable v2.2.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

view on npm npm module downloads Build Status

opacity-streamable

A proof of concept, streamable implementation of the Opacity Protocol (not fully compatible) with a few significant changes:

  • CryptoJS replaced with node-forge
  • Encryption/decryption key is the SHA-256 hash of the handle, per-chunk IV (see #109)
  • Communication with the nodes via IXI module to save bandwidth
  • Pipelined processing:
    • File > Encryption > Upload
    • Download > Decryption > Blob

API Reference

opacity-streamable

Importing opacity-streamable

Example (As an ES Module)

import Opacity from 'opacity-streamable'

Example (As a Node Module)

const Opacity = require(`opacity-streamable`)

Example (As a UMD Module)

window.Opacity = Opacity.default

Opacity.Download

Kind: static class of opacity-streamable
Emits: METADATA, DOWNLOAD_PROGRESS, FINISH

Download.EVENTS

Events fired during the download lifecycle

Kind: static constant of Download

"DOWNLOAD_PROGRESS"

Fired when a successful poll is performed while retrieving a file

Kind: event emitted by EVENTS
Properties

NameTypeDescription
progressObjecta progress object
progress.progressNumberthe percentage of progress for the download

"FINISH"

Fired when the file has been reconstructed and is ready for use

Kind: event emitted by EVENTS
Properties

NameTypeDescription
fileFile | Bufferthe file as an object as the target type of the download instance
metadataObjectthe metadata object associated with the file

"METADATA"

Fired when the file metadata has been reconstructed and is ready for use

Kind: event emitted by EVENTS
Properties

NameTypeDescription
fileNameStringthe name of the file being downloaded
extStringthe file extension of the file being downloaded
numberOfChunksNumberthe number of chunks that the file is stored in

Download.toBuffer(handle, options) ⇒ Download

Kind: static method of Download

ParamTypeDefaultDescription
handleStringthe handle of the file to download
optionsObjectthe options for the download
options.iotaProvidersArray.<Object> | Array.<IOTA>an array of IOTA initialization Objects or IOTA instances
options.autoStartBooleantrueimmediately start the download

Example (To Buffer object (node))

const download = Opacity.Download.toBuffer(handle, {
  iotaProviders: [
    { provider: 'https://poll.oysternodes.com:14265/' },
    { provider: 'https://download.oysternodes.com:14265/' }
  ]
})

download.on('meta', metadata => {
  console.log(metadata)
  // {fileName: "opacity.txt", ext: "txt", numberOfChunks: 2}
})
download.on('finish', filedata => {
  console.log(filedata)
  // {file: Buffer(), metadata: {…}, target: Download}
})

Download.toBlob(handle, options) ⇒ Download

Kind: static method of Download

ParamTypeDefaultDescription
handleStringthe handle of the file to download
optionsObjectthe options for the download
options.iotaProvidersArray.<Object> | Array.<IOTA>an array of IOTA initialization Objects or IOTA instances
options.autoStartBooleantrueimmediately start the download

Example (To Blob object (browser))

const download = Opacity.Download.toBlob(handle, {
  iotaProviders: [
    { provider: 'https://poll.oysternodes.com:14265/' },
    { provider: 'https://download.oysternodes.com:14265/' }
  ]
})

download.on('meta', metadata => {
  console.log(metadata)
  // {fileName: "opacity.txt", ext: "txt", numberOfChunks: 2}
})
download.on('finish', filedata => {
  console.log(filedata)
  // {file: Blob(), metadata: {…}, target: Download}
})

Opacity.Upload

Kind: static class of opacity-streamable
Emits: INVOICE, CHUNKS_PROGRESS, UPLOAD_PROGRESS, FINISH

Upload.EVENTS

Events fired during the upload lifecycle

Kind: static constant of Upload

"INVOICE"

Fired when an invoice is recieved from the broker node

Kind: event emitted by EVENTS
Properties

NameTypeDescription
handleStringthe handle of the file uploaded
addressStringan ethereum address to send the pearl to
costNumberthe cost of the file upload

"CHUNKS_PROGRESS"

Fired when a chunk is uploaded to the broker

Kind: event emitted by EVENTS
Properties

NameTypeDescription
progressObjecta progress object
progress.progressNumberthe percentage of progress for the chunk upload

"UPLOADED"

Fired all chunks have been uploaded to the brokers.

Kind: event emitted by EVENTS

"META_ATTACHED"

Fired when the meta chunk has been attached. This is needed in order to resume polling for upload progress.

Kind: event emitted by EVENTS
Properties

NameTypeDescription
targetObjectthe upload object
handleStringthe handle of the uploaded file
numberOfChunksNumberthe number of chunks for the file
metadataObjectthe metadata object

"UPLOAD_PROGRESS"

Fired when a chunk is attached to the tangle

Kind: event emitted by EVENTS
Properties

NameTypeDescription
progressObjecta progress object
progress.progressNumberthe percentage of progress for the chunk attachment

"FINISH"

Fired when the file has been completely attached to the tangle

Kind: event emitted by EVENTS
Properties

NameTypeDescription
handleStringthe handle of the file uploaded
metadataObjectthe metadata object associated with the file

Upload.EVENTS

Events fired during the upload lifecycle

Kind: static constant of Upload

"INVOICE"

Fired when an invoice is recieved from the broker node

Kind: event emitted by EVENTS
Properties

NameTypeDescription
handleStringthe handle of the file uploaded
addressStringan ethereum address to send the pearl to
costNumberthe cost of the file upload

"CHUNKS_PROGRESS"

Fired when a chunk is uploaded to the broker

Kind: event emitted by EVENTS
Properties

NameTypeDescription
progressObjecta progress object
progress.progressNumberthe percentage of progress for the chunk upload

"UPLOADED"

Fired all chunks have been uploaded to the brokers.

Kind: event emitted by EVENTS

"META_ATTACHED"

Fired when the meta chunk has been attached. This is needed in order to resume polling for upload progress.

Kind: event emitted by EVENTS
Properties

NameTypeDescription
targetObjectthe upload object
handleStringthe handle of the uploaded file
numberOfChunksNumberthe number of chunks for the file
metadataObjectthe metadata object

"UPLOAD_PROGRESS"

Fired when a chunk is attached to the tangle

Kind: event emitted by EVENTS
Properties

NameTypeDescription
progressObjecta progress object
progress.progressNumberthe percentage of progress for the chunk attachment

"FINISH"

Fired when the file has been completely attached to the tangle

Kind: event emitted by EVENTS
Properties

NameTypeDescription
handleStringthe handle of the file uploaded
metadataObjectthe metadata object associated with the file

Upload.fromFile(file, options) ⇒ Upload

Kind: static method of Upload

ParamTypeDefaultDescription
fileFilethe file to upload
optionsObjectthe options for the upload
options.iotaProviderObject | IOTAan IOTA initialization Object or IOTA instance
options.alphaStringthe endpoint for the alpha broker
options.betaStringthe endpoint for the beta broker
options.epochsNumberthe number of years to store the file
options.autoStartBooleantrueimmediately start the upload

Example (From File object (browser))

const file = fileInput.files[0];
const upload = Opacity.Upload.fromFile(file, {
  iotaProvider: { provider: 'https://poll.oysternodes.com:14265/' },
  alpha: 'https://broker-1.oysternodes.com/',
  beta: 'https://broker-2.oysternodes.com/',
  epochs: 1
});

upload.on('invoice', invoice => {
  console.log(invoice)
  // {address: "<ETH_ADDRESS>", cost: 20}
});
upload.on('finish', filedata => {
  console.log(filedata)
  // {handle: "<OPACITY_HANDLE>", metadata: {…}, target: Upload}
});

Upload.fromData(buffer, filename, options) ⇒ Upload

Kind: static method of Upload

ParamTypeDefaultDescription
bufferBufferthe data Buffer to upload
filenameStringthe name of the file
optionsObjectthe options for the upload
options.iotaProviderObject | IOTAan IOTA initialization Object or IOTA instance
options.alphaStringthe endpoint for the alpha broker
options.betaStringthe endpoint for the beta broker
options.epochsNumberthe number of years to store the file
options.autoStartBooleantrueimmediately start the upload

Example (From Buffer object (node))

const fs = require('fs');
const path = './path/to/file';
const filename = 'somefile.txt';

fs.readFile(`${path}/${filename}`, (err, data) => {
  if (err) throw err;

  const upload = Opacity.Upload.fromData(data, filename, {
    iotaProvider: { provider: 'https://poll.oysternodes.com:14265/' },
    alpha: 'https://broker-1.oysternodes.com/',
    beta: 'https://broker-2.oysternodes.com/',
    epochs: 1
  });

  upload.on('invoice', invoice => {
    console.log(invoice)
    // {address: "<ETH_ADDRESS>", cost: 20}
  });
  upload.on('finish', filedata => {
    console.log(filedata)
    // {handle: "<OPACITY_HANDLE>", metadata: {…}, target: Upload}
  });
});

What's next?

Possible things to look into:

  • Full compatibility
  • Pluggable source streams for uploads and target streams for downloads, allowing:
  • Node compatability
  • Video streams via MediaSource
  • Arbitrary size downloads via StreamSaver

Credits

This repo is based on the official opacity/webinterface and shares a fair amount of code with the project where it makes sense.

Big thanks to everyone working on Opacity!