Skip to content

Commit

Permalink
Merge pull request #396 from valist-io/feat/add-addAllNode
Browse files Browse the repository at this point in the history
Feat/add add all node
  • Loading branch information
jiyuu-jin authored Mar 19, 2024
2 parents 45f407a + 64805bf commit 1486747
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 28 deletions.
154 changes: 132 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/valist-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@valist/sdk",
"version": "2.9.4",
"version": "2.9.5",
"description": "Web3-native software distribution.",
"author": "Valist, Inc.",
"maintainers": [
Expand Down Expand Up @@ -49,7 +49,8 @@
"@capacitor-community/electron": "1.4.2",
"axios": "^1.4.0",
"ethers": "^5.5.4",
"node-fetch": "^2.6.1",
"proxy-from-env": "^1.1.0",
"ua-parser-js": "1.0.35"
}
}
}
11 changes: 11 additions & 0 deletions packages/valist-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ export default class Client {
return `${this.ipfsGateway}/ipfs/${cids[cids.length - 1]}`;
}

async writeFolderNode(files: ImportCandidate[], wrapWithDirectory = false, onProgress?: (percent: number) => void) {
if (files.length == 0) throw new Error("files.length == 0, must pin at least one file");
if (this.ipfs.addAllNode === undefined) throw new Error("not nodejs environment");
const cids = await this.ipfs.addAllNode(files, {
cidVersion: 1,
wrapWithDirectory,
});

return `${this.ipfsGateway}/ipfs/${cids[cids.length - 1].Hash}`;
}

async sendTx(unsigned: PopulatedTransaction): Promise<ethers.providers.TransactionResponse> {
if (!this.signer) throw new Error('valist client is read-only');

Expand Down
59 changes: 55 additions & 4 deletions packages/valist-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as contracts from './contracts';
import * as graphql from './graphql';
import axios, { AxiosProgressEvent, AxiosRequestConfig } from 'axios';
import https from "https";
import http from "http";
import { formatBytes } from './utils';

export type Provider = providers.Provider | ethers.Signer;
Expand All @@ -31,16 +32,17 @@ export type IPFSOptions = {
export type IPFSCLIENT = {
add: (value: any, options: IPFSOptions) => Promise<string | undefined>;
addAll: (values: any, options: any) => Promise<string[]>;
addAllNode?: (values: any[], addOptions: IPFSOptions) => Promise<{ Hash: string, Name: string, Size: number }[]>;
}

// Dynamically determine which FormData to use based on the environment
const isBrowser = typeof window !== 'undefined';
const FormData = isBrowser ? window.FormData : require('form-data');
export const isBrowser = typeof window !== 'undefined';

export const createIPFS = (_value: Record<string, unknown>): IPFSCLIENT => {
const API = 'https://pin-1.valist.io/api/v0';

const addAll = async (values: any[], options: IPFSOptions) => {
const FormData = isBrowser ? window.FormData : require('form-data');

const data: { Name: string, Hash: string }[] = [];
let path = `${API}/add?progress=true`;

Expand Down Expand Up @@ -111,10 +113,59 @@ export const createIPFS = (_value: Record<string, unknown>): IPFSCLIENT => {
return data[data.length - 1];
}

return {
const ipfsClient: IPFSCLIENT = {
addAll,
add,
};

if (!isBrowser) {
const addAllNode = async (values: any[], addOptions: IPFSOptions) => {
const FormData = require('form-data');
const formData = new FormData(); // @ts-expect-error sdcd
const fetch = (...args: any) => import('node-fetch').then(({ default: fetch }) => fetch(...args));

for (const { path, content } of values) {
formData.append('file', content, {
filepath: path,
contentType: 'application/octet-stream',
});
}

const url = new URL(`${API}/add`);
url.search = new URLSearchParams({
'chunker': 'rabin-131072-262144-524288',
'cid-version': `${addOptions.cidVersion || 0}`,
...addOptions.wrapWithDirectory && { 'wrap-with-directory': 'true' }
}).toString();

const agent = url.protocol === 'https:' ? new https.Agent({ keepAlive: false }) : new http.Agent({ keepAlive: false });

try {
const response = await fetch(url.toString(), {
method: 'POST',
body: formData,
agent,
});


if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const text = await response.text();
const lines = text.split('\n').filter((line: string) => line.trim());
const data = lines.map((line: string) => JSON.parse(line)).filter((item: null) => item !== null);
return data;
} catch (error) {
console.error('Upload failed:', error);
return [];
}
};

ipfsClient.addAllNode = addAllNode;
}

return ipfsClient;
};

/**
Expand Down

0 comments on commit 1486747

Please sign in to comment.