Bzz API
Installation
Core package
This package doesn't use third-party dependencies but does not support all the APIs provided by
@erebos/bzz-browser
and@erebos/bzz-node
npm install @erebos/bzz
For browser
npm install @erebos/bzz-browser
For Node
npm install @erebos/bzz-node
Usage
import { Bzz } from '@erebos/bzz' // core
// or
import { BzzBrowser as Bzz } from '@erebos/bzz-browser' // browser
// or
import { BzzNode as Bzz } from '@erebos/bzz-node' // node
const bzz = new Bzz({ url: 'http://localhost:8500' })
Interfaces and types
DirectoryEntry
interface DirectoryEntry {
data: string | Buffer
contentType?: string
size?: number
}
DirectoryData
type DirectoryData = Record<string, DirectoryEntry>
FileEntry
interface FileEntry {
data: string | Buffer
path: string
size?: number
}
ListEntry
interface ListEntry {
hash: string
path: string
contentType: string
size: number
mod_time: string
}
ListResult
interface ListResult {
common_prefixes?: Array<string>
entries?: Array<ListEntry>
}
FetchOptions
Common options to all HTTP requests. The timeout
value can be set to 0
to prevent applying any timeout, for example if a default timeout is set at the instance level, but a particular request needs to ignore it.
interface FetchOptions {
headers?: Record<string, any>
timeout?: number
}
FileOptions
Extends FetchOptions
interface FileOptions extends FetchOptions {
contentType?: string
path?: string
}
BzzMode
type BzzMode = 'default' | 'immutable' | 'raw'
DownloadOptions
Extends FileOptions
interface DownloadOptions extends FileOptions {
mode?: BzzMode
}
UploadOptions
Extends FileOptions
interface UploadOptions extends FileOptions {
defaultPath?: string
encrypt?: boolean
manifestHash?: string
pin?: boolean
size?: number
}
PollOptions
Extends FetchOptions
interface PollOptions extends FetchOptions {
interval: number // in milliseconds
immediate?: boolean // defaults to true
}
PinOptions
Extends FetchOptions
interface PinOptions extends FetchOptions {
download?: boolean
raw?: boolean
}
PinnedFile
interface PinnedFile {
address: string
counter: number
raw: boolean
size: number
}
Tag
interface Tag {
uid: number
name: string
address: string
total: number
split: number
seen: number
stored: number
sent: number
synced: number
startedAt: Date
}
BzzConfig
interface BzzConfig {
timeout?: number
url: string
}
HTTPError class
This class is used when errors from Swarm are thrown
Arguments
status: number
: HTTP status codemessage: string
: error message
Properties
status: number
: HTTP status codemessage: string
: error message
Bzz class
new Bzz()
Arguments
config: BzzConfig
, see below
Configuration
url: string
: address of the Swarm HTTP gatewaytimeout?: number
: default timeout to apply to all requests
.getDownloadURL()
Returns the Swarm download URL for a given resource based on the provided arguments.
Arguments
hashOrDomain: string
: ENS name or Swarm hashoptions?: DownloadOptions = {}
: optional object providing thepath
.raw?: boolean
Returns string
.getUploadURL()
Returns the Swarm upload URL for a given resource based on the provided arguments.
Arguments
options?: UploadOptions = {}
raw?: boolean
Returns string
.getPinURL()
Returns the Swarm URL for a pin based on the provided arguments.
Arguments
hash?: string
: hash of the resourceraw: boolean = false
Returns string
.hash()
Returns the hash of the provided domain
.
Arguments
domain: string
options?: FetchOptions = {}
Returns Promise<string>
.getTag()
Arguments
hash: string
options: FetchOptions
Returns Promise<Tag>
the Tag
of the given hash
.list()
Returns the manifest data for the provided hashOrDomain
and optional path
.
Arguments
hashOrDomain: string
: ENS name or Swarm hashoptions?: DownloadOptions = {}
: optional object providing thepath
.
Returns Promise<ListResult>
.download()
The download()
method returns a Response
instance if the request succeeds, or throws a HTTPError
.
Arguments
hashOrDomain: string
: ENS name or Swarm hashoptions?: DownloadOptions = {}
optional object providing thepath
,mode
andcontentType
.
Returns Promise<Response>
.downloadData()
Downloads and parses JSON data.
Arguments
hashOrDomain: string
: ENS name or Swarm hashoptions?: DownloadOptions = {}
Returns Promise<any>
.uploadFile()
Uploads a single file and returns the hash. If the contentType
option is provided, it will return the manifest hash, otherwise the file will be uploaded as raw data and will return the hash of the data itself.
Arguments
data: string | Buffer | Readable
options: UploadOptions = {}
Returns Promise<string>
.uploadData()
Stringifies and uploads JSON data.
Arguments
data: any
options: UploadOptions = {}
Returns Promise<string>
.deleteResource()
Deletes the resource with at the provided path
in the manifest and returns the hash of the new manifest without this resource.
Arguments
hashOrDomain: string
: ENS name or Swarm hashpath: string
options?: FetchOptions = {}
Returns Promise<string>
.pin()
Pins the specified resource. To make sure the resource is available on the node, the download
option can be set to explicitely download it before pinning.
Arguments
hashOrDomain: string
: ENS name or Swarm hashoptions?: PinOptions = {}
Returns Promise<void>
.unpin()
Arguments
hashOrDomain: string
: ENS name or Swarm hashoptions?: FetchOptions = {}
Returns Promise<void>
.pins()
Returns the list of resources currently pinned on the node.
Arguments
Returns Promise<Array<PinnedFile>>
the list of PinnedFile
BzzBrowser and BzzNode class
The following methods are only provided by
@erebos/bzz-browser
and@erebos/bzz-node
.downloadDirectory()
Arguments
hashOrDomain: string
: ENS name or Swarm hashoptions?: DownloadOptions = {}
Returns Promise<DirectoryData>
.uploadDirectory()
Uploads multiple files and returns the hash of the manifest containing these files.
By setting the defaultPath
option, a file can be defined as the default one when resolving a manifest root, for example a static website could be uploaded with the index.html
default path so that accessing bzz://domain.eth
would resolve to bzz://domain.eth/index.html
.
Arguments
Returns Promise<string>