npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chia-dl-file-store

v1.0.7

Published

A package for managing file storage in Chia DataLayer.

Downloads

5

Readme

ChiaDLFileStore

ChiaDLFileStore is a Node.js package that provides functionality for interacting with Chia blockchain's DataLayer to manage files.

Installation

npm install chia-dl-file-store

Usage

const ChiaDLFileStore = require('chia-dl-file-store');

// Create an instance of ChiaDLFileStore
const fileStore = new ChiaDLFileStore();

Configuration

The ChiaDLFileStore constructor accepts an optional configuration object. Here are the available configuration options along with their default values:

  • getRpcMaxConnections (default: 5): Maximum number of concurrent RPC connections to get a splitted file.
  • timeoutPending (default: 5000): Timeout value for checking pending request.
  • certPath (default: auto-generated based on the user's home directory and Chia configuration): Path to the SSL certificate file.
  • keyPath (default: auto-generated based on the user's home directory and Chia configuration): Path to the SSL key file.
  • host (default: 'localhost'): Hostname for the RPC server.
  • port (default: 8562): Port number for the RPC server.
  • chunkSize (default: 2000000): Size of each data chunk in bytes (2MB).
const config = {
    getRpcMaxConnections: 10,
    timeoutPending: 10000,
    certPath: '/path/to/cert/file.crt',
    keyPath: '/path/to/key/file.key',
    host: 'localhost',
    port: 8562,
    chunkSize: 2000000, // 2MB
};

const fileStore = new ChiaDLFileStore(config);

API Documentation

getFileList(idStore)

Description: Retrieve the list of files in the data store.

  • idStore: Identifier of the data store.

Returns:

  • success: Whether the operation was successful.
  • fileList: List of files in the data store.

getFile(idStore, fileName)

Description: Retrieve a file from the data store.

  • idStore: Identifier of the data store.
  • fileName: Name of the file to retrieve.

Returns:

  • success: Whether the operation was successful.
  • message: Status message.
  • hexFile: Hex-encoded content of the file.

insertFile(idStore, filePath, fee)

Description: Insert a file into the data store.

  • idStore: Identifier of the data store.
  • filePath: Path to the file to be inserted.
  • fee: Transaction fee (optional).

Returns:

  • success: Whether the operation was successful.
  • message: Status message.

createDataStore(fee)

Description: Create a new data store.

  • fee: Transaction fee (optional).

Returns:

  • success: Whether the operation was successful.
  • message: Status message

cancelProcess()

Description: Cancel the ongoing process insertion / getfile.

deleteFile(idStore, fileName, fee)

Description: Delete a file from the data store.

  • idStore: Identifier of the data store.
  • fileName: Name of the file to delete.
  • fee: Transaction fee (optional).

Returns:

  • success: Whether the operation was successful.
  • message: Status message.

Events

ChiaDLFileStore is an EventEmitter and emits the following events:

  • logGetFile: Logged when a file is being retrieved.
  • logInsertFile: Logged when a file is being inserted.
fileStore.on('logGetFile', (fileName, partNumber, hexData, message) => {
    console.log(`Getting ${partNumber}/${totalParts} of ${fileName}: ${message}`);
});

fileStore.on('logInsertFile', (fileName, partNumber, message) => {
    console.log(`Inserting ${partNumber} of ${fileName}: ${message}`);
});

Examples

Get File List

const ChiaDLFileStore = require('chia-dl-file-store');

async function getFileListExample() {
    const fileStore = new ChiaDLFileStore();
    
    try {
        const idStore = 'STORE_ID';
        const result = await fileStore.getFileList(idStore);
        if(!result.success) {
            return;
        }
        for(const file of result.fileList) {
            console.log("File name: ", file);
        }
    } catch (error) {
        console.error('Error:', error.message);
    }
}

getFileListExample();

Insert File

const ChiaDLFileStore = require('chia-dl-file-store');

async function insertFileExample() {
    const fileStore = new ChiaDLFileStore();


    fileStore.on('logInsertFile', (fileName, partNumber, message) => {
        console.log(`log ${partNumber} of ${fileName}: ${message}`);
    });

    try {
        const idStore = 'STORE_ID';
        const filePath = "PathTofile";
        const fee = 100;
        const result = await fileStore.insertFile(idStore, filePath, fee);
       if(result.success) {
           console.log('Success:', result.message);
           return;
       }
       console.error('Error:', result.error);
    } catch (error) {
        console.error('Error:', error.message);
    }
}

insertFileExample();

Get File

const ChiaDLFileStore = require('chia-dl-file-store');
const path = require('path');
const fs = require('fs');

async function getFileExample() {
    const fileStore = new ChiaDLFileStore();
    fileStore.on('logGetFile', (fileName, partNumber, hexData, message) => {
        console.log(`log Part ${partNumber} of ${fileName}: ${message}`);

    });
    try {
        const idStore = 'STORE_ID';
        const fileName = 'YOUR_FILE_NAME_HERE';
        const result = await fileStore.getFile(idStore, fileName);

        if (result.success === true) {
            const filePath = path.join(__dirname, fileName);
            fs.writeFileSync(filePath, result.hexFile, 'hex');
            console.log('File retrieved and saved:', filePath);
        } else {
            console.error('Error:', result.error);
        }
    } catch (error) {
        console.error('Error:', error.message);
    }
}

getFileExample();

Delete File

const ChiaDLFileStore = require('chia-dl-file-store');

async function deleteFileExample() {
    const fileStore = new ChiaDLFileStore();

    try {
        const idStore = 'STORE_ID';
        const fileName = 'FILENAME';
        const result = await fileStore.deleteFile(idStore, fileName,100);
        
        if (result.success === true) {
            console.log('File deleted successfully');
        } else {
            console.error('Error deleting file:', result.error);
        }
    } catch (error) {
        console.error('Error:', error.message);
    }
}
deleteFileExample();

Cancel Process insertFile / getFile

const ChiaDLFileStore = require('chia-dl-file-store');

async function cancelProcessExample() {
    const fileStore = new ChiaDLFileStore();

    try {
        await fileStore.cancelProcess();
        console.log('process cancelled');
    } catch (error) {
        console.error('Error:', error.message);
    }
}
cancelProcessExample();

Create Store

const ChiaDLFileStore = require('chia-dl-file-store');

async function createDataStoreExample() {
    const fileStore = new ChiaDLFileStore();

    try {
        const result = await fileStore.createDataStore(100);
        if(result.success) {
            console.log('Data store created with ID:', result.id);
        }
        else {  
            console.error('Error:', result.error);
        }
    } catch (error) {
        console.error('Error:', error.message);
    }
}
createDataStoreExample();

Usage Notes and Limitations

Inserting Large Files

When inserting large files into the data store, please be patient as each part of the file is being inserted. If the upload process is interrupted, you can resume the insertion process, and it will continue from where it left off.

It's important to note that when inserting a large file that will be split into multiple parts, the insertion should occur continuously in the hash history. If the insertion process is interrupted and another file insertion is initiated, the interrupted file's data might become unreadable. In such cases, it's recommended to delete the interrupted file and reinsert it from the beginning.

This limitation is due to the reliance on the hash history to ensure the integrity and consistency of data stored in the data store. Interrupting the insertion process and mixing it with another insertion can result in an inconsistent state.

To avoid these issues, it's advisable to ensure the continuous and uninterrupted insertion of each file part into the hash history until the insertion is complete.

The package has been successfully tested with chunk sizes of 2MB and 10MB. However, when attempting to insert files larger than 10MB, such as 15MB, 20MB, or 25MB, you might encounter an error message indicating that the request size limit has been exceeded.

Example error message: "Maximum request body size 26214400 exceeded, actual body size 26279790"

Support and Donations

If you find this package helpful or valuable, consider showing your appreciation by making a donation to the following Chia wallet address:

Wallet Address: xch1flkl7v6uhxepk5se3542478e38m0t20wchjp28gwxeglzxvmzdnq2hh33d

Your support helps to motivate ongoing development and maintenance of this package. Even a small contribution can make a difference and encourage further improvements.

Thank you for your support!