web3-file v0.2.0
web3-file
The Web File implementation for Web3.
Table of Contents
Motivation
The Web File implementation and standard are still on their beginning.
The current implementation has a non standard File.webkitRelativePath read-only
property that we cannot use outside the webkitdirectory
context. As a result, we cannot set a File path programatically in the Browser, nor in Node.js @web-std/file.
Web3File
bridges this gap by adding a path property and other extras, while being a close implementation of Web File. This allows
files to be transformed into their UnixFs representation and back into the original structure, and leverage the IPFS Content Routing primitives.
Install
# install it as a dependency
$ npm i web3-file
API
To create your Web3File
with an async iterable content, you can simply create an instance of Web3File. Otherwise, you can leverage one of the static functions provided to create a Web3File
from another data type.
Please note that the same options of the constructor can be provided in the static functions.
class Web3File
Name | Type | Description |
---|---|---|
content | AsyncIterable<Uint8Array> | File content to be read |
filename | string | filename |
options | object | Web3File options |
options.path | string | File Path |
options.lastModified | number | Last modified timestamp |
import { Web3File } from 'web3-file'
const file = new Web3File(
fs.createReadStream('path/to/file.zip'),
'file.zip',
{
path: 'path/to/file.zip',
lastModified: Date.now()
}
)
Web3File#name
- Returns
string
Get file name.
Web3File#path
- Returns
string
Get file path.
Web3File#lastModified
- Returns
number
Get file last modified timestamp.
Web3File#content
- Returns
AsyncIterable<Uint8Array>
Get file content readable source.
Web3File#iterator()
- Returns
AsyncIterable<Uint8Array>
Get file content readable source iterator.
Web3File#blob()
- Returns
Promise<Blob>
Get file content as a Blob.
Web3File#text()
- Returns
Promise<String>
Get file content as Text.
Web3File.fromBytes
Takes Uint8Array bytes to create a Web3File.
import Web3File from 'web3-file'
const file = Web3File.fromBytes(new Uint8Array([2, 44, 1]), 'file.zip')
Web3File.fromText
Takes a string content to create a Web3File.
import { Web3File } from 'web3-file'
const file = Web3File.fromText('web3file', 'file.txt')
Web3File.fromReadableStream
Takes a Readable Stream content to create a Web3File.
import { Web3File } from 'web3-file'
const response = await fetch('https://example.org/image.png')
const file = Web3File.fromReadableStream(response.body, 'image.png')
Web3File.fromBlob
Takes a Blob content to create a Web3File.
import { Web3File } from 'web3-file'
const response = await fetch('https://example.org/image.png')
const blob = await response.blob()
const file = Web3File.fromBlob(blob, 'image.png')
Web3File.fromFile
Takes a File content to create a Web3File.
import { Web3File } from 'web3-file'
const image = new File([bytes], 'image.png')
const file = Web3File.fromFile(webFile)