The Web File implementation for Web3
npm install web3-file




> The Web File implementation for Web3.
* Motivation
* Install
* API
+ class Web3File
+ Web3File#name
+ Web3File#path
+ Web3File#lastModified
+ Web3File#content
+ Web3File#iterator()
+ Web3File#blob()
+ Web3File.fromBytes
+ Web3File.fromText
+ Web3File.fromReadableStream
+ Web3File.fromBlob
+ Web3File.fromFile
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.
``sh`install it as a dependency
$ npm i web3-file
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.
| Name | Type | Description |
|------|------|-------------|
| content | AsyncIterable | File content to be read |string
| filename | | filename |object
| [options] | | Web3File options |string
| [options.path] | | File Path |number
| [options.lastModified] | | Last modified timestamp |
`js
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()
}
)
`
- Returns string
Get file name.
- Returns string
Get file path.
- Returns number
Get file last modified timestamp.
- Returns AsyncIterable
Get file content readable source.
- Returns AsyncIterable
Get file content readable source iterator.
- Returns Promise
Get file content as a Blob.
- Returns Promise
Get file content as Text.
Takes Uint8Array bytes to create a Web3File.
`js
import Web3File from 'web3-file'
const file = Web3File.fromBytes(new Uint8Array([2, 44, 1]), 'file.zip')
`
Takes a string content to create a Web3File.
`js
import { Web3File } from 'web3-file'
const file = Web3File.fromText('web3file', 'file.txt')
`
Takes a Readable Stream content to create a Web3File.
`js
import { Web3File } from 'web3-file'
const response = await fetch('https://example.org/image.png')
const file = Web3File.fromReadableStream(response.body, 'image.png')
`
Takes a Blob content to create a Web3File.
`js
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')
`
Takes a File content to create a Web3File.
`js
import { Web3File } from 'web3-file'
const image = new File([bytes], 'image.png')
const file = Web3File.fromFile(webFile)
``